From 9739f45fc783a87f02d1f435cf04fde3899ad4c7 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 20 Jun 2009 20:57:32 +0100 Subject: [PATCH] dictionary-update-receiver emails again --- pctb/dictionary-update-receiver | 166 +++++++++++++++++++++++++++++--- 1 file changed, 153 insertions(+), 13 deletions(-) diff --git a/pctb/dictionary-update-receiver b/pctb/dictionary-update-receiver index 3e04f6c..078b9f8 100755 --- a/pctb/dictionary-update-receiver +++ b/pctb/dictionary-update-receiver @@ -51,7 +51,24 @@ sub parseentryin__pixmap ($) { $ppm .= "\n"; } - return ('',$def,$ppm,$ppm,$def); + my $icon= pipeval($ppm, + 'ppmtopgm', + 'pnmscale -xysize 156 80', + 'pnmnorm -bpercent 40 -wpercent 20', + 'pgmtopbm -threshold', + 'pnminvert', + 'pbmtoascii -2x4'); + + my $whole= pipeval($ppm, + 'ppmtopgm', + 'pnmnorm -bpercent 40 -wpercent 20', + 'pgmtopbm -threshold', + 'pnminvert', + 'pbmtoascii'); + + my $entry= "$def\n$ppm"; + + return ('',$def,$ppm,$ppm,$def, $w,$icon,$whole,$entry); } #---------- characters ---------- @@ -73,18 +90,75 @@ sub parseentryin__char ($$) { } @d; my $w= @d; my $ppm= "P2\n$w $h\n1\n"; + my $whole=''; for (my $y=0; $y<$h; $y++) { for (my $x=0; $x<$w; $x++) { - $ppm .= sprintf " %d", !($d[$x] & (1<<$y)); + my $pix= !($d[$x] & (1<<$y)); + $ppm .= sprintf " %d", $pix; + $whole .= $pix ? ' ' : '<>'; } $ppm .= "\n"; + $whole .= "\n"; } - my $key= join ' ', $ctx, map { sprintf "%x", $_; } @d; + map { $_= sprintf "%x", $_; } @d; + my $key= join ' ', $ctx, @d; + +# my $whole= pipeval($ppm, +# "pnmscale 2", +# 'pgmtopbm -threshold', +# 'pbmtoascii'); + + my $entry= "$ctx\n$str\n". join("\n", @d). "\n"; + + return ($ctx,$str,$ppm,$key,$str, $w*2,'',$whole,$entry); +} + +#---------- useful stuff ---------- + +sub pipeval ($@) { + my ($val, @cmds) = @_; + my (@pids); + + my $lastpipe; - return ($ctx,$str,$ppm,$key,$str); + foreach my $cmd ('',@cmds) { + my $pipe= new IO::Pipe or die $!; + my $pid= fork(); defined $pid or die $!; + + if (!$pid) { + $pipe->writer(); + if (!$lastpipe) { + print $pipe $val or die $!; + exit 0; + } else { + open STDIN, '<&', $lastpipe or die $!; + open STDOUT, '>&', $pipe or die $!; + close $lastpipe or die $!; + close $pipe or die $!; + exec $cmd; die $!; + } + } + $pipe->reader(); + if ($lastpipe) { close $lastpipe or die $!; } + $lastpipe= $pipe; + push @pids, $pid; + } + + $!=0; { local ($/)=undef; $val= <$lastpipe>; } + defined $val or die $!; + $lastpipe->error and die $!; close $lastpipe or die $!; + + foreach my $cmd ('(paste)', @cmds) { + my $pid= shift @pids; + waitpid($pid,0) == $pid or die "$pid $? $!"; + $?==0 or $?==13 or die "$cmd $?"; + } + return $val; } -#---------- main program ---------- +#========== main program ========== + +#---------- determine properties of the submission ---------- my $dict= param('dict'); my $entry_in= param('entry'); @@ -104,10 +178,6 @@ if (defined $ocean && defined $pirate) { } } -my $du=$ENV{'YPPSC_DICTUPDATES'}; -chdir $du or die "$du $!" - if defined $du; - my $kind; my @xa; @@ -120,7 +190,58 @@ if ($dict =~ m/^pixmap$/) { } $dict= $&; -my ($ctx,$def,$image,$key,$val)= &{"parseentryin__$kind"}($entry_in, @xa); +my ($ctx,$def,$image,$key,$val, $width,$icon,$whole,$entry)= + &{"parseentryin__$kind"}($entry_in, @xa); + +#---------- compute the email to send ---------- + +my $whoami= `whoami`; $? and die $?; +chomp $whoami; + +my $email= < $fn_t" or die "$fn_t $!"; @@ -140,8 +261,27 @@ my @tm= localtime; my $tm= strftime "%Y-%m-%d %H:%M:%S %Z", @tm; open L, ">> _dict.log" or die $!; -my $ll= sprintf "%s %-6s %-31s %s %s\n", $tm, $dict, $pirate, $fn_i, "submit"; -print L $ll or die $!; +my $ll= sprintf "%s %-6s %-31s %s", $tm, $dict, $pirate, $fn_i; + +#---------- commit everything ---------- + +print L "$ll submit\n" or die $!; +L->flush or die $!; + +if (eval { + + open S, "|sendmail -odb -oee -oi -t" or die $!; + print S $email or die $!; + $!=0; $?=0; close S or die $!; $? and die $?; + + rename $fn_t, $fn_i or die "$fn_t $fn_i $!"; + + 1; +}) { + print L "$ll stored\n" or die $!; +} else { + print L "$ll ERROR! $@\n" or die $!; +} close L or die $!; -rename $fn_t, $fn_i or die "$fn_t $fn_i $!"; +print header('text/plain'), "$fn_i\n" or die $!; -- 2.30.2