]> git.notmuchmail.org Git - notmuch/blobdiff - lib/message-file.c
lib: cache user prefixes in database object
[notmuch] / lib / message-file.c
index ee305202fffa63f0e1a1d024c738a1d69a39ebb4..24c5fda43b1338fb6f5c1c45a2d79e3c51005a98 100644 (file)
@@ -13,7 +13,7 @@
  * 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/ .
+ * along with this program.  If not, see https://www.gnu.org/licenses/ .
  *
  * Author: Carl Worth <cworth@cworth.org>
  */
@@ -27,8 +27,8 @@
 #include <glib.h> /* GHashTable */
 
 struct _notmuch_message_file {
-    /* File object */
-    FILE *file;
+    /* open stream to (possibly gzipped) file */
+    GMimeStream *stream;
     char *filename;
 
     /* Cache for decoded headers */
@@ -46,8 +46,8 @@ _notmuch_message_file_destructor (notmuch_message_file_t *message)
     if (message->message)
        g_object_unref (message->message);
 
-    if (message->file)
-       fclose (message->file);
+    if (message->stream)
+       g_object_unref (message->stream);
 
     return 0;
 }
@@ -64,15 +64,14 @@ _notmuch_message_file_open_ctx (notmuch_database_t *notmuch,
     if (unlikely (message == NULL))
        return NULL;
 
-    /* Only needed for error messages during parsing. */
     message->filename = talloc_strdup (message, filename);
     if (message->filename == NULL)
        goto FAIL;
 
     talloc_set_destructor (message, _notmuch_message_file_destructor);
 
-    message->file = fopen (filename, "r");
-    if (message->file == NULL)
+    message->stream = g_mime_stream_gzfile_open (filename);
+    if (message->stream == NULL)
        goto FAIL;
 
     return message;
@@ -92,24 +91,30 @@ _notmuch_message_file_open (notmuch_database_t *notmuch,
     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 notmuch_bool_t
-_is_mbox (FILE *file)
+static bool
+_is_mbox (GMimeStream *stream)
 {
     char from_buf[5];
-    notmuch_bool_t ret = FALSE;
+    bool ret = false;
 
     /* Is this mbox? */
-    if (fread (from_buf, sizeof (from_buf), 1, file) == 1 &&
+    if (g_mime_stream_read (stream, from_buf, sizeof (from_buf)) == sizeof(from_buf) &&
        strncmp (from_buf, "From ", 5) == 0)
-       ret = TRUE;
+       ret = true;
 
-    rewind (file);
+    g_mime_stream_reset (stream);
 
     return ret;
 }
@@ -117,19 +122,18 @@ _is_mbox (FILE *file)
 notmuch_status_t
 _notmuch_message_file_parse (notmuch_message_file_t *message)
 {
-    GMimeStream *stream;
     GMimeParser *parser;
     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
     static int initialized = 0;
-    notmuch_bool_t is_mbox;
+    bool is_mbox;
 
     if (message->message)
        return NOTMUCH_STATUS_SUCCESS;
 
-    is_mbox = _is_mbox (message->file);
+    is_mbox = _is_mbox (message->stream);
 
     if (! initialized) {
-       g_mime_init (GMIME_ENABLE_RFC2047_WORKAROUNDS);
+       g_mime_init ();
        initialized = 1;
     }
 
@@ -138,15 +142,10 @@ _notmuch_message_file_parse (notmuch_message_file_t *message)
     if (! message->headers)
        return NOTMUCH_STATUS_OUT_OF_MEMORY;
 
-    stream = g_mime_stream_file_new (message->file);
-
-    /* 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);
+    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);
+    message->message = g_mime_parser_construct_message (parser, NULL);
     if (! message->message) {
        status = NOTMUCH_STATUS_FILE_NOT_EMAIL;
        goto DONE;
@@ -161,7 +160,7 @@ _notmuch_message_file_parse (notmuch_message_file_t *message)
     }
 
   DONE:
-    g_object_unref (stream);
+    g_mime_stream_reset (message->stream);
     g_object_unref (parser);
 
     if (status) {
@@ -173,7 +172,6 @@ _notmuch_message_file_parse (notmuch_message_file_t *message)
            message->message = NULL;
        }
 
-       rewind (message->file);
     }
 
     return status;
@@ -201,67 +199,66 @@ _notmuch_message_file_get_mime_message (notmuch_message_file_t *message,
  *
  * 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)
 {
-    GMimeHeaderList *headers;
-    GMimeHeaderIter *iter;
     char *combined = NULL;
+    GMimeHeaderList *headers;
 
     headers = g_mime_object_get_header_list (GMIME_OBJECT (message->message));
     if (! headers)
        return NULL;
 
-    iter = g_mime_header_iter_new ();
-    if (! iter)
-       return NULL;
-
-    if (! g_mime_header_list_get_iter (headers, iter))
-       goto DONE;
 
-    do {
+    for (int i=0; i < g_mime_header_list_get_count (headers); i++) {
        const char *value;
-       char *decoded;
+       GMimeHeader *g_header = g_mime_header_list_get_header_at (headers, i);
 
-       if (strcasecmp (g_mime_header_iter_get_name (iter), header) != 0)
+       if (strcasecmp (g_mime_header_get_name (g_header), header) != 0)
            continue;
 
-       /* Note that GMime retains ownership of value... */
-       value = g_mime_header_iter_get_value (iter);
+       /* GMime retains ownership of value, we hope */
+       value = g_mime_header_get_value (g_header);
 
-       /* ... 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;
-       }
-
-       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;
-       }
-    } while (g_mime_header_iter_next (iter));
+       combined = _extend_header (combined, value);
+    }
 
     /* Return empty string for non-existing headers. */
     if (! combined)
        combined = g_strdup ("");
 
-  DONE:
-    g_mime_header_iter_free (iter);
-
     return combined;
 }
 
@@ -291,7 +288,7 @@ _notmuch_message_file_get_header (notmuch_message_file_t *message,
        value = g_mime_object_get_header (GMIME_OBJECT (message->message),
                                          header);
        if (value)
-           decoded = g_mime_utils_header_decode_text (value);
+           decoded = g_mime_utils_header_decode_text (NULL, value);
        else
            decoded = g_strdup ("");
     }
@@ -304,3 +301,83 @@ _notmuch_message_file_get_header (notmuch_message_file_t *message,
 
     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;
+}