]> git.notmuchmail.org Git - notmuch/blob - notmuch-reply.c
f217577e8f9b6762e49d682ecfc88a6c4eeddaee
[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 static void
96 add_recipients_for_address_list (GMimeMessage *message,
97                                  notmuch_config_t *config,
98                                  GMimeRecipientType type,
99                                  InternetAddressList *list)
100 {
101     InternetAddress *address;
102     int i;
103
104     for (i = 0; i < internet_address_list_length (list); i++) {
105         address = internet_address_list_get_address (list, i);
106         if (INTERNET_ADDRESS_IS_GROUP (address)) {
107             InternetAddressGroup *group;
108             InternetAddressList *group_list;
109
110             group = INTERNET_ADDRESS_GROUP (address);
111             group_list = internet_address_group_get_members (group);
112             if (group_list == NULL)
113                 continue;
114
115             add_recipients_for_address_list (message, config,
116                                              type, group_list);
117         } else {
118             InternetAddressMailbox *mailbox;
119             const char *name;
120             const char *addr;
121
122             mailbox = INTERNET_ADDRESS_MAILBOX (address);
123
124             name = internet_address_get_name (address);
125             addr = internet_address_mailbox_get_addr (mailbox);
126
127             if (! address_is_users (addr, config))
128                 g_mime_message_add_recipient (message, type, name, addr);
129         }
130     }
131 }
132
133 static void
134 add_recipients_for_string (GMimeMessage *message,
135                            notmuch_config_t *config,
136                            GMimeRecipientType type,
137                            const char *recipients)
138 {
139     InternetAddressList *list;
140
141     list = internet_address_list_parse_string (recipients);
142     if (list == NULL)
143         return;
144
145     add_recipients_for_address_list (message, config, type, list);
146 }
147
148 int
149 notmuch_reply_command (void *ctx, int argc, char *argv[])
150 {
151     notmuch_config_t *config;
152     notmuch_database_t *notmuch;
153     notmuch_query_t *query;
154     GMimeMessage *reply;
155     char *query_string;
156     notmuch_messages_t *messages;
157     notmuch_message_t *message;
158     int ret = 0;
159     const char *subject, *recipients;
160     const char *in_reply_to, *orig_references, *references;
161     char *reply_headers;
162     struct {
163         const char *header;
164         GMimeRecipientType recipient_type;
165     } reply_to_map[] = {
166         { "from", GMIME_RECIPIENT_TYPE_TO  },
167         { "to",   GMIME_RECIPIENT_TYPE_TO  },
168         { "cc",   GMIME_RECIPIENT_TYPE_CC  },
169         { "bcc",  GMIME_RECIPIENT_TYPE_BCC }
170     };
171     unsigned int i;
172
173     config = notmuch_config_open (ctx, NULL, NULL);
174     if (config == NULL)
175         return 1;
176
177     notmuch = notmuch_database_open (notmuch_config_get_database_path (config));
178     if (notmuch == NULL)
179         return 1;
180
181     query_string = query_string_from_args (ctx, argc, argv);
182     if (query_string == NULL) {
183         fprintf (stderr, "Out of memory\n");
184         return 1;
185     }
186
187     query = notmuch_query_create (notmuch, query_string);
188     if (query == NULL) {
189         fprintf (stderr, "Out of memory\n");
190         return 1;
191     }
192
193     for (messages = notmuch_query_search_messages (query);
194          notmuch_messages_has_more (messages);
195          notmuch_messages_advance (messages))
196     {
197         message = notmuch_messages_get (messages);
198
199         /* The 1 means we want headers in a "pretty" order. */
200         reply = g_mime_message_new (1);
201         if (reply == NULL) {
202             fprintf (stderr, "Out of memory\n");
203             return 1;
204         }
205
206         /* XXX: We need a configured email address (or addresses) for
207          * the user here, so that we can prevent replying to the user,
208          * and also call _mime_message_set_sender to set From: (either
209          * from the first "owned" address mentioned as a recipient in
210          * the original message, or else some default address).
211          */
212
213         subject = notmuch_message_get_header (message, "subject");
214
215         if (strncasecmp (subject, "Re:", 3))
216             subject = talloc_asprintf (ctx, "Re: %s", subject);
217         g_mime_message_set_subject (reply, subject);
218
219         for (i = 0; i < ARRAY_SIZE (reply_to_map); i++) {
220             recipients = notmuch_message_get_header (message,
221                                                      reply_to_map[i].header);
222             add_recipients_for_string (reply, config,
223                                        reply_to_map[i].recipient_type,
224                                        recipients);
225         }
226
227         in_reply_to = talloc_asprintf (ctx, "<%s>",
228                              notmuch_message_get_message_id (message));
229
230         g_mime_object_set_header (GMIME_OBJECT (reply),
231                                   "In-Reply-To", in_reply_to);
232
233         orig_references = notmuch_message_get_header (message, "references");
234         references = talloc_asprintf (ctx, "%s%s%s",
235                                       orig_references ? orig_references : "",
236                                       orig_references ? " " : "",
237                                       in_reply_to);
238         g_mime_object_set_header (GMIME_OBJECT (reply),
239                                   "References", references);
240
241         reply_headers = g_mime_object_to_string (GMIME_OBJECT (reply));
242         printf ("%s", reply_headers);
243         free (reply_headers);
244
245         g_object_unref (G_OBJECT (reply));
246         reply = NULL;
247
248         printf ("On %s, %s wrote:\n",
249                 notmuch_message_get_header (message, "date"),
250                 notmuch_message_get_header (message, "from"));
251
252         show_message_body (notmuch_message_get_filename (message), reply_part);
253
254         notmuch_message_destroy (message);
255     }
256
257     notmuch_query_destroy (query);
258     notmuch_database_close (notmuch);
259
260     return ret;
261 }