ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/Net-IRC3/lib/Net/IRC3/Client/Connection.pm
Revision: 1.28
Committed: Mon Jun 25 15:11:52 2007 UTC (19 years, 4 months ago) by elmex
Branch: MAIN
Changes since 1.27: +2 -1 lines
Log Message:
added a prefix for nicknames that i missed ;-/

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 elmex 1.28 (eg. in the callback for the C<registered> event).
395 elmex 1.27
396     =cut
397    
398     sub enable_ping {
399     my ($self, $int, $cb) = @_;
400    
401     $self->{last_pong_recv} = 0;
402     $self->{last_ping_sent} = time;
403    
404     $self->send_srv (PING => "Net::IRC3");
405    
406     $self->{_ping_timer} =
407     AnyEvent->timer (after => $int, cb => sub {
408     if ($self->{last_pong_recv} < $self->{last_ping_sent}) {
409     delete $self->{_ping_timer};
410     if ($cb) {
411     $cb->($self);
412     } else {
413     $self->disconnect ("Server timeout");
414     }
415    
416     } else {
417     $self->enable_ping ($int, $cb);
418     }
419     });
420     }
421    
422 elmex 1.1 ################################################################################
423 elmex 1.2 # Private utility functions
424     ################################################################################
425    
426     sub _was_me {
427     my ($self, $msg) = @_;
428 elmex 1.4 lc prefix_nick ($msg) eq lc $self->nick ()
429 elmex 1.2 }
430    
431     ################################################################################
432 elmex 1.1 # Callbacks
433     ################################################################################
434    
435 elmex 1.16 sub channel_remove_event_cb {
436 elmex 1.2 my ($self, $chan, @nicks) = @_;
437    
438     for my $nick (@nicks) {
439 elmex 1.12 if (lc ($nick) eq lc ($self->nick ())) {
440     delete $self->{chan_queue}->{lc $chan};
441 elmex 1.13 delete $self->{channel_list}->{lc $chan};
442 elmex 1.2 last;
443     } else {
444 elmex 1.13 delete $self->{channel_list}->{lc $chan}->{$nick};
445 elmex 1.2 }
446     }
447    
448     1;
449     }
450    
451 elmex 1.16 sub channel_add_event_cb {
452 elmex 1.2 my ($self, $chan, @nicks) = @_;
453    
454     for my $nick (@nicks) {
455 elmex 1.12 if (lc ($nick) eq lc ($self->nick ())) {
456     for (@{$self->{chan_queue}->{lc $chan}}) {
457 elmex 1.2 $self->send_msg (undef, @$_);
458     }
459 elmex 1.11 $self->clear_chan_queue ($chan);
460 elmex 1.2 }
461    
462 elmex 1.13 $self->{channel_list}->{lc $chan}->{$nick} = 1;
463 elmex 1.2 }
464    
465     1;
466     }
467    
468 elmex 1.19 sub _filter_new_nicks_from_channel {
469     my ($self, $chan, @nicks) = @_;
470     grep { not exists $self->{channel_list}->{lc $chan}->{$_} } @nicks;
471     }
472    
473 elmex 1.1 sub anymsg_cb {
474     my ($self, $msg) = @_;
475    
476     my $cmd = lc $msg->{command};
477    
478 elmex 1.13 if ( $cmd ne "privmsg"
479 elmex 1.1 and $cmd ne "notice"
480     and $cmd ne "part"
481     and $cmd ne "join"
482 elmex 1.13 and not ($cmd >= 400 and $cmd <= 599)
483     )
484 elmex 1.1 {
485     $self->event (statmsg => $msg);
486 elmex 1.13 } elsif ($cmd >= 400 and $cmd <= 599) {
487 elmex 1.21 $self->event (error => $msg->{command}, $msg->{trailing}, $msg);
488 elmex 1.1 }
489    
490     1;
491     }
492    
493     sub privmsg_cb {
494     my ($self, $msg) = @_;
495    
496 elmex 1.25 my ($trail, $ctcp) = decode_ctcp ($msg->{trailing});
497    
498     for (@$ctcp) {
499     $self->event (ctcp => prefix_nick ($msg), $msg->{params}->[0], $_->[0], $_->[1], $msg->{command});
500     $self->event ("ctcp_$_->[0]", prefix_nick ($msg), $msg->{params}->[0], $_->[1], $msg->{command});
501     }
502    
503     $msg->{trailing} = $trail;
504 elmex 1.1
505 elmex 1.25 if ($msg->{trailing} ne '') {
506     my $targ = $msg->{params}->[0];
507     if ($targ =~ m/^(?:[#+&]|![A-Z0-9]{5})/) {
508     $self->event (publicmsg => $targ, $msg);
509    
510     } else {
511     $self->event (privatemsg => $targ, $msg);
512     }
513 elmex 1.1 }
514    
515     1;
516     }
517    
518     sub welcome_cb {
519     my ($self, $msg) = @_;
520    
521 elmex 1.17 $self->{registered} = 1;
522 elmex 1.1
523     for (@{$self->{con_queue}}) {
524     $self->send_msg (undef, @$_);
525     }
526 elmex 1.11 $self->clear_srv_queue ();
527 elmex 1.1
528 elmex 1.12 $self->event ('registered');
529    
530 elmex 1.1 1;
531     }
532    
533     sub ping_cb {
534     my ($self, $msg) = @_;
535 elmex 1.8 $self->send_msg (undef, "PONG", $msg->{params}->[0]);
536 elmex 1.2
537     1;
538 elmex 1.1 }
539    
540 elmex 1.27 sub pong_cb {
541     my ($self, $msg) = @_;
542     $self->{last_pong_recv} = time;
543     1;
544     }
545    
546 elmex 1.12 sub nick_cb {
547     my ($self, $msg) = @_;
548     my $nick = prefix_nick ($msg);
549     my $newnick = $msg->{params}->[0];
550     my $wasme = $self->_was_me ($msg);
551    
552 elmex 1.13 if ($wasme) { $self->{nick} = $newnick }
553 elmex 1.12
554 elmex 1.13 my @chans;
555 elmex 1.12
556 elmex 1.13 for my $channame (keys %{$self->{channel_list}}) {
557     my $chan = $self->{channel_list}->{$channame};
558 elmex 1.12 if (exists $chan->{$nick}) {
559     delete $chan->{$nick};
560     $chan->{$newnick} = 1;
561 elmex 1.23
562 elmex 1.13 push @chans, $channame;
563 elmex 1.12 }
564     }
565    
566 elmex 1.13 for (@chans) {
567     $self->event (channel_change => $_, $nick, $newnick, $wasme);
568     }
569 elmex 1.12 $self->event (nick_change => $nick, $newnick, $wasme);
570 elmex 1.23
571     1;
572 elmex 1.12 }
573    
574 elmex 1.1 sub namereply_cb {
575     my ($self, $msg) = @_;
576 elmex 1.2 my @nicks = split / /, $msg->{trailing};
577     push @{$self->{_tmp_namereply}}, @nicks;
578 elmex 1.10
579     1;
580 elmex 1.1 }
581    
582     sub endofnames_cb {
583     my ($self, $msg) = @_;
584 elmex 1.12 my $chan = $msg->{params}->[1];
585 elmex 1.20 my @nicks =
586 elmex 1.19 $self->_filter_new_nicks_from_channel (
587 elmex 1.28 $chan, map { s/^[~@\+%&]//; $_ } @{delete $self->{_tmp_namereply}}
588 elmex 1.20 );
589    
590     $self->event (channel_add => $chan, @nicks) if @nicks;
591 elmex 1.10
592     1;
593 elmex 1.1 }
594    
595     sub join_cb {
596     my ($self, $msg) = @_;
597 elmex 1.2 my $chan = $msg->{params}->[0];
598 elmex 1.4 my $nick = prefix_nick ($msg);
599 elmex 1.1
600 elmex 1.12 $self->event (channel_add => $chan, $nick);
601 elmex 1.2 $self->event (join => $nick, $chan, $self->_was_me ($msg));
602 elmex 1.1
603 elmex 1.2 1;
604     }
605    
606     sub part_cb {
607     my ($self, $msg) = @_;
608     my $chan = $msg->{params}->[0];
609 elmex 1.4 my $nick = prefix_nick ($msg);
610 elmex 1.1
611 elmex 1.2 $self->event (part => $nick, $chan, $self->_was_me ($msg), $msg->{params}->[1]);
612     $self->event (channel_remove => $chan, $nick);
613 elmex 1.1
614 elmex 1.2 1;
615     }
616 elmex 1.1
617 elmex 1.2 sub kick_cb {
618     my ($self, $msg) = @_;
619     my $chan = $msg->{params}->[0];
620     my $kicked_nick = $msg->{params}->[1];
621 elmex 1.1
622 elmex 1.2 $self->event (channel_remove => $chan, $kicked_nick);
623 elmex 1.1
624     1;
625     }
626    
627 elmex 1.2 sub quit_cb {
628 elmex 1.1 my ($self, $msg) = @_;
629 elmex 1.4 my $nick = prefix_nick ($msg);
630 elmex 1.1
631 elmex 1.2 $self->event (quit => $nick, $msg->{params}->[1]);
632 elmex 1.1
633 elmex 1.12 for (keys %{$self->{channel_list}}) {
634 elmex 1.14 $self->event (channel_remove => $_, $nick)
635     if $self->{channel_list}->{$_}->{$nick};
636 elmex 1.1 }
637 elmex 1.10
638     1;
639 elmex 1.1 }
640    
641     sub debug_cb {
642     my ($self, $msg) = @_;
643     print "$self->{h}:$self->{p} > ";
644 elmex 1.22 print (join " ", map { $_ => $msg->{$_} } grep { $_ ne 'params' } sort keys %$msg);
645 elmex 1.1 print " params:";
646 elmex 1.21 print (join ",", @{$msg->{params}});
647 elmex 1.1 print "\n";
648 elmex 1.10
649     1;
650 elmex 1.1 }
651    
652 elmex 1.16 sub change_nick_login_cb {
653 elmex 1.13 my ($self, $msg) = @_;
654    
655 elmex 1.17 unless ($self->registered) {
656 elmex 1.13 my $newnick = $self->{nick_change}->($self->nick);
657    
658     if (lc $newnick eq lc $self->{nick}) {
659     $self->disconnect;
660     return 0;
661     }
662    
663     $self->{nick} = $newnick;
664     $self->send_msg (undef, "NICK", undef, $newnick);
665     }
666    
667 elmex 1.17 not ($self->registered) # kill the cb when registered
668 elmex 1.13 }
669    
670 elmex 1.15 sub disconnect_cb {
671     my ($self) = @_;
672    
673     for (keys %{$self->{channel_list}}) {
674     $self->event (channel_remove => $_, $self->nick)
675     }
676    
677     1
678     }
679    
680 elmex 1.23 sub rpl_topic_cb {
681     my ($self, $msg) = @_;
682     my $chan = $msg->{params}->[1];
683     my $topic = $msg->{trailing};
684    
685     $self->event (channel_topic => $chan, $topic);
686    
687     1
688     }
689    
690     sub topic_change_cb {
691     my ($self, $msg) = @_;
692     my $who = prefix_nick ($msg);
693     my $chan = $msg->{params}->[0];
694     my $topic = $msg->{trailing};
695    
696     $self->event (channel_topic => $chan, $topic, $who);
697    
698     1
699     }
700    
701 elmex 1.1 =back
702    
703     =head1 EXAMPLES
704    
705     See samples/netirc3cl and other samples in samples/ for some examples on how to use Net::IRC3::Client::Connection.
706    
707     =head1 AUTHOR
708    
709     Robin Redeker, C<< <elmex@ta-sa.org> >>
710    
711     =head1 SEE ALSO
712    
713 elmex 1.7 L<Net::IRC3::Connection>
714 elmex 1.1
715     RFC 2812 - Internet Relay Chat: Client Protocol
716    
717     =head1 COPYRIGHT & LICENSE
718    
719 elmex 1.6 Copyright 2006 Robin Redeker, all rights reserved.
720 elmex 1.1
721     This program is free software; you can redistribute it and/or modify it
722     under the same terms as Perl itself.
723    
724     =cut
725    
726     1;