3 # This script is invoked when the yarrg 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.
29 # clientname "ypp-sc-tools"
30 # clientversion 2.1-g2e06a26 [from git-describe --tags HEAD]
31 # clientfixes "lastpage" [space separated list]
33 # Timestamp requests contain:
37 # ocean canonical mixed case
38 # island canonical mixed case
39 # data filename=deduped.tsv.gz output of yarrg --tsv
42 use strict (qw(vars));
48 $CGI::POST_MAX= 3*1024*1024;
50 use CGI qw/:standard -private_tempfiles/;
52 setlocale(LC_CTYPE, "en_GB.UTF-8");
54 my $re_any= "^(.*)\$";
56 parse_info_serverside();
60 print header(-status=>'400 Bad commodity update',
62 -charset=>'us-ascii');
63 print "Error: $msg\n";
70 fail("missing form parameter $n") unless defined $v;
71 fail("invalid form parameter $n ($re)") unless $v =~ m/$re/;
77 $o{'clientname'}= must_param('clientname',$re_any);
78 my $clientinfo= $clients{$o{'clientname'}};
79 fail('unknown client') unless defined $clientinfo;
81 my $clientfixes= must_param('clientfixes', "^([-0-9a-z ]*)\$");
82 my @clientfixes= sort grep { m/./ } split /\s+/, $clientfixes;
83 $o{'clientfixes'}= "@clientfixes";
84 foreach my $bug (@$clientinfo) {
85 fail("client out of date - missing bugfix \`$bug'")
86 unless grep { $_ eq $bug } @clientfixes;
89 $o{'clientversion'}= must_param('clientversion', "^(\\d[-+._0-9a-zA-Z]+)\$");
91 if (param('requesttimestamp')) {
92 my $now= time; defined $now or die;
93 print header(-type=>'text/plain', -charset=>'us-ascii'), "OK $now.\n";
97 $o{'ocean'}= must_param('ocean', $re_any);
98 $o{'island'}= must_param('island', $re_any);
100 my $arches= $oceans{$o{'ocean'}};
101 fail("unknown ocean") unless $arches;
104 foreach my $islands (values %$arches) {
105 my $sources= $islands->{$o{'island'}};
106 next unless $sources;
107 die if $island_found;
108 $island_found= $sources;
110 fail("unknown island") unless $island_found;
112 $o{'timestamp'}= must_param('timestamp', "^([1-9]\\d{1,20})\$");
113 my $now= time; defined $now or die $!;
114 fail("clock skew") if $o{'timestamp'} >= $now;
116 my $indatafh= upload('data');
117 defined $indatafh or fail("data is not a file");
118 my $datafile= must_param('data',"^(deduped\\.tsv\\.gz)\$");
120 my $mcontent= MIME::Entity->build(To => 'yarrg-commod-updates',
121 Type => 'multipart/mixed',
125 get_our_version(\%o, 'server');
126 foreach my $cs (qw(client server)) {
127 $o{"${cs}spec"}= join "\t", map { $o{$cs.$_} } qw(name version fixes);
134 my $i=0; grep { return $i if $_ eq $v; $i++ } qw(ocean island timestamp);
135 sprintf "z %d %s", (length $v) / 8, $v;
138 foreach my $vn (sort { ksmap($a) cmp ksmap($b) } keys %o) {
140 die if $val =~ m/\n|\r/;
141 $metadata .= "$vn\t$o{$vn}\n";
144 my $mdpart= MIME::Entity->build(Top => 0,
145 Type => 'text/plain',
147 Disposition => 'inline',
148 Encoding => 'quoted-printable',
149 Filename => 'metadata',
151 $mcontent->add_part($mdpart);
153 my $gunzchild= open(GZ, "-|"); defined $gunzchild or die $!;
155 open STDIN, "<&=", $indatafh or die $!;
156 exec 'gunzip'; die $!;
159 my $dedupedtsv= pipethrough_prep();
162 my @v= check_tsv_line($_,\&fail);
163 print $dedupedtsv join('\t',@v),"\n" or die $!;
166 GZ->error and die $!;
167 $?=0; close GZ; $? and fail("gunzip for check failed code $?");
169 my $launderedgz= pipethrough_run($dedupedtsv,undef,'gzip','gzip');
171 my $mdatafile= MIME::Entity->build(Top => 0,
172 Type => 'application/octet-stream',
173 Disposition => 'attachment',
174 Encoding => 'base64',
175 Filename => 'deduped.tsv.gz',
176 Data => $launderedgz);
177 $mcontent->add_part($mdatafile);
179 open M, "|/usr/sbin/sendmail -t -oi -oee -odb"
180 or fail("fork sendmail failed! ($!)");
181 $mcontent->print(\*M);
183 M->error and fail("write sendmail failed! ($!)");
184 $?=0; close M; $? and fail("sendmail failed code $?");
186 print header(-type=>'text/plain', -charset=>'us-ascii'),