Re: [PATCH v4 01/16] add util/search-path.{c, h} to test for executables in $PATH
[notmuch-archives.git] / b3 / 417724a1c1860b798d1c2e947e45aa820f3ab3
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 C7A10431FBF\r
6         for <notmuch@notmuchmail.org>; Sat, 27 Apr 2013 14:24:40 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: -0.7\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
12         tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
13 Received: from olra.theworths.org ([127.0.0.1])\r
14         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
15         with ESMTP id vCzTYHVxf+kc for <notmuch@notmuchmail.org>;\r
16         Sat, 27 Apr 2013 14:24:38 -0700 (PDT)\r
17 Received: from mail-la0-f49.google.com (mail-la0-f49.google.com\r
18         [209.85.215.49]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
19         (No client certificate requested)\r
20         by olra.theworths.org (Postfix) with ESMTPS id 6BE77431FAE\r
21         for <notmuch@notmuchmail.org>; Sat, 27 Apr 2013 14:24:38 -0700 (PDT)\r
22 Received: by mail-la0-f49.google.com with SMTP id fp13so2195942lab.8\r
23         for <notmuch@notmuchmail.org>; Sat, 27 Apr 2013 14:24:36 -0700 (PDT)\r
24 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
25         d=google.com; s=20120113;\r
26         h=x-received:from:to:subject:in-reply-to:references:user-agent:date\r
27         :message-id:mime-version:content-type:content-transfer-encoding\r
28         :x-gm-message-state;\r
29         bh=I5GpV/6rgqYgKg0jeDb/C/UMiD5W7PYr4wO94Y3Ywz4=;\r
30         b=PfJvWoVOZE7iOcIco3x/sEXK39JPlYK0PQBj5pHiCbvZ364IhrwBmnKL7DVHvw6EcK\r
31         DFZbhmbZNsx9ZmJysicxqPTJykqlJ3fKEj3Zp6Y7+EdryCol13XZ77rqKNGE+LaTzD6y\r
32         0r/s1iemlrZNc8k/X+eUtIPK9a7h9dF+n5C4BFw93gYbTZtESEQ/CNvldxNw+pD2/moU\r
33         RHm2ZeZBqzQf5YU78mwsZW5mQQ5ijRySwdjZnUnWfugouw1ssVDwu1JdcvmoKTj+mwft\r
34         Kj+J9UZn/7qSDSyhnDu4k6abhggRciQDVAtA1mWvzLGK7cU6Y0jlqvXVpBLEQ4vbllFp\r
35         DZ/g==\r
36 X-Received: by 10.112.129.137 with SMTP id nw9mr24536735lbb.56.1367097875460; \r
37         Sat, 27 Apr 2013 14:24:35 -0700 (PDT)\r
38 Received: from localhost (dsl-hkibrasgw2-58c376-211.dhcp.inet.fi.\r
39         [88.195.118.211])\r
40         by mx.google.com with ESMTPSA id r9sm3509338lbr.3.2013.04.27.14.24.33\r
41         for <multiple recipients>\r
42         (version=TLSv1.2 cipher=RC4-SHA bits=128/128);\r
43         Sat, 27 Apr 2013 14:24:34 -0700 (PDT)\r
44 From: Jani Nikula <jani@nikula.org>\r
45 To: Peter Wang <novalazy@gmail.com>, notmuch@notmuchmail.org\r
46 Subject: Re: [PATCH v5 03/12] cli: add insert command\r
47 In-Reply-To: <1364942517-6982-4-git-send-email-novalazy@gmail.com>\r
48 References: <1364942517-6982-1-git-send-email-novalazy@gmail.com>\r
49         <1364942517-6982-4-git-send-email-novalazy@gmail.com>\r
50 User-Agent: Notmuch/0.15.2+70~g2eeb96a (http://notmuchmail.org) Emacs/24.2.1\r
51         (x86_64-pc-linux-gnu)\r
52 Date: Sun, 28 Apr 2013 00:24:28 +0300\r
53 Message-ID: <8761z7r7jn.fsf@nikula.org>\r
54 MIME-Version: 1.0\r
55 Content-Type: text/plain; charset=utf-8\r
56 Content-Transfer-Encoding: quoted-printable\r
57 X-Gm-Message-State:\r
58  ALoCoQkTLEevSBETDEHJudgQoaJ4yi3L/7y2+FG6ldcU288Uridvy/LHIzVhrwO02MVgUqJtzZY/\r
59 X-BeenThere: notmuch@notmuchmail.org\r
60 X-Mailman-Version: 2.1.13\r
61 Precedence: list\r
62 List-Id: "Use and development of the notmuch mail system."\r
63         <notmuch.notmuchmail.org>\r
64 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
65         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
66 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
67 List-Post: <mailto:notmuch@notmuchmail.org>\r
68 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
69 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
70         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
71 X-List-Received-Date: Sat, 27 Apr 2013 21:24:40 -0000\r
72 \r
73 On Wed, 03 Apr 2013, Peter Wang <novalazy@gmail.com> wrote:\r
74 > The notmuch insert command reads a message from standard input,\r
75 > writes it to a Maildir folder, and then incorporates the message into\r
76 > the notmuch database.  Essentially it moves the functionality of\r
77 > notmuch-deliver into notmuch.\r
78 >\r
79 > Though it could be used as an alternative to notmuch new, the reason\r
80 > I want this is to allow my notmuch frontend to add postponed or sent\r
81 > messages to the mail store and notmuch database, without resorting to\r
82 > another tool (e.g. notmuch-deliver) nor directly modifying the maildir.\r
83 > ---\r
84 >  Makefile.local   |   1 +\r
85 >  notmuch-client.h |   3 +\r
86 >  notmuch-insert.c | 336 +++++++++++++++++++++++++++++++++++++++++++++++++=\r
87 ++++++\r
88 >  notmuch.c        |   3 +\r
89 >  4 files changed, 343 insertions(+)\r
90 >  create mode 100644 notmuch-insert.c\r
91 >\r
92 > diff --git a/Makefile.local b/Makefile.local\r
93 > index c274f07..bb2381d 100644\r
94 > --- a/Makefile.local\r
95 > +++ b/Makefile.local\r
96 > @@ -261,6 +261,7 @@ notmuch_client_srcs =3D           \\r
97 >       notmuch-config.c        \\r
98 >       notmuch-count.c         \\r
99 >       notmuch-dump.c          \\r
100 > +     notmuch-insert.c        \\r
101 >       notmuch-new.c           \\r
102 >       notmuch-reply.c         \\r
103 >       notmuch-restore.c       \\r
104 > diff --git a/notmuch-client.h b/notmuch-client.h\r
105 > index 45749a6..da332f3 100644\r
106 > --- a/notmuch-client.h\r
107 > +++ b/notmuch-client.h\r
108 > @@ -180,6 +180,9 @@ int\r
109 >  notmuch_new_command (notmuch_config_t *config, int argc, char *argv[]);\r
110 >=20=20\r
111 >  int\r
112 > +notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[]=\r
113 );\r
114 > +\r
115 > +int\r
116 >  notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[]);\r
117 >=20=20\r
118 >  int\r
119 > diff --git a/notmuch-insert.c b/notmuch-insert.c\r
120 > new file mode 100644\r
121 > index 0000000..19b1cf9\r
122 > --- /dev/null\r
123 > +++ b/notmuch-insert.c\r
124 > @@ -0,0 +1,336 @@\r
125 > +/* notmuch - Not much of an email program, (just index and search)\r
126 > + *\r
127 > + * Copyright =C2=A9 2013 Peter Wang\r
128 > + *\r
129 > + * Based in part on notmuch-deliver\r
130 > + * Copyright =C2=A9 2010 Ali Polatel\r
131 > + *\r
132 > + * This program is free software: you can redistribute it and/or modify\r
133 > + * it under the terms of the GNU General Public License as published by\r
134 > + * the Free Software Foundation, either version 3 of the License, or\r
135 > + * (at your option) any later version.\r
136 > + *\r
137 > + * This program is distributed in the hope that it will be useful,\r
138 > + * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
139 > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
140 > + * GNU General Public License for more details.\r
141 > + *\r
142 > + * You should have received a copy of the GNU General Public License\r
143 > + * along with this program.  If not, see http://www.gnu.org/licenses/ .\r
144 > + *\r
145 > + * Author: Peter Wang <novalazy@gmail.com>\r
146 > + */\r
147 > +\r
148 > +#include "notmuch-client.h"\r
149 > +#include "tag-util.h"\r
150 > +\r
151 > +#include <sys/types.h>\r
152 > +#include <sys/stat.h>\r
153 > +#include <fcntl.h>\r
154 > +\r
155 > +static volatile sig_atomic_t interrupted;\r
156 > +\r
157 > +static void\r
158 > +handle_sigint (unused (int sig))\r
159 > +{\r
160 > +    static char msg[] =3D "Stopping...         \n";\r
161 > +\r
162 > +    /* This write is "opportunistic", so it's okay to ignore the\r
163 > +     * result.  It is not required for correctness, and if it does\r
164 > +     * fail or produce a short write, we want to get out of the signal\r
165 > +     * handler as quickly as possible, not retry it. */\r
166 > +    IGNORE_RESULT (write (2, msg, sizeof (msg) - 1));\r
167 > +    interrupted =3D 1;\r
168 > +}\r
169 > +\r
170 > +/* Like gethostname but guarantees that a null-terminated hostname is\r
171 > + * returned, even if it has to make one up. Invalid characters are\r
172 > + * substituted such that the hostname can be used within a filename.\r
173 > + */\r
174 > +static void\r
175 > +safe_gethostname (char *hostname, size_t len)\r
176 > +{\r
177 > +    char *p;\r
178 > +\r
179 > +    if (gethostname (hostname, len) =3D=3D -1) {\r
180 > +     strncpy (hostname, "unknown", len);\r
181 > +    }\r
182 > +    hostname[len - 1] =3D '\0';\r
183 > +\r
184 > +    for (p =3D hostname; *p !=3D '\0'; p++) {\r
185 > +     if (*p =3D=3D '/' || *p =3D=3D ':')\r
186 > +         *p =3D '_';\r
187 > +    }\r
188 > +}\r
189 > +\r
190 > +/* Call fsync() on a directory path. */\r
191 > +static notmuch_bool_t\r
192 > +sync_dir (const char *dir)\r
193 > +{\r
194 > +    notmuch_bool_t ret;\r
195 > +    int fd;\r
196 > +\r
197 > +    fd =3D open (dir, O_RDONLY);\r
198 > +    if (fd =3D=3D -1) {\r
199 > +     fprintf (stderr, "Error: open() dir failed: %s\n", strerror (errno));\r
200 > +     return FALSE;\r
201 > +    }\r
202 > +    ret =3D (fsync (fd) =3D=3D 0);\r
203 > +    if (! ret) {\r
204 > +     fprintf (stderr, "Error: fsync() dir failed: %s\n", strerror (errno));\r
205 > +    }\r
206 > +    close (fd);\r
207 > +    return ret;\r
208 > +}\r
209 > +\r
210 > +/* Open a unique file in the Maildir 'tmp' directory.\r
211 > + * Returns the file descriptor on success, or -1 on failure.\r
212 > + * On success, file paths for the message in the 'tmp' and 'new'\r
213 > + * directories are returned via tmppath and newpath,\r
214 > + * and the path of the 'new' directory itself in newdir. */\r
215 > +static int\r
216 > +maildir_open_tmp_file (void *ctx, const char *dir,\r
217 > +                    char **tmppath, char **newpath, char **newdir)\r
218 > +{\r
219 > +    pid_t pid;\r
220 > +    char hostname[256];\r
221 > +    struct timeval tv;\r
222 > +    char *filename;\r
223 > +    int fd =3D -1;\r
224 > +\r
225 > +    /* We follow the Dovecot file name generation algorithm. */\r
226 > +    pid =3D getpid ();\r
227 > +    safe_gethostname (hostname, sizeof (hostname));\r
228 > +    do {\r
229 > +     gettimeofday (&tv, NULL);\r
230 > +     filename =3D talloc_asprintf (ctx, "%ld.M%ldP%d.%s",\r
231 > +                                 tv.tv_sec, tv.tv_usec, pid, hostname);\r
232 > +     if (! filename) {\r
233 > +         fprintf (stderr, "Out of memory\n");\r
234 > +         return -1;\r
235 > +     }\r
236 > +\r
237 > +     *tmppath =3D talloc_asprintf (ctx, "%s/tmp/%s", dir, filename);\r
238 > +     if (! *tmppath) {\r
239 > +         fprintf (stderr, "Out of memory\n");\r
240 > +         return -1;\r
241 > +     }\r
242 > +\r
243 > +     fd =3D open (*tmppath, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600);\r
244 > +    } while (fd =3D=3D -1 && errno =3D=3D EEXIST);\r
245 > +\r
246 > +    if (fd =3D=3D -1) {\r
247 > +     fprintf (stderr, "Error: opening %s: %s\n", *tmppath, strerror (errno));\r
248 > +     return -1;\r
249 > +    }\r
250 > +\r
251 > +    *newdir =3D talloc_asprintf (ctx, "%s/new", dir);\r
252 > +    *newpath =3D talloc_asprintf (ctx, "%s/new/%s", dir, filename);\r
253 > +    if (! *newdir || ! *newpath) {\r
254 > +     fprintf (stderr, "Out of memory\n");\r
255 > +     close (fd);\r
256 > +     unlink (*tmppath);\r
257 > +     return -1;\r
258 > +    }\r
259 > +\r
260 > +    talloc_free (filename);\r
261 > +\r
262 > +    return fd;\r
263 > +}\r
264 > +\r
265 > +/* Copy the contents of standard input (fdin) into fdout. */\r
266 > +static notmuch_bool_t\r
267 > +copy_stdin (int fdin, int fdout)\r
268 > +{\r
269 > +    char buf[4096];\r
270 > +    char *p;\r
271 > +    ssize_t remain;\r
272 > +    ssize_t written;\r
273 > +\r
274 > +    while (! interrupted) {\r
275 > +     remain =3D read (fdin, buf, sizeof (buf));\r
276 > +     if (remain =3D=3D 0)\r
277 > +         break;\r
278 > +     if (remain < 0) {\r
279 > +         if (errno =3D=3D EINTR)\r
280 > +             continue;\r
281 > +         fprintf (stderr, "Error: reading from standard input: %s\n",\r
282 > +                  strerror (errno));\r
283 > +         return FALSE;\r
284 > +     }\r
285 > +\r
286 > +     p =3D buf;\r
287 > +     do {\r
288 > +         written =3D write (fdout, p, remain);\r
289 > +         if (written < 0 && errno =3D=3D EINTR)\r
290 > +             continue;\r
291 > +         if (written <=3D 0) {\r
292 > +             fprintf (stderr, "Error: writing to temporary file: %s",\r
293 > +                      strerror (errno));\r
294 > +             return FALSE;\r
295 > +         }\r
296 > +         p +=3D written;\r
297 > +         remain -=3D written;\r
298 > +     } while (remain > 0);\r
299 > +    }\r
300 > +\r
301 > +    return ! interrupted;\r
302 > +}\r
303 > +\r
304 > +/* Add the specified message file to the notmuch database, applying tags.\r
305 > + * The file is renamed to encode notmuch tags as maildir flags. */\r
306 > +static notmuch_bool_t\r
307 > +add_file_to_database (notmuch_database_t *notmuch, const char *path,\r
308 > +                   tag_op_list_t *tag_ops)\r
309 > +{\r
310 > +    notmuch_message_t *message;\r
311 > +    notmuch_status_t status;\r
312 > +\r
313 > +    status =3D notmuch_database_add_message (notmuch, path, &message);\r
314 > +    switch (status) {\r
315 > +    case NOTMUCH_STATUS_SUCCESS:\r
316 > +    case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:\r
317 > +     break;\r
318 > +    default:\r
319 > +    case NOTMUCH_STATUS_FILE_NOT_EMAIL:\r
320 \r
321 If such a message really arrives, the mail system will keep trying if\r
322 failure is returned. Maybe deliver the file without indexing, and return\r
323 success?\r
324 \r
325 > +    case NOTMUCH_STATUS_READ_ONLY_DATABASE:\r
326 > +    case NOTMUCH_STATUS_XAPIAN_EXCEPTION:\r
327 > +    case NOTMUCH_STATUS_OUT_OF_MEMORY:\r
328 > +    case NOTMUCH_STATUS_FILE_ERROR:\r
329 > +    case NOTMUCH_STATUS_NULL_POINTER:\r
330 > +    case NOTMUCH_STATUS_TAG_TOO_LONG:\r
331 > +    case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW:\r
332 > +    case NOTMUCH_STATUS_UNBALANCED_ATOMIC:\r
333 > +    case NOTMUCH_STATUS_LAST_STATUS:\r
334 > +     fprintf (stderr, "Error: failed to add `%s' to notmuch database: %s\n",\r
335 > +              path, notmuch_status_to_string (status));\r
336 > +     return FALSE;\r
337 > +    }\r
338 > +\r
339 > +    if (status =3D=3D NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {\r
340 > +     /* Don't change tags of an existing message. */\r
341 > +     status =3D notmuch_message_tags_to_maildir_flags (message);\r
342 > +     if (status !=3D NOTMUCH_STATUS_SUCCESS)\r
343 > +         fprintf (stderr, "Error: failed to sync tags to maildir flags\n");\r
344 > +    } else {\r
345 > +     status =3D tag_op_list_apply (message, tag_ops, TAG_FLAG_MAILDIR_SYNC);\r
346 \r
347 Syncing tags to maildir flags is more interesting here than above. And\r
348 it should be done because notmuch insert allows arbitrary tags on the\r
349 command line. Having, for example, -unread or +flagged on the command\r
350 line makes the flags go out of sync. (notmuch new should do the syncing\r
351 too, but it's less important because it only adds new.tags.)\r
352 \r
353 However, calling notmuch_message_tags_to_maildir_flags() may rename the\r
354 file from new to cur, which blows up the directory syncing and file\r
355 unlinking on the error path in insert_message() below.\r
356 \r
357 > +    }\r
358 > +\r
359 > +    notmuch_message_destroy (message);\r
360 > +\r
361 > +    return (status =3D=3D NOTMUCH_STATUS_SUCCESS);\r
362 > +}\r
363 > +\r
364 > +static notmuch_bool_t\r
365 > +insert_message (void *ctx, notmuch_database_t *notmuch, int fdin,\r
366 > +             const char *dir, tag_op_list_t *tag_ops)\r
367 > +{\r
368 > +    char *tmppath;\r
369 > +    char *newpath;\r
370 > +    char *newdir;\r
371 > +    int fdout;\r
372 > +    char *cleanup_path;\r
373 > +\r
374 > +    fdout =3D maildir_open_tmp_file (ctx, dir, &tmppath, &newpath, &newd=\r
375 ir);\r
376 > +    if (fdout < 0)\r
377 > +     return FALSE;\r
378 > +\r
379 > +    cleanup_path =3D tmppath;\r
380 > +\r
381 > +    if (! copy_stdin (fdin, fdout))\r
382 > +     goto FAIL;\r
383 > +\r
384 > +    if (fsync (fdout) !=3D 0) {\r
385 > +     fprintf (stderr, "Error: fsync failed: %s\n", strerror (errno));\r
386 > +     goto FAIL;\r
387 > +    }\r
388 > +\r
389 > +    close (fdout);\r
390 > +    fdout =3D -1;\r
391 > +\r
392 > +    /* Atomically move the new message file from the Maildir 'tmp' direc=\r
393 tory\r
394 > +     * to the 'new' directory.  We follow the Dovecot recommendation to\r
395 > +     * simply use rename() instead of link() and unlink().\r
396 > +     * See also: http://wiki.dovecot.org/MailboxFormat/Maildir#Mail_deli=\r
397 very\r
398 > +     */\r
399 > +    if (rename (tmppath, newpath) !=3D 0) {\r
400 > +     fprintf (stderr, "Error: rename() failed: %s\n", strerror (errno));\r
401 > +     goto FAIL;\r
402 > +    }\r
403 > +\r
404 > +    cleanup_path =3D newpath;\r
405 > +\r
406 > +    if (! add_file_to_database (notmuch, newpath, tag_ops)) {\r
407 > +     /* XXX add an option to keep the file in maildir? */\r
408 \r
409 Possibly a good idea to let the user decide. This is the part that I\r
410 worry most about in the series. It's not unusual to hit xapian\r
411 exceptions when the database is already locked, which results\r
412 (hopefully!) in another attempt at delivery. If not, mail is lost.\r
413 \r
414 However, keeping the file in maildir on indexing errors ignores the tags\r
415 specified on the notmuch insert command line, and the message will get\r
416 just new.tags next time notmuch new is run. This is not nice either.\r
417 \r
418 BR,\r
419 Jani.\r
420 \r
421 > +     goto FAIL;\r
422 > +    }\r
423 > +\r
424 > +    if (! sync_dir (newdir))\r
425 > +     goto FAIL;\r
426 > +\r
427 > +    return TRUE;\r
428 > +\r
429 > +  FAIL:\r
430 > +    if (fdout >=3D 0)\r
431 > +     close (fdout);\r
432 > +    unlink (cleanup_path);\r
433 > +    return FALSE;\r
434 > +}\r
435 > +\r
436 > +int\r
437 > +notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])\r
438 > +{\r
439 > +    notmuch_database_t *notmuch;\r
440 > +    struct sigaction action;\r
441 > +    const char *db_path;\r
442 > +    const char **new_tags;\r
443 > +    size_t new_tags_length;\r
444 > +    tag_op_list_t *tag_ops;\r
445 > +    char *query_string =3D NULL;\r
446 > +    const char *maildir;\r
447 > +    int opt_index =3D 1;\r
448 > +    unsigned int i;\r
449 > +    notmuch_bool_t ret;\r
450 > +\r
451 > +    db_path =3D notmuch_config_get_database_path (config);\r
452 > +    new_tags =3D notmuch_config_get_new_tags (config, &new_tags_length);\r
453 > +\r
454 > +    tag_ops =3D tag_op_list_create (config);\r
455 > +    if (tag_ops =3D=3D NULL) {\r
456 > +     fprintf (stderr, "Out of memory.\n");\r
457 > +     return 1;\r
458 > +    }\r
459 > +    for (i =3D 0; i < new_tags_length; i++) {\r
460 > +     if (tag_op_list_append (tag_ops, new_tags[i], FALSE))\r
461 > +         return 1;\r
462 > +    }\r
463 > +\r
464 > +    if (parse_tag_command_line (config, argc - opt_index, argv + opt_ind=\r
465 ex,\r
466 > +                             &query_string, tag_ops))\r
467 > +     return 1;\r
468 > +\r
469 > +    if (*query_string !=3D '\0') {\r
470 > +     fprintf (stderr, "Error: unexpected query string: %s\n", query_string);\r
471 > +     return 1;\r
472 > +    }\r
473 > +\r
474 > +    maildir =3D db_path;\r
475 > +\r
476 > +    /* Setup our handler for SIGINT. We do not set SA_RESTART so that co=\r
477 pying\r
478 > +     * from standard input may be interrupted. */\r
479 > +    memset (&action, 0, sizeof (struct sigaction));\r
480 > +    action.sa_handler =3D handle_sigint;\r
481 > +    sigemptyset (&action.sa_mask);\r
482 > +    action.sa_flags =3D 0;\r
483 > +    sigaction (SIGINT, &action, NULL);\r
484 > +\r
485 > +    if (notmuch_database_open (notmuch_config_get_database_path (config),\r
486 > +                            NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))\r
487 > +     return 1;\r
488 > +\r
489 > +    ret =3D insert_message (config, notmuch, STDIN_FILENO, maildir, tag_=\r
490 ops);\r
491 > +\r
492 > +    notmuch_database_destroy (notmuch);\r
493 > +\r
494 > +    return (ret) ? 0 : 1;\r
495 > +}\r
496 > diff --git a/notmuch.c b/notmuch.c\r
497 > index f51a84f..1a44f11 100644\r
498 > --- a/notmuch.c\r
499 > +++ b/notmuch.c\r
500 > @@ -48,6 +48,9 @@ static command_t commands[] =3D {\r
501 >      { "new", notmuch_new_command, FALSE,\r
502 >        "[options...]",\r
503 >        "Find and import new messages to the notmuch database." },\r
504 > +    { "insert", notmuch_insert_command, FALSE,\r
505 > +      "[options...] [--] [+<tag>|-<tag> ...] < message",\r
506 > +      "Add a new message into the maildir and notmuch database." },\r
507 >      { "search", notmuch_search_command, FALSE,\r
508 >        "[options...] <search-terms> [...]",\r
509 >        "Search for messages matching the given search terms." },\r
510 > --=20\r
511 > 1.7.12.1\r
512 >\r
513 > _______________________________________________\r
514 > notmuch mailing list\r
515 > notmuch@notmuchmail.org\r
516 > http://notmuchmail.org/mailman/listinfo/notmuch\r