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"
27 reply_part(GMimeObject *part, int *part_count)
29 GMimeContentDisposition *disposition;
30 GMimeContentType *content_type;
31 GMimeDataWrapper *wrapper;
34 disposition = g_mime_object_get_content_disposition (part);
36 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
38 const char *filename = g_mime_part_get_filename (GMIME_PART (part));
39 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
41 printf ("Attachment: %s (%s)\n", filename,
42 g_mime_content_type_to_string (content_type));
46 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
48 if (g_mime_content_type_is_type (content_type, "text", "*") &&
49 !g_mime_content_type_is_type (content_type, "text", "html"))
51 GMimeStream *stream_stdout = NULL, *stream_filter = NULL;
52 stream_stdout = g_mime_stream_file_new (stdout);
54 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
55 stream_filter = g_mime_stream_filter_new(stream_stdout);
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 printf ("Non-text part: %s\n",
70 g_mime_content_type_to_string (content_type));
74 /* Is the given address configured as one of the user's "personal" or
75 * "other" addresses. */
77 address_is_users (const char *address, notmuch_config_t *config)
81 unsigned int i, other_len;
83 primary = notmuch_config_get_user_primary_email (config);
84 if (strcmp (primary, address) == 0)
87 other = notmuch_config_get_user_other_email (config, &other_len);
88 for (i = 0; i < other_len; i++)
89 if (strcmp (other[i], address) == 0)
95 /* For each address in 'list' that is not configured as one of the
96 * user's addresses in 'config', add that address to 'message' as an
99 * The first address encountered that *is* the user's address will be
100 * returned, (otherwise NULL is returned).
103 add_recipients_for_address_list (GMimeMessage *message,
104 notmuch_config_t *config,
105 GMimeRecipientType type,
106 InternetAddressList *list)
108 InternetAddress *address;
110 const char *ret = NULL;
112 for (i = 0; i < internet_address_list_length (list); i++) {
113 address = internet_address_list_get_address (list, i);
114 if (INTERNET_ADDRESS_IS_GROUP (address)) {
115 InternetAddressGroup *group;
116 InternetAddressList *group_list;
118 group = INTERNET_ADDRESS_GROUP (address);
119 group_list = internet_address_group_get_members (group);
120 if (group_list == NULL)
123 add_recipients_for_address_list (message, config,
126 InternetAddressMailbox *mailbox;
130 mailbox = INTERNET_ADDRESS_MAILBOX (address);
132 name = internet_address_get_name (address);
133 addr = internet_address_mailbox_get_addr (mailbox);
135 if (address_is_users (addr, config)) {
139 g_mime_message_add_recipient (message, type, name, addr);
147 /* For each address in 'recipients' that is not configured as one of
148 * the user's addresses in 'config', add that address to 'message' as
149 * an address of 'type'.
151 * The first address encountered that *is* the user's address will be
152 * returned, (otherwise NULL is returned).
155 add_recipients_for_string (GMimeMessage *message,
156 notmuch_config_t *config,
157 GMimeRecipientType type,
158 const char *recipients)
160 InternetAddressList *list;
162 list = internet_address_list_parse_string (recipients);
166 return add_recipients_for_address_list (message, config, type, list);
170 notmuch_reply_command (void *ctx, int argc, char *argv[])
172 notmuch_config_t *config;
173 notmuch_database_t *notmuch;
174 notmuch_query_t *query;
177 notmuch_messages_t *messages;
178 notmuch_message_t *message;
180 const char *subject, *recipients, *from_addr = NULL;
181 const char *in_reply_to, *orig_references, *references;
185 GMimeRecipientType recipient_type;
187 { "from", GMIME_RECIPIENT_TYPE_TO },
188 { "to", GMIME_RECIPIENT_TYPE_TO },
189 { "cc", GMIME_RECIPIENT_TYPE_CC },
190 { "bcc", GMIME_RECIPIENT_TYPE_BCC }
194 config = notmuch_config_open (ctx, NULL, NULL);
198 notmuch = notmuch_database_open (notmuch_config_get_database_path (config));
202 query_string = query_string_from_args (ctx, argc, argv);
203 if (query_string == NULL) {
204 fprintf (stderr, "Out of memory\n");
208 query = notmuch_query_create (notmuch, query_string);
210 fprintf (stderr, "Out of memory\n");
214 for (messages = notmuch_query_search_messages (query);
215 notmuch_messages_has_more (messages);
216 notmuch_messages_advance (messages))
218 message = notmuch_messages_get (messages);
220 /* The 1 means we want headers in a "pretty" order. */
221 reply = g_mime_message_new (1);
223 fprintf (stderr, "Out of memory\n");
227 subject = notmuch_message_get_header (message, "subject");
229 if (strncasecmp (subject, "Re:", 3))
230 subject = talloc_asprintf (ctx, "Re: %s", subject);
231 g_mime_message_set_subject (reply, subject);
233 for (i = 0; i < ARRAY_SIZE (reply_to_map); i++) {
236 recipients = notmuch_message_get_header (message,
237 reply_to_map[i].header);
238 addr = add_recipients_for_string (reply, config,
239 reply_to_map[i].recipient_type,
241 if (from_addr == NULL)
245 if (from_addr == NULL)
246 from_addr = notmuch_config_get_user_primary_email (config);
248 from_addr = talloc_asprintf (ctx, "%s <%s>",
249 notmuch_config_get_user_name (config),
251 g_mime_object_set_header (GMIME_OBJECT (reply),
254 in_reply_to = talloc_asprintf (ctx, "<%s>",
255 notmuch_message_get_message_id (message));
257 g_mime_object_set_header (GMIME_OBJECT (reply),
258 "In-Reply-To", in_reply_to);
260 orig_references = notmuch_message_get_header (message, "references");
261 references = talloc_asprintf (ctx, "%s%s%s",
262 orig_references ? orig_references : "",
263 orig_references ? " " : "",
265 g_mime_object_set_header (GMIME_OBJECT (reply),
266 "References", references);
268 reply_headers = g_mime_object_to_string (GMIME_OBJECT (reply));
269 printf ("%s", reply_headers);
270 free (reply_headers);
272 g_object_unref (G_OBJECT (reply));
275 printf ("On %s, %s wrote:\n",
276 notmuch_message_get_header (message, "date"),
277 notmuch_message_get_header (message, "from"));
279 show_message_body (notmuch_message_get_filename (message), reply_part);
281 notmuch_message_destroy (message);
284 notmuch_query_destroy (query);
285 notmuch_database_close (notmuch);