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