unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Tomas Carnecky <tom@dbservice.com>
To: notmuch@notmuchmail.org
Subject: [PATCH] Add post-add and post-tag hooks
Date: Tue, 22 Dec 2009 03:56:57 +0100	[thread overview]
Message-ID: <1261450617-24616-1-git-send-email-tom@dbservice.com> (raw)

The post-add hook is run by 'notmuch new' after each new message is added,
post-tag is run after a tag has been added or removed. The hooks are stored
in the users home directory (~/.notmuch/hooks/).

Since post-tag is run unconditionally every time a new tag is added or removed,
that means it is also invoked when 'notmuch new' adds the two implicit
tags (inbox, unread). So make sure your scripts don't choke on that and can
be both executed in parallel.

Signed-off-by: Tomas Carnecky <tom@dbservice.com>
---
 lib/message.cc |   45 ++++++++++++++++++++++++++++++++++++++
 notmuch-new.c  |   66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 111 insertions(+), 0 deletions(-)

diff --git a/lib/message.cc b/lib/message.cc
index 49519f1..bcd8abb 100644
--- a/lib/message.cc
+++ b/lib/message.cc
@@ -664,6 +664,47 @@ _notmuch_message_remove_term (notmuch_message_t *message,
     return NOTMUCH_PRIVATE_STATUS_SUCCESS;
 }
 
+/* Run the post-tag hook */
+static void
+post_tag_hook (notmuch_message_t *message, const char *tag, int added)
+{
+    /* Skip tags that notmuch itself assigns to new messages */
+    const char *skip[] = {
+        "inbox", "unread"
+    };
+
+    for (int i = 0; i < sizeof (skip) / sizeof (skip[0]); ++i) {
+        if (strcmp(skip[i], tag) == 0)
+            return;
+    }
+
+    char proc[PATH_MAX];
+    snprintf (proc, PATH_MAX, "%s/.notmuch/hooks/post-tag", getenv("HOME"));
+    if (access (proc, X_OK))
+        return;
+
+    int pid = fork ();
+    if (pid == -1)
+        return;
+
+    /* Wait for the hook to finish. This behaviour might be changed in the
+     * future, but for now I think it's better to take the safe route. */
+    if (pid > 0) {
+        waitpid (0, NULL, 0);
+        return;
+    }
+
+    const char *filename = notmuch_message_get_filename (message);
+    const char *message_id = notmuch_message_get_message_id (message);
+
+    const char *args[] = {
+        proc, message_id, filename, tag, added ? "added" : "removed", NULL
+    };
+
+    execv (proc, (char *const *) &args);
+    exit (0);
+}
+
 notmuch_status_t
 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
 {
@@ -684,6 +725,8 @@ notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
     if (! message->frozen)
 	_notmuch_message_sync (message);
 
+    post_tag_hook (message, tag, 1);
+
     return NOTMUCH_STATUS_SUCCESS;
 }
 
@@ -707,6 +750,8 @@ notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
     if (! message->frozen)
 	_notmuch_message_sync (message);
 
+    post_tag_hook (message, tag, 0);
+
     return NOTMUCH_STATUS_SUCCESS;
 }
 
diff --git a/notmuch-new.c b/notmuch-new.c
index 837ae4f..d984aae 100644
--- a/notmuch-new.c
+++ b/notmuch-new.c
@@ -42,6 +42,71 @@ handle_sigint (unused (int sig))
     interrupted = 1;
 }
 
+/* Run the post-add hook. The hook is given the chance to specify additional tags
+ * that should be added to the message. The hook writes the tags to its stdout,
+ * separated by a newline. The script's stdout is redirected to a pipe so that
+ * notmuch can process its output. The tags can be prefixed with '+' or '-' to
+ * indicate if the tag should be added or removed. Absence of one of these prefixes
+ * means that the tag will be added. */
+static void
+post_add_hook (notmuch_message_t *message)
+{
+    char proc[PATH_MAX];
+    snprintf (proc, PATH_MAX, "%s/.notmuch/hooks/post-add", getenv ("HOME"));
+    if (access (proc, X_OK))
+        return;
+
+    /* The pipe between the hook and the notmuch process. The script writes
+     * into fds[0], notmuch reads from fds[1]. */
+    int fds[2];
+    if (pipe (fds))
+	return;
+
+    int pid = fork ();
+    if (pid == -1) {
+	close (fds[0]);
+	close (fds[1]);
+	return;
+    } else if (pid > 0) {
+	close (fds[0]);
+	waitpid (0, NULL, 0);
+
+	char buffer[256] = { 0, };
+	read (fds[1], buffer, sizeof (buffer));
+
+	char *tag;
+	for (tag = buffer; tag && *tag; ) {
+	    char *end = strchr (tag, '\n');
+	    if (end)
+		*end = 0;
+
+	    if (tag[0] == '+')
+		notmuch_message_add_tag (message, tag + 1);
+	    else if (tag[0] == '-')
+		notmuch_message_remove_tag (message, tag + 1);
+	    else
+		notmuch_message_add_tag (message, tag);
+
+	    tag = end ? end + 1 : end;
+	}
+
+	return;
+    }
+
+    /* This is the child process (where the hook runs) */
+    close (fds[1]);
+    dup2 (fds[0], 1);
+
+    const char *filename = notmuch_message_get_filename (message);
+    const char *message_id = notmuch_message_get_message_id (message);
+    const char *args[] = {
+	proc, message_id, filename, NULL
+    };
+
+    execv (proc, (char *const *) &args);
+    exit (0);
+}
+
 static void
 tag_inbox_and_unread (notmuch_message_t *message)
 {
@@ -253,6 +318,7 @@ add_files_recursive (notmuch_database_t *notmuch,
 		    case NOTMUCH_STATUS_SUCCESS:
 			state->added_messages++;
 			tag_inbox_and_unread (message);
+			post_add_hook (message);
 			break;
 		    /* Non-fatal issues (go on to next file) */
 		    case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
-- 
1.6.6.rc3

             reply	other threads:[~2009-12-22  2:57 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-12-22  2:56 Tomas Carnecky [this message]
2009-12-22  3:18 ` [PATCH] Add post-add and post-tag hooks Tomas Carnecky
2009-12-22 23:02 ` Olly Betts
2009-12-23  6:57   ` Tomas Carnecky
2009-12-23  8:29     ` Olly Betts

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1261450617-24616-1-git-send-email-tom@dbservice.com \
    --to=tom@dbservice.com \
    --cc=notmuch@notmuchmail.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).