From 4b56427b97221b4f725b36e2dd6f414329c707a8 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sun, 24 Jan 2010 23:41:04 -0600 Subject: add logging plugin --- lib/Crawl/Bot/Plugin/Logging.pm | 133 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 lib/Crawl/Bot/Plugin/Logging.pm diff --git a/lib/Crawl/Bot/Plugin/Logging.pm b/lib/Crawl/Bot/Plugin/Logging.pm new file mode 100644 index 0000000..7f58fd0 --- /dev/null +++ b/lib/Crawl/Bot/Plugin/Logging.pm @@ -0,0 +1,133 @@ +package Crawl::Bot::Plugin::Logging; +use Moose; +extends 'Crawl::Bot::Plugin'; + +use autodie; + +has date => ( + is => 'ro', + isa => 'Str', + lazy => 1, + clearer => 'clear_date', + builder => '_build_date', +); + +sub _build_date { + my ($year, $month, $day) = (localtime)[5, 4, 3]; + $year += 1900; + $month++; + return sprintf '%04d%02d%02d', $year, $month, $day; +} + +has logfile => ( + is => 'ro', + isa => 'FileHandle', + lazy => 1, + clearer => 'clear_logfile', + default => sub { + my $self = shift; + my $filepath = File::Spec->catfile($self->logfilepath, ($self->bot->channels)[0] . '-' . $self->date . '.log'); + open my $fh, '>>', $filepath; + $fh->autoflush(1); + return $fh; + } +); + +has logfilepath => ( + is => 'ro', + isa => 'Str', + lazy => 1, + default => sub { + my $self = shift; + my $path = File::Spec->catdir($self->data_dir, 'log'); + mkdir $path unless -d $path; + return $path; + }, +); + +before logfile => sub { + my $self = shift; + if ($self->date ne $self->_build_date) { + $self->clear_date; + $self->clear_logfile; + } +}; + +sub log_message { + my $self = shift; + my ($message) = @_; + + my $timestamp = sprintf '%02d:%02d:%02d', (localtime)[2, 1, 0]; + $self->logfile->print("$timestamp $message\n"); +} + +sub said { + my $self = shift; + my ($args) = @_; + + $self->log_message("<$args->{who}> $args->{body}"); + return; +} + +sub emoted { + my $self = shift; + my ($args) = @_; + + $self->log_message("* $args->{who} $args->{body}"); + return; +} + +sub chanjoin { + my $self = shift; + my ($args) = @_; + + $self->log_message("-!- $args->{who} has joined $args->{channel}"); + return; +} + +sub chanpart { + my $self = shift; + my ($args) = @_; + + $self->log_message("-!- $args->{who} has left $args->{channel}"); + return; +} + +sub nick_change { + my $self = shift; + my ($args) = @_; + + $self->log_message("-!- $args->{from} is now known as $args->{to}"); + return; +} + +sub kicked { + my $self = shift; + my ($args) = @_; + + $self->log_message("-!- $args->{kicked} was kicked from $args->{channel} by $args->{who} [$args->{reason}]"); + return; +} + +sub topic { + my $self = shift; + my ($args) = @_; + + if (defined($args->{who})) { + $self->log_message("-!- $args->{who} changed the topic of $args->{channel} to: $args->{topic}"); + } + else { + $self->log_message("-!- The topic of $args->{channel} is: $args->{topic}"); + } + return; +} + +sub userquit { + my $self = shift; + my ($args) = @_; + + $self->log_message("-!- $args->{who} has quit [$args->{body}]"); + return; +} + +1; -- cgit v1.2.3-54-g00ecf