X-Git-Url: https://defaria.com/gitweb/?a=blobdiff_plain;f=bin%2FannounceEmail.pl;h=a864634338d6891b1a68da8da7229dc0924fb8a1;hb=da4fa1f3928d1f5fec480761337a6798aed2a7f3;hp=d21003bc64d709a7f39433ff8fe479083817d1a2;hpb=f3e0cdceb40c69dc13b1edf6d9d2af3bd38798c1;p=clearscm.git diff --git a/bin/announceEmail.pl b/bin/announceEmail.pl index d21003b..a864634 100755 --- a/bin/announceEmail.pl +++ b/bin/announceEmail.pl @@ -33,7 +33,7 @@ $Date: 2019/04/04 13:40:10 $ Usage: announceEmail.pl [-usa|ge] [-h|elp] [-v|erbose] [-de|bug] [-use|rname ] [-p|assword ] - [-i|map ] + [-i|map ] [-t|imeout ] [-an|nouce] [-ap|pend] [-da|emon] [-n|name ] [-uses|sl] [-useb|locking] @@ -46,6 +46,7 @@ $Date: 2019/04/04 13:40:10 $ -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) + -t|imeout Timeout IMAP idle call (Sefault: 1200 seconds or 20 minutes) -an|nounce Announce startup (Default: False) -ap|pend Append to logfile (Default: Noappend) @@ -86,6 +87,8 @@ use Speak; use TimeUtils; use Utils; +local $0 = "$FindBin::Script " . join ' ', @ARGV; + my $defaultIMAPServer = 'defaria.com'; my $IMAP; my %unseen; @@ -105,23 +108,38 @@ my @greetings = ( "What's this? A new message", ); +my $icon = '/home/andrew/.icons/Thunderbird.jpg'; +my $notifyTimeout = 5 * 1000; +my $IMAPTimeout = 20 * 60; + my %opts = ( usage => sub { pod2usage }, help => sub { pod2usage(-verbose => 2)}, verbose => sub { set_verbose }, debug => sub { set_debug }, daemon => 1, + timeout => $IMAPTimeout, username => $ENV{USER}, password => $ENV{PASSWORD}, imap => $defaultIMAPServer, ); +sub notify($) { + my ($msg) = @_; + + my $cmd = "notify-send -i $icon -t $notifyTimeout '$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 @@ -129,12 +147,16 @@ sub interrupted { } # interrupted sub Connect2IMAP; +sub MonitorMail; sub restart { - $log->dbug("Re-establishing connection to $opts{imap} as $opts{username}"); + my $msg = "Re-establishing connection to $opts{imap} as $opts{username}"; + + $log->dbug($msg); + Connect2IMAP; - goto MONITORMAIL; + MonitorMail; } # restart $SIG{USR1} = \&interrupted; @@ -221,24 +243,26 @@ 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"; my $greeting = $greetings[int rand $#greetings]; my $msg = "$greeting from $from... $subject"; - $msg =~ s/\"/\\"/g; - - my $hour = (localtime)[2]; + my $hour = (localtime)[2]; # 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->msg($logmsg); $log->dbug("Calling speak"); speak $msg, $log; - $log->msg($logmsg); } else { - $log->msg("$logmsg [silent]"); + $log->msg("$logmsg [silent nighttime]"); } # if $unseen{$_} = 1; @@ -248,21 +272,37 @@ sub MonitorMail() { my $startTime = time; # Re-establish callback - $log->dbug("Evaling idle"); - eval { $IMAP->idle(\&MonitorMail) }; + $log->dbug("Calling IMAP->idle"); + eval { + $IMAP->idle(\&MonitorMail, $opts{timeout}) + }; + + my $msg = 'Returned from IMAP->idle '; + + if ($@) { + speak($msg . $@, $log); + } else { + $log->msg($msg . 'no error'); + } # if # 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. - restart; + unless ($IMAP->get_response_code('timeout')) { + $msg = "IMAP Idle for $opts{name} timed out in " . howlong $startTime, time; - return; + speak $msg; + + $log->msg($msg); + } # unless + + restart; } # MonitorMail END { # If $log is not yet defined then the exit is not unexpected if ($log) { - my $msg = "$FindBin::Script ending unexpectedly!"; + my $msg = "$FindBin::Script $opts{name} ending unexpectedly!"; speak $msg, $log; @@ -282,6 +322,7 @@ GetOptions( 'name=s', 'password=s', 'imap=s', + 'timeout=i', 'usessl', 'useblocking', 'announce!',