1 Return-Path: <tomc@caurea.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 8F53C431FBC
\r
6 for <notmuch@notmuchmail.org>; Mon, 21 Dec 2009 18:57:06 -0800 (PST)
\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org
\r
8 Received: from olra.theworths.org ([127.0.0.1])
\r
9 by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)
\r
10 with ESMTP id v-NFs4nqX+IY for <notmuch@notmuchmail.org>;
\r
11 Mon, 21 Dec 2009 18:57:06 -0800 (PST)
\r
12 X-Greylist: delayed 135368 seconds by postgrey-1.32 at olra;
\r
13 Mon, 21 Dec 2009 18:57:05 PST
\r
14 Received: from hobbes.caurea.org
\r
15 (gw.ptr-62-65-144-46.customer.ch.netstream.com [62.65.144.46])
\r
16 by olra.theworths.org (Postfix) with ESMTP id DB350431FAE
\r
17 for <notmuch@notmuchmail.org>; Mon, 21 Dec 2009 18:57:05 -0800 (PST)
\r
18 Received: by hobbes.caurea.org (Postfix, from userid 101)
\r
19 id 255592A5F5; Tue, 22 Dec 2009 03:57:03 +0100 (CET)
\r
20 From: Tomas Carnecky <tom@dbservice.com>
\r
21 To: notmuch@notmuchmail.org
\r
22 Date: Tue, 22 Dec 2009 03:56:57 +0100
\r
23 Message-Id: <1261450617-24616-1-git-send-email-tom@dbservice.com>
\r
24 X-Mailer: git-send-email 1.6.6.rc3
\r
25 Subject: [notmuch] [PATCH] Add post-add and post-tag hooks
\r
26 X-BeenThere: notmuch@notmuchmail.org
\r
27 X-Mailman-Version: 2.1.12
\r
29 List-Id: "Use and development of the notmuch mail system."
\r
30 <notmuch.notmuchmail.org>
\r
31 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,
\r
32 <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>
\r
33 List-Archive: <http://notmuchmail.org/pipermail/notmuch>
\r
34 List-Post: <mailto:notmuch@notmuchmail.org>
\r
35 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>
\r
36 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,
\r
37 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
38 X-List-Received-Date: Tue, 22 Dec 2009 02:57:06 -0000
\r
40 The post-add hook is run by 'notmuch new' after each new message is added,
\r
41 post-tag is run after a tag has been added or removed. The hooks are stored
\r
42 in the users home directory (~/.notmuch/hooks/).
\r
44 Since post-tag is run unconditionally every time a new tag is added or removed,
\r
45 that means it is also invoked when 'notmuch new' adds the two implicit
\r
46 tags (inbox, unread). So make sure your scripts don't choke on that and can
\r
47 be both executed in parallel.
\r
49 Signed-off-by: Tomas Carnecky <tom@dbservice.com>
\r
51 lib/message.cc | 45 ++++++++++++++++++++++++++++++++++++++
\r
52 notmuch-new.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
\r
53 2 files changed, 111 insertions(+), 0 deletions(-)
\r
55 diff --git a/lib/message.cc b/lib/message.cc
\r
56 index 49519f1..bcd8abb 100644
\r
57 --- a/lib/message.cc
\r
58 +++ b/lib/message.cc
\r
59 @@ -664,6 +664,47 @@ _notmuch_message_remove_term (notmuch_message_t *message,
\r
60 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
\r
63 +/* Run the post-tag hook */
\r
65 +post_tag_hook (notmuch_message_t *message, const char *tag, int added)
\r
67 + /* Skip tags that notmuch itself assigns to new messages */
\r
68 + const char *skip[] = {
\r
72 + for (int i = 0; i < sizeof (skip) / sizeof (skip[0]); ++i) {
\r
73 + if (strcmp(skip[i], tag) == 0)
\r
77 + char proc[PATH_MAX];
\r
78 + snprintf (proc, PATH_MAX, "%s/.notmuch/hooks/post-tag", getenv("HOME"));
\r
79 + if (access (proc, X_OK))
\r
82 + int pid = fork ();
\r
86 + /* Wait for the hook to finish. This behaviour might be changed in the
\r
87 + * future, but for now I think it's better to take the safe route. */
\r
89 + waitpid (0, NULL, 0);
\r
93 + const char *filename = notmuch_message_get_filename (message);
\r
94 + const char *message_id = notmuch_message_get_message_id (message);
\r
96 + const char *args[] = {
\r
97 + proc, message_id, filename, tag, added ? "added" : "removed", NULL
\r
100 + execv (proc, (char *const *) &args);
\r
105 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
\r
107 @@ -684,6 +725,8 @@ notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
\r
108 if (! message->frozen)
\r
109 _notmuch_message_sync (message);
\r
111 + post_tag_hook (message, tag, 1);
\r
113 return NOTMUCH_STATUS_SUCCESS;
\r
116 @@ -707,6 +750,8 @@ notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
\r
117 if (! message->frozen)
\r
118 _notmuch_message_sync (message);
\r
120 + post_tag_hook (message, tag, 0);
\r
122 return NOTMUCH_STATUS_SUCCESS;
\r
125 diff --git a/notmuch-new.c b/notmuch-new.c
\r
126 index 837ae4f..d984aae 100644
\r
127 --- a/notmuch-new.c
\r
128 +++ b/notmuch-new.c
\r
129 @@ -42,6 +42,71 @@ handle_sigint (unused (int sig))
\r
133 +/* Run the post-add hook. The hook is given the chance to specify additional tags
\r
134 + * that should be added to the message. The hook writes the tags to its stdout,
\r
135 + * separated by a newline. The script's stdout is redirected to a pipe so that
\r
136 + * notmuch can process its output. The tags can be prefixed with '+' or '-' to
\r
137 + * indicate if the tag should be added or removed. Absence of one of these prefixes
\r
138 + * means that the tag will be added. */
\r
140 +post_add_hook (notmuch_message_t *message)
\r
142 + char proc[PATH_MAX];
\r
143 + snprintf (proc, PATH_MAX, "%s/.notmuch/hooks/post-add", getenv ("HOME"));
\r
144 + if (access (proc, X_OK))
\r
147 + /* The pipe between the hook and the notmuch process. The script writes
\r
148 + * into fds[0], notmuch reads from fds[1]. */
\r
153 + int pid = fork ();
\r
158 + } else if (pid > 0) {
\r
160 + waitpid (0, NULL, 0);
\r
162 + char buffer[256] = { 0, };
\r
163 + read (fds[1], buffer, sizeof (buffer));
\r
166 + for (tag = buffer; tag && *tag; ) {
\r
167 + char *end = strchr (tag, '\n');
\r
171 + if (tag[0] == '+')
\r
172 + notmuch_message_add_tag (message, tag + 1);
\r
173 + else if (tag[0] == '-')
\r
174 + notmuch_message_remove_tag (message, tag + 1);
\r
176 + notmuch_message_add_tag (message, tag);
\r
178 + tag = end ? end + 1 : end;
\r
184 + /* This is the child process (where the hook runs) */
\r
186 + dup2 (fds[0], 1);
\r
188 + const char *filename = notmuch_message_get_filename (message);
\r
189 + const char *message_id = notmuch_message_get_message_id (message);
\r
190 + const char *args[] = {
\r
191 + proc, message_id, filename, NULL
\r
194 + execv (proc, (char *const *) &args);
\r
199 tag_inbox_and_unread (notmuch_message_t *message)
\r
201 @@ -253,6 +318,7 @@ add_files_recursive (notmuch_database_t *notmuch,
\r
202 case NOTMUCH_STATUS_SUCCESS:
\r
203 state->added_messages++;
\r
204 tag_inbox_and_unread (message);
\r
205 + post_add_hook (message);
\r
207 /* Non-fatal issues (go on to next file) */
\r
208 case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
\r