File Coverage

blib/lib/POE/Component/IRC/Plugin/Infobot.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package POE::Component::IRC::Plugin::Infobot;
2              
3 1     1   31129 use 5.014000;
  1         4  
  1         39  
4 1     1   8 use strict;
  1         2  
  1         35  
5 1     1   5 use warnings;
  1         6  
  1         49  
6              
7             our $VERSION = '0.001002';
8              
9 1     1   470 use DB_File;
  0            
  0            
10              
11             use IRC::Utils qw/parse_user/;
12             use POE::Component::IRC::Plugin qw/PCI_EAT_NONE/;
13              
14             use constant +{
15             OK => [ 'sure, %s', 'ok, %s', 'gotcha, %s'],
16             A_IS_B => [ '%s is %s', 'I think %s is %s', 'hmmm... %s is %s', 'it has been said that %s is %s', '%s is probably %s', 'rumour has it %s is %s', 'i heard %s was %s', 'somebody said %s is %s', 'i guess %s is %s', 'well, %s is %s', '%s is, like, %s', 'methinks %s is %s'],
17             I_DONT_KNOW => [ 'I don\'t know, %s', 'Dunno, %s', 'No idea, %s', '%s: huh?', 'nem tudom, %s', 'anlamıyorum, %s', 'bilmiyorum, %s', 'nu ştiu d\'astea, %s', 'Je ne sais pas, %s', 'Я не знаю, %s'],
18             };
19              
20             sub new {
21             my $class = shift;
22             my $self = {
23             filename => 'factoids.db',
24             @_
25             };
26              
27             my %db;
28             $self->{dbobj} = tie %db, DB_File => $self->{filename} if defined $self->{filename};
29             $self->{db} = \%db;
30             bless $self, $class
31             }
32              
33             sub getstr {
34             my $rstrings = shift;
35             my @strings = @$rstrings;
36             sprintf $strings[int rand $#strings], @_
37             }
38              
39             sub infobot_add {
40             my ($self, $irc, $key, $value, $to, $nick) = @_;
41             if (exists $self->{db}->{$key}) {
42             $irc->yield(privmsg => $to => "I already had it that way, $nick") if $value eq $self->{db}->{$key};
43             $irc->yield(privmsg => $to => "... but $key is $self->{db}->{$key}!") unless $value eq $self->{db}->{$key};
44             } else {
45             $self->{db}->{$key} = $value;
46             $self->{dbobj}->sync if exists $self->{dbobj};
47             $irc->yield(privmsg => $to => getstr OK, $nick);
48             }
49             }
50              
51             sub infobot_query {
52             my ($self, $irc, $key, $to, $nick, $addressed) = @_;
53             if (exists $self->{db}->{$key}) {
54             my @answers = split /\s+\|\s+/, $self->{db}->{$key};
55             local $_ = $answers[int rand $#answers];
56              
57             if (/^ (.+)$/i) {
58             $irc->yield(ctcp => $to => "ACTION $1")
59             } elsif (/^ (.*)$/i) {
60             $irc->yield(privmsg => $to => $1)
61             } else {
62             $irc->yield(privmsg => $to => getstr A_IS_B, $key, $_)
63             }
64             } elsif ($addressed) {
65             $irc->yield(privmsg => $to => getstr I_DONT_KNOW, $nick)
66             }
67             }
68              
69             sub infobot_forget {
70             my ($self, $irc, $key, $to, $nick) = @_;
71             if (exists $self->{db}->{$key}) {
72             delete $self->{db}->{$key};
73             $self->{dbobj}->sync if exists $self->{dbobj};
74             $irc->yield(privmsg => $to => "$nick: I forgot $key")
75             } else {
76             $irc->yield(privmsg => $to => "I didn't have anything matching $key, $nick")
77             }
78             }
79              
80             sub runcmd{
81             my ($self, $irc, $to, $nick, $message, $addressed) = @_;
82              
83             local $_= $message;
84              
85             if (/^(.+)\s+is\s+(.*[^?])$/) {
86             infobot_add $self, $irc, $1, $2, $to, $nick if $addressed
87             } elsif (/^(.+)\?$/) {
88             infobot_query $self, $irc, $1, $to, $nick, $addressed
89             } elsif (/^forget\s+(.*)$/) {
90             infobot_forget $self, $irc, $1, $to, $nick if $addressed
91             }
92             }
93              
94             sub PCI_register {
95             my ($self, $irc) = @_;
96             $irc->plugin_register($self, SERVER => qw/public msg/);
97             1
98             }
99              
100             sub PCI_unregister{ 1 }
101              
102             sub S_public {
103             my ($self, $irc, $rfullname, $rchannels, $rmessage) = @_;
104             my $nick = parse_user $$rfullname;
105              
106             for my $channel (@$$rchannels) {
107             local $_ = $$rmessage;
108              
109             my $addressed=0;
110             my $mynick=$irc->nick_name;
111             if (/^$mynick [,:]\s+/x) {
112             $addressed=1;
113             s/^$mynick [,:]\s+//x;
114             }
115              
116             runcmd $self, $irc, $channel, $nick, $_, $addressed
117             }
118              
119             PCI_EAT_NONE
120             }
121              
122             sub S_msg{
123             my ($self, $irc, $rfullname, $rtargets, $rmessage) = @_;
124             my $nick = parse_user $$rfullname;
125              
126             runcmd $self, $irc, $nick, $nick, $$rmessage, 1;
127              
128             PCI_EAT_NONE
129             }
130              
131             1;
132             __END__