]> git.notmuchmail.org Git - notmuch/blobdiff - show-message.c
Add infrastructure for building shared library on OS X.
[notmuch] / show-message.c
index 2d3189e33474b02e2e177827f4ab17b3459d24b3..b1b61be40c2618fddcc56c46e683e517f9101f7d 100644 (file)
@@ -26,20 +26,11 @@ static void
 show_message_part (GMimeObject *part, int *part_count,
                   void (*show_part) (GMimeObject *part, int *part_count))
 {
-    *part_count = *part_count + 1;
-
     if (GMIME_IS_MULTIPART (part)) {
        GMimeMultipart *multipart = GMIME_MULTIPART (part);
        int i;
 
        for (i = 0; i < g_mime_multipart_get_count (multipart); i++) {
-           if (GMIME_IS_MULTIPART_SIGNED (multipart)) {
-               /* Don't index the signature. */
-               if (i == 1)
-                   continue;
-               if (i > 1)
-                   fprintf (stderr, "Warning: Unexpected extra parts of mutlipart/signed. Continuing.\n");
-           }
            show_message_part (g_mime_multipart_get_part (multipart, i),
                               part_count, show_part);
        }
@@ -63,6 +54,8 @@ show_message_part (GMimeObject *part, int *part_count,
        return;
     }
 
+    *part_count = *part_count + 1;
+
     (*show_part) (part, part_count);
 }
 
@@ -74,15 +67,9 @@ show_message_body (const char *filename,
     GMimeParser *parser = NULL;
     GMimeMessage *mime_message = NULL;
     notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
-    static int initialized = 0;
     FILE *file = NULL;
     int part_count = 0;
 
-    if (! initialized) {
-       g_mime_init (0);
-       initialized = 1;
-    }
-
     file = fopen (filename, "r");
     if (! file) {
        fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
@@ -115,3 +102,95 @@ show_message_body (const char *filename,
 
     return ret;
 }
+
+static void
+show_one_part_output (GMimeObject *part)
+{
+    GMimeStream *stream_filter = NULL;
+    GMimeDataWrapper *wrapper;
+    GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
+
+    stream_filter = g_mime_stream_filter_new(stream_stdout);
+    wrapper = g_mime_part_get_content_object (GMIME_PART (part));
+    if (wrapper && stream_filter)
+       g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
+    if (stream_filter)
+       g_object_unref(stream_filter);
+}
+
+static void
+show_one_part_worker (GMimeObject *part, int *part_count, int desired_part)
+{
+    if (GMIME_IS_MULTIPART (part)) {
+       GMimeMultipart *multipart = GMIME_MULTIPART (part);
+       int i;
+
+       for (i = 0; i < g_mime_multipart_get_count (multipart); i++) {
+               show_one_part_worker (g_mime_multipart_get_part (multipart, i),
+                                     part_count, desired_part);
+       }
+       return;
+    }
+
+    if (GMIME_IS_MESSAGE_PART (part)) {
+       GMimeMessage *mime_message;
+
+       mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
+
+       show_one_part_worker (g_mime_message_get_mime_part (mime_message),
+                             part_count, desired_part);
+
+       return;
+    }
+
+    if (! (GMIME_IS_PART (part)))
+       return;
+
+    *part_count = *part_count + 1;
+
+    if (*part_count == desired_part)
+           show_one_part_output (part);
+}
+
+notmuch_status_t
+show_one_part (const char *filename, int part)
+{
+       GMimeStream *stream = NULL;
+       GMimeParser *parser = NULL;
+       GMimeMessage *mime_message = NULL;
+       notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
+       FILE *file = NULL;
+       int part_count = 0;
+
+       file = fopen (filename, "r");
+       if (! file) {
+               fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
+               ret = NOTMUCH_STATUS_FILE_ERROR;
+               goto DONE;
+       }
+
+       stream = g_mime_stream_file_new (file);
+       g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream), FALSE);
+
+       parser = g_mime_parser_new_with_stream (stream);
+
+       mime_message = g_mime_parser_construct_message (parser);
+
+       show_one_part_worker (g_mime_message_get_mime_part (mime_message),
+                             &part_count, part);
+
+ DONE:
+       if (mime_message)
+               g_object_unref (mime_message);
+
+       if (parser)
+               g_object_unref (parser);
+
+       if (stream)
+               g_object_unref (stream);
+
+       if (file)
+               fclose (file);
+
+       return ret;
+}