3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-12-25 04:02:37 +01:00

Move URL titles to UrlTitles.pm plugin

Add $event->{interpreted} field to events to notify other handlers
whether an event was successfully interpreted by the interpreter.

An $event->{interpreted} that is equal to or greater than 100 means the entire
message was consumed and handlers shouldn't do any further processing of it
that would generate any output to channels or users.

Otherwise, $event->{interpreted} is incremented by 1 for each referenced
command that was processed.
This commit is contained in:
Pragmatic Software 2015-09-08 01:37:34 -07:00
parent fa2865fb10
commit e5097eee69
3 changed files with 88 additions and 28 deletions

View File

@ -101,7 +101,7 @@ sub on_public {
my $host = $event->{event}->host; my $host = $event->{event}->host;
my $text = $event->{event}->{args}[0]; my $text = $event->{event}->{args}[0];
$self->{pbot}->{interpreter}->process_line($from, $nick, $user, $host, $text); $event->{interpreted} = $self->{pbot}->{interpreter}->process_line($from, $nick, $user, $host, $text);
return 0; return 0;
} }

View File

@ -33,9 +33,6 @@ sub initialize {
$self->{pbot} = delete $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__); $self->{pbot} = delete $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__);
$self->{pbot}->{registry}->add_default('text', 'general', 'show_url_titles', $conf{show_url_titles} // 1);
$self->{pbot}->{registry}->add_default('array', 'general', 'show_url_titles_channels', $conf{show_url_titles_channels} // '.*');
$self->{pbot}->{registry}->add_default('array', 'general', 'show_url_titles_ignore_channels', $conf{show_url_titles_ignore_channels} // 'none');
$self->{pbot}->{registry}->add_default('text', 'general', 'compile_blocks', $conf{compile_blocks} // 1); $self->{pbot}->{registry}->add_default('text', 'general', 'compile_blocks', $conf{compile_blocks} // 1);
$self->{pbot}->{registry}->add_default('array', 'general', 'compile_blocks_channels', $conf{compile_blocks_channels} // '.*'); $self->{pbot}->{registry}->add_default('array', 'general', 'compile_blocks_channels', $conf{compile_blocks_channels} // '.*');
$self->{pbot}->{registry}->add_default('array', 'general', 'compile_blocks_ignore_channels', $conf{compile_blocks_ignore_channels} // 'none'); $self->{pbot}->{registry}->add_default('array', 'general', 'compile_blocks_ignore_channels', $conf{compile_blocks_ignore_channels} // 'none');
@ -53,10 +50,10 @@ sub process_line {
my ($from, $nick, $user, $host, $text) = @_; my ($from, $nick, $user, $host, $text) = @_;
my $command; my $command;
my $has_url;
my $has_code; my $has_code;
my $nick_override; my $nick_override;
my $botnick = $self->{pbot}->{registry}->get_value('irc', 'botnick'); my $botnick = $self->{pbot}->{registry}->get_value('irc', 'botnick');
my $processed = 0;
$from = lc $from if defined $from; $from = lc $from if defined $from;
@ -75,9 +72,10 @@ sub process_line {
$flood_threshold, $flood_time_threshold, $flood_threshold, $flood_time_threshold,
$pbot->{messagehistory}->{MSG_CHAT}) if defined $from; $pbot->{messagehistory}->{MSG_CHAT}) if defined $from;
my $preserve_whitespace = 0;
$text =~ s/^\s+//; $text =~ s/^\s+//;
$text =~ s/\s+$//; $text =~ s/\s+$//;
my $preserve_whitespace = 0;
my $cmd_text = $text; my $cmd_text = $text;
$cmd_text =~ s/^\/me\s+//; $cmd_text =~ s/^\/me\s+//;
@ -94,24 +92,26 @@ sub process_line {
while (++$count <= 3) { while (++$count <= 3) {
$referenced = 0; $referenced = 0;
$command = undef; $command = undef;
$has_url = undef;
$has_code = undef; $has_code = undef;
if($cmd_text =~ s/^(?:$bot_trigger|$botnick.?)?\s*{\s*(.*)\s*}\s*$//) { if($cmd_text =~ s/^(?:$bot_trigger|$botnick.?)?\s*{\s*(.*)\s*}\s*$//) {
$has_code = $1 if length $1; $has_code = $1 if length $1;
$preserve_whitespace = 1; $preserve_whitespace = 1;
} elsif($cmd_text =~ s/^$bot_trigger(.*)$//) { $processed += 100;
$command = $1;
} elsif($cmd_text =~ s/^.?$botnick.?\s*(.*?)$//i) {
$command = $1;
} elsif($cmd_text =~ s/^(.*?),?\s*$botnick[?!.]*$//i) {
$command = $1;
} elsif($cmd_text =~ s/https?:\/\/([^\s]+)//i) {
$has_url = $1;
} elsif($cmd_text =~ s/^\s*([^,:\(\)\+\*\/ ]+)[,:]*\s*{\s*(.*)\s*}\s*$//) { } elsif($cmd_text =~ s/^\s*([^,:\(\)\+\*\/ ]+)[,:]*\s*{\s*(.*)\s*}\s*$//) {
$nick_override = $1; $nick_override = $1;
$has_code = $2 if length $2 and $nick_override ne 'enum' and $nick_override ne 'struct'; $has_code = $2 if length $2 and $nick_override !~ /^(?:enum|struct|union)$/;
$preserve_whitespace = 1; $preserve_whitespace = 1;
$processed += 100;
} elsif($cmd_text =~ s/^$bot_trigger(.*)$//) {
$command = $1;
$processed += 100;
} elsif($cmd_text =~ s/^.?$botnick.?\s*(.*?)$//i) {
$command = $1;
$processed += 100;
} elsif($cmd_text =~ s/^(.*?),?\s*$botnick[?!.]*$//i) {
$command = $1;
$processed += 100;
} elsif ($cmd_text =~ s/\B$bot_trigger([^ ]+)//) { } elsif ($cmd_text =~ s/\B$bot_trigger([^ ]+)//) {
my $cmd = $1; my $cmd = $1;
$cmd =~ s/(.)[.!?;,)]$/$1/; $cmd =~ s/(.)[.!?;,)]$/$1/;
@ -125,23 +125,18 @@ sub process_line {
$referenced = 1; $referenced = 1;
} }
last if not defined $command and not defined $has_url and not defined $has_code; last if not defined $command and not defined $has_code;
if((!defined $command || $command !~ /^login/) && defined $from && $pbot->{ignorelist}->check_ignore($nick, $user, $host, $from)) { if((!defined $command || $command !~ /^login /) && defined $from && $pbot->{ignorelist}->check_ignore($nick, $user, $host, $from)) {
my $admin = $pbot->{admins}->loggedin($from, "$nick!$user\@$host"); my $admin = $pbot->{admins}->loggedin($from, "$nick!$user\@$host");
if (!defined $admin || $admin->{level} < 10) { if (!defined $admin || $admin->{level} < 10) {
# ignored hostmask # ignored hostmask
return; return 1;
} }
} }
if(defined $has_url) { if(defined $has_code) {
if($pbot->{registry}->get_value('general', 'show_url_titles') and not $pbot->{registry}->get_value($from, 'no_url_titles') $processed += 100; # ensure no other plugins try to parse this message
and not grep { $from =~ /$_/i } $pbot->{registry}->get_value('general', 'show_url_titles_ignore_channels')
and grep { $from =~ /$_/i } $pbot->{registry}->get_value('general', 'show_url_titles_channels')) {
$pbot->{factoids}->{factoidmodulelauncher}->execute_module($from, undef, $nick, $user, $host, $text, "title", "$nick http://$has_url", $preserve_whitespace);
}
} elsif(defined $has_code) {
if($pbot->{registry}->get_value('general', 'compile_blocks') and not $pbot->{registry}->get_value($from, 'no_compile_blocks') if($pbot->{registry}->get_value('general', 'compile_blocks') and not $pbot->{registry}->get_value($from, 'no_compile_blocks')
and not grep { $from =~ /$_/i } $pbot->{registry}->get_value('general', 'compile_blocks_ignore_channels') and not grep { $from =~ /$_/i } $pbot->{registry}->get_value('general', 'compile_blocks_ignore_channels')
and grep { $from =~ /$_/i } $pbot->{registry}->get_value('general', 'compile_blocks_channels')) { and grep { $from =~ /$_/i } $pbot->{registry}->get_value('general', 'compile_blocks_channels')) {
@ -150,9 +145,10 @@ sub process_line {
} }
} }
} else { } else {
$self->handle_result($from, $nick, $user, $host, $text, $command, $self->interpret($from, $nick, $user, $host, 1, $command, undef, $referenced), 1, $preserve_whitespace); $processed++ if $self->handle_result($from, $nick, $user, $host, $text, $command, $self->interpret($from, $nick, $user, $host, 1, $command, undef, $referenced), 1, $preserve_whitespace);
} }
} }
return $processed;
} }
sub interpret { sub interpret {
@ -248,7 +244,7 @@ sub handle_result {
my ($self, $from, $nick, $user, $host, $text, $command, $result, $checkflood, $preserve_whitespace) = @_; my ($self, $from, $nick, $user, $host, $text, $command, $result, $checkflood, $preserve_whitespace) = @_;
if (not defined $result or length $result == 0) { if (not defined $result or length $result == 0) {
return; return 0;
} }
my $original_result = $result; my $original_result = $result;
@ -324,6 +320,7 @@ sub handle_result {
} }
} }
$self->{pbot}->{logger}->log("---------------------------------------------\n"); $self->{pbot}->{logger}->log("---------------------------------------------\n");
return 1;
} }
sub output_result { sub output_result {

63
PBot/Plugins/UrlTitles.pm Normal file
View File

@ -0,0 +1,63 @@
# File: UrlTitles.pm
# Author: pragma-
#
# Purpose: Display titles of URLs in channel messages.
package PBot::Plugins::UrlTitles;
use warnings;
use strict;
use Carp ();
sub new {
Carp::croak("Options to " . __FILE__ . " should be key/value pairs, not hash reference") if ref $_[1] eq 'HASH';
my ($class, %conf) = @_;
my $self = bless {}, $class;
$self->initialize(%conf);
return $self;
}
sub initialize {
my ($self, %conf) = @_;
$self->{pbot} = delete $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__);
$self->{pbot}->{registry}->add_default('text', 'general', 'show_url_titles', $conf{show_url_titles} // 1);
$self->{pbot}->{registry}->add_default('array', 'general', 'show_url_titles_channels', $conf{show_url_titles_channels} // '.*');
$self->{pbot}->{registry}->add_default('array', 'general', 'show_url_titles_ignore_channels', $conf{show_url_titles_ignore_channels} // 'none');
$self->{pbot}->{event_dispatcher}->register_handler('irc.public', sub { $self->show_url_titles(@_) });
$self->{pbot}->{event_dispatcher}->register_handler('irc.caction', sub { $self->show_url_titles(@_) });
}
sub unload {
my $self = shift;
}
sub show_url_titles {
my ($self, $event_type, $event) = @_;
my $channel = $event->{event}->{to}[0];
my ($nick, $user, $host) = ($event->{event}->nick, $event->{event}->user, $event->{event}->host);
my $msg = $event->{event}->{args}[0];
if ($self->{pbot}->{ignorelist}->check_ignore($nick, $user, $host, $channel)) {
my $admin = $self->{pbot}->{admins}->loggedin($channel, "$nick!$user\@$host");
if (!defined $admin || $admin->{level} < 10) {
return 0;
}
}
while ($msg =~ s/(https?:\/\/[^\s]+)//i && ++$event->{interpreted} <= 3) {
my $url = $1;
if($self->{pbot}->{registry}->get_value('general', 'show_url_titles') and not $self->{pbot}->{registry}->get_value($channel, 'no_url_titles')
and not grep { $channel =~ /$_/i } $self->{pbot}->{registry}->get_value('general', 'show_url_titles_ignore_channels')
and grep { $channel =~ /$_/i } $self->{pbot}->{registry}->get_value('general', 'show_url_titles_channels')) {
$self->{pbot}->{factoids}->{factoidmodulelauncher}->execute_module($channel, undef, $nick, $user, $host, $msg, "title", "$nick $url");
}
}
return 0;
}
1;