| 1 | package Amethyst::Brain::Infobot::Module::Help; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use vars qw(@ISA); | ||
| 5 | use Amethyst::Message; | ||
| 6 | use Amethyst::Store; | ||
| 7 | use Amethyst::Brain::Infobot; | ||
| 8 | use Amethyst::Brain::Infobot::Module; | ||
| 9 | |||
| 10 | @ISA = qw(Amethyst::Brain::Infobot::Module); | ||
| 11 | |||
| 12 | sub new { | ||
| 13 | my $class = shift; | ||
| 14 | my $self = $class->SUPER::new( | ||
| 15 | Name => 'Help', | ||
| 16 | Regex => qr/^\s*help\b/i, | ||
| 17 | Usage => 'help [<module>]', | ||
| 18 | Description => "Help handler", | ||
| 19 | @_ | ||
| 20 | ); | ||
| 21 | |||
| 22 | return bless $self, $class; | ||
| 23 | } | ||
| 24 | |||
| 25 | sub action { | ||
| 26 | my ($self, $message) = @_; | ||
| 27 | |||
| 28 | my $content = $message->content; | ||
| 29 | |||
| 30 | $content =~ s/\s+/ /g; | ||
| 31 | |||
| 32 | my $re = qr/(\(.*?\)|[^(++)(--)\s]+)(\+\+|--)/; | ||
| 33 | |||
| 34 | my $infobot = $self->{Infobot}; | ||
| 35 | my @modules = @{$infobot->{Modules}}; | ||
| 36 | |||
| 37 | if ($content =~ /^\s*help (.*)/) { | ||
| 38 | my $rq = lc $1; | ||
| 39 | foreach my $module (@modules) { | ||
| 40 | if (lc $module->{Name} eq $rq) { | ||
| 41 | my $msg = $module->{Name}; | ||
| 42 | $msg .= " ($module->{Description})" | ||
| 43 | if $module->{Description}; | ||
| 44 | $msg .= ": $module->{Usage}" | ||
| 45 | if $module->{Usage}; | ||
| 46 | my $reply = $self->reply_to($message, $msg); | ||
| 47 | $reply->send; | ||
| 48 | return 1; | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | my $reply = $self->reply_to($message, "Module $rq not found"); | ||
| 53 | $reply->send; | ||
| 54 | return 1; | ||
| 55 | } | ||
| 56 | elsif ($content =~ /^\s*help\b/i) { | ||
| 57 | my @names = map { $_->{Name} } @modules; | ||
| 58 | my $reply = $self->reply_to($message, "Available modules: " . | ||
| 59 | join(", ", @names)); | ||
| 60 | $reply->send; | ||
| 61 | return 1; | ||
| 62 | } | ||
| 63 | |||
| 64 | return undef; | ||
| 65 | } | ||
| 66 | |||
| 67 | 1; |