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 40 -wpercent 20',
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)\n([^\n]+)\n/s or die;
72 my ($ctx,$str)= ($1,$2);
73 #print STDERR ">$'<\n";
74 my @d= grep { m/./ } split /\n/, $';
75 #print STDERR ">@d<\n";
78 my $maxval= (1<<$h)-1;
80 m/^[0-9a-f]{1,8}$/ or die;
82 die "$_ ?" if $_ > $maxval;
85 my $ppm= "P2\n$w $h\n1\n";
87 for (my $y=0; $y<$h; $y++) {
88 for (my $x=0; $x<$w; $x++) {
89 my $pix= !($d[$x] & (1<<$y));
90 $ppm .= sprintf " %d", $pix;
91 $whole .= $pix ? ' ' : '<>';
96 map { $_= sprintf "%x", $_; } @d;
97 my $key= join ' ', $ctx, @d;
99 # my $whole= pipeval($ppm,
101 # 'pgmtopbm -threshold',
104 my $entry= "$ctx\n$str\n". join("\n", @d). "\n";
106 return ($ctx,$str,$ppm,$key,$str, $w*2,'',$whole,$entry);
109 #---------- useful stuff ----------
112 my ($val, @cmds) = @_;
117 foreach my $cmd ('',@cmds) {
118 my $pipe= new IO::Pipe or die $!;
119 my $pid= fork(); defined $pid or die $!;
124 print $pipe $val or die $!;
127 open STDIN, '<&', $lastpipe or die $!;
128 open STDOUT, '>&', $pipe or die $!;
129 close $lastpipe or die $!;
130 close $pipe or die $!;
135 if ($lastpipe) { close $lastpipe or die $!; }
140 $!=0; { local ($/)=undef; $val= <$lastpipe>; }
141 defined $val or die $!;
142 $lastpipe->error and die $!; close $lastpipe or die $!;
144 foreach my $cmd ('(paste)', @cmds) {
145 my $pid= shift @pids;
146 waitpid($pid,0) == $pid or die "$pid $? $!";
147 $?==0 or $?==13 or die "$cmd $?";
152 #========== main program ==========
154 #---------- determine properties of the submission ----------
156 my $dict= param('dict');
157 my $entry_in= param('entry');
158 defined $entry_in or die Dump()." ?";
160 my $ocean= param('ocean');
161 my $pirate= param('pirate');
162 if (defined $ocean && defined $pirate) {
163 $pirate= "$ocean - $pirate";
168 my $caller= $ENV{'REMOTE_ADDR'};
169 $caller= 'LOCAL' unless defined $caller;
171 my $fwdf= $ENV{'HTTP_X_FORWARDED_FOR'};
174 $fwdf =~ s/[^0-9.,]/?/g;
175 $caller= "$fwdf,$pirate";
181 if ($dict =~ m/^pixmap$/) {
183 } elsif ($dict =~ m/^(char)([1-9]\d?)$/) {
184 ($kind,@xa)= ($1,$2);
190 my ($ctx,$def,$image,$key,$val, $width,$icon,$whole,$entry)=
191 &{"parseentryin__$kind"}($entry_in, @xa);
193 #---------- compute the email to send ----------
195 my $whoami= `whoami`; $? and die $?;
200 Subject: pctb $dict $ctx $def [ypp-sc-tools]
212 $email .= "$icon\n\n";
215 $whole =~ s/(.*)\n/ sprintf "%-${width}s\n", $1 /mge;
217 $whole =~ s/\n/|\n/mg;
218 $whole =~ s/^(.*)/ ",".('_' x $width).".\n".$1 /e;
219 $whole =~ s/(.*)$/ $1."\n\`".('~' x $width)."'\n" /e;
223 while ($whole =~ m/../) {
225 $lhs =~ s/^(.{0,$lw}).*$/$1/mg;
226 $whole =~ s/^.{1,$lw}//mg;
227 #print STDERR "[[[[[$lhs########$whole]]]]]\n";
234 my $cutline= "-8<-\n";
235 $email .= $cutline.$entry.$cutline;
237 #---------- prepare the database entry ----------
239 my $du=$ENV{'YPPSC_DICTUPDATES'};
240 chdir $du or die "$du $!"
244 my $fn_t= "_update.$$-xxxxxxxxxxxxxxxx.tmp";
245 open F, "> $fn_t" or die "$fn_t $!";
247 my $fn_i= sprintf "_update.$$-%016x.rdy", (stat _)[1];
249 print F "ypp-sc-tools dictionary update v1\n";
251 foreach my $v ($pirate,$caller,$dict,$ctx,$def,$image,$key,$val) {
252 printf F "%d\n", length($v) or die $!;
253 print F $v,"\n" or die $!;
259 my $tm= strftime "%Y-%m-%d %H:%M:%S %Z", @tm;
261 open L, ">> _dict.log" or die $!;
262 my $ll= sprintf "%s %-6s %-31s %-31s %s", $tm, $dict, $pirate, $caller, $fn_i;
264 #---------- commit everything ----------
266 print L "$ll submit\n" or die $!;
271 open S, "|/usr/lib/sendmail -odb -oee -oi -t" or die $!;
272 print S $email or die $!;
273 $!=0; $?=0; close S or die $!; $? and die $?;
275 rename $fn_t, $fn_i or die "$fn_t $fn_i $!";
279 print L "$ll stored\n" or die $!;
281 print L "$ll ERROR! $@\n" or die $!;
286 print header('text/plain'), "OK $fn_i\n" or die $!;