2021-07-21 06:38:07 +02:00
|
|
|
# File: Chat.pm
|
|
|
|
#
|
|
|
|
# Purpose: IRC handlers for chat/message events.
|
|
|
|
|
2023-02-21 06:31:52 +01:00
|
|
|
# SPDX-FileCopyrightText: 2001-2023 Pragmatic Software <pragma78@gmail.com>
|
2021-07-21 06:38:07 +02:00
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
2021-07-24 01:57:33 +02:00
|
|
|
package PBot::Core::Handlers::Chat;
|
2021-07-21 06:38:07 +02:00
|
|
|
|
|
|
|
use PBot::Imports;
|
2021-07-23 16:24:30 +02:00
|
|
|
use parent 'PBot::Core::Class';
|
2021-07-21 06:38:07 +02:00
|
|
|
|
|
|
|
sub initialize {
|
|
|
|
my ($self, %conf) = @_;
|
|
|
|
|
|
|
|
$self->{pbot}->{event_dispatcher}->register_handler('irc.notice', sub { $self->on_notice (@_) });
|
|
|
|
$self->{pbot}->{event_dispatcher}->register_handler('irc.public', sub { $self->on_public (@_) });
|
|
|
|
$self->{pbot}->{event_dispatcher}->register_handler('irc.caction', sub { $self->on_action (@_) });
|
|
|
|
$self->{pbot}->{event_dispatcher}->register_handler('irc.msg', sub { $self->on_msg (@_) });
|
|
|
|
}
|
|
|
|
|
|
|
|
sub on_notice {
|
|
|
|
my ($self, $event_type, $event) = @_;
|
|
|
|
|
|
|
|
my ($nick, $user, $host, $to, $text) = (
|
2023-01-31 14:44:34 +01:00
|
|
|
$event->nick,
|
|
|
|
$event->user,
|
|
|
|
$event->host,
|
|
|
|
$event->to,
|
|
|
|
$event->{args}[0],
|
2021-07-21 06:38:07 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
# don't handle non-chat NOTICE
|
|
|
|
return undef if $to eq '*';
|
|
|
|
|
|
|
|
# log notice
|
|
|
|
$self->{pbot}->{logger}->log("NOTICE from $nick!$user\@$host to $to: $text\n");
|
|
|
|
|
|
|
|
# if NOTICE is sent to the bot then replace the `to` field with the
|
|
|
|
# sender's nick instead so when we pass it on to on_public ...
|
|
|
|
if ($to eq $self->{pbot}->{registry}->get_value('irc', 'botnick')) {
|
2023-01-31 14:44:34 +01:00
|
|
|
$event->{to}[0] = $nick;
|
2021-07-21 06:38:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# handle this NOTICE as a public message
|
|
|
|
# (check for bot commands, anti-flooding, etc)
|
|
|
|
$self->on_public($event_type, $event);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub on_public {
|
|
|
|
my ($self, $event_type, $event) = @_;
|
|
|
|
|
2023-01-27 20:48:01 +01:00
|
|
|
my ($from, $nick, $user, $host, $text, $tags) = (
|
2023-01-31 14:44:34 +01:00
|
|
|
$event->{to}[0],
|
|
|
|
$event->nick,
|
|
|
|
$event->user,
|
|
|
|
$event->host,
|
|
|
|
$event->{args}[0],
|
|
|
|
$event->{args}[1],
|
2021-07-21 06:38:07 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
($nick, $user, $host) = $self->{pbot}->{irchandlers}->normalize_hostmask($nick, $user, $host);
|
|
|
|
|
|
|
|
# send text to be processed for bot commands, anti-flood enforcement, etc
|
2023-01-27 20:48:01 +01:00
|
|
|
$event->{interpreted} = $self->{pbot}->{interpreter}->process_line($from, $nick, $user, $host, $text, $tags);
|
2021-07-21 06:38:07 +02:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub on_action {
|
|
|
|
my ($self, $event_type, $event) = @_;
|
|
|
|
|
|
|
|
# prepend "/me " to the message text
|
2023-01-31 14:44:34 +01:00
|
|
|
$event->{args}[0] = "/me " . $event->{args}[0];
|
2021-07-21 06:38:07 +02:00
|
|
|
|
|
|
|
# pass this along to on_public
|
|
|
|
$self->on_public($event_type, $event);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub on_msg {
|
|
|
|
my ($self, $event_type, $event) = @_;
|
|
|
|
|
2023-01-27 20:48:01 +01:00
|
|
|
my ($nick, $user, $host, $text, $tags) = (
|
2023-01-31 14:44:34 +01:00
|
|
|
$event->nick,
|
|
|
|
$event->user,
|
|
|
|
$event->host,
|
|
|
|
$event->{args}[0],
|
|
|
|
$event->{args}[1],
|
2021-07-21 06:38:07 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
($nick, $user, $host) = $self->{pbot}->{irchandlers}->normalize_hostmask($nick, $user, $host);
|
|
|
|
|
2023-01-22 16:36:11 +01:00
|
|
|
# send text to be processed as a bot command, in "channel" $nick
|
2023-01-27 20:48:01 +01:00
|
|
|
$event->{interpreted} = $self->{pbot}->{interpreter}->process_line($nick, $nick, $user, $host, $text, $tags, 1);
|
2021-07-21 06:38:07 +02:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|