pjs/webtools/mozbot/BotModules/General.bm

94 строки
3.4 KiB
Plaintext

# -*- Mode: perl; tab-width: 4; indent-tabs-mode: nil; -*-
################################
# General Module #
################################
package BotModules::General;
use vars qw(@ISA);
@ISA = qw(BotModules);
1;
my $VERSION = '2.5';
# RegisterConfig - Called when initialised, should call registerVariables
sub RegisterConfig {
my $self = shift;
$self->SUPER::RegisterConfig(@_);
$self->registerVariables(
# [ name, save?, settable?, value ]
['preferredHelpLineLength', 1, 1, 90],
);
}
sub Help {
my $self = shift;
my ($event) = @_;
return {
'' => 'The module that provides the bot-wide services.',
'help' => 'Gives information about modules and commands. Syntax: help [<topic>]',
};
}
# Told - Called for messages prefixed by the bot's nick
sub Told {
my $self = shift;
my ($event, $message) = @_;
if ($message =~ /^\s*help(?:\s+($variablepattern))?[ ?!.]*\s*$/osi) {
if ($1) {
# display help for that command
# first, build the help file...
my %topicList;
foreach my $module (@modules) {
my $commands = $module->Help($event);
if ($commands->{''}) {
$topicList{lc($module->{'_name'})} = [] unless defined($topicList{lc($module->{'_name'})});
push(@{$topicList{lc($module->{'_name'})}}, $commands->{''});
}
foreach (keys %$commands) {
$topicList{lc($_)} = [] unless defined($topicList{lc($_)});
push(@{$topicList{lc($_)}}, $commands->{lc($_)});
}
}
if (defined($topicList{lc($1)})) {
foreach (@{$topicList{lc($1)}}) {
$self->say($event, "$1: $_");
}
} else {
$self->say($event, "No help for topic '$1'.");
}
} else {
my $helpline = $self->getHelpLine();
$self->directSay($event, "Help topics for mozbot $VERSION ($helpline):");
$self->say($event, "$event->{'from'}: help info /msg'ed") if ($event->{'channel'});
local @" = ', '; # to reset font-lock: "
my @helplist;
foreach my $module ($self->getModules()) {
$module = $self->getModule($module);
my %commands = %{$module->Help($event)};
my $moduleHelp = delete($commands{''});
my @commands = sort keys %commands;
if (@commands) {
push(@helplist, "$module->{'_name'}: @commands");
} elsif ($moduleHelp) {
push(@helplist, "$module->{'_name'}");
}
}
foreach ($self->prettyPrint($self->{'preferredHelpLineLength'}, undef, ' ', '; ', @helplist)) {
$self->directSay($event, $_);
}
$self->directSay($event, 'For help on a particular topic, type \'help <topic>\'. Note that some commands may be disabled in certain channels.');
}
} else {
return $self->SUPER::Told(@_);
}
return 0; # dealt with it, do nothing else
}
sub CTCPVersion {
my $self = shift;
my ($event, $who, $what) = @_;
my @modulenames = $self->getModules();
local $" = ', ';
$self->ctcpReply($event, 'VERSION', "mozbot $VERSION (@modulenames)");
}