diff --git a/README.md b/README.md index 1579805..9aca25d 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" ]; ``` diff --git a/bin/imapnotify b/bin/imapnotify index 5ee657c..71360c7 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,6 +58,16 @@ Notifier.prototype.add_box = function(box, cb, debug) { delimiter = connection.namespaces.personal[0].delimiter; } + //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) { if (err) throw err; function addConnectionListener(event, message) { @@ -240,10 +250,13 @@ 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 ? postCommand : ''), formattedBox); return executeCommands(formattedCommand, formattedPostCommand) } } +function executeOnStartup(config) { + return (config.executeOnStartup || false); +} function runOnSig(signal, config, cb) { signal = 'SIG' + signal @@ -275,13 +288,13 @@ 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() { 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)}) }) }) @@ -289,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)}) }) }) 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