2010-03-17 07:36:54 +01:00
|
|
|
# File: PBot.pm
|
|
|
|
# Author: pragma_
|
|
|
|
#
|
|
|
|
# Purpose: IRC Bot (3rd generation)
|
|
|
|
|
2017-03-05 22:33:31 +01:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2010-03-17 07:36:54 +01:00
|
|
|
package PBot::PBot;
|
|
|
|
|
2020-02-08 20:04:13 +01:00
|
|
|
use strict; use warnings;
|
2019-07-11 03:40:53 +02:00
|
|
|
use feature 'unicode_strings';
|
|
|
|
|
2010-03-17 07:36:54 +01:00
|
|
|
# unbuffer stdout
|
|
|
|
STDOUT->autoflush(1);
|
|
|
|
|
|
|
|
use Carp ();
|
|
|
|
use PBot::Logger;
|
2018-01-23 08:48:25 +01:00
|
|
|
use PBot::VERSION;
|
2020-02-08 20:04:13 +01:00
|
|
|
use PBot::HashObject;
|
|
|
|
use PBot::DualIndexHashObject;
|
2014-05-17 22:08:19 +02:00
|
|
|
use PBot::Registry;
|
2020-02-03 18:50:38 +01:00
|
|
|
use PBot::Capabilities;
|
2014-03-14 06:51:15 +01:00
|
|
|
use PBot::SelectHandler;
|
2010-03-17 07:36:54 +01:00
|
|
|
use PBot::StdinReader;
|
2011-01-22 09:35:31 +01:00
|
|
|
use PBot::IRC;
|
2014-11-01 01:15:21 +01:00
|
|
|
use PBot::EventDispatcher;
|
2010-03-22 08:33:44 +01:00
|
|
|
use PBot::IRCHandlers;
|
2010-03-23 19:24:02 +01:00
|
|
|
use PBot::Channels;
|
2011-02-13 06:07:02 +01:00
|
|
|
use PBot::BanTracker;
|
2014-11-15 02:18:33 +01:00
|
|
|
use PBot::NickList;
|
2011-01-25 00:56:55 +01:00
|
|
|
use PBot::LagChecker;
|
2014-05-13 12:15:52 +02:00
|
|
|
use PBot::MessageHistory;
|
2010-03-17 07:36:54 +01:00
|
|
|
use PBot::AntiFlood;
|
2018-08-06 07:41:08 +02:00
|
|
|
use PBot::AntiSpam;
|
2010-03-22 08:33:44 +01:00
|
|
|
use PBot::Interpreter;
|
|
|
|
use PBot::Commands;
|
|
|
|
use PBot::ChanOps;
|
2019-06-26 18:34:19 +02:00
|
|
|
use PBot::Factoids;
|
2020-01-25 21:28:05 +01:00
|
|
|
use PBot::Users;
|
2010-03-22 08:33:44 +01:00
|
|
|
use PBot::IgnoreList;
|
2015-03-17 05:08:25 +01:00
|
|
|
use PBot::BlackList;
|
2010-03-22 08:33:44 +01:00
|
|
|
use PBot::Timer;
|
2015-06-16 04:55:46 +02:00
|
|
|
use PBot::Refresher;
|
2015-09-07 07:52:39 +02:00
|
|
|
use PBot::Plugins;
|
2017-12-06 06:05:44 +01:00
|
|
|
use PBot::WebPaste;
|
2019-06-07 06:46:00 +02:00
|
|
|
use PBot::Utils::ParseDate;
|
2020-02-14 07:36:05 +01:00
|
|
|
use PBot::Functions;
|
2010-03-17 07:36:54 +01:00
|
|
|
|
|
|
|
sub new {
|
2020-02-08 20:04:13 +01:00
|
|
|
my ($proto, %conf) = @_;
|
|
|
|
my $class = ref($proto) || $proto;
|
2010-03-22 08:33:44 +01:00
|
|
|
my $self = bless {}, $class;
|
2019-12-29 20:06:52 +01:00
|
|
|
$self->initialize(%conf);
|
2010-03-22 08:33:44 +01:00
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub initialize {
|
|
|
|
my ($self, %conf) = @_;
|
2019-12-29 19:44:05 +01:00
|
|
|
$self->{startup_timestamp} = time;
|
2015-06-16 04:55:46 +02:00
|
|
|
|
2020-02-08 20:04:13 +01:00
|
|
|
$self->{atexit} = PBot::Registerable->new(%conf, pbot => $self);
|
|
|
|
$self->register_signal_handlers;
|
|
|
|
|
2019-12-22 04:04:39 +01:00
|
|
|
my $data_dir = $conf{data_dir};
|
|
|
|
my $module_dir = $conf{module_dir};
|
|
|
|
my $plugin_dir = $conf{plugin_dir};
|
|
|
|
|
|
|
|
# check command-line arguments for directory overrides
|
|
|
|
foreach my $arg (@ARGV) {
|
2020-01-17 05:42:58 +01:00
|
|
|
if ($arg =~ m/^-?(?:general\.)?((?:data|module|plugin)_dir)=(.*)$/) {
|
2019-12-22 04:04:39 +01:00
|
|
|
my $override = $1;
|
|
|
|
my $value = $2;
|
|
|
|
$data_dir = $value if $override eq 'data_dir';
|
|
|
|
$module_dir = $value if $override eq 'module_dir';
|
|
|
|
$plugin_dir = $value if $override eq 'plugin_dir';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-29 19:44:05 +01:00
|
|
|
# logger created first to allow other modules to log things
|
2019-12-29 20:06:52 +01:00
|
|
|
$self->{logger} = PBot::Logger->new(pbot => $self, filename => "$data_dir/log/log", %conf);
|
2019-12-29 19:44:05 +01:00
|
|
|
|
2019-12-22 04:04:39 +01:00
|
|
|
# make sure the environment is sane
|
|
|
|
if (not -d $data_dir) {
|
|
|
|
$self->{logger}->log("Data directory ($data_dir) does not exist; aborting...\n");
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (not -d $module_dir) {
|
|
|
|
$self->{logger}->log("Modules directory ($module_dir) does not exist; aborting...\n");
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (not -d $plugin_dir) {
|
|
|
|
$self->{logger}->log("Plugins directory ($plugin_dir) does not exist; aborting...\n");
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2020-02-03 18:50:38 +01:00
|
|
|
# then capabilities so commands can add new capabilities
|
|
|
|
$self->{capabilities} = PBot::Capabilities->new(pbot => $self, filename => "$data_dir/capabilities", %conf);
|
|
|
|
|
2020-01-20 05:00:01 +01:00
|
|
|
# then commands so the modules can register new commands
|
|
|
|
$self->{commands} = PBot::Commands->new(pbot => $self, filename => "$data_dir/commands", %conf);
|
|
|
|
|
2020-01-25 21:28:05 +01:00
|
|
|
# add some commands
|
2020-02-03 18:50:38 +01:00
|
|
|
$self->{commands}->register(sub { $self->listcmd(@_) }, "list");
|
|
|
|
$self->{commands}->register(sub { $self->ack_die(@_) }, "die", 1);
|
|
|
|
$self->{commands}->register(sub { $self->export(@_) }, "export", 1);
|
|
|
|
$self->{commands}->register(sub { $self->reload(@_) }, "reload", 1);
|
|
|
|
$self->{commands}->register(sub { $self->evalcmd(@_) }, "eval", 1);
|
|
|
|
$self->{commands}->register(sub { $self->sl(@_) }, "sl", 1);
|
|
|
|
|
|
|
|
# add 'cap' capability command
|
|
|
|
$self->{commands}->register(sub { $self->{capabilities}->capcmd(@_) }, "cap");
|
2020-01-25 21:28:05 +01:00
|
|
|
|
|
|
|
# prepare the version
|
2020-02-08 20:04:13 +01:00
|
|
|
$self->{version} = PBot::VERSION->new(pbot => $self, %conf);
|
2020-01-20 05:00:01 +01:00
|
|
|
$self->{logger}->log($self->{version}->version . "\n");
|
|
|
|
$self->{logger}->log("Args: @ARGV\n") if @ARGV;
|
|
|
|
|
|
|
|
# log the configured paths
|
2019-12-22 04:04:39 +01:00
|
|
|
$self->{logger}->log("data_dir: $data_dir\n");
|
|
|
|
$self->{logger}->log("module_dir: $module_dir\n");
|
|
|
|
$self->{logger}->log("plugin_dir: $plugin_dir\n");
|
2014-05-17 22:08:19 +02:00
|
|
|
|
2020-02-08 20:04:13 +01:00
|
|
|
$self->{timer} = PBot::Timer->new(pbot => $self, timeout => 10, %conf);
|
2020-02-14 07:36:05 +01:00
|
|
|
$self->{functions} = PBot::Functions->new(pbot => $self, %conf);
|
2020-01-19 06:49:55 +01:00
|
|
|
$self->{refresher} = PBot::Refresher->new(pbot => $self);
|
|
|
|
|
2019-12-22 04:04:39 +01:00
|
|
|
# create registry and set some defaults
|
|
|
|
$self->{registry} = PBot::Registry->new(pbot => $self, filename => "$data_dir/registry", %conf);
|
2014-05-17 22:08:19 +02:00
|
|
|
|
2019-12-22 04:04:39 +01:00
|
|
|
$self->{registry}->add_default('text', 'general', 'data_dir', $data_dir);
|
|
|
|
$self->{registry}->add_default('text', 'general', 'module_dir', $module_dir);
|
|
|
|
$self->{registry}->add_default('text', 'general', 'plugin_dir', $plugin_dir);
|
2017-08-06 06:38:46 +02:00
|
|
|
$self->{registry}->add_default('text', 'general', 'trigger', $conf{trigger} // '!');
|
|
|
|
|
|
|
|
$self->{registry}->add_default('text', 'irc', 'debug', $conf{irc_debug} // 0);
|
|
|
|
$self->{registry}->add_default('text', 'irc', 'show_motd', $conf{show_motd} // 1);
|
|
|
|
$self->{registry}->add_default('text', 'irc', 'max_msg_len', $conf{max_msg_len} // 425);
|
2020-01-01 08:28:25 +01:00
|
|
|
$self->{registry}->add_default('text', 'irc', 'server', $conf{server} // "irc.freenode.net");
|
2017-08-06 06:38:46 +02:00
|
|
|
$self->{registry}->add_default('text', 'irc', 'port', $conf{port} // 6667);
|
|
|
|
$self->{registry}->add_default('text', 'irc', 'SSL', $conf{SSL} // 0);
|
|
|
|
$self->{registry}->add_default('text', 'irc', 'SSL_ca_file', $conf{SSL_ca_file} // 'none');
|
|
|
|
$self->{registry}->add_default('text', 'irc', 'SSL_ca_path', $conf{SSL_ca_path} // 'none');
|
2019-12-22 04:04:39 +01:00
|
|
|
$self->{registry}->add_default('text', 'irc', 'botnick', $conf{botnick} // "");
|
2017-08-06 06:38:46 +02:00
|
|
|
$self->{registry}->add_default('text', 'irc', 'username', $conf{username} // "pbot3");
|
2020-01-01 08:28:25 +01:00
|
|
|
$self->{registry}->add_default('text', 'irc', 'realname', $conf{realname} // "https://github.com/pragma-/pbot");
|
2019-12-22 04:04:39 +01:00
|
|
|
$self->{registry}->add_default('text', 'irc', 'identify_password', $conf{identify_password} // '');
|
2014-08-11 09:34:30 +02:00
|
|
|
$self->{registry}->add_default('text', 'irc', 'log_default_handler', 1);
|
2019-06-26 18:34:19 +02:00
|
|
|
|
2015-07-06 02:48:27 +02:00
|
|
|
$self->{registry}->set_default('irc', 'SSL_ca_file', 'private', 1);
|
|
|
|
$self->{registry}->set_default('irc', 'SSL_ca_path', 'private', 1);
|
|
|
|
$self->{registry}->set_default('irc', 'identify_password', 'private', 1);
|
2014-05-17 22:08:19 +02:00
|
|
|
|
2019-12-22 04:04:39 +01:00
|
|
|
# load existing registry entries from file (if exists) to overwrite defaults
|
|
|
|
if (-e $self->{registry}->{registry}->{filename}) {
|
|
|
|
$self->{registry}->load;
|
|
|
|
}
|
|
|
|
|
|
|
|
# update important paths
|
|
|
|
$self->{registry}->set('general', 'data_dir', 'value', $data_dir, 0, 1);
|
|
|
|
$self->{registry}->set('general', 'module_dir', 'value', $module_dir, 0, 1);
|
|
|
|
$self->{registry}->set('general', 'plugin_dir', 'value', $plugin_dir, 0, 1);
|
|
|
|
|
|
|
|
# override registry entries with command-line arguments, if any
|
|
|
|
foreach my $arg (@ARGV) {
|
2020-01-17 05:42:58 +01:00
|
|
|
next if $arg =~ m/^-?(?:general\.)?(?:config|data|module|plugin)_dir=.*$/; # already processed
|
2019-12-22 04:04:39 +01:00
|
|
|
my ($item, $value) = split /=/, $arg, 2;
|
|
|
|
|
|
|
|
if (not defined $item or not defined $value) {
|
|
|
|
$self->{logger}->log("Fatal error: unknown argument `$arg`; arguments must be in the form of `section.key=value` (e.g.: irc.botnick=newnick)\n");
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
my ($section, $key) = split /\./, $item, 2;
|
|
|
|
|
|
|
|
if (not defined $section or not defined $key) {
|
|
|
|
$self->{logger}->log("Fatal error: bad argument `$arg`; registry entries must be in the form of section.key (e.g.: irc.botnick)\n");
|
2020-01-26 04:55:08 +01:00
|
|
|
exit;
|
2019-12-22 04:04:39 +01:00
|
|
|
}
|
|
|
|
|
2020-01-17 06:30:01 +01:00
|
|
|
$section =~ s/^-//; # remove a leading - to allow arguments like -irc.botnick due to habitual use of -args
|
|
|
|
|
2019-12-22 04:04:39 +01:00
|
|
|
$self->{logger}->log("Overriding $section.$key to $value\n");
|
|
|
|
$self->{registry}->set($section, $key, 'value', $value, 0, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
# registry triggers fire when value changes
|
2014-05-17 22:08:19 +02:00
|
|
|
$self->{registry}->add_trigger('irc', 'botnick', sub { $self->change_botnick_trigger(@_) });
|
2014-05-31 03:03:42 +02:00
|
|
|
$self->{registry}->add_trigger('irc', 'debug', sub { $self->irc_debug_trigger(@_) });
|
2019-06-26 18:34:19 +02:00
|
|
|
|
2019-12-22 04:04:39 +01:00
|
|
|
# ensure user has attempted to configure the bot
|
|
|
|
if (not length $self->{registry}->get_value('irc', 'botnick')) {
|
|
|
|
$self->{logger}->log("Fatal error: IRC nickname not defined; please set registry key irc.botnick in $data_dir/registry to continue.\n");
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2014-12-28 01:44:15 +01:00
|
|
|
$self->{event_dispatcher} = PBot::EventDispatcher->new(pbot => $self, %conf);
|
2016-09-22 17:26:42 +02:00
|
|
|
$self->{irchandlers} = PBot::IRCHandlers->new(pbot => $self, %conf);
|
2014-12-28 01:44:15 +01:00
|
|
|
$self->{select_handler} = PBot::SelectHandler->new(pbot => $self, %conf);
|
2020-01-25 21:28:05 +01:00
|
|
|
$self->{users} = PBot::Users->new(pbot => $self, filename => "$data_dir/users", %conf);
|
2020-01-24 05:47:18 +01:00
|
|
|
$self->{stdin_reader} = PBot::StdinReader->new(pbot => $self, %conf);
|
2014-12-28 01:44:15 +01:00
|
|
|
$self->{bantracker} = PBot::BanTracker->new(pbot => $self, %conf);
|
|
|
|
$self->{lagchecker} = PBot::LagChecker->new(pbot => $self, %conf);
|
2019-12-22 04:04:39 +01:00
|
|
|
$self->{messagehistory} = PBot::MessageHistory->new(pbot => $self, filename => "$data_dir/message_history.sqlite3", %conf);
|
2014-12-28 01:44:15 +01:00
|
|
|
$self->{antiflood} = PBot::AntiFlood->new(pbot => $self, %conf);
|
2018-08-06 07:41:08 +02:00
|
|
|
$self->{antispam} = PBot::AntiSpam->new(pbot => $self, %conf);
|
2019-12-22 04:04:39 +01:00
|
|
|
$self->{ignorelist} = PBot::IgnoreList->new(pbot => $self, filename => "$data_dir/ignorelist", %conf);
|
|
|
|
$self->{blacklist} = PBot::BlackList->new(pbot => $self, filename => "$data_dir/blacklist", %conf);
|
2014-12-28 01:44:15 +01:00
|
|
|
$self->{irc} = PBot::IRC->new();
|
2019-12-22 04:04:39 +01:00
|
|
|
$self->{channels} = PBot::Channels->new(pbot => $self, filename => "$data_dir/channels", %conf);
|
2014-12-28 01:44:15 +01:00
|
|
|
$self->{chanops} = PBot::ChanOps->new(pbot => $self, %conf);
|
2016-09-22 17:26:42 +02:00
|
|
|
$self->{nicklist} = PBot::NickList->new(pbot => $self, %conf);
|
2017-12-06 06:05:44 +01:00
|
|
|
$self->{webpaste} = PBot::WebPaste->new(pbot => $self, %conf);
|
2019-06-07 06:46:00 +02:00
|
|
|
$self->{parsedate} = PBot::Utils::ParseDate->new(pbot => $self, %conf);
|
2014-12-28 01:44:15 +01:00
|
|
|
|
|
|
|
$self->{interpreter} = PBot::Interpreter->new(pbot => $self, %conf);
|
2020-02-06 10:07:44 +01:00
|
|
|
$self->{interpreter}->register(sub { $self->{commands}->interpreter(@_) });
|
|
|
|
$self->{interpreter}->register(sub { $self->{factoids}->interpreter(@_) });
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2019-12-31 04:57:47 +01:00
|
|
|
$self->{factoids} = PBot::Factoids->new(pbot => $self, filename => "$data_dir/factoids", %conf);
|
2010-03-23 19:24:02 +01:00
|
|
|
|
2015-09-07 07:52:39 +02:00
|
|
|
$self->{plugins} = PBot::Plugins->new(pbot => $self, %conf);
|
2015-09-07 07:17:07 +02:00
|
|
|
|
2019-05-09 06:07:57 +02:00
|
|
|
# load available plugins
|
|
|
|
$self->{plugins}->autoload(%conf);
|
|
|
|
|
2014-05-17 22:08:19 +02:00
|
|
|
# start timer
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{timer}->start();
|
2020-02-03 18:50:38 +01:00
|
|
|
|
|
|
|
# give botowner all capabilities
|
|
|
|
$self->{capabilities}->rebuild_botowner_capabilities();
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
|
|
|
|
2015-01-11 00:56:43 +01:00
|
|
|
sub random_nick {
|
2020-01-25 21:28:05 +01:00
|
|
|
my ($self, $length) = @_;
|
|
|
|
$length //= 9;
|
2015-01-11 00:56:43 +01:00
|
|
|
my @chars = ("A".."Z", "a".."z", "0".."9");
|
2015-02-14 13:01:09 +01:00
|
|
|
my $nick = $chars[rand @chars - 10]; # nicks cannot start with a digit
|
2020-01-25 21:28:05 +01:00
|
|
|
$nick .= $chars[rand @chars] for 1..$length;
|
2015-01-11 00:56:43 +01:00
|
|
|
return $nick;
|
|
|
|
}
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2015-01-11 00:56:43 +01:00
|
|
|
# TODO: add disconnect subroutine
|
2010-03-17 07:36:54 +01:00
|
|
|
sub connect {
|
|
|
|
my ($self, $server) = @_;
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if ($self->{connected}) {
|
2010-03-22 08:33:44 +01:00
|
|
|
# TODO: disconnect, clean-up, etc
|
|
|
|
}
|
|
|
|
|
2020-01-01 08:28:25 +01:00
|
|
|
$server = $self->{registry}->get_value('irc', 'server') if not defined $server;
|
2014-05-17 22:08:19 +02:00
|
|
|
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{logger}->log("Connecting to $server ...\n");
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2019-06-26 18:34:19 +02:00
|
|
|
while (not $self->{conn} = $self->{irc}->newconn(
|
2020-01-25 21:28:05 +01:00
|
|
|
Nick => $self->{registry}->get_value('irc', 'randomize_nick') ? $self->random_nick : $self->{registry}->get_value('irc', 'botnick'),
|
2014-11-01 01:15:21 +01:00
|
|
|
Username => $self->{registry}->get_value('irc', 'username'),
|
2020-01-01 08:28:25 +01:00
|
|
|
Ircname => $self->{registry}->get_value('irc', 'realname'),
|
2014-11-01 01:15:21 +01:00
|
|
|
Server => $server,
|
2018-07-01 12:07:30 +02:00
|
|
|
Pacing => 1,
|
2019-07-01 00:08:18 +02:00
|
|
|
UTF8 => 1,
|
2014-11-01 01:15:21 +01:00
|
|
|
SSL => $self->{registry}->get_value('irc', 'SSL'),
|
|
|
|
SSL_ca_file => $self->{registry}->get_value('irc', 'SSL_ca_file'),
|
|
|
|
SSL_ca_path => $self->{registry}->get_value('irc', 'SSL_ca_path'),
|
|
|
|
Port => $self->{registry}->get_value('irc', 'port'))) {
|
2015-01-23 14:35:06 +01:00
|
|
|
$self->{logger}->log("$0: Can't connect to $server:" . $self->{registry}->get_value('irc', 'port') . ". Retrying in 15 seconds...\n");
|
2014-11-01 01:15:21 +01:00
|
|
|
sleep 15;
|
|
|
|
}
|
2010-03-22 08:33:44 +01:00
|
|
|
|
|
|
|
$self->{connected} = 1;
|
|
|
|
|
2014-08-11 09:34:30 +02:00
|
|
|
#set up handlers for the IRC engine
|
|
|
|
$self->{conn}->add_default_handler(sub { $self->{irchandlers}->default_handler(@_) }, 1);
|
2014-11-01 01:15:21 +01:00
|
|
|
$self->{conn}->add_handler([ 251,252,253,254,255,302 ], sub { $self->{irchandlers}->on_init(@_) });
|
2014-08-11 09:34:30 +02:00
|
|
|
|
|
|
|
# ignore these events
|
2019-06-26 18:34:19 +02:00
|
|
|
$self->{conn}->add_handler(['whoisserver',
|
|
|
|
'whoiscountry',
|
2014-08-11 09:34:30 +02:00
|
|
|
'whoischannels',
|
|
|
|
'whoisidle',
|
|
|
|
'motdstart',
|
2014-11-01 01:15:21 +01:00
|
|
|
'endofmotd',
|
2014-08-11 09:34:30 +02:00
|
|
|
'away',
|
|
|
|
'endofbanlist'], sub {});
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#main loop
|
|
|
|
sub do_one_loop {
|
2010-03-22 08:33:44 +01:00
|
|
|
my $self = shift;
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{irc}->do_one_loop();
|
2014-03-14 06:51:15 +01:00
|
|
|
$self->{select_handler}->do_select();
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub start {
|
|
|
|
my $self = shift;
|
2019-06-26 18:34:19 +02:00
|
|
|
while (1) {
|
2014-11-01 01:15:21 +01:00
|
|
|
$self->connect() if not $self->{connected};
|
2019-06-26 18:34:19 +02:00
|
|
|
$self->do_one_loop() if $self->{connected};
|
2014-11-01 01:15:21 +01:00
|
|
|
}
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
|
|
|
|
2014-05-17 00:11:31 +02:00
|
|
|
sub register_signal_handlers {
|
|
|
|
my $self = shift;
|
|
|
|
$SIG{INT} = sub { $self->atexit; exit 0; };
|
|
|
|
}
|
|
|
|
|
|
|
|
sub atexit {
|
|
|
|
my $self = shift;
|
|
|
|
$self->{atexit}->execute_all;
|
|
|
|
}
|
|
|
|
|
2014-05-31 03:03:42 +02:00
|
|
|
sub irc_debug_trigger {
|
|
|
|
my ($self, $section, $item, $newvalue) = @_;
|
|
|
|
$self->{irc}->debug($newvalue);
|
|
|
|
$self->{conn}->debug($newvalue) if $self->{connected};
|
|
|
|
}
|
|
|
|
|
2014-05-17 22:08:19 +02:00
|
|
|
sub change_botnick_trigger {
|
|
|
|
my ($self, $section, $item, $newvalue) = @_;
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{conn}->nick($newvalue) if $self->{connected};
|
2010-03-23 19:24:02 +01:00
|
|
|
}
|
|
|
|
|
2020-01-25 21:28:05 +01:00
|
|
|
sub listcmd {
|
|
|
|
my $self = shift;
|
|
|
|
my ($from, $nick, $user, $host, $arguments) = @_;
|
|
|
|
my $text;
|
|
|
|
|
2020-02-10 04:29:32 +01:00
|
|
|
my $usage = "Usage: list <modules|commands>";
|
2020-01-25 21:28:05 +01:00
|
|
|
|
|
|
|
if (not defined $arguments) {
|
|
|
|
return $usage;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($arguments =~ /^modules$/i) {
|
|
|
|
$text = "Loaded modules: ";
|
2020-02-13 22:31:36 +01:00
|
|
|
foreach my $channel (sort $self->{factoids}->{factoids}->get_keys) {
|
|
|
|
foreach my $command (sort $self->{factoids}->{factoids}->get_keys($channel)) {
|
2020-01-25 21:28:05 +01:00
|
|
|
next if $command eq '_name';
|
2020-02-13 22:31:36 +01:00
|
|
|
if ($self->{factoids}->{factoids}->get_data($channel, $command, 'type') eq 'module') {
|
|
|
|
$text .= $self->{factoids}->{factoids}->get_data($channel, $command, '_name') . ' ';
|
2020-01-25 21:28:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($arguments =~ /^commands$/i) {
|
|
|
|
$text = "Registered commands: ";
|
|
|
|
foreach my $command (sort { $a->{name} cmp $b->{name} } @{ $self->{commands}->{handlers} }) {
|
2020-02-04 02:19:04 +01:00
|
|
|
if ($command->{requires_cap}) {
|
|
|
|
$text .= "+$command->{name} ";
|
|
|
|
} else {
|
|
|
|
$text .= "$command->{name} ";
|
|
|
|
}
|
2020-01-25 21:28:05 +01:00
|
|
|
}
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
return $usage;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub sl {
|
|
|
|
my $self = shift;
|
|
|
|
my ($from, $nick, $user, $host, $arguments) = @_;
|
|
|
|
return "Usage: sl <ircd command>" if not length $arguments;
|
|
|
|
$self->{conn}->sl($arguments);
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
sub ack_die {
|
|
|
|
my $self = shift;
|
|
|
|
my ($from, $nick, $user, $host, $arguments) = @_;
|
|
|
|
$self->{logger}->log("$nick!$user\@$host made me exit.\n");
|
|
|
|
$self->atexit();
|
|
|
|
$self->{conn}->privmsg($from, "Good-bye.") if defined $from;
|
|
|
|
$self->{conn}->quit("Departure requested.");
|
|
|
|
exit 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub export {
|
|
|
|
my $self = shift;
|
|
|
|
my ($from, $nick, $user, $host, $arguments) = @_;
|
|
|
|
|
2020-01-29 22:40:48 +01:00
|
|
|
return "Usage: export <factoids>" if not defined $arguments;
|
2020-01-25 21:28:05 +01:00
|
|
|
|
|
|
|
if ($arguments =~ /^factoids$/i) {
|
|
|
|
return $self->{factoids}->export_factoids;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub evalcmd {
|
|
|
|
my ($self, $from, $nick, $user, $host, $arguments) = @_;
|
|
|
|
|
|
|
|
$self->{logger}->log("[$from] $nick!$user\@$host Evaluating [$arguments]\n");
|
|
|
|
|
2020-02-10 04:29:32 +01:00
|
|
|
my $ret = '';
|
2020-01-25 21:28:05 +01:00
|
|
|
my $result = eval $arguments;
|
|
|
|
if ($@) {
|
|
|
|
if (length $result) {
|
|
|
|
$ret .= "[Error: $@] ";
|
|
|
|
} else {
|
|
|
|
$ret .= "Error: $@";
|
|
|
|
}
|
|
|
|
$ret =~ s/ at \(eval \d+\) line 1.//;
|
|
|
|
}
|
2020-02-10 04:29:32 +01:00
|
|
|
$result = 'Undefined.' if not defined $result;
|
|
|
|
$result = 'No output.' if not length $result;
|
2020-01-25 21:28:05 +01:00
|
|
|
return "/say $ret $result";
|
|
|
|
}
|
|
|
|
|
|
|
|
sub reload {
|
|
|
|
my $self = shift;
|
|
|
|
my ($from, $nick, $user, $host, $arguments) = @_;
|
|
|
|
|
|
|
|
my %reloadables = (
|
2020-02-12 06:14:27 +01:00
|
|
|
'capabilities' => sub {
|
2020-02-12 06:49:43 +01:00
|
|
|
$self->{capabilities}->{caps}->load;
|
2020-02-12 06:14:27 +01:00
|
|
|
return "Capabilities reloaded.";
|
|
|
|
},
|
|
|
|
|
2020-01-25 21:28:05 +01:00
|
|
|
'commands' => sub {
|
2020-02-12 06:49:43 +01:00
|
|
|
$self->{commands}->{metadata}->load;
|
2020-01-25 21:28:05 +01:00
|
|
|
return "Commands metadata reloaded.";
|
|
|
|
},
|
|
|
|
|
|
|
|
'blacklist' => sub {
|
|
|
|
$self->{blacklist}->clear_blacklist;
|
|
|
|
$self->{blacklist}->load_blacklist;
|
|
|
|
return "Blacklist reloaded.";
|
|
|
|
},
|
|
|
|
|
2020-02-12 06:19:31 +01:00
|
|
|
'ban-exemptions' => sub {
|
|
|
|
$self->{antiflood}->{'ban-exemptions'}->clear;
|
|
|
|
$self->{antiflood}->{'ban-exemptions'}->load;
|
|
|
|
return "Ban exemptions reloaded.";
|
2020-01-25 21:28:05 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
'ignores' => sub {
|
|
|
|
$self->{ignorelist}->clear_ignores;
|
|
|
|
$self->{ignorelist}->load_ignores;
|
|
|
|
return "Ignore list reloaded.";
|
|
|
|
},
|
|
|
|
|
|
|
|
'users' => sub {
|
|
|
|
$self->{users}->load;
|
|
|
|
return "Users reloaded.";
|
|
|
|
},
|
|
|
|
|
|
|
|
'channels' => sub {
|
2020-02-12 06:49:43 +01:00
|
|
|
$self->{channels}->{channels}->load;
|
2020-01-25 21:28:05 +01:00
|
|
|
return "Channels reloaded.";
|
|
|
|
},
|
|
|
|
|
|
|
|
'bantimeouts' => sub {
|
|
|
|
$self->{chanops}->{unban_timeout}->clear;
|
|
|
|
$self->{chanops}->{unban_timeout}->load;
|
|
|
|
return "Ban timeouts reloaded.";
|
|
|
|
},
|
|
|
|
|
|
|
|
'mutetimeouts' => sub {
|
|
|
|
$self->{chanops}->{unmute_timeout}->clear;
|
|
|
|
$self->{chanops}->{unmute_timeout}->load;
|
|
|
|
return "Mute timeouts reloaded.";
|
|
|
|
},
|
|
|
|
|
|
|
|
'registry' => sub {
|
|
|
|
$self->{registry}->{registry}->clear;
|
|
|
|
$self->{registry}->load;
|
|
|
|
return "Registry reloaded.";
|
|
|
|
},
|
|
|
|
|
|
|
|
'factoids' => sub {
|
|
|
|
$self->{factoids}->{factoids}->clear;
|
|
|
|
$self->{factoids}->load_factoids;
|
|
|
|
return "Factoids reloaded.";
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
if (not length $arguments or not exists $reloadables{$arguments}) {
|
|
|
|
my $usage = 'Usage: reload <';
|
|
|
|
$usage .= join '|', sort keys %reloadables;
|
|
|
|
$usage .= '>';
|
|
|
|
return $usage;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $reloadables{$arguments}();
|
|
|
|
}
|
|
|
|
|
2010-03-17 07:36:54 +01:00
|
|
|
1;
|