#!/usr/bin/perl -w
#################################################################
# emi.pl - Michael Karr
# Exim Message Info
# Gathers information about a specified Exim ID.
#
# Git: http://git.toolbox.hostgator.com/emi/emi
# Wiki: https://gatorwiki.hostgator.com/Security/EMI
# Wiki: https://gatorwiki.hostgator.com/Security/SpamReportsDashboard
# 
# Please submit all bug reports at http://bugs.hostgator.com
#
# (c) 2012 - HostGator.com, LLC.
#################################################################

BEGIN { # begin package EMI::Config
package EMI::Config;

use strict;
use warnings;

our $log_directory = '/var/log'; # Exim log directory
our $log_pattern = 'exim_mainlog((-\d{8})|(\.\d))?(\.gz)?'; # Exim log file name pattern

1;
} # end package EMI::Config


{ # start main package
package EMI;

use strict;
use warnings;

sub main {
    print "emi.pl - Michael Karr\n\n"; 

    my %search_ids;

    while (my $arg = shift @ARGV) {
        if (($arg eq '-h') || ($arg eq '--help') || ($arg eq '-u') || ($arg eq '--usage') | ($arg eq '-?')) {
            usage();
            exit 0;
        } elsif (($arg eq '-f') || ($arg eq '--file')) {
            my $file = shift @ARGV;
            map {$search_ids{$_} = undef} @{read_file_list($file)};
        } else {
            if ($arg =~ /(\w{6}-\w{6}-\w{2})/) {
                $search_ids{$arg} = undef;
            }
        }

    }

    if (scalar keys %search_ids) {
        print "Searching for ".scalar(keys %search_ids)." Exim IDs.\n\n";

        my $log_list = MK::Utility::LogList->new(
            directory => $EMI::Config::log_directory,
            pattern => $EMI::Config::log_pattern,
            chrono => 1,
        );

        my @logs = @{$log_list->get_list};
        my @found_ids;

        while (scalar(@logs) && scalar(keys %search_ids)) { # do we still have an ids to search for and logs to search for them in?
            my $log = shift @logs;
            my $found = 0;
            my $prog_cnt = 1000;
            print "Checking log: $log\n";

            my $log_reader = MK::Utility::LogReader->new( file => $log );
            my $progress = MK::Utility::Progress->new( end => $log_reader->size );

            $log_reader->read_all_lines(sub {
                if (/(\w{6}-\w{6}-\w{2})\s<=/) {
                    if (exists($search_ids{"$1"})) {
                        my $entry = EMI::LogEntry->new(entry => $_);
                        push (@found_ids, $entry);

                        delete($search_ids{"$1"});
                        $found++;

                        unless(scalar(keys %search_ids)) { # don't bother checking rest of log if no more id's
                            print "\nNo more IDs to look for.";
                            return 1;
                        }
                    }
                }

                if ($prog_cnt <= 0) {
                    $progress->update($log_reader->tell);
                    $prog_cnt = 1000;
                }

                $prog_cnt--;
                return undef;
            });

            $progress->update($log_reader->tell);

            print "\n";

            if ($found) {
                print 'Found '.$found." IDs in log file.\n";
            }
        }

        print "\n";

        if (@found_ids) {
            print 'Found '.scalar(@found_ids)." IDs in total.\n";
        }

        if (scalar keys %search_ids) {
            print 'There were '.scalar(keys %search_ids)." IDs not found.\n";
        }

        print "\n";

        for my $found (@found_ids) {
            display_entry($found);
        }
        
    } else {
        print STDERR "It does not appear that you supplied me with a valid Exim ID or filename.\n";
        exit 1;
    }

    exit 0;
}

sub read_file_list {
    my ($file) = @_;
    if (open(my $fh, $file)) {
        my @ids = map {/(\w{6}-\w{6}-\w{2})/ ? $1 : ()} <$fh>;
        close($fh);
        return \@ids;
    } else {
        print STDERR "Failed to open file '$file': $!\n";
        return [];
    }
}

sub display_entry {
    my ($entry) = @_;
    print $entry->TO_TEXT;
}

sub usage {
    print "Usage:\n\n";
    print "emi.pl [options] <1st eximid> <2nd eximid> ... <nth eximid>\n\n";
    print "Options:\n\n";
    print "--usage\n-?\n\nDisplay this help message.\n\n";
    print "--file <filename>\n-f <filename>\n\nProcess list of eximids contained in a file.\n\n";
}

__PACKAGE__->main unless caller; # call main function unless we were included as module

1;    
} # end main package


BEGIN { # begin package EMI::LogEntry
package EMI::LogEntry;

use strict;
use warnings;
use Carp;

use Time::Local;

my @time = localtime(time);
my $offset = timegm(@time) - timelocal(@time);

sub new {
    my ($class, %params) = @_;
    $params{entry} or croak "No file specified.";

    my $self = build_entry($params{entry});

    bless($self, $class);
    return $self;
}

sub build_entry {
    my ($entry) = @_;
    my $parsed = {entry => $entry};

    if ($entry =~ /<=/) { # is this an outbound email?
        $parsed->{direction} = 'outbound';

        if ($entry =~ /^(\d{4})\-(\d{2})\-(\d{2})\s(\d{2}):(\d{2}):(\d{2})\s(\[\d{1,8}\]\s)?(\w{6}\-\w{6}\-\w{2})\s<=\s/gc) { # grab time and id
            $parsed->{time} = timegm($6, $5, $4, $3, $2 - 1, $1) - $offset;      
            $parsed->{id} = $8;
            
            if ($entry =~ /\G((?:[^\s]*[\+@][^\s]*)|(?:<>))\s/gc) { # do we have a sender?
                $parsed->{sender} = $1;
                
                if ($entry =~ /\G(H=([^\s]*\s)?((\([^\(]*\)\s)?\[([^\]]*)\]))(:\d{1,5})?\s/gc) { # is this a remote user?
                    $parsed->{helo} = $1;
                    $parsed->{ip} = $5;
                    
                    if ($entry =~ /(A=(fixed|courier|dovecot)_(login|plain):(([^\+@\s]*)|([^\+@\s]*[\+@]([^\+@\s]*))))\s/) { # do we have a valid authenticator?
                        $parsed->{auth} = $1;
                    } 
                } elsif ($entry =~ /\G(U=([^\s]*))\s/gc) { # is this a local user?
                    $parsed->{auth} = $1;
                } elsif ($entry =~ /\GR=(\w{6}\-\w{6}\-\w{2})\s/gc) { # is this a returned mail?
                    $parsed->{bounce_id} = $1;
                }                   
            }
        }

        $entry =~ /^(.*from\s<[^>]*>\sfor\s)(.*)$/;
        my @emails = split(/\s/, $2);
        $parsed->{recipients} = \@emails;

        if ($entry =~ /(T=\"([^\"]*)\")/) {
            $parsed->{subject} = $2;
        } else {
            $parsed->{subject} = 'Blank Subject';
        }
    }

    return $parsed;
}

sub TO_TEXT {
    my $self = shift;
    return join ('', map {
        $self->{id}.': '.$self->{sender}.' ('.($self->{auth} ? $self->{auth}: 'no auth').') -> '.$_."\n"
    } @{$self->{recipients}});
}

1;
} # end package EMI::LogEntry


BEGIN { ## begin class MK::Utility::LogReader
package MK::Utility::LogReader;

use strict;
use warnings;
use Carp;

use FileHandle;
use File::stat;

sub new {
    my ($class, %params) = @_;
    $params{file} or croak "No file specified.";
    
    my ($fh, $fs, $is_gz);
    
    if ($params{file} =~ /^.*?\.gz$/) {
        $is_gz = 1;
        open($fh, "-|", "zcat " . $params{file}) or croak "Failed to open compressed file '$params{file}'.";
        my $gziplist=`gzip -l $params{file}`;
        
        if ($gziplist =~ /.\n\s*\d*\s*(\d*)/) {
            $fs=$1;
        } else {
            croak "Failed to get gzip header for '$params{file}'.";
        }
    } else {
        $is_gz = 0;
        $fh = FileHandle->new($params{file}, '<') or croak "Failed to open file '$params{file}'.";
        $fs = stat($params{file})->size
    }
    
    my $self = {
        filename => $params{file},
        handle => $fh,
        size => $fs,
        is_gz => $is_gz,
        bytes_read => 0,
    };
    
    bless($self, $class);
    return $self;
}

sub close {
    my $self = shift;
    close($self->{handle});
}

sub read_line {
    my $self = shift;
    my $fh = $self->{handle};
    my $line = <$fh>;
    
    if ($line) {
        $self->{bytes_read} += length($line);
    }

    return $line;
}

sub read_all_lines (&) {
    my $self = shift;
    my $proc_func = shift;
    my $fh = $self->{handle};

    while ($_ = <$fh>) {
        $self->{bytes_read} += length($_);
        if ($proc_func->()) {
            last;
        }
    }
}

sub size {
    my $self = shift;
    return $self->{size};
}

sub tell {
    my $self = shift;
    return $self->{is_gz} ?  $self->{bytes_read} : tell($self->{handle});
}

sub is_gz {
    my $self = shift;
    return $self->{is_gz};
}

1;
} ## end class MK::Utility::LogReader


BEGIN { ## begin class MK::Utility::LogList
package MK::Utility::LogList;

use strict;
use warnings;
use Carp;

use File::Spec;
use File::stat;

sub new {
    my ($class, %params) = @_;
    
    $params{directory} or croak "No directory specified.";
    $params{pattern} or croak "No pattern specified.";
    
    my $self = {
        directory => $params{directory},
        pattern => $params{pattern},
        chrono => exists ($params{chrono}) ? $params{chrono} : 1,
        files => [],
    };
    
    opendir(my $dh, $self->{directory}) or croak "Failed to open directory: $self->{directory}";
    
    while(my $file = readdir($dh)) {
        if ($file =~ /$self->{pattern}/) {
            push (@{$self->{files}}, File::Spec->rel2abs($file, $self->{directory}));
        }
    }
    
    closedir($dh);
    
    if ($self->{chrono}) {
        @{$self->{files}} = sort {stat($b)->mtime <=> stat($a)->mtime} @{$self->{files}};
    }
    
    bless($self, $class);
    return $self;
}

sub get_list {
    my $self = shift;
    return $self->{files};
}

1;
} ## end class MK::Utility::LogList


BEGIN { ## begin class MK::Utility::Progress
package MK::Utility::Progress;

use strict;
use warnings;
use Carp;

sub new {
    my ($class, %params) = @_;
    $params{end} or croak "No end specified.";
    
    my $self = {
        end => $params{end},
        value => 0,
        percentage => 0,
    };
    
    bless($self, $class);
    return $self;
}

sub update {
    my ($self, $value) = @_;
    my $percentage = int(($value / $self->{end}) * 100);
    
    $| = 1; # disable output buffering
    
    if ($percentage != $self->{percentage}) {
        print "\r               \rProgress: $percentage%"; #replace prev line with new progress
    }
    
    $self->{value} = $value;
    $self->{percentage} = $percentage;
}

1;
} ## end class MK::Utility::Progress
