aboutsummaryrefslogtreecommitdiffhomepage
path: root/show-message.c
diff options
context:
space:
mode:
authorGravatar Keith Packard <keithp@keithp.com>2009-11-06 10:00:38 -0800
committerGravatar Carl Worth <cworth@cworth.org>2009-11-10 13:32:02 -0800
commit357aba3ec8177c11a7ce22cbe26d92482f6a5e53 (patch)
tree33f04c027b4de0b525a695878540bfc14e5da563 /show-message.c
parent50144f95cababfb73027ca95ad1fb303c235a893 (diff)
notmuch reply: Add (incomplete) reply command
Reviewed-by: Carl Worth <cworth@cworth.org> Keith wrote all the code here against notmuch before notmuch.c was split up into multiple files. So I've pushed the code around in various ways to match the new code structure, but have generally tried to avoid making any changes to the behavior of the code. I did fix one bug---a missing call to g_mime_stream_file_set_owner in show_part which would cause "notmuch show" to go off into the weeds when trying to show multiple messages, (since the first stream would fclose stdout).
Diffstat (limited to 'show-message.c')
-rw-r--r--show-message.c117
1 files changed, 117 insertions, 0 deletions
diff --git a/show-message.c b/show-message.c
new file mode 100644
index 00000000..2d3189e3
--- /dev/null
+++ b/show-message.c
@@ -0,0 +1,117 @@
+/* notmuch - Not much of an email program, (just index and search)
+ *
+ * Copyright © 2009 Carl Worth
+ * Copyright © 2009 Keith Packard
+ *
+ * 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/ .
+ *
+ * Authors: Carl Worth <cworth@cworth.org>
+ * Keith Packard <keithp@keithp.com>
+ */
+
+#include "notmuch-client.h"
+
+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);
+ }
+ return;
+ }
+
+ if (GMIME_IS_MESSAGE_PART (part)) {
+ GMimeMessage *mime_message;
+
+ mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
+
+ show_message_part (g_mime_message_get_mime_part (mime_message),
+ part_count, show_part);
+
+ return;
+ }
+
+ if (! (GMIME_IS_PART (part))) {
+ fprintf (stderr, "Warning: Not displaying unknown mime part: %s.\n",
+ g_type_name (G_OBJECT_TYPE (part)));
+ return;
+ }
+
+ (*show_part) (part, part_count);
+}
+
+notmuch_status_t
+show_message_body (const char *filename,
+ void (*show_part) (GMimeObject *part, int *part_count))
+{
+ GMimeStream *stream = NULL;
+ 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));
+ 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_message_part (g_mime_message_get_mime_part (mime_message),
+ &part_count, show_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;
+}