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