]> git.somenet.org - irc/bugbot.git/blob - BotModules/General.bm
some old base
[irc/bugbot.git] / BotModules / General.bm
1 # -*- Mode: perl; tab-width: 4; indent-tabs-mode: nil; -*-
2 ################################
3 # General Module               #
4 ################################
5
6 package BotModules::General;
7 use vars qw(@ISA);
8 @ISA = qw(BotModules);
9 1;
10
11 my $VERSION = '2.6';
12
13 # RegisterConfig - Called when initialised, should call registerVariables
14 sub RegisterConfig {
15     my $self = shift;
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
21     );
22 }
23
24 sub Help {
25     my $self = shift;
26     my ($event) = @_;
27     return {
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',
31     };
32 }
33
34 # Told - Called for messages prefixed by the bot's nick
35 sub Told {
36     my $self = shift;
37     my ($event, $message) = @_;
38     if ($message =~ /^\s*(?:help|commands?)(?:\s+($variablepattern))?[ ?!.]*\s*$/osi) {
39         if ($1) {
40             # display help for that command
41             # first, build the help file...
42             my %topicList;
43             foreach my $module (@modules) {
44                 my $commands;
45                 eval {
46                     $commands = $module->Help($event);
47                 };
48                 if ($@) {
49                     $self->debug("Module $module is having errors reporting help:\n$@");
50                     next;
51                 }
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->{''});
56                     if (@commands) {
57                         local $" = ', ';
58                         push(@{$topicList{lc($module->{'_name'})}}, "The $module->{'_name'} module has the following help topics: @commands");
59                     }
60                 }
61                 foreach (keys %$commands) {
62                     $topicList{lc($_)} = [] unless defined($topicList{lc($_)});
63                     push(@{$topicList{lc($_)}}, $commands->{$_});
64                 }
65             }
66             if (defined($topicList{lc($1)})) {
67                 foreach (@{$topicList{lc($1)}}) {
68                     $self->say($event, "$1: $_");
69                 }
70             } else {
71                 $self->say($event, "No help for topic '$1'.");
72             }
73         } else {
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);
79             } else {
80                 $self->printHelpTidy($event);
81             }
82             $self->directSay($event, 'For help on a particular topic, type \'help <topic>\'. Note that some commands may be disabled in certain channels.');
83         }
84     } elsif ($message =~ /^\s*shut\s*up\s*$/osi) {
85         my $queue = $self->getMessageQueue();
86         my @messages = @$queue;
87         @$queue = ();
88         my $count = 0;
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) {
94                     ++$count;
95                 } else {
96                     push(@$queue, $message);
97                 }
98             }
99         } else {
100             foreach my $message (@messages) {
101                 if (lc $message->[0] eq lc $event->{'from'}) {
102                     ++$count;
103                 } else {
104                     push(@$queue, $message);
105                 }
106             }
107         }
108         if ($count) {
109             $self->say($event, "$event->{'from'}: Dropped $count messages.");
110         } else {
111             $self->say($event, "$event->{'from'}: I wasn't talking to you.");
112         }
113     } else {
114         return $self->SUPER::Told(@_);
115     }
116     return 0; # dealt with it, do nothing else
117 }
118
119 sub CTCPVersion {
120     my $self = shift;
121     my ($event, $who, $what) = @_;
122     my @modulenames = $self->getModules();
123     local $" = ', ';
124     $self->ctcpReply($event, 'VERSION', "mozbot $VERSION (@modulenames)");
125 }
126
127 sub printHelpCompact {
128     my $self = shift;
129     my ($event) = @_;
130     local $" = ', '; # to reset font-lock: "
131     my @helplist;
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;
137         if (@commands) {
138             push(@helplist, "$module->{'_name'}: @commands");
139         } elsif ($moduleHelp) {
140             push(@helplist, "$module->{'_name'}");
141         }
142     }
143     foreach ($self->prettyPrint($self->{'preferredHelpLineLength'}, undef, '  ', ';  ', @helplist)) {
144         $self->directSay($event, $_);
145     }
146 }
147
148 sub printHelpTidy {
149     my $self = shift;
150     my ($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)}];
156     }
157     foreach my $module (@modules) {
158         my $title = shift(@$module);
159         my $topicCount = @$module;
160         if (@$module and $module->[0] eq '') {
161             shift(@$module);
162         }
163         my @topics = @$module;
164         $module = ' ' x ($longestTitle - length($title)) . $title;
165         if (@topics) {
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)");
169         }
170     }
171 }