small-hacks/urgent-mail-check.plx

209 lines
7.3 KiB
Text
Raw Normal View History

2015-09-07 16:28:55 -07:00
#!/usr/bin/perl
# urgent-mail-check.plx
#
# Copyright (C) 2013, 2015, 2016, Bradley M. Kuhn
2015-09-07 16:28:55 -07:00
#
# This program gives you software freedom; you can copy, modify, convey,
# and/or redistribute it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program in a file called 'GPLv3'. If not, write to the:
# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor
# Boston, MA 02110-1301, USA.
#
# I needed a quick script to parse the output of emacs script.
use strict;
use warnings;
use Date::Manip;
use Net::IMAP::Client;
use File::Spec;
2015-09-07 19:22:54 -07:00
use File::Temp;
use Encode qw(encode decode);
2015-09-07 16:28:55 -07:00
my $VERBOSE = 0;
if (@ARGV != 3) {
print STDERR "usage: <server> <username> <password_file>\n";
2015-09-07 19:22:54 -07:00
exit 1;
}
2015-09-07 16:28:55 -07:00
my($SERVER, $USERNAME, $PASSWORD_FILE) = @ARGV;
2015-09-07 16:28:55 -07:00
my $DIR = File::Spec->catdir("$ENV{HOME}", 'tmp', '.urgent-email-displayed');
chdir($DIR) or die "unable to go to $DIR";
######################################################################
sub ReadRecentUrgentEmailAnnouncements ($$) {
my($dir, $server) = @_;
2015-09-07 16:28:55 -07:00
my %info;
my $file = File::Spec->catfile($dir, $server . '.urgent-email-announce-recent');
open(RECENT_ALERTS, '<:encoding(UTF-8)', $file) or die "unable to open $file for reading: $!";
2015-09-07 16:28:55 -07:00
my $key;
foreach my $line (<RECENT_ALERTS>) {
chomp $line;
next if $line =~ /^\s*$/;
if ($line =~ /^\s*\"(.*)\"\s*:\s*([\d\:\-]+)\s*$/) {
my($key, $date) = ($1, $2);
$info{$key} = $date if defined $key;
2015-09-07 16:28:55 -07:00
}
}
close RECENT_ALERTS; die "error($?) reading $file: $!" unless $? == 0;
return \%info;
}
######################################################################
sub WriteRecentUrgentEmailAnnouncements ($$$) {
my($dir, $server, $info) = @_;
2015-09-07 16:28:55 -07:00
my $file = File::Spec->catfile($dir, $server . '.urgent-email-announce-recent');
print STDERR "writing to $file\n" if $VERBOSE;
open(RECENT_ALERTS, '>:encoding(UTF-8)', $file) or die "unable to open $file for reading: $!";
2015-09-07 16:28:55 -07:00
foreach my $key (sort keys %$info) {
print STDERR "\"$key\": $info->{$key}\n" if $VERBOSE;
print RECENT_ALERTS "\"$key\": $info->{$key}\n";
2015-09-07 16:28:55 -07:00
}
close RECENT_ALERTS; die "error($?) writing $file: $!" unless $? == 0;
print STDERR "Closed $file\n" if $VERBOSE;
2015-09-07 16:28:55 -07:00
}
######################################################################
# Test if network is up
if (-f "$ENV{HOME}/.shoddy-network") {
print "\${color5}Shoddy Network, no urgent email check!\n";
exit 0;
}
2015-09-07 16:28:55 -07:00
system('/bin/ping -q -w 20 -c 5 pine.sfconservancy.org > /dev/null 2>&1');
2015-09-07 19:25:30 -07:00
if ($? != 0) {
print "\${color5}Network not up!\n";
exit 0;
}
if (not -f $PASSWORD_FILE) {
print "\${color5}File $PASSWORD_FILE not readable!\n";
2015-09-07 19:25:30 -07:00
exit 0;
}
2015-09-07 16:28:55 -07:00
my $record = "";
my $info = ReadRecentUrgentEmailAnnouncements($DIR, $SERVER);
2015-09-07 16:28:55 -07:00
# open a connection to the IMAP server
use Net::IMAP::Client;
open(PW, "<", $PASSWORD_FILE) or die "unable to open $PASSWORD_FILE to find password!";
2015-09-07 16:28:55 -07:00
my %passwords;
while (my $line = <PW>) {
die "invalid line in password file: $PASSWORD_FILE"
2015-09-07 19:22:54 -07:00
unless $line =~ /^\s*(\S+)\s+(\S+)\s*$/;
2015-09-07 16:28:55 -07:00
$passwords{$1} = $2;
}
close PW;
my $imap = Net::IMAP::Client->new(
server => $SERVER,
user => $USERNAME,
pass => $passwords{$USERNAME},
2015-09-07 16:28:55 -07:00
ssl => 1, # (use SSL? default no)
ssl_verify_peer => 1, # (use ca to verify server, default yes)
# ssl_ca_file => '/etc/ssl/certs/certa.pm', # (CA file used for verify server) or
ssl_ca_path => '/etc/ssl/certs/', # (CA path used for SSL)
port => 993 # (but defaults are sane)
);
$imap->login or
die('Login failed: ' . $imap->last_error);
$imap->select('URGENT');
2015-09-07 19:22:54 -07:00
my $messages = $imap->search('ALL', [ 'DATE' ]);
my $summaries = $imap->get_summaries($messages);
use Data::Dumper;
my(%noticeOutput);
my $now = ParseDate("now");
2015-09-07 19:22:54 -07:00
foreach my $summary (@{$summaries}) {
my($who, $subject) = ($summary->{from}->[0]->name, $summary->subject);
my $datetime = ParseDate($summary->date);
my $output = "";
my $ago = Delta_Format(DateCalc($datetime, $now), "approx", 0, "%dt days");
$ago = Delta_Format(DateCalc($datetime, $now), "approx", 0, "%ht hours")
if (defined $ago and $ago =~ /(^|\s+)0\s+day/);
$ago = Delta_Format(DateCalc($datetime, $now), "approx", 0, "%mt minutes")
if (defined $ago and $ago =~ /(^|\s+)0\s+hour/);
$ago = Delta_Format(DateCalc($datetime, $now), "approx", 0, "%st seconds")
if (defined $ago and $ago =~ /(^|\s+)0\s+minute/);
2015-09-07 19:22:54 -07:00
$ago = "" if not defined $ago;
$subject = "" unless defined $subject;
2015-09-07 19:22:54 -07:00
$subject = (split('\n', $subject))[0];
2015-09-07 19:22:54 -07:00
$subject =~ s/^\s*Re\s*:\s*//i;
next if not defined $subject or $subject =~ /^\s*$/;
$who = "unknown" unless defined $who;
my $record = encode('UTF-8', "$who about $subject");
$noticeOutput{$record}{output} = $output;
$noticeOutput{$record}{datetime} = $datetime;
$noticeOutput{$record}{who} = $who;
$noticeOutput{$record}{subject} = $subject;
}
my $olderCount = 0;
my $oldestNow = $now;
my $SEVEN_DAYS_AGO = DateCalc($now, "-7 days");
my $allOutput = "";
foreach my $record (sort {$noticeOutput{$a}{datetime} cmp $noticeOutput{$b}{datetime} } keys %noticeOutput ) {
my $alreadyDone = (defined $info->{$record} ) ? $info->{$record} : undef;
#and (Delta_Format(DateCalc($info->{$record}, $now), "approx", 0, "%ht") < 96));
# uncomment above and put into expression if you want reminders on older emails you're ignoring in urgent.
$info->{$record} = $now;
if ($noticeOutput{$record}{datetime} lt $SEVEN_DAYS_AGO) {
$olderCount++;
$oldestNow = $alreadyDone if defined $alreadyDone and $alreadyDone lt $oldestNow;
next;
} else {
$allOutput .= $noticeOutput{$record}{output};
2015-09-07 19:22:54 -07:00
}
next if defined $alreadyDone;
$info->{$record} = $now;
my $fh = File::Temp->new();
$fh->unlink_on_destroy( 1 );
my $fname = $fh->filename;
print $fh "Urgent email: ";
print $fh $record;
$fh->close();
# system("$ENV{HOME}/bin/myosd", $fname);
system('/usr/bin/notify-send', '-u', 'critical', '-t', '300000',
'Urgent Email', $record);
system("$ENV{HOME}/bin/myspeakbyfile", $fname)
unless -f "$ENV{HOME}/.silent-running";
2015-09-07 19:22:54 -07:00
}
WriteRecentUrgentEmailAnnouncements($DIR, $SERVER, $info);
2015-09-07 16:28:55 -07:00
if ($allOutput ne "") {
print "\$hr\n\${font :size=17}Urgent Emails:\n$allOutput";
print "\${font Inconsolata:size=13}... and $olderCount emails older than 7 days\n" if $olderCount > 0;
print "\$hr";
}
my $hoursSinceNotify = Delta_Format(DateCalc($oldestNow, $now), "approx", 0, "%ht");
if ($olderCount > 0 and $hoursSinceNotify > 1) {
system('/usr/bin/notify-send', '-u', 'critical', '-t', '300000',
'LONG PENDING URGENT EMAIL',
"$olderCount urgent emails in folder older than 7 days (last remind: $hoursSinceNotify)");
}
2015-09-07 16:28:55 -07:00
###############################################################################
#
# Local variables:
# compile-command: "perl -c urgent-mail-check.plx"
# End: