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
22d30d0
..
cb6667b
100755
(executable)
--- a/
bin/announceEmail.pl
+++ b/
bin/announceEmail.pl
@@
-46,7
+46,7
@@
$Date: 2019/04/04 13:40:10 $
-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:
600 seconds or 1
0 minutes)
+ -t|imeout <s> Timeout IMAP idle call (Sefault:
1200 seconds or 2
0 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)
@@
-75,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";
@@
-89,6
+90,16
@@
use Utils;
local $0 = "$FindBin::Script " . join ' ', @ARGV;
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;
@@
-108,8
+119,9
@@
my @greetings = (
"What's this? A new message",
);
"What's this? A new message",
);
-my $icon = '/home/andrew/.icons/Thunderbird.jpg';
-my $timeout = 5 * 1000;
+my $icon = '/home/andrew/.icons/Thunderbird.jpg';
+my $notifyTimeout = 5 * 1000;
+my $IMAPTimeout = 20 * 60;
my %opts = (
usage => sub { pod2usage },
my %opts = (
usage => sub { pod2usage },
@@
-117,7
+129,7
@@
my %opts = (
verbose => sub { set_verbose },
debug => sub { set_debug },
daemon => 1,
verbose => sub { set_verbose },
debug => sub { set_debug },
daemon => 1,
- timeout =>
600, # 10 minutes
+ timeout =>
$IMAPTimeout,
username => $ENV{USER},
password => $ENV{PASSWORD},
imap => $defaultIMAPServer,
username => $ENV{USER},
password => $ENV{PASSWORD},
imap => $defaultIMAPServer,
@@
-126,7
+138,7
@@
my %opts = (
sub notify($) {
my ($msg) = @_;
sub notify($) {
my ($msg) = @_;
- my $cmd = "notify-send -i $icon -t $
t
imeout '$msg'";
+ my $cmd = "notify-send -i $icon -t $
notifyT
imeout '$msg'";
Execute $cmd;
Execute $cmd;
@@
-146,17
+158,20
@@
sub interrupted {
} # interrupted
sub Connect2IMAP;
} # interrupted
sub Connect2IMAP;
+sub MonitorMail;
-#sub restart {
-# $log->dbug("Re-establishing connection to $opts{imap} as $opts{username}");
-#
-# Connect2IMAP;
-#
-# goto MONITORMAIL;
-#} # restart
+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;
+$SIG{USR2} = \&restart;
sub unseenMsgs() {
$IMAP->select('inbox') or
sub unseenMsgs() {
$IMAP->select('inbox') or
@@
-192,7
+207,7
@@
sub Connect2IMAP() {
} # Connect2IMAP
sub MonitorMail() {
} # Connect2IMAP
sub MonitorMail() {
-
#
MONITORMAIL:
+ MONITORMAIL:
$log->dbug("Top of MonitorMail loop");
# First close and reselect the INBOX to get its current status
$log->dbug("Top of MonitorMail loop");
# First close and reselect the INBOX to get its current status
@@
-248,19
+263,17
@@
sub MonitorMail() {
my $greeting = $greetings[int rand $#greetings];
my $msg = "$greeting 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;
@@
-270,28
+283,37
@@
sub MonitorMail() {
my $startTime = time;
# Re-establish callback
my $startTime = time;
# Re-establish callback
- $log->dbug("Evaling idle");
- eval { $IMAP->idle(\&MonitorMail, $opts{timeout}) };
+ $log->dbug("Calling IMAP->idle");
+ eval {
+ $IMAP->idle(\&MonitorMail, $opts{timeout})
+ };
+
+ my $msg = 'Returned from IMAP->idle ';
- $log->err("Unable to set IMAP Idle - AS $@", 1) if $@;
- $log->msg("IMAP Idle for $opts{name} timed out in " . howlong $startTime, time);
+ 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
# 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 re
turn
- #
back to main which will re-establish the connection and call us again
.
+ # the server seems to time out around 30-40 minutes. Here we simply re
connect
+ #
to the imap server and continue to MonitorMail
.
unless ($IMAP->get_response_code('timeout')) {
unless ($IMAP->get_response_code('timeout')) {
- my $errstr = $IMAP->get_last_error;
+ $msg = "IMAP Idle for $opts{name} timed out in " . howlong $startTime, time;
+
+ speak $msg;
- $log->
dbug("$opts{name} went away - $errstr"
);
+ $log->
msg($msg
);
} # unless
} # unless
- re
turn 0
;
+ 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 $opts{name} end
ed
unexpectedly!";
+ my $msg = "$FindBin::Script $opts{name} end
ing
unexpectedly!";
speak $msg, $log;
speak $msg, $log;
@@
-343,6
+365,8
@@
$log = Logger->new(
append => $opts{append},
);
append => $opts{append},
);
+Connect2IMAP;
+
if ($opts{username} =~ /(.*)\@/) {
$opts{user} = $1;
} else {
if ($opts{username} =~ /(.*)\@/) {
$opts{user} = $1;
} else {
@@
-351,20
+375,11
@@
if ($opts{username} =~ /(.*)\@/) {
my $msg = "Now monitoring email for $opts{user}\@$opts{name}";
my $msg = "Now monitoring email for $opts{user}\@$opts{name}";
-# Changed to loop here - better than using a goto. This kinda kills the idea of
-# using siguser2 to interrupt announceEmail.pl to kick it into re-establishing
-# the connection.
-while () {
- Connect2IMAP;
-
- speak $msg, $log if $opts{announce};
+speak $msg, $log if $opts{announce};
- $log->msg($msg);
-
- MonitorMail;
+$log->msg($msg);
- $log->dbug("$opts{name} timed out! Re-establishing connection");
-} # while
+MonitorMail;
# Should not get here
$log->err("Falling off the edge of $0", 1);
# Should not get here
$log->err("Falling off the edge of $0", 1);