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 https://www.gnu.org/licenses/ .
19 * Authors: Carl Worth <cworth@cworth.org>
20 * Keith Packard <keithp@keithp.com>
23 #include "notmuch-client.h"
24 #include "string-util.h"
28 show_reply_headers (GMimeStream *stream, GMimeMessage *message)
30 /* Output RFC 2822 formatted (and RFC 2047 encoded) headers. */
31 if (g_mime_object_write_to_stream (GMIME_OBJECT (message), NULL, stream) < 0) {
32 INTERNAL_ERROR ("failed to write headers to stdout\n");
37 format_part_reply (GMimeStream *stream, mime_node_t *node)
41 if (node->envelope_file) {
42 g_mime_stream_printf (stream, "On %s, %s wrote:\n",
43 notmuch_message_get_header (node->envelope_file, "date"),
44 notmuch_message_get_header (node->envelope_file, "from"));
45 } else if (GMIME_IS_MESSAGE (node->part)) {
46 GMimeMessage *message = GMIME_MESSAGE (node->part);
47 char *recipients_string;
49 g_mime_stream_printf (stream, "> From: %s\n", g_mime_message_get_from_string (message));
50 recipients_string = g_mime_message_get_address_string (message, GMIME_ADDRESS_TYPE_TO);
51 if (recipients_string)
52 g_mime_stream_printf (stream, "> To: %s\n",
54 g_free (recipients_string);
55 recipients_string = g_mime_message_get_address_string (message, GMIME_ADDRESS_TYPE_CC);
56 if (recipients_string)
57 g_mime_stream_printf (stream, "> Cc: %s\n",
59 g_free (recipients_string);
60 g_mime_stream_printf (stream, "> Subject: %s\n", g_mime_message_get_subject (message));
61 g_mime_stream_printf (stream, "> Date: %s\n", g_mime_message_get_date_string (node, message));
62 g_mime_stream_printf (stream, ">\n");
63 } else if (GMIME_IS_PART (node->part)) {
64 GMimeContentType *content_type = g_mime_object_get_content_type (node->part);
65 GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (node->part);
67 if (g_mime_content_type_is_type (content_type, "application", "pgp-encrypted") ||
68 g_mime_content_type_is_type (content_type, "application", "pgp-signature")) {
69 /* Ignore PGP/MIME cruft parts */
70 } else if (g_mime_content_type_is_type (content_type, "text", "*") &&
71 ! g_mime_content_type_is_type (content_type, "text", "html")) {
72 show_text_part_content (node->part, stream, NOTMUCH_SHOW_TEXT_PART_REPLY);
73 } else if (disposition &&
74 strcasecmp (g_mime_content_disposition_get_disposition (disposition),
75 GMIME_DISPOSITION_ATTACHMENT) == 0) {
76 const char *filename = g_mime_part_get_filename (GMIME_PART (node->part));
77 g_mime_stream_printf (stream, "Attachment: %s (%s)\n", filename,
78 g_mime_content_type_get_mime_type (content_type));
80 g_mime_stream_printf (stream, "Non-text part: %s\n",
81 g_mime_content_type_get_mime_type (content_type));
85 for (i = 0; i < node->nchildren; i++)
86 format_part_reply (stream, mime_node_child (node, i));
90 USER_ADDRESS_IN_STRING,
91 STRING_IN_USER_ADDRESS,
92 STRING_IS_USER_ADDRESS,
95 /* Match given string against given address according to mode. */
97 match_address (const char *str, const char *address, address_match_t mode)
100 case USER_ADDRESS_IN_STRING:
101 return strcasestr (str, address) != NULL;
102 case STRING_IN_USER_ADDRESS:
103 return strcasestr (address, str) != NULL;
104 case STRING_IS_USER_ADDRESS:
105 return strcasecmp (address, str) == 0;
111 /* Match given string against user's configured "primary" and "other"
112 * addresses according to mode. */
114 address_match (const char *str, notmuch_config_t *config, address_match_t mode)
120 if (! str || *str == '\0')
123 primary = notmuch_config_get_user_primary_email (config);
124 if (match_address (str, primary, mode))
127 other = notmuch_config_get_user_other_email (config, &other_len);
128 for (i = 0; i < other_len; i++) {
129 if (match_address (str, other[i], mode))
136 /* Does the given string contain an address configured as one of the
137 * user's "primary" or "other" addresses. If so, return the matching
138 * address, NULL otherwise. */
140 user_address_in_string (const char *str, notmuch_config_t *config)
142 return address_match (str, config, USER_ADDRESS_IN_STRING);
145 /* Do any of the addresses configured as one of the user's "primary"
146 * or "other" addresses contain the given string. If so, return the
147 * matching address, NULL otherwise. */
149 string_in_user_address (const char *str, notmuch_config_t *config)
151 return address_match (str, config, STRING_IN_USER_ADDRESS);
154 /* Is the given address configured as one of the user's "primary" or
155 * "other" addresses. */
157 address_is_users (const char *address, notmuch_config_t *config)
159 return address_match (address, config, STRING_IS_USER_ADDRESS) != NULL;
162 /* Scan addresses in 'list'.
164 * If 'message' is non-NULL, then for each address in 'list' that is
165 * not configured as one of the user's addresses in 'config', add that
166 * address to 'message' as an address of 'type'.
168 * If 'user_from' is non-NULL and *user_from is NULL, *user_from will
169 * be set to the first address encountered in 'list' that is the
172 * Return the number of addresses added to 'message'. (If 'message' is
173 * NULL, the function returns 0 by definition.)
176 scan_address_list (InternetAddressList *list,
177 notmuch_config_t *config,
178 GMimeMessage *message,
179 GMimeAddressType type,
180 const char **user_from)
182 InternetAddress *address;
189 for (i = 0; i < internet_address_list_length (list); i++) {
190 address = internet_address_list_get_address (list, i);
191 if (INTERNET_ADDRESS_IS_GROUP (address)) {
192 InternetAddressGroup *group;
193 InternetAddressList *group_list;
195 group = INTERNET_ADDRESS_GROUP (address);
196 group_list = internet_address_group_get_members (group);
197 n += scan_address_list (group_list, config, message, type, user_from);
199 InternetAddressMailbox *mailbox;
203 mailbox = INTERNET_ADDRESS_MAILBOX (address);
205 name = internet_address_get_name (address);
206 addr = internet_address_mailbox_get_addr (mailbox);
208 if (address_is_users (addr, config)) {
209 if (user_from && *user_from == NULL)
211 } else if (message) {
212 g_mime_message_add_mailbox (message, type, name, addr);
221 /* Does the address in the Reply-To header of 'message' already appear
222 * in either the 'To' or 'Cc' header of the message?
225 reply_to_header_is_redundant (GMimeMessage *message,
226 InternetAddressList *reply_to_list)
228 const char *addr, *reply_to;
229 InternetAddress *address;
230 InternetAddressMailbox *mailbox;
231 InternetAddressList *recipients;
235 if (reply_to_list == NULL ||
236 internet_address_list_length (reply_to_list) != 1)
239 address = internet_address_list_get_address (reply_to_list, 0);
240 if (INTERNET_ADDRESS_IS_GROUP (address))
243 mailbox = INTERNET_ADDRESS_MAILBOX (address);
244 reply_to = internet_address_mailbox_get_addr (mailbox);
246 recipients = g_mime_message_get_all_recipients (message);
248 for (i = 0; i < internet_address_list_length (recipients); i++) {
249 address = internet_address_list_get_address (recipients, i);
250 if (INTERNET_ADDRESS_IS_GROUP (address))
253 mailbox = INTERNET_ADDRESS_MAILBOX (address);
254 addr = internet_address_mailbox_get_addr (mailbox);
255 if (strcmp (addr, reply_to) == 0) {
261 g_object_unref (G_OBJECT (recipients));
266 static InternetAddressList *
267 get_sender (GMimeMessage *message)
269 InternetAddressList *reply_to_list;
271 reply_to_list = g_mime_message_get_reply_to_list (message);
273 internet_address_list_length (reply_to_list) > 0) {
275 * Some mailing lists munge the Reply-To header despite it
276 * being A Bad Thing, see
277 * http://marc.merlins.org/netrants/reply-to-harmful.html
279 * The munging is easy to detect, because it results in a
280 * redundant reply-to header, (with an address that already
281 * exists in either To or Cc). So in this case, we ignore the
282 * Reply-To field and use the From header. This ensures the
283 * original sender will get the reply even if not subscribed
284 * to the list. Note that the address in the Reply-To header
285 * will always appear in the reply if reply_all is true.
287 if (! reply_to_header_is_redundant (message, reply_to_list))
288 return reply_to_list;
291 return g_mime_message_get_from (message);
294 static InternetAddressList *
295 get_to (GMimeMessage *message)
297 return g_mime_message_get_addresses (message, GMIME_ADDRESS_TYPE_TO);
300 static InternetAddressList *
301 get_cc (GMimeMessage *message)
303 return g_mime_message_get_addresses (message, GMIME_ADDRESS_TYPE_CC);
306 static InternetAddressList *
307 get_bcc (GMimeMessage *message)
309 return g_mime_message_get_addresses (message, GMIME_ADDRESS_TYPE_BCC);
312 /* Augment the recipients of 'reply' from the "Reply-to:", "From:",
313 * "To:", "Cc:", and "Bcc:" headers of 'message'.
315 * If 'reply_all' is true, use sender and all recipients, otherwise
316 * scan the headers for the first that contains something other than
317 * the user's addresses and add the recipients from this header
318 * (typically this would be reply-to-sender, but also handles reply to
319 * user's own message in a sensible way).
321 * If any of the user's addresses were found in these headers, the
322 * first of these returned, otherwise NULL is returned.
325 add_recipients_from_message (GMimeMessage *reply,
326 notmuch_config_t *config,
327 GMimeMessage *message,
331 InternetAddressList * (*get_header)(GMimeMessage *message);
332 GMimeAddressType recipient_type;
334 { get_sender, GMIME_ADDRESS_TYPE_TO },
335 { get_to, GMIME_ADDRESS_TYPE_TO },
336 { get_cc, GMIME_ADDRESS_TYPE_CC },
337 { get_bcc, GMIME_ADDRESS_TYPE_BCC },
339 const char *from_addr = NULL;
343 for (i = 0; i < ARRAY_SIZE (reply_to_map); i++) {
344 InternetAddressList *recipients;
346 recipients = reply_to_map[i].get_header (message);
348 n += scan_address_list (recipients, config, reply,
349 reply_to_map[i].recipient_type, &from_addr);
351 if (! reply_all && n) {
352 /* Stop adding new recipients in reply-to-sender mode if
353 * we have added some recipient(s) above.
355 * This also handles the case of user replying to his own
356 * message, where reply-to/from is not a recipient. In
357 * this case there may be more than one recipient even if
358 * not replying to all.
362 /* From address and some recipients are enough, bail out. */
368 /* If no recipients were added but we found one of the user's
369 * addresses to use as a from address then the message is from the
370 * user to the user - add the discovered from address to the list
371 * of recipients so that the reply goes back to the user.
373 if (n == 0 && from_addr)
374 g_mime_message_add_mailbox (reply, GMIME_ADDRESS_TYPE_TO, NULL, from_addr);
380 * Look for the user's address in " for <email@add.res>" in the
383 * Return the address that was found, if any, and NULL otherwise.
386 guess_from_in_received_for (notmuch_config_t *config, const char *received)
390 ptr = strstr (received, " for ");
394 return user_address_in_string (ptr, config);
398 * Parse all the " by MTA ..." parts in received headers to guess the
399 * email address that this was originally delivered to.
401 * Extract just the MTA here by removing leading whitespace and
402 * assuming that the MTA name ends at the next whitespace. Test for
403 * *(by+4) to be non-'\0' to make sure there's something there at all
404 * - and then assume that the first whitespace delimited token that
405 * follows is the receiving system in this step of the receive chain.
407 * Return the address that was found, if any, and NULL otherwise.
410 guess_from_in_received_by (notmuch_config_t *config, const char *received)
413 const char *by = received;
414 char *domain, *tld, *mta, *ptr, *token;
416 while ((by = strstr (by, " by ")) != NULL) {
421 token = strtok (mta, " \t");
427 * Now extract the last two components of the MTA host name as
431 while ((ptr = strsep (&token, ". \t")) != NULL) {
440 * Recombine domain and tld and look for it among the
441 * configured email addresses. This time we have a known
442 * domain name and nothing else - so the test is the other
443 * way around: we check if this is a substring of one of
444 * the email addresses.
448 addr = string_in_user_address (domain, config);
461 * Get the concatenated Received: headers and search from the front
462 * (last Received: header added) and try to extract from them
463 * indications to which email address this message was delivered.
465 * The Received: header is special in our get_header function and is
466 * always concatenated.
468 * Return the address that was found, if any, and NULL otherwise.
471 guess_from_in_received_headers (notmuch_config_t *config,
472 notmuch_message_t *message)
474 const char *received, *addr;
477 received = notmuch_message_get_header (message, "received");
481 sanitized = sanitize_string (NULL, received);
485 addr = guess_from_in_received_for (config, sanitized);
487 addr = guess_from_in_received_by (config, sanitized);
489 talloc_free (sanitized);
495 * Try to find user's email address in one of the extra To-like
496 * headers: Envelope-To, X-Original-To, and Delivered-To (searched in
499 * Return the address that was found, if any, and NULL otherwise.
502 get_from_in_to_headers (notmuch_config_t *config, notmuch_message_t *message)
505 const char *tohdr, *addr;
506 const char *to_headers[] = {
512 for (i = 0; i < ARRAY_SIZE (to_headers); i++) {
513 tohdr = notmuch_message_get_header (message, to_headers[i]);
515 /* Note: tohdr potentially contains a list of email addresses. */
516 addr = user_address_in_string (tohdr, config);
524 static GMimeMessage *
525 create_reply_message (void *ctx,
526 notmuch_config_t *config,
527 notmuch_message_t *message,
528 GMimeMessage *mime_message,
532 const char *subject, *from_addr = NULL;
533 const char *in_reply_to, *orig_references, *references;
536 * Use the below header order for limited headers, "pretty" order
539 GMimeMessage *reply = g_mime_message_new (limited ? 0 : 1);
542 fprintf (stderr, "Out of memory\n");
546 in_reply_to = talloc_asprintf (ctx, "<%s>",
547 notmuch_message_get_message_id (message));
549 g_mime_object_set_header (GMIME_OBJECT (reply), "In-Reply-To", in_reply_to, NULL);
551 orig_references = notmuch_message_get_header (message, "references");
552 if (orig_references && *orig_references)
553 references = talloc_asprintf (ctx, "%s %s", orig_references,
556 references = talloc_strdup (ctx, in_reply_to);
558 g_mime_object_set_header (GMIME_OBJECT (reply), "References", references, NULL);
560 from_addr = add_recipients_from_message (reply, config,
561 mime_message, reply_all);
563 /* The above is all that is needed for limited headers. */
568 * Sadly, there is no standard way to find out to which email
569 * address a mail was delivered - what is in the headers depends
570 * on the MTAs used along the way.
572 * If none of the user's email addresses are in the To: or Cc:
573 * headers, we try a number of heuristics which hopefully will
574 * answer this question.
576 * First, check for Envelope-To:, X-Original-To:, and
577 * Delivered-To: headers.
579 if (from_addr == NULL)
580 from_addr = get_from_in_to_headers (config, message);
583 * Check for a (for <email@add.res>) clause in Received: headers,
584 * and the domain part of known email addresses in the 'by' part
585 * of Received: headers
587 if (from_addr == NULL)
588 from_addr = guess_from_in_received_headers (config, message);
590 /* Default to user's primary address. */
591 if (from_addr == NULL)
592 from_addr = notmuch_config_get_user_primary_email (config);
594 from_addr = talloc_asprintf (ctx, "%s <%s>",
595 notmuch_config_get_user_name (config),
597 g_mime_object_set_header (GMIME_OBJECT (reply), "From", from_addr, NULL);
599 subject = g_mime_message_get_subject (mime_message);
601 if (strncasecmp (subject, "Re:", 3))
602 subject = talloc_asprintf (ctx, "Re: %s", subject);
603 g_mime_message_set_subject (reply, subject, NULL);
617 do_reply (notmuch_config_t *config,
618 notmuch_query_t *query,
619 notmuch_show_params_t *params,
625 notmuch_messages_t *messages;
626 notmuch_message_t *message;
627 notmuch_status_t status;
628 struct sprinter *sp = NULL;
630 if (format == FORMAT_JSON || format == FORMAT_SEXP) {
633 status = notmuch_query_count_messages (query, &count);
634 if (print_status_query ("notmuch reply", query, status))
638 fprintf (stderr, "Error: search term did not match precisely one message (matched %u messages).\n", count);
642 if (format == FORMAT_JSON)
643 sp = sprinter_json_create (config, stdout);
645 sp = sprinter_sexp_create (config, stdout);
648 status = notmuch_query_search_messages (query, &messages);
649 if (print_status_query ("notmuch reply", query, status))
653 notmuch_messages_valid (messages);
654 notmuch_messages_move_to_next (messages)) {
655 message = notmuch_messages_get (messages);
657 if (mime_node_open (config, message, ¶ms->crypto, &node))
660 reply = create_reply_message (config, config, message,
661 GMIME_MESSAGE (node->part), reply_all,
662 format == FORMAT_HEADERS_ONLY);
666 if (format == FORMAT_JSON || format == FORMAT_SEXP) {
669 /* The headers of the reply message we've created */
670 sp->map_key (sp, "reply-headers");
671 /* FIXME: send msg_crypto here to avoid killing the
672 * subject line on reply to encrypted messages! */
673 format_headers_sprinter (sp, reply, true, NULL);
675 /* Start the original */
676 sp->map_key (sp, "original");
677 format_part_sprinter (config, sp, node, true, false);
682 GMimeStream *stream_stdout = stream_stdout = g_mime_stream_stdout_new ();
684 show_reply_headers (stream_stdout, reply);
685 if (format == FORMAT_DEFAULT)
686 format_part_reply (stream_stdout, node);
688 g_mime_stream_flush (stream_stdout);
689 g_object_unref (stream_stdout);
692 g_object_unref (G_OBJECT (reply));
695 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;
708 notmuch_show_params_t params = {
710 .crypto = { .decrypt = NOTMUCH_DECRYPT_AUTO },
712 int format = FORMAT_DEFAULT;
713 int reply_all = true;
715 notmuch_opt_desc_t options[] = {
716 { .opt_keyword = &format, .name = "format", .keywords =
717 (notmuch_keyword_t []){ { "default", FORMAT_DEFAULT },
718 { "json", FORMAT_JSON },
719 { "sexp", FORMAT_SEXP },
720 { "headers-only", FORMAT_HEADERS_ONLY },
722 { .opt_int = ¬much_format_version, .name = "format-version" },
723 { .opt_keyword = &reply_all, .name = "reply-to", .keywords =
724 (notmuch_keyword_t []){ { "all", true },
727 { .opt_keyword = (int *) (¶ms.crypto.decrypt), .name = "decrypt",
728 .keyword_no_arg_value = "true", .keywords =
729 (notmuch_keyword_t []){ { "false", NOTMUCH_DECRYPT_FALSE },
730 { "auto", NOTMUCH_DECRYPT_AUTO },
731 { "true", NOTMUCH_DECRYPT_NOSTASH },
733 { .opt_inherit = notmuch_shared_options },
737 opt_index = parse_arguments (argc, argv, options, 1);
741 notmuch_process_shared_options (argv[0]);
743 notmuch_exit_if_unsupported_format ();
745 query_string = query_string_from_args (config, argc - opt_index, argv + opt_index);
746 if (query_string == NULL) {
747 fprintf (stderr, "Out of memory\n");
751 if (*query_string == '\0') {
752 fprintf (stderr, "Error: notmuch reply requires at least one search term.\n");
756 if (notmuch_database_open (notmuch_config_get_database_path (config),
757 NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much))
760 notmuch_exit_if_unmatched_db_uuid (notmuch);
762 query = notmuch_query_create (notmuch, query_string);
764 fprintf (stderr, "Out of memory\n");
768 if (do_reply (config, query, ¶ms, format, reply_all) != 0)
771 _notmuch_crypto_cleanup (¶ms.crypto);
772 notmuch_query_destroy (query);
773 notmuch_database_destroy (notmuch);