3 # This script is invoked when the YPP SC PCTB client talks to the
4 # dictionary server. See README.privacy.
7 # upload testing runes:
9 # YPPSC_PCTB_DICT_UPDATE=./ YPPSC_PCTB_DICT_SUBMIT=./ ./ypp-commodities --ocean midnight --pirate aristarchus --find-island --same --raw-tsv >raw.tsv
10 # ./dictionary-manager --debug --approve-updates '' . .
12 use strict (qw(vars));
15 $CGI::POST_MAX= 1024*1024;
16 $CGI::DISABLE_UPLOADS= 1;
18 use CGI qw/:standard -private_tempfiles/;
24 #---------- pixmaps ----------
26 sub parseentryin__pixmap ($) {
29 m/^(\S+ \- .*)\nP3\n([1-9]\d{1,3}) ([1-9]\d{1,3})\n255\n/s or die;
30 my ($def,$w,$h)= ($1, $2+0, $3+0);
31 my @d= grep { m/./ } split /\s+/, $';
32 @d == $w*$h*3 or die "$d[0]|$d[1]|...|$d[$#d-1]|$d[$#d] ?";
36 $_ >= 0 or die "$_ ?";
37 $_ <= 255 or die "$_ ?";
39 my $ppm= "P3\n$w $h\n255\n";
41 for (my $y=0; $y<$h; $y++) {
42 for (my $x=0; $x<$w; $x++, $di+=3) {
43 #print STDERR ">$x,$y,$di,",scalar(@d),"<\n";
44 $ppm .= sprintf " %3d %3d %3d", @d[$di..$di+2];
49 my $icon= pipeval($ppm,
51 'pnmscale -xysize 156 80',
52 'pnmnorm -bpercent 40 -wpercent 20',
53 'pgmtopbm -threshold',
57 my $whole= pipeval($ppm,
59 'pnmnorm -bpercent 10 -wpercent 5',
60 'pgmtopbm -threshold',
64 my $entry= "$def\n$ppm";
66 return ('',$def,$ppm,$ppm,$def, $w,$icon,$whole,$entry);
69 #---------- characters ----------
71 sub parseentryin__char ($$) {
73 $ei =~ m/^(Digit|Upper|Lower|Word)\n([^\n]+)\n/s or die;
74 my ($ctx,$str)= ($1,$2);
75 print STDERR ">ctx=$ctx|str=$str|$'<\n";
76 my @d= grep { m/./ } split /\n/, $';
77 print STDERR ">@d<\n";
83 my $maxval= (1<<$aadepth)-1;
84 die 'cannot do [^0...$maxval]!' if $maxval>9;
86 my $pgm= "P2\n$h $w\n$maxval\n";
87 map { # x, left to right
88 m/[^0-$maxval]/ and die "$_ ?";
94 my $key= join ' ', $ctx, @d;
100 my $icon= pipeval($pgm,
101 "pnmscale -xysize 156 ".($h*4),
102 'pgmtopbm -threshold',
106 my $whole= pipeval($pgm,
112 my $entry= "$ctx\n$str\n$key\n";
114 return ($ctx,$str,$pgm,$key,$str, $w*4,$icon,$whole,$entry);
117 #---------- useful stuff ----------
120 my ($val, @cmds) = @_;
125 foreach my $cmd ('',@cmds) {
126 my $pipe= new IO::Pipe or die $!;
127 my $pid= fork(); defined $pid or die $!;
132 print $pipe $val or die $!;
135 open STDIN, '<&', $lastpipe or die $!;
136 open STDOUT, '>&', $pipe or die $!;
137 close $lastpipe or die $!;
138 close $pipe or die $!;
143 if ($lastpipe) { close $lastpipe or die $!; }
148 $!=0; { local ($/)=undef; $val= <$lastpipe>; }
149 defined $val or die $!;
150 $lastpipe->error and die $!; close $lastpipe or die $!;
152 foreach my $cmd ('(paste)', @cmds) {
153 my $pid= shift @pids;
154 waitpid($pid,0) == $pid or die "$pid $? $!";
155 $?==0 or $?==13 or die "$cmd $?";
160 #========== main program ==========
162 #---------- determine properties of the submission ----------
164 my $dict= param('dict');
165 my $entry_in= param('entry');
166 defined $entry_in or die Dump()." ?";
168 my $ocean= param('ocean');
169 my $pirate= param('pirate');
170 if (defined $ocean && defined $pirate) {
171 $pirate= "$ocean - $pirate";
176 my $caller= $ENV{'REMOTE_ADDR'};
177 $caller= 'LOCAL' unless defined $caller;
179 my $fwdf= $ENV{'HTTP_X_FORWARDED_FOR'};
182 $fwdf =~ s/[^0-9.,]/?/g;
183 $caller= "$fwdf,$pirate";
189 if ($dict =~ m/^pixmap$/) {
191 } elsif ($dict =~ m/^(char)([1-9]\d?)$/) {
192 ($kind,@xa)= ($1,$2);
198 my ($ctx,$def,$image,$key,$val, $width,$icon,$whole,$entry)=
199 &{"parseentryin__$kind"}($entry_in, @xa);
201 my $du=$ENV{'YPPSC_DICTUPDATES'};
202 chdir $du or die "$du $!"
206 $instance =~ s,ypp-sc-tools,,ig;
207 $instance =~ s,ypp,,ig;
208 $instance =~ s,pctb,,ig;
209 $instance =~ s,/\W+/,/,g;
210 $instance =~ s,/+$,,;
211 $instance =~ s,^.*/,,;
213 #---------- compute the email to send ----------
215 my $whoami= `whoami`; $? and die $?;
220 Subject: pctb /$instance/ $dict $ctx $def [ypp-sc-tools]
233 $email .= "$icon\n\n";
236 $whole =~ s/(.*)\n/ sprintf "%-${width}s\n", $1 /mge;
238 $whole =~ s/\n/|\n/mg;
239 $whole =~ s/^(.*)/ ",".('_' x $width).".\n".$1 /e;
240 $whole =~ s/(.*)$/ $1."\n\`".('~' x $width)."'\n" /e;
244 while ($whole =~ m/../) {
246 $lhs =~ s/^(.{0,$lw}).*$/$1/mg;
247 $whole =~ s/^.{1,$lw}//mg;
248 #print STDERR "[[[[[$lhs########$whole]]]]]\n";
255 my $cutline= "-8<-\n";
256 $email .= $cutline.$entry.$cutline;
258 #---------- prepare the database entry ----------
260 my $fn_t= "_update.$$-xxxxxxxxxxxxxxxx.tmp";
261 open F, "> $fn_t" or die "$fn_t $!";
263 my $fn_i= sprintf "_update.$$-%016x.rdy", (stat _)[1];
265 print F "ypp-sc-tools dictionary update v3 depth=$aadepth\n";
267 foreach my $v ($pirate,$caller,$dict,$ctx,$def,$image,$key,$val) {
268 printf F "%d\n", length($v) or die $!;
269 print F $v,"\n" or die $!;
275 my $tm= strftime "%Y-%m-%d %H:%M:%S %Z", @tm;
277 open L, ">> _dict.log" or die $!;
278 my $ll= sprintf "%s %-6s %-31s %-31s %s", $tm, $dict, $pirate, $caller, $fn_i;
280 #---------- commit everything ----------
282 print L "$ll submit\n" or die $!;
287 open S, "|/usr/lib/sendmail -odb -oee -oi -t" or die $!;
288 print S $email or die $!;
289 $!=0; $?=0; close S or die $!; $? and die $?;
291 rename $fn_t, $fn_i or die "$fn_t $fn_i $!";
295 print L "$ll stored\n" or die $!;
297 print L "$ll ERROR! $@\n" or die $!;
302 print header('text/plain'), "OK $fn_i\n" or die $!;