[notmuch] [PATCH v3 6/6] Add 'cat' subcommand

Michal Sojka sojkam1 at fel.cvut.cz
Sat Mar 27 13:44:21 PDT 2010


This command dumps raw message identified by filename to standard
output. It uses mail store interface to get the message from the right
place.

notmuch.el was modified to use this command to access the raw message
content (view/save attachments and view raw message).

Signed-off-by: Michal Sojka <sojkam1 at fel.cvut.cz>
---
 emacs/notmuch.el |    8 +++++-
 notmuch-client.h |    3 ++
 notmuch-show.c   |   62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 notmuch.c        |    4 +++
 4 files changed, 75 insertions(+), 2 deletions(-)

diff --git a/emacs/notmuch.el b/emacs/notmuch.el
index 117a365..38ba0e8 100644
--- a/emacs/notmuch.el
+++ b/emacs/notmuch.el
@@ -357,7 +357,11 @@ buffer."
 (defun notmuch-show-view-raw-message ()
   "View the raw email of the current message."
   (interactive)
-  (view-file (notmuch-show-get-filename)))
+  (let ((filename (notmuch-show-get-filename)))
+    (let ((buf (get-buffer-create (concat "*notmuch-raw-" filename "*"))))
+      (switch-to-buffer buf)
+      (save-excursion
+	(call-process notmuch-command nil t nil "cat" filename)))))
 
 (defmacro with-current-notmuch-show-message (&rest body)
   "Evaluate body with current buffer set to the text of current message"
@@ -365,7 +369,7 @@ buffer."
      (let ((filename (notmuch-show-get-filename)))
        (let ((buf (generate-new-buffer (concat "*notmuch-msg-" filename "*"))))
          (with-current-buffer buf
-           (insert-file-contents filename nil nil nil t)
+	    (call-process notmuch-command nil t nil "cat" filename)
            , at body)
 	 (kill-buffer buf)))))
 
diff --git a/notmuch-client.h b/notmuch-client.h
index f5106cd..b9ddad2 100644
--- a/notmuch-client.h
+++ b/notmuch-client.h
@@ -107,6 +107,9 @@ notmuch_tag_command (void *ctx, int argc, char *argv[]);
 int
 notmuch_search_tags_command (void *ctx, int argc, char *argv[]);
 
+int
+notmuch_cat_command (void *ctx, int argc, char *argv[]);
+
 const char *
 notmuch_time_relative_date (const void *ctx, time_t then);
 
diff --git a/notmuch-show.c b/notmuch-show.c
index 0fcaacf..537d2e2 100644
--- a/notmuch-show.c
+++ b/notmuch-show.c
@@ -510,3 +510,65 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
 
     return 0;
 }
+
+int
+notmuch_cat_command (void *ctx, unused (int argc), unused (char *argv[]))
+{
+    notmuch_config_t *config;
+    notmuch_database_t *notmuch;
+    notmuch_mailstore_t *mailstore;
+    int i;
+    FILE *file;
+    const char *filename;
+    size_t size;
+    char buf[4096];
+
+    for (i = 0; i < argc && argv[i][0] == '-'; i++) {
+/* 	if (STRNCMP_LITERAL (argv[i], "--part=") == 0) { */
+/* 	    part = atoi(argv[i] + sizeof ("--part=") - 1); */
+/* 	} else { */
+	    fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
+/* 	    return 1; */
+/* 	} */
+    }
+
+    argc -= i;
+    argv += i;
+
+    if (argc == 0) {
+	fprintf (stderr, "Error: No filename given\n");
+	return 1;
+    }
+    filename = argv[0];
+
+    config = notmuch_config_open (ctx, NULL, NULL);
+    if (config == NULL)
+	return 1;
+
+    mailstore = notmuch_config_get_mailstore (config);
+
+    if (mailstore == NULL) {
+	fprintf (stderr, "Error: I have no mailstore\n");
+	return 1;
+    }
+
+    notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
+				     NOTMUCH_DATABASE_MODE_READ_ONLY,
+				     mailstore);
+
+    file = notmuch_mailstore_open_file(mailstore, filename);
+    if (file == NULL) {
+	fprintf (stderr, "Error: Cannot open %s in %s: %s\n", filename,
+		 notmuch_mailstore_get_type (mailstore), strerror (errno));
+	return 1;
+    }
+    while  (!feof (file)) {
+	size = fread(buf, 1, sizeof(buf), file);
+	fwrite (buf, size, 1, stdout);
+    }
+    fclose (file);
+
+    notmuch_database_close (notmuch);
+
+    return 0;
+}
diff --git a/notmuch.c b/notmuch.c
index 95f057e..f8bb8f5 100644
--- a/notmuch.c
+++ b/notmuch.c
@@ -294,6 +294,10 @@ command_t commands[] = {
       "\t\tcontain tags only from messages that match the search-term(s).\n"
       "\n"
       "\t\tIn both cases the list will be alphabetically sorted." },
+    { "cat", notmuch_cat_command,
+      "<path>",
+      "\t\tDumps raw message identified by <path> to standard output.",
+      "\n" },
     { "help", notmuch_help_command,
       "[<command>]",
       "\t\tThis message, or more detailed help for the named command.",
-- 
1.7.0.2



More information about the notmuch mailing list