3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-16 08:59:34 +01:00
pbot/lib/PBot/Core/Handlers/Channel.pm

331 lines
12 KiB
Perl
Raw Normal View History

# File: Channel.pm
#
# Purpose: Handlers for general channel-related IRC events that aren't handled
# by any specialized Handler modules.
2023-02-21 06:31:52 +01:00
# SPDX-FileCopyrightText: 2005-2023 Pragmatic Software <pragma78@gmail.com>
# SPDX-License-Identifier: MIT
2021-07-24 01:57:33 +02:00
package PBot::Core::Handlers::Channel;
2021-07-21 07:44:51 +02:00
use parent 'PBot::Core::Class';
use PBot::Imports;
2021-07-21 07:44:51 +02:00
use PBot::Core::MessageHistory::Constants ':all';
use Data::Dumper;
use Encode;
use MIME::Base64;
use Time::HiRes qw/time/;
sub initialize($self, %conf) {
$self->{pbot}->{event_dispatcher}->register_handler('irc.mode', sub { $self->on_mode (@_) });
$self->{pbot}->{event_dispatcher}->register_handler('irc.join', sub { $self->on_join (@_) });
$self->{pbot}->{event_dispatcher}->register_handler('irc.part', sub { $self->on_departure (@_) });
2023-02-02 02:47:25 +01:00
$self->{pbot}->{event_dispatcher}->register_handler('irc.quit', sub { $self->on_departure (@_) });
$self->{pbot}->{event_dispatcher}->register_handler('irc.kick', sub { $self->on_kick (@_) });
$self->{pbot}->{event_dispatcher}->register_handler('irc.invite', sub { $self->on_invite (@_) });
$self->{pbot}->{event_dispatcher}->register_handler('irc.channelmodeis', sub { $self->on_channelmodeis (@_) });
$self->{pbot}->{event_dispatcher}->register_handler('irc.topic', sub { $self->on_topic (@_) });
$self->{pbot}->{event_dispatcher}->register_handler('irc.topicinfo', sub { $self->on_topicinfo (@_) });
$self->{pbot}->{event_dispatcher}->register_handler('irc.channelcreate', sub { $self->on_channelcreate (@_) });
$self->{pbot}->{event_dispatcher}->register_handler('irc.modeflag', sub { $self->on_modeflag (@_) });
}
sub on_mode($self, $event_type, $event) {
my ($nick, $user, $host, $mode_string, $channel) = (
2023-01-31 14:44:34 +01:00
$event->nick,
$event->user,
$event->host,
$event->{args}[0],
lc $event->{to}[0],
);
($nick, $user, $host) = $self->{pbot}->{irchandlers}->normalize_hostmask($nick, $user, $host);
my $i = 0;
my ($modifier, $flag, $mode, $target);
my $source = "$nick!$user\@$host";
# split combined modes
while ($mode_string =~ m/(.)/g) {
$flag = $1;
if ($flag eq '-' or $flag eq '+') {
$modifier = $flag;
next;
}
$mode = $modifier . $flag;
2023-01-31 14:44:34 +01:00
$target = $event->{args}[++$i];
2023-01-31 00:50:26 +01:00
$self->{pbot}->{logger}->log("[MODE] $channel $mode" . (length $target ? " $target" : '') . " by $source\n");
# dispatch a single mode flag event
$self->{pbot}->{event_dispatcher}->dispatch_event(
'irc.modeflag',
{
source => $source,
channel => $channel,
mode => $mode,
target => $target,
},
);
}
return 1;
}
sub on_modeflag($self, $event_type, $event) {
my ($source, $channel, $mode, $target) = (
$event->{source},
$event->{channel},
$event->{mode},
$event->{target},
);
# disregard mode set on user instead of channel
return if defined $target and length $target;
my ($modifier, $flag) = split //, $mode;
my $modes = $self->{pbot}->{channels}->get_meta($channel, 'MODE') // '';
if ($modifier eq '+') {
$modes = '+' if not length $modes;
$modes .= $flag if $modes !~ /\Q$flag/;
} else {
$modes =~ s/\Q$flag//g;
$modes = '' if $modes eq '+';
}
$self->{pbot}->{channels}->{storage}->set($channel, 'MODE', $modes, 1);
return 1;
}
sub on_join($self, $event_type, $event) {
my ($nick, $user, $host, $channel) = (
2023-01-31 14:44:34 +01:00
$event->nick,
$event->user,
$event->host,
lc $event->{to}[0],
);
($nick, $user, $host) = $self->{pbot}->{irchandlers}->normalize_hostmask($nick, $user, $host);
my $message_account = $self->{pbot}->{messagehistory}->get_message_account($nick, $user, $host);
$self->{pbot}->{messagehistory}->add_message($message_account, "$nick!$user\@$host", $channel, "JOIN", MSG_JOIN);
$self->{pbot}->{messagehistory}->{database}->devalidate_channel($message_account, $channel);
my $msg = 'JOIN';
# IRCv3 extended-join capability provides more details about user
if (exists $self->{pbot}->{irc_capabilities}->{'extended-join'}) {
my ($nickserv, $gecos) = (
2023-01-31 14:44:34 +01:00
$event->{args}[0],
$event->{args}[1],
);
$msg .= " $nickserv :$gecos";
$self->{pbot}->{messagehistory}->{database}->update_gecos($message_account, $gecos, scalar time);
if ($nickserv ne '*') {
$self->{pbot}->{messagehistory}->{database}->link_aliases($message_account, undef, $nickserv);
$self->{pbot}->{messagehistory}->{database}->update_nickserv_account($message_account, $nickserv, scalar time);
$self->{pbot}->{messagehistory}->{database}->set_current_nickserv_account($message_account, $nickserv);
} else {
$self->{pbot}->{messagehistory}->{database}->set_current_nickserv_account($message_account, '');
}
2023-01-31 14:44:34 +01:00
$self->{pbot}->{antiflood}->check_bans($message_account, $event->from, $channel);
}
$self->{pbot}->{antiflood}->check_flood(
$channel, $nick, $user, $host, $msg,
$self->{pbot}->{registry}->get_value('antiflood', 'join_flood_threshold'),
$self->{pbot}->{registry}->get_value('antiflood', 'join_flood_time_threshold'),
MSG_JOIN,
);
return 1;
}
sub on_invite($self, $event_type, $event) {
my ($nick, $user, $host, $target, $channel) = (
2023-01-31 14:44:34 +01:00
$event->nick,
$event->user,
$event->host,
$event->to,
lc $event->{args}[0]
);
($nick, $user, $host) = $self->{pbot}->{irchandlers}->normalize_hostmask($nick, $user, $host);
$self->{pbot}->{logger}->log("$nick!$user\@$host invited $target to $channel!\n");
# if invited to a channel on our channel list, go ahead and join it
if ($target eq $self->{pbot}->{conn}->nick) {
if ($self->{pbot}->{channels}->is_active($channel)) {
$self->{pbot}->{interpreter}->add_botcmd_to_command_queue($channel, "join $channel", 0);
}
}
return 1;
}
sub on_kick($self, $event_type, $event) {
my ($nick, $user, $host, $target, $channel, $reason) = (
2023-01-31 14:44:34 +01:00
$event->nick,
$event->user,
$event->host,
$event->to,
lc $event->{args}[0],
$event->{args}[1]
);
($nick, $user, $host) = $self->{pbot}->{irchandlers}->normalize_hostmask($nick, $user, $host);
$self->{pbot}->{logger}->log("$nick!$user\@$host kicked $target from $channel ($reason)\n");
# hostmask of the person being kicked
my $target_hostmask;
# look up message history account for person being kicked
my ($message_account) = $self->{pbot}->{messagehistory}->{database}->find_message_account_by_nick($target);
if (defined $message_account) {
# update target hostmask
$target_hostmask = $self->{pbot}->{messagehistory}->{database}->find_most_recent_hostmask($message_account);
# add "KICKED by" to kicked person's message history
my $text = "KICKED by $nick!$user\@$host ($reason)";
$self->{pbot}->{messagehistory}->add_message($message_account, $target_hostmask, $channel, $text, MSG_DEPARTURE);
# do stuff that happens in check_flood
my ($target_nick, $target_user, $target_host) = $target_hostmask =~ m/^([^!]+)!([^@]+)@(.*)/;
$self->{pbot}->{antiflood}->check_flood(
$channel, $target_nick, $target_user, $target_host, $text,
$self->{pbot}->{registry}->get_value('antiflood', 'join_flood_threshold'),
$self->{pbot}->{registry}->get_value('antiflood', 'join_flood_time_threshold'),
MSG_DEPARTURE,
);
}
# look up message history account for person doing the kicking
$message_account = $self->{pbot}->{messagehistory}->{database}->get_message_account_id("$nick!$user\@$host");
if (defined $message_account) {
# replace target nick with target hostmask if available
if (defined $target_hostmask) {
$target = $target_hostmask;
}
# add "KICKED $target" to kicker's message history
my $text = "KICKED $target from $channel ($reason)";
$self->{pbot}->{messagehistory}->add_message($message_account, "$nick!$user\@$host", $channel, $text, MSG_CHAT);
}
return 1;
}
sub on_departure($self, $event_type, $event) {
my ($nick, $user, $host, $channel, $args) = (
2023-01-31 14:44:34 +01:00
$event->nick,
$event->user,
$event->host,
lc $event->{to}[0],
$event->args
);
($nick, $user, $host) = $self->{pbot}->{irchandlers}->normalize_hostmask($nick, $user, $host);
2023-01-31 14:44:34 +01:00
my $text = uc ($event->type) . ' ' . $args;
my $message_account = $self->{pbot}->{messagehistory}->get_message_account($nick, $user, $host);
if ($text =~ m/^QUIT/) {
# QUIT messages must be added to the mesasge history of each channel the user is on
my $channels = $self->{pbot}->{nicklist}->get_channels($nick);
foreach my $chan (@$channels) {
next if $chan !~ m/^#/;
$self->{pbot}->{messagehistory}->add_message($message_account, "$nick!$user\@$host", $chan, $text, MSG_DEPARTURE);
}
} else {
$self->{pbot}->{messagehistory}->add_message($message_account, "$nick!$user\@$host", $channel, $text, MSG_DEPARTURE);
}
$self->{pbot}->{antiflood}->check_flood(
$channel, $nick, $user, $host, $text,
$self->{pbot}->{registry}->get_value('antiflood', 'join_flood_threshold'),
$self->{pbot}->{registry}->get_value('antiflood', 'join_flood_time_threshold'),
MSG_DEPARTURE,
);
my $u = $self->{pbot}->{users}->find_user($channel, "$nick!$user\@$host");
# log user out if logged in and not stayloggedin
# TODO: this should probably be in Users.pm with its own part/quit/kick handler
if (defined $u and $u->{loggedin} and not $u->{stayloggedin}) {
$self->{pbot}->{logger}->log("Logged out $nick.\n");
delete $u->{loggedin};
$self->{pbot}->{users}->save;
}
return 1;
}
sub on_channelmodeis($self, $event_type, $event) {
2023-01-31 14:44:34 +01:00
my (undef, $channel, $modes) = $event->args;
$self->{pbot}->{logger}->log("Channel $channel modes: $modes\n");
$self->{pbot}->{channels}->{storage}->set($channel, 'MODE', $modes, 1);
return 1;
}
sub on_channelcreate($self, $event_type, $event) {
2023-01-31 14:44:34 +01:00
my ($owner, $channel, $timestamp) = $event->args;
$self->{pbot}->{logger}->log("Channel $channel created by $owner on " . localtime($timestamp) . "\n");
$self->{pbot}->{channels}->{storage}->set($channel, 'CREATED_BY', $owner, 1);
$self->{pbot}->{channels}->{storage}->set($channel, 'CREATED_ON', $timestamp, 1);
return 1;
}
sub on_topic($self, $event_type, $event) {
2023-01-31 14:44:34 +01:00
if (not length $event->{to}[0]) {
# on join
2023-01-31 14:44:34 +01:00
my (undef, $channel, $topic) = $event->args;
$self->{pbot}->{logger}->log("Topic for $channel: $topic\n");
$self->{pbot}->{channels}->{storage}->set($channel, 'TOPIC', $topic, 1);
} else {
# user changing topic
2023-01-31 14:44:34 +01:00
my ($nick, $user, $host) = ($event->nick, $event->user, $event->host);
my $channel = $event->{to}[0];
my $topic = $event->{args}[0];
$self->{pbot}->{logger}->log("$nick!$user\@$host changed topic for $channel to: $topic\n");
$self->{pbot}->{channels}->{storage}->set($channel, 'TOPIC', $topic, 1);
$self->{pbot}->{channels}->{storage}->set($channel, 'TOPIC_SET_BY', "$nick!$user\@$host", 1);
$self->{pbot}->{channels}->{storage}->set($channel, 'TOPIC_SET_ON', time);
}
return 1;
}
sub on_topicinfo($self, $event_type, $event) {
2023-01-31 14:44:34 +01:00
my (undef, $channel, $by, $timestamp) = $event->args;
$self->{pbot}->{logger}->log("Topic for $channel set by $by on " . localtime($timestamp) . "\n");
$self->{pbot}->{channels}->{storage}->set($channel, 'TOPIC_SET_BY', $by, 1);
$self->{pbot}->{channels}->{storage}->set($channel, 'TOPIC_SET_ON', $timestamp, 1);
return 1;
}
1;