#!/usr/bin/perl -w
#
# Copyright (C) 2007, Joshua D. Abraham (jabra@spl0it.org)
#
# 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.
#
# 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
# use strict;
#
# genemailaddrs.pl - generates email lists based on name list, domain and scheme.
#
# Examples: ./genemailaddrs.pl -d google.com -n names.txt -s 1
#           
#           ./genemailaddrs.pl -d google.com -n names.txt -s 13
# 
#           ./genemailaddrs.pl -d google.com -n names.txt -s 2,13,30  
#           
#           ./genemailaddrs.pl -d google.com -n names.txt --all
#
use strict;
use Getopt::Long;
use vars qw( $PROG );
( $PROG = $0 ) =~ s/^.*[\/\\]//;    # Truncate calling path from the prog name

my $AUTH    = 'Joshua D. Abraham';  # author
my $EMAIL    = 'jabra@spl0it.org';  # email
my $VERSION = '1.0';                # version

my %options;

my $domain;
my @names;
my @schemes;

#
# help: ->
# display help information
# side effect:  exits program
#
sub help {
    print "Usage: $PROG [Options]

Options:
   -d   --domain [domain]       Domain
   -n   --names [name list]     File containing list of names
   -s   --scheme [scheme]       Scheme Number(s) (Comma Separated)
   
   -a   --all                   Generate list with all schemes
   
   -t   --type [number]         Generate list using a specific type
   -g   --group [number]        Generate list with for a specific grouping

   -v   --version               Display version
   -h   --help                  Display this information

Schemes Examples:                   
        Scheme                  Separator     
        ------                  ----------  
        1                       none            (ex: johnsmith\@domain)
        2                       dash            (ex: john-smith\@domain)
        3                       underscore      (ex: john_smith\@domain)
        4                       dot             (ex: john.smith\@domain)

        11                      no separator    (ex: jsmith\@domain)
        22                      dash            (ex: j-smith\@domain)

        ...  This continues for all the types below    ...
       
Schemes Definition:
        Scheme      Group     
        1-10        1           firstname lastname
        11-20       2           first_char_firstname lastname
        21-30       3           five_chars_firstname lastname
        31-40       4           five_chars_firstname first_char_lastname

Send Comments to $AUTH ( $EMAIL )\n";
    exit;
}

#
# print_version ->
# displays version
# side effect: exits program
#
sub print_version {
    print "$PROG version $VERSION by $AUTH ( $EMAIL )\n";
    exit;
}

#
# make_email_address: domain scheme name  ->
# convert a name into a email address
#
sub make_email_address {
    my ($domain, $scheme, $name) = @_;
   
    $name =~ s/\s+$//;
    $name =~ s/^\s+//;
    
    $domain = '@' . $domain;
    my ( $fname, $lname ) = split(' ', $name);

    # set first name to first characters of first name
    # set last name to first five characters of the last name
    if ( $scheme > 30 ) {
        if ( $fname =~ /(\w{1})/ ) {
            $fname = $1;
        }
        if ( $lname =~ /(\w{5})/ ) {
            $lname = $1;
        }
    }
    # set first name to first five characters of first name
    if ( $scheme > 20 ) {
        if ( $fname =~ /(\w{5})/ ) { 
            $fname = $1;
        }
    }
    # set first name to first character of first name
    elsif ( $scheme > 10 ) { 
        if ( $fname =~ /(\w{1})/ ) {
            $fname = $1;
        }   
    }
    if ( $scheme == 1 or $scheme == 11 or $scheme == 21 or $scheme == 31 ) { 
        print "$fname$lname$domain\n";
    }
    elsif ( $scheme == 2 or $scheme == 12 or $scheme == 22 or $scheme == 32 ) {
        print $fname . '-' . $lname . "$domain\n";
    }
    elsif ( $scheme == 3 or $scheme == 13 or $scheme == 23 or $scheme == 33 ) {
        print $fname . '_' . $lname . "$domain\n";
    }
    elsif ( $scheme == 4 or $scheme == 14 or $scheme == 24 or $scheme == 34 ) {
        print $fname . '.' . $lname . "$domain\n";
    }
    else {    
        print "Error: Scheme not fond\n";
        exit;
    }
}

GetOptions(
    \%options,
    'domain|d=s', 'names|n=s','scheme|s=s','all|a','group|g=s','type|t=s',
    'help|h'    => sub { help(); },
    'version|v' => sub { print_version(); },
    )
    or exit 1;
if ( $options{domain} ) {
    $domain = $options{domain};
}
if ( $options{all} ) {
    @schemes = qw( 1 2 3 4 11 12 13 14 21 22 23 24 31 32 33 34 ) ;
    
}
if ( $options{type} ) {
    if ( $options{type} == 1 ) {   
        @schemes = qw( 1 11 21 31 );
    }
    elsif ( $options{type} == 2 ) {
        @schemes = qw( 2 12 22 32 );
    }
    elsif ( $options{type} == 3  )  {
        @schemes = qw( 3 13 23 33 );
    }
    elsif ( $options{type} == 4 ) {
        @schemes = qw( 4 14 24 34 );
    }
} 
if ( $options{group} ) {
    if ( $options{group} > 30 ) {
        @schemes = qw( 31 32 33 34 ) ;
    }
    elsif ( $options{group} > 20 ) {
        @schemes = qw( 21 22 23 24 ) ;
    }
    elsif ( $options{group} > 10 ) {
        @schemes = qw( 11 12 13 14 ) ;
    }
    elsif ( $options{group} < 10 ) {
        @schemes = qw( 1 2 3 4 );     
    }

}
elsif ( $options{scheme} ) {
    @schemes = split(',', $options{scheme});
}

if ( $options{names} ) {
    my $file = $options{names};
    open(INPUT, "<$file");
    while( <INPUT> ) {
        chomp;
        push(@names,$_);
    }
}

if ( scalar(@schemes) == 0 or !defined($domain) or scalar(@names) == 0  ) {
    help();
}

foreach my $name ( @names ) {
    chomp($name);
    foreach my $scheme (@schemes) {
        make_email_address($domain, $scheme, $name);
    }
}
