X-Git-Url: https://defaria.com/gitweb/?a=blobdiff_plain;f=bin%2FannounceEmail.pl;h=cf3dc499fe8b4aaf255d3359fef2e21961040e51;hb=dddc679d2bee64978cc936d70d6c05ae88969232;hp=f90426ebf2f92137c46e70f01e1da7fdb4aed3be;hpb=ded8d315fd3f6c050f759865b09f053ae85e52a5;p=clearscm.git diff --git a/bin/announceEmail.pl b/bin/announceEmail.pl index f90426e..cf3dc49 100755 --- a/bin/announceEmail.pl +++ b/bin/announceEmail.pl @@ -31,23 +31,32 @@ $Date: 2019/04/04 13:40:10 $ =head1 SYNOPSIS - Usage: announceEmail.pl [-usa|ge] [-h|elp] [-v|erbose] [-de|bug] [-da|emon] + Usage: announceEmail.pl [-usa|ge] [-h|elp] [-v|erbose] [-de|bug] [-use|rname ] [-p|assword ] - [-i|map ] + [-an|nouce] [-ap|pend] [-da|emon] [-n|name ] + [-uses|sl] [-useb|locking] Where: - -usa|ge: Print this usage - -h|elp: Detailed help - -v|erbose: Verbose mode (Default: -verbose) - -de|bug: Turn on debugging (Default: Off) - -da|emon: Run in daemon mode (Default: -daemon) - -user|name: User name to log in with (Default: $USER) - -p|assword: Password to use (Default: prompted) - -n|ame: Name of account (Default: imap) - -i|map: IMAP server to talk to (Default: defaria.com) - -uses|sl: Whether or not to use SSL to connect (Default: False) + -usa|ge Print this usage + -h|elp Detailed help + -v|erbose Verbose mode (Default: Not verbose) + -de|bug Turn on debugging (Default: Off) + + -user|name User name to log in with (Default: $USER) + -p|assword Password to use (Default: prompted) + -i|map IMAP server to talk to (Default: defaria.com) + + -an|nounce Announce startup (Default: False) + -ap|pend Append to logfile (Default: Noappend) + -da|emon Run in daemon mode (Default: -daemon) + -n|ame Name of account (Default: imap) + -uses|sl Whether or not to use SSL to connect (Default: False) -useb|locking Whether to block on socket (Default: False) - -a-nnounce Announce startup (Default: False) + + Signals: + $SIG{USR1}: Toggles debug option + $SIG{USR2}: Reestablishes connection to IMAP server =head1 DESCRIPTION @@ -93,8 +102,12 @@ my @greetings = ( 'I was looking in your inbox and found a message', 'Not sure you want to hear this message', 'Good news', + "What's this? A new message", ); +my $icon = '/home/andrew/.icons/Thunderbird.jpg'; +my $timeout = 5 * 1000; + my %opts = ( usage => sub { pod2usage }, help => sub { pod2usage(-verbose => 2)}, @@ -104,24 +117,36 @@ my %opts = ( username => $ENV{USER}, password => $ENV{PASSWORD}, imap => $defaultIMAPServer, - usessl => 0, - useblocking => 0, - announce => 0, ); +sub notify($) { + my ($msg) = @_; + + my $cmd = "notify-send -i $icon -t $timeout '$msg'"; + + Execute $cmd; + + return; +} # notify + sub interrupted { if (get_debug) { - $log->msg("Turning off debugging"); + notify 'Turning off debugging'; set_debug 0; } else { - $log->msg("Turning on debugging"); + notify ('Turning on debugging'); set_debug 1; } # if return; } # interrupted +sub restart; + +sub Connect2IMAP; + $SIG{USR1} = \&interrupted; +$SIG{USR2} = \&restart; sub unseenMsgs() { $IMAP->select('inbox') or @@ -133,6 +158,9 @@ sub unseenMsgs() { sub Connect2IMAP() { $log->dbug("Connecting to $opts{imap} as $opts{username}"); + # Destroy any old connections + undef $IMAP; + $IMAP = Mail::IMAPTalk->new( Server => $opts{imap}, Username => $opts{username}, @@ -155,18 +183,21 @@ sub Connect2IMAP() { sub MonitorMail() { MONITORMAIL: + $log->dbug("Top of MonitorMail loop"); # First close and reselect the INBOX to get its current status $IMAP->close; $IMAP->select('INBOX') or $log->err("Unable to select INBOX - ". $IMAP->errstr(), 1); + $log->dbug("Closed and reselected INBOX"); # Go through all of the unseen messages and add them to %unseen if they were # not there already from a prior run and read my %newUnseen = unseenMsgs; # Now clean out any messages in %unseen that were not in the %newUnseen and # marked as previously read + $log->dbug("Cleaning out unseen"); for (keys %unseen) { if (defined $newUnseen{$_}) { if ($unseen{$_}) { @@ -177,6 +208,7 @@ sub MonitorMail() { } # if } # for + $log->dbug("Processing new unseen messages"); for (keys %newUnseen) { next if $unseen{$_}; @@ -197,6 +229,10 @@ sub MonitorMail() { # Google Talk doesn't like # $subject =~ s/\#//g; + # Remove long strings of numbers like order numbers. They are uninteresting + my $longNumber = 5; + $subject =~ s/\s+\S*\d{$longNumber,}\S*\s*//g; + # Now speak it! my $logmsg = "From $from $subject"; @@ -208,7 +244,9 @@ sub MonitorMail() { # Only announce if after 6 Am. Note this will announce up until # midnight but that's ok. I want midnight to 6 Am as silent time. + $log->dbug("About to speak/log"); if ($hour >= 7) { + $log->dbug("Calling speak"); speak $msg, $log; $log->msg($logmsg); } else { @@ -218,33 +256,30 @@ sub MonitorMail() { $unseen{$_} = 1; } # for + # Let's time things + my $startTime = time; + # Re-establish callback + $log->dbug("Evaling idle"); eval { $IMAP->idle(\&MonitorMail) }; # If we return from idle then the server went away for some reason. With Gmail # the server seems to time out around 30-40 minutes. Here we simply reconnect # to the imap server and continue to MonitorMail. - $log->dbug("MonitorMail: Connection to $opts{imap} ended. Reconnecting"); - - # Destroy current IMAP connection - $log->dbug("MonitorMail: Destorying IMAP connection to $opts{imap}"); - - undef $IMAP; + restart; - # Re-establish connection - Connect2IMAP; + return; +} # MonitorMail - $log->dbug("MonitorMail: Reconnected to IMAP server $opts{imap}"); +sub restart { + my $msg = "Re-establishing connection to $opts{imap} as $opts{username}"; - # MonitorMail again - the dreaded goto! Seems the cleanest way to restart - # in this instance. I could call MonitorMail() recursively but that would - # leave junk on the stack. - $log->dbug('MonitorMail: Going back to the top of the loop'); + $log->dbug($msg); - goto MONITORMAIL; + Connect2IMAP; - return; # To make perlcritic happy -} # MonitorMail + MonitorMail; +} # restart END { # If $log is not yet defined then the exit is not unexpected @@ -272,6 +307,7 @@ GetOptions( 'usessl', 'useblocking', 'announce!', + 'append', ) || pod2usage; unless ($opts{password}) { @@ -287,15 +323,16 @@ if ($opts{username} =~ /.*\@(.*)$/) { if ($opts{daemon}) { # Perl complains if we reference $DB::OUT only once - my $foo = $DB::OUT; - EnterDaemonMode unless defined $DB::OUT; + no warnings; + EnterDaemonMode unless defined $DB::OUT or get_debug; + use warnings; } # if $log = Logger->new( - path => '/var/log', + path => '/var/local/log', name => "$Logger::me.$opts{name}", timestamped => 'yes', - append => 'yes', + append => $opts{append}, ); Connect2IMAP;