#!/usr/local/bin/perl ############################################################################### # Sam Sultan # # Bulletin Board application ############################################################################### require 'date.pl'; # import date.pl $htmlfile = "../../web/demo/perl/bbs.html"; $bbfile = "../../data/bbs.file"; if ($ARGV[0] eq '') { &view(); } if ($ARGV[0] eq 'D') { &display(); } if ($ARGV[0] eq 'U') { &update(); } ############################################################################## # view: View the entire bulettin board entries ############################################################################## sub view { print "Content-type: text/html\n"; print "\n"; print " \n"; print " \n"; print " \n"; print "

Bulletin Board

\n"; print " \n"; print "
"; print "ADD ENTRY \n"; open(BBS, "<$bbfile") || print "Cannot open $bbfile for input!"; @entries = ; #read all entries into array close(BBS); @entries = sort by_node @entries; #sort by nodes (by_node func) for ($i=0; $i<@entries; $i++) { ($node,$name,$email,$comment,$sec) = split(/##/, $entries[$i]); ($day,$date,$time) = &dateTime($sec); #extract proper date/time $cmt = substr($comment, 0, 50); #shorten the comment if (index($node, '.') == -1) { #if node does not have . print "

\n"; #print
} $indent = $node; $indent =~ s/[0-9]//g; #strip out all numbers $indent =~ s/\./       /g; #chg every . to spaces print "
"; print " R \n"; print " $name \n"; print " $indent - "; print " $cmt \n"; print " ... --- $day $date $time \n"; } print "

\n"; print "<\TABLE> \n"; } ############################################################################## # display: Display the new entry/reply html form ############################################################################## sub display { $reqNode = $ARGV[1]; #obtain requested node num print "Content-type: text/html\n"; print "\n"; print " \n"; print " \n"; print " \n"; print "

Bulletin Board

\n"; print " \n"; open(BBS, "<$bbfile") || print "Cannot open $bbfile for input!"; while() { ($node,$name,$email,$comment,$sec) = split(/##/); if ($node eq $reqNode) { $comment =~ s/__/
/g; #change all __ to
($day,$date,$time) = &dateTime($sec); #extract proper date/time print "
$name \n"; print "--- $day $date $time \n"; print "
$comment \n"; } } print "

\n"; close(BBS); open(HTML, "<$htmlfile") || print "Cannot open $htmlfile input!"; while ($line = ) { #print entry/reply form print $line; } close(HTML); } ############################################################################## # update: Insert a new entry in the bulletin board ############################################################################## sub update { $reqNode = $ARGV[1]; #obtain requested node num use CGI "param"; $name = param('name'); $email = param('email'); $comment = param('comment'); open(BBS, "<$bbfile") || print "Cannot open $bbfile for input!"; while() { ($node) = split(/##/); $firstDot = index($node, '.'); #does the node have a . if ($firstDot > 0) { $parent = substr($node, 0, $firstDot); } else { $parent = $node; } if ($parent > $maxParent) { #determine the highest $maxParent = $parent; #parent number } if ($node =~ /^$reqNode/) { #if reqNode = beginning of node $child = $child +1; #count the children } } close(BBS); if ($reqNode eq '') { #if adding a brand new entry $newNode = $maxParent +1; #add 1 to highest parent } else { #if reply to an existing entry $newNode = $reqNode . "." . $child; #append reqNode.child } $comment =~ s/\r\n/__/g; #change newlines to __ $newEntry = join('##',$newNode,$name,$email,$comment,time); open(BBS, ">>$bbfile") || print "Cannot open $bbfile for output!"; print BBS $newEntry, "\n"; #write new entry to file close(BBS); &view(); #re-list the bulletin board } ############################################################################## # Sort Descending parent, Ascending child ############################################################################## sub by_node { my ($node1) = split(/##/, $a); my ($node2) = split(/##/, $b); $firstDot = index($node1, '.'); $parent1 = ($firstDot > 0) ? substr($node1, 0, $firstDot) : $node1; $child1 = ($firstDot > 0) ? substr($node1, $firstDot+1) : 0; $firstDot = index($node2, '.'); $parent2 = ($firstDot > 0) ? substr($node2, 0, $firstDot) : $node2; $child2 = ($firstDot > 0) ? substr($node2, $firstDot+1) : 0; if ($parent1 > $parent2) {return -1;} # parent num descending if ($parent1 < $parent2) {return 1;} if ($parent1 == $parent2) { if ($child1 gt $child2) {return 1;} # child alpha ascending if ($child1 lt $child2) {return -1;} if ($child1 eq $child2) {return 0;} } } ##############################################################################