1 /* message.c - Utility functions for parsing an email message for notmuch.
3 * Copyright © 2009 Carl Worth
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.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see https://www.gnu.org/licenses/ .
18 * Author: Carl Worth <cworth@cworth.org>
23 #include "notmuch-private.h"
25 #include <gmime/gmime.h>
27 #include <glib.h> /* GHashTable */
29 struct _notmuch_message_file {
30 /* open stream to (possibly gzipped) file */
34 /* Cache for decoded headers */
37 GMimeMessage *message;
41 _notmuch_message_file_destructor (notmuch_message_file_t *message)
44 g_hash_table_destroy (message->headers);
47 g_object_unref (message->message);
50 g_object_unref (message->stream);
55 /* Create a new notmuch_message_file_t for 'filename' with 'ctx' as
56 * the talloc owner. */
57 notmuch_message_file_t *
58 _notmuch_message_file_open_ctx (notmuch_database_t *notmuch,
59 void *ctx, const char *filename)
61 notmuch_message_file_t *message;
63 message = talloc_zero (ctx, notmuch_message_file_t);
64 if (unlikely (message == NULL))
67 const char *prefix = notmuch_config_get (notmuch, NOTMUCH_CONFIG_MAIL_ROOT);
72 if (*filename == '/') {
73 if (strncmp (filename, prefix, strlen (prefix)) != 0) {
74 _notmuch_database_log (notmuch, "Error opening %s: path outside mail root\n",
79 message->filename = talloc_strdup (message, filename);
81 message->filename = talloc_asprintf (message, "%s/%s", prefix, filename);
84 if (message->filename == NULL)
87 talloc_set_destructor (message, _notmuch_message_file_destructor);
89 message->stream = g_mime_stream_gzfile_open (message->filename);
90 if (message->stream == NULL)
97 _notmuch_database_log (notmuch, "Error opening %s: %s\n",
98 filename, strerror (errno));
99 _notmuch_message_file_close (message);
104 notmuch_message_file_t *
105 _notmuch_message_file_open (notmuch_database_t *notmuch,
106 const char *filename)
108 return _notmuch_message_file_open_ctx (notmuch, NULL, filename);
112 _notmuch_message_file_get_filename (notmuch_message_file_t *message_file)
114 return message_file->filename;
118 _notmuch_message_file_close (notmuch_message_file_t *message)
120 talloc_free (message);
124 _is_mbox (GMimeStream *stream)
130 if (g_mime_stream_read (stream, from_buf, sizeof (from_buf)) == sizeof (from_buf) &&
131 strncmp (from_buf, "From ", 5) == 0)
134 g_mime_stream_reset (stream);
140 _notmuch_message_file_parse (notmuch_message_file_t *message)
143 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
144 static int initialized = 0;
147 if (message->message)
148 return NOTMUCH_STATUS_SUCCESS;
150 is_mbox = _is_mbox (message->stream);
157 message->headers = g_hash_table_new_full (strcase_hash, strcase_equal,
159 if (! message->headers)
160 return NOTMUCH_STATUS_OUT_OF_MEMORY;
162 parser = g_mime_parser_new_with_stream (message->stream);
163 g_mime_parser_set_scan_from (parser, is_mbox);
165 message->message = g_mime_parser_construct_message (parser, NULL);
166 if (! message->message) {
167 status = NOTMUCH_STATUS_FILE_NOT_EMAIL;
171 if (is_mbox && ! g_mime_parser_eos (parser)) {
173 * This is a multi-message mbox. (For historical reasons, we
174 * do support single-message mboxes.)
176 status = NOTMUCH_STATUS_FILE_NOT_EMAIL;
180 g_mime_stream_reset (message->stream);
181 g_object_unref (parser);
184 g_hash_table_destroy (message->headers);
185 message->headers = NULL;
187 if (message->message) {
188 g_object_unref (message->message);
189 message->message = NULL;
198 _notmuch_message_file_get_mime_message (notmuch_message_file_t *message,
199 GMimeMessage **mime_message)
201 notmuch_status_t status;
203 status = _notmuch_message_file_parse (message);
207 *mime_message = message->message;
209 return NOTMUCH_STATUS_SUCCESS;
213 * Get all instances of a header decoded and concatenated.
215 * The result must be freed using g_free().
217 * Return NULL on errors, empty string for non-existing headers.
221 _extend_header (char *combined, const char *value)
225 decoded = g_mime_utils_header_decode_text (NULL, value);
235 char *tmp = g_strdup_printf ("%s %s", combined, decoded);
252 _notmuch_message_file_get_combined_header (notmuch_message_file_t *message,
255 char *combined = NULL;
256 GMimeHeaderList *headers;
258 headers = g_mime_object_get_header_list (GMIME_OBJECT (message->message));
263 for (int i = 0; i < g_mime_header_list_get_count (headers); i++) {
265 GMimeHeader *g_header = g_mime_header_list_get_header_at (headers, i);
267 if (strcasecmp (g_mime_header_get_name (g_header), header) != 0)
270 /* GMime retains ownership of value, we hope */
271 value = g_mime_header_get_value (g_header);
273 combined = _extend_header (combined, value);
276 /* Return empty string for non-existing headers. */
278 combined = g_strdup ("");
284 _notmuch_message_file_get_header (notmuch_message_file_t *message,
290 if (_notmuch_message_file_parse (message))
293 /* If we have a cached decoded value, use it. */
294 value = g_hash_table_lookup (message->headers, header);
298 if (strcasecmp (header, "received") == 0) {
300 * The Received: header is special. We concatenate all
301 * instances of the header as we use this when analyzing the
302 * path the mail has taken from sender to recipient.
304 decoded = _notmuch_message_file_get_combined_header (message, header);
306 value = g_mime_object_get_header (GMIME_OBJECT (message->message),
309 decoded = g_mime_utils_header_decode_text (NULL, value);
311 decoded = g_strdup ("");
317 /* Cache the decoded value. We also own the strings. */
318 g_hash_table_insert (message->headers, xstrdup (header), decoded);
324 _notmuch_message_file_get_headers (notmuch_message_file_t *message_file,
325 const char **from_out,
326 const char **subject_out,
328 const char **date_out,
329 char **message_id_out)
331 notmuch_status_t ret;
333 const char *from, *to, *subject, *date;
334 char *message_id = NULL;
336 /* Parse message up front to get better error status. */
337 ret = _notmuch_message_file_parse (message_file);
341 /* Before we do any real work, (especially before doing a
342 * potential SHA-1 computation on the entire file's contents),
343 * let's make sure that what we're looking at looks like an
344 * actual email message.
346 from = _notmuch_message_file_get_header (message_file, "from");
347 subject = _notmuch_message_file_get_header (message_file, "subject");
348 to = _notmuch_message_file_get_header (message_file, "to");
349 date = _notmuch_message_file_get_header (message_file, "date");
351 if ((from == NULL || *from == '\0') &&
352 (subject == NULL || *subject == '\0') &&
353 (to == NULL || *to == '\0')) {
354 ret = NOTMUCH_STATUS_FILE_NOT_EMAIL;
358 /* Now that we're sure it's mail, the first order of business
359 * is to find a message ID (or else create one ourselves).
361 header = _notmuch_message_file_get_header (message_file, "message-id");
362 if (header && *header != '\0') {
363 message_id = _notmuch_message_id_parse (message_file, header, NULL);
365 /* So the header value isn't RFC-compliant, but it's
366 * better than no message-id at all.
368 if (message_id == NULL)
369 message_id = talloc_strdup (message_file, header);
372 if (message_id == NULL ) {
373 /* No message-id at all, let's generate one by taking a
374 * hash over the file's contents.
376 char *sha1 = _notmuch_sha1_of_file (_notmuch_message_file_get_filename (message_file));
378 /* If that failed too, something is really wrong. Give up. */
380 ret = NOTMUCH_STATUS_FILE_ERROR;
384 message_id = talloc_asprintf (message_file, "notmuch-sha1-%s", sha1);
388 if (ret == NOTMUCH_STATUS_SUCCESS) {
392 *subject_out = subject;
398 *message_id_out = message_id;