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"
25 #include "gmime-filter-headers.h"
28 reply_headers_message_part (GMimeMessage *message);
31 reply_part_content (GMimeObject *part);
33 static const notmuch_show_format_t format_reply = {
36 "", NULL, reply_headers_message_part, ">\n",
50 show_reply_headers (GMimeMessage *message)
52 GMimeStream *stream_stdout = NULL, *stream_filter = NULL;
54 stream_stdout = g_mime_stream_file_new (stdout);
56 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
57 stream_filter = g_mime_stream_filter_new(stream_stdout);
59 g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter),
60 g_mime_filter_headers_new());
61 g_mime_object_write_to_stream(GMIME_OBJECT(message), stream_filter);
62 g_object_unref(stream_filter);
64 g_object_unref(stream_stdout);
69 reply_headers_message_part (GMimeMessage *message)
71 InternetAddressList *recipients;
72 const char *recipients_string;
74 printf ("> From: %s\n", g_mime_message_get_sender (message));
75 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO);
76 recipients_string = internet_address_list_to_string (recipients, 0);
77 if (recipients_string)
80 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC);
81 recipients_string = internet_address_list_to_string (recipients, 0);
82 if (recipients_string)
85 printf ("> Subject: %s\n", g_mime_message_get_subject (message));
86 printf ("> Date: %s\n", g_mime_message_get_date_as_string (message));
91 reply_part_content (GMimeObject *part)
93 GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
94 GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (part);
96 if (g_mime_content_type_is_type (content_type, "multipart", "*") ||
97 g_mime_content_type_is_type (content_type, "message", "rfc822"))
99 /* Output nothing, since multipart subparts will be handled individually. */
101 else if (g_mime_content_type_is_type (content_type, "application", "pgp-encrypted") ||
102 g_mime_content_type_is_type (content_type, "application", "pgp-signature"))
104 /* Ignore PGP/MIME cruft parts */
106 else if (g_mime_content_type_is_type (content_type, "text", "*") &&
107 !g_mime_content_type_is_type (content_type, "text", "html"))
109 GMimeStream *stream_stdout = NULL, *stream_filter = NULL;
110 GMimeDataWrapper *wrapper;
113 charset = g_mime_object_get_content_type_parameter (part, "charset");
114 stream_stdout = g_mime_stream_file_new (stdout);
116 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
117 stream_filter = g_mime_stream_filter_new(stream_stdout);
119 g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter),
120 g_mime_filter_charset_new(charset, "UTF-8"));
123 g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter),
124 g_mime_filter_reply_new(TRUE));
125 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
126 if (wrapper && stream_filter)
127 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
129 g_object_unref(stream_filter);
131 g_object_unref(stream_stdout);
136 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
138 const char *filename = g_mime_part_get_filename (GMIME_PART (part));
139 printf ("Attachment: %s (%s)\n", filename,
140 g_mime_content_type_to_string (content_type));
144 printf ("Non-text part: %s\n",
145 g_mime_content_type_to_string (content_type));
150 /* Is the given address configured as one of the user's "personal" or
151 * "other" addresses. */
153 address_is_users (const char *address, notmuch_config_t *config)
159 primary = notmuch_config_get_user_primary_email (config);
160 if (strcasecmp (primary, address) == 0)
163 other = notmuch_config_get_user_other_email (config, &other_len);
164 for (i = 0; i < other_len; i++)
165 if (strcasecmp (other[i], address) == 0)
171 /* Scan addresses in 'list'.
173 * If 'message' is non-NULL, then for each address in 'list' that is
174 * not configured as one of the user's addresses in 'config', add that
175 * address to 'message' as an address of 'type'.
177 * If 'user_from' is non-NULL and *user_from is NULL, *user_from will
178 * be set to the first address encountered in 'list' that is the
181 * Return the number of addresses added to 'message'. (If 'message' is
182 * NULL, the function returns 0 by definition.)
185 scan_address_list (InternetAddressList *list,
186 notmuch_config_t *config,
187 GMimeMessage *message,
188 GMimeRecipientType type,
189 const char **user_from)
191 InternetAddress *address;
195 for (i = 0; i < internet_address_list_length (list); i++) {
196 address = internet_address_list_get_address (list, i);
197 if (INTERNET_ADDRESS_IS_GROUP (address)) {
198 InternetAddressGroup *group;
199 InternetAddressList *group_list;
201 group = INTERNET_ADDRESS_GROUP (address);
202 group_list = internet_address_group_get_members (group);
203 if (group_list == NULL)
206 n += scan_address_list (group_list, config, message, type, user_from);
208 InternetAddressMailbox *mailbox;
212 mailbox = INTERNET_ADDRESS_MAILBOX (address);
214 name = internet_address_get_name (address);
215 addr = internet_address_mailbox_get_addr (mailbox);
217 if (address_is_users (addr, config)) {
218 if (user_from && *user_from == NULL)
220 } else if (message) {
221 g_mime_message_add_recipient (message, type, name, addr);
230 /* Scan addresses in 'recipients'.
232 * See the documentation of scan_address_list() above. This function
233 * does exactly the same, but converts 'recipients' to an
234 * InternetAddressList first.
237 scan_address_string (const char *recipients,
238 notmuch_config_t *config,
239 GMimeMessage *message,
240 GMimeRecipientType type,
241 const char **user_from)
243 InternetAddressList *list;
245 if (recipients == NULL)
248 list = internet_address_list_parse_string (recipients);
252 return scan_address_list (list, config, message, type, user_from);
255 /* Does the address in the Reply-To header of 'message' already appear
256 * in either the 'To' or 'Cc' header of the message?
259 reply_to_header_is_redundant (notmuch_message_t *message)
261 const char *reply_to, *to, *cc, *addr;
262 InternetAddressList *list;
263 InternetAddress *address;
264 InternetAddressMailbox *mailbox;
266 reply_to = notmuch_message_get_header (message, "reply-to");
267 if (reply_to == NULL || *reply_to == '\0')
270 list = internet_address_list_parse_string (reply_to);
272 if (internet_address_list_length (list) != 1)
275 address = internet_address_list_get_address (list, 0);
276 if (INTERNET_ADDRESS_IS_GROUP (address))
279 mailbox = INTERNET_ADDRESS_MAILBOX (address);
280 addr = internet_address_mailbox_get_addr (mailbox);
282 to = notmuch_message_get_header (message, "to");
283 cc = notmuch_message_get_header (message, "cc");
285 if ((to && strstr (to, addr) != 0) ||
286 (cc && strstr (cc, addr) != 0))
294 /* Augment the recipients of 'reply' from the "Reply-to:", "From:",
295 * "To:", "Cc:", and "Bcc:" headers of 'message'.
297 * If 'reply_all' is true, use sender and all recipients, otherwise
298 * scan the headers for the first that contains something other than
299 * the user's addresses and add the recipients from this header
300 * (typically this would be reply-to-sender, but also handles reply to
301 * user's own message in a sensible way).
303 * If any of the user's addresses were found in these headers, the
304 * first of these returned, otherwise NULL is returned.
307 add_recipients_from_message (GMimeMessage *reply,
308 notmuch_config_t *config,
309 notmuch_message_t *message,
310 notmuch_bool_t reply_all)
314 const char *fallback;
315 GMimeRecipientType recipient_type;
317 { "reply-to", "from", GMIME_RECIPIENT_TYPE_TO },
318 { "to", NULL, GMIME_RECIPIENT_TYPE_TO },
319 { "cc", NULL, GMIME_RECIPIENT_TYPE_CC },
320 { "bcc", NULL, GMIME_RECIPIENT_TYPE_BCC }
322 const char *from_addr = NULL;
326 /* Some mailing lists munge the Reply-To header despite it being A Bad
327 * Thing, see http://www.unicom.com/pw/reply-to-harmful.html
329 * The munging is easy to detect, because it results in a
330 * redundant reply-to header, (with an address that already exists
331 * in either To or Cc). So in this case, we ignore the Reply-To
332 * field and use the From header. This ensures the original sender
333 * will get the reply even if not subscribed to the list. Note
334 * that the address in the Reply-To header will always appear in
337 if (reply_to_header_is_redundant (message)) {
338 reply_to_map[0].header = "from";
339 reply_to_map[0].fallback = NULL;
342 for (i = 0; i < ARRAY_SIZE (reply_to_map); i++) {
343 const char *recipients;
345 recipients = notmuch_message_get_header (message,
346 reply_to_map[i].header);
347 if ((recipients == NULL || recipients[0] == '\0') && reply_to_map[i].fallback)
348 recipients = notmuch_message_get_header (message,
349 reply_to_map[i].fallback);
351 n += scan_address_string (recipients, config, reply,
352 reply_to_map[i].recipient_type, &from_addr);
354 if (!reply_all && n) {
355 /* Stop adding new recipients in reply-to-sender mode if
356 * we have added some recipient(s) above.
358 * This also handles the case of user replying to his own
359 * message, where reply-to/from is not a recipient. In
360 * this case there may be more than one recipient even if
361 * not replying to all.
365 /* From address and some recipients are enough, bail out. */
375 guess_from_received_header (notmuch_config_t *config, notmuch_message_t *message)
377 const char *received,*primary,*by;
380 char *mta,*ptr,*token;
383 const char *delim=". \t";
384 size_t i,j,other_len;
386 const char *to_headers[] = {"Envelope-to", "X-Original-To"};
388 primary = notmuch_config_get_user_primary_email (config);
389 other = notmuch_config_get_user_other_email (config, &other_len);
391 /* sadly, there is no standard way to find out to which email
392 * address a mail was delivered - what is in the headers depends
393 * on the MTAs used along the way. So we are trying a number of
394 * heuristics which hopefully will answer this question.
396 * We only got here if none of the users email addresses are in
397 * the To: or Cc: header. From here we try the following in order:
398 * 1) check for an Envelope-to: header
399 * 2) check for an X-Original-To: header
400 * 3) check for a (for <email@add.res>) clause in Received: headers
401 * 4) check for the domain part of known email addresses in the
402 * 'by' part of Received headers
403 * If none of these work, we give up and return NULL
405 for (i = 0; i < sizeof(to_headers)/sizeof(*to_headers); i++) {
406 tohdr = xstrdup(notmuch_message_get_header (message, to_headers[i]));
407 if (tohdr && *tohdr) {
408 /* tohdr is potentialy a list of email addresses, so here we
409 * check if one of the email addresses is a substring of tohdr
411 if (strcasestr(tohdr, primary)) {
415 for (j = 0; j < other_len; j++)
416 if (strcasestr (tohdr, other[j])) {
424 /* We get the concatenated Received: headers and search from the
425 * front (last Received: header added) and try to extract from
426 * them indications to which email address this message was
428 * The Received: header is special in our get_header function
429 * and is always concatenated.
431 received = notmuch_message_get_header (message, "received");
432 if (received == NULL)
435 /* First we look for a " for <email@add.res>" in the received
438 ptr = strstr (received, " for ");
440 /* the text following is potentialy a list of email addresses,
441 * so again we check if one of the email addresses is a
444 if (strcasestr(ptr, primary)) {
447 for (i = 0; i < other_len; i++)
448 if (strcasestr (ptr, other[i])) {
452 /* Finally, we parse all the " by MTA ..." headers to guess the
453 * email address that this was originally delivered to.
454 * We extract just the MTA here by removing leading whitespace and
455 * assuming that the MTA name ends at the next whitespace.
456 * We test for *(by+4) to be non-'\0' to make sure there's
457 * something there at all - and then assume that the first
458 * whitespace delimited token that follows is the receiving
459 * system in this step of the receive chain
462 while((by = strstr (by, " by ")) != NULL) {
467 token = strtok(mta," \t");
472 /* Now extract the last two components of the MTA host name
476 while ((ptr = strsep (&token, delim)) != NULL) {
484 /* Recombine domain and tld and look for it among the configured
486 * This time we have a known domain name and nothing else - so
487 * the test is the other way around: we check if this is a
488 * substring of one of the email addresses.
492 if (strcasestr(primary, domain)) {
496 for (i = 0; i < other_len; i++)
497 if (strcasestr (other[i],domain)) {
508 static GMimeMessage *
509 create_reply_message(void *ctx,
510 notmuch_config_t *config,
511 notmuch_message_t *message,
512 notmuch_bool_t reply_all)
514 const char *subject, *from_addr = NULL;
515 const char *in_reply_to, *orig_references, *references;
517 /* The 1 means we want headers in a "pretty" order. */
518 GMimeMessage *reply = g_mime_message_new (1);
520 fprintf (stderr, "Out of memory\n");
524 subject = notmuch_message_get_header (message, "subject");
526 if (strncasecmp (subject, "Re:", 3))
527 subject = talloc_asprintf (ctx, "Re: %s", subject);
528 g_mime_message_set_subject (reply, subject);
531 from_addr = add_recipients_from_message (reply, config,
534 if (from_addr == NULL)
535 from_addr = guess_from_received_header (config, message);
537 if (from_addr == NULL)
538 from_addr = notmuch_config_get_user_primary_email (config);
540 from_addr = talloc_asprintf (ctx, "%s <%s>",
541 notmuch_config_get_user_name (config),
543 g_mime_object_set_header (GMIME_OBJECT (reply),
546 in_reply_to = talloc_asprintf (ctx, "<%s>",
547 notmuch_message_get_message_id (message));
549 g_mime_object_set_header (GMIME_OBJECT (reply),
550 "In-Reply-To", in_reply_to);
552 orig_references = notmuch_message_get_header (message, "references");
553 references = talloc_asprintf (ctx, "%s%s%s",
554 orig_references ? orig_references : "",
555 orig_references ? " " : "",
557 g_mime_object_set_header (GMIME_OBJECT (reply),
558 "References", references);
564 notmuch_reply_format_default(void *ctx,
565 notmuch_config_t *config,
566 notmuch_query_t *query,
567 notmuch_show_params_t *params,
568 notmuch_bool_t reply_all)
571 notmuch_messages_t *messages;
572 notmuch_message_t *message;
573 const notmuch_show_format_t *format = &format_reply;
575 for (messages = notmuch_query_search_messages (query);
576 notmuch_messages_valid (messages);
577 notmuch_messages_move_to_next (messages))
579 message = notmuch_messages_get (messages);
581 reply = create_reply_message (ctx, config, message, reply_all);
583 /* If reply creation failed, we're out of memory, so don't
584 * bother trying any more messages.
587 notmuch_message_destroy (message);
591 show_reply_headers (reply);
593 g_object_unref (G_OBJECT (reply));
596 printf ("On %s, %s wrote:\n",
597 notmuch_message_get_header (message, "date"),
598 notmuch_message_get_header (message, "from"));
600 show_message_body (message, format, params);
602 notmuch_message_destroy (message);
608 notmuch_reply_format_json(void *ctx,
609 notmuch_config_t *config,
610 notmuch_query_t *query,
611 notmuch_show_params_t *params,
612 notmuch_bool_t reply_all)
615 notmuch_messages_t *messages;
616 notmuch_message_t *message;
619 if (notmuch_query_count_messages (query) != 1) {
620 fprintf (stderr, "Error: search term did not match precisely one message.\n");
624 messages = notmuch_query_search_messages (query);
625 message = notmuch_messages_get (messages);
626 if (mime_node_open (ctx, message, params->cryptoctx, params->decrypt,
627 &node) != NOTMUCH_STATUS_SUCCESS)
630 reply = create_reply_message (ctx, config, message, reply_all);
634 /* The headers of the reply message we've created */
635 printf ("{\"reply-headers\": ");
636 format_headers_json (ctx, reply, TRUE);
637 g_object_unref (G_OBJECT (reply));
640 /* Start the original */
641 printf (", \"original\": ");
643 format_part_json (ctx, node, TRUE);
647 notmuch_message_destroy (message);
652 /* This format is currently tuned for a git send-email --notmuch hook */
654 notmuch_reply_format_headers_only(void *ctx,
655 notmuch_config_t *config,
656 notmuch_query_t *query,
657 unused (notmuch_show_params_t *params),
658 notmuch_bool_t reply_all)
661 notmuch_messages_t *messages;
662 notmuch_message_t *message;
663 const char *in_reply_to, *orig_references, *references;
666 for (messages = notmuch_query_search_messages (query);
667 notmuch_messages_valid (messages);
668 notmuch_messages_move_to_next (messages))
670 message = notmuch_messages_get (messages);
672 /* The 0 means we do not want headers in a "pretty" order. */
673 reply = g_mime_message_new (0);
675 fprintf (stderr, "Out of memory\n");
679 in_reply_to = talloc_asprintf (ctx, "<%s>",
680 notmuch_message_get_message_id (message));
682 g_mime_object_set_header (GMIME_OBJECT (reply),
683 "In-Reply-To", in_reply_to);
686 orig_references = notmuch_message_get_header (message, "references");
688 /* We print In-Reply-To followed by References because git format-patch treats them
689 * specially. Git does not interpret the other headers specially
691 references = talloc_asprintf (ctx, "%s%s%s",
692 orig_references ? orig_references : "",
693 orig_references ? " " : "",
695 g_mime_object_set_header (GMIME_OBJECT (reply),
696 "References", references);
698 (void)add_recipients_from_message (reply, config, message, reply_all);
700 reply_headers = g_mime_object_to_string (GMIME_OBJECT (reply));
701 printf ("%s", reply_headers);
702 free (reply_headers);
704 g_object_unref (G_OBJECT (reply));
707 notmuch_message_destroy (message);
719 notmuch_reply_command (void *ctx, int argc, char *argv[])
721 notmuch_config_t *config;
722 notmuch_database_t *notmuch;
723 notmuch_query_t *query;
725 int opt_index, ret = 0;
726 int (*reply_format_func)(void *ctx, notmuch_config_t *config, notmuch_query_t *query, notmuch_show_params_t *params, notmuch_bool_t reply_all);
727 notmuch_show_params_t params = { .part = -1 };
728 int format = FORMAT_DEFAULT;
729 int reply_all = TRUE;
731 notmuch_opt_desc_t options[] = {
732 { NOTMUCH_OPT_KEYWORD, &format, "format", 'f',
733 (notmuch_keyword_t []){ { "default", FORMAT_DEFAULT },
734 { "json", FORMAT_JSON },
735 { "headers-only", FORMAT_HEADERS_ONLY },
737 { NOTMUCH_OPT_KEYWORD, &reply_all, "reply-to", 'r',
738 (notmuch_keyword_t []){ { "all", TRUE },
741 { NOTMUCH_OPT_BOOLEAN, ¶ms.decrypt, "decrypt", 'd', 0 },
745 opt_index = parse_arguments (argc, argv, options, 1);
747 /* diagnostics already printed */
751 if (format == FORMAT_HEADERS_ONLY)
752 reply_format_func = notmuch_reply_format_headers_only;
753 else if (format == FORMAT_JSON)
754 reply_format_func = notmuch_reply_format_json;
756 reply_format_func = notmuch_reply_format_default;
758 if (params.decrypt) {
759 #ifdef GMIME_ATLEAST_26
760 /* TODO: GMimePasswordRequestFunc */
761 params.cryptoctx = g_mime_gpg_context_new (NULL, "gpg");
763 GMimeSession* session = g_object_new (g_mime_session_get_type(), NULL);
764 params.cryptoctx = g_mime_gpg_context_new (session, "gpg");
766 if (params.cryptoctx) {
767 g_mime_gpg_context_set_always_trust ((GMimeGpgContext*) params.cryptoctx, FALSE);
769 params.decrypt = FALSE;
770 fprintf (stderr, "Failed to construct gpg context.\n");
772 #ifndef GMIME_ATLEAST_26
773 g_object_unref (session);
777 config = notmuch_config_open (ctx, NULL, NULL);
781 query_string = query_string_from_args (ctx, argc-opt_index, argv+opt_index);
782 if (query_string == NULL) {
783 fprintf (stderr, "Out of memory\n");
787 if (*query_string == '\0') {
788 fprintf (stderr, "Error: notmuch reply requires at least one search term.\n");
792 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
793 NOTMUCH_DATABASE_MODE_READ_ONLY);
797 query = notmuch_query_create (notmuch, query_string);
799 fprintf (stderr, "Out of memory\n");
803 if (reply_format_func (ctx, config, query, ¶ms, reply_all) != 0)
806 notmuch_query_destroy (query);
807 notmuch_database_close (notmuch);
809 if (params.cryptoctx)
810 g_object_unref(params.cryptoctx);