#!@PERL@
$rcsid = q$Id: w3mmail.cgi.in,v 1.5 2002/01/21 15:56:13 ukai Exp $;
($id = $rcsid) =~ s/^.*,v ([\d\.]*).*/$1/;
($prog=$0) =~ s/.*\///;
$query = $ENV{'QUERY_STRING'};
$local_cookie = $ENV{'LOCAL_COOKIE'};
$SENDMAIL = '/usr/lib/sendmail';
$SENDMAIL = '/usr/sbin/sendmail' if -x '/usr/sbin/sendmail';
$SENDMAIL_OPT = '-oi -t';
if ($query =~ s/^\w+://) {
    $url = $query;
    $qurl = &html_quote($url);
    $to = $query;
    $opt = '';
    if ($to =~ /^([^?]*)\?(.*)$/) {
	$to = $1;
	$opt = $2;
    }
    $to = &url_unquote($to);
    %opt = &parse_opt($opt);
    @to = ($to);
    push(@to, $opt{'to'}) if ($opt{'to'});
    $opt{'to'} = join(',', @to);
    $body = $opt{'body'};
    delete $opt{'body'};
    &lang_setup;
    print "Content-Type: text/html\r\n";
    print "w3m-control: END\r\n";
    print "w3m-control: PREV_LINK\r\n";
    print "\r\n";
    print "
W3M Mailer: $qurl\n";
    print "W3M Mailer: $qurl
\n";
    print "\n";
    print "\n";
    exit(0);
} else {
    sysread(STDIN, $req, $ENV{'CONTENT_LENGTH'});
    %opt = &parse_opt($req);
    if ($local_cookie ne $opt{'cookie'}) {
	print "Content-Type: text/plain\r\n";
	print "\r\n";
	print "Local cookie doesn't match: It may be an illegal execution\n";
	exit 1;
    }
    delete $opt{'cookie'};
    $body = $opt{'body'};
    delete $opt{'body'};
    $act = $opt{'action'};
    delete $opt{'action'};
    &lang_setup;
    if ($act eq "Preview") {
	print "Content-Type: text/html\r\n";
	print "w3m-control: DELETE_PREVBUF\r\n";
	print "w3m-control: NEXT_LINK\r\n";
	print "\r\n";
	print "W3M Mailer\n";
	print "\n";
	print "W3M Mailer: preview
\n";
	print "