#!/usr/local/bin/perl5 -w
#
#	majordomo
#
#	Build a form with some fields preset.  The generated form will
#	allow most of the options which majordomo allows.
#
#	This puts up a fairly simple screen to be filled out.
#	This also takes a query string which can be used to
#	fill in some defaults, as well as limit the options available.
#
#	Parameters:
#		The first parameter is the name of the mailing list
#		and host.  Something like:
#			oracle-bugs:peak.org
#
#		The next set of parameters are those to avoid displaying
#		fields.  They are:
#			-s -u -h -i -I -w -W -l -g
#			-s	No subscribe
#			-u	No unsubscribe
#			-h	No help
#			-i	No info
#			-I	No index
#			-w	No which
#			-W	No who
#			-l	No lists
#			-g	No get
#
#		The parameter "ro" says that the fields for mailing
#		list name and machine name cannot be changed.
#
#		The next parameter is for color:
#			bgcolor=COLOR
#		If specified, this will be used as the background color.
#		Note that case is significant.  The "bgcolor" must be all
#		lower case leters.
#
#		The next parameter is for the next link off of the
#		majordomo2 page:
#			link=http://www....
#
#	Note that if I were to rewrite this program it would look
#	quite different.  CGI.pm may have been one of those choices.
#
#	Written by Dave Regan
#	24 June 1996
#	This program is in the Public Domain.
#	Do what you want with it.
#

###
###	Configuration
###
$DefaultHost	= "peak.org";		# Default mail server

###
###	Main program
###
    HTMLhead("Marjordomo meets the web");
    $Vars{'bgcolor'} = "#FFFFFF";
    $Vars{'link'} = "";
    $| = 1;
    ParseQueryString();
#   printenv();
    print "<body bgcolor=\"$Vars{'bgcolor'}\">\n";
    $Vars[0] = "" if (!defined($Vars[0]));
    ($mlist, $host) = split(/[:\@]/, $Vars[0]);
    DisplayForm($mlist, $host);
    HTMLterm();
    exit 0;


###
###	DisplayForm
###
###	Build a form for the user to fill in.
###
sub DisplayForm
    {
    local($mlist, $host) = @_;

    print "<p>This form will let you communicate with any majordomo mailing list server.\n";
    $mlist = "" if (!defined($mlist));
    $host = "" if (!defined($host));
    if ($mlist ne "")
	{
	$host = $DefaultHost if ($host eq "");
	if (!defined($Vars{'ro'}))
	    {
	    print "The current mailing list ($mlist on host $host)\n";
	    print "is selected as a default, but you\n";
	    print "can alter the mailing list name or host if you want.\n";
	    }
	}
#   print "You must enter your email address and your full name.\n";
    print "You must enter your email address.\n";
    print "<p>\n";
    print "You can select any of the majordomo commands, and you can choose to select\n";
    print "several of them.\n";
    print "<p>\n";

    print "<form action=\"/cgi-bin/majordomo2\" method=\"POST\">\n";
    print "<table width=100% border=2>\n";
    print "<tr><td>\n";
    print "<pre>\n";
    print "Email address:   <input NAME=\"email\" TYPE=\"text\" SIZE=\"40\"><p>\n";
#   print "Real Name:       <input NAME=\"name\" TYPE=\"text\" SIZE=\"40\"><p>\n";
    if (!defined($Vars{'ro'}))
    	{
	print "Mailing List:    <input NAME=\"mlist\" TYPE=\"text\" SIZE=\"40\" VALUE=\"$mlist\"><p>\n";
	print "Host:            <input NAME=\"host\" TYPE=\"text\" SIZE=\"40\" VALUE=\"$host\"><p>\n";
	}
    else
    	{
	print "Mailing List:    $mlist<input NAME=\"mlist\" TYPE=\"hidden\" SIZE=\"40\" VALUE=\"$mlist\"><p>\n";
	print "Host:            $host<input NAME=\"host\" TYPE=\"hidden\" SIZE=\"40\" VALUE=\"$host\"><p>\n";
    	}
    print "<p>\n";
    print "</pre>\n";

    if (!defined($Vars{'-s'}))
    	{
	print "<input TYPE=\"checkbox\" NAME=\"subscribe\" checked> ";
	print "Subscribe -- Subscribe to the specified mailing list.<br>\n";
	}

    if (!defined($Vars{'-u'}))
    	{
	print "<input TYPE=\"checkbox\" NAME=\"unsubscribe\">";
	print "Unsubscribe -- Unsubscribe from the specified mailing list.<br> ";
	}

    if (!defined($Vars{'-h'}))
    	{
	print "<input TYPE=\"checkbox\" NAME=\"help\"> ";
	print "Help -- Get a help message from Majordomo.<br>\n";
	}

    if (!defined($Vars{'-i'}))
    	{
	print "<input TYPE=\"checkbox\" NAME=\"info\"> ";
	print "Info -- Get information on the specified mailing list.<br>\n";
	}

    if (!defined($Vars{'-I'}))
    	{
	print "<input TYPE=\"checkbox\" NAME=\"index\"> ";
	print "Index -- Return an index of files you can \"get\" for the specified list.<br>\n";
	}

    if (!defined($Vars{'-w'}))
    	{
	print "<input TYPE=\"checkbox\" NAME=\"which\"> ";
	print "Which -- Find out which lists you are on for the specified host.<br>\n";
	}

    if (!defined($Vars{'-W'}))
    	{
	print "<input TYPE=\"checkbox\" NAME=\"who\"> ";
	print "Who -- Find out who is on the specified list.<br>\n";
	}

    if (!defined($Vars{'-l'}))
    	{
	print "<input TYPE=\"checkbox\" NAME=\"lists\"> ";
	print "Lists -- Get an index of the mailing lists available on the specified host.<br>\n";
	}

    if (!defined($Vars{'-g'}))
    	{
	print "<input TYPE=\"checkbox\" NAME=\"get\"> ";
	print "Get -- Get a file from the archive associated with the specified mailing list.<br>\n";
	print "<pre>\n";
	print "Filename:        <input NAME=\"fname\" TYPE=\"text\" SIZE=\"40\">(for get command)<p>\n";
	print "</pre>\n";
	}

    print "<center>\n";
    print "<input NAME=\"bgcolor\" TYPE=\"hidden\" VALUE=\"$Vars{'bgcolor'}\">\n";
    print "<input NAME=\"link\" TYPE=\"hidden\" VALUE=\"$Vars{'link'}\">\n";
    print "<input NAME=\"Submit\" TYPE=\"submit\" VALUE=\"Submit\">\n";
    print "<input NAME=\"Reset\" TYPE=\"reset\" VALUE=\"Reset\">\n";
    print "</center>\n";
    print "</td></tr>\n";
    print "</table>\n";
    }


###
###	HTMLhead
###
###	Put out a HTML header
###
sub HTMLhead
    {
    local($title) = @_;

    print "Content-type: text/html\n\n";
    print "<html><head><title>$title</title></head>\n";
    }


###
###	HTMLterm
###
###	Put out the end of an HTML body.
###
sub HTMLterm
    {
    print "</body></html>\n";
    }


###
###	ParseQueryString
###
###	Break apart a query string.
###	This probably will need to be smarter.
###	It should unescape values.
###
###	A special hack is that the variables are also put into
###	an ordered list for backwards compatibility.
###
sub ParseQueryString
    {
    my($field, @fields, $name, $value);

    return if (!defined($ENV{'QUERY_STRING'}) || $ENV{'QUERY_STRING'} eq "");
    @fields = split(/\&/, $ENV{'QUERY_STRING'});
    for $field (@fields)
    	{
    	($name, $value) = split(/=/, $field, 2);
    	$value = "" if (!defined($value));
    	$Vars{$name} = unquote($value);
    	push(@Vars, "$field");
    	}
    }


###
###     printenv
###
###     Display the environment.
###	This assumes that we can write on stdout.  This may not
###	be true if we haven't written the header line yet.
###
sub printenv
    {
    local(@env, $var);

    @env = `printenv`;
    for $var (@env)
        {
        print "$var<br>\n";
        }
    }


###
###	unquote
###
###	Unescape a CGI form variable.
###	There are better ways to do this.
###
sub unquote
    {
    my($raw) = @_;
    my($code, @pieces, $piece);

    $raw =~ s/\+/ /mg;
    @pieces = split(/%/, $raw);
    for ($piece = 1; $piece <= $#pieces; $piece++)
    	{
    	$pieces[$piece] =~ s/^%//;
    	$code = substr($pieces[$piece], 0, 2);
    	$code = hex($code);
    	$pieces[$piece] = sprintf("%c%s", $code, substr($pieces[$piece], 2));
    	}
    return join("", @pieces);
    }


###
###     untaint
###
###     Pass over a string.
###     This is somewhat cheating.
###
sub untaint
    {
    local($var) = @_;

    $var =~ m#^(.*)$#;
    return $1;
    }
