X-Git-Url: https://defaria.com/gitweb/?a=blobdiff_plain;f=bin%2FannounceEmail.pl;h=cb6667be3d343c51a59b1f88f39b4dd1b5845f15;hb=10b78beac53843192f7da8a6dbaa2d5e0572b307;hp=94789e84d27a695ecead72c234fb7043e99c1ef8;hpb=30b20fd03559202343ac8de28938e33866b38c3f;p=clearscm.git diff --git a/bin/announceEmail.pl b/bin/announceEmail.pl index 94789e8..cb6667b 100755 --- a/bin/announceEmail.pl +++ b/bin/announceEmail.pl @@ -31,23 +31,33 @@ $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 ] [-t|imeout ] + [-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: -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) + -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) + -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 @@ -65,16 +75,30 @@ use warnings; use FindBin; use Getopt::Long; -use Pod::Usage; use Mail::IMAPTalk; use MIME::Base64; +use Pod::Usage; +use Proc::ProcessTable; use lib "$FindBin::Bin/../lib"; use Display; use Logger; -use Utils; +use Speak; use TimeUtils; +use Utils; + +local $0 = "$FindBin::Script " . join ' ', @ARGV; + +my $processes = Proc::ProcessTable->new; + +for my $process (@{$processes->table}) { + if ($process->cmndline eq $0 and $process->pid != $$) { + verbose "$FindBin::Script already running"; + + exit 0; + } # if +} # for my $defaultIMAPServer = 'defaria.com'; my $IMAP; @@ -92,35 +116,62 @@ 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 $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, - usessl => 0, - useblocking => 0, - announce => 0, ); +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 return; } # interrupted +sub Connect2IMAP; +sub MonitorMail; + +sub restart { + my $msg = "Re-establishing connection to $opts{imap} as $opts{username}"; + + $log->dbug($msg); + + Connect2IMAP; + + MonitorMail; +} # restart + $SIG{USR1} = \&interrupted; +$SIG{USR2} = \&restart; sub unseenMsgs() { $IMAP->select('inbox') or @@ -132,6 +183,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}, @@ -152,31 +206,23 @@ sub Connect2IMAP() { return; } # Connect2IMAP -sub Say($) { - my ($msg) = @_; - - 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() { 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{$_}) { @@ -187,6 +233,7 @@ sub MonitorMail() { } # if } # for + $log->dbug("Processing new unseen messages"); for (keys %newUnseen) { next if $unseen{$_}; @@ -207,61 +254,68 @@ 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. - if ($hour >= 6) { - Say $msg; + $log->dbug("About to speak/log"); + if ($hour >= 7) { $log->msg($logmsg); + $log->dbug("Calling speak"); + speak $msg, $log; } else { - $log->msg("$logmsg [silent]"); + $log->msg("$logmsg [silent nighttime]"); } # if $unseen{$_} = 1; } # for + # Let's time things + my $startTime = time; + # Re-establish callback - 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. - $log->dbug("MonitorMail: Connection to $opts{imap} ended. Reconnecting"); - - # Destroy current IMAP connection - $log->dbug("MonitorMail: Destorying IMAP connection to $opts{imap}"); - - undef $IMAP; - - # Re-establish connection - Connect2IMAP; - - $log->dbug("MonitorMail: Reconnected to IMAP server $opts{imap}"); + unless ($IMAP->get_response_code('timeout')) { + $msg = "IMAP Idle for $opts{name} timed out in " . howlong $startTime, time; - # 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'); + speak $msg; - goto MONITORMAIL; + $log->msg($msg); + } # unless - return; # To make perlcritic happy + 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!"; - Say $msg; + speak $msg, $log; $log->err($msg); } # if @@ -279,9 +333,11 @@ GetOptions( 'name=s', 'password=s', 'imap=s', + 'timeout=i', 'usessl', 'useblocking', 'announce!', + 'append', ) || pod2usage; unless ($opts{password}) { @@ -297,15 +353,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; @@ -318,7 +375,7 @@ if ($opts{username} =~ /(.*)\@/) { my $msg = "Now monitoring email for $opts{user}\@$opts{name}"; -Say $msg if $opts{announce}; +speak $msg, $log if $opts{announce}; $log->msg($msg);