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"
29 GMimeRecipientType recipient_type;
31 { "reply-to", "from", GMIME_RECIPIENT_TYPE_TO },
32 { "to", NULL, GMIME_RECIPIENT_TYPE_TO },
33 { "cc", NULL, GMIME_RECIPIENT_TYPE_CC },
34 { "bcc", NULL, GMIME_RECIPIENT_TYPE_BCC }
38 reply_part_content (GMimeObject *part)
40 GMimeStream *stream_stdout = NULL, *stream_filter = NULL;
41 GMimeDataWrapper *wrapper;
44 charset = g_mime_object_get_content_type_parameter (part, "charset");
45 stream_stdout = g_mime_stream_file_new (stdout);
47 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
48 stream_filter = g_mime_stream_filter_new(stream_stdout);
50 g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter),
51 g_mime_filter_charset_new(charset, "UTF-8"));
54 g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter),
55 g_mime_filter_reply_new(TRUE));
56 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
57 if (wrapper && stream_filter)
58 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
60 g_object_unref(stream_filter);
62 g_object_unref(stream_stdout);
66 reply_part (GMimeObject *part, int *part_count)
68 GMimeContentDisposition *disposition;
69 GMimeContentType *content_type;
72 disposition = g_mime_object_get_content_disposition (part);
74 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
76 const char *filename = g_mime_part_get_filename (GMIME_PART (part));
77 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
79 if (g_mime_content_type_is_type (content_type, "text", "*") &&
80 !g_mime_content_type_is_type (content_type, "text", "html"))
82 reply_part_content (part);
86 printf ("Attachment: %s (%s)\n", filename,
87 g_mime_content_type_to_string (content_type));
93 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
95 if (g_mime_content_type_is_type (content_type, "text", "*") &&
96 !g_mime_content_type_is_type (content_type, "text", "html"))
98 reply_part_content (part);
102 printf ("Non-text part: %s\n",
103 g_mime_content_type_to_string (content_type));
107 /* Is the given address configured as one of the user's "personal" or
108 * "other" addresses. */
110 address_is_users (const char *address, notmuch_config_t *config)
116 primary = notmuch_config_get_user_primary_email (config);
117 if (strcasecmp (primary, address) == 0)
120 other = notmuch_config_get_user_other_email (config, &other_len);
121 for (i = 0; i < other_len; i++)
122 if (strcasecmp (other[i], address) == 0)
128 /* For each address in 'list' that is not configured as one of the
129 * user's addresses in 'config', add that address to 'message' as an
132 * The first address encountered that *is* the user's address will be
133 * returned, (otherwise NULL is returned).
136 add_recipients_for_address_list (GMimeMessage *message,
137 notmuch_config_t *config,
138 GMimeRecipientType type,
139 InternetAddressList *list)
141 InternetAddress *address;
143 const char *ret = NULL;
145 for (i = 0; i < internet_address_list_length (list); i++) {
146 address = internet_address_list_get_address (list, i);
147 if (INTERNET_ADDRESS_IS_GROUP (address)) {
148 InternetAddressGroup *group;
149 InternetAddressList *group_list;
151 group = INTERNET_ADDRESS_GROUP (address);
152 group_list = internet_address_group_get_members (group);
153 if (group_list == NULL)
156 add_recipients_for_address_list (message, config,
159 InternetAddressMailbox *mailbox;
163 mailbox = INTERNET_ADDRESS_MAILBOX (address);
165 name = internet_address_get_name (address);
166 addr = internet_address_mailbox_get_addr (mailbox);
168 if (address_is_users (addr, config)) {
172 g_mime_message_add_recipient (message, type, name, addr);
180 /* For each address in 'recipients' that is not configured as one of
181 * the user's addresses in 'config', add that address to 'message' as
182 * an address of 'type'.
184 * The first address encountered that *is* the user's address will be
185 * returned, (otherwise NULL is returned).
188 add_recipients_for_string (GMimeMessage *message,
189 notmuch_config_t *config,
190 GMimeRecipientType type,
191 const char *recipients)
193 InternetAddressList *list;
195 list = internet_address_list_parse_string (recipients);
199 return add_recipients_for_address_list (message, config, type, list);
203 notmuch_reply_format_default(void *ctx, notmuch_config_t *config, notmuch_query_t *query)
206 notmuch_messages_t *messages;
207 notmuch_message_t *message;
208 const char *subject, *recipients, *from_addr = NULL;
209 const char *in_reply_to, *orig_references, *references;
213 for (messages = notmuch_query_search_messages (query);
214 notmuch_messages_has_more (messages);
215 notmuch_messages_advance (messages))
217 message = notmuch_messages_get (messages);
219 /* The 1 means we want headers in a "pretty" order. */
220 reply = g_mime_message_new (1);
222 fprintf (stderr, "Out of memory\n");
226 subject = notmuch_message_get_header (message, "subject");
228 if (strncasecmp (subject, "Re:", 3))
229 subject = talloc_asprintf (ctx, "Re: %s", subject);
230 g_mime_message_set_subject (reply, subject);
232 for (i = 0; i < ARRAY_SIZE (reply_to_map); i++) {
235 recipients = notmuch_message_get_header (message,
236 reply_to_map[i].header);
237 if ((recipients == NULL || recipients[0] == '\0') && reply_to_map[i].fallback)
238 recipients = notmuch_message_get_header (message,
239 reply_to_map[i].fallback);
241 addr = add_recipients_for_string (reply, config,
242 reply_to_map[i].recipient_type,
244 if (from_addr == NULL)
248 if (from_addr == NULL)
249 from_addr = notmuch_config_get_user_primary_email (config);
251 from_addr = talloc_asprintf (ctx, "%s <%s>",
252 notmuch_config_get_user_name (config),
254 g_mime_object_set_header (GMIME_OBJECT (reply),
257 g_mime_object_set_header (GMIME_OBJECT (reply), "Bcc",
258 notmuch_config_get_user_primary_email (config));
260 in_reply_to = talloc_asprintf (ctx, "<%s>",
261 notmuch_message_get_message_id (message));
263 g_mime_object_set_header (GMIME_OBJECT (reply),
264 "In-Reply-To", in_reply_to);
266 orig_references = notmuch_message_get_header (message, "references");
267 references = talloc_asprintf (ctx, "%s%s%s",
268 orig_references ? orig_references : "",
269 orig_references ? " " : "",
271 g_mime_object_set_header (GMIME_OBJECT (reply),
272 "References", references);
274 reply_headers = g_mime_object_to_string (GMIME_OBJECT (reply));
275 printf ("%s", reply_headers);
276 free (reply_headers);
278 g_object_unref (G_OBJECT (reply));
281 printf ("On %s, %s wrote:\n",
282 notmuch_message_get_header (message, "date"),
283 notmuch_message_get_header (message, "from"));
285 show_message_body (notmuch_message_get_filename (message), reply_part);
287 notmuch_message_destroy (message);
292 /* This format is currently tuned for a git send-email --notmuch hook */
294 notmuch_reply_format_headers_only(void *ctx, notmuch_config_t *config, notmuch_query_t *query)
297 notmuch_messages_t *messages;
298 notmuch_message_t *message;
299 const char *recipients, *in_reply_to, *orig_references, *references;
303 for (messages = notmuch_query_search_messages (query);
304 notmuch_messages_has_more (messages);
305 notmuch_messages_advance (messages))
307 message = notmuch_messages_get (messages);
309 /* The 0 means we do not want headers in a "pretty" order. */
310 reply = g_mime_message_new (0);
312 fprintf (stderr, "Out of memory\n");
316 in_reply_to = talloc_asprintf (ctx, "<%s>",
317 notmuch_message_get_message_id (message));
319 g_mime_object_set_header (GMIME_OBJECT (reply),
320 "In-Reply-To", in_reply_to);
323 orig_references = notmuch_message_get_header (message, "references");
325 /* We print In-Reply-To followed by References because git format-patch treats them
326 * specially. Git does not interpret the other headers specially
328 references = talloc_asprintf (ctx, "%s%s%s",
329 orig_references ? orig_references : "",
330 orig_references ? " " : "",
332 g_mime_object_set_header (GMIME_OBJECT (reply),
333 "References", references);
335 for (i = 0; i < ARRAY_SIZE (reply_to_map); i++) {
338 recipients = notmuch_message_get_header (message,
339 reply_to_map[i].header);
340 if ((recipients == NULL || recipients[0] == '\0') && reply_to_map[i].fallback)
341 recipients = notmuch_message_get_header (message,
342 reply_to_map[i].fallback);
344 addr = add_recipients_for_string (reply, config,
345 reply_to_map[i].recipient_type,
349 g_mime_object_set_header (GMIME_OBJECT (reply), "Bcc",
350 notmuch_config_get_user_primary_email (config));
352 reply_headers = g_mime_object_to_string (GMIME_OBJECT (reply));
353 printf ("%s", reply_headers);
354 free (reply_headers);
356 g_object_unref (G_OBJECT (reply));
359 notmuch_message_destroy (message);
365 notmuch_reply_command (void *ctx, int argc, char *argv[])
367 notmuch_config_t *config;
368 notmuch_database_t *notmuch;
369 notmuch_query_t *query;
370 char *opt, *query_string;
372 int (*reply_format_func)(void *ctx, notmuch_config_t *config, notmuch_query_t *query);
374 reply_format_func = notmuch_reply_format_default;
376 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
377 if (strcmp (argv[i], "--") == 0) {
381 if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
382 opt = argv[i] + sizeof ("--format=") - 1;
383 if (strcmp (opt, "default") == 0) {
384 reply_format_func = notmuch_reply_format_default;
385 } else if (strcmp (opt, "headers-only") == 0) {
386 reply_format_func = notmuch_reply_format_headers_only;
388 fprintf (stderr, "Invalid value for --format: %s\n", opt);
392 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
400 config = notmuch_config_open (ctx, NULL, NULL);
404 query_string = query_string_from_args (ctx, argc, argv);
405 if (query_string == NULL) {
406 fprintf (stderr, "Out of memory\n");
410 if (*query_string == '\0') {
411 fprintf (stderr, "Error: notmuch reply requires at least one search term.\n");
415 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
416 NOTMUCH_DATABASE_MODE_READ_ONLY);
420 query = notmuch_query_create (notmuch, query_string);
422 fprintf (stderr, "Out of memory\n");
426 if (reply_format_func (ctx, config, query) != 0)
429 notmuch_query_destroy (query);
430 notmuch_database_close (notmuch);