]> git.notmuchmail.org Git - notmuch/blob - notmuch-reply.c
notmuch new: Remove an unnecessary stat of every regular file in the mail store.
[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 const struct {
27     const char *header;
28     const char *fallback;
29     GMimeRecipientType recipient_type;
30 } reply_to_map[] = {
31     { "reply-to", "from", GMIME_RECIPIENT_TYPE_TO  },
32     { "to",         NULL, GMIME_RECIPIENT_TYPE_TO  },
33     { "cc",         NULL, GMIME_RECIPIENT_TYPE_CC  },
34     { "bcc",        NULL, GMIME_RECIPIENT_TYPE_BCC }
35 };
36
37 static void
38 reply_part_content (GMimeObject *part)
39 {
40     GMimeStream *stream_stdout = NULL, *stream_filter = NULL;
41     GMimeDataWrapper *wrapper;
42     const char *charset;
43
44     charset = g_mime_object_get_content_type_parameter (part, "charset");
45     stream_stdout = g_mime_stream_file_new (stdout);
46     if (stream_stdout) {
47         g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
48         stream_filter = g_mime_stream_filter_new(stream_stdout);
49         if (charset) {
50           g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter),
51                                    g_mime_filter_charset_new(charset, "UTF-8"));
52         }
53     }
54     g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter),
55                              g_mime_filter_reply_new(TRUE));
56     wrapper = g_mime_part_get_content_object (GMIME_PART (part));
57     if (wrapper && stream_filter)
58         g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
59     if (stream_filter)
60         g_object_unref(stream_filter);
61     if (stream_stdout)
62         g_object_unref(stream_stdout);
63 }
64
65 static void
66 reply_part (GMimeObject *part, int *part_count)
67 {
68     GMimeContentDisposition *disposition;
69     GMimeContentType *content_type;
70
71     (void) part_count;
72     disposition = g_mime_object_get_content_disposition (part);
73     if (disposition &&
74         strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
75     {
76         const char *filename = g_mime_part_get_filename (GMIME_PART (part));
77         content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
78
79         if (g_mime_content_type_is_type (content_type, "text", "*") &&
80             !g_mime_content_type_is_type (content_type, "text", "html"))
81         {
82             reply_part_content (part);
83         }
84         else
85         {
86             printf ("Attachment: %s (%s)\n", filename,
87                     g_mime_content_type_to_string (content_type));
88         }
89
90         return;
91     }
92
93     content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
94
95     if (g_mime_content_type_is_type (content_type, "text", "*") &&
96         !g_mime_content_type_is_type (content_type, "text", "html"))
97     {
98         reply_part_content (part);
99     }
100     else
101     {
102         printf ("Non-text part: %s\n",
103                 g_mime_content_type_to_string (content_type));
104     }
105 }
106
107 /* Is the given address configured as one of the user's "personal" or
108  * "other" addresses. */
109 static int
110 address_is_users (const char *address, notmuch_config_t *config)
111 {
112     const char *primary;
113     char **other;
114     size_t i, other_len;
115
116     primary = notmuch_config_get_user_primary_email (config);
117     if (strcasecmp (primary, address) == 0)
118         return 1;
119
120     other = notmuch_config_get_user_other_email (config, &other_len);
121     for (i = 0; i < other_len; i++)
122         if (strcasecmp (other[i], address) == 0)
123             return 1;
124
125     return 0;
126 }
127
128 /* For each address in 'list' that is not configured as one of the
129  * user's addresses in 'config', add that address to 'message' as an
130  * address of 'type'.
131  *
132  * The first address encountered that *is* the user's address will be
133  * returned, (otherwise NULL is returned).
134  */
135 static const char *
136 add_recipients_for_address_list (GMimeMessage *message,
137                                  notmuch_config_t *config,
138                                  GMimeRecipientType type,
139                                  InternetAddressList *list)
140 {
141     InternetAddress *address;
142     int i;
143     const char *ret = NULL;
144
145     for (i = 0; i < internet_address_list_length (list); i++) {
146         address = internet_address_list_get_address (list, i);
147         if (INTERNET_ADDRESS_IS_GROUP (address)) {
148             InternetAddressGroup *group;
149             InternetAddressList *group_list;
150
151             group = INTERNET_ADDRESS_GROUP (address);
152             group_list = internet_address_group_get_members (group);
153             if (group_list == NULL)
154                 continue;
155
156             add_recipients_for_address_list (message, config,
157                                              type, group_list);
158         } else {
159             InternetAddressMailbox *mailbox;
160             const char *name;
161             const char *addr;
162
163             mailbox = INTERNET_ADDRESS_MAILBOX (address);
164
165             name = internet_address_get_name (address);
166             addr = internet_address_mailbox_get_addr (mailbox);
167
168             if (address_is_users (addr, config)) {
169                 if (ret == NULL)
170                     ret = addr;
171             } else {
172                 g_mime_message_add_recipient (message, type, name, addr);
173             }
174         }
175     }
176
177     return ret;
178 }
179
180 /* For each address in 'recipients' that is not configured as one of
181  * the user's addresses in 'config', add that address to 'message' as
182  * an address of 'type'.
183  *
184  * The first address encountered that *is* the user's address will be
185  * returned, (otherwise NULL is returned).
186  */
187 static const char *
188 add_recipients_for_string (GMimeMessage *message,
189                            notmuch_config_t *config,
190                            GMimeRecipientType type,
191                            const char *recipients)
192 {
193     InternetAddressList *list;
194
195     list = internet_address_list_parse_string (recipients);
196     if (list == NULL)
197         return NULL;
198
199     return add_recipients_for_address_list (message, config, type, list);
200 }
201
202 static int
203 notmuch_reply_format_default(void *ctx, notmuch_config_t *config, notmuch_query_t *query)
204 {
205     GMimeMessage *reply;
206     notmuch_messages_t *messages;
207     notmuch_message_t *message;
208     const char *subject, *recipients, *from_addr = NULL;
209     const char *in_reply_to, *orig_references, *references;
210     char *reply_headers;
211     unsigned int i;
212
213     for (messages = notmuch_query_search_messages (query);
214          notmuch_messages_has_more (messages);
215          notmuch_messages_advance (messages))
216     {
217         message = notmuch_messages_get (messages);
218
219         /* The 1 means we want headers in a "pretty" order. */
220         reply = g_mime_message_new (1);
221         if (reply == NULL) {
222             fprintf (stderr, "Out of memory\n");
223             return 1;
224         }
225
226         subject = notmuch_message_get_header (message, "subject");
227
228         if (strncasecmp (subject, "Re:", 3))
229             subject = talloc_asprintf (ctx, "Re: %s", subject);
230         g_mime_message_set_subject (reply, subject);
231
232         for (i = 0; i < ARRAY_SIZE (reply_to_map); i++) {
233             const char *addr;
234
235             recipients = notmuch_message_get_header (message,
236                                                      reply_to_map[i].header);
237             if ((recipients == NULL || recipients[0] == '\0') && reply_to_map[i].fallback)
238                 recipients = notmuch_message_get_header (message,
239                                                          reply_to_map[i].fallback);
240
241             addr = add_recipients_for_string (reply, config,
242                                               reply_to_map[i].recipient_type,
243                                               recipients);
244             if (from_addr == NULL)
245                 from_addr = addr;
246         }
247
248         if (from_addr == NULL)
249             from_addr = notmuch_config_get_user_primary_email (config);
250
251         from_addr = talloc_asprintf (ctx, "%s <%s>",
252                                      notmuch_config_get_user_name (config),
253                                      from_addr);
254         g_mime_object_set_header (GMIME_OBJECT (reply),
255                                   "From", from_addr);
256
257         g_mime_object_set_header (GMIME_OBJECT (reply), "Bcc",
258                            notmuch_config_get_user_primary_email (config));
259
260         in_reply_to = talloc_asprintf (ctx, "<%s>",
261                              notmuch_message_get_message_id (message));
262
263         g_mime_object_set_header (GMIME_OBJECT (reply),
264                                   "In-Reply-To", in_reply_to);
265
266         orig_references = notmuch_message_get_header (message, "references");
267         references = talloc_asprintf (ctx, "%s%s%s",
268                                       orig_references ? orig_references : "",
269                                       orig_references ? " " : "",
270                                       in_reply_to);
271         g_mime_object_set_header (GMIME_OBJECT (reply),
272                                   "References", references);
273
274         reply_headers = g_mime_object_to_string (GMIME_OBJECT (reply));
275         printf ("%s", reply_headers);
276         free (reply_headers);
277
278         g_object_unref (G_OBJECT (reply));
279         reply = NULL;
280
281         printf ("On %s, %s wrote:\n",
282                 notmuch_message_get_header (message, "date"),
283                 notmuch_message_get_header (message, "from"));
284
285         show_message_body (notmuch_message_get_filename (message), reply_part);
286
287         notmuch_message_destroy (message);
288     }
289     return 0;
290 }
291
292 /* This format is currently tuned for a git send-email --notmuch hook */
293 static int
294 notmuch_reply_format_headers_only(void *ctx, notmuch_config_t *config, notmuch_query_t *query)
295 {
296     GMimeMessage *reply;
297     notmuch_messages_t *messages;
298     notmuch_message_t *message;
299     const char *recipients, *in_reply_to, *orig_references, *references;
300     char *reply_headers;
301     unsigned int i;
302
303     for (messages = notmuch_query_search_messages (query);
304          notmuch_messages_has_more (messages);
305          notmuch_messages_advance (messages))
306     {
307         message = notmuch_messages_get (messages);
308
309         /* The 0 means we do not want headers in a "pretty" order. */
310         reply = g_mime_message_new (0);
311         if (reply == NULL) {
312             fprintf (stderr, "Out of memory\n");
313             return 1;
314         }
315
316         in_reply_to = talloc_asprintf (ctx, "<%s>",
317                              notmuch_message_get_message_id (message));
318
319         g_mime_object_set_header (GMIME_OBJECT (reply),
320                                   "In-Reply-To", in_reply_to);
321
322
323         orig_references = notmuch_message_get_header (message, "references");
324
325         /* We print In-Reply-To followed by References because git format-patch treats them
326          * specially.  Git does not interpret the other headers specially
327          */
328         references = talloc_asprintf (ctx, "%s%s%s",
329                                       orig_references ? orig_references : "",
330                                       orig_references ? " " : "",
331                                       in_reply_to);
332         g_mime_object_set_header (GMIME_OBJECT (reply),
333                                   "References", references);
334
335         for (i = 0; i < ARRAY_SIZE (reply_to_map); i++) {
336             const char *addr;
337
338             recipients = notmuch_message_get_header (message,
339                                                      reply_to_map[i].header);
340             if ((recipients == NULL || recipients[0] == '\0') && reply_to_map[i].fallback)
341                 recipients = notmuch_message_get_header (message,
342                                                          reply_to_map[i].fallback);
343
344             addr = add_recipients_for_string (reply, config,
345                                               reply_to_map[i].recipient_type,
346                                               recipients);
347         }
348
349         g_mime_object_set_header (GMIME_OBJECT (reply), "Bcc",
350                            notmuch_config_get_user_primary_email (config));
351
352         reply_headers = g_mime_object_to_string (GMIME_OBJECT (reply));
353         printf ("%s", reply_headers);
354         free (reply_headers);
355
356         g_object_unref (G_OBJECT (reply));
357         reply = NULL;
358
359         notmuch_message_destroy (message);
360     }
361     return 0;
362 }
363
364 int
365 notmuch_reply_command (void *ctx, int argc, char *argv[])
366 {
367     notmuch_config_t *config;
368     notmuch_database_t *notmuch;
369     notmuch_query_t *query;
370     char *opt, *query_string;
371     int i, ret = 0;
372     int (*reply_format_func)(void *ctx, notmuch_config_t *config, notmuch_query_t *query);
373
374     reply_format_func = notmuch_reply_format_default;
375
376     for (i = 0; i < argc && argv[i][0] == '-'; i++) {
377         if (strcmp (argv[i], "--") == 0) {
378             i++;
379             break;
380         }
381         if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
382             opt = argv[i] + sizeof ("--format=") - 1;
383             if (strcmp (opt, "default") == 0) {
384                 reply_format_func = notmuch_reply_format_default;
385             } else if (strcmp (opt, "headers-only") == 0) {
386                 reply_format_func = notmuch_reply_format_headers_only;
387             } else {
388                 fprintf (stderr, "Invalid value for --format: %s\n", opt);
389                 return 1;
390             }
391         } else {
392             fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
393             return 1;
394         }
395     }
396
397     argc -= i;
398     argv += i;
399
400     config = notmuch_config_open (ctx, NULL, NULL);
401     if (config == NULL)
402         return 1;
403
404     query_string = query_string_from_args (ctx, argc, argv);
405     if (query_string == NULL) {
406         fprintf (stderr, "Out of memory\n");
407         return 1;
408     }
409
410     if (*query_string == '\0') {
411         fprintf (stderr, "Error: notmuch reply requires at least one search term.\n");
412         return 1;
413     }
414
415     notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
416                                      NOTMUCH_DATABASE_MODE_READ_ONLY);
417     if (notmuch == NULL)
418         return 1;
419
420     query = notmuch_query_create (notmuch, query_string);
421     if (query == NULL) {
422         fprintf (stderr, "Out of memory\n");
423         return 1;
424     }
425
426     if (reply_format_func (ctx, config, query) != 0)
427         return 1;
428
429     notmuch_query_destroy (query);
430     notmuch_database_close (notmuch);
431
432     return ret;
433 }