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 = {
46 show_reply_headers (GMimeMessage *message)
48 GMimeStream *stream_stdout = NULL, *stream_filter = NULL;
50 stream_stdout = g_mime_stream_file_new (stdout);
52 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
53 stream_filter = g_mime_stream_filter_new(stream_stdout);
55 g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter),
56 g_mime_filter_headers_new());
57 g_mime_object_write_to_stream(GMIME_OBJECT(message), stream_filter);
58 g_object_unref(stream_filter);
60 g_object_unref(stream_stdout);
65 reply_part_content (GMimeObject *part)
67 GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
68 GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (part);
70 if (g_mime_content_type_is_type (content_type, "text", "*") &&
71 !g_mime_content_type_is_type (content_type, "text", "html"))
73 GMimeStream *stream_stdout = NULL, *stream_filter = NULL;
74 GMimeDataWrapper *wrapper;
77 charset = g_mime_object_get_content_type_parameter (part, "charset");
78 stream_stdout = g_mime_stream_file_new (stdout);
80 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
81 stream_filter = g_mime_stream_filter_new(stream_stdout);
83 g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter),
84 g_mime_filter_charset_new(charset, "UTF-8"));
87 g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter),
88 g_mime_filter_reply_new(TRUE));
89 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
90 if (wrapper && stream_filter)
91 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
93 g_object_unref(stream_filter);
95 g_object_unref(stream_stdout);
100 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
102 const char *filename = g_mime_part_get_filename (GMIME_PART (part));
103 printf ("Attachment: %s (%s)\n", filename,
104 g_mime_content_type_to_string (content_type));
108 printf ("Non-text part: %s\n",
109 g_mime_content_type_to_string (content_type));
114 /* Is the given address configured as one of the user's "personal" or
115 * "other" addresses. */
117 address_is_users (const char *address, notmuch_config_t *config)
123 primary = notmuch_config_get_user_primary_email (config);
124 if (strcasecmp (primary, address) == 0)
127 other = notmuch_config_get_user_other_email (config, &other_len);
128 for (i = 0; i < other_len; i++)
129 if (strcasecmp (other[i], address) == 0)
135 /* For each address in 'list' that is not configured as one of the
136 * user's addresses in 'config', add that address to 'message' as an
139 * The first address encountered that *is* the user's address will be
140 * returned, (otherwise NULL is returned).
143 add_recipients_for_address_list (GMimeMessage *message,
144 notmuch_config_t *config,
145 GMimeRecipientType type,
146 InternetAddressList *list)
148 InternetAddress *address;
150 const char *ret = NULL;
152 for (i = 0; i < internet_address_list_length (list); i++) {
153 address = internet_address_list_get_address (list, i);
154 if (INTERNET_ADDRESS_IS_GROUP (address)) {
155 InternetAddressGroup *group;
156 InternetAddressList *group_list;
158 group = INTERNET_ADDRESS_GROUP (address);
159 group_list = internet_address_group_get_members (group);
160 if (group_list == NULL)
163 add_recipients_for_address_list (message, config,
166 InternetAddressMailbox *mailbox;
170 mailbox = INTERNET_ADDRESS_MAILBOX (address);
172 name = internet_address_get_name (address);
173 addr = internet_address_mailbox_get_addr (mailbox);
175 if (address_is_users (addr, config)) {
179 g_mime_message_add_recipient (message, type, name, addr);
187 /* For each address in 'recipients' that is not configured as one of
188 * the user's addresses in 'config', add that address to 'message' as
189 * an address of 'type'.
191 * The first address encountered that *is* the user's address will be
192 * returned, (otherwise NULL is returned).
195 add_recipients_for_string (GMimeMessage *message,
196 notmuch_config_t *config,
197 GMimeRecipientType type,
198 const char *recipients)
200 InternetAddressList *list;
202 if (recipients == NULL)
205 list = internet_address_list_parse_string (recipients);
209 return add_recipients_for_address_list (message, config, type, list);
212 /* Does the address in the Reply-To header of 'message' already appear
213 * in either the 'To' or 'Cc' header of the message?
216 reply_to_header_is_redundant (notmuch_message_t *message)
218 const char *reply_to, *to, *cc, *addr;
219 InternetAddressList *list;
220 InternetAddress *address;
221 InternetAddressMailbox *mailbox;
223 reply_to = notmuch_message_get_header (message, "reply-to");
224 if (reply_to == NULL || *reply_to == '\0')
227 list = internet_address_list_parse_string (reply_to);
229 if (internet_address_list_length (list) != 1)
232 address = internet_address_list_get_address (list, 0);
233 if (INTERNET_ADDRESS_IS_GROUP (address))
236 mailbox = INTERNET_ADDRESS_MAILBOX (address);
237 addr = internet_address_mailbox_get_addr (mailbox);
239 to = notmuch_message_get_header (message, "to");
240 cc = notmuch_message_get_header (message, "cc");
242 if ((to && strstr (to, addr) != 0) ||
243 (cc && strstr (cc, addr) != 0))
251 /* Augments the recipients of reply from the headers of message.
253 * If any of the user's addresses were found in these headers, the first
254 * of these returned, otherwise NULL is returned.
257 add_recipients_from_message (GMimeMessage *reply,
258 notmuch_config_t *config,
259 notmuch_message_t *message)
263 const char *fallback;
264 GMimeRecipientType recipient_type;
266 { "reply-to", "from", GMIME_RECIPIENT_TYPE_TO },
267 { "to", NULL, GMIME_RECIPIENT_TYPE_TO },
268 { "cc", NULL, GMIME_RECIPIENT_TYPE_CC },
269 { "bcc", NULL, GMIME_RECIPIENT_TYPE_BCC }
271 const char *from_addr = NULL;
274 /* Some mailing lists munge the Reply-To header despite it being A Bad
275 * Thing, see http://www.unicom.com/pw/reply-to-harmful.html
277 * The munging is easy to detect, because it results in a
278 * redundant reply-to header, (with an address that already exists
279 * in either To or Cc). So in this case, we ignore the Reply-To
280 * field and use the From header. Thie ensures the original sender
281 * will get the reply even if not subscribed to the list. Note
282 * that the address in the Reply-To header will always appear in
285 if (reply_to_header_is_redundant (message)) {
286 reply_to_map[0].header = "from";
287 reply_to_map[0].fallback = NULL;
290 for (i = 0; i < ARRAY_SIZE (reply_to_map); i++) {
291 const char *addr, *recipients;
293 recipients = notmuch_message_get_header (message,
294 reply_to_map[i].header);
295 if ((recipients == NULL || recipients[0] == '\0') && reply_to_map[i].fallback)
296 recipients = notmuch_message_get_header (message,
297 reply_to_map[i].fallback);
299 addr = add_recipients_for_string (reply, config,
300 reply_to_map[i].recipient_type,
302 if (from_addr == NULL)
310 guess_from_received_header (notmuch_config_t *config, notmuch_message_t *message)
312 const char *received,*primary,*by;
315 char *mta,*ptr,*token;
318 const char *delim=". \t";
319 size_t i,j,other_len;
321 const char *to_headers[] = {"Envelope-to", "X-Original-To"};
323 primary = notmuch_config_get_user_primary_email (config);
324 other = notmuch_config_get_user_other_email (config, &other_len);
326 /* sadly, there is no standard way to find out to which email
327 * address a mail was delivered - what is in the headers depends
328 * on the MTAs used along the way. So we are trying a number of
329 * heuristics which hopefully will answer this question.
331 * We only got here if none of the users email addresses are in
332 * the To: or Cc: header. From here we try the following in order:
333 * 1) check for an Envelope-to: header
334 * 2) check for an X-Original-To: header
335 * 3) check for a (for <email@add.res>) clause in Received: headers
336 * 4) check for the domain part of known email addresses in the
337 * 'by' part of Received headers
338 * If none of these work, we give up and return NULL
340 for (i = 0; i < sizeof(to_headers)/sizeof(*to_headers); i++) {
341 tohdr = xstrdup(notmuch_message_get_header (message, to_headers[i]));
342 if (tohdr && *tohdr) {
343 /* tohdr is potentialy a list of email addresses, so here we
344 * check if one of the email addresses is a substring of tohdr
346 if (strcasestr(tohdr, primary)) {
350 for (j = 0; j < other_len; j++)
351 if (strcasestr (tohdr, other[j])) {
359 /* We get the concatenated Received: headers and search from the
360 * front (last Received: header added) and try to extract from
361 * them indications to which email address this message was
363 * The Received: header is special in our get_header function
364 * and is always concated.
366 received = notmuch_message_get_header (message, "received");
367 if (received == NULL)
370 /* First we look for a " for <email@add.res>" in the received
373 ptr = strstr (received, " for ");
375 /* the text following is potentialy a list of email addresses,
376 * so again we check if one of the email addresses is a
379 if (strcasestr(ptr, primary)) {
382 for (i = 0; i < other_len; i++)
383 if (strcasestr (ptr, other[i])) {
387 /* Finally, we parse all the " by MTA ..." headers to guess the
388 * email address that this was originally delivered to.
389 * We extract just the MTA here by removing leading whitespace and
390 * assuming that the MTA name ends at the next whitespace.
391 * We test for *(by+4) to be non-'\0' to make sure there's
392 * something there at all - and then assume that the first
393 * whitespace delimited token that follows is the receiving
394 * system in this step of the receive chain
397 while((by = strstr (by, " by ")) != NULL) {
402 token = strtok(mta," \t");
405 /* Now extract the last two components of the MTA host name
408 while ((ptr = strsep (&token, delim)) != NULL) {
416 /* Recombine domain and tld and look for it among the configured
418 * This time we have a known domain name and nothing else - so
419 * the test is the other way around: we check if this is a
420 * substring of one of the email addresses.
424 if (strcasestr(primary, domain)) {
428 for (i = 0; i < other_len; i++)
429 if (strcasestr (other[i],domain)) {
441 notmuch_reply_format_default(void *ctx, notmuch_config_t *config, notmuch_query_t *query)
444 notmuch_messages_t *messages;
445 notmuch_message_t *message;
446 const char *subject, *from_addr = NULL;
447 const char *in_reply_to, *orig_references, *references;
448 const notmuch_show_format_t *format = &format_reply;
449 notmuch_show_params_t params;
451 params.cryptoctx = NULL;
453 for (messages = notmuch_query_search_messages (query);
454 notmuch_messages_valid (messages);
455 notmuch_messages_move_to_next (messages))
457 message = notmuch_messages_get (messages);
459 /* The 1 means we want headers in a "pretty" order. */
460 reply = g_mime_message_new (1);
462 fprintf (stderr, "Out of memory\n");
466 subject = notmuch_message_get_header (message, "subject");
468 if (strncasecmp (subject, "Re:", 3))
469 subject = talloc_asprintf (ctx, "Re: %s", subject);
470 g_mime_message_set_subject (reply, subject);
473 from_addr = add_recipients_from_message (reply, config, message);
475 if (from_addr == NULL)
476 from_addr = guess_from_received_header (config, message);
478 if (from_addr == NULL)
479 from_addr = notmuch_config_get_user_primary_email (config);
481 from_addr = talloc_asprintf (ctx, "%s <%s>",
482 notmuch_config_get_user_name (config),
484 g_mime_object_set_header (GMIME_OBJECT (reply),
487 in_reply_to = talloc_asprintf (ctx, "<%s>",
488 notmuch_message_get_message_id (message));
490 g_mime_object_set_header (GMIME_OBJECT (reply),
491 "In-Reply-To", in_reply_to);
493 orig_references = notmuch_message_get_header (message, "references");
494 references = talloc_asprintf (ctx, "%s%s%s",
495 orig_references ? orig_references : "",
496 orig_references ? " " : "",
498 g_mime_object_set_header (GMIME_OBJECT (reply),
499 "References", references);
501 show_reply_headers (reply);
503 g_object_unref (G_OBJECT (reply));
506 printf ("On %s, %s wrote:\n",
507 notmuch_message_get_header (message, "date"),
508 notmuch_message_get_header (message, "from"));
510 show_message_body (notmuch_message_get_filename (message),
513 notmuch_message_destroy (message);
518 /* This format is currently tuned for a git send-email --notmuch hook */
520 notmuch_reply_format_headers_only(void *ctx, notmuch_config_t *config, notmuch_query_t *query)
523 notmuch_messages_t *messages;
524 notmuch_message_t *message;
525 const char *in_reply_to, *orig_references, *references;
528 for (messages = notmuch_query_search_messages (query);
529 notmuch_messages_valid (messages);
530 notmuch_messages_move_to_next (messages))
532 message = notmuch_messages_get (messages);
534 /* The 0 means we do not want headers in a "pretty" order. */
535 reply = g_mime_message_new (0);
537 fprintf (stderr, "Out of memory\n");
541 in_reply_to = talloc_asprintf (ctx, "<%s>",
542 notmuch_message_get_message_id (message));
544 g_mime_object_set_header (GMIME_OBJECT (reply),
545 "In-Reply-To", in_reply_to);
548 orig_references = notmuch_message_get_header (message, "references");
550 /* We print In-Reply-To followed by References because git format-patch treats them
551 * specially. Git does not interpret the other headers specially
553 references = talloc_asprintf (ctx, "%s%s%s",
554 orig_references ? orig_references : "",
555 orig_references ? " " : "",
557 g_mime_object_set_header (GMIME_OBJECT (reply),
558 "References", references);
560 (void)add_recipients_from_message (reply, config, message);
562 reply_headers = g_mime_object_to_string (GMIME_OBJECT (reply));
563 printf ("%s", reply_headers);
564 free (reply_headers);
566 g_object_unref (G_OBJECT (reply));
569 notmuch_message_destroy (message);
575 notmuch_reply_command (void *ctx, int argc, char *argv[])
577 notmuch_config_t *config;
578 notmuch_database_t *notmuch;
579 notmuch_query_t *query;
580 char *opt, *query_string;
582 int (*reply_format_func)(void *ctx, notmuch_config_t *config, notmuch_query_t *query);
584 reply_format_func = notmuch_reply_format_default;
586 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
587 if (strcmp (argv[i], "--") == 0) {
591 if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
592 opt = argv[i] + sizeof ("--format=") - 1;
593 if (strcmp (opt, "default") == 0) {
594 reply_format_func = notmuch_reply_format_default;
595 } else if (strcmp (opt, "headers-only") == 0) {
596 reply_format_func = notmuch_reply_format_headers_only;
598 fprintf (stderr, "Invalid value for --format: %s\n", opt);
602 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
610 config = notmuch_config_open (ctx, NULL, NULL);
614 query_string = query_string_from_args (ctx, argc, argv);
615 if (query_string == NULL) {
616 fprintf (stderr, "Out of memory\n");
620 if (*query_string == '\0') {
621 fprintf (stderr, "Error: notmuch reply requires at least one search term.\n");
625 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
626 NOTMUCH_DATABASE_MODE_READ_ONLY);
630 query = notmuch_query_create (notmuch, query_string);
632 fprintf (stderr, "Out of memory\n");
636 if (reply_format_func (ctx, config, query) != 0)
639 notmuch_query_destroy (query);
640 notmuch_database_close (notmuch);