#! /csuite/bin/bash

# mdml-create,v 1.2 1998/12/04 19:08:04 potter Exp
# Copyright (C) 1995, 1996 Chebucto Community Net Society
# This file is part of Chebucto Suite.  Use is governed by the terms
# laid out in the file COPYRIGHT.

# template version 1.0

## mdml-create - MajorDomo List Creation
#
#	This script processes a form used to request the creation
#	of majordomo mailing lists.

cd /tmp

. $DOCUMENT_ROOT/.cgi-funclib

cs_html_head -t userhelp $"Mailing List Creation"

trap cs_html_foot 0

#
# Main
#
majordomodir=$MD_LISTS
archivedir=$MD_VAR/archive
digestdir=$MD_VAR/digests
spooldir=$MD_HOME/spool

if [ -z "$USER" ]; then
	echo $"<h1>ERROR:</h1>"
	echo $"<h2>This form must be submitted using lynx.</h2>"
	exit 1
fi

if [ ! -d $majordomodir -o ! -d $archivedir -o ! -d $digestdir  -o ! -d $spooldir ]; then
	echo $"<h1>ERROR:</h1>"
	echo $"<h2>missing majordomo system directories.</h2>"
	exit 2
fi

if [ -z "$CONTENT_LENGTH" ] || [ "$CONTENT_LENGTH" -eq 0 ]; then
	cd $spooldir
echo $"
<h1>Process Spooled Mailing List Requests</h1>
<p>
Please select the request you would like to process:
<p>
<FORM method=POST action=\"lynxcgi:$LYNXCGI/mdml-create\">
"
	for sf in *
	do
		if [ "$sf" = "*" ]; then
			echo $"No requests currently spooled."
			exit 0
		fi
		cat <<EOM
<input type=radio name="spoolfile" value="$sf"> $sf
EOM
	done
echo $"
<center>
<br>
<input type=\"submit\" value=\"Process Request\">
</center>
</FORM>
"
	exit 0
fi

if [ ! -z "$cgi_spoolfile" ]; then
	echo $"<h1>Approve Mailing List Creation</h1>"

	cd $spooldir
	eval `gawk -F= '
	{ printf ("cgi_%s=%c%s%c\n", $1, 34, $2, 34) }
	' $cgi_spoolfile`
	#echo "<pre>"
	#set
	#echo "</pre>"
	check_regular=
	check_digest=
	check_regular_digest=
	case "$cgi_type" in
	  "regular")
		check_regular="checked"
		;;
	  "digest")
		check_digest="checked"
		;;
	  "regular-digest")
		check_regular_digest="checked"
		;;
	esac
	check_open=
	check_closed=
	case "$cgi_subscribe_policy" in
	  "open")
		check_open="checked"
		;;
	  "closed")
		check_closed="checked"
		;;
	esac
	check_mno=
	check_myes=
	case "$cgi_moderate" in
	  "no")
		check_mno="checked"
		;;
	  "yes")
		check_myes="checked"
		;;
	esac
	check_pno=
	check_pyes=
	case "$cgi_private_who" in
	  "no")
		check_pno="checked"
		;;
	  "yes")
		check_pyes="checked"
		;;
	esac
echo $"
<hr>
You may need to
<a href=\"lynxexec:$LYNXEXEC/cs_sudo -v\">update your sudo permissions</a>.
<hr>
<FORM method=POST action=\"lynxcgi:$LYNXCGI/mdml-create\">
<h1>Mailing List Approval Form</h1>
<pre>
List Type:        <input type=radio name=\"listtype\" value=\"regular\" $check_regular> Regular <input type=radio name=\"listtype\" value=\"digest\" $check_digest> Digest <input type=radio name=\"listtype\" value=\"regular-digest\" $check_regular_digest> 
Regular+Digest

List Name:        <input type=\"text\" name=\"listname\" value=\"$cgi_name\">
Description:      <input type=\"text\" name=\"description\" value=\"$cgi_description\">
List Owner:       <input type=\"text\" name=\"owner\" value=\"$cgi_owner\">
Admin Password:   <input type=\"text\" name=\"admin_p\" value=\"$cgi_admin_p\">
Approve Password: <input type=\"text\" name=\"approve_p\" value=\"$cgi_approve_p\">

Subscribe Policy: <input type=radio name=\"subscribe_policy\" value=\"open\" $check_open> Open <input type=radio name=\"subscribe_policy\" value=\"closed\" $check_closed> Closed

Moderated:        <input type=radio name=\"moderate\" value=\"no\" $check_mno> No <input type=radio name=\"moderate\" value=\"yes\" $check_myes> Yes

Private:          <input type=radio name=\"private\" value=\"no\" $check_pno> No <input type=radio name=\"private\" value=\"yes\" $check_pyes> Yes
</pre>
<input type=hidden name=\"private_get\" value=\"$cgi_private_get\">
<input type=hidden name=\"private_index\" value=\"$cgi_private_index\">
<input type=hidden name=\"private_info\" value=\"$cgi_private_info\">
<input type=hidden name=\"private_which\" value=\"$cgi_private_which\">
<input type=hidden name=\"private_who\" value=\"$cgi_private_who\">
<center>
<br>
<input type=radio name=\"approve\" value=\"yes\" check> Approve Request <input type=radio name=\"approve\" value=\"no\"> Deny Request
<p>
<input type=\"submit\" value=\"Submit Form\"> / <input type=\"reset\" value=\"Reset Form\">
</center>
</FORM>
"
	exit 0
fi

echo $"<h1>Mailing List Creation Results</h1>"

cgi_listname=`echo $cgi_listname | cgi2text | tr -cd 0-9A-Za-z-_ | tr A-Z a-z`
cgi_description=`echo $cgi_description | cgi2text`


trap 'unlock $spooldir/$cgi_listname $$ >/dev/null 2>&1' 0 1 2 15

if lock $spooldir/$cgi_listname $$
then
	:
else
	echo $"<h1>ERROR:</h1>"
	echo $"<h2>unable to get system lock for $cgi_listname.</h2>"
	exit 3
fi

if [ -z "$cgi_listname" ]; then
	echo $"<h1>ERROR:</h1>"
	echo $"<h2>No name for the list was entered.</h2>"
	exit 4
fi

#
# recreate spoolfile and set permissions
#
cd $spooldir
>$cgi_listname
chmod 660 $spooldir/$cgi_listname

#
# add data to spoolfile
#
cat >>$cgi_listname <<EOI
name=$cgi_listname
type=$cgi_listtype
owner=$cgi_owner
description=$cgi_description
admin_p=$cgi_admin_p
approve_p=$cgi_approve_p
subscribe_policy=$cgi_subscribe_policy
moderate=$cgi_moderate
private_get=$cgi_private_get
private_index=$cgi_private_index
private_info=$cgi_private_info
private_which=$cgi_private
private_who=$cgi_private
EOI

case "$cgi_approve" in
  "yes")
	$CS_ROOT/admin-bin/sudo $CS_ROOT/admin-bin/mkmdml $spooldir/$cgi_listname
	status=$?
	case $status in
	  "0")
	echo $"From: majordomo-owner (Mailing List Creator)
To: $cgi_listname-owner
Subject: Request for mailing list ($cgi_listname) approved

Your request for a mailing list has been approved and created.
Remember to setup your info file for your new list.
The info file should contain information about the list (its purpose etc.),
that will get mailed to the new subcribers.

			$FULL_NAME Mailing List Management	
	" | $SENDMAIL

#
# new... this should subscribe the list owner to the list...
# and cc the message to the owner... this will give them the password.

echo "To: majordomo
Cc: $owner
From: $ipcreator

approve $approve_p subscribe $name $owner

" | $SENDMAIL -t

	echo $"
	<p>
	The mailing list \"$cgi_listname\" ($cgi_description) has been created.
	<p>
	"
		;;
	  *)
	echo $"
	<p>
	The mailing list \"$cgi_listname\" ($cgi_description) could not be created.
	<p>
	"
		unlock $spooldir/$cgi_listname $$
		exit 5
		;;
	esac
	;;

  "no")
  echo $"From: majordomo-owner (Mailing List Creator)
To: $cgi_owner
Subject: Request for mailing list ($cgi_listname) denied

Your request for a mailing list has been denied.
Please reply to this message if you require information on
why your request has been denied.

				$FULL_NAME Mailing List Management
" | $SENDMAIL

        echo $"
	<p>
	The mailing list \"$cgi_listname\" ($cgi_description) was denied approval for creation.
	"
	;;
esac

echo $"<hr>
<a href=\"file://localhost/$DOCUMENT_ROOT/adm/MDAdmin.html\">[ Mailing List Administration
]</a>"

unlock $spooldir/$cgi_listname $$
rm $spooldir/$cgi_listname

echo "<hr>"
exit 0
