]> git.notmuchmail.org Git - notmuch/blob - notmuch-show.c
Fix minor style issues in show_part_content function.
[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     time_t date;
143     const char *relative_date;
144
145     date = notmuch_message_get_date (message);
146     relative_date = notmuch_time_relative_date (ctx, date);
147
148     printf ("\"id\": %s, \"match\": %s, \"filename\": %s, \"timestamp\": %ld, \"date_relative\": \"%s\", \"tags\": [",
149             json_quote_str (ctx_quote, notmuch_message_get_message_id (message)),
150             notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH) ? "true" : "false",
151             json_quote_str (ctx_quote, notmuch_message_get_filename (message)),
152             date, relative_date);
153
154     for (tags = notmuch_message_get_tags (message);
155          notmuch_tags_valid (tags);
156          notmuch_tags_move_to_next (tags))
157     {
158          printf("%s%s", first ? "" : ",",
159                json_quote_str (ctx_quote, notmuch_tags_get (tags)));
160          first = 0;
161     }
162     printf("]");
163     talloc_free (ctx_quote);
164 }
165
166 static void
167 format_headers_text (const void *ctx, notmuch_message_t *message)
168 {
169     const char *headers[] = {
170         "Subject", "From", "To", "Cc", "Bcc", "Date"
171     };
172     const char *name, *value;
173     unsigned int i;
174
175     printf ("%s\n", _get_one_line_summary (ctx, message));
176
177     for (i = 0; i < ARRAY_SIZE (headers); i++) {
178         name = headers[i];
179         value = notmuch_message_get_header (message, name);
180         if (value && strlen (value))
181             printf ("%s: %s\n", name, value);
182     }
183 }
184
185 static void
186 format_headers_json (const void *ctx, notmuch_message_t *message)
187 {
188     const char *headers[] = {
189         "Subject", "From", "To", "Cc", "Bcc", "Date"
190     };
191     const char *name, *value;
192     unsigned int i;
193     int first_header = 1;
194     void *ctx_quote = talloc_new (ctx);
195
196     for (i = 0; i < ARRAY_SIZE (headers); i++) {
197         name = headers[i];
198         value = notmuch_message_get_header (message, name);
199         if (value)
200         {
201             if (!first_header)
202                 fputs (", ", stdout);
203             first_header = 0;
204
205             printf ("%s: %s",
206                     json_quote_str (ctx_quote, name),
207                     json_quote_str (ctx_quote, value));
208         }
209     }
210
211     talloc_free (ctx_quote);
212 }
213
214 static void
215 show_part_content (GMimeObject *part, GMimeStream *stream_out)
216 {
217     GMimeStream *stream_filter = NULL;
218     GMimeDataWrapper *wrapper;
219     const char *charset;
220
221     charset = g_mime_object_get_content_type_parameter (part, "charset");
222
223     if (stream_out) {
224         stream_filter = g_mime_stream_filter_new (stream_out);
225         g_mime_stream_filter_add(GMIME_STREAM_FILTER (stream_filter),
226                                  g_mime_filter_crlf_new (FALSE, FALSE));
227         if (charset) {
228             GMimeFilter *charset_filter;
229             charset_filter = g_mime_filter_charset_new (charset, "UTF-8");
230             /* This result can be NULL for things like "unknown-8bit".
231              * Don't set a NULL filter as that makes GMime print
232              * annoying assertion-failure messages on stderr. */
233             if (charset_filter)
234                 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
235                                           charset_filter);
236         }
237     }
238
239     wrapper = g_mime_part_get_content_object (GMIME_PART (part));
240     if (wrapper && stream_filter)
241         g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
242     if (stream_filter)
243         g_object_unref(stream_filter);
244 }
245
246 static void
247 format_part_text (GMimeObject *part, int *part_count)
248 {
249     GMimeContentDisposition *disposition;
250     GMimeContentType *content_type;
251
252     disposition = g_mime_object_get_content_disposition (part);
253     if (disposition &&
254         strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
255     {
256         const char *filename = g_mime_part_get_filename (GMIME_PART (part));
257         content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
258
259         printf ("\fattachment{ ID: %d, Content-type: %s\n",
260                 *part_count,
261                 g_mime_content_type_to_string (content_type));
262         printf ("Attachment: %s (%s)\n", filename,
263                 g_mime_content_type_to_string (content_type));
264
265         if (g_mime_content_type_is_type (content_type, "text", "*") &&
266             !g_mime_content_type_is_type (content_type, "text", "html"))
267         {
268             GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
269             g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
270             show_part_content (part, stream_stdout);
271             g_object_unref(stream_stdout);
272         }
273
274         printf ("\fattachment}\n");
275
276         return;
277     }
278
279     content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
280
281     printf ("\fpart{ ID: %d, Content-type: %s\n",
282             *part_count,
283             g_mime_content_type_to_string (content_type));
284
285     if (g_mime_content_type_is_type (content_type, "text", "*") &&
286         !g_mime_content_type_is_type (content_type, "text", "html"))
287     {
288         GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
289         g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
290         show_part_content (part, stream_stdout);
291         g_object_unref(stream_stdout);
292     }
293     else
294     {
295         printf ("Non-text part: %s\n",
296                 g_mime_content_type_to_string (content_type));
297     }
298
299     printf ("\fpart}\n");
300 }
301
302 static void
303 format_part_json (GMimeObject *part, int *part_count)
304 {
305     GMimeContentType *content_type;
306     GMimeContentDisposition *disposition;
307     void *ctx = talloc_new (NULL);
308     GMimeStream *stream_memory = g_mime_stream_mem_new ();
309     GByteArray *part_content;
310
311     content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
312
313     if (*part_count > 1)
314         fputs (", ", stdout);
315
316     printf ("{\"id\": %d, \"content-type\": %s",
317             *part_count,
318             json_quote_str (ctx, g_mime_content_type_to_string (content_type)));
319
320     disposition = g_mime_object_get_content_disposition (part);
321     if (disposition &&
322         strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
323     {
324         const char *filename = g_mime_part_get_filename (GMIME_PART (part));
325
326         printf (", \"filename\": %s", json_quote_str (ctx, filename));
327     }
328
329     if (g_mime_content_type_is_type (content_type, "text", "*") &&
330         !g_mime_content_type_is_type (content_type, "text", "html"))
331     {
332         show_part_content (part, stream_memory);
333         part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));
334
335         printf (", \"content\": %s", json_quote_chararray (ctx, (char *) part_content->data, part_content->len));
336     }
337
338     fputs ("}", stdout);
339
340     talloc_free (ctx);
341     if (stream_memory)
342         g_object_unref (stream_memory);
343 }
344
345 static void
346 show_message (void *ctx, const show_format_t *format, notmuch_message_t *message, int indent)
347 {
348     fputs (format->message_start, stdout);
349     if (format->message)
350         format->message(ctx, message, indent);
351
352     fputs (format->header_start, stdout);
353     if (format->header)
354         format->header(ctx, message);
355     fputs (format->header_end, stdout);
356
357     fputs (format->body_start, stdout);
358     if (format->part)
359         show_message_body (notmuch_message_get_filename (message), format->part);
360     fputs (format->body_end, stdout);
361
362     fputs (format->message_end, stdout);
363 }
364
365
366 static void
367 show_messages (void *ctx, const show_format_t *format, notmuch_messages_t *messages, int indent,
368                notmuch_bool_t entire_thread)
369 {
370     notmuch_message_t *message;
371     notmuch_bool_t match;
372     int first_set = 1;
373     int next_indent;
374
375     fputs (format->message_set_start, stdout);
376
377     for (;
378          notmuch_messages_valid (messages);
379          notmuch_messages_move_to_next (messages))
380     {
381         if (!first_set)
382             fputs (format->message_set_sep, stdout);
383         first_set = 0;
384
385         fputs (format->message_set_start, stdout);
386
387         message = notmuch_messages_get (messages);
388
389         match = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH);
390
391         next_indent = indent;
392
393         if (match || entire_thread) {
394             show_message (ctx, format, message, indent);
395             next_indent = indent + 1;
396
397             fputs (format->message_set_sep, stdout);
398         }
399
400         show_messages (ctx, format, notmuch_message_get_replies (message),
401                        next_indent, entire_thread);
402
403         notmuch_message_destroy (message);
404
405         fputs (format->message_set_end, stdout);
406     }
407
408     fputs (format->message_set_end, stdout);
409 }
410
411 int
412 notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
413 {
414     notmuch_config_t *config;
415     notmuch_database_t *notmuch;
416     notmuch_query_t *query;
417     notmuch_threads_t *threads;
418     notmuch_thread_t *thread;
419     notmuch_messages_t *messages;
420     char *query_string;
421     char *opt;
422     const show_format_t *format = &format_text;
423     int entire_thread = 0;
424     int i;
425     int first_toplevel = 1;
426
427     for (i = 0; i < argc && argv[i][0] == '-'; i++) {
428         if (strcmp (argv[i], "--") == 0) {
429             i++;
430             break;
431         }
432         if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
433             opt = argv[i] + sizeof ("--format=") - 1;
434             if (strcmp (opt, "text") == 0) {
435                 format = &format_text;
436             } else if (strcmp (opt, "json") == 0) {
437                 format = &format_json;
438                 entire_thread = 1;
439             } else {
440                 fprintf (stderr, "Invalid value for --format: %s\n", opt);
441                 return 1;
442             }
443         } else if (STRNCMP_LITERAL (argv[i], "--entire-thread") == 0) {
444             entire_thread = 1;
445         } else {
446             fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
447             return 1;
448         }
449     }
450
451     argc -= i;
452     argv += i;
453
454     config = notmuch_config_open (ctx, NULL, NULL);
455     if (config == NULL)
456         return 1;
457
458     query_string = query_string_from_args (ctx, argc, argv);
459     if (query_string == NULL) {
460         fprintf (stderr, "Out of memory\n");
461         return 1;
462     }
463
464     if (*query_string == '\0') {
465         fprintf (stderr, "Error: notmuch show requires at least one search term.\n");
466         return 1;
467     }
468
469     notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
470                                      NOTMUCH_DATABASE_MODE_READ_ONLY);
471     if (notmuch == NULL)
472         return 1;
473
474     query = notmuch_query_create (notmuch, query_string);
475     if (query == NULL) {
476         fprintf (stderr, "Out of memory\n");
477         return 1;
478     }
479
480     fputs (format->message_set_start, stdout);
481
482     for (threads = notmuch_query_search_threads (query);
483          notmuch_threads_valid (threads);
484          notmuch_threads_move_to_next (threads))
485     {
486         thread = notmuch_threads_get (threads);
487
488         messages = notmuch_thread_get_toplevel_messages (thread);
489
490         if (messages == NULL)
491             INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
492                             notmuch_thread_get_thread_id (thread));
493
494         if (!first_toplevel)
495             fputs (format->message_set_sep, stdout);
496         first_toplevel = 0;
497
498         show_messages (ctx, format, messages, 0, entire_thread);
499
500         notmuch_thread_destroy (thread);
501
502     }
503
504     fputs (format->message_set_end, stdout);
505
506     notmuch_query_destroy (query);
507     notmuch_database_close (notmuch);
508
509     return 0;
510 }
511
512 int
513 notmuch_part_command (void *ctx, unused (int argc), unused (char *argv[]))
514 {
515         notmuch_config_t *config;
516         notmuch_database_t *notmuch;
517         notmuch_query_t *query;
518         notmuch_messages_t *messages;
519         notmuch_message_t *message;
520         char *query_string;
521         int i;
522         int part = 0;
523
524         for (i = 0; i < argc && argv[i][0] == '-'; i++) {
525                 if (strcmp (argv[i], "--") == 0) {
526                         i++;
527                         break;
528                 }
529                 if (STRNCMP_LITERAL (argv[i], "--part=") == 0) {
530                         part = atoi(argv[i] + sizeof ("--part=") - 1);
531                 } else {
532                         fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
533                         return 1;
534                 }
535         }
536
537         argc -= i;
538         argv += i;
539
540         config = notmuch_config_open (ctx, NULL, NULL);
541         if (config == NULL)
542                 return 1;
543
544         query_string = query_string_from_args (ctx, argc, argv);
545         if (query_string == NULL) {
546                 fprintf (stderr, "Out of memory\n");
547                 return 1;
548         }
549
550         if (*query_string == '\0') {
551                 fprintf (stderr, "Error: notmuch part requires at least one search term.\n");
552                 return 1;
553         }
554
555         notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
556                                          NOTMUCH_DATABASE_MODE_READ_ONLY);
557         if (notmuch == NULL)
558                 return 1;
559
560         query = notmuch_query_create (notmuch, query_string);
561         if (query == NULL) {
562                 fprintf (stderr, "Out of memory\n");
563                 return 1;
564         }
565
566         if (notmuch_query_count_messages (query) != 1) {
567                 fprintf (stderr, "Error: search term did not match precisely one message.\n");
568                 return 1;
569         }
570
571         messages = notmuch_query_search_messages (query);
572         message = notmuch_messages_get (messages);
573
574         if (message == NULL) {
575                 fprintf (stderr, "Error: cannot find matching message.\n");
576                 return 1;
577         }
578
579         show_one_part (notmuch_message_get_filename (message), part);
580
581         notmuch_query_destroy (query);
582         notmuch_database_close (notmuch);
583
584         return 0;
585 }