From b4842e8d22929fffd378de807af88cffca3e4a64 Mon Sep 17 00:00:00 2001 From: Thibaut Meyer Date: Wed, 19 Feb 2020 11:41:21 +0100 Subject: [PATCH 1/9] add executeOnStartup option --- bin/imapnotify | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/bin/imapnotify b/bin/imapnotify index 5ee657c..37aa5bf 100755 --- a/bin/imapnotify +++ b/bin/imapnotify @@ -35,7 +35,7 @@ function Notifier(config) { this.logger = logger.child({'server': this.config.server}) } -Notifier.prototype.add_box = function(box, cb, debug) { +Notifier.prototype.add_box = function(box, cb, executeOnStartup, debug) { if (typeof cb !== 'function') { cb = function() {} } @@ -58,7 +58,17 @@ Notifier.prototype.add_box = function(box, cb, debug) { delimiter = connection.namespaces.personal[0].delimiter; } - connection.openBox(replace(box, '/', delimiter), true, function(err) { + //retrieve status information about the box before opening it + if (executeOnStartup) { + connection.status(replace(box, '/', delimiter), function(err, boxStatus) { + if (err) throw err; + if (boxStatus.messages.unseen > 0) { + cb(box, 'mail'); + } + }); + } + + connection.openBox(replace(box, '/', delimiter), true, function(err, openedBox) { if (err) throw err; function addConnectionListener(event, message) { connection.on(event, function() { @@ -244,6 +254,9 @@ function executeOnNotify(config) { return executeCommands(formattedCommand, formattedPostCommand) } } +function executeOnStartup(config) { + return (config.executeOnStartup || false); +} function runOnSig(signal, config, cb) { signal = 'SIG' + signal @@ -275,7 +288,7 @@ function main(cb) { var notifier = new Notifier(config); config.boxes.forEach(function (box) { - notifier.add_box(box, executeOnNotify(config), process.env['DEBUG'] === 'imap' ? logger.debug : null) + notifier.add_box(box, executeOnNotify(config), executeOnStartup(config), process.env['DEBUG'] === 'imap' ? logger.debug : null) }) process.on('SIGINT', function() { From f17c547cefdcb44e835a9853b398e67cc3b7af4e Mon Sep 17 00:00:00 2001 From: Thibaut Meyer Date: Wed, 19 Feb 2020 11:47:30 +0100 Subject: [PATCH 2/9] update README to reflect executeOnStartup --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1579805..f2e883e 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ Execute scripts on IMAP mailbox changes (new/deleted/updated messages) using IDL "password": "", "onNotify": "/usr/bin/mbsync test-%s", "onNotifyPost": {"mail": "/usr/bin/notmuch new && notify-send 'New mail arrived'"}, + "executeOnStartup": true, "boxes": [ "box1", @@ -27,10 +28,13 @@ Execute scripts on IMAP mailbox changes (new/deleted/updated messages) using IDL [string]: shell command to run on any event [object]: shell commands to run on for each type of event keys: "mail" for new mail, "update" for existing messages updates, "expunge" for messages deletions - onNotifyPost: + onNotifyPost: [string]: shell command to run after onNotify event [object]: shell commands to run after onNotify for each type of event keys: "mail" for new mail, "update" for existing messages updates, "expunge" for messages deletions + executeOnStartup: + [boolean]: execute the onNotify and onNotifyPost shell command at startup as + well, if new mail is available (execute the command for the 'mail' event) ``` ### extra options @@ -82,6 +86,7 @@ follows. Assuming the script ~/getpass.sh prints out your password. exports.password = getStdout("~/getpass.sh"); exports.onNotify = "" exports.onNotifyPost = "" + exports.executeOnStartup = true exports.boxes = [ "box1", "box2", "some/other/box" ]; ``` From b0ea9ab72bdd3b2f52440f6ac9818eeafdb9901b Mon Sep 17 00:00:00 2001 From: Thibaut Meyer Date: Wed, 19 Feb 2020 11:54:11 +0100 Subject: [PATCH 3/9] reverted unneeded change --- bin/imapnotify | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/imapnotify b/bin/imapnotify index 37aa5bf..9b31699 100755 --- a/bin/imapnotify +++ b/bin/imapnotify @@ -68,7 +68,7 @@ Notifier.prototype.add_box = function(box, cb, executeOnStartup, debug) { }); } - connection.openBox(replace(box, '/', delimiter), true, function(err, openedBox) { + connection.openBox(replace(box, '/', delimiter), true, function(err) { if (err) throw err; function addConnectionListener(event, message) { connection.on(event, function() { From febd299c3c2739c36610c66438800664f65a5af4 Mon Sep 17 00:00:00 2001 From: Thibaut Meyer Date: Wed, 19 Feb 2020 11:56:53 +0100 Subject: [PATCH 4/9] corrected typos --- bin/imapnotify | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/imapnotify b/bin/imapnotify index 9b31699..f59ccd5 100755 --- a/bin/imapnotify +++ b/bin/imapnotify @@ -294,7 +294,7 @@ function main(cb) { process.on('SIGINT', function() { logger.info('Caught Ctrl-C, exiting...') notifier.stop(function() { - logger.info('imap-inotify stoped') + logger.info('imap-inotify stopped') runOnSig('INT', config, function() {process.exit(0)}) }) }) @@ -302,7 +302,7 @@ function main(cb) { process.on('SIGTERM', function() { logger.info('Caught SIGTERM, exiting...') notifier.stop(function() { - logger.info('imap-inotify stoped') + logger.info('imap-inotify stopped') runOnSig('TERM', config, function() {process.exit(2)}) }) }) From 79cd83480e480548fd777aa17f9e3ea3d9c12a76 Mon Sep 17 00:00:00 2001 From: Thibaut Meyer Date: Wed, 19 Feb 2020 11:58:06 +0100 Subject: [PATCH 5/9] fix post command execution --- bin/imapnotify | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/imapnotify b/bin/imapnotify index f59ccd5..d40416c 100755 --- a/bin/imapnotify +++ b/bin/imapnotify @@ -250,7 +250,7 @@ function executeOnNotify(config) { return function notify(box, event) { var formattedBox = replace(box.toLowerCase(), '/', '-') , formattedCommand = printf(commandIsEventMap ? command[event] || '': command, formattedBox) - , formattedPostCommand = printf(postCommandIsEventMap ? postCommand[event] || '': command, formattedBox) + , formattedPostCommand = printf(postCommandIsEventMap ? postCommand[event] || '': postCommand, formattedBox) return executeCommands(formattedCommand, formattedPostCommand) } } From 7336740ead3bd879815f4350f2fa417454723924 Mon Sep 17 00:00:00 2001 From: Thibaut Meyer Date: Sat, 29 Feb 2020 13:11:11 +0100 Subject: [PATCH 6/9] readme formatting --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f2e883e..32ffb52 100644 --- a/README.md +++ b/README.md @@ -33,8 +33,8 @@ Execute scripts on IMAP mailbox changes (new/deleted/updated messages) using IDL [object]: shell commands to run after onNotify for each type of event keys: "mail" for new mail, "update" for existing messages updates, "expunge" for messages deletions executeOnStartup: - [boolean]: execute the onNotify and onNotifyPost shell command at startup as - well, if new mail is available (execute the command for the 'mail' event) + [boolean]: execute the onNotify and onNotifyPost shell command at startup as + well, if new mail is available (execute the command for the 'mail' event) ``` ### extra options From ae1ac7c8bf0edcd9db604e0eaa8324ab748c62bc Mon Sep 17 00:00:00 2001 From: Thibaut Meyer Date: Sat, 29 Feb 2020 13:11:43 +0100 Subject: [PATCH 7/9] readme formatting --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 32ffb52..9aca25d 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ Execute scripts on IMAP mailbox changes (new/deleted/updated messages) using IDL keys: "mail" for new mail, "update" for existing messages updates, "expunge" for messages deletions executeOnStartup: [boolean]: execute the onNotify and onNotifyPost shell command at startup as - well, if new mail is available (execute the command for the 'mail' event) + well, if new mail is available (execute the command for the 'mail' event) ``` ### extra options From 1ae3593ccef9bbfdc16a1ec3d1dd15b2dce3f35e Mon Sep 17 00:00:00 2001 From: Thibaut Meyer Date: Wed, 25 Mar 2020 15:46:52 +0100 Subject: [PATCH 8/9] bug fix: doesn't crash when not using post command --- bin/imapnotify | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/imapnotify b/bin/imapnotify index d40416c..71360c7 100755 --- a/bin/imapnotify +++ b/bin/imapnotify @@ -250,7 +250,7 @@ function executeOnNotify(config) { return function notify(box, event) { var formattedBox = replace(box.toLowerCase(), '/', '-') , formattedCommand = printf(commandIsEventMap ? command[event] || '': command, formattedBox) - , formattedPostCommand = printf(postCommandIsEventMap ? postCommand[event] || '': postCommand, formattedBox) + , formattedPostCommand = printf(postCommandIsEventMap ? postCommand[event] || '' : (postCommand ? postCommand : ''), formattedBox); return executeCommands(formattedCommand, formattedPostCommand) } } From f8867151c291576ccee40a5e2716597a759cd1c1 Mon Sep 17 00:00:00 2001 From: Thibaut Meyer Date: Fri, 16 Sep 2022 20:50:32 +0200 Subject: [PATCH 9/9] adds service file --- imapnotify@.service | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 imapnotify@.service diff --git a/imapnotify@.service b/imapnotify@.service new file mode 100644 index 0000000..2624c71 --- /dev/null +++ b/imapnotify@.service @@ -0,0 +1,12 @@ +[Unit] +Description=Execute scripts on new messages using IDLE imap command +After=network.target + +[Service] +Type=simple +ExecStart=/home/user/node-imapnotify/bin/imapnotify -c %h/.config/imapnotify/%I +Restart=on-failure +RestartSec=20 + +[Install] +WantedBy=default.target