]> git.notmuchmail.org Git - notmuch/blob - notmuch-reply.c
notmuch-reply.c: Factor adding recipients into common function
[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 /* Augments the recipients of reply from the headers of message.
192  *
193  * If any of the user's addresses were found in these headers, the first
194  * of these returned, otherwise NULL is returned.
195  */
196 static const char *
197 add_recipients_from_message (GMimeMessage *reply,
198                              notmuch_config_t *config,
199                              notmuch_message_t *message)
200 {
201     static const struct {
202         const char *header;
203         const char *fallback;
204         GMimeRecipientType recipient_type;
205     } reply_to_map[] = {
206         { "reply-to", "from", GMIME_RECIPIENT_TYPE_TO  },
207         { "to",         NULL, GMIME_RECIPIENT_TYPE_TO  },
208         { "cc",         NULL, GMIME_RECIPIENT_TYPE_CC  },
209         { "bcc",        NULL, GMIME_RECIPIENT_TYPE_BCC }
210     };
211     const char *from_addr = NULL;
212     unsigned int i;
213
214     for (i = 0; i < ARRAY_SIZE (reply_to_map); i++) {
215         const char *addr, *recipients;
216
217         recipients = notmuch_message_get_header (message,
218                                                  reply_to_map[i].header);
219         if ((recipients == NULL || recipients[0] == '\0') && reply_to_map[i].fallback)
220             recipients = notmuch_message_get_header (message,
221                                                      reply_to_map[i].fallback);
222
223         addr = add_recipients_for_string (reply, config,
224                                           reply_to_map[i].recipient_type,
225                                           recipients);
226         if (from_addr == NULL)
227             from_addr = addr;
228     }
229     return from_addr;
230 }
231
232 static int
233 notmuch_reply_format_default(void *ctx, notmuch_config_t *config, notmuch_query_t *query)
234 {
235     GMimeMessage *reply;
236     notmuch_messages_t *messages;
237     notmuch_message_t *message;
238     const char *subject, *from_addr = NULL;
239     const char *in_reply_to, *orig_references, *references;
240     char *reply_headers;
241
242     for (messages = notmuch_query_search_messages (query);
243          notmuch_messages_has_more (messages);
244          notmuch_messages_advance (messages))
245     {
246         message = notmuch_messages_get (messages);
247
248         /* The 1 means we want headers in a "pretty" order. */
249         reply = g_mime_message_new (1);
250         if (reply == NULL) {
251             fprintf (stderr, "Out of memory\n");
252             return 1;
253         }
254
255         subject = notmuch_message_get_header (message, "subject");
256
257         if (strncasecmp (subject, "Re:", 3))
258             subject = talloc_asprintf (ctx, "Re: %s", subject);
259         g_mime_message_set_subject (reply, subject);
260
261         from_addr = add_recipients_from_message (reply, config, message);
262
263         if (from_addr == NULL)
264             from_addr = notmuch_config_get_user_primary_email (config);
265
266         from_addr = talloc_asprintf (ctx, "%s <%s>",
267                                      notmuch_config_get_user_name (config),
268                                      from_addr);
269         g_mime_object_set_header (GMIME_OBJECT (reply),
270                                   "From", from_addr);
271
272         g_mime_object_set_header (GMIME_OBJECT (reply), "Bcc",
273                            notmuch_config_get_user_primary_email (config));
274
275         in_reply_to = talloc_asprintf (ctx, "<%s>",
276                              notmuch_message_get_message_id (message));
277
278         g_mime_object_set_header (GMIME_OBJECT (reply),
279                                   "In-Reply-To", in_reply_to);
280
281         orig_references = notmuch_message_get_header (message, "references");
282         references = talloc_asprintf (ctx, "%s%s%s",
283                                       orig_references ? orig_references : "",
284                                       orig_references ? " " : "",
285                                       in_reply_to);
286         g_mime_object_set_header (GMIME_OBJECT (reply),
287                                   "References", references);
288
289         reply_headers = g_mime_object_to_string (GMIME_OBJECT (reply));
290         printf ("%s", reply_headers);
291         free (reply_headers);
292
293         g_object_unref (G_OBJECT (reply));
294         reply = NULL;
295
296         printf ("On %s, %s wrote:\n",
297                 notmuch_message_get_header (message, "date"),
298                 notmuch_message_get_header (message, "from"));
299
300         show_message_body (notmuch_message_get_filename (message), reply_part);
301
302         notmuch_message_destroy (message);
303     }
304     return 0;
305 }
306
307 /* This format is currently tuned for a git send-email --notmuch hook */
308 static int
309 notmuch_reply_format_headers_only(void *ctx, notmuch_config_t *config, notmuch_query_t *query)
310 {
311     GMimeMessage *reply;
312     notmuch_messages_t *messages;
313     notmuch_message_t *message;
314     const char *in_reply_to, *orig_references, *references;
315     char *reply_headers;
316
317     for (messages = notmuch_query_search_messages (query);
318          notmuch_messages_has_more (messages);
319          notmuch_messages_advance (messages))
320     {
321         message = notmuch_messages_get (messages);
322
323         /* The 0 means we do not want headers in a "pretty" order. */
324         reply = g_mime_message_new (0);
325         if (reply == NULL) {
326             fprintf (stderr, "Out of memory\n");
327             return 1;
328         }
329
330         in_reply_to = talloc_asprintf (ctx, "<%s>",
331                              notmuch_message_get_message_id (message));
332
333         g_mime_object_set_header (GMIME_OBJECT (reply),
334                                   "In-Reply-To", in_reply_to);
335
336
337         orig_references = notmuch_message_get_header (message, "references");
338
339         /* We print In-Reply-To followed by References because git format-patch treats them
340          * specially.  Git does not interpret the other headers specially
341          */
342         references = talloc_asprintf (ctx, "%s%s%s",
343                                       orig_references ? orig_references : "",
344                                       orig_references ? " " : "",
345                                       in_reply_to);
346         g_mime_object_set_header (GMIME_OBJECT (reply),
347                                   "References", references);
348
349         (void)add_recipients_from_message (reply, config, message);
350
351         g_mime_object_set_header (GMIME_OBJECT (reply), "Bcc",
352                            notmuch_config_get_user_primary_email (config));
353
354         reply_headers = g_mime_object_to_string (GMIME_OBJECT (reply));
355         printf ("%s", reply_headers);
356         free (reply_headers);
357
358         g_object_unref (G_OBJECT (reply));
359         reply = NULL;
360
361         notmuch_message_destroy (message);
362     }
363     return 0;
364 }
365
366 int
367 notmuch_reply_command (void *ctx, int argc, char *argv[])
368 {
369     notmuch_config_t *config;
370     notmuch_database_t *notmuch;
371     notmuch_query_t *query;
372     char *opt, *query_string;
373     int i, ret = 0;
374     int (*reply_format_func)(void *ctx, notmuch_config_t *config, notmuch_query_t *query);
375
376     reply_format_func = notmuch_reply_format_default;
377
378     for (i = 0; i < argc && argv[i][0] == '-'; i++) {
379         if (strcmp (argv[i], "--") == 0) {
380             i++;
381             break;
382         }
383         if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
384             opt = argv[i] + sizeof ("--format=") - 1;
385             if (strcmp (opt, "default") == 0) {
386                 reply_format_func = notmuch_reply_format_default;
387             } else if (strcmp (opt, "headers-only") == 0) {
388                 reply_format_func = notmuch_reply_format_headers_only;
389             } else {
390                 fprintf (stderr, "Invalid value for --format: %s\n", opt);
391                 return 1;
392             }
393         } else {
394             fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
395             return 1;
396         }
397     }
398
399     argc -= i;
400     argv += i;
401
402     config = notmuch_config_open (ctx, NULL, NULL);
403     if (config == NULL)
404         return 1;
405
406     query_string = query_string_from_args (ctx, argc, argv);
407     if (query_string == NULL) {
408         fprintf (stderr, "Out of memory\n");
409         return 1;
410     }
411
412     if (*query_string == '\0') {
413         fprintf (stderr, "Error: notmuch reply requires at least one search term.\n");
414         return 1;
415     }
416
417     notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
418                                      NOTMUCH_DATABASE_MODE_READ_ONLY);
419     if (notmuch == NULL)
420         return 1;
421
422     query = notmuch_query_create (notmuch, query_string);
423     if (query == NULL) {
424         fprintf (stderr, "Out of memory\n");
425         return 1;
426     }
427
428     if (reply_format_func (ctx, config, query) != 0)
429         return 1;
430
431     notmuch_query_destroy (query);
432     notmuch_database_close (notmuch);
433
434     return ret;
435 }