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 return ('',$def,$ppm,$ppm,$def);
57 #---------- characters ----------
59 sub parseentryin__char ($$) {
61 $ei =~ m/^(Digit|Upper|Lower)\n((?:[-&\'A-F0-9a-f ]|\x20)+)\n/s or die;
62 my ($ctx,$str)= ($1,$2);
63 #print STDERR ">$'<\n";
64 my @d= grep { m/./ } split /\n/, $';
65 #print STDERR ">@d<\n";
68 my $maxval= (1<<$h)-1;
70 m/^[0-9a-f]{1,8}$/ or die;
72 die "$_ ?" if $_ > $maxval;
75 my $ppm= "P2\n$w $h\n1\n";
76 for (my $y=0; $y<$h; $y++) {
77 for (my $x=0; $x<$w; $x++) {
78 $ppm .= sprintf " %d", !($d[$x] & (1<<$y));
82 my $key= join ' ', $ctx, map { sprintf "%x", $_; } @d;
84 return ($ctx,$str,$ppm,$key,$str);
87 #---------- main program ----------
89 my $dict= param('dict');
90 my $entry_in= param('entry');
91 defined $entry_in or die;
93 my $ocean= param('ocean');
94 my $pirate= param('pirate');
95 if (defined $ocean && defined $pirate) {
96 $pirate= "$ocean - $pirate";
98 $pirate= $ENV{'REMOTE_ADDR'};
99 my $fwdf= $ENV{'HTTP_X_FORWARDED_FOR'};
102 $fwdf =~ s/[^0-9.,]/?/g;
103 $pirate= "$fwdf,$pirate";
107 my $du=$ENV{'YPPSC_DICTUPDATES'};
108 chdir $du or die "$du $!"
114 if ($dict =~ m/^pixmap$/) {
116 } elsif ($dict =~ m/^(char)([1-9]\d?)$/) {
117 ($kind,@xa)= ($1,$2);
123 my ($ctx,$def,$image,$key,$val)= &{"parseentryin__$kind"}($entry_in, @xa);
125 my $fn_t= "_update.$$-xxxxxxxxxxxxxxxx.tmp";
126 open F, "> $fn_t" or die "$fn_t $!";
128 my $fn_i= sprintf "_update.$$-%016x.rdy", (stat _)[1];
130 print F "ypp-sc-tools dictionary update v1\n";
132 foreach my $v ($pirate,$dict,$ctx,$def,$image,$key,$val) {
133 printf F "%d\n", length($v) or die $!;
134 print F $v,"\n" or die $!;
140 my $tm= strftime "%Y-%m-%d %H:%M:%S %Z", @tm;
142 open L, ">> _dict.log" or die $!;
143 my $ll= sprintf "%s %-6s %-31s %s %s\n", $tm, $dict, $pirate, $fn_i, "submit";
144 print L $ll or die $!;
147 rename $fn_t, $fn_i or die "$fn_t $fn_i $!";