home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / doc / popularity-contest / examples / prepop.pl < prev    next >
Encoding:
Perl Script  |  2004-02-01  |  1.8 KB  |  77 lines

  1. #!/usr/bin/perl -wT
  2. # Accept popularity-contest entries on stdin and drop them into a
  3. # subdirectory with a name based on their MD5 ID.
  4. #
  5. # Only the most recent entry with a given MD5 ID is kept.
  6. #
  7.  
  8. $dirname = 'popcon-entries';
  9. $now = time;
  10. $state='initial'; # one of ('initial','accept','reject')
  11.  
  12. my($file,$mtime);
  13. while(<>)
  14. {
  15.     $state eq 'initial' and do
  16.     {
  17.        /^POPULARITY-CONTEST-0/ or next;
  18.        my @line=split(/ +/);
  19.        my %field;
  20.        for (@line)
  21.        {
  22.         my ($key, $value) = split(':', $_, 2);
  23.             $field{$key}=$value;
  24.        };
  25.        $id=$field{'ID'};
  26.        if (!defined($id) || $id !~ /^([a-f0-9]{32})$/) 
  27.        {
  28.          print STDERR "Bad hostid: $id\n";
  29.          $state='reject'; next;
  30.        }
  31.        $id=$1; #untaint $id
  32.        $mtime=$field{'TIME'};
  33.        if (!defined($mtime) || $mtime!~/^([0-9]+)$/)
  34.        {
  35.          print STDERR "Bad mtime $mtime\n";
  36.          $state='reject'; next;
  37.        }
  38.        $mtime=int $1; #untaint $mtime;
  39.        $mtime=$now if ($mtime > $now);
  40.        my $dir=substr($id,0,2);
  41.        $file="$dirname/$dir/$id"; 
  42.        open REPORT, ">",$file or do {$state='reject';next;};
  43.        print REPORT $_;
  44.        $state='accept'; next;
  45.     };
  46.     $state eq 'reject' and do
  47.     {
  48.       /^From/ or next;
  49.       $state='initial';next;
  50.     };
  51.     $state eq 'accept' and do
  52.     {
  53.       /^From/ and do 
  54.       {
  55.         close REPORT; 
  56.         unlink $file; 
  57.         print STDERR "Bad report $file";
  58.         $state='initial';
  59.         next;
  60.       };
  61.       print REPORT $_; #accept line.
  62.       /^END-POPULARITY-CONTEST-0/ and do 
  63.       {
  64.         close REPORT; 
  65.         utime $mtime, $mtime, $file;
  66.         $state='initial';
  67.         next;
  68.       };
  69.     };
  70. }
  71. if ($state eq 'accept')
  72. {
  73.         close REPORT;
  74.         unlink $file; #Reject
  75.         print STDERR "Bad report $file";
  76. }
  77.