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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
|
#!/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.79 2010-01-06 16:50:26 louise 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/../commonlib/perllib";
use Encode;
use Error qw(:try);
use File::Slurp;
use JSON;
use LWP::Simple;
use CGI; # Trying awkward kludge
use CronFns;
use EastHantsWSDL;
use Cobrand;
use Utils;
use mySociety::Config;
use mySociety::DBHandle qw(dbh);
use mySociety::Email;
use mySociety::EmailUtil;
use mySociety::Locale;
use mySociety::MaPit;
use mySociety::Random qw(random_bytes);
use mySociety::Web qw(ent);
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)
);
}
# Set up site, language etc.
my ($verbose, $nomail) = CronFns::options();
my $base_url = mySociety::Config::get('BASE_URL');
my $site = CronFns::site($base_url);
my $query = "SELECT id, council, category, title, detail, name, email, phone,
used_map, latitude, longitude, (photo is not null) as has_photo, lang,
cobrand, cobrand_data
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);
my $cobrand;
my $cobrand_data;
foreach my $row (@$unsent) {
$cobrand = $row->{cobrand};
$cobrand_data = $row->{cobrand_data};
# Cobranded and non-cobranded messages can share a database. In this case, the conf file
# should specify a vhost to send the reports for each cobrand, so that they don't get sent
# more than once if there are multiple vhosts running off the same database. The email_host
# call checks if this is the host that sends mail for this cobrand.
next unless (Cobrand::email_host($cobrand));
Cobrand::set_lang_and_domain($cobrand, $row->{lang}, 1);
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;
}
my $send_email = 0;
my $send_web = 0;
mySociety::Locale::change($row->{lang});
# Template variables for the email
my $email_base_url = Cobrand::base_url_for_emails($cobrand, $cobrand_data);
my %h = map { $_ => $row->{$_} } qw/title detail name email phone category latitude longitude/;
$h{url} = $email_base_url . '/report/' . $row->{id};
$h{phone_line} = $h{phone} ? _('Phone:') . " $h{phone}\n\n" : '';
if ($row->{has_photo}) {
$h{has_photo} = _("This web page also contains a photo of the problem, provided by the user.") . "\n\n";
$h{image_url} = $email_base_url . '/photo?id=' . $row->{id};
} else {
$h{has_photo} = '';
$h{image_url} = '';
}
$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} = '';
# If we are in the UK include eastings and northings, and nearest stuff
$h{easting_northing} = '';
if ( mySociety::Config::get('COUNTRY') eq 'GB' ) {
( $h{easting}, $h{northing} ) = Utils::convert_latlon_to_en( $h{latitude}, $h{longitude} );
# email templates don't have conditionals so we need to farmat this here
$h{easting_northing} #
= "Easting: $h{easting}\n\n" #
. "Northing: $h{northing}\n\n";
$h{closest_address} = find_closest($row, $h{latitude}, $h{longitude});
}
$h{closest_address_machine} = $h{closest_address};
my (@to, @recips, $template, $areas_info);
if ($site eq 'emptyhomes') {
my $council = $row->{council};
$areas_info = mySociety::MaPit::call('areas', $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);
$send_email = 1;
$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);
$areas_info = mySociety::MaPit::call('areas', \@all_councils);
my (@dear, %recips);
my $all_confirmed = 1;
foreach my $council (@councils) {
my $name = $areas_info->{$council}->{name};
push @dear, $name;
if ($council == 2330) { # E. Hants have a web service
$send_web = 1;
$h{category} = 'Customer Services' if $h{category} eq 'Other';
} else {
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->{latitude}, $row->{longitude}) if $council == 2225;
$council_email = oxfordshire_contact($row->{latitude}, $row->{longitude}) if $council == 2237 && $council_email eq 'SPECIAL';
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 ];
$recips{$council_email} = 1;
$send_email = 1;
}
}
@recips = keys %recips;
next unless $all_confirmed;
$template = 'submit-council';
$template = 'submit-brent' if $row->{council} eq 2488 || $row->{council} eq 2237;
$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_line} = '';
} else {
$h{category_footer} = "'" . $h{category} . "'";
$h{category_line} = sprintf(_("Category: %s"), $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} = '[ '
. sprintf(_('We realise this problem might be the responsibility of %s; however, we don\'t currently have any contact details for them. If you know of an appropriate contact address, please do get in touch.'), $name)
. " ]\n\n";
}
}
unless ($send_email || $send_web) {
die 'Report not going anywhere for ID ' . $row->{id} . '!';
}
my $testing_email = mySociety::Config::get('TESTING_EMAIL');
if ($row->{email} eq $testing_email) {
@recips = ( $testing_email );
$send_web = 0;
$send_email = 1;
} elsif (mySociety::Config::get('STAGING_SITE')) {
# on a staging server send emails to ourselves rather than the councils
@recips = ( mySociety::Config::get('CONTACT_EMAIL') );
} elsif ($site eq 'emptyhomes') {
my $council = $row->{council};
my $country = $areas_info->{$council}->{country};
if ($country eq 'W') {
push @recips, 'shelter@' . mySociety::Config::get('EMAIL_DOMAIN');
} else {
push @recips, 'eha@' . mySociety::Config::get('EMAIL_DOMAIN');
}
push @recips, 'matthew@' . mySociety::Config::get('EMAIL_DOMAIN');
}
# 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);
# }
# Multiply results together, so one success counts as a success.
my $result = -1;
if ($send_email) {
$template = _($template);
my $email = mySociety::Locale::in_gb_locale { mySociety::Email::construct_email({
_template_ => $template,
_parameters_ => \%h,
To => \@to,
From => [ $row->{email}, $row->{name} ],
'Message-ID' => sprintf('<report-%s-%s@mysociety.org>', time(), unpack('h*', random_bytes(5, 1))),
}) };
if (!$nomail) {
$result *= mySociety::EmailUtil::send_email($email, mySociety::Config::get('CONTACT_EMAIL'), @recips);
} else {
print $email;
}
}
if ($send_web) {
$h{message} = construct_easthants_message(%h);
if (!$nomail) {
$result *= post_easthants_message(%h);
}
}
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";
}
}
}
sub _get_district_for_contact {
my ( $lat, $lon ) = @_;
my $district =
mySociety::MaPit::call( 'point', "4326/$lon,$lat", type => 'DIS' );
($district) = keys %$district;
return $district;
}
# 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 $district = _get_district_for_contact(@_);
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";
}
# Oxfordshire has different contact addresses depending upon the district
sub oxfordshire_contact {
my $district = _get_district_for_contact(@_);
my $email;
$email = 'northernarea' if $district == 2419 || $district == 2420 || $district == 2421;
$email = 'southernarea' if $district == 2417 || $district == 2418;
die "Returned district $district which is not in Oxfordshire!" unless $email;
return "$email\@oxfordshire.gov.uk";
}
sub construct_easthants_message {
my %h = @_;
my $message = '';
$message .= "[ This report was also sent to the district council covering the location of the problem, as the user did not categorise it; please ignore if you're not the correct council to deal with the issue. ]\n\n"
if $h{multiple};
$message .= <<EOF;
Subject: $h{title}
Details: $h{detail}
$h{fuzzy}, or to provide an update on the problem, please visit the following link:
$h{url}
$h{closest_address}
EOF
return $message;
}
my $eh_service;
sub post_easthants_message {
my %h = @_;
my $return = 1;
$eh_service ||= EastHantsWSDL->on_fault(sub { my($soap, $res) = @_; die ref $res ? $res->faultstring : $soap->transport->status, "\n"; });
try {
# ServiceName, RemoteCreatedBy, Salutation, FirstName, Name, Email, Telephone, HouseNoName, Street, Town, County, Country, Postcode, Comments, FurtherInfo, ImageURL
my $message = ent(encode_utf8($h{message}));
my $name = ent(encode_utf8($h{name}));
my $result = $eh_service->INPUTFEEDBACK(
$h{category}, 'FixMyStreet', '', '', $name, $h{email}, $h{phone},
'', '', '', '', '', '', $message, 'Yes', $h{image_url}
);
$return = 0 if $result eq 'Report received';
} otherwise {
my $e = shift;
print "Caught an error: $e\n";
};
return $return;
}
sub find_closest {
my ($row, $latitude, $longitude) = @_;
my $str = '';
return '' unless $row->{used_map};
# Get nearest road-type thing from Bing
my $url = "http://dev.virtualearth.net/REST/v1/Locations/$latitude,$longitude?c=en-GB&key=" . mySociety::Config::get('BING_MAPS_API_KEY');
my $j = LWP::Simple::get($url);
if ($j) {
$j = JSON->new->utf8->allow_nonref->decode($j);
if ($j->{resourceSets}[0]{resources}[0]{name}) {
$str .= "Nearest road to the pin placed on the map (automatically generated by Bing Maps): $j->{resourceSets}[0]{resources}[0]{name}\n\n";
}
}
# Get nearest postcode from Matthew's random gazetteer (put in MaPit? Or elsewhere?)
$url = "http://gazetteer.dracos.vm.bytemark.co.uk/point/$latitude,$longitude.json";
$j = LWP::Simple::get($url);
if ($j) {
$j = JSON->new->utf8->allow_nonref->decode($j);
if ($j->{postcode}) {
$str .= "Nearest postcode to the pin placed on the map (automatically generated): $j->{postcode}[0] ($j->{postcode}[1]m away)\n\n";
}
}
return $str;
}
|