chiark / gitweb /
WIP commod-update-receiver testing - seems to work
[ypp-sc-tools.web-live.git] / pctb / commod-update-receiver
1 #!/usr/bin/perl -w
2 #
3 # This script is invoked when the YPP SC PCTB client uploads to
4 # the chiark database.
5
6 # This is part of ypp-sc-tools, a set of third-party tools for assisting
7 # players of Yohoho Puzzle Pirates.
8 #
9 # Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
10 #
11 # This program is free software: you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation, either version 3 of the License, or
14 # (at your option) any later version.
15 #
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 #
24 # Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
25 # are used without permission.  This program is not endorsed or
26 # sponsored by Three Rings.
27
28
29 # Uploads contain:
30 #  ocean                        canonical mixed case
31 #  island                       canonical mixed case
32 #  clientname                   "ypp-sc-tools"
33 #  clientversion                2.1-g2e06a26  [from git-describe --tags HEAD]
34 #  clientfixes                  "lastpage"  [space separated list]
35 #  data filename=deduped.tsv.gz output of ypp-commodities --tsv
36
37 use strict (qw(vars));
38 use POSIX;
39 use MIME::Entity;
40
41 use Commods;
42
43 $CGI::POST_MAX= 3*1024*1024;
44
45 use CGI qw/:standard -private_tempfiles/;
46
47 setlocale(LC_CTYPE, "en_GB.UTF-8");
48
49 my $re_any= "^(.*)\$";
50
51 parse_masters();
52
53 sub fail ($) {
54     my ($msg) = @_;
55     print header(-status=>'400 Bad commodity update',
56                  -type=>'text/plain',
57                  -charset=>'us-ascii');
58     print "Error: $msg\n";
59     exit 0;
60 }
61
62 sub must_param ($$) {
63     my ($n,$re)= @_;
64     my $v= param($n);
65     fail("missing form parameter $n") unless defined $v;
66     fail("invalid form parameter $n ($re)") unless $v =~ m/$re/;
67     return $1;
68 }
69
70 my %o;
71
72 $o{'clientname'}= must_param('clientname',$re_any);
73 my $clientinfo= $clients{$o{'clientname'}};
74 fail('unknown client') unless defined $clientinfo;
75
76 my $clientfixes= must_param('clientfixes', "^([-0-9a-z ]*)\$");
77 my @clientfixes= sort grep { m/./ } split /\s+/, $clientfixes;
78 $o{'clientfixes'}= "@clientfixes";
79 foreach my $bug (@$clientinfo) {
80     fail("client out of date - missing bugfix \`$bug'")
81         unless grep { $_ eq $bug } @clientfixes;
82 }
83
84 $o{'clientversion'}= must_param('clientversion', "^(\\d[-+._0-9a-zA-Z]+)\$");
85
86 $o{'ocean'}= must_param('ocean', $re_any);
87 $o{'island'}= must_param('island', $re_any);
88
89 my $arches= $oceans{$o{'ocean'}};
90 fail("unknown ocean") unless $arches;
91
92 my $island_found= 0;
93 foreach my $islands (values %$arches) {
94     my $sources= $islands->{$o{'island'}};
95     next unless $sources;
96     die if $island_found;
97     $island_found= $sources;
98 }
99 fail("unknown island") unless $island_found;
100
101 $o{'timestamp'}= must_param('timestamp', "^([1-9]\\d{1,20})\$");
102 my $now= time;  defined $now or die $!;
103 fail("clock skew") if $o{'timestamp'} >= $now;
104
105 die if $o{'ocean'} =~ m/\=/;
106 die if $o{'island'} =~ m/\=/;
107
108 my $indatafh= upload('data');
109 defined $indatafh or fail("data is not a file");
110 my $datafile= must_param('data',"^(deduped\\.tsv\\.gz)\$");
111
112 my $mcontent= MIME::Entity->build(To => 'yarrg-commod-updates',
113                                   Type => 'multipart/mixed',
114                                   Boundary => '=',
115                                   Charset => 'utf-8');
116
117 get_our_version(\%o, 'server');
118 foreach my $cs (qw(client server)) {
119     $o{"${cs}spec"}= join ' ', map { $o{$cs.$_} } qw(name version fixes);
120 }
121
122 foreach my $vn (sort keys %o) {
123     my $mpart= MIME::Entity->build(Top => 0,
124                                    Type => 'text/plain',
125                                    Charset => 'utf-8',
126                                    Disposition => 'attachment',
127                                    Filename => $vn,
128                                    Data => $o{$vn});
129     $mcontent->add_part($mpart);
130 }
131
132 my $gunzchild= open(GZ, "-|"); defined $gunzchild or die $!;
133 if (!$gunzchild) {
134     open STDIN, "<&=", $indatafh or die $!;
135     exec 'gunzip'; die $!;
136 }
137
138 sub bad_data ($) { fail("bad data: line $.: $_[0]"); }
139
140 our %done;
141
142 while (<GZ>) {
143     !m/\P{IsPrint}/ or die bad_data('nonprinting char(s)');
144     !m/\\/ or bad_data('data contains backslashes');
145     my @v= split /\t/;
146     @v==6 or bad_data('wrong number of fields');
147     my ($commod,$stall) = @v;
148     defined $commods{$commod} or bad_data("unknown commodity \`$commod'");
149     $stall =~ m/^\p{IsUpper}|^[0-9]/ or bad_data("stall not capitalised");
150     !exists $done{$commod,$stall} or bad_data("repeated data");
151     $done{$commod,$stall}= 1;
152     foreach my $i (2..5) {
153         my $f= $v[$i];
154         $f =~ m/^(|0|[1-9][0-9]{0,5}|\>1000)$/ or bad_data("bad field $i");
155         ($i % 2) or ($f !~ m/\>/) or bad_data("> in field $i price");
156     }
157 }
158 GZ->error and die $!;
159 $?=0; close GZ; $? and fail("gunzip for check failed code $?");
160
161 my $mdatafile= MIME::Entity->build(Top => 0,
162                                    Type => 'application/octet-stream',
163                                    Disposition => 'attachment',
164                                    Encoding => 'base64',
165                                    Filename => 'deduped.tsv.gz',
166                                    Path => $datafile);
167 $mcontent->add_part($mdatafile);
168
169 open M, "|/usr/sbin/sendmail -t -oi -oee -odb"
170     or fail("fork sendmail failed! ($!)");
171 $mcontent->print(\*M);
172
173 M->error and fail("write sendmail failed! ($!)");
174 $?=0; close M; $? and fail("sendmail failed code $?");
175
176 print header(-type=>'text/plain', -charset=>'us-ascii'),
177       "OK\n"
178     or die $!;