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