X-Git-Url: https://defaria.com/gitweb/?a=blobdiff_plain;f=bin%2FannounceEmail.pl;h=22d30d01e5a9f1897ec15fa05d1992686c02c8bc;hb=dbfad6be88c74de9bd715bbb673835d9a550a8d3;hp=96da9823b2df9700ba64bb597e0bcf055ca9b02f;hpb=ceea2eb19e1e7d27c0e41d055ce7a3f09a95a86a;p=clearscm.git diff --git a/bin/announceEmail.pl b/bin/announceEmail.pl index 96da982..22d30d0 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: 600 seconds or 10 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; @@ -114,6 +117,7 @@ my %opts = ( verbose => sub { set_verbose }, debug => sub { set_debug }, daemon => 1, + timeout => 600, # 10 minutes username => $ENV{USER}, password => $ENV{PASSWORD}, imap => $defaultIMAPServer, @@ -125,6 +129,8 @@ sub notify($) { my $cmd = "notify-send -i $icon -t $timeout '$msg'"; Execute $cmd; + + return; } # notify sub interrupted { @@ -141,18 +147,16 @@ sub interrupted { sub Connect2IMAP; -sub restart { - my $msg = "Re-establishing connection to $opts{imap} as $opts{username}"; - - $log->dbug($msg); - - Connect2IMAP; - - goto MONITORMAIL; -} # restart +#sub restart { +# $log->dbug("Re-establishing connection to $opts{imap} as $opts{username}"); +# +# Connect2IMAP; +# +# goto MONITORMAIL; +#} # restart $SIG{USR1} = \&interrupted; -$SIG{USR2} = \&restart; +#$SIG{USR2} = \&restart; sub unseenMsgs() { $IMAP->select('inbox') or @@ -188,7 +192,7 @@ sub Connect2IMAP() { } # Connect2IMAP sub MonitorMail() { - MONITORMAIL: +# MONITORMAIL: $log->dbug("Top of MonitorMail loop"); # First close and reselect the INBOX to get its current status @@ -235,6 +239,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"; @@ -263,20 +271,27 @@ sub MonitorMail() { # Re-establish callback $log->dbug("Evaling idle"); - eval { $IMAP->idle(\&MonitorMail) }; + eval { $IMAP->idle(\&MonitorMail, $opts{timeout}) }; + + $log->err("Unable to set IMAP Idle - AS $@", 1) if $@; + $log->msg("IMAP Idle for $opts{name} timed out in " . howlong $startTime, time); # 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; + # the server seems to time out around 30-40 minutes. Here we simply return + # back to main which will re-establish the connection and call us again. + unless ($IMAP->get_response_code('timeout')) { + my $errstr = $IMAP->get_last_error; - return; + $log->dbug("$opts{name} went away - $errstr"); + } # unless + + return 0; } # 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} ended unexpectedly!"; speak $msg, $log; @@ -296,6 +311,7 @@ GetOptions( 'name=s', 'password=s', 'imap=s', + 'timeout=i', 'usessl', 'useblocking', 'announce!', @@ -327,8 +343,6 @@ $log = Logger->new( append => $opts{append}, ); -Connect2IMAP; - if ($opts{username} =~ /(.*)\@/) { $opts{user} = $1; } else { @@ -337,11 +351,20 @@ if ($opts{username} =~ /(.*)\@/) { my $msg = "Now monitoring email for $opts{user}\@$opts{name}"; -speak $msg, $log if $opts{announce}; +# Changed to loop here - better than using a goto. This kinda kills the idea of +# using siguser2 to interrupt announceEmail.pl to kick it into re-establishing +# the connection. +while () { + Connect2IMAP; + + speak $msg, $log if $opts{announce}; + + $log->msg($msg); -$log->msg($msg); + MonitorMail; -MonitorMail; + $log->dbug("$opts{name} timed out! Re-establishing connection"); +} # while # Should not get here $log->err("Falling off the edge of $0", 1);