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/;
22 #---------- pixmaps ----------
24 sub parseentryin__pixmap ($) {
27 m/^(\S+ \- .*)\nP3\n([1-9]\d{1,3}) ([1-9]\d{1,3})\n255\n/s or die;
28 my ($def,$w,$h)= ($1, $2+0, $3+0);
29 my @d= grep { m/./ } split /\s+/, $';
30 @d == $w*$h*3 or die "$d[0]|$d[1]|...|$d[$#d-1]|$d[$#d] ?";
34 $_ >= 0 or die "$_ ?";
35 $_ <= 255 or die "$_ ?";
37 my $ppm= "P3\n$w $h\n255\n";
39 for (my $y=0; $y<$h; $y++) {
40 for (my $x=0; $x<$w; $x++, $di+=3) {
41 #print STDERR ">$x,$y,$di,",scalar(@d),"<\n";
42 $ppm .= sprintf " %3d %3d %3d", @d[$di..$di+2];
47 my $icon= pipeval($ppm,
49 'pnmscale -xysize 156 80',
50 'pnmnorm -bpercent 40 -wpercent 20',
51 'pgmtopbm -threshold',
55 my $whole= pipeval($ppm,
57 'pnmnorm -bpercent 10 -wpercent 5',
58 'pgmtopbm -threshold',
62 my $entry= "$def\n$ppm";
64 return ('',$def,$ppm,$ppm,$def, $w,$icon,$whole,$entry);
67 #---------- characters ----------
69 sub parseentryin__char ($$) {
71 $ei =~ m/^(Digit|Upper|Lower|Word)\n([^\n]+)\n/s or die;
72 my ($ctx,$str)= ($1,$2);
73 print STDERR ">ctx=$ctx|str=$str|$'<\n";
74 my @d= grep { m/./ } split /\n/, $';
75 print STDERR ">@d<\n";
81 my $pgm= "P2\n$h $w\n7\n";
82 map { # x, left to right
83 m/[^0-7]/ and die "$_ ?";
89 my $key= join ' ', $ctx, @d;
95 my $icon= pipeval($pgm,
96 "pnmscale -xysize 156 ".($h*4),
97 'pgmtopbm -threshold',
101 my $whole= pipeval($pgm,
107 my $entry= "$ctx\n$str\n$key\n";
109 return ($ctx,$str,$pgm,$key,$str, $w*4,$icon,$whole,$entry);
112 #---------- useful stuff ----------
115 my ($val, @cmds) = @_;
120 foreach my $cmd ('',@cmds) {
121 my $pipe= new IO::Pipe or die $!;
122 my $pid= fork(); defined $pid or die $!;
127 print $pipe $val or die $!;
130 open STDIN, '<&', $lastpipe or die $!;
131 open STDOUT, '>&', $pipe or die $!;
132 close $lastpipe or die $!;
133 close $pipe or die $!;
138 if ($lastpipe) { close $lastpipe or die $!; }
143 $!=0; { local ($/)=undef; $val= <$lastpipe>; }
144 defined $val or die $!;
145 $lastpipe->error and die $!; close $lastpipe or die $!;
147 foreach my $cmd ('(paste)', @cmds) {
148 my $pid= shift @pids;
149 waitpid($pid,0) == $pid or die "$pid $? $!";
150 $?==0 or $?==13 or die "$cmd $?";
155 #========== main program ==========
157 #---------- determine properties of the submission ----------
159 my $dict= param('dict');
160 my $entry_in= param('entry');
161 defined $entry_in or die Dump()." ?";
163 my $ocean= param('ocean');
164 my $pirate= param('pirate');
165 if (defined $ocean && defined $pirate) {
166 $pirate= "$ocean - $pirate";
171 my $caller= $ENV{'REMOTE_ADDR'};
172 $caller= 'LOCAL' unless defined $caller;
174 my $fwdf= $ENV{'HTTP_X_FORWARDED_FOR'};
177 $fwdf =~ s/[^0-9.,]/?/g;
178 $caller= "$fwdf,$pirate";
184 if ($dict =~ m/^pixmap$/) {
186 } elsif ($dict =~ m/^(char)([1-9]\d?)$/) {
187 ($kind,@xa)= ($1,$2);
193 my ($ctx,$def,$image,$key,$val, $width,$icon,$whole,$entry)=
194 &{"parseentryin__$kind"}($entry_in, @xa);
196 #---------- compute the email to send ----------
198 my $whoami= `whoami`; $? and die $?;
203 Subject: pctb $dict $ctx $def [ypp-sc-tools]
216 $email .= "$icon\n\n";
219 $whole =~ s/(.*)\n/ sprintf "%-${width}s\n", $1 /mge;
221 $whole =~ s/\n/|\n/mg;
222 $whole =~ s/^(.*)/ ",".('_' x $width).".\n".$1 /e;
223 $whole =~ s/(.*)$/ $1."\n\`".('~' x $width)."'\n" /e;
227 while ($whole =~ m/../) {
229 $lhs =~ s/^(.{0,$lw}).*$/$1/mg;
230 $whole =~ s/^.{1,$lw}//mg;
231 #print STDERR "[[[[[$lhs########$whole]]]]]\n";
238 my $cutline= "-8<-\n";
239 $email .= $cutline.$entry.$cutline;
241 #---------- prepare the database entry ----------
243 my $du=$ENV{'YPPSC_DICTUPDATES'};
244 chdir $du or die "$du $!"
248 my $fn_t= "_update.$$-xxxxxxxxxxxxxxxx.tmp";
249 open F, "> $fn_t" or die "$fn_t $!";
251 my $fn_i= sprintf "_update.$$-%016x.rdy", (stat _)[1];
253 print F "ypp-sc-tools dictionary update v2\n";
255 foreach my $v ($pirate,$caller,$dict,$ctx,$def,$image,$key,$val) {
256 printf F "%d\n", length($v) or die $!;
257 print F $v,"\n" or die $!;
263 my $tm= strftime "%Y-%m-%d %H:%M:%S %Z", @tm;
265 open L, ">> _dict.log" or die $!;
266 my $ll= sprintf "%s %-6s %-31s %-31s %s", $tm, $dict, $pirate, $caller, $fn_i;
268 #---------- commit everything ----------
270 print L "$ll submit\n" or die $!;
275 open S, "|/usr/lib/sendmail -odb -oee -oi -t" or die $!;
276 print S $email or die $!;
277 $!=0; $?=0; close S or die $!; $? and die $?;
279 rename $fn_t, $fn_i or die "$fn_t $fn_i $!";
283 print L "$ll stored\n" or die $!;
285 print L "$ll ERROR! $@\n" or die $!;
290 print header('text/plain'), "OK $fn_i\n" or die $!;