cvs commit: www manage_news.pl

lizardo at linuxfromscratch.org lizardo at linuxfromscratch.org
Thu Nov 13 17:44:53 PST 2003


lizardo     03/11/13 18:44:53

  Modified:    .        manage_news.pl
  Log:
  manage_news.pl: Implemented basic syntax check for news databases.
  
  Revision  Changes    Path
  1.19      +91 -7     www/manage_news.pl
  
  Index: manage_news.pl
  ===================================================================
  RCS file: /home/cvsroot/www/manage_news.pl,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- manage_news.pl	13 Nov 2003 14:07:05 -0000	1.18
  +++ manage_news.pl	14 Nov 2003 01:44:53 -0000	1.19
  @@ -17,13 +17,12 @@
   # along with this program; if not, write to the Free Software
   # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
   
  -# $Id: manage_news.pl,v 1.18 2003/11/13 14:07:05 jeroen Exp $
  +# $Id: manage_news.pl,v 1.19 2003/11/14 01:44:53 lizardo Exp $
   
   use warnings;
   use strict;
   
   use File::Path qw(mkpath);
  -use File::Spec qw(splitpath);
   use POSIX qw(strftime);
   use Getopt::Long;
   use MIME::Parser;
  @@ -65,11 +64,13 @@
   
   # Read the input file if one is given; otherwise, read from STDIN
   if ($infile) {
  -    $input_name = $infile;
  +    $input_name = $ENV{"PWD"} . "/" . $infile;
  +    check_syntax($infile);
       eval { $mime_entity = $parser->parse_open($infile) } or pod2usage("$0\: $@");
   }
   else {
       $input_name = "<STDIN>";
  +    check_syntax(\*STDIN);
       $mime_entity = $parser->parse(\*STDIN);
   }
   
  @@ -238,10 +239,8 @@
       # Extract the field from the header
       my $hdr_content = $header->get($name);
       # Die if the field is not found
  -    if (!defined($hdr_content)) {
  -        die "$0\: $input_name\: Could not find header field " . $name . "\n" .
  -        "Header contents:\n" . $header->as_string . "\n";
  -    }
  +    die "$0\: $input_name\: Could not find header field " . $name . "\n" .
  +    "Header contents:\n" . $header->as_string . "\n" unless defined($hdr_content);
       $hdr_content =~ s/^\s*//;
       $hdr_content =~ s/\s*$//;
       $hdr_content =~ s/\n$//;
  @@ -283,6 +282,91 @@
       "\t\t<h4>" . Get_header($mime_part->head, "author") . " - " .
       Get_header($mime_part->head, "date") . "</h4>\n" .
       $mime_part->bodyhandle->as_string . "\n";
  +}
  +
  +{
  +# Number of errors reported
  +my $errors = 0;
  +
  +# Validate MIME database for syntax
  +sub check_syntax {
  +    my ($filename) = @_;
  +
  +    my %tests = ();
  +    my $cur_line = 0;
  +    my $boundary = "";
  +    # Boundary line number of current item
  +    my $cur_item = 0;
  +
  +    open(FILE, $filename);
  +    while (<FILE>) {
  +        $cur_line++;
  +        if (/^(\s*)Content-Type:\s+multipart\/mixed;/i) {
  +            Error("Invalid space character at start of line $cur_line.") if $1;
  +            $tests{has_content_type} = 1;
  +        }
  +        if (/^\s*boundary="([^"]+)"/i) {
  +            $tests{has_boundary} = 1;
  +            $boundary = $1;
  +        }
  +        if (/^(\s*)MIME-Version:\s+1\.0\s*$/i) {
  +            Error("Invalid space character at start of line $cur_line.") if $1;
  +            $tests{has_mime_version} = 1;
  +        }
  +        if (/^(\s*)Section:\s+[\w-]+\s*$/i) {
  +            Error("Invalid space character at start of line $cur_line.") if $1;
  +            $tests{has_section} = 1;
  +        }
  +        if (/^(\s*)--(\w+)/) {
  +            Error("Invalid space character at start of line $cur_line.") if $1;
  +            if ($2 ne $boundary and $boundary) {
  +                Error("Wrong boundary at line $cur_line; it should be \"$boundary\".");
  +            }
  +            elsif ($boundary) {
  +                if ($cur_item) {
  +                    Error("No \"Title\" field found for item at line $cur_item.") unless $tests{has_title};
  +                    Error("No \"Author\" field found for item at line $cur_item.") unless $tests{has_author};
  +                    Error("No \"Date\" field found for item at line $cur_item.") unless $tests{has_date};
  +                }
  +                $cur_item = $cur_line;
  +                delete $tests{has_title};
  +                delete $tests{has_author};
  +                delete $tests{has_date};
  +            }
  +        }
  +        if (/^(\s*)Title:/i) {
  +            Error("Invalid space character at start of line $cur_line.") if $1;
  +            $tests{has_title} = 1;
  +        }
  +        if (/^(\s*)Author:/i) {
  +            Error("Invalid space character at start of line $cur_line.") if $1;
  +            $tests{has_author} = 1;
  +        }
  +        if (/^(\s*)Date:/i) {
  +            Error("Invalid space character at start of line $cur_line.") if $1;
  +            $tests{has_date} = 1;
  +        }
  +        if (/^(\s*)--$boundary--$/) {
  +            Error("Invalid space character at start of line $cur_line.") if $1;
  +            $tests{has_end_boundary} = 1;
  +        }
  +        
  +    }
  +    close FILE;
  +    Error("\"Content-Type\" field invalid or not found.") unless $tests{"has_content_type"};
  +    Error("MIME boundary not declared (eg. boundary=\"NEWS_ITEM_BOUNDARY\").") unless $tests{"has_boundary"};
  +    Error("\"MIME-Version\" field invalid or not found.") unless $tests{"has_mime_version"};
  +    Error("\"Section\" field not found.") unless $tests{"has_section"};
  +    Error("MIME end boundary (eg. \"--NEWS_ITEM_BOUNDARY--\") invalid or not found.") unless $tests{"has_end_boundary"};
  +    die "$errors syntax error(s) found on $input_name.\n" if $errors;
  +}
  +
  +sub Error {
  +    my ($message) = @_;
  +    print STDERR "$0\: $input_name\: $message\n";
  +    $errors++;
  +}
  +
   }
   
   __END__
  
  
  



More information about the website mailing list