3 # This script is invoked when the yarrg client uploads to
6 # This is part of the YARRG website. YARRG is a tool and website
7 # for assisting 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 Affero General Public License as
13 # published by the Free Software Foundation, either version 3 of the
14 # License, or (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 Affero General Public License for more details.
21 # You should have received a copy of the GNU Affero 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= "^(.*)\$";
46 addlog("failing $msg");
47 print header(-status=>'400 Bad commodity update',
49 -charset=>'us-ascii');
50 print "\nError: $msg\n";
57 fail("missing form parameter $n") unless defined $v;
58 fail("invalid form parameter $n ($re)") unless $v =~ m/$re/;
64 if (param('get_source')) {
65 header('application/octet-stream');
66 source_tarball('..', sub { print $_[0] or die $!; });
70 parse_info_serverside();
72 my $midtmp= "_mid-pid$$.hold";
73 open MIDTMP, ">$midtmp" or die "$midtmp $!";
74 stat MIDTMP or die $!;
76 my $midino= "_mid-ino$$.hold";
77 rename $midtmp, $midino or die "$midtmp $midino $!";
78 close MIDTMP or die $!;
80 our $hostname= `hostname -f`; $? and die $?; chomp $hostname or die;
81 our $mid= "<$now.$$.$ino\@$hostname>";
82 our $pwd= `pwd`; $? and die $?; chomp($pwd);
83 our $caller= cgi_get_caller;
86 print LOG "$mid $caller $_[0]\n" or die $!;
90 open LOG, ">>_upload.log" or die $!;
93 $o{'clientname'}= must_param('clientname',$re_any);
94 my $clientinfo= $clients{$o{'clientname'}};
95 fail('unknown client') unless defined $clientinfo;
97 my $clientfixes= must_param('clientfixes', "^([-0-9a-z ]*)\$");
98 my @clientfixes= sort grep { m/./ } split /\s+/, $clientfixes;
99 $o{'clientfixes'}= "@clientfixes";
100 foreach my $bug (@$clientinfo) {
101 fail("client out of date - missing bugfix \`$bug'")
102 unless grep { $_ eq $bug } @clientfixes;
105 $o{'clientversion'}= must_param('clientversion', "^(\\d[-+._0-9a-zA-Z]+)\$");
107 if (param('requesttimestamp')) {
108 my $now= time; defined $now or die;
109 print header(-type=>'text/plain', -charset=>'us-ascii'), "OK $now.\n";
113 $o{'ocean'}= must_param('ocean', $re_any);
114 $o{'island'}= must_param('island', $re_any);
116 my $arches= $oceans{$o{'ocean'}};
117 fail("unknown ocean") unless $arches;
119 parse_info_serverside_ocean($o{'ocean'});
122 foreach my $islands (values %$arches) {
123 my $sources= $islands->{$o{'island'}};
124 next unless $sources;
125 die if $island_found;
126 $island_found= $sources;
128 fail("unknown island") unless $island_found;
130 $o{'timestamp'}= must_param('timestamp', "^([1-9]\\d{1,20})\$");
131 fail("clock skew") if $o{'timestamp'} >= $now;
133 my $indatafh= upload('data');
134 defined $indatafh or fail("data is not a file");
135 my $datafile= must_param('data',"^(deduped\\.tsv\\.gz)\$");
137 foreach my $mid (<_mid-*.hold>) {
138 if (!stat $mid) { $!==&ENOENT or die "$mid $!"; next; }
139 my $age= (stat _)[9];
141 unlink $mid or $!==&ENOENT or die "$mid $!";
144 $o{'instance'}= $ENV{'YARRG_INSTANCE'};
146 my $mcontent= MIME::Entity->build(To => 'yarrg-commod-updates',
147 Subject => $ENV{'YARRG_INSTANCE'},
148 Type => 'multipart/mixed',
150 'Message-ID' => $mid,
153 get_our_version(\%o, 'server');
154 foreach my $cs (qw(client server)) {
155 $o{"${cs}spec"}= join "\t", map { $o{$cs.$_} } qw(name version fixes);
162 my $i=0; grep { return $i if $_ eq $v; $i++ } qw(ocean island timestamp);
163 sprintf "z %d %s", (length $v) / 8, $v;
166 foreach my $vn (sort { ksmap($a) cmp ksmap($b) } keys %o) {
168 die if $val =~ m/\n|\r/;
169 $metadata .= "$vn\t$o{$vn}\n";
172 my $mdpart= MIME::Entity->build(Top => 0,
173 Type => 'text/plain',
175 Disposition => 'inline',
176 Encoding => 'quoted-printable',
177 Filename => 'metadata',
179 $mcontent->add_part($mdpart);
181 my $gunzchild= open(GZ, "-|"); defined $gunzchild or die $!;
183 open STDIN, "<&=", $indatafh or die $!;
184 exec 'gunzip'; die $!;
187 my $dedupedtsv= pipethrough_prep();
190 my @v= check_tsv_line($_,\&fail);
191 print $dedupedtsv join("\t",@v),"\n" or die $!;
194 GZ->error and die $!;
195 $?=0; close GZ; $? and fail("gunzip for check failed code $?");
197 my $launderedgz= pipethrough_run($dedupedtsv,undef,'gzip','gzip');
199 my $mdatafile= MIME::Entity->build(Top => 0,
200 Type => 'application/octet-stream',
201 Disposition => 'attachment',
202 Encoding => 'base64',
203 Filename => 'deduped.tsv.gz',
204 Data => $launderedgz);
205 $mcontent->add_part($mdatafile);
207 open M, "|/usr/sbin/sendmail -t -oi -oee -odb"
208 or fail("fork sendmail failed! ($!)");
209 $mcontent->print(\*M);
211 M->error and fail("write sendmail failed! ($!)");
212 $?=0; close M; $? and fail("sendmail failed code $?");
214 print header(-type=>'text/plain', -charset=>'us-ascii'),
215 "OK\nThank you for your submission to YARRG.\n"
218 addlog("accepted $o{'clientspec'}");