X-Git-Url: https://defaria.com/gitweb/?a=blobdiff_plain;f=bin%2FannounceEmail.pl;h=7a76fc408ab3804c639ed33806469d7df85a8948;hb=b19aff368e8114dd44bbd7e306b0946f060e3051;hp=31c75960e52b3bcbf9fab4d9848d4b507616e249;hpb=c0e00f11bf9b54692b2ee00e2488b7b08cb94026;p=clearscm.git diff --git a/bin/announceEmail.pl b/bin/announceEmail.pl index 31c7596..7a76fc4 100755 --- a/bin/announceEmail.pl +++ b/bin/announceEmail.pl @@ -17,7 +17,7 @@ Andrew DeFaria =item Revision -$Revision: 1.1 $ +$Revision: 1.2 $ =item Created: @@ -36,14 +36,18 @@ $Date: 2019/04/04 13:40:10 $ [-i|map sub { pod2usage }, - help => sub { pod2usage(-verbose => 2)}, - verbose => sub { set_verbose }, - debug => sub { set_debug }, - daemon => 1, - username => $ENV{USER}, - password => $ENV{PASSWORD}, - imap => $defaultIMAPServer, + usage => sub { pod2usage }, + help => sub { pod2usage(-verbose => 2)}, + verbose => sub { set_verbose }, + debug => sub { set_debug }, + daemon => 1, + username => $ENV{USER}, + password => $ENV{PASSWORD}, + imap => $defaultIMAPServer, + usessl => 0, + useblocking => 0, + announce => 0, ); sub interrupted { @@ -114,14 +122,6 @@ sub interrupted { $SIG{USR1} = \&interrupted; -sub debugit($) { - my ($msg) = @_; - - $log->msg($msg) if get_debug; - - return; -} # logit - sub unseenMsgs() { $IMAP->select('inbox') or $log->err("Unable to select inbox: " . get_last_error(), 1); @@ -130,15 +130,17 @@ sub unseenMsgs() { } # unseenMsgs sub Connect2IMAP() { - $log->msg("Connecting to $opts{imap} as $opts{username}"); + $log->dbug("Connecting to $opts{imap} as $opts{username}"); $IMAP = Mail::IMAPTalk->new( - Server => $opts{imap}, - Username => $opts{username}, - Password => $opts{password}, + Server => $opts{imap}, + Username => $opts{username}, + Password => $opts{password}, + UseSSL => $opts{usessl}, + UseBlocking => $opts{useblocking}, ) or $log->err("Unable to connect to IMAP server $opts{imap}: $@", 1); - $log->msg('Connected'); + $log->dbug("Connected to $opts{imap} as $opts{username}"); # Focus on INBOX only $IMAP->select('inbox'); @@ -150,90 +152,125 @@ sub Connect2IMAP() { return; } # Connect2IMAP +sub Say($) { + my ($msg) = @_; + + if (-f "$FindBin::Bin/shh") { + $log->msg("Not speaking because we were asked to be quiet - $msg"); + + return; + } # if + + my ($status, @output) = Execute "/usr/local/bin/gt \"$msg\""; + + $log->err("Unable to speak (Status: $status) - " + . join ("\n", @output), $status) if $status; + + return; +} # Say + sub MonitorMail() { - while () { - # First close and reselect the INBOX to get its current status - debugit "Reconnecting to INBOX"; - $IMAP->close; - $IMAP->select('INBOX') - or $log->err("Unable to select INBOX - ". $IMAP->errstr(), 1); - - # 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 - for (keys %unseen) { - if (defined $newUnseen{$_}) { - if ($unseen{$_}) { - delete $newUnseen{$_}; - } # if - } else { - delete $unseen{$_} + MONITORMAIL: + + # 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); + + # 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 + for (keys %unseen) { + if (defined $newUnseen{$_}) { + if ($unseen{$_}) { + delete $newUnseen{$_}; } # if - } # for + } else { + delete $unseen{$_} + } # if + } # for - debugit "Processing newUnseen"; - for (keys %newUnseen) { - next if $unseen{$_}; + for (keys %newUnseen) { + next if $unseen{$_}; - my $envelope = $IMAP->fetch($_, '(envelope)'); - my $from = $envelope->{$_}{envelope}{From}; - my $subject = $envelope->{$_}{envelope}{Subject}; - $subject //= 'Unknown subject'; + my $envelope = $IMAP->fetch($_, '(envelope)'); + my $from = $envelope->{$_}{envelope}{From}; + my $subject = $envelope->{$_}{envelope}{Subject}; + $subject //= 'Unknown subject'; - # Extract the name only when the email is of the format "name " - if ($from =~ /^"?(.*?)"?\s*\<(\S*)>/) { - $from = $1 if $1 ne ''; - } # if + # Extract the name only when the email is of the format "name " + if ($from =~ /^"?(.*?)"?\s*\<(\S*)>/) { + $from = $1 if $1 ne ''; + } # if - if ($subject =~ /=?\S+?(Q|B)\?(.+)\?=/) { - $subject = decode_base64($2); - } # if + if ($subject =~ /=?\S+?(Q|B)\?(.+)\?=/) { + $subject = decode_base64($2); + } # if - # Google Talk doesn't like # - $subject =~ s/\#//g; + # Google Talk doesn't like # + $subject =~ s/\#//g; - # Now speak it! - debugit "Speaking message from $from"; + # Now speak it! + my $logmsg = "From $from $subject"; - my $logmsg = "From $from $subject"; + my $greeting = $greetings[int rand $#greetings]; + my $msg = "$greeting from $from... $subject"; + $msg =~ s/\"/\\"/g; - my $greeting = $greetings[int rand $#greetings]; - my $msg = "$greeting from $from... " . quotemeta $subject; - $msg =~ s/\"/\\"/g; + my $hour = (localtime)[2]; - # Log it + # 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. + if ($hour >= 6) { + Say $msg; $log->msg($logmsg); + } else { + $log->msg("$logmsg [silent]"); + } # if - debugit $logmsg; + $unseen{$_} = 1; + } # for - my $cmd = "/usr/local/bin/gt \"$msg\""; + # Re-establish callback + eval { $IMAP->idle(\&MonitorMail) }; - my $hour = (localtime)[2]; + # 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"); - # 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. - if ($hour > 6) { - my ($status, @output) = Execute $cmd; + # Destroy current IMAP connection + $log->dbug("MonitorMail: Destorying IMAP connection to $opts{imap}"); - if ($status) { - $log->err("Unable to execute $cmd" . join("\n", @output)); - } # if - } # if + undef $IMAP; - $unseen{$_} = 1; - } # for - } # while + # Re-establish connection + Connect2IMAP; - return; + $log->dbug("MonitorMail: Reconnected to IMAP server $opts{imap}"); + + # 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'); + + goto MONITORMAIL; + + return; # To make perlcritic happy } # MonitorMail END { - $IMAP->quit if $IMAP; + # If $log is not yet defined then the exit is not unexpected + if ($log) { + my $msg = "$FindBin::Script ending unexpectedly!"; - $log->msg("$FindBin::Script ending!"); + Say $msg; + + $log->err($msg); + } # if } # END ## Main @@ -245,35 +282,53 @@ GetOptions( 'debug', 'daemon!', 'username=s', + 'name=s', 'password=s', 'imap=s', - 'sleep', -); + 'usessl', + 'useblocking', + 'announce!', +) || pod2usage; unless ($opts{password}) { verbose "I need $opts{username}'s password"; $opts{password} = GetPassword; } # unless -$opts{debug} = get_debug; +$opts{name} //= $opts{imap}; -EnterDaemonMode if $opts{daemon}; +if ($opts{username} =~ /.*\@(.*)$/) { + $opts{name} = $1; +} # if + +if ($opts{daemon}) { + # Perl complains if we reference $DB::OUT only once + my $foo = $DB::OUT; + EnterDaemonMode unless defined $DB::OUT; +} # if $log = Logger->new( path => '/var/log', + name => "$Logger::me.$opts{name}", timestamped => 'yes', append => 'yes', ); Connect2IMAP; -my $msg = "Now monitoring email for $opts{username}\@$opts{imap}"; +if ($opts{username} =~ /(.*)\@/) { + $opts{user} = $1; +} else { + $opts{user} = $opts{username}; +} # if -$log->msg($msg); +my $msg = "Now monitoring email for $opts{user}\@$opts{name}"; -my $cmd = "/usr/local/bin/gt \"$msg\""; +Say $msg if $opts{announce}; -my ($status, @output) = Execute $cmd; +$log->msg($msg); -$IMAP->idle(\&MonitorMail); +MonitorMail; +# Should not get here +$log->err("Falling off the edge of $0", 1);