oz (owner)

Revisions

  • d2ca68 oz Wed Jun 17 15:36:53 -0700 2009
gist: 131558 Download_button fork
public
Description:
Quick irc bot that pipes everything it receives on a TCP port to IRC.
Public Clone URL: git://gist.github.com/131558.git
blop.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env perl
 
use strict;
use warnings;
use POE qw< Component::Server::TCP Component::IRC >;
 
# irc config
my @channels = ("#test");
my $irc_nick = 'blop';
my $irc_name = 'blop the bot';
my $irc_server = 'irc.oftc.net';
 
# tcp server config
my @listens = (
    { addr => "0.0.0.0", port => 1983 },
);
 
# IRC Client
# ----------
 
# We create a new PoCo-IRC object
my $irc = POE::Component::IRC->spawn(
    nick => $irc_nick,
    ircname => $irc_name,
    server => $irc_server,
) or die "Oh noooo! $!";
 
POE::Session->create(
    package_states => [
        main => [ qw(_start irc_001) ],
    ],
    heap => { irc => $irc },
);
 
sub _start {
    my $heap = $_[HEAP];
 
    # retrieve our component's object from the heap where we stashed it
    my $irc = $heap->{irc};
 
    $irc->yield( register => 'all' );
    $irc->yield( connect => { } );
    return;
}
 
sub irc_001 {
    my $sender = $_[SENDER];
 
    # Since this is an irc_* event, we can get the component's object by
    # accessing the heap of the sender. Then we register and connect to the
    # specified server.
    my $irc = $sender->get_heap();
 
    print "Connected to ", $irc->server_name(), "\n";
 
    # we join our channels
    $irc->yield( join => $_ ) for @channels;
    return;
}
 
# TCP SERVER
# ----------
 
for my $listen (@listens) {
    POE::Component::Server::TCP->new(
        Address => $listen->{addr},
        Port => $listen->{port},
        ClientInput => sub {
            my ($kernel, $input) = @_[ KERNEL, ARG0 ];
 
            $_[HEAP]{client}->put($input);
            for my $channel (@channels) {
                $irc->yield( privmsg => $channel => $input );
            }
        },
    );
}
 
POE::Kernel->run;