chiark / gitweb /
WIP ocean scraper before redo parser
[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 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 my $island_found= 0;
114 foreach my $islands (values %$arches) {
115     my $sources= $islands->{$o{'island'}};
116     next unless $sources;
117     die if $island_found;
118     $island_found= $sources;
119 }
120 fail("unknown island") unless $island_found;
121
122 $o{'timestamp'}= must_param('timestamp', "^([1-9]\\d{1,20})\$");
123 fail("clock skew") if $o{'timestamp'} >= $now;
124
125 my $indatafh= upload('data');
126 defined $indatafh or fail("data is not a file");
127 my $datafile= must_param('data',"^(deduped\\.tsv\\.gz)\$");
128
129 foreach my $mid (<_mid-*.hold>) {
130     if (!stat $mid) { $!==&ENOENT or die "$mid $!"; next; }
131     my $age= (stat _)[9];
132     next if $age < 60;
133     unlink $mid or $!==&ENOENT or die "$mid $!";
134 }
135
136 $o{'instance'}= $ENV{'YARRG_INSTANCE'};
137
138 my $mcontent= MIME::Entity->build(To => 'yarrg-commod-updates',
139                                   Subject => $ENV{'YARRG_INSTANCE'},
140                                   Type => 'multipart/mixed',
141                                   Boundary => '=',
142                                   'Message-ID' => $mid,
143                                   Charset => 'utf-8');
144
145 get_our_version(\%o, 'server');
146 foreach my $cs (qw(client server)) {
147     $o{"${cs}spec"}= join "\t", map { $o{$cs.$_} } qw(name version fixes);
148 }
149
150 my $metadata= '';
151
152 sub ksmap ($) {
153     my ($v) = @_;
154     my $i=0; grep { return $i if $_ eq $v; $i++ } qw(ocean island timestamp);
155     sprintf "z %d %s", (length $v) / 8, $v;
156 }
157
158 foreach my $vn (sort { ksmap($a) cmp ksmap($b) } keys %o) {
159     my $val= $o{$vn};
160     die if $val =~ m/\n|\r/;
161     $metadata .= "$vn\t$o{$vn}\n";
162 }
163
164 my $mdpart= MIME::Entity->build(Top => 0,
165                                 Type => 'text/plain',
166                                 Charset => 'utf-8',
167                                 Disposition => 'inline',
168                                 Encoding => 'quoted-printable',
169                                 Filename => 'metadata',
170                                 Data => $metadata);
171 $mcontent->add_part($mdpart);
172
173 my $gunzchild= open(GZ, "-|"); defined $gunzchild or die $!;
174 if (!$gunzchild) {
175     open STDIN, "<&=", $indatafh or die $!;
176     exec 'gunzip'; die $!;
177 }
178
179 my $dedupedtsv= pipethrough_prep();
180
181 while (<GZ>) {
182     my @v= check_tsv_line($_,\&fail);
183     print $dedupedtsv join("\t",@v),"\n" or die $!;
184 }
185
186 GZ->error and die $!;
187 $?=0; close GZ; $? and fail("gunzip for check failed code $?");
188
189 my $launderedgz= pipethrough_run($dedupedtsv,undef,'gzip','gzip');
190
191 my $mdatafile= MIME::Entity->build(Top => 0,
192                                    Type => 'application/octet-stream',
193                                    Disposition => 'attachment',
194                                    Encoding => 'base64',
195                                    Filename => 'deduped.tsv.gz',
196                                    Data => $launderedgz);
197 $mcontent->add_part($mdatafile);
198
199 open M, "|/usr/sbin/sendmail -t -oi -oee -odb"
200     or fail("fork sendmail failed! ($!)");
201 $mcontent->print(\*M);
202
203 M->error and fail("write sendmail failed! ($!)");
204 $?=0; close M; $? and fail("sendmail failed code $?");
205
206 print header(-type=>'text/plain', -charset=>'us-ascii'),
207       "OK\nThank you for your submission to YARRG.\n"
208     or die $!;
209
210 addlog("accepted $o{'clientspec'}");
211 close LOG or die $!;