home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / doc / popularity-contest / examples / prepop.pl < prev    next >
Encoding:
Perl Script  |  2008-11-04  |  1.9 KB  |  80 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.        unless (-d "$dirname/$dir") {
  42.          mkdir("$dirname/$dir",0755) or do {$state='reject';next;};
  43.        };
  44.        $file="$dirname/$dir/$id"; 
  45.        open REPORT, ">",$file or do {$state='reject';next;};
  46.        print REPORT $_;
  47.        $state='accept'; next;
  48.     };
  49.     $state eq 'reject' and do
  50.     {
  51.       /^From/ or next;
  52.       $state='initial';next;
  53.     };
  54.     $state eq 'accept' and do
  55.     {
  56.       /^From/ and do 
  57.       {
  58.         close REPORT; 
  59.         unlink $file; 
  60.         print STDERR "Bad report $file\n";
  61.         $state='initial';
  62.         next;
  63.       };
  64.       print REPORT $_; #accept line.
  65.       /^END-POPULARITY-CONTEST-0/ and do 
  66.       {
  67.         close REPORT; 
  68.         utime $mtime, $mtime, $file;
  69.         $state='initial';
  70.         next;
  71.       };
  72.     };
  73. }
  74. if ($state eq 'accept')
  75. {
  76.         close REPORT;
  77.         unlink $file; #Reject
  78.         print STDERR "Bad report $file\n";
  79. }
  80.