3 # This script is invoked when the YPP SC PCTB client phones home to
4 # provide updated character set OCR data or updated screenshot pixmap
5 # interpretation (island name) data.
7 # The client will also phone home anyway to fetch the latest parsedb
10 # This allows me (the operator of the SC server) to:
11 # - review the choices made by the user
12 # - if they are correct, incorporate them in the next client version
13 # - if they are wrong, incorporate fixes of them, or contradictions of them,
16 # The information reported
17 # The SC PCTB client does this so that
19 use strict (qw(vars));
22 $CGI::POST_MAX= 65536;
23 $CGI::DISABLE_UPLOADS= 1;
25 use CGI qw/:standard -private_tempfiles/;
29 #---------- pixmaps ----------
31 sub parseentryin__pixmap ($) {
34 m/^(\w+ \- \w[-+\'\"\#! 0-9a-z]*)\nP3\n([1-9]\d{1,3}) ([1-9]\d{1,3})\n255\n/s or die;
35 my ($def,$w,$h)= ($1, $2+0, $3+0);
36 my @d= grep { m/./ } split /\s+/, $';
37 @d == $w*$h*3 or die "$d[0]|$d[1]|...|$d[$#d-1]|$d[$#d] ?";
41 $_ >= 0 or die "$_ ?";
42 $_ <= 255 or die "$_ ?";
44 my $ppm= "P3\n$w $h\n255\n";
46 for (my $y=0; $y<$h; $y++) {
47 for (my $x=0; $x<$w; $x++, $di+=3) {
48 #print STDERR ">$x,$y,$di,",scalar(@d),"<\n";
49 $ppm .= sprintf " %3d %3d %3d", @d[$di..$di+2];
54 my $icon= pipeval($ppm,
56 'pnmscale -xysize 156 80',
57 'pnmnorm -bpercent 40 -wpercent 20',
58 'pgmtopbm -threshold',
62 my $whole= pipeval($ppm,
64 'pnmnorm -bpercent 40 -wpercent 20',
65 'pgmtopbm -threshold',
69 my $entry= "$def\n$ppm";
71 return ('',$def,$ppm,$ppm,$def, $w,$icon,$whole,$entry);
74 #---------- characters ----------
76 sub parseentryin__char ($$) {
78 $ei =~ m/^(Digit|Upper|Lower)\n((?:[-&\'A-F0-9a-f ]|\x20)+)\n/s or die;
79 my ($ctx,$str)= ($1,$2);
80 #print STDERR ">$'<\n";
81 my @d= grep { m/./ } split /\n/, $';
82 #print STDERR ">@d<\n";
85 my $maxval= (1<<$h)-1;
87 m/^[0-9a-f]{1,8}$/ or die;
89 die "$_ ?" if $_ > $maxval;
92 my $ppm= "P2\n$w $h\n1\n";
94 for (my $y=0; $y<$h; $y++) {
95 for (my $x=0; $x<$w; $x++) {
96 my $pix= !($d[$x] & (1<<$y));
97 $ppm .= sprintf " %d", $pix;
98 $whole .= $pix ? ' ' : '<>';
103 map { $_= sprintf "%x", $_; } @d;
104 my $key= join ' ', $ctx, @d;
106 # my $whole= pipeval($ppm,
108 # 'pgmtopbm -threshold',
111 my $entry= "$ctx\n$str\n". join("\n", @d). "\n";
113 return ($ctx,$str,$ppm,$key,$str, $w*2,'',$whole,$entry);
116 #---------- useful stuff ----------
119 my ($val, @cmds) = @_;
124 foreach my $cmd ('',@cmds) {
125 my $pipe= new IO::Pipe or die $!;
126 my $pid= fork(); defined $pid or die $!;
131 print $pipe $val or die $!;
134 open STDIN, '<&', $lastpipe or die $!;
135 open STDOUT, '>&', $pipe or die $!;
136 close $lastpipe or die $!;
137 close $pipe or die $!;
142 if ($lastpipe) { close $lastpipe or die $!; }
147 $!=0; { local ($/)=undef; $val= <$lastpipe>; }
148 defined $val or die $!;
149 $lastpipe->error and die $!; close $lastpipe or die $!;
151 foreach my $cmd ('(paste)', @cmds) {
152 my $pid= shift @pids;
153 waitpid($pid,0) == $pid or die "$pid $? $!";
154 $?==0 or $?==13 or die "$cmd $?";
159 #========== main program ==========
161 #---------- determine properties of the submission ----------
163 my $dict= param('dict');
164 my $entry_in= param('entry');
165 defined $entry_in or die;
167 my $ocean= param('ocean');
168 my $pirate= param('pirate');
169 if (defined $ocean && defined $pirate) {
170 $pirate= "$ocean - $pirate";
172 $pirate= $ENV{'REMOTE_ADDR'};
173 my $fwdf= $ENV{'HTTP_X_FORWARDED_FOR'};
176 $fwdf =~ s/[^0-9.,]/?/g;
177 $pirate= "$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]
214 $email .= "$icon\n\n";
217 $whole =~ s/(.*)\n/ sprintf "%-${width}s\n", $1 /mge;
219 $whole =~ s/\n/|\n/mg;
220 $whole =~ s/^(.*)/ ",".('_' x $width).".\n".$1 /e;
221 $whole =~ s/(.*)$/ $1."\n\`".('~' x $width)."'\n" /e;
225 while ($whole =~ m/../) {
227 $lhs =~ s/^(.{0,$lw}).*$/$1/mg;
228 $whole =~ s/^.{1,$lw}//mg;
229 #print STDERR "[[[[[$lhs########$whole]]]]]\n";
236 my $cutline= "-8<-\n";
237 $email .= $cutline.$entry.$cutline;
239 #---------- prepare the database entry ----------
241 my $du=$ENV{'YPPSC_DICTUPDATES'};
242 chdir $du or die "$du $!"
246 my $fn_t= "_update.$$-xxxxxxxxxxxxxxxx.tmp";
247 open F, "> $fn_t" or die "$fn_t $!";
249 my $fn_i= sprintf "_update.$$-%016x.rdy", (stat _)[1];
251 print F "ypp-sc-tools dictionary update v1\n";
253 foreach my $v ($pirate,$dict,$ctx,$def,$image,$key,$val) {
254 printf F "%d\n", length($v) or die $!;
255 print F $v,"\n" or die $!;
261 my $tm= strftime "%Y-%m-%d %H:%M:%S %Z", @tm;
263 open L, ">> _dict.log" or die $!;
264 my $ll= sprintf "%s %-6s %-31s %s", $tm, $dict, $pirate, $fn_i;
266 #---------- commit everything ----------
268 print L "$ll submit\n" or die $!;
273 open S, "|sendmail -odb -oee -oi -t" or die $!;
274 print S $email or die $!;
275 $!=0; $?=0; close S or die $!; $? and die $?;
277 rename $fn_t, $fn_i or die "$fn_t $fn_i $!";
281 print L "$ll stored\n" or die $!;
283 print L "$ll ERROR! $@\n" or die $!;
287 print header('text/plain'), "$fn_i\n" or die $!;