chiark / gitweb /
routesearch: sort out debugging output
[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 ypp-sc-tools, a set of third-party tools for assisting
7 # 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 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.
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 General Public License for more details.
20 #
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/>.
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 use strict (qw(vars));
29 use POSIX;
30 use MIME::Entity;
31
32 use Commods;
33
34 $CGI::POST_MAX= 3*1024*1024;
35
36 use CGI qw/:standard -private_tempfiles/;
37
38 set_ctype_utf8();
39
40 our $now= time;  defined $now or die $!;
41
42 my $re_any= "^(.*)\$";
43
44 parse_info_serverside();
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 my $midtmp= "_mid-pid$$.hold";
67 open MIDTMP, ">$midtmp" or die "$midtmp $!";
68 stat MIDTMP or die $!;
69 my $ino= (stat _)[1];
70 my $midino= "_mid-ino$$.hold";
71 rename $midtmp, $midino or die "$midtmp $midino $!";
72 close MIDTMP or die $!;
73
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;
78
79 sub addlog ($) {
80     print LOG "$mid $caller $_[0]\n" or die $!;
81     flush LOG or die $!;
82 }
83
84 open LOG, ">>_upload.log" or die $!;
85 addlog("receiving");
86
87 $o{'clientname'}= must_param('clientname',$re_any);
88 my $clientinfo= $clients{$o{'clientname'}};
89 fail('unknown client') unless defined $clientinfo;
90
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;
97 }
98
99 $o{'clientversion'}= must_param('clientversion', "^(\\d[-+._0-9a-zA-Z]+)\$");
100
101 if (param('requesttimestamp')) {
102     my $now= time; defined $now or die;
103     print header(-type=>'text/plain', -charset=>'us-ascii'), "OK $now.\n";
104     exit(0);
105 }
106
107 $o{'ocean'}= must_param('ocean', $re_any);
108 $o{'island'}= must_param('island', $re_any);
109
110 my $arches= $oceans{$o{'ocean'}};
111 fail("unknown ocean") unless $arches;
112
113 parse_info_serverside_ocean($o{'ocean'});
114
115 my $island_found= 0;
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;
121 }
122 fail("unknown island") unless $island_found;
123
124 $o{'timestamp'}= must_param('timestamp', "^([1-9]\\d{1,20})\$");
125 fail("clock skew") if $o{'timestamp'} >= $now;
126
127 my $indatafh= upload('data');
128 defined $indatafh or fail("data is not a file");
129 my $datafile= must_param('data',"^(deduped\\.tsv\\.gz)\$");
130
131 foreach my $mid (<_mid-*.hold>) {
132     if (!stat $mid) { $!==&ENOENT or die "$mid $!"; next; }
133     my $age= (stat _)[9];
134     next if $age < 60;
135     unlink $mid or $!==&ENOENT or die "$mid $!";
136 }
137
138 $o{'instance'}= $ENV{'YARRG_INSTANCE'};
139
140 my $mcontent= MIME::Entity->build(To => 'yarrg-commod-updates',
141                                   Subject => $ENV{'YARRG_INSTANCE'},
142                                   Type => 'multipart/mixed',
143                                   Boundary => '=',
144                                   'Message-ID' => $mid,
145                                   Charset => 'utf-8');
146
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);
150 }
151
152 my $metadata= '';
153
154 sub ksmap ($) {
155     my ($v) = @_;
156     my $i=0; grep { return $i if $_ eq $v; $i++ } qw(ocean island timestamp);
157     sprintf "z %d %s", (length $v) / 8, $v;
158 }
159
160 foreach my $vn (sort { ksmap($a) cmp ksmap($b) } keys %o) {
161     my $val= $o{$vn};
162     die if $val =~ m/\n|\r/;
163     $metadata .= "$vn\t$o{$vn}\n";
164 }
165
166 my $mdpart= MIME::Entity->build(Top => 0,
167                                 Type => 'text/plain',
168                                 Charset => 'utf-8',
169                                 Disposition => 'inline',
170                                 Encoding => 'quoted-printable',
171                                 Filename => 'metadata',
172                                 Data => $metadata);
173 $mcontent->add_part($mdpart);
174
175 my $gunzchild= open(GZ, "-|"); defined $gunzchild or die $!;
176 if (!$gunzchild) {
177     open STDIN, "<&=", $indatafh or die $!;
178     exec 'gunzip'; die $!;
179 }
180
181 my $dedupedtsv= pipethrough_prep();
182
183 while (<GZ>) {
184     my @v= check_tsv_line($_,\&fail);
185     print $dedupedtsv join("\t",@v),"\n" or die $!;
186 }
187
188 GZ->error and die $!;
189 $?=0; close GZ; $? and fail("gunzip for check failed code $?");
190
191 my $launderedgz= pipethrough_run($dedupedtsv,undef,'gzip','gzip');
192
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);
200
201 open M, "|/usr/sbin/sendmail -t -oi -oee -odb"
202     or fail("fork sendmail failed! ($!)");
203 $mcontent->print(\*M);
204
205 M->error and fail("write sendmail failed! ($!)");
206 $?=0; close M; $? and fail("sendmail failed code $?");
207
208 print header(-type=>'text/plain', -charset=>'us-ascii'),
209       "OK\nThank you for your submission to YARRG.\n"
210     or die $!;
211
212 addlog("accepted $o{'clientspec'}");
213 close LOG or die $!;