| 1 | package Bot::BasicBot::Pluggable::Module::RD; | ||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | |||||||||||
| 4 | 2 | our $VERSION = '0.02'; | |||||||||
| 5 | 2 | ▶ | use base qw(Bot::BasicBot::Pluggable::Module); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | ▶ | use Bot::BasicBot::Pluggable::Module::RD_Basic; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | ▶ | use Parse::RecDescent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 2 | ▶ | use Parse::RecDescent::Deparse; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 2 | ▶ | use Parse::RecDescent::Topiary; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | =head1 NAME | ||||||||||
| 12 | |||||||||||
| 13 | Bot::BasicBot::Pluggable::Module::RD - RecDescent grammar adaptor plugin | ||||||||||
| 14 | |||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||
| 16 | |||||||||||
| 17 | !load RD | ||||||||||
| 18 | |||||||||||
| 19 | See the synopsis of L<Bot::BasicBot::Pluggable> for how to load this plugin. | ||||||||||
| 20 | |||||||||||
| 21 | =head1 DESCRIPTION | ||||||||||
| 22 | |||||||||||
| 23 | Many bots are concerned with responding to commands issued on IRC. The simple | ||||||||||
| 24 | approach of regular expressions is used by many of the bot plugins. However, | ||||||||||
| 25 | this module offers full syntax parsing via L<Parse::RecDescent>. | ||||||||||
| 26 | |||||||||||
| 27 | Rather than each bot plugin doing its own parsing in an overridden C<said()> | ||||||||||
| 28 | routine, the approach used here is for a single grammar shared between the | ||||||||||
| 29 | bot plugins. This module and namespace is the keeper of the grammar, and | ||||||||||
| 30 | others can extend the grammar by adding rules to it. | ||||||||||
| 31 | |||||||||||
| 32 | The following simple commands are provided: | ||||||||||
| 33 | |||||||||||
| 34 | say Hello channel | ||||||||||
| 35 | tell ivorw I'm connected | ||||||||||
| 36 | |||||||||||
| 37 | =head2 Methods C<init>, C<told>, C<help> | ||||||||||
| 38 | |||||||||||
| 39 | See the documentation for L<Bot::BasicBot::Pluggable::Module> and | ||||||||||
| 40 | L<Bot::BasicBot>. | ||||||||||
| 41 | |||||||||||
| 42 | =head2 extend | ||||||||||
| 43 | |||||||||||
| 44 | This is called by any pluggable modules in order to add rules to the bot's | ||||||||||
| 45 | Parse::RecDescent grammar. | ||||||||||
| 46 | |||||||||||
| 47 | =head1 BUGS | ||||||||||
| 48 | |||||||||||
| 49 | Please report any bugs to L<http://rt.cpan.org>. | ||||||||||
| 50 | |||||||||||
| 51 | =head1 AUTHOR | ||||||||||
| 52 | |||||||||||
| 53 | Ivor Williams | ||||||||||
| 54 | CPAN ID: IVORW | ||||||||||
| 55 | |||||||||||
| 56 | ivorw@cpan.org | ||||||||||
| 57 | |||||||||||
| 58 | |||||||||||
| 59 | =head1 COPYRIGHT | ||||||||||
| 60 | |||||||||||
| 61 | This program is free software; you can redistribute | ||||||||||
| 62 | it and/or modify it under the same terms as Perl itself. | ||||||||||
| 63 | |||||||||||
| 64 | The full text of the license can be found in the | ||||||||||
| 65 | LICENSE file included with this module. | ||||||||||
| 66 | |||||||||||
| 67 | |||||||||||
| 68 | =head1 SEE ALSO | ||||||||||
| 69 | |||||||||||
| 70 | perl(1). | ||||||||||
| 71 | |||||||||||
| 72 | =cut | ||||||||||
| 73 | |||||||||||
| 74 | 2 | my $grammar = <<'END'; | |||||||||
| 75 | <autotree> | ||||||||||
| 76 | |||||||||||
| 77 | END | ||||||||||
| 78 | |||||||||||
| 79 | 2 | our @namespace; | |||||||||
| 80 | 2 | our $parser; | |||||||||
| 81 | |||||||||||
| 82 | 1 | ▶ | sub init { | ||||||||
|
Subroutine
init: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 83 | 1 | my $self = shift; | |||||||||
| 84 | |||||||||||
| 85 | 1 | ▶ | $parser = Parse::RecDescent->new($grammar) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | or die "Bad grammar"; | ||||||||||
| 87 | |||||||||||
| 88 | 1 | $self->Bot::BasicBot::Pluggable::Module::RD_Basic::init(); | |||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | 3 | ▶ | sub extend { | ||||||||
|
Subroutine
extend: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 92 | 3 | my ( $self, $newg, $class ) = @_; | |||||||||
| 93 | |||||||||||
| 94 | 3 | ▶ | die "RD Parser not initialised - load RD module" unless $parser; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 95 | 2 | $parser->Extend($newg); | |||||||||
| 96 | 2 | ▶ | if ($class) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | 2 | push @namespace, $class; | |||||||||
| 98 | } | ||||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | 0 | ▶ | sub told { | ||||||||
|
Subroutine
told: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||
| 102 | 0 | my ( $self, $mess ) = @_; | |||||||||
| 103 | |||||||||||
| 104 | 0 | my $body = $mess->{body}; | |||||||||
| 105 | |||||||||||
| 106 | 0 | ▶ | return unless $mess->{address}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 107 | |||||||||||
| 108 | 0 | my $tree = topiary( | |||||||||
| 109 | tree => $parser->command($body), | ||||||||||
| 110 | namespace => [ __PACKAGE__, @namespace ], | ||||||||||
| 111 | ucfirst => 1, | ||||||||||
| 112 | consolidate => 1, | ||||||||||
| 113 | ); | ||||||||||
| 114 | |||||||||||
| 115 | 0 | ▶ | if ($tree) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 116 | 0 | $tree->despatch( $self, $mess ); | |||||||||
| 117 | } | ||||||||||
| 118 | else { | ||||||||||
| 119 | 0 | ▶ | my $bot = $self->bot or die "No bot"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 120 | 0 | my $nick = $bot->nick; | |||||||||
| 121 | 0 | ▶ | return ( $mess->{raw_body} =~ /^$nick[:,]/ ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 122 | ? 0 | ||||||||||
| 123 | : "Sorry, I don't understand $body"; | ||||||||||
| 124 | } | ||||||||||
| 125 | } | ||||||||||
| 126 | |||||||||||
| 127 | 0 | ▶ | sub help { | ||||||||
|
Subroutine
help: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 128 | 0 | my $self = shift; | |||||||||
| 129 | |||||||||||
| 130 | 0 | $self->Bot::BasicBot::Pluggable::Module::RD_Basic::help; | |||||||||
| 131 | } | ||||||||||
| 132 | |||||||||||
| 133 | package Bot::BasicBot::Pluggable::Module::RD::Command; | ||||||||||
| 134 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 135 | |||||||||||
| 136 | 2 | ▶ | use base qw(Parse::RecDescent::Topiary::Base); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 137 | |||||||||||
| 138 | 2 | 1; | |||||||||
| 139 | |||||||||||
| 140 | 0 | ▶ | sub despatch { | ||||||||
|
Subroutine
despatch: not called CC=2 SCAR=17.9
| |||||||||||
| 141 | 0 | my $self = shift; | |||||||||
| 142 | |||||||||||
| 143 | 0 | ▶ | if ( exists $self->{__STRING1__} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 144 | 0 | my $verb = $self->{__STRING1__}; | |||||||||
| 145 | 0 | my $class = Parse::RecDescent::Topiary::delegation_class( 'Command', | |||||||||
| 146 | \@namespace, $verb ); | ||||||||||
| 147 | 0 | my $meth = $class . '::' . $verb; | |||||||||
| 148 | 0 | return $self->$meth(@_); | |||||||||
| 149 | } | ||||||||||
| 150 | } | ||||||||||