]> git.notmuchmail.org Git - notmuch/blob - notmuch-show.c
emacs: Fix the notmuch-search-authors-width variable.
[notmuch] / notmuch-show.c
1 /* notmuch - Not much of an email program, (just index and search)
2  *
3  * Copyright © 2009 Carl Worth
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see http://www.gnu.org/licenses/ .
17  *
18  * Author: Carl Worth <cworth@cworth.org>
19  */
20
21 #include "notmuch-client.h"
22
23 typedef struct show_format {
24     const char *message_set_start;
25     const char *message_start;
26     void (*message) (const void *ctx,
27                      notmuch_message_t *message,
28                      int indent);
29     const char *header_start;
30     void (*header) (const void *ctx,
31                     notmuch_message_t *message);
32     const char *header_end;
33     const char *body_start;
34     void (*part) (GMimeObject *part,
35                   int *part_count);
36     const char *body_end;
37     const char *message_end;
38     const char *message_set_sep;
39     const char *message_set_end;
40 } show_format_t;
41
42 static void
43 format_message_text (unused (const void *ctx),
44                      notmuch_message_t *message,
45                      int indent);
46 static void
47 format_headers_text (const void *ctx,
48                      notmuch_message_t *message);
49 static void
50 format_part_text (GMimeObject *part,
51                   int *part_count);
52 static const show_format_t format_text = {
53     "",
54         "\fmessage{ ", format_message_text,
55             "\fheader{\n", format_headers_text, "\fheader}\n",
56             "\fbody{\n", format_part_text, "\fbody}\n",
57         "\fmessage}\n", "",
58     ""
59 };
60
61 static void
62 format_message_json (const void *ctx,
63                      notmuch_message_t *message,
64                      unused (int indent));
65 static void
66 format_headers_json (const void *ctx,
67                      notmuch_message_t *message);
68 static void
69 format_part_json (GMimeObject *part,
70                   int *part_count);
71 static const show_format_t format_json = {
72     "[",
73         "{", format_message_json,
74             ", \"headers\": {", format_headers_json, "}",
75             ", \"body\": [", format_part_json, "]",
76         "}", ", ",
77     "]"
78 };
79
80 static const char *
81 _get_tags_as_string (const void *ctx, notmuch_message_t *message)
82 {
83     notmuch_tags_t *tags;
84     int first = 1;
85     const char *tag;
86     char *result;
87
88     result = talloc_strdup (ctx, "");
89     if (result == NULL)
90         return NULL;
91
92     for (tags = notmuch_message_get_tags (message);
93          notmuch_tags_valid (tags);
94          notmuch_tags_move_to_next (tags))
95     {
96         tag = notmuch_tags_get (tags);
97
98         result = talloc_asprintf_append (result, "%s%s",
99                                          first ? "" : " ", tag);
100         first = 0;
101     }
102
103     return result;
104 }
105
106 /* Get a nice, single-line summary of message. */
107 static const char *
108 _get_one_line_summary (const void *ctx, notmuch_message_t *message)
109 {
110     const char *from;
111     time_t date;
112     const char *relative_date;
113     const char *tags;
114
115     from = notmuch_message_get_header (message, "from");
116
117     date = notmuch_message_get_date (message);
118     relative_date = notmuch_time_relative_date (ctx, date);
119
120     tags = _get_tags_as_string (ctx, message);
121
122     return talloc_asprintf (ctx, "%s (%s) (%s)",
123                             from, relative_date, tags);
124 }
125
126 static void
127 format_message_text (unused (const void *ctx), notmuch_message_t *message, int indent)
128 {
129     printf ("id:%s depth:%d match:%d filename:%s\n",
130             notmuch_message_get_message_id (message),
131             indent,
132             notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH),
133             notmuch_message_get_filename (message));
134 }
135
136 static void
137 format_message_json (const void *ctx, notmuch_message_t *message, unused (int indent))
138 {
139     notmuch_tags_t *tags;
140     int first = 1;
141     void *ctx_quote = talloc_new (ctx);
142
143     printf ("\"id\": %s, \"match\": %s, \"filename\": %s, \"tags\": [",
144             json_quote_str (ctx_quote, notmuch_message_get_message_id (message)),
145             notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH) ? "true" : "false",
146             json_quote_str (ctx_quote, notmuch_message_get_filename (message)));
147
148     for (tags = notmuch_message_get_tags (message);
149          notmuch_tags_valid (tags);
150          notmuch_tags_move_to_next (tags))
151     {
152          printf("%s%s", first ? "" : ",",
153                json_quote_str (ctx_quote, notmuch_tags_get (tags)));
154          first = 0;
155     }
156     printf("]");
157     talloc_free (ctx_quote);
158 }
159
160 static void
161 format_headers_text (const void *ctx, notmuch_message_t *message)
162 {
163     const char *headers[] = {
164         "Subject", "From", "To", "Cc", "Bcc", "Date"
165     };
166     const char *name, *value;
167     unsigned int i;
168
169     printf ("%s\n", _get_one_line_summary (ctx, message));
170
171     for (i = 0; i < ARRAY_SIZE (headers); i++) {
172         name = headers[i];
173         value = notmuch_message_get_header (message, name);
174         if (value && strlen (value))
175             printf ("%s: %s\n", name, value);
176     }
177 }
178
179 static void
180 format_headers_json (const void *ctx, notmuch_message_t *message)
181 {
182     const char *headers[] = {
183         "Subject", "From", "To", "Cc", "Bcc", "Date"
184     };
185     const char *name, *value;
186     unsigned int i;
187     int first_header = 1;
188     void *ctx_quote = talloc_new (ctx);
189
190     for (i = 0; i < ARRAY_SIZE (headers); i++) {
191         name = headers[i];
192         value = notmuch_message_get_header (message, name);
193         if (value)
194         {
195             if (!first_header)
196                 fputs (", ", stdout);
197             first_header = 0;
198
199             printf ("%s: %s",
200                     json_quote_str (ctx_quote, name),
201                     json_quote_str (ctx_quote, value));
202         }
203     }
204
205     talloc_free (ctx_quote);
206 }
207
208 static void
209 show_part_content (GMimeObject *part, GMimeStream *stream_out)
210 {
211     GMimeStream *stream_filter = NULL;
212     GMimeDataWrapper *wrapper;
213     const char *charset;
214
215     charset = g_mime_object_get_content_type_parameter (part, "charset");
216
217     if (stream_out) {
218         stream_filter = g_mime_stream_filter_new(stream_out);
219         g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter),
220                                  g_mime_filter_crlf_new(FALSE, FALSE));
221         if (charset) {
222           g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter),
223                                    g_mime_filter_charset_new(charset, "UTF-8"));
224         }
225     }
226
227     wrapper = g_mime_part_get_content_object (GMIME_PART (part));
228     if (wrapper && stream_filter)
229         g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
230     if (stream_filter)
231         g_object_unref(stream_filter);
232 }
233
234 static void
235 format_part_text (GMimeObject *part, int *part_count)
236 {
237     GMimeContentDisposition *disposition;
238     GMimeContentType *content_type;
239     GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
240
241     g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
242
243     disposition = g_mime_object_get_content_disposition (part);
244     if (disposition &&
245         strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
246     {
247         const char *filename = g_mime_part_get_filename (GMIME_PART (part));
248         content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
249
250         printf ("\fattachment{ ID: %d, Content-type: %s\n",
251                 *part_count,
252                 g_mime_content_type_to_string (content_type));
253         printf ("Attachment: %s (%s)\n", filename,
254                 g_mime_content_type_to_string (content_type));
255
256         if (g_mime_content_type_is_type (content_type, "text", "*") &&
257             !g_mime_content_type_is_type (content_type, "text", "html"))
258         {
259             show_part_content (part, stream_stdout);
260         }
261
262         printf ("\fattachment}\n");
263
264         if (stream_stdout)
265             g_object_unref(stream_stdout);
266
267         return;
268     }
269
270     content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
271
272     printf ("\fpart{ ID: %d, Content-type: %s\n",
273             *part_count,
274             g_mime_content_type_to_string (content_type));
275
276     if (g_mime_content_type_is_type (content_type, "text", "*") &&
277         !g_mime_content_type_is_type (content_type, "text", "html"))
278     {
279         show_part_content (part, stream_stdout);
280     }
281     else
282     {
283         printf ("Non-text part: %s\n",
284                 g_mime_content_type_to_string (content_type));
285     }
286
287     printf ("\fpart}\n");
288
289     if (stream_stdout)
290         g_object_unref(stream_stdout);
291 }
292
293 static void
294 format_part_json (GMimeObject *part, int *part_count)
295 {
296     GMimeContentType *content_type;
297     GMimeContentDisposition *disposition;
298     void *ctx = talloc_new (NULL);
299     GMimeStream *stream_memory = g_mime_stream_mem_new ();
300     GByteArray *part_content;
301
302     content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
303
304     if (*part_count > 1)
305         fputs (", ", stdout);
306
307     printf ("{\"id\": %d, \"content-type\": %s",
308             *part_count,
309             json_quote_str (ctx, g_mime_content_type_to_string (content_type)));
310
311     disposition = g_mime_object_get_content_disposition (part);
312     if (disposition &&
313         strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
314     {
315         const char *filename = g_mime_part_get_filename (GMIME_PART (part));
316
317         printf (", \"filename\": %s", json_quote_str (ctx, filename));
318     }
319
320     if (g_mime_content_type_is_type (content_type, "text", "*") &&
321         !g_mime_content_type_is_type (content_type, "text", "html"))
322     {
323         show_part_content (part, stream_memory);
324         part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));
325
326         printf (", \"content\": %s", json_quote_str (ctx, (char *) part_content->data));
327     }
328
329     fputs ("}", stdout);
330
331     talloc_free (ctx);
332     if (stream_memory)
333         g_object_unref (stream_memory);
334 }
335
336 static void
337 show_message (void *ctx, const show_format_t *format, notmuch_message_t *message, int indent)
338 {
339     fputs (format->message_start, stdout);
340     if (format->message)
341         format->message(ctx, message, indent);
342
343     fputs (format->header_start, stdout);
344     if (format->header)
345         format->header(ctx, message);
346     fputs (format->header_end, stdout);
347
348     fputs (format->body_start, stdout);
349     if (format->part)
350         show_message_body (notmuch_message_get_filename (message), format->part);
351     fputs (format->body_end, stdout);
352
353     fputs (format->message_end, stdout);
354 }
355
356
357 static void
358 show_messages (void *ctx, const show_format_t *format, notmuch_messages_t *messages, int indent,
359                notmuch_bool_t entire_thread)
360 {
361     notmuch_message_t *message;
362     notmuch_bool_t match;
363     int first_set = 1;
364     int next_indent;
365
366     fputs (format->message_set_start, stdout);
367
368     for (;
369          notmuch_messages_valid (messages);
370          notmuch_messages_move_to_next (messages))
371     {
372         if (!first_set)
373             fputs (format->message_set_sep, stdout);
374         first_set = 0;
375
376         fputs (format->message_set_start, stdout);
377
378         message = notmuch_messages_get (messages);
379
380         match = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH);
381
382         next_indent = indent;
383
384         if (match || entire_thread) {
385             show_message (ctx, format, message, indent);
386             next_indent = indent + 1;
387
388             fputs (format->message_set_sep, stdout);
389         }
390
391         show_messages (ctx, format, notmuch_message_get_replies (message),
392                        next_indent, entire_thread);
393
394         notmuch_message_destroy (message);
395
396         fputs (format->message_set_end, stdout);
397     }
398
399     fputs (format->message_set_end, stdout);
400 }
401
402 int
403 notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
404 {
405     notmuch_config_t *config;
406     notmuch_database_t *notmuch;
407     notmuch_query_t *query;
408     notmuch_threads_t *threads;
409     notmuch_thread_t *thread;
410     notmuch_messages_t *messages;
411     char *query_string;
412     char *opt;
413     const show_format_t *format = &format_text;
414     int entire_thread = 0;
415     int i;
416     int first_toplevel = 1;
417
418     for (i = 0; i < argc && argv[i][0] == '-'; i++) {
419         if (strcmp (argv[i], "--") == 0) {
420             i++;
421             break;
422         }
423         if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
424             opt = argv[i] + sizeof ("--format=") - 1;
425             if (strcmp (opt, "text") == 0) {
426                 format = &format_text;
427             } else if (strcmp (opt, "json") == 0) {
428                 format = &format_json;
429                 entire_thread = 1;
430             } else {
431                 fprintf (stderr, "Invalid value for --format: %s\n", opt);
432                 return 1;
433             }
434         } else if (STRNCMP_LITERAL (argv[i], "--entire-thread") == 0) {
435             entire_thread = 1;
436         } else {
437             fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
438             return 1;
439         }
440     }
441
442     argc -= i;
443     argv += i;
444
445     config = notmuch_config_open (ctx, NULL, NULL);
446     if (config == NULL)
447         return 1;
448
449     query_string = query_string_from_args (ctx, argc, argv);
450     if (query_string == NULL) {
451         fprintf (stderr, "Out of memory\n");
452         return 1;
453     }
454
455     if (*query_string == '\0') {
456         fprintf (stderr, "Error: notmuch show requires at least one search term.\n");
457         return 1;
458     }
459
460     notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
461                                      NOTMUCH_DATABASE_MODE_READ_ONLY);
462     if (notmuch == NULL)
463         return 1;
464
465     query = notmuch_query_create (notmuch, query_string);
466     if (query == NULL) {
467         fprintf (stderr, "Out of memory\n");
468         return 1;
469     }
470
471     fputs (format->message_set_start, stdout);
472
473     for (threads = notmuch_query_search_threads (query);
474          notmuch_threads_valid (threads);
475          notmuch_threads_move_to_next (threads))
476     {
477         thread = notmuch_threads_get (threads);
478
479         messages = notmuch_thread_get_toplevel_messages (thread);
480
481         if (messages == NULL)
482             INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
483                             notmuch_thread_get_thread_id (thread));
484
485         if (!first_toplevel)
486             fputs (format->message_set_sep, stdout);
487         first_toplevel = 0;
488
489         show_messages (ctx, format, messages, 0, entire_thread);
490
491         notmuch_thread_destroy (thread);
492
493     }
494
495     fputs (format->message_set_end, stdout);
496
497     notmuch_query_destroy (query);
498     notmuch_database_close (notmuch);
499
500     return 0;
501 }