]> git.notmuchmail.org Git - notmuch/blob - notmuch-reply.c
notmuch: Move welcome messages from "notmuch" to "notmuch setup".
[notmuch] / notmuch-reply.c
1 /* notmuch - Not much of an email program, (just index and search)
2  *
3  * Copyright © 2009 Carl Worth
4  * Copyright © 2009 Keith Packard
5  *
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.
10  *
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.
15  *
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/ .
18  *
19  * Authors: Carl Worth <cworth@cworth.org>
20  *          Keith Packard <keithp@keithp.com>
21  */
22
23 #include "notmuch-client.h"
24 #include "gmime-filter-reply.h"
25
26 static void
27 reply_part(GMimeObject *part, int *part_count)
28 {
29     GMimeContentDisposition *disposition;
30     GMimeContentType *content_type;
31     GMimeDataWrapper *wrapper;
32
33     (void) part_count;
34     disposition = g_mime_object_get_content_disposition (part);
35     if (disposition &&
36         strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
37     {
38         const char *filename = g_mime_part_get_filename (GMIME_PART (part));
39         content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
40
41         printf ("Attachment: %s (%s)\n", filename,
42                 g_mime_content_type_to_string (content_type));
43         return;
44     }
45
46     content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
47
48     if (g_mime_content_type_is_type (content_type, "text", "*") &&
49         !g_mime_content_type_is_type (content_type, "text", "html"))
50     {
51         GMimeStream *stream_stdout = NULL, *stream_filter = NULL;
52         stream_stdout = g_mime_stream_file_new (stdout);
53         if (stream_stdout) {
54             g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
55             stream_filter = g_mime_stream_filter_new(stream_stdout);
56         }
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);
62         if (stream_filter)
63             g_object_unref(stream_filter);
64         if (stream_stdout)
65             g_object_unref(stream_stdout);
66     }
67     else
68     {
69         printf ("Non-text part: %s\n",
70                 g_mime_content_type_to_string (content_type));
71     }
72 }
73
74 static void
75 add_recipients_for_address_list (GMimeMessage *message,
76                                  GMimeRecipientType type,
77                                  InternetAddressList *list)
78 {
79     InternetAddress *address;
80     int i;
81
82     for (i = 0; i < internet_address_list_length (list); i++) {
83         address = internet_address_list_get_address (list, i);
84         if (INTERNET_ADDRESS_IS_GROUP (address)) {
85             InternetAddressGroup *group;
86             InternetAddressList *group_list;
87
88             group = INTERNET_ADDRESS_GROUP (address);
89             group_list = internet_address_group_get_members (group);
90             if (group_list == NULL)
91                 continue;
92
93             add_recipients_for_address_list (message, type, group_list);
94         } else {
95             InternetAddressMailbox *mailbox;
96             const char *name;
97             const char *addr;
98
99             mailbox = INTERNET_ADDRESS_MAILBOX (address);
100
101             name = internet_address_get_name (address);
102             addr = internet_address_mailbox_get_addr (mailbox);
103
104             g_mime_message_add_recipient (message, type, name, addr);
105         }
106     }
107 }
108
109 static void
110 add_recipients_for_string (GMimeMessage *message,
111                            GMimeRecipientType type,
112                            const char *recipients)
113 {
114     InternetAddressList *list;
115
116     list = internet_address_list_parse_string (recipients);
117     if (list == NULL)
118         return;
119
120     add_recipients_for_address_list (message, type, list);
121 }
122
123 int
124 notmuch_reply_command (void *ctx, int argc, char *argv[])
125 {
126     void *local = talloc_new (ctx);
127     notmuch_query_t *query = NULL;
128     notmuch_database_t *notmuch = NULL;
129     GMimeMessage *reply = NULL;
130     char *query_string;
131     notmuch_messages_t *messages;
132     notmuch_message_t *message;
133     int ret = 0;
134     const char *subject, *recipients;
135     const char *in_reply_to, *orig_references, *references;
136     char *reply_headers;
137     struct {
138         const char *header;
139         GMimeRecipientType recipient_type;
140     } reply_to_map[] = {
141         { "from", GMIME_RECIPIENT_TYPE_TO  },
142         { "to",   GMIME_RECIPIENT_TYPE_TO  },
143         { "cc",   GMIME_RECIPIENT_TYPE_CC  },
144         { "bcc",  GMIME_RECIPIENT_TYPE_BCC }
145     };
146     unsigned int i;
147
148     notmuch = notmuch_database_open (NULL);
149     if (notmuch == NULL) {
150         ret = 1;
151         goto DONE;
152     }
153
154     query_string = query_string_from_args (local, argc, argv);
155     if (query_string == NULL) {
156         fprintf (stderr, "Out of memory\n");
157         ret = 1;
158         goto DONE;
159     }
160
161     query = notmuch_query_create (notmuch, query_string);
162     if (query == NULL) {
163         fprintf (stderr, "Out of memory\n");
164         ret = 1;
165         goto DONE;
166     }
167
168     for (messages = notmuch_query_search_messages (query);
169          notmuch_messages_has_more (messages);
170          notmuch_messages_advance (messages))
171     {
172         message = notmuch_messages_get (messages);
173
174         /* The 1 means we want headers in a "pretty" order. */
175         reply = g_mime_message_new (1);
176         if (reply == NULL) {
177             fprintf (stderr, "Out of memory\n");
178             ret = 1;
179             goto DONE;
180         }
181
182         /* XXX: We need a configured email address (or addresses) for
183          * the user here, so that we can prevent replying to the user,
184          * and also call _mime_message_set_sender to set From: (either
185          * from the first "owned" address mentioned as a recipient in
186          * the original message, or else some default address).
187          */
188
189         subject = notmuch_message_get_header (message, "subject");
190
191         if (strncasecmp (subject, "Re:", 3))
192             subject = talloc_asprintf (ctx, "Re: %s", subject);
193         g_mime_message_set_subject (reply, subject);
194
195         for (i = 0; i < ARRAY_SIZE (reply_to_map); i++) {
196             recipients = notmuch_message_get_header (message,
197                                                      reply_to_map[i].header);
198             add_recipients_for_string (reply,
199                                        reply_to_map[i].recipient_type,
200                                        recipients);
201         }
202
203         in_reply_to = talloc_asprintf (ctx, "<%s>",
204                              notmuch_message_get_message_id (message));
205
206         g_mime_object_set_header (GMIME_OBJECT (reply),
207                                   "In-Reply-To", in_reply_to);
208
209         orig_references = notmuch_message_get_header (message, "references");
210         references = talloc_asprintf (ctx, "%s%s%s",
211                                       orig_references ? orig_references : "",
212                                       orig_references ? " " : "",
213                                       in_reply_to);
214         g_mime_object_set_header (GMIME_OBJECT (reply),
215                                   "References", references);
216
217         reply_headers = g_mime_object_to_string (GMIME_OBJECT (reply));
218         printf ("%s", reply_headers);
219         free (reply_headers);
220
221         g_object_unref (G_OBJECT (reply));
222         reply = NULL;
223
224         printf ("On %s, %s wrote:\n",
225                 notmuch_message_get_header (message, "date"),
226                 notmuch_message_get_header (message, "from"));
227
228         show_message_body (notmuch_message_get_filename (message), reply_part);
229
230         notmuch_message_destroy (message);
231     }
232
233   DONE:
234     if (local)
235         talloc_free (local);
236
237     if (query)
238         notmuch_query_destroy (query);
239
240     if (notmuch)
241         notmuch_database_close (notmuch);
242
243     if (reply)
244         g_object_unref (G_OBJECT (reply));
245
246     return ret;
247 }