]> git.notmuchmail.org Git - notmuch/blob - notmuch-reply.c
lib/query: Fix notmuch_threads_t to stream results rather than blocking.
[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
32     stream_stdout = g_mime_stream_file_new (stdout);
33     if (stream_stdout) {
34         g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
35         stream_filter = g_mime_stream_filter_new(stream_stdout);
36     }
37     g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter),
38                              g_mime_filter_reply_new(TRUE));
39     wrapper = g_mime_part_get_content_object (GMIME_PART (part));
40     if (wrapper && stream_filter)
41         g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
42     if (stream_filter)
43         g_object_unref(stream_filter);
44     if (stream_stdout)
45         g_object_unref(stream_stdout);
46 }
47
48 static void
49 reply_part (GMimeObject *part, int *part_count)
50 {
51     GMimeContentDisposition *disposition;
52     GMimeContentType *content_type;
53
54     (void) part_count;
55     disposition = g_mime_object_get_content_disposition (part);
56     if (disposition &&
57         strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
58     {
59         const char *filename = g_mime_part_get_filename (GMIME_PART (part));
60         content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
61
62         if (g_mime_content_type_is_type (content_type, "text", "*") &&
63             !g_mime_content_type_is_type (content_type, "text", "html"))
64         {
65             reply_part_content (part);
66         }
67         else
68         {
69             printf ("Attachment: %s (%s)\n", filename,
70                     g_mime_content_type_to_string (content_type));
71         }
72
73         return;
74     }
75
76     content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
77
78     if (g_mime_content_type_is_type (content_type, "text", "*") &&
79         !g_mime_content_type_is_type (content_type, "text", "html"))
80     {
81         reply_part_content (part);
82     }
83     else
84     {
85         printf ("Non-text part: %s\n",
86                 g_mime_content_type_to_string (content_type));
87     }
88 }
89
90 /* Is the given address configured as one of the user's "personal" or
91  * "other" addresses. */
92 static int
93 address_is_users (const char *address, notmuch_config_t *config)
94 {
95     const char *primary;
96     char **other;
97     size_t i, other_len;
98
99     primary = notmuch_config_get_user_primary_email (config);
100     if (strcasecmp (primary, address) == 0)
101         return 1;
102
103     other = notmuch_config_get_user_other_email (config, &other_len);
104     for (i = 0; i < other_len; i++)
105         if (strcasecmp (other[i], address) == 0)
106             return 1;
107
108     return 0;
109 }
110
111 /* For each address in 'list' that is not configured as one of the
112  * user's addresses in 'config', add that address to 'message' as an
113  * address of 'type'.
114  *
115  * The first address encountered that *is* the user's address will be
116  * returned, (otherwise NULL is returned).
117  */
118 static const char *
119 add_recipients_for_address_list (GMimeMessage *message,
120                                  notmuch_config_t *config,
121                                  GMimeRecipientType type,
122                                  InternetAddressList *list)
123 {
124     InternetAddress *address;
125     int i;
126     const char *ret = NULL;
127
128     for (i = 0; i < internet_address_list_length (list); i++) {
129         address = internet_address_list_get_address (list, i);
130         if (INTERNET_ADDRESS_IS_GROUP (address)) {
131             InternetAddressGroup *group;
132             InternetAddressList *group_list;
133
134             group = INTERNET_ADDRESS_GROUP (address);
135             group_list = internet_address_group_get_members (group);
136             if (group_list == NULL)
137                 continue;
138
139             add_recipients_for_address_list (message, config,
140                                              type, group_list);
141         } else {
142             InternetAddressMailbox *mailbox;
143             const char *name;
144             const char *addr;
145
146             mailbox = INTERNET_ADDRESS_MAILBOX (address);
147
148             name = internet_address_get_name (address);
149             addr = internet_address_mailbox_get_addr (mailbox);
150
151             if (address_is_users (addr, config)) {
152                 if (ret == NULL)
153                     ret = addr;
154             } else {
155                 g_mime_message_add_recipient (message, type, name, addr);
156             }
157         }
158     }
159
160     return ret;
161 }
162
163 /* For each address in 'recipients' that is not configured as one of
164  * the user's addresses in 'config', add that address to 'message' as
165  * an address of 'type'.
166  *
167  * The first address encountered that *is* the user's address will be
168  * returned, (otherwise NULL is returned).
169  */
170 static const char *
171 add_recipients_for_string (GMimeMessage *message,
172                            notmuch_config_t *config,
173                            GMimeRecipientType type,
174                            const char *recipients)
175 {
176     InternetAddressList *list;
177
178     list = internet_address_list_parse_string (recipients);
179     if (list == NULL)
180         return NULL;
181
182     return add_recipients_for_address_list (message, config, type, list);
183 }
184
185 int
186 notmuch_reply_command (void *ctx, int argc, char *argv[])
187 {
188     notmuch_config_t *config;
189     notmuch_database_t *notmuch;
190     notmuch_query_t *query;
191     GMimeMessage *reply;
192     char *query_string;
193     notmuch_messages_t *messages;
194     notmuch_message_t *message;
195     int ret = 0;
196     const char *subject, *recipients, *from_addr = NULL;
197     const char *in_reply_to, *orig_references, *references;
198     char *reply_headers;
199     struct {
200         const char *header;
201         const char *fallback;
202         GMimeRecipientType recipient_type;
203     } reply_to_map[] = {
204         { "reply-to", "from", GMIME_RECIPIENT_TYPE_TO  },
205         { "to",         NULL, GMIME_RECIPIENT_TYPE_TO  },
206         { "cc",         NULL, GMIME_RECIPIENT_TYPE_CC  },
207         { "bcc",        NULL, GMIME_RECIPIENT_TYPE_BCC }
208     };
209     unsigned int i;
210
211     config = notmuch_config_open (ctx, NULL, NULL);
212     if (config == NULL)
213         return 1;
214
215     query_string = query_string_from_args (ctx, argc, argv);
216     if (query_string == NULL) {
217         fprintf (stderr, "Out of memory\n");
218         return 1;
219     }
220
221     if (*query_string == '\0') {
222         fprintf (stderr, "Error: notmuch reply requires at least one search term.\n");
223         return 1;
224     }
225
226     notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
227                                      NOTMUCH_DATABASE_MODE_READ_ONLY);
228     if (notmuch == NULL)
229         return 1;
230
231     query = notmuch_query_create (notmuch, query_string);
232     if (query == NULL) {
233         fprintf (stderr, "Out of memory\n");
234         return 1;
235     }
236
237     for (messages = notmuch_query_search_messages (query, 0, -1);
238          notmuch_messages_has_more (messages);
239          notmuch_messages_advance (messages))
240     {
241         message = notmuch_messages_get (messages);
242
243         /* The 1 means we want headers in a "pretty" order. */
244         reply = g_mime_message_new (1);
245         if (reply == NULL) {
246             fprintf (stderr, "Out of memory\n");
247             return 1;
248         }
249
250         subject = notmuch_message_get_header (message, "subject");
251
252         if (strncasecmp (subject, "Re:", 3))
253             subject = talloc_asprintf (ctx, "Re: %s", subject);
254         g_mime_message_set_subject (reply, subject);
255
256         for (i = 0; i < ARRAY_SIZE (reply_to_map); i++) {
257             const char *addr;
258
259             recipients = notmuch_message_get_header (message,
260                                                      reply_to_map[i].header);
261             if ((recipients == NULL || recipients[0] == '\0') && reply_to_map[i].fallback)
262                 recipients = notmuch_message_get_header (message,
263                                                          reply_to_map[i].fallback);
264
265             addr = add_recipients_for_string (reply, config,
266                                               reply_to_map[i].recipient_type,
267                                               recipients);
268             if (from_addr == NULL)
269                 from_addr = addr;
270         }
271
272         if (from_addr == NULL)
273             from_addr = notmuch_config_get_user_primary_email (config);
274
275         from_addr = talloc_asprintf (ctx, "%s <%s>",
276                                      notmuch_config_get_user_name (config),
277                                      from_addr);
278         g_mime_object_set_header (GMIME_OBJECT (reply),
279                                   "From", from_addr);
280
281         g_mime_object_set_header (GMIME_OBJECT (reply), "Bcc",
282                            notmuch_config_get_user_primary_email (config));
283
284         in_reply_to = talloc_asprintf (ctx, "<%s>",
285                              notmuch_message_get_message_id (message));
286
287         g_mime_object_set_header (GMIME_OBJECT (reply),
288                                   "In-Reply-To", in_reply_to);
289
290         orig_references = notmuch_message_get_header (message, "references");
291         references = talloc_asprintf (ctx, "%s%s%s",
292                                       orig_references ? orig_references : "",
293                                       orig_references ? " " : "",
294                                       in_reply_to);
295         g_mime_object_set_header (GMIME_OBJECT (reply),
296                                   "References", references);
297
298         reply_headers = g_mime_object_to_string (GMIME_OBJECT (reply));
299         printf ("%s", reply_headers);
300         free (reply_headers);
301
302         g_object_unref (G_OBJECT (reply));
303         reply = NULL;
304
305         printf ("On %s, %s wrote:\n",
306                 notmuch_message_get_header (message, "date"),
307                 notmuch_message_get_header (message, "from"));
308
309         show_message_body (notmuch_message_get_filename (message), reply_part);
310
311         notmuch_message_destroy (message);
312     }
313
314     notmuch_query_destroy (query);
315     notmuch_database_close (notmuch);
316
317     return ret;
318 }