[RFC PATCH 3/5] util: add a homebrew scandir implementation
[notmuch-archives.git] / c2 / 8b70bc8eee9b25f5e44ed1044a01c24063c15f
1 Return-Path: <sojkam1@fel.cvut.cz>\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 F1414431FBC\r
6         for <notmuch@notmuchmail.org>; Sun,  5 Oct 2014 13:52:47 -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\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
12         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 vLaLYUE6gpS0 for <notmuch@notmuchmail.org>;\r
16         Sun,  5 Oct 2014 13:52:41 -0700 (PDT)\r
17 Received: from max.feld.cvut.cz (max.feld.cvut.cz [147.32.192.36])\r
18         by olra.theworths.org (Postfix) with ESMTP id 8F637431FBD\r
19         for <notmuch@notmuchmail.org>; Sun,  5 Oct 2014 13:52:38 -0700 (PDT)\r
20 Received: from localhost (unknown [192.168.200.7])\r
21         by max.feld.cvut.cz (Postfix) with ESMTP id C7DF05CCEFA\r
22         for <notmuch@notmuchmail.org>; Sun,  5 Oct 2014 22:52:35 +0200 (CEST)\r
23 X-Virus-Scanned: IMAP STYX AMAVIS\r
24 Received: from max.feld.cvut.cz ([192.168.200.1])\r
25         by localhost (styx.feld.cvut.cz [192.168.200.7]) (amavisd-new,\r
26         port 10044) with ESMTP id hDdYsKRwVOMs for <notmuch@notmuchmail.org>;\r
27         Sun,  5 Oct 2014 22:52:32 +0200 (CEST)\r
28 Received: from imap.feld.cvut.cz (imap.feld.cvut.cz [147.32.192.34])\r
29         by max.feld.cvut.cz (Postfix) with ESMTP id 5EA8A5CCEF9\r
30         for <notmuch@notmuchmail.org>; Sun,  5 Oct 2014 22:52:31 +0200 (CEST)\r
31 Received: from wsh by steelpick.2x.cz with local (Exim 4.84)\r
32         (envelope-from <sojkam1@fel.cvut.cz>)\r
33         id 1Xasn2-0005E4-R1; Sun, 05 Oct 2014 22:52:24 +0200\r
34 From: Michal Sojka <sojkam1@fel.cvut.cz>\r
35 To: notmuch@notmuchmail.org\r
36 Subject: [PATCH v2 2/4] cli: Extend the search command for --output=addresses\r
37         and similar\r
38 Date: Sun,  5 Oct 2014 22:51:57 +0200\r
39 Message-Id: <1412542319-20017-3-git-send-email-sojkam1@fel.cvut.cz>\r
40 X-Mailer: git-send-email 2.1.1\r
41 In-Reply-To: <1412542319-20017-1-git-send-email-sojkam1@fel.cvut.cz>\r
42 References: <874mvqxrnp.fsf@nikula.org>\r
43         <1412542319-20017-1-git-send-email-sojkam1@fel.cvut.cz>\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: Sun, 05 Oct 2014 20:52:48 -0000\r
60 \r
61 The new outputs allow printing senders, recipients or both of matching\r
62 messages.\r
63 \r
64 This code based on a patch from Jani Nikula.\r
65 ---\r
66  completion/notmuch-completion.bash |   2 +-\r
67  completion/notmuch-completion.zsh  |   3 +-\r
68  doc/man1/notmuch-search.rst        |  22 +++++++-\r
69  notmuch-search.c                   | 100 ++++++++++++++++++++++++++++++++++---\r
70  test/T090-search-output.sh         |  64 ++++++++++++++++++++++++\r
71  5 files changed, 182 insertions(+), 9 deletions(-)\r
72 \r
73 diff --git a/completion/notmuch-completion.bash b/completion/notmuch-completion.bash\r
74 index 0571dc9..c37ddf5 100644\r
75 --- a/completion/notmuch-completion.bash\r
76 +++ b/completion/notmuch-completion.bash\r
77 @@ -294,7 +294,7 @@ _notmuch_search()\r
78             return\r
79             ;;\r
80         --output)\r
81 -           COMPREPLY=( $( compgen -W "summary threads messages files tags" -- "${cur}" ) )\r
82 +           COMPREPLY=( $( compgen -W "summary threads messages files tags sender recipients addresses" -- "${cur}" ) )\r
83             return\r
84             ;;\r
85         --sort)\r
86 diff --git a/completion/notmuch-completion.zsh b/completion/notmuch-completion.zsh\r
87 index 67a9aba..bff8fd5 100644\r
88 --- a/completion/notmuch-completion.zsh\r
89 +++ b/completion/notmuch-completion.zsh\r
90 @@ -52,7 +52,8 @@ _notmuch_search()\r
91    _arguments -s : \\r
92      '--max-threads=[display only the first x threads from the search results]:number of threads to show: ' \\r
93      '--first=[omit the first x threads from the search results]:number of threads to omit: ' \\r
94 -    '--sort=[sort results]:sorting:((newest-first\:"reverse chronological order" oldest-first\:"chronological order"))'\r
95 +    '--sort=[sort results]:sorting:((newest-first\:"reverse chronological order" oldest-first\:"chronological order"))' \\r
96 +    '--output=[select what to output]:output:((summary threads messages files tags sender recipients addresses))'\r
97  }\r
98  \r
99  _notmuch()\r
100 diff --git a/doc/man1/notmuch-search.rst b/doc/man1/notmuch-search.rst\r
101 index 90160f2..3447820 100644\r
102 --- a/doc/man1/notmuch-search.rst\r
103 +++ b/doc/man1/notmuch-search.rst\r
104 @@ -35,7 +35,7 @@ Supported options for **search** include\r
105          intended for programs that invoke **notmuch(1)** internally. If\r
106          omitted, the latest supported version will be used.\r
107  \r
108 -    ``--output=(summary|threads|messages|files|tags)``\r
109 +    ``--output=(summary|threads|messages|files|tags|sender|recipients|addresses)``\r
110  \r
111          **summary**\r
112              Output a summary of each thread with any message matching\r
113 @@ -78,6 +78,26 @@ Supported options for **search** include\r
114              by null characters (--format=text0), as a JSON array\r
115              (--format=json), or as an S-Expression list (--format=sexp).\r
116  \r
117 +       **sender**\r
118 +            Output all addresses from the *From* header that appear on\r
119 +            any message matching the search terms, either one per line\r
120 +            (--format=text), separated by null characters\r
121 +            (--format=text0), as a JSON array (--format=json), or as\r
122 +            an S-Expression list (--format=sexp).\r
123 +\r
124 +           Note: Searching for **sender** should be much faster than\r
125 +           searching for **recipients** or **addresses**, because\r
126 +           sender addresses are cached directly in the database\r
127 +           whereas other addresses need to be fetched from message\r
128 +           files.\r
129 +\r
130 +       **recipients**\r
131 +            Like **sender** but for addresses from *To*, *Cc* and\r
132 +           *Bcc* headers.\r
133 +\r
134 +       **addresses**\r
135 +           Like **sender** and **recipients** together.\r
136 +\r
137      ``--sort=``\ (**newest-first**\ \|\ **oldest-first**)\r
138          This option can be used to present results in either\r
139          chronological order (**oldest-first**) or reverse chronological\r
140 diff --git a/notmuch-search.c b/notmuch-search.c\r
141 index 5ac2a26..0614f10 100644\r
142 --- a/notmuch-search.c\r
143 +++ b/notmuch-search.c\r
144 @@ -23,11 +23,14 @@\r
145  #include "string-util.h"\r
146  \r
147  typedef enum {\r
148 -    OUTPUT_SUMMARY,\r
149 -    OUTPUT_THREADS,\r
150 -    OUTPUT_MESSAGES,\r
151 -    OUTPUT_FILES,\r
152 -    OUTPUT_TAGS\r
153 +    OUTPUT_SUMMARY     = 1 << 0,\r
154 +    OUTPUT_THREADS     = 1 << 1,\r
155 +    OUTPUT_MESSAGES    = 1 << 2,\r
156 +    OUTPUT_FILES       = 1 << 3,\r
157 +    OUTPUT_TAGS                = 1 << 4,\r
158 +    OUTPUT_SENDER      = 1 << 5,\r
159 +    OUTPUT_RECIPIENTS  = 1 << 6,\r
160 +    OUTPUT_ADDRESSES   = OUTPUT_SENDER | OUTPUT_RECIPIENTS,\r
161  } output_t;\r
162  \r
163  typedef struct {\r
164 @@ -220,6 +223,67 @@ do_search_threads (search_options_t *o)\r
165      return 0;\r
166  }\r
167  \r
168 +static void\r
169 +print_address_list (const search_options_t *o, InternetAddressList *list)\r
170 +{\r
171 +    InternetAddress *address;\r
172 +    int i;\r
173 +\r
174 +    for (i = 0; i < internet_address_list_length (list); i++) {\r
175 +       address = internet_address_list_get_address (list, i);\r
176 +       if (INTERNET_ADDRESS_IS_GROUP (address)) {\r
177 +           InternetAddressGroup *group;\r
178 +           InternetAddressList *group_list;\r
179 +\r
180 +           group = INTERNET_ADDRESS_GROUP (address);\r
181 +           group_list = internet_address_group_get_members (group);\r
182 +           if (group_list == NULL)\r
183 +               continue;\r
184 +\r
185 +           print_address_list (o, group_list);\r
186 +       } else {\r
187 +           InternetAddressMailbox *mailbox;\r
188 +           const char *name;\r
189 +           const char *addr;\r
190 +           char *full_address;\r
191 +\r
192 +           mailbox = INTERNET_ADDRESS_MAILBOX (address);\r
193 +\r
194 +           name = internet_address_get_name (address);\r
195 +           addr = internet_address_mailbox_get_addr (mailbox);\r
196 +\r
197 +           if (name && *name)\r
198 +               full_address = talloc_asprintf (o->format, "%s <%s>", name, addr);\r
199 +           else\r
200 +               full_address = talloc_strdup (o->format, addr);\r
201 +\r
202 +           if (!full_address) {\r
203 +               fprintf (stderr, "Error: out of memory\n");\r
204 +               break;\r
205 +           }\r
206 +           o->format->string (o->format, full_address);\r
207 +           o->format->separator (o->format);\r
208 +\r
209 +           talloc_free (full_address);\r
210 +       }\r
211 +    }\r
212 +}\r
213 +\r
214 +static void\r
215 +print_address_string (const search_options_t *o, const char *recipients)\r
216 +{\r
217 +    InternetAddressList *list;\r
218 +\r
219 +    if (recipients == NULL)\r
220 +       return;\r
221 +\r
222 +    list = internet_address_list_parse_string (recipients);\r
223 +    if (list == NULL)\r
224 +       return;\r
225 +\r
226 +    print_address_list (o, list);\r
227 +}\r
228 +\r
229  static int\r
230  do_search_messages (search_options_t *o)\r
231  {\r
232 @@ -266,11 +330,29 @@ do_search_messages (search_options_t *o)\r
233             \r
234             notmuch_filenames_destroy( filenames );\r
235  \r
236 -       } else { /* output == OUTPUT_MESSAGES */\r
237 +       } else if (o->output == OUTPUT_MESSAGES) {\r
238             format->set_prefix (format, "id");\r
239             format->string (format,\r
240                             notmuch_message_get_message_id (message));\r
241             format->separator (format);\r
242 +       } else {\r
243 +           if (o->output & OUTPUT_SENDER) {\r
244 +               const char *addrs;\r
245 +\r
246 +               addrs = notmuch_message_get_header (message, "from");\r
247 +               print_address_string (o, addrs);\r
248 +           }\r
249 +\r
250 +           if (o->output & OUTPUT_RECIPIENTS) {\r
251 +               const char *hdrs[] = { "to", "cc", "bcc" };\r
252 +               const char *addrs;\r
253 +               size_t j;\r
254 +\r
255 +               for (j = 0; j < ARRAY_SIZE (hdrs); j++) {\r
256 +                   addrs = notmuch_message_get_header (message, hdrs[j]);\r
257 +                   print_address_string (o, addrs);\r
258 +               }\r
259 +           }\r
260         }\r
261  \r
262         notmuch_message_destroy (message);\r
263 @@ -370,6 +452,9 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
264           (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },\r
265                                   { "threads", OUTPUT_THREADS },\r
266                                   { "messages", OUTPUT_MESSAGES },\r
267 +                                 { "sender", OUTPUT_SENDER },\r
268 +                                 { "recipients", OUTPUT_RECIPIENTS },\r
269 +                                 { "addresses", OUTPUT_ADDRESSES },\r
270                                   { "files", OUTPUT_FILES },\r
271                                   { "tags", OUTPUT_TAGS },\r
272                                   { 0, 0 } } },\r
273 @@ -461,6 +546,9 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
274         ret = do_search_threads (&o);\r
275         break;\r
276      case OUTPUT_MESSAGES:\r
277 +    case OUTPUT_SENDER:\r
278 +    case OUTPUT_RECIPIENTS:\r
279 +    case OUTPUT_ADDRESSES:\r
280      case OUTPUT_FILES:\r
281         ret = do_search_messages (&o);\r
282         break;\r
283 diff --git a/test/T090-search-output.sh b/test/T090-search-output.sh\r
284 index 947d572..5458de1 100755\r
285 --- a/test/T090-search-output.sh\r
286 +++ b/test/T090-search-output.sh\r
287 @@ -387,6 +387,70 @@ cat <<EOF >EXPECTED\r
288  EOF\r
289  test_expect_equal_file OUTPUT EXPECTED\r
290  \r
291 +test_begin_subtest "--output=sender"\r
292 +notmuch search --output=sender '*' | sort | uniq --count >OUTPUT\r
293 +cat <<EOF >EXPECTED\r
294 +      1 Adrian Perez de Castro <aperez@igalia.com>\r
295 +      2 Alex Botero-Lowry <alex.boterolowry@gmail.com>\r
296 +      4 Alexander Botero-Lowry <alex.boterolowry@gmail.com>\r
297 +      1 Aron Griffis <agriffis@n01se.net>\r
298 +     12 Carl Worth <cworth@cworth.org>\r
299 +      1 Chris Wilson <chris@chris-wilson.co.uk>\r
300 +      1 François Boulogne <boulogne.f@gmail.com>\r
301 +      1 Ingmar Vanhassel <ingmar@exherbo.org>\r
302 +      1 Israel Herraiz <isra@herraiz.org>\r
303 +      4 Jan Janak <jan@ryngle.com>\r
304 +      2 Jjgod Jiang <gzjjgod@gmail.com>\r
305 +      7 Keith Packard <keithp@keithp.com>\r
306 +      5 Lars Kellogg-Stedman <lars@seas.harvard.edu>\r
307 +      5 Mikhail Gusarov <dottedmag@dottedmag.net>\r
308 +      1 Olivier Berger <olivier.berger@it-sudparis.eu>\r
309 +      1 Rolland Santimano <rollandsantimano@yahoo.com>\r
310 +      3 Stewart Smith <stewart@flamingspork.com>\r
311 +EOF\r
312 +test_expect_equal_file OUTPUT EXPECTED\r
313 +\r
314 +test_begin_subtest "--output=recipients"\r
315 +notmuch search --output=recipients '*' | sort | uniq --count >OUTPUT\r
316 +cat <<EOF >EXPECTED\r
317 +      1 Allan McRae <allan@archlinux.org>\r
318 +      1 Discussion about the Arch User Repository (AUR) <aur-general@archlinux.org>\r
319 +      1 Keith Packard <keithp@keithp.com>\r
320 +      1 Mikhail Gusarov <dottedmag@dottedmag.net>\r
321 +      2 notmuch <notmuch@notmuchmail.org>\r
322 +     48 notmuch@notmuchmail.org\r
323 +      1 olivier.berger@it-sudparis.eu\r
324 +EOF\r
325 +test_expect_equal_file OUTPUT EXPECTED\r
326 +\r
327 +test_begin_subtest "--output=addresses"\r
328 +notmuch search --output=addresses '*' | sort | uniq --count >OUTPUT\r
329 +cat <<EOF >EXPECTED\r
330 +      1 Adrian Perez de Castro <aperez@igalia.com>\r
331 +      2 Alex Botero-Lowry <alex.boterolowry@gmail.com>\r
332 +      4 Alexander Botero-Lowry <alex.boterolowry@gmail.com>\r
333 +      1 Allan McRae <allan@archlinux.org>\r
334 +      1 Aron Griffis <agriffis@n01se.net>\r
335 +     12 Carl Worth <cworth@cworth.org>\r
336 +      1 Chris Wilson <chris@chris-wilson.co.uk>\r
337 +      1 Discussion about the Arch User Repository (AUR) <aur-general@archlinux.org>\r
338 +      1 François Boulogne <boulogne.f@gmail.com>\r
339 +      1 Ingmar Vanhassel <ingmar@exherbo.org>\r
340 +      1 Israel Herraiz <isra@herraiz.org>\r
341 +      4 Jan Janak <jan@ryngle.com>\r
342 +      2 Jjgod Jiang <gzjjgod@gmail.com>\r
343 +      8 Keith Packard <keithp@keithp.com>\r
344 +      5 Lars Kellogg-Stedman <lars@seas.harvard.edu>\r
345 +      6 Mikhail Gusarov <dottedmag@dottedmag.net>\r
346 +      1 Olivier Berger <olivier.berger@it-sudparis.eu>\r
347 +      1 Rolland Santimano <rollandsantimano@yahoo.com>\r
348 +      3 Stewart Smith <stewart@flamingspork.com>\r
349 +      2 notmuch <notmuch@notmuchmail.org>\r
350 +     48 notmuch@notmuchmail.org\r
351 +      1 olivier.berger@it-sudparis.eu\r
352 +EOF\r
353 +test_expect_equal_file OUTPUT EXPECTED\r
354 +\r
355  test_begin_subtest "sanitize output for quoted-printable line-breaks in author and subject"\r
356  add_message "[subject]='two =?ISO-8859-1?Q?line=0A_subject?=\r
357         headers'"\r
358 -- \r
359 2.1.1\r
360 \r