Mojo/IRC/UA.pm untested

filter: Statementstmt 0.0% 0 / 276 Branchbran 0.0% 0 / 94 Conditioncond 0.0% 0 / 92 MC/DCmcdc 0.0% 0 / 48 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 543 CC 47 SCAR 37.6
CC
47
Cov
0%
channel_topic
90.0
join_channel
72.0
nick
72.0
CRAP 43.0
1 package Mojo::IRC::UA;
2 use Mojo::Base 'Mojo::IRC';
3  
4 use Data::Dumper ();
5 use IRC::Utils ();
6 use Mojo::Util ();
7 use constant DEBUG => $ENV{MOJO_IRC_DEBUG} || 0;
8  
9 Mojo::Util::deprecated 'Mojo::IRC::UA is deprecated and should no longer be used';
10  
11 has op_timeout => 10;
12  
13 has _parse_namreply_map => sub {
14 my $self = shift;
15 my @prefix = split /[\(\)]/, $self->server_settings->{prefix} || '(@+)ov';
16 my $i = 0;
17 my %map = map { (substr($prefix[2], $i++, 1), $_) } split //, $prefix[1];
18 my $re = "^([$prefix[2]])";
19  
20 warn "[$self->{debug_key}] : parse_namreply_map=@{[Data::Dumper->new([[$re, \%map]])->Indent(0)->Terse(1)->Dump]}\n"
21 if DEBUG == 2;
22  
23 return [qr{$re}, \%map];
24 };
25  
26 sub channels {
27 my ($self, $cb) = @_;
28 my %channels;
29  
30 return $self->_write_and_wait(
31 Parse::IRC::parse_irc("LIST"),
32 {
33 rpl_listend => {}, # :hybrid8.debian.local 323 superman :End of /LIST
34 rpl_list => sub {
35 my ($self, $msg) = @_;
36 my $topic = $msg->{params}[3] // '';
37 $topic =~ s!^\[\+[a-z]+\]\s?!!; # remove mode from topic, such as [+nt]
38 $channels{$msg->{params}[1]} = {n_users => $msg->{params}[2], topic => $topic};
39 },
40 },
41 sub {
42 my ($self, $event, $err, $msg) = @_;
43 my $n = 0;
44  
45 return $self->$cb($err || $msg->{params}[1] || $event, {}) if $event =~ /^err_/;
46 return $self->$cb('', \%channels);
47 },
48 );
49 }
50  
51 sub channel_topic {
52 my $cb = pop;
53 my ($self, $channel, $topic) = @_;
54 my $res = length($topic // '') ? {} : undef;
55  
56 if (!$channel) {
57 $self->ioloop->next_tick(sub { $self->$cb('Cannot get/set topic without channel name.', {}) });
58 return $self;
59 }
60 if ($channel =~ /\s/) {
61 $self->ioloop->next_tick(sub { $self->$cb('Cannot get/set topic on channel with spaces in name.', {}) });
62 return $self;
63 }
64  
65 return $self->_write_and_wait(
66 $res ? Parse::IRC::parse_irc("TOPIC $channel :$topic") : Parse::IRC::parse_irc("TOPIC $channel"),
67 {
68 err_chanoprivsneeded => {1 => $channel},
69 err_nochanmodes => {1 => $channel},
70 err_notonchannel => {1 => $channel},
71 rpl_notopic => {1 => $channel},
72 rpl_topic => {1 => $channel}, # :hybrid8.debian.local 332 superman #convos :get cool topic
73 topic => {0 => $channel}, # set
74 },
75 sub {
76 my ($self, $event, $err, $msg) = @_;
77  
78 if ($event eq 'rpl_notopic') {
79 $res->{topic} = '';
80 }
81 elsif ($event eq 'rpl_topic') {
82 $res->{topic} = $msg->{params}[2] // '';
83 }
84 elsif ($event eq 'topic') {
85 $err = '';
86 }
87 else {
88 $err ||= $msg->{params}[2] || $event;
89 }
90  
91 return $self->$cb($err, $res) if $res;
92 return $self->$cb($err);
93 }
94 );
95 }
96  
97 sub channel_users {
98 my ($self, $channel, $cb) = @_;
99 my $users = {};
100  
101 if (!$channel) {
102 $self->ioloop->next_tick(sub { $self->$cb('Cannot get users without channel name.', {}) });
103 return $self;
104 }
105  
106 return $self->_write_and_wait(
107 Parse::IRC::parse_irc("NAMES $channel"),
108 {
109 err_toomanymatches => {1 => $channel},
110 err_nosuchserver => {},
111 rpl_endofnames => {1 => $channel},
112 rpl_namreply => sub {
113 my ($self, $msg) = @_;
114 $self->_parse_namreply($msg, $users) if lc $msg->{params}[2] eq lc $channel;
115 },
116 },
117 sub {
118 my ($self, $event, $err, $msg) = @_;
119 $self->$cb($event =~ /^err_/ ? $err || $msg->{params}[2] || $event : '', $users);
120 }
121 );
122 }
123  
124 sub join_channel {
125 my ($self, $command, $cb) = @_;
126 my ($channel) = split /\s/, $command, 2;
127 my $info = {topic => '', topic_by => '', users => {}};
128  
129 # err_needmoreparams and will not allow special "JOIN 0"
130  
131 if (!$channel) {
132 $self->ioloop->next_tick(sub { $self->$cb('Cannot join without channel name.') });
133 return $self;
134 }
135  
136 return $self->_write_and_wait(
137 Parse::IRC::parse_irc("JOIN $command"),
138 {
139 err_badchanmask => {1 => $channel},
140 err_badchannelkey => {1 => $channel},
141 err_bannedfromchan => {1 => $channel}, # :hybrid8.debian.local 474 superman #convos :Cannot join channel (+b)
142 err_channelisfull => {1 => $channel},
143 err_inviteonlychan => {1 => $channel},
144 err_nosuchchannel => {1 => $channel}, # :hybrid8.debian.local 403 nick #convos :No such channel
145 err_toomanychannels => {1 => $channel},
146 err_toomanytargets => {1 => $channel},
147 err_unavailresource => {1 => $channel},
148 479 => {1 => $channel}, # Illegal channel name
149 rpl_endofnames => {1 => $channel}, # :hybrid8.debian.local 366 superman #convos :End of /NAMES list.
150 err_linkchannel => sub {
151 my ($self, $msg) = @_;
152 return unless lc $msg->{params}[1] eq lc $channel;
153 for my $item (values %{$msg->{look_for}}) {
154 $item->{1} = $msg->{params}[2] if ref $item eq 'HASH' and $item->{1} and $item->{1} eq $channel;
155 }
156 $channel = $msg->{params}[2];
157 },
158 rpl_namreply => sub {
159 my ($self, $msg) = @_;
160 $self->_parse_namreply($msg, $info->{users}) if lc $msg->{params}[2] eq lc $channel;
161 },
162 rpl_topic => sub {
163 my ($self, $msg) = @_;
164 $info->{topic} = $msg->{params}[2] if lc $msg->{params}[1] eq lc $channel;
165 },
166 rpl_topicwhotime => sub {
167 my ($self, $msg) = @_;
168 $info->{topic_by} = $msg->{params}[2] if lc $msg->{params}[1] eq lc $channel;
169 },
170 },
171 sub {
172 my ($self, $event, $err, $msg) = @_;
173 $info->{name} = $channel;
174 $self->$cb($event =~ /^(?:err_|479)/ ? $err || $msg->{params}[2] || $event : '', $info);
175 }
176 );
177 }
178  
179 sub kick {
180 my ($self, $command, $cb) = @_;
181 my ($target, $user) = $command =~ /(\S+)\s+(.*)/;
182 my $res = {reason => ''};
183  
184 $target //= '';
185 $user //= '';
186 $self->_write_and_wait(
187 Parse::IRC::parse_irc("KICK $command"),
188 {
189 err_needmoreparams => {},
190 err_nosuchchannel => {1 => $target},
191 err_nosuchnick => {1 => $user},
192 err_badchanmask => {1 => $target},
193 err_chanoprivsneeded => {1 => $target},
194 err_usernotinchannel => {1 => $user},
195 err_notonchannel => {1 => $target},
196 kick => {0 => $target, 1 => $user},
197 },
198 sub {
199 my ($self, $event, $err, $msg) = @_;
200 my ($nick) = IRC::Utils::parse_user($msg->{prefix});
201 $msg->{params}[2] //= '';
202 $res->{reason} = lc $msg->{params}[2] eq lc $nick ? '' : $msg->{params}[2];
203 $self->$cb($event =~ /^err_/ ? $err || $msg->{params}[2] || $event : '', $res);
204 }
205 );
206 }
207  
208 sub mode {
209 my $cb = pop;
210 my ($self, $mode) = @_;
211 return $self->_mode_for_channel($1, $2, $cb) if $mode and $mode =~ /(\S+)\s+(.+)/;
212 return $self->_mode_for_user($mode, $cb); # get or set
213 }
214  
215 sub nick {
216 my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
217 my ($self, $nick) = @_;
218  
219 unless ($cb) {
220 return $self->{nick} ||= $self->_build_nick unless defined $nick;
221 $self->{nick} = $nick;
222 return $self;
223 }
224  
225 if ($nick) {
226 if ($self->{stream}) {
227 $self->_write_and_wait(
228 Parse::IRC::parse_irc("NICK $nick"),
229 {
230 err_erroneusnickname => {0 => $nick},
231 err_nickcollision => {0 => $nick},
232 err_nicknameinuse => {1 => $nick},
233 err_restricted => {},
234 err_unavailresource => {},
235 nick => {0 => $nick}, # :Superman12923!superman@i.love.debian.org NICK :Supermanx12923
236 },
237 sub {
238 my ($self, $event, $err, $msg) = @_;
239 $self->nick($nick) if $event eq 'nick';
240 $self->$cb($event =~ /^err_/ ? $err || $msg->{params}[2] || $event : '');
241 }
242 );
243 }
244 else {
245 $self->nick($nick)->$cb('');
246 }
247 }
248 else {
249 $self->$cb('', $self->nick);
250 }
251  
252 return $self;
253 }
254  
255 sub part_channel {
256 my ($self, $channel, $cb) = @_;
257  
258 # err_needmoreparams
259 if (!$channel) {
260 $self->ioloop->next_tick(sub { $self->$cb('Cannot part without channel name.') });
261 return $self;
262 }
263 if ($channel =~ /\s/) {
264 $self->ioloop->next_tick(sub { $self->$cb('Cannot part channel with spaces.') });
265 return $self;
266 }
267  
268 return $self->_write_and_wait(
269 Parse::IRC::parse_irc("PART $channel"),
270 {
271 err_nosuchchannel => {1 => $channel}, # :hybrid8.debian.local 403 nick #convos :No such channel
272 err_notonchannel => {1 => $channel},
273 479 => {1 => $channel}, # Illegal channel name
274 part => {0 => $channel},
275 },
276 sub {
277 my ($self, $event, $err, $msg) = @_;
278 $self->$cb($event =~ /^(?:err_|479)/ ? $err || $msg->{params}[2] || $event : '');
279 }
280 );
281  
282 }
283  
284 sub whois {
285 my ($self, $target, $cb) = @_;
286 my $info = {channels => {}, name => '', nick => $target, server => '', user => ''};
287  
288 unless ($target) {
289 $self->ioloop->next_tick(sub { $self->$cb('Cannot retrieve whois information without target.', {}) });
290 return $self;
291 }
292  
293 return $self->_write_and_wait(
294 Parse::IRC::parse_irc("WHOIS $target"),
295 {
296 err_nosuchnick => {1 => $target}, # :hybrid8.debian.local 401 superman batman :No such nick/channel
297 err_nosuchserver => {1 => $target},
298 rpl_away => {1 => $target},
299 rpl_endofwhois => {1 => $target},
300 rpl_whoischannels => sub {
301 my ($self, $msg) = @_;
302 return unless lc $msg->{params}[1] eq lc $target;
303 for (split /\s+/, $msg->{params}[2] || '') {
304 my ($mode, $channel) = /^([+@]?)(.+)$/;
305 $info->{channels}{$channel} = {mode => $mode};
306 }
307 },
308 rpl_whoisidle => sub {
309 my ($self, $msg) = @_;
310 return unless lc $msg->{params}[1] eq lc $target;
311 $info->{idle_for} = 0 + $msg->{params}[2];
312 },
313 rpl_whoisoperator => {}, # TODO
314 rpl_whoisserver => sub {
315 my ($self, $msg) = @_;
316 return unless lc $msg->{params}[1] eq lc $target;
317 $info->{server} = $msg->{params}[2];
318 $info->{server_info} = $msg->{params}[3];
319 },
320 rpl_whoisuser => sub {
321 my ($self, $msg) = @_;
322 return unless lc $msg->{params}[1] eq lc $target;
323 $info->{nick} = $msg->{params}[1];
324 $info->{user} = $msg->{params}[2];
325 $info->{host} = $msg->{params}[3];
326 $info->{name} = $msg->{params}[5];
327 },
328 },
329 sub {
330 my ($self, $event, $err, $msg) = @_;
331 $self->$cb($event =~ /^err_/ ? $err || $msg->{params}[2] || $event : '', $info);
332 }
333 );
334 }
335  
336 sub _dispatch_message {
337 my ($self, $msg) = @_;
338 my $listeners = $self->{write_and_wait}{$msg->{event}} || {};
339  
340 $self->$_($msg) for values %$listeners;
341 $self->SUPER::_dispatch_message($msg);
342 }
343  
344 sub _mode_for_channel {
345 my ($self, $target, $mode, $cb) = @_;
346 my $res = {banlist => [], exceptlist => [], invitelist => [], uniqopis => [], mode => $mode, params => ''};
347  
348 return $self->_write_and_wait(
349 Parse::IRC::parse_irc("MODE $target $mode"),
350 {
351 err_chanoprivsneeded => {1 => $target},
352 err_keyset => {1 => $target},
353 err_needmoreparams => {1 => $target},
354 err_nochanmodes => {1 => $target},
355 err_unknownmode => {1 => $target},
356 err_usernotinchannel => {1 => $target},
357 mode => {0 => $target},
358 rpl_endofbanlist => {1 => $target},
359 rpl_endofexceptlist => {1 => $target},
360 rpl_endofinvitelist => {1 => $target},
361 rpl_channelmodeis => sub { @$res{qw(mode params)} = @{$_[1]->{params}}[1, 2] },
362 rpl_banlist => sub { push @{$res->{banlist}}, $_[1]->{params}[1] },
363 rpl_exceptlist => sub { push @{$res->{exceptlist}}, $_[1]->{params}[1] },
364 rpl_invitelist => sub { push @{$res->{invitelist}}, $_[1]->{params}[1] },
365 rpl_uniqopis => sub { push @{$res->{uniqopis}}, $_[1]->{params}[1] },
366 },
367 sub {
368 my ($self, $event, $err, $msg) = @_;
369 $self->$cb($event =~ /^(?:err_)/ ? $err || $msg->{params}[2] || $event : '', $res);
370 }
371 );
372 }
373  
374 sub _mode_for_user {
375 my ($self, $mode, $cb) = @_;
376 my $nick = $self->nick;
377  
378 return $self->_write_and_wait(
379 Parse::IRC::parse_irc($mode ? "MODE $nick $mode" : "MODE $nick"),
380 {
381 err_umodeunknownflag => {},
382 err_needmoreparams => {},
383 err_usersdontmatch => {},
384 mode => {0 => $nick},
385 rpl_umodeis => {0 => $nick},
386 },
387 sub {
388 my ($self, $event, $err, $msg) = @_;
389 $self->$cb($event =~ /^(?:err_)/ ? $err || $event : '', $msg->{params}[1]);
390 }
391 );
392 }
393  
394 sub _parse_namreply {
395 my ($self, $msg, $users) = @_;
396 my ($re, $map) = @{$self->_parse_namreply_map};
397  
398 for my $nick (sort { lc $a cmp lc $b } split /\s+/, $msg->{params}[3]) {
399 $users->{$nick}{mode} = $nick =~ s/$re// ? $map->{$1} || $1 : '';
400 }
401 }
402  
403 sub _write_and_wait {
404 my ($self, $msg, $look_for, $handler) = @_;
405 my $cmd = $msg->{raw_line};
406 my ($tid, $timeout);
407  
408 # This method will send a IRC command to the server and wait for a
409 # corresponding IRC event is returned from the server. On such an
410 # event, the $handler callback will be called, but only if the event
411 # received match the rules set in $look_for.
412  
413 Scalar::Util::weaken($self);
414  
415 # We want a "master timeout" as well, in case the server never send
416 # us any response.
417 $tid = $self->ioloop->timer(
418 ($timeout = $self->op_timeout),
419 sub {
420 delete $self->{write_and_wait}{$_}{$cmd} for keys %$look_for;
421 $self->$handler(err_timeout => "Response timeout after ${timeout}s.", {});
422 }
423 );
424  
425 # Set up which IRC events to look for
426 for my $event (keys %$look_for) {
427 $self->{write_and_wait}{$event}{$cmd} = sub {
428 my ($self, $res) = @_;
429 my $needle = $look_for->{$event};
430 $res->{look_for} = $look_for;
431 return $self->$needle($res) if ref $needle eq 'CODE';
432  
433 for my $k (keys %$needle) {
434 my $v = $k =~ /^\d/ ? $res->{params}[$k] : $res->{$k};
435 return unless lc $v eq lc $needle->{$k};
436 }
437  
438 $self->ioloop->remove($tid);
439 delete $self->{write_and_wait}{$_}{$cmd} for keys %$look_for;
440 $self->$handler($event => '', $res);
441 };
442 }
443  
444 # Write the command to the IRC server and stop looking for events
445 # if the write fails.
446 $self->write(
447 $msg->{raw_line},
448 sub {
449 my ($self, $err) = @_;
450 return unless $err; # no error
451 $self->ioloop->remove($tid);
452 delete $self->{write_and_wait}{$_}{$cmd} for keys %$look_for;
453 $self->$handler(err_write => $err, {});
454 }
455 );
456  
457 return $self;
458 }
459  
460 1;
461  
462 =encoding utf8
463  
464 =head1 NAME
465  
466 Mojo::IRC::UA - IRC Client with sugar on top (DEPRECATED)
467  
468 =head1 SYNOPSIS
469  
470 use Mojo::IRC::UA;
471 my $irc = Mojo::IRC::UA->new;
472  
473 =head1 DESCRIPTION
474  
475 L<Mojo::IRC::UA> is a module which extends L<Mojo::IRC> with methods
476 that can track changes in state on the IRC server.
477  
478 This module is DEPRECATED and should no longer be used.
479  
480 =head1 ATTRIBUTES
481  
482 L<Mojo::IRC::UA> inherits all attributes from L<Mojo::IRC> and implements the
483 following new ones.
484  
485 =head2 op_timeout
486  
487 $int = $self->op_timeout;
488 $self = $self->op_timeout($int);
489  
490 Max number of seconds to wait for a response from the IRC server.
491  
492 =head1 EVENTS
493  
494 L<Mojo::IRC::UA> inherits all events from L<Mojo::IRC> and implements the
495 following new ones.
496  
497 =head1 METHODS
498  
499 L<Mojo::IRC::UA> inherits all methods from L<Mojo::IRC> and implements the
500 following new ones.
501  
502 =head2 channels
503  
504 $self = $self->channels(sub { my ($self, $err, $channels) = @_; });
505  
506 Will retrieve available channels on the IRC server. C<$channels> has this
507 structure on success:
508  
509 {
510 "#convos" => {n_users => 4, topic => "[+nt] some cool topic"},
511 }
512  
513 NOTE: This might take a long time, if the server has a lot of channels.
514  
515 =head2 channel_topic
516  
517 $self = $self->channel_topic($channel, $topic, sub { my ($self, $err) = @_; });
518 $self = $self->channel_topic($channel, sub { my ($self, $err, $res) = @_; });
519  
520 Used to get or set topic for a channel. C<$res> is a hash with a key "topic" which
521 holds the current topic.
522  
523 =head2 channel_users
524  
525 $self = $self->channel_users($channel, sub { my ($self, $err, $users) = @_; });
526  
527 This can retrieve the users in a channel. C<$users> contains this structure:
528  
529 {
530 jhthorsen => {mode => "o"},
531 Superman => {mode => ""},
532 }
533  
534 This method is EXPERIMENTAL and can change without warning.
535  
536 =head2 join_channel
537  
538 $self = $self->join_channel($channel => sub { my ($self, $err, $info) = @_; });
539  
540 Used to join an IRC channel. C<$err> will be false (empty string) on a
541 successful join. C<$info> will contain information about the joined channel:
542  
543 {
544 name => "#channel_name",
545 topic => "some cool topic",
546 topic_by => "jhthorsen",
547 users => {
548 jhthorsen => {mode => "@"},
549 Superman => {mode => ""},
550 },
551 }
552  
553 "name" in C<$info> holds the actual channel name that is joined. This will not
554 be the same as C<$channel> in case of "ERR_LINKCHANNEL" (470) events, where you
555 are automatically redirected to another channel.
556  
557 NOTE! This method will fail if the channel is already joined. Unfortunately,
558 the way it will fail is simply by not calling the callback. This should be
559 fixed - Just don't know how yet.
560  
561 =head2 kick
562  
563 $self = $self->kick("#channel superman", sub { my ($self, $err, $res) = @_; });
564  
565 Used to kick a user. C<$res> looks like this:
566  
567 {reason => "you don't behave"}
568  
569 =head2 mode
570  
571 $self = $self->mode(sub { my ($self, $err, $mode) = @_; });
572 $self = $self->mode("-i", sub { my ($self, $err, $mode) = @_; });
573 $self = $self->mode("#channel +k secret", sub { my ($self, $err, $mode) = @_; });
574  
575 This method is used to get or set a user mode or set a channel mode.
576  
577 C<$mode> is EXPERIMENTAL, but holds a hash, with "mode" as key.
578  
579 Note that this method seems to be unstable. Working on a fix:
580 L<https://github.com/jhthorsen/mojo-irc/issues/28>.
581  
582 =head2 nick
583  
584 $self = $self->nick($nick => sub { my ($self, $err) = @_; });
585 $self = $self->nick(sub { my ($self, $err, $nick) = @_; });
586  
587 Used to set or get the nick for this connection.
588  
589 Setting the nick will change L</nick> I<after> the nick is actually
590 changed on the server.
591  
592 =head2 part_channel
593  
594 $self = $self->part_channel($channel => sub { my ($self, $err) = @_; });
595  
596 Used to part/leave a channel.
597  
598 =head2 whois
599  
600 $self = $self->whois($target, sub { my ($self, $err, $info) = @_; });
601  
602 Used to retrieve information about a user. C<$info> contains this information
603 on success:
604  
605 {
606 channels => {"#convos => {mode => "@"}],
607 host => "example.com",
608 idle_for => 17454,
609 name => "Jan Henning Thorsen",
610 nick => "batman",
611 server => "hybrid8.debian.local",
612 user => "jhthorsen",
613 },
614  
615 =head1 COPYRIGHT AND LICENSE
616  
617 Copyright (C) 2014, Jan Henning Thorsen
618  
619 This program is free software, you can redistribute it and/or modify it under
620 the terms of the Artistic License version 2.0.
621  
622 =head1 AUTHOR
623  
624 Jan Henning Thorsen - C<jhthorsen@cpan.org>
625  
626 =cut