#!/usr/bin/perl -w

# create-mailman-aliases.pl
# (c) Christian Boltz 2009 - www.cboltz.de
# License: GPL v2 or later

use strict; use warnings;
use DBI;
use DBD::mysql;

# read secrets
our ($database, $host, $dbuser, $dbpass);
require "/etc/cb-conf/create-mailman-aliases.pl.conf";

my $dbh; my $sth;

$dbh = DBI->connect(
	"DBI:mysql:database=$database;host=$host", "$dbuser", "$dbpass"
) || die "Cannot connect to database";

# read defaults
my $default_mailman_domain = `cat /etc/cb-conf/default-mailman-domain`;


                        # example:
my $domain = shift;     # example.com
my $listname = shift;   # domain-newsletter
my $zieldomain = shift; # lists.example.com

if ($listname =~ /^$/) { die "missing parameter"; }
if ($zieldomain =~ /^$/) { $zieldomain = $default_mailman_domain; }


$sth = $dbh->prepare("INSERT INTO `alias` (`address`, `goto`, `domain`, `created`, `active`) VALUES (?, ?, ?, NOW(), '1');");

my $adresse;
my @adressen = (
	"", 
	"-admin",
	"-bounces",
	"-confirm",
	"-join",
	"-leave",
	"-owner",
	"-request",
	"-subscribe",
	"-unsubscribe"
);

foreach $adresse(@adressen) {
	$sth->execute("$listname$adresse\@$domain", "$listname$adresse\@$zieldomain", $domain);
}
