ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/Net-IRC3/lib/Net/IRC3/Client/Connection.pm
Revision: 1.27
Committed: Thu Apr 19 20:12:58 2007 UTC (19 years, 6 months ago) by elmex
Branch: MAIN
Changes since 1.26: +51 -0 lines
Log Message:
implemented enable_ping

File Contents

# User Rev Content
1 elmex 1.1 package Net::IRC3::Client::Connection;
2 elmex 1.4 use base "Net::IRC3::Connection";
3 elmex 1.25 use Net::IRC3::Util qw/prefix_nick decode_ctcp/;
4 elmex 1.12 use strict;
5 elmex 1.17 no warnings;
6 elmex 1.1
7 elmex 1.4 our $DEBUG;
8 elmex 1.1
9     =head1 NAME
10    
11     Net::IRC3::Client::Connection - A highlevel IRC connection
12    
13     =head1 SYNOPSIS
14    
15 elmex 1.5 use AnyEvent;
16     use Net::IRC3::Client::Connection;
17 elmex 1.1
18 elmex 1.5 my $c = AnyEvent->condvar;
19 elmex 1.1
20 elmex 1.5 my $timer;
21     my $con = new Net::IRC3::Client::Connection;
22 elmex 1.1
23 elmex 1.5 $con->reg_cb (registered => sub { print "I'm in!\n"; 0 });
24     $con->reg_cb (disconnect => sub { print "I'm out!\n"; 0 });
25     $con->reg_cb (
26     sent => sub {
27     if ($_[2] eq 'PRIVMSG') {
28     print "Sent message!\n";
29     $timer = AnyEvent->timer (after => 1, cb => sub { $c->broadcast });
30     }
31     1
32     }
33     );
34    
35     $con->send_srv (PRIVMSG => "Hello there i'm the cool Net::IRC3 test script!", 'elmex');
36    
37     $con->connect ("localhost", 6667);
38     $con->register (qw/testbot testbot testbot/);
39 elmex 1.1
40 elmex 1.5 $c->wait;
41     undef $timer;
42    
43     $con->disconnect;
44 elmex 1.1
45     =head1 DESCRIPTION
46    
47 elmex 1.25 L<Net::IRC3::Client::Connection> is a (nearly) highlevel client connection,
48 elmex 1.5 that manages all the stuff that noone wants to implement again and again
49     when handling with IRC. For example it PONGs the server or keeps track
50     of the users on a channel.
51 elmex 1.1
52 elmex 1.25 Please note that CTCP handling is still up to you. It will be decoded
53     for you and events will be generated. But generating replies
54     is up to you.
55    
56 elmex 1.13 =head2 A NOTE TO CASE MANAGEMENT
57    
58     The case insensitivity of channelnames and nicknames can lead to headaches
59     when dealing with IRC in an automated client which tracks channels and nicknames.
60    
61     I tried to preserve the case in all channel and nicknames
62     Net::IRC3::Client::Connection passes to his user. But in the internal
63     structures i'm using lower case for the channel names.
64    
65     The returned hash from C<channel_list> for example has the lower case of the
66     joined channels as keys.
67    
68     But i tried to preserve the case in all events that are emitted.
69     Please keep this in mind when handling the events.
70    
71     For example a user might joins #TeSt and parts #test later.
72    
73 elmex 1.2 =head1 EVENTS
74    
75     The following events are emitted by L<Net::IRC3::Client::Connection>.
76     Use C<reg_cb> as described in L<Net::IRC3::Connection> to register to such an
77     event.
78 elmex 1.1
79     =over 4
80    
81 elmex 1.3 =item B<registered>
82    
83     Emitted when the connection got successfully registered.
84    
85 elmex 1.2 =item B<channel_add $channel @nicks>
86    
87     Emitted when C<@nicks> are added to the channel C<$channel>,
88     this happens for example when someone JOINs a channel or when you
89     get a RPL_NAMREPLY (see RFC2812).
90    
91     =item B<channel_remove $channel @nicks>
92    
93     Emitted when C<@nicks> are removed from the channel C<$channel>,
94     happens for example when they PART, QUIT or get KICKed.
95    
96 elmex 1.13 =item B<channel_change $channel $old_nick $new_nick $is_myself>
97    
98     Emitted when a nickname on a channel changes. This is emitted when a NICK
99     change occurs from C<$old_nick> to C<$new_nick> give the application a chance
100     to quickly analyze what channels were affected. C<$is_myself> is true when
101     youself was the one who changed the nick.
102    
103 elmex 1.23 =item B<channel_topic $channel $topic $who>
104    
105     This is emitted when the topic for a channel is discovered. C<$channel>
106     is the channel for which C<$topic> is the current topic now.
107     Which is set by C<$who>. C<$who> might be undefined when it's not known
108     who set the channel topic.
109    
110 elmex 1.2 =item B<join $nick $channel $is_myself>
111    
112     Emitted when C<$nick> enters the channel C<$channel> by JOINing.
113     C<$is_myself> is true if youself are the one who JOINs.
114    
115     =item B<part $nick $channel $is_myself $msg>
116    
117     Emitted when C<$nick> PARTs the channel C<$channel>.
118     C<$is_myself> is true if youself are the one who PARTs.
119     C<$msg> is the PART message.
120 elmex 1.1
121 elmex 1.13 =item B<nick_change $old_nick $new_nick $is_myself>
122 elmex 1.12
123     Emitted when C<$old_nick> is renamed to C<$new_nick>.
124     C<$is_myself> is true when youself was the one who changed the nick.
125    
126 elmex 1.25 =item B<ctcp $src, $target, $tag, $msg, $type>
127    
128     Emitted when a CTCP message was found in either a NOTICE or PRIVMSG
129     message. C<$tag> is the CTCP message tag. (eg. "PING", "VERSION", ...).
130     C<$msg> is the CTCP message and C<$type> is either "NOTICE" or "PRIVMSG".
131    
132     C<$src> is the source nick the message came from.
133     C<$target> is the target nickname (yours) or the channel the ctcp was sent
134     on.
135    
136     =item B<"ctcp_$tag", $src, $target, $msg, $type>
137    
138     Emitted when a CTCP message was found in either a NOTICE or PRIVMSG
139 elmex 1.26 message. C<$tag> is the CTCP message tag (in lower case). (eg. "ping", "version", ...).
140 elmex 1.25 C<$msg> is the CTCP message and C<$type> is either "NOTICE" or "PRIVMSG".
141    
142     C<$src> is the source nick the message came from.
143     C<$target> is the target nickname (yours) or the channel the ctcp was sent
144     on.
145    
146 elmex 1.3 =item B<quit $nick $msg>
147    
148     Emitted when the nickname C<$nick> QUITs with the message C<$msg>.
149    
150     =item B<publicmsg $channel $ircmsg>
151    
152     Emitted for NOTICE and PRIVMSG where the target C<$channel> is a channel.
153     C<$ircmsg> is the original IRC message hash like it is returned by C<parse_irc_msg>.
154    
155 elmex 1.25 The trailing part of the C<$ircmsg> will have all CTCP messages stripped off.
156    
157 elmex 1.3 =item B<privatemsg $nick $ircmsg>
158    
159     Emitted for NOTICE and PRIVMSG where the target C<$nick> (most of the time you) is a nick.
160     C<$ircmsg> is the original IRC message hash like it is returned by C<parse_irc_msg>.
161    
162 elmex 1.25 The trailing part of the C<$ircmsg> will have all CTCP messages stripped off.
163    
164 elmex 1.21 =item B<error $code $message $ircmsg>
165 elmex 1.13
166     Emitted when any error occurs. C<$code> is the 3 digit error id string from RFC
167 elmex 1.21 2812 and C<$message> is a description of the error. C<$ircmsg> is the complete
168     error irc message.
169 elmex 1.13
170     You may use Net::IRC3::Util::rfc_code_to_name to convert C<$code> to the error
171     name from the RFC 2812. eg.:
172    
173     rfc_code_to_name ('471') => 'ERR_CHANNELISFULL'
174    
175 elmex 1.2 =back
176    
177     =head1 METHODS
178    
179     =over 4
180 elmex 1.1
181 elmex 1.16 =item B<new>
182    
183     This constructor takes no arguments.
184    
185 elmex 1.1 =cut
186    
187 elmex 1.2 sub new {
188     my $this = shift;
189     my $class = ref($this) || $this;
190     my $self = $class->SUPER::new (@_);
191 elmex 1.1 $self->reg_cb (irc_001 => \&welcome_cb);
192     $self->reg_cb (irc_join => \&join_cb);
193 elmex 1.12 $self->reg_cb (irc_nick => \&nick_cb);
194 elmex 1.1 $self->reg_cb (irc_part => \&part_cb);
195 elmex 1.2 $self->reg_cb (irc_kick => \&kick_cb);
196     $self->reg_cb (irc_quit => \&quit_cb);
197 elmex 1.1 $self->reg_cb (irc_353 => \&namereply_cb);
198     $self->reg_cb (irc_366 => \&endofnames_cb);
199     $self->reg_cb (irc_ping => \&ping_cb);
200 elmex 1.27 $self->reg_cb (irc_pong => \&pong_cb);
201 elmex 1.1
202     $self->reg_cb (irc_privmsg => \&privmsg_cb);
203     $self->reg_cb (irc_notice => \&privmsg_cb);
204    
205     $self->reg_cb ('irc_*' => \&debug_cb)
206     if $DEBUG;
207     $self->reg_cb ('irc_*' => \&anymsg_cb);
208    
209 elmex 1.16 $self->reg_cb (channel_remove => \&channel_remove_event_cb);
210     $self->reg_cb (channel_add => \&channel_add_event_cb);
211     $self->reg_cb (disconnect => \&disconnect_cb);
212 elmex 1.2
213 elmex 1.16 $self->reg_cb (irc_437 => \&change_nick_login_cb);
214     $self->reg_cb (irc_433 => \&change_nick_login_cb);
215 elmex 1.13
216 elmex 1.23 $self->reg_cb (irc_332 => \&rpl_topic_cb);
217     $self->reg_cb (irc_topic => \&topic_change_cb);
218    
219 elmex 1.17 $self->{def_nick_change} = $self->{nick_change} =
220     sub {
221     my ($old_nick) = @_;
222     "${old_nick}_"
223     };
224 elmex 1.13
225 elmex 1.2 return $self;
226     }
227    
228 elmex 1.24 =item B<register ($nick, $user, $real, $server_pass)>
229 elmex 1.2
230     Sends the IRC registration commands NICK and USER.
231 elmex 1.24 If C<$server_pass> is passed also a PASS command is generated.
232 elmex 1.2
233     =cut
234    
235     sub register {
236 elmex 1.24 my ($self, $nick, $user, $real, $pass) = @_;
237 elmex 1.2
238 elmex 1.13 $self->{nick} = $nick;
239     $self->{user} = $user;
240     $self->{real} = $real;
241 elmex 1.24 $self->{server_pass} = $pass;
242 elmex 1.13
243 elmex 1.24 $self->send_msg (undef, "PASS", undef, $pass) if defined $pass;
244 elmex 1.1 $self->send_msg (undef, "NICK", undef, $nick);
245     $self->send_msg (undef, "USER", $real || $nick, $user || $nick, "*", "0");
246 elmex 1.13 }
247    
248     =item B<set_nick_change_cb $callback>
249    
250     This method lets you modify the nickname renaming mechanism when registering
251     the connection. C<$callback> is called with the current nickname as first
252     argument when a ERR_NICKNAMEINUSE or ERR_UNAVAILRESOURCE error occurs on login.
253     The returnvalue of C<$callback> will then be used to change the nickname.
254    
255 elmex 1.17 If C<$callback> is not defined the default nick change callback will be used
256     again.
257    
258 elmex 1.13 The default callback appends '_' to the end of the nickname supplied in the
259     C<register> routine.
260    
261     If the callback returns the same nickname that was given it the connection
262     will be terminated.
263 elmex 1.1
264 elmex 1.13 =cut
265    
266     sub set_nick_change_cb {
267     my ($self, $cb) = @_;
268 elmex 1.17 $cb = $self->{def_nick_change} unless defined $cb;
269 elmex 1.13 $self->{nick_change} = $cb;
270 elmex 1.1 }
271    
272     =item B<nick ()>
273    
274     Returns the current nickname, under which this connection
275     is registered at the IRC server. It might be different from the
276     one that was passed to C<register> as a nick-collision might happened
277     on login.
278    
279     =cut
280    
281     sub nick { $_[0]->{nick} }
282    
283 elmex 1.17 =item B<registered ()>
284 elmex 1.13
285     Returns a true value when the connection has been registered successfull and
286     you can send commands.
287    
288     =cut
289    
290 elmex 1.17 sub registered { $_[0]->{registered} }
291 elmex 1.13
292 elmex 1.1 =item B<channel_list ()>
293    
294 elmex 1.13 This returns a hash reference. The keys are the currently joined channels in lower case.
295     The values are hash references which contain the joined nicks as key.
296    
297     NOTE: Future versions might preserve the case from the JOIN command to the channels.
298 elmex 1.1
299     =cut
300    
301     sub channel_list {
302     my ($self) = @_;
303 elmex 1.12 return $self->{channel_list} || {};
304 elmex 1.1 }
305    
306 elmex 1.5 =item B<send_srv ($command, $trailing, @params)>
307 elmex 1.1
308 elmex 1.5 This function sends an IRC message that is constructed by C<mk_msg (undef, $command, $trailing, @params)> (see L<Net::IRC3::Util>).
309 elmex 1.1 If the connection isn't yet registered (for example if the connection is slow) and hasn't got a
310     welcome (IRC command 001) from the server yet, the IRC message is queued until it gets a welcome.
311    
312     =cut
313    
314 elmex 1.27 sub send_msg {
315     my ($self, @a) = @_;
316     if ($DEBUG) {
317     print "<< [@a]\n";
318     }
319     $self->SUPER::send_msg (@a);
320     }
321    
322 elmex 1.1 sub send_srv {
323     my ($self, @msg) = @_;
324    
325 elmex 1.17 if ($self->registered) {
326 elmex 1.1 $self->send_msg (undef, @msg);
327    
328     } else {
329     push @{$self->{con_queue}}, \@msg;
330     }
331     }
332    
333     =item B<clear_srv_queue>
334    
335     Clears the server send queue.
336    
337     =cut
338    
339     sub clear_srv_queue {
340     my ($self) = @_;
341     $self->{con_queue} = [];
342     }
343    
344    
345 elmex 1.5 =item B<send_chan ($channel, $command, $trailing, @params))>
346    
347 elmex 1.13 This function sends a message (constructed by C<mk_msg (undef, $command,
348     $trailing, @params)> to the server, like C<send_srv> only that it will queue
349     the messages if it hasn't joined the channel C<$channel> yet. The queued
350     messages will be send once the connection successfully JOINed the C<$channel>.
351 elmex 1.1
352 elmex 1.12 C<$channel> will be lowercased so that any case that comes from the server matches.
353     (Yes, IRC handles upper and lower case as equal :-(
354    
355     Be careful with this, there are chances you might not join the channel you
356     wanted to join. You may wanted to join #bla and the server redirects that
357     and sends you that you joined #blubb. You may use C<clear_chan_queue> to
358     remove the queue after some timeout after joining, so that you don't end up
359     with a memory leak.
360    
361 elmex 1.1 =cut
362    
363     sub send_chan {
364     my ($self, $chan, @msg) = @_;
365    
366 elmex 1.14 if ($self->{channel_list}->{lc $chan}) {
367 elmex 1.1 $self->send_msg (undef, @msg);
368    
369     } else {
370     push @{$self->{chan_queue}->{lc $chan}}, \@msg;
371     }
372     }
373    
374     =item B<clear_chan_queue ($channel)>
375    
376     Clears the channel queue of the channel C<$channel>.
377    
378     =cut
379    
380     sub clear_chan_queue {
381     my ($self, $chan) = @_;
382     $self->{chan_queue}->{lc $chan} = [];
383     }
384    
385 elmex 1.27 =item B<enable_ping ($interval, $cb)>
386    
387     This method enables a periodical ping to the server with an interval of
388     C<$interval> seconds. If no PONG was received from the server until the next
389     interval the connection will be terminated or the callback in C<$cb> will be called.
390    
391     (C<$cb> will have the connection object as it's first argument.)
392    
393     Make sure you call this method after the connection has been established.
394    
395     =cut
396    
397     sub enable_ping {
398     my ($self, $int, $cb) = @_;
399    
400     $self->{last_pong_recv} = 0;
401     $self->{last_ping_sent} = time;
402    
403     $self->send_srv (PING => "Net::IRC3");
404    
405     $self->{_ping_timer} =
406     AnyEvent->timer (after => $int, cb => sub {
407     if ($self->{last_pong_recv} < $self->{last_ping_sent}) {
408     delete $self->{_ping_timer};
409     if ($cb) {
410     $cb->($self);
411     } else {
412     $self->disconnect ("Server timeout");
413     }
414    
415     } else {
416     $self->enable_ping ($int, $cb);
417     }
418     });
419     }
420    
421 elmex 1.1 ################################################################################
422 elmex 1.2 # Private utility functions
423     ################################################################################
424    
425     sub _was_me {
426     my ($self, $msg) = @_;
427 elmex 1.4 lc prefix_nick ($msg) eq lc $self->nick ()
428 elmex 1.2 }
429    
430     ################################################################################
431 elmex 1.1 # Callbacks
432     ################################################################################
433    
434 elmex 1.16 sub channel_remove_event_cb {
435 elmex 1.2 my ($self, $chan, @nicks) = @_;
436    
437     for my $nick (@nicks) {
438 elmex 1.12 if (lc ($nick) eq lc ($self->nick ())) {
439     delete $self->{chan_queue}->{lc $chan};
440 elmex 1.13 delete $self->{channel_list}->{lc $chan};
441 elmex 1.2 last;
442     } else {
443 elmex 1.13 delete $self->{channel_list}->{lc $chan}->{$nick};
444 elmex 1.2 }
445     }
446    
447     1;
448     }
449    
450 elmex 1.16 sub channel_add_event_cb {
451 elmex 1.2 my ($self, $chan, @nicks) = @_;
452    
453     for my $nick (@nicks) {
454 elmex 1.12 if (lc ($nick) eq lc ($self->nick ())) {
455     for (@{$self->{chan_queue}->{lc $chan}}) {
456 elmex 1.2 $self->send_msg (undef, @$_);
457     }
458 elmex 1.11 $self->clear_chan_queue ($chan);
459 elmex 1.2 }
460    
461 elmex 1.13 $self->{channel_list}->{lc $chan}->{$nick} = 1;
462 elmex 1.2 }
463    
464     1;
465     }
466    
467 elmex 1.19 sub _filter_new_nicks_from_channel {
468     my ($self, $chan, @nicks) = @_;
469     grep { not exists $self->{channel_list}->{lc $chan}->{$_} } @nicks;
470     }
471    
472 elmex 1.1 sub anymsg_cb {
473     my ($self, $msg) = @_;
474    
475     my $cmd = lc $msg->{command};
476    
477 elmex 1.13 if ( $cmd ne "privmsg"
478 elmex 1.1 and $cmd ne "notice"
479     and $cmd ne "part"
480     and $cmd ne "join"
481 elmex 1.13 and not ($cmd >= 400 and $cmd <= 599)
482     )
483 elmex 1.1 {
484     $self->event (statmsg => $msg);
485 elmex 1.13 } elsif ($cmd >= 400 and $cmd <= 599) {
486 elmex 1.21 $self->event (error => $msg->{command}, $msg->{trailing}, $msg);
487 elmex 1.1 }
488    
489     1;
490     }
491    
492     sub privmsg_cb {
493     my ($self, $msg) = @_;
494    
495 elmex 1.25 my ($trail, $ctcp) = decode_ctcp ($msg->{trailing});
496    
497     for (@$ctcp) {
498     $self->event (ctcp => prefix_nick ($msg), $msg->{params}->[0], $_->[0], $_->[1], $msg->{command});
499     $self->event ("ctcp_$_->[0]", prefix_nick ($msg), $msg->{params}->[0], $_->[1], $msg->{command});
500     }
501    
502     $msg->{trailing} = $trail;
503 elmex 1.1
504 elmex 1.25 if ($msg->{trailing} ne '') {
505     my $targ = $msg->{params}->[0];
506     if ($targ =~ m/^(?:[#+&]|![A-Z0-9]{5})/) {
507     $self->event (publicmsg => $targ, $msg);
508    
509     } else {
510     $self->event (privatemsg => $targ, $msg);
511     }
512 elmex 1.1 }
513    
514     1;
515     }
516    
517     sub welcome_cb {
518     my ($self, $msg) = @_;
519    
520 elmex 1.17 $self->{registered} = 1;
521 elmex 1.1
522     for (@{$self->{con_queue}}) {
523     $self->send_msg (undef, @$_);
524     }
525 elmex 1.11 $self->clear_srv_queue ();
526 elmex 1.1
527 elmex 1.12 $self->event ('registered');
528    
529 elmex 1.1 1;
530     }
531    
532     sub ping_cb {
533     my ($self, $msg) = @_;
534 elmex 1.8 $self->send_msg (undef, "PONG", $msg->{params}->[0]);
535 elmex 1.2
536     1;
537 elmex 1.1 }
538    
539 elmex 1.27 sub pong_cb {
540     my ($self, $msg) = @_;
541     $self->{last_pong_recv} = time;
542     1;
543     }
544    
545 elmex 1.12 sub nick_cb {
546     my ($self, $msg) = @_;
547     my $nick = prefix_nick ($msg);
548     my $newnick = $msg->{params}->[0];
549     my $wasme = $self->_was_me ($msg);
550    
551 elmex 1.13 if ($wasme) { $self->{nick} = $newnick }
552 elmex 1.12
553 elmex 1.13 my @chans;
554 elmex 1.12
555 elmex 1.13 for my $channame (keys %{$self->{channel_list}}) {
556     my $chan = $self->{channel_list}->{$channame};
557 elmex 1.12 if (exists $chan->{$nick}) {
558     delete $chan->{$nick};
559     $chan->{$newnick} = 1;
560 elmex 1.23
561 elmex 1.13 push @chans, $channame;
562 elmex 1.12 }
563     }
564    
565 elmex 1.13 for (@chans) {
566     $self->event (channel_change => $_, $nick, $newnick, $wasme);
567     }
568 elmex 1.12 $self->event (nick_change => $nick, $newnick, $wasme);
569 elmex 1.23
570     1;
571 elmex 1.12 }
572    
573 elmex 1.1 sub namereply_cb {
574     my ($self, $msg) = @_;
575 elmex 1.2 my @nicks = split / /, $msg->{trailing};
576     push @{$self->{_tmp_namereply}}, @nicks;
577 elmex 1.10
578     1;
579 elmex 1.1 }
580    
581     sub endofnames_cb {
582     my ($self, $msg) = @_;
583 elmex 1.12 my $chan = $msg->{params}->[1];
584 elmex 1.20 my @nicks =
585 elmex 1.19 $self->_filter_new_nicks_from_channel (
586 elmex 1.20 $chan, map { s/^[@\+%&]//; $_ } @{delete $self->{_tmp_namereply}}
587     );
588    
589     $self->event (channel_add => $chan, @nicks) if @nicks;
590 elmex 1.10
591     1;
592 elmex 1.1 }
593    
594     sub join_cb {
595     my ($self, $msg) = @_;
596 elmex 1.2 my $chan = $msg->{params}->[0];
597 elmex 1.4 my $nick = prefix_nick ($msg);
598 elmex 1.1
599 elmex 1.12 $self->event (channel_add => $chan, $nick);
600 elmex 1.2 $self->event (join => $nick, $chan, $self->_was_me ($msg));
601 elmex 1.1
602 elmex 1.2 1;
603     }
604    
605     sub part_cb {
606     my ($self, $msg) = @_;
607     my $chan = $msg->{params}->[0];
608 elmex 1.4 my $nick = prefix_nick ($msg);
609 elmex 1.1
610 elmex 1.2 $self->event (part => $nick, $chan, $self->_was_me ($msg), $msg->{params}->[1]);
611     $self->event (channel_remove => $chan, $nick);
612 elmex 1.1
613 elmex 1.2 1;
614     }
615 elmex 1.1
616 elmex 1.2 sub kick_cb {
617     my ($self, $msg) = @_;
618     my $chan = $msg->{params}->[0];
619     my $kicked_nick = $msg->{params}->[1];
620 elmex 1.1
621 elmex 1.2 $self->event (channel_remove => $chan, $kicked_nick);
622 elmex 1.1
623     1;
624     }
625    
626 elmex 1.2 sub quit_cb {
627 elmex 1.1 my ($self, $msg) = @_;
628 elmex 1.4 my $nick = prefix_nick ($msg);
629 elmex 1.1
630 elmex 1.2 $self->event (quit => $nick, $msg->{params}->[1]);
631 elmex 1.1
632 elmex 1.12 for (keys %{$self->{channel_list}}) {
633 elmex 1.14 $self->event (channel_remove => $_, $nick)
634     if $self->{channel_list}->{$_}->{$nick};
635 elmex 1.1 }
636 elmex 1.10
637     1;
638 elmex 1.1 }
639    
640     sub debug_cb {
641     my ($self, $msg) = @_;
642     print "$self->{h}:$self->{p} > ";
643 elmex 1.22 print (join " ", map { $_ => $msg->{$_} } grep { $_ ne 'params' } sort keys %$msg);
644 elmex 1.1 print " params:";
645 elmex 1.21 print (join ",", @{$msg->{params}});
646 elmex 1.1 print "\n";
647 elmex 1.10
648     1;
649 elmex 1.1 }
650    
651 elmex 1.16 sub change_nick_login_cb {
652 elmex 1.13 my ($self, $msg) = @_;
653    
654 elmex 1.17 unless ($self->registered) {
655 elmex 1.13 my $newnick = $self->{nick_change}->($self->nick);
656    
657     if (lc $newnick eq lc $self->{nick}) {
658     $self->disconnect;
659     return 0;
660     }
661    
662     $self->{nick} = $newnick;
663     $self->send_msg (undef, "NICK", undef, $newnick);
664     }
665    
666 elmex 1.17 not ($self->registered) # kill the cb when registered
667 elmex 1.13 }
668    
669 elmex 1.15 sub disconnect_cb {
670     my ($self) = @_;
671    
672     for (keys %{$self->{channel_list}}) {
673     $self->event (channel_remove => $_, $self->nick)
674     }
675    
676     1
677     }
678    
679 elmex 1.23 sub rpl_topic_cb {
680     my ($self, $msg) = @_;
681     my $chan = $msg->{params}->[1];
682     my $topic = $msg->{trailing};
683    
684     $self->event (channel_topic => $chan, $topic);
685    
686     1
687     }
688    
689     sub topic_change_cb {
690     my ($self, $msg) = @_;
691     my $who = prefix_nick ($msg);
692     my $chan = $msg->{params}->[0];
693     my $topic = $msg->{trailing};
694    
695     $self->event (channel_topic => $chan, $topic, $who);
696    
697     1
698     }
699    
700 elmex 1.1 =back
701    
702     =head1 EXAMPLES
703    
704     See samples/netirc3cl and other samples in samples/ for some examples on how to use Net::IRC3::Client::Connection.
705    
706     =head1 AUTHOR
707    
708     Robin Redeker, C<< <elmex@ta-sa.org> >>
709    
710     =head1 SEE ALSO
711    
712 elmex 1.7 L<Net::IRC3::Connection>
713 elmex 1.1
714     RFC 2812 - Internet Relay Chat: Client Protocol
715    
716     =head1 COPYRIGHT & LICENSE
717    
718 elmex 1.6 Copyright 2006 Robin Redeker, all rights reserved.
719 elmex 1.1
720     This program is free software; you can redistribute it and/or modify it
721     under the same terms as Perl itself.
722    
723     =cut
724    
725     1;