#! /usr/bin/perl -w

use strict;
use lib '/usr/share/perl5';

use Lire::DlfSchema;
use Lire::Syslog;
use Lire::Program qw/:msg :dlf/;

use vars qw/ $dlf_maker $dlflines $debug /;

sub print_dlf {
    my $dlf=$dlf_maker->($_[0]);
    print join( " ", @$dlf ), "\n";
    $dlflines++;
}

my $schema = eval { Lire::DlfSchema::load_schema( "msgstore" ) };
lr_err( "failed to load msgstore schema: $@" ) if $@;

$dlf_maker =
  $schema->make_hashref2asciidlf_func( qw/time localserver client_ip user protocol prot_cmd messages_downloaded bytes_downloaded stored_messages stored_size session_duration status/);

my $lines       = 0;
$dlflines       = 0;
my $errorlines  = 0;
my @server_msg  = ();
$debug          = 0;
my $syslog_parser = new Lire::Syslog;

my $parser = new Lire::Syslog;
init_dlf_converter( "msgstore" );
my $failed_line = undef;
my %data = ();
while ( <> ) {
    chomp;
    $lines++;

    my $rec = eval { $syslog_parser->parse( $_ ) };
    if ($@) {
        lr_warn( "line $. is an invalid syslog message: $@" );
        $errorlines++;
    }

    next unless defined $rec->{process} && $rec->{process} =~/^dbmail\//;

    my $pid     = $rec->{pid};
    my $line    = $rec->{content};

    my %dlf=();
    $dlf{time}          = $rec->{timestamp};
    $dlf{localserver}   = $rec->{hostname};
    $dlf{protocol}      = "pop" if ($rec->{process} eq "dbmail/pop3d");
    $dlf{protocol}      = "imap" if ($rec->{process} eq "dbmail/imap4");
    # yes, they might be uninitialized but will not anymore when they're
    # used later on:
    $dlf{client_ip}     = $data{$pid}{client_ip};
    $dlf{user}          = $data{$pid}{user};;

    if ($line=~/^handle_client..: incoming connection from \[([\d\.]+)\]$/) {
        $data{$pid}{client_ip}  = $1;
        $data{$pid}{time_start} = $rec->{timestamp};

        $dlf{client_ip} = $data{$pid}{client_ip};
        $dlf{user}      = "[unauthenticated]";
        $dlf{prot_cmd}  = "connect";
        print_dlf(\%dlf);
        next;
    }

    if ($line=~/^handle_client..: user [\w\d]+ logging out \[message=(\d+), octets=(\d+)\]/) {

        # ignore leftovers from previous logfiles
        next if (!defined $data{$pid}{client_ip});

        $data{$pid}{messages_start}     =0 if (!defined $data{$pid}{messages_start});
        $data{$pid}{bytes_start}        =0 if (!defined $data{$pid}{bytes_start});

        $data{$pid}{messages_stop}      = $1;
        $data{$pid}{bytes_stop} = $2;
        $data{$pid}{time_stop}  = $rec->{timestamp};

        $dlf{prot_cmd}          = "close";
        $dlf{messages_downloaded} = $data{$pid}{messages_start} - $data{$pid}{messages_stop};
        $dlf{bytes_downloaded}  = $data{$pid}{bytes_start} - $data{$pid}{bytes_stop};
        $dlf{session_duration}  = $data{$pid}{time_stop} - $data{$pid}{time_start};

        $dlf{stored_messages}   = $data{$pid}{messages_stop};
        $dlf{stored_size}       = $data{$pid}{bytes_stop};

        print_dlf(\%dlf);
        $data{$pid}=();
        next;
    }

    if ($line=~
        /^pop3..: user ([\w\d]+) logged in \[messages=(\d+), octets=(\d+)\]$/) {

        # ignore leftovers from previous logfiles
        next if (!defined $data{$pid}{client_ip});

        $data{$pid}{user}               = $1;
        $data{$pid}{messages_start}     = $2;
        $data{$pid}{bytes_start}        = $3;
        $data{$pid}{time_start}         = $rec->{timestamp};;

        $dlf{user}      = $data{$pid}{user};
        $dlf{prot_cmd}  = "login";
        print_dlf(\%dlf);

        next;
    }

    #
    # problemos et al
    #
    if ($line=~
        /^pop3..: user \[([\w\d]+)\] tried to login with wrong password/) {

        # ignore leftovers from previous logfiles
        next if (!defined $data{$pid}{client_ip});

        $dlf{user}      = $1;
        $dlf{prot_cmd}  = "badlogin";

        print_dlf(\%dlf);
        %data=();
        next;
    }

}

end_dlf_converter( $lines, $dlflines, $errorlines );

__END__


=pod

=head1 NAME

dbmail2dlf - convert DBMAIL IMAP or POP log files to Lire msgstore DLF format

=head1 SYNOPSIS

B<dbmail2dlf>

=head1 DESCRIPTION

This program converts DBMAIL ( http://www.dbmail.org/ ) syslog files generated
by the IMAP or POP services to the msgstore DLF.

DBMAIL is a group of programs that enable the possiblilty of storing and
retrieving mail messages from a (mysql) database 

=head1 LIMITATIONS

The DBMAIL IMAP logging is hopeless and not supported yet.

=head1 EXAMPLES

To process a log as produced by DBMAIL:

 $ dbmail2dlf < mail-log

dbmail2dlf will be rarely used on its own, but is more likely
called by lr_log2report:

 $ lr_log2report dbmail < /var/log/mail-log

=head1 SEE ALSO

nmsmmp2dlf(1), nmsstore2dlf(1), http://www.dbmail.org/

=head1 VERSION

$Id: dbmail2dlf.in,v 1.6 2004/10/13 10:17:28 vanbaal Exp $

=head1 AUTHORS

Edwin Groothuis <edwin@mavetju.org>

=head1 COPYRIGHT

Copyright (C) 2002 Edwin Groothuis <edwin@mavetju.org>

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

3. The name of the copyright holder may not be used to endorse or promote
products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.

=cut
