[PATCH 2/2] cli: add support for running notmuch new pre and post hooks
authorJani Nikula <jani@nikula.org>
Fri, 2 Dec 2011 21:00:06 +0000 (23:00 +0200)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:40:32 +0000 (09:40 -0800)
61/c96e1d786b25a40fbe5a3d50f2ed1efe4ec846 [new file with mode: 0644]

diff --git a/61/c96e1d786b25a40fbe5a3d50f2ed1efe4ec846 b/61/c96e1d786b25a40fbe5a3d50f2ed1efe4ec846
new file mode 100644 (file)
index 0000000..205d161
--- /dev/null
@@ -0,0 +1,179 @@
+Return-Path: <jani@nikula.org>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+       by olra.theworths.org (Postfix) with ESMTP id 7EE61429E21\r
+       for <notmuch@notmuchmail.org>; Fri,  2 Dec 2011 13:00:19 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+       tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+       by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+       with ESMTP id fr+YZLJRn6MS for <notmuch@notmuchmail.org>;\r
+       Fri,  2 Dec 2011 13:00:18 -0800 (PST)\r
+Received: from mail-ww0-f45.google.com (mail-ww0-f45.google.com\r
+ [74.125.82.45])       (using TLSv1 with cipher RC4-SHA (128/128 bits))        (No client\r
+ certificate requested)        by olra.theworths.org (Postfix) with ESMTPS id\r
+ 3A42B429E34   for <notmuch@notmuchmail.org>; Fri,  2 Dec 2011 13:00:18 -0800\r
+ (PST)\r
+Received: by wgbds13 with SMTP id ds13so1494759wgb.2\r
+       for <notmuch@notmuchmail.org>; Fri, 02 Dec 2011 13:00:16 -0800 (PST)\r
+Received: by 10.216.137.137 with SMTP id y9mr803861wei.112.1322859616200;\r
+       Fri, 02 Dec 2011 13:00:16 -0800 (PST)\r
+Received: from localhost (dsl-hkibrasgw4-fe5cdc00-23.dhcp.inet.fi.\r
+       [80.220.92.23])\r
+       by mx.google.com with ESMTPS id em4sm12046848wbb.20.2011.12.02.13.00.14\r
+       (version=SSLv3 cipher=OTHER); Fri, 02 Dec 2011 13:00:14 -0800 (PST)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH 2/2] cli: add support for running notmuch new pre and post\r
+       hooks\r
+Date: Fri,  2 Dec 2011 23:00:06 +0200\r
+Message-Id:\r
+ <716da00e176e1dc6af0ba248caee40acee733120.1322859389.git.jani@nikula.org>\r
+X-Mailer: git-send-email 1.7.5.4\r
+In-Reply-To:\r
+ <7fbe6befcf31881a9bca672f55b93501249a220c.1322859389.git.jani@nikula.org>\r
+References:\r
+ <7fbe6befcf31881a9bca672f55b93501249a220c.1322859389.git.jani@nikula.org>\r
+In-Reply-To:\r
+ <7fbe6befcf31881a9bca672f55b93501249a220c.1322859389.git.jani@nikula.org>\r
+References:\r
+ <7fbe6befcf31881a9bca672f55b93501249a220c.1322859389.git.jani@nikula.org>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+       <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Fri, 02 Dec 2011 21:00:19 -0000\r
+\r
+Run notmuch new pre and post hooks if specified in the notmuch config\r
+file. The hooks will be run before and after incorporating new messages to\r
+the database. Also add command line option --no-hooks to notmuch new to\r
+bypass the hooks.\r
+\r
+With this patch, you can add hooks in your config, for example:\r
+\r
+[new]\r
+prehook=offlineimap\r
+posthook=my-tagging-script\r
+\r
+As the value is passed to system(1), you can actually have multiple\r
+commands, redirections, pipes, etc. in there. If the tagging is simple\r
+enough, you can just add the tagging in-line without a script.\r
+\r
+TODO:\r
+\r
+* Tests.\r
+\r
+* Documentation (manpage and help).\r
+\r
+Signed-off-by: Jani Nikula <jani@nikula.org>\r
+---\r
+ notmuch-client.h |    3 ++-\r
+ notmuch-config.c |   16 +++++++++++++++-\r
+ notmuch-new.c    |    9 +++++++++\r
+ 3 files changed, 26 insertions(+), 2 deletions(-)\r
+\r
+diff --git a/notmuch-client.h b/notmuch-client.h\r
+index 5e2fed2..d2ebc73 100644\r
+--- a/notmuch-client.h\r
++++ b/notmuch-client.h\r
+@@ -88,7 +88,8 @@ typedef struct notmuch_show_params {\r
+ } notmuch_show_params_t;\r
\r
+ typedef enum {\r
+-    NOTMUCH_HOOK_PLACEHOLDER,\r
++    NOTMUCH_HOOK_PRE_NEW,\r
++    NOTMUCH_HOOK_POST_NEW,\r
+ } notmuch_hook_t;\r
\r
+ /* There's no point in continuing when we've detected that we've done\r
+diff --git a/notmuch-config.c b/notmuch-config.c\r
+index 8f1a038..277f197 100644\r
+--- a/notmuch-config.c\r
++++ b/notmuch-config.c\r
+@@ -43,7 +43,13 @@ static const char new_config_comment[] =\r
+     " The following options are supported here:\n"\r
+     "\n"\r
+     "\ttags   A list (separated by ';') of the tags that will be\n"\r
+-    "\t       added to all messages incorporated by \"notmuch new\".\n";\r
++    "\t       added to all messages incorporated by \"notmuch new\".\n"\r
++    "\tprehook        A command to be executed before \"notmuch new\" starts\n"\r
++    "\t       incorporating new messages. For example, this could be used to fetch\n"\r
++    "\t       and deliver new messages to the mail directory.\n"\r
++    "\tposthook       A command to be executed after \"notmuch new\" has\n"\r
++    "\t       incorporated and tagged all new messages. For example, this could\n"\r
++    "\t       be used to perform further tagging on new messages.\n";\r
\r
+ static const char user_config_comment[] =\r
+     " User configuration\n"\r
+@@ -615,6 +621,14 @@ notmuch_config_get_hook (notmuch_config_t *config, notmuch_hook_t hook)\r
+     const char *group, *key;\r
\r
+     switch (hook) {\r
++    case NOTMUCH_HOOK_PRE_NEW:\r
++      group = "new";\r
++      key = "prehook";\r
++      break;\r
++    case NOTMUCH_HOOK_POST_NEW:\r
++      group = "new";\r
++      key = "posthook";\r
++      break;\r
+     default:\r
+       INTERNAL_ERROR ("Unknown hook %d\n.", hook);\r
+     }\r
+diff --git a/notmuch-new.c b/notmuch-new.c\r
+index 0c70e64..09cc3f2 100644\r
+--- a/notmuch-new.c\r
++++ b/notmuch-new.c\r
+@@ -836,6 +836,7 @@ notmuch_new_command (void *ctx, int argc, char *argv[])\r
+     _filename_node_t *f;\r
+     int i;\r
+     notmuch_bool_t timer_is_active = FALSE;\r
++    int run_hooks = 1;\r
\r
+     add_files_state.verbose = 0;\r
+     add_files_state.output_is_a_tty = isatty (fileno (stdout));\r
+@@ -845,6 +846,8 @@ notmuch_new_command (void *ctx, int argc, char *argv[])\r
+     for (i = 0; i < argc && argv[i][0] == '-'; i++) {\r
+       if (STRNCMP_LITERAL (argv[i], "--verbose") == 0) {\r
+           add_files_state.verbose = 1;\r
++      } else if (STRNCMP_LITERAL (argv[i], "--no-hooks") == 0) {\r
++          run_hooks = 0;\r
+       } else {\r
+           fprintf (stderr, "Unrecognized option: %s\n", argv[i]);\r
+           return 1;\r
+@@ -854,6 +857,9 @@ notmuch_new_command (void *ctx, int argc, char *argv[])\r
+     if (config == NULL)\r
+       return 1;\r
\r
++    if (run_hooks && notmuch_run_hook (config, NOTMUCH_HOOK_PRE_NEW))\r
++      return 1;\r
++\r
+     add_files_state.new_tags = notmuch_config_get_new_tags (config, &add_files_state.new_tags_length);\r
+     add_files_state.synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);\r
+     db_path = notmuch_config_get_database_path (config);\r
+@@ -1006,5 +1012,8 @@ notmuch_new_command (void *ctx, int argc, char *argv[])\r
\r
+     notmuch_database_close (notmuch);\r
\r
++    if (run_hooks && !interrupted)\r
++      ret |= notmuch_run_hook (config, NOTMUCH_HOOK_POST_NEW);\r
++\r
+     return ret || interrupted;\r
+ }\r
+-- \r
+1.7.5.4\r
+\r