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_part_content (GMimeObject *part);
30 static const notmuch_show_format_t format_reply = {
45 show_reply_headers (GMimeMessage *message)
47 GMimeStream *stream_stdout = NULL, *stream_filter = NULL;
49 stream_stdout = g_mime_stream_file_new (stdout);
51 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
52 stream_filter = g_mime_stream_filter_new(stream_stdout);
54 g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter),
55 g_mime_filter_headers_new());
56 g_mime_object_write_to_stream(GMIME_OBJECT(message), stream_filter);
57 g_object_unref(stream_filter);
59 g_object_unref(stream_stdout);
64 reply_part_content (GMimeObject *part)
66 GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
67 GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (part);
69 if (g_mime_content_type_is_type (content_type, "text", "*") &&
70 !g_mime_content_type_is_type (content_type, "text", "html"))
72 GMimeStream *stream_stdout = NULL, *stream_filter = NULL;
73 GMimeDataWrapper *wrapper;
76 charset = g_mime_object_get_content_type_parameter (part, "charset");
77 stream_stdout = g_mime_stream_file_new (stdout);
79 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
80 stream_filter = g_mime_stream_filter_new(stream_stdout);
82 g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter),
83 g_mime_filter_charset_new(charset, "UTF-8"));
86 g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter),
87 g_mime_filter_reply_new(TRUE));
88 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
89 if (wrapper && stream_filter)
90 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
92 g_object_unref(stream_filter);
94 g_object_unref(stream_stdout);
99 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
101 const char *filename = g_mime_part_get_filename (GMIME_PART (part));
102 printf ("Attachment: %s (%s)\n", filename,
103 g_mime_content_type_to_string (content_type));
107 printf ("Non-text part: %s\n",
108 g_mime_content_type_to_string (content_type));
113 /* Is the given address configured as one of the user's "personal" or
114 * "other" addresses. */
116 address_is_users (const char *address, notmuch_config_t *config)
122 primary = notmuch_config_get_user_primary_email (config);
123 if (strcasecmp (primary, address) == 0)
126 other = notmuch_config_get_user_other_email (config, &other_len);
127 for (i = 0; i < other_len; i++)
128 if (strcasecmp (other[i], address) == 0)
134 /* For each address in 'list' that is not configured as one of the
135 * user's addresses in 'config', add that address to 'message' as an
138 * The first address encountered that *is* the user's address will be
139 * returned, (otherwise NULL is returned).
142 add_recipients_for_address_list (GMimeMessage *message,
143 notmuch_config_t *config,
144 GMimeRecipientType type,
145 InternetAddressList *list)
147 InternetAddress *address;
149 const char *ret = NULL;
151 for (i = 0; i < internet_address_list_length (list); i++) {
152 address = internet_address_list_get_address (list, i);
153 if (INTERNET_ADDRESS_IS_GROUP (address)) {
154 InternetAddressGroup *group;
155 InternetAddressList *group_list;
157 group = INTERNET_ADDRESS_GROUP (address);
158 group_list = internet_address_group_get_members (group);
159 if (group_list == NULL)
162 add_recipients_for_address_list (message, config,
165 InternetAddressMailbox *mailbox;
169 mailbox = INTERNET_ADDRESS_MAILBOX (address);
171 name = internet_address_get_name (address);
172 addr = internet_address_mailbox_get_addr (mailbox);
174 if (address_is_users (addr, config)) {
178 g_mime_message_add_recipient (message, type, name, addr);
186 /* For each address in 'recipients' that is not configured as one of
187 * the user's addresses in 'config', add that address to 'message' as
188 * an address of 'type'.
190 * The first address encountered that *is* the user's address will be
191 * returned, (otherwise NULL is returned).
194 add_recipients_for_string (GMimeMessage *message,
195 notmuch_config_t *config,
196 GMimeRecipientType type,
197 const char *recipients)
199 InternetAddressList *list;
201 if (recipients == NULL)
204 list = internet_address_list_parse_string (recipients);
208 return add_recipients_for_address_list (message, config, type, list);
211 /* Does the address in the Reply-To header of 'message' already appear
212 * in either the 'To' or 'Cc' header of the message?
215 reply_to_header_is_redundant (notmuch_message_t *message)
217 const char *reply_to, *to, *cc, *addr;
218 InternetAddressList *list;
219 InternetAddress *address;
220 InternetAddressMailbox *mailbox;
222 reply_to = notmuch_message_get_header (message, "reply-to");
223 if (reply_to == NULL || *reply_to == '\0')
226 list = internet_address_list_parse_string (reply_to);
228 if (internet_address_list_length (list) != 1)
231 address = internet_address_list_get_address (list, 0);
232 if (INTERNET_ADDRESS_IS_GROUP (address))
235 mailbox = INTERNET_ADDRESS_MAILBOX (address);
236 addr = internet_address_mailbox_get_addr (mailbox);
238 to = notmuch_message_get_header (message, "to");
239 cc = notmuch_message_get_header (message, "cc");
241 if ((to && strstr (to, addr) != 0) ||
242 (cc && strstr (cc, addr) != 0))
250 /* Augments the recipients of reply from the headers of message.
252 * If any of the user's addresses were found in these headers, the first
253 * of these returned, otherwise NULL is returned.
256 add_recipients_from_message (GMimeMessage *reply,
257 notmuch_config_t *config,
258 notmuch_message_t *message)
262 const char *fallback;
263 GMimeRecipientType recipient_type;
265 { "reply-to", "from", GMIME_RECIPIENT_TYPE_TO },
266 { "to", NULL, GMIME_RECIPIENT_TYPE_TO },
267 { "cc", NULL, GMIME_RECIPIENT_TYPE_CC },
268 { "bcc", NULL, GMIME_RECIPIENT_TYPE_BCC }
270 const char *from_addr = NULL;
273 /* Some mailing lists munge the Reply-To header despite it being A Bad
274 * Thing, see http://www.unicom.com/pw/reply-to-harmful.html
276 * The munging is easy to detect, because it results in a
277 * redundant reply-to header, (with an address that already exists
278 * in either To or Cc). So in this case, we ignore the Reply-To
279 * field and use the From header. Thie ensures the original sender
280 * will get the reply even if not subscribed to the list. Note
281 * that the address in the Reply-To header will always appear in
284 if (reply_to_header_is_redundant (message)) {
285 reply_to_map[0].header = "from";
286 reply_to_map[0].fallback = NULL;
289 for (i = 0; i < ARRAY_SIZE (reply_to_map); i++) {
290 const char *addr, *recipients;
292 recipients = notmuch_message_get_header (message,
293 reply_to_map[i].header);
294 if ((recipients == NULL || recipients[0] == '\0') && reply_to_map[i].fallback)
295 recipients = notmuch_message_get_header (message,
296 reply_to_map[i].fallback);
298 addr = add_recipients_for_string (reply, config,
299 reply_to_map[i].recipient_type,
301 if (from_addr == NULL)
309 guess_from_received_header (notmuch_config_t *config, notmuch_message_t *message)
311 const char *received,*primary,*by;
314 char *mta,*ptr,*token;
317 const char *delim=". \t";
318 size_t i,j,other_len;
320 const char *to_headers[] = {"Envelope-to", "X-Original-To"};
322 primary = notmuch_config_get_user_primary_email (config);
323 other = notmuch_config_get_user_other_email (config, &other_len);
325 /* sadly, there is no standard way to find out to which email
326 * address a mail was delivered - what is in the headers depends
327 * on the MTAs used along the way. So we are trying a number of
328 * heuristics which hopefully will answer this question.
330 * We only got here if none of the users email addresses are in
331 * the To: or Cc: header. From here we try the following in order:
332 * 1) check for an Envelope-to: header
333 * 2) check for an X-Original-To: header
334 * 3) check for a (for <email@add.res>) clause in Received: headers
335 * 4) check for the domain part of known email addresses in the
336 * 'by' part of Received headers
337 * If none of these work, we give up and return NULL
339 for (i = 0; i < sizeof(to_headers)/sizeof(*to_headers); i++) {
340 tohdr = xstrdup(notmuch_message_get_header (message, to_headers[i]));
341 if (tohdr && *tohdr) {
342 /* tohdr is potentialy a list of email addresses, so here we
343 * check if one of the email addresses is a substring of tohdr
345 if (strcasestr(tohdr, primary)) {
349 for (j = 0; j < other_len; j++)
350 if (strcasestr (tohdr, other[j])) {
358 /* We get the concatenated Received: headers and search from the
359 * front (last Received: header added) and try to extract from
360 * them indications to which email address this message was
362 * The Received: header is special in our get_header function
363 * and is always concated.
365 received = notmuch_message_get_header (message, "received");
366 if (received == NULL)
369 /* First we look for a " for <email@add.res>" in the received
372 ptr = strstr (received, " for ");
374 /* the text following is potentialy a list of email addresses,
375 * so again we check if one of the email addresses is a
378 if (strcasestr(ptr, primary)) {
381 for (i = 0; i < other_len; i++)
382 if (strcasestr (ptr, other[i])) {
386 /* Finally, we parse all the " by MTA ..." headers to guess the
387 * email address that this was originally delivered to.
388 * We extract just the MTA here by removing leading whitespace and
389 * assuming that the MTA name ends at the next whitespace.
390 * We test for *(by+4) to be non-'\0' to make sure there's
391 * something there at all - and then assume that the first
392 * whitespace delimited token that follows is the receiving
393 * system in this step of the receive chain
396 while((by = strstr (by, " by ")) != NULL) {
401 token = strtok(mta," \t");
404 /* Now extract the last two components of the MTA host name
407 while ((ptr = strsep (&token, delim)) != NULL) {
415 /* Recombine domain and tld and look for it among the configured
417 * This time we have a known domain name and nothing else - so
418 * the test is the other way around: we check if this is a
419 * substring of one of the email addresses.
423 if (strcasestr(primary, domain)) {
427 for (i = 0; i < other_len; i++)
428 if (strcasestr (other[i],domain)) {
440 notmuch_reply_format_default(void *ctx, notmuch_config_t *config, notmuch_query_t *query)
443 notmuch_messages_t *messages;
444 notmuch_message_t *message;
445 const char *subject, *from_addr = NULL;
446 const char *in_reply_to, *orig_references, *references;
447 const notmuch_show_format_t *format = &format_reply;
448 notmuch_show_params_t params;
451 for (messages = notmuch_query_search_messages (query);
452 notmuch_messages_valid (messages);
453 notmuch_messages_move_to_next (messages))
455 message = notmuch_messages_get (messages);
457 /* The 1 means we want headers in a "pretty" order. */
458 reply = g_mime_message_new (1);
460 fprintf (stderr, "Out of memory\n");
464 subject = notmuch_message_get_header (message, "subject");
466 if (strncasecmp (subject, "Re:", 3))
467 subject = talloc_asprintf (ctx, "Re: %s", subject);
468 g_mime_message_set_subject (reply, subject);
471 from_addr = add_recipients_from_message (reply, config, message);
473 if (from_addr == NULL)
474 from_addr = guess_from_received_header (config, message);
476 if (from_addr == NULL)
477 from_addr = notmuch_config_get_user_primary_email (config);
479 from_addr = talloc_asprintf (ctx, "%s <%s>",
480 notmuch_config_get_user_name (config),
482 g_mime_object_set_header (GMIME_OBJECT (reply),
485 in_reply_to = talloc_asprintf (ctx, "<%s>",
486 notmuch_message_get_message_id (message));
488 g_mime_object_set_header (GMIME_OBJECT (reply),
489 "In-Reply-To", in_reply_to);
491 orig_references = notmuch_message_get_header (message, "references");
492 references = talloc_asprintf (ctx, "%s%s%s",
493 orig_references ? orig_references : "",
494 orig_references ? " " : "",
496 g_mime_object_set_header (GMIME_OBJECT (reply),
497 "References", references);
499 show_reply_headers (reply);
501 g_object_unref (G_OBJECT (reply));
504 printf ("On %s, %s wrote:\n",
505 notmuch_message_get_header (message, "date"),
506 notmuch_message_get_header (message, "from"));
508 show_message_body (notmuch_message_get_filename (message),
511 notmuch_message_destroy (message);
516 /* This format is currently tuned for a git send-email --notmuch hook */
518 notmuch_reply_format_headers_only(void *ctx, notmuch_config_t *config, notmuch_query_t *query)
521 notmuch_messages_t *messages;
522 notmuch_message_t *message;
523 const char *in_reply_to, *orig_references, *references;
526 for (messages = notmuch_query_search_messages (query);
527 notmuch_messages_valid (messages);
528 notmuch_messages_move_to_next (messages))
530 message = notmuch_messages_get (messages);
532 /* The 0 means we do not want headers in a "pretty" order. */
533 reply = g_mime_message_new (0);
535 fprintf (stderr, "Out of memory\n");
539 in_reply_to = talloc_asprintf (ctx, "<%s>",
540 notmuch_message_get_message_id (message));
542 g_mime_object_set_header (GMIME_OBJECT (reply),
543 "In-Reply-To", in_reply_to);
546 orig_references = notmuch_message_get_header (message, "references");
548 /* We print In-Reply-To followed by References because git format-patch treats them
549 * specially. Git does not interpret the other headers specially
551 references = talloc_asprintf (ctx, "%s%s%s",
552 orig_references ? orig_references : "",
553 orig_references ? " " : "",
555 g_mime_object_set_header (GMIME_OBJECT (reply),
556 "References", references);
558 (void)add_recipients_from_message (reply, config, message);
560 reply_headers = g_mime_object_to_string (GMIME_OBJECT (reply));
561 printf ("%s", reply_headers);
562 free (reply_headers);
564 g_object_unref (G_OBJECT (reply));
567 notmuch_message_destroy (message);
573 notmuch_reply_command (void *ctx, int argc, char *argv[])
575 notmuch_config_t *config;
576 notmuch_database_t *notmuch;
577 notmuch_query_t *query;
578 char *opt, *query_string;
580 int (*reply_format_func)(void *ctx, notmuch_config_t *config, notmuch_query_t *query);
582 reply_format_func = notmuch_reply_format_default;
584 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
585 if (strcmp (argv[i], "--") == 0) {
589 if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
590 opt = argv[i] + sizeof ("--format=") - 1;
591 if (strcmp (opt, "default") == 0) {
592 reply_format_func = notmuch_reply_format_default;
593 } else if (strcmp (opt, "headers-only") == 0) {
594 reply_format_func = notmuch_reply_format_headers_only;
596 fprintf (stderr, "Invalid value for --format: %s\n", opt);
600 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
608 config = notmuch_config_open (ctx, NULL, NULL);
612 query_string = query_string_from_args (ctx, argc, argv);
613 if (query_string == NULL) {
614 fprintf (stderr, "Out of memory\n");
618 if (*query_string == '\0') {
619 fprintf (stderr, "Error: notmuch reply requires at least one search term.\n");
623 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
624 NOTMUCH_DATABASE_MODE_READ_ONLY);
628 query = notmuch_query_create (notmuch, query_string);
630 fprintf (stderr, "Out of memory\n");
634 if (reply_format_func (ctx, config, query) != 0)
637 notmuch_query_destroy (query);
638 notmuch_database_close (notmuch);