chiark / gitweb /
Version protocol
[ypp-sc-tools.web-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','403 Client out of date');
168     print "\nYour YPP SC client is out of date.\n";
169     exit 0;
170 }
171
172 my $dict= param('dict');
173 my $entry_in= param('entry');
174 defined $entry_in or die Dump()." ?";
175
176 my $ocean= param('ocean');
177 my $pirate= param('pirate');
178 if (defined $ocean && defined $pirate) {
179     $pirate= "$ocean - $pirate";
180 } else {
181     $pirate= '';
182 }
183
184 my $caller= $ENV{'REMOTE_ADDR'};
185 $caller= 'LOCAL' unless defined $caller;
186
187 my $fwdf= $ENV{'HTTP_X_FORWARDED_FOR'};
188 if (defined $fwdf) {
189     $fwdf =~ s/\s//g;
190     $fwdf =~ s/[^0-9.,]/?/g;
191     $caller= "$fwdf,$pirate";
192 }
193
194 my $kind;
195 my @xa;
196
197 if ($dict =~ m/^pixmap$/) {
198     $kind= $&;
199 } elsif ($dict =~ m/^(char)([1-9]\d?)$/) {
200     ($kind,@xa)= ($1,$2);
201 } else {
202     die "$dict ?";
203 }
204 $dict= $&;
205
206 my ($ctx,$def,$image,$key,$val, $width,$icon,$whole,$entry)=
207     &{"parseentryin__$kind"}($entry_in, @xa);
208
209 my $du=$ENV{'YPPSC_DICTUPDATES'};
210 chdir $du or die "$du $!"
211     if defined $du;
212
213 my $instance= $du;
214 $instance =~ s,ypp-sc-tools,,ig;
215 $instance =~ s,ypp,,ig;
216 $instance =~ s,pctb,,ig;
217 $instance =~ s,/\W+/,/,g;
218 $instance =~ s,/+$,,;
219 $instance =~ s,^.*/,,;
220
221 #---------- compute the email to send ----------
222
223 my $whoami= `whoami`; $? and die $?;
224 chomp $whoami;
225
226 my $email= <<END
227 To: $whoami
228 Subject: pctb /$instance/ $dict $ctx $def [ypp-sc-tools]
229
230 Pirate:     $pirate
231 Caller:     $caller
232 Dictionary: $dict
233 Context:    $ctx
234 Definition: $def
235
236 END
237     ;
238
239 if (length $icon) {
240     $icon =~ s/^/ /gm;
241     $email .= "$icon\n\n";
242 }
243
244 $whole =~ s/(.*)\n/ sprintf "%-${width}s\n", $1 /mge;
245 $whole =~ s/^/|/mg;
246 $whole =~ s/\n/|\n/mg;
247 $whole =~ s/^(.*)/ ",".('_' x $width).".\n".$1 /e;
248 $whole =~ s/(.*)$/ $1."\n\`".('~' x $width)."'\n" /e;
249
250 my $lw= 79;
251
252 while ($whole =~ m/../) {
253     my $lhs= $whole;
254     $lhs =~ s/^(.{0,$lw}).*$/$1/mg;
255     $whole =~ s/^.{1,$lw}//mg;
256 #print STDERR "[[[[[$lhs########$whole]]]]]\n";
257     $email .= $lhs;
258 }
259
260 END
261     ;
262
263 my $cutline= "-8<-\n";
264 $email .= $cutline.$entry.$cutline;
265
266 #---------- prepare the database entry ----------
267
268 my $fn_t= "_update.$$-xxxxxxxxxxxxxxxx.tmp";
269 open F, "> $fn_t" or die "$fn_t $!";
270 (stat F) or die $!;
271 my $fn_i= sprintf "_update.$$-%016x.rdy", (stat _)[1];
272
273 print F "ypp-sc-tools dictionary update v3 depth=$aadepth\n";
274
275 foreach my $v ($pirate,$caller,$dict,$ctx,$def,$image,$key,$val) {
276     printf F "%d\n", length($v) or die $!;
277     print F $v,"\n" or die $!;
278 }
279
280 close F or die $!;
281
282 my @tm= localtime;
283 my $tm= strftime "%Y-%m-%d %H:%M:%S %Z", @tm;
284
285 open L, ">> _dict.log" or die $!;
286 my $ll= sprintf "%s %-6s %-31s %-31s %s", $tm, $dict, $pirate, $caller, $fn_i;
287
288 #---------- commit everything ----------
289
290 print L "$ll submit\n" or die $!;
291 L->flush or die $!;
292
293 if (eval {
294
295     open S, "|/usr/lib/sendmail -odb -oee -oi -t" or die $!;
296     print S $email or die $!;
297     $!=0; $?=0; close S or die $!; $? and die $?;
298
299     rename $fn_t, $fn_i or die "$fn_t $fn_i $!";
300
301     1;
302 }) {
303     print L "$ll stored\n" or die $!;
304 } else {
305     print L "$ll ERROR! $@\n" or die $!;
306     exit 1;
307 }
308 close L or die $!;
309
310 print header('text/plain'), "OK $fn_i\n" or die $!;