#!/usr/bin/perl -w

##
## SEND_MESSAGE
##
## Warren Sack
## February 2005
##
##

use CGI;
use News::NNTPClient;

# NNTPServer
# NNTPUID
# NNTPPassword
$log_file = 'newsgroup_posting_log.txt';

##
## load_params
##
sub load_params
{
    my(@params) = @_;
    my($param,$param_value);
    foreach $param (@params) {
	$param_value = $request->param(lc($param));
	if (not($param_value)) { $param_value = $request->param(uc($param)); }
	if ($param_value) { 
	    $params{lc($param)} = $param_value;
	}
    }
}


$request = new CGI;
%params = ();
$log_text = '';
## read possible parameters
&load_params('url','cgi_url','archive','smid','id','subject','news_group','sender','message_body','language_locale');


## PRINT_TO_LOG
##
sub print_to_log
{
    my($status_message_string,$is_html_p) = @_;
    
    my($time);

    open(LOG,'>>'.$log_file);
    $time = localtime;
    print LOG $time.' >> ';
    print LOG $status_message_string."\n";
    close(LOG);
    if ( $is_html_p ) {
	$log_text .= '<B class=listtext>'.$time.'&nbsp;'.$status_message_string.'</B><BR>'."\n";
    }
}


##
## post_to_usenet
##
sub post_to_usenet
{
    my($article,@article);

    if ( $params{'id'} ) {
	@article = ('Newsgroups: '.$params{'news_group'}, 
		    'Subject: '.$params{'subject'},
		    'From: '.$params{'sender'},
		    'References: '.$params{'id'},
		    '',
		    $params{'message_body'});
    }
    else {
	@article = ('Newsgroups: '.$params{'news_group'}, 
		    'Subject: '.$params{'subject'},
		    'From: '.$params{'sender'},
		    '',
		    $params{'message_body'});
    }

    if ( $params{'language_locale'} =~ /^EN/ ) { 
	&print_to_log("Connecting to newsgroup server $nntp_server...",1);
    }
    elsif ( $params{'language_locale'} =~ /^FR/ ) { 
	&print_to_log("agonistics se relie au serveur $nntp_server...",1);
    }
    elsif ( $params{'language_locale'} =~ /^DE/ ) { 
	&print_to_log("Agonistics kontaktiert den Server $nntp_server ...",1);
    }
    eval { $nntp = new News::NNTPClient($nntp_server) };
    if ( $@ ) {
	&print_to_log("ERROR: Couldn't connect to newsgroup server...",1);
	return;
    }
    &print_to_log("Logging in...");
    if ( $nntp_uid and $nntp_password ) {
	eval { $nntp->authinfo($nntp_uid,$nntp_password) };
	if ( $@ ) {
	    &print_to_log("ERROR: Couldn't log in to newsgroup server...",1);
	    return;
	}
    }
    &print_to_log("Posting message...");
    eval { $nntp->post(@article) };
    if ( $@ ) {
	&print_to_log("ERROR: Couldn't post to newsgroup server...",1);
	return;
    }
    $article = join "\n".'<BR>',@article;
    $article = '<BR><BR><BR>'.$article.'<BR><BR><BR>';
    &print_to_log($article,0);
    if ( $params{'language_locale'} =~ /^EN/ ) { 
	&print_to_log("Message posted to newsgroup ".$params{'news_group'}.'.',1);
    }
    elsif ( $params{'language_locale'} =~ /^FR/ ) { 
	&print_to_log("Message envoy&eacute au newsgroup ".$params{'news_group'}.'.',1);
    }
    elsif ( $params{'language_locale'} =~ /^DE/ ) { 
	&print_to_log("Nachricht an Newsgroup ".$params{'news_group'}.' gesendet.',1);
    }
    eval { $nntp->quit };
    if ( $@ ) {	&print_to_log("ERROR: Failed logout...",1) };
    return;
}


##
## create_send_message_page
##
sub create_send_message_page
{
    print "Content-Type: text/html; charset=utf-8\n\n";
    print '<HTML>

    <HEAD>

         <META HTTP-EQUIV="Content-Type" content="text/html; charset=utf-8">

        <TITLE>agonistics / send message</TITLE>

        <LINK type="text/css" rel="stylesheet" href="http://'.$params{'url'}.'/Agonistics/'.$params{'archive'}.'/Interface/Messages/message_style.css">
       

    </HEAD>

    <BODY>

      <SCRIPT>window.resizeTo((screen.availWidth/2),(screen.availHeight/2));</SCRIPT>

      '.$log_text.'
       <INPUT name="close_window" class=buttonstyle type="SUBMIT" value="CLOSE&nbsp;WINDOW" onMouseDown="window.close();">
    </BODY>

    </HTML>';
}

&post_to_usenet();
&create_send_message_page();
