Skip to content

Instantly share code, notes, and snippets.

@yusukebe
Created September 10, 2009 23:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yusukebe/184920 to your computer and use it in GitHub Desktop.
Save yusukebe/184920 to your computer and use it in GitHub Desktop.
irc_http_stream.pl
#!/usr/bin/perl
use strict;
use Plack::Loader;
use AnyEvent;
use AnyEvent::IRC::Client;
use DateTime;
use Data::Dumper;
use Encode;
my $channel = shift;
my $nick = 'irc_stream';
my $impl = Plack::Loader->load( 'AnyEvent', port => 8080 );
my $writer;
my $cv = AnyEvent->condvar;
my $pc = AnyEvent::IRC::Client->new;
$cv->begin;
$pc->reg_cb(
connect => sub {
my ( $pc, $err ) = @_;
if ( defined $err ) {
warn "Couldn't connect to server: $err\n";
}
},
registered => sub {
my ($self) = @_;
warn "join $channel\n";
$pc->enable_ping(60);
},
disconnect => sub {
warn "disconnected: $_[1]!\n";
},
irc_notice => sub {
http_write( @_ );
},
irc_privmsg => sub {
http_write ( @_ );
}
);
$pc->send_srv( "JOIN", $channel );
$pc->connect( "irc.freenode.net", 6667,
{ nick => $nick, user => $nick, real => $nick } );
$cv->end;
$impl->run(
sub {
my ( $env, $start_response ) = @_;
$writer = $start_response->( 200, [ 'Content-Type' => 'text/plain' ] );
$writer->write("IRC HTTP Stream!\n");
return [];
}
);
warn "start server port: $impl->{port}\n";
$impl->run_loop;
sub http_write {
my ( $name, $msg ) = @_;
if ( $msg->{params}->[1] && $msg->{prefix} =~ /([^!]+)/ ) {
my $nick = $1;
my $text = $msg->{params}->[1];
my $now = DateTime->now( time_zone => 'Asia/Tokyo' );
$writer->write( $now->hms(':') . " $nick: $text\n" );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment