1 /* notmuch - Not much of an email program, (just index and search)
3 * Copyright © 2009 Carl Worth
4 * Copyright © 2009 Keith Packard
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.
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.
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/ .
19 * Authors: Carl Worth <cworth@cworth.org>
20 * Keith Packard <keithp@keithp.com>
23 #include "notmuch-client.h"
24 #include "gmime-filter-reply.h"
27 reply_part_content (GMimeObject *part)
29 GMimeStream *stream_stdout = NULL, *stream_filter = NULL;
30 GMimeDataWrapper *wrapper;
32 stream_stdout = g_mime_stream_file_new (stdout);
34 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
35 stream_filter = g_mime_stream_filter_new(stream_stdout);
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);
43 g_object_unref(stream_filter);
45 g_object_unref(stream_stdout);
49 reply_part (GMimeObject *part, int *part_count)
51 GMimeContentDisposition *disposition;
52 GMimeContentType *content_type;
55 disposition = g_mime_object_get_content_disposition (part);
57 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
59 const char *filename = g_mime_part_get_filename (GMIME_PART (part));
60 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
62 if (g_mime_content_type_is_type (content_type, "text", "*") &&
63 !g_mime_content_type_is_type (content_type, "text", "html"))
65 reply_part_content (part);
69 printf ("Attachment: %s (%s)\n", filename,
70 g_mime_content_type_to_string (content_type));
76 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
78 if (g_mime_content_type_is_type (content_type, "text", "*") &&
79 !g_mime_content_type_is_type (content_type, "text", "html"))
81 reply_part_content (part);
85 printf ("Non-text part: %s\n",
86 g_mime_content_type_to_string (content_type));
90 /* Is the given address configured as one of the user's "personal" or
91 * "other" addresses. */
93 address_is_users (const char *address, notmuch_config_t *config)
99 primary = notmuch_config_get_user_primary_email (config);
100 if (strcasecmp (primary, address) == 0)
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)
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
115 * The first address encountered that *is* the user's address will be
116 * returned, (otherwise NULL is returned).
119 add_recipients_for_address_list (GMimeMessage *message,
120 notmuch_config_t *config,
121 GMimeRecipientType type,
122 InternetAddressList *list)
124 InternetAddress *address;
126 const char *ret = NULL;
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;
134 group = INTERNET_ADDRESS_GROUP (address);
135 group_list = internet_address_group_get_members (group);
136 if (group_list == NULL)
139 add_recipients_for_address_list (message, config,
142 InternetAddressMailbox *mailbox;
146 mailbox = INTERNET_ADDRESS_MAILBOX (address);
148 name = internet_address_get_name (address);
149 addr = internet_address_mailbox_get_addr (mailbox);
151 if (address_is_users (addr, config)) {
155 g_mime_message_add_recipient (message, type, name, addr);
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'.
167 * The first address encountered that *is* the user's address will be
168 * returned, (otherwise NULL is returned).
171 add_recipients_for_string (GMimeMessage *message,
172 notmuch_config_t *config,
173 GMimeRecipientType type,
174 const char *recipients)
176 InternetAddressList *list;
178 list = internet_address_list_parse_string (recipients);
182 return add_recipients_for_address_list (message, config, type, list);
186 notmuch_reply_command (void *ctx, int argc, char *argv[])
188 notmuch_config_t *config;
189 notmuch_database_t *notmuch;
190 notmuch_query_t *query;
193 notmuch_messages_t *messages;
194 notmuch_message_t *message;
196 const char *subject, *recipients, *from_addr = NULL;
197 const char *in_reply_to, *orig_references, *references;
201 const char *fallback;
202 GMimeRecipientType recipient_type;
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 }
211 config = notmuch_config_open (ctx, NULL, NULL);
215 query_string = query_string_from_args (ctx, argc, argv);
216 if (query_string == NULL) {
217 fprintf (stderr, "Out of memory\n");
221 if (*query_string == '\0') {
222 fprintf (stderr, "Error: notmuch reply requires at least one search term.\n");
226 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
227 NOTMUCH_DATABASE_MODE_READ_ONLY);
231 query = notmuch_query_create (notmuch, query_string);
233 fprintf (stderr, "Out of memory\n");
237 for (messages = notmuch_query_search_messages (query);
238 notmuch_messages_has_more (messages);
239 notmuch_messages_advance (messages))
241 message = notmuch_messages_get (messages);
243 /* The 1 means we want headers in a "pretty" order. */
244 reply = g_mime_message_new (1);
246 fprintf (stderr, "Out of memory\n");
250 subject = notmuch_message_get_header (message, "subject");
252 if (strncasecmp (subject, "Re:", 3))
253 subject = talloc_asprintf (ctx, "Re: %s", subject);
254 g_mime_message_set_subject (reply, subject);
256 for (i = 0; i < ARRAY_SIZE (reply_to_map); i++) {
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);
265 addr = add_recipients_for_string (reply, config,
266 reply_to_map[i].recipient_type,
268 if (from_addr == NULL)
272 if (from_addr == NULL)
273 from_addr = notmuch_config_get_user_primary_email (config);
275 from_addr = talloc_asprintf (ctx, "%s <%s>",
276 notmuch_config_get_user_name (config),
278 g_mime_object_set_header (GMIME_OBJECT (reply),
281 g_mime_object_set_header (GMIME_OBJECT (reply), "Bcc",
282 notmuch_config_get_user_primary_email (config));
284 in_reply_to = talloc_asprintf (ctx, "<%s>",
285 notmuch_message_get_message_id (message));
287 g_mime_object_set_header (GMIME_OBJECT (reply),
288 "In-Reply-To", in_reply_to);
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 ? " " : "",
295 g_mime_object_set_header (GMIME_OBJECT (reply),
296 "References", references);
298 reply_headers = g_mime_object_to_string (GMIME_OBJECT (reply));
299 printf ("%s", reply_headers);
300 free (reply_headers);
302 g_object_unref (G_OBJECT (reply));
305 printf ("On %s, %s wrote:\n",
306 notmuch_message_get_header (message, "date"),
307 notmuch_message_get_header (message, "from"));
309 show_message_body (notmuch_message_get_filename (message), reply_part);
311 notmuch_message_destroy (message);
314 notmuch_query_destroy (query);
315 notmuch_database_close (notmuch);