projects
/
clearscm.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Fixed bug in setbg
[clearscm.git]
/
bin
/
announceEmail.pl
diff --git
a/bin/announceEmail.pl
b/bin/announceEmail.pl
index
2826ed3
..
cb6667b
100755
(executable)
--- 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 <username>] [-p|assword <password>]
Usage: announceEmail.pl [-usa|ge] [-h|elp] [-v|erbose] [-de|bug]
[-use|rname <username>] [-p|assword <password>]
- [-i|map <server>]
+ [-i|map <server>]
[-t|imeout <secs>]
[-an|nouce] [-ap|pend] [-da|emon] [-n|name <name>]
[-uses|sl] [-useb|locking]
[-an|nouce] [-ap|pend] [-da|emon] [-n|name <name>]
[-uses|sl] [-useb|locking]
@@
-42,10
+42,11
@@
$Date: 2019/04/04 13:40:10 $
-h|elp Detailed help
-v|erbose Verbose mode (Default: -verbose)
-de|bug Turn on debugging (Default: Off)
-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)
-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 <s> Timeout IMAP idle call (Sefault: 1200 seconds or 20 minutes)
-an|nounce Announce startup (Default: False)
-ap|pend Append to logfile (Default: Noappend)
-an|nounce Announce startup (Default: False)
-ap|pend Append to logfile (Default: Noappend)
@@
-54,6
+55,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)
-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
=head1 DESCRIPTION
This script will connect to an IMAP server, login and then monitor the user's
@@
-70,9
+75,10
@@
use warnings;
use FindBin;
use Getopt::Long;
use FindBin;
use Getopt::Long;
-use Pod::Usage;
use Mail::IMAPTalk;
use MIME::Base64;
use Mail::IMAPTalk;
use MIME::Base64;
+use Pod::Usage;
+use Proc::ProcessTable;
use lib "$FindBin::Bin/../lib";
use lib "$FindBin::Bin/../lib";
@@
-82,6
+88,18
@@
use Speak;
use TimeUtils;
use Utils;
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;
my %unseen;
my $defaultIMAPServer = 'defaria.com';
my $IMAP;
my %unseen;
@@
-98,32
+116,62
@@
my @greetings = (
'I was looking in your inbox and found a message',
'Not sure you want to hear this message',
'Good news',
'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,
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,
);
username => $ENV{USER},
password => $ENV{PASSWORD},
imap => $defaultIMAPServer,
);
+sub notify($) {
+ my ($msg) = @_;
+
+ my $cmd = "notify-send -i $icon -t $notifyTimeout '$msg'";
+
+ Execute $cmd;
+
+ return;
+} # notify
+
sub interrupted {
if (get_debug) {
sub interrupted {
if (get_debug) {
-
$log->msg("Turning off debugging")
;
+
notify 'Turning off debugging'
;
set_debug 0;
} else {
set_debug 0;
} else {
-
$log->msg("Turning on debugging"
);
+
notify ('Turning on debugging'
);
set_debug 1;
} # if
return;
} # interrupted
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{USR1} = \&interrupted;
+$SIG{USR2} = \&restart;
sub unseenMsgs() {
$IMAP->select('inbox') or
sub unseenMsgs() {
$IMAP->select('inbox') or
@@
-135,6
+183,9
@@
sub unseenMsgs() {
sub Connect2IMAP() {
$log->dbug("Connecting to $opts{imap} as $opts{username}");
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},
$IMAP = Mail::IMAPTalk->new(
Server => $opts{imap},
Username => $opts{username},
@@
-203,24
+254,26
@@
sub MonitorMail() {
# Google Talk doesn't like #
$subject =~ s/\#//g;
# 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";
# 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.
$log->dbug("About to speak/log");
if ($hour >= 7) {
# 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.
$log->dbug("About to speak/log");
if ($hour >= 7) {
+ $log->msg($logmsg);
$log->dbug("Calling speak");
speak $msg, $log;
$log->dbug("Calling speak");
speak $msg, $log;
- $log->msg($logmsg);
} else {
} else {
- $log->msg("$logmsg [silent]");
+ $log->msg("$logmsg [silent
nighttime
]");
} # if
$unseen{$_} = 1;
} # if
$unseen{$_} = 1;
@@
-230,39
+283,37
@@
sub MonitorMail() {
my $startTime = time;
# Re-establish callback
my $startTime = time;
# Re-establish callback
- $log->dbug("Evaling idle");
- 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.
# 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;
-
- # 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
- re
turn; # To make perlcritic happy
+ re
start;
} # MonitorMail
END {
# If $log is not yet defined then the exit is not unexpected
if ($log) {
} # 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!";
speak $msg, $log;
speak $msg, $log;
@@
-282,6
+333,7
@@
GetOptions(
'name=s',
'password=s',
'imap=s',
'name=s',
'password=s',
'imap=s',
+ 'timeout=i',
'usessl',
'useblocking',
'announce!',
'usessl',
'useblocking',
'announce!',