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 the YARRG website. YARRG is a tool and website
7 # for assisting 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 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.
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.
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/>.
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.
28 # upload testing runes:
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 '' . .
33 BEGIN { unshift @INC, qw(.) }
35 use strict (qw(vars));
40 $CGI::POST_MAX= 1024*1024;
41 $CGI::DISABLE_UPLOADS= 1;
43 use CGI qw/:standard -private_tempfiles/;
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 $!; });
58 #---------- pixmaps ----------
60 sub parseentryin__pixmap ($) {
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] ?";
70 $_ >= 0 or die "$_ ?";
71 $_ <= 255 or die "$_ ?";
73 my $ppm= "P3\n$w $h\n255\n";
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];
83 my $icon= pipeval($ppm,
85 'pnmscale -xysize 156 80',
86 'pnmnorm -bpercent 40 -wpercent 20',
87 'pgmtopbm -threshold',
91 my $whole= pipeval($ppm,
93 'pnmnorm -bpercent 10 -wpercent 5',
94 'pgmtopbm -threshold',
98 my $entry= "$def\n$ppm";
100 return ('',$def,$ppm,$ppm,$def, $w,$icon,$whole,$entry);
103 #---------- characters ----------
105 sub parseentryin__char ($$) {
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";
117 my $maxval= (1<<$aadepth)-1;
118 die 'cannot do [^0...$maxval]!' if $maxval>9;
120 my $pgm= "P2\n$h $w\n$maxval\n";
121 map { # x, left to right
122 m/[^0-$maxval]/ and die "$_ ?";
128 my $key= join ' ', $ctx, @d;
134 my $icon= pipeval($pgm,
135 "pnmscale -xysize 156 ".($h*4),
136 'pgmtopbm -threshold',
140 my $whole= pipeval($pgm,
146 my $entry= "$ctx\n$str\n$key\n";
148 return ($ctx,$str,$pgm,$key,$str, $w*4,$icon,$whole,$entry);
151 #---------- useful stuff ----------
154 my ($val, @cmds) = @_;
159 foreach my $cmd ('',@cmds) {
160 my $pipe= new IO::Pipe or die $!;
161 my $pid= fork(); defined $pid or die $!;
166 print $pipe $val or die $!;
169 open STDIN, '<&', $lastpipe or die $!;
170 open STDOUT, '>&', $pipe or die $!;
171 close $lastpipe or die $!;
172 close $pipe or die $!;
177 if ($lastpipe) { close $lastpipe or die $!; }
182 $!=0; { local ($/)=undef; $val= <$lastpipe>; }
183 defined $val or die $!;
184 $lastpipe->error and die $!; close $lastpipe or die $!;
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 $?";
194 #========== main program ==========
196 #---------- determine properties of the submission ----------
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";
208 my $dict= param('dict');
209 my $entry_in= param('entry');
210 defined $entry_in or die Dump()." ?";
212 my $ocean= param('ocean');
213 my $pirate= param('pirate');
214 if (defined $ocean && defined $pirate) {
215 $pirate= "$ocean - $pirate";
220 my $caller= cgi_get_caller();
225 if ($dict =~ m/^pixmap$/) {
227 } elsif ($dict =~ m/^(char)([1-9]\d?)$/) {
228 ($kind,@xa)= ($1,$2);
234 my ($ctx,$def,$image,$key,$val, $width,$icon,$whole,$entry)=
235 &{"parseentryin__$kind"}($entry_in, @xa);
237 my $du=$ENV{'YPPSC_DICTUPDATES'};
238 chdir $du or die "$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,^.*/,,;
249 #---------- compute the email to send ----------
251 my $whoami= `whoami`; $? and die $?;
256 Subject: pctb /$instance/ $dict $ctx $def [ypp-sc-tools]
269 $email .= "$icon\n\n";
272 $whole =~ s/(.*)\n/ sprintf "%-${width}s\n", $1 /mge;
274 $whole =~ s/\n/|\n/mg;
275 $whole =~ s/^(.*)/ ",".('_' x $width).".\n".$1 /e;
276 $whole =~ s/(.*)$/ $1."\n\`".('~' x $width)."'\n" /e;
280 while ($whole =~ m/../) {
282 $lhs =~ s/^(.{0,$lw}).*$/$1/mg;
283 $whole =~ s/^.{1,$lw}//mg;
284 #print STDERR "[[[[[$lhs########$whole]]]]]\n";
291 my $cutline= "-8<-\n";
292 $email .= $cutline.$entry.$cutline;
294 #---------- prepare the database entry ----------
296 my $fn_t= "_update.$$-xxxxxxxxxxxxxxxx.tmp";
297 open F, "> $fn_t" or die "$fn_t $!";
299 my $fn_i= sprintf "_update.$$-%016x.rdy", (stat _)[1];
301 print F "ypp-sc-tools dictionary update v3 depth=$aadepth\n";
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 $!;
311 my $tm= strftime "%Y-%m-%d %H:%M:%S %Z", @tm;
313 open L, ">> _dict.log" or die $!;
314 my $ll= sprintf "%s %-6s %-31s %-31s %s", $tm, $dict, $pirate, $caller, $fn_i;
316 #---------- commit everything ----------
318 print L "$ll submit\n" or die $!;
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 $?;
327 rename $fn_t, $fn_i or die "$fn_t $fn_i $!";
331 print L "$ll stored\n" or die $!;
333 print L "$ll ERROR! $@\n" or die $!;
338 print header('text/plain'), "OK $fn_i\n" or die $!;