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