| 1 | package Amethyst::Brain::Infobot::Module; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Amethyst::Message; | ||
| 5 | |||
| 6 | sub new { | ||
| 7 | my $class = shift; | ||
| 8 | my $self = ($#_ == 0) ? { %{ (shift) } } : { @_ }; | ||
| 9 | |||
| 10 | die "No Name in $class" unless $self->{Name}; | ||
| 11 | warn "No Description in $class" unless $self->{Description}; | ||
| 12 | warn "No Usage in $class" unless $self->{Usage}; | ||
| 13 | |||
| 14 | if ($class->can('process') ne __PACKAGE__) { | ||
| 15 | # Custom process sub; | ||
| 16 | } | ||
| 17 | elsif ($class->can('action') ne __PACKAGE__) { | ||
| 18 | die "No Regex or process subroutine in $class" | ||
| 19 | unless $self->{Regex}; | ||
| 20 | } | ||
| 21 | else { | ||
| 22 | die "No action subroutine in $class"; | ||
| 23 | } | ||
| 24 | |||
| 25 | return bless $self, $class; | ||
| 26 | } | ||
| 27 | |||
| 28 | sub init { | ||
| 29 | my $self = shift; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub reply_to { | ||
| 33 | my ($self, $message, $text) = @_; | ||
| 34 | my $reply = $message->reply($text); | ||
| 35 | # Unless it's a tell | ||
| 36 | # $reply->channel('spam') unless $reply->channel eq CHAN_PRIVATE; | ||
| 37 | return $reply; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub action { | ||
| 41 | my ($self, $message, @args) = @_; | ||
| 42 | print STDERR ref($self) . " does not define 'action'\n"; | ||
| 43 | return undef; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub process { | ||
| 47 | my $self = shift; | ||
| 48 | my $message = shift; | ||
| 49 | |||
| 50 | my $re = $self->{Regex}; | ||
| 51 | unless (defined $re) { | ||
| 52 | print STDERR ref($self) ." defines neither Regex nor process\n"; | ||
| 53 | return undef; | ||
| 54 | } | ||
| 55 | |||
| 56 | return undef unless $message->content =~ /$re/; | ||
| 57 | print STDERR "Executing brain " . ref($self) . "\n"; | ||
| 58 | return $self->action($message, | ||
| 59 | $1, $2, $3, $4, $5, $6, $7, $8, $9); | ||
| 60 | } | ||
| 61 | |||
| 62 | 1; |