1 # -*- Mode: perl; tab-width: 4; indent-tabs-mode: nil; -*-
2 ################################
4 ################################
6 package BotModules::General;
13 # RegisterConfig - Called when initialised, should call registerVariables
16 $self->SUPER::RegisterConfig(@_);
17 $self->registerVariables(
18 # [ name, save?, settable?, value ]
19 ['preferredHelpLineLength', 1, 1, 90],
20 ['helpStyle', 1, 1, 'compact'], # change this to 'tidy' to use alternate style
28 '' => 'The module that provides the bot-wide services.',
29 'help' => 'Gives information about modules and commands. Syntax: help [<topic>]',
30 'shutup' => 'Tells the bot to stop talking to you. Syntax: shut up',
34 # Told - Called for messages prefixed by the bot's nick
37 my ($event, $message) = @_;
38 if ($message =~ /^\s*(?:help|commands?)(?:\s+($variablepattern))?[ ?!.]*\s*$/osi) {
40 # display help for that command
41 # first, build the help file...
43 foreach my $module (@modules) {
46 $commands = $module->Help($event);
49 $self->debug("Module $module is having errors reporting help:\n$@");
52 if ($commands->{''}) {
53 my @commands = grep { /./os } keys %$commands;
54 $topicList{lc($module->{'_name'})} = [] unless defined($topicList{lc($module->{'_name'})});
55 push(@{$topicList{lc($module->{'_name'})}}, $commands->{''});
58 push(@{$topicList{lc($module->{'_name'})}}, "The $module->{'_name'} module has the following help topics: @commands");
61 foreach (keys %$commands) {
62 $topicList{lc($_)} = [] unless defined($topicList{lc($_)});
63 push(@{$topicList{lc($_)}}, $commands->{$_});
66 if (defined($topicList{lc($1)})) {
67 foreach (@{$topicList{lc($1)}}) {
68 $self->say($event, "$1: $_");
71 $self->say($event, "No help for topic '$1'.");
74 my $helpline = $self->getHelpLine();
75 $self->directSay($event, "Help topics for mozbot $VERSION ($helpline):");
76 $self->say($event, "$event->{'from'}: help info /msg'ed") if ($event->{'channel'});
77 if ($self->{'helpStyle'} eq 'compact') {
78 $self->printHelpCompact($event);
80 $self->printHelpTidy($event);
82 $self->directSay($event, 'For help on a particular topic, type \'help <topic>\'. Note that some commands may be disabled in certain channels.');
84 } elsif ($message =~ /^\s*shut\s*up\s*$/osi) {
85 my $queue = $self->getMessageQueue();
86 my @messages = @$queue;
89 if ($event->{'channel'}) {
90 foreach my $message (@messages) {
91 if ($message->[0] eq $event->{'channel'} and
92 ref $message->[1] eq 'SCALAR' and
93 $message->[1] =~ m/^\Q$event->{'from'}\E:/osi) {
96 push(@$queue, $message);
100 foreach my $message (@messages) {
101 if (lc $message->[0] eq lc $event->{'from'}) {
104 push(@$queue, $message);
109 $self->say($event, "$event->{'from'}: Dropped $count messages.");
111 $self->say($event, "$event->{'from'}: I wasn't talking to you.");
114 return $self->SUPER::Told(@_);
116 return 0; # dealt with it, do nothing else
121 my ($event, $who, $what) = @_;
122 my @modulenames = $self->getModules();
124 $self->ctcpReply($event, 'VERSION', "mozbot $VERSION (@modulenames)");
127 sub printHelpCompact {
130 local $" = ', '; # to reset font-lock: "
132 foreach my $module ($self->getModules()) {
133 $module = $self->getModule($module);
134 my %commands = %{$module->Help($event)};
135 my $moduleHelp = delete($commands{''});
136 my @commands = sort keys %commands;
138 push(@helplist, "$module->{'_name'}: @commands");
139 } elsif ($moduleHelp) {
140 push(@helplist, "$module->{'_name'}");
143 foreach ($self->prettyPrint($self->{'preferredHelpLineLength'}, undef, ' ', '; ', @helplist)) {
144 $self->directSay($event, $_);
151 my @modules = sort $self->getModules();
152 my $longestTitle = 0;
153 foreach my $module (@modules) {
154 $longestTitle = length($module) if length($module) > $longestTitle;
155 $module = [$module, sort keys %{$self->getModule($module)->Help($event)}];
157 foreach my $module (@modules) {
158 my $title = shift(@$module);
159 my $topicCount = @$module;
160 if (@$module and $module->[0] eq '') {
163 my @topics = @$module;
164 $module = ' ' x ($longestTitle - length($title)) . $title;
166 $self->directSay($event, $module . ': ' . join(",\n" . ' ' x ($longestTitle + 2), $self->wordWrap($self->{'preferredHelpLineLength'} - $longestTitle - 2, undef, undef, ', ', @topics)));
167 } elsif ($topicCount) {
168 $self->directSay($event, "$module: (no commands)");