#!/bin/perl -w
#
# File: mailmanWrapper.pl
# Author: Morgan Jones (morgan@commnav.com)
# Date: 10/15/02
#
#   Description: This is a workaround to allow mailman to work with
#       iPlanet/Sun ONE Messenger.  iPlanet/Sun ONE Messenger requires a
#       mail delivery command to be registered with argument list with
#       the MTA prior to execution.  Mailman requires commands like the
#       below to be run for each mailing list.  The problem is that for
#       each new mailing list that's created, three new commands would
#       need to be registered with the mta.  This would quickly get
#       cumbersome.
#
#       ## morgantest mailing list
#       ## created: 14-Oct-2002 root
#       morgantest:         "|/opt/mailman/mail/wrapper post morgantest"
#       morgantest-admin:   "|/opt/mailman/mail/wrapper mailowner morgantest"
#       morgantest-request: "|/opt/mailman/mail/wrapper mailcmd morgantest"
#       morgantest-owner:   morgantest-admin
#
#       Rather than registering commands such as the above with the MTA,
#       place this perl script it <messaging
#       root>/msg-<instance>/imta/programs and set any Mailman aliases
#       to deliver to it.  The ldap entries for the above aliases are
#       below the script.
#
use strict;

my $domain = "kutztown.edu";
my $wrapperCmd = "/opt/mailman/mail/wrapper";

# $ENV{LD_LIBRARY_PATH} = "/usr/local/ssl/lib";

open (OUT, ">/tmp/mailmanWrapper.$$");

# set input separator to two or more carriage returns.
$/="";
# get the first chunk, the headers
my @message = <>;
my $headers = $message[0];

# pull out the To: header
my $to = $headers;
die "no To: header !?"
    unless ($to =~ /^To:\s*([^\n]+)$/mi);
$to = $1;

if ($to !~ /^[^@]+@[^\.]*\.*$domain/) {
    # sanity check to make sure we're sending to this domain.  This
    # may not be necessary. (?)
    print OUT "not in $domain.  Exiting.\n";
} else {
    # parse the to: address, call the mailman wrapper appropriately
    $to =~ /^([^@]+)@/;
    my $addr = $1;
    my $cmd;
    if ($addr =~ /(-admin)$/i || $addr =~ /(-owner)$/i ) {
	$addr =~ s/$1$//i;
	$cmd = "$wrapperCmd mailowner $addr";
    } elsif ($addr =~ /(-request)$/i)  {
	$addr =~ s/$1$//i;
	$cmd = "$wrapperCmd mailcmd $addr\n";
    } else {
	$cmd = "$wrapperCmd post $addr\n";
    }
    print OUT "execing /$cmd/\n";

    open (CMD, "|$cmd");
    print CMD @message;
    close CMD;
    print OUT @message;
    my $rc = $? >> 8;
    print OUT "rc: $rc";
}

close OUT;




__END__

## morgantest mailing list
## created: 14-Oct-2002 root
morgantest:         "|/opt/mailman/mail/wrapper post morgantest"
morgantest-admin:   "|/opt/mailman/mail/wrapper mailowner morgantest"
morgantest-request: "|/opt/mailman/mail/wrapper mailcmd morgantest"
morgantest-owner:   morgantest-admin




dn: cn=morgantest,ou=groups,o=domain.com
mailProgramDeliveryInfo: mailmanWrapper
mailDeliveryOption: program
mailAlternateAddress: morgantest@mailhost.domain.com
mail: morgantest@domain.com
mailHost: mailhost.domain.com
objectClass: top
objectClass: inetLocalMailRecipient
objectClass: inetMailGroup
objectClass: groupOfUniqueNames
inetMailGroupStatus: active
cn: morgantest

dn: cn=morgantest-admin,ou=groups,o=domain.com
mailProgramDeliveryInfo: mailmanWrapper
mailDeliveryOption: program
mailAlternateAddress: morgantest-admin@mailhost.domain.com
mail: morgantest-admin@domain.com
mailHost: mailhost.domain.com
objectClass: top
objectClass: inetLocalMailRecipient
objectClass: inetMailGroup
objectClass: groupOfUniqueNames
inetMailGroupStatus: active
cn: morgantest-admin

dn: cn=morgantest-request,ou=groups,o=domain.com
mailProgramDeliveryInfo: mailmanWrapper
mailDeliveryOption: program
mailAlternateAddress: morgantest-request@mailhost.domain.com
mail: morgantest-request@domain.com
mailHost: mailhost.domain.com
objectClass: top
objectClass: inetLocalMailRecipient
objectClass: inetMailGroup
objectClass: groupOfUniqueNames
inetMailGroupStatus: active
cn: morgantest-request

dn: cn=morgantest-owner,ou=groups,o=domain.com
mailAlternateAddress: morgantest-owner@mailhost.domain.com
mail: morgantest-owner@domain.com
mailHost: mailhost.domain.com
objectClass: top
objectClass: inetLocalMailRecipient
objectClass: inetMailGroup
objectClass: groupOfUniqueNames
inetMailGroupStatus: active
cn: morgantest-owner
mgrpRFC822MailMember: morgantest-admin 
