Skip to content

Commit

Permalink
[Echo] Downtime script.
Browse files Browse the repository at this point in the history
  • Loading branch information
dracos committed Jul 23, 2024
1 parent f312cd6 commit 0e9e842
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions bin/handlemail-echodowntime
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/usr/bin/env perl
#
# handlemail-echodowntime:
# Handle an individual incoming mail message.
#
# This script should be invoked through the .forward mechanism.
# It processes emails from Echo about its downtime.

use v5.14;
use warnings;

use File::Basename qw(dirname);
use File::Spec;
my $root;

BEGIN {
$root = dirname(File::Spec->rel2abs($0)) . '/../..';
require "$root/fixmystreet/setenv.pl";
}

use Time::Piece;
use Path::Tiny;
use mySociety::HandleMail;
use FixMyStreet;
use FixMyStreet::Email::Sender;

my %data = mySociety::HandleMail::get_message();
my $m = $data{message} or exit 0;

my $out = "$root/data/echo-downtime.csv";

my $body = join " ", @{$m->body};
if ($body =~ /will be taken offline on\s+(.*?)\s+at\s+(.*?)\s+for\s+approximately\s+(\d+)\s+hours/s) {
my ($date, $time, $length) = ($1, $2, $3);

my $year = localtime->year;
$date =~ s/(?<=\d)(st|nd|rd|th)//;
my $start = eval { Time::Piece->strptime("$date $year $time", '%A %d %B %Y %H:%M'); };
exit 0 if $@;

my $end = $start + $length * 60 * 60;

path($out)->append([ $start->datetime, ',', $end->datetime, "\n" ]);
forward_on($start, $end);
}

exit 0;

# ---

sub forward_on {
my ($start, $end) = @_;
my $from = FixMyStreet->config('CONTACT_EMAIL');
my ($to) = $m->head()->get('From') =~ /<(.*)>/;
my ($id) = $m->head()->get('Message-ID') =~ /(<.*>)/;
my $subject = $m->head()->get('Subject');
my @lines = (
"Subject: Re: $subject",
"In-Reply-To: $id",
"From: $from",
"To: $to",
"",
"Email noticed and downtime logged for $start to $end",
);
unless (FixMyStreet::Email::Sender->try_to_send(
join("\n", @lines) . "\n",
{
from => $from,
to => $to,
}
)) {
exit 75;
}
exit 0;
}

0 comments on commit 0e9e842

Please sign in to comment.