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

Revision 7015, 2.0 kB (checked in by kan, 5 years ago)

initial import XIRCD(Pluggable IRC Gateway(Server))

Line 
1package XIRCD::Server;
2use strict;
3use warnings;
4
5use Clone qw/clone/;
6use Encode;
7
8use POE qw/Component::Server::IRC/;
9
10sub spawn {
11    my $class = shift;
12    my $config = @_ > 1 ? {@_} : $_[0];
13
14    $config->{servername} ||= 'xircd.ircd';
15    $config->{client_encoding} ||= 'utf-8';
16
17    my $ircd = POE::Component::Server::IRC->spawn( config => clone($config) );
18    POE::Session->create(
19        package_states => [
20            __PACKAGE__, [qw/_start ircd_daemon_public publish_message join_channel/],
21        ],
22        heap => { ircd => $ircd, config => $config },
23    );
24}
25
26sub debug(@) { ## no critic.
27    print @_ if $ENV{XIRCD_DEBUG};
28}
29
30sub _start {
31    my ($kernel, $heap) = @_[KERNEL, HEAP];
32
33    $kernel->alias_set('ircd');
34
35    my ($ircd, $config) = @$heap{qw/ircd config/};
36
37    $ircd->yield('register');
38    $ircd->add_auth( mask => '*@*' );
39    $ircd->add_listener( port => $config->{port} || 6667 );
40
41    debug "start irc \n\n";
42
43    $ircd->yield( add_spoofed_nick => { nick => $config->{server_nick} } );
44
45    $heap->{nicknames} = {};
46}
47
48sub ircd_daemon_public {
49    my ($kernel, $heap, $user, $channel, $text) = @_[KERNEL, HEAP, ARG0, ARG1, ARG2];
50    my $encoding = $heap->{config}{client_encoding};
51
52    $kernel->post( im => send_message => decode( $encoding, $text ) );
53    $kernel->post( ustream => say => decode( $encoding, $text ) );
54}
55
56sub publish_message {
57    my ($kernel, $heap, $channel, $message) = @_[KERNEL, HEAP, ARG0, ARG1];
58
59    debug "publish to irc: [$channel] $message \n\n";
60
61    my ($ircd, $config) = @$heap{qw/ircd config/};
62    $message = encode( $config->{client_encoding}, $message );
63
64    my $say = sub {
65        my ($nick, $text) = @_;
66        $ircd->yield( daemon_cmd_privmsg => $nick => $channel, $_ )
67            for split /\r?\n/, $text;
68    };
69
70    $say->($config->{server_nick}, $message);
71}
72
73sub join_channel {
74    my ($kernel, $heap, $channel) = @_[KERNEL, HEAP, ARG0];
75    my ($ircd, $config) = @$heap{qw/ircd config/};
76
77    debug "join channel: $channel";
78
79    $ircd->yield( daemon_cmd_join => $config->{server_nick}, $channel );
80}
81
821;
Note: See TracBrowser for help on using the browser.