chiark / gitweb /
dictionary-update-receiver emails again
authorIan Jackson <ian@liberator.relativity.greenend.org.uk>
Sat, 20 Jun 2009 19:57:32 +0000 (20:57 +0100)
committerIan Jackson <ian@liberator.relativity.greenend.org.uk>
Sat, 20 Jun 2009 19:57:32 +0000 (20:57 +0100)
pctb/dictionary-update-receiver

index 3e04f6c3015d5ebf65a5672d6d2e0acd24021e77..078b9f8c312ffd300d6bbf205b743a672b7937f8 100755 (executable)
@@ -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= <<END
+To: $whoami
+Subject: pctb $dict $ctx $def [ypp-sc-tools]
+
+Pirate:     $pirate
+Dictionary: $dict
+Context:    $ctx
+Definition: $def
+
+END
+    ;
+
+if (length $icon) {
+    $email .= "$icon\n\n";
+}
+
+$whole =~ s/(.*)\n/ sprintf "%-${width}s\n", $1 /mge;
+$whole =~ s/^/|/mg;
+$whole =~ s/\n/|\n/mg;
+$whole =~ s/^(.*)/ ",".('_' x $width).".\n".$1 /e;
+$whole =~ s/(.*)$/ $1."\n\`".('~' x $width)."'\n" /e;
+
+my $lw= 79;
+
+while ($whole =~ m/../) {
+    my $lhs= $whole;
+    $lhs =~ s/^(.{0,$lw}).*$/$1/mg;
+    $whole =~ s/^.{1,$lw}//mg;
+#print STDERR "[[[[[$lhs########$whole]]]]]\n";
+    $email .= $lhs;
+}
+
+END
+    ;
+
+my $cutline= "-8<-\n";
+$email .= $cutline.$entry.$cutline;
+
+#---------- prepare the database entry ----------
+
+my $du=$ENV{'YPPSC_DICTUPDATES'};
+chdir $du or die "$du $!"
+    if defined $du;
+
 
 my $fn_t= "_update.$$-xxxxxxxxxxxxxxxx.tmp";
 open F, "> $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 $!;