]> git.notmuchmail.org Git - notmuch/blob - show-message.c
Makefile: Fix Makefiles to depend on all child Makefile fragments.
[notmuch] / show-message.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
25 static void
26 show_message_part (GMimeObject *part, int *part_count,
27                    void (*show_part) (GMimeObject *part, int *part_count))
28 {
29     if (GMIME_IS_MULTIPART (part)) {
30         GMimeMultipart *multipart = GMIME_MULTIPART (part);
31         int i;
32
33         for (i = 0; i < g_mime_multipart_get_count (multipart); i++) {
34             show_message_part (g_mime_multipart_get_part (multipart, i),
35                                part_count, show_part);
36         }
37         return;
38     }
39
40     if (GMIME_IS_MESSAGE_PART (part)) {
41         GMimeMessage *mime_message;
42
43         mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
44
45         show_message_part (g_mime_message_get_mime_part (mime_message),
46                            part_count, show_part);
47
48         return;
49     }
50
51     if (! (GMIME_IS_PART (part))) {
52         fprintf (stderr, "Warning: Not displaying unknown mime part: %s.\n",
53                  g_type_name (G_OBJECT_TYPE (part)));
54         return;
55     }
56
57     *part_count = *part_count + 1;
58
59     (*show_part) (part, part_count);
60 }
61
62 notmuch_status_t
63 show_message_body (const char *filename,
64                    void (*show_part) (GMimeObject *part, int *part_count))
65 {
66     GMimeStream *stream = NULL;
67     GMimeParser *parser = NULL;
68     GMimeMessage *mime_message = NULL;
69     notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
70     FILE *file = NULL;
71     int part_count = 0;
72
73     file = fopen (filename, "r");
74     if (! file) {
75         fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
76         ret = NOTMUCH_STATUS_FILE_ERROR;
77         goto DONE;
78     }
79
80     stream = g_mime_stream_file_new (file);
81     g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream), FALSE);
82
83     parser = g_mime_parser_new_with_stream (stream);
84
85     mime_message = g_mime_parser_construct_message (parser);
86
87     show_message_part (g_mime_message_get_mime_part (mime_message),
88                        &part_count, show_part);
89
90   DONE:
91     if (mime_message)
92         g_object_unref (mime_message);
93
94     if (parser)
95         g_object_unref (parser);
96
97     if (stream)
98         g_object_unref (stream);
99
100     if (file)
101         fclose (file);
102
103     return ret;
104 }