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 GMimeStream *stream_stdout = NULL, *stream_filter = NULL;
31 GMimeDataWrapper *wrapper;
34 charset = g_mime_object_get_content_type_parameter (part, "charset");
35 stream_stdout = g_mime_stream_file_new (stdout);
37 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
38 stream_filter = g_mime_stream_filter_new(stream_stdout);
40 g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter),
41 g_mime_filter_charset_new(charset, "UTF-8"));
44 g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter),
45 g_mime_filter_reply_new(TRUE));
46 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
47 if (wrapper && stream_filter)
48 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
50 g_object_unref(stream_filter);
52 g_object_unref(stream_stdout);
56 show_reply_headers (GMimeMessage *message)
58 GMimeStream *stream_stdout = NULL, *stream_filter = NULL;
60 stream_stdout = g_mime_stream_file_new (stdout);
62 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
63 stream_filter = g_mime_stream_filter_new(stream_stdout);
65 g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter),
66 g_mime_filter_headers_new());
67 g_mime_object_write_to_stream(GMIME_OBJECT(message), stream_filter);
68 g_object_unref(stream_filter);
70 g_object_unref(stream_stdout);
75 reply_part (GMimeObject *part, int *part_count)
77 GMimeContentDisposition *disposition;
78 GMimeContentType *content_type;
81 disposition = g_mime_object_get_content_disposition (part);
83 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
85 const char *filename = g_mime_part_get_filename (GMIME_PART (part));
86 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
88 if (g_mime_content_type_is_type (content_type, "text", "*") &&
89 !g_mime_content_type_is_type (content_type, "text", "html"))
91 reply_part_content (part);
95 printf ("Attachment: %s (%s)\n", filename,
96 g_mime_content_type_to_string (content_type));
102 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
104 if (g_mime_content_type_is_type (content_type, "text", "*") &&
105 !g_mime_content_type_is_type (content_type, "text", "html"))
107 reply_part_content (part);
111 printf ("Non-text part: %s\n",
112 g_mime_content_type_to_string (content_type));
116 /* Is the given address configured as one of the user's "personal" or
117 * "other" addresses. */
119 address_is_users (const char *address, notmuch_config_t *config)
125 primary = notmuch_config_get_user_primary_email (config);
126 if (strcasecmp (primary, address) == 0)
129 other = notmuch_config_get_user_other_email (config, &other_len);
130 for (i = 0; i < other_len; i++)
131 if (strcasecmp (other[i], address) == 0)
137 /* For each address in 'list' that is not configured as one of the
138 * user's addresses in 'config', add that address to 'message' as an
141 * The first address encountered that *is* the user's address will be
142 * returned, (otherwise NULL is returned).
145 add_recipients_for_address_list (GMimeMessage *message,
146 notmuch_config_t *config,
147 GMimeRecipientType type,
148 InternetAddressList *list)
150 InternetAddress *address;
152 const char *ret = NULL;
154 for (i = 0; i < internet_address_list_length (list); i++) {
155 address = internet_address_list_get_address (list, i);
156 if (INTERNET_ADDRESS_IS_GROUP (address)) {
157 InternetAddressGroup *group;
158 InternetAddressList *group_list;
160 group = INTERNET_ADDRESS_GROUP (address);
161 group_list = internet_address_group_get_members (group);
162 if (group_list == NULL)
165 add_recipients_for_address_list (message, config,
168 InternetAddressMailbox *mailbox;
172 mailbox = INTERNET_ADDRESS_MAILBOX (address);
174 name = internet_address_get_name (address);
175 addr = internet_address_mailbox_get_addr (mailbox);
177 if (address_is_users (addr, config)) {
181 g_mime_message_add_recipient (message, type, name, addr);
189 /* For each address in 'recipients' that is not configured as one of
190 * the user's addresses in 'config', add that address to 'message' as
191 * an address of 'type'.
193 * The first address encountered that *is* the user's address will be
194 * returned, (otherwise NULL is returned).
197 add_recipients_for_string (GMimeMessage *message,
198 notmuch_config_t *config,
199 GMimeRecipientType type,
200 const char *recipients)
202 InternetAddressList *list;
204 if (recipients == NULL)
207 list = internet_address_list_parse_string (recipients);
211 return add_recipients_for_address_list (message, config, type, list);
214 /* Does the address in the Reply-To header of 'message' already appear
215 * in either the 'To' or 'Cc' header of the message?
218 reply_to_header_is_redundant (notmuch_message_t *message)
220 const char *reply_to, *to, *cc, *addr;
221 InternetAddressList *list;
222 InternetAddress *address;
223 InternetAddressMailbox *mailbox;
225 reply_to = notmuch_message_get_header (message, "reply-to");
226 if (reply_to == NULL || *reply_to == '\0')
229 list = internet_address_list_parse_string (reply_to);
231 if (internet_address_list_length (list) != 1)
234 address = internet_address_list_get_address (list, 0);
235 if (INTERNET_ADDRESS_IS_GROUP (address))
238 mailbox = INTERNET_ADDRESS_MAILBOX (address);
239 addr = internet_address_mailbox_get_addr (mailbox);
241 to = notmuch_message_get_header (message, "to");
242 cc = notmuch_message_get_header (message, "cc");
244 if ((to && strstr (to, addr) != 0) ||
245 (cc && strstr (cc, addr) != 0))
253 /* Augments the recipients of reply from the headers of message.
255 * If any of the user's addresses were found in these headers, the first
256 * of these returned, otherwise NULL is returned.
259 add_recipients_from_message (GMimeMessage *reply,
260 notmuch_config_t *config,
261 notmuch_message_t *message)
265 const char *fallback;
266 GMimeRecipientType recipient_type;
268 { "reply-to", "from", GMIME_RECIPIENT_TYPE_TO },
269 { "to", NULL, GMIME_RECIPIENT_TYPE_TO },
270 { "cc", NULL, GMIME_RECIPIENT_TYPE_CC },
271 { "bcc", NULL, GMIME_RECIPIENT_TYPE_BCC }
273 const char *from_addr = NULL;
276 /* Some mailing lists munge the Reply-To header despite it being A Bad
277 * Thing, see http://www.unicom.com/pw/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 exists
281 * in either To or Cc). So in this case, we ignore the Reply-To
282 * field and use the From header. Thie ensures the original sender
283 * will get the reply even if not subscribed to the list. Note
284 * that the address in the Reply-To header will always appear in
287 if (reply_to_header_is_redundant (message)) {
288 reply_to_map[0].header = "from";
289 reply_to_map[0].fallback = NULL;
292 for (i = 0; i < ARRAY_SIZE (reply_to_map); i++) {
293 const char *addr, *recipients;
295 recipients = notmuch_message_get_header (message,
296 reply_to_map[i].header);
297 if ((recipients == NULL || recipients[0] == '\0') && reply_to_map[i].fallback)
298 recipients = notmuch_message_get_header (message,
299 reply_to_map[i].fallback);
301 addr = add_recipients_for_string (reply, config,
302 reply_to_map[i].recipient_type,
304 if (from_addr == NULL)
312 guess_from_received_header (notmuch_config_t *config, notmuch_message_t *message)
314 const char *received,*primary,*by;
316 char *mta,*ptr,*token;
319 const char *delim=". \t";
320 size_t i,j,other_len;
322 const char *to_headers[] = {"Envelope-to", "X-Original-To"};
324 primary = notmuch_config_get_user_primary_email (config);
325 other = notmuch_config_get_user_other_email (config, &other_len);
327 /* sadly, there is no standard way to find out to which email
328 * address a mail was delivered - what is in the headers depends
329 * on the MTAs used along the way. So we are trying a number of
330 * heuristics which hopefully will answer this question.
332 * We only got here if none of the users email addresses are in
333 * the To: or Cc: header. From here we try the following in order:
334 * 1) check for an Envelope-to: header
335 * 2) check for an X-Original-To: header
336 * 3) check for a (for <email@add.res>) clause in Received: headers
337 * 4) check for the domain part of known email addresses in the
338 * 'by' part of Received headers
339 * If none of these work, we give up and return NULL
341 for (i = 0; i < sizeof(to_headers)/sizeof(*to_headers); i++) {
342 tohdr = xstrdup(notmuch_message_get_header (message, to_headers[i]));
343 if (tohdr && *tohdr) {
344 /* tohdr is potentialy a list of email addresses, so here we
345 * check if one of the email addresses is a substring of tohdr
347 if (strcasestr(tohdr, primary)) {
351 for (j = 0; j < other_len; j++)
352 if (strcasestr (tohdr, other[j])) {
360 /* We get the concatenated Received: headers and search from the
361 * front (last Received: header added) and try to extract from
362 * them indications to which email address this message was
364 * The Received: header is special in our get_header function
365 * and is always concated.
367 received = notmuch_message_get_header (message, "received");
368 if (received == NULL)
371 /* First we look for a " for <email@add.res>" in the received
374 ptr = strstr (received, " for ");
376 /* the text following is potentialy a list of email addresses,
377 * so again we check if one of the email addresses is a
380 if (strcasestr(ptr, primary)) {
383 for (i = 0; i < other_len; i++)
384 if (strcasestr (ptr, other[i])) {
388 /* Finally, we parse all the " by MTA ..." headers to guess the
389 * email address that this was originally delivered to.
390 * We extract just the MTA here by removing leading whitespace and
391 * assuming that the MTA name ends at the next whitespace.
392 * We test for *(by+4) to be non-'\0' to make sure there's
393 * something there at all - and then assume that the first
394 * whitespace delimited token that follows is the receiving
395 * system in this step of the receive chain
398 while((by = strstr (by, " by ")) != NULL) {
403 token = strtok(mta," \t");
406 /* Now extract the last two components of the MTA host name
409 while ((ptr = strsep (&token, delim)) != NULL) {
417 /* Recombine domain and tld and look for it among the configured
419 * This time we have a known domain name and nothing else - so
420 * the test is the other way around: we check if this is a
421 * substring of one of the email addresses.
425 if (strcasestr(primary, domain)) {
429 for (i = 0; i < other_len; i++)
430 if (strcasestr (other[i],domain)) {
442 notmuch_reply_format_default(void *ctx, notmuch_config_t *config, notmuch_query_t *query)
445 notmuch_messages_t *messages;
446 notmuch_message_t *message;
447 const char *subject, *from_addr = NULL;
448 const char *in_reply_to, *orig_references, *references;
450 for (messages = notmuch_query_search_messages (query);
451 notmuch_messages_valid (messages);
452 notmuch_messages_move_to_next (messages))
454 message = notmuch_messages_get (messages);
456 /* The 1 means we want headers in a "pretty" order. */
457 reply = g_mime_message_new (1);
459 fprintf (stderr, "Out of memory\n");
463 subject = notmuch_message_get_header (message, "subject");
465 if (strncasecmp (subject, "Re:", 3))
466 subject = talloc_asprintf (ctx, "Re: %s", subject);
467 g_mime_message_set_subject (reply, subject);
470 from_addr = add_recipients_from_message (reply, config, message);
472 if (from_addr == NULL)
473 from_addr = guess_from_received_header (config, message);
475 if (from_addr == NULL)
476 from_addr = notmuch_config_get_user_primary_email (config);
478 from_addr = talloc_asprintf (ctx, "%s <%s>",
479 notmuch_config_get_user_name (config),
481 g_mime_object_set_header (GMIME_OBJECT (reply),
484 g_mime_object_set_header (GMIME_OBJECT (reply), "Bcc",
485 notmuch_config_get_user_primary_email (config));
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), reply_part);
512 notmuch_message_destroy (message);
517 /* This format is currently tuned for a git send-email --notmuch hook */
519 notmuch_reply_format_headers_only(void *ctx, notmuch_config_t *config, notmuch_query_t *query)
522 notmuch_messages_t *messages;
523 notmuch_message_t *message;
524 const char *in_reply_to, *orig_references, *references;
527 for (messages = notmuch_query_search_messages (query);
528 notmuch_messages_valid (messages);
529 notmuch_messages_move_to_next (messages))
531 message = notmuch_messages_get (messages);
533 /* The 0 means we do not want headers in a "pretty" order. */
534 reply = g_mime_message_new (0);
536 fprintf (stderr, "Out of memory\n");
540 in_reply_to = talloc_asprintf (ctx, "<%s>",
541 notmuch_message_get_message_id (message));
543 g_mime_object_set_header (GMIME_OBJECT (reply),
544 "In-Reply-To", in_reply_to);
547 orig_references = notmuch_message_get_header (message, "references");
549 /* We print In-Reply-To followed by References because git format-patch treats them
550 * specially. Git does not interpret the other headers specially
552 references = talloc_asprintf (ctx, "%s%s%s",
553 orig_references ? orig_references : "",
554 orig_references ? " " : "",
556 g_mime_object_set_header (GMIME_OBJECT (reply),
557 "References", references);
559 (void)add_recipients_from_message (reply, config, message);
561 g_mime_object_set_header (GMIME_OBJECT (reply), "Bcc",
562 notmuch_config_get_user_primary_email (config));
564 reply_headers = g_mime_object_to_string (GMIME_OBJECT (reply));
565 printf ("%s", reply_headers);
566 free (reply_headers);
568 g_object_unref (G_OBJECT (reply));
571 notmuch_message_destroy (message);
577 notmuch_reply_command (void *ctx, int argc, char *argv[])
579 notmuch_config_t *config;
580 notmuch_database_t *notmuch;
581 notmuch_query_t *query;
582 char *opt, *query_string;
584 int (*reply_format_func)(void *ctx, notmuch_config_t *config, notmuch_query_t *query);
586 reply_format_func = notmuch_reply_format_default;
588 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
589 if (strcmp (argv[i], "--") == 0) {
593 if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
594 opt = argv[i] + sizeof ("--format=") - 1;
595 if (strcmp (opt, "default") == 0) {
596 reply_format_func = notmuch_reply_format_default;
597 } else if (strcmp (opt, "headers-only") == 0) {
598 reply_format_func = notmuch_reply_format_headers_only;
600 fprintf (stderr, "Invalid value for --format: %s\n", opt);
604 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
612 config = notmuch_config_open (ctx, NULL, NULL);
616 query_string = query_string_from_args (ctx, argc, argv);
617 if (query_string == NULL) {
618 fprintf (stderr, "Out of memory\n");
622 if (*query_string == '\0') {
623 fprintf (stderr, "Error: notmuch reply requires at least one search term.\n");
627 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
628 NOTMUCH_DATABASE_MODE_READ_ONLY);
632 query = notmuch_query_create (notmuch, query_string);
634 fprintf (stderr, "Out of memory\n");
638 if (reply_format_func (ctx, config, query) != 0)
641 notmuch_query_destroy (query);
642 notmuch_database_close (notmuch);