#!/usr/bin/perl -w # send-reports: # Send new problem reports to councils # # Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved. # Email: matthew@mysociety.org. WWW: http://www.mysociety.org # # $Id: send-reports,v 1.59 2008-10-09 15:23:08 matthew Exp $ use strict; require 5.8.0; # Horrible boilerplate to set up appropriate library paths. use FindBin; use lib "$FindBin::Bin/../perllib"; use lib "$FindBin::Bin/../../perllib"; use File::Slurp; use mySociety::Config; use mySociety::DBHandle qw(dbh); use mySociety::Email; use mySociety::EmailUtil; use mySociety::MaPit; use mySociety::Random qw(random_bytes); BEGIN { mySociety::Config::set_file("$FindBin::Bin/../conf/general"); mySociety::DBHandle::configure( Name => mySociety::Config::get('BCI_DB_NAME'), User => mySociety::Config::get('BCI_DB_USER'), Password => mySociety::Config::get('BCI_DB_PASS'), Host => mySociety::Config::get('BCI_DB_HOST', undef), Port => mySociety::Config::get('BCI_DB_PORT', undef) ); } use mySociety::Dress; die "Either no arguments, --nomail or --verbose" if (@ARGV>1); my $nomail = 0; my $verbose = 0; $nomail = 1 if (@ARGV==1 && $ARGV[0] eq '--nomail'); $verbose = 1 if (@ARGV==1 && $ARGV[0] eq '--verbose'); $verbose = 1 if $nomail; my $base_url = mySociety::Config::get('BASE_URL'); my $site = 'fixmystreet'; $site = 'emptyhomes' if $base_url =~ 'emptyhomes' || $base_url eq 'http://matthew.fixmystreet.com'; # XXX my $query = "SELECT id, council, category, title, detail, name, email, phone, used_map, easting, northing, (photo is not null) as has_photo FROM problem WHERE state in ('confirmed','fixed') AND whensent IS NULL AND council IS NOT NULL"; my $unsent = dbh()->selectall_arrayref($query, { Slice => {} }); my (%notgot, %note); foreach my $row (@$unsent) { if (dbh()->selectrow_array('select email from abuse where lower(email)=?', {}, lc($row->{email}))) { dbh()->do("update problem set state='hidden' where id=?", {}, $row->{id}); dbh()->commit(); next; } # Template variables for the email my %h = map { $_ => $row->{$_} } qw/title detail name email phone category easting northing/; $h{phone} = "Phone: $h{phone}\n\n" if $h{phone}; $h{has_photo} = ''; $h{has_photo} = "This web page also contains a photo of the problem, provided by the user.\n\n" if $row->{has_photo}; $h{url} = $base_url . '/report/' . $row->{id}; $h{fuzzy} = $row->{used_map} ? 'To view a map of the precise location of this issue' : 'The user could not locate the problem on a map, but to see the area around the location they entered'; $h{closest_address} = ''; my ($address, $distance) = mySociety::Dress::find_nearest($row->{easting}, $row->{northing}); if ($address) { $h{closest_address} = sprintf("The closest address, as the crow flies, to the location of this problem, %.0fm away, is: %s - please note that this is automatically generated, so ensure that you check it against the details provided by the user and, if possible, the map.\n\n", $distance, $address); ($h{closest_address_machine} = $h{closest_address}) =~ s/is: /is:\n\n/; $h{closest_address_machine} =~ s/ - please note/\n\n - please note/; } my (@to, @recips, $template); if ($site eq 'emptyhomes') { my $council = $row->{council}; my $areas_info = mySociety::MaPit::get_voting_areas_info([ $council ]); my $name = $areas_info->{$council}->{name}; my ($council_email, $confirmed, $note) = dbh()->selectrow_array( "SELECT email,confirmed,note FROM contacts WHERE deleted='f' and area_id=? AND category=?", {}, $council, 'Empty Property'); unless ($confirmed) { $note = 'Council ' . $council . ' deleted' unless $note; $council_email = 'N/A' unless $council_email; $notgot{$council_email}{$row->{category}}++; $note{$council_email}{$row->{category}} = $note; next; } push @to, [ $council_email, $name ]; @recips = ($council_email); $template = File::Slurp::read_file("$FindBin::Bin/../templates/emails/submit-eha"); } else { # XXX Needs locks! my @all_councils = split /,|\|/, $row->{council}; my ($councils, $missing) = $row->{council} =~ /^([\d,]+)(?:\|([\d,]+))?/; my @councils = split /,/, $councils; my $areas_info = mySociety::MaPit::get_voting_areas_info(\@all_councils); my (@dear, %recips); my $all_confirmed = 1; foreach my $council (@councils) { my $name = $areas_info->{$council}->{name}; my ($council_email, $confirmed, $note) = dbh()->selectrow_array( "SELECT email,confirmed,note FROM contacts WHERE deleted='f' and area_id=? AND category=?", {}, $council, $row->{category}); $council_email = essex_contact($row->{easting}, $row->{northing}) if $council == 2225; $council_email = notts_contact($row->{easting}, $row->{northing}) if $council == 2236; unless ($confirmed) { $all_confirmed = 0; $note = 'Council ' . $row->{council} . ' deleted' unless $note; $council_email = 'N/A' unless $council_email; $notgot{$council_email}{$row->{category}}++; $note{$council_email}{$row->{category}} = $note; } push @to, [ $council_email, $name ]; push @dear, $name; $recips{$council_email} = 1; } @recips = keys %recips; if (!@to) { if ($verbose) { print 'Need to send problem #' . $row->{id} . ' to council(s) ' . join(',',@recips) . "\n"; print " ...but we have no contact details for any of them!\n"; } next; } next unless ($all_confirmed); $template = 'submit-council'; $template = 'submit-brent' if $row->{council} eq 2488; $template = File::Slurp::read_file("$FindBin::Bin/../templates/emails/$template"); if ($h{category} eq 'Other') { $h{category_footer} = 'this type of local problem'; $h{category} = ''; } else { $h{category_footer} = "'" . $h{category} . "'"; $h{category} = "Category: $h{category}\n\n"; } $h{councils_name} = join(' and ', @dear); $h{multiple} = @dear>1 ? "[ This email has been sent to both councils covering the location of the problem, as the user did not categorise it; please ignore it if you're not the correct council to deal with the issue, or let us know what category of problem this is so we can add it to our system. ]\n\n" : ''; $h{missing} = ''; if ($missing) { my $name = $areas_info->{$missing}->{name}; $h{missing} = '[ We realise this problem might be the responsibility of ' . $name . "; however, we don't currently have any contact details for them. If you know of an appropriate contact address, please do get in touch. ]\n\n"; } } unless (@recips) { die 'Email not going anywhere for ID ' . $row->{id} . '!'; } if (mySociety::Config::get('STAGING_SITE')) { @recips = ( mySociety::Config::get('CONTACT_EMAIL') ); } else { push @recips, mySociety::Config::get('CONTACT_EMAIL'); } # Special case for this parish council # if ($address && $address =~ /Sprowston/ && $row->{council} == 2233 && $row->{category} eq 'Street lighting') { # $h{councils_name} = 'Sprowston Parish Council'; # my $e = 'parishclerk' . '@' . 'sprowston-pc.gov.uk'; # @to = ( [ $e, $h{councils_name} ] ); # @recips = ($e); # } my $email = mySociety::Email::construct_email({ _template_ => $template, _parameters_ => \%h, To => \@to, From => [ $row->{email}, $row->{name} ], 'Message-ID' => sprintf('', time(), unpack('h*', random_bytes(5, 1))), }); my $result; if ($nomail) { $result = -1; } else { $result = mySociety::EmailUtil::send_email($email, mySociety::Config::get('CONTACT_EMAIL'), @recips); } if ($result == mySociety::EmailUtil::EMAIL_SUCCESS) { dbh()->do('UPDATE problem SET whensent=ms_current_timestamp(), lastupdate=ms_current_timestamp() WHERE id=?', {}, $row->{id}); dbh()->commit(); } else { dbh()->rollback(); } } if ($verbose) { print "Council email addresses that need checking:\n" if keys %notgot; foreach my $e (keys %notgot) { foreach my $c (keys %{$notgot{$e}}) { print $notgot{$e}{$c} . " problem, to $e category $c (" . $note{$e}{$c}. ")\n"; } } } # Essex has different contact addresses depending upon the district # Might be easier if we start storing in the db all areas covered by a point # Will do for now :) sub essex_contact { my ($E, $N) = @_; my $district = mySociety::MaPit::get_voting_areas_by_location({easting=>$E, northing=>$N}, 'polygon', 'DIS'); ($district) = keys %$district; my $email; $email = 'eastarea' if $district == 2315 || $district == 2312; $email = 'midarea' if $district == 2317 || $district == 2314 || $district == 2316; $email = 'southarea' if $district == 2319 || $district == 2320 || $district == 2310; $email = 'westarea' if $district == 2309 || $district == 2311 || $district == 2318 || $district == 2313; die "Returned district $district which is not in Essex!" unless $email; return "highways.$email\@essexcc.gov.uk"; } # Notts has different contact addresses depending upon the district sub notts_contact { my ($E, $N) = @_; my $district = mySociety::MaPit::get_voting_areas_by_location({easting=>$E, northing=>$N}, 'polygon', 'DIS'); ($district) = keys %$district; my $email; $email = 'cshighsouth.en' if $district == 2411 || $district == 2412 || $district == 2415; $email = 'etwall' if $district == 2413 || $district == 2416; $email = 'bassetlawao' if $district == 2410; $email = 'newarkao' if $district == 2414; die "Returned district $district which is not in Nottinghamshire!" unless $email; return "$email\@nottscc.gov.uk"; }