2010-03-17 07:36:54 +01:00
|
|
|
# File: IRCHandlers.pm
|
|
|
|
#
|
2021-07-21 07:44:51 +02:00
|
|
|
# Purpose: Pipes the PBot::Core::IRC default handler through PBot::Core::EventDispatcher,
|
2021-07-21 06:38:07 +02:00
|
|
|
# and loads all the packages in the IRCHandlers directory.
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2021-07-11 00:00:22 +02:00
|
|
|
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
|
|
|
|
# SPDX-License-Identifier: MIT
|
2017-03-05 22:33:31 +01:00
|
|
|
|
2021-07-21 07:44:51 +02:00
|
|
|
package PBot::Core::IRCHandlers;
|
|
|
|
use parent 'PBot::Core::Class';
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2021-06-19 06:23:34 +02:00
|
|
|
use PBot::Imports;
|
2019-07-11 03:40:53 +02:00
|
|
|
|
2021-07-21 06:38:07 +02:00
|
|
|
use PBot::Utils::LoadPackages;
|
2020-02-15 23:38:32 +01:00
|
|
|
|
2021-07-21 06:38:07 +02:00
|
|
|
use Data::Dumper;
|
2021-06-12 10:23:37 +02:00
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
sub initialize {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, %conf) = @_;
|
2021-06-25 03:28:49 +02:00
|
|
|
|
2021-07-21 06:38:07 +02:00
|
|
|
# register all the IRC handlers in the IRCHandlers directory
|
|
|
|
$self->register_handlers(%conf);
|
|
|
|
}
|
|
|
|
|
2021-07-21 07:44:51 +02:00
|
|
|
# registers handlers with a PBot::Core::IRC connection
|
2021-07-21 06:38:07 +02:00
|
|
|
|
|
|
|
sub add_handlers {
|
|
|
|
my ($self) = @_;
|
2021-06-25 03:28:49 +02:00
|
|
|
|
2021-07-21 06:38:07 +02:00
|
|
|
# set up handlers for the IRC engine
|
|
|
|
$self->{pbot}->{conn}->add_default_handler(
|
|
|
|
sub { $self->default_handler(@_) }, 1);
|
2020-02-15 23:38:32 +01:00
|
|
|
|
2021-07-21 06:38:07 +02:00
|
|
|
# send these events to on_init()
|
|
|
|
$self->{pbot}->{conn}->add_handler([251, 252, 253, 254, 255, 302],
|
|
|
|
sub { $self->{irchandlers}->{Server}->on_init(@_) });
|
2021-06-12 10:23:37 +02:00
|
|
|
|
2021-07-21 06:38:07 +02:00
|
|
|
# ignore these events
|
|
|
|
$self->{pbot}->{conn}->add_handler(
|
|
|
|
[
|
|
|
|
'myinfo',
|
|
|
|
'whoisserver',
|
|
|
|
'whoiscountry',
|
|
|
|
'whoischannels',
|
|
|
|
'whoisidle',
|
|
|
|
'motdstart',
|
|
|
|
'endofmotd',
|
|
|
|
'away',
|
|
|
|
],
|
|
|
|
sub { }
|
|
|
|
);
|
|
|
|
}
|
2021-06-12 10:23:37 +02:00
|
|
|
|
2021-07-21 06:38:07 +02:00
|
|
|
# registers all the IRC handler files in the IRCHandlers directory
|
2020-02-15 23:38:32 +01:00
|
|
|
|
2021-07-21 06:38:07 +02:00
|
|
|
sub register_handlers {
|
|
|
|
my ($self, %conf) = @_;
|
|
|
|
|
|
|
|
$self->{pbot}->{logger}->log("Registering IRC handlers:\n");
|
|
|
|
load_packages($self, 'IRCHandlers');
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2021-07-21 06:38:07 +02:00
|
|
|
# this default handler prepends 'irc.' to the event-type and then dispatches
|
2021-07-21 07:44:51 +02:00
|
|
|
# the event to the rest of PBot via PBot::Core::EventDispatcher.
|
2021-07-21 06:38:07 +02:00
|
|
|
|
2014-08-11 09:34:30 +02:00
|
|
|
sub default_handler {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $conn, $event) = @_;
|
2014-08-11 09:34:30 +02:00
|
|
|
|
2021-06-25 03:28:49 +02:00
|
|
|
my $result = $self->{pbot}->{event_dispatcher}->dispatch_event(
|
|
|
|
"irc.$event->{type}",
|
|
|
|
{
|
|
|
|
conn => $conn,
|
|
|
|
event => $event
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2021-07-21 06:38:07 +02:00
|
|
|
# log event if it was not handled and logging is requested
|
2021-06-25 03:28:49 +02:00
|
|
|
if (not defined $result and $self->{pbot}->{registry}->get_value('irc', 'log_default_handler')) {
|
|
|
|
$Data::Dumper::Sortkeys = 1;
|
2021-07-07 03:42:44 +02:00
|
|
|
$Data::Dumper::Indent = 2;
|
|
|
|
$Data::Dumper::Useqq = 1;
|
2021-06-25 03:28:49 +02:00
|
|
|
$self->{pbot}->{logger}->log(Dumper $event);
|
2014-11-01 01:15:21 +01:00
|
|
|
}
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
|
|
|
|
2021-07-21 06:38:07 +02:00
|
|
|
# replace randomized gibberish in certain hostmasks with identifying information
|
2018-08-06 07:47:38 +02:00
|
|
|
|
2017-06-18 12:40:51 +02:00
|
|
|
sub normalize_hostmask {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $nick, $user, $host) = @_;
|
2017-06-18 12:40:51 +02:00
|
|
|
|
2021-07-21 06:38:07 +02:00
|
|
|
if ($host =~ m{^(gateway|nat)/(.*)/x-[^/]+$}) {
|
|
|
|
$host = "$1/$2/x-$user";
|
|
|
|
}
|
2017-06-18 12:40:51 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
$host =~ s{/session$}{/x-$user};
|
2017-06-20 03:21:47 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
return ($nick, $user, $host);
|
2017-06-18 12:40:51 +02:00
|
|
|
}
|
|
|
|
|
2010-03-17 07:36:54 +01:00
|
|
|
1;
|