Skip to content

Commit

Permalink
added multi room bridge.
Browse files Browse the repository at this point in the history
  • Loading branch information
hiratara committed Apr 24, 2009
1 parent 1dcf53c commit 538c7ba
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 46 deletions.
1 change: 1 addition & 0 deletions Makefile.PL
Expand Up @@ -8,6 +8,7 @@ requires 'MooseX::Getopt' => '0.15';
requires 'MooseX::ConfigFromFile' => '0.02';
requires 'POE::Component::Client::Lingr' => '0.04'; # 0.04_01
requires 'POE::Component::IRC' => '6.04';
requires 'UNIVERSAL::require' => '0.11';

tests 't/*.t';
author_tests 'xt';
Expand Down
8 changes: 6 additions & 2 deletions bilingr.json
@@ -1,11 +1,14 @@
{
'lingr': {
"rooms" : [
{
'protocol' : 'Lingr',
'api_key': 'your api key',
'room' : 'your room name',
'pass' : 'rooms password (if needed)',
'nick' : 'bot nick name'
},
'irc': {
{
'protocol' : 'IRC',
'server' : 'irc host name',
'port' : 'irc host port',
'password' : 'irc host password',
Expand All @@ -14,4 +17,5 @@
'nick' : 'bot nick name',
'charset': 'charset of send and recieve data(default: utf8)'
}
]
}
11 changes: 2 additions & 9 deletions lib/BiLingr.pm
Expand Up @@ -12,19 +12,12 @@ has +configfile => (
default => '',
);

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

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


# required from MooseX::ConfigFromFile
sub get_config_from_file{
my( $class, $file ) = @_;
Expand Down
31 changes: 27 additions & 4 deletions lib/BiLingr/Bot.pm
Expand Up @@ -3,23 +3,46 @@ package BiLingr::Bot;
use MooseX::POE;
use BiLingr::Lingr;
use BiLingr::IRC;
use UNIVERSAL::require;

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

has rooms => (
isa => 'ArrayRef[Object]',
is => 'ro',
default => sub { [] },
);

# 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 );
for my $setting( @{$self->parent->rooms} ){
my $class = 'BiLingr::' . $setting->{protocol};
$class->require;
my $room = $class->new(
%{$setting},
parent => $self->get_session_id,
);
push @{ $self->rooms }, $room;
}
}

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

my $sender = $_[SENDER];
for my $room ( @{ $self->rooms } ){
next if $room->get_session_id == $sender->ID;
$room->yield('said' => $who, $what);
}
};



no MooseX::POE;
1;
4 changes: 2 additions & 2 deletions lib/BiLingr/IRC.pm
Expand Up @@ -42,7 +42,7 @@ has charset => (
default => 'utf8',
);

has lingr => (
has parent => (
isa => 'Int',
is => 'rw',
);
Expand Down Expand Up @@ -85,7 +85,7 @@ event irc_public => sub {
my ($who, $where) = split /!/, $who_where, 2;

$poe_kernel->post(
$self->lingr => said =>
$self->parent => notify_message =>
$who, Encode::decode($self->charset, $what),
);
};
Expand Down
4 changes: 2 additions & 2 deletions lib/BiLingr/Lingr.pm
Expand Up @@ -30,7 +30,7 @@ has irc => (
is => 'rw',
);

has irc => (
has parent => (
isa => 'Int',
is => 'rw',
);
Expand Down Expand Up @@ -78,7 +78,7 @@ event 'lingr.room.observe' => sub {
for my $msg (@{ $event->{messages} || []}){
next unless $msg->{client_type} eq 'human';
$poe_kernel->post(
$self->irc => said =>
$self->parent => notify_message =>
$msg->{nickname}, $msg->{text},
);
}
Expand Down
42 changes: 24 additions & 18 deletions t/config.t
@@ -1,26 +1,32 @@
use strict;
use warnings;
use BiLingr;
use Test::More tests => 12;

use Test::More tests => 13;

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

ok $b1->lingr->{api_key};
ok $b1->lingr->{room};
ok $b1->lingr->{pass};
ok $b1->lingr->{nick};
ok $b1->irc->{server};
ok $b1->irc->{channel};
ok $b1->irc->{key};
ok $b1->irc->{nick};
ok $b1->irc->{charset};
my $b1 = BiLingr->new_with_config();
my $lingr = $b1->rooms->[0];
my $irc = $b1->rooms->[1];

ok $lingr->{api_key};
ok $lingr->{room};
ok $lingr->{pass};
ok $lingr->{nick};
ok $irc->{server};
ok $irc->{channel};
ok $irc->{key};
ok $irc->{nick};
ok $irc->{charset};
}

# set config file explicitly
my $b2 = BiLingr->new_with_config( configfile => 't/test.yaml' );
{
# set config file explicitly
my $b2 = BiLingr->new_with_config( configfile => 't/test.yaml' );
my $lingr = $b2->rooms->[0];

is $b2->lingr->{api_key}, 'api_key';
is $b2->lingr->{room}, 'room';
is $b2->lingr->{nick}, 'nick';
is $lingr->{protocol}, 'Lingr';
is $lingr->{api_key}, 'api_key';
is $lingr->{room}, 'room';
is $lingr->{nick}, 'nick';
}
19 changes: 10 additions & 9 deletions t/test.yaml
@@ -1,9 +1,10 @@
lingr:
api_key: api_key
room: room
nick: nick
irc:
server: irc_server
channel: irc_channel
nick: irc_nick
charset: charset
rooms :
- protocol : Lingr
api_key : api_key
room : room
nick : nick
- protocol : IRC
server : irc_server
channel : irc_channel
nick : irc_nick
charset : charset

0 comments on commit 538c7ba

Please sign in to comment.