]> git.notmuchmail.org Git - notmuch/blob - notmuch-reply.c
notmuch new: Don't ignore files with mtime of 0.
[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         GMimeRecipientType recipient_type;
186     } reply_to_map[] = {
187         { "from", GMIME_RECIPIENT_TYPE_TO  },
188         { "to",   GMIME_RECIPIENT_TYPE_TO  },
189         { "cc",   GMIME_RECIPIENT_TYPE_CC  },
190         { "bcc",  GMIME_RECIPIENT_TYPE_BCC }
191     };
192     unsigned int i;
193
194     config = notmuch_config_open (ctx, NULL, NULL);
195     if (config == NULL)
196         return 1;
197
198     notmuch = notmuch_database_open (notmuch_config_get_database_path (config));
199     if (notmuch == NULL)
200         return 1;
201
202     query_string = query_string_from_args (ctx, argc, argv);
203     if (query_string == NULL) {
204         fprintf (stderr, "Out of memory\n");
205         return 1;
206     }
207
208     query = notmuch_query_create (notmuch, query_string);
209     if (query == NULL) {
210         fprintf (stderr, "Out of memory\n");
211         return 1;
212     }
213
214     for (messages = notmuch_query_search_messages (query);
215          notmuch_messages_has_more (messages);
216          notmuch_messages_advance (messages))
217     {
218         message = notmuch_messages_get (messages);
219
220         /* The 1 means we want headers in a "pretty" order. */
221         reply = g_mime_message_new (1);
222         if (reply == NULL) {
223             fprintf (stderr, "Out of memory\n");
224             return 1;
225         }
226
227         subject = notmuch_message_get_header (message, "subject");
228
229         if (strncasecmp (subject, "Re:", 3))
230             subject = talloc_asprintf (ctx, "Re: %s", subject);
231         g_mime_message_set_subject (reply, subject);
232
233         for (i = 0; i < ARRAY_SIZE (reply_to_map); i++) {
234             const char *addr;
235
236             recipients = notmuch_message_get_header (message,
237                                                      reply_to_map[i].header);
238             addr = add_recipients_for_string (reply, config,
239                                               reply_to_map[i].recipient_type,
240                                               recipients);
241             if (from_addr == NULL)
242                 from_addr = addr;
243         }
244
245         if (from_addr == NULL)
246             from_addr = notmuch_config_get_user_primary_email (config);
247
248         from_addr = talloc_asprintf (ctx, "%s <%s>",
249                                      notmuch_config_get_user_name (config),
250                                      from_addr);
251         g_mime_object_set_header (GMIME_OBJECT (reply),
252                                   "From", from_addr);
253
254         in_reply_to = talloc_asprintf (ctx, "<%s>",
255                              notmuch_message_get_message_id (message));
256
257         g_mime_object_set_header (GMIME_OBJECT (reply),
258                                   "In-Reply-To", in_reply_to);
259
260         orig_references = notmuch_message_get_header (message, "references");
261         references = talloc_asprintf (ctx, "%s%s%s",
262                                       orig_references ? orig_references : "",
263                                       orig_references ? " " : "",
264                                       in_reply_to);
265         g_mime_object_set_header (GMIME_OBJECT (reply),
266                                   "References", references);
267
268         reply_headers = g_mime_object_to_string (GMIME_OBJECT (reply));
269         printf ("%s", reply_headers);
270         free (reply_headers);
271
272         g_object_unref (G_OBJECT (reply));
273         reply = NULL;
274
275         printf ("On %s, %s wrote:\n",
276                 notmuch_message_get_header (message, "date"),
277                 notmuch_message_get_header (message, "from"));
278
279         show_message_body (notmuch_message_get_filename (message), reply_part);
280
281         notmuch_message_destroy (message);
282     }
283
284     notmuch_query_destroy (query);
285     notmuch_database_close (notmuch);
286
287     return ret;
288 }