3 # This script is invoked when the yarrg wants to send an update to the
4 # dictionary server. See README.privacy.
6 # This is part of ypp-sc-tools, a set of third-party tools for assisting
7 # players of Yohoho Puzzle Pirates.
9 # Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
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.
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.
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/>.
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.
29 # upload testing runes:
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 '' . .
34 use strict (qw(vars));
37 $CGI::POST_MAX= 1024*1024;
38 $CGI::DISABLE_UPLOADS= 1;
40 use CGI qw/:standard -private_tempfiles/;
48 #---------- pixmaps ----------
50 sub parseentryin__pixmap ($) {
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] ?";
60 $_ >= 0 or die "$_ ?";
61 $_ <= 255 or die "$_ ?";
63 my $ppm= "P3\n$w $h\n255\n";
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];
73 my $icon= pipeval($ppm,
75 'pnmscale -xysize 156 80',
76 'pnmnorm -bpercent 40 -wpercent 20',
77 'pgmtopbm -threshold',
81 my $whole= pipeval($ppm,
83 'pnmnorm -bpercent 10 -wpercent 5',
84 'pgmtopbm -threshold',
88 my $entry= "$def\n$ppm";
90 return ('',$def,$ppm,$ppm,$def, $w,$icon,$whole,$entry);
93 #---------- characters ----------
95 sub parseentryin__char ($$) {
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";
107 my $maxval= (1<<$aadepth)-1;
108 die 'cannot do [^0...$maxval]!' if $maxval>9;
110 my $pgm= "P2\n$h $w\n$maxval\n";
111 map { # x, left to right
112 m/[^0-$maxval]/ and die "$_ ?";
118 my $key= join ' ', $ctx, @d;
124 my $icon= pipeval($pgm,
125 "pnmscale -xysize 156 ".($h*4),
126 'pgmtopbm -threshold',
130 my $whole= pipeval($pgm,
136 my $entry= "$ctx\n$str\n$key\n";
138 return ($ctx,$str,$pgm,$key,$str, $w*4,$icon,$whole,$entry);
141 #---------- useful stuff ----------
144 my ($val, @cmds) = @_;
149 foreach my $cmd ('',@cmds) {
150 my $pipe= new IO::Pipe or die $!;
151 my $pid= fork(); defined $pid or die $!;
156 print $pipe $val or die $!;
159 open STDIN, '<&', $lastpipe or die $!;
160 open STDOUT, '>&', $pipe or die $!;
161 close $lastpipe or die $!;
162 close $pipe or die $!;
167 if ($lastpipe) { close $lastpipe or die $!; }
172 $!=0; { local ($/)=undef; $val= <$lastpipe>; }
173 defined $val or die $!;
174 $lastpipe->error and die $!; close $lastpipe or die $!;
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 $?";
184 #========== main program ==========
186 #---------- determine properties of the submission ----------
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";
198 my $dict= param('dict');
199 my $entry_in= param('entry');
200 defined $entry_in or die Dump()." ?";
202 my $ocean= param('ocean');
203 my $pirate= param('pirate');
204 if (defined $ocean && defined $pirate) {
205 $pirate= "$ocean - $pirate";
210 my $caller= cgi_get_caller();
215 if ($dict =~ m/^pixmap$/) {
217 } elsif ($dict =~ m/^(char)([1-9]\d?)$/) {
218 ($kind,@xa)= ($1,$2);
224 my ($ctx,$def,$image,$key,$val, $width,$icon,$whole,$entry)=
225 &{"parseentryin__$kind"}($entry_in, @xa);
227 my $du=$ENV{'YPPSC_DICTUPDATES'};
228 chdir $du or die "$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,^.*/,,;
239 #---------- compute the email to send ----------
241 my $whoami= `whoami`; $? and die $?;
246 Subject: pctb /$instance/ $dict $ctx $def [ypp-sc-tools]
259 $email .= "$icon\n\n";
262 $whole =~ s/(.*)\n/ sprintf "%-${width}s\n", $1 /mge;
264 $whole =~ s/\n/|\n/mg;
265 $whole =~ s/^(.*)/ ",".('_' x $width).".\n".$1 /e;
266 $whole =~ s/(.*)$/ $1."\n\`".('~' x $width)."'\n" /e;
270 while ($whole =~ m/../) {
272 $lhs =~ s/^(.{0,$lw}).*$/$1/mg;
273 $whole =~ s/^.{1,$lw}//mg;
274 #print STDERR "[[[[[$lhs########$whole]]]]]\n";
281 my $cutline= "-8<-\n";
282 $email .= $cutline.$entry.$cutline;
284 #---------- prepare the database entry ----------
286 my $fn_t= "_update.$$-xxxxxxxxxxxxxxxx.tmp";
287 open F, "> $fn_t" or die "$fn_t $!";
289 my $fn_i= sprintf "_update.$$-%016x.rdy", (stat _)[1];
291 print F "ypp-sc-tools dictionary update v3 depth=$aadepth\n";
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 $!;
301 my $tm= strftime "%Y-%m-%d %H:%M:%S %Z", @tm;
303 open L, ">> _dict.log" or die $!;
304 my $ll= sprintf "%s %-6s %-31s %-31s %s", $tm, $dict, $pirate, $caller, $fn_i;
306 #---------- commit everything ----------
308 print L "$ll submit\n" or die $!;
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 $?;
317 rename $fn_t, $fn_i or die "$fn_t $fn_i $!";
321 print L "$ll stored\n" or die $!;
323 print L "$ll ERROR! $@\n" or die $!;
328 print header('text/plain'), "OK $fn_i\n" or die $!;