ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Net-IRC3/lib/Net/IRC3/Client/Connection.pm
Revision: 1.33
Committed: Tue Sep 23 10:33:00 2008 UTC (18 years, 1 month ago) by elmex
Branch: MAIN
CVS Tags: HEAD
Changes since 1.32: +5 -0 lines
Log Message:
last release: 0.6

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