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