Skip to content

Instantly share code, notes, and snippets.

@syranez
Created October 7, 2011 19:07
Show Gist options
  • Save syranez/1271112 to your computer and use it in GitHub Desktop.
Save syranez/1271112 to your computer and use it in GitHub Desktop.
added a dependencies section
#!/bin/bash
#
# connects to a remote host and counts the unseen entries on an imap box
#+
#+ Usage: ./imap_unseen.sh [host] [mailbox]
#+
#+ Dependencies:
#+ - on the localhost: ssh
#+ - on the remote host: ssh, dovecot
# host to connect to
IU_HOST="localhost";
# use first parameter als IU_HOST
if [ ! -z "$1" ]; then
IU_HOST="$1";
fi;
# Inbox to check
IU_INBOX="INBOX";
# use second parameter as IU_INBOX
if [ ! -z "$2" ]; then
IU_INBOX="$2";
fi;
# sources an script, that adds an ssh-key to your keychain.
#+ you have to write that script
IU_SSH_KEY="";
# unread entries in the inbox
IU_UNSEEN_COUNT=0;
# cleans the namespace
cleanup () {
unset IU_HOST;
unset IU_INBOX;
unset IU_SSH_KEY;
unset IU_UNSEEN_COUNT;
}
# reads the unseen entries
#+ result ist available throug $COUNT
#+ @param $1 host
#+ @param $2 inbox
#+ @param $3 sshkeychain
imap_unseen_count () {
if [ -z $1 ]; then
echo "Parameter host is missing. Fial";
exit 1;
fi
local host="${1}";
if [ -z "$2" ]; then
echo "Parameter inbox is missing. Fial";
exit 1;
fi
local inbox="${2}";
if [ -z "$3" ]; then
echo "Parameter sshkeychain is missing. Fial";
exit 1;
fi
local sshkeychain="${3}";
local imap_command='? status '"$inbox"' (unseen)';
# todo: test the existence of the host
if [ -f "$sshkeychain" ]; then
. "$sshkeychain";
fi
local imap_status=$(printf '%s' "${imap_command}" |
ssh -q $host /usr/sbin/dovecot --exec-mail imap);
IU_UNSEEN_COUNT=$(printf '%s' "$imap_status" |
grep UNSEEN |
awk '{ print substr($5,0,length($5) - 2); }');
return 0;
}
imap_unseen_count "$IU_HOST" "$IU_INBOX" "$IU_SSH_KEY";
if [ $? -eq 1 ]; then
echo "Could not get the unseen mail count. Sorry";
cleanup;
exit 1;
fi;
printf '%s\n' $IU_UNSEEN_COUNT;
cleanup;
exit 0
@syranez
Copy link
Author

syranez commented Oct 7, 2011

Original author is http://github.com/rtwo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment