#! /usr/bin/perl
# 
# $Id$
# Mailman page Perl template.
# Copyright (C) 2003 Jan Kratochvil <project-www.jankratochvil.net@jankratochvil.net>
# 
# 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; exactly version 2 of June 1991 is required
# 
# 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


package Mailman;
require 5.6.0;	# at least 'use warnings;' but we need some 5.6.0+ modules anyway
use vars qw($VERSION $CVS_ID);
$VERSION=do { my @r=(q$Revision$=~/\d+/g); sprintf "%d.".("%03d"x$#r),@r; };
$CVS_ID=q$Id$;
use strict;
use warnings;

BEGIN{ open F,"Makefile"; our $top_dir=pop @{[split /\s/,(grep /^top_srcdir/,<F>)[0]]}; eval "use lib '$top_dir'"; close F; }
use My::Web;
require CGI;
use URI::Escape;
require LWP::Simple;


my $W=My::Web->init(
		"__PACKAGE__"=>__PACKAGE__,
		"title"=>"Mailman Interface",
		"no_job"=>1,
		"args_check"=>{
				"email"=>'',
				"pw"=>'',
				"pw-conf"=>'',
				"list"=>['^[-\w]+$'],
				"back"=>'',
				},
		);
My::Web->heading();


for my $list (@{$W->{"args"}{"list"}}) {
	print "<h1>List <i>$list</i></h1><blockquote>\n";
		my $url_base=&{$W->{"mailman_url_sub"}}()."/subscribe/$list";
		my $t=LWP::Simple::get("$url_base?"
				.join('&',
						"list=$list",
						map({ $_."=".uri_escape($W->{"args"}{$_}); } qw(email pw pw-conf))));
		$t=($t=~m#^.*<body\b[^>]*>(.*?)</body>#is)[0];
		$t=~s#(href=")(../[^"]*)(")#$1$url_base/$2$3#gi;
		print $t;
	print "</blockquote>\n";
	}

if ($W->{"args"}{"back"}) {
	print '<h1>'.a_href($W->{"args"}{"back"},'Back to the original page').'</h1>'."\n";
	}


My::Web->footer();
