#!/usr/bin/perl
#------------------------------------------------------------------------------
# Copyright (C) 2001 Thomax Kaulmann
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.


use CGI;
use Net::NNTP;
use NEWS;
use lib '../oma-bin';
use OMA;

$nntp = NEWS::nntpconnect();

local %in;
undef(%in);

CGI::ReadParse();

print "
<html>
<head>
</head>
<body>
";


sub sendmsg {
	local $msg;
	local $newmsg;
	local $grp;

	$grp = OMA::newsname($in{'node'});

#	$msg = "Path meta.orang.org!not-for-mail
	$msg = "From: $in{'name'} <$in{'email'}>
Subject: $in{'subject'}
Newsgroups: $grp
Message-ID: <$in{'msgid'}>
References: <$in{'reference'}>

$in{'msgbody'}
";
	if($nntp->postok()) {
		if($nntp->post($msg) == 1) {
			print "posting ok ($grp)\n";
		} else {
			print "couldn't post\n";
		}
	} else {
		print "can't send\n";
	}
	NEWS::updatehtml($in{'node'});
	print "<br><a href=\"javascript:history.go(-2)\">BACK</a>";
	$nntp->quit();
	exit;
}

sub editor {
	local $msg = shift;
	local $msgid = shift;
	local $subject = shift;
	local $head = `cat template/omahead.html`;
	local $foot = `cat template/omafoot.html`;

	print "$head";

	local $formpart="";

	if($msgid) {
		$formpart = "
<input type=\"hidden\" name=\"reference\" value=\"$msgid\" />
<input type=\"hidden\" name=\"subject\" value=\"$subject\" />
<input type=\"hidden\" name=\"msgid\" value=\"". NEWS::newmsgid ."\" />
		";
		print "Reply to: $subject<br />";
	} else {
		$formpart = "
<input type=\"hidden\" name=\"msgid\" value=\"". NEWS::newmsgid ."\" />
Subject:<br /><input type=\"text\" name=\"subject\" /><br />
		";
	}

	print "
<form method=post>
$formpart
Name:<br /> <input type=\"text\" name=\"name\" /><br />
Email:<br /> <input type=\"email\" name=\"email\" /><br />
Message:<br />
<font size=\"-1\">
<textarea cols=\"80\" rows=\"24\" name=\"msgbody\">$msg</textarea>
</font>
<br />
<input type=\"submit\" name=\"send\" value=\"send\" />
<input type=\"hidden\" name=\"op\" value=\"send\" />
<input type=\"hidden\" name=\"node\" value=\"$in{'node'}\" />
</form>
$foot
	";
}

sub followup {
	local $msgid = $in{'msgid'};
	local $msg = "";
	local $i;
	local $j;
	local $head;
	local $article;
	local @lines;
	local @sub;
	local $subject = "";

	$head = $nntp->head("<$msgid>");
	@lines = @$head;
    for($i=0;$i<=$#lines;$i++) {
		if($lines[$i] =~ /^Subject: /) {
    	    ($t,$subject) = split(/Subject: /,$lines[$i]);
			chop($subject);
		}
    }

	$article = $nntp->body("<$msgid>");
	@lines = @$article;

    for($i=0;$i<=$#lines;$i++) {
        $msg .= "> $lines[$i]";
    }

	$subject = "Re: $subject" if(!(lc($subject) =~ /^re: /));
	editor($msg,$msgid,$subject);
	$nntp->quit();
	exit;
}

sub newmsg {
	editor;
}

followup if($in{'op'} eq "followup");
sendmsg if($in{'op'} eq "send");
newmsg if($in{'op'} eq "newmsg");

