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