#!/usr/bin/perl -w #------------------------------------------------ # Read the newsboy.ini file # sub ReadIniFile() { open (IF, "/home/scotfree/public_html/news/newwwsboy.ini") || die "cannot read ini file"; while () { if (/(^.*?)=(.*$)/) { local($key, $val) = /(^.*?)=(.*$)/; $configOptions{$key} = $val; } } close IF; } sub CommandLineProcessing() { $debug = 1; $inputFormat = "message"; @writeToFile = (); while (@ARGV) { $arg = shift @ARGV; $arg =~ /^-format:(.*)$/ && ($format = $1); $arg =~ /^-file:(.*)$/ && push(@writeToFile, $1); $arg =~ /^-debug$/ && ($debug = 1); if ($arg eq "-help" || $arg eq "-h" || $arg eq "-?") { exit(0); } } } # ProcessFile# infile, outfile, subst key, value pairs sub ProcessFile() { my ($infile, $outfile, $substKey, %values); $infile = shift @_; $outfile = shift @_; $substKey = shift @_; $debug && print "ProcessFile($infile,$outfile,$substKey)\n"; %values = @_; $debug && print "ProcessFile: url $values{'url'}\n"; my ($in, $out); open(OF, ">$outfile") || die "unable to open $outfile for writing\n"; open(IF, $infile) || die "unable to open $infile for reading\n"; $in = ""; while () { $in = $in.$_; } close IF; $out = ""; my($inMacroBlock) = 0; while ($in ne "") { if ($in =~ /(^.*?)()(.*$)/s) { my $cmd; if (0 == $inMacroBlock) { $out = $out.$1; } $cmd = $2; $in = $3; if ($cmd =~ //s) { my($var); my($string) = $1; $inMacroBlock++; $out = $out.$cmd; foreach $var (keys %values) { $string =~ s/\$$var([^a-zA-Z0-9]|$)/$values{$var}$1/sg; } $out = $out.$string; } elsif ($cmd =~ //s) { $out = $out.$cmd; if ($inMacroBlock > 0) { $inMacroBlock--; } } elsif (!$inMacroBlock) { if ($cmd =~ //s) { my ($var,$match,$where); $where = $2; $match = ",$1,"; $debug && print "Trying to match $substKey to $match\n"; if ($match =~ /,$substKey,/) { if ($where eq "before") { $out = $out.$cmd; } $out = $out.$values{$substKey}; if ($where eq "after") { $out = $out.$cmd; } } else { $out = $out.$cmd; } } elsif ($cmd =~ //s) { my ($var,$string,$where,$match); $string = $3; $where = $2; $match = ",$1,"; $debug && print "Trying to link $substKey with $match\n"; if ($match =~ /,$substKey,/) { if ($where eq "before") { $out = $out.$cmd; } foreach $var (keys %values) { $string =~ s/\$$var([^a-zA-Z]|$)/$values{$var}$1/sg; } $out = $out.$string; if ($where eq "after") { $out = $out.$cmd; } } else { $out = $out.$cmd; } } elsif ($cmd =~ //s) { open(IF1, $configOptions{'includepath'}); while () { $out = $out.$_; } } else { $out = $out.$cmd; } } } else { $out = $out.$in; $in = ""; } } print OF $out; close OF; # foreach $var (keys %values) # { # print "XXXX--> $var -> $values{$var}\n\n"; # } } &ReadIniFile(); &CommandLineProcessing(); &ProcessMessage(); sub ProcessMessage { my($head,$body, %substitutions); ($head,$body) = &ReadMessage; if ("message" eq $inputFormat) { $body = &HTMLifyText($body); } elsif ("text" eq $inputFormat) { $head = ""; $body = ""; while (<>) { $body = $body.$_; } } elsif ("html" eq $inputFormat) { $head = ""; $body = ""; while (<>) { $body = $body.$_; } $body = &HTMLifyText($body); } else { print "Unrecognized input format\n"; exit(1); } %substitutions = &InitSubstitutionHash($head); foreach $filename (@writeToFile) { ProcessDataToFile($filename, $filename, $filename, $body, %substitutions); } } # ProcessDataToFile # fileclassname # sub ProcessDataToFile() { my $filename = shift @_; my $blockName = shift @_; $debug && print "ProcessDataToFile($filename, $blockName)\n"; my %argList = @_; my ($path, $infile, $outfile, $createdNewFile); $path = &DoPathMacroSubstitution($configOptions{"$filename.path"}, %argList); &MakePathForFile($configOptions{'documentroot'},$path); if (-f $configOptions{'documentroot'}.$path) { $infile = "$configOptions{'documentroot'}/$path"; $outfile = "$configOptions{'documentroot'}/$path.bak"; $locked = 0; while (!$locked) { if (-f $outfile) { for ($i = 360; $i > 0; --$i) { sleep(1); last unless -f "$outfile"; } } die "File locked : $infile\n" if -f $outfile; $debug && print "Creating $outfile$$\n"; open(PTMP, ">$outfile$$"); close PTMP; $debug && print "Linking $outfile to $outfile$$\n"; $locked = link("$outfile$$",$outfile); $debug && print "Unliking $outfile$$\n"; unlink "$outfile$$"; } $createdNewFile = 0; } else { $infile = $configOptions{'templateroot'}."/" .$configOptions{"$filename.template"}; $outfile = "$configOptions{'documentroot'}/$path.bak"; $createdNewFile = 1; } &ProcessFile($infile, $outfile, $blockName, %argList); if ($createdNewFile) { my($link, @links); if ($configOptions{"$filename.links"} =~ /[^ \n\t]/) { @links = split / /, $configOptions{"$filename.links"}; $argList{'url'} = $path; foreach $link (@links) { ProcessDataToFile($link, $filename, %argList); } } } else { $debug && print "Unlinking $infile\n"; unlink ($infile); $debug && print "Renaming $outfile to $infile\n"; rename ($outfile, $infile); } } sub InitSubstitutionHash() { local @monthAbbrs = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"); local @dowAbbrs = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"); local %months = ("Jan", "January", "Feb", "February", "Mar", "March", "Apr", "April", "May", "May", "Jun", "June", "Jul", "July", "Aug", "August", "Sep", "September", "Oct", "October", "Nov", "November", "Dec", "December"); local %dows = ("Mon", "Monday", "Tue", "Tuesday", "Wed", "Wednesday", "Thu", "Thursday", "Fri", "Friday", "Sat", "Saturday", "Sun", "Sunday"); my($header); local($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); $year += 1900; $mon = $monthAbbrs[$mon]; $day = $dowAbbrs[$wday]; if ($sec || $hour || $min || $isdst || $yday) { } $header = shift @_; { $header = "\n$header\n"; my($filename); $filename = ""; ($header =~ /\nSubject: (.*?)\n/s) && ($filename = $1); $substitutions{'title'} = $filename; $filename =~ s/[^A-Za-z0-9]//g; $filename =~ s/:/./g; $substitutions{'filename'} = $filename; if ($header =~ /\nDate: +(Mon|Tue|Wed|Thu|Fri), +([0-9]+) +(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) +([0-9]+) +([0-9:]+) +[0-9\-+]+/) { ($day,$mday,$mon,$year,$time,$tz) =($header =~ /\nDate: +(Mon|Tue|Wed|Thu|Fri), +([0-9]+) +(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) +([0-9]+) +([0-9:]+) +[0-9\-+]+/); } } $substitutions{'mon'} = $mon; $substitutions{'dow'} = $day; $substitutions{'day'} = $mday; $substitutions{'year'} = $year; $substitutions{'month'} = $months{$mon}; $substitutions{'dow'} = $dows{$day}; $substitutions{'dayord'} = &OrdinalExtension($mday); return %substitutions; } sub OrdinalExtension { ($_[0] % 10) == 1 && "st" || ($_[0] % 10) == 2 && "nd" || ($_[0] % 10) == 3 && "rd" || "th" ; } sub MakePathForFile() { local(@toMake); local($root, $arg) = @_; while ($arg ne "") { $arg =~ /(.*)\//; push @toMake, $1; $arg = $1; } while ($#toMake > -1) { $arg = pop @toMake; mkdir "$root/$arg", oct "0777"; } } sub DoPathMacroSubstitution { my ($path, %substitutions); $path = shift @_; %substitutions = @_; foreach $subst (keys %substitutions) { $path =~ s/\$$subst([^a-zA-Z0-9]|$)/$substitutions{$subst}$1/g; } return $path; } sub ReadMessage { $body = ""; $header = ""; while (<>) { (/^$/) && last; $header = $header.$_; } while (<>) { s/^>From /From /; $body = $body.$_; } return ($header,$body); } sub FindReference { my($reference) = shift; my $out; $out = "$reference"; return $out; } sub HTMLifyChunk { my $chunk; $chunk = shift; $chunk =~ s/[\n\s]/ /sg; $chunk =~ s/ / /sg; $chunk =~ s/\&([^a-zA-Z#])/\&\;$1/sg; $chunk =~ s/>/\>\;/sg; $chunk =~ s/$2\<\/A>$4/sgx; while ($chunk =~ /(^|.* )_([A-Za-z][^_]*[A-Za-z'!\?0-9])_([,.\!\?\:\; ].*|$)/sg) { $chunk = $1.&FindReference($2).$3; } $chunk =~ s/(^|[^""])(http:[^" ]*[^",.\;\:\!\?) ])/$1$2<\/A>/sg; return $chunk; } sub HTMLifyPreChunk { my $chunk; $chunk = shift; $chunk =~ s/\&/\&\;/sg; $chunk =~ s/>/\>\;/sg; $chunk =~ s/$2<\/A>/sg; return $chunk; } sub HTMLifyText { my ($output, $text, $para); my (%validHTMLTags) = ( 'P', 'n', 'IMG', 'n', 'BR', 'n', 'HR', 'n', 'A', 'y', 'CENTER', 'y', 'FONT', 'y', 'SUB', 'y', 'SUP', 'y', 'TT', 'y', 'I', 'y', 'B', 'y', 'H1', 'y', 'H2', 'y', 'H3', 'y', 'H4', 'y', 'H5', 'y', 'H6', 'y', 'CITE', 'y', 'EM', 'y', 'STRONG', 'y', 'CODE', 'y', 'SAMP', 'y', 'KBD', 'y', 'PRE', 'y', 'BLOCKQUOTE','y', 'ADDRESS', 'y', 'OL', 'y', 'DL', 'y', 'UL', 'y', 'DT', 'y', 'LI', 'y', 'DIR', 'y', 'MENU', 'y', 'TABLE', 'y', 'TR', 'y', 'TD', 'y', 'TH', 'y', ); $text = shift; $output = ''; while ($text ne '') { my(@tagstack, $tag); if ($text =~ /^\n*(.*?)(\n\n)(.*)$/s) { $para = $1; $text = $3; } else { $para = $text; $text = ''; } if ($para =~ /^(\#|\/\*)/) { $output .= "

\n".&HTMLifyPreChunk($para)."\n

\n"; } elsif ($para =~ /^>/) { $para =~ s/(^|\n)> */\n/sg; $output .= '

'
		.&HTMLifyPreChunk($para)."\n

\n"; } else { $output = $output.'

'; if ($para =~ /^ /) { $output .= '

'; push @tagstack, 'BLOCKQUOTE'; } while ($para ne '') { if ($para =~ /^(.*?)(<\/*[A-Za-z].*?>)(.*)$/s) { $chunk = $1; $tag = $2; $para = $3; $tag =~ s/[\s]/ /sg; $tag =~ s/ / /sg; $output .= &HTMLifyChunk($chunk); if ($tag =~ /^<\/([A-Za-z]+).*>/) { my $tagname = uc($1); if ($validHTMLTags{$tagname}) { $closetag = pop @tagstack; while ($closetag && ($tagname ne $closetag)) { $output .= ""; $closetag = pop @tagstack; } if ($closetag) { $output .= ""; } } else { $output .= "</$tagname>"; } } else { if ($tag =~ /([A-Za-z]+)/) { my $tagname = uc($1); if ($validHTMLTags{$tagname}) { if ($validHTMLTags{$tagname} eq 'y') { push @tagstack, $1; } $output .= $tag; } else { $tag =~ s/\&/\&/sg; $tag =~ s//\>/sg; $output .= $tag; } } else { $tag =~ s/\&/\&/sg; $tag =~ s//\>/sg; $output .= "<".$tag.">"; } } } else { $output .= &HTMLifyChunk($para); $para = ''; } } $closetag = pop @tagstack; while ($closetag) { $output .= ""; $closetag = pop @tagstack; } $output .= "

\n\n"; } } return $output; } #sub HTMLifyText() #{ # my($text,$html,$output,$inputtext); # $inputtext = $_[0]; # $output = "

"; # while ($inputtext) # { # $text = ""; # $html = ""; # # ($text,$html,$inputtext) = ($inputtext =~ /(^[^<]*)(<[^>]*>|$)(.*$)/s); # # $text =~ s/_([A-Za-z0-9][A-Za-z0-9\'. \t\n]*)_[ \t\n]*\((http:[^)]*)\)/$1<\/A>/sg; # $text =~ s/(^|[^""])(http:[^\s]*)/$1$2<\/A>/sg; # $text =~ s/_([A-Za-z0-9][A-Za-z0-9\'.\s]*)_/$1<\/CITE>/sg; # $text =~ s/(ISBN:[\s]*)([0-9\-]+)([^0-9\-])/$1$2<\/A>$3/sg; # # $text =~ s/((\n|^)[^\s>][^\n]*)\n\n/$1<\/P>\n\n/sg; # $text =~ s/\n\n([^\n\t >])/\n\n

$1/sg; # # $text =~ s/((\n|^)[ \t][^\n]*)\n\n+/$1<\/BLOCKQUOTE>\n\n/sg; # $text =~ s/\n\n[\t ]+/\n\n

/sg; # # $text =~ s/((\n|^)>[ \t][^\n]*)\n\n+/$1\n<\/PRE>\n\n/sg; # $text =~ s/\n\n(>[ \t])/\n\n
\n$1/sg;
#		$text =~ s/([\s])>([\s])/$1>\;$2/sg;
#		$text =~ s/<([^A-Za-z\/!])/\n<\;$1/sg;
#
#		$text =~ s/\n\n$/\n\n

/sg; # $output = $output.$text.$html; # } # # $output = $output."

\n"; # $output =~ s/

[\s]*<\/P>//sg; # return $output; #} #