[Patch v4 9/9] ruby: use new query_search API
[notmuch-archives.git] / 33 / 25d0100606c09cca99d8d0be95e339eb61a3fb
1 Return-Path: <tomi.ollila@iki.fi>\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 13E5E431FD8\r
6         for <notmuch@notmuchmail.org>; Fri, 24 Oct 2014 02:38:50 -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 nJf74m1qpBbu for <notmuch@notmuchmail.org>;\r
16         Fri, 24 Oct 2014 02:38:41 -0700 (PDT)\r
17 Received: from guru.guru-group.fi (guru.guru-group.fi [46.183.73.34])\r
18         by olra.theworths.org (Postfix) with ESMTP id A9723431FBD\r
19         for <notmuch@notmuchmail.org>; Fri, 24 Oct 2014 02:38:40 -0700 (PDT)\r
20 Received: from guru.guru-group.fi (localhost [IPv6:::1])\r
21         by guru.guru-group.fi (Postfix) with ESMTP id 8F83110008C;\r
22         Fri, 24 Oct 2014 12:38:18 +0300 (EEST)\r
23 From: Tomi Ollila <tomi.ollila@iki.fi>\r
24 To: Mark Walters <markwalters1009@gmail.com>,\r
25         Michal Sojka <sojkam1@fel.cvut.cz>, notmuch@notmuchmail.org\r
26 Subject: Re: [PATCH v3 3/4] cli: Extend the search command\r
27         for     --output={sender, recipients}\r
28 In-Reply-To: <87egtzazs8.fsf@qmul.ac.uk>\r
29 References: <87zjd51phx.fsf@steelpick.2x.cz>\r
30         <1413150093-8383-1-git-send-email-sojkam1@fel.cvut.cz>\r
31         <1413150093-8383-4-git-send-email-sojkam1@fel.cvut.cz>\r
32         <87egtzazs8.fsf@qmul.ac.uk>\r
33 User-Agent: Notmuch/0.18.1+130~ga61922f (http://notmuchmail.org) Emacs/24.3.1\r
34         (x86_64-unknown-linux-gnu)\r
35 X-Face: HhBM'cA~<r"^Xv\KRN0P{vn'Y"Kd;zg_y3S[4)KSN~s?O\"QPoL\r
36         $[Xv_BD:i/F$WiEWax}R(MPS`^UaptOGD`*/=@\1lKoVa9tnrg0TW?"r7aRtgk[F\r
37         !)g;OY^,BjTbr)Np:%c_o'jj,Z\r
38 Date: Fri, 24 Oct 2014 12:38:18 +0300\r
39 Message-ID: <m2bnp1erjp.fsf@guru.guru-group.fi>\r
40 MIME-Version: 1.0\r
41 Content-Type: text/plain; charset=utf-8\r
42 Content-Transfer-Encoding: quoted-printable\r
43 X-BeenThere: notmuch@notmuchmail.org\r
44 X-Mailman-Version: 2.1.13\r
45 Precedence: list\r
46 List-Id: "Use and development of the notmuch mail system."\r
47         <notmuch.notmuchmail.org>\r
48 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
49         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
50 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
51 List-Post: <mailto:notmuch@notmuchmail.org>\r
52 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
53 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
54         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
55 X-List-Received-Date: Fri, 24 Oct 2014 09:38:50 -0000\r
56 \r
57 On Thu, Oct 23 2014, Mark Walters <markwalters1009@gmail.com> wrote:\r
58 \r
59 > On Sun, 12 Oct 2014, Michal Sojka <sojkam1@fel.cvut.cz> wrote:\r
60 >> The new outputs allow printing senders, recipients or both of matching\r
61 >> messages. The --output option is converted from "keyword" argument to\r
62 >> "flags" argument, which means that the user can use --output=3Dsender and\r
63 >> --output=3Drecipients simultaneously, to print both. Other combinations\r
64 >> produce an error.\r
65 >>\r
66 >> ...\r
67 >>\r
68 >> +static void\r
69 >> +print_address_list (const search_options_t *o, InternetAddressList *lis=\r
70 t)\r
71 >> +{\r
72 >> +    InternetAddress *address;\r
73 >> +    int i;\r
74 >> +\r
75 >> +    for (i =3D 0; i < internet_address_list_length (list); i++) {\r
76 >> +    address =3D internet_address_list_get_address (list, i);\r
77 >> +    if (INTERNET_ADDRESS_IS_GROUP (address)) {\r
78 >> +        InternetAddressGroup *group;\r
79 >> +        InternetAddressList *group_list;\r
80 >> +\r
81 >> +        group =3D INTERNET_ADDRESS_GROUP (address);\r
82 >> +        group_list =3D internet_address_group_get_members (group);\r
83 >> +        if (group_list =3D=3D NULL)\r
84 >> +            continue;\r
85 >> +\r
86 >> +        print_address_list (o, group_list);\r
87 >> +    } else {\r
88 >> +        InternetAddressMailbox *mailbox;\r
89 >> +        const char *name;\r
90 >> +        const char *addr;\r
91 >> +        char *full_address;\r
92 >> +\r
93 >> +        mailbox =3D INTERNET_ADDRESS_MAILBOX (address);\r
94 >> +\r
95 >> +        name =3D internet_address_get_name (address);\r
96 >> +        addr =3D internet_address_mailbox_get_addr (mailbox);\r
97 >> +\r
98 >> +        if (name && *name)\r
99 >> +            full_address =3D talloc_asprintf (o->format, "%s <%s>", name, addr);\r
100 >> +        else\r
101 >> +            full_address =3D talloc_strdup (o->format, addr);\r
102 >> +\r
103 >> +        if (!full_address) {\r
104 >> +            fprintf (stderr, "Error: out of memory\n");\r
105 >> +            break;\r
106 >> +        }\r
107 >> +        o->format->string (o->format, full_address);\r
108 >> +        o->format->separator (o->format);\r
109 >> +\r
110 >> +        talloc_free (full_address);\r
111 >\r
112 > Thinking about this some more how about printing the name and address as\r
113 > a structured pair/map (at least for all cases except text/text0 output):\r
114 > something like (in JSON)\r
115 > [name: "John Doe" address: "john.doe@example.com"]\r
116 >\r
117 > It seems wrong to me to go to the effort of separating them in the C and\r
118 > then combining them in the output.\r
119 >\r
120 > This could also help with the questions about uniqueness. If the client\r
121 > can get the data ready parsed into name/address then it can deal with\r
122 > much of the uniqueness itself.\r
123 \r
124 In that case client can also filter based on some substring, reducing the\r
125 memory requirements...\r
126 \r
127 >\r
128 > My preference would be for the default to print one line for each\r
129 > distinct full_address, and then any filter-by options to refine from\r
130 > there.\r
131 \r
132 Hmm, now I cannot decide whether this or just print out all addresses of\r
133 messages, or do this distinct full_address output -- it looks like all\r
134 other --output options prints unique lines, but there is potential of=20\r
135 quite a lot of memory usage there...\r
136 \r
137 ... probably the memory usage is not problem there, OOM-killer eventually\r
138 does it's job if necessary (!) (but machine may be slow (and trashing) for\r
139 a while (just thinking out loud))\r
140 \r
141 (!) but could we have general filter option for search to drop data before\r
142 it is even considered for caching! -- maybe later ?\r
143 \r
144 \r
145 > One other advantage of structuring the output is that it is extensible:\r
146 > for example, at some later stage, we could include a "count" in the map\r
147 > allowing the client can pick the most popular variant.\r
148 \r
149 , and in this case notmuch cannot print any output until the full address\r
150 list is gathered... :D\r
151 \r
152 >\r
153 > Best wishes\r
154 >\r
155 > Mark\r
156 \r
157 Tomi\r
158 \r
159 >\r
160 >\r
161 >\r
162 >\r
163 >> +    }\r
164 >> +    }\r
165 >> +}\r
166 >> +\r
167 >> +static void\r
168 >> +print_address_string (const search_options_t *o, const char *recipients)\r
169 >> +{\r
170 >> +    InternetAddressList *list;\r
171 >> +\r
172 >> +    if (recipients =3D=3D NULL)\r
173 >> +    return;\r
174 >> +\r
175 >> +    list =3D internet_address_list_parse_string (recipients);\r
176 >> +    if (list =3D=3D NULL)\r
177 >> +    return;\r
178 >> +\r
179 >> +    print_address_list (o, list);\r
180 >> +}\r
181 >> +\r
182 >>  static int\r
183 >>  do_search_messages (search_options_t *o)\r
184 >>  {\r
185 >> @@ -266,11 +330,29 @@ do_search_messages (search_options_t *o)\r
186 >>=20=20=09=20=20=20=20\r
187 >>          notmuch_filenames_destroy( filenames );\r
188 >>=20=20\r
189 >> -    } else { /* output =3D=3D OUTPUT_MESSAGES */\r
190 >> +    } else if (o->output =3D=3D OUTPUT_MESSAGES) {\r
191 >>          format->set_prefix (format, "id");\r
192 >>          format->string (format,\r
193 >>                          notmuch_message_get_message_id (message));\r
194 >>          format->separator (format);\r
195 >> +    } else {\r
196 >> +        if (o->output & OUTPUT_SENDER) {\r
197 >> +            const char *addrs;\r
198 >> +\r
199 >> +            addrs =3D notmuch_message_get_header (message, "from");\r
200 >> +            print_address_string (o, addrs);\r
201 >> +        }\r
202 >> +\r
203 >> +        if (o->output & OUTPUT_RECIPIENTS) {\r
204 >> +            const char *hdrs[] =3D { "to", "cc", "bcc" };\r
205 >> +            const char *addrs;\r
206 >> +            size_t j;\r
207 >> +\r
208 >> +            for (j =3D 0; j < ARRAY_SIZE (hdrs); j++) {\r
209 >> +                addrs =3D notmuch_message_get_header (message, hdrs[j]);\r
210 >> +                print_address_string (o, addrs);\r
211 >> +            }\r
212 >> +        }\r
213 >>      }\r
214 >>=20=20\r
215 >>      notmuch_message_destroy (message);\r
216 >> @@ -337,7 +419,7 @@ notmuch_search_command (notmuch_config_t *config, in=\r
217 t argc, char *argv[])\r
218 >>      notmuch_database_t *notmuch;\r
219 >>      search_options_t o =3D {\r
220 >>      .sort =3D NOTMUCH_SORT_NEWEST_FIRST,\r
221 >> -    .output =3D OUTPUT_SUMMARY,\r
222 >> +    .output =3D 0,\r
223 >>      .offset =3D 0,\r
224 >>      .limit =3D -1, /* unlimited */\r
225 >>      .dupe =3D -1,\r
226 >> @@ -366,10 +448,12 @@ notmuch_search_command (notmuch_config_t *config, =\r
227 int argc, char *argv[])\r
228 >>                                { "text0", NOTMUCH_FORMAT_TEXT0 },\r
229 >>                                { 0, 0 } } },\r
230 >>      { NOTMUCH_OPT_INT, &notmuch_format_version, "format-version", 0, 0 },\r
231 >> -    { NOTMUCH_OPT_KEYWORD, &o.output, "output", 'o',\r
232 >> +    { NOTMUCH_OPT_KEYWORD_FLAGS, &o.output, "output", 'o',\r
233 >>        (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },\r
234 >>                                { "threads", OUTPUT_THREADS },\r
235 >>                                { "messages", OUTPUT_MESSAGES },\r
236 >> +                              { "sender", OUTPUT_SENDER },\r
237 >> +                              { "recipients", OUTPUT_RECIPIENTS },\r
238 >>                                { "files", OUTPUT_FILES },\r
239 >>                                { "tags", OUTPUT_TAGS },\r
240 >>                                { 0, 0 } } },\r
241 >> @@ -389,6 +473,9 @@ notmuch_search_command (notmuch_config_t *config, in=\r
242 t argc, char *argv[])\r
243 >>      if (opt_index < 0)\r
244 >>      return EXIT_FAILURE;\r
245 >>=20=20\r
246 >> +    if (! o.output)\r
247 >> +    o.output =3D OUTPUT_SUMMARY;\r
248 >> +\r
249 >>      switch (format_sel) {\r
250 >>      case NOTMUCH_FORMAT_TEXT:\r
251 >>      o.format =3D sprinter_text_create (config, stdout);\r
252 >> @@ -455,18 +542,23 @@ notmuch_search_command (notmuch_config_t *config, =\r
253 int argc, char *argv[])\r
254 >>      }\r
255 >>=20=20\r
256 >>      switch (o.output) {\r
257 >> -    default:\r
258 >>      case OUTPUT_SUMMARY:\r
259 >>      case OUTPUT_THREADS:\r
260 >>      ret =3D do_search_threads (&o);\r
261 >>      break;\r
262 >>      case OUTPUT_MESSAGES:\r
263 >> +    case OUTPUT_SENDER:\r
264 >> +    case OUTPUT_RECIPIENTS:\r
265 >> +    case OUTPUT_ADDRESSES:\r
266 >>      case OUTPUT_FILES:\r
267 >>      ret =3D do_search_messages (&o);\r
268 >>      break;\r
269 >>      case OUTPUT_TAGS:\r
270 >>      ret =3D do_search_tags (notmuch, o.format, o.query);\r
271 >>      break;\r
272 >> +    default:\r
273 >> +    fprintf (stderr, "Error: the combination of outputs is not supported.\=\r
274 n");\r
275 >> +    ret =3D 1;\r
276 >>      }\r
277 >>=20=20\r
278 >>      notmuch_query_destroy (o.query);\r
279 >> diff --git a/test/T090-search-output.sh b/test/T090-search-output.sh\r
280 >> index 947d572..e696c01 100755\r
281 >> --- a/test/T090-search-output.sh\r
282 >> +++ b/test/T090-search-output.sh\r
283 >> @@ -387,6 +387,70 @@ cat <<EOF >EXPECTED\r
284 >>  EOF\r
285 >>  test_expect_equal_file OUTPUT EXPECTED\r
286 >>=20=20\r
287 >> +test_begin_subtest "--output=3Dsender"\r
288 >> +notmuch search --output=3Dsender '*' | sort | uniq --count >OUTPUT\r
289 >> +cat <<EOF >EXPECTED\r
290 >> +      1 Adrian Perez de Castro <aperez@igalia.com>\r
291 >> +      2 Alex Botero-Lowry <alex.boterolowry@gmail.com>\r
292 >> +      4 Alexander Botero-Lowry <alex.boterolowry@gmail.com>\r
293 >> +      1 Aron Griffis <agriffis@n01se.net>\r
294 >> +     12 Carl Worth <cworth@cworth.org>\r
295 >> +      1 Chris Wilson <chris@chris-wilson.co.uk>\r
296 >> +      1 Fran=C3=A7ois Boulogne <boulogne.f@gmail.com>\r
297 >> +      1 Ingmar Vanhassel <ingmar@exherbo.org>\r
298 >> +      1 Israel Herraiz <isra@herraiz.org>\r
299 >> +      4 Jan Janak <jan@ryngle.com>\r
300 >> +      2 Jjgod Jiang <gzjjgod@gmail.com>\r
301 >> +      7 Keith Packard <keithp@keithp.com>\r
302 >> +      5 Lars Kellogg-Stedman <lars@seas.harvard.edu>\r
303 >> +      5 Mikhail Gusarov <dottedmag@dottedmag.net>\r
304 >> +      1 Olivier Berger <olivier.berger@it-sudparis.eu>\r
305 >> +      1 Rolland Santimano <rollandsantimano@yahoo.com>\r
306 >> +      3 Stewart Smith <stewart@flamingspork.com>\r
307 >> +EOF\r
308 >> +test_expect_equal_file OUTPUT EXPECTED\r
309 >> +\r
310 >> +test_begin_subtest "--output=3Drecipients"\r
311 >> +notmuch search --output=3Drecipients '*' | sort | uniq --count >OUTPUT\r
312 >> +cat <<EOF >EXPECTED\r
313 >> +      1 Allan McRae <allan@archlinux.org>\r
314 >> +      1 Discussion about the Arch User Repository (AUR) <aur-general@ar=\r
315 chlinux.org>\r
316 >> +      1 Keith Packard <keithp@keithp.com>\r
317 >> +      1 Mikhail Gusarov <dottedmag@dottedmag.net>\r
318 >> +      2 notmuch <notmuch@notmuchmail.org>\r
319 >> +     48 notmuch@notmuchmail.org\r
320 >> +      1 olivier.berger@it-sudparis.eu\r
321 >> +EOF\r
322 >> +test_expect_equal_file OUTPUT EXPECTED\r
323 >> +\r
324 >> +test_begin_subtest "--output=3Dsender --output=3Drecipients"\r
325 >> +notmuch search --output=3Dsender --output=3Drecipients '*' | sort | uni=\r
326 q --count >OUTPUT\r
327 >> +cat <<EOF >EXPECTED\r
328 >> +      1 Adrian Perez de Castro <aperez@igalia.com>\r
329 >> +      2 Alex Botero-Lowry <alex.boterolowry@gmail.com>\r
330 >> +      4 Alexander Botero-Lowry <alex.boterolowry@gmail.com>\r
331 >> +      1 Allan McRae <allan@archlinux.org>\r
332 >> +      1 Aron Griffis <agriffis@n01se.net>\r
333 >> +     12 Carl Worth <cworth@cworth.org>\r
334 >> +      1 Chris Wilson <chris@chris-wilson.co.uk>\r
335 >> +      1 Discussion about the Arch User Repository (AUR) <aur-general@ar=\r
336 chlinux.org>\r
337 >> +      1 Fran=C3=A7ois Boulogne <boulogne.f@gmail.com>\r
338 >> +      1 Ingmar Vanhassel <ingmar@exherbo.org>\r
339 >> +      1 Israel Herraiz <isra@herraiz.org>\r
340 >> +      4 Jan Janak <jan@ryngle.com>\r
341 >> +      2 Jjgod Jiang <gzjjgod@gmail.com>\r
342 >> +      8 Keith Packard <keithp@keithp.com>\r
343 >> +      5 Lars Kellogg-Stedman <lars@seas.harvard.edu>\r
344 >> +      6 Mikhail Gusarov <dottedmag@dottedmag.net>\r
345 >> +      1 Olivier Berger <olivier.berger@it-sudparis.eu>\r
346 >> +      1 Rolland Santimano <rollandsantimano@yahoo.com>\r
347 >> +      3 Stewart Smith <stewart@flamingspork.com>\r
348 >> +      2 notmuch <notmuch@notmuchmail.org>\r
349 >> +     48 notmuch@notmuchmail.org\r
350 >> +      1 olivier.berger@it-sudparis.eu\r
351 >> +EOF\r
352 >> +test_expect_equal_file OUTPUT EXPECTED\r
353 >> +\r
354 >>  test_begin_subtest "sanitize output for quoted-printable line-breaks in=\r
355  author and subject"\r
356 >>  add_message "[subject]=3D'two =3D?ISO-8859-1?Q?line=3D0A_subject?=3D\r
357 >>      headers'"\r
358 >> --=20\r
359 >> 2.1.1\r
360 >>\r
361 >> _______________________________________________\r
362 >> notmuch mailing list\r
363 >> notmuch@notmuchmail.org\r
364 >> http://notmuchmail.org/mailman/listinfo/notmuch\r
365 > _______________________________________________\r
366 > notmuch mailing list\r
367 > notmuch@notmuchmail.org\r
368 > http://notmuchmail.org/mailman/listinfo/notmuch\r