| 1 | package Horris::Connection::Plugin::Relay; | ||
| 2 | # ABSTRACT: Relay Plugin on Horris | ||
| 3 | |||
| 4 | |||
| 5 | use Moose; | ||
| 6 | use AnyEvent::MP qw(configure port rcv snd); | ||
| 7 | use AnyEvent::MP::Global qw(grp_reg grp_mon grp_get); | ||
| 8 | use namespace::clean -except => qw(meta); | ||
| 9 | use Encode (); | ||
| 10 | |||
| 11 | extends 'Horris::Connection::Plugin'; | ||
| 12 | with 'MooseX::Role::Pluggable::Plugin'; | ||
| 13 | |||
| 14 | has group => ( | ||
| 15 | is => 'ro', | ||
| 16 | isa => 'Str', | ||
| 17 | default => 'morris', | ||
| 18 | required => 1, | ||
| 19 | ); | ||
| 20 | |||
| 21 | has profile => ( | ||
| 22 | is => 'ro', | ||
| 23 | isa => 'Str', | ||
| 24 | default => 'morris', | ||
| 25 | required => 1, | ||
| 26 | ); | ||
| 27 | |||
| 28 | has from => ( | ||
| 29 | traits => [ 'Hash' ], | ||
| 30 | is => 'ro', | ||
| 31 | isa => 'HashRef', | ||
| 32 | required => 1, | ||
| 33 | handles => { | ||
| 34 | get_instance => 'get', | ||
| 35 | } | ||
| 36 | ); | ||
| 37 | |||
| 38 | has __guard => ( | ||
| 39 | is => 'rw', | ||
| 40 | clearer => 'clear_guard', | ||
| 41 | ); | ||
| 42 | |||
| 43 | around BUILDARGS => sub { | ||
| 44 | my ($next, $class, @args) = @_; | ||
| 45 | my $args = $next->($class, @args); | ||
| 46 | return $args; | ||
| 47 | }; | ||
| 48 | |||
| 49 | sub on_connect { | ||
| 50 | my ($self) = @_; | ||
| 51 | |||
| 52 | my $conn = $self->connection; | ||
| 53 | my $server = port; | ||
| 54 | rcv $server, notice => sub { | ||
| 55 | my ($channel, $message) = @_; | ||
| 56 | $conn->irc_notice( { | ||
| 57 | channel => $channel, | ||
| 58 | message => $message | ||
| 59 | }); | ||
| 60 | }; | ||
| 61 | rcv $server, privmsg => sub { | ||
| 62 | my ($channel, $message) = @_; | ||
| 63 | $conn->irc_privmsg( { | ||
| 64 | channel => $channel, | ||
| 65 | message => $message | ||
| 66 | }); | ||
| 67 | }; | ||
| 68 | warn "MP Server - $server\n"; | ||
| 69 | $self->__guard( grp_reg $self->group, $server ); | ||
| 70 | |||
| 71 | return $self->pass; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub irc_privmsg { | ||
| 75 | my ($self, $msg) = @_; | ||
| 76 | |||
| 77 | my $channel = $msg->channel; | ||
| 78 | my $message = $msg->message; | ||
| 79 | my $nickname = $msg->nickname; | ||
| 80 | |||
| 81 | return unless $channel; | ||
| 82 | return unless $message; | ||
| 83 | |||
| 84 | my $config = $self->from->{'\\'.$channel}; | ||
| 85 | |||
| 86 | return unless $config; | ||
| 87 | |||
| 88 | my $ports = grp_get $config->{target}; | ||
| 89 | if($ports) { | ||
| 90 | my $server = $ports->[0]; | ||
| 91 | my $msg = sprintf('<%s> %s', $nickname, $message); | ||
| 92 | if ($config->{encode} && $config->{decode}) { | ||
| 93 | $msg = Encode::encode($config->{encode}, Encode::decode($config->{decode}, $msg)); | ||
| 94 | } | ||
| 95 | |||
| 96 | snd $server, $config->{type} => $_, $msg for @{ $config->{to} }; | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | __PACKAGE__->meta->make_immutable(); | ||
| 101 | |||
| 102 | 1; | ||
| 103 | |||
| 104 | |||
| 105 | |||
| 106 | =pod | ||
| 107 | |||
| 108 | =encoding utf-8 | ||
| 109 | |||
| 110 | =head1 NAME | ||
| 111 | |||
| 112 | Horris::Connection::Plugin::Relay - Relay Plugin on Horris | ||
| 113 | |||
| 114 | =head1 VERSION | ||
| 115 | |||
| 116 | version v0.1.2 | ||
| 117 | |||
| 118 | =head1 SYNOPSIS | ||
| 119 | |||
| 120 | <Plugin MP::Relay> | ||
| 121 | <From \#perl-kr> | ||
| 122 | Target freenode | ||
| 123 | To \#perl | ||
| 124 | Type privmsg | ||
| 125 | </From> | ||
| 126 | </Plugin> | ||
| 127 | |||
| 128 | =head1 DESCRIPTION | ||
| 129 | |||
| 130 | Relay | ||
| 131 | |||
| 132 | =head1 SEE ALSO | ||
| 133 | |||
| 134 | L<AnyEvent::MP> | ||
| 135 | |||
| 136 | =head1 AUTHOR | ||
| 137 | |||
| 138 | hshong <hshong@perl.kr> | ||
| 139 | |||
| 140 | =head1 COPYRIGHT AND LICENSE | ||
| 141 | |||
| 142 | This software is copyright (c) 2011 by hshong. | ||
| 143 | |||
| 144 | This is free software; you can redistribute it and/or modify it under | ||
| 145 | the same terms as the Perl 5 programming language system itself. | ||
| 146 | |||
| 147 | =cut | ||
| 148 | |||
| 149 | |||
| 150 | __END__ |