blib/lib/ZenIRCBot.pm untested

filter: Statementstmt 0.0% 0 / 66 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 115 CC 8 SCAR 23.2
CC
8
Cov
0%
register_commands
30.0
subscribe
6.0
send_privmsg
6.0
CRAP 10.2
1 package ZenIRCBot;
2  
3 use 5.010;
4 use strict;
5 use warnings;
6  
7 our $VERSION = '0.01_01';
8  
9 use AnyEvent::Redis;
10 use JSON;
11  
12 =head1 NAME
13  
14 ZenIRCBot - Perl API for ZenIRCBot
15  
16 =head1 SYNOPSIS
17  
18 use ZenIRCBot;
19  
20 my $bot = ZenIRCBot->new();
21 $bot->register_commands('ZenIRCBot::Test', [
22 { name => 'foo', description => 'Returns bar' }
23 ]);
24  
25 $bot->subscribe(sub {
26 my ($msg, $channel) = @_;
27
28 if ($msg->{version} == 1 and $msg->{type} eq 'privmsg') {
29 if ($msg->{data}->{message} =~ m/foo/) {
30 $bot->send_privmsg(
31 $msg->{data}->{channel},
32 sprintf("%s: bar", $msg->{data}->{sender})
33 );
34 }
35 }
36 });
37  
38 $bot->run;
39  
40 =head1 DESCRIPTION
41  
42 Perl API for ZenIRCBot.
43  
44 =head1 METHODS
45  
46 =head2 Standard API Methods
47  
48 See the L<ZenIRCBot documentation|http://zenircbot.readthedocs.org/en/latest/index.html>
49 for more information.
50  
51 =cut
52  
53 sub new {
54 my $class = shift;
55 my $host = shift // 'localhost';
56 my $port = shift // 6379;
57 my $db = shift // 0;
58
59 my $redis = AnyEvent::Redis->new(
60 host => $host,
61 port => $port,
62 on_error => sub { warn @_; }
63 );
64 # TODO handle db
65  
66 bless({
67 redis => $redis,
68 services => [],
69 }, $class);
70 }
71  
72 sub redis { return $_[0]->{redis}; }
73  
74  
75 =head2 subscribe($func)
76  
77 Set callback for redis publish events.
78  
79 =head3 callback($message, $channel)
80  
81 The callback function is called on Redis subscribe events, with the message
82 hashref and channel name.
83  
84 =cut
85  
86 sub subscribe {
87 my ($self, $func) = @_;
88 die "Not a CODEREF" if (ref $func ne "CODE");
89 $self->get_redis_client->subscribe('in', sub {
90 my ($raw, $channel) = @_;
91 my $json = JSON->new->allow_nonref;
92 my $msg = $json->decode($raw);
93 &{$func}($msg, $channel);
94 });
95 }
96  
97 # Blocking AE loop
98 sub run {
99 my $self = shift;
100 AnyEvent->condvar->recv;
101 }
102  
103 # ZenIRC functions from std api
104 sub send_privmsg {
105 my ($self, $to, $message) = @_;
106 if (ref $to ne 'ARRAY') {
107 $to = [$to];
108 }
109  
110 for my $channel (@{$to}) {
111 my $json = JSON->new->allow_nonref->allow_blessed->convert_blessed;
112 my $data = $json->encode({
113 version => 1,
114 type => 'privmsg',
115 data => {
116 to => $channel,
117 message => $message
118 }
119 });
120
121 $self->get_redis_client->publish('out', $data);
122 }
123 }
124  
125 sub send_admin_message {
126 my ($self, $message) = @_;
127 $self->redis->get('zenircbot:admin_spew_channels', sub {
128 $self->send_privmsg(@_, $message) if (@_);
129 });
130 }
131  
132 sub register_commands {
133 my ($self, $service, $commands) = @_;
134
135 return if (!defined($commands));
136 $commands = [$commands]
137 if (ref($commands) ne "ARRAY");
138
139 $self->{cv} = $self->get_redis_client->subscribe('in', sub {
140 my $raw = shift;
141
142 my $json = JSON->new->allow_nonref;
143 my $message = $json->decode($raw);
144  
145 if ($message->{version} == 1 and $message->{type} eq 'privmsg') {
146 if ($message->{data}->{message} eq 'commands') {
147 for my $command (@{$commands}) {
148 $self->send_privmsg(
149 $message->{data}->{sender},
150 sprintf(
151 "%s: %s - %s",
152 $service,
153 $command->{name},
154 $command->{description}
155 ));
156 }
157 }
158 }
159 });
160  
161 $self->send_admin_message(sprintf("%s online!", $service));
162 }
163  
164 sub get_redis_client {
165 my ($self) = @_;
166 return AnyEvent::Redis->new(
167 host => $self->{host},
168 port => $self->{port},
169 on_error => sub { warn @_; }
170 );
171 }
172  
173 1;
174 __END__