unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
blob 647ccf3abedaaf52f95bcc44e9a3199de8d4b42d 10018 bytes (raw)
name: lib/message-file.c 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
 
/* 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 https://www.gnu.org/licenses/ .
 *
 * Author: Carl Worth <cworth@cworth.org>
 */

#include <stdarg.h>

#include "notmuch-private.h"

#include <gmime/gmime.h>

#include <glib.h> /* GHashTable */

struct _notmuch_message_file {
    /* open stream to (possibly gzipped) file */
    GMimeStream *stream;
    char *filename;

    /* Cache for decoded headers */
    GHashTable *headers;

    GMimeMessage *message;
};

static int
_notmuch_message_file_destructor (notmuch_message_file_t *message)
{
    if (message->headers)
	g_hash_table_destroy (message->headers);

    if (message->message)
	g_object_unref (message->message);

    if (message->stream)
	g_object_unref (message->stream);

    return 0;
}

/* Create a new notmuch_message_file_t for 'filename' with 'ctx' as
 * the talloc owner. */
notmuch_message_file_t *
_notmuch_message_file_open_ctx (notmuch_database_t *notmuch,
				void *ctx, const char *filename)
{
    notmuch_message_file_t *message;

    message = talloc_zero (ctx, notmuch_message_file_t);
    if (unlikely (message == NULL))
	return NULL;

    const char *prefix = notmuch_config_get (notmuch, NOTMUCH_CONFIG_MAIL_ROOT);

    if (prefix == NULL)
	goto FAIL;

    if (*filename == '/') {
	if (strncmp (filename, prefix, strlen (prefix)) != 0) {
	    _notmuch_database_log (notmuch, "Error opening %s: path outside mail root\n",
				   filename);
	    errno = 0;
	    goto FAIL;
	}
	message->filename = talloc_strdup (message, filename);
    } else {
	message->filename = talloc_asprintf (message, "%s/%s", prefix, filename);
    }

    if (message->filename == NULL)
	goto FAIL;

    talloc_set_destructor (message, _notmuch_message_file_destructor);

    message->stream = g_mime_stream_gzfile_open (message->filename);
    if (message->stream == NULL)
	goto FAIL;

    return message;

  FAIL:
    if (errno)
	_notmuch_database_log (notmuch, "Error opening %s: %s\n",
			       filename, strerror (errno));
    _notmuch_message_file_close (message);

    return NULL;
}

notmuch_message_file_t *
_notmuch_message_file_open (notmuch_database_t *notmuch,
			    const char *filename)
{
    return _notmuch_message_file_open_ctx (notmuch, NULL, filename);
}

const char *
_notmuch_message_file_get_filename (notmuch_message_file_t *message_file)
{
    return message_file->filename;
}

void
_notmuch_message_file_close (notmuch_message_file_t *message)
{
    talloc_free (message);
}

static bool
_is_mbox (GMimeStream *stream)
{
    char from_buf[5];
    bool ret = false;

    /* Is this mbox? */
    if (g_mime_stream_read (stream, from_buf, sizeof (from_buf)) == sizeof (from_buf) &&
	strncmp (from_buf, "From ", 5) == 0)
	ret = true;

    g_mime_stream_reset (stream);

    return ret;
}

notmuch_status_t
_notmuch_message_file_parse (notmuch_message_file_t *message)
{
    GMimeParser *parser;
    notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
    bool is_mbox;

    if (message->message)
	return NOTMUCH_STATUS_SUCCESS;

    is_mbox = _is_mbox (message->stream);

    _notmuch_init ();

    message->headers = g_hash_table_new_full (strcase_hash, strcase_equal,
					      free, g_free);
    if (! message->headers)
	return NOTMUCH_STATUS_OUT_OF_MEMORY;

    parser = g_mime_parser_new_with_stream (message->stream);
    g_mime_parser_set_scan_from (parser, is_mbox);

    message->message = g_mime_parser_construct_message (parser, NULL);
    if (! message->message) {
	status = NOTMUCH_STATUS_FILE_NOT_EMAIL;
	goto DONE;
    }

    if (is_mbox && ! g_mime_parser_eos (parser)) {
	/*
	 * This is a multi-message mbox. (For historical reasons, we
	 * do support single-message mboxes.)
	 */
	status = NOTMUCH_STATUS_FILE_NOT_EMAIL;
    }

  DONE:
    g_mime_stream_reset (message->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;
	}

    }

    return status;
}

notmuch_status_t
_notmuch_message_file_get_mime_message (notmuch_message_file_t *message,
					GMimeMessage **mime_message)
{
    notmuch_status_t status;

    status = _notmuch_message_file_parse (message);
    if (status)
	return status;

    *mime_message = message->message;

    return NOTMUCH_STATUS_SUCCESS;
}

/*
 * 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 *
_extend_header (char *combined, const char *value)
{
    char *decoded;

    decoded = g_mime_utils_header_decode_text (NULL, value);
    if (! decoded) {
	if (combined) {
	    g_free (combined);
	    combined = NULL;
	}
	goto DONE;
    }

    if (combined) {
	char *tmp = g_strdup_printf ("%s %s", combined, decoded);
	g_free (decoded);
	g_free (combined);
	if (! tmp) {
	    combined = NULL;
	    goto DONE;
	}

	combined = tmp;
    } else {
	combined = decoded;
    }
  DONE:
    return combined;
}

static char *
_notmuch_message_file_get_combined_header (notmuch_message_file_t *message,
					   const char *header)
{
    char *combined = NULL;
    GMimeHeaderList *headers;

    headers = g_mime_object_get_header_list (GMIME_OBJECT (message->message));
    if (! headers)
	return NULL;


    for (int i = 0; i < g_mime_header_list_get_count (headers); i++) {
	const char *value;
	GMimeHeader *g_header = g_mime_header_list_get_header_at (headers, i);

	if (strcasecmp (g_mime_header_get_name (g_header), header) != 0)
	    continue;

	/* GMime retains ownership of value, we hope */
	value = g_mime_header_get_value (g_header);

	combined = _extend_header (combined, value);
    }

    /* Return empty string for non-existing headers. */
    if (! combined)
	combined = g_strdup ("");

    return combined;
}

const char *
_notmuch_message_file_get_header (notmuch_message_file_t *message,
				  const char *header)
{
    const char *value;
    char *decoded;

    if (_notmuch_message_file_parse (message))
	return NULL;

    /* 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 (NULL, value);
	else
	    decoded = g_strdup ("");
    }

    if (! decoded)
	return NULL;

    /* Cache the decoded value. We also own the strings. */
    g_hash_table_insert (message->headers, xstrdup (header), decoded);

    return decoded;
}

notmuch_status_t
_notmuch_message_file_get_headers (notmuch_message_file_t *message_file,
				   const char **from_out,
				   const char **subject_out,
				   const char **to_out,
				   const char **date_out,
				   char **message_id_out)
{
    notmuch_status_t ret;
    const char *header;
    const char *from, *to, *subject, *date;
    char *message_id = NULL;

    /* Parse message up front to get better error status. */
    ret = _notmuch_message_file_parse (message_file);
    if (ret)
	goto DONE;

    /* Before we do any real work, (especially before doing a
     * potential SHA-1 computation on the entire file's contents),
     * let's make sure that what we're looking at looks like an
     * actual email message.
     */
    from = _notmuch_message_file_get_header (message_file, "from");
    subject = _notmuch_message_file_get_header (message_file, "subject");
    to = _notmuch_message_file_get_header (message_file, "to");
    date = _notmuch_message_file_get_header (message_file, "date");

    if ((from == NULL || *from == '\0') &&
	(subject == NULL || *subject == '\0') &&
	(to == NULL || *to == '\0')) {
	ret = NOTMUCH_STATUS_FILE_NOT_EMAIL;
	goto DONE;
    }

    /* Now that we're sure it's mail, the first order of business
     * is to find a message ID (or else create one ourselves).
     */
    header = _notmuch_message_file_get_header (message_file, "message-id");
    if (header && *header != '\0') {
	message_id = _notmuch_message_id_parse (message_file, header, NULL);

	/* So the header value isn't RFC-compliant, but it's
	 * better than no message-id at all.
	 */
	if (message_id == NULL)
	    message_id = talloc_strdup (message_file, header);
    }

    if (message_id == NULL ) {
	/* No message-id at all, let's generate one by taking a
	 * hash over the file's contents.
	 */
	char *sha1 = _notmuch_sha1_of_file (_notmuch_message_file_get_filename (message_file));

	/* If that failed too, something is really wrong. Give up. */
	if (sha1 == NULL) {
	    ret = NOTMUCH_STATUS_FILE_ERROR;
	    goto DONE;
	}

	message_id = talloc_asprintf (message_file, "notmuch-sha1-%s", sha1);
	free (sha1);
    }
  DONE:
    if (ret == NOTMUCH_STATUS_SUCCESS) {
	if (from_out)
	    *from_out = from;
	if (subject_out)
	    *subject_out = subject;
	if (to_out)
	    *to_out = to;
	if (date_out)
	    *date_out = date;
	if (message_id_out)
	    *message_id_out = message_id;
    }
    return ret;
}

debug log:

solving 647ccf3a ...
found 647ccf3a in https://yhetil.org/notmuch.git/

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).