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;
33 charset = g_mime_object_get_content_type_parameter (part, "charset");
34 stream_stdout = g_mime_stream_file_new (stdout);
36 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
37 stream_filter = g_mime_stream_filter_new(stream_stdout);
39 g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter),
40 g_mime_filter_charset_new(charset, "UTF-8"));
43 g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter),
44 g_mime_filter_reply_new(TRUE));
45 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
46 if (wrapper && stream_filter)
47 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
49 g_object_unref(stream_filter);
51 g_object_unref(stream_stdout);
55 reply_part (GMimeObject *part, int *part_count)
57 GMimeContentDisposition *disposition;
58 GMimeContentType *content_type;
61 disposition = g_mime_object_get_content_disposition (part);
63 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
65 const char *filename = g_mime_part_get_filename (GMIME_PART (part));
66 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
68 if (g_mime_content_type_is_type (content_type, "text", "*") &&
69 !g_mime_content_type_is_type (content_type, "text", "html"))
71 reply_part_content (part);
75 printf ("Attachment: %s (%s)\n", filename,
76 g_mime_content_type_to_string (content_type));
82 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
84 if (g_mime_content_type_is_type (content_type, "text", "*") &&
85 !g_mime_content_type_is_type (content_type, "text", "html"))
87 reply_part_content (part);
91 printf ("Non-text part: %s\n",
92 g_mime_content_type_to_string (content_type));
96 /* Is the given address configured as one of the user's "personal" or
97 * "other" addresses. */
99 address_is_users (const char *address, notmuch_config_t *config)
105 primary = notmuch_config_get_user_primary_email (config);
106 if (strcasecmp (primary, address) == 0)
109 other = notmuch_config_get_user_other_email (config, &other_len);
110 for (i = 0; i < other_len; i++)
111 if (strcasecmp (other[i], address) == 0)
117 /* For each address in 'list' that is not configured as one of the
118 * user's addresses in 'config', add that address to 'message' as an
121 * The first address encountered that *is* the user's address will be
122 * returned, (otherwise NULL is returned).
125 add_recipients_for_address_list (GMimeMessage *message,
126 notmuch_config_t *config,
127 GMimeRecipientType type,
128 InternetAddressList *list)
130 InternetAddress *address;
132 const char *ret = NULL;
134 for (i = 0; i < internet_address_list_length (list); i++) {
135 address = internet_address_list_get_address (list, i);
136 if (INTERNET_ADDRESS_IS_GROUP (address)) {
137 InternetAddressGroup *group;
138 InternetAddressList *group_list;
140 group = INTERNET_ADDRESS_GROUP (address);
141 group_list = internet_address_group_get_members (group);
142 if (group_list == NULL)
145 add_recipients_for_address_list (message, config,
148 InternetAddressMailbox *mailbox;
152 mailbox = INTERNET_ADDRESS_MAILBOX (address);
154 name = internet_address_get_name (address);
155 addr = internet_address_mailbox_get_addr (mailbox);
157 if (address_is_users (addr, config)) {
161 g_mime_message_add_recipient (message, type, name, addr);
169 /* For each address in 'recipients' that is not configured as one of
170 * the user's addresses in 'config', add that address to 'message' as
171 * an address of 'type'.
173 * The first address encountered that *is* the user's address will be
174 * returned, (otherwise NULL is returned).
177 add_recipients_for_string (GMimeMessage *message,
178 notmuch_config_t *config,
179 GMimeRecipientType type,
180 const char *recipients)
182 InternetAddressList *list;
184 list = internet_address_list_parse_string (recipients);
188 return add_recipients_for_address_list (message, config, type, list);
191 /* Some mailing lists munge the Reply-To header despite it being A Bad
192 * Thing, see http://www.unicom.com/pw/reply-to-harmful.html
194 * This function detects such munging so that reasonable headers can be
195 * generated anyway. Returns 1 if munged, else 0.
197 * The current logic is fairly naive, Reply-To is diagnosed as munged if
198 * it contains exactly one address, and this address is also present in
199 * the To or Cc fields.
202 mailing_list_munged_reply_to (notmuch_message_t *message)
204 const char *header, *addr;
205 InternetAddressList *list;
206 InternetAddress *address;
207 InternetAddressMailbox *mailbox;
209 header = notmuch_message_get_header (message, "reply-to");
210 list = internet_address_list_parse_string (header);
212 if (internet_address_list_length (list) != 1)
215 address = internet_address_list_get_address (list, 0);
216 if (INTERNET_ADDRESS_IS_GROUP (address))
219 mailbox = INTERNET_ADDRESS_MAILBOX (address);
220 addr = internet_address_mailbox_get_addr (mailbox);
222 /* Note that strcasestr() is a GNU extension, strstr() might be sufficient */
223 if (strcasestr (notmuch_message_get_header (message, "to"), addr) != 0 ||
224 strcasestr (notmuch_message_get_header (message, "cc"), addr) != 0)
232 /* Augments the recipients of reply from the headers of message.
234 * If any of the user's addresses were found in these headers, the first
235 * of these returned, otherwise NULL is returned.
238 add_recipients_from_message (GMimeMessage *reply,
239 notmuch_config_t *config,
240 notmuch_message_t *message)
244 const char *fallback;
245 GMimeRecipientType recipient_type;
247 { "reply-to", "from", GMIME_RECIPIENT_TYPE_TO },
248 { "to", NULL, GMIME_RECIPIENT_TYPE_TO },
249 { "cc", NULL, GMIME_RECIPIENT_TYPE_CC },
250 { "bcc", NULL, GMIME_RECIPIENT_TYPE_BCC }
252 const char *from_addr = NULL;
255 /* When we have detected Reply-To munging, we ignore the Reply-To
256 * field (because it appears in the To or Cc headers) and use the
257 * From header so that person will get pinged and will actually
258 * receive the response if not subscribed to the list. Note that
259 * under no circumstances does this fail to reply to the address in
260 * the Reply-To header.
262 if (mailing_list_munged_reply_to (message)) {
263 reply_to_map[0].header = "from";
264 reply_to_map[0].fallback = NULL;
267 for (i = 0; i < ARRAY_SIZE (reply_to_map); i++) {
268 const char *addr, *recipients;
270 recipients = notmuch_message_get_header (message,
271 reply_to_map[i].header);
272 if ((recipients == NULL || recipients[0] == '\0') && reply_to_map[i].fallback)
273 recipients = notmuch_message_get_header (message,
274 reply_to_map[i].fallback);
276 addr = add_recipients_for_string (reply, config,
277 reply_to_map[i].recipient_type,
279 if (from_addr == NULL)
287 notmuch_reply_format_default(void *ctx, notmuch_config_t *config, notmuch_query_t *query)
290 notmuch_messages_t *messages;
291 notmuch_message_t *message;
292 const char *subject, *from_addr = NULL;
293 const char *in_reply_to, *orig_references, *references;
296 for (messages = notmuch_query_search_messages (query);
297 notmuch_messages_has_more (messages);
298 notmuch_messages_advance (messages))
300 message = notmuch_messages_get (messages);
302 /* The 1 means we want headers in a "pretty" order. */
303 reply = g_mime_message_new (1);
305 fprintf (stderr, "Out of memory\n");
309 subject = notmuch_message_get_header (message, "subject");
311 if (strncasecmp (subject, "Re:", 3))
312 subject = talloc_asprintf (ctx, "Re: %s", subject);
313 g_mime_message_set_subject (reply, subject);
315 from_addr = add_recipients_from_message (reply, config, message);
317 if (from_addr == NULL)
318 from_addr = notmuch_config_get_user_primary_email (config);
320 from_addr = talloc_asprintf (ctx, "%s <%s>",
321 notmuch_config_get_user_name (config),
323 g_mime_object_set_header (GMIME_OBJECT (reply),
326 g_mime_object_set_header (GMIME_OBJECT (reply), "Bcc",
327 notmuch_config_get_user_primary_email (config));
329 in_reply_to = talloc_asprintf (ctx, "<%s>",
330 notmuch_message_get_message_id (message));
332 g_mime_object_set_header (GMIME_OBJECT (reply),
333 "In-Reply-To", in_reply_to);
335 orig_references = notmuch_message_get_header (message, "references");
336 references = talloc_asprintf (ctx, "%s%s%s",
337 orig_references ? orig_references : "",
338 orig_references ? " " : "",
340 g_mime_object_set_header (GMIME_OBJECT (reply),
341 "References", references);
343 reply_headers = g_mime_object_to_string (GMIME_OBJECT (reply));
344 printf ("%s", reply_headers);
345 free (reply_headers);
347 g_object_unref (G_OBJECT (reply));
350 printf ("On %s, %s wrote:\n",
351 notmuch_message_get_header (message, "date"),
352 notmuch_message_get_header (message, "from"));
354 show_message_body (notmuch_message_get_filename (message), reply_part);
356 notmuch_message_destroy (message);
361 /* This format is currently tuned for a git send-email --notmuch hook */
363 notmuch_reply_format_headers_only(void *ctx, notmuch_config_t *config, notmuch_query_t *query)
366 notmuch_messages_t *messages;
367 notmuch_message_t *message;
368 const char *in_reply_to, *orig_references, *references;
371 for (messages = notmuch_query_search_messages (query);
372 notmuch_messages_has_more (messages);
373 notmuch_messages_advance (messages))
375 message = notmuch_messages_get (messages);
377 /* The 0 means we do not want headers in a "pretty" order. */
378 reply = g_mime_message_new (0);
380 fprintf (stderr, "Out of memory\n");
384 in_reply_to = talloc_asprintf (ctx, "<%s>",
385 notmuch_message_get_message_id (message));
387 g_mime_object_set_header (GMIME_OBJECT (reply),
388 "In-Reply-To", in_reply_to);
391 orig_references = notmuch_message_get_header (message, "references");
393 /* We print In-Reply-To followed by References because git format-patch treats them
394 * specially. Git does not interpret the other headers specially
396 references = talloc_asprintf (ctx, "%s%s%s",
397 orig_references ? orig_references : "",
398 orig_references ? " " : "",
400 g_mime_object_set_header (GMIME_OBJECT (reply),
401 "References", references);
403 (void)add_recipients_from_message (reply, config, message);
405 g_mime_object_set_header (GMIME_OBJECT (reply), "Bcc",
406 notmuch_config_get_user_primary_email (config));
408 reply_headers = g_mime_object_to_string (GMIME_OBJECT (reply));
409 printf ("%s", reply_headers);
410 free (reply_headers);
412 g_object_unref (G_OBJECT (reply));
415 notmuch_message_destroy (message);
421 notmuch_reply_command (void *ctx, int argc, char *argv[])
423 notmuch_config_t *config;
424 notmuch_database_t *notmuch;
425 notmuch_query_t *query;
426 char *opt, *query_string;
428 int (*reply_format_func)(void *ctx, notmuch_config_t *config, notmuch_query_t *query);
430 reply_format_func = notmuch_reply_format_default;
432 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
433 if (strcmp (argv[i], "--") == 0) {
437 if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
438 opt = argv[i] + sizeof ("--format=") - 1;
439 if (strcmp (opt, "default") == 0) {
440 reply_format_func = notmuch_reply_format_default;
441 } else if (strcmp (opt, "headers-only") == 0) {
442 reply_format_func = notmuch_reply_format_headers_only;
444 fprintf (stderr, "Invalid value for --format: %s\n", opt);
448 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
456 config = notmuch_config_open (ctx, NULL, NULL);
460 query_string = query_string_from_args (ctx, argc, argv);
461 if (query_string == NULL) {
462 fprintf (stderr, "Out of memory\n");
466 if (*query_string == '\0') {
467 fprintf (stderr, "Error: notmuch reply requires at least one search term.\n");
471 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
472 NOTMUCH_DATABASE_MODE_READ_ONLY);
476 query = notmuch_query_create (notmuch, query_string);
478 fprintf (stderr, "Out of memory\n");
482 if (reply_format_func (ctx, config, query) != 0)
485 notmuch_query_destroy (query);
486 notmuch_database_close (notmuch);