Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add executeOnStartup functionality #27

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
Expand Down Expand Up @@ -82,6 +86,7 @@ follows. Assuming the script ~/getpass.sh prints out your password.
exports.password = getStdout("~/getpass.sh");
exports.onNotify = "<sync command>"
exports.onNotifyPost = "<command>"
exports.executeOnStartup = true
exports.boxes = [ "box1", "box2", "some/other/box" ];
```

Expand Down
23 changes: 18 additions & 5 deletions bin/imapnotify
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -275,21 +288,21 @@ 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)})
})
})

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)})
})
})
Expand Down
12 changes: 12 additions & 0 deletions [email protected]
Original file line number Diff line number Diff line change
@@ -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