[PATCH 1/2] cli: add mechanism for running user configurable hooks

2011-12-02 Thread Jani Nikula
On Fri,  2 Dec 2011 23:00:05 +0200, Jani Nikula  wrote:
> TODO:

I meant these as RFC, but forgot to add the subject prefix.

Jani.


[PATCH 2/2] cli: add support for running notmuch new pre and post hooks

2011-12-02 Thread Jani Nikula
Run notmuch new pre and post hooks if specified in the notmuch config
file. The hooks will be run before and after incorporating new messages to
the database. Also add command line option --no-hooks to notmuch new to
bypass the hooks.

With this patch, you can add hooks in your config, for example:

[new]
prehook=offlineimap
posthook=my-tagging-script

As the value is passed to system(1), you can actually have multiple
commands, redirections, pipes, etc. in there. If the tagging is simple
enough, you can just add the tagging in-line without a script.

TODO:

* Tests.

* Documentation (manpage and help).

Signed-off-by: Jani Nikula 
---
 notmuch-client.h |3 ++-
 notmuch-config.c |   16 +++-
 notmuch-new.c|9 +
 3 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/notmuch-client.h b/notmuch-client.h
index 5e2fed2..d2ebc73 100644
--- a/notmuch-client.h
+++ b/notmuch-client.h
@@ -88,7 +88,8 @@ typedef struct notmuch_show_params {
 } notmuch_show_params_t;

 typedef enum {
-NOTMUCH_HOOK_PLACEHOLDER,
+NOTMUCH_HOOK_PRE_NEW,
+NOTMUCH_HOOK_POST_NEW,
 } notmuch_hook_t;

 /* There's no point in continuing when we've detected that we've done
diff --git a/notmuch-config.c b/notmuch-config.c
index 8f1a038..277f197 100644
--- a/notmuch-config.c
+++ b/notmuch-config.c
@@ -43,7 +43,13 @@ static const char new_config_comment[] =
 " The following options are supported here:\n"
 "\n"
 "\ttagsA list (separated by ';') of the tags that will be\n"
-"\tadded to all messages incorporated by \"notmuch new\".\n";
+"\tadded to all messages incorporated by \"notmuch new\".\n"
+"\tprehook A command to be executed before \"notmuch new\" starts\n"
+"\tincorporating new messages. For example, this could be used to 
fetch\n"
+"\tand deliver new messages to the mail directory.\n"
+"\tposthookA command to be executed after \"notmuch new\" has\n"
+"\tincorporated and tagged all new messages. For example, this 
could\n"
+"\tbe used to perform further tagging on new messages.\n";

 static const char user_config_comment[] =
 " User configuration\n"
@@ -615,6 +621,14 @@ notmuch_config_get_hook (notmuch_config_t *config, 
notmuch_hook_t hook)
 const char *group, *key;

 switch (hook) {
+case NOTMUCH_HOOK_PRE_NEW:
+   group = "new";
+   key = "prehook";
+   break;
+case NOTMUCH_HOOK_POST_NEW:
+   group = "new";
+   key = "posthook";
+   break;
 default:
INTERNAL_ERROR ("Unknown hook %d\n.", hook);
 }
diff --git a/notmuch-new.c b/notmuch-new.c
index 0c70e64..09cc3f2 100644
--- a/notmuch-new.c
+++ b/notmuch-new.c
@@ -836,6 +836,7 @@ notmuch_new_command (void *ctx, int argc, char *argv[])
 _filename_node_t *f;
 int i;
 notmuch_bool_t timer_is_active = FALSE;
+int run_hooks = 1;

 add_files_state.verbose = 0;
 add_files_state.output_is_a_tty = isatty (fileno (stdout));
@@ -845,6 +846,8 @@ notmuch_new_command (void *ctx, int argc, char *argv[])
 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
if (STRNCMP_LITERAL (argv[i], "--verbose") == 0) {
add_files_state.verbose = 1;
+   } else if (STRNCMP_LITERAL (argv[i], "--no-hooks") == 0) {
+   run_hooks = 0;
} else {
fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
return 1;
@@ -854,6 +857,9 @@ notmuch_new_command (void *ctx, int argc, char *argv[])
 if (config == NULL)
return 1;

+if (run_hooks && notmuch_run_hook (config, NOTMUCH_HOOK_PRE_NEW))
+   return 1;
+
 add_files_state.new_tags = notmuch_config_get_new_tags (config, 
_files_state.new_tags_length);
 add_files_state.synchronize_flags = 
notmuch_config_get_maildir_synchronize_flags (config);
 db_path = notmuch_config_get_database_path (config);
@@ -1006,5 +1012,8 @@ notmuch_new_command (void *ctx, int argc, char *argv[])

 notmuch_database_close (notmuch);

+if (run_hooks && !interrupted)
+   ret |= notmuch_run_hook (config, NOTMUCH_HOOK_POST_NEW);
+
 return ret || interrupted;
 }
-- 
1.7.5.4



[PATCH 1/2] cli: add mechanism for running user configurable hooks

2011-12-02 Thread Jani Nikula
Add support functions for running hooks configurable in the notmuch config
file. The hooks will be run using system(1).

TODO:

* Move notmuch_run_hook() out of notmuch-new.c. It's there and static only
  because the first user will be there.

* Consider merging this with the following patch, as this is slightly
  artificial as it is.

Signed-off-by: Jani Nikula 
---
 notmuch-client.h |7 +++
 notmuch-config.c |   21 +
 notmuch-new.c|   25 +
 3 files changed, 53 insertions(+), 0 deletions(-)

diff --git a/notmuch-client.h b/notmuch-client.h
index b50cb38..5e2fed2 100644
--- a/notmuch-client.h
+++ b/notmuch-client.h
@@ -87,6 +87,10 @@ typedef struct notmuch_show_params {
 int decrypt;
 } notmuch_show_params_t;

+typedef enum {
+NOTMUCH_HOOK_PLACEHOLDER,
+} notmuch_hook_t;
+
 /* There's no point in continuing when we've detected that we've done
  * something wrong internally (as opposed to the user passing in a
  * bogus value).
@@ -235,6 +239,9 @@ void
 notmuch_config_set_maildir_synchronize_flags (notmuch_config_t *config,
  notmuch_bool_t synchronize_flags);

+const char *
+notmuch_config_get_hook (notmuch_config_t *config, notmuch_hook_t hook);
+
 notmuch_bool_t
 debugger_is_active (void);

diff --git a/notmuch-config.c b/notmuch-config.c
index 1a7ed58..8f1a038 100644
--- a/notmuch-config.c
+++ b/notmuch-config.c
@@ -608,6 +608,27 @@ notmuch_config_set_new_tags (notmuch_config_t *config,
 config->new_tags = NULL;
 }

+const char *
+notmuch_config_get_hook (notmuch_config_t *config, notmuch_hook_t hook)
+{
+char *command;
+const char *group, *key;
+
+switch (hook) {
+default:
+   INTERNAL_ERROR ("Unknown hook %d\n.", hook);
+}
+
+command = g_key_file_get_string (config->key_file, group, key, NULL);
+if (command) {
+   char *p = command;
+   command = talloc_strdup (config, command);
+   free (p);
+}
+
+return command;
+}
+
 /* Given a configuration item of the form . return the
  * component group and key. If any error occurs, print a message on
  * stderr and return 1. Otherwise, return 0.
diff --git a/notmuch-new.c b/notmuch-new.c
index 81a9350..0c70e64 100644
--- a/notmuch-new.c
+++ b/notmuch-new.c
@@ -795,6 +795,31 @@ _remove_directory (void *ctx,
 notmuch_directory_destroy (directory);
 }

+static int
+notmuch_run_hook (notmuch_config_t *config, notmuch_hook_t hook)
+{
+const char *command;
+int r;
+
+command = notmuch_config_get_hook (config, hook);
+if (!command)
+   return NOTMUCH_STATUS_SUCCESS; /* It's okay not to have a hook */
+
+r = system(command);
+if (r) {
+   if (WIFSIGNALED(r))
+   fprintf(stderr, "hook '%s' terminated with signal %d\n",
+   command, WTERMSIG(r));
+   else
+   fprintf(stderr, "hook '%s' failed with status %d\n",
+   command, WEXITSTATUS(r));
+
+   r = NOTMUCH_STATUS_FILE_ERROR; /* FIXME */
+}
+
+return r;
+}
+
 int
 notmuch_new_command (void *ctx, int argc, char *argv[])
 {
-- 
1.7.5.4



[PATCH 1/2] python: add classes to wrap all notmuch_*_t types

2011-12-02 Thread James Westby
On Fri, 02 Dec 2011 09:20:35 -0500, James Westby  
wrote:
> I'll test again to make sure that I have this correct, but my tests
> yesterday certainly suggested that your patches fixed this.

Yep, segfaults a plenty dropping your second patch that go away again
when it is applied once more.

Thanks,

James


[PATCH 1/2] python: add classes to wrap all notmuch_*_t types

2011-12-02 Thread Justus Winter
Quoting James Westby (2011-12-02 00:32:30)
>On Thu, 01 Dec 2011 22:25:41 +0100, Sebastian Spaeth  
>wrote:
>> This strikes me as a rather good thing, so the patches went in.
>
>Hah, I've just seen this, and I'm going to guess that it fixes my
>problems too.
>
>...
>
>I've tested and it seems to work, so my patch is unneeded witht his one.

Huh, strange. My patch isn't supposed to change anything, it just
enables the ctypes package to check whether the functions from
libnotmuch are called with the right parameters, thus preventing
mistakes when changing the python bindings in the future.

Cheers,
Justus
-- next part --
A non-text attachment was scrubbed...
Name: .signature
Type: application/octet-stream
Size: 17 bytes
Desc: not available
URL: 
<http://notmuchmail.org/pipermail/notmuch/attachments/20111202/9430ae7e/attachment.obj>


ANNOUNCE: nottoomuch-addresses.pl

2011-12-02 Thread Tomi Ollila
On Fri, 02 Dec 2011 05:38:01 +1100, Bart Bunting  wrote:
> Hi Tomi,
> 
> I have had a quick try of setting up nottoomuch-addresses addresses.
> 
> Here are a couple of issues I have hit and I think it's still not quite
>  right.
> 
> I attach the shell output from emacs below.
> 
> - firstly it appears that if the term is set to DUMB then the script
> assumes it's being called from emacs and exits if there is no search
> string.  This is problematic for me as I run all my stuff including the
> --update command from a shell.  

Good point. I'll remove that 'optimization' -- mysterious are the ways
users run commands :) (Well, I occasionally run commands from emacs
myself, too).

You could try removing part '''$ENV{TERM} eq 'dumb' or ''' in line 25

> - The instructions don't tell you to create the .config/nottoomuch
> directory.  Perhaps you could check for it's existance and create if
> required?

It does that (in line 91):

mv .config .xconfig
~/mail/nottoomuch/nottoomuch-addresses.pl --update
Creating '/home/too/.config/nottoomuch/addresses'. This may take some time...
Added 3335 addresses in 14 seconds. Total number of addresses: 3335.

It seems your other problems make it seem the directories are not created,
however I check this... 

> - Even when I got this far I had an odd result when running the script
> as shown below.  First time I got a strange error.  Second run it added
> 900 or so addresses.  Subsiquent runs added no more addresses.  I find
> this strange as I have over 180k emails.

Haa, the ls output '0 addresses' tells me something:

rm .config/nottoomuch/addresses
touch .config/nottoomuch/addresses
guru$ ~/mail/nottoomuch/nottoomuch-addresses.pl --update
Updating '/home/too/.config/nottoomuch/addresses', since -604800.
Unrecognized option: -604800..
Added 0 addresses in 0 seconds. Total number of addresses: 0.

> Anyway great work.  Heading in a cool direction.  Hope this somewhat
> rambling report helps.

Thanks a lot for your bug report, it's been very helpful. please retry
doing the following:

TERM=emacs /Users/bart/bin/nottoomuch-addresses.pl --update --rebuild

And report if you get more problems.

> Bart

Tomi

> 
> bart at bit:~$
> bart at bit:~$shasum ~/bin/nottoomuch-addresses.pl 
> 125f3917b3f2dc68bdf30af04ab623d53321e50c  
> /Users/bart/bin/nottoomuch-addresses.pl
> bart at bit:~$chmod 755 ~/bin/nottoomuch-addresses.pl 
> bart at bit:~$~/bin/nottoomuch-addresses.pl  --update
> Cannot open database, maybe not created yet.
> run /Users/bart/bin/nottoomuch-addresses.pl --update from command line first.
> bart at bit:~$cd
> bart at bit:~$cd .config
> bash: cd: .config: No such file or directory
> bart at bit:~$
> bart at bit:~$mkdir .config
> bart at bit:~$nottoomuch --update
> bash: nottoomuch: command not found
> bart at bit:~$~/bin/nottoomuch-addresses.pl  --update
> Cannot open database, maybe not created yet.
> run /Users/bart/bin/nottoomuch-addresses.pl --update from command line first.
> bart at bit:~$
> bart at bit:~$cd
> bart at bit:~$cd .config
> bart at bit:~/.config$mkdir nottoomuch
> bart at bit:~/.config$
> bart at bit:~/.config$~/bin/nottoomuch-addresses.pl  --update
> Cannot open database, maybe not created yet.
> run /Users/bart/bin/nottoomuch-addresses.pl --update from command line first.
> bart at bit:~/.config$cd nottoomuch/
> bart at bit:~/.config/nottoomuch$touch addresses
> bart at bit:~/.config/nottoomuch$~/bin/nottoomuch-addresses.pl  --update
> bart at bit:~/.config/nottoomuch$ls
> addresses
> bart at bit:~/.config/nottoomuch$ls -s addresses 
> 0 addresses
> bart at bit:~/.config/nottoomuch$echo $TERM
> dumb
> bart at bit:~/.config/nottoomuch$export TERM=emacs
> bart at bit:~/.config/nottoomuch$ls -s addresses 
> 0 addresses
> bart at bit:~/.config/nottoomuch$~/bin/nottoomuch-addresses.pl  --update
> Updating '/Users/bart/.config/nottoomuch/addresses', since -604800.
> Unrecognized option: -604800..
> Added 0 addresses in 0 seconds. Total number of addresses: 0.
> bart at bit:~/.config/nottoomuch$~/bin/nottoomuch-addresses.pl  --update
> Updating '/Users/bart/.config/nottoomuch/addresses', since 1322159542.
> Added 993 addresses in 4 seconds. Total number of addresses: 993.
> bart at bit:~/.config/nottoomuch$~/bin/nottoomuch-addresses.pl  --update
> Updating '/Users/bart/.config/nottoomuch/addresses', since 1322159560.
> Added 0 addresses in 1 seconds. Total number of addresses: 993.
> bart at bit:~/.config/nottoomuch$ On Thu, 01 Dec 2011 17:36:24 +0200, Tomi 
> Ollila  wrote:


[PATCH 1/2] python: add classes to wrap all notmuch_*_t types

2011-12-02 Thread James Westby
On Fri, 02 Dec 2011 13:35:11 +0100, Justus Winter <4winter at 
informatik.uni-hamburg.de> wrote:
> Huh, strange. My patch isn't supposed to change anything, it just
> enables the ctypes package to check whether the functions from
> libnotmuch are called with the right parameters, thus preventing
> mistakes when changing the python bindings in the future.

Where .restype is set to c_void_p ctypes spots this and returns it as
int32. Where it's set to another callable (e.g. your custom types) it
actually wraps the value by passing it to the callable and returning the
result.

Then, because your custom types are pointers, it stores them in an
appropriate value, and also stops the storage being reused.

I'll test again to make sure that I have this correct, but my tests
yesterday certainly suggested that your patches fixed this.

Thanks,

James


ANNOUNCE: nottoomuch-addresses.pl

2011-12-02 Thread Bart Bunting
Hi Tomi,

I have had a quick try of setting up nottoomuch-addresses addresses.

Here are a couple of issues I have hit and I think it's still not quite
 right.

I attach the shell output from emacs below.

- firstly it appears that if the term is set to DUMB then the script
assumes it's being called from emacs and exits if there is no search
string.  This is problematic for me as I run all my stuff including the
--update command from a shell.  
- The instructions don't tell you to create the .config/nottoomuch
directory.  Perhaps you could check for it's existance and create if
required?
- Even when I got this far I had an odd result when running the script
as shown below.  First time I got a strange error.  Second run it added
900 or so addresses.  Subsiquent runs added no more addresses.  I find
this strange as I have over 180k emails.

Anyway great work.  Heading in a cool direction.  Hope this somewhat
rambling report helps.

Bart

bart at bit:~$
bart at bit:~$shasum ~/bin/nottoomuch-addresses.pl 
125f3917b3f2dc68bdf30af04ab623d53321e50c  
/Users/bart/bin/nottoomuch-addresses.pl
bart at bit:~$chmod 755 ~/bin/nottoomuch-addresses.pl 
bart at bit:~$~/bin/nottoomuch-addresses.pl  --update
Cannot open database, maybe not created yet.
run /Users/bart/bin/nottoomuch-addresses.pl --update from command line first.
bart at bit:~$cd
bart at bit:~$cd .config
bash: cd: .config: No such file or directory
bart at bit:~$
bart at bit:~$mkdir .config
bart at bit:~$nottoomuch --update
bash: nottoomuch: command not found
bart at bit:~$~/bin/nottoomuch-addresses.pl  --update
Cannot open database, maybe not created yet.
run /Users/bart/bin/nottoomuch-addresses.pl --update from command line first.
bart at bit:~$
bart at bit:~$cd
bart at bit:~$cd .config
bart at bit:~/.config$mkdir nottoomuch
bart at bit:~/.config$
bart at bit:~/.config$~/bin/nottoomuch-addresses.pl  --update
Cannot open database, maybe not created yet.
run /Users/bart/bin/nottoomuch-addresses.pl --update from command line first.
bart at bit:~/.config$cd nottoomuch/
bart at bit:~/.config/nottoomuch$touch addresses
bart at bit:~/.config/nottoomuch$~/bin/nottoomuch-addresses.pl  --update
bart at bit:~/.config/nottoomuch$ls
addresses
bart at bit:~/.config/nottoomuch$ls -s addresses 
0 addresses
bart at bit:~/.config/nottoomuch$echo $TERM
dumb
bart at bit:~/.config/nottoomuch$export TERM=emacs
bart at bit:~/.config/nottoomuch$ls -s addresses 
0 addresses
bart at bit:~/.config/nottoomuch$~/bin/nottoomuch-addresses.pl  --update
Updating '/Users/bart/.config/nottoomuch/addresses', since -604800.
Unrecognized option: -604800..
Added 0 addresses in 0 seconds. Total number of addresses: 0.
bart at bit:~/.config/nottoomuch$~/bin/nottoomuch-addresses.pl  --update
Updating '/Users/bart/.config/nottoomuch/addresses', since 1322159542.
Added 993 addresses in 4 seconds. Total number of addresses: 993.
bart at bit:~/.config/nottoomuch$~/bin/nottoomuch-addresses.pl  --update
Updating '/Users/bart/.config/nottoomuch/addresses', since 1322159560.
Added 0 addresses in 1 seconds. Total number of addresses: 993.
bart at bit:~/.config/nottoomuch$ On Thu, 01 Dec 2011 17:36:24 +0200, Tomi 
Ollila  wrote:


[PATCH] test: cleanup gdb external dependency in atomicity tests

2011-12-02 Thread Dmitry Kurochkin
Change atomicity tests to use the new external binary dependencies.
This simplifies the code and makes output consistent.
---
 test/atomicity |   12 
 1 files changed, 4 insertions(+), 8 deletions(-)

diff --git a/test/atomicity b/test/atomicity
index ad7d4a3..6df0a00 100755
--- a/test/atomicity
+++ b/test/atomicity
@@ -7,8 +7,7 @@ test_description='atomicity'
 # final database contents should be the same regardless of when (or
 # if) it is killed and restarted.

-if which gdb 1>/dev/null 2>&1; then
-test_set_prereq GDB
+if test_require_external_prereq gdb; then

 # Create a maildir structure to also stress flag synchronization
 mkdir $MAIL_DIR/cur
@@ -91,14 +90,11 @@ if which gdb 1>/dev/null 2>&1; then
i=$(expr $end - 1)
fi
 done
-else
-say_color info "%-6s" "WARNING"
-echo " Missing test prerequisite GDB"
-fi  
+fi

 test_begin_subtest '"notmuch new" is idempotent under arbitrary aborts'
-test_expect_equal_file GDB searchall expectall
+test_expect_equal_file searchall expectall

-test_expect_success GDB "detected $outcount>10 abort points" "test $outcount 
-gt 10"
+test_expect_success "detected $outcount>10 abort points" "test $outcount -gt 
10"

 test_done
-- 
1.7.7.3



Re: [PATCH 1/2] python: add classes to wrap all notmuch_*_t types

2011-12-02 Thread Justus Winter
Quoting James Westby (2011-12-02 00:32:30)
On Thu, 01 Dec 2011 22:25:41 +0100, Sebastian Spaeth sebast...@sspaeth.de 
wrote:
 This strikes me as a rather good thing, so the patches went in.

Hah, I've just seen this, and I'm going to guess that it fixes my
problems too.

...

I've tested and it seems to work, so my patch is unneeded witht his one.

Huh, strange. My patch isn't supposed to change anything, it just
enables the ctypes package to check whether the functions from
libnotmuch are called with the right parameters, thus preventing
mistakes when changing the python bindings in the future.

Cheers,
Justus

.signature
Description: Binary data
___
notmuch mailing list
notmuch@notmuchmail.org
http://notmuchmail.org/mailman/listinfo/notmuch


Re: [PATCH 1/2] python: add classes to wrap all notmuch_*_t types

2011-12-02 Thread James Westby
On Fri, 02 Dec 2011 13:35:11 +0100, Justus Winter 
4win...@informatik.uni-hamburg.de wrote:
 Huh, strange. My patch isn't supposed to change anything, it just
 enables the ctypes package to check whether the functions from
 libnotmuch are called with the right parameters, thus preventing
 mistakes when changing the python bindings in the future.

Where .restype is set to c_void_p ctypes spots this and returns it as
int32. Where it's set to another callable (e.g. your custom types) it
actually wraps the value by passing it to the callable and returning the
result.

Then, because your custom types are pointers, it stores them in an
appropriate value, and also stops the storage being reused.

I'll test again to make sure that I have this correct, but my tests
yesterday certainly suggested that your patches fixed this.

Thanks,

James
___
notmuch mailing list
notmuch@notmuchmail.org
http://notmuchmail.org/mailman/listinfo/notmuch


[PATCH 1/2] cli: add mechanism for running user configurable hooks

2011-12-02 Thread Jani Nikula
Add support functions for running hooks configurable in the notmuch config
file. The hooks will be run using system(1).

TODO:

* Move notmuch_run_hook() out of notmuch-new.c. It's there and static only
  because the first user will be there.

* Consider merging this with the following patch, as this is slightly
  artificial as it is.

Signed-off-by: Jani Nikula j...@nikula.org
---
 notmuch-client.h |7 +++
 notmuch-config.c |   21 +
 notmuch-new.c|   25 +
 3 files changed, 53 insertions(+), 0 deletions(-)

diff --git a/notmuch-client.h b/notmuch-client.h
index b50cb38..5e2fed2 100644
--- a/notmuch-client.h
+++ b/notmuch-client.h
@@ -87,6 +87,10 @@ typedef struct notmuch_show_params {
 int decrypt;
 } notmuch_show_params_t;
 
+typedef enum {
+NOTMUCH_HOOK_PLACEHOLDER,
+} notmuch_hook_t;
+
 /* There's no point in continuing when we've detected that we've done
  * something wrong internally (as opposed to the user passing in a
  * bogus value).
@@ -235,6 +239,9 @@ void
 notmuch_config_set_maildir_synchronize_flags (notmuch_config_t *config,
  notmuch_bool_t synchronize_flags);
 
+const char *
+notmuch_config_get_hook (notmuch_config_t *config, notmuch_hook_t hook);
+
 notmuch_bool_t
 debugger_is_active (void);
 
diff --git a/notmuch-config.c b/notmuch-config.c
index 1a7ed58..8f1a038 100644
--- a/notmuch-config.c
+++ b/notmuch-config.c
@@ -608,6 +608,27 @@ notmuch_config_set_new_tags (notmuch_config_t *config,
 config-new_tags = NULL;
 }
 
+const char *
+notmuch_config_get_hook (notmuch_config_t *config, notmuch_hook_t hook)
+{
+char *command;
+const char *group, *key;
+
+switch (hook) {
+default:
+   INTERNAL_ERROR (Unknown hook %d\n., hook);
+}
+
+command = g_key_file_get_string (config-key_file, group, key, NULL);
+if (command) {
+   char *p = command;
+   command = talloc_strdup (config, command);
+   free (p);
+}
+
+return command;
+}
+
 /* Given a configuration item of the form group.key return the
  * component group and key. If any error occurs, print a message on
  * stderr and return 1. Otherwise, return 0.
diff --git a/notmuch-new.c b/notmuch-new.c
index 81a9350..0c70e64 100644
--- a/notmuch-new.c
+++ b/notmuch-new.c
@@ -795,6 +795,31 @@ _remove_directory (void *ctx,
 notmuch_directory_destroy (directory);
 }
 
+static int
+notmuch_run_hook (notmuch_config_t *config, notmuch_hook_t hook)
+{
+const char *command;
+int r;
+
+command = notmuch_config_get_hook (config, hook);
+if (!command)
+   return NOTMUCH_STATUS_SUCCESS; /* It's okay not to have a hook */
+
+r = system(command);
+if (r) {
+   if (WIFSIGNALED(r))
+   fprintf(stderr, hook '%s' terminated with signal %d\n,
+   command, WTERMSIG(r));
+   else
+   fprintf(stderr, hook '%s' failed with status %d\n,
+   command, WEXITSTATUS(r));
+
+   r = NOTMUCH_STATUS_FILE_ERROR; /* FIXME */
+}
+
+return r;
+}
+
 int
 notmuch_new_command (void *ctx, int argc, char *argv[])
 {
-- 
1.7.5.4

___
notmuch mailing list
notmuch@notmuchmail.org
http://notmuchmail.org/mailman/listinfo/notmuch


[PATCH 2/2] cli: add support for running notmuch new pre and post hooks

2011-12-02 Thread Jani Nikula
Run notmuch new pre and post hooks if specified in the notmuch config
file. The hooks will be run before and after incorporating new messages to
the database. Also add command line option --no-hooks to notmuch new to
bypass the hooks.

With this patch, you can add hooks in your config, for example:

[new]
prehook=offlineimap
posthook=my-tagging-script

As the value is passed to system(1), you can actually have multiple
commands, redirections, pipes, etc. in there. If the tagging is simple
enough, you can just add the tagging in-line without a script.

TODO:

* Tests.

* Documentation (manpage and help).

Signed-off-by: Jani Nikula j...@nikula.org
---
 notmuch-client.h |3 ++-
 notmuch-config.c |   16 +++-
 notmuch-new.c|9 +
 3 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/notmuch-client.h b/notmuch-client.h
index 5e2fed2..d2ebc73 100644
--- a/notmuch-client.h
+++ b/notmuch-client.h
@@ -88,7 +88,8 @@ typedef struct notmuch_show_params {
 } notmuch_show_params_t;
 
 typedef enum {
-NOTMUCH_HOOK_PLACEHOLDER,
+NOTMUCH_HOOK_PRE_NEW,
+NOTMUCH_HOOK_POST_NEW,
 } notmuch_hook_t;
 
 /* There's no point in continuing when we've detected that we've done
diff --git a/notmuch-config.c b/notmuch-config.c
index 8f1a038..277f197 100644
--- a/notmuch-config.c
+++ b/notmuch-config.c
@@ -43,7 +43,13 @@ static const char new_config_comment[] =
  The following options are supported here:\n
 \n
 \ttagsA list (separated by ';') of the tags that will be\n
-\tadded to all messages incorporated by \notmuch new\.\n;
+\tadded to all messages incorporated by \notmuch new\.\n
+\tprehook A command to be executed before \notmuch new\ starts\n
+\tincorporating new messages. For example, this could be used to 
fetch\n
+\tand deliver new messages to the mail directory.\n
+\tposthookA command to be executed after \notmuch new\ has\n
+\tincorporated and tagged all new messages. For example, this 
could\n
+\tbe used to perform further tagging on new messages.\n;
 
 static const char user_config_comment[] =
  User configuration\n
@@ -615,6 +621,14 @@ notmuch_config_get_hook (notmuch_config_t *config, 
notmuch_hook_t hook)
 const char *group, *key;
 
 switch (hook) {
+case NOTMUCH_HOOK_PRE_NEW:
+   group = new;
+   key = prehook;
+   break;
+case NOTMUCH_HOOK_POST_NEW:
+   group = new;
+   key = posthook;
+   break;
 default:
INTERNAL_ERROR (Unknown hook %d\n., hook);
 }
diff --git a/notmuch-new.c b/notmuch-new.c
index 0c70e64..09cc3f2 100644
--- a/notmuch-new.c
+++ b/notmuch-new.c
@@ -836,6 +836,7 @@ notmuch_new_command (void *ctx, int argc, char *argv[])
 _filename_node_t *f;
 int i;
 notmuch_bool_t timer_is_active = FALSE;
+int run_hooks = 1;
 
 add_files_state.verbose = 0;
 add_files_state.output_is_a_tty = isatty (fileno (stdout));
@@ -845,6 +846,8 @@ notmuch_new_command (void *ctx, int argc, char *argv[])
 for (i = 0; i  argc  argv[i][0] == '-'; i++) {
if (STRNCMP_LITERAL (argv[i], --verbose) == 0) {
add_files_state.verbose = 1;
+   } else if (STRNCMP_LITERAL (argv[i], --no-hooks) == 0) {
+   run_hooks = 0;
} else {
fprintf (stderr, Unrecognized option: %s\n, argv[i]);
return 1;
@@ -854,6 +857,9 @@ notmuch_new_command (void *ctx, int argc, char *argv[])
 if (config == NULL)
return 1;
 
+if (run_hooks  notmuch_run_hook (config, NOTMUCH_HOOK_PRE_NEW))
+   return 1;
+
 add_files_state.new_tags = notmuch_config_get_new_tags (config, 
add_files_state.new_tags_length);
 add_files_state.synchronize_flags = 
notmuch_config_get_maildir_synchronize_flags (config);
 db_path = notmuch_config_get_database_path (config);
@@ -1006,5 +1012,8 @@ notmuch_new_command (void *ctx, int argc, char *argv[])
 
 notmuch_database_close (notmuch);
 
+if (run_hooks  !interrupted)
+   ret |= notmuch_run_hook (config, NOTMUCH_HOOK_POST_NEW);
+
 return ret || interrupted;
 }
-- 
1.7.5.4

___
notmuch mailing list
notmuch@notmuchmail.org
http://notmuchmail.org/mailman/listinfo/notmuch


Re: [PATCH 1/2] cli: add mechanism for running user configurable hooks

2011-12-02 Thread Jani Nikula
On Fri,  2 Dec 2011 23:00:05 +0200, Jani Nikula j...@nikula.org wrote:
 TODO:

I meant these as RFC, but forgot to add the subject prefix.

Jani.
___
notmuch mailing list
notmuch@notmuchmail.org
http://notmuchmail.org/mailman/listinfo/notmuch


Re: [PATCH 1/2] python: add classes to wrap all notmuch_*_t types

2011-12-02 Thread James Westby
On Fri, 02 Dec 2011 09:20:35 -0500, James Westby jw+deb...@jameswestby.net 
wrote:
 I'll test again to make sure that I have this correct, but my tests
 yesterday certainly suggested that your patches fixed this.

Yep, segfaults a plenty dropping your second patch that go away again
when it is applied once more.

Thanks,

James
___
notmuch mailing list
notmuch@notmuchmail.org
http://notmuchmail.org/mailman/listinfo/notmuch


Re: [PATCH 1/2] python: add classes to wrap all notmuch_*_t types

2011-12-02 Thread Justus Winter
Quoting James Westby (2011-12-03 00:24:18)
On Fri, 02 Dec 2011 09:20:35 -0500, James Westby jw+deb...@jameswestby.net 
wrote:
 I'll test again to make sure that I have this correct, but my tests
 yesterday certainly suggested that your patches fixed this.

Yep, segfaults a plenty dropping your second patch that go away again
when it is applied once more.

Happy to hear that :)

Justus

.signature
Description: Binary data
___
notmuch mailing list
notmuch@notmuchmail.org
http://notmuchmail.org/mailman/listinfo/notmuch