# This is a basic logfile parser for the deride bot. 
# derived from pisg parser template.
#
# Licensed under the GPL.
# Upstream Author is Morten Brix Pedersen <morten@wtf.dk>
# Basic adaptations for Deride by Grant Bowman <grantbow@grantbow.com>  Setp 25, 2002
## ;;; 2002-11-2x T04:20:24-0500     D. Goel
## thirdline code modified by D. Goel and Grant Bowman 
## to identify topic changes. 

package Pisg::Parser::Format::erbot;

use strict;
$^W = 1;

# The 3 variables in the new subrountine, 'normalline', 'actionline' and
# 'thirdline' represents regular expressions for extracting information from
# the logfile. normalline is for lines where the person merely said
# something, actionline is for lines where the person performed an action,
# and thirdline matches everything else, including things like kicks, nick
# changes, and op grants.  See the thirdline subroutine for a list of
# everything it should match.

sub new
  {
    my ($type, %args) = @_;
    my $self = {
                cfg => $args{cfg},
                normalline => '^(\d+)\.(\d+):\d+:\d+ <([^>]+)> (.*)',
                actionline => '^(\d+)\.(\d+):\d+:\d+ \* ([^ ]+) (.*)',
                thirdline => '^(\d+)\.(\d+):(\d+):\d+ \*\*\* ([^ ]+) \(.+?\) (has|is) (left channel|set the topic|changed mode|now known as|quit|kicked) (.*)',
                ##^[@+ ]?.*info@(\d+):(\d+).*\}.(\w+) .*topic.*to:(.*)',
                ##thirdline => '(.*)}\ [@+ ]?([^>]+) changed\ the\ topic\ to:(.*)',
                ##thirdline => '^.*info.*',
                # the third line and the corresponding function need major help.
               };

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

# Parse a normal line - returns a hash with 'hour', 'nick' and 'saying'
sub normalline
  {
    my ($self, $line, $lines) = @_;
    my %hash;

    if ($line =~ /$self->{normalline}/o) {

      # Most log formats are regular enough that you can just match the
      # appropriate things with parentheses in the regular expression.

      $hash{nick}   = $3;
      $hash{hour}   = $2;
      $hash{saying} = $4;

      return \%hash;
    } else {
      return;
    }
  }

# Parse an action line - returns a hash with 'hour', 'nick' and 'saying'
sub actionline
  {
    my ($self, $line, $lines) = @_;
    my %hash;

    if ($line =~ /$self->{actionline}/o) {
      ##print "Reached 1";
      # Most log formats are regular enough that you can just match the
      # appropriate things with parentheses in the regular expression.
      $hash{nick}   = $3;
      $hash{hour}   = $2;
      $hash{saying} = $4;

      return \%hash;
    } else {
      return;
    }
  }

# Parses the 'third' line - (the third line is everything else, like
# topic changes, mode changes, kicks, etc.)
# thirdline() has to return a hash with the following keys, for
# every format:
#   hour            - the hour we're in (for timestamp logging)
#   min             - the minute we're in (for timestamp logging)
#   nick            - the nick
#   kicker          - the nick which kicked somebody (if any)
#   newtopic        - the new topic (if any)
#   newmode         - deops or ops, must be '+o' or '-o', or '+ooo'
#   newjoin         - a new nick which has joined the channel
#   newnick         - a person has changed nick and this is the new nick
#
# The hash may also have a "repeated" key indicating the number of times
# the line was repeated. (Used by eggdrops log for example.)
sub thirdline
  {
    my ($self, $line, $lines) = @_;
    my %hash;
    #print "reeached here third";

     if ($line =~ /$self->{thirdline}/o) {

      $hash{hour} = $2;
      $hash{min}  = $3;

      if ($6 eq 'kicked') {
        $hash{kicker} = $4;
        $7 =~ /^([\S]+) off .+?: (.*)/;
        $hash{kicktext} = $2;
        $hash{nick} = $1;
      }
      elsif ($6 eq 'set the topic') {
        $hash{nick} = $4;
        #print $7;
        $7 =~ /for #.+: "(.*)"/;
        $hash{newtopic} = $1;
      }
      elsif ($6 eq 'changed mode') {
        $hash{nick} = $4;
        $7 =~ /for #.+ to (.+)/;
        $1 =~ /(\+[\S]+) (.*)/;
        $hash{modechanges} = $2;
        $hash{newmode} = $1;
      }
#       elsif ($5 eq 'joined') {
#         $hash{newjoin} = $3;
# 
#       } elsif (($3.$4) eq 'Nickchange:') {
#         $hash{nick} = $5;
#         $7 =~ /([\S]+)/;
#         $hash{newnick} = $1;
# 
#       } elsif (($3.$4.$5) eq 'Lastmessagerepeated') {
#         $hash{repeated} = $6;
#       }
# 
#       $hash{nick} =~ /([^!]+)/;
#       $hash{nick} = $1;

      return \%hash;

     } else {
       return;
    }
  }

1;

