| 1 | package XIRCD::Component::Server; |
|---|
| 2 | use strict; |
|---|
| 3 | use MooseX::POE; |
|---|
| 4 | |
|---|
| 5 | with qw(MooseX::POE::Aliased); |
|---|
| 6 | |
|---|
| 7 | use Clone qw/clone/; |
|---|
| 8 | use Encode; |
|---|
| 9 | |
|---|
| 10 | use XIRCD::Component; |
|---|
| 11 | use POE qw/Component::Server::IRC/; |
|---|
| 12 | |
|---|
| 13 | has 'ircd' => ( |
|---|
| 14 | isa => 'POE::Component::Server::IRC', |
|---|
| 15 | is => 'rw', |
|---|
| 16 | ); |
|---|
| 17 | |
|---|
| 18 | has 'config' => ( |
|---|
| 19 | isa => 'HashRef', |
|---|
| 20 | is => 'rw', |
|---|
| 21 | ); |
|---|
| 22 | |
|---|
| 23 | has 'nicknames' => ( |
|---|
| 24 | isa => 'HashRef', |
|---|
| 25 | is => 'rw', |
|---|
| 26 | default => sub { {} }, |
|---|
| 27 | ); |
|---|
| 28 | |
|---|
| 29 | has 'message_stack' => ( |
|---|
| 30 | isa => 'HashRef', |
|---|
| 31 | is => 'rw', |
|---|
| 32 | default => sub { {} }, |
|---|
| 33 | ); |
|---|
| 34 | |
|---|
| 35 | has 'joined' => ( |
|---|
| 36 | isa => 'HashRef', |
|---|
| 37 | is => 'rw', |
|---|
| 38 | default => sub { {} }, |
|---|
| 39 | ); |
|---|
| 40 | |
|---|
| 41 | has 'components' => ( |
|---|
| 42 | isa => 'HashRef', |
|---|
| 43 | is => 'rw', |
|---|
| 44 | default => sub { {} }, |
|---|
| 45 | ); |
|---|
| 46 | |
|---|
| 47 | sub START { |
|---|
| 48 | self->alias('ircd'); |
|---|
| 49 | |
|---|
| 50 | debug "start irc"; |
|---|
| 51 | |
|---|
| 52 | self->config->{servername} ||= 'xircd.ircd'; |
|---|
| 53 | self->config->{client_encoding} ||= 'utf-8'; |
|---|
| 54 | |
|---|
| 55 | self->ircd(POE::Component::Server::IRC->spawn( config => clone(self->config) )); |
|---|
| 56 | self->ircd->yield('register'); |
|---|
| 57 | self->ircd->add_auth( mask => '*@*' ); |
|---|
| 58 | self->ircd->add_listener( port => self->config->{port} || 6667 ); |
|---|
| 59 | |
|---|
| 60 | self->ircd->yield( add_spoofed_nick => { nick => self->config->{server_nick} } ); |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | event ircd_daemon_join => sub { |
|---|
| 64 | my($user, $channel) = get_args; |
|---|
| 65 | |
|---|
| 66 | return unless my($nick) = $user =~ /^([^!]+)!/; |
|---|
| 67 | return if self->nicknames->{$channel}->{$nick}; |
|---|
| 68 | return if $nick eq self->config->{server_nick}; |
|---|
| 69 | |
|---|
| 70 | self->joined->{$channel} = 1; |
|---|
| 71 | |
|---|
| 72 | for my $message ( @{ self->message_stack->{$channel} || [] } ) { |
|---|
| 73 | self->ircd->yield( daemon_cmd_privmsg => $message->{nick}, $channel, $_ ) |
|---|
| 74 | for split /\r?\n/, $message->{text}; |
|---|
| 75 | } |
|---|
| 76 | self->message_stack->{$channel} = []; |
|---|
| 77 | }; |
|---|
| 78 | |
|---|
| 79 | event ircd_daemon_quit => sub { |
|---|
| 80 | my($user,) = get_args; |
|---|
| 81 | |
|---|
| 82 | return unless my($nick) = $user =~ /^([^!]+)!/; |
|---|
| 83 | return if $nick eq self->config->{server_nick}; |
|---|
| 84 | |
|---|
| 85 | for my $channel ( keys %{self->joined} ) { |
|---|
| 86 | next if self->nicknames->{$channel}->{$nick}; |
|---|
| 87 | self->joined->{$channel} = 0; |
|---|
| 88 | } |
|---|
| 89 | }; |
|---|
| 90 | |
|---|
| 91 | event ircd_daemon_part => sub { |
|---|
| 92 | my($user, $channel) = get_args; |
|---|
| 93 | |
|---|
| 94 | return unless my($nick) = $user =~ /^([^!]+)!/; |
|---|
| 95 | return if self->nicknames->{$channel}->{$nick}; |
|---|
| 96 | return if $nick eq self->config->{server_nick}; |
|---|
| 97 | |
|---|
| 98 | self->joined->{$channel} = 0; |
|---|
| 99 | }; |
|---|
| 100 | |
|---|
| 101 | event ircd_daemon_public => sub { |
|---|
| 102 | my($nick, $channel, $text) = get_args; |
|---|
| 103 | my $encoding = self->config->{client_encoding}; |
|---|
| 104 | |
|---|
| 105 | debug "public [$channel] $nick : $text"; |
|---|
| 106 | |
|---|
| 107 | my $component = self->components->{$channel}; |
|---|
| 108 | return unless $component; |
|---|
| 109 | debug "send to $component"; |
|---|
| 110 | |
|---|
| 111 | post $component => send_message => decode( $encoding, $text ); |
|---|
| 112 | }; |
|---|
| 113 | |
|---|
| 114 | event _publish_message => sub { |
|---|
| 115 | my ($nick, $channel, $message) = get_args; |
|---|
| 116 | |
|---|
| 117 | debug "publish to irc: [$channel] $nick : $message"; |
|---|
| 118 | |
|---|
| 119 | self->nicknames->{$channel} ||= {}; |
|---|
| 120 | if ($nick && !self->nicknames->{$channel}->{$nick}) { |
|---|
| 121 | self->nicknames->{$channel}->{$nick}++; |
|---|
| 122 | self->ircd->yield( add_spoofed_nick => { nick => $nick } ); |
|---|
| 123 | self->ircd->yield( daemon_cmd_join => $nick, $channel ); |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | #$message = encode( self->config->{client_encoding}, $message ); |
|---|
| 127 | |
|---|
| 128 | if ( self->joined->{$channel} ) { |
|---|
| 129 | self->ircd->yield( daemon_cmd_privmsg => $nick => $channel, $_ ) |
|---|
| 130 | for split /\r?\n/, $message; |
|---|
| 131 | } else { |
|---|
| 132 | self->message_stack->{$channel} ||= []; |
|---|
| 133 | push @{self->message_stack->{$channel}}, { nick => $nick, text => $message }; |
|---|
| 134 | } |
|---|
| 135 | }; |
|---|
| 136 | |
|---|
| 137 | event _publish_notice => sub { |
|---|
| 138 | my ($channel, $message) = get_args; |
|---|
| 139 | |
|---|
| 140 | debug "notice to irc: [$channel] $message"; |
|---|
| 141 | |
|---|
| 142 | #$message = encode( self->config->{client_encoding}, $message ); |
|---|
| 143 | |
|---|
| 144 | self->ircd->yield( daemon_cmd_notice => self->config->{server_nick} => $channel, $_ ) |
|---|
| 145 | for split /\r?\n/, $message; |
|---|
| 146 | }; |
|---|
| 147 | |
|---|
| 148 | event join_channel => sub { |
|---|
| 149 | my ($channel, $component) = get_args; |
|---|
| 150 | |
|---|
| 151 | debug "join channel: $channel"; |
|---|
| 152 | debug "register: $channel => $component"; |
|---|
| 153 | |
|---|
| 154 | self->components->{$channel} = $component; |
|---|
| 155 | self->ircd->yield( daemon_cmd_join => self->config->{server_nick}, $channel ); |
|---|
| 156 | }; |
|---|
| 157 | |
|---|
| 158 | 1; |
|---|