Skip to content

Instantly share code, notes, and snippets.

@aufflick
Created April 19, 2011 02:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aufflick/926703 to your computer and use it in GitHub Desktop.
Save aufflick/926703 to your computer and use it in GitHub Desktop.
Quick hack to provide bot access to IRC history logs
#!/opt/bin/perl -w
package IRC::Bot2;
use strict;
use warnings;
use Moose;
extends 'IRC::Bot';
use IRC::Bot::Log::Extended;
use POE;
after 'bot_start' => sub {
my $self = shift;
no warnings;
$IRC::Bot::log = IRC::Bot::Log::Extended->new(
Path => $self->{LogPath},
split_channel => 1,
split_day => 1,
);
};
sub on_dcc_start
{
my ( $self, $kernel, $id, $who, $type ) = @_[ OBJECT, KERNEL, ARG0, ARG1, ARG2 ];
my $nick = ( split m/!/, $who )[0];
if ( $type eq 'CHAT' )
{
$kernel->post( "bot", 'dcc_chat', $id, "Available commands:" );
$kernel->post( "bot", 'dcc_chat', $id, " today on #channel /* this will give you today's entire log for that channel */" );
}
else
{
$kernel->post( "bot", 'dcc_close', $id );
}
}
sub on_dcc_chat {
my ( $self, $kernel, $id, $who, $msg ) = @_[ OBJECT, KERNEL, ARG0, ARG1, ARG3 ];
my $nick = ( split m/!/, $who )[0];
# should auth...
my $time = sprintf( "%02d:%02d", ( localtime( time() ) )[ 2, 1 ] );
if ($msg =~ /today on #([a-zA-Z_]+)/)
{
my @ymd = ( localtime( time() ))[5, 4, 3];
my $logfile = $self->{LogPath} . $1 . "_" . sprintf( '%4d%02d%02d', $ymd[0] + 1900, $ymd[1] + 1, $ymd[2] ) . ".log";
my $fh;
if (-f $logfile && open( $fh, $logfile))
{
while (my $log_line = <$fh>)
{
chomp $log_line;
$kernel->post( "bot", 'dcc_chat', $id, $log_line );
}
$kernel->post( "bot", 'dcc_close', $id );
}
else
{
$kernel->post( "bot", 'dcc_chat', $id, "unable to open $logfile" );
}
}
else
{
$kernel->post( "bot", 'dcc_chat', $id, "you said: $msg" );
}
}
package main;
my $bot = IRC::Bot2->new(
Debug => 0,
Nick => 'dexter',
Ircname => 'My IRC Bot',
NSPass => '****',
Server => 'localhost',
Password => '****',
Admin => 'tehAdmin',
Apass => '****',
Channels => [ '#My', '#awsm', '#channels' ],
LogPath => '/path/to/irc-logs/',
);
$bot->daemon();
$bot->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment