X-Git-Url: https://defaria.com/gitweb/?a=blobdiff_plain;f=bin%2FannounceEmail.pl;h=31c75960e52b3bcbf9fab4d9848d4b507616e249;hb=b5908dd87cbfc53926d81f04d3a4d12949a9b2a3;hp=4eb288ac21c436c93fafaaba80b9832518d756b6;hpb=0da31d3e334d634082a354edc33592ba1ca33208;p=clearscm.git diff --git a/bin/announceEmail.pl b/bin/announceEmail.pl index 4eb288a..31c7596 100755 --- a/bin/announceEmail.pl +++ b/bin/announceEmail.pl @@ -17,7 +17,7 @@ Andrew DeFaria =item Revision -$Revision: 1.0 $ +$Revision: 1.1 $ =item Created: @@ -29,7 +29,7 @@ $Date: 2019/04/04 13:40:10 $ =back -=hade1 SYNOPSIS +=head1 SYNOPSIS Usage: announceEmail.pl [-usa|ge] [-h|elp] [-v|erbose] [-de|bug] [-da|emon] [-use|rname ] [-p|assword ] @@ -44,7 +44,6 @@ $Date: 2019/04/04 13:40:10 $ -use|rname: 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) - -s|leep: Number of minutes to sleep inbetween checking mail (Default: 5) =head1 DESCRIPTION @@ -63,19 +62,32 @@ use warnings; use FindBin; use Getopt::Long; use Pod::Usage; -use Net::IMAP::Simple; -use Email::Simple; +use Mail::IMAPTalk; use MIME::Base64; use lib "$FindBin::Bin/../lib"; use Display; +use Logger; use Utils; my $defaultIMAPServer = 'defaria.com'; -my $defaultSleeptime = 5; my $IMAP; my %unseen; +my $log; + +my @greetings = ( + 'Incoming message', + 'You have received a new message', + 'Hey I found this in your inbox', + 'For some unknown reason this guy send you a message', + 'Did you know you just got a message', + 'Potential spam', + 'You received a communique', + 'I was looking in your inbox and found a message', + 'Not sure you want to hear this message', + 'Good news', +); my %opts = ( usage => sub { pod2usage }, @@ -84,52 +96,67 @@ my %opts = ( debug => sub { set_debug }, daemon => 1, username => $ENV{USER}, + password => $ENV{PASSWORD}, imap => $defaultIMAPServer, - sleep => $defaultSleeptime, ); -sub unseenMsgs() { - my %unseenMsgs; +sub interrupted { + if (get_debug) { + $log->msg("Turning off debugging"); + set_debug 0; + } else { + $log->msg("Turning on debugging"); + set_debug 1; + } # if - for (my $i = 1; $i <= $IMAP->status; $i++) { - $unseenMsgs{$i} = 0 unless $IMAP->seen($i); - } # for + return; +} # interrupted - return %unseenMsgs; -} # unseenMsgs +$SIG{USR1} = \&interrupted; -sub Connect2IMAP() { - verbose "Connecting to $opts{imap} as $opts{username}"; +sub debugit($) { + my ($msg) = @_; - $IMAP = Net::IMAP::Simple->new($opts{imap}) || - error("Unable to connect to IMAP server $opts{imap}: " . $Net::IMAP::Simple::errstr, 1); + $log->msg($msg) if get_debug; - verbose "Connected"; + return; +} # logit - verbose "Logging onto $opts{imap} as $opts{username}"; +sub unseenMsgs() { + $IMAP->select('inbox') or + $log->err("Unable to select inbox: " . get_last_error(), 1); + + return map { $_=> 0 } @{$IMAP->search('not', 'seen')}; +} # unseenMsgs + +sub Connect2IMAP() { + $log->msg("Connecting to $opts{imap} as $opts{username}"); - unless ($IMAP->login($opts{username}, $opts{password})) { - error("Login to $opts{imap} as $opts{username} failed: " . $IMAP->errstr, 1); - } # unless + $IMAP = Mail::IMAPTalk->new( + Server => $opts{imap}, + Username => $opts{username}, + Password => $opts{password}, + ) or $log->err("Unable to connect to IMAP server $opts{imap}: $@", 1); - verbose "Logged on"; + $log->msg('Connected'); # Focus on INBOX only - $IMAP->select('INBOX'); + $IMAP->select('inbox'); # Setup %unseen to have each unseen message index set to 0 meaning not read # aloud yet %unseen = unseenMsgs; + + return; } # Connect2IMAP sub MonitorMail() { - verbose "Monitoring email"; - while () { - verbose "Looking for unread messages"; # First close and reselect the INBOX to get its current status + debugit "Reconnecting to INBOX"; $IMAP->close; - $IMAP->select('INBOX'); + $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 @@ -147,43 +174,66 @@ sub MonitorMail() { } # if } # for + debugit "Processing newUnseen"; for (keys %newUnseen) { next if $unseen{$_}; - my $email = Email::Simple->new(join '', @{$IMAP->top($_)}); - - my $from = $email->header('From'); + 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*)>/) { + if ($from =~ /^"?(.*?)"?\s*\<(\S*)>/) { $from = $1 if $1 ne ''; } # if - my $subject = $email->header('Subject'); - if ($subject =~ /=?\S+?(Q|B)\?(.+)\?=/) { $subject = decode_base64($2); } # if + # Google Talk doesn't like # + $subject =~ s/\#//g; + # Now speak it! - my $msg = "Message from $from... " . quotemeta $subject; - $msg =~ s/\"/\\"/g; + debugit "Speaking message from $from"; + + my $logmsg = "From $from $subject"; + + my $greeting = $greetings[int rand $#greetings]; + my $msg = "$greeting from $from... " . quotemeta $subject; + $msg =~ s/\"/\\"/g; + + # Log it + $log->msg($logmsg); - verbose "Announcing $msg"; + debugit $logmsg; - Execute "/usr/local/bin/gt \"$msg\""; + my $cmd = "/usr/local/bin/gt \"$msg\""; + + 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) { + my ($status, @output) = Execute $cmd; + + if ($status) { + $log->err("Unable to execute $cmd" . join("\n", @output)); + } # if + } # if $unseen{$_} = 1; } # for - - verbose "Sleeping for $opts{sleep} minutes"; - sleep 60 * $opts{sleep}; - verbose "Ah that was refreshing!"; } # while + + return; } # MonitorMail END { - $IMAP->quit; + $IMAP->quit if $IMAP; + + $log->msg("$FindBin::Script ending!"); } # END ## Main @@ -205,7 +255,25 @@ unless ($opts{password}) { $opts{password} = GetPassword; } # unless +$opts{debug} = get_debug; + EnterDaemonMode if $opts{daemon}; +$log = Logger->new( + path => '/var/log', + timestamped => 'yes', + append => 'yes', +); + Connect2IMAP; -MonitorMail; + +my $msg = "Now monitoring email for $opts{username}\@$opts{imap}"; + +$log->msg($msg); + +my $cmd = "/usr/local/bin/gt \"$msg\""; + +my ($status, @output) = Execute $cmd; + +$IMAP->idle(\&MonitorMail); +