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