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-headers.h"
28 show_reply_headers (GMimeMessage *message)
30 GMimeStream *stream_stdout = NULL, *stream_filter = NULL;
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_headers_new());
39 g_mime_object_write_to_stream(GMIME_OBJECT(message), stream_filter);
40 g_object_unref(stream_filter);
42 g_object_unref(stream_stdout);
47 format_part_reply (mime_node_t *node)
51 if (node->envelope_file) {
52 printf ("On %s, %s wrote:\n",
53 notmuch_message_get_header (node->envelope_file, "date"),
54 notmuch_message_get_header (node->envelope_file, "from"));
55 } else if (GMIME_IS_MESSAGE (node->part)) {
56 GMimeMessage *message = GMIME_MESSAGE (node->part);
57 InternetAddressList *recipients;
58 const char *recipients_string;
60 printf ("> From: %s\n", g_mime_message_get_sender (message));
61 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO);
62 recipients_string = internet_address_list_to_string (recipients, 0);
63 if (recipients_string)
66 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC);
67 recipients_string = internet_address_list_to_string (recipients, 0);
68 if (recipients_string)
71 printf ("> Subject: %s\n", g_mime_message_get_subject (message));
72 printf ("> Date: %s\n", g_mime_message_get_date_as_string (message));
74 } else if (GMIME_IS_PART (node->part)) {
75 GMimeContentType *content_type = g_mime_object_get_content_type (node->part);
76 GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (node->part);
78 if (g_mime_content_type_is_type (content_type, "application", "pgp-encrypted") ||
79 g_mime_content_type_is_type (content_type, "application", "pgp-signature")) {
80 /* Ignore PGP/MIME cruft parts */
81 } else if (g_mime_content_type_is_type (content_type, "text", "*") &&
82 !g_mime_content_type_is_type (content_type, "text", "html")) {
83 GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
84 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
85 show_text_part_content (node->part, stream_stdout, NOTMUCH_SHOW_TEXT_PART_REPLY);
86 g_object_unref(stream_stdout);
87 } else if (disposition &&
88 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0) {
89 const char *filename = g_mime_part_get_filename (GMIME_PART (node->part));
90 printf ("Attachment: %s (%s)\n", filename,
91 g_mime_content_type_to_string (content_type));
93 printf ("Non-text part: %s\n",
94 g_mime_content_type_to_string (content_type));
98 for (i = 0; i < node->nchildren; i++)
99 format_part_reply (mime_node_child (node, i));
103 USER_ADDRESS_IN_STRING,
104 STRING_IN_USER_ADDRESS,
105 STRING_IS_USER_ADDRESS,
108 /* Match given string against given address according to mode. */
109 static notmuch_bool_t
110 match_address (const char *str, const char *address, address_match_t mode)
113 case USER_ADDRESS_IN_STRING:
114 return strcasestr (str, address) != NULL;
115 case STRING_IN_USER_ADDRESS:
116 return strcasestr (address, str) != NULL;
117 case STRING_IS_USER_ADDRESS:
118 return strcasecmp (address, str) == 0;
124 /* Match given string against user's configured "primary" and "other"
125 * addresses according to mode. */
127 address_match (const char *str, notmuch_config_t *config, address_match_t mode)
133 if (!str || *str == '\0')
136 primary = notmuch_config_get_user_primary_email (config);
137 if (match_address (str, primary, mode))
140 other = notmuch_config_get_user_other_email (config, &other_len);
141 for (i = 0; i < other_len; i++) {
142 if (match_address (str, other[i], mode))
149 /* Does the given string contain an address configured as one of the
150 * user's "primary" or "other" addresses. If so, return the matching
151 * address, NULL otherwise. */
153 user_address_in_string (const char *str, notmuch_config_t *config)
155 return address_match (str, config, USER_ADDRESS_IN_STRING);
158 /* Do any of the addresses configured as one of the user's "primary"
159 * or "other" addresses contain the given string. If so, return the
160 * matching address, NULL otherwise. */
162 string_in_user_address (const char *str, notmuch_config_t *config)
164 return address_match (str, config, STRING_IN_USER_ADDRESS);
167 /* Is the given address configured as one of the user's "primary" or
168 * "other" addresses. */
169 static notmuch_bool_t
170 address_is_users (const char *address, notmuch_config_t *config)
172 return address_match (address, config, STRING_IS_USER_ADDRESS) != NULL;
175 /* Scan addresses in 'list'.
177 * If 'message' is non-NULL, then for each address in 'list' that is
178 * not configured as one of the user's addresses in 'config', add that
179 * address to 'message' as an address of 'type'.
181 * If 'user_from' is non-NULL and *user_from is NULL, *user_from will
182 * be set to the first address encountered in 'list' that is the
185 * Return the number of addresses added to 'message'. (If 'message' is
186 * NULL, the function returns 0 by definition.)
189 scan_address_list (InternetAddressList *list,
190 notmuch_config_t *config,
191 GMimeMessage *message,
192 GMimeRecipientType type,
193 const char **user_from)
195 InternetAddress *address;
199 for (i = 0; i < internet_address_list_length (list); i++) {
200 address = internet_address_list_get_address (list, i);
201 if (INTERNET_ADDRESS_IS_GROUP (address)) {
202 InternetAddressGroup *group;
203 InternetAddressList *group_list;
205 group = INTERNET_ADDRESS_GROUP (address);
206 group_list = internet_address_group_get_members (group);
207 if (group_list == NULL)
210 n += scan_address_list (group_list, config, message, type, user_from);
212 InternetAddressMailbox *mailbox;
216 mailbox = INTERNET_ADDRESS_MAILBOX (address);
218 name = internet_address_get_name (address);
219 addr = internet_address_mailbox_get_addr (mailbox);
221 if (address_is_users (addr, config)) {
222 if (user_from && *user_from == NULL)
224 } else if (message) {
225 g_mime_message_add_recipient (message, type, name, addr);
234 /* Scan addresses in 'recipients'.
236 * See the documentation of scan_address_list() above. This function
237 * does exactly the same, but converts 'recipients' to an
238 * InternetAddressList first.
241 scan_address_string (const char *recipients,
242 notmuch_config_t *config,
243 GMimeMessage *message,
244 GMimeRecipientType type,
245 const char **user_from)
247 InternetAddressList *list;
249 if (recipients == NULL)
252 list = internet_address_list_parse_string (recipients);
256 return scan_address_list (list, config, message, type, user_from);
259 /* Does the address in the Reply-To header of 'message' already appear
260 * in either the 'To' or 'Cc' header of the message?
263 reply_to_header_is_redundant (notmuch_message_t *message)
265 const char *reply_to, *to, *cc, *addr;
266 InternetAddressList *list;
267 InternetAddress *address;
268 InternetAddressMailbox *mailbox;
270 reply_to = notmuch_message_get_header (message, "reply-to");
271 if (reply_to == NULL || *reply_to == '\0')
274 list = internet_address_list_parse_string (reply_to);
276 if (internet_address_list_length (list) != 1)
279 address = internet_address_list_get_address (list, 0);
280 if (INTERNET_ADDRESS_IS_GROUP (address))
283 mailbox = INTERNET_ADDRESS_MAILBOX (address);
284 addr = internet_address_mailbox_get_addr (mailbox);
286 to = notmuch_message_get_header (message, "to");
287 cc = notmuch_message_get_header (message, "cc");
289 if ((to && strstr (to, addr) != 0) ||
290 (cc && strstr (cc, addr) != 0))
298 /* Augment the recipients of 'reply' from the "Reply-to:", "From:",
299 * "To:", "Cc:", and "Bcc:" headers of 'message'.
301 * If 'reply_all' is true, use sender and all recipients, otherwise
302 * scan the headers for the first that contains something other than
303 * the user's addresses and add the recipients from this header
304 * (typically this would be reply-to-sender, but also handles reply to
305 * user's own message in a sensible way).
307 * If any of the user's addresses were found in these headers, the
308 * first of these returned, otherwise NULL is returned.
311 add_recipients_from_message (GMimeMessage *reply,
312 notmuch_config_t *config,
313 notmuch_message_t *message,
314 notmuch_bool_t reply_all)
318 const char *fallback;
319 GMimeRecipientType recipient_type;
321 { "reply-to", "from", GMIME_RECIPIENT_TYPE_TO },
322 { "to", NULL, GMIME_RECIPIENT_TYPE_TO },
323 { "cc", NULL, GMIME_RECIPIENT_TYPE_CC },
324 { "bcc", NULL, GMIME_RECIPIENT_TYPE_BCC }
326 const char *from_addr = NULL;
330 /* Some mailing lists munge the Reply-To header despite it being A Bad
331 * Thing, see http://www.unicom.com/pw/reply-to-harmful.html
333 * The munging is easy to detect, because it results in a
334 * redundant reply-to header, (with an address that already exists
335 * in either To or Cc). So in this case, we ignore the Reply-To
336 * field and use the From header. This ensures the original sender
337 * will get the reply even if not subscribed to the list. Note
338 * that the address in the Reply-To header will always appear in
341 if (reply_to_header_is_redundant (message)) {
342 reply_to_map[0].header = "from";
343 reply_to_map[0].fallback = NULL;
346 for (i = 0; i < ARRAY_SIZE (reply_to_map); i++) {
347 const char *recipients;
349 recipients = notmuch_message_get_header (message,
350 reply_to_map[i].header);
351 if ((recipients == NULL || recipients[0] == '\0') && reply_to_map[i].fallback)
352 recipients = notmuch_message_get_header (message,
353 reply_to_map[i].fallback);
355 n += scan_address_string (recipients, config, reply,
356 reply_to_map[i].recipient_type, &from_addr);
358 if (!reply_all && n) {
359 /* Stop adding new recipients in reply-to-sender mode if
360 * we have added some recipient(s) above.
362 * This also handles the case of user replying to his own
363 * message, where reply-to/from is not a recipient. In
364 * this case there may be more than one recipient even if
365 * not replying to all.
369 /* From address and some recipients are enough, bail out. */
379 guess_from_received_header (notmuch_config_t *config, notmuch_message_t *message)
381 const char *addr, *received, *by;
382 char *mta,*ptr,*token;
385 const char *delim=". \t";
388 const char *to_headers[] = {
394 /* sadly, there is no standard way to find out to which email
395 * address a mail was delivered - what is in the headers depends
396 * on the MTAs used along the way. So we are trying a number of
397 * heuristics which hopefully will answer this question.
399 * We only got here if none of the users email addresses are in
400 * the To: or Cc: header. From here we try the following in order:
401 * 1) check for an Envelope-to: header
402 * 2) check for an X-Original-To: header
403 * 3) check for a Delivered-To: header
404 * 4) check for a (for <email@add.res>) clause in Received: headers
405 * 5) check for the domain part of known email addresses in the
406 * 'by' part of Received headers
407 * If none of these work, we give up and return NULL
409 for (i = 0; i < ARRAY_SIZE (to_headers); i++) {
410 const char *tohdr = notmuch_message_get_header (message, to_headers[i]);
412 /* Note: tohdr potentially contains a list of email addresses. */
413 addr = user_address_in_string (tohdr, config);
418 /* We get the concatenated Received: headers and search from the
419 * front (last Received: header added) and try to extract from
420 * them indications to which email address this message was
422 * The Received: header is special in our get_header function
423 * and is always concatenated.
425 received = notmuch_message_get_header (message, "received");
426 if (received == NULL)
429 /* First we look for a " for <email@add.res>" in the received
432 ptr = strstr (received, " for ");
434 /* Note: ptr potentially contains a list of email addresses. */
435 addr = user_address_in_string (ptr, config);
439 /* Finally, we parse all the " by MTA ..." headers to guess the
440 * email address that this was originally delivered to.
441 * We extract just the MTA here by removing leading whitespace and
442 * assuming that the MTA name ends at the next whitespace.
443 * We test for *(by+4) to be non-'\0' to make sure there's
444 * something there at all - and then assume that the first
445 * whitespace delimited token that follows is the receiving
446 * system in this step of the receive chain
449 while((by = strstr (by, " by ")) != NULL) {
454 token = strtok(mta," \t");
459 /* Now extract the last two components of the MTA host name
463 while ((ptr = strsep (&token, delim)) != NULL) {
471 /* Recombine domain and tld and look for it among the configured
473 * This time we have a known domain name and nothing else - so
474 * the test is the other way around: we check if this is a
475 * substring of one of the email addresses.
479 addr = string_in_user_address (domain, config);
491 static GMimeMessage *
492 create_reply_message(void *ctx,
493 notmuch_config_t *config,
494 notmuch_message_t *message,
495 notmuch_bool_t reply_all)
497 const char *subject, *from_addr = NULL;
498 const char *in_reply_to, *orig_references, *references;
500 /* The 1 means we want headers in a "pretty" order. */
501 GMimeMessage *reply = g_mime_message_new (1);
503 fprintf (stderr, "Out of memory\n");
507 subject = notmuch_message_get_header (message, "subject");
509 if (strncasecmp (subject, "Re:", 3))
510 subject = talloc_asprintf (ctx, "Re: %s", subject);
511 g_mime_message_set_subject (reply, subject);
514 from_addr = add_recipients_from_message (reply, config,
517 if (from_addr == NULL)
518 from_addr = guess_from_received_header (config, message);
520 if (from_addr == NULL)
521 from_addr = notmuch_config_get_user_primary_email (config);
523 from_addr = talloc_asprintf (ctx, "%s <%s>",
524 notmuch_config_get_user_name (config),
526 g_mime_object_set_header (GMIME_OBJECT (reply),
529 in_reply_to = talloc_asprintf (ctx, "<%s>",
530 notmuch_message_get_message_id (message));
532 g_mime_object_set_header (GMIME_OBJECT (reply),
533 "In-Reply-To", in_reply_to);
535 orig_references = notmuch_message_get_header (message, "references");
536 references = talloc_asprintf (ctx, "%s%s%s",
537 orig_references ? orig_references : "",
538 orig_references ? " " : "",
540 g_mime_object_set_header (GMIME_OBJECT (reply),
541 "References", references);
547 notmuch_reply_format_default(void *ctx,
548 notmuch_config_t *config,
549 notmuch_query_t *query,
550 notmuch_show_params_t *params,
551 notmuch_bool_t reply_all,
552 unused (sprinter_t *sp))
555 notmuch_messages_t *messages;
556 notmuch_message_t *message;
559 for (messages = notmuch_query_search_messages (query);
560 notmuch_messages_valid (messages);
561 notmuch_messages_move_to_next (messages))
563 message = notmuch_messages_get (messages);
565 reply = create_reply_message (ctx, config, message, reply_all);
567 /* If reply creation failed, we're out of memory, so don't
568 * bother trying any more messages.
571 notmuch_message_destroy (message);
575 show_reply_headers (reply);
577 g_object_unref (G_OBJECT (reply));
580 if (mime_node_open (ctx, message, &(params->crypto), &root) == NOTMUCH_STATUS_SUCCESS) {
581 format_part_reply (root);
585 notmuch_message_destroy (message);
591 notmuch_reply_format_sprinter(void *ctx,
592 notmuch_config_t *config,
593 notmuch_query_t *query,
594 notmuch_show_params_t *params,
595 notmuch_bool_t reply_all,
599 notmuch_messages_t *messages;
600 notmuch_message_t *message;
603 if (notmuch_query_count_messages (query) != 1) {
604 fprintf (stderr, "Error: search term did not match precisely one message.\n");
608 messages = notmuch_query_search_messages (query);
609 message = notmuch_messages_get (messages);
610 if (mime_node_open (ctx, message, &(params->crypto), &node) != NOTMUCH_STATUS_SUCCESS)
613 reply = create_reply_message (ctx, config, message, reply_all);
619 /* The headers of the reply message we've created */
620 sp->map_key (sp, "reply-headers");
621 format_headers_sprinter (sp, reply, TRUE);
622 g_object_unref (G_OBJECT (reply));
625 /* Start the original */
626 sp->map_key (sp, "original");
627 format_part_sprinter (ctx, sp, node, TRUE, TRUE);
631 notmuch_message_destroy (message);
636 /* This format is currently tuned for a git send-email --notmuch hook */
638 notmuch_reply_format_headers_only(void *ctx,
639 notmuch_config_t *config,
640 notmuch_query_t *query,
641 unused (notmuch_show_params_t *params),
642 notmuch_bool_t reply_all,
643 unused (sprinter_t *sp))
646 notmuch_messages_t *messages;
647 notmuch_message_t *message;
648 const char *in_reply_to, *orig_references, *references;
651 for (messages = notmuch_query_search_messages (query);
652 notmuch_messages_valid (messages);
653 notmuch_messages_move_to_next (messages))
655 message = notmuch_messages_get (messages);
657 /* The 0 means we do not want headers in a "pretty" order. */
658 reply = g_mime_message_new (0);
660 fprintf (stderr, "Out of memory\n");
664 in_reply_to = talloc_asprintf (ctx, "<%s>",
665 notmuch_message_get_message_id (message));
667 g_mime_object_set_header (GMIME_OBJECT (reply),
668 "In-Reply-To", in_reply_to);
671 orig_references = notmuch_message_get_header (message, "references");
673 /* We print In-Reply-To followed by References because git format-patch treats them
674 * specially. Git does not interpret the other headers specially
676 references = talloc_asprintf (ctx, "%s%s%s",
677 orig_references ? orig_references : "",
678 orig_references ? " " : "",
680 g_mime_object_set_header (GMIME_OBJECT (reply),
681 "References", references);
683 (void)add_recipients_from_message (reply, config, message, reply_all);
685 reply_headers = g_mime_object_to_string (GMIME_OBJECT (reply));
686 printf ("%s", reply_headers);
687 free (reply_headers);
689 g_object_unref (G_OBJECT (reply));
692 notmuch_message_destroy (message);
705 notmuch_reply_command (void *ctx, int argc, char *argv[])
707 notmuch_config_t *config;
708 notmuch_database_t *notmuch;
709 notmuch_query_t *query;
711 int opt_index, ret = 0;
712 int (*reply_format_func) (void *ctx,
713 notmuch_config_t *config,
714 notmuch_query_t *query,
715 notmuch_show_params_t *params,
716 notmuch_bool_t reply_all,
717 struct sprinter *sp);
718 notmuch_show_params_t params = {
725 int format = FORMAT_DEFAULT;
726 int reply_all = TRUE;
727 struct sprinter *sp = NULL;
729 notmuch_opt_desc_t options[] = {
730 { NOTMUCH_OPT_KEYWORD, &format, "format", 'f',
731 (notmuch_keyword_t []){ { "default", FORMAT_DEFAULT },
732 { "json", FORMAT_JSON },
733 { "sexp", FORMAT_SEXP },
734 { "headers-only", FORMAT_HEADERS_ONLY },
736 { NOTMUCH_OPT_KEYWORD, &reply_all, "reply-to", 'r',
737 (notmuch_keyword_t []){ { "all", TRUE },
740 { NOTMUCH_OPT_BOOLEAN, ¶ms.crypto.decrypt, "decrypt", 'd', 0 },
744 opt_index = parse_arguments (argc, argv, options, 1);
746 /* diagnostics already printed */
750 if (format == FORMAT_HEADERS_ONLY) {
751 reply_format_func = notmuch_reply_format_headers_only;
752 } else if (format == FORMAT_JSON) {
753 reply_format_func = notmuch_reply_format_sprinter;
754 sp = sprinter_json_create (ctx, stdout);
755 } else if (format == FORMAT_SEXP) {
756 reply_format_func = notmuch_reply_format_sprinter;
757 sp = sprinter_sexp_create (ctx, stdout);
759 reply_format_func = notmuch_reply_format_default;
762 config = notmuch_config_open (ctx, NULL, NULL);
766 query_string = query_string_from_args (ctx, argc-opt_index, argv+opt_index);
767 if (query_string == NULL) {
768 fprintf (stderr, "Out of memory\n");
772 if (*query_string == '\0') {
773 fprintf (stderr, "Error: notmuch reply requires at least one search term.\n");
777 if (notmuch_database_open (notmuch_config_get_database_path (config),
778 NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much))
781 query = notmuch_query_create (notmuch, query_string);
783 fprintf (stderr, "Out of memory\n");
787 if (reply_format_func (ctx, config, query, ¶ms, reply_all, sp) != 0)
790 notmuch_crypto_cleanup (¶ms.crypto);
791 notmuch_query_destroy (query);
792 notmuch_database_destroy (notmuch);