Chapter 9

Bulletin Boards


CONTENTS

A part from e-mail-based mailing lists and IRC (Internet Relay Chat), another method of "group communication" on the Internet is usenet, a global collection of "newsgroups" where discussion is carried on over subjects that vary from the technical to the bizarre. No matter what interests you, you'll probably find a newsgroup that covers it, perhaps even several as in the case of many popular programming topics, like scripting.

On the other hand, if you're running a Web site with rather unique subject matter, it might be interesting to give your visitors a forum for discussion that's local to your site. That way, people can stop by, poke around, and join in the discussion-all from the comfort of your Web pages. Borrowing a term from another type of computer intercommunication, a system such as this is called a bulletin board.

Understanding Bulletin Boards

The principle behind a bulletin board is relatively simple:

Users can peruse a list of posted messages, reading those that interest them.
Users can post their own messages into the system.

Both sides of the bulletin board revolve around the central message database, where every posting is stored. The easiest way to deal with individual messages is to handle them as separate files, all stored in a common directory.

Because the directory will hold multiple files, being able to provide a unique name for each file is also important. Taking another cue from electronic BBS systems, where messages are identified by number, each message file will be a number (say 8 digits long). Successive messages produce successive numbers. This means another file that keeps track of the "last number used" will also be needed.

Posting a Message

Adding a new message to the database can be handled through an HTML form, similar to the one shown in listing 9.1.


Listing 9.1  The Message Posting Form
<FORM METHOD=POST ACTION="/cgi-bin/post.cgi">
   Name: <INPUT TYPE=TEXT NAME="realname" SIZE=40><BR>
   Email: <INPUT TYPE=TEXT NAME="email" SIZE=40><BR>
   Subject: <INPUT TYPE=TEXT NAME="subject" SIZE=80>

   <P>

   Type your message:<BR>
   <TEXTAREA TYPE=TEXT NAME="message" WRAP COLS=60 ROWS=15>
   </TEXTAREA>

   <INPUT TYPE=SUBMIT NAME="submit" VALUE=" Post It!">
   <INPUT TYPE=RESET NAME="reset"   VALUE="  Clear  ">
</FORM>

Nothing really fancy is needed here-the work happens after the user clicks the Post It! button. At that point, the post.cgi script takes over and does the following:

  1. Reads in the high message number from the counter file.
  2. Increments the number and writes it back out for the next message.
  3. Copies the contents of the message field into a new file.
  4. Rewrites the subject browse document, adding the new messages subject those displayed.

First things first, though-you need to retrieve the form data from the environment variables you need (as shown in listing 9.2).


Listing 9.2  Reading Form Fields
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);

foreach $pair (@pairs)
{
   ($name, $value) = split(/=/, $pair, 2);
   $value =~ tr/+/ /;
   $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
   $contents{$name} = $value;
}

Once the message is available for processing, grab a new message number from the counter file (listing 9.3).


Listing 9.3  Incrementing a Counter
# Get the current high message number
open(FILE,"messagecounter");
$highMsg = <FILE>;
close(FILE);
$highMsg++;

# Write out the new number
open(FILE,">messagecounter");
print FILE "$highMsg";
close(FILE);

At this point, you have enough information to write the new message into a file in the database. Listing 9.4 demonstrates writing the message out as an HTML file for easy retrieval from the browser and assumes that all messages are stored in a directory messagebase/ off of where the post.cgi script is located.


Listing 9.4  Writing a New Message
open(FILE,">messages/$highNum.html");
print FILE "<HTML>\n";
print FILE "<HEAD>\n";
print FILE "   <TITLE>$contents{'subject'}</TITLE>\n";
print FILE "</HEAD>\n";
print FILE "<BODY>\n";
print FILE "<H1>$contents{'subject'}</H1>\n";
print FILE "<HR>\n";
print FILE "Posted by ";
print FILE "<A HREF=\"mailto:$contents{'email'}\">
$contents{'realname'}</A>\n";
print FILE "<P>\n";
print FILE "$message\n";
print FILE "</BODY></HTML>\n";
close(FILE);

The HTML Front End

With the message now part of the database, it's necessary to add it to the HTML document that lists all available messages. Listing 9.5 shows the document that serves as a template for displaying the current messages.


Listing 9.5  Listing of Current Messages
<HTML>
<HEAD>
   <TITLE>Current Messages</TITLE>
</HEAD>

<BODY>
<CENTER><H1>Who's Sayin' What...</H1></CENTER>

<HR>

<!--START-->

</BODY>
</HTML>

At first glance this template doesn't say much. When combined with a CGI script, however, it becomes the subject list. The trick here is to get the script to rewrite this file each time a new message is posted. This is more efficient than using server-side includes to generate the message list dynamically ("on-the-fly") each time the user pulls up the subject page.

In chapter 8, "Advertising with Billboards," you are introduced to the trick of embedding a specific HTML comment within a document that works as a placeholder for the CGI script to find and handle. The document in listing 9.5 uses the same trick. The placeholder is <!--START--> but this time the script is going to handle things a little differently. Because it makes sense to make the most recently posted messages list first, the placeholder is used to keep track of the top of the list and not be deleted from the file. The code block in listing 9.6 demonstrates this.


Listing 9.6  Rewriting the Subject List
# load the HTML document into memory
open(FILE,"bbs.html");
@html = <FILE>;
close(FILE);

# prepare to write out the new document
open(FILE,">bbs.html");

foreach $line (@html) {
   print FILE "$line";

   if ($line =~ /<!--START-->/) {
      print FILE "<A HREF=\"messages/$highNum.html\">
             $content{'subject'}</A> - $content{'realname'}<BR>\n";
   }
}

close(FILE);

As demonstrated, the placeholder line is kept in the document with the new message link being added immediately after it. This way, the latest messages are always listed first.

From Here…

This is a simple look at the process behind adding a bulletin board to your Web site. If you're interested in exploring related topics, check out: