]> git.notmuchmail.org Git - notmuch/blob - show-message.c
emacs: Add an accessor function for emacs code to get at user.other_email
[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 typedef struct show_message_state {
26     int part_count;
27     int in_zone;
28 } show_message_state_t;
29
30 static void
31 show_message_part (GMimeObject *part,
32                    show_message_state_t *state,
33                    const notmuch_show_format_t *format,
34                    notmuch_show_params_t *params,
35                    int first)
36 {
37     int selected;
38     state->part_count += 1;
39
40     if (! (GMIME_IS_PART (part) || GMIME_IS_MULTIPART (part) || GMIME_IS_MESSAGE_PART (part))) {
41         fprintf (stderr, "Warning: Not displaying unknown mime part: %s.\n",
42                  g_type_name (G_OBJECT_TYPE (part)));
43         return;
44     }
45
46     selected = (params->part <= 0 || state->part_count == params->part);
47
48     if (selected || state->in_zone) {
49         if (!first && (params->part <= 0 || state->in_zone) && format->part_sep)
50             fputs (format->part_sep, stdout);
51
52         format->part (part, &(state->part_count));
53     }
54
55     if (GMIME_IS_MULTIPART (part)) {
56         GMimeMultipart *multipart = GMIME_MULTIPART (part);
57         int i;
58
59         if (selected)
60             state->in_zone = 1;
61
62         for (i = 0; i < g_mime_multipart_get_count (multipart); i++) {
63             show_message_part (g_mime_multipart_get_part (multipart, i),
64                                state, format, params, i == 0);
65         }
66
67         if (selected)
68             state->in_zone = 0;
69
70     } else if (GMIME_IS_MESSAGE_PART (part)) {
71         GMimeMessage *mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
72
73         if (selected)
74             state->in_zone = 1;
75
76         show_message_part (g_mime_message_get_mime_part (mime_message),
77                            state, format, params, TRUE);
78
79         if (selected)
80             state->in_zone = 0;
81     }
82
83     if (selected || state->in_zone) {
84         if (format->part_end)
85             format->part_end (part);
86     }
87 }
88
89 notmuch_status_t
90 show_message_body (const char *filename,
91                    const notmuch_show_format_t *format,
92                    notmuch_show_params_t *params)
93 {
94     GMimeStream *stream = NULL;
95     GMimeParser *parser = NULL;
96     GMimeMessage *mime_message = NULL;
97     notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
98     FILE *file = NULL;
99     show_message_state_t state;
100
101     state.part_count = 0;
102     state.in_zone = 0;
103
104     file = fopen (filename, "r");
105     if (! file) {
106         fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
107         ret = NOTMUCH_STATUS_FILE_ERROR;
108         goto DONE;
109     }
110
111     stream = g_mime_stream_file_new (file);
112     g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream), FALSE);
113
114     parser = g_mime_parser_new_with_stream (stream);
115
116     mime_message = g_mime_parser_construct_message (parser);
117
118     show_message_part (g_mime_message_get_mime_part (mime_message),
119                        &state,
120                        format,
121                        params,
122                        TRUE);
123
124   DONE:
125     if (mime_message)
126         g_object_unref (mime_message);
127
128     if (parser)
129         g_object_unref (parser);
130
131     if (stream)
132         g_object_unref (stream);
133
134     if (file)
135         fclose (file);
136
137     return ret;
138 }