chiark / gitweb /
Version protocol better error message
[ypp-sc-tools.db-live.git] / pctb / dictionary-update-receiver
1 #!/usr/bin/perl -w
2 #
3 # This script is invoked when the YPP SC PCTB client talks to the
4 # dictionary server.  See README.privacy.
5
6
7 # upload testing runes:
8 #
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 '' . .
11
12 use strict (qw(vars));
13 use POSIX;
14
15 $CGI::POST_MAX= 1024*1024;
16 $CGI::DISABLE_UPLOADS= 1;
17
18 use CGI qw/:standard -private_tempfiles/;
19 use IO::Pipe;
20 use IO::Handle;
21
22 my $aadepth=2;
23
24 #---------- pixmaps ----------
25
26 sub parseentryin__pixmap ($) {
27     my ($entry_in) = @_;
28     $entry_in =~
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] ?";
33     map {
34         m/\D/ and die "$& ?";
35         $_ += 0;
36         $_ >= 0 or die "$_ ?";
37         $_ <= 255 or die "$_ ?";
38     } @d;
39     my $ppm= "P3\n$w $h\n255\n";
40     my $di=0;
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];
45         }
46         $ppm .= "\n";
47     }
48
49     my $icon= pipeval($ppm,
50                          'ppmtopgm',
51                          'pnmscale -xysize 156 80',
52                          'pnmnorm -bpercent 40 -wpercent 20',
53                          'pgmtopbm -threshold',
54                          'pnminvert',
55                          'pbmtoascii -2x4');
56
57     my $whole= pipeval($ppm,
58                        'ppmtopgm',
59                        'pnmnorm -bpercent 10 -wpercent 5',
60                        'pgmtopbm -threshold',
61                        'pnminvert',
62                        'pbmtoascii');
63
64     my $entry= "$def\n$ppm";
65
66     return ('',$def,$ppm,$ppm,$def, $w,$icon,$whole,$entry);
67 }
68
69 #---------- characters ----------
70
71 sub parseentryin__char ($$) {
72     my ($ei,$h) = @_;
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";
78     die if $h>100;
79     die if @d>400;
80
81     my $w= @d;
82
83     my $maxval= (1<<$aadepth)-1;
84     die 'cannot do [^0...$maxval]!' if $maxval>9;
85
86     my $pgm= "P2\n$h $w\n$maxval\n";
87     map { # x, left to right
88         m/[^0-$maxval]/ and die "$_ ?";
89         my $l= $_;
90         $l =~ s/./ $&/g;
91         $pgm .= "$l\n";
92     } @d;
93
94     my $key= join ' ', $ctx, @d;
95
96     $pgm= pipeval($pgm,
97                   "pnmflip -xy",
98                   "pnmnoraw");
99
100     my $icon= pipeval($pgm,
101                       "pnmscale -xysize 156 ".($h*4),
102                       'pgmtopbm -threshold',
103                       'pnminvert',
104                       'pbmtoascii -2x4');
105
106     my $whole= pipeval($pgm,
107                        "pnmscale 4",
108                        'pgmtopbm -fs',
109                        'pnminvert',
110                        'pbmtoascii');
111
112     my $entry= "$ctx\n$str\n$key\n";
113     
114     return ($ctx,$str,$pgm,$key,$str, $w*4,$icon,$whole,$entry);
115 }
116
117 #---------- useful stuff ----------
118
119 sub pipeval ($@) {
120     my ($val, @cmds) = @_;
121     my (@pids);
122
123     my $lastpipe;
124     
125     foreach my $cmd ('',@cmds) {
126         my $pipe= new IO::Pipe or die $!;
127         my $pid= fork();  defined $pid or die $!;
128
129         if (!$pid) {
130             $pipe->writer();
131             if (!$lastpipe) {
132                  print $pipe $val or die $!;
133                  exit 0;
134              } else {
135                  open STDIN, '<&', $lastpipe or die $!;
136                  open STDOUT, '>&', $pipe or die $!;
137                  close $lastpipe or die $!;
138                  close $pipe or die $!;
139                  exec $cmd; die $!;
140              }
141         }
142         $pipe->reader();
143         if ($lastpipe) { close $lastpipe or die $!; }
144         $lastpipe= $pipe;
145         push @pids, $pid;
146     }
147
148     $!=0; { local ($/)=undef; $val= <$lastpipe>; }
149     defined $val or die $!;
150     $lastpipe->error and die $!;  close $lastpipe or die $!;
151
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 $?";
156     }
157     return $val;
158 }
159
160 #========== main program ==========
161
162 #---------- determine properties of the submission ----------
163
164 my $version= param('version');
165 my $spec_aadepth= param('depth');
166 if ($version ne '3'  ||  $spec_aadepth ne $aadepth) {
167     print header('text/plain',
168                  "403 YPP SC PCTB client is out of date".
169                  " ($version, $spec_aadepth)");
170     print "\nYour YPP SC client is out of date.\n";
171     exit 0;
172 }
173
174 my $dict= param('dict');
175 my $entry_in= param('entry');
176 defined $entry_in or die Dump()." ?";
177
178 my $ocean= param('ocean');
179 my $pirate= param('pirate');
180 if (defined $ocean && defined $pirate) {
181     $pirate= "$ocean - $pirate";
182 } else {
183     $pirate= '';
184 }
185
186 my $caller= $ENV{'REMOTE_ADDR'};
187 $caller= 'LOCAL' unless defined $caller;
188
189 my $fwdf= $ENV{'HTTP_X_FORWARDED_FOR'};
190 if (defined $fwdf) {
191     $fwdf =~ s/\s//g;
192     $fwdf =~ s/[^0-9.,]/?/g;
193     $caller= "$fwdf,$pirate";
194 }
195
196 my $kind;
197 my @xa;
198
199 if ($dict =~ m/^pixmap$/) {
200     $kind= $&;
201 } elsif ($dict =~ m/^(char)([1-9]\d?)$/) {
202     ($kind,@xa)= ($1,$2);
203 } else {
204     die "$dict ?";
205 }
206 $dict= $&;
207
208 my ($ctx,$def,$image,$key,$val, $width,$icon,$whole,$entry)=
209     &{"parseentryin__$kind"}($entry_in, @xa);
210
211 my $du=$ENV{'YPPSC_DICTUPDATES'};
212 chdir $du or die "$du $!"
213     if defined $du;
214
215 my $instance= $du;
216 $instance =~ s,ypp-sc-tools,,ig;
217 $instance =~ s,ypp,,ig;
218 $instance =~ s,pctb,,ig;
219 $instance =~ s,/\W+/,/,g;
220 $instance =~ s,/+$,,;
221 $instance =~ s,^.*/,,;
222
223 #---------- compute the email to send ----------
224
225 my $whoami= `whoami`; $? and die $?;
226 chomp $whoami;
227
228 my $email= <<END
229 To: $whoami
230 Subject: pctb /$instance/ $dict $ctx $def [ypp-sc-tools]
231
232 Pirate:     $pirate
233 Caller:     $caller
234 Dictionary: $dict
235 Context:    $ctx
236 Definition: $def
237
238 END
239     ;
240
241 if (length $icon) {
242     $icon =~ s/^/ /gm;
243     $email .= "$icon\n\n";
244 }
245
246 $whole =~ s/(.*)\n/ sprintf "%-${width}s\n", $1 /mge;
247 $whole =~ s/^/|/mg;
248 $whole =~ s/\n/|\n/mg;
249 $whole =~ s/^(.*)/ ",".('_' x $width).".\n".$1 /e;
250 $whole =~ s/(.*)$/ $1."\n\`".('~' x $width)."'\n" /e;
251
252 my $lw= 79;
253
254 while ($whole =~ m/../) {
255     my $lhs= $whole;
256     $lhs =~ s/^(.{0,$lw}).*$/$1/mg;
257     $whole =~ s/^.{1,$lw}//mg;
258 #print STDERR "[[[[[$lhs########$whole]]]]]\n";
259     $email .= $lhs;
260 }
261
262 END
263     ;
264
265 my $cutline= "-8<-\n";
266 $email .= $cutline.$entry.$cutline;
267
268 #---------- prepare the database entry ----------
269
270 my $fn_t= "_update.$$-xxxxxxxxxxxxxxxx.tmp";
271 open F, "> $fn_t" or die "$fn_t $!";
272 (stat F) or die $!;
273 my $fn_i= sprintf "_update.$$-%016x.rdy", (stat _)[1];
274
275 print F "ypp-sc-tools dictionary update v3 depth=$aadepth\n";
276
277 foreach my $v ($pirate,$caller,$dict,$ctx,$def,$image,$key,$val) {
278     printf F "%d\n", length($v) or die $!;
279     print F $v,"\n" or die $!;
280 }
281
282 close F or die $!;
283
284 my @tm= localtime;
285 my $tm= strftime "%Y-%m-%d %H:%M:%S %Z", @tm;
286
287 open L, ">> _dict.log" or die $!;
288 my $ll= sprintf "%s %-6s %-31s %-31s %s", $tm, $dict, $pirate, $caller, $fn_i;
289
290 #---------- commit everything ----------
291
292 print L "$ll submit\n" or die $!;
293 L->flush or die $!;
294
295 if (eval {
296
297     open S, "|/usr/lib/sendmail -odb -oee -oi -t" or die $!;
298     print S $email or die $!;
299     $!=0; $?=0; close S or die $!; $? and die $?;
300
301     rename $fn_t, $fn_i or die "$fn_t $fn_i $!";
302
303     1;
304 }) {
305     print L "$ll stored\n" or die $!;
306 } else {
307     print L "$ll ERROR! $@\n" or die $!;
308     exit 1;
309 }
310 close L or die $!;
311
312 print header('text/plain'), "OK $fn_i\n" or die $!;