Re: notmuch and "mute" -- useful to anyone?
[notmuch-archives.git] / 79 / 4ce3752a973de2f20f38fed1da76ad3e2b90d0
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 CF948429E30\r
6         for <notmuch@notmuchmail.org>; Thu,  8 Dec 2011 14:48:42 -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 XmwCePFckjfU for <notmuch@notmuchmail.org>;\r
17         Thu,  8 Dec 2011 14:48:41 -0800 (PST)\r
18 Received: from mail-ee0-f53.google.com (mail-ee0-f53.google.com\r
19  [74.125.83.53])        (using TLSv1 with cipher RC4-SHA (128/128 bits))        (No client\r
20  certificate requested) by olra.theworths.org (Postfix) with ESMTPS id\r
21  50ACB431FB6    for <notmuch@notmuchmail.org>; Thu,  8 Dec 2011 14:48:41 -0800\r
22  (PST)\r
23 Received: by eekb57 with SMTP id b57so1738541eek.26\r
24         for <notmuch@notmuchmail.org>; Thu, 08 Dec 2011 14:48:40 -0800 (PST)\r
25 Received: by 10.14.6.146 with SMTP id 18mr413431een.58.1323384520176;\r
26         Thu, 08 Dec 2011 14:48:40 -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 q28sm23178289eea.6.2011.12.08.14.48.38\r
30         (version=SSLv3 cipher=OTHER); Thu, 08 Dec 2011 14:48:39 -0800 (PST)\r
31 From: Jani Nikula <jani@nikula.org>\r
32 To: notmuch@notmuchmail.org\r
33 Subject: [PATCH v4 1/3] cli: introduce the concept of user defined hooks\r
34 Date: Fri,  9 Dec 2011 00:48:29 +0200\r
35 Message-Id:\r
36  <1e4ddb5e3a6b980e47418f67e16709a42e63bc47.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 MIME-Version: 1.0\r
45 Content-Type: text/plain; charset=UTF-8\r
46 Content-Transfer-Encoding: 8bit\r
47 X-BeenThere: notmuch@notmuchmail.org\r
48 X-Mailman-Version: 2.1.13\r
49 Precedence: list\r
50 List-Id: "Use and development of the notmuch mail system."\r
51         <notmuch.notmuchmail.org>\r
52 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
53         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
54 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
55 List-Post: <mailto:notmuch@notmuchmail.org>\r
56 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
57 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
58         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
59 X-List-Received-Date: Thu, 08 Dec 2011 22:48:43 -0000\r
60 \r
61 Add mechanism for running user defined hooks. Hooks are executables or\r
62 symlinks to executables stored under the new notmuch hooks directory,\r
63 <database-path>/.notmuch/hooks.\r
64 \r
65 No hooks are introduced here, but adding support for a hook is now a simple\r
66 matter of calling the new notmuch_run_hook() function at an appropriate\r
67 location with the hook name.\r
68 \r
69 Signed-off-by: Jani Nikula <jani@nikula.org>\r
70 ---\r
71  Makefile.local   |    1 +\r
72  hooks.c          |   93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++\r
73  notmuch-client.h |    3 ++\r
74  3 files changed, 97 insertions(+), 0 deletions(-)\r
75  create mode 100644 hooks.c\r
76 \r
77 diff --git a/Makefile.local b/Makefile.local\r
78 index 15e6d88..88365da 100644\r
79 --- a/Makefile.local\r
80 +++ b/Makefile.local\r
81 @@ -298,6 +298,7 @@ notmuch_client_srcs =               \\r
82         debugger.c              \\r
83         gmime-filter-reply.c    \\r
84         gmime-filter-headers.c  \\r
85 +       hooks.c                 \\r
86         notmuch.c               \\r
87         notmuch-config.c        \\r
88         notmuch-count.c         \\r
89 diff --git a/hooks.c b/hooks.c\r
90 new file mode 100644\r
91 index 0000000..44ee419\r
92 --- /dev/null\r
93 +++ b/hooks.c\r
94 @@ -0,0 +1,93 @@\r
95 +/* notmuch - Not much of an email program, (just index and search)\r
96 + *\r
97 + * This file is part of notmuch.\r
98 + *\r
99 + * Copyright © 2011 Jani Nikula\r
100 + *\r
101 + * This program is free software: you can redistribute it and/or modify\r
102 + * it under the terms of the GNU General Public License as published by\r
103 + * the Free Software Foundation, either version 3 of the License, or\r
104 + * (at your option) any later version.\r
105 + *\r
106 + * This program is distributed in the hope that it will be useful,\r
107 + * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
108 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
109 + * GNU General Public License for more details.\r
110 + *\r
111 + * You should have received a copy of the GNU General Public License\r
112 + * along with this program.  If not, see http://www.gnu.org/licenses/ .\r
113 + *\r
114 + * Author: Jani Nikula <jani@nikula.org>\r
115 + */\r
116 +\r
117 +#include "notmuch-client.h"\r
118 +#include <sys/wait.h>\r
119 +\r
120 +int\r
121 +notmuch_run_hook (const char *db_path, const char *hook)\r
122 +{\r
123 +    char *hook_path;\r
124 +    int status = 0;\r
125 +    pid_t pid;\r
126 +\r
127 +    hook_path = talloc_asprintf (NULL, "%s/%s/%s/%s", db_path, ".notmuch",\r
128 +                                "hooks", hook);\r
129 +    if (hook_path == NULL) {\r
130 +       fprintf (stderr, "Out of memory\n");\r
131 +       return 1;\r
132 +    }\r
133 +\r
134 +    /* Check access before fork() for speed and simplicity of error handling. */\r
135 +    if (access (hook_path, X_OK) == -1) {\r
136 +       /* Ignore ENOENT. It's okay not to have a hook, hook dir, or even\r
137 +        * notmuch dir. Dangling symbolic links also result in ENOENT, but\r
138 +        * we'll ignore that too for simplicity. */\r
139 +       if (errno != ENOENT) {\r
140 +           fprintf (stderr, "Error: %s hook access failed: %s\n", hook,\r
141 +                    strerror (errno));\r
142 +           status = 1;\r
143 +       }\r
144 +       goto DONE;\r
145 +    }\r
146 +\r
147 +    pid = fork();\r
148 +    if (pid == -1) {\r
149 +       fprintf (stderr, "Error: %s hook fork failed: %s\n", hook,\r
150 +                strerror (errno));\r
151 +       status = 1;\r
152 +       goto DONE;\r
153 +    } else if (pid == 0) {\r
154 +       execl (hook_path, hook_path, NULL);\r
155 +       /* Same as above for ENOENT, but unlikely now. Indicate all other errors\r
156 +        * to parent through non-zero exit status. */\r
157 +       if (errno != ENOENT) {\r
158 +           fprintf (stderr, "Error: %s hook execution failed: %s\n", hook,\r
159 +                    strerror (errno));\r
160 +           status = 1;\r
161 +       }\r
162 +       exit (status);\r
163 +    }\r
164 +\r
165 +    if (waitpid (pid, &status, 0) == -1) {\r
166 +       fprintf (stderr, "Error: %s hook wait failed: %s\n", hook,\r
167 +                strerror (errno));\r
168 +       status = 1;\r
169 +       goto DONE;\r
170 +    }\r
171 +\r
172 +    if (!WIFEXITED (status) || WEXITSTATUS (status)) {\r
173 +       if (WIFEXITED (status)) {\r
174 +           fprintf (stderr, "Error: %s hook failed with status %d\n",\r
175 +                    hook, WEXITSTATUS (status));\r
176 +       } else if (WIFSIGNALED (status)) {\r
177 +           fprintf (stderr, "Error: %s hook terminated with signal %d\n",\r
178 +                    hook, WTERMSIG (status));\r
179 +       }\r
180 +       status = 1;\r
181 +    }\r
182 +\r
183 +  DONE:\r
184 +    talloc_free (hook_path);\r
185 +\r
186 +    return status;\r
187 +}\r
188 diff --git a/notmuch-client.h b/notmuch-client.h\r
189 index b50cb38..a91ad6c 100644\r
190 --- a/notmuch-client.h\r
191 +++ b/notmuch-client.h\r
192 @@ -235,6 +235,9 @@ void\r
193  notmuch_config_set_maildir_synchronize_flags (notmuch_config_t *config,\r
194                                               notmuch_bool_t synchronize_flags);\r
195  \r
196 +int\r
197 +notmuch_run_hook (const char *db_path, const char *hook);\r
198 +\r
199  notmuch_bool_t\r
200  debugger_is_active (void);\r
201  \r
202 -- \r
203 1.7.5.4\r
204 \r