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"
27 show_reply_headers (GMimeMessage *message)
29 GMimeStream *stream_stdout = NULL;
31 stream_stdout = g_mime_stream_file_new (stdout);
33 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
34 /* Output RFC 2822 formatted (and RFC 2047 encoded) headers. */
35 g_mime_object_write_to_stream (GMIME_OBJECT(message), stream_stdout);
36 g_object_unref(stream_stdout);
41 format_part_reply (mime_node_t *node)
45 if (node->envelope_file) {
46 printf ("On %s, %s wrote:\n",
47 notmuch_message_get_header (node->envelope_file, "date"),
48 notmuch_message_get_header (node->envelope_file, "from"));
49 } else if (GMIME_IS_MESSAGE (node->part)) {
50 GMimeMessage *message = GMIME_MESSAGE (node->part);
51 InternetAddressList *recipients;
52 const char *recipients_string;
54 printf ("> From: %s\n", g_mime_message_get_sender (message));
55 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO);
56 recipients_string = internet_address_list_to_string (recipients, 0);
57 if (recipients_string)
60 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC);
61 recipients_string = internet_address_list_to_string (recipients, 0);
62 if (recipients_string)
65 printf ("> Subject: %s\n", g_mime_message_get_subject (message));
66 printf ("> Date: %s\n", g_mime_message_get_date_as_string (message));
68 } else if (GMIME_IS_PART (node->part)) {
69 GMimeContentType *content_type = g_mime_object_get_content_type (node->part);
70 GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (node->part);
72 if (g_mime_content_type_is_type (content_type, "application", "pgp-encrypted") ||
73 g_mime_content_type_is_type (content_type, "application", "pgp-signature")) {
74 /* Ignore PGP/MIME cruft parts */
75 } else if (g_mime_content_type_is_type (content_type, "text", "*") &&
76 !g_mime_content_type_is_type (content_type, "text", "html")) {
77 GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
78 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
79 show_text_part_content (node->part, stream_stdout, NOTMUCH_SHOW_TEXT_PART_REPLY);
80 g_object_unref(stream_stdout);
81 } else if (disposition &&
82 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0) {
83 const char *filename = g_mime_part_get_filename (GMIME_PART (node->part));
84 printf ("Attachment: %s (%s)\n", filename,
85 g_mime_content_type_to_string (content_type));
87 printf ("Non-text part: %s\n",
88 g_mime_content_type_to_string (content_type));
92 for (i = 0; i < node->nchildren; i++)
93 format_part_reply (mime_node_child (node, i));
97 USER_ADDRESS_IN_STRING,
98 STRING_IN_USER_ADDRESS,
99 STRING_IS_USER_ADDRESS,
102 /* Match given string against given address according to mode. */
103 static notmuch_bool_t
104 match_address (const char *str, const char *address, address_match_t mode)
107 case USER_ADDRESS_IN_STRING:
108 return strcasestr (str, address) != NULL;
109 case STRING_IN_USER_ADDRESS:
110 return strcasestr (address, str) != NULL;
111 case STRING_IS_USER_ADDRESS:
112 return strcasecmp (address, str) == 0;
118 /* Match given string against user's configured "primary" and "other"
119 * addresses according to mode. */
121 address_match (const char *str, notmuch_config_t *config, address_match_t mode)
127 if (!str || *str == '\0')
130 primary = notmuch_config_get_user_primary_email (config);
131 if (match_address (str, primary, mode))
134 other = notmuch_config_get_user_other_email (config, &other_len);
135 for (i = 0; i < other_len; i++) {
136 if (match_address (str, other[i], mode))
143 /* Does the given string contain an address configured as one of the
144 * user's "primary" or "other" addresses. If so, return the matching
145 * address, NULL otherwise. */
147 user_address_in_string (const char *str, notmuch_config_t *config)
149 return address_match (str, config, USER_ADDRESS_IN_STRING);
152 /* Do any of the addresses configured as one of the user's "primary"
153 * or "other" addresses contain the given string. If so, return the
154 * matching address, NULL otherwise. */
156 string_in_user_address (const char *str, notmuch_config_t *config)
158 return address_match (str, config, STRING_IN_USER_ADDRESS);
161 /* Is the given address configured as one of the user's "primary" or
162 * "other" addresses. */
163 static notmuch_bool_t
164 address_is_users (const char *address, notmuch_config_t *config)
166 return address_match (address, config, STRING_IS_USER_ADDRESS) != NULL;
169 /* Scan addresses in 'list'.
171 * If 'message' is non-NULL, then for each address in 'list' that is
172 * not configured as one of the user's addresses in 'config', add that
173 * address to 'message' as an address of 'type'.
175 * If 'user_from' is non-NULL and *user_from is NULL, *user_from will
176 * be set to the first address encountered in 'list' that is the
179 * Return the number of addresses added to 'message'. (If 'message' is
180 * NULL, the function returns 0 by definition.)
183 scan_address_list (InternetAddressList *list,
184 notmuch_config_t *config,
185 GMimeMessage *message,
186 GMimeRecipientType type,
187 const char **user_from)
189 InternetAddress *address;
193 for (i = 0; i < internet_address_list_length (list); i++) {
194 address = internet_address_list_get_address (list, i);
195 if (INTERNET_ADDRESS_IS_GROUP (address)) {
196 InternetAddressGroup *group;
197 InternetAddressList *group_list;
199 group = INTERNET_ADDRESS_GROUP (address);
200 group_list = internet_address_group_get_members (group);
201 if (group_list == NULL)
204 n += scan_address_list (group_list, config, message, type, user_from);
206 InternetAddressMailbox *mailbox;
210 mailbox = INTERNET_ADDRESS_MAILBOX (address);
212 name = internet_address_get_name (address);
213 addr = internet_address_mailbox_get_addr (mailbox);
215 if (address_is_users (addr, config)) {
216 if (user_from && *user_from == NULL)
218 } else if (message) {
219 g_mime_message_add_recipient (message, type, name, addr);
228 /* Scan addresses in 'recipients'.
230 * See the documentation of scan_address_list() above. This function
231 * does exactly the same, but converts 'recipients' to an
232 * InternetAddressList first.
235 scan_address_string (const char *recipients,
236 notmuch_config_t *config,
237 GMimeMessage *message,
238 GMimeRecipientType type,
239 const char **user_from)
241 InternetAddressList *list;
243 if (recipients == NULL)
246 list = internet_address_list_parse_string (recipients);
250 return scan_address_list (list, config, message, type, user_from);
253 /* Does the address in the Reply-To header of 'message' already appear
254 * in either the 'To' or 'Cc' header of the message?
257 reply_to_header_is_redundant (notmuch_message_t *message)
259 const char *reply_to, *to, *cc, *addr;
260 InternetAddressList *list;
261 InternetAddress *address;
262 InternetAddressMailbox *mailbox;
264 reply_to = notmuch_message_get_header (message, "reply-to");
265 if (reply_to == NULL || *reply_to == '\0')
268 list = internet_address_list_parse_string (reply_to);
270 if (internet_address_list_length (list) != 1)
273 address = internet_address_list_get_address (list, 0);
274 if (INTERNET_ADDRESS_IS_GROUP (address))
277 mailbox = INTERNET_ADDRESS_MAILBOX (address);
278 addr = internet_address_mailbox_get_addr (mailbox);
280 to = notmuch_message_get_header (message, "to");
281 cc = notmuch_message_get_header (message, "cc");
283 if ((to && strstr (to, addr) != 0) ||
284 (cc && strstr (cc, addr) != 0))
292 /* Augment the recipients of 'reply' from the "Reply-to:", "From:",
293 * "To:", "Cc:", and "Bcc:" headers of 'message'.
295 * If 'reply_all' is true, use sender and all recipients, otherwise
296 * scan the headers for the first that contains something other than
297 * the user's addresses and add the recipients from this header
298 * (typically this would be reply-to-sender, but also handles reply to
299 * user's own message in a sensible way).
301 * If any of the user's addresses were found in these headers, the
302 * first of these returned, otherwise NULL is returned.
305 add_recipients_from_message (GMimeMessage *reply,
306 notmuch_config_t *config,
307 notmuch_message_t *message,
308 notmuch_bool_t reply_all)
312 const char *fallback;
313 GMimeRecipientType recipient_type;
315 { "reply-to", "from", GMIME_RECIPIENT_TYPE_TO },
316 { "to", NULL, GMIME_RECIPIENT_TYPE_TO },
317 { "cc", NULL, GMIME_RECIPIENT_TYPE_CC },
318 { "bcc", NULL, GMIME_RECIPIENT_TYPE_BCC }
320 const char *from_addr = NULL;
324 /* Some mailing lists munge the Reply-To header despite it being A Bad
325 * Thing, see http://www.unicom.com/pw/reply-to-harmful.html
327 * The munging is easy to detect, because it results in a
328 * redundant reply-to header, (with an address that already exists
329 * in either To or Cc). So in this case, we ignore the Reply-To
330 * field and use the From header. This ensures the original sender
331 * will get the reply even if not subscribed to the list. Note
332 * that the address in the Reply-To header will always appear in
335 if (reply_to_header_is_redundant (message)) {
336 reply_to_map[0].header = "from";
337 reply_to_map[0].fallback = NULL;
340 for (i = 0; i < ARRAY_SIZE (reply_to_map); i++) {
341 const char *recipients;
343 recipients = notmuch_message_get_header (message,
344 reply_to_map[i].header);
345 if ((recipients == NULL || recipients[0] == '\0') && reply_to_map[i].fallback)
346 recipients = notmuch_message_get_header (message,
347 reply_to_map[i].fallback);
349 n += scan_address_string (recipients, config, reply,
350 reply_to_map[i].recipient_type, &from_addr);
352 if (!reply_all && n) {
353 /* Stop adding new recipients in reply-to-sender mode if
354 * we have added some recipient(s) above.
356 * This also handles the case of user replying to his own
357 * message, where reply-to/from is not a recipient. In
358 * this case there may be more than one recipient even if
359 * not replying to all.
363 /* From address and some recipients are enough, bail out. */
373 guess_from_received_header (notmuch_config_t *config, notmuch_message_t *message)
375 const char *addr, *received, *by;
376 char *mta,*ptr,*token;
379 const char *delim=". \t";
382 const char *to_headers[] = {
388 /* sadly, there is no standard way to find out to which email
389 * address a mail was delivered - what is in the headers depends
390 * on the MTAs used along the way. So we are trying a number of
391 * heuristics which hopefully will answer this question.
393 * We only got here if none of the users email addresses are in
394 * the To: or Cc: header. From here we try the following in order:
395 * 1) check for an Envelope-to: header
396 * 2) check for an X-Original-To: header
397 * 3) check for a Delivered-To: header
398 * 4) check for a (for <email@add.res>) clause in Received: headers
399 * 5) check for the domain part of known email addresses in the
400 * 'by' part of Received headers
401 * If none of these work, we give up and return NULL
403 for (i = 0; i < ARRAY_SIZE (to_headers); i++) {
404 const char *tohdr = notmuch_message_get_header (message, to_headers[i]);
406 /* Note: tohdr potentially contains a list of email addresses. */
407 addr = user_address_in_string (tohdr, config);
412 /* We get the concatenated Received: headers and search from the
413 * front (last Received: header added) and try to extract from
414 * them indications to which email address this message was
416 * The Received: header is special in our get_header function
417 * and is always concatenated.
419 received = notmuch_message_get_header (message, "received");
420 if (received == NULL)
423 /* First we look for a " for <email@add.res>" in the received
426 ptr = strstr (received, " for ");
428 /* Note: ptr potentially contains a list of email addresses. */
429 addr = user_address_in_string (ptr, config);
433 /* Finally, we parse all the " by MTA ..." headers to guess the
434 * email address that this was originally delivered to.
435 * We extract just the MTA here by removing leading whitespace and
436 * assuming that the MTA name ends at the next whitespace.
437 * We test for *(by+4) to be non-'\0' to make sure there's
438 * something there at all - and then assume that the first
439 * whitespace delimited token that follows is the receiving
440 * system in this step of the receive chain
443 while((by = strstr (by, " by ")) != NULL) {
448 token = strtok(mta," \t");
453 /* Now extract the last two components of the MTA host name
457 while ((ptr = strsep (&token, delim)) != NULL) {
465 /* Recombine domain and tld and look for it among the configured
467 * This time we have a known domain name and nothing else - so
468 * the test is the other way around: we check if this is a
469 * substring of one of the email addresses.
473 addr = string_in_user_address (domain, config);
485 static GMimeMessage *
486 create_reply_message(void *ctx,
487 notmuch_config_t *config,
488 notmuch_message_t *message,
489 notmuch_bool_t reply_all)
491 const char *subject, *from_addr = NULL;
492 const char *in_reply_to, *orig_references, *references;
494 /* The 1 means we want headers in a "pretty" order. */
495 GMimeMessage *reply = g_mime_message_new (1);
497 fprintf (stderr, "Out of memory\n");
501 subject = notmuch_message_get_header (message, "subject");
503 if (strncasecmp (subject, "Re:", 3))
504 subject = talloc_asprintf (ctx, "Re: %s", subject);
505 g_mime_message_set_subject (reply, subject);
508 from_addr = add_recipients_from_message (reply, config,
511 if (from_addr == NULL)
512 from_addr = guess_from_received_header (config, message);
514 if (from_addr == NULL)
515 from_addr = notmuch_config_get_user_primary_email (config);
517 from_addr = talloc_asprintf (ctx, "%s <%s>",
518 notmuch_config_get_user_name (config),
520 g_mime_object_set_header (GMIME_OBJECT (reply),
523 in_reply_to = talloc_asprintf (ctx, "<%s>",
524 notmuch_message_get_message_id (message));
526 g_mime_object_set_header (GMIME_OBJECT (reply),
527 "In-Reply-To", in_reply_to);
529 orig_references = notmuch_message_get_header (message, "references");
530 if (!orig_references)
531 /* Treat errors like missing References headers. */
532 orig_references = "";
533 references = talloc_asprintf (ctx, "%s%s%s",
534 *orig_references ? orig_references : "",
535 *orig_references ? " " : "",
537 g_mime_object_set_header (GMIME_OBJECT (reply),
538 "References", references);
544 notmuch_reply_format_default(void *ctx,
545 notmuch_config_t *config,
546 notmuch_query_t *query,
547 notmuch_show_params_t *params,
548 notmuch_bool_t reply_all,
549 unused (sprinter_t *sp))
552 notmuch_messages_t *messages;
553 notmuch_message_t *message;
556 for (messages = notmuch_query_search_messages (query);
557 notmuch_messages_valid (messages);
558 notmuch_messages_move_to_next (messages))
560 message = notmuch_messages_get (messages);
562 reply = create_reply_message (ctx, config, message, reply_all);
564 /* If reply creation failed, we're out of memory, so don't
565 * bother trying any more messages.
568 notmuch_message_destroy (message);
572 show_reply_headers (reply);
574 g_object_unref (G_OBJECT (reply));
577 if (mime_node_open (ctx, message, &(params->crypto), &root) == NOTMUCH_STATUS_SUCCESS) {
578 format_part_reply (root);
582 notmuch_message_destroy (message);
588 notmuch_reply_format_sprinter(void *ctx,
589 notmuch_config_t *config,
590 notmuch_query_t *query,
591 notmuch_show_params_t *params,
592 notmuch_bool_t reply_all,
596 notmuch_messages_t *messages;
597 notmuch_message_t *message;
600 if (notmuch_query_count_messages (query) != 1) {
601 fprintf (stderr, "Error: search term did not match precisely one message.\n");
605 messages = notmuch_query_search_messages (query);
606 message = notmuch_messages_get (messages);
607 if (mime_node_open (ctx, message, &(params->crypto), &node) != NOTMUCH_STATUS_SUCCESS)
610 reply = create_reply_message (ctx, config, message, reply_all);
616 /* The headers of the reply message we've created */
617 sp->map_key (sp, "reply-headers");
618 format_headers_sprinter (sp, reply, TRUE);
619 g_object_unref (G_OBJECT (reply));
622 /* Start the original */
623 sp->map_key (sp, "original");
624 format_part_sprinter (ctx, sp, node, TRUE, TRUE);
628 notmuch_message_destroy (message);
633 /* This format is currently tuned for a git send-email --notmuch hook */
635 notmuch_reply_format_headers_only(void *ctx,
636 notmuch_config_t *config,
637 notmuch_query_t *query,
638 unused (notmuch_show_params_t *params),
639 notmuch_bool_t reply_all,
640 unused (sprinter_t *sp))
643 notmuch_messages_t *messages;
644 notmuch_message_t *message;
645 const char *in_reply_to, *orig_references, *references;
648 for (messages = notmuch_query_search_messages (query);
649 notmuch_messages_valid (messages);
650 notmuch_messages_move_to_next (messages))
652 message = notmuch_messages_get (messages);
654 /* The 0 means we do not want headers in a "pretty" order. */
655 reply = g_mime_message_new (0);
657 fprintf (stderr, "Out of memory\n");
661 in_reply_to = talloc_asprintf (ctx, "<%s>",
662 notmuch_message_get_message_id (message));
664 g_mime_object_set_header (GMIME_OBJECT (reply),
665 "In-Reply-To", in_reply_to);
668 orig_references = notmuch_message_get_header (message, "references");
670 /* We print In-Reply-To followed by References because git format-patch treats them
671 * specially. Git does not interpret the other headers specially
673 references = talloc_asprintf (ctx, "%s%s%s",
674 orig_references ? orig_references : "",
675 orig_references ? " " : "",
677 g_mime_object_set_header (GMIME_OBJECT (reply),
678 "References", references);
680 (void)add_recipients_from_message (reply, config, message, reply_all);
682 reply_headers = g_mime_object_to_string (GMIME_OBJECT (reply));
683 printf ("%s", reply_headers);
684 free (reply_headers);
686 g_object_unref (G_OBJECT (reply));
689 notmuch_message_destroy (message);
702 notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[])
704 notmuch_database_t *notmuch;
705 notmuch_query_t *query;
707 int opt_index, ret = 0;
708 int (*reply_format_func) (void *ctx,
709 notmuch_config_t *config,
710 notmuch_query_t *query,
711 notmuch_show_params_t *params,
712 notmuch_bool_t reply_all,
713 struct sprinter *sp);
714 notmuch_show_params_t params = {
721 int format = FORMAT_DEFAULT;
722 int reply_all = TRUE;
723 struct sprinter *sp = NULL;
725 notmuch_opt_desc_t options[] = {
726 { NOTMUCH_OPT_KEYWORD, &format, "format", 'f',
727 (notmuch_keyword_t []){ { "default", FORMAT_DEFAULT },
728 { "json", FORMAT_JSON },
729 { "sexp", FORMAT_SEXP },
730 { "headers-only", FORMAT_HEADERS_ONLY },
732 { NOTMUCH_OPT_INT, ¬much_format_version, "format-version", 0, 0 },
733 { NOTMUCH_OPT_KEYWORD, &reply_all, "reply-to", 'r',
734 (notmuch_keyword_t []){ { "all", TRUE },
737 { NOTMUCH_OPT_BOOLEAN, ¶ms.crypto.decrypt, "decrypt", 'd', 0 },
741 opt_index = parse_arguments (argc, argv, options, 1);
743 /* diagnostics already printed */
747 if (format == FORMAT_HEADERS_ONLY) {
748 reply_format_func = notmuch_reply_format_headers_only;
749 } else if (format == FORMAT_JSON) {
750 reply_format_func = notmuch_reply_format_sprinter;
751 sp = sprinter_json_create (config, stdout);
752 } else if (format == FORMAT_SEXP) {
753 reply_format_func = notmuch_reply_format_sprinter;
754 sp = sprinter_sexp_create (config, stdout);
756 reply_format_func = notmuch_reply_format_default;
759 notmuch_exit_if_unsupported_format ();
761 query_string = query_string_from_args (config, argc-opt_index, argv+opt_index);
762 if (query_string == NULL) {
763 fprintf (stderr, "Out of memory\n");
767 if (*query_string == '\0') {
768 fprintf (stderr, "Error: notmuch reply requires at least one search term.\n");
772 if (notmuch_database_open (notmuch_config_get_database_path (config),
773 NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much))
776 query = notmuch_query_create (notmuch, query_string);
778 fprintf (stderr, "Out of memory\n");
782 if (reply_format_func (config, config, query, ¶ms, reply_all, sp) != 0)
785 notmuch_crypto_cleanup (¶ms.crypto);
786 notmuch_query_destroy (query);
787 notmuch_database_destroy (notmuch);