1 # autodisplay quotes from quotesites
3 # (c) 2007-2008 by Ralf Ertzinger <ralf@camperquake.de>
4 # licensed under GNU GPL v2
7 use Irssi 20020324 qw (command_bind command_runsub signal_add_first signal_add_last window_find_refnum);
8 use vars qw($VERSION %IRSSI);
15 my $conffile = File::Spec->catfile(Irssi::get_irssi_dir(), 'quotesite.xml');
16 my $plugindir = File::Spec->catfile(Irssi::get_irssi_dir(), 'scripts', 'quotesite');
25 # "message public", SERVER_REC, char *msg, char *nick, char *address, char *target
26 signal_add_last("message public" => sub {check_for_link(\@_,1,4,2,0);});
27 # "message own_public", SERVER_REC, char *msg, char *target
28 signal_add_last("message own_public" => sub {check_for_link(\@_,1,2,-1,0);});
30 # "message private", SERVER_REC, char *msg, char *nick, char *address
31 signal_add_last("message private" => sub {check_for_link(\@_,1,-1,2,0);});
32 # "message own_private", SERVER_REC, char *msg, char *target, char *orig_target
33 signal_add_last("message own_private" => sub {check_for_link(\@_,1,2,-1,0);});
35 # "message irc action", SERVER_REC, char *msg, char *nick, char *address, char *target
36 signal_add_last("message irc action" => sub {check_for_link(\@_,1,4,2,0);});
37 # "message irc own_action", SERVER_REC, char *msg, char *target
38 signal_add_last("message irc own_action" => sub {check_for_link(\@_,1,2,-1,0);});
44 $text[0] = 'quotesite: ' . $text[0];
46 if (defined($witem) && ref($witem)) {
47 $witem->print(sprintf(shift(@text), @text), MSGLEVEL_CLIENTCRAP);
49 Irssi::print(sprintf(shift(@text), @text));
56 write_irssi(shift, @_);
61 my ($signal,$parammessage,$paramchannel,$paramnick,$paramserver) = @_;
62 my $server = $signal->[$paramserver];
63 my $target = $signal->[$paramchannel];
64 my $message = ($parammessage == -1) ? '' : $signal->[$parammessage];
71 if (defined $server) {
72 $witem = $server->window_item_find($target);
74 $witem = Irssi::window_item_find($target);
77 # Look if we should ignore this line
78 if ($message =~ m,(?:\s|^)/nosave(?:\s|$),) {
84 # Offer the message to all Grabbers in turn
85 foreach $g (@grabbers) {
86 # $g->pushio(sub{ write_irssi($witem, @_); });
87 ($m, $p) = $g->get($message);
90 write_irssi($witem, '%%R>>> %%Y%s%%N %%G%s', $m->{'SOURCE'}, $m->{'ID'});
92 foreach (split(/[\n\r]+/, $m->{'CONTENT'})) {
93 write_irssi($witem, ' %%g%s', $_);
96 # Remove the matched part from the message and try again (there may be
101 ($m, $p) = $g->get($message);
109 open(CONF, '>'.$conffile) or die 'Could not open config file';
110 print CONF XML::Simple::XMLout($conf, KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'});
114 write_irssi(undef, 'Could not save config to %s: %s', ($conffile, $@));
116 write_irssi(undef, 'configuration saved to %s', $conffile);
126 foreach $p (@grabbers) {
127 if ($p->{'NAME'} eq $target) {
128 $p->setval($key, $val);
132 write_irssi(undef, 'No such module');
140 foreach $p (@grabbers) {
141 if ($p->{'NAME'} eq $target) {
146 write_irssi(undef, 'No such module');
154 foreach $p (@grabbers) {
155 if ($p->{'NAME'} eq $target) {
160 write_irssi(undef, 'No such module');
169 if (defined($target)) {
170 foreach $p (@grabbers) {
171 if ($p->{'NAME'} eq $target) {
172 write_irssi(undef, $p->getconfstr());
176 write_irssi(undef, 'No such module');
178 write_irssi(undef, 'Loaded grabbers (* denotes enabled modules):');
179 foreach $p (@grabbers) {
180 $e = $p->_getval('enabled');
181 write_irssi(undef, ' %s%s', $p->{'NAME'}, $e?'*':'');
190 if (defined($target)) {
191 foreach $p (@grabbers) {
192 if ($p->{'NAME'} eq $target) {
193 write_irssi(undef, $p->gethelpstr());
197 write_irssi(undef, 'No such module');
199 write_irssi(undef, <<'EOT');
201 save: Save the current configuration
202 help [modulename]: Display this help, or module specific help
203 show [modulename]: Show loaded modules, or the current parameters of a module
204 set modulename parameter value: set a module parameter to a new value
205 enable [modulename]: enable the usage of this module (grabbers only)
206 disable [modulename]: disable the usage of this module (grabbers only)
207 reload: reload all modules (this is somewhat experimental)
208 debug: enable debugging messages
209 nodebug: disable debugging messages
215 sub sig_command_script_unload {
217 if ($script =~ /(.*\/)?quotesite(\.pl)?$/) {
232 opendir(D, $dir) || return ();
233 @list = grep {/$pattern/ && -f File::Spec->catfile($dir, $_) } readdir(D);
237 write_debug(undef, "Trying to load $p:");
239 eval qq{ require $p; };
241 write_debug(undef, "Failed to load plugin: $@");
245 $g = eval $p.q{->new();};
247 write_debug(undef, "Failed to instanciate: $@");
252 write_debug(undef, "found $g->{'TYPE'} $g->{'NAME'}");
253 if ($type eq $g->{'TYPE'}) {
255 $g->setio(sub {Irssi::print(shift)});
257 write_irssi(undef, '%s has wrong type (got %s, expected %s)', $p, $g->{'TYPE'}, $type);
262 write_debug(undef, "Loaded %d plugins", $#g+1);
267 sub _load_modules($) {
271 foreach (keys(%INC)) {
272 if ($INC{$_} =~ m|^$path|) {
273 write_debug(undef, "Removing %s from \$INC", $_);
277 @grabbers = ploader($path, '.*Grabber\.pm$', 'grabber');
283 my $bindings = shift;
286 unless(-r $conffile && defined($conf = XML::Simple::XMLin($conffile, ForceArray => ['config', 'option'], KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'}))) {
287 # No config, start with an empty one
288 write_debug(undef, 'No config found, using defaults');
289 $conf = { 'quotesite' => { }};
291 foreach (keys(%{$PARAMS})) {
292 unless (exists($conf->{'quotesite'}->{$_})) {
293 $conf->{'quotesite'}->{$_} = $PARAMS->{$_};
297 _load_modules($plugindir);
299 unless (defined(@grabbers)) {
300 write_irssi(undef, 'No grabbers found, can not proceed.');
305 # Loop through all plugins and load the config
306 foreach $p (@grabbers) {
307 $conf->{'quotesite'}->{'config'}->{$p->{'NAME'}} = $p->mergeconfig($conf->{'quotesite'}->{'config'}->{$p->{'NAME'}});
312 Irssi::signal_add_first('command script load', 'sig_command_script_unload');
313 Irssi::signal_add_first('command script unload', 'sig_command_script_unload');
314 Irssi::signal_add('setup saved', 'cmd_save');
317 Irssi::command_bind('quotesite' => \&cmdhandler);
320 write_irssi(undef, 'quotesite initialized');
324 my ($data, $server, $item) = @_;
325 my @params = split(/\s+/, $data);
327 if ($params[0] eq 'save') {
329 } elsif ($params[0] eq 'set') {
332 } elsif ($params[0] eq 'show') {
335 } elsif ($params[0] eq 'help') {
338 } elsif ($params[0] eq 'enable') {
341 } elsif ($params[0] eq 'disable') {
343 cmd_disable(@params);
344 } elsif ($params[0] eq 'reload') {
346 } elsif ($params[0] eq 'debug') {
348 foreach (@grabbers) {
351 write_irssi($item, 'Enabled debugging');
352 } elsif ($params[0] eq 'nodebug') {
354 foreach (@grabbers) {
357 write_irssi($item, 'Disabled debugging');
361 unshift(@INC, $plugindir);