Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added classes

  • Loading branch information...
commit 81ee8689b1861c6f354af20c831a6cbe14da442f 1 parent 8d8bb74
Daisuke Murase authored
9 config.yaml
View
@@ -0,0 +1,9 @@
+httpd:
+ port: 3000
+
+irc:
+ server: chat.freenode.net
+ port: 6667
+ nick: gitbot
+ channels:
+ - #kamaitachi
46 lib/Github/IRCBot.pm
View
@@ -1,9 +1,49 @@
package Github::IRCBot;
-use strict;
-use warnings;
+use Moose;
our $VERSION = '0.01';
+use POE;
+use Github::IRCBot::IRC;
+use Github::IRCBot::HTTPD;
+
+has config => (
+ is => 'rw',
+ isa => 'HashRef',
+ default => sub { {} },
+);
+
+has irc => (
+ is => 'rw',
+ isa => 'Github::IRCBot::IRC',
+ lazy => 1,
+ default => sub {
+ my $self = shift;
+ Github::IRCBot::IRC->new( $self->config->{irc} );
+ },
+);
+
+has httpd => (
+ is => 'rw',
+ isa => 'Github::IRCBot::HTTPD',
+ lazy => 1,
+ default => sub {
+ my $self = shift;
+ Github::IRCBot::HTTPD->new( $self->config->{httpd} );
+ },
+);
+
+no Moose;
+
+sub run {
+ my $self = shift;
+
+ $self->irc->spawn;
+ $self->httpd->spawn;
+
+ POE::Kernel->run;
+}
+
=head1 NAME
Github::IRCBot - Module abstract (<= 44 characters) goes here
@@ -35,4 +75,4 @@ LICENSE file included with this module.
=cut
-1;
+__PACKAGE__->meta->make_immutable;
63 lib/Github/IRCBot/HTTPD.pm
View
@@ -0,0 +1,63 @@
+package Github::IRCBot::HTTPD;
+use Moose;
+
+use POE qw/Component::Server::HTTP/;
+use JSON::XS ();
+
+has port => (
+ is => 'rw',
+ isa => 'Int',
+ default => sub { 3000 },
+);
+
+has session => (
+ is => 'rw',
+ isa => 'POE::Session',
+ lazy => 1,
+ default => sub {
+ my $self = shift;
+ POE::Session->create(
+ object_states => [
+ $self => {
+ map { $_ => "poe_$_" } qw/_start/,
+ },
+ ],
+ );
+ },
+);
+
+has aliases => (
+ is => 'rw',
+ isa => 'HashRef',
+);
+
+has json => (
+ is => 'rw',
+ isa => 'JSON::XS',
+ lazy => 1,
+ default => sub { JSON::XS->new->latin1 },
+);
+
+sub spawn {
+ my $self = shift;
+ $self->session;
+}
+
+sub poe__start {
+ my ($self, $kernel, $session) = @_[OBJECT, KERNEL, SESSION];
+
+ $kernel->alias_set('httpd');
+
+ POE::Component::Server::HTTPD->new(
+ Port => $self->port,
+ ContentHandler => {
+ '/' => sub {
+ my ($request, $response) = @_;
+
+ },
+ },
+ );
+}
+
+__PACKAGE__->meta->make_immutable;
+
106 lib/Github/IRCBot/IRC.pm
View
@@ -0,0 +1,106 @@
+package Github::IRCBot::IRC;
+use Moose;
+
+use POE qw/Component::IRC/;
+
+has server => (
+ is => 'rw',
+ isa => 'Str',
+ required => 1,
+);
+
+has port => (
+ is => 'rw',
+ isa => 'Int',
+ default => sub { 6667 },
+);
+
+has nick => (
+ is => 'rw',
+ isa => 'Str',
+ default => sub { 'bot' },
+);
+
+has session => (
+ is => 'rw',
+ isa => 'POE::Session',
+ lazy => 1,
+ default => sub {
+ my $self = shift;
+ POE::Session->create(
+ object_states => [
+ $self => {
+ map { $_ => "poe_$_" } qw/_start _default irc_001 irc_433 say/,
+ },
+ ],
+ );
+ },
+);
+
+has channels => (
+ is => 'rw',
+ isa => 'ArrayRef',
+ lazy => 1,
+ default => sub { [] },
+);
+
+has component => (
+ is => 'rw',
+ isa => 'POE::Component::IRC',
+);
+
+no Moose;
+
+sub spawn {
+ my $self = shift;
+ $self->session;
+}
+
+sub poe__start {
+ my ($self, $kernel, $session) = @_[OBJECT, KERNEL, SESSION];
+
+ $kernel->alias_set('irc');
+
+ my $irc = $self->{component} = POE::Component::IRC->spawn(
+ Nick => $self->nick,
+ Ircname => $self->nick,
+ Username => $self->nick,
+ Server => $self->server,
+ Port => $self->port,
+ );
+ $irc->yield( register => 'all' );
+ $irc->yield( connect => {} );
+}
+
+sub poe_irc_001 {
+ my ($self, $kernel) = @_[OBJECT, KERNEL];
+ $self->component->yield( join => $_ ) for @{ $self->channels };
+}
+
+sub poe_irc_433 {
+ my ($self, $kernel) = @_[OBJECT, KERNEL];
+ my ($nick, $d) = $self->nick =~ /^(.*?)(\d*)$/;
+ $d = defined $d ? ++$d : 1;
+
+ $self->component->yield( nick => $nick . $d );
+}
+
+sub poe__default {
+ my ($event, $args) = @_[ARG0 .. $#_];
+
+ print "$event: ";
+ for my $arg (@$args) {
+ print ref($arg) ? '[' . join(',', @$args) . ']'
+ : $arg;
+ print ", ";
+ }
+ print "\n";
+}
+
+sub poe_say {
+ my ($self, $kernel, $session, $channel, $message) = @_[OBJECT, KERNEL, SESSION, ARG0, ARG1];
+ $self->component->yield( privmsg => $channel => $message );
+}
+
+__PACKAGE__->meta->make_immutable;
+
24 script/bot.pl
View
@@ -0,0 +1,24 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use FindBin::libs;
+
+use Getopt::Long;
+use Pod::Usage;
+use YAML;
+
+use Github::IRCBot;
+
+GetOptions(
+ \my %option,
+ qw/help config=s/,
+);
+pod2usage(0) if $option{help};
+
+my $config = $option{config} || "$FindBin::Bin/../config.yaml";
+$config = YAML::LoadFile($config);
+
+my $bot = Github::IRCBot->new( config => $config );
+$bot->run;
+
Please sign in to comment.
Something went wrong with that request. Please try again.