Changed announceEmail.pl to remove things like order numbers
[clearscm.git] / bin / announceEmail.pl
index 2826ed3..906b22a 100755 (executable)
@@ -42,7 +42,7 @@ $Date: 2019/04/04 13:40:10 $
    -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)
@@ -54,6 +54,10 @@ $Date: 2019/04/04 13:40:10 $
    -uses|sl      Whether or not to use SSL to connect (Default: False)
    -useb|locking Whether to block on socket (Default: False)
 
+ Signals:
+   $SIG{USR1}:   Toggles debug option
+   $SIG{USR2}:   Reestablishes connection to IMAP server
+
 =head1 DESCRIPTION
 
 This script will connect to an IMAP server, login and then monitor the user's
@@ -98,8 +102,12 @@ 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 $timeout = 5 * 1000;
+
 my %opts = (
   usage       => sub { pod2usage },
   help        => sub { pod2usage(-verbose => 2)},
@@ -111,19 +119,42 @@ my %opts = (
   imap        => $defaultIMAPServer,
 );
 
+sub notify($) {
+  my ($msg) = @_;
+
+  my $cmd = "notify-send -i $icon -t $timeout '$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 restart {
+  my $msg = "Re-establishing connection to $opts{imap} as $opts{username}";
+
+  $log->dbug($msg);
+
+  Connect2IMAP;
+
+  goto MONITORMAIL;
+} # restart
+
 $SIG{USR1} = \&interrupted;
+$SIG{USR2} = \&restart;
 
 sub unseenMsgs() {
   $IMAP->select('inbox') or
@@ -135,6 +166,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},
@@ -203,6 +237,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";
 
@@ -236,27 +274,9 @@ sub MonitorMail() {
   # 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 - lasted "
-           . howlong $startTime);
-
-  # Destroy current IMAP connection
-  $log->dbug("MonitorMail: Destroying IMAP connection to $opts{imap}");
-
-  undef $IMAP;
+  restart;
 
-  # Re-establish connection
-  Connect2IMAP;
-
-  $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
+  return;
 } # MonitorMail
 
 END {