aboutsummaryrefslogtreecommitdiffstats
path: root/bin/handlemail-support
blob: 0ccde8ca77cc9be5c01f3ec3dea3fb6b74590f9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/perl
#
# handlemail-support:
# Handle an individual incoming mail message.
#
# This script should be invoked through the .forward mechanism. It processes
# emails to the support address to remove out of office and so on, before
# forwarding on.
#
# Copyright (c) 2013 UK Citizens Online Democracy. All rights reserved.
# Email: matthew@mysociety.org; WWW: http://www.mysociety.org/

use strict;
use warnings;
require 5.8.0;

BEGIN {
    use File::Basename qw(dirname);
    use File::Spec;
    my $d = dirname(File::Spec->rel2abs($0));
    require "$d/../setenv.pl";
}

use FixMyStreet;
use mySociety::EmailUtil;
use mySociety::HandleMail;

my %data = mySociety::HandleMail::get_message();
exit 0 if is_ignorable($data{message});
forward_on();

# ---

sub forward_on {
    my ($l, $d) = split /\@/, FixMyStreet->config('CONTACT_EMAIL');
    if (mySociety::EmailUtil::EMAIL_SUCCESS
            != mySociety::EmailUtil::send_email(
                join("\n", @{$data{lines}}) . "\n",
                $data{return_path},
                join('@', join('_deli', $l, 'very'), $d)
            )) {
        exit 75;
    }
    exit 0;
}

sub is_ignorable {
    my $m = shift;
    my $head = $m->head();
    my ($from, $subject, $body) = ($head->get('From'), $head->get('Subject'), $m->body);
    $body = join("\n", @$body);

    my $fp = FixMyStreet->path_to("..", "data", "ignored-emails.csv")->open or exit 75;
    while (<$fp>) {
        chomp;
        my ($f, $s, $b) = split /,/;
        next unless $f || $s || $b;
        return 1 unless ( $f && $from !~ /$f/ ) || ( $s && $subject !~ /$s/ ) || ( $b && $body !~ /$b/ );
    }
    return 0;
}