#!/bin/sh


################
# Configuration
################

scptname="shbbs.cgi"

# If your server has got nkf, then set some string. If not then leave empty.
withnkf=""

# Master password for removing entries
masterpass="abcdefg"

# $CONTENT_LENGTH restriction
qlimit=512

# The number of entries to hold
maxe=200

# The number of entries you want to show at once
maxv=25

# If you want to save old entries which overflowed $maxe, then set some string.
enablelog=""

# Used only if $enablelog is set.
logdir="logdir"


datfile="shbbs.dat"

titlfn="titl"
namefn="name"
mailfn="mail"
dpasfn="pass"
descfn="desc"
delefn="rm"


##############
# Main Logic
##############

echo "Content-type: text/html; charset=Shift_JIS"
echo


### Remove the entry on line $1... =============

if [ "$1" -gt 0 -a "$REQUEST_METHOD" = "POST" ] 2>/dev/null
then
cat << FIN
<html>
<head>
<title>The Shell Script BBS</title>
</head>
<body>
<h1>Removing the entry...</h1>
FIN

if [ -n "$withnkf" ]; then
pass="`sed -n \"1s/^${delefn}=\\([^&]*\\)/\\1/p\"|tr '%+' '= '|nkf -mQj`"
else
pass="`sed -n \"1s/^${delefn}=\\([^&]*\\)/\\1/p\"|sed -e 's/%/\\x/g' -e 'y/+/ /'|xargs -0 -n 1 printf`"
fi
dpass="`sed -n \"${1}p\" \"$datfile\" | cut -f 6`"

#echo $pass $dpass

if test -n "$pass" -a "$pass" = "$dpass" -o "$pass" = "$masterpass" && ed -s "$datfile" 2>&1 << FIN
${1}s/^.*$/#&/
wq
FIN
then
echo '<p>Successfully removed the article.</p>'
else
echo '<p>The password does not match.</p>'
fi

echo "<a href=\"${scptname}\">Go back</a>"
echo '</body></html>'
exit
fi

### End of entry removing... =================

cat << FIN
<html>
<head>
<title>The Shell Script BBS</title>
</head>
<body>
<h1>The Shell Script BBS</h1>
<form action="$scptname" method="POST">
<table><tr><td>
<pre>TITLE   <input type="text" name="$titlfn">
NAME    <input type="text" name="$namefn">
MAIL    <input type="text" name="$mailfn">
DELPASS <input type="password" name="$dpasfn"></pre>
</td></tr><tr><td>
<textarea name="$descfn" rows=10 cols=50></textarea>
</td></tr></table>
<input type="submit"> <input type="reset">
</form>
<hr>
FIN


### Write the posted data into the datfile... =======

if [ "$REQUEST_METHOD" = "POST" ]
then

if [ "$CONTENT_LENGTH" -gt "$qlimit" ]
then
echo '<h1>The entry is too large.</h1>'
echo '<h1>Please make it  shorter.</h1></body></html>'
exit
fi


d=`date +'%F %T'`
l="`head -n 1 \"$datfile\" | cut -f 1 | tr -d '#'`"
l=`expr "$l" + 1 2>/dev/null`

if [ -n "$enablelog" ]
then
if [ "`expr \"$l\" '%' \"$maxe\" 2>/dev/null`" = '1' -a "$l" -ne 1 ] 2>/dev/null
then
test -d "./$logdir" || mkdir "./$logdir"
cp -n "$datfile" "./$logdir/`date +'%Y%m%d-%H%M-'`$datfile"
fi
fi

if [ "$l" -gt "$maxe" ]
then
b='$d'
else
b='w'
fi

a=`cat << FIN
function decurl(str)	{
	i=1;
	while (i <= length(str)) {
		if(substr(str,i,3) ~ /%../)
			str = substr(str,1,i-1) sprintf("%c",("0x"substr(str,i+1,2))+0) substr(str,i+3);
		i++;
	}
	return str;
}

BEGIN{
	FS="=";
	RS="&";
	OFS="\\t";
} 
"$dpasfn"{outp[\\$1]=\\$2} 
"$mailfn"||"$titlfn"||"$namefn"||"$descfn"{
	gsub(/[^[:alnum:]%+&-_.~*]/,"",\\$2);
	if(! "$withnkf"){
	tstr=decurl(\\$2);
	gsub(/\\t/," ",tstr);
	gsub(/&/,"\\\\\\\\&amp;",tstr);
	gsub(/</,"\\\\\\\\&lt;",tstr);
	gsub(/>/,"\\\\\\\\&gt;",tstr);
	gsub(/\\+/," ",tstr);
	if(\\$1=="$descfn") {gsub(/\\r?\\n/,"<br>",tstr);}
	else {gsub(/\\r?\\n/," ",tstr);}
	outp[\\$1]=tstr;
	}else{
	gsub(/%09/,"+",\\$2);
	if(\\$1!="$descfn") gsub(/\\r?\\n/,"+",\\$2);
	outp[\\$1]=\\$2;
	}
} 
END{
	printf "%s\\t%s\\t%s\\t%s\\t%s\\t%s\\t%s\\t%s\\t%s", "$l","$d",outp["$titlfn"],outp["$namefn"],outp["$mailfn"],outp["$dpasfn"],outp["$descfn"],"$REMOTE_ADDR","$REMOTE_HOST";
}
FIN`

#`awk "$a" | tr -d '\0-\010\012-\037\277'`
if [ -z "$withnkf" ]; then
ed -s "$datfile" 2>&1 << FIN
H
0a
`awk "$a" `
.
$b
wq
FIN
else
ed -s "$datfile" 2>&1 << FIN
H
1i
`awk "$a" | tr '%+' '= ' | nkf -mQs | sed -e 's/&/\&amp;/g'  -e 's/</\&lt;/g' -e 's/>/\&gt;/g' -e 's/\r?$/<br>/g' `
.
$b
wq
FIN
fi

fi

### Writing to the datfile ends... =============


### Output the entries... =================

if [ "$1" -gt "0" ] 2>/dev/null
then
cv="$1"
bv=`expr "$1" "*" "$maxv" + 1`
ev=`expr "$bv" + "$maxv"`
else
cv="0"
bv="1"
ev="$maxv"
fi

nl="`wc \"$datfile\" | awk '{print $1}'`"

sed -n "${bv},${ev}p" "$datfile" | awk 'BEGIN {FS="\t"} $0 !~ /^#/ {printf "<p>[%s] %s by %s %s</p>\n<p>%s</p>\n<form action=\"'"$scptname"'?%d\" method=\"POST\"><input type=\"text\" name=\"'"$delefn"'\"><input type=\"submit\" value=\"Delete\"></form>\n<hr>\n",$1,$3,$4,$2,$7,NR;}'

if [ "$cv" = "1" ]; then
echo "<a href=\"${scptname}\">Back</a>"
else
test "$cv" -gt "0" && echo "<a href=\"${scptname}?`expr \"$cv\" - 1 2>/dev/null`\">Back</a>"
fi
test "$ev" -lt "$nl" && echo "<a href=\"${scptname}?`expr \"$cv\" + 1 2>/dev/null`\">Next</a>"
echo "</body></html>"

### Output ends... =========================




exit

### My memo... >>>>>>>>>>>>>>>>>>>>>>

#|sed 's/%/\\\\x/g'|xargs -0t printf
#`awk 'BEGIN{FS="=";RS="&"} "name"{outp[$1]=$2;} "desc"{outp[$1]=$2;} END{printf "%s\t%s\n",outp["name"],outp["desc"];}'|tr '%+' '= '|nkf -mQ|sed 's/\r\n/<br>/g'`

#a="function decurl(str){\
#	while(str~/%../){\
#		i=index(str,\"%\");\
#		sub(/%../,sprintf(\"%c\",(\"0x\"substr(str,i+1,2))+0),str);\
#	}\
#	return str;\
#} \
#BEGIN{\
#	FS=\"=\";\
#	RS=\"&\";\
#} \
#\"$namefn\"||\"$descfn\"{\
#	gsub(/[^[:alnum:]%+&-_.~*]/,\"\",\$2);\
#	gsub(/%09/,\"%20\",\$2);\
#	outp[\$1]=decurl(\$2);\
#} \
#END{\
#	printf \"%s%%09%s\\n\",outp[\"$namefn\"],outp[\"$descfn\"];\
#}"
#function decurl(str){
#	while(str~/%[0-9a-fA-F]./){
#		i=index(str,"%");
#		sub(/%[0-9a-fA-F]./,sprintf("%c",("0x"substr(str,i+1,2))+0),str);
#	}
#	return str;
#} 
