ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Net-IRC3/samples/netirc3
Revision: 1.4
Committed: Sat Feb 17 13:01:38 2007 UTC (19 years, 7 months ago) by elmex
Branch: MAIN
CVS Tags: HEAD
Changes since 1.3: +14 -4 lines
Log Message:
removed json examples,
fixed a few minor bugs and added connect/connect_error events
with improved network code.

File Contents

# User Rev Content
1 elmex 1.4 #!/opt/perl/bin/perl
2 elmex 1.1 use strict;
3 elmex 1.4 use AnyEvent::Impl::Perl;
4     use AnyEvent;
5 elmex 1.3 use Net::IRC3::Connection;
6 elmex 1.1 $Net::IRC3::Client::DEBUG = 1;
7    
8     my $c = AnyEvent->condvar;
9    
10 elmex 1.3 my $con = Net::IRC3::Connection->new;
11 elmex 1.1
12    
13     my ($nick, $user, $real) = qw/BinDepp BinDepp depp/;
14    
15     # send IRC registration
16     $con->send_msg (undef, "NICK", undef, $nick);
17     $con->send_msg (undef, "USER", $real || $nick, $user || $nick, "*", "0");
18    
19     $con->reg_cb (irc_001 => sub {
20     my ($con) = @_;
21     $con->event ('welcome'); # emit a self defined event
22     1;
23     });
24    
25     # display all irc messages for debugging
26     $con->reg_cb ('irc_*' => sub { warn "DEBUG: " . join ('|', %{$_[1]}) . "\n"; 1; });
27    
28     # we register now a callback on our self defined event
29     $con->reg_cb (welcome => sub {
30     my ($con) = @_;
31 elmex 1.2 $con->send_msg (undef, "PRIVMSG", "Hi!!!", "elmex");
32 elmex 1.1 1;
33     });
34    
35     my $t;
36 elmex 1.4 $t = AnyEvent->timer (after => 10, cb => sub {
37 elmex 1.2 $con->disconnect ("Timeout exceeded");
38 elmex 1.1 undef $t;
39     });
40    
41     # lets
42     $con->reg_cb (
43 elmex 1.4 connect => sub {
44     warn "Connected! Yay!\n";
45     1;
46     },
47     connect_error => sub {
48     warn "Connection error: $_[1]\n";
49     1;
50     },
51 elmex 1.1 disconnect => sub {
52 elmex 1.2 warn "Oh, got a disconnect: $_[1], exiting...\n";
53 elmex 1.1 1;
54     }
55     );
56    
57 elmex 1.4 $con->connect ("localhost", 6667);
58    
59 elmex 1.1 $c->wait;