]> git.notmuchmail.org Git - notmuch/blob - notmuch-reply.c
notmuch.el: Add a binding ('r') to reply to the current message.
[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 int
75 notmuch_reply_command (void *ctx, int argc, char *argv[])
76 {
77     void *local = talloc_new (ctx);
78     char *query_string;
79     notmuch_database_t *notmuch = NULL;
80     notmuch_query_t *query = NULL;
81     notmuch_messages_t *messages;
82     notmuch_message_t *message;
83     int ret = 0;
84     int has_recipient;
85     const char *subject, *to, *references;
86
87     notmuch = notmuch_database_open (NULL);
88     if (notmuch == NULL) {
89         ret = 1;
90         goto DONE;
91     }
92
93     query_string = query_string_from_args (local, argc, argv);
94     if (query_string == NULL) {
95         fprintf (stderr, "Out of memory\n");
96         ret = 1;
97         goto DONE;
98     }
99
100     query = notmuch_query_create (notmuch, query_string);
101     if (query == NULL) {
102         fprintf (stderr, "Out of memory\n");
103         ret = 1;
104         goto DONE;
105     }
106
107     for (messages = notmuch_query_search_messages (query);
108          notmuch_messages_has_more (messages);
109          notmuch_messages_advance (messages))
110     {
111         message = notmuch_messages_get (messages);
112
113         subject = notmuch_message_get_header (message, "subject");
114
115         /* XXX: Should have the user's email address(es) configured
116          * somewhere, and should fish it out of any recipient headers
117          * to reply by default from the same address that the original
118          * email was sent to */
119         printf ("From: \n");
120
121         /* XXX: Should fold long recipient lists. */
122         printf ("To:");
123         has_recipient = 0;
124
125         to = notmuch_message_get_header (message, "from");
126         if (to) {
127             printf (" %s", to);
128             has_recipient = 1;
129         }
130         to = notmuch_message_get_header (message, "to");
131         if (to) {
132             printf ("%s%s",
133                     has_recipient ? ", " : " ", to);
134             has_recipient = 1;
135         }
136         to = notmuch_message_get_header (message, "cc");
137         if (to) {
138             printf ("%s%s",
139                     has_recipient ? ", " : " ", to);
140             has_recipient = 1;
141         }
142         to = notmuch_message_get_header (message, "bcc");
143         if (to) {
144             printf ("%s%s",
145                     has_recipient ? ", " : " ", to);
146             has_recipient = 1;
147         }
148
149         printf ("\n");
150
151         if (strncasecmp (subject, "Re:", 3))
152             subject = talloc_asprintf (ctx, "Re: %s", subject);
153         printf ("Subject: %s\n", subject);
154
155         printf ("In-Reply-To: <%s>\n",
156                 notmuch_message_get_message_id (message));
157
158         /* XXX: Should fold long references lists. */
159         references = notmuch_message_get_header (message, "references");
160         printf ("References: %s <%s>\n",
161                 references ? references : "",
162                 notmuch_message_get_message_id (message));
163
164         printf ("--text follows this line--\n");
165
166         show_message_body (notmuch_message_get_filename (message), reply_part);
167
168         notmuch_message_destroy (message);
169     }
170
171   DONE:
172     if (local)
173         talloc_free (local);
174
175     if (query)
176         notmuch_query_destroy (query);
177
178     if (notmuch)
179         notmuch_database_close (notmuch);
180
181     return ret;
182 }