This document describes using Plack and AnyEvent to create an IRC to HTTP stream server. It connects to an IRC server using AnyEvent::IRC::Client, registers a callback for IRC privmsgs, and runs a Plack server on port 8080 that writes received privmsgs to responses. This allows viewing an IRC channel through a web browser without requiring IRC port access. Key points are using AnyEvent asynchronously, registering an IRC privmsg callback, and running the Plack server to handle HTTP requests.
6. use Plack with AnyEvent!
Plack
Async
AnyEvent::
IRC IRC::Client
HTTP
7. use Plack::Loader;
use AnyEvent;
use AnyEvent::IRC::Client;
my $impl = Plack::Loader->load( 'AnyEvent', port =>
8080 );
my $cv = AnyEvent->condvar;
my $pc = AnyEvent::IRC::Client->new;
8. # Set and Start IRC Client!
$cv->begin;
$pc->reg_cb(
irc_privmsg => sub {
my ( $name, $msg ) = @_;
$writer->write( $msg->{params}->[1] );
}
);
$pc->send_srv( "JOIN", 'yapc.asia-ja' );
$pc->connect( "irc.freenode.net", 6667,
{ nick => 'nick', user => 'nick', real => 'nick' } );
$cv->end;
9. # Run plack server
$impl->run(
sub {
my ( $env, $start_response ) = @_;
$writer = $start_response->( 200, [ 'Content-Type'
=> 'text/plain' ] );
$writer->write("IRC HTTP Stream!n");
return [];
}
);
$impl->run_loop;