3 # This script is invoked when the YPP SC PCTB client uploads to
6 # This is part of ypp-sc-tools, a set of third-party tools for assisting
7 # players of Yohoho Puzzle Pirates.
9 # Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
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.
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.
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/>.
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.
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
38 use strict (qw(vars));
44 $CGI::POST_MAX= 3*1024*1024;
46 use CGI qw/:standard -private_tempfiles/;
48 setlocale(LC_CTYPE, "en_GB.UTF-8");
50 my $re_any= "^(.*)\$";
56 print header(-status=>'400 Bad commodity update',
58 -charset=>'us-ascii');
59 print "Error: $msg\n";
66 fail("missing form parameter $n") unless defined $v;
67 fail("invalid form parameter $n ($re)") unless $v =~ m/$re/;
73 $o{'clientname'}= must_param('clientname',$re_any);
74 my $clientinfo= $clients{$o{'clientname'}};
75 fail('unknown client') unless defined $clientinfo;
77 my $clientfixes= must_param('clientfixes', "^([-0-9a-z ]*)\$");
78 my @clientfixes= sort grep { m/./ } split /\s+/, $clientfixes;
79 $o{'clientfixes'}= "@clientfixes";
80 foreach my $bug (@$clientinfo) {
81 fail("client out of date - missing bugfix \`$bug'")
82 unless grep { $_ eq $bug } @clientfixes;
85 $o{'clientversion'}= must_param('clientversion', "^(\\d[-+._0-9a-zA-Z]+)\$");
87 $o{'ocean'}= must_param('ocean', $re_any);
88 $o{'island'}= must_param('island', $re_any);
90 my $arches= $oceans{$o{'ocean'}};
91 fail("unknown ocean") unless $arches;
94 foreach my $islands (values %$arches) {
95 my $sources= $islands->{$o{'island'}};
98 $island_found= $sources;
100 fail("unknown island") unless $island_found;
102 $o{'timestamp'}= must_param('timestamp', "^([1-9]\\d{1,20})\$");
103 my $now= time; defined $now or die $!;
104 fail("clock skew") if $o{'timestamp'} >= $now;
106 my $indatafh= upload('data');
107 defined $indatafh or fail("data is not a file");
108 my $datafile= must_param('data',"^(deduped\\.tsv\\.gz)\$");
110 my $mcontent= MIME::Entity->build(To => 'yarrg-commod-updates',
111 Type => 'multipart/mixed',
115 get_our_version(\%o, 'server');
116 foreach my $cs (qw(client server)) {
117 $o{"${cs}spec"}= join "\t", map { $o{$cs.$_} } qw(name version fixes);
124 my $i=0; grep { return $i if $_ eq $v; $i++ } qw(ocean island timestamp);
125 sprintf "z %d %s", (length $v) / 8, $v;
128 foreach my $vn (sort { ksmap($a) cmp ksmap($b) } keys %o) {
130 die if $val =~ m/\n|\r|\t/;
131 $metadata .= "$vn\t$o{$vn}\n";
134 my $mdpart= MIME::Entity->build(Top => 0,
135 Type => 'text/plain',
137 Disposition => 'inline',
138 Encoding => 'quoted-printable',
139 Filename => 'metadata',
141 $mcontent->add_part($mdpart);
143 my $gunzchild= open(GZ, "-|"); defined $gunzchild or die $!;
145 open STDIN, "<&=", $indatafh or die $!;
146 exec 'gunzip'; die $!;
149 sub bad_data ($) { fail("bad data: line $.: $_[0]"); }
154 !m/\P{IsPrint}/ or die bad_data('nonprinting char(s)');
155 !m/\\/ or bad_data('data contains backslashes');
157 @v==6 or bad_data('wrong number of fields');
158 my ($commod,$stall) = @v;
159 defined $commods{$commod} or bad_data("unknown commodity \`$commod'");
160 $stall =~ m/^\p{IsUpper}|^[0-9]/ or bad_data("stall not capitalised");
161 !exists $done{$commod,$stall} or bad_data("repeated data");
162 $done{$commod,$stall}= 1;
163 foreach my $i (2..5) {
165 $f =~ m/^(|0|[1-9][0-9]{0,5}|\>1000)$/ or bad_data("bad field $i");
166 ($i % 2) or ($f !~ m/\>/) or bad_data("> in field $i price");
169 GZ->error and die $!;
170 $?=0; close GZ; $? and fail("gunzip for check failed code $?");
172 my $mdatafile= MIME::Entity->build(Top => 0,
173 Type => 'application/octet-stream',
174 Disposition => 'attachment',
175 Encoding => 'base64',
176 Filename => 'deduped.tsv.gz',
178 $mcontent->add_part($mdatafile);
180 open M, "|/usr/sbin/sendmail -t -oi -oee -odb"
181 or fail("fork sendmail failed! ($!)");
182 $mcontent->print(\*M);
184 M->error and fail("write sendmail failed! ($!)");
185 $?=0; close M; $? and fail("sendmail failed code $?");
187 print header(-type=>'text/plain', -charset=>'us-ascii'),