chiark / gitweb /
Cope with modern Perl's default @INC
[ypp-sc-tools.db-live.git] / yarrg / commod-update-receiver
1 #!/usr/bin/perl -w
2 #
3 # This script is invoked when the yarrg client uploads to
4 # the chiark database.
5
6 # This is part of the YARRG website.  YARRG is a tool and website
7 # for assisting players of Yohoho Puzzle Pirates.
8 #
9 # Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
10 #
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.
15 #
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.
20 #
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/>.
23 #
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.
27
28 BEGIN { unshift @INC, qw(.) }
29
30 use strict (qw(vars));
31 use POSIX;
32 use MIME::Entity;
33
34 use Commods;
35
36 $CGI::POST_MAX= 3*1024*1024;
37
38 use CGI qw/:standard -private_tempfiles/;
39
40 set_ctype_utf8();
41
42 our $now= time;  defined $now or die $!;
43
44 my $re_any= "^(.*)\$";
45
46 sub fail ($) {
47     my ($msg) = @_;
48     addlog("failing $msg");
49     print header(-status=>'400 Bad commodity update',
50                  -type=>'text/plain',
51                  -charset=>'us-ascii');
52     print "\nError: $msg\n";
53     exit 0;
54 }
55
56 sub must_param ($$) {
57     my ($n,$re)= @_;
58     my $v= param($n);
59     fail("missing form parameter $n") unless defined $v;
60     fail("invalid form parameter $n ($re)") unless $v =~ m/$re/;
61     return $1;
62 }
63
64 my %o;
65
66 if (param('get_source')) {
67     # There's another copy of this in dictionary-update-receiver.  Sorry.
68     print header('application/octet-stream') or die $!;
69     source_tarball('..', sub { print $_[0] or die $!; });
70     exit 0;
71 }
72
73 parse_info_serverside();
74
75 my $midtmp= "_mid-pid$$.hold";
76 open MIDTMP, ">$midtmp" or die "$midtmp $!";
77 stat MIDTMP or die $!;
78 my $ino= (stat _)[1];
79 my $midino= "_mid-ino$$.hold";
80 rename $midtmp, $midino or die "$midtmp $midino $!";
81 close MIDTMP or die $!;
82
83 our $hostname= `hostname -f`; $? and die $?;  chomp $hostname or die;
84 our $mid= "<$now.$$.$ino\@$hostname>";
85 our $pwd= `pwd`; $? and die $?; chomp($pwd);
86 our $caller= cgi_get_caller;
87
88 sub addlog ($) {
89     print LOG "$mid $caller $_[0]\n" or die $!;
90     flush LOG or die $!;
91 }
92
93 open LOG, ">>_upload.log" or die $!;
94 addlog("receiving");
95
96 $o{'clientname'}= must_param('clientname',$re_any);
97 my $clientinfo= $clients{$o{'clientname'}};
98 fail('unknown client '.errsan($o{'clientname'})) unless defined $clientinfo;
99
100 my $clientfixes= must_param('clientfixes', "^([-0-9a-z ]*)\$");
101 my @clientfixes= sort grep { m/./ } split /\s+/, $clientfixes;
102 $o{'clientfixes'}= "@clientfixes";
103 foreach my $bug (@$clientinfo) {
104     fail("client out of date - missing bugfix \`$bug'")
105         unless grep { $_ eq $bug } @clientfixes;
106 }
107
108 $o{'clientversion'}= must_param('clientversion', "^(\\d[-+._0-9a-zA-Z]+)\$");
109
110 if (param('requesttimestamp')) {
111     my $now= time; defined $now or die;
112     print header(-type=>'text/plain', -charset=>'us-ascii'), "OK $now.\n";
113     exit(0);
114 }
115
116 $o{'ocean'}= must_param('ocean', $re_any);
117 $o{'island'}= must_param('island', $re_any);
118
119 my $arches= $oceans{$o{'ocean'}};
120 fail("unknown ocean ".errsan($o{'ocean'})) unless $arches;
121
122 parse_info_serverside_ocean($o{'ocean'});
123
124 my $island_found= 0;
125 foreach my $islands (values %$arches) {
126     my $sources= $islands->{$o{'island'}};
127     next unless $sources;
128     die if $island_found;
129     $island_found= $sources;
130 }
131 fail("unknown island ".errsan($o{'island'})) unless $island_found;
132
133 $o{'timestamp'}= must_param('timestamp', "^([1-9]\\d{1,20})\$");
134 fail("clock skew") if $o{'timestamp'} > $now;
135
136 my $indatafh= upload('data');
137 defined $indatafh or fail("data is not a file");
138 my $datafile= must_param('data',"^(deduped\\.tsv\\.gz)\$");
139
140 foreach my $mid (<_mid-*.hold>) {
141     if (!stat $mid) { $!==&ENOENT or die "$mid $!"; next; }
142     my $age= (stat _)[9];
143     next if $age < 60;
144     unlink $mid or $!==&ENOENT or die "$mid $!";
145 }
146
147 $o{'instance'}= $ENV{'YARRG_INSTANCE'};
148
149 my $mcontent= MIME::Entity->build(To => 'yarrg-commod-updates',
150                                   Subject => $ENV{'YARRG_INSTANCE'},
151                                   Type => 'multipart/mixed',
152                                   Boundary => '=',
153                                   'Message-ID' => $mid,
154                                   Charset => 'utf-8');
155
156 get_our_version(\%o, 'server');
157 foreach my $cs (qw(client server)) {
158     $o{"${cs}spec"}= join "\t", map { $o{$cs.$_} } qw(name version fixes);
159 }
160
161 my $metadata= '';
162
163 sub ksmap ($) {
164     my ($v) = @_;
165     my $i=0; grep { return $i if $_ eq $v; $i++ } qw(ocean island timestamp);
166     sprintf "z %d %s", (length $v) / 8, $v;
167 }
168
169 foreach my $vn (sort { ksmap($a) cmp ksmap($b) } keys %o) {
170     my $val= $o{$vn};
171     die if $val =~ m/\n|\r/;
172     $metadata .= "$vn\t$o{$vn}\n";
173 }
174
175 my $mdpart= MIME::Entity->build(Top => 0,
176                                 Type => 'text/plain',
177                                 Charset => 'utf-8',
178                                 Disposition => 'inline',
179                                 Encoding => 'quoted-printable',
180                                 Filename => 'metadata',
181                                 Data => $metadata);
182 $mcontent->add_part($mdpart);
183
184 my $gunzchild= open(GZ, "-|"); defined $gunzchild or die $!;
185 if (!$gunzchild) {
186     open STDIN, "<&=", $indatafh or die $!;
187     exec 'gunzip'; die $!;
188 }
189
190 my $dedupedtsv= pipethrough_prep();
191
192 while (<GZ>) {
193     my @v= check_tsv_line($_,\&fail);
194     print $dedupedtsv join("\t",@v),"\n" or die $!;
195 }
196
197 GZ->error and die $!;
198 $?=0; close GZ; $? and fail("gunzip for check failed code $?");
199
200 my $launderedgz= pipethrough_run($dedupedtsv,undef,'gzip','gzip');
201
202 my $mdatafile= MIME::Entity->build(Top => 0,
203                                    Type => 'application/octet-stream',
204                                    Disposition => 'attachment',
205                                    Encoding => 'base64',
206                                    Filename => 'deduped.tsv.gz',
207                                    Data => $launderedgz);
208 $mcontent->add_part($mdatafile);
209
210 open M, "|/usr/sbin/sendmail -t -oi -oee -odb"
211     or fail("fork sendmail failed! ($!)");
212 $mcontent->print(\*M);
213
214 M->error and fail("write sendmail failed! ($!)");
215 $?=0; close M; $? and fail("sendmail failed code $?");
216
217 print header(-type=>'text/plain', -charset=>'us-ascii'),
218       "OK\nThank you for your submission to YARRG.\n"
219     or die $!;
220
221 addlog("accepted $o{'clientspec'}");
222 close LOG or die $!;