TABLE OF CONTENTS
- 1. IRCParser/new
- 2. IRCParser/parse_logfile
- 3. IRCParser/process_directory
- 4. IRCParser/process_logfile
- 5. IRCParser/register_callbacks
- 6. IRCParser/set_regexps
- 7. IRCParser/start_output_module
- 8. IRCParser/stop_output_module
IRCParser/new [ Functions ]
FUNCTION
Construct a new IRCParser object.
INPUTS
options -- hash with options.
SOURCE
sub IRCParser::new { my $type = shift; my %options = @_; my $self = { 'channel' => $options{'channel'}, 'directory' => $options{'directory'}, 'logfile' => $options{'logfile'}, 'input-format' => $options{'input-format'}, 'output-handler' => $options{'output-handler'}, 'options' => {}, 'loaded_formats' => {}, 'loaded_handlers' => {}, 'regexps' => {}, 'callbacks' => {} }; bless ($self, $type); return $self; }
IRCParser/parse_logfile [ Functions ]
FUNCTION
Parse logfile line by line.
INPUTS
logfile -- name of logfile to parse.
SOURCE
sub IRCParser::parse_logfile { my $self = shift; my $logfile = shift; print "INFO: Parsing logfile $logfile\n"; # extract YYYYMMDD components from filename my ($year, $month, $day) = $self->extract_yyyymmdd($logfile); # open logfile unless (open LOG, "< $logfile") { if ($self->{directory}) { print "WARNING: Can not open logfile $logfile, do we have read permissions?\n"; return; } else { die "ERROR: Can not open logfile $logfile, aborting.\n" } } my ($line_no, $type, $time, $nick, $content); my $timestamp = $self->{'timestamp'}; my %regexps = %{$self->{'regexps'}}; my $callbacks = $self->{'callbacks'}; $line_no = 1; # process each line from the logfile while (<LOG>) { chomp; # identify line and split if match if (/${timestamp}$regexps{'say'}/) { $type = Common::SAY; $time = $1; ($nick, $content) = $callbacks->{handle_say}(); } elsif (/${timestamp}$regexps{'join'}/) { $type = Common::JOIN; $time = $1; ($nick, $content) = $callbacks->{handle_join}(); } elsif (/${timestamp}$regexps{'left'}/) { $type = Common::LEFT; $time = $1; ($nick, $content) = $callbacks->{handle_left}(); } elsif (/${timestamp}$regexps{'left_reason'}/) { $type = Common::LEFT; $time = $1; ($nick, $content) = $callbacks->{handle_left_reason}(); } elsif (/${timestamp}$regexps{'quit'}/) { $type = Common::QUIT; $time = $1; ($nick, $content) = $callbacks->{handle_quit}(); } elsif (/${timestamp}$regexps{'quit_reason'}/) { $type = Common::QUIT; $time = $1; ($nick, $content) = $callbacks->{handle_quit_reason}(); } elsif (/${timestamp}$regexps{'action'}/) { $type = Common::ACTION; $time = $1; ($nick, $content) = $callbacks->{handle_action}(); } elsif (/${timestamp}$regexps{'mode_change'}/) { $type = Common::MODE; $time = $1; ($nick, $content) = $callbacks->{handle_mode_change}(); } elsif (/${timestamp}$regexps{'kick'}/) { $type = Common::KICK; $time = $1; ($nick, $content) = $callbacks->{handle_kick}(); } elsif (/${timestamp}$regexps{'nick_change'}/) { $type = Common::NICK; $time = $1; ($nick, $content) = $callbacks->{handle_nick_change}(); } elsif (/${timestamp}$regexps{'topic_change'}/) { $type = Common::TOPIC; $time = $1; ($nick, $content) = $callbacks->{handle_topic_change}(); } else { Debug::print("NOTICE: No match for line: $line_no"); } # process event if (defined($type)) { $callbacks->{handle_event}( $year, $month, $day, $line_no, $type, $time, $nick, $content ); } $type = undef; $line_no++; } close LOG; }
IRCParser/process_directory [ Functions ]
FUNCTION
Wrapper function for parse_logfile.
INPUTS
directory -- name of directory to scan for logfiles. channel -- name of channel to use as search criteria for scan.
SOURCE
sub IRCParser::process_directory { my $self = shift; my $directory = $self->{'directory'} || shift; my $channel = $self->{'channel'} || shift; my $prefix = "$channel.log"; print "INFO: Processing directory $directory\n"; die "ERROR: Directory $directory does not exists, aborting.\n" if -f $directory; opendir(DIR, $directory) or die "ERROR: Can not open directory $directory\n"; # enum logfiles my @logfiles = grep { /^[^\.]/ && /$prefix/i && -f "$directory/$_" } readdir(DIR); # foreach logfile call process_logfile foreach my $logfile (sort @logfiles) { $self->process_logfile("$directory$logfile"); } closedir(DIR); }
IRCParser/process_logfile [ Functions ]
FUNCTION
Wrapper function for parse_logfile.
INPUTS
logfile -- name of logfile to parse. callbacks -- stack of event handlers from output handler.
SOURCE
sub IRCParser::process_logfile { my $self = shift; my $logfile = $self->{'logfile'} || shift; my $callbacks = $self->{'callbacks'}; # only abort if a logfile does not exist when processing a single logfile if (!-f $logfile) { if ($self->{directory}) { print "WARNING: Logfile $logfile does not exists.\n" } else { die "ERROR: Logfile $logfile does not exists, aborting.\n" } } $self->parse_logfile($logfile); }
IRCParser/register_callbacks [ Functions ]
FUNCTION
Hooks event handlers from modules into callback stack.
INPUTS
module -- name of module to load event handlers from.
SOURCE
sub IRCParser::register_callbacks { my $self = shift; my $module = shift; my $current = $self->{'callbacks'}; my $temp; my $new; eval <<EVAL_END; \$new = ${module}::get_callbacks(); EVAL_END # push old @$temp{keys %$current} = values %$current; # push new @$temp{keys %$new} = values %$new; $self->{'callbacks'} = $temp; }
IRCParser/set_regexps [ Functions ]
FUNCTION
Retrieve regular expressions from input format module.
INPUTS
module -- name of module to load regular expressions from.
SOURCE
sub IRCParser::set_regexps { my $self = shift; my $format = $self->{'input-format'} || shift; my ($timestamp, %regexps); # conversion my $module = $format; eval <<EVAL_END; (\$timestamp, \%regexps) = ${module}::get_regexps(); EVAL_END # set regular expressions $self->{'timestamp'} = $timestamp; %{$self->{'regexps'}} = %regexps; }
IRCParser/start_output_module [ Functions ]
FUNCTION
Initialize output module
INPUTS
settings -- settings needed to set up the output module.
SOURCE
sub IRCParser::start_output_module { my $self = shift; my %settings = @_; my $callbacks = $self->{'callbacks'}; $callbacks->{set_settings}(%settings); $callbacks->{verify_settings}(); # do initialization for output module if (!$callbacks->{init}()) { die "ERROR: Could not initialize output module, aborting.\n"; } }
IRCParser/stop_output_module [ Functions ]
FUNCTION
Deinitialize output module
SOURCE
sub IRCParser::stop_output_module { my $self = shift; my $callbacks = $self->{'callbacks'}; # do deinitialization for output module if (!$callbacks->{deinit}()) { print "WARNING: Could not deinit output module.\n"; } }