From 9d36248a712e98424d36dbb279975c0c776485ae Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Sun, 12 Sep 2010 11:39:13 +0000 Subject: [PATCH 01/53] Added support for logging and storage of data from multiple servers. --- README | 6 +++--- bot.conf | 20 +++++++++++++++----- ilbot.sql | 1 + ilbot2.pl | 50 +++++++++++++++++++++++++++++--------------------- 4 files changed, 48 insertions(+), 29 deletions(-) diff --git a/README b/README index 156190d..3c2b1b6 100644 --- a/README +++ b/README @@ -1,5 +1,5 @@ -The documentation can be found at +This is an extension of ilbot, whose homepage is http://moritz.faui2k3.org/en/ilbot -However it is very incomplete, in case you really want to use it, ask moritz_ -on #perl6 +This extension adds the following new features: + * Support for logging multiple servers from a single instance. diff --git a/bot.conf b/bot.conf index 755f50b..c953d10 100644 --- a/bot.conf +++ b/bot.conf @@ -1,5 +1,15 @@ -# Configuration for the bot -NICK = irclogbot_backup -SERVER = irc.freenode.net -# multiple channels on the same server should be separated by whitespace -CHANNEL = \#perl6 \#bioclipse +# Server configuration for the bot +servers { + freenode = { + NICK = ilbot6; # This is the default value + SERVER = irc.freenode.net; # This is the default value + PORT = 6667; # This is the default value + # multiple channels on the same server should be separated by whitespace + CHANNEL = '#openhatch #perl6'; + }; + # Example of a second configuration + mozilla = { + SERVER = irc.mozilla.org; + CHANNEL = '#nightingale #songbird'; + }; +} diff --git a/ilbot.sql b/ilbot.sql index 0845e15..8ee4164 100644 --- a/ilbot.sql +++ b/ilbot.sql @@ -24,6 +24,7 @@ SET @saved_cs_client = @@character_set_client; SET character_set_client = utf8; CREATE TABLE `irclog` ( `id` int(11) NOT NULL auto_increment, + `server` varchar(30), `channel` varchar(30) default NULL, `day` char(10) default NULL, `nick` varchar(40) default NULL, diff --git a/ilbot2.pl b/ilbot2.pl index cbad6a3..9d349be 100755 --- a/ilbot2.pl +++ b/ilbot2.pl @@ -2,7 +2,7 @@ use warnings; use strict; use lib 'lib'; -use Config::File; +use Config::Scoped; use Bot::BasicBot 0.81; use Carp qw(confess); @@ -18,15 +18,15 @@ package IrcLogBot; sub prepare { my $dbh = shift; - return $dbh->prepare("INSERT INTO irclog (channel, day, nick, timestamp, line) VALUES(?, ?, ?, ?, ?)"); + return $dbh->prepare("INSERT INTO irclog (server, channel, day, nick, timestamp, line) VALUES(?, ?, ?, ?, ?, ?)"); } my $q = prepare($dbh); sub dbwrite { - my ($channel, $who, $line) = @_; + my ($server, $channel, $who, $line) = @_; # mncharity aka putter has an IRC client that prepends some lines with # a BOM. Remove that: $line =~ s/\A\x{ffef}//; - my @sql_args = ($channel, gmt_today(), $who, time, $line); + my @sql_args = ($server, $channel, gmt_today(), $who, time, $line); if ($dbh->ping){ $q->execute(@sql_args); } else { @@ -41,14 +41,14 @@ package IrcLogBot; sub said { my $self = shift; my $e = shift; - dbwrite($e->{channel}, $e->{who}, $e->{body}); + dbwrite($self->{server}, $e->{channel}, $e->{who}, $e->{body}); return undef; } sub emoted { my $self = shift; my $e = shift; - dbwrite($e->{channel}, '* ' . $e->{who}, $e->{body}); + dbwrite($self->{server}, $e->{channel}, '* ' . $e->{who}, $e->{body}); return undef; } @@ -56,21 +56,21 @@ package IrcLogBot; sub chanjoin { my $self = shift; my $e = shift; - dbwrite($e->{channel}, '', $e->{who} . ' joined ' . $e->{channel}); + dbwrite($self->{server}, $e->{channel}, '', $e->{who} . ' joined ' . $e->{channel}); return undef; } sub chanquit { my $self = shift; my $e = shift; - dbwrite($e->{channel}, '', $e->{who} . ' left ' . $e->{channel}); + dbwrite($self->{server}, $e->{channel}, '', $e->{who} . ' left ' . $e->{channel}); return undef; } sub chanpart { my $self = shift; my $e = shift; - dbwrite($e->{channel}, '', $e->{who} . ' left ' . $e->{channel}); + dbwrite($self->{server}, $e->{channel}, '', $e->{who} . ' left ' . $e->{channel}); return undef; } @@ -94,7 +94,7 @@ package IrcLogBot; sub topic { my $self = shift; my $e = shift; - dbwrite($e->{channel}, "", 'Topic for ' . $e->{channel} . ' is now ' . $e->{topic}); + dbwrite($self->{server}, $e->{channel}, "", 'Topic for ' . $e->{channel} . ' is now ' . $e->{topic}); return undef; } @@ -103,7 +103,7 @@ package IrcLogBot; my($old, $new) = @_; foreach my $channel ($self->_channels_for_nick($new)) { - dbwrite($channel, "", $old . ' is now known as ' . $new); + dbwrite($self->{server}, $channel, "", $old . ' is now known as ' . $new); } return undef; @@ -112,7 +112,7 @@ package IrcLogBot; sub kicked { my $self = shift; my $e = shift; - dbwrite($e->{channel}, "", $e->{kicked} . ' was kicked by ' . $e->{who} . ': ' . $e->{reason}); + dbwrite($self->{server}, $e->{channel}, "", $e->{kicked} . ' was kicked by ' . $e->{who} . ': ' . $e->{reason}); return undef; } @@ -124,14 +124,18 @@ package IrcLogBot; package main; -my $conf = Config::File::read_config_file(shift @ARGV || "bot.conf"); -my $nick = shift @ARGV || $conf->{NICK} || "ilbot6"; -my $server = $conf->{SERVER} || "irc.freenode.net"; -my $port = $conf->{PORT} || 6667; -my $channels = [ split m/\s+/, $conf->{CHANNEL}]; - -my $bot = IrcLogBot->new( - server => $server, +my $conf = Config::Scoped->new( file => shift @ARGV || "bot.conf")->parse; +die "Could not read config!\n" unless ref $conf; +my $servers = $conf->{'servers'}; +foreach my $server (keys %$servers) +{ + my $nick = $servers->{$server}->{'NICK'} || "ilbot6"; + my $address = $servers->{$server}->{'SERVER'} || "irc.freenode.net"; + my $port = $servers->{$server}->{'PORT'} || 6667; + my $channels = [ split m/\s+/, $servers->{$server}->{'CHANNEL'}]; + + my $bot = IrcLogBot->new( + server => $address, port => $port, channels => $channels, nick => $nick, @@ -139,7 +143,11 @@ package main; username => "bot", name => "irc log bot, http://moritz.faui2k3.org/en/ilbot", charset => "utf-8", + no_run => 1, ); -$bot->run(); + $bot->run(); +} +use POE; +$poe_kernel->run(); # vim: ts=4 sw=4 expandtab From 937ec7325ed99948552b0c2dd98cbba2f46a60b5 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Mon, 13 Sep 2010 05:28:16 +0000 Subject: [PATCH 02/53] Update towards viewing of logs from multiple servers. Currently broken. --- cgi/channel-index.pl | 21 +++++++----- cgi/index.pl | 10 +++--- cgi/server-index.pl | 58 +++++++++++++++++++++++++++++++++ cgi/template/channel-index.tmpl | 4 +-- cgi/template/index.tmpl | 6 ++-- cgi/template/server-index.tmpl | 25 ++++++++++++++ 6 files changed, 105 insertions(+), 19 deletions(-) create mode 100755 cgi/server-index.pl create mode 100644 cgi/template/server-index.tmpl diff --git a/cgi/channel-index.pl b/cgi/channel-index.pl index 1c50542..b85e956 100755 --- a/cgi/channel-index.pl +++ b/cgi/channel-index.pl @@ -15,6 +15,7 @@ sub go { my $q = new CGI; + my $server = $q->url_param('server'); my $channel = $q->url_param('channel'); print "Content-Type: text/html; charset=utf-8\n\n"; @@ -23,7 +24,7 @@ sub go { my $data = $cache->get($cache_name); if (! defined $data) { - $data = get_channel_index($channel); + $data = get_channel_index($server, $channel); $cache->set($data, '2 hours'); } @@ -31,16 +32,17 @@ sub go { } sub test_calendar { + my $server = 'irc.freenode.net'; my $channel = '#parrotsketch'; - my $base_url = '/irclog/'; + my $base_url = '/'; my $dates = [qw( 2009-09-28 2009-09-30 2009-10-01 2009-10-02 2009-10-05 2009-10-12 )]; - print calendar_for_channel($channel, $dates, $base_url); + print calendar_for_channel($server, $channel, $dates, $base_url); } sub get_channel_index { - my $channel = shift; + my ($server, $channel) = @_; my $conf = Config::File::read_config_file('cgi.conf'); my $base_url = $conf->{BASE_URL} || q{/}; @@ -52,23 +54,24 @@ sub get_channel_index { # we are evil and create a calendar entry for month between the first # and last date my $dbh = get_dbh(); - my $get_dates = 'SELECT DISTINCT day FROM irclog WHERE channel = ? ORDER BY day'; - my $dates = $dbh->selectcol_arrayref($get_dates, undef, '#' . $channel); + my $get_dates = 'SELECT DISTINCT day FROM irclog WHERE server = ? AND channel = ? ORDER BY day'; + my $dates = $dbh->selectcol_arrayref($get_dates, undef, ($server, '#' . $channel)); + $t->param(SERVER => $server); $t->param(CHANNEL => $channel); $t->param(BASE_URL => $base_url); - $t->param(CALENDAR => calendar_for_channel($channel, $dates, $base_url)); + $t->param(CALENDAR => calendar_for_channel($server, $channel, $dates, $base_url)); return $t->output; } sub calendar_for_channel { - my ($channel, $dates, $base_url) = @_; + my ($server, $channel, $dates, $base_url) = @_; $channel =~ s/\A\#//smx; my (%months, %link); for my $date (@$dates) { my ($Y, $M, $D) = split '-', $date; - $link{$date} = "$base_url$channel/$date"; + $link{$date} = "$base_url$server/$channel/$date"; $months{"$Y-$M"}++; } diff --git a/cgi/index.pl b/cgi/index.pl index 747ac19..b186542 100755 --- a/cgi/index.pl +++ b/cgi/index.pl @@ -28,16 +28,16 @@ sub get_index { my $dbh = get_dbh(); my $conf = Config::File::read_config_file('cgi.conf'); - my $base_url = $conf->{BASE_URL} || q{/irclog/}; + my $base_url = $conf->{BASE_URL} || q{/}; - my $sth = $dbh->prepare("SELECT DISTINCT channel FROM irclog"); + my $sth = $dbh->prepare("SELECT DISTINCT server FROM irclog"); $sth->execute(); - my @channels; + my @servers; while (my @row = $sth->fetchrow_array()){ $row[0] =~ s/^\#//; - push @channels, { channel => $row[0] }; + push @servers, { server => $row[0] }; } my $template = HTML::Template->new( @@ -47,7 +47,7 @@ sub get_index { die_on_bad_params => 0, ); $template->param(BASE_URL => $base_url); - $template->param( channels => \@channels ); + $template->param( servers => \@servers ); return $template->output; diff --git a/cgi/server-index.pl b/cgi/server-index.pl new file mode 100755 index 0000000..18cb0f9 --- /dev/null +++ b/cgi/server-index.pl @@ -0,0 +1,58 @@ +#!/usr/bin/perl +use CGI::Carp qw(fatalsToBrowser); +use CGI; +use strict; +use warnings; +use Config::File; +use HTML::Template; +use lib 'lib'; +use IrcLog qw(get_dbh); +use IrcLog::WWW qw(http_header); + +use Cache::FileCache; + +print http_header(); +my $q = new CGI; +my $server = $q->url_param('server'); +my $cache = new Cache::FileCache( { + namespace => 'irclog', + } ); + +my $data; +$data = $cache->get($server); +if ( ! defined $data){ + $data = get_server_index($server); + $cache->set($server, $data, '5 hours'); +} +print $data; + +sub get_server_index { + my $server = shift; + my $dbh = get_dbh(); + + my $conf = Config::File::read_config_file('cgi.conf'); + my $base_url = $conf->{BASE_URL} || q{/}; + + my $sth = $dbh->prepare("SELECT DISTINCT channel FROM irclog WHERE server = $server"); + $sth->execute(); + + my @channels; + + while (my @row = $sth->fetchrow_array()){ + $row[0] =~ s/^\#//; + push @channels, { channel => $row[0] }; + } + + my $template = HTML::Template->new( + filename => 'template/server-index.tmpl', + loop_context_vars => 1, + global_vars => 1, + die_on_bad_params => 0, + ); + $template->param(BASE_URL => $base_url); + $template->param( server => $server ); + $template->param( channels => \@channels ); + + + return $template->output; +} diff --git a/cgi/template/channel-index.tmpl b/cgi/template/channel-index.tmpl index ccd1016..48ac4b3 100644 --- a/cgi/template/channel-index.tmpl +++ b/cgi/template/channel-index.tmpl @@ -13,8 +13,8 @@ -

#

-

Today

+

/#

+

Today

diff --git a/cgi/template/index.tmpl b/cgi/template/index.tmpl index ff88738..cbeaded 100644 --- a/cgi/template/index.tmpl +++ b/cgi/template/index.tmpl @@ -13,10 +13,10 @@

Search the logs

-

Channels

+

Servers

-