]> git.notmuchmail.org Git - notmuch/blob - notmuch-reply.c
notmuch: Document the new special-case syntax of "*".
[notmuch] / notmuch-reply.c
1 /* notmuch - Not much of an email program, (just index and search)
2  *
3  * Copyright © 2009 Carl Worth
4  * Copyright © 2009 Keith Packard
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see http://www.gnu.org/licenses/ .
18  *
19  * Authors: Carl Worth <cworth@cworth.org>
20  *          Keith Packard <keithp@keithp.com>
21  */
22
23 #include "notmuch-client.h"
24 #include "gmime-filter-reply.h"
25
26 static void
27 reply_part_content (GMimeObject *part)
28 {
29     GMimeStream *stream_stdout = NULL, *stream_filter = NULL;
30     GMimeDataWrapper *wrapper;
31     const char *charset;
32
33     charset = g_mime_object_get_content_type_parameter (part, "charset");
34     stream_stdout = g_mime_stream_file_new (stdout);
35     if (stream_stdout) {
36         g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
37         stream_filter = g_mime_stream_filter_new(stream_stdout);
38         if (charset) {
39           g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter),
40                                    g_mime_filter_charset_new(charset, "UTF-8"));
41         }
42     }
43     g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter),
44                              g_mime_filter_reply_new(TRUE));
45     wrapper = g_mime_part_get_content_object (GMIME_PART (part));
46     if (wrapper && stream_filter)
47         g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
48     if (stream_filter)
49         g_object_unref(stream_filter);
50     if (stream_stdout)
51         g_object_unref(stream_stdout);
52 }
53
54 static void
55 reply_part (GMimeObject *part, int *part_count)
56 {
57     GMimeContentDisposition *disposition;
58     GMimeContentType *content_type;
59
60     (void) part_count;
61     disposition = g_mime_object_get_content_disposition (part);
62     if (disposition &&
63         strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
64     {
65         const char *filename = g_mime_part_get_filename (GMIME_PART (part));
66         content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
67
68         if (g_mime_content_type_is_type (content_type, "text", "*") &&
69             !g_mime_content_type_is_type (content_type, "text", "html"))
70         {
71             reply_part_content (part);
72         }
73         else
74         {
75             printf ("Attachment: %s (%s)\n", filename,
76                     g_mime_content_type_to_string (content_type));
77         }
78
79         return;
80     }
81
82     content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
83
84     if (g_mime_content_type_is_type (content_type, "text", "*") &&
85         !g_mime_content_type_is_type (content_type, "text", "html"))
86     {
87         reply_part_content (part);
88     }
89     else
90     {
91         printf ("Non-text part: %s\n",
92                 g_mime_content_type_to_string (content_type));
93     }
94 }
95
96 /* Is the given address configured as one of the user's "personal" or
97  * "other" addresses. */
98 static int
99 address_is_users (const char *address, notmuch_config_t *config)
100 {
101     const char *primary;
102     char **other;
103     size_t i, other_len;
104
105     primary = notmuch_config_get_user_primary_email (config);
106     if (strcasecmp (primary, address) == 0)
107         return 1;
108
109     other = notmuch_config_get_user_other_email (config, &other_len);
110     for (i = 0; i < other_len; i++)
111         if (strcasecmp (other[i], address) == 0)
112             return 1;
113
114     return 0;
115 }
116
117 /* For each address in 'list' that is not configured as one of the
118  * user's addresses in 'config', add that address to 'message' as an
119  * address of 'type'.
120  *
121  * The first address encountered that *is* the user's address will be
122  * returned, (otherwise NULL is returned).
123  */
124 static const char *
125 add_recipients_for_address_list (GMimeMessage *message,
126                                  notmuch_config_t *config,
127                                  GMimeRecipientType type,
128                                  InternetAddressList *list)
129 {
130     InternetAddress *address;
131     int i;
132     const char *ret = NULL;
133
134     for (i = 0; i < internet_address_list_length (list); i++) {
135         address = internet_address_list_get_address (list, i);
136         if (INTERNET_ADDRESS_IS_GROUP (address)) {
137             InternetAddressGroup *group;
138             InternetAddressList *group_list;
139
140             group = INTERNET_ADDRESS_GROUP (address);
141             group_list = internet_address_group_get_members (group);
142             if (group_list == NULL)
143                 continue;
144
145             add_recipients_for_address_list (message, config,
146                                              type, group_list);
147         } else {
148             InternetAddressMailbox *mailbox;
149             const char *name;
150             const char *addr;
151
152             mailbox = INTERNET_ADDRESS_MAILBOX (address);
153
154             name = internet_address_get_name (address);
155             addr = internet_address_mailbox_get_addr (mailbox);
156
157             if (address_is_users (addr, config)) {
158                 if (ret == NULL)
159                     ret = addr;
160             } else {
161                 g_mime_message_add_recipient (message, type, name, addr);
162             }
163         }
164     }
165
166     return ret;
167 }
168
169 /* For each address in 'recipients' that is not configured as one of
170  * the user's addresses in 'config', add that address to 'message' as
171  * an address of 'type'.
172  *
173  * The first address encountered that *is* the user's address will be
174  * returned, (otherwise NULL is returned).
175  */
176 static const char *
177 add_recipients_for_string (GMimeMessage *message,
178                            notmuch_config_t *config,
179                            GMimeRecipientType type,
180                            const char *recipients)
181 {
182     InternetAddressList *list;
183
184     list = internet_address_list_parse_string (recipients);
185     if (list == NULL)
186         return NULL;
187
188     return add_recipients_for_address_list (message, config, type, list);
189 }
190
191 /* Does the address in the Reply-To header of 'message' already appear
192  * in either the 'To' or 'Cc' header of the message?
193  */
194 static int
195 reply_to_header_is_redundant (notmuch_message_t *message)
196 {
197     const char *header, *addr;
198     InternetAddressList *list;
199     InternetAddress *address;
200     InternetAddressMailbox *mailbox;
201
202     header = notmuch_message_get_header (message, "reply-to");
203     if (*header == '\0')
204         return 0;
205
206     list = internet_address_list_parse_string (header);
207
208     if (internet_address_list_length (list) != 1)
209         return 0;
210
211     address = internet_address_list_get_address (list, 0);
212     if (INTERNET_ADDRESS_IS_GROUP (address))
213         return 0;
214
215     mailbox = INTERNET_ADDRESS_MAILBOX (address);
216     addr = internet_address_mailbox_get_addr (mailbox);
217
218     if (strstr (notmuch_message_get_header (message, "to"), addr) != 0 ||
219         strstr (notmuch_message_get_header (message, "cc"), addr) != 0)
220     {
221         return 1;
222     }
223
224     return 0;
225 }
226
227 /* Augments the recipients of reply from the headers of message.
228  *
229  * If any of the user's addresses were found in these headers, the first
230  * of these returned, otherwise NULL is returned.
231  */
232 static const char *
233 add_recipients_from_message (GMimeMessage *reply,
234                              notmuch_config_t *config,
235                              notmuch_message_t *message)
236 {
237     struct {
238         const char *header;
239         const char *fallback;
240         GMimeRecipientType recipient_type;
241     } reply_to_map[] = {
242         { "reply-to", "from", GMIME_RECIPIENT_TYPE_TO  },
243         { "to",         NULL, GMIME_RECIPIENT_TYPE_TO  },
244         { "cc",         NULL, GMIME_RECIPIENT_TYPE_CC  },
245         { "bcc",        NULL, GMIME_RECIPIENT_TYPE_BCC }
246     };
247     const char *from_addr = NULL;
248     unsigned int i;
249
250     /* Some mailing lists munge the Reply-To header despite it being A Bad
251      * Thing, see http://www.unicom.com/pw/reply-to-harmful.html
252      *
253      * The munging is easy to detect, because it results in a
254      * redundant reply-to header, (with an address that already exists
255      * in either To or Cc). So in this case, we ignore the Reply-To
256      * field and use the From header. Thie ensures the original sender
257      * will get the reply even if not subscribed to the list. Note
258      * that the address in the Reply-To header will always appear in
259      * the reply.
260      */
261     if (reply_to_header_is_redundant (message)) {
262         reply_to_map[0].header = "from";
263         reply_to_map[0].fallback = NULL;
264     }
265
266     for (i = 0; i < ARRAY_SIZE (reply_to_map); i++) {
267         const char *addr, *recipients;
268
269         recipients = notmuch_message_get_header (message,
270                                                  reply_to_map[i].header);
271         if ((recipients == NULL || recipients[0] == '\0') && reply_to_map[i].fallback)
272             recipients = notmuch_message_get_header (message,
273                                                      reply_to_map[i].fallback);
274
275         addr = add_recipients_for_string (reply, config,
276                                           reply_to_map[i].recipient_type,
277                                           recipients);
278         if (from_addr == NULL)
279             from_addr = addr;
280     }
281
282     return from_addr;
283 }
284
285 static const char *
286 guess_from_received_header (notmuch_config_t *config, notmuch_message_t *message)
287 {
288     const char *received,*primary;
289     char **other;
290     char *by,*mta,*ptr,*token;
291     char *domain=NULL;
292     char *tld=NULL;
293     const char *delim=". \t";
294     size_t i,other_len;
295
296     received = notmuch_message_get_header (message, "received");
297     by = strstr (received, " by ");
298     if (by && *(by+4)) {
299         /* sadly, the format of Received: headers is a bit inconsistent,
300          * depending on the MTA used. So we try to extract just the MTA
301          * here by removing leading whitespace and assuming that the MTA
302          * name ends at the next whitespace
303          * we test for *(by+4) to be non-'\0' to make sure there's something
304          * there at all - and then assume that the first whitespace delimited
305          * token that follows is the last receiving server
306          */
307         mta = strdup (by+4);
308         if (mta == NULL)
309             return NULL;
310         token = strtok(mta," \t");
311         if (token == NULL)
312             return NULL;
313         /* Now extract the last two components of the MTA host name
314          * as domain and tld
315          */
316         while ((ptr = strsep (&token, delim)) != NULL) {
317             if (*ptr == '\0')
318                 continue;
319             domain = tld;
320             tld = ptr;
321         }
322
323         if (domain) {
324             /* recombine domain and tld and look for it among the configured
325              * email addresses
326              */
327             *(tld-1) = '.';
328             primary = notmuch_config_get_user_primary_email (config);
329             if (strcasestr (primary, domain)) {
330                 free (mta);
331                 return primary;
332             }
333             other = notmuch_config_get_user_other_email (config, &other_len);
334             for (i = 0; i < other_len; i++)
335                 if (strcasestr (other[i], domain)) {
336                     free (mta);
337                     return other[i];
338                 }
339         }
340
341         free (mta);
342     }
343
344     return NULL;
345 }
346
347 static int
348 notmuch_reply_format_default(void *ctx, notmuch_config_t *config, notmuch_query_t *query)
349 {
350     GMimeMessage *reply;
351     notmuch_messages_t *messages;
352     notmuch_message_t *message;
353     const char *subject, *from_addr = NULL;
354     const char *in_reply_to, *orig_references, *references;
355     char *reply_headers;
356
357     for (messages = notmuch_query_search_messages (query);
358          notmuch_messages_valid (messages);
359          notmuch_messages_move_to_next (messages))
360     {
361         message = notmuch_messages_get (messages);
362
363         /* The 1 means we want headers in a "pretty" order. */
364         reply = g_mime_message_new (1);
365         if (reply == NULL) {
366             fprintf (stderr, "Out of memory\n");
367             return 1;
368         }
369
370         subject = notmuch_message_get_header (message, "subject");
371
372         if (strncasecmp (subject, "Re:", 3))
373             subject = talloc_asprintf (ctx, "Re: %s", subject);
374         g_mime_message_set_subject (reply, subject);
375
376         from_addr = add_recipients_from_message (reply, config, message);
377
378         if (from_addr == NULL)
379             from_addr = guess_from_received_header (config, message);
380
381         if (from_addr == NULL)
382             from_addr = notmuch_config_get_user_primary_email (config);
383
384         from_addr = talloc_asprintf (ctx, "%s <%s>",
385                                      notmuch_config_get_user_name (config),
386                                      from_addr);
387         g_mime_object_set_header (GMIME_OBJECT (reply),
388                                   "From", from_addr);
389
390         g_mime_object_set_header (GMIME_OBJECT (reply), "Bcc",
391                            notmuch_config_get_user_primary_email (config));
392
393         in_reply_to = talloc_asprintf (ctx, "<%s>",
394                              notmuch_message_get_message_id (message));
395
396         g_mime_object_set_header (GMIME_OBJECT (reply),
397                                   "In-Reply-To", in_reply_to);
398
399         orig_references = notmuch_message_get_header (message, "references");
400         references = talloc_asprintf (ctx, "%s%s%s",
401                                       orig_references ? orig_references : "",
402                                       orig_references ? " " : "",
403                                       in_reply_to);
404         g_mime_object_set_header (GMIME_OBJECT (reply),
405                                   "References", references);
406
407         reply_headers = g_mime_object_to_string (GMIME_OBJECT (reply));
408         printf ("%s", reply_headers);
409         free (reply_headers);
410
411         g_object_unref (G_OBJECT (reply));
412         reply = NULL;
413
414         printf ("On %s, %s wrote:\n",
415                 notmuch_message_get_header (message, "date"),
416                 notmuch_message_get_header (message, "from"));
417
418         show_message_body (notmuch_message_get_filename (message), reply_part);
419
420         notmuch_message_destroy (message);
421     }
422     return 0;
423 }
424
425 /* This format is currently tuned for a git send-email --notmuch hook */
426 static int
427 notmuch_reply_format_headers_only(void *ctx, notmuch_config_t *config, notmuch_query_t *query)
428 {
429     GMimeMessage *reply;
430     notmuch_messages_t *messages;
431     notmuch_message_t *message;
432     const char *in_reply_to, *orig_references, *references;
433     char *reply_headers;
434
435     for (messages = notmuch_query_search_messages (query);
436          notmuch_messages_valid (messages);
437          notmuch_messages_move_to_next (messages))
438     {
439         message = notmuch_messages_get (messages);
440
441         /* The 0 means we do not want headers in a "pretty" order. */
442         reply = g_mime_message_new (0);
443         if (reply == NULL) {
444             fprintf (stderr, "Out of memory\n");
445             return 1;
446         }
447
448         in_reply_to = talloc_asprintf (ctx, "<%s>",
449                              notmuch_message_get_message_id (message));
450
451         g_mime_object_set_header (GMIME_OBJECT (reply),
452                                   "In-Reply-To", in_reply_to);
453
454
455         orig_references = notmuch_message_get_header (message, "references");
456
457         /* We print In-Reply-To followed by References because git format-patch treats them
458          * specially.  Git does not interpret the other headers specially
459          */
460         references = talloc_asprintf (ctx, "%s%s%s",
461                                       orig_references ? orig_references : "",
462                                       orig_references ? " " : "",
463                                       in_reply_to);
464         g_mime_object_set_header (GMIME_OBJECT (reply),
465                                   "References", references);
466
467         (void)add_recipients_from_message (reply, config, message);
468
469         g_mime_object_set_header (GMIME_OBJECT (reply), "Bcc",
470                            notmuch_config_get_user_primary_email (config));
471
472         reply_headers = g_mime_object_to_string (GMIME_OBJECT (reply));
473         printf ("%s", reply_headers);
474         free (reply_headers);
475
476         g_object_unref (G_OBJECT (reply));
477         reply = NULL;
478
479         notmuch_message_destroy (message);
480     }
481     return 0;
482 }
483
484 int
485 notmuch_reply_command (void *ctx, int argc, char *argv[])
486 {
487     notmuch_config_t *config;
488     notmuch_database_t *notmuch;
489     notmuch_query_t *query;
490     char *opt, *query_string;
491     int i, ret = 0;
492     int (*reply_format_func)(void *ctx, notmuch_config_t *config, notmuch_query_t *query);
493
494     reply_format_func = notmuch_reply_format_default;
495
496     for (i = 0; i < argc && argv[i][0] == '-'; i++) {
497         if (strcmp (argv[i], "--") == 0) {
498             i++;
499             break;
500         }
501         if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
502             opt = argv[i] + sizeof ("--format=") - 1;
503             if (strcmp (opt, "default") == 0) {
504                 reply_format_func = notmuch_reply_format_default;
505             } else if (strcmp (opt, "headers-only") == 0) {
506                 reply_format_func = notmuch_reply_format_headers_only;
507             } else {
508                 fprintf (stderr, "Invalid value for --format: %s\n", opt);
509                 return 1;
510             }
511         } else {
512             fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
513             return 1;
514         }
515     }
516
517     argc -= i;
518     argv += i;
519
520     config = notmuch_config_open (ctx, NULL, NULL);
521     if (config == NULL)
522         return 1;
523
524     query_string = query_string_from_args (ctx, argc, argv);
525     if (query_string == NULL) {
526         fprintf (stderr, "Out of memory\n");
527         return 1;
528     }
529
530     if (*query_string == '\0') {
531         fprintf (stderr, "Error: notmuch reply requires at least one search term.\n");
532         return 1;
533     }
534
535     notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
536                                      NOTMUCH_DATABASE_MODE_READ_ONLY);
537     if (notmuch == NULL)
538         return 1;
539
540     query = notmuch_query_create (notmuch, query_string);
541     if (query == NULL) {
542         fprintf (stderr, "Out of memory\n");
543         return 1;
544     }
545
546     if (reply_format_func (ctx, config, query) != 0)
547         return 1;
548
549     notmuch_query_destroy (query);
550     notmuch_database_close (notmuch);
551
552     return ret;
553 }