#!/usr/bin/perl
#
# mailname: give email address information
#
# Copyright (C) 1997 Mark Rigby-Jones
#
# 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; either version 2 of the License, or
# (at your option) any later version.
#
# 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.
#
# If you do not have a copy of the GNU General Public License then
# you can get one by writing to the Free Software Foundation, Inc.,
# 675 Mass Ave, Cambridge, MA 02139, USA.
#

$version = "mailname v1.0 by Mark Rigby-Jones, 5th March 1997";

$domain = "ox.compsoc.org.uk";

$user = $ARGV[0];
if ($user eq "") {
    $user = $ENV{USER};
}

if (($user eq "-h") or ($user eq "--help")) {
    print(<<"EOT");
Usage: $0 <username>
Print email information for yourself or a supplied username.

-h, --help     Print this information and then exit.
-V, --version  Print version information and then exit.
EOT
    exit(0);
} elsif (($user eq "-V") or ($user eq "--version")) {
    print("$version\n");
    exit(0);
}

@pinfo = getpwnam($user);

if (@pinfo == 0) {
    print("$0: User $user unknown to the system!\n");
    exit(1);
}

@name = split(',', $pinfo[6]);
$name = @name[0];

open(USERDB, "/etc/userdb");

$outgoing = "$user\@$domain";
$incoming{$user} = 0;

while (chop($line = <USERDB>)) {
    @line = split(' ', $line);
    if ($line[0] eq "$user:mailname") {
	$outgoing = $line[1];
    } elsif ($line[1] eq $user) {
	@line = split(':', $line[0]);
	$incoming{$line[0]} = 0;
    }
}

close(USERDB);

print("email information for $name ($user):\n");
print("Outgoing email is rewritten as being from:\n  $outgoing\n");
print("Incoming email can be to any of the following:\n");

foreach $line (sort(keys(%incoming))) {
    print("  $line\@$domain\n");
}
