root/lang/perl/XIRCD/trunk/lib/XIRCD/Server.pm @ 11636

Revision 11636, 2.7 kB (checked in by kan, 7 years ago)

X:C:Twitter !

Line 
1package XIRCD::Server;
2use MooseX::POE;
3
4with qw(MooseX::POE::Aliased);
5
6use Clone qw/clone/;
7use Encode;
8
9use POE qw/Component::Server::IRC/;
10
11has 'ircd' => (
12    isa => 'POE::Component::Server::IRC',
13    is  => 'rw',
14);
15
16has 'config' => (
17    isa => 'HashRef',
18    is  => 'rw',
19);
20
21has 'nicknames' => (
22    isa => 'HashRef',
23    is  => 'rw',
24    default => sub { {} },
25);
26
27has 'components' => (
28    isa => 'HashRef',
29    is  => 'rw',
30    default => sub { {} },
31);
32
33sub debug(@) { ## no critic.
34    print @_, "\n\n" if $ENV{XIRCD_DEBUG};
35}
36
37sub get_args(@) { ## no critic.
38    return @_[9..19];
39}
40
41sub 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
59event 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
72event 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
91event 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
103event 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
1141;
Note: See TracBrowser for help on using the browser.