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, "text", "*") &&
97 !g_mime_content_type_is_type (content_type, "text", "html"))
99 GMimeStream *stream_stdout = NULL, *stream_filter = NULL;
100 GMimeDataWrapper *wrapper;
103 charset = g_mime_object_get_content_type_parameter (part, "charset");
104 stream_stdout = g_mime_stream_file_new (stdout);
106 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
107 stream_filter = g_mime_stream_filter_new(stream_stdout);
109 g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter),
110 g_mime_filter_charset_new(charset, "UTF-8"));
113 g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter),
114 g_mime_filter_reply_new(TRUE));
115 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
116 if (wrapper && stream_filter)
117 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
119 g_object_unref(stream_filter);
121 g_object_unref(stream_stdout);
123 else if (g_mime_content_type_is_type (content_type, "message", "rfc822"))
125 /* Output nothing, since rfc822 subparts will be handled individually. */
130 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
132 const char *filename = g_mime_part_get_filename (GMIME_PART (part));
133 printf ("Attachment: %s (%s)\n", filename,
134 g_mime_content_type_to_string (content_type));
138 printf ("Non-text part: %s\n",
139 g_mime_content_type_to_string (content_type));
144 /* Is the given address configured as one of the user's "personal" or
145 * "other" addresses. */
147 address_is_users (const char *address, notmuch_config_t *config)
153 primary = notmuch_config_get_user_primary_email (config);
154 if (strcasecmp (primary, address) == 0)
157 other = notmuch_config_get_user_other_email (config, &other_len);
158 for (i = 0; i < other_len; i++)
159 if (strcasecmp (other[i], address) == 0)
165 /* For each address in 'list' that is not configured as one of the
166 * user's addresses in 'config', add that address to 'message' as an
169 * The first address encountered that *is* the user's address will be
170 * returned, (otherwise NULL is returned).
173 add_recipients_for_address_list (GMimeMessage *message,
174 notmuch_config_t *config,
175 GMimeRecipientType type,
176 InternetAddressList *list)
178 InternetAddress *address;
180 const char *ret = NULL;
182 for (i = 0; i < internet_address_list_length (list); i++) {
183 address = internet_address_list_get_address (list, i);
184 if (INTERNET_ADDRESS_IS_GROUP (address)) {
185 InternetAddressGroup *group;
186 InternetAddressList *group_list;
188 group = INTERNET_ADDRESS_GROUP (address);
189 group_list = internet_address_group_get_members (group);
190 if (group_list == NULL)
193 add_recipients_for_address_list (message, config,
196 InternetAddressMailbox *mailbox;
200 mailbox = INTERNET_ADDRESS_MAILBOX (address);
202 name = internet_address_get_name (address);
203 addr = internet_address_mailbox_get_addr (mailbox);
205 if (address_is_users (addr, config)) {
209 g_mime_message_add_recipient (message, type, name, addr);
217 /* For each address in 'recipients' that is not configured as one of
218 * the user's addresses in 'config', add that address to 'message' as
219 * an address of 'type'.
221 * The first address encountered that *is* the user's address will be
222 * returned, (otherwise NULL is returned).
225 add_recipients_for_string (GMimeMessage *message,
226 notmuch_config_t *config,
227 GMimeRecipientType type,
228 const char *recipients)
230 InternetAddressList *list;
232 if (recipients == NULL)
235 list = internet_address_list_parse_string (recipients);
239 return add_recipients_for_address_list (message, config, type, list);
242 /* Does the address in the Reply-To header of 'message' already appear
243 * in either the 'To' or 'Cc' header of the message?
246 reply_to_header_is_redundant (notmuch_message_t *message)
248 const char *reply_to, *to, *cc, *addr;
249 InternetAddressList *list;
250 InternetAddress *address;
251 InternetAddressMailbox *mailbox;
253 reply_to = notmuch_message_get_header (message, "reply-to");
254 if (reply_to == NULL || *reply_to == '\0')
257 list = internet_address_list_parse_string (reply_to);
259 if (internet_address_list_length (list) != 1)
262 address = internet_address_list_get_address (list, 0);
263 if (INTERNET_ADDRESS_IS_GROUP (address))
266 mailbox = INTERNET_ADDRESS_MAILBOX (address);
267 addr = internet_address_mailbox_get_addr (mailbox);
269 to = notmuch_message_get_header (message, "to");
270 cc = notmuch_message_get_header (message, "cc");
272 if ((to && strstr (to, addr) != 0) ||
273 (cc && strstr (cc, addr) != 0))
281 /* Augments the recipients of reply from the headers of message.
283 * If any of the user's addresses were found in these headers, the first
284 * of these returned, otherwise NULL is returned.
287 add_recipients_from_message (GMimeMessage *reply,
288 notmuch_config_t *config,
289 notmuch_message_t *message)
293 const char *fallback;
294 GMimeRecipientType recipient_type;
296 { "reply-to", "from", GMIME_RECIPIENT_TYPE_TO },
297 { "to", NULL, GMIME_RECIPIENT_TYPE_TO },
298 { "cc", NULL, GMIME_RECIPIENT_TYPE_CC },
299 { "bcc", NULL, GMIME_RECIPIENT_TYPE_BCC }
301 const char *from_addr = NULL;
304 /* Some mailing lists munge the Reply-To header despite it being A Bad
305 * Thing, see http://www.unicom.com/pw/reply-to-harmful.html
307 * The munging is easy to detect, because it results in a
308 * redundant reply-to header, (with an address that already exists
309 * in either To or Cc). So in this case, we ignore the Reply-To
310 * field and use the From header. This ensures the original sender
311 * will get the reply even if not subscribed to the list. Note
312 * that the address in the Reply-To header will always appear in
315 if (reply_to_header_is_redundant (message)) {
316 reply_to_map[0].header = "from";
317 reply_to_map[0].fallback = NULL;
320 for (i = 0; i < ARRAY_SIZE (reply_to_map); i++) {
321 const char *addr, *recipients;
323 recipients = notmuch_message_get_header (message,
324 reply_to_map[i].header);
325 if ((recipients == NULL || recipients[0] == '\0') && reply_to_map[i].fallback)
326 recipients = notmuch_message_get_header (message,
327 reply_to_map[i].fallback);
329 addr = add_recipients_for_string (reply, config,
330 reply_to_map[i].recipient_type,
332 if (from_addr == NULL)
340 guess_from_received_header (notmuch_config_t *config, notmuch_message_t *message)
342 const char *received,*primary,*by;
345 char *mta,*ptr,*token;
348 const char *delim=". \t";
349 size_t i,j,other_len;
351 const char *to_headers[] = {"Envelope-to", "X-Original-To"};
353 primary = notmuch_config_get_user_primary_email (config);
354 other = notmuch_config_get_user_other_email (config, &other_len);
356 /* sadly, there is no standard way to find out to which email
357 * address a mail was delivered - what is in the headers depends
358 * on the MTAs used along the way. So we are trying a number of
359 * heuristics which hopefully will answer this question.
361 * We only got here if none of the users email addresses are in
362 * the To: or Cc: header. From here we try the following in order:
363 * 1) check for an Envelope-to: header
364 * 2) check for an X-Original-To: header
365 * 3) check for a (for <email@add.res>) clause in Received: headers
366 * 4) check for the domain part of known email addresses in the
367 * 'by' part of Received headers
368 * If none of these work, we give up and return NULL
370 for (i = 0; i < sizeof(to_headers)/sizeof(*to_headers); i++) {
371 tohdr = xstrdup(notmuch_message_get_header (message, to_headers[i]));
372 if (tohdr && *tohdr) {
373 /* tohdr is potentialy a list of email addresses, so here we
374 * check if one of the email addresses is a substring of tohdr
376 if (strcasestr(tohdr, primary)) {
380 for (j = 0; j < other_len; j++)
381 if (strcasestr (tohdr, other[j])) {
389 /* We get the concatenated Received: headers and search from the
390 * front (last Received: header added) and try to extract from
391 * them indications to which email address this message was
393 * The Received: header is special in our get_header function
394 * and is always concatenated.
396 received = notmuch_message_get_header (message, "received");
397 if (received == NULL)
400 /* First we look for a " for <email@add.res>" in the received
403 ptr = strstr (received, " for ");
405 /* the text following is potentialy a list of email addresses,
406 * so again we check if one of the email addresses is a
409 if (strcasestr(ptr, primary)) {
412 for (i = 0; i < other_len; i++)
413 if (strcasestr (ptr, other[i])) {
417 /* Finally, we parse all the " by MTA ..." headers to guess the
418 * email address that this was originally delivered to.
419 * We extract just the MTA here by removing leading whitespace and
420 * assuming that the MTA name ends at the next whitespace.
421 * We test for *(by+4) to be non-'\0' to make sure there's
422 * something there at all - and then assume that the first
423 * whitespace delimited token that follows is the receiving
424 * system in this step of the receive chain
427 while((by = strstr (by, " by ")) != NULL) {
432 token = strtok(mta," \t");
437 /* Now extract the last two components of the MTA host name
441 while ((ptr = strsep (&token, delim)) != NULL) {
449 /* Recombine domain and tld and look for it among the configured
451 * This time we have a known domain name and nothing else - so
452 * the test is the other way around: we check if this is a
453 * substring of one of the email addresses.
457 if (strcasestr(primary, domain)) {
461 for (i = 0; i < other_len; i++)
462 if (strcasestr (other[i],domain)) {
474 notmuch_reply_format_default(void *ctx,
475 notmuch_config_t *config,
476 notmuch_query_t *query,
477 notmuch_show_params_t *params)
480 notmuch_messages_t *messages;
481 notmuch_message_t *message;
482 const char *subject, *from_addr = NULL;
483 const char *in_reply_to, *orig_references, *references;
484 const notmuch_show_format_t *format = &format_reply;
486 for (messages = notmuch_query_search_messages (query);
487 notmuch_messages_valid (messages);
488 notmuch_messages_move_to_next (messages))
490 message = notmuch_messages_get (messages);
492 /* The 1 means we want headers in a "pretty" order. */
493 reply = g_mime_message_new (1);
495 fprintf (stderr, "Out of memory\n");
499 subject = notmuch_message_get_header (message, "subject");
501 if (strncasecmp (subject, "Re:", 3))
502 subject = talloc_asprintf (ctx, "Re: %s", subject);
503 g_mime_message_set_subject (reply, subject);
506 from_addr = add_recipients_from_message (reply, config, message);
508 if (from_addr == NULL)
509 from_addr = guess_from_received_header (config, message);
511 if (from_addr == NULL)
512 from_addr = notmuch_config_get_user_primary_email (config);
514 from_addr = talloc_asprintf (ctx, "%s <%s>",
515 notmuch_config_get_user_name (config),
517 g_mime_object_set_header (GMIME_OBJECT (reply),
520 in_reply_to = talloc_asprintf (ctx, "<%s>",
521 notmuch_message_get_message_id (message));
523 g_mime_object_set_header (GMIME_OBJECT (reply),
524 "In-Reply-To", in_reply_to);
526 orig_references = notmuch_message_get_header (message, "references");
527 references = talloc_asprintf (ctx, "%s%s%s",
528 orig_references ? orig_references : "",
529 orig_references ? " " : "",
531 g_mime_object_set_header (GMIME_OBJECT (reply),
532 "References", references);
534 show_reply_headers (reply);
536 g_object_unref (G_OBJECT (reply));
539 printf ("On %s, %s wrote:\n",
540 notmuch_message_get_header (message, "date"),
541 notmuch_message_get_header (message, "from"));
543 show_message_body (notmuch_message_get_filename (message),
546 notmuch_message_destroy (message);
551 /* This format is currently tuned for a git send-email --notmuch hook */
553 notmuch_reply_format_headers_only(void *ctx,
554 notmuch_config_t *config,
555 notmuch_query_t *query,
556 unused (notmuch_show_params_t *params))
559 notmuch_messages_t *messages;
560 notmuch_message_t *message;
561 const char *in_reply_to, *orig_references, *references;
564 for (messages = notmuch_query_search_messages (query);
565 notmuch_messages_valid (messages);
566 notmuch_messages_move_to_next (messages))
568 message = notmuch_messages_get (messages);
570 /* The 0 means we do not want headers in a "pretty" order. */
571 reply = g_mime_message_new (0);
573 fprintf (stderr, "Out of memory\n");
577 in_reply_to = talloc_asprintf (ctx, "<%s>",
578 notmuch_message_get_message_id (message));
580 g_mime_object_set_header (GMIME_OBJECT (reply),
581 "In-Reply-To", in_reply_to);
584 orig_references = notmuch_message_get_header (message, "references");
586 /* We print In-Reply-To followed by References because git format-patch treats them
587 * specially. Git does not interpret the other headers specially
589 references = talloc_asprintf (ctx, "%s%s%s",
590 orig_references ? orig_references : "",
591 orig_references ? " " : "",
593 g_mime_object_set_header (GMIME_OBJECT (reply),
594 "References", references);
596 (void)add_recipients_from_message (reply, config, message);
598 reply_headers = g_mime_object_to_string (GMIME_OBJECT (reply));
599 printf ("%s", reply_headers);
600 free (reply_headers);
602 g_object_unref (G_OBJECT (reply));
605 notmuch_message_destroy (message);
611 notmuch_reply_command (void *ctx, int argc, char *argv[])
613 notmuch_config_t *config;
614 notmuch_database_t *notmuch;
615 notmuch_query_t *query;
616 char *opt, *query_string;
618 int (*reply_format_func)(void *ctx, notmuch_config_t *config, notmuch_query_t *query, notmuch_show_params_t *params);
619 notmuch_show_params_t params;
621 reply_format_func = notmuch_reply_format_default;
623 params.cryptoctx = NULL;
625 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
626 if (strcmp (argv[i], "--") == 0) {
630 if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
631 opt = argv[i] + sizeof ("--format=") - 1;
632 if (strcmp (opt, "default") == 0) {
633 reply_format_func = notmuch_reply_format_default;
634 } else if (strcmp (opt, "headers-only") == 0) {
635 reply_format_func = notmuch_reply_format_headers_only;
637 fprintf (stderr, "Invalid value for --format: %s\n", opt);
640 } else if ((STRNCMP_LITERAL (argv[i], "--decrypt") == 0)) {
641 if (params.cryptoctx == NULL) {
642 GMimeSession* session = g_object_new(g_mime_session_get_type(), NULL);
643 if (NULL == (params.cryptoctx = g_mime_gpg_context_new(session, "gpg")))
644 fprintf (stderr, "Failed to construct gpg context.\n");
646 g_mime_gpg_context_set_always_trust((GMimeGpgContext*)params.cryptoctx, FALSE);
647 g_object_unref (session);
651 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
659 config = notmuch_config_open (ctx, NULL, NULL);
663 query_string = query_string_from_args (ctx, argc, argv);
664 if (query_string == NULL) {
665 fprintf (stderr, "Out of memory\n");
669 if (*query_string == '\0') {
670 fprintf (stderr, "Error: notmuch reply requires at least one search term.\n");
674 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
675 NOTMUCH_DATABASE_MODE_READ_ONLY);
679 query = notmuch_query_create (notmuch, query_string);
681 fprintf (stderr, "Out of memory\n");
685 if (reply_format_func (ctx, config, query, ¶ms) != 0)
688 notmuch_query_destroy (query);
689 notmuch_database_close (notmuch);
691 if (params.cryptoctx)
692 g_object_unref(params.cryptoctx);