Debian Codegraph @

samples/debug_console

raw
1 #!/usr/bin/env perl
2 # This is a simple script that connects stdin/stdout with a client
3 # connection to an irc server. the command line arguments are:
4 #
5 # $ ./debug_console <nick> <server> <port>
6 #
7 use common::sense;
8 use IO::Handle;
9 use AnyEvent;
10 use AnyEvent::IRC::Client;
11 use AnyEvent::IRC::Util qw/mk_msg parse_irc_msg encode_ctcp/;
12 use Data::Dumper;
13
14 my ($nick, $server, $port) = @ARGV;
15
16 my $c = AnyEvent->condvar;
17 my $stdout = AnyEvent::Handle->new (fh => \*STDOUT);
18 my $con = new AnyEvent::IRC::Client;
19
20 $con->reg_cb (
21 connect => sub {
22 my ($con, $err) = @_;
23
24 if (defined $err) {
25 warn "Couldn't connect: $err\n";
26 $c->broadcast;
27 } else {
28 $stdout->push_write ("Connected!\n");
29 }
30
31 $con->register ($nick, $nick, $nick);
32 },
33 debug_recv => sub {
34 my ($con, $msg) = @_;
35 $stdout->push_write (
36 "< "
37 . mk_msg ($msg->{prefix}, $msg->{command}, @{$msg->{params}})
38 . "\n"
39 );
40 },
41 debug_send => sub {
42 my ($con, @msg) = @_;
43 $stdout->push_write (
44 "> " . mk_msg (undef, @msg) . "\n"
45 );
46 },
47 registered => sub {
48 my ($con) = @_;
49
50 my $stdin;
51 $stdin = AnyEvent::Handle->new (
52 fh => \*STDIN,
53 on_eof => sub {
54 warn "EOF on STDIN, disconnecting...\n";
55 $con->disconnect ("Console EOF");
56 },
57 on_error => sub {
58 warn "Error on STDIN: $!\n";
59 },
60 on_read => sub {
61 $stdin->push_read (line => sub {
62 my ($stdin, $line) = @_;
63
64 if ($line =~ /^!/) {
65 my $r = eval $line;
66 if ($@) {
67 warn "eval error: $@\n";
68 } else {
69 $Data::Dumper::Terse = 1;
70 $stdout->push_write ("result: " . Data::Dumper::Dumper ($r));
71 }
72 } else {
73 my $msg = parse_irc_msg ($line);
74 $con->send_msg ($msg->{command}, @{$msg->{params}});
75 }
76 });
77 }
78 );
79 },
80 disconnect => sub {
81 warn "disconnect: $_[1]!\n";
82 $c->broadcast
83 },
84 );
85
86 $con->ctcp_auto_reply ('VERSION', ['VERSION', 'DebugConsole:0.1:Perl']);
87
88 $con->connect ($server, $port || 6667);
89
90 $c->wait;