#!/usr/athena/bin/perl

unshift(@INC, '/var/www/cgi-bin/lib');

require 'parseform.pl';

$regfile = '/tmp/regfile';
$logfile = '/var/www/logs/reg_activity.log';

if (! $ENV{'QUERY_STRING'})
{
    print "Location: /doc/activity-guidelines.html\n\n";
    exit;
}

&reg_activity();

sub reg_activity {
    local($query);

    $query = $ENV{'QUERY_STRING'};
    %form = &parseform($query);

    while (-e $regfile) { sleep 3; }

    print "Content-Type: text/html\n\n";
    print "<head>\n";
    print "<link rev=made href=\"tritan\@mit.edu\">\n";
    print "<link rev=owns href=\"webmaster\@mit.edu\">\n";
    print "<title>register output</title>\n";
    print "</head>\n";
    print "<body>\n";

    if (!open(REGFILE, ">$regfile")) {
      print "<p>Internal Error (1) processing request. Sorry.</p>\n";
      print "<p>Try again later or send mail to ",
            "<a href=\"mailto:webmaster\@mit.edu\">webmaster\@mit.edu</a>.",
            "</p>\n";
    } else {
      print "<h1>Activity Registered</h1>\n";

      print "Your activity is now registered with the SIPB ";
      print "webmasters. Below is the information that you provided.";
      print "<p>\n";

      print "Please verify the correctness of the information below. ";
      print "If there are any errors, please re-submit your request.";
      print "<p>\n";

      print "Once your activity has been placed on the SIPB activities ";
      print "page, your contact person should receive email confimation.";
      print "<p>\n";

      print "If you have any questions, please send email to ";
      print "<a href=\"mailto:webmaster\@mit.edu\">webmaster\@mit.edu</a>.";
      print "<p>\n";

      print "<hr>\n";

      print "<strong>Activity name =</strong> \"$form{'name'}\"<br>\n";
      print "<strong>Category      =</strong> \"$form{'category'}\"<br>\n";
      print "<strong>Contact       =</strong> \"$form{'contact'}\"<br>\n";
      print "<strong>Home page     =</strong> \"$form{'location'}\" ";
      print "($form{'type'})<br>\n";
      print "<strong>Icon          =</strong> \"$form{'icon'}\" ";
      print "($form{'icon_type'})<br>\n";
      print "<strong>Blurb:</strong><br>\n";
      print "<blockquote>\n";
      print $form{'blurb'};

      print "</blockquote>\n";
    }

    print "<hr>\n";

    print "<address>\n";
    print "<a href=\"/webmasters.html\">webmaster\@mit.edu</a>\n";
    print "</address>\n";
    print "</body>\n";

    print REGFILE "From: webmaster\@mit.edu\n";
    print REGFILE "To: webmaster\@mit.edu\n";
    $new_name = $form{'name'};
    chomp($new_name);
    if (length($new_name) > 80)
    {
	$new_name = "";
    }
    elsif ($new_name !~ /^[A-Za-z0-9!\@\$&\*\(\)_\+:'",\.\?\/ \t-]+$/)
    {
        $new_name = "";
    }
    print REGFILE "Subject: Activity Registration Request from $new_name\n\n";
    print REGFILE "*** Activity Registration Request ***\n\n";
    print REGFILE "Activity name = $form{'name'}\n";
    print REGFILE "Category      = $form{'category'}\n";
    print REGFILE "Contact       = $form{'contact'}\n";
    print REGFILE "Location      = $form{'location'} ($form{'type'})\n";
    print REGFILE "Icon          = $form{'location'} ($form{'icon_type'})\n";
    print REGFILE "Blurb:\n";
    print REGFILE "$form{'blurb'}\n\n";

    print REGFILE "Semi-Automatic-Activity-HTML (void where prohibited):\n";
    print REGFILE "<a href=\"$form{'location'}\"> </a> $form{'blurb'}\n";
    print REGFILE "<!-- Contact=\"$form{'contact'}\" -->\n";

    close(REGFILE);

    system("/usr/sbin/sendmail -t -fwebm-mtg\@charon.mit.edu < $regfile");
    if (-e $logfile) {
      system("/usr/bin/tail +5 $regfile >> $logfile");
    } else {
      system("/usr/bin/tail +5 $regfile > $logfile");
    }
    unlink($regfile);
}

1;
