Hubot/Adapter/Irc.pm

filter: Statementstmt 21.2% 28 / 132 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 21.2% 7 / 33 Podpod 0.0% 0 / 16 totaltotal 14.2% 35 / 246 CC 29 SCAR 28.8
CC
29
Cov
15%
__ANON__
56.0
run
42.0
checkCanStart
30.0
CRAP 17.8
↑ Summary Hubot/Creator.pm »
1 package Hubot::Adapter::Irc;
2 1 $Hubot::Adapter::Irc::VERSION = '0.2.8';
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 extends 'Hubot::Adapter';
7  
8 1 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use AnyEvent::IRC::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Time::HiRes 'time';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Encode qw/encode_utf8 decode_utf8/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use Hubot::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 has 'robot' => ( is => 'ro', isa => 'Hubot::Robot', );
16  
17 1 has 'cv' => ( is => 'ro', lazy_build => 1, );
18  
19 1 has 'irc' => ( is => 'ro', lazy_build => 1, );
20  
21 0 ▶sub _build_cv { AnyEvent->condvar }
Subroutine _build_cv: not called CC=1 SCAR=6.9
22 0 ▶sub _build_irc { AnyEvent::IRC::Client->new }
Subroutine _build_irc: not called CC=1 SCAR=6.9
23  
24 0 ▶sub notice { }
Subroutine notice: not called CC=1 SCAR=0.0
Pod undocumented
25  
26 0 ▶sub join {
Subroutine join: not called CC=1 SCAR=6.9
Pod undocumented
27 0 my ( $self, $channel ) = @_;
28 0 $self->irc->send_srv( JOIN => $channel );
29 }
30 0 ▶sub part { }
Subroutine part: not called CC=1 SCAR=0.0
Pod undocumented
31 0 ▶sub kick { }
Subroutine kick: not called CC=1 SCAR=0.0
Pod undocumented
32 0 ▶sub command { }
Subroutine command: not called CC=1 SCAR=0.0
Pod undocumented
33  
34 0 ▶sub whois {
Subroutine whois: not called CC=1 SCAR=6.9
Pod undocumented
35 0 my ( $self, $nick ) = @_;
36 0 return $self->irc->nick_ident($nick);
37 }
38  
39 0 ▶sub parse_msg {
Subroutine parse_msg: not called CC=1 SCAR=6.9
Pod undocumented
40 0 my ( $self, $irc_msg ) = @_;
41  
42 0 my ($nickname) = $irc_msg->{prefix} =~ m/^([^!]+)/;
43 0 my ($ip) = $irc_msg->{prefix} =~ m/\b((?:[0-9]{1,3}\.){3}[0-9]{1,3})\b/;
44 0 my $message = decode_utf8( $irc_msg->{params}[1] );
45 0 return ( $nickname, $message, $ip );
46 }
47  
48 0 ▶sub send {
Subroutine send: not called CC=2 SCAR=17.9
Pod undocumented
49 0 my ( $self, $user, @strings ) = @_;
50 0 for my $str (@strings) {
51 0 $self->irc->send_srv( 'PRIVMSG', $user->{room}, encode_utf8($str) );
52 0 Time::HiRes::sleep(0.1);
53 }
54 }
55  
56 0 ▶sub whisper {
Subroutine whisper: not called CC=2 SCAR=17.9
Pod undocumented
57 0 my ( $self, $user, $to, @strings ) = @_;
58 $self->irc->send_srv( 'PRIVMSG', $to->{name}, encode_utf8($_) )
59 0 for @strings;
60 }
61  
62 0 ▶sub reply {
Subroutine reply: not called CC=1 SCAR=6.9
Pod undocumented
63 0 my ( $self, $user, @strings ) = @_;
64 0 @strings = map { $user->{name} . ": $_" } @strings;
65 0 $self->send( $user, @strings );
66 }
67  
68 0 ▶sub run {
Subroutine run: not called CC=6 SCAR=37.4
Pod undocumented
69 0 my $self = shift;
70  
71 0 $self->checkCanStart;
72  
73 my %options = (
74 nick => $ENV{HUBOT_IRC_NICK} || $self->robot->name,
75 port => $ENV{HUBOT_IRC_PORT} || 6667,
76 rooms => [split( /,/, $ENV{HUBOT_IRC_ROOMS} )],
77 server => $ENV{HUBOT_IRC_SERVER},
78 user => $ENV{HUBOT_IRC_USER},
79 password => $ENV{HUBOT_IRC_PASSWORD},
80 realname => $ENV{HUBOT_IRC_REALNAME},
81 nickserv => $ENV{HUBOT_IRC_NICKSERV} || 'NickServ',
82 nickservpw => $ENV{HUBOT_IRC_NICKSERV_PASSWORD},
83 0 ▶ );
Condition0%0/7
$ENV{'HUBOT_IRC_NICK'} || $self->robot->name
l!l&&r!l&&!r
000
$ENV{'HUBOT_IRC_PORT'} || 6667
l!l
00
$ENV{'HUBOT_IRC_NICKSERV'} || 'NickServ'
l!l
00
MC/DC0%0/2
$ENV{'HUBOT_IRC_NICK'} || $self->robot->name
$ENV{'HUBOT_IRC_NICK'} $self->robot->name
MC/DC0%0/1
$ENV{'HUBOT_IRC_PORT'} || 6667
$ENV{'HUBOT_IRC_PORT'}
MC/DC0%0/1
$ENV{'HUBOT_IRC_NICKSERV'} || 'NickServ'
$ENV{'HUBOT_IRC_NICKSERV'}
Truth table0%0/3
A || B
A= $ENV{'HUBOT_IRC_NICK'}B= $self->robot->name
ABresult
1X1
011
000
Truth table0%0/2
A
A= $ENV{'HUBOT_IRC_PORT'}
Aresult
11
00
Truth table0%0/2
A
A= $ENV{'HUBOT_IRC_NICKSERV'}
Aresult
11
00
84  
85 0 $self->robot->name( $options{nick} );
86  
87 $self->irc->reg_cb(
88 0 ▶ connect => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
89 0 my ( $con, $err ) = @_;
90  
91 0 ▶ if ( defined $options{nickservpw} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{'nickservpw'}) { } 0 0 0
92 $self->irc->send_srv(
93 'PRIVMSG' => $options{nickserv},
94 0 "identify $options{nickservpw}"
95 );
96 }
97 else {
98 0 $self->join($_) for @{ $options{rooms} };
99 }
100 },
101 0 ▶ join => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
102 0 my ( $cl, $nick, $channel, $is_myself ) = @_;
103 0 print "$nick joined $channel\n";
104 0 my $user = $self->createUser( $channel, $nick );
105 0 $self->receive( new Hubot::EnterMessage( user => $user ) );
106 },
107 0 ▶ publicmsg => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
108 0 my ( $cl, $channel, $ircmsg ) = @_;
109 0 my ( $nick, $msg, $ip ) = $self->parse_msg($ircmsg);
110 0 my $user = $self->createUser( $channel, $nick );
111 0 ▶ $user->{room} = $channel if $channel =~ m/^#/;
Branch: 1 branch
BranchTrueFalseTotal
if $channel =~ /^#/ 0 0 0
112 0 ▶ $user->{ip} = $ip if $ip;
Branch: 1 branch
BranchTrueFalseTotal
if $ip 0 0 0
113  
114 0 my $is_notice = $ircmsg->{command} eq 'NOTICE';
115 0 ▶ my $class
Branch: 1 branch
BranchTrueFalseTotal
$is_notice ? : 0 0 0
116 = $is_notice ? 'Hubot::NoticeMessage' : 'Hubot::TextMessage';
117 0 $self->receive( $class->new( user => $user, text => $msg, ) );
118 },
119 0 ▶ privatemsg => sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
120 0 my ( $cl, $nick, $ircmsg ) = @_;
121 0 my ( $from, $msg ) = $self->parse_msg($ircmsg);
122 0 ▶ my ($channel) = $msg =~ m/^\#/ ? split / /, $msg : '';
Branch: 1 branch
BranchTrueFalseTotal
$msg =~ /^\#/ ? : 0 0 0
123 0 $msg =~ s/^$channel\s*//;
124  
125 0 my $is_notice = $ircmsg->{command} eq 'NOTICE';
126  
127 # -NickServ- You are now identified for <nick>.
128 0 ▶ if ( $is_notice
Branch: 1 branch
BranchTrueFalseTotal
if ($is_notice and $from eq $options{"nickserv"} and $msg =~ /identified/) 0 0 0
Condition0%0/6
$is_notice and $from eq $options{"nickserv"} and $msg =~ /identified/
!ll&&!rl&&r
000
$is_notice and $from eq $options{"nickserv"}
!ll&&!rl&&r
000
MC/DC0%0/3
$is_notice and $from eq $options{"nickserv"} and $msg =~ /identified/
$is_notice $from eq $options{"nickserv"} $msg =~ /identified/
Truth table0%0/4
A and B and C
A= $is_noticeB= $from eq $options{"nickserv"}C= $msg =~ /identified/
ABCresult
0XX0
10X0
1100
1111
129 && $from eq $options{nickserv}
130 && $msg =~ /identified/ )
131 {
132 0 $self->join($_) for @{ $options{rooms} };
133 0 return;
134 }
135  
136 0 ▶ my $class
Branch: 1 branch
BranchTrueFalseTotal
$is_notice ? : 0 0 0
137 = $is_notice
138 ? 'Hubot::NoticeMessage'
139 : 'Hubot::WhisperMessage';
140 0 my $user = $self->createUser( $channel, $from );
141 0 $self->receive( $class->new( user => $user, text => $msg, ) );
142 },
143 0 ▶ part => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
144 0 my ( $cl, $nick, $channel, $is_myself, $msg ) = @_;
145  
146 0 ▶ $msg = "no quit message" unless $msg;
Branch: 1 branch
BranchTrueFalseTotal
unless $msg 0 0 0
147  
148 0 print "$nick leaves $channel: $msg\n";
149 0 my $user = $self->createUser( $channel, $nick );
150 0 $self->receive(
151 new Hubot::LeaveMessage( user => $user, text => $msg ) );
152 },
153 0 ▶ quit => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
154 0 my ( $cl, $nick, $msg ) = @_;
155  
156 0 ▶ $msg = "no quit message" unless $msg;
Branch: 1 branch
BranchTrueFalseTotal
unless $msg 0 0 0
157  
158 0 print "$nick quit: $msg\n";
159 0 my $user = $self->createUser( '', $nick )
160 ; # room is empty, maybe raise a error case.
161 0 $self->receive(
162 new Hubot::LeaveMessage( user => $user, text => $msg ) );
163 },
164 0 ▶ irc_330 => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
165 ## 330 is RPL_WHOWAS_TIME
166 0 my ( $cl, $ircmsg ) = @_;
167  
168 0 my $user = $self->createUser( '', '*' );
169 $self->receive(
170 new Hubot::NoticeMessage(
171 user => $user,
172 text =>
173 0 sprintf( "%s %s %s", @{ $ircmsg->{params} }[1, 3, 2] ),
174 )
175 );
176 },
177 0 ▶ irc_mode => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
178 0 my ( $cl, $ircmsg ) = @_;
179 0 my ( $channel, $mode, $target ) = @{ $ircmsg->{params} };
180  
181 0 ▶ $self->robot->mode( $mode || '' )
Branch: 1 branch
BranchTrueFalseTotal
if $target and $target eq $self->robot->name 0 0 0
Condition0%0/5
$target and $target eq $self->robot->name
!ll&&!rl&&r
000
$mode || ''
l!l
00
MC/DC0%0/2
$target and $target eq $self->robot->name
$target $target eq $self->robot->name
MC/DC0%0/1
$mode || ''
$mode
Truth table0%0/3
A and B
A= $targetB= $target eq $self->robot->name
ABresult
0X0
100
111
Truth table0%0/2
A
A= $mode
Aresult
11
00
182 if $target && $target eq $self->robot->name;
183 },
184 0 );
185  
186 0 $self->emit('connected');
187 0 $self->cv->begin;
188 0 ▶ if ( $ENV{HUBOT_IRC_ENABLE_SSL} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"HUBOT_IRC_ENABLE_SSL"}) 0 0 0
189 0 eval "require Net::SSLeay; 1";
190 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
191 0 die "HUBOT_IRC_ENABLE_SSL requires `Net::SSLeay`: $@\n";
192 }
193 else {
194 0 $self->irc->enable_ssl;
195 }
196 }
197 $self->irc->connect(
198 $options{server},
199 $options{port},
200 {
201 nick => $options{nick},
202 user => $options{user},
203 real => $options{realname},
204 password => $options{password},
205 0 timeout => 10, # wait 10 seconds
206 }
207 );
208  
209 0 $self->cv->recv;
210 }
211  
212 0 ▶sub close {
Subroutine close: not called CC=1 SCAR=6.9
Pod undocumented
213 0 my $self = shift;
214 0 $self->irc->disconnect;
215 0 $self->cv->send;
216 }
217  
218 0 ▶sub exist {
Subroutine exist: not called CC=1 SCAR=6.9
Pod undocumented
219 0 my ( $self, $user, $nick ) = @_;
220 0 return $self->findUser( $user->{room}, $nick );
221 }
222  
223 0 ▶sub createUser {
Subroutine createUser: not called CC=2 SCAR=17.9
Pod undocumented
224 0 my ( $self, $channel, $from ) = @_;
225 0 my $user = $self->userForName($from);
226 0 ▶ unless ($user) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($user) 0 0 0
227 0 my $id = time;
228 0 $id =~ s/\.//;
229 0 $user = $self->userForId( $id, { name => $from, room => $channel, } );
230 }
231  
232 0 return $user;
233 }
234  
235 0 ▶sub findUser {
Subroutine findUser: not called CC=1 SCAR=6.9
Pod undocumented
236 0 my ( $self, $channel, $nick ) = @_;
237 0 return $self->irc->nick_modes( $channel, $nick );
238 }
239  
240 0 ▶sub checkCanStart {
Subroutine checkCanStart: not called CC=5 SCAR=34.0
Pod undocumented
241 0 my $self = shift;
242  
243 0 ▶ if ( !$ENV{HUBOT_IRC_NICK} && !$self->robot->name ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $ENV{'HUBOT_IRC_NICK'} || $self->robot->name) { } 0 0 0
elsif (not $ENV{'HUBOT_IRC_ROOMS'}) { } 0 0 0
elsif (not $ENV{'HUBOT_IRC_SERVER'}) { } 0 0 0
Condition0%0/3
$ENV{'HUBOT_IRC_NICK'} || $self->robot->name
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{'HUBOT_IRC_NICK'} || $self->robot->name
$ENV{'HUBOT_IRC_NICK'} $self->robot->name
Truth table0%0/3
A || B
A= $ENV{'HUBOT_IRC_NICK'}B= $self->robot->name
ABresult
1X1
011
000
244 ## use die?
245 0 print STDERR
246 "HUBOT_IRC_NICK is not defined, try: export HUBOT_IRC_NICK='mybot'\n";
247 0 exit(2); # TODO: research standard exit value
248 }
249 elsif ( !$ENV{HUBOT_IRC_ROOMS} ) {
250 0 print STDERR
251 "HUBOT_IRC_ROOMS is not defined, try: export HUBOT_IRC_ROOMS='#myroom'\n";
252 0 exit(2);
253 }
254 elsif ( !$ENV{HUBOT_IRC_SERVER} ) {
255 0 print STDERR
256 "HUBOT_IRC_SERVER is not defined, try: export HUBOT_IRC_SERVER='irc.myserver.com'\n";
257 0 exit(2);
258 }
259 }
260  
261 1 __PACKAGE__->meta->make_immutable;
262  
263 1 1;
264  
265 =pod
266  
267 =encoding utf-8
268  
269 =head1 NAME
270  
271 Hubot::Adapter::Irc - IRC adapter for L<Hubot>
272  
273 =head1 VERSION
274  
275 version 0.2.8
276  
277 =head1 SYNOPSIS
278  
279 $ export HUBOT_IRC_SERVER='irc.myserver.com'
280 $ export HUBOT_IRC_ROOMS='#mychannel'
281 $ export HUBOT_IRC_ENABLE_SSL=1 # use SSL connection?
282 $ hubot -a irc
283  
284 =head1 DESCRIPTION
285  
286 IRC is a fairly old protocol for Internet chat.
287  
288 =head1 CONFIGURATION
289  
290 =head2 REQUIRED
291  
292 =over
293  
294 =item HUBOT_IRC_SERVER
295  
296 This is the full hostname or IP address of the IRC server you want your hubot to connect to. Make a note of it.
297  
298 =item HUBOT_IRC_ROOMS
299  
300 This is a comma separated list of the IRC channels you want your hubot to join. They must include the C<#>. Make a note of them.
301  
302 =back
303  
304 =head2 OPTIONAL
305  
306 =over
307  
308 =item HUBOT_IRC_NICK
309  
310 This is the optional nick you want your hubot to join with. If omitted it will default to the name of your hubot.
311  
312 =item HUBOT_IRC_PORT
313  
314 This is the optional port of the IRC server you want your hubot to connect to. If omitted the default is C<6667>. Make a note of it if required.
315  
316 =item HUBOT_IRC_USER
317  
318 This is the optional username of the IRC server you want your hubot to connect to.
319  
320 =item HUBOT_IRC_PASSWORD
321  
322 This is the optional password of the IRC server you want your hubot to connect to. If the IRC server doesn't require a password, this can be omitted. Make a note of it if required.
323  
324 =item HUBOT_IRC_REALNAME
325  
326 Your realname on IRC server.
327  
328 =item HUBOT_IRC_ENABLE_SSL
329  
330 using L<AnyEvent::IRC::Connection> C<enable_ssl> at connect.
331  
332 requires L<Net::SSLeay>.
333  
334 =back
335  
336 =head1 SEE ALSO
337  
338 L<https://github.com/github/hubot/wiki/Adapter:-IRC>
339  
340 =head1 AUTHOR
341  
342 Hyungsuk Hong <hshong@perl.kr>
343  
344 =head1 COPYRIGHT AND LICENSE
345  
346 This software is copyright (c) 2012 by Hyungsuk Hong.
347  
348 This is free software; you can redistribute it and/or modify it under
349 the same terms as the Perl 5 programming language system itself.
350  
351 =cut
↑ Summary Hubot/Creator.pm »