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-24 01:57:33 +02:00
|
|
|
# and registers default IRC handlers.
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2023-02-21 06:31:52 +01:00
|
|
|
# SPDX-FileCopyrightText: 20001-2023 Pragmatic Software <pragma78@gmail.com>
|
2021-07-11 00:00:22 +02:00
|
|
|
# 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 Data::Dumper;
|
2021-06-12 10:23:37 +02:00
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
sub initialize {
|
2021-07-24 01:57:33 +02:00
|
|
|
# nothing to do here
|
|
|
|
}
|
2021-06-25 03:28:49 +02:00
|
|
|
|
2023-01-31 14:44:34 +01:00
|
|
|
# this default handler prepends 'irc.' to the event-name and then dispatches
|
2021-07-24 01:57:33 +02:00
|
|
|
# the event to the rest of PBot via PBot::Core::EventDispatcher.
|
|
|
|
|
|
|
|
sub default_handler {
|
|
|
|
my ($self, $conn, $event) = @_;
|
|
|
|
|
2023-01-31 14:44:34 +01:00
|
|
|
# add conn to event object so we can access it within handlers
|
|
|
|
$event->{conn} = $conn;
|
|
|
|
|
2021-07-24 01:57:33 +02:00
|
|
|
my $result = $self->{pbot}->{event_dispatcher}->dispatch_event(
|
|
|
|
"irc.$event->{type}",
|
2023-01-31 14:44:34 +01:00
|
|
|
$event
|
2021-07-24 01:57:33 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
# log event if it was not handled and logging is requested
|
|
|
|
if (not defined $result and $self->{pbot}->{registry}->get_value('irc', 'log_default_handler')) {
|
|
|
|
$Data::Dumper::Sortkeys = 1;
|
|
|
|
$Data::Dumper::Indent = 2;
|
|
|
|
$Data::Dumper::Useqq = 1;
|
2023-01-31 14:44:34 +01:00
|
|
|
delete $event->{conn}; # don't include conn in dump
|
2021-07-24 01:57:33 +02:00
|
|
|
$self->{pbot}->{logger}->log(Dumper $event);
|
|
|
|
}
|
2021-07-21 06:38:07 +02:00
|
|
|
}
|
|
|
|
|
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],
|
2021-07-24 01:57:33 +02:00
|
|
|
sub { $self->{pbot}->{handlers}->{modules}->{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
|
|
|
# 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;
|