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 (GMimeObject *part,
29 unused (int *part_count));
31 static const notmuch_show_format_t format_reply = {
35 NULL, reply_part, NULL, NULL, NULL,
41 reply_part_content (GMimeObject *part)
43 GMimeStream *stream_stdout = NULL, *stream_filter = NULL;
44 GMimeDataWrapper *wrapper;
47 charset = g_mime_object_get_content_type_parameter (part, "charset");
48 stream_stdout = g_mime_stream_file_new (stdout);
50 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
51 stream_filter = g_mime_stream_filter_new(stream_stdout);
53 g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter),
54 g_mime_filter_charset_new(charset, "UTF-8"));
57 g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter),
58 g_mime_filter_reply_new(TRUE));
59 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
60 if (wrapper && stream_filter)
61 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
63 g_object_unref(stream_filter);
65 g_object_unref(stream_stdout);
69 show_reply_headers (GMimeMessage *message)
71 GMimeStream *stream_stdout = NULL, *stream_filter = NULL;
73 stream_stdout = g_mime_stream_file_new (stdout);
75 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
76 stream_filter = g_mime_stream_filter_new(stream_stdout);
78 g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter),
79 g_mime_filter_headers_new());
80 g_mime_object_write_to_stream(GMIME_OBJECT(message), stream_filter);
81 g_object_unref(stream_filter);
83 g_object_unref(stream_stdout);
88 reply_part (GMimeObject *part,
89 unused (int *part_count))
91 GMimeContentDisposition *disposition;
92 GMimeContentType *content_type;
94 disposition = g_mime_object_get_content_disposition (part);
96 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
98 const char *filename = g_mime_part_get_filename (GMIME_PART (part));
99 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
101 if (g_mime_content_type_is_type (content_type, "text", "*") &&
102 !g_mime_content_type_is_type (content_type, "text", "html"))
104 reply_part_content (part);
108 printf ("Attachment: %s (%s)\n", filename,
109 g_mime_content_type_to_string (content_type));
115 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
117 if (g_mime_content_type_is_type (content_type, "text", "*") &&
118 !g_mime_content_type_is_type (content_type, "text", "html"))
120 reply_part_content (part);
124 printf ("Non-text part: %s\n",
125 g_mime_content_type_to_string (content_type));
129 /* Is the given address configured as one of the user's "personal" or
130 * "other" addresses. */
132 address_is_users (const char *address, notmuch_config_t *config)
138 primary = notmuch_config_get_user_primary_email (config);
139 if (strcasecmp (primary, address) == 0)
142 other = notmuch_config_get_user_other_email (config, &other_len);
143 for (i = 0; i < other_len; i++)
144 if (strcasecmp (other[i], address) == 0)
150 /* For each address in 'list' that is not configured as one of the
151 * user's addresses in 'config', add that address to 'message' as an
154 * The first address encountered that *is* the user's address will be
155 * returned, (otherwise NULL is returned).
158 add_recipients_for_address_list (GMimeMessage *message,
159 notmuch_config_t *config,
160 GMimeRecipientType type,
161 InternetAddressList *list)
163 InternetAddress *address;
165 const char *ret = NULL;
167 for (i = 0; i < internet_address_list_length (list); i++) {
168 address = internet_address_list_get_address (list, i);
169 if (INTERNET_ADDRESS_IS_GROUP (address)) {
170 InternetAddressGroup *group;
171 InternetAddressList *group_list;
173 group = INTERNET_ADDRESS_GROUP (address);
174 group_list = internet_address_group_get_members (group);
175 if (group_list == NULL)
178 add_recipients_for_address_list (message, config,
181 InternetAddressMailbox *mailbox;
185 mailbox = INTERNET_ADDRESS_MAILBOX (address);
187 name = internet_address_get_name (address);
188 addr = internet_address_mailbox_get_addr (mailbox);
190 if (address_is_users (addr, config)) {
194 g_mime_message_add_recipient (message, type, name, addr);
202 /* For each address in 'recipients' that is not configured as one of
203 * the user's addresses in 'config', add that address to 'message' as
204 * an address of 'type'.
206 * The first address encountered that *is* the user's address will be
207 * returned, (otherwise NULL is returned).
210 add_recipients_for_string (GMimeMessage *message,
211 notmuch_config_t *config,
212 GMimeRecipientType type,
213 const char *recipients)
215 InternetAddressList *list;
217 if (recipients == NULL)
220 list = internet_address_list_parse_string (recipients);
224 return add_recipients_for_address_list (message, config, type, list);
227 /* Does the address in the Reply-To header of 'message' already appear
228 * in either the 'To' or 'Cc' header of the message?
231 reply_to_header_is_redundant (notmuch_message_t *message)
233 const char *reply_to, *to, *cc, *addr;
234 InternetAddressList *list;
235 InternetAddress *address;
236 InternetAddressMailbox *mailbox;
238 reply_to = notmuch_message_get_header (message, "reply-to");
239 if (reply_to == NULL || *reply_to == '\0')
242 list = internet_address_list_parse_string (reply_to);
244 if (internet_address_list_length (list) != 1)
247 address = internet_address_list_get_address (list, 0);
248 if (INTERNET_ADDRESS_IS_GROUP (address))
251 mailbox = INTERNET_ADDRESS_MAILBOX (address);
252 addr = internet_address_mailbox_get_addr (mailbox);
254 to = notmuch_message_get_header (message, "to");
255 cc = notmuch_message_get_header (message, "cc");
257 if ((to && strstr (to, addr) != 0) ||
258 (cc && strstr (cc, addr) != 0))
266 /* Augments the recipients of reply from the headers of message.
268 * If any of the user's addresses were found in these headers, the first
269 * of these returned, otherwise NULL is returned.
272 add_recipients_from_message (GMimeMessage *reply,
273 notmuch_config_t *config,
274 notmuch_message_t *message)
278 const char *fallback;
279 GMimeRecipientType recipient_type;
281 { "reply-to", "from", GMIME_RECIPIENT_TYPE_TO },
282 { "to", NULL, GMIME_RECIPIENT_TYPE_TO },
283 { "cc", NULL, GMIME_RECIPIENT_TYPE_CC },
284 { "bcc", NULL, GMIME_RECIPIENT_TYPE_BCC }
286 const char *from_addr = NULL;
289 /* Some mailing lists munge the Reply-To header despite it being A Bad
290 * Thing, see http://www.unicom.com/pw/reply-to-harmful.html
292 * The munging is easy to detect, because it results in a
293 * redundant reply-to header, (with an address that already exists
294 * in either To or Cc). So in this case, we ignore the Reply-To
295 * field and use the From header. Thie ensures the original sender
296 * will get the reply even if not subscribed to the list. Note
297 * that the address in the Reply-To header will always appear in
300 if (reply_to_header_is_redundant (message)) {
301 reply_to_map[0].header = "from";
302 reply_to_map[0].fallback = NULL;
305 for (i = 0; i < ARRAY_SIZE (reply_to_map); i++) {
306 const char *addr, *recipients;
308 recipients = notmuch_message_get_header (message,
309 reply_to_map[i].header);
310 if ((recipients == NULL || recipients[0] == '\0') && reply_to_map[i].fallback)
311 recipients = notmuch_message_get_header (message,
312 reply_to_map[i].fallback);
314 addr = add_recipients_for_string (reply, config,
315 reply_to_map[i].recipient_type,
317 if (from_addr == NULL)
325 guess_from_received_header (notmuch_config_t *config, notmuch_message_t *message)
327 const char *received,*primary,*by;
330 char *mta,*ptr,*token;
333 const char *delim=". \t";
334 size_t i,j,other_len;
336 const char *to_headers[] = {"Envelope-to", "X-Original-To"};
338 primary = notmuch_config_get_user_primary_email (config);
339 other = notmuch_config_get_user_other_email (config, &other_len);
341 /* sadly, there is no standard way to find out to which email
342 * address a mail was delivered - what is in the headers depends
343 * on the MTAs used along the way. So we are trying a number of
344 * heuristics which hopefully will answer this question.
346 * We only got here if none of the users email addresses are in
347 * the To: or Cc: header. From here we try the following in order:
348 * 1) check for an Envelope-to: header
349 * 2) check for an X-Original-To: header
350 * 3) check for a (for <email@add.res>) clause in Received: headers
351 * 4) check for the domain part of known email addresses in the
352 * 'by' part of Received headers
353 * If none of these work, we give up and return NULL
355 for (i = 0; i < sizeof(to_headers)/sizeof(*to_headers); i++) {
356 tohdr = xstrdup(notmuch_message_get_header (message, to_headers[i]));
357 if (tohdr && *tohdr) {
358 /* tohdr is potentialy a list of email addresses, so here we
359 * check if one of the email addresses is a substring of tohdr
361 if (strcasestr(tohdr, primary)) {
365 for (j = 0; j < other_len; j++)
366 if (strcasestr (tohdr, other[j])) {
374 /* We get the concatenated Received: headers and search from the
375 * front (last Received: header added) and try to extract from
376 * them indications to which email address this message was
378 * The Received: header is special in our get_header function
379 * and is always concated.
381 received = notmuch_message_get_header (message, "received");
382 if (received == NULL)
385 /* First we look for a " for <email@add.res>" in the received
388 ptr = strstr (received, " for ");
390 /* the text following is potentialy a list of email addresses,
391 * so again we check if one of the email addresses is a
394 if (strcasestr(ptr, primary)) {
397 for (i = 0; i < other_len; i++)
398 if (strcasestr (ptr, other[i])) {
402 /* Finally, we parse all the " by MTA ..." headers to guess the
403 * email address that this was originally delivered to.
404 * We extract just the MTA here by removing leading whitespace and
405 * assuming that the MTA name ends at the next whitespace.
406 * We test for *(by+4) to be non-'\0' to make sure there's
407 * something there at all - and then assume that the first
408 * whitespace delimited token that follows is the receiving
409 * system in this step of the receive chain
412 while((by = strstr (by, " by ")) != NULL) {
417 token = strtok(mta," \t");
420 /* Now extract the last two components of the MTA host name
423 while ((ptr = strsep (&token, delim)) != NULL) {
431 /* Recombine domain and tld and look for it among the configured
433 * This time we have a known domain name and nothing else - so
434 * the test is the other way around: we check if this is a
435 * substring of one of the email addresses.
439 if (strcasestr(primary, domain)) {
443 for (i = 0; i < other_len; i++)
444 if (strcasestr (other[i],domain)) {
456 notmuch_reply_format_default(void *ctx, notmuch_config_t *config, notmuch_query_t *query)
459 notmuch_messages_t *messages;
460 notmuch_message_t *message;
461 const char *subject, *from_addr = NULL;
462 const char *in_reply_to, *orig_references, *references;
463 const notmuch_show_format_t *format = &format_reply;
464 notmuch_show_params_t params;
467 for (messages = notmuch_query_search_messages (query);
468 notmuch_messages_valid (messages);
469 notmuch_messages_move_to_next (messages))
471 message = notmuch_messages_get (messages);
473 /* The 1 means we want headers in a "pretty" order. */
474 reply = g_mime_message_new (1);
476 fprintf (stderr, "Out of memory\n");
480 subject = notmuch_message_get_header (message, "subject");
482 if (strncasecmp (subject, "Re:", 3))
483 subject = talloc_asprintf (ctx, "Re: %s", subject);
484 g_mime_message_set_subject (reply, subject);
487 from_addr = add_recipients_from_message (reply, config, message);
489 if (from_addr == NULL)
490 from_addr = guess_from_received_header (config, message);
492 if (from_addr == NULL)
493 from_addr = notmuch_config_get_user_primary_email (config);
495 from_addr = talloc_asprintf (ctx, "%s <%s>",
496 notmuch_config_get_user_name (config),
498 g_mime_object_set_header (GMIME_OBJECT (reply),
501 in_reply_to = talloc_asprintf (ctx, "<%s>",
502 notmuch_message_get_message_id (message));
504 g_mime_object_set_header (GMIME_OBJECT (reply),
505 "In-Reply-To", in_reply_to);
507 orig_references = notmuch_message_get_header (message, "references");
508 references = talloc_asprintf (ctx, "%s%s%s",
509 orig_references ? orig_references : "",
510 orig_references ? " " : "",
512 g_mime_object_set_header (GMIME_OBJECT (reply),
513 "References", references);
515 show_reply_headers (reply);
517 g_object_unref (G_OBJECT (reply));
520 printf ("On %s, %s wrote:\n",
521 notmuch_message_get_header (message, "date"),
522 notmuch_message_get_header (message, "from"));
524 show_message_body (notmuch_message_get_filename (message),
527 notmuch_message_destroy (message);
532 /* This format is currently tuned for a git send-email --notmuch hook */
534 notmuch_reply_format_headers_only(void *ctx, notmuch_config_t *config, notmuch_query_t *query)
537 notmuch_messages_t *messages;
538 notmuch_message_t *message;
539 const char *in_reply_to, *orig_references, *references;
542 for (messages = notmuch_query_search_messages (query);
543 notmuch_messages_valid (messages);
544 notmuch_messages_move_to_next (messages))
546 message = notmuch_messages_get (messages);
548 /* The 0 means we do not want headers in a "pretty" order. */
549 reply = g_mime_message_new (0);
551 fprintf (stderr, "Out of memory\n");
555 in_reply_to = talloc_asprintf (ctx, "<%s>",
556 notmuch_message_get_message_id (message));
558 g_mime_object_set_header (GMIME_OBJECT (reply),
559 "In-Reply-To", in_reply_to);
562 orig_references = notmuch_message_get_header (message, "references");
564 /* We print In-Reply-To followed by References because git format-patch treats them
565 * specially. Git does not interpret the other headers specially
567 references = talloc_asprintf (ctx, "%s%s%s",
568 orig_references ? orig_references : "",
569 orig_references ? " " : "",
571 g_mime_object_set_header (GMIME_OBJECT (reply),
572 "References", references);
574 (void)add_recipients_from_message (reply, config, message);
576 reply_headers = g_mime_object_to_string (GMIME_OBJECT (reply));
577 printf ("%s", reply_headers);
578 free (reply_headers);
580 g_object_unref (G_OBJECT (reply));
583 notmuch_message_destroy (message);
589 notmuch_reply_command (void *ctx, int argc, char *argv[])
591 notmuch_config_t *config;
592 notmuch_database_t *notmuch;
593 notmuch_query_t *query;
594 char *opt, *query_string;
596 int (*reply_format_func)(void *ctx, notmuch_config_t *config, notmuch_query_t *query);
598 reply_format_func = notmuch_reply_format_default;
600 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
601 if (strcmp (argv[i], "--") == 0) {
605 if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
606 opt = argv[i] + sizeof ("--format=") - 1;
607 if (strcmp (opt, "default") == 0) {
608 reply_format_func = notmuch_reply_format_default;
609 } else if (strcmp (opt, "headers-only") == 0) {
610 reply_format_func = notmuch_reply_format_headers_only;
612 fprintf (stderr, "Invalid value for --format: %s\n", opt);
616 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
624 config = notmuch_config_open (ctx, NULL, NULL);
628 query_string = query_string_from_args (ctx, argc, argv);
629 if (query_string == NULL) {
630 fprintf (stderr, "Out of memory\n");
634 if (*query_string == '\0') {
635 fprintf (stderr, "Error: notmuch reply requires at least one search term.\n");
639 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
640 NOTMUCH_DATABASE_MODE_READ_ONLY);
644 query = notmuch_query_create (notmuch, query_string);
646 fprintf (stderr, "Out of memory\n");
650 if (reply_format_func (ctx, config, query) != 0)
653 notmuch_query_destroy (query);
654 notmuch_database_close (notmuch);