Created
June 17, 2009 22:36
-
-
Save oz/131558 to your computer and use it in GitHub Desktop.
Quick irc bot that pipes everything it receives on a TCP port to IRC.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment