Re: [PATCH 9/9] add has: query prefix to search for specific properties
[notmuch-archives.git] / 98 / f8c40d77fcbb8f7e20db993eb306e0886e08aa
1 Return-Path: <jani@nikula.org>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id 5B3AB429E34\r
6         for <notmuch@notmuchmail.org>; Thu,  8 Dec 2011 14:48:49 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\r
9 X-Spam-Flag: NO\r
10 X-Spam-Score: -0.7\r
11 X-Spam-Level: \r
12 X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
13         tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
14 Received: from olra.theworths.org ([127.0.0.1])\r
15         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
16         with ESMTP id syWTiRlJOAI6 for <notmuch@notmuchmail.org>;\r
17         Thu,  8 Dec 2011 14:48:46 -0800 (PST)\r
18 Received: from mail-ey0-f181.google.com (mail-ey0-f181.google.com\r
19         [209.85.215.181]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
20         (No client certificate requested)\r
21         by olra.theworths.org (Postfix) with ESMTPS id 24E26429E31\r
22         for <notmuch@notmuchmail.org>; Thu,  8 Dec 2011 14:48:43 -0800 (PST)\r
23 Received: by mail-ey0-f181.google.com with SMTP id l13so1711208eaa.26\r
24         for <notmuch@notmuchmail.org>; Thu, 08 Dec 2011 14:48:43 -0800 (PST)\r
25 Received: by 10.213.14.75 with SMTP id f11mr1267153eba.28.1323384523787;\r
26         Thu, 08 Dec 2011 14:48:43 -0800 (PST)\r
27 Received: from localhost (dsl-hkibrasgw4-fe5cdc00-23.dhcp.inet.fi.\r
28         [80.220.92.23])\r
29         by mx.google.com with ESMTPS id 53sm23202951eef.2.2011.12.08.14.48.41\r
30         (version=SSLv3 cipher=OTHER); Thu, 08 Dec 2011 14:48:42 -0800 (PST)\r
31 From: Jani Nikula <jani@nikula.org>\r
32 To: notmuch@notmuchmail.org\r
33 Subject: [PATCH v4 2/3] cli: add support for pre and post notmuch new hooks\r
34 Date: Fri,  9 Dec 2011 00:48:30 +0200\r
35 Message-Id:\r
36  <dcad231e374d410c95ccdbdf072dfe4da96f1d4f.1323384304.git.jani@nikula.org>\r
37 X-Mailer: git-send-email 1.7.5.4\r
38 In-Reply-To: <cover.1323384304.git.jani@nikula.org>\r
39 References:\r
40  <7fbe6befcf31881a9bca672f55b93501249a220c.1322859389.git.jani@nikula.org>\r
41         <cover.1323384304.git.jani@nikula.org>\r
42 In-Reply-To: <cover.1323384304.git.jani@nikula.org>\r
43 References: <cover.1323384304.git.jani@nikula.org>\r
44 X-BeenThere: notmuch@notmuchmail.org\r
45 X-Mailman-Version: 2.1.13\r
46 Precedence: list\r
47 List-Id: "Use and development of the notmuch mail system."\r
48         <notmuch.notmuchmail.org>\r
49 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
50         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
51 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
52 List-Post: <mailto:notmuch@notmuchmail.org>\r
53 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
54 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
55         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
56 X-List-Received-Date: Thu, 08 Dec 2011 22:48:49 -0000\r
57 \r
58 Run notmuch new pre and post hooks, named "pre-new" and "post-new", if\r
59 present in the notmuch hooks directory. The hooks will be run before and\r
60 after incorporating new messages to the database.\r
61 \r
62 Typical use cases for pre-new and post-new hooks are fetching or delivering\r
63 new mail to the maildir, and custom tagging of the mail incorporated to the\r
64 database.\r
65 \r
66 Also add command line option --no-hooks to notmuch new to bypass the hooks.\r
67 \r
68 Signed-off-by: Jani Nikula <jani@nikula.org>\r
69 ---\r
70  NEWS          |   10 ++++++++++\r
71  notmuch-new.c |   12 ++++++++++++\r
72  notmuch.1     |   50 +++++++++++++++++++++++++++++++++++++++++++++++++-\r
73  notmuch.c     |   39 ++++++++++++++++++++++++++++++++++++++-\r
74  4 files changed, 109 insertions(+), 2 deletions(-)\r
75 \r
76 diff --git a/NEWS b/NEWS\r
77 index bb5e4d5..eaed960 100644\r
78 --- a/NEWS\r
79 +++ b/NEWS\r
80 @@ -1,6 +1,16 @@\r
81  Notmuch 0.11 (201x-xx-xx)\r
82  =========================\r
83  \r
84 +New command-line features\r
85 +-------------------------\r
86 +\r
87 +Hooks\r
88 +\r
89 +  Hooks have been introduced to notmuch. Hooks are scripts that notmuch\r
90 +  invokes before and after certain actions. Initially, "notmuch new"\r
91 +  supports "pre-new" and "post-new" hooks that are run before and after\r
92 +  importing new messages into the database.\r
93 +\r
94  Performance\r
95  -----------\r
96  \r
97 diff --git a/notmuch-new.c b/notmuch-new.c\r
98 index 81a9350..bfb4600 100644\r
99 --- a/notmuch-new.c\r
100 +++ b/notmuch-new.c\r
101 @@ -811,6 +811,7 @@ notmuch_new_command (void *ctx, int argc, char *argv[])\r
102      _filename_node_t *f;\r
103      int i;\r
104      notmuch_bool_t timer_is_active = FALSE;\r
105 +    notmuch_bool_t run_hooks = TRUE;\r
106  \r
107      add_files_state.verbose = 0;\r
108      add_files_state.output_is_a_tty = isatty (fileno (stdout));\r
109 @@ -820,6 +821,8 @@ notmuch_new_command (void *ctx, int argc, char *argv[])\r
110      for (i = 0; i < argc && argv[i][0] == '-'; i++) {\r
111         if (STRNCMP_LITERAL (argv[i], "--verbose") == 0) {\r
112             add_files_state.verbose = 1;\r
113 +       } else if (strcmp (argv[i], "--no-hooks") == 0) {\r
114 +           run_hooks = FALSE;\r
115         } else {\r
116             fprintf (stderr, "Unrecognized option: %s\n", argv[i]);\r
117             return 1;\r
118 @@ -833,6 +836,12 @@ notmuch_new_command (void *ctx, int argc, char *argv[])\r
119      add_files_state.synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);\r
120      db_path = notmuch_config_get_database_path (config);\r
121  \r
122 +    if (run_hooks) {\r
123 +       ret = notmuch_run_hook (db_path, "pre-new");\r
124 +       if (ret)\r
125 +           return ret;\r
126 +    }\r
127 +\r
128      dot_notmuch_path = talloc_asprintf (ctx, "%s/%s", db_path, ".notmuch");\r
129  \r
130      if (stat (dot_notmuch_path, &st)) {\r
131 @@ -981,5 +990,8 @@ notmuch_new_command (void *ctx, int argc, char *argv[])\r
132  \r
133      notmuch_database_close (notmuch);\r
134  \r
135 +    if (run_hooks && !ret && !interrupted)\r
136 +       ret = notmuch_run_hook (db_path, "post-new");\r
137 +\r
138      return ret || interrupted;\r
139  }\r
140 diff --git a/notmuch.1 b/notmuch.1\r
141 index 147319e..3dbd67e 100644\r
142 --- a/notmuch.1\r
143 +++ b/notmuch.1\r
144 @@ -85,7 +85,7 @@ The\r
145  command is used to incorporate new mail into the notmuch database.\r
146  .RS 4\r
147  .TP 4\r
148 -.B new\r
149 +.BR new " [options...]"\r
150  \r
151  Find and import any new messages to the database.\r
152  \r
153 @@ -118,6 +118,22 @@ if\r
154  has previously been completed, but\r
155  .B "notmuch new"\r
156  has not previously been run.\r
157 +\r
158 +The\r
159 +.B new\r
160 +command supports hooks. See the\r
161 +.B "HOOKS"\r
162 +section below for more details on hooks.\r
163 +\r
164 +Supported options for\r
165 +.B new\r
166 +include\r
167 +.RS 4\r
168 +.TP 4\r
169 +.BR \-\-no\-hooks\r
170 +\r
171 +Prevents hooks from being run.\r
172 +.RE\r
173  .RE\r
174  \r
175  Several of the notmuch commands accept search terms with a common\r
176 @@ -705,6 +721,38 @@ specify a date range to return messages from 2009\-10\-01 until the\r
177  current time:\r
178  \r
179         $(date +%s \-d 2009\-10\-01)..$(date +%s)\r
180 +.SH HOOKS\r
181 +Hooks are scripts (or arbitrary executables or symlinks to such) that notmuch\r
182 +invokes before and after certain actions. These scripts reside in\r
183 +the .notmuch/hooks directory within the database directory and must have\r
184 +executable permissions.\r
185 +\r
186 +The currently available hooks are described below.\r
187 +.RS 4\r
188 +.TP 4\r
189 +.B pre\-new\r
190 +This hook is invoked by the\r
191 +.B new\r
192 +command before scanning or importing new messages into the database. If this\r
193 +hook exits with a non-zero status, notmuch will abort further processing of the\r
194 +.B new\r
195 +command.\r
196 +\r
197 +Typically this hook is used for fetching or delivering new mail to be imported\r
198 +into the database.\r
199 +.RE\r
200 +.RS 4\r
201 +.TP 4\r
202 +.B post\-new\r
203 +This hook is invoked by the\r
204 +.B new\r
205 +command after new messages have been imported into the database and initial tags\r
206 +have been applied. The hook will not be run if there have been any errors during\r
207 +the scan or import.\r
208 +\r
209 +Typically this hook is used to perform additional query\-based tagging on the\r
210 +imported messages.\r
211 +.RE\r
212  .SH ENVIRONMENT\r
213  The following environment variables can be used to control the\r
214  behavior of notmuch.\r
215 diff --git a/notmuch.c b/notmuch.c\r
216 index d44ce9a..c0ce026 100644\r
217 --- a/notmuch.c\r
218 +++ b/notmuch.c\r
219 @@ -127,6 +127,32 @@ static const char search_terms_help[] =\r
220      "\n"\r
221      "\t\t$(date +%%s -d 2009-10-01)..$(date +%%s)\n\n";\r
222  \r
223 +static const char hooks_help[] =\r
224 +    "\tHooks are scripts (or arbitrary executables or symlinks to such) that\n"\r
225 +    "\tnotmuch invokes before and after certain actions. These scripts reside\n"\r
226 +    "\tin the .notmuch/hooks directory within the database directory and must\n"\r
227 +    "\thave executable permissions.\n"\r
228 +    "\n"\r
229 +    "\tThe currently available hooks are described below.\n"\r
230 +    "\n"\r
231 +    "\tpre-new\n"\r
232 +    "\t\tThis hook is invoked by the new command before scanning or\n"\r
233 +    "\t\timporting new messages into the database. If this hook exits\n"\r
234 +    "\t\twith a non-zero status, notmuch will abort further processing\n"\r
235 +    "\t\tof the new command.\n"\r
236 +    "\n"\r
237 +    "\t\tTypically this hook is used for fetching or delivering new\n"\r
238 +    "\t\tmail to be imported into the database.\n"\r
239 +    "\n"\r
240 +    "\tpost-new\n"\r
241 +    "\t\tThis hook is invoked by the new command after new messages\n"\r
242 +    "\t\thave been imported into the database and initial tags have\n"\r
243 +    "\t\tbeen applied. The hook will not be run if there have been any\n"\r
244 +    "\t\terrors during the scan or import.\n"\r
245 +    "\n"\r
246 +    "\t\tTypically this hook is used to perform additional query-based\n"\r
247 +    "\t\ttagging on the imported messages.\n\n";\r
248 +\r
249  static command_t commands[] = {\r
250      { "setup", notmuch_setup_command,\r
251        NULL,\r
252 @@ -144,7 +170,7 @@ static command_t commands[] = {\r
253        "\tInvoking notmuch with no command argument will run setup if\n"\r
254        "\tthe setup command has not previously been completed." },\r
255      { "new", notmuch_new_command,\r
256 -      "[--verbose]",\r
257 +      "[options...]",\r
258        "Find and import new messages to the notmuch database.",\r
259        "\tScans all sub-directories of the mail directory, performing\n"\r
260        "\tfull-text indexing on new messages that are found. Each new\n"\r
261 @@ -159,8 +185,15 @@ static command_t commands[] = {\r
262        "\tis delivered and you wish to incorporate it into the database.\n"\r
263        "\tThese subsequent runs will be much quicker than the initial run.\n"\r
264        "\n"\r
265 +      "\tThe new command supports hooks. See \"notmuch help hooks\" for\n"\r
266 +      "\tmore details on hooks.\n"\r
267 +      "\n"\r
268        "\tSupported options for new include:\n"\r
269        "\n"\r
270 +      "\t--no-hooks\n"\r
271 +      "\n"\r
272 +      "\t\tPrevent hooks from being run.\n"\r
273 +      "\n"\r
274        "\t--verbose\n"\r
275        "\n"\r
276        "\t\tVerbose operation. Shows paths of message files as\n"\r
277 @@ -529,6 +562,10 @@ notmuch_help_command (unused (void *ctx), int argc, char *argv[])\r
278         printf ("\n");\r
279         printf (search_terms_help);\r
280         return 0;\r
281 +    } else if (strcmp (argv[0], "hooks") == 0) {\r
282 +       printf ("Help for <%s>\n\n", argv[0]);\r
283 +       printf (hooks_help);\r
284 +       return 0;\r
285      }\r
286  \r
287      fprintf (stderr,\r
288 -- \r
289 1.7.5.4\r
290 \r