| 1 | package XIRCD::Server; |
|---|
| 2 | use MooseX::POE; |
|---|
| 3 | |
|---|
| 4 | with qw(MooseX::POE::Aliased); |
|---|
| 5 | |
|---|
| 6 | use Clone qw/clone/; |
|---|
| 7 | use Encode; |
|---|
| 8 | |
|---|
| 9 | use POE qw/Component::Server::IRC/; |
|---|
| 10 | |
|---|
| 11 | has 'ircd' => ( |
|---|
| 12 | isa => 'POE::Component::Server::IRC', |
|---|
| 13 | is => 'rw', |
|---|
| 14 | ); |
|---|
| 15 | |
|---|
| 16 | has 'config' => ( |
|---|
| 17 | isa => 'HashRef', |
|---|
| 18 | is => 'rw', |
|---|
| 19 | ); |
|---|
| 20 | |
|---|
| 21 | has 'nicknames' => ( |
|---|
| 22 | isa => 'HashRef', |
|---|
| 23 | is => 'rw', |
|---|
| 24 | default => sub { {} }, |
|---|
| 25 | ); |
|---|
| 26 | |
|---|
| 27 | has 'components' => ( |
|---|
| 28 | isa => 'HashRef', |
|---|
| 29 | is => 'rw', |
|---|
| 30 | default => sub { {} }, |
|---|
| 31 | ); |
|---|
| 32 | |
|---|
| 33 | sub debug(@) { ## no critic. |
|---|
| 34 | print @_, "\n\n" if $ENV{XIRCD_DEBUG}; |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | sub get_args(@) { ## no critic. |
|---|
| 38 | return @_[9..19]; |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | sub START { |
|---|
| 42 | my $self = shift; |
|---|
| 43 | |
|---|
| 44 | $self->alias('ircd'); |
|---|
| 45 | |
|---|
| 46 | debug "start irc"; |
|---|
| 47 | |
|---|
| 48 | $self->config->{servername} ||= 'xircd.ircd'; |
|---|
| 49 | $self->config->{client_encoding} ||= 'utf-8'; |
|---|
| 50 | |
|---|
| 51 | $self->ircd(POE::Component::Server::IRC->spawn( config => clone($self->config) )); |
|---|
| 52 | $self->ircd->yield('register'); |
|---|
| 53 | $self->ircd->add_auth( mask => '*@*' ); |
|---|
| 54 | $self->ircd->add_listener( port => $self->config->{port} || 6667 ); |
|---|
| 55 | |
|---|
| 56 | $self->ircd->yield( add_spoofed_nick => { nick => $self->config->{server_nick} } ); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | event ircd_daemon_public => sub { |
|---|
| 60 | my $self = shift; |
|---|
| 61 | my($nick, $channel, $text) = get_args(@_); |
|---|
| 62 | my $encoding = $self->config->{client_encoding}; |
|---|
| 63 | |
|---|
| 64 | debug "public [$channel] $nick : $text"; |
|---|
| 65 | |
|---|
| 66 | my $component = $self->components->{$channel}; |
|---|
| 67 | return unless $component; |
|---|
| 68 | |
|---|
| 69 | POE::Kernel->post( $component => send_message => decode( $encoding, $text ) ); |
|---|
| 70 | }; |
|---|
| 71 | |
|---|
| 72 | event publish_message => sub { |
|---|
| 73 | my $self = shift; |
|---|
| 74 | my ($nick, $channel, $message) = get_args(@_); |
|---|
| 75 | |
|---|
| 76 | debug "publish to irc: [$channel] $nick : $message"; |
|---|
| 77 | |
|---|
| 78 | $self->nicknames->{$channel} ||= {}; |
|---|
| 79 | if ($nick && !$self->nicknames->{$channel}->{$nick}) { |
|---|
| 80 | $self->nicknames->{$channel}->{$nick}++; |
|---|
| 81 | $self->ircd->yield( add_spoofed_nick => { nick => $nick } ); |
|---|
| 82 | $self->ircd->yield( daemon_cmd_join => $nick, $channel ); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | #$message = encode( $self->config->{client_encoding}, $message ); |
|---|
| 86 | |
|---|
| 87 | $self->ircd->yield( daemon_cmd_privmsg => $nick => $channel, $_ ) |
|---|
| 88 | for split /\r?\n/, $message; |
|---|
| 89 | }; |
|---|
| 90 | |
|---|
| 91 | event publish_notice => sub { |
|---|
| 92 | my $self = shift; |
|---|
| 93 | my ($channel, $message) = get_args(@_); |
|---|
| 94 | |
|---|
| 95 | debug "notice to irc: [$channel] $message"; |
|---|
| 96 | |
|---|
| 97 | #$message = encode( $self->config->{client_encoding}, $message ); |
|---|
| 98 | |
|---|
| 99 | $self->ircd->yield( daemon_cmd_notice => $self->config->{server_nick} => $channel, $_ ) |
|---|
| 100 | for split /\r?\n/, $message; |
|---|
| 101 | }; |
|---|
| 102 | |
|---|
| 103 | event join_channel => sub { |
|---|
| 104 | my $self = shift; |
|---|
| 105 | my ($channel, $component) = get_args(@_); |
|---|
| 106 | |
|---|
| 107 | debug "join channel: $channel"; |
|---|
| 108 | debug "register: $channel => $component"; |
|---|
| 109 | |
|---|
| 110 | $self->components->{$channel} = $component; |
|---|
| 111 | $self->ircd->yield( daemon_cmd_join => $self->config->{server_nick}, $channel ); |
|---|
| 112 | }; |
|---|
| 113 | |
|---|
| 114 | 1; |
|---|