Skip to content

Commit

Permalink
commited initial version.
Browse files Browse the repository at this point in the history
  • Loading branch information
hiratara committed Apr 19, 2009
1 parent 9a24555 commit a2a3e2f
Show file tree
Hide file tree
Showing 13 changed files with 313 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ inc
pm_to_blib
MANIFEST
Makefile.old
myprivate.yaml
7 changes: 6 additions & 1 deletion Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ use inc::Module::Install;
name 'BiLingr';
all_from 'lib/BiLingr.pm';

# requires '';
requires 'Config::Any' => '0.07';
requires 'MooseX::POE' => '0.1';
requires 'MooseX::Getopt' => '0.15';
requires 'MooseX::ConfigFromFile' => '0.02';
requires 'POE::Component::Client::Lingr' => '0.04_01';
requires 'POE::Component::IRC' => '6.04';

tests 't/*.t';
author_tests 'xt';
Expand Down
4 changes: 3 additions & 1 deletion README
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
This is Perl module BiLingr.
*** THIS MODULE IS UNDER CONSTRUCTING. YOU CAN'T USE IT. ***

This is Perl module BiLingr. It's a simple IRC - Lingr bridge.

INSTALLATION

Expand Down
4 changes: 4 additions & 0 deletions bilingr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env perl
use BiLingr;
my $bilingr = BiLingr->new_with_config(configfile => $ARGV[0]);
$bilingr->run;
12 changes: 12 additions & 0 deletions bilingr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
'lingr': {
'api_key': 'API_KEY',
'room' : 'ROOM',
'nick' : 'NICK'
},
'irc': {
'server' : 'IRC_SERVER',
'channel': 'IRC_CHANNEL',
'nick' : 'IRC_NICK'
}
}
64 changes: 60 additions & 4 deletions lib/BiLingr.pm
Original file line number Diff line number Diff line change
@@ -1,15 +1,71 @@
package BiLingr;

use strict;
use warnings;
use Moose;
use Config::Any;
use POE;
use BiLingr::Bot;
our $VERSION = '0.01';

with 'MooseX::ConfigFromFile';

# set default value so that MooseX::ConfigFromFile call get_config_from_file()
has +configfile => (
default => '',
);

has lingr => (
isa => 'HashRef[Str]',
is => 'ro',
required => 1,
);

has irc => (
isa => 'HashRef[Str]',
is => 'ro',
required => 1,
);


# required from MooseX::ConfigFromFile
sub get_config_from_file{
my( $class, $file ) = @_;

if (! $file ){
my $cfg = Config::Any->load_stems({
stems => [ 'bilingr' ],
use_ext => 1,
});
my $first_file = ( keys %{ $cfg->[0] } )[0];
return $cfg->[0]->{ $first_file } or die "config can't be loaded.";
}elsif(-f $file) {
my $cfg = Config::Any->load_files({
files => [ $file ],
use_ext => 1,
});
return $cfg->[0]->{ $file } or die "$file can't be loaded.";
}

die "$file does not exists.";
}


sub run {
my $self = shift;

BiLingr::Bot->new(parent => $self);

$poe_kernel->run;
}

__PACKAGE__->meta->make_immutable;
no Moose;


1;
__END__
=head1 NAME
BiLingr -
BiLingr - Simple IRC-Lingr Bridge bot.
=head1 SYNOPSIS
Expand Down
25 changes: 25 additions & 0 deletions lib/BiLingr/Bot.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package BiLingr::Bot;

use MooseX::POE;
use BiLingr::Lingr;
use BiLingr::IRC;

has parent => (
isa => 'BiLingr',
is => 'ro',
required => 1,
);

# POE events ----------------------------------------------
sub START {
my ( $self ) = @_[OBJECT, ARG0 .. $#_];
my $lingr = BiLingr::Lingr->new( %{ $self->parent->lingr } );
my $irc = BiLingr::IRC->new( %{ $self->parent->irc } );

$lingr->irc( $irc->get_session_id );
$irc->lingr( $lingr->get_session_id );
}


no MooseX::POE;
1;
73 changes: 73 additions & 0 deletions lib/BiLingr/IRC.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package BiLingr::IRC;
use MooseX::POE;
use POE qw(Component::IRC);

has server => (
isa => 'Str',
is => 'ro',
required => 1,
);

has nick => (
isa => 'Str',
is => 'ro',
default => 'bilingr_irc',
);

has channel => (
isa => 'Str',
is => 'ro',
required => 1,
);

has lingr => (
isa => 'Int',
is => 'rw',
);

has _irc => (
isa => 'POE::Component::IRC',
is => 'rw',
);


# POE events ----------------------------------------------
sub START {
my ( $self ) = @_[OBJECT, ARG0 .. $#_];
my $irc = POE::Component::IRC->spawn(
nick => $self->nick,
# ircname => $self->nick,
server => $self->server,
);

$irc->yield( register => 'all' );
$irc->yield( connect => {} );

$self->_irc( $irc );
}

event irc_001 => sub {
my ( $self ) = @_[OBJECT, ARG0 .. $#_];
$self->_irc->yield( join => $self->channel );
};

event irc_public => sub {
my ( $self, $who, $where, $what ) = @_[OBJECT, ARG0 .. $#_];

$poe_kernel->post(
$self->lingr => said =>
$who, $what,
);
};

event said => sub {
my ( $self, $who, $what ) = @_[OBJECT, ARG0 .. $#_];

$self->_irc->yield(
privmsg => $self->channel, "$who: $what",
);
};


no MooseX::POE;
1;
95 changes: 95 additions & 0 deletions lib/BiLingr/Lingr.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package BiLingr::Lingr;
use MooseX::POE;
use POE qw(Component::Client::Lingr);

has api_key => (
isa => 'Str',
is => 'ro',
required => 1,
);

has room => (
isa => 'Str',
is => 'ro',
required => 1,
);

has nick => (
isa => 'Str',
is => 'ro',
default => __PACKAGE__,
);

has irc => (
isa => 'Int',
is => 'rw',
);

has _lingr => (
isa => 'Str',
is => 'ro',
lazy_build => 1,
);

sub _build__lingr{
my $self = shift;
return 'lingr' . ($self + 0);
}


# POE events ----------------------------------------------
sub START {
my ( $self ) = @_[OBJECT, ARG0 .. $#_];
POE::Component::Client::Lingr->spawn(alias => $self->_lingr);
$poe_kernel->post( $self->_lingr => 'register' );
$poe_kernel->post(
$self->_lingr => call =>
'session.create', { api_key => $self->api_key }
);
}

event 'lingr.session.create' => sub {
my ( $self, $event ) = @_[OBJECT, ARG0 .. $#_];
$poe_kernel->call(
$self->_lingr => call =>
'room.enter', { id => $self->room, nickname => $self->nick });
};

event 'lingr.room.enter' => sub {
my ( $self, $event ) = @_[OBJECT, ARG0 .. $#_];
};

event 'lingr.room.observe' => sub {
my ( $self, $event ) = @_[OBJECT, ARG0 .. $#_];
for my $msg (@{ $event->{messages} || []}){
next unless $msg->{client_type} eq 'human';
$poe_kernel->post(
$self->irc => said =>
$msg->{nickname}, $msg->{text},
);
}
};

event said => sub {
my ( $self, $who, $what ) = @_[OBJECT, ARG0 .. $#_];

$poe_kernel->post(
$self->_lingr => call =>
'room.say', { message => "$who: $what" }
);
};

# for DEBUG ==================
use Data::Dumper;
event 'lingr.error.http' => sub {
my ( $self, $event ) = @_[OBJECT, ARG0 .. $#_];
die Dumper($event);
};

event 'lingr.error.response' => sub {
my ( $self, $event ) = @_[OBJECT, ARG0 .. $#_];
die Dumper($event);
};

no MooseX::POE;
1;
4 changes: 3 additions & 1 deletion t/00_compile.t
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use strict;
use Test::More tests => 1;

BEGIN { use_ok 'BiLingr' }
BEGIN {
use_ok qw/BiLingr BiLingr::Bot BiLingr::Lingr/;
}
20 changes: 20 additions & 0 deletions t/config.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use strict;
use warnings;
use BiLingr;
use Test::More tests => 6;


# default config file is 'bilingr.[ANY]'.
my $b1 = BiLingr->new_with_config();

is $b1->lingr->{api_key}, 'API_KEY';
is $b1->lingr->{room}, 'ROOM';
is $b1->lingr->{nick}, 'NICK';


# set config file explicitly
my $b2 = BiLingr->new_with_config( configfile => 't/test.yaml' );

is $b2->lingr->{api_key}, 'api_key';
is $b2->lingr->{room}, 'room';
is $b2->lingr->{nick}, 'nick';
8 changes: 8 additions & 0 deletions t/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
lingr:
api_key: api_key
room: room
nick: nick
irc:
server: irc_server
channel: irc_channel
nick: irc_nick
3 changes: 3 additions & 0 deletions xt/perlcriticrc
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
[TestingAndDebugging::ProhibitNoStrict]
allow=refs

[TestingAndDebugging::RequireUseStrict]
equivalent_modules = MooseX::POE

0 comments on commit a2a3e2f

Please sign in to comment.