Re: Query to get emails from notmuch mailing list?
[notmuch-archives.git] / b2 / 0a67e0656dbfb542a766a3653dffb17e1bb393
1 Return-Path: <bart@po8.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 B6919431FBC\r
6         for <notmuch@notmuchmail.org>; Mon, 22 Feb 2010 12:27:37 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: -0.384\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-0.384 tagged_above=-999 required=5\r
12         tests=[AWL=-0.385, BAYES_50=0.001] autolearn=ham\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 W7Eq1ewMlHVw for <notmuch@notmuchmail.org>;\r
16         Mon, 22 Feb 2010 12:27:34 -0800 (PST)\r
17 X-Greylist: delayed 1415 seconds by postgrey-1.32 at olra;\r
18         Mon, 22 Feb 2010 12:27:33 PST\r
19 Received: from po8.org (po8.org [69.168.48.167])\r
20         by olra.theworths.org (Postfix) with ESMTP id EE889431FAE\r
21         for <notmuch@notmuchmail.org>; Mon, 22 Feb 2010 12:27:33 -0800 (PST)\r
22 Received: from po8.org ([127.0.0.1]) by po8.org with esmtp (Exim 4.69)\r
23         (envelope-from <bart@po8.org>) id 1NjeZ1-0001qw-HZ\r
24         for notmuch@notmuchmail.org; Mon, 22 Feb 2010 12:07:34 -0800\r
25 To: Notmuch Mail <notmuch@notmuchmail.org>\r
26 From: Bart Massey <bart@po8.org>\r
27 Date: Mon, 22 Feb 2010 12:07:31 -0800\r
28 Sender: bart@po8.org\r
29 Message-Id: <E1NjeZ1-0001qw-HZ@po8.org>\r
30 Subject: [notmuch] [PATCH] Added mail directory filename pattern support.\r
31 X-BeenThere: notmuch@notmuchmail.org\r
32 X-Mailman-Version: 2.1.13\r
33 Precedence: list\r
34 List-Id: "Use and development of the notmuch mail system."\r
35         <notmuch.notmuchmail.org>\r
36 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
37         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
38 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
39 List-Post: <mailto:notmuch@notmuchmail.org>\r
40 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
41 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
42         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
43 X-List-Received-Date: Mon, 22 Feb 2010 20:27:38 -0000\r
44 \r
45 Typically, the filenames in a mail directory that actually\r
46 contain mail obey some specific format.  For example, in my\r
47 MH email directory, all mail filenames consist only of\r
48 digits.\r
49 \r
50 This patch adds support for a config file variable\r
51 "filename_pattern" which maybe set to a regex used to filter\r
52 only valid mail filenames when scanning.  Effective use of\r
53 filename_pattern cuts down on the noise from notmuch, and\r
54 may speed it up in some cases.\r
55 \r
56 Signed-off-by: Bart Massey <bart@cs.pdx.edu>\r
57 ---\r
58  notmuch-client.h |    7 +++++++\r
59  notmuch-config.c |   47 +++++++++++++++++++++++++++++++++++++++++++++--\r
60  notmuch-new.c    |   35 +++++++++++++++++++++++++++++++----\r
61  3 files changed, 83 insertions(+), 6 deletions(-)\r
62 \r
63 diff --git a/notmuch-client.h b/notmuch-client.h\r
64 index 77766de..191988c 100644\r
65 --- a/notmuch-client.h\r
66 +++ b/notmuch-client.h\r
67 @@ -146,6 +146,13 @@ notmuch_config_set_database_path (notmuch_config_t *config,\r
68                                   const char *database_path);\r
69  \r
70  const char *\r
71 +notmuch_config_get_filename_regex (notmuch_config_t *config);\r
72 +\r
73 +void\r
74 +notmuch_config_set_filename_regex (notmuch_config_t *config,\r
75 +                                 const char *filename_regex);\r
76 +\r
77 +const char *\r
78  notmuch_config_get_user_name (notmuch_config_t *config);\r
79  \r
80  void\r
81 diff --git a/notmuch-config.c b/notmuch-config.c\r
82 index 95430db..4189f03 100644\r
83 --- a/notmuch-config.c\r
84 +++ b/notmuch-config.c\r
85 @@ -31,11 +31,22 @@ static const char toplevel_config_comment[] =\r
86  static const char database_config_comment[] =\r
87      " Database configuration\n"\r
88      "\n"\r
89 -    " The only value supported here is 'path' which should be the top-level\n"\r
90 +    " The value 'path' should be the top-level\n"\r
91      " directory where your mail currently exists and to where mail will be\n"\r
92      " delivered in the future. Files should be individual email messages.\n"\r
93      " Notmuch will store its database within a sub-directory of the path\n"\r
94 -    " configured here named \".notmuch\".\n";\r
95 +    " configured here named \".notmuch\".\n"\r
96 +    "\n"\r
97 +    " The optional value 'filename_pattern' should be\n"\r
98 +    " a POSIX regular expression matching only those\n"\r
99 +    " filenames that will be checked for email\n"\r
100 +    " messages.  The match is against the last\n"\r
101 +    " component of the pathname only.  Anchors may be\n"\r
102 +    " used, and probably should be.  Typically, this\n"\r
103 +    " is used to match only files whose name is a\n"\r
104 +    " number ala MH, or to match only files in\n"\r
105 +    " standard maildir format.  The default pattern\n"\r
106 +    " matches anything.\n";\r
107  \r
108  static const char user_config_comment[] =\r
109      " User configuration\n"\r
110 @@ -58,6 +69,7 @@ struct _notmuch_config {\r
111      GKeyFile *key_file;\r
112  \r
113      char *database_path;\r
114 +    char *filename_regex;\r
115      char *user_name;\r
116      char *user_primary_email;\r
117      char **user_other_email;\r
118 @@ -151,6 +163,8 @@ get_username_from_passwd_file (void *ctx)\r
119   *\r
120   *     database_path:          $HOME/mail\r
121   *\r
122 + *     filename_pattern:       .*\r
123 + *\r
124   *     user_name:              From /etc/passwd\r
125   *\r
126   *     user_primary_mail:      $EMAIL variable if set, otherwise\r
127 @@ -195,6 +209,7 @@ notmuch_config_open (void *ctx,\r
128      config->key_file = g_key_file_new ();\r
129  \r
130      config->database_path = NULL;\r
131 +    config->filename_regex = NULL;\r
132      config->user_name = NULL;\r
133      config->user_primary_email = NULL;\r
134      config->user_other_email = NULL;\r
135 @@ -354,6 +369,34 @@ notmuch_config_set_database_path (notmuch_config_t *config,\r
136  }\r
137  \r
138  const char *\r
139 +notmuch_config_get_filename_regex (notmuch_config_t *config)\r
140 +{\r
141 +    char *regex;\r
142 +\r
143 +    if (config->filename_regex == NULL) {\r
144 +       regex = g_key_file_get_string (config->key_file,\r
145 +                                     "database", "filename_pattern", NULL);\r
146 +       if (regex) {\r
147 +           config->filename_regex = talloc_strdup (config, regex);\r
148 +           free (regex);\r
149 +       }\r
150 +    }\r
151 +\r
152 +    return config->filename_regex;\r
153 +}\r
154 +\r
155 +void\r
156 +notmuch_config_set_filename_regex (notmuch_config_t *config,\r
157 +                                  const char *filename_regex)\r
158 +{\r
159 +    g_key_file_set_string (config->key_file,\r
160 +                          "database", "filename_pattern", filename_regex);\r
161 +\r
162 +    talloc_free (config->filename_regex);\r
163 +    config->filename_regex = NULL;\r
164 +}\r
165 +\r
166 +const char *\r
167  notmuch_config_get_user_name (notmuch_config_t *config)\r
168  {\r
169      char *name;\r
170 diff --git a/notmuch-new.c b/notmuch-new.c\r
171 index f25c71f..531f9a3 100644\r
172 --- a/notmuch-new.c\r
173 +++ b/notmuch-new.c\r
174 @@ -21,6 +21,8 @@\r
175  #include "notmuch-client.h"\r
176  \r
177  #include <unistd.h>\r
178 +#include <sys/types.h>\r
179 +#include <regex.h>\r
180  \r
181  typedef struct _filename_node {\r
182      char *filename;\r
183 @@ -207,6 +209,7 @@ _entries_resemble_maildir (struct dirent **entries, int count)\r
184  static notmuch_status_t\r
185  add_files_recursive (notmuch_database_t *notmuch,\r
186                      const char *path,\r
187 +                    const regex_t *maybe_regex,\r
188                      add_files_state_t *state)\r
189  {\r
190      DIR *dir = NULL;\r
191 @@ -302,7 +305,7 @@ add_files_recursive (notmuch_database_t *notmuch,\r
192         }\r
193  \r
194         next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);\r
195 -       status = add_files_recursive (notmuch, next, state);\r
196 +       status = add_files_recursive (notmuch, next, maybe_regex, state);\r
197         if (status && ret == NOTMUCH_STATUS_SUCCESS)\r
198             ret = status;\r
199         talloc_free (next);\r
200 @@ -389,7 +392,7 @@ add_files_recursive (notmuch_database_t *notmuch,\r
201         }\r
202  \r
203         /* We're now looking at a regular file that doesn't yet exist\r
204 -        * in the database, so add it. */\r
205 +        * in the database. */\r
206         next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);\r
207  \r
208         state->processed_files++;\r
209 @@ -407,6 +410,14 @@ add_files_recursive (notmuch_database_t *notmuch,\r
210             fflush (stdout);\r
211         }\r
212  \r
213 +       /* Check against the regex (if any) for valid mail\r
214 +        * file names and bail on failure */\r
215 +       if (maybe_regex) {\r
216 +           status = regexec(maybe_regex, entry->d_name, 0, 0, 0);\r
217 +           if (status)\r
218 +               goto CLEANUP;\r
219 +       }\r
220 +\r
221         status = notmuch_database_add_message (notmuch, next, &message);\r
222         switch (status) {\r
223         /* success */\r
224 @@ -445,6 +456,7 @@ add_files_recursive (notmuch_database_t *notmuch,\r
225             message = NULL;\r
226         }\r
227  \r
228 +    CLEANUP:\r
229         if (do_add_files_print_progress) {\r
230             do_add_files_print_progress = 0;\r
231             add_files_print_progress (state);\r
232 @@ -509,6 +521,7 @@ add_files_recursive (notmuch_database_t *notmuch,\r
233  static notmuch_status_t\r
234  add_files (notmuch_database_t *notmuch,\r
235            const char *path,\r
236 +          const regex_t *maybe_regex,\r
237            add_files_state_t *state)\r
238  {\r
239      notmuch_status_t status;\r
240 @@ -546,7 +559,7 @@ add_files (notmuch_database_t *notmuch,\r
241         return NOTMUCH_STATUS_FILE_ERROR;\r
242      }\r
243  \r
244 -    status = add_files_recursive (notmuch, path, state);\r
245 +    status = add_files_recursive (notmuch, path, maybe_regex, state);\r
246  \r
247      if (timer_is_active) {\r
248         /* Now stop the timer. */\r
249 @@ -713,6 +726,9 @@ notmuch_new_command (void *ctx, int argc, char *argv[])\r
250      int ret = 0;\r
251      struct stat st;\r
252      const char *db_path;\r
253 +    const char *filename_regex;\r
254 +    regex_t regex;\r
255 +    const regex_t *maybe_regex = 0;\r
256      char *dot_notmuch_path;\r
257      struct sigaction action;\r
258      _filename_node_t *f;\r
259 @@ -738,6 +754,17 @@ notmuch_new_command (void *ctx, int argc, char *argv[])\r
260  \r
261      db_path = notmuch_config_get_database_path (config);\r
262  \r
263 +    filename_regex = notmuch_config_get_filename_regex (config);\r
264 +    if (filename_regex) {\r
265 +       status = regcomp(&regex, filename_regex, REG_EXTENDED | REG_NOSUB);\r
266 +       if (status) {\r
267 +           fprintf (stderr, "Note: Ignoring bad filename_pattern "\r
268 +                    "in config file: %s\n", filename_regex);\r
269 +       } else {\r
270 +           maybe_regex = &regex;\r
271 +       }\r
272 +    }\r
273 +\r
274      dot_notmuch_path = talloc_asprintf (ctx, "%s/%s", db_path, ".notmuch");\r
275  \r
276      if (stat (dot_notmuch_path, &st)) {\r
277 @@ -791,7 +818,7 @@ notmuch_new_command (void *ctx, int argc, char *argv[])\r
278      add_files_state.removed_files = _filename_list_create (ctx);\r
279      add_files_state.removed_directories = _filename_list_create (ctx);\r
280  \r
281 -    ret = add_files (notmuch, db_path, &add_files_state);\r
282 +    ret = add_files (notmuch, db_path, maybe_regex, &add_files_state);\r
283  \r
284      removed_files = 0;\r
285      renamed_files = 0;\r
286 -- \r
287 1.6.6.1\r
288 \r