Changeset 11630

Show
Ignore:
Timestamp:
05/15/08 14:24:25 (5 years ago)
Author:
kan
Message:

Component::Wassr can send messages.

Location:
lang/perl/XIRCD/trunk/lib/XIRCD
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/XIRCD/trunk/lib/XIRCD/Component/Wassr.pm

    r11554 r11630  
    2424); 
    2525 
     26has 'jid' => ( 
     27    isa     => 'Str', 
     28    is      => 'rw', 
     29); 
     30 
    2631sub debug(@) { ## no critic. 
    2732    print @_ if $ENV{XIRCD_DEBUG}; 
     
    3540    my $self = shift; 
    3641 
    37     $self->alias('jabber'); 
     42    $self->alias('wassr'); 
    3843 
    3944    debug "start wassr\n"; 
     
    5863    ); 
    5964 
    60     POE::Kernel->post( ircd => 'join_channel', $self->config->{channel} ); 
     65    POE::Kernel->post( ircd => 'join_channel', $self->config->{channel}, $self->alias ); 
    6166    POE::Kernel->post( jabber => 'connect' ); 
    6267} 
     
    6974        debug "init finished\n"; 
    7075        $self->jid($self->jabber->jid); 
    71         #$heap->{sid} = $sender->ID; 
    7276 
    7377        POE::Kernel->post(jabber => 'output_handler', POE::Filter::XML::Node->new('presence')); 
     
    8286    debug "recv:", $node->to_str, "\n\n"; 
    8387 
    84     my ($body) = $node->get_tag('body'); 
     88    my ($body,) = $node->get_tag('body'); 
     89 
    8590    if ($body && $node->attr('from') =~ /^wassr-bot\@wassr\.jp/) { 
    86         my ($nick, $text) = $body =~ /^(\w+): (.*)/s; 
    87         POE::Kernel->post( ircd => 'publish_message', $nick, $self->config->{channel}, $text ) if $nick; 
     91        my ($nick, $text) = $body->data =~ /^([A-Za-z0-9_.-]+): (.*)/s; 
     92        if ($nick && $text) { 
     93            POE::Kernel->post( ircd => 'publish_message', $nick, $self->config->{channel}, $text ); 
     94        } else { 
     95            POE::Kernel->post( ircd => 'publish_notice', $self->config->{channel}, $body->data ); 
     96        } 
     97    } 
     98}; 
     99 
     100event send_message => sub { 
     101    my $self = shift; 
     102    my ($message,) = get_args(@_); 
     103 
     104    my $node = POE::Filter::XML::Node->new('message'); 
     105 
     106    $node->attr('to', 'wassr-bot@wassr.jp'); 
     107    $node->attr('from', $self->{jid} ); 
     108    $node->attr('type', 'chat'); 
     109    $node->insert_tag('body')->data( $message ); 
     110 
     111    debug "send:", $node->to_str, "\n\n"; 
     112 
     113    POE::Kernel->post( jabber => output_handler => $node ); 
     114}; 
     115 
     116event error_handler => sub { 
     117    my $self = shift; 
     118    my ($error,) = get_args(@_); 
     119 
     120    if ( $error == +PCJ_SOCKETFAIL or $error == +PCJ_SOCKETDISCONNECT or $error == +PCJ_CONNECTFAIL ) { 
     121        print "Reconnecting!\n"; 
     122        POE::Kernel->post( jabber => 'reconnect' ); 
     123    } 
     124    elsif ( $error == +PCJ_SSLFAIL ) { 
     125        print "TLS/SSL negotiation failed\n"; 
     126    } 
     127    elsif ( $error == +PCJ_AUTHFAIL ) { 
     128        print "Failed to authenticate\n"; 
     129    } 
     130    elsif ( $error == +PCJ_BINDFAIL ) { 
     131        print "Failed to bind a resource\n"; 
     132    } 
     133    elsif ( $error == +PCJ_SESSIONFAIL ) { 
     134        print "Failed to establish a session\n"; 
    88135    } 
    89136}; 
  • lang/perl/XIRCD/trunk/lib/XIRCD/Server.pm

    r11554 r11630  
    2020 
    2121has 'nicknames' => ( 
     22    isa => 'HashRef', 
     23    is  => 'rw', 
     24    default => sub { {} }, 
     25); 
     26 
     27has 'components' => ( 
    2228    isa => 'HashRef', 
    2329    is  => 'rw', 
     
    5258 
    5359event ircd_daemon_public => sub { 
    54     my ($self, $user, $channel, $text) = @_; 
     60    my $self = shift; 
     61    my($nick, $channel, $text) = get_args(@_); 
    5562    my $encoding = $self->config->{client_encoding}; 
    5663 
    57     POE::Kernel->post( im => send_message => decode( $encoding, $text ) ); 
    58     POE::Kernel->post( ustream => say => decode( $encoding, $text ) ); 
     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 ) ); 
    5970}; 
    6071 
     
    7283    } 
    7384 
    74     $message = encode( $self->config->{client_encoding}, $message ); 
     85    #$message = encode( $self->config->{client_encoding}, $message ); 
    7586 
    7687    $self->ircd->yield( daemon_cmd_privmsg => $nick => $channel, $_ ) 
     
    7889}; 
    7990 
     91event publish_notice => sub { 
     92    my $self = shift; 
     93    my ($channel, $message) = get_args(@_); 
     94 
     95    debug "notice to irc: [$channel] $message \n\n"; 
     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 
    80103event join_channel => sub { 
    81104    my $self = shift; 
    82     my ($channel,) = get_args(@_); 
     105    my ($channel, $component) = get_args(@_); 
    83106 
    84107    debug "join channel: $channel"; 
     108    debug "register: $channel => $component"; 
    85109 
     110    $self->components->{$channel} = $component; 
    86111    $self->ircd->yield( daemon_cmd_join => $self->config->{server_nick}, $channel ); 
    87112};