[PATCH v2 2/2] cli: add support for pre and post notmuch new hooks
authorJani Nikula <jani@nikula.org>
Sat, 3 Dec 2011 23:16:49 +0000 (01:16 +0200)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:40:33 +0000 (09:40 -0800)
1f/71b62fe95f593257a8f23ebd64f55f7531051b [new file with mode: 0644]

diff --git a/1f/71b62fe95f593257a8f23ebd64f55f7531051b b/1f/71b62fe95f593257a8f23ebd64f55f7531051b
new file mode 100644 (file)
index 0000000..a41fe12
--- /dev/null
@@ -0,0 +1,197 @@
+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 AB951429E26\r
+       for <notmuch@notmuchmail.org>; Sat,  3 Dec 2011 15:16:58 -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 uIBM-oH53Pye for <notmuch@notmuchmail.org>;\r
+       Sat,  3 Dec 2011 15:16:58 -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
+ CA89A429E25   for <notmuch@notmuchmail.org>; Sat,  3 Dec 2011 15:16:57 -0800\r
+ (PST)\r
+Received: by mail-ww0-f45.google.com with SMTP id ds13so3394124wgb.2\r
+       for <notmuch@notmuchmail.org>; Sat, 03 Dec 2011 15:16:57 -0800 (PST)\r
+Received: by 10.227.205.197 with SMTP id fr5mr10797322wbb.3.1322954217527;\r
+       Sat, 03 Dec 2011 15:16:57 -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 dd4sm6904421wib.1.2011.12.03.15.16.55\r
+       (version=SSLv3 cipher=OTHER); Sat, 03 Dec 2011 15:16:56 -0800 (PST)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v2 2/2] cli: add support for pre and post notmuch new hooks\r
+Date: Sun,  4 Dec 2011 01:16:49 +0200\r
+Message-Id:\r
+ <6ccaa31da55b0dfc9e339780e43e24e1489235e8.1322953841.git.jani@nikula.org>\r
+X-Mailer: git-send-email 1.7.5.4\r
+In-Reply-To:\r
+ <6688b09fffa2a66b496af78008102f88ab4e9450.1322953841.git.jani@nikula.org>\r
+References:\r
+ <7fbe6befcf31881a9bca672f55b93501249a220c.1322859389.git.jani@nikula.org>\r
+       <6688b09fffa2a66b496af78008102f88ab4e9450.1322953841.git.jani@nikula.org>\r
+In-Reply-To:\r
+ <6688b09fffa2a66b496af78008102f88ab4e9450.1322953841.git.jani@nikula.org>\r
+References:\r
+ <6688b09fffa2a66b496af78008102f88ab4e9450.1322953841.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: Sat, 03 Dec 2011 23:16:58 -0000\r
+\r
+Run notmuch new pre and post hooks, named "pre-new" and "post-new", if\r
+present in the notmuch hooks directory. The hooks will be run before and\r
+after incorporating new messages to the database.\r
+\r
+Typical use cases for pre-new and post-new hooks are fetching or delivering\r
+new mail to the maildir, and custom tagging of the mail incorporated to the\r
+database.\r
+\r
+Also add command line option --no-hooks to notmuch new to bypass the hooks.\r
+\r
+Signed-off-by: Jani Nikula <jani@nikula.org>\r
+---\r
+ notmuch-new.c |   12 ++++++++++++\r
+ notmuch.1     |   50 +++++++++++++++++++++++++++++++++++++++++++++++++-\r
+ 2 files changed, 61 insertions(+), 1 deletions(-)\r
+\r
+diff --git a/notmuch-new.c b/notmuch-new.c\r
+index 81a9350..27dde0c 100644\r
+--- a/notmuch-new.c\r
++++ b/notmuch-new.c\r
+@@ -811,6 +811,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
+@@ -820,6 +821,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
+@@ -833,6 +836,12 @@ notmuch_new_command (void *ctx, int argc, char *argv[])\r
+     add_files_state.synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);\r
+     db_path = notmuch_config_get_database_path (config);\r
\r
++    if (run_hooks) {\r
++      ret = notmuch_run_hook (db_path, "pre-new");\r
++      if (ret)\r
++          return ret;\r
++    }\r
++\r
+     dot_notmuch_path = talloc_asprintf (ctx, "%s/%s", db_path, ".notmuch");\r
\r
+     if (stat (dot_notmuch_path, &st)) {\r
+@@ -981,5 +990,8 @@ notmuch_new_command (void *ctx, int argc, char *argv[])\r
\r
+     notmuch_database_close (notmuch);\r
\r
++    if (run_hooks && !ret && !interrupted)\r
++      ret = notmuch_run_hook (db_path, "post-new");\r
++\r
+     return ret || interrupted;\r
+ }\r
+diff --git a/notmuch.1 b/notmuch.1\r
+index 92931d7..66f82e9 100644\r
+--- a/notmuch.1\r
++++ b/notmuch.1\r
+@@ -85,7 +85,7 @@ The\r
+ command is used to incorporate new mail into the notmuch database.\r
+ .RS 4\r
+ .TP 4\r
+-.B new\r
++.BR new " [options...]"\r
\r
+ Find and import any new messages to the database.\r
\r
+@@ -118,6 +118,22 @@ if\r
+ has previously been completed, but\r
+ .B "notmuch new"\r
+ has not previously been run.\r
++\r
++The\r
++.B new\r
++command supports hooks. See the\r
++.B "HOOKS"\r
++section below for more details on hooks.\r
++\r
++Supported options for\r
++.B new\r
++include\r
++.RS 4\r
++.TP 4\r
++.BR \-\-no\-hooks\r
++\r
++Prevents hooks from being run.\r
++.RE\r
+ .RE\r
\r
+ Several of the notmuch commands accept search terms with a common\r
+@@ -705,6 +721,38 @@ specify a date range to return messages from 2009\-10\-01 until the\r
+ current time:\r
\r
+       $(date +%s \-d 2009\-10\-01)..$(date +%s)\r
++.SH HOOKS\r
++Hooks are scripts (or arbitrary executables or symlinks to such) you can place\r
++in the notmuch hooks directory to trigger action at certain points. The hooks\r
++directory is .notmuch/hooks within the database directory. The user must have\r
++executable permission set on the scripts.\r
++\r
++The currently available hooks are described below.\r
++.RS 4\r
++.TP 4\r
++.B pre\-new\r
++This hook is invoked by the\r
++.B new\r
++command before scanning or importing new messages into the database. Any errors\r
++in running the hook will abort further processing of the\r
++.B new\r
++command.\r
++\r
++Typical use case for this hook is fetching or delivering new mail to be imported\r
++into the database.\r
++.RE\r
++.RS 4\r
++.TP 4\r
++.B post\-new\r
++This hook is invoked by the\r
++.B new\r
++command after new messages have been imported into the database and initial tags\r
++have been applied. The hook will not be run if there have been any errors during\r
++the scan or import.\r
++\r
++Typical use case for this hook is performing additional query based tagging on\r
++the imported messages.\r
++.RE\r
+ .SH ENVIRONMENT\r
+ The following environment variables can be used to control the\r
+ behavior of notmuch.\r
+-- \r
+1.7.5.4\r
+\r