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 (GMimeMessage *message)
30 GMimeStream *stream_stdout = NULL;
32 stream_stdout = g_mime_stream_file_new (stdout);
34 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
35 /* Output RFC 2822 formatted (and RFC 2047 encoded) headers. */
36 g_mime_object_write_to_stream (GMIME_OBJECT(message), stream_stdout);
37 g_object_unref(stream_stdout);
42 format_part_reply (mime_node_t *node)
46 if (node->envelope_file) {
47 printf ("On %s, %s wrote:\n",
48 notmuch_message_get_header (node->envelope_file, "date"),
49 notmuch_message_get_header (node->envelope_file, "from"));
50 } else if (GMIME_IS_MESSAGE (node->part)) {
51 GMimeMessage *message = GMIME_MESSAGE (node->part);
52 InternetAddressList *recipients;
53 const char *recipients_string;
55 printf ("> From: %s\n", g_mime_message_get_sender (message));
56 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO);
57 recipients_string = internet_address_list_to_string (recipients, 0);
58 if (recipients_string)
61 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC);
62 recipients_string = internet_address_list_to_string (recipients, 0);
63 if (recipients_string)
66 printf ("> Subject: %s\n", g_mime_message_get_subject (message));
67 printf ("> Date: %s\n", g_mime_message_get_date_as_string (message));
69 } else if (GMIME_IS_PART (node->part)) {
70 GMimeContentType *content_type = g_mime_object_get_content_type (node->part);
71 GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (node->part);
73 if (g_mime_content_type_is_type (content_type, "application", "pgp-encrypted") ||
74 g_mime_content_type_is_type (content_type, "application", "pgp-signature")) {
75 /* Ignore PGP/MIME cruft parts */
76 } else if (g_mime_content_type_is_type (content_type, "text", "*") &&
77 !g_mime_content_type_is_type (content_type, "text", "html")) {
78 GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
79 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
80 show_text_part_content (node->part, stream_stdout, NOTMUCH_SHOW_TEXT_PART_REPLY);
81 g_object_unref(stream_stdout);
82 } else if (disposition &&
83 strcasecmp (g_mime_content_disposition_get_disposition (disposition),
84 GMIME_DISPOSITION_ATTACHMENT) == 0) {
85 const char *filename = g_mime_part_get_filename (GMIME_PART (node->part));
86 printf ("Attachment: %s (%s)\n", filename,
87 g_mime_content_type_to_string (content_type));
89 printf ("Non-text part: %s\n",
90 g_mime_content_type_to_string (content_type));
94 for (i = 0; i < node->nchildren; i++)
95 format_part_reply (mime_node_child (node, i));
99 USER_ADDRESS_IN_STRING,
100 STRING_IN_USER_ADDRESS,
101 STRING_IS_USER_ADDRESS,
104 /* Match given string against given address according to mode. */
105 static notmuch_bool_t
106 match_address (const char *str, const char *address, address_match_t mode)
109 case USER_ADDRESS_IN_STRING:
110 return strcasestr (str, address) != NULL;
111 case STRING_IN_USER_ADDRESS:
112 return strcasestr (address, str) != NULL;
113 case STRING_IS_USER_ADDRESS:
114 return strcasecmp (address, str) == 0;
120 /* Match given string against user's configured "primary" and "other"
121 * addresses according to mode. */
123 address_match (const char *str, notmuch_config_t *config, address_match_t mode)
129 if (!str || *str == '\0')
132 primary = notmuch_config_get_user_primary_email (config);
133 if (match_address (str, primary, mode))
136 other = notmuch_config_get_user_other_email (config, &other_len);
137 for (i = 0; i < other_len; i++) {
138 if (match_address (str, other[i], mode))
145 /* Does the given string contain an address configured as one of the
146 * user's "primary" or "other" addresses. If so, return the matching
147 * address, NULL otherwise. */
149 user_address_in_string (const char *str, notmuch_config_t *config)
151 return address_match (str, config, USER_ADDRESS_IN_STRING);
154 /* Do any of the addresses configured as one of the user's "primary"
155 * or "other" addresses contain the given string. If so, return the
156 * matching address, NULL otherwise. */
158 string_in_user_address (const char *str, notmuch_config_t *config)
160 return address_match (str, config, STRING_IN_USER_ADDRESS);
163 /* Is the given address configured as one of the user's "primary" or
164 * "other" addresses. */
165 static notmuch_bool_t
166 address_is_users (const char *address, notmuch_config_t *config)
168 return address_match (address, config, STRING_IS_USER_ADDRESS) != NULL;
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;
198 for (i = 0; i < internet_address_list_length (list); i++) {
199 address = internet_address_list_get_address (list, i);
200 if (INTERNET_ADDRESS_IS_GROUP (address)) {
201 InternetAddressGroup *group;
202 InternetAddressList *group_list;
204 group = INTERNET_ADDRESS_GROUP (address);
205 group_list = internet_address_group_get_members (group);
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 /* Does the address in the Reply-To header of 'message' already appear
231 * in either the 'To' or 'Cc' header of the message?
233 static notmuch_bool_t
234 reply_to_header_is_redundant (GMimeMessage *message,
235 InternetAddressList *reply_to_list)
237 const char *addr, *reply_to;
238 InternetAddress *address;
239 InternetAddressMailbox *mailbox;
240 InternetAddressList *recipients;
241 notmuch_bool_t ret = FALSE;
244 if (reply_to_list == NULL ||
245 internet_address_list_length (reply_to_list) != 1)
248 address = internet_address_list_get_address (reply_to_list, 0);
249 if (INTERNET_ADDRESS_IS_GROUP (address))
252 mailbox = INTERNET_ADDRESS_MAILBOX (address);
253 reply_to = internet_address_mailbox_get_addr (mailbox);
255 recipients = g_mime_message_get_all_recipients (message);
257 for (i = 0; i < internet_address_list_length (recipients); i++) {
258 address = internet_address_list_get_address (recipients, i);
259 if (INTERNET_ADDRESS_IS_GROUP (address))
262 mailbox = INTERNET_ADDRESS_MAILBOX (address);
263 addr = internet_address_mailbox_get_addr (mailbox);
264 if (strcmp (addr, reply_to) == 0) {
270 g_object_unref (G_OBJECT (recipients));
275 static InternetAddressList *get_sender(unused(notmuch_message_t *message),
276 GMimeMessage *mime_message)
278 const char *reply_to;
280 reply_to = g_mime_message_get_reply_to (mime_message);
281 if (reply_to && *reply_to) {
282 InternetAddressList *reply_to_list;
285 * Some mailing lists munge the Reply-To header despite it
286 * being A Bad Thing, see
287 * http://marc.merlins.org/netrants/reply-to-harmful.html
289 * The munging is easy to detect, because it results in a
290 * redundant reply-to header, (with an address that already
291 * exists in either To or Cc). So in this case, we ignore the
292 * Reply-To field and use the From header. This ensures the
293 * original sender will get the reply even if not subscribed
294 * to the list. Note that the address in the Reply-To header
295 * will always appear in the reply if reply_all is true.
297 reply_to_list = internet_address_list_parse_string (reply_to);
298 if (! reply_to_header_is_redundant (mime_message, reply_to_list))
299 return reply_to_list;
301 g_object_unref (G_OBJECT (reply_to_list));
304 return internet_address_list_parse_string (
305 g_mime_message_get_sender (mime_message));
308 static InternetAddressList *get_to(unused(notmuch_message_t *message),
309 GMimeMessage *mime_message)
311 return g_mime_message_get_recipients (mime_message,
312 GMIME_RECIPIENT_TYPE_TO);
315 static InternetAddressList *get_cc(unused(notmuch_message_t *message),
316 GMimeMessage *mime_message)
318 return g_mime_message_get_recipients (mime_message,
319 GMIME_RECIPIENT_TYPE_CC);
322 static InternetAddressList *get_bcc(unused(notmuch_message_t *message),
323 GMimeMessage *mime_message)
325 return g_mime_message_get_recipients (mime_message,
326 GMIME_RECIPIENT_TYPE_BCC);
329 /* Augment the recipients of 'reply' from the "Reply-to:", "From:",
330 * "To:", "Cc:", and "Bcc:" headers of 'message'.
332 * If 'reply_all' is true, use sender and all recipients, otherwise
333 * scan the headers for the first that contains something other than
334 * the user's addresses and add the recipients from this header
335 * (typically this would be reply-to-sender, but also handles reply to
336 * user's own message in a sensible way).
338 * If any of the user's addresses were found in these headers, the
339 * first of these returned, otherwise NULL is returned.
342 add_recipients_from_message (GMimeMessage *reply,
343 notmuch_config_t *config,
344 notmuch_message_t *message,
345 GMimeMessage *mime_message,
346 notmuch_bool_t reply_all)
349 InternetAddressList * (*get_header)(notmuch_message_t *message,
350 GMimeMessage *mime_message);
351 GMimeRecipientType recipient_type;
353 { get_sender, GMIME_RECIPIENT_TYPE_TO },
354 { get_to, GMIME_RECIPIENT_TYPE_TO },
355 { get_cc, GMIME_RECIPIENT_TYPE_CC },
356 { get_bcc, GMIME_RECIPIENT_TYPE_BCC },
358 const char *from_addr = NULL;
362 for (i = 0; i < ARRAY_SIZE (reply_to_map); i++) {
363 InternetAddressList *recipients;
365 recipients = reply_to_map[i].get_header (message, mime_message);
367 n += scan_address_list (recipients, config, reply,
368 reply_to_map[i].recipient_type, &from_addr);
370 if (!reply_all && n) {
371 /* Stop adding new recipients in reply-to-sender mode if
372 * we have added some recipient(s) above.
374 * This also handles the case of user replying to his own
375 * message, where reply-to/from is not a recipient. In
376 * this case there may be more than one recipient even if
377 * not replying to all.
381 /* From address and some recipients are enough, bail out. */
391 * Look for the user's address in " for <email@add.res>" in the
394 * Return the address that was found, if any, and NULL otherwise.
397 guess_from_in_received_for (notmuch_config_t *config, const char *received)
401 ptr = strstr (received, " for ");
405 return user_address_in_string (ptr, config);
409 * Parse all the " by MTA ..." parts in received headers to guess the
410 * email address that this was originally delivered to.
412 * Extract just the MTA here by removing leading whitespace and
413 * assuming that the MTA name ends at the next whitespace. Test for
414 * *(by+4) to be non-'\0' to make sure there's something there at all
415 * - and then assume that the first whitespace delimited token that
416 * follows is the receiving system in this step of the receive chain.
418 * Return the address that was found, if any, and NULL otherwise.
421 guess_from_in_received_by (notmuch_config_t *config, const char *received)
424 const char *by = received;
425 char *domain, *tld, *mta, *ptr, *token;
427 while ((by = strstr (by, " by ")) != NULL) {
432 token = strtok(mta," \t");
438 * Now extract the last two components of the MTA host name as
442 while ((ptr = strsep (&token, ". \t")) != NULL) {
451 * Recombine domain and tld and look for it among the
452 * configured email addresses. This time we have a known
453 * domain name and nothing else - so the test is the other
454 * way around: we check if this is a substring of one of
455 * the email addresses.
459 addr = string_in_user_address (domain, config);
472 * Get the concatenated Received: headers and search from the front
473 * (last Received: header added) and try to extract from them
474 * indications to which email address this message was delivered.
476 * The Received: header is special in our get_header function and is
477 * always concatenated.
479 * Return the address that was found, if any, and NULL otherwise.
482 guess_from_in_received_headers (notmuch_config_t *config,
483 notmuch_message_t *message)
485 const char *received, *addr;
488 received = notmuch_message_get_header (message, "received");
492 sanitized = sanitize_string (NULL, received);
496 addr = guess_from_in_received_for (config, sanitized);
498 addr = guess_from_in_received_by (config, sanitized);
500 talloc_free (sanitized);
506 * Try to find user's email address in one of the extra To-like
507 * headers: Envelope-To, X-Original-To, and Delivered-To (searched in
510 * Return the address that was found, if any, and NULL otherwise.
513 get_from_in_to_headers (notmuch_config_t *config, notmuch_message_t *message)
516 const char *tohdr, *addr;
517 const char *to_headers[] = {
523 for (i = 0; i < ARRAY_SIZE (to_headers); i++) {
524 tohdr = notmuch_message_get_header (message, to_headers[i]);
526 /* Note: tohdr potentially contains a list of email addresses. */
527 addr = user_address_in_string (tohdr, config);
535 static GMimeMessage *
536 create_reply_message(void *ctx,
537 notmuch_config_t *config,
538 notmuch_message_t *message,
539 GMimeMessage *mime_message,
540 notmuch_bool_t reply_all,
541 notmuch_bool_t limited)
543 const char *subject, *from_addr = NULL;
544 const char *in_reply_to, *orig_references, *references;
547 * Use the below header order for limited headers, "pretty" order
550 GMimeMessage *reply = g_mime_message_new (limited ? 0 : 1);
552 fprintf (stderr, "Out of memory\n");
556 in_reply_to = talloc_asprintf (ctx, "<%s>",
557 notmuch_message_get_message_id (message));
559 g_mime_object_set_header (GMIME_OBJECT (reply), "In-Reply-To", in_reply_to);
561 orig_references = notmuch_message_get_header (message, "references");
562 if (orig_references && *orig_references)
563 references = talloc_asprintf (ctx, "%s %s", orig_references,
566 references = talloc_strdup (ctx, in_reply_to);
568 g_mime_object_set_header (GMIME_OBJECT (reply), "References", references);
570 from_addr = add_recipients_from_message (reply, config, message,
571 mime_message, reply_all);
573 /* The above is all that is needed for limited headers. */
578 * Sadly, there is no standard way to find out to which email
579 * address a mail was delivered - what is in the headers depends
580 * on the MTAs used along the way.
582 * If none of the user's email addresses are in the To: or Cc:
583 * headers, we try a number of heuristics which hopefully will
584 * answer this question.
586 * First, check for Envelope-To:, X-Original-To:, and
587 * Delivered-To: headers.
589 if (from_addr == NULL)
590 from_addr = get_from_in_to_headers (config, message);
593 * Check for a (for <email@add.res>) clause in Received: headers,
594 * and the domain part of known email addresses in the 'by' part
595 * of Received: headers
597 if (from_addr == NULL)
598 from_addr = guess_from_in_received_headers (config, message);
600 /* Default to user's primary address. */
601 if (from_addr == NULL)
602 from_addr = notmuch_config_get_user_primary_email (config);
604 from_addr = talloc_asprintf (ctx, "%s <%s>",
605 notmuch_config_get_user_name (config),
607 g_mime_object_set_header (GMIME_OBJECT (reply), "From", from_addr);
609 subject = notmuch_message_get_header (message, "subject");
611 if (strncasecmp (subject, "Re:", 3))
612 subject = talloc_asprintf (ctx, "Re: %s", subject);
613 g_mime_message_set_subject (reply, subject);
626 static int do_reply(notmuch_config_t *config,
627 notmuch_query_t *query,
628 notmuch_show_params_t *params,
630 notmuch_bool_t reply_all)
634 notmuch_messages_t *messages;
635 notmuch_message_t *message;
636 notmuch_status_t status;
637 struct sprinter *sp = NULL;
639 if (format == FORMAT_JSON || format == FORMAT_SEXP) {
642 status = notmuch_query_count_messages_st (query, &count);
643 if (print_status_query ("notmuch reply", query, status))
647 fprintf (stderr, "Error: search term did not match precisely one message (matched %d messages).\n", count);
651 if (format == FORMAT_JSON)
652 sp = sprinter_json_create (config, stdout);
654 sp = sprinter_sexp_create (config, stdout);
657 status = notmuch_query_search_messages_st (query, &messages);
658 if (print_status_query ("notmuch reply", query, status))
662 notmuch_messages_valid (messages);
663 notmuch_messages_move_to_next (messages))
665 message = notmuch_messages_get (messages);
667 if (mime_node_open (config, message, ¶ms->crypto, &node))
670 reply = create_reply_message (config, config, message,
671 GMIME_MESSAGE (node->part), reply_all,
672 format == FORMAT_HEADERS_ONLY);
676 if (format == FORMAT_JSON || format == FORMAT_SEXP) {
679 /* The headers of the reply message we've created */
680 sp->map_key (sp, "reply-headers");
681 format_headers_sprinter (sp, reply, TRUE);
683 /* Start the original */
684 sp->map_key (sp, "original");
685 format_part_sprinter (config, sp, node, TRUE, TRUE, FALSE);
690 show_reply_headers (reply);
691 if (format == FORMAT_DEFAULT)
692 format_part_reply (node);
695 g_object_unref (G_OBJECT (reply));
698 notmuch_message_destroy (message);
705 notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[])
707 notmuch_database_t *notmuch;
708 notmuch_query_t *query;
711 notmuch_show_params_t params = {
719 int format = FORMAT_DEFAULT;
720 int reply_all = TRUE;
722 notmuch_opt_desc_t options[] = {
723 { NOTMUCH_OPT_KEYWORD, &format, "format", 'f',
724 (notmuch_keyword_t []){ { "default", FORMAT_DEFAULT },
725 { "json", FORMAT_JSON },
726 { "sexp", FORMAT_SEXP },
727 { "headers-only", FORMAT_HEADERS_ONLY },
729 { NOTMUCH_OPT_INT, ¬much_format_version, "format-version", 0, 0 },
730 { NOTMUCH_OPT_KEYWORD, &reply_all, "reply-to", 'r',
731 (notmuch_keyword_t []){ { "all", TRUE },
734 { NOTMUCH_OPT_BOOLEAN, ¶ms.crypto.decrypt, "decrypt", 'd', 0 },
735 { NOTMUCH_OPT_INHERIT, (void *) ¬much_shared_options, NULL, 0, 0 },
739 opt_index = parse_arguments (argc, argv, options, 1);
743 notmuch_process_shared_options (argv[0]);
745 notmuch_exit_if_unsupported_format ();
747 query_string = query_string_from_args (config, argc-opt_index, argv+opt_index);
748 if (query_string == NULL) {
749 fprintf (stderr, "Out of memory\n");
753 if (*query_string == '\0') {
754 fprintf (stderr, "Error: notmuch reply requires at least one search term.\n");
758 params.crypto.gpgpath = notmuch_config_get_crypto_gpg_path (config);
760 if (notmuch_database_open (notmuch_config_get_database_path (config),
761 NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much))
764 notmuch_exit_if_unmatched_db_uuid (notmuch);
766 query = notmuch_query_create (notmuch, query_string);
768 fprintf (stderr, "Out of memory\n");
772 if (do_reply (config, query, ¶ms, format, reply_all) != 0)
775 notmuch_crypto_cleanup (¶ms.crypto);
776 notmuch_query_destroy (query);
777 notmuch_database_destroy (notmuch);