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.
28 use strict (qw(vars));
34 $CGI::POST_MAX= 3*1024*1024;
36 use CGI qw/:standard -private_tempfiles/;
40 our $now= time; defined $now or die $!;
42 my $re_any= "^(.*)\$";
44 parse_info_serverside();
48 addlog("failing $msg");
49 print header(-status=>'400 Bad commodity update',
51 -charset=>'us-ascii');
52 print "\nError: $msg\n";
59 fail("missing form parameter $n") unless defined $v;
60 fail("invalid form parameter $n ($re)") unless $v =~ m/$re/;
66 my $midtmp= "_mid-pid$$.hold";
67 open MIDTMP, ">$midtmp" or die "$midtmp $!";
68 stat MIDTMP or die $!;
70 my $midino= "_mid-ino$$.hold";
71 rename $midtmp, $midino or die "$midtmp $midino $!";
72 close MIDTMP or die $!;
74 our $hostname= `hostname -f`; $? and die $?; chomp $hostname or die;
75 our $mid= "<$now.$$.$ino\@$hostname>";
76 our $pwd= `pwd`; $? and die $?; chomp($pwd);
77 our $caller= cgi_get_caller;
80 print LOG "$mid $caller $_[0]\n" or die $!;
84 open LOG, ">>_upload.log" or die $!;
87 $o{'clientname'}= must_param('clientname',$re_any);
88 my $clientinfo= $clients{$o{'clientname'}};
89 fail('unknown client') unless defined $clientinfo;
91 my $clientfixes= must_param('clientfixes', "^([-0-9a-z ]*)\$");
92 my @clientfixes= sort grep { m/./ } split /\s+/, $clientfixes;
93 $o{'clientfixes'}= "@clientfixes";
94 foreach my $bug (@$clientinfo) {
95 fail("client out of date - missing bugfix \`$bug'")
96 unless grep { $_ eq $bug } @clientfixes;
99 $o{'clientversion'}= must_param('clientversion', "^(\\d[-+._0-9a-zA-Z]+)\$");
101 if (param('requesttimestamp')) {
102 my $now= time; defined $now or die;
103 print header(-type=>'text/plain', -charset=>'us-ascii'), "OK $now.\n";
107 $o{'ocean'}= must_param('ocean', $re_any);
108 $o{'island'}= must_param('island', $re_any);
110 my $arches= $oceans{$o{'ocean'}};
111 fail("unknown ocean") unless $arches;
113 parse_info_serverside_ocean($o{'ocean'});
116 foreach my $islands (values %$arches) {
117 my $sources= $islands->{$o{'island'}};
118 next unless $sources;
119 die if $island_found;
120 $island_found= $sources;
122 fail("unknown island") unless $island_found;
124 $o{'timestamp'}= must_param('timestamp', "^([1-9]\\d{1,20})\$");
125 fail("clock skew") if $o{'timestamp'} >= $now;
127 my $indatafh= upload('data');
128 defined $indatafh or fail("data is not a file");
129 my $datafile= must_param('data',"^(deduped\\.tsv\\.gz)\$");
131 foreach my $mid (<_mid-*.hold>) {
132 if (!stat $mid) { $!==&ENOENT or die "$mid $!"; next; }
133 my $age= (stat _)[9];
135 unlink $mid or $!==&ENOENT or die "$mid $!";
138 $o{'instance'}= $ENV{'YARRG_INSTANCE'};
140 my $mcontent= MIME::Entity->build(To => 'yarrg-commod-updates',
141 Subject => $ENV{'YARRG_INSTANCE'},
142 Type => 'multipart/mixed',
144 'Message-ID' => $mid,
147 get_our_version(\%o, 'server');
148 foreach my $cs (qw(client server)) {
149 $o{"${cs}spec"}= join "\t", map { $o{$cs.$_} } qw(name version fixes);
156 my $i=0; grep { return $i if $_ eq $v; $i++ } qw(ocean island timestamp);
157 sprintf "z %d %s", (length $v) / 8, $v;
160 foreach my $vn (sort { ksmap($a) cmp ksmap($b) } keys %o) {
162 die if $val =~ m/\n|\r/;
163 $metadata .= "$vn\t$o{$vn}\n";
166 my $mdpart= MIME::Entity->build(Top => 0,
167 Type => 'text/plain',
169 Disposition => 'inline',
170 Encoding => 'quoted-printable',
171 Filename => 'metadata',
173 $mcontent->add_part($mdpart);
175 my $gunzchild= open(GZ, "-|"); defined $gunzchild or die $!;
177 open STDIN, "<&=", $indatafh or die $!;
178 exec 'gunzip'; die $!;
181 my $dedupedtsv= pipethrough_prep();
184 my @v= check_tsv_line($_,\&fail);
185 print $dedupedtsv join("\t",@v),"\n" or die $!;
188 GZ->error and die $!;
189 $?=0; close GZ; $? and fail("gunzip for check failed code $?");
191 my $launderedgz= pipethrough_run($dedupedtsv,undef,'gzip','gzip');
193 my $mdatafile= MIME::Entity->build(Top => 0,
194 Type => 'application/octet-stream',
195 Disposition => 'attachment',
196 Encoding => 'base64',
197 Filename => 'deduped.tsv.gz',
198 Data => $launderedgz);
199 $mcontent->add_part($mdatafile);
201 open M, "|/usr/sbin/sendmail -t -oi -oee -odb"
202 or fail("fork sendmail failed! ($!)");
203 $mcontent->print(\*M);
205 M->error and fail("write sendmail failed! ($!)");
206 $?=0; close M; $? and fail("sendmail failed code $?");
208 print header(-type=>'text/plain', -charset=>'us-ascii'),
209 "OK\nThank you for your submission to YARRG.\n"
212 addlog("accepted $o{'clientspec'}");