Debian Codegraph @

samples/anyeventirc

raw
1 #!/usr/bin/env perl
2 use common::sense;
3 use AnyEvent::Impl::Perl;
4 use AnyEvent;
5 use AnyEvent::IRC::Connection;
6
7 my $c = AnyEvent->condvar;
8
9 my $con = AnyEvent::IRC::Connection->new;
10
11
12 my ($nick, $user, $real) = qw/BinDepp BinDepp depp/;
13
14 $con->reg_cb (irc_001 => sub {
15 my ($con) = @_;
16 $con->event ('welcome'); # emit a self defined event
17 });
18
19 # display all irc messages for debugging
20 $con->reg_cb ('irc_*' => sub {
21 my @p = @{delete $_[1]->{params} || []};
22 warn "DEBUG: " . join ('|', %{$_[1]}, @p) . "\n";
23 });
24 $con->reg_cb ('sent' => sub {
25 shift; warn "DEBUG SENT: " . join ('|', @_) . "\n";
26 });
27
28 # we register now a callback on our self defined event
29 $con->reg_cb (welcome => sub {
30 my ($con) = @_;
31 $con->send_msg ("PRIVMSG", "elmex", "Hi!!!");
32 });
33
34 # Disconnect after 10 seconds:
35 my $t;
36 $t = AnyEvent->timer (after => 10, cb => sub {
37 $con->disconnect ("Timeout exceeded");
38 undef $t;
39 });
40
41 # lets register connect and disconnect handlers.
42 $con->reg_cb (
43 connect => sub {
44 my ($con, $err) = @_;
45
46 if (defined $err) {
47 warn "Connect ERROR! => $err\n";
48 $c->broadcast;
49 } else {
50 warn "Connected! Yay!\n";
51 }
52
53 # send IRC registration
54 $con->send_msg ("NICK", $nick);
55 $con->send_msg ("USER", $user || $nick, "*", "0", $real || $nick);
56 },
57 disconnect => sub {
58 warn "Oh, got a disconnect: $_[1], exiting...\n";
59 $c->broadcast;
60 }
61 );
62
63 $con->connect ("localhost", 6667);
64
65 $c->wait;