From: Andrew DeFaria Date: Fri, 30 Oct 2020 20:40:03 +0000 (-0700) Subject: Merge branch 'master' of git+ssh://github.com/adefaria/clearscm X-Git-Url: https://defaria.com/gitweb/?a=commitdiff_plain;h=b19aff368e8114dd44bbd7e306b0946f060e3051;hp=f0d498f76188921859a3e3b72f8532732d021ad9;p=clearscm.git Merge branch 'master' of git+ssh://github.com/adefaria/clearscm --- diff --git a/bin/announceEmail.pl b/bin/announceEmail.pl index a0113a1..7a76fc4 100755 --- a/bin/announceEmail.pl +++ b/bin/announceEmail.pl @@ -36,15 +36,18 @@ $Date: 2019/04/04 13:40:10 $ [-i|map sub { pod2usage }, - help => sub { pod2usage(-verbose => 2)}, - verbose => sub { set_verbose }, - debug => sub { set_debug }, - daemon => 1, - username => $ENV{USER}, - password => $ENV{PASSWORD}, - imap => $defaultIMAPServer, - usessl => 0, + usage => sub { pod2usage }, + help => sub { pod2usage(-verbose => 2)}, + verbose => sub { set_verbose }, + debug => sub { set_debug }, + daemon => 1, + username => $ENV{USER}, + password => $ENV{PASSWORD}, + imap => $defaultIMAPServer, + usessl => 0, + useblocking => 0, + announce => 0, ); sub interrupted { @@ -124,16 +130,17 @@ sub unseenMsgs() { } # unseenMsgs sub Connect2IMAP() { - $log->msg("Connecting to $opts{imap} as $opts{username}"); + $log->dbug("Connecting to $opts{imap} as $opts{username}"); $IMAP = Mail::IMAPTalk->new( - Server => $opts{imap}, - Username => $opts{username}, - Password => $opts{password}, - UseSSL => $opts{usessl}, + Server => $opts{imap}, + Username => $opts{username}, + Password => $opts{password}, + UseSSL => $opts{usessl}, + UseBlocking => $opts{useblocking}, ) or $log->err("Unable to connect to IMAP server $opts{imap}: $@", 1); - $log->msg('Connected'); + $log->dbug("Connected to $opts{imap} as $opts{username}"); # Focus on INBOX only $IMAP->select('inbox'); @@ -145,7 +152,26 @@ sub Connect2IMAP() { return; } # Connect2IMAP +sub Say($) { + my ($msg) = @_; + + if (-f "$FindBin::Bin/shh") { + $log->msg("Not speaking because we were asked to be quiet - $msg"); + + return; + } # if + + 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: + # First close and reselect the INBOX to get its current status $IMAP->close; $IMAP->select('INBOX') @@ -191,39 +217,60 @@ sub MonitorMail() { my $logmsg = "From $from $subject"; my $greeting = $greetings[int rand $#greetings]; - my $msg = "$greeting from $from... " . quotemeta $subject; + my $msg = "$greeting from $from... $subject"; $msg =~ s/\"/\\"/g; - # Log it - $log->msg($logmsg); - 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 $cmd = "/usr/local/bin/gt \"$msg\""; - my ($status, @output) = Execute $cmd; - - if ($status) { - $log->err("Unable to execute $cmd" . join("\n", @output)); - } # if + if ($hour >= 6) { + Say $msg; + $log->msg($logmsg); + } else { + $log->msg("$logmsg [silent]"); } # if $unseen{$_} = 1; } # for # Re-establish callback - $IMAP->idle(\&MonitorMail); + eval { $IMAP->idle(\&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. Reconnecting"); - # Should not get here - $log->err("Unable to re-establish IDLE callback from IMAP server $opts{imap}", 1); + # 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}"); + + # 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 } # MonitorMail END { - $IMAP->quit if $IMAP; + # If $log is not yet defined then the exit is not unexpected + if ($log) { + my $msg = "$FindBin::Script ending unexpectedly!"; + + Say $msg; - $log->msg("$FindBin::Script ending unexpectedly!"); + $log->err($msg); + } # if } # END ## Main @@ -235,40 +282,53 @@ GetOptions( 'debug', 'daemon!', 'username=s', + 'name=s', 'password=s', 'imap=s', 'usessl', - 'sleep', -); + 'useblocking', + 'announce!', +) || pod2usage; unless ($opts{password}) { verbose "I need $opts{username}'s password"; $opts{password} = GetPassword; } # unless -$opts{debug} = get_debug; +$opts{name} //= $opts{imap}; + +if ($opts{username} =~ /.*\@(.*)$/) { + $opts{name} = $1; +} # if if ($opts{daemon}) { + # Perl complains if we reference $DB::OUT only once + my $foo = $DB::OUT; EnterDaemonMode unless defined $DB::OUT; } # if $log = Logger->new( path => '/var/log', + name => "$Logger::me.$opts{name}", timestamped => 'yes', append => 'yes', ); Connect2IMAP; -my $msg = "Now monitoring email for $opts{username}\@$opts{imap}"; +if ($opts{username} =~ /(.*)\@/) { + $opts{user} = $1; +} else { + $opts{user} = $opts{username}; +} # if -$log->msg($msg); +my $msg = "Now monitoring email for $opts{user}\@$opts{name}"; -my $cmd = "/usr/local/bin/gt \"$msg\""; +Say $msg if $opts{announce}; -my ($status, @output) = Execute $cmd; +$log->msg($msg); -$IMAP->idle(\&MonitorMail); +MonitorMail; # Should not get here $log->err("Falling off the edge of $0", 1); diff --git a/bin/setbg b/bin/setbg index 93a339a..17bdc08 100755 --- a/bin/setbg +++ b/bin/setbg @@ -32,7 +32,7 @@ $Date: 2012/11/09 15:31:30 $ Usage: setbg [-u|sage] [-h|elp] [-ve|rbose] [-d|ebug] [-s|leep ] [-bgdirs -bgdirs ...] - + Where: -u|sage: Displays this usage @@ -130,12 +130,14 @@ sub fillPictures () { $totals{bgdirs} = 0; for (@{$opts{bgdirs}}) { - my ($status, @pics) = Execute "find \"$_\" -type f"; + my ($status, @pics) = Execute "find \"$_/\" -type f"; chomp @pics; push @images, grep(/jpg$|png$|gif$/i, @pics); + @pics = grep(/jpg$|png$|gif$/i, @pics); + push @{$opts{bgdircnt}}, scalar @pics; $totals{bgdirs}++; @@ -143,8 +145,11 @@ sub fillPictures () { $totals{images} = scalar @images; + displayStats; + return @images; } # fillPictures + sub writeHistory($) { my ($msg) = @_; @@ -237,17 +242,26 @@ truncate "$ENV{HOME}/.$FindBin::Script.hist", 0; EnterDaemonMode unless defined $DB::OUT; +my $pickNewImages = 1; +my ($bgimage, $lockimage); + while () { - my $bgimage = escapeHTML ($images[int (rand $#images)]); - my $lockimage = escapeHTML ($images[int (rand $#images)]); + if ($pickNewImages) { + $bgimage = escapeHTML ($images[int (rand $#images)]); + $lockimage = escapeHTML ($images[int (rand $#images)]); + } # if my $monitorIsOn; my ($status, @output) = Execute("xset q | grep Monitor | awk '{print \$3}'"); if ($status or $output[0] eq 'Off') { - writeHistory ":Monitor off, not setting background to $bgimage"; + writeHistory ":Monitor off, not setting background to $bgimage - will keep trying"; + + $pickNewImages = 0; } else { + $pickNewImages = 1; + my $cmd = "$setbg $setbgOpts$bgimage\" 2> /dev/null"; ($status, @output) = Execute $cmd; @@ -275,10 +289,10 @@ while () { } # if updateSetBG $bgimage, $lockimage; - - displayStats; } # if + displayStats; + $today = YMD; sleep $opts{sleep} * 60; diff --git a/etc/cleantmp.conf b/etc/cleantmp.conf index 88d7fb3..e2ccfc2 100644 --- a/etc/cleantmp.conf +++ b/etc/cleantmp.conf @@ -31,3 +31,5 @@ tmp* tracker-* SWT-* grilo-plugin-cache* +qipc* +birdtry* diff --git a/lib/Logger.pm b/lib/Logger.pm index a1ad323..084fbcb 100644 --- a/lib/Logger.pm +++ b/lib/Logger.pm @@ -83,7 +83,7 @@ use Utils; my ($error_color, $warning_color, $command_color, $highlight_color, $normal) = ""; -my $me; +our $me; BEGIN { # Extract relative path and basename from script name. @@ -166,7 +166,7 @@ Returns: my $append = $parms{append} ? '>>' : '>'; my $logfile; - if ($parms{extension}) { + if (defined $parms{extension}) { $name .= ".$parms{extension}" unless $parms{extension} eq ''; } else { $name .= '.log'; @@ -845,7 +845,7 @@ Returns: sub dbug($) { my ($self, $msg) = @_; - $self->log("DEBUG: $msg") unless get_debug; + $self->log("DEBUG: $msg") if get_debug; return; } # dbug diff --git a/lib/Utils.pm b/lib/Utils.pm index 8aabcaa..29e020c 100644 --- a/lib/Utils.pm +++ b/lib/Utils.pm @@ -341,10 +341,9 @@ Returns: =cut - $prompt ||= 'Password'; - my $password; + my $password = ''; local $| = 1; @@ -365,9 +364,18 @@ Returns: last; } # if - print '*'; + # Handle backspaces + if ($key eq chr(127)) { + unless ($password eq '') { + chop $password; + + print "\b \b"; + } # unless + } else { + print '*'; - $password .= $key; + $password .= $key; + } # if } # while ReadMode 'restore'; # Reset tty mode before exiting. diff --git a/rc/bash_login b/rc/bash_login index fcfa0a4..798dacf 100644 --- a/rc/bash_login +++ b/rc/bash_login @@ -125,6 +125,8 @@ fi if [ $ARCHITECTURE = "cygwin" ]; then alias ping=$(echo $SYSTEMROOT | tr '\\' '\/')/system32/ping alias rdp=mstsc +else + alias sys=systemctl fi # We specify /home/$USER here so that when we sudo to another user