Mojo/IRC.pm

filter: Statementstmt 88.7% 206 / 232 Branchbran 68.0% 49 / 72 Conditioncond 50.0% 21 / 42 MC/DCmcdc 26.0% 6 / 23 Subroutinesub 85.4% 53 / 62 Podpod 100% 17 / 17 totaltotal 78.5% 352 / 448 CC 56 SCAR 15.8
CC
56
Cov
80%
connect
21.2
__ANON__
12.0
_read
6.5
CRAP 4.9
« Mojo/IRC/UA.pm ↑ Summary Test/Mojo/IRC.pm »
1 package Mojo::IRC;
2 11 ▶use Mojo::Base 'Mojo::EventEmitter';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 11 ▶use Mojo::IOLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use Mojo::Promise;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use File::Basename 'dirname';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use File::Spec::Functions 'catfile';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use IRC::Utils ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use Parse::IRC ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use Unicode::UTF8;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use constant DEBUG => $ENV{MOJO_IRC_DEBUG} || 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'MOJO_IRC_DEBUG'} || 0
l!l
011
MC/DC0%0/1
$ENV{'MOJO_IRC_DEBUG'} || 0
$ENV{'MOJO_IRC_DEBUG'}
Truth table50%1/2
A
A= $ENV{'MOJO_IRC_DEBUG'}
Aresult
11
00
12 11 ▶use constant DEFAULT_CERT => $ENV{MOJO_IRC_CERT_FILE} || catfile dirname(__FILE__), 'mojo-irc-client.crt';
Subroutine BEGIN: called CC=2 SCAR=7.6
Condition33%1/3
$ENV{'MOJO_IRC_CERT_FILE'} || catfile(dirname(__FILE__), 'mojo-irc-client.crt')
l!l&&r!l&&!r
0110
MC/DC0%0/2
$ENV{'MOJO_IRC_CERT_FILE'} || catfile(dirname(__FILE__), 'mojo-irc-client.crt')
$ENV{'MOJO_IRC_CERT_FILE'} catfile(dirname(__FILE__), 'mojo-irc-client.crt')
Truth table33%1/3
A || B
A= $ENV{'MOJO_IRC_CERT_FILE'}B= catfile(dirname(__FILE__), 'mojo-irc-client.crt')
ABresult
1X1
011
000
13 11 ▶use constant DEFAULT_KEY => $ENV{MOJO_IRC_KEY_FILE} || catfile dirname(__FILE__), 'mojo-irc-client.key';
Subroutine BEGIN: called CC=2 SCAR=7.6
Condition33%1/3
$ENV{'MOJO_IRC_KEY_FILE'} || catfile(dirname(__FILE__), 'mojo-irc-client.key')
l!l&&r!l&&!r
0110
MC/DC0%0/2
$ENV{'MOJO_IRC_KEY_FILE'} || catfile(dirname(__FILE__), 'mojo-irc-client.key')
$ENV{'MOJO_IRC_KEY_FILE'} catfile(dirname(__FILE__), 'mojo-irc-client.key')
Truth table33%1/3
A || B
A= $ENV{'MOJO_IRC_KEY_FILE'}B= catfile(dirname(__FILE__), 'mojo-irc-client.key')
ABresult
1X1
011
000
14  
15 11 our $VERSION = '0.47';
16  
17 11 our %NUMERIC2NAME = (470 => 'ERR_LINKCHANNEL');
18  
19 11 my %CTCP_QUOTE = ("\012" => 'n', "\015" => 'r', "\0" => '0', "\cP" => "\cP");
20  
21 11 my @DEFAULT_EVENTS = qw(
22 irc_ping irc_nick irc_notice irc_rpl_welcome err_nicknameinuse
23 irc_rpl_isupport ctcp_ping ctcp_time ctcp_version
24 );
25  
26 7 ▶has connect_timeout => sub { $ENV{MOJO_IRC_CONNECT_TIMEOUT} || 30 };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"MOJO_IRC_CONNECT_TIMEOUT"} or 30 1 6 7
27 10 ▶has ioloop => sub { Mojo::IOLoop->singleton };
Subroutine __ANON__: called CC=1 SCAR=0.0
28 11 has local_address => '';
29 11 has name => 'Mojo IRC';
30 3 ▶has nick => sub { shift->_build_nick };
Subroutine __ANON__: called CC=1 SCAR=0.0
31 2 ▶has parser => sub { Parse::IRC->new; };
Subroutine __ANON__: called CC=1 SCAR=0.0
32 11 has pass => '';
33 11 has real_host => '';
34  
35 1 ▶has server_settings => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
36 1 return {chantypes => '#', prefix => '(ov)@+'};
37 11 };
38  
39 11 has tls => undef;
40 3 ▶has user => sub { $ENV{USER} || getlogin || getpwuid($<) || 'anonymous' };
Subroutine __ANON__: called CC=4 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"USER"} or getlogin or getpwuid $< or "anonymous" 3 0 3
Condition33%2/6
$ENV{"USER"} or getlogin or getpwuid $<
l!l&&r!l&&!r
030
$ENV{"USER"} or getlogin
l!l&&r!l&&!r
003
MC/DC0%0/3
$ENV{"USER"} or getlogin or getpwuid $<
$ENV{"USER"} getlogin getpwuid $<
Truth table25%1/4
A or B or C
A= $ENV{"USER"}B= getloginC= getpwuid $<
ABCresult
1XX1
01X1
0011
0000
41  
42 10 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
43 10 my $self = shift->SUPER::new(@_);
44 10 $self->on(message => \&_legacy_dispatch_message);
45 10 return $self;
46 }
47  
48 15 ▶sub server {
Subroutine server: called CC=6 SCAR=18.3
Pod documented
49 15 my ($self, $server) = @_;
50 15 ▶ my $old = $self->{server} || '';
Condition100%2/2
$self->{'server'} || ''
l!l
141
MC/DC100%1/1
$self->{'server'} || ''
$self->{'server'}
Truth table100%2/2
A
A= $self->{'server'}
Aresult
11
00
51  
52 15 Scalar::Util::weaken($self);
53 15 ▶ return $old unless defined $server;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $server 11 4 15
54 4 ▶ return $self if $old and $old eq $server;
Branch: 1 branch
BranchTrueFalseTotal
if $old and $old eq $server 1 3 4
Condition100%3/3
$old and $old eq $server
!ll&&!rl&&r
121
MC/DC100%2/2
$old and $old eq $server
$old $old eq $server
Truth table100%3/3
A and B
A= $oldB= $old eq $server
ABresult
0X0
100
111
55 3 $self->{server} = $server;
56 3 ▶ return $self unless $self->{stream_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"stream_id"} 3 0 3
57 0 ▶ $self->disconnect(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
58 0 ▶ $self->connect(sub { });
Subroutine __ANON__: not called CC=1 SCAR=6.9
59 0 });
60 0 $self;
61 }
62  
63 10 ▶sub connect {
Subroutine connect: called CC=12 SCAR=30.5
Pod documented
64 10 my ($self, $cb) = @_;
65 10 my ($host, $port) = split /:/, $self->server;
66 10 my @extra;
67  
68 10 ▶ if (!$host) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($host) 0 10 10
69 0 ▶ $self->ioloop->next_tick(sub { $self->$cb('server() is not set.') });
Subroutine __ANON__: not called CC=1 SCAR=6.9
70 0 return $self;
71 }
72 10 ▶ if ($self->{stream_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"stream_id"}) 0 10 10
73 0 ▶ $self->ioloop->next_tick(sub { $self->$cb('') });
Subroutine __ANON__: not called CC=1 SCAR=6.9
74 0 return $self;
75 }
76  
77 10 ▶ if ($self->local_address) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->local_address) 1 9 10
78 1 push @extra, local_address => $self->local_address;
79 }
80 10 ▶ if (my $tls = $self->tls) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tls = $self->tls) 0 10 10
81 0 push @extra, tls => 1;
82 0 ▶ push @extra, tls_ca => $tls->{ca} if $tls->{ca}; # not sure why this should be supported, but adding it anyway
Branch: 1 branch
BranchTrueFalseTotal
if $tls->{"ca"} 0 0 0
83 0 ▶ push @extra, tls_cert => $tls->{cert} || DEFAULT_CERT;
Condition0%0/2
$tls->{'cert'} || '/home/cpancover/.cpan/build/Mojo-IRC-0.47-0/blib/lib/Mojo/mojo-irc-client.crt'
l!l
00
MC/DC0%0/1
$tls->{'cert'} || '/home/cpancover/.cpan/build/Mojo-IRC-0.47-0/blib/lib/Mojo/mojo-irc-client.crt'
$tls->{'cert'}
Truth table0%0/2
A
A= $tls->{'cert'}
Aresult
11
00
84 0 ▶ push @extra, tls_key => $tls->{key} || DEFAULT_KEY;
Condition0%0/2
$tls->{'key'} || '/home/cpancover/.cpan/build/Mojo-IRC-0.47-0/blib/lib/Mojo/mojo-irc-client.key'
l!l
00
MC/DC0%0/1
$tls->{'key'} || '/home/cpancover/.cpan/build/Mojo-IRC-0.47-0/blib/lib/Mojo/mojo-irc-client.key'
$tls->{'key'}
Truth table0%0/2
A
A= $tls->{'key'}
Aresult
11
00
85 0 ▶ push @extra, tls_verify => 0x00 if $tls->{insecure}; # Mojolicious < 9.0
Branch: 1 branch
BranchTrueFalseTotal
if $tls->{"insecure"} 0 0 0
86 0 ▶ push @extra, tls_options => {SSL_verify_mode => 0x00} if $tls->{insecure}; # Mojolicious >= 9.0
Branch: 1 branch
BranchTrueFalseTotal
if $tls->{"insecure"} 0 0 0
87 }
88  
89 10 ▶ $port ||= 6667;
Condition100%2/2
$port ||= 6667
l!l
91
MC/DC100%1/1
$port ||= 6667
$port
Truth table100%2/2
A
A= $port
Aresult
11
00
90 10 $self->{buffer} = '';
91 10 ▶ $self->{debug_key} ||= "$host:$port";
Condition100%2/2
$self->{"debug_key"} ||= "${host}:$port"
l!l
37
MC/DC100%1/1
$self->{"debug_key"} ||= "${host}:$port"
$self->{"debug_key"}
Truth table100%2/2
A
A= $self->{"debug_key"}
Aresult
11
00
92 10 $self->register_default_event_handlers;
93  
94 10 Scalar::Util::weaken($self);
95 $self->{stream_id} = $self->ioloop->client(
96 address => $host,
97 port => $port,
98 timeout => $self->connect_timeout,
99 @extra,
100 9 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
101 9 my ($loop, $err, $stream) = @_;
102  
103 9 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 1 8 9
104 1 delete $self->{stream_id};
105 1 return $self->$cb($err);
106 }
107  
108 8 $stream->timeout(0);
109 $stream->on(
110 5 ▶ close => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
111 5 ▶ $self or return;
Branch: 1 branch
BranchTrueFalseTotal
$self or return 5 0 5
112 5 warn "[$self->{debug_key}] : close\n" if DEBUG;
113 5 delete $self->{stream};
114 5 delete $self->{stream_id};
115 5 $self->emit('close');
116 }
117 8 );
118 $stream->on(
119 0 ▶ error => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
120 0 ▶ $self or return;
Branch: 1 branch
BranchTrueFalseTotal
$self or return 0 0 0
121 0 ▶ $self->ioloop or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->ioloop or return 0 0 0
122 0 $self->ioloop->remove(delete $self->{stream_id});
123 0 $self->emit(error => $_[1]);
124 }
125 8 );
126 8 ▶ $stream->on(read => sub { $self->_read($_[1]) });
Subroutine __ANON__: called CC=1 SCAR=0.0
127  
128 8 $self->{stream} = $stream;
129 8 ▶ $self->ioloop->next_tick(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
130 8 my @promises;
131 8 ▶ push @promises, $self->write_p(PASS => $self->pass) if length $self->pass;
Branch: 1 branch
BranchTrueFalseTotal
if length $self->pass 1 7 8
132 8 push @promises, $self->write_p(NICK => $self->nick);
133 8 push @promises, $self->write_p(USER => $self->user, 8, '*', ':' . $self->name);
134 8 ▶ Mojo::Promise->all(@promises)->finally(sub { $self->$cb('') });
Subroutine __ANON__: called CC=1 SCAR=0.0
135 8 });
136 }
137 10 );
138  
139 10 return $self;
140 }
141  
142 3 ▶sub ctcp {
Subroutine ctcp: called CC=1 SCAR=0.0
Pod documented
143 3 my $self = shift;
144 3 local $_ = join ' ', @_;
145 3 s/([\012\015\0\cP])/\cP$CTCP_QUOTE{$1}/g;
146 3 s/\001/\\a/g;
147 3 ":\001${_}\001";
148 }
149  
150 3 ▶sub disconnect {
Subroutine disconnect: called CC=4 SCAR=14.6
Pod documented
151 3 my ($self, $cb) = @_;
152  
153 3 ▶ if (my $tid = delete $self->{ping_tid}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tid = delete $self->{"ping_tid"}) 0 3 3
154 0 $self->ioloop->remove($tid);
155 }
156  
157 3 ▶ if ($self->{stream}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'stream'}) { } 2 1 3
elsif ($cb) { } 0 1 1
158 2 Scalar::Util::weaken($self);
159 $self->{stream}->write(
160 "QUIT\r\n",
161 2 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
162 2 $self->{stream}->close;
163 2 ▶ $self->$cb if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 1 1 2
164 }
165 2 );
166 }
167 elsif ($cb) {
168 0 ▶ $self->ioloop->next_tick(sub { $self->$cb });
Subroutine __ANON__: not called CC=1 SCAR=6.9
169 }
170  
171 3 $self;
172 }
173  
174 11 ▶sub register_default_event_handlers {
Subroutine register_default_event_handlers: called CC=3 SCAR=11.0
Pod documented
175 11 my $self = shift;
176  
177 11 for my $event (@DEFAULT_EVENTS) {
178 99 ▶ $self->on($event => $self->can($event)) unless $self->has_subscribers($event);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_subscribers($event) 72 27 99
179 }
180  
181 11 return $self;
182 }
183  
184 25 ▶sub write {
Subroutine write: called CC=3 SCAR=11.3
Pod documented
185 11 ▶ no warnings 'utf8';
Subroutine BEGIN: called CC=1 SCAR=0.0
186 25 ▶ my $cb = ref $_[-1] eq 'CODE' ? pop : sub { };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'CODE' ? : 18 7 25
187 25 my $self = shift;
188 0 ▶ my $buf = Unicode::UTF8::encode_utf8(join(' ', @_), sub { $_[0] });
Subroutine __ANON__: not called CC=1 SCAR=1.2
189  
190 25 Scalar::Util::weaken($self);
191 25 ▶ if (ref $self->{stream}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'stream'}) { } 25 0 25
192 25 warn "[$self->{debug_key}] <<< $buf\n" if DEBUG;
193 25 ▶ $self->{stream}->write("$buf\r\n", sub { $self->$cb(''); });
Subroutine __ANON__: called CC=1 SCAR=0.0
194 }
195 else {
196 0 ▶ $self->ioloop->next_tick(sub { $self->$cb('Not connected.') });
Subroutine __ANON__: not called CC=1 SCAR=6.9
197 }
198  
199 25 $self;
200 }
201  
202 17 ▶sub write_p {
Subroutine write_p: called CC=1 SCAR=0.0
Pod documented
203 17 my ($self, @args) = @_;
204 17 my $p = Mojo::Promise->new->ioloop($self->ioloop);
205 17 ▶ $self->write(@args, sub { length $_[1] ? $p->reject($_[1]) : $p->resolve(1) });
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
length $_[1] ? : 0 13 13
206 17 return $p;
207 }
208  
209 1 ▶sub ctcp_ping {
Subroutine ctcp_ping: called CC=2 SCAR=7.0
Pod documented
210 1 my ($self, $message) = @_;
211 1 my $ts = $message->{params}[1];
212 1 my $nick = IRC::Utils::parse_user($message->{prefix});
213  
214 1 ▶ return $self unless $ts;
Branch: 1 branch
BranchTrueFalseTotal
unless $ts 0 1 1
215 1 return $self->write('NOTICE', $nick, $self->ctcp(PING => $ts));
216 }
217  
218 1 ▶sub ctcp_time {
Subroutine ctcp_time: called CC=1 SCAR=0.0
Pod documented
219 1 my ($self, $message) = @_;
220 1 my $nick = IRC::Utils::parse_user($message->{prefix});
221  
222 1 $self->write(NOTICE => $nick, $self->ctcp(TIME => scalar localtime));
223 }
224  
225 1 ▶sub ctcp_version {
Subroutine ctcp_version: called CC=1 SCAR=0.0
Pod documented
226 1 my ($self, $message) = @_;
227 1 my $nick = IRC::Utils::parse_user($message->{prefix});
228  
229 1 $self->write(NOTICE => $nick, $self->ctcp(VERSION => 'Mojo-IRC', $VERSION));
230 }
231  
232 7 ▶sub irc_nick {
Subroutine irc_nick: called CC=3 SCAR=11.0
Pod documented
233 7 my ($self, $message) = @_;
234 7 ▶ my $old_nick = ($message->{prefix} =~ /^[~&@%+]?(.*?)!/)[0] || '';
Condition50%1/2
($message->{'prefix'} =~ /^[~&\@%+]?(.*?)!/u)[0] || ''
l!l
70
MC/DC0%0/1
($message->{'prefix'} =~ /^[~&\@%+]?(.*?)!/u)[0] || ''
($message->{'prefix'} =~ /^[~&\@%+]?(.*?)!/u)[0]
Truth table50%1/2
A
A= ($message->{'prefix'} =~ /^[~&\@%+]?(.*?)!/u)[0]
Aresult
11
00
235  
236 7 ▶ if (lc $old_nick eq lc $self->nick) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $old_nick eq lc $self->nick) 6 1 7
237 6 $self->nick($message->{params}[0]);
238 }
239 }
240  
241 10 ▶sub irc_notice {
Subroutine irc_notice: called CC=2 SCAR=6.9
Pod documented
242 10 my ($self, $message) = @_;
243  
244 # NOTICE AUTH :*** Ident broken or disabled, to continue to connect you must type /QUOTE PASS 21105
245 10 ▶ if ($message->{params}[1] =~ m!Ident broken.*QUOTE PASS (\S+)!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($message->{"params"}[1] =~ /Ident broken.*QUOTE PASS (\S+)/u) 1 9 10
246 1 $self->write(QUOTE => PASS => $1);
247 }
248 }
249  
250 1 ▶sub irc_ping {
Subroutine irc_ping: called CC=1 SCAR=0.0
Pod documented
251 1 my ($self, $message) = @_;
252 1 $self->write(PONG => $message->{params}[0]);
253 }
254  
255 2 ▶sub irc_rpl_isupport {
Subroutine irc_rpl_isupport: called CC=4 SCAR=13.9
Pod documented
256 2 my ($self, $message) = @_;
257 2 my $params = $message->{params};
258 2 my $server_settings = $self->server_settings;
259 2 my %got;
260  
261 2 for my $i (1 .. @$params - 1) {
262 24 ▶ next unless $params->[$i] =~ /([A-Z]+)=?(\S*)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $params->[$i] =~ /([A-Z]+)=?(\S*)/u 2 22 24
263 22 my ($k, $v) = (lc $1, $2);
264 22 $got{$k} = 1;
265 22 ▶ $server_settings->{$k} = $v || 1;
Condition100%2/2
$v || 1
l!l
193
MC/DC100%1/1
$v || 1
$v
Truth table100%2/2
A
A= $v
Aresult
11
00
266 }
267 }
268  
269 2 ▶sub irc_rpl_welcome {
Subroutine irc_rpl_welcome: called CC=3 SCAR=11.6
Pod documented
270 2 my ($self, $message) = @_;
271 2 $self->nick($message->{params}[0]);
272  
273 2 Scalar::Util::weaken($self);
274 2 $self->real_host($message->{prefix});
275 $self->{ping_tid} ||= $self->ioloop->recurring(
276 $self->{ping_pong_interval} || 60, # $self->{ping_pong_interval} is EXPERIMENTAL
277 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
278 1 $self->write(PING => $self->real_host);
279 }
280 2 ▶ );
Condition40%2/5
$self->{"ping_tid"} ||= $self->ioloop->recurring($self->{"ping_pong_interval"} || 60, sub { $self->write("PING", $self->real_host); } )
l!l&&r!l&&!r
020
$self->{'ping_pong_interval'} || 60
l!l
02
MC/DC0%0/2
$self->{"ping_tid"} ||= $self->ioloop->recurring($self->{"ping_pong_interval"} || 60, sub { $self->write("PING", $self->real_host); } )
$self->{"ping_tid"} $self->ioloop->recurring($self->{"ping_pong_interval"} || 60, sub { $self->write("PING", $self->real_host); } )
MC/DC0%0/1
$self->{'ping_pong_interval'} || 60
$self->{'ping_pong_interval'}
Truth table33%1/3
A ||= B
A= $self->{"ping_tid"}B= $self->ioloop->recurring($self->{"ping_pong_interval"} || 60, sub { $self->write("PING", $self->real_host); } )
ABresult
1X1
011
000
Truth table50%1/2
A
A= $self->{'ping_pong_interval'}
Aresult
11
00
281 }
282  
283 1 ▶sub err_nicknameinuse {
Subroutine err_nicknameinuse: called CC=1 SCAR=0.0
Pod documented
284 1 my ($self, $message) = @_;
285 1 my $nick = $message->{params}[1];
286  
287 1 $self->nick($nick . '_');
288 1 ▶ $self->write(NICK => $self->nick, sub { });
Subroutine __ANON__: called CC=1 SCAR=0.0
289 }
290  
291 7 ▶sub DESTROY {
Subroutine DESTROY: called CC=4 SCAR=13.9
292 7 my $self = shift;
293 7 ▶ my $ioloop = $self->ioloop or return;
Branch: 1 branch
BranchTrueFalseTotal
my $ioloop = $self->ioloop or return 7 0 7
294 7 my $tid = $self->{ping_tid};
295 7 my $sid = $self->{stream_id};
296  
297 7 ▶ $ioloop->remove($sid) if $sid;
Branch: 1 branch
BranchTrueFalseTotal
if $sid 4 3 7
298 7 ▶ $ioloop->remove($tid) if $tid;
Branch: 1 branch
BranchTrueFalseTotal
if $tid 1 6 7
299 }
300  
301 3 ▶sub _build_nick {
Subroutine _build_nick: called CC=1 SCAR=0.0
302 3 my $nick = shift->user;
303 3 $nick =~ s![^a-z_]!_!g;
304 3 $nick;
305 }
306  
307 53 ▶sub _dispatch_message {
Subroutine _dispatch_message: called CC=1 SCAR=0.0
308 53 my ($self, $msg) = @_;
309 53 $self->emit(message => $msg);
310 }
311  
312 53 ▶sub _legacy_dispatch_message {
Subroutine _legacy_dispatch_message: called CC=2 SCAR=6.9
313 53 my ($self, $msg) = @_;
314 53 my $event = $msg->{event};
315  
316 53 ▶ $event = "irc_$event" unless $event =~ /^(ctcp(reply)?|err)_/;
Branch: 1 branch
BranchTrueFalseTotal
unless $event =~ /^(ctcp(reply)?|err)_/u 49 4 53
317 53 warn "[$self->{debug_key}] === $event\n" if DEBUG == 2;
318 53 $self->emit($event => $msg);
319 }
320  
321 # Can be used in unittest to mock input data:
322 # $irc->_read($bytes);
323 100 ▶sub _read {
Subroutine _read: called CC=6 SCAR=18.7
324 100 my $self = shift;
325  
326 11 ▶ no warnings 'utf8';
Subroutine BEGIN: called CC=1 SCAR=0.0
327 0 ▶ $self->{buffer} .= Unicode::UTF8::decode_utf8($_[0], sub { $_[0] });
Subroutine __ANON__: not called CC=1 SCAR=1.2
328  
329 CHUNK:
330 100 ▶ while ($self->{buffer} =~ s/^([^\015\012]+)[\015\012]//m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"buffer"} =~ s/^([^\015\012]+)[\015\012]//mu) 53 100 153
331 53 warn "[$self->{debug_key}] >>> $1\n" if DEBUG;
332 53 my $msg = $self->parser->parse($1);
333 53 ▶ my $cmd = $msg->{command} or next CHUNK;
Branch: 1 branch
BranchTrueFalseTotal
my $cmd = $msg->{"command"} or next CHUNK 53 0 53
334 53 ▶ $msg->{command} = $NUMERIC2NAME{$cmd} || IRC::Utils::numeric_to_name($cmd) || $cmd if $cmd =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $cmd =~ /^\d+$/u 39 14 53
Condition33%2/6
$Mojo::IRC::NUMERIC2NAME{$cmd} || IRC::Utils::numeric_to_name($cmd) || $cmd
l!l&&r!l&&!r
3900
$Mojo::IRC::NUMERIC2NAME{$cmd} or IRC::Utils::numeric_to_name($cmd)
l!l&&r!l&&!r
0390
MC/DC0%0/3
$Mojo::IRC::NUMERIC2NAME{$cmd} || IRC::Utils::numeric_to_name($cmd) || $cmd
$Mojo::IRC::NUMERIC2NAME{$cmd} IRC::Utils::numeric_to_name($cmd) $cmd
Truth table0%0/4
A or B || C
A= $Mojo::IRC::NUMERIC2NAME{$cmd}B= IRC::Utils::numeric_to_name($cmd)C= $cmd
ABCresult
1XX1
01X1
0011
0000
335 53 $msg->{event} = lc $msg->{command};
336 53 $self->_dispatch_message($msg);
337 }
338 }
339  
340 11 1;
341  
342 =encoding utf8
343  
344 =head1 NAME
345  
346 Mojo::IRC - IRC Client for the Mojo IOLoop
347  
348 =head1 VERSION
349  
350 0.46
351  
352 =head1 SYNOPSIS
353  
354 my $irc = Mojo::IRC->new(
355 nick => 'test123',
356 user => 'my name',
357 server => 'irc.perl.org:6667',
358 );
359  
360 $irc->on(irc_join => sub {
361 my($self, $message) = @_;
362 warn "yay! i joined $message->{params}[0]";
363 });
364  
365 $irc->on(irc_privmsg => sub {
366 my($self, $message) = @_;
367 say $message->{prefix}, " said: ", $message->{params}[1];
368 });
369  
370 $irc->connect(sub {
371 my($irc, $err) = @_;
372 return warn $err if $err;
373 $irc->write(join => '#mojo');
374 });
375  
376 Mojo::IOLoop->start;
377  
378 =head1 DESCRIPTION
379  
380 L<Mojo::IRC> is a non-blocking IRC client using L<Mojo::IOLoop> from the
381 wonderful L<Mojolicious> framework.
382  
383 It features IPv6 and TLS, with additional optional modules:
384 L<IO::Socket::IP> and L<IO::Socket::SSL>.
385  
386 By default this module will only emit standard IRC events, but by
387 settings L</parser> to a custom object it will also emit CTCP events.
388 Example:
389  
390 my $irc = Mojo::IRC->new;
391 $irc->parser(Parse::IRC->new(ctcp => 1);
392 $irc->on(ctcp_action => sub {
393 # ...
394 });
395  
396 It will also set up some default events: L</ctcp_ping>, L</ctcp_time>,
397 and L</ctcp_version>.
398  
399 This class inherits from L<Mojo::EventEmitter>.
400  
401 =head1 TESTING
402  
403 The module L<Test::Mojo::IRC> is useful if you want to write tests without
404 having a running IRC server.
405  
406 L<MOJO_IRC_OFFLINE> (from v0.20) is now DEPRECATED in favor of
407 L<Test::Mojo::IRC>.
408  
409 =head1 EVENTS
410  
411 =head2 close
412  
413 $self->on(close => sub { my ($self) = @_; });
414  
415 Emitted once the connection to the server closes.
416  
417 =head2 error
418  
419 $self->on(error => sub { my ($self, $err) = @_; });
420  
421 Emitted once the stream emits an error.
422  
423 =head2 message
424  
425 $self->on(message => sub { my ($self, $msg) = @_; });
426  
427 Emitted when a new IRC message arrives. Will dispatch to a default handler,
428 which will again emit L</err_event_name> L</ctcp_event_name> and
429 L</irc_event_name> below.
430  
431 Here is an example C<$msg>:
432  
433 {
434 command => "PRIVMSG",
435 event => "privmsg",
436 params => ["#convos", "hey!"],
437 prefix => "jan_henning",
438 raw_line => ":jan_henning PRIVMSG #convos :hey",
439 }
440  
441 =head2 err_event_name
442  
443 Events that start with "err_" are emitted when there is an IRC response that
444 indicates an error. See L<Mojo::IRC::Events> for sample events.
445  
446 =head2 ctcp_event_name
447  
448 Events that start with "ctcp_" are emitted if the L</parser> can understand
449 CTCP messages, and there is a CTCP response.
450  
451 $self->parser(Parse::IRC->new(ctcp => 1);
452  
453 See L<Mojo::IRC::Events> for sample events.
454  
455 =head2 irc_event_name
456  
457 Events that start with "irc_" are emitted when there is a normal IRC response.
458 See L<Mojo::IRC::Events> for sample events.
459  
460 =head1 ATTRIBUTES
461  
462 =head2 connect_timeout
463  
464 $int = $self->connect_timeout;
465 $self = $self->connect_timeout(60);
466  
467 Maximum amount of time in seconds establishing a connection may take before
468 getting canceled, defaults to the value of the C<MOJO_IRC_CONNECT_TIMEOUT>
469 environment variable or 30.
470  
471 =head2 ioloop
472  
473 Holds an instance of L<Mojo::IOLoop>.
474  
475 =head2 local_address
476  
477 $str = $self->local_address;
478 $self = $self->local_address("10.20.30.40");
479  
480 Local address to bind to. See L<Mojo::IOLoop::Client/local_address>.
481  
482 =head2 name
483  
484 The name of this IRC client. Defaults to "Mojo IRC".
485  
486 =head2 nick
487  
488 IRC nick name accessor. Default to L</user>.
489  
490 =head2 parser
491  
492 $self = $self->parser($obj);
493 $self = $self->parser(Parse::IRC->new(ctcp => 1));
494 $obj = $self->parser;
495  
496 Holds a L<Parse::IRC> object by default.
497  
498 =head2 pass
499  
500 Password for authentication
501  
502 =head2 real_host
503  
504 Will be set by L</irc_rpl_welcome>. Holds the actual hostname of the IRC
505 server that we are connected to.
506  
507 =head2 server
508  
509 Server name and, optionally, a port to connect to. Changing this while
510 connected to the IRC server will issue a reconnect.
511  
512 =head2 server_settings
513  
514 $hash = $self->server_settings;
515  
516 Holds information about the server. See
517 L<https://github.com/jhthorsen/mojo-irc/blob/master/t/ua-channel-users.t> for
518 example data structure.
519  
520 Note that this attribute is EXPERIMENTAL and the structure of the values it
521 holds.
522  
523 =head2 user
524  
525 IRC username. Defaults to current logged in user or falls back to "anonymous".
526  
527 =head2 tls
528  
529 $self->tls(undef) # disable (default)
530 $self->tls({}) # enable
531  
532 Default is "undef" which disables TLS. Setting this to an empty hash will
533 enable TLS and this module will load in default certs. It is also possible
534 to set custom cert/key:
535  
536 $self->tls({ cert => "/path/to/client.crt", key => ... })
537  
538 This can be generated using
539  
540 # certtool --generate-privkey --outfile client.key
541 # certtool --generate-self-signed --load-privkey client.key --outfile client.crt
542  
543 To disable the verification of server certificates, the "insecure" option
544 can be set:
545  
546 $self->tls({insecure => 1});
547  
548 =head1 METHODS
549  
550 =head2 connect
551  
552 $self = $self->connect(\&callback);
553  
554 Will log in to the IRC L</server> and call C<&callback>. The
555 C<&callback> will be called once connected or if connect fails. The second
556 argument will be an error message or empty string on success.
557  
558 =head2 ctcp
559  
560 $str = $self->ctcp(@str);
561  
562 This message will quote CTCP messages. Example:
563  
564 $self->write(PRIVMSG => nickname => $self->ctcp(TIME => time));
565  
566 The code above will write this message to IRC server:
567  
568 PRIVMSG nickname :\001TIME 1393006707\001
569  
570 =head2 disconnect
571  
572 $self->disconnect(\&callback);
573  
574 Will disconnect form the server and run the callback once it is done.
575  
576 =head2 new
577  
578 $self = Mojo::IRC->new(%attrs);
579  
580 Object constructor.
581  
582 =head2 register_default_event_handlers
583  
584 $self->register_default_event_handlers;
585  
586 This method sets up the default L</DEFAULT EVENT HANDLERS> unless someone has
587 already subscribed to the event.
588  
589 =head2 write
590  
591 $self->write(@str, \&callback);
592  
593 This method writes a message to the IRC server. C<@str> will be concatenated
594 with " " and "\r\n" will be appended. C<&callback> is called once the message is
595 delivered over the stream. The second argument to the callback will be
596 an error message: Empty string on success and a description on error.
597  
598 =head2 write_p
599  
600 $promise = $self->write_p(@str);
601  
602 Like L</"write">, but returns a L<Mojo::Promise> instead of taking a callback.
603 The promise will be resolved on success, or rejected with the error message on
604 error.
605  
606 =head1 DEFAULT EVENT HANDLERS
607  
608 =head2 ctcp_ping
609  
610 Will respond to the sender with the difference in time.
611  
612 Ping reply from $sender: 0.53 second(s)
613  
614 =head2 ctcp_time
615  
616 Will respond to the sender with the current localtime. Example:
617  
618 TIME Fri Feb 21 18:56:50 2014
619  
620 NOTE! The localtime format may change.
621  
622 =head2 ctcp_version
623  
624 Will respond to the sender with:
625  
626 VERSION Mojo-IRC $VERSION
627  
628 NOTE! Additional information may be added later on.
629  
630 =head2 irc_nick
631  
632 Used to update the L</nick> attribute when the nick has changed.
633  
634 =head2 irc_notice
635  
636 Responds to the server with "QUOTE PASS ..." if the notice contains "Ident
637 broken...QUOTE PASS...".
638  
639 =head2 irc_ping
640  
641 Responds to the server with "PONG ...".
642  
643 =head2 irc_rpl_isupport
644  
645 Used to populate L</server_settings> with information about the server.
646  
647 =head2 irc_rpl_welcome
648  
649 Used to get the hostname of the server. Will also set up automatic PING
650 requests to prevent timeout and update the L</nick> attribute.
651  
652 =head2 err_nicknameinuse
653  
654 This handler will add "_" to the failed nick before trying to register again.
655  
656 =head1 COPYRIGHT
657  
658 This program is free software, you can redistribute it and/or modify it under
659 the terms of the Artistic License version 2.0.
660  
661 =head1 AUTHOR
662  
663 Marcus Ramberg - C<mramberg@cpan.org>
664  
665 Jan Henning Thorsen - C<jhthorsen@cpan.org>
666  
667 =cut
« Mojo/IRC/UA.pm ↑ Summary Test/Mojo/IRC.pm »