| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Bot::Cobalt::Plugin::RandomQuote; | 
| 2 |  |  |  |  |  |  | # ABSTRACT: Bot::Cobalt plugin for retrieving random quotes from files | 
| 3 |  |  |  |  |  |  | $Bot::Cobalt::Plugin::RandomQuote::VERSION = '0.001'; | 
| 4 | 1 |  |  | 1 |  | 19456 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 36 |  | 
| 5 | 1 |  |  | 1 |  | 6 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 31 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 1 |  |  | 1 |  | 451 | use Bot::Cobalt; | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | use Bot::Cobalt::Common; | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | use Path::Tiny; | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | sub new { bless {}, shift } | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | sub quotesdir { shift->{quotesdir} } | 
| 15 |  |  |  |  |  |  | sub commands  { shift->{commands}  } | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | sub Cobalt_register { | 
| 18 |  |  |  |  |  |  | my $self = shift; | 
| 19 |  |  |  |  |  |  | my $core = shift; | 
| 20 |  |  |  |  |  |  | my $conf = $core->get_plugin_cfg($self); | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | my $quotesdir = $conf->{quotedir} // path( $core->var, 'quotes' ); | 
| 23 |  |  |  |  |  |  | $self->{quotesdir} = path($quotesdir)->absolute; | 
| 24 |  |  |  |  |  |  | $self->{commands}  = []; | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | my @files = $self->quotesdir->children; | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | foreach my $file (@files) { | 
| 29 |  |  |  |  |  |  | next if not $file->is_file; | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | my $cmd = lc( $file->basename(qr/\..+$/) ); | 
| 32 |  |  |  |  |  |  | my $handler = sub { | 
| 33 |  |  |  |  |  |  | my $self = shift; | 
| 34 |  |  |  |  |  |  | my $core = shift; | 
| 35 |  |  |  |  |  |  | my $msg  = ${ shift() }; | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | my $channel = $msg->channel; | 
| 38 |  |  |  |  |  |  | my $context = $msg->context; | 
| 39 |  |  |  |  |  |  | my $nick    = $msg->src_nick; | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | my @lines = grep { /\W/ } ( $file->lines ); | 
| 42 |  |  |  |  |  |  | my $num   = int(rand(@lines)); | 
| 43 |  |  |  |  |  |  | my $quote = $lines[$num]; | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | $quote =~ s/\$\{nick\}/$nick/g; | 
| 46 |  |  |  |  |  |  | $quote =~ s/\$\{channel\}/$channel/g; | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | broadcast( 'message', $context, $channel, $quote ); | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | return PLUGIN_EAT_ALL; | 
| 51 |  |  |  |  |  |  | }; | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | { | 
| 54 |  |  |  |  |  |  | no strict 'refs'; | 
| 55 |  |  |  |  |  |  | *{__PACKAGE__ . '::Bot_public_cmd_' . $cmd } = $handler; | 
| 56 |  |  |  |  |  |  | } | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | logger->info('Registered, command: !' . $cmd); | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | push @{ $self->{commands} }, $cmd; | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | register( $self, 'SERVER', 'public_cmd_' . $cmd ); | 
| 63 |  |  |  |  |  |  | } | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | return PLUGIN_EAT_NONE; | 
| 66 |  |  |  |  |  |  | } | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | sub Cobalt_unregister { | 
| 69 |  |  |  |  |  |  | my $self = shift; | 
| 70 |  |  |  |  |  |  | my $core = shift; | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | foreach my $cmd (@{ $self->commands }) { | 
| 73 |  |  |  |  |  |  | unregister($self, 'SERVER', 'public_cmd_' . $cmd); | 
| 74 |  |  |  |  |  |  | } | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | logger->info("Unregistered"); | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | return PLUGIN_EAT_NONE; | 
| 79 |  |  |  |  |  |  | } | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | 1; | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | __END__ |