From 4e748bf6684b36a73f73312c4c694dd6d5655e84 Mon Sep 17 00:00:00 2001 From: Ralf Ertzinger Date: Fri, 4 Feb 2011 22:30:39 +0100 Subject: [PATCH] Change quotesite to use BettIrssi, clean up module hierarchy for the plugins --- quotesite.pl | 136 +++++++++++++++++++++++---------------- quotesite/Base.pm | 21 +----- quotesite/BashOrgGrabber.pm | 6 +- quotesite/GermanBashGrabber.pm | 6 +- quotesite/GrabberBase.pm | 6 +- quotesite/PolitBashOrgGrabber.pm | 6 +- quotesite/QdbGrabber.pm | 6 +- quotesite/TotalBashGrabber.pm | 6 +- quotesite/TwitterGrabber.pm | 6 +- quotesite/iBashGrabber.pm | 6 +- 10 files changed, 105 insertions(+), 100 deletions(-) diff --git a/quotesite.pl b/quotesite.pl index a6414a9..94859d7 100644 --- a/quotesite.pl +++ b/quotesite.pl @@ -3,17 +3,28 @@ # (c) 2007-2008 by Ralf Ertzinger # licensed under GNU GPL v2 + +BEGIN { + # Get rid of a (possibly old) version of BettIrssi + # This is a hack to prevent having to reload irssi just + # because BettIrssi.pm changed + + delete($INC{'BettIrssi.pm'}); +} + use strict; use Irssi 20020324 qw (command_bind command_runsub signal_add_first signal_add_last window_find_refnum); use vars qw($VERSION %IRSSI); use XML::Simple; use Data::Dumper; use File::Spec; +use BettIrssi 101 qw(_bcb _bcs); my @grabbers; my $conf; my $conffile = File::Spec->catfile(Irssi::get_irssi_dir(), 'quotesite.xml'); my $plugindir = File::Spec->catfile(Irssi::get_irssi_dir(), 'scripts', 'quotesite'); +my @outputstack = (undef); my $PARAMS = { }; @@ -23,74 +34,79 @@ my $PARAMS = { my $debug = 0; # "message public", SERVER_REC, char *msg, char *nick, char *address, char *target -signal_add_last("message public" => sub {check_for_link(\@_,1,4,2,0);}); +signal_add_last(_bcs("message public" => sub {check_for_link(@_)})); # "message own_public", SERVER_REC, char *msg, char *target -signal_add_last("message own_public" => sub {check_for_link(\@_,1,2,-1,0);}); +signal_add_last(_bcs("message own_public" => sub {check_for_link(@_)})); # "message private", SERVER_REC, char *msg, char *nick, char *address -signal_add_last("message private" => sub {check_for_link(\@_,1,-1,2,0);}); +signal_add_last(_bcs("message private" => sub {check_for_link(@_)})); # "message own_private", SERVER_REC, char *msg, char *target, char *orig_target -signal_add_last("message own_private" => sub {check_for_link(\@_,1,2,-1,0);}); +signal_add_last(_bcs("message own_private" => sub {check_for_link(@_)})); # "message irc action", SERVER_REC, char *msg, char *nick, char *address, char *target -signal_add_last("message irc action" => sub {check_for_link(\@_,1,4,2,0);}); +signal_add_last(_bcs("message irc action" => sub {check_for_link(@_)})); # "message irc own_action", SERVER_REC, char *msg, char *target -signal_add_last("message irc own_action" => sub {check_for_link(\@_,1,2,-1,0);}); +signal_add_last(_bcs("message irc own_action" => sub {check_for_link(@_)})); + +sub push_output { + unshift(@outputstack, shift); +} + +sub pop_output { + shift(@outputstack); + + @outputstack = (undef) unless (@outputstack); +} sub write_irssi { - my $witem = shift; my @text = @_; + my $output = $outputstack[0]; - $text[0] = 'quotesite: ' . $text[0]; + my $format = "%%mquotesite: %%n" . shift(@text); - if (defined($witem) && ref($witem)) { - $witem->print(sprintf(shift(@text), @text), MSGLEVEL_CLIENTCRAP); + # escape % in parameters for irssi + s/%/%%/g foreach @text; + + if (defined($output)) { + $output->(sprintf($format, @text), MSGLEVEL_CLIENTCRAP); } else { - Irssi::print(sprintf(shift(@text), @text)); + Irssi::print(sprintf($format, @text)); } } sub write_debug { if ($debug) { - write_irssi(shift, @_); + write_irssi(@_); } } sub check_for_link { - my ($signal,$parammessage,$paramchannel,$paramnick,$paramserver) = @_; - my $server = $signal->[$paramserver]; - my $target = $signal->[$paramchannel]; - my $message = ($parammessage == -1) ? '' : $signal->[$parammessage]; + my $event = shift; + my $message = $event->message(); + my $channel = $event->channel(); my $g; my $m; my $p; - my $witem; - if (defined $server) { - $witem = $server->window_item_find($target); - } else { - $witem = Irssi::window_item_find($target); - } - # Look if we should ignore this line if ($message =~ m,(?:\s|^)/nosave(?:\s|$),) { return; } + push_output($event->ewpf); + study($message); # Offer the message to all Grabbers in turn foreach $g (@grabbers) { - # $g->pushio(sub{ write_irssi($witem, @_); }); ($m, $p) = $g->get($message); - # $g->popio(); while (defined($m)) { - write_irssi($witem, '%%R>>> %%Y%s%%N %%G%s', $m->{'SOURCE'}, $m->{'ID'}); + write_irssi('%%R>>> %%Y%s%%N %%G%s', $m->{'SOURCE'}, $m->{'ID'}); foreach (split(/[\n\r]+/, $m->{'CONTENT'})) { - write_irssi($witem, ' %%g%s', $_); + write_irssi(' %%g%s', $_); } # Remove the matched part from the message and try again (there may be @@ -101,6 +117,8 @@ sub check_for_link { ($m, $p) = $g->get($message); } } + + pop_output(); } sub cmd_save { @@ -111,9 +129,9 @@ sub cmd_save { close(CONF); }; if ($@) { - write_irssi(undef, 'Could not save config to %s: %s', ($conffile, $@)); + write_irssi('Could not save config to %s: %s', ($conffile, $@)); } else { - write_irssi(undef, 'configuration saved to %s', $conffile); + write_irssi('configuration saved to %s', $conffile); } } @@ -129,7 +147,7 @@ sub cmd_set { return; } } - write_irssi(undef, 'No such module'); + write_irssi('No such module'); } @@ -143,7 +161,7 @@ sub cmd_enable { return; } } - write_irssi(undef, 'No such module'); + write_irssi('No such module'); } @@ -157,7 +175,7 @@ sub cmd_disable { return; } } - write_irssi(undef, 'No such module'); + write_irssi('No such module'); } @@ -169,16 +187,16 @@ sub cmd_show { if (defined($target)) { foreach $p (@grabbers) { if ($p->{'NAME'} eq $target) { - write_irssi(undef, $p->getconfstr()); + write_irssi($p->getconfstr()); return; } } - write_irssi(undef, 'No such module'); + write_irssi('No such module'); } else { - write_irssi(undef, 'Loaded grabbers (* denotes enabled modules):'); + write_irssi('Loaded grabbers (* denotes enabled modules):'); foreach $p (@grabbers) { $e = $p->_getval('enabled'); - write_irssi(undef, ' %s%s', $p->{'NAME'}, $e?'*':''); + write_irssi(' %s%s', $p->{'NAME'}, $e?'*':''); }; } } @@ -190,13 +208,13 @@ sub cmd_help { if (defined($target)) { foreach $p (@grabbers) { if ($p->{'NAME'} eq $target) { - write_irssi(undef, $p->gethelpstr()); + write_irssi($p->gethelpstr()); return; } } - write_irssi(undef, 'No such module'); + write_irssi('No such module'); } else { - write_irssi(undef, <<'EOT'); + write_irssi(<<'EOT'); Supported commands: save: Save the current configuration help [modulename]: Display this help, or module specific help @@ -234,32 +252,32 @@ sub ploader { closedir(D); foreach $p (@list) { - write_debug(undef, "Trying to load $p:"); + write_debug("Trying to load $p:"); $p =~ s/\.pm$//; - eval qq{ require $p; }; + eval qq{ require quotesite::$p; }; if ($@) { - write_debug(undef, "Failed to load plugin: $@"); + write_debug("Failed to load plugin: $@"); next; } - $g = eval $p.q{->new();}; + $g = eval qq{ quotesite::$p->new(); }; if ($@) { - write_debug(undef, "Failed to instanciate: $@"); + write_debug("Failed to instanciate: $@"); delete($INC{$p}); next; } - write_debug(undef, "found $g->{'TYPE'} $g->{'NAME'}"); + write_debug("found $g->{'TYPE'} $g->{'NAME'}"); if ($type eq $g->{'TYPE'}) { push(@g, $g); - $g->setio(sub {Irssi::print(shift)}); + $g->setio(\&write_irssi); } else { - write_irssi(undef, '%s has wrong type (got %s, expected %s)', $p, $g->{'TYPE'}, $type); + write_irssi('%s has wrong type (got %s, expected %s)', $p, $g->{'TYPE'}, $type); delete($INC{$p}); } } - write_debug(undef, "Loaded %d plugins", $#g+1); + write_debug("Loaded %d plugins", $#g+1); return @g; } @@ -270,7 +288,7 @@ sub _load_modules($) { foreach (keys(%INC)) { if ($INC{$_} =~ m|^$path|) { - write_debug(undef, "Removing %s from \$INC", $_); + write_debug("Removing %s from \$INC", $_); delete($INC{$_}); } } @@ -285,7 +303,7 @@ sub init_quotesite { unless(-r $conffile && defined($conf = XML::Simple::XMLin($conffile, ForceArray => ['config', 'option'], KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'}))) { # No config, start with an empty one - write_debug(undef, 'No config found, using defaults'); + write_debug('No config found, using defaults'); $conf = { 'quotesite' => { }}; } foreach (keys(%{$PARAMS})) { @@ -297,7 +315,7 @@ sub init_quotesite { _load_modules($plugindir); unless (defined(@grabbers)) { - write_irssi(undef, 'No grabbers found, can not proceed.'); + write_irssi('No grabbers found, can not proceed.'); return; } @@ -314,15 +332,17 @@ sub init_quotesite { Irssi::signal_add('setup saved', 'cmd_save'); - Irssi::command_bind('quotesite' => \&cmdhandler); + Irssi::command_bind(_bcb('quotesite' => \&cmdhandler)); } - write_irssi(undef, 'quotesite initialized'); + write_irssi('quotesite initialized'); } sub cmdhandler { - my ($data, $server, $item) = @_; - my @params = split(/\s+/, $data); + my $event = shift; + my @params = split(/\s+/, $event->message()); + + push_output($event->ewpf); if ($params[0] eq 'save') { cmd_save(); @@ -348,14 +368,16 @@ sub cmdhandler { foreach (@grabbers) { $_->setdebug(1); } - write_irssi($item, 'Enabled debugging'); + write_irssi('Enabled debugging'); } elsif ($params[0] eq 'nodebug') { $debug = 0; foreach (@grabbers) { $_->setdebug(0); } - write_irssi($item, 'Disabled debugging'); + write_irssi('Disabled debugging'); } + + pop_output(); } unshift(@INC, $plugindir); diff --git a/quotesite/Base.pm b/quotesite/Base.pm index 08897c9..ccbb657 100644 --- a/quotesite/Base.pm +++ b/quotesite/Base.pm @@ -1,14 +1,14 @@ # (c) 2007 by Ralf Ertzinger # licensed under GNU GPL v2 -package Base; +package quotesite::Base; use strict; use Data::Dumper; sub new { my $class = shift; - my $self = {'_DEBUG' => 0, '_OUT' => sub {print shift}, '_OUTSTACK' => []}; + my $self = {'_DEBUG' => 0, '_OUT' => sub {print shift}}; bless($self, $class); @@ -91,23 +91,6 @@ sub setio { $self->{'_OUT'} = $io; } -sub pushio { - my $self = shift; - my $io = shift; - - push(@{$self->{'_OUTSTACK'}}, $self->{'_OUT'}); - $self->setio($io); -} - -sub popio { - my $self = shift; - my $io = pop(@{$self->{'_OUTSTACK'}}); - - if (defined($io)) { - $self->setio($io); - } -} - sub getconfstr { my $self = shift; my $s = 'Options for ' . $self->{'NAME'} . ":\n"; diff --git a/quotesite/BashOrgGrabber.pm b/quotesite/BashOrgGrabber.pm index 7f0d197..5e67069 100644 --- a/quotesite/BashOrgGrabber.pm +++ b/quotesite/BashOrgGrabber.pm @@ -3,10 +3,10 @@ # # Grabber for bash.org -package BashOrgGrabber; +package quotesite::BashOrgGrabber; -use GrabberBase; -@ISA = qw(GrabberBase); +use quotesite::GrabberBase; +@ISA = qw(quotesite::GrabberBase); use LWP::Simple qw(!get); use HTML::TokeParser; diff --git a/quotesite/GermanBashGrabber.pm b/quotesite/GermanBashGrabber.pm index e65b660..747968c 100644 --- a/quotesite/GermanBashGrabber.pm +++ b/quotesite/GermanBashGrabber.pm @@ -3,10 +3,10 @@ # # Grabber for german-bash.org -package GermanBashGrabber; +package quotesite::GermanBashGrabber; -use GrabberBase; -@ISA = qw(GrabberBase); +use quotesite::GrabberBase; +@ISA = qw(quotesite::GrabberBase); use LWP::UserAgent; use HTML::TokeParser; diff --git a/quotesite/GrabberBase.pm b/quotesite/GrabberBase.pm index 6a1c70a..6bbe830 100644 --- a/quotesite/GrabberBase.pm +++ b/quotesite/GrabberBase.pm @@ -1,9 +1,9 @@ # (c) 2007 by Ralf Ertzinger # licensed under GNU GPL v2 -package GrabberBase; -use Base; -@ISA = qw(Base); +package quotesite::GrabberBase; +use quotesite::Base; +@ISA = qw(quotesite::Base); use strict; diff --git a/quotesite/PolitBashOrgGrabber.pm b/quotesite/PolitBashOrgGrabber.pm index 1341b96..c5a57a5 100644 --- a/quotesite/PolitBashOrgGrabber.pm +++ b/quotesite/PolitBashOrgGrabber.pm @@ -3,10 +3,10 @@ # # Grabber for polit-bash.org -package PolitBashOrgGrabber; +package quotesite::PolitBashOrgGrabber; -use GrabberBase; -@ISA = qw(GrabberBase); +use quotesite::GrabberBase; +@ISA = qw(quotesite::GrabberBase); use LWP::Simple qw(!get); use HTML::TokeParser; diff --git a/quotesite/QdbGrabber.pm b/quotesite/QdbGrabber.pm index 1c56ee7..f9c7387 100644 --- a/quotesite/QdbGrabber.pm +++ b/quotesite/QdbGrabber.pm @@ -3,10 +3,10 @@ # # Grabber for qdb.us -package QdbGrabber; +package quotesite::QdbGrabber; -use GrabberBase; -@ISA = qw(GrabberBase); +use quotesite::GrabberBase; +@ISA = qw(quotesite::GrabberBase); use LWP::Simple qw(!get); use HTML::TokeParser; diff --git a/quotesite/TotalBashGrabber.pm b/quotesite/TotalBashGrabber.pm index 2c15eb8..2712316 100644 --- a/quotesite/TotalBashGrabber.pm +++ b/quotesite/TotalBashGrabber.pm @@ -3,10 +3,10 @@ # # Grabber for totalbash.org -package TotalBashGrabber; +package quotesite::TotalBashGrabber; -use GrabberBase; -@ISA = qw(GrabberBase); +use quotesite::GrabberBase; +@ISA = qw(quotesite::GrabberBase); use LWP::Simple qw(!get); use HTML::TokeParser; diff --git a/quotesite/TwitterGrabber.pm b/quotesite/TwitterGrabber.pm index dda3bae..ef4887a 100644 --- a/quotesite/TwitterGrabber.pm +++ b/quotesite/TwitterGrabber.pm @@ -3,10 +3,10 @@ # # Grabber for twitter -package TwitterGrabber; +package quotesite::TwitterGrabber; -use GrabberBase; -@ISA = qw(GrabberBase); +use quotesite::GrabberBase; +@ISA = qw(quotesite::GrabberBase); use LWP::Simple qw(!get); use Data::Dumper; diff --git a/quotesite/iBashGrabber.pm b/quotesite/iBashGrabber.pm index c6ebe2c..973d721 100644 --- a/quotesite/iBashGrabber.pm +++ b/quotesite/iBashGrabber.pm @@ -3,10 +3,10 @@ # # Grabber for german-bash.org -package iBashGrabber; +package quotesite::iBashGrabber; -use GrabberBase; -@ISA = qw(GrabberBase); +use quotesite::GrabberBase; +@ISA = qw(quotesite::GrabberBase); use LWP::Simple qw(!get); use HTML::TokeParser; -- 1.8.3.1