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