chiark / gitweb /
3baef669ddf6b4eefcc7a7942cb709118a9fc9dc
[ypp-sc-tools.db-test.git] / yarrg / dictionary-update-receiver
1 #!/usr/bin/perl -w
2 #
3 # This script is invoked when the yarrg wants to send an update to the
4 # dictionary server.  See README.privacy.
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 # upload testing runes:
29 #
30 # YPPSC_YARRG_DICT_UPDATE=./ YPPSC_YARRG_DICT_SUBMIT=./ ./yarrg --ocean midnight --pirate aristarchus --find-island --same --raw-tsv >raw.tsv  
31 # ./dictionary-manager --debug --approve-updates '' . .
32
33 use strict (qw(vars));
34 use POSIX;
35
36 $CGI::POST_MAX= 1024*1024;
37 $CGI::DISABLE_UPLOADS= 1;
38
39 use CGI qw/:standard -private_tempfiles/;
40 use IO::Pipe;
41 use IO::Handle;
42
43 use Commods;
44
45 if (param('get_source')) {
46     # There's another copy of this in commod-update-receiver.  Sorry.
47     print header('application/octet-stream') or die $!;
48     source_tarball('..', sub { print $_[0] or die $!; });
49     exit 0;
50 }
51
52 my $aadepth=2;
53
54 #---------- pixmaps ----------
55
56 sub parseentryin__pixmap ($) {
57     my ($entry_in) = @_;
58     $entry_in =~
59         m/^(\S+ \- .*)\nP3\n([1-9]\d{1,3}) ([1-9]\d{1,3})\n255\n/s or die;
60     my ($def,$w,$h)= ($1, $2+0, $3+0);
61     my @d= grep { m/./ } split /\s+/, $';
62     @d == $w*$h*3 or die "$d[0]|$d[1]|...|$d[$#d-1]|$d[$#d] ?";
63     map {
64         m/\D/ and die "$& ?";
65         $_ += 0;
66         $_ >= 0 or die "$_ ?";
67         $_ <= 255 or die "$_ ?";
68     } @d;
69     my $ppm= "P3\n$w $h\n255\n";
70     my $di=0;
71     for (my $y=0; $y<$h; $y++) {
72         for (my $x=0; $x<$w; $x++, $di+=3) {
73 #print STDERR ">$x,$y,$di,",scalar(@d),"<\n";
74             $ppm .= sprintf "  %3d %3d %3d", @d[$di..$di+2];
75         }
76         $ppm .= "\n";
77     }
78
79     my $icon= pipeval($ppm,
80                          'ppmtopgm',
81                          'pnmscale -xysize 156 80',
82                          'pnmnorm -bpercent 40 -wpercent 20',
83                          'pgmtopbm -threshold',
84                          'pnminvert',
85                          'pbmtoascii -2x4');
86
87     my $whole= pipeval($ppm,
88                        'ppmtopgm',
89                        'pnmnorm -bpercent 10 -wpercent 5',
90                        'pgmtopbm -threshold',
91                        'pnminvert',
92                        'pbmtoascii');
93
94     my $entry= "$def\n$ppm";
95
96     return ('',$def,$ppm,$ppm,$def, $w,$icon,$whole,$entry);
97 }
98
99 #---------- characters ----------
100
101 sub parseentryin__char ($$) {
102     my ($ei,$h) = @_;
103     $ei =~ m/^(Digit|Upper|Lower|Word)\n([^\n]+)\n/s or die;
104     my ($ctx,$str)= ($1,$2);
105 print STDERR ">ctx=$ctx|str=$str|$'<\n";
106     my @d= grep { m/./ } split /\n/, $';
107 print STDERR ">@d<\n";
108     die if $h>100;
109     die if @d>400;
110
111     my $w= @d;
112
113     my $maxval= (1<<$aadepth)-1;
114     die 'cannot do [^0...$maxval]!' if $maxval>9;
115
116     my $pgm= "P2\n$h $w\n$maxval\n";
117     map { # x, left to right
118         m/[^0-$maxval]/ and die "$_ ?";
119         my $l= $_;
120         $l =~ s/./ $&/g;
121         $pgm .= "$l\n";
122     } @d;
123
124     my $key= join ' ', $ctx, @d;
125
126     $pgm= pipeval($pgm,
127                   "pnmflip -xy",
128                   "pnmnoraw");
129
130     my $icon= pipeval($pgm,
131                       "pnmscale -xysize 156 ".($h*4),
132                       'pgmtopbm -threshold',
133                       'pnminvert',
134                       'pbmtoascii -2x4');
135
136     my $whole= pipeval($pgm,
137                        "pnmscale 4",
138                        'pgmtopbm -fs',
139                        'pnminvert',
140                        'pbmtoascii');
141
142     my $entry= "$ctx\n$str\n$key\n";
143     
144     return ($ctx,$str,$pgm,$key,$str, $w*4,$icon,$whole,$entry);
145 }
146
147 #---------- useful stuff ----------
148
149 sub pipeval ($@) {
150     my ($val, @cmds) = @_;
151     my (@pids);
152
153     my $lastpipe;
154     
155     foreach my $cmd ('',@cmds) {
156         my $pipe= new IO::Pipe or die $!;
157         my $pid= fork();  defined $pid or die $!;
158
159         if (!$pid) {
160             $pipe->writer();
161             if (!$lastpipe) {
162                  print $pipe $val or die $!;
163                  exit 0;
164              } else {
165                  open STDIN, '<&', $lastpipe or die $!;
166                  open STDOUT, '>&', $pipe or die $!;
167                  close $lastpipe or die $!;
168                  close $pipe or die $!;
169                  exec $cmd; die $!;
170              }
171         }
172         $pipe->reader();
173         if ($lastpipe) { close $lastpipe or die $!; }
174         $lastpipe= $pipe;
175         push @pids, $pid;
176     }
177
178     $!=0; { local ($/)=undef; $val= <$lastpipe>; }
179     defined $val or die $!;
180     $lastpipe->error and die $!;  close $lastpipe or die $!;
181
182     foreach my $cmd ('(paste)', @cmds) {
183         my $pid= shift @pids;
184         waitpid($pid,0) == $pid or die "$pid $? $!";
185         $?==0 or $?==13 or die "$cmd $?";
186     }
187     return $val;
188 }
189
190 #========== main program ==========
191
192 #---------- determine properties of the submission ----------
193
194 my $version= param('version');
195 my $spec_aadepth= param('depth');
196 if ($version ne '3'  ||  $spec_aadepth ne $aadepth) {
197     print header('text/plain',
198                  "403 yarrg client is out of date".
199                  " ($version, $spec_aadepth)");
200     print "\nYour YPP SC client is out of date.\n";
201     exit 0;
202 }
203
204 my $dict= param('dict');
205 my $entry_in= param('entry');
206 defined $entry_in or die Dump()." ?";
207
208 my $ocean= param('ocean');
209 my $pirate= param('pirate');
210 if (defined $ocean && defined $pirate) {
211     $pirate= "$ocean - $pirate";
212 } else {
213     $pirate= '';
214 }
215
216 my $caller= cgi_get_caller();
217
218 my $kind;
219 my @xa;
220
221 if ($dict =~ m/^pixmap$/) {
222     $kind= $&;
223 } elsif ($dict =~ m/^(char)([1-9]\d?)$/) {
224     ($kind,@xa)= ($1,$2);
225 } else {
226     die "$dict ?";
227 }
228 $dict= $&;
229
230 my ($ctx,$def,$image,$key,$val, $width,$icon,$whole,$entry)=
231     &{"parseentryin__$kind"}($entry_in, @xa);
232
233 my $du=$ENV{'YPPSC_DICTUPDATES'};
234 chdir $du or die "$du $!"
235     if defined $du;
236
237 my $instance= $du;
238 $instance =~ s,ypp-sc-tools,,ig;
239 $instance =~ s,ypp,,ig;
240 $instance =~ s,pctb,,ig;
241 $instance =~ s,/\W+/,/,g;
242 $instance =~ s,/+$,,;
243 $instance =~ s,^.*/,,;
244
245 #---------- compute the email to send ----------
246
247 my $whoami= `whoami`; $? and die $?;
248 chomp $whoami;
249
250 my $email= <<END
251 To: $whoami
252 Subject: pctb /$instance/ $dict $ctx $def [ypp-sc-tools]
253
254 Pirate:     $pirate
255 Caller:     $caller
256 Dictionary: $dict
257 Context:    $ctx
258 Definition: $def
259
260 END
261     ;
262
263 if (length $icon) {
264     $icon =~ s/^/ /gm;
265     $email .= "$icon\n\n";
266 }
267
268 $whole =~ s/(.*)\n/ sprintf "%-${width}s\n", $1 /mge;
269 $whole =~ s/^/|/mg;
270 $whole =~ s/\n/|\n/mg;
271 $whole =~ s/^(.*)/ ",".('_' x $width).".\n".$1 /e;
272 $whole =~ s/(.*)$/ $1."\n\`".('~' x $width)."'\n" /e;
273
274 my $lw= 79;
275
276 while ($whole =~ m/../) {
277     my $lhs= $whole;
278     $lhs =~ s/^(.{0,$lw}).*$/$1/mg;
279     $whole =~ s/^.{1,$lw}//mg;
280 #print STDERR "[[[[[$lhs########$whole]]]]]\n";
281     $email .= $lhs;
282 }
283
284 END
285     ;
286
287 my $cutline= "-8<-\n";
288 $email .= $cutline.$entry.$cutline;
289
290 #---------- prepare the database entry ----------
291
292 my $fn_t= "_update.$$-xxxxxxxxxxxxxxxx.tmp";
293 open F, "> $fn_t" or die "$fn_t $!";
294 (stat F) or die $!;
295 my $fn_i= sprintf "_update.$$-%016x.rdy", (stat _)[1];
296
297 print F "ypp-sc-tools dictionary update v3 depth=$aadepth\n";
298
299 foreach my $v ($pirate,$caller,$dict,$ctx,$def,$image,$key,$val) {
300     printf F "%d\n", length($v) or die $!;
301     print F $v,"\n" or die $!;
302 }
303
304 close F or die $!;
305
306 my @tm= localtime;
307 my $tm= strftime "%Y-%m-%d %H:%M:%S %Z", @tm;
308
309 open L, ">> _dict.log" or die $!;
310 my $ll= sprintf "%s %-6s %-31s %-31s %s", $tm, $dict, $pirate, $caller, $fn_i;
311
312 #---------- commit everything ----------
313
314 print L "$ll submit\n" or die $!;
315 L->flush or die $!;
316
317 if (eval {
318
319     open S, "|/usr/lib/sendmail -odb -oee -oi -t" or die $!;
320     print S $email or die $!;
321     $!=0; $?=0; close S or die $!; $? and die $?;
322
323     rename $fn_t, $fn_i or die "$fn_t $fn_i $!";
324
325     1;
326 }) {
327     print L "$ll stored\n" or die $!;
328 } else {
329     print L "$ll ERROR! $@\n" or die $!;
330     exit 1;
331 }
332 close L or die $!;
333
334 print header('text/plain'), "OK $fn_i\n" or die $!;