2009-10-19 21:54:40 +02:00
|
|
|
/* message.c - Utility functions for parsing an email message for notmuch.
|
|
|
|
*
|
|
|
|
* Copyright © 2009 Carl Worth
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see http://www.gnu.org/licenses/ .
|
|
|
|
*
|
|
|
|
* Author: Carl Worth <cworth@cworth.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
#include "notmuch-private.h"
|
|
|
|
|
2009-11-02 23:32:20 +01:00
|
|
|
#include <gmime/gmime.h>
|
|
|
|
|
2009-10-19 22:40:56 +02:00
|
|
|
#include <glib.h> /* GHashTable */
|
2009-10-19 21:54:40 +02:00
|
|
|
|
2009-10-21 00:09:51 +02:00
|
|
|
struct _notmuch_message_file {
|
2009-10-20 01:38:44 +02:00
|
|
|
/* File object */
|
|
|
|
FILE *file;
|
2014-03-30 23:21:49 +02:00
|
|
|
char *filename;
|
2009-10-19 21:54:40 +02:00
|
|
|
|
2014-03-30 23:21:49 +02:00
|
|
|
/* Cache for decoded headers */
|
2009-10-19 21:54:40 +02:00
|
|
|
GHashTable *headers;
|
2009-10-20 01:38:44 +02:00
|
|
|
|
2014-03-30 23:21:49 +02:00
|
|
|
GMimeMessage *message;
|
2009-10-19 21:54:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
strcase_equal (const void *a, const void *b)
|
|
|
|
{
|
|
|
|
return strcasecmp (a, b) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int
|
|
|
|
strcase_hash (const void *ptr)
|
|
|
|
{
|
|
|
|
const char *s = ptr;
|
|
|
|
|
|
|
|
/* This is the djb2 hash. */
|
|
|
|
unsigned int hash = 5381;
|
|
|
|
while (s && *s) {
|
|
|
|
hash = ((hash << 5) + hash) + tolower (*s);
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2009-10-27 01:35:31 +01:00
|
|
|
static int
|
|
|
|
_notmuch_message_file_destructor (notmuch_message_file_t *message)
|
|
|
|
{
|
|
|
|
if (message->headers)
|
|
|
|
g_hash_table_destroy (message->headers);
|
|
|
|
|
2014-03-30 23:21:49 +02:00
|
|
|
if (message->message)
|
|
|
|
g_object_unref (message->message);
|
|
|
|
|
2009-10-27 01:35:31 +01:00
|
|
|
if (message->file)
|
|
|
|
fclose (message->file);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a new notmuch_message_file_t for 'filename' with 'ctx' as
|
|
|
|
* the talloc owner. */
|
2009-10-21 00:09:51 +02:00
|
|
|
notmuch_message_file_t *
|
2014-12-26 18:34:49 +01:00
|
|
|
_notmuch_message_file_open_ctx (notmuch_database_t *notmuch,
|
|
|
|
void *ctx, const char *filename)
|
2009-10-19 21:54:40 +02:00
|
|
|
{
|
2009-10-21 00:09:51 +02:00
|
|
|
notmuch_message_file_t *message;
|
2009-10-19 21:54:40 +02:00
|
|
|
|
2009-10-27 01:35:31 +01:00
|
|
|
message = talloc_zero (ctx, notmuch_message_file_t);
|
|
|
|
if (unlikely (message == NULL))
|
|
|
|
return NULL;
|
|
|
|
|
2014-03-30 23:21:49 +02:00
|
|
|
/* Only needed for error messages during parsing. */
|
|
|
|
message->filename = talloc_strdup (message, filename);
|
|
|
|
if (message->filename == NULL)
|
|
|
|
goto FAIL;
|
|
|
|
|
2009-10-27 01:35:31 +01:00
|
|
|
talloc_set_destructor (message, _notmuch_message_file_destructor);
|
2009-10-19 21:54:40 +02:00
|
|
|
|
2009-10-20 01:38:44 +02:00
|
|
|
message->file = fopen (filename, "r");
|
|
|
|
if (message->file == NULL)
|
2009-10-19 21:54:40 +02:00
|
|
|
goto FAIL;
|
|
|
|
|
|
|
|
return message;
|
|
|
|
|
|
|
|
FAIL:
|
2014-12-26 18:34:49 +01:00
|
|
|
_notmuch_database_log (notmuch, "Error opening %s: %s\n",
|
|
|
|
filename, strerror (errno));
|
2014-05-13 11:44:05 +02:00
|
|
|
_notmuch_message_file_close (message);
|
2009-10-19 21:54:40 +02:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-10-27 01:35:31 +01:00
|
|
|
notmuch_message_file_t *
|
2014-12-26 18:34:49 +01:00
|
|
|
_notmuch_message_file_open (notmuch_database_t *notmuch,
|
|
|
|
const char *filename)
|
2009-10-27 01:35:31 +01:00
|
|
|
{
|
2014-12-26 18:34:49 +01:00
|
|
|
return _notmuch_message_file_open_ctx (notmuch, NULL, filename);
|
2009-10-27 01:35:31 +01:00
|
|
|
}
|
|
|
|
|
2009-10-19 21:54:40 +02:00
|
|
|
void
|
2014-05-13 11:44:05 +02:00
|
|
|
_notmuch_message_file_close (notmuch_message_file_t *message)
|
2009-10-19 21:54:40 +02:00
|
|
|
{
|
2009-10-26 23:17:10 +01:00
|
|
|
talloc_free (message);
|
2009-10-19 21:54:40 +02:00
|
|
|
}
|
|
|
|
|
2014-03-30 23:21:49 +02:00
|
|
|
static notmuch_bool_t
|
2014-06-05 08:34:09 +02:00
|
|
|
_is_mbox (FILE *file)
|
2009-10-19 21:54:40 +02:00
|
|
|
{
|
2014-03-30 23:21:49 +02:00
|
|
|
char from_buf[5];
|
|
|
|
notmuch_bool_t ret = FALSE;
|
2009-10-19 21:54:40 +02:00
|
|
|
|
2014-03-30 23:21:49 +02:00
|
|
|
/* Is this mbox? */
|
|
|
|
if (fread (from_buf, sizeof (from_buf), 1, file) == 1 &&
|
|
|
|
strncmp (from_buf, "From ", 5) == 0)
|
|
|
|
ret = TRUE;
|
2009-10-19 21:54:40 +02:00
|
|
|
|
2014-03-30 23:21:49 +02:00
|
|
|
rewind (file);
|
2009-10-19 21:54:40 +02:00
|
|
|
|
2014-03-30 23:21:49 +02:00
|
|
|
return ret;
|
2009-10-19 21:54:40 +02:00
|
|
|
}
|
|
|
|
|
2014-03-30 23:21:49 +02:00
|
|
|
notmuch_status_t
|
|
|
|
_notmuch_message_file_parse (notmuch_message_file_t *message)
|
2009-10-19 21:54:40 +02:00
|
|
|
{
|
2014-03-30 23:21:49 +02:00
|
|
|
GMimeStream *stream;
|
|
|
|
GMimeParser *parser;
|
|
|
|
notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
|
|
|
|
static int initialized = 0;
|
2014-06-05 08:34:09 +02:00
|
|
|
notmuch_bool_t is_mbox;
|
2009-10-19 21:54:40 +02:00
|
|
|
|
2014-03-30 23:21:49 +02:00
|
|
|
if (message->message)
|
|
|
|
return NOTMUCH_STATUS_SUCCESS;
|
2009-10-19 21:54:40 +02:00
|
|
|
|
2014-06-05 08:34:09 +02:00
|
|
|
is_mbox = _is_mbox (message->file);
|
2009-10-19 21:54:40 +02:00
|
|
|
|
2014-03-30 23:21:49 +02:00
|
|
|
if (! initialized) {
|
|
|
|
g_mime_init (GMIME_ENABLE_RFC2047_WORKAROUNDS);
|
|
|
|
initialized = 1;
|
|
|
|
}
|
2009-10-20 01:38:44 +02:00
|
|
|
|
2014-03-30 23:21:49 +02:00
|
|
|
message->headers = g_hash_table_new_full (strcase_hash, strcase_equal,
|
|
|
|
free, g_free);
|
|
|
|
if (! message->headers)
|
|
|
|
return NOTMUCH_STATUS_OUT_OF_MEMORY;
|
2009-10-20 01:38:44 +02:00
|
|
|
|
2014-03-30 23:21:49 +02:00
|
|
|
stream = g_mime_stream_file_new (message->file);
|
2009-10-20 01:38:44 +02:00
|
|
|
|
2014-03-30 23:21:49 +02:00
|
|
|
/* We'll own and fclose the FILE* ourselves. */
|
|
|
|
g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream), FALSE);
|
|
|
|
|
|
|
|
parser = g_mime_parser_new_with_stream (stream);
|
2014-06-05 08:34:09 +02:00
|
|
|
g_mime_parser_set_scan_from (parser, is_mbox);
|
2009-10-19 21:54:40 +02:00
|
|
|
|
2014-03-30 23:21:49 +02:00
|
|
|
message->message = g_mime_parser_construct_message (parser);
|
|
|
|
if (! message->message) {
|
|
|
|
status = NOTMUCH_STATUS_FILE_NOT_EMAIL;
|
|
|
|
goto DONE;
|
2009-10-19 21:54:40 +02:00
|
|
|
}
|
|
|
|
|
2014-11-23 12:15:12 +01:00
|
|
|
if (is_mbox && ! g_mime_parser_eos (parser)) {
|
2014-06-05 08:34:09 +02:00
|
|
|
/*
|
2014-11-23 12:15:12 +01:00
|
|
|
* This is a multi-message mbox. (For historical reasons, we
|
|
|
|
* do support single-message mboxes.)
|
2014-06-05 08:34:09 +02:00
|
|
|
*/
|
2014-11-23 12:15:12 +01:00
|
|
|
status = NOTMUCH_STATUS_FILE_NOT_EMAIL;
|
2014-06-05 08:34:09 +02:00
|
|
|
}
|
|
|
|
|
2014-03-30 23:21:49 +02:00
|
|
|
DONE:
|
|
|
|
g_object_unref (stream);
|
|
|
|
g_object_unref (parser);
|
|
|
|
|
|
|
|
if (status) {
|
|
|
|
g_hash_table_destroy (message->headers);
|
|
|
|
message->headers = NULL;
|
|
|
|
|
|
|
|
if (message->message) {
|
|
|
|
g_object_unref (message->message);
|
|
|
|
message->message = NULL;
|
|
|
|
}
|
2009-10-19 21:54:40 +02:00
|
|
|
|
2014-03-30 23:21:49 +02:00
|
|
|
rewind (message->file);
|
2009-10-20 01:38:44 +02:00
|
|
|
}
|
2014-03-30 23:21:49 +02:00
|
|
|
|
|
|
|
return status;
|
2009-10-19 21:54:40 +02:00
|
|
|
}
|
|
|
|
|
2014-03-30 23:21:49 +02:00
|
|
|
notmuch_status_t
|
|
|
|
_notmuch_message_file_get_mime_message (notmuch_message_file_t *message,
|
|
|
|
GMimeMessage **mime_message)
|
2009-10-19 21:54:40 +02:00
|
|
|
{
|
2014-03-30 23:21:49 +02:00
|
|
|
notmuch_status_t status;
|
2009-11-02 23:32:20 +01:00
|
|
|
|
2014-03-30 23:21:49 +02:00
|
|
|
status = _notmuch_message_file_parse (message);
|
|
|
|
if (status)
|
|
|
|
return status;
|
2010-04-26 21:58:34 +02:00
|
|
|
|
2014-03-30 23:21:49 +02:00
|
|
|
*mime_message = message->message;
|
2009-10-19 21:54:40 +02:00
|
|
|
|
2014-03-30 23:21:49 +02:00
|
|
|
return NOTMUCH_STATUS_SUCCESS;
|
|
|
|
}
|
2009-10-20 01:38:44 +02:00
|
|
|
|
2014-03-30 23:21:49 +02:00
|
|
|
/*
|
|
|
|
* Get all instances of a header decoded and concatenated.
|
|
|
|
*
|
|
|
|
* The result must be freed using g_free().
|
|
|
|
*
|
|
|
|
* Return NULL on errors, empty string for non-existing headers.
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
_notmuch_message_file_get_combined_header (notmuch_message_file_t *message,
|
|
|
|
const char *header)
|
|
|
|
{
|
|
|
|
GMimeHeaderList *headers;
|
|
|
|
GMimeHeaderIter *iter;
|
|
|
|
char *combined = NULL;
|
2009-10-19 21:54:40 +02:00
|
|
|
|
2014-03-30 23:21:49 +02:00
|
|
|
headers = g_mime_object_get_header_list (GMIME_OBJECT (message->message));
|
|
|
|
if (! headers)
|
|
|
|
return NULL;
|
2009-10-19 21:54:40 +02:00
|
|
|
|
2014-03-30 23:21:49 +02:00
|
|
|
iter = g_mime_header_iter_new ();
|
|
|
|
if (! iter)
|
|
|
|
return NULL;
|
2009-10-19 21:54:40 +02:00
|
|
|
|
2014-03-30 23:21:49 +02:00
|
|
|
if (! g_mime_header_list_get_iter (headers, iter))
|
|
|
|
goto DONE;
|
2009-10-19 21:54:40 +02:00
|
|
|
|
2014-03-30 23:21:49 +02:00
|
|
|
do {
|
|
|
|
const char *value;
|
|
|
|
char *decoded;
|
|
|
|
|
|
|
|
if (strcasecmp (g_mime_header_iter_get_name (iter), header) != 0)
|
2009-10-19 21:54:40 +02:00
|
|
|
continue;
|
|
|
|
|
2014-03-30 23:21:49 +02:00
|
|
|
/* Note that GMime retains ownership of value... */
|
|
|
|
value = g_mime_header_iter_get_value (iter);
|
2009-10-29 16:51:12 +01:00
|
|
|
|
2014-03-30 23:21:49 +02:00
|
|
|
/* ... while decoded needs to be freed with g_free(). */
|
|
|
|
decoded = g_mime_utils_header_decode_text (value);
|
|
|
|
if (! decoded) {
|
|
|
|
if (combined) {
|
|
|
|
g_free (combined);
|
|
|
|
combined = NULL;
|
|
|
|
}
|
|
|
|
goto DONE;
|
|
|
|
}
|
2009-10-20 08:08:49 +02:00
|
|
|
|
2014-03-30 23:21:49 +02:00
|
|
|
if (combined) {
|
|
|
|
char *tmp = g_strdup_printf ("%s %s", combined, decoded);
|
|
|
|
g_free (decoded);
|
|
|
|
g_free (combined);
|
|
|
|
if (! tmp) {
|
|
|
|
combined = NULL;
|
|
|
|
goto DONE;
|
|
|
|
}
|
2009-10-19 21:54:40 +02:00
|
|
|
|
2014-03-30 23:21:49 +02:00
|
|
|
combined = tmp;
|
|
|
|
} else {
|
|
|
|
combined = decoded;
|
2009-10-19 21:54:40 +02:00
|
|
|
}
|
2014-03-30 23:21:49 +02:00
|
|
|
} while (g_mime_header_iter_next (iter));
|
2009-10-19 21:54:40 +02:00
|
|
|
|
2014-03-30 23:21:49 +02:00
|
|
|
/* Return empty string for non-existing headers. */
|
|
|
|
if (! combined)
|
|
|
|
combined = g_strdup ("");
|
2009-10-19 21:54:40 +02:00
|
|
|
|
2014-03-30 23:21:49 +02:00
|
|
|
DONE:
|
|
|
|
g_mime_header_iter_free (iter);
|
2009-10-19 21:54:40 +02:00
|
|
|
|
2014-03-30 23:21:49 +02:00
|
|
|
return combined;
|
|
|
|
}
|
2009-10-20 01:38:44 +02:00
|
|
|
|
2014-03-30 23:21:49 +02:00
|
|
|
const char *
|
2014-05-13 11:44:05 +02:00
|
|
|
_notmuch_message_file_get_header (notmuch_message_file_t *message,
|
2014-03-30 23:21:49 +02:00
|
|
|
const char *header)
|
|
|
|
{
|
|
|
|
const char *value;
|
|
|
|
char *decoded;
|
2009-11-17 16:28:37 +01:00
|
|
|
|
2014-03-30 23:21:49 +02:00
|
|
|
if (_notmuch_message_file_parse (message))
|
|
|
|
return NULL;
|
2009-10-20 01:38:44 +02:00
|
|
|
|
2014-03-30 23:21:49 +02:00
|
|
|
/* If we have a cached decoded value, use it. */
|
|
|
|
value = g_hash_table_lookup (message->headers, header);
|
|
|
|
if (value)
|
|
|
|
return value;
|
|
|
|
|
|
|
|
if (strcasecmp (header, "received") == 0) {
|
|
|
|
/*
|
|
|
|
* The Received: header is special. We concatenate all
|
|
|
|
* instances of the header as we use this when analyzing the
|
|
|
|
* path the mail has taken from sender to recipient.
|
|
|
|
*/
|
|
|
|
decoded = _notmuch_message_file_get_combined_header (message, header);
|
|
|
|
} else {
|
|
|
|
value = g_mime_object_get_header (GMIME_OBJECT (message->message),
|
|
|
|
header);
|
|
|
|
if (value)
|
|
|
|
decoded = g_mime_utils_header_decode_text (value);
|
|
|
|
else
|
|
|
|
decoded = g_strdup ("");
|
2009-10-20 01:38:44 +02:00
|
|
|
}
|
|
|
|
|
2014-03-30 23:21:49 +02:00
|
|
|
if (! decoded)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Cache the decoded value. We also own the strings. */
|
|
|
|
g_hash_table_insert (message->headers, xstrdup (header), decoded);
|
2009-10-23 00:46:22 +02:00
|
|
|
|
2014-03-30 23:21:49 +02:00
|
|
|
return decoded;
|
2009-10-19 21:54:40 +02:00
|
|
|
}
|