move all configuration into enviroment variables
[MQR.git] / scripts / mqr-irc-client.pl
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 AnyEvent::Redis;
13 use Data::Dump qw(dump);
14
15 my $nick = $ENV{IRC_NICK} || die "IRC_NICK";
16
17 my $pub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT} );
18 my $sub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT} );
19
20 my $c = AnyEvent->condvar;
21 my $stdout = AnyEvent::Handle->new (fh => \*STDOUT);
22 my $con = new AnyEvent::IRC::Client;
23
24 $con->reg_cb (
25    connect => sub {
26       my ($con, $err) = @_;
27
28       if (defined $err) {
29          warn "Couldn't connect: $err\n";
30          $c->broadcast;
31       } else {
32          $stdout->push_write ("Connected!\n");
33       }
34
35       $con->register ($nick, $nick, $nick);
36    },
37    debug_recv => sub {
38       my ($con, $msg) = @_;
39       $stdout->push_write (
40          "< "
41          . mk_msg ($msg->{prefix}, $msg->{command}, @{$msg->{params}})
42          . "\n"
43       );
44
45                 if ( $msg->{command} eq 'PRIVMSG' ) {
46                         my $channel = join(' ', 'channel', $msg->{params}->[0], $msg->{prefix});
47                         $pub->publish( $channel, $msg->{params}->[1] );
48                 }
49    },
50    debug_send => sub {
51       my ($con, @msg) = @_;
52       $stdout->push_write (
53          "> " . mk_msg (undef, @msg) . "\n"
54       );
55 warn dump(@msg);
56    },
57    registered => sub {
58       my ($con) = @_;
59
60       my $stdin;
61       $stdin = AnyEvent::Handle->new (
62          fh => \*STDIN,
63          on_eof => sub {
64             warn "EOF on STDIN, disconnecting...\n";
65             $con->disconnect ("Console EOF");
66          },
67          on_error => sub {
68             warn "Error on STDIN: $!\n";
69          },
70          on_read => sub {
71             $stdin->push_read (line => sub {
72                my ($stdin, $line) = @_;
73
74                if ($line =~ /^!/) {
75                   my $r = eval $line;
76                   if ($@) {
77                      warn "eval error: $@\n";
78                   } else {
79                      $Data::Dumper::Terse = 1;
80                      $stdout->push_write ("result: " . dump($r));
81                   }
82                } else {
83                   my $msg = parse_irc_msg ($line);
84                   $con->send_msg ($msg->{command}, @{$msg->{params}});
85                }
86             });
87          }
88       );
89    },
90    disconnect => sub {
91       warn "disconnect: $_[1]!\n";
92       $c->broadcast
93    },
94 );
95
96 $con->ctcp_auto_reply('VERSION', ['VERSION', 'MQR-IRC:0.1:Perl']);
97
98 $con->connect ($ENV{IRC_SERVER}, $ENV{IRC_PORT} || 6667);
99
100 $c->wait;