#!/usr/local/bin/perl

# move problem addresses from <list> to "bounces"
#
# Assumes that the "approve" password for each list is stored in a file
# called ".majordomo" in the user's home directory, in the following format:
# 
# 	List		Password	Majordomo-Address
# 
# When you bounce someone from a list, it looks up that lists's password
# and Majordomo-Address (the address of the Majordomo server serving that
# list) in the .majordomo file, and looks for another list named "bounces"
# with the same Majordomo-Address.
# 
# Here's an example of what a .majordomo file should look like:
# 
# 	this-list	passwd1		Majordomo@This.COM
# 	other-list	passwd2		Majordomo@Other.GOV
# 	bounces		passwd3		Majordomo@This.COM
# 	bounces		passwd4		Majordomo@Other.GOV
# 
# A command of "bounce this-list user@fubar.com" will mail the
# following message to Majordomo@This.COM:
# 
# 	approve passwd1 unsubscribe this-list user@fubar.com
# 	approve passwd3 subscribe bounces user@fubar.com (930401 this-list)
# 
# Note that the date and the list the user was bounced from are included
# as a comment in the address used for the "subscribe bounces" command.
# 
# Brent Chapman                                   Great Circle Associates
# Brent@GreatCircle.COM                           1057 West Dana Street
# +1 415 962 0841                                 Mountain View, CA  94041

# $Source: /sources/cvsrepos/majordomo/bounce,v $
# $Revision: 1.1.1.1 $
# $Date: 1993/08/31 16:05:00 $
# $Author: rouilj $
# $State: Exp $
#
# $Locker:  $
#

require "getopts.pl";

&Getopts("df:") ||
    die("USAGE: bounce [-f <config-file>] [-d] <list> <user>\nStopped");

if (! defined($opt_f)) {
    $opt_f = <~/.majordomo>;
}

&read_config();

$list = shift(@ARGV);
$list =~ tr/A-Z/a-z/;
$list =~ s/@.*//;

$list_passwd = $passwd{$list};
if (! $list_passwd) {
    die("no password for list $list; stopping");
}

$bounce_passwd = $passwd{"bounces@$majordomo{$list}"};
if (! $bounce_passwd) {
    die("no password for list bounces; stopping");
}

local($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;

if (defined($opt_d)) {
    open(MSG, ">&STDOUT");
} else {
    open(MSG, "|/usr/lib/sendmail $majordomo{$list}") || 
	die("open(MSG, \"|/usr/lib/sendmail $majordomo{$list}\"): $!\nStopped");
}

print MSG <<EOF;
To: $majordomo{$list}

EOF

foreach (@ARGV) {
    printf MSG "approve %s unsubscribe %s %s\n", $list_passwd, $list, $_;
    printf MSG "approve %s subscribe bounces %s (%02d%02d%02d %s)\n",
	$bounce_passwd, $_, $year, $mon+1, $mday, $list;
}
close(MSG);

exit 0;

sub read_config {
    open(CONF, $opt_f) || die("open(CONF, \"$opt_f\"): $!");
    while (<CONF>) {
	s/\n$//;
	s/#.*//;
	if (/^$/) { next; }
	split;
	$_[0] =~ tr/A-Z/a-z/;
	$_[2] =~ tr/A-Z/a-z/;
	$passwd{$_[0]} = $_[1];
	$passwd{"$_[0]@$_[2]"} = $_[1];
	$majordomo{$_[0]} = $_[2];
    }
    close(CONF);
}
