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