]> git.notmuchmail.org Git - notmuch/blob - notmuch-show.c
Fixup string list author
[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 void
81 format_message_mbox (const void *ctx,
82                      notmuch_message_t *message,
83                      unused (int indent));
84
85 static const show_format_t format_mbox = {
86     "",
87         "", format_message_mbox,
88             "", NULL, "",
89             "", NULL, "",
90         "", "",
91     ""
92 };
93
94 static const char *
95 _get_tags_as_string (const void *ctx, notmuch_message_t *message)
96 {
97     notmuch_tags_t *tags;
98     int first = 1;
99     const char *tag;
100     char *result;
101
102     result = talloc_strdup (ctx, "");
103     if (result == NULL)
104         return NULL;
105
106     for (tags = notmuch_message_get_tags (message);
107          notmuch_tags_valid (tags);
108          notmuch_tags_move_to_next (tags))
109     {
110         tag = notmuch_tags_get (tags);
111
112         result = talloc_asprintf_append (result, "%s%s",
113                                          first ? "" : " ", tag);
114         first = 0;
115     }
116
117     return result;
118 }
119
120 /* Get a nice, single-line summary of message. */
121 static const char *
122 _get_one_line_summary (const void *ctx, notmuch_message_t *message)
123 {
124     const char *from;
125     time_t date;
126     const char *relative_date;
127     const char *tags;
128
129     from = notmuch_message_get_header (message, "from");
130
131     date = notmuch_message_get_date (message);
132     relative_date = notmuch_time_relative_date (ctx, date);
133
134     tags = _get_tags_as_string (ctx, message);
135
136     return talloc_asprintf (ctx, "%s (%s) (%s)",
137                             from, relative_date, tags);
138 }
139
140 static void
141 format_message_text (unused (const void *ctx), notmuch_message_t *message, int indent)
142 {
143     printf ("id:%s depth:%d match:%d filename:%s\n",
144             notmuch_message_get_message_id (message),
145             indent,
146             notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH),
147             notmuch_message_get_filename (message));
148 }
149
150 static void
151 format_message_json (const void *ctx, notmuch_message_t *message, unused (int indent))
152 {
153     notmuch_tags_t *tags;
154     int first = 1;
155     void *ctx_quote = talloc_new (ctx);
156     time_t date;
157     const char *relative_date;
158
159     date = notmuch_message_get_date (message);
160     relative_date = notmuch_time_relative_date (ctx, date);
161
162     printf ("\"id\": %s, \"match\": %s, \"filename\": %s, \"timestamp\": %ld, \"date_relative\": \"%s\", \"tags\": [",
163             json_quote_str (ctx_quote, notmuch_message_get_message_id (message)),
164             notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH) ? "true" : "false",
165             json_quote_str (ctx_quote, notmuch_message_get_filename (message)),
166             date, relative_date);
167
168     for (tags = notmuch_message_get_tags (message);
169          notmuch_tags_valid (tags);
170          notmuch_tags_move_to_next (tags))
171     {
172          printf("%s%s", first ? "" : ",",
173                json_quote_str (ctx_quote, notmuch_tags_get (tags)));
174          first = 0;
175     }
176     printf("]");
177     talloc_free (ctx_quote);
178 }
179
180 /* Extract just the email address from the contents of a From:
181  * header. */
182 static const char *
183 _extract_email_address (const void *ctx, const char *from)
184 {
185     InternetAddressList *addresses;
186     InternetAddress *address;
187     InternetAddressMailbox *mailbox;
188     const char *email = "MAILER-DAEMON";
189
190     addresses = internet_address_list_parse_string (from);
191
192     /* Bail if there is no address here. */
193     if (addresses == NULL || internet_address_list_length (addresses) < 1)
194         goto DONE;
195
196     /* Otherwise, just use the first address. */
197     address = internet_address_list_get_address (addresses, 0);
198
199     /* The From header should never contain an address group rather
200      * than a mailbox. So bail if it does. */
201     if (! INTERNET_ADDRESS_IS_MAILBOX (address))
202         goto DONE;
203
204     mailbox = INTERNET_ADDRESS_MAILBOX (address);
205     email = internet_address_mailbox_get_addr (mailbox);
206     email = talloc_strdup (ctx, email);
207
208   DONE:
209     /* XXX: How to free addresses here? */
210     return email;
211    }
212
213 /* Return 1 if 'line' is an mbox From_ line---that is, a line
214  * beginning with zero or more '>' characters followed by the
215  * characters 'F', 'r', 'o', 'm', and space.
216  *
217  * Any characters at all may appear after that in the line.
218  */
219 static int
220 _is_from_line (const char *line)
221 {
222     const char *s = line;
223
224     if (line == NULL)
225         return 0;
226
227     while (*s == '>')
228         s++;
229
230     if (STRNCMP_LITERAL (s, "From ") == 0)
231         return 1;
232     else
233         return 0;
234 }
235
236 /* Print a message in "mboxrd" format as documented, for example,
237  * here:
238  *
239  * http://qmail.org/qmail-manual-html/man5/mbox.html
240  */
241 static void
242 format_message_mbox (const void *ctx,
243                      notmuch_message_t *message,
244                      unused (int indent))
245 {
246     const char *filename;
247     FILE *file;
248     const char *from;
249
250     time_t date;
251     struct tm date_gmtime;
252     char date_asctime[26];
253
254     char *line = NULL;
255     size_t line_size;
256     ssize_t line_len;
257
258     filename = notmuch_message_get_filename (message);
259     file = fopen (filename, "r");
260     if (file == NULL) {
261         fprintf (stderr, "Failed to open %s: %s\n",
262                  filename, strerror (errno));
263         return;
264     }
265
266     from = notmuch_message_get_header (message, "from");
267     from = _extract_email_address (ctx, from);
268
269     date = notmuch_message_get_date (message);
270     gmtime_r (&date, &date_gmtime);
271     asctime_r (&date_gmtime, date_asctime);
272
273     printf ("From %s %s", from, date_asctime);
274
275     while ((line_len = getline (&line, &line_size, file)) != -1 ) {
276         if (_is_from_line (line))
277             putchar ('>');
278         printf ("%s", line);
279     }
280
281     printf ("\n");
282
283     fclose (file);
284 }
285
286 static void
287 format_headers_text (const void *ctx, notmuch_message_t *message)
288 {
289     const char *headers[] = {
290         "Subject", "From", "To", "Cc", "Bcc", "Date"
291     };
292     const char *name, *value;
293     unsigned int i;
294
295     printf ("%s\n", _get_one_line_summary (ctx, message));
296
297     for (i = 0; i < ARRAY_SIZE (headers); i++) {
298         name = headers[i];
299         value = notmuch_message_get_header (message, name);
300         if (value && strlen (value))
301             printf ("%s: %s\n", name, value);
302     }
303 }
304
305 static void
306 format_headers_json (const void *ctx, notmuch_message_t *message)
307 {
308     const char *headers[] = {
309         "Subject", "From", "To", "Cc", "Bcc", "Date"
310     };
311     const char *name, *value;
312     unsigned int i;
313     int first_header = 1;
314     void *ctx_quote = talloc_new (ctx);
315
316     for (i = 0; i < ARRAY_SIZE (headers); i++) {
317         name = headers[i];
318         value = notmuch_message_get_header (message, name);
319         if (value)
320         {
321             if (!first_header)
322                 fputs (", ", stdout);
323             first_header = 0;
324
325             printf ("%s: %s",
326                     json_quote_str (ctx_quote, name),
327                     json_quote_str (ctx_quote, value));
328         }
329     }
330
331     talloc_free (ctx_quote);
332 }
333
334 static void
335 show_part_content (GMimeObject *part, GMimeStream *stream_out)
336 {
337     GMimeStream *stream_filter = NULL;
338     GMimeDataWrapper *wrapper;
339     const char *charset;
340
341     charset = g_mime_object_get_content_type_parameter (part, "charset");
342
343     if (stream_out) {
344         stream_filter = g_mime_stream_filter_new (stream_out);
345         g_mime_stream_filter_add(GMIME_STREAM_FILTER (stream_filter),
346                                  g_mime_filter_crlf_new (FALSE, FALSE));
347         if (charset) {
348             GMimeFilter *charset_filter;
349             charset_filter = g_mime_filter_charset_new (charset, "UTF-8");
350             /* This result can be NULL for things like "unknown-8bit".
351              * Don't set a NULL filter as that makes GMime print
352              * annoying assertion-failure messages on stderr. */
353             if (charset_filter)
354                 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
355                                           charset_filter);
356         }
357     }
358
359     wrapper = g_mime_part_get_content_object (GMIME_PART (part));
360     if (wrapper && stream_filter)
361         g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
362     if (stream_filter)
363         g_object_unref(stream_filter);
364 }
365
366 static void
367 format_part_text (GMimeObject *part, int *part_count)
368 {
369     GMimeContentDisposition *disposition;
370     GMimeContentType *content_type;
371
372     disposition = g_mime_object_get_content_disposition (part);
373     if (disposition &&
374         strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
375     {
376         const char *filename = g_mime_part_get_filename (GMIME_PART (part));
377         content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
378
379         printf ("\fattachment{ ID: %d, Content-type: %s\n",
380                 *part_count,
381                 g_mime_content_type_to_string (content_type));
382         printf ("Attachment: %s (%s)\n", filename,
383                 g_mime_content_type_to_string (content_type));
384
385         if (g_mime_content_type_is_type (content_type, "text", "*") &&
386             !g_mime_content_type_is_type (content_type, "text", "html"))
387         {
388             GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
389             g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
390             show_part_content (part, stream_stdout);
391             g_object_unref(stream_stdout);
392         }
393
394         printf ("\fattachment}\n");
395
396         return;
397     }
398
399     content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
400
401     printf ("\fpart{ ID: %d, Content-type: %s\n",
402             *part_count,
403             g_mime_content_type_to_string (content_type));
404
405     if (g_mime_content_type_is_type (content_type, "text", "*") &&
406         !g_mime_content_type_is_type (content_type, "text", "html"))
407     {
408         GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
409         g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
410         show_part_content (part, stream_stdout);
411         g_object_unref(stream_stdout);
412     }
413     else
414     {
415         printf ("Non-text part: %s\n",
416                 g_mime_content_type_to_string (content_type));
417     }
418
419     printf ("\fpart}\n");
420 }
421
422 static void
423 format_part_json (GMimeObject *part, int *part_count)
424 {
425     GMimeContentType *content_type;
426     GMimeContentDisposition *disposition;
427     void *ctx = talloc_new (NULL);
428     GMimeStream *stream_memory = g_mime_stream_mem_new ();
429     GByteArray *part_content;
430
431     content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
432
433     if (*part_count > 1)
434         fputs (", ", stdout);
435
436     printf ("{\"id\": %d, \"content-type\": %s",
437             *part_count,
438             json_quote_str (ctx, g_mime_content_type_to_string (content_type)));
439
440     disposition = g_mime_object_get_content_disposition (part);
441     if (disposition &&
442         strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
443     {
444         const char *filename = g_mime_part_get_filename (GMIME_PART (part));
445
446         printf (", \"filename\": %s", json_quote_str (ctx, filename));
447     }
448
449     if (g_mime_content_type_is_type (content_type, "text", "*") &&
450         !g_mime_content_type_is_type (content_type, "text", "html"))
451     {
452         show_part_content (part, stream_memory);
453         part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));
454
455         printf (", \"content\": %s", json_quote_chararray (ctx, (char *) part_content->data, part_content->len));
456     }
457
458     fputs ("}", stdout);
459
460     talloc_free (ctx);
461     if (stream_memory)
462         g_object_unref (stream_memory);
463 }
464
465 static void
466 show_message (void *ctx, const show_format_t *format, notmuch_message_t *message, int indent)
467 {
468     fputs (format->message_start, stdout);
469     if (format->message)
470         format->message(ctx, message, indent);
471
472     fputs (format->header_start, stdout);
473     if (format->header)
474         format->header(ctx, message);
475     fputs (format->header_end, stdout);
476
477     fputs (format->body_start, stdout);
478     if (format->part)
479         show_message_body (notmuch_message_get_filename (message), format->part);
480     fputs (format->body_end, stdout);
481
482     fputs (format->message_end, stdout);
483 }
484
485
486 static void
487 show_messages (void *ctx, const show_format_t *format, notmuch_messages_t *messages, int indent,
488                notmuch_bool_t entire_thread)
489 {
490     notmuch_message_t *message;
491     notmuch_bool_t match;
492     int first_set = 1;
493     int next_indent;
494
495     fputs (format->message_set_start, stdout);
496
497     for (;
498          notmuch_messages_valid (messages);
499          notmuch_messages_move_to_next (messages))
500     {
501         if (!first_set)
502             fputs (format->message_set_sep, stdout);
503         first_set = 0;
504
505         fputs (format->message_set_start, stdout);
506
507         message = notmuch_messages_get (messages);
508
509         match = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH);
510
511         next_indent = indent;
512
513         if (match || entire_thread) {
514             show_message (ctx, format, message, indent);
515             next_indent = indent + 1;
516
517             fputs (format->message_set_sep, stdout);
518         }
519
520         show_messages (ctx, format, notmuch_message_get_replies (message),
521                        next_indent, entire_thread);
522
523         notmuch_message_destroy (message);
524
525         fputs (format->message_set_end, stdout);
526     }
527
528     fputs (format->message_set_end, stdout);
529 }
530
531 /* Support for --format=raw */
532 static int
533 do_show_raw (unused(void *ctx), notmuch_query_t *query)
534 {
535     notmuch_messages_t *messages;
536     notmuch_message_t *message;
537     const char *filename;
538     FILE *file;
539     size_t size;
540     char buf[4096];
541
542     if (notmuch_query_count_messages (query) != 1) {
543         fprintf (stderr, "Error: search term did not match precisely one message.\n");
544         return 1;
545     }
546
547     messages = notmuch_query_search_messages (query);
548     message = notmuch_messages_get (messages);
549
550     if (message == NULL) {
551         fprintf (stderr, "Error: Cannot find matching message.\n");
552         return 1;
553     }
554
555     filename = notmuch_message_get_filename (message);
556     if (filename == NULL) {
557         fprintf (stderr, "Error: Cannot message filename.\n");
558         return 1;
559     }
560
561     file = fopen (filename, "r");
562     if (file == NULL) {
563         fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, strerror (errno));
564         return 1;
565     }
566
567     while (!feof (file)) {
568         size = fread (buf, 1, sizeof (buf), file);
569         fwrite (buf, size, 1, stdout);
570     }
571
572     fclose (file);
573
574     return 0;
575 }
576
577 /* Support for --format=text|json|mbox */
578 static int
579 do_show (void *ctx,
580          notmuch_query_t *query,
581          const show_format_t *format,
582          int entire_thread)
583 {
584     notmuch_threads_t *threads;
585     notmuch_thread_t *thread;
586     notmuch_messages_t *messages;
587     int first_toplevel = 1;
588
589     fputs (format->message_set_start, stdout);
590
591     for (threads = notmuch_query_search_threads (query);
592          notmuch_threads_valid (threads);
593          notmuch_threads_move_to_next (threads))
594     {
595         thread = notmuch_threads_get (threads);
596
597         messages = notmuch_thread_get_toplevel_messages (thread);
598
599         if (messages == NULL)
600             INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
601                             notmuch_thread_get_thread_id (thread));
602
603         if (!first_toplevel)
604             fputs (format->message_set_sep, stdout);
605         first_toplevel = 0;
606
607         show_messages (ctx, format, messages, 0, entire_thread);
608
609         notmuch_thread_destroy (thread);
610
611     }
612
613     fputs (format->message_set_end, stdout);
614
615     return 0;
616 }
617
618 int
619 notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
620 {
621     notmuch_config_t *config;
622     notmuch_database_t *notmuch;
623     notmuch_query_t *query;
624     char *query_string;
625     char *opt;
626     const show_format_t *format = &format_text;
627     int entire_thread = 0;
628     int i;
629     int raw = 0;
630
631     for (i = 0; i < argc && argv[i][0] == '-'; i++) {
632         if (strcmp (argv[i], "--") == 0) {
633             i++;
634             break;
635         }
636         if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
637             opt = argv[i] + sizeof ("--format=") - 1;
638             if (strcmp (opt, "text") == 0) {
639                 format = &format_text;
640             } else if (strcmp (opt, "json") == 0) {
641                 format = &format_json;
642                 entire_thread = 1;
643             } else if (strcmp (opt, "mbox") == 0) {
644                 format = &format_mbox;
645             } else if (strcmp (opt, "raw") == 0) {
646                 raw = 1;
647             } else {
648                 fprintf (stderr, "Invalid value for --format: %s\n", opt);
649                 return 1;
650             }
651         } else if (STRNCMP_LITERAL (argv[i], "--entire-thread") == 0) {
652             entire_thread = 1;
653         } else {
654             fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
655             return 1;
656         }
657     }
658
659     argc -= i;
660     argv += i;
661
662     config = notmuch_config_open (ctx, NULL, NULL);
663     if (config == NULL)
664         return 1;
665
666     query_string = query_string_from_args (ctx, argc, argv);
667     if (query_string == NULL) {
668         fprintf (stderr, "Out of memory\n");
669         return 1;
670     }
671
672     if (*query_string == '\0') {
673         fprintf (stderr, "Error: notmuch show requires at least one search term.\n");
674         return 1;
675     }
676
677     notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
678                                      NOTMUCH_DATABASE_MODE_READ_ONLY);
679     if (notmuch == NULL)
680         return 1;
681
682     query = notmuch_query_create (notmuch, query_string);
683     if (query == NULL) {
684         fprintf (stderr, "Out of memory\n");
685         return 1;
686     }
687
688     if (raw)
689         return do_show_raw (ctx, query);
690     else
691         return do_show (ctx, query, format, entire_thread);
692
693     notmuch_query_destroy (query);
694     notmuch_database_close (notmuch);
695
696     return 0;
697 }
698
699 int
700 notmuch_part_command (void *ctx, unused (int argc), unused (char *argv[]))
701 {
702         notmuch_config_t *config;
703         notmuch_database_t *notmuch;
704         notmuch_query_t *query;
705         notmuch_messages_t *messages;
706         notmuch_message_t *message;
707         char *query_string;
708         int i;
709         int part = 0;
710
711         for (i = 0; i < argc && argv[i][0] == '-'; i++) {
712                 if (strcmp (argv[i], "--") == 0) {
713                         i++;
714                         break;
715                 }
716                 if (STRNCMP_LITERAL (argv[i], "--part=") == 0) {
717                         part = atoi(argv[i] + sizeof ("--part=") - 1);
718                 } else {
719                         fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
720                         return 1;
721                 }
722         }
723
724         argc -= i;
725         argv += i;
726
727         config = notmuch_config_open (ctx, NULL, NULL);
728         if (config == NULL)
729                 return 1;
730
731         query_string = query_string_from_args (ctx, argc, argv);
732         if (query_string == NULL) {
733                 fprintf (stderr, "Out of memory\n");
734                 return 1;
735         }
736
737         if (*query_string == '\0') {
738                 fprintf (stderr, "Error: notmuch part requires at least one search term.\n");
739                 return 1;
740         }
741
742         notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
743                                          NOTMUCH_DATABASE_MODE_READ_ONLY);
744         if (notmuch == NULL)
745                 return 1;
746
747         query = notmuch_query_create (notmuch, query_string);
748         if (query == NULL) {
749                 fprintf (stderr, "Out of memory\n");
750                 return 1;
751         }
752
753         if (notmuch_query_count_messages (query) != 1) {
754                 fprintf (stderr, "Error: search term did not match precisely one message.\n");
755                 return 1;
756         }
757
758         messages = notmuch_query_search_messages (query);
759         message = notmuch_messages_get (messages);
760
761         if (message == NULL) {
762                 fprintf (stderr, "Error: cannot find matching message.\n");
763                 return 1;
764         }
765
766         show_one_part (notmuch_message_get_filename (message), part);
767
768         notmuch_query_destroy (query);
769         notmuch_database_close (notmuch);
770
771         return 0;
772 }