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