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 our $now= time; defined $now or die $!;
56 my $re_any= "^(.*)\$";
58 parse_info_serverside();
62 addlog("failing $msg");
63 print header(-status=>'400 Bad commodity update',
65 -charset=>'us-ascii');
66 print "Error: $msg\n";
73 fail("missing form parameter $n") unless defined $v;
74 fail("invalid form parameter $n ($re)") unless $v =~ m/$re/;
80 my $midtmp= "_mid-pid$$.hold";
81 open MIDTMP, ">$midtmp" or die "$midtmp $!";
82 stat MIDTMP or die $!;
84 my $midino= "_mid-ino$$.hold";
85 rename $midtmp, $midino or die "$midtmp $midino $!";
86 close MIDTMP or die $!;
88 our $hostname= `hostname -f`; $? and die $?; chomp $hostname or die;
89 our $mid= "<$now.$$.$ino\@$hostname>";
90 our $pwd= `pwd`; $? and die $?; chomp($pwd);
91 our $caller= cgi_get_caller;
94 print LOG "$mid $caller $_[0]\n" or die $!;
98 open LOG, ">>_upload.log" or die $!;
101 $o{'clientname'}= must_param('clientname',$re_any);
102 my $clientinfo= $clients{$o{'clientname'}};
103 fail('unknown client') unless defined $clientinfo;
105 my $clientfixes= must_param('clientfixes', "^([-0-9a-z ]*)\$");
106 my @clientfixes= sort grep { m/./ } split /\s+/, $clientfixes;
107 $o{'clientfixes'}= "@clientfixes";
108 foreach my $bug (@$clientinfo) {
109 fail("client out of date - missing bugfix \`$bug'")
110 unless grep { $_ eq $bug } @clientfixes;
113 $o{'clientversion'}= must_param('clientversion', "^(\\d[-+._0-9a-zA-Z]+)\$");
115 if (param('requesttimestamp')) {
116 my $now= time; defined $now or die;
117 print header(-type=>'text/plain', -charset=>'us-ascii'), "OK $now.\n";
121 $o{'ocean'}= must_param('ocean', $re_any);
122 $o{'island'}= must_param('island', $re_any);
124 my $arches= $oceans{$o{'ocean'}};
125 fail("unknown ocean") unless $arches;
128 foreach my $islands (values %$arches) {
129 my $sources= $islands->{$o{'island'}};
130 next unless $sources;
131 die if $island_found;
132 $island_found= $sources;
134 fail("unknown island") unless $island_found;
136 $o{'timestamp'}= must_param('timestamp', "^([1-9]\\d{1,20})\$");
137 fail("clock skew") if $o{'timestamp'} >= $now;
139 my $indatafh= upload('data');
140 defined $indatafh or fail("data is not a file");
141 my $datafile= must_param('data',"^(deduped\\.tsv\\.gz)\$");
143 foreach my $mid (<_mid-*.hold>) {
144 if (!stat $mid) { $!==&ENOENT or die "$mid $!"; next; }
145 my $age= (stat _)[9];
147 unlink $mid or $!==&ENOENT or die "$mid $!";
151 my $mcontent= MIME::Entity->build(To => 'yarrg-commod-updates',
153 Type => 'multipart/mixed',
155 'Message-ID' => $mid,
158 get_our_version(\%o, 'server');
159 foreach my $cs (qw(client server)) {
160 $o{"${cs}spec"}= join "\t", map { $o{$cs.$_} } qw(name version fixes);
167 my $i=0; grep { return $i if $_ eq $v; $i++ } qw(ocean island timestamp);
168 sprintf "z %d %s", (length $v) / 8, $v;
171 foreach my $vn (sort { ksmap($a) cmp ksmap($b) } keys %o) {
173 die if $val =~ m/\n|\r/;
174 $metadata .= "$vn\t$o{$vn}\n";
177 my $mdpart= MIME::Entity->build(Top => 0,
178 Type => 'text/plain',
180 Disposition => 'inline',
181 Encoding => 'quoted-printable',
182 Filename => 'metadata',
184 $mcontent->add_part($mdpart);
186 my $gunzchild= open(GZ, "-|"); defined $gunzchild or die $!;
188 open STDIN, "<&=", $indatafh or die $!;
189 exec 'gunzip'; die $!;
192 my $dedupedtsv= pipethrough_prep();
195 my @v= check_tsv_line($_,\&fail);
196 print $dedupedtsv join("\t",@v),"\n" or die $!;
199 GZ->error and die $!;
200 $?=0; close GZ; $? and fail("gunzip for check failed code $?");
202 my $launderedgz= pipethrough_run($dedupedtsv,undef,'gzip','gzip');
204 my $mdatafile= MIME::Entity->build(Top => 0,
205 Type => 'application/octet-stream',
206 Disposition => 'attachment',
207 Encoding => 'base64',
208 Filename => 'deduped.tsv.gz',
209 Data => $launderedgz);
210 $mcontent->add_part($mdatafile);
212 open M, "|/usr/sbin/sendmail -t -oi -oee -odb"
213 or fail("fork sendmail failed! ($!)");
214 $mcontent->print(\*M);
216 M->error and fail("write sendmail failed! ($!)");
217 $?=0; close M; $? and fail("sendmail failed code $?");
219 print header(-type=>'text/plain', -charset=>'us-ascii'),
223 addlog("accepted $o{'clientspec'}");