Changeset 11679 for lang/perl/XIRCD

Show
Ignore:
Timestamp:
05/16/08 10:46:51 (5 years ago)
Author:
kan
Message:

use self.pm self & get_args so cool.

Location:
lang/perl/XIRCD/trunk
Files:
6 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/XIRCD/trunk/TODO

    r11669 r11679  
    1 - use self 
    2 - get_args use Devel::Caller 
    31- auto START & STOP 
    42-- auto join channel 
  • lang/perl/XIRCD/trunk/lib/XIRCD/Component.pm

    r11670 r11679  
    22use Moose; 
    33 
     4use self; 
     5use Devel::Caller qw(caller_args); 
     6 
    47use Sub::Exporter -setup => { 
    5     exports => [qw(debug get_args http_alias)], 
     8    exports => [qw(self debug get_args http_alias)], 
    69    groups  => {  
    710        default => [ -all ],  
     
    1316} 
    1417 
    15 sub get_args (@) { 
    16     return @_[9..19]; 
     18sub get_args { 
     19    return (caller_args(1))[10..20]; 
    1720} 
    1821 
    1922sub http_alias { 
    20     my $self = shift; 
    21     return 'twitter_' . $self->get_session_id; 
     23    return 'twitter_' . self->get_session_id; 
    2224} 
    2325 
  • lang/perl/XIRCD/trunk/lib/XIRCD/Component/Server.pm

    r11669 r11679  
    3333 
    3434sub START { 
    35     my $self = shift; 
    36  
    37     $self->alias('ircd'); 
     35    self->alias('ircd'); 
    3836 
    3937    debug "start irc"; 
    4038 
    41     $self->config->{servername} ||= 'xircd.ircd'; 
    42     $self->config->{client_encoding} ||= 'utf-8'; 
     39    self->config->{servername} ||= 'xircd.ircd'; 
     40    self->config->{client_encoding} ||= 'utf-8'; 
    4341 
    44     $self->ircd(POE::Component::Server::IRC->spawn( config => clone($self->config) )); 
    45     $self->ircd->yield('register'); 
    46     $self->ircd->add_auth( mask => '*@*' ); 
    47     $self->ircd->add_listener( port => $self->config->{port} || 6667 ); 
     42    self->ircd(POE::Component::Server::IRC->spawn( config => clone(self->config) )); 
     43    self->ircd->yield('register'); 
     44    self->ircd->add_auth( mask => '*@*' ); 
     45    self->ircd->add_listener( port => self->config->{port} || 6667 ); 
    4846 
    49     $self->ircd->yield( add_spoofed_nick => { nick => $self->config->{server_nick} } ); 
     47    self->ircd->yield( add_spoofed_nick => { nick => self->config->{server_nick} } ); 
    5048} 
    5149 
    5250event ircd_daemon_public => sub { 
    53     my $self = shift; 
    54     my($nick, $channel, $text) = get_args(@_); 
    55     my $encoding = $self->config->{client_encoding}; 
     51    my($nick, $channel, $text) = get_args; 
     52    my $encoding = self->config->{client_encoding}; 
    5653 
    5754    debug "public [$channel] $nick : $text"; 
    5855 
    59     my $component = $self->components->{$channel}; 
     56    my $component = self->components->{$channel}; 
    6057    return unless $component; 
    6158 
     
    6461 
    6562event publish_message => sub { 
    66     my $self = shift; 
    67     my ($nick, $channel, $message) = get_args(@_); 
     63    my ($nick, $channel, $message) = get_args; 
    6864 
    6965    debug "publish to irc: [$channel] $nick : $message"; 
    7066 
    71     $self->nicknames->{$channel} ||= {}; 
    72     if ($nick && !$self->nicknames->{$channel}->{$nick}) { 
    73         $self->nicknames->{$channel}->{$nick}++; 
    74         $self->ircd->yield( add_spoofed_nick => { nick => $nick } ); 
    75         $self->ircd->yield( daemon_cmd_join => $nick, $channel ); 
     67    self->nicknames->{$channel} ||= {}; 
     68    if ($nick && !self->nicknames->{$channel}->{$nick}) { 
     69        self->nicknames->{$channel}->{$nick}++; 
     70        self->ircd->yield( add_spoofed_nick => { nick => $nick } ); 
     71        self->ircd->yield( daemon_cmd_join => $nick, $channel ); 
    7672    } 
    7773 
    78     #$message = encode( $self->config->{client_encoding}, $message ); 
     74    #$message = encode( self->config->{client_encoding}, $message ); 
    7975 
    80     $self->ircd->yield( daemon_cmd_privmsg => $nick => $channel, $_ ) 
     76    self->ircd->yield( daemon_cmd_privmsg => $nick => $channel, $_ ) 
    8177        for split /\r?\n/, $message; 
    8278}; 
    8379 
    8480event publish_notice => sub { 
    85     my $self = shift; 
    86     my ($channel, $message) = get_args(@_); 
     81    my ($channel, $message) = get_args; 
    8782 
    8883    debug "notice to irc: [$channel] $message"; 
    8984 
    90     #$message = encode( $self->config->{client_encoding}, $message ); 
     85    #$message = encode( self->config->{client_encoding}, $message ); 
    9186 
    92     $self->ircd->yield( daemon_cmd_notice => $self->config->{server_nick} => $channel, $_ ) 
     87    self->ircd->yield( daemon_cmd_notice => self->config->{server_nick} => $channel, $_ ) 
    9388        for split /\r?\n/, $message; 
    9489}; 
    9590 
    9691event join_channel => sub { 
    97     my $self = shift; 
    98     my ($channel, $component) = get_args(@_); 
     92    my ($channel, $component) = get_args; 
    9993 
    10094    debug "join channel: $channel"; 
    10195    debug "register: $channel => $component"; 
    10296 
    103     $self->components->{$channel} = $component; 
    104     $self->ircd->yield( daemon_cmd_join => $self->config->{server_nick}, $channel ); 
     97    self->components->{$channel} = $component; 
     98    self->ircd->yield( daemon_cmd_join => self->config->{server_nick}, $channel ); 
    10599}; 
    106100 
  • lang/perl/XIRCD/trunk/lib/XIRCD/Component/Time.pm

    r11669 r11679  
    1919 
    2020sub START { 
    21     my $self = shift; 
    22  
    23     $self->alias('time'); 
     21    self->alias('time'); 
    2422 
    2523    debug 'start time'; 
    2624 
    27     POE::Kernel->post( ircd => 'join_channel', $self->config->{channel} ); 
    28     $self->yield('timecall'); 
     25    POE::Kernel->post( ircd => 'join_channel', self->config->{channel} ); 
     26    self->yield('timecall'); 
    2927} 
    3028 
    3129event timecall => sub { 
    32     my $self = shift; 
    33  
    3430    debug "timecall"; 
    3531 
    36     POE::Kernel->post( ircd => 'publish_message' => 'time', $self->config->{channel}, $self->date->strftime("%Y/%m/%d %H:%M:%S") ); 
    37     POE::Kernel->delay('timecall', 10); 
     32    POE::Kernel->post( 
     33        ircd => 'publish_message' => 'time', 
     34        self->config->{channel}, self->date->strftime("%Y/%m/%d %H:%M:%S") 
     35    ); 
     36    POE::Kernel->delay( 'timecall', 10 ); 
    3837}; 
    3938 
  • lang/perl/XIRCD/trunk/lib/XIRCD/Component/Twitter.pm

    r11669 r11679  
    2525    my $call = shift; 
    2626 
    27     my $self = $call->(@_); 
     27    my self = $call->(@_); 
    2828 
    2929    POE::Component::Client::HTTP->spawn( 
    3030        Agent => 'xircd_component_twitter/0.1', 
    31         Alias => $self->http_alias, 
     31        Alias => self->http_alias, 
    3232    ); 
    3333 
    34     $self->config->{apiurl}   ||= 'http://twitter.com/statuses'; 
    35     $self->config->{apihost}  ||= 'twitter.com:80'; 
    36     $self->config->{apirealm} ||= 'Twitter API'; 
    37     $self->config->{alias}    ||= 'twitter'; 
     34    self->config->{apiurl}   ||= 'http://twitter.com/statuses'; 
     35    self->config->{apihost}  ||= 'twitter.com:80'; 
     36    self->config->{apirealm} ||= 'Twitter API'; 
     37    self->config->{alias}    ||= 'twitter'; 
    3838 
    39     return $self; 
     39    return self; 
    4040}; 
    4141 
    4242sub START { 
    43     my $self = shift; 
    44  
    45     $self->alias('twitter'); 
     43    self->alias('twitter'); 
    4644 
    4745    debug "start twitter"; 
    4846 
    49     POE::Kernel->post( ircd => 'join_channel', $self->config->{channel}, $self->alias ); 
    50     $self->yield('read_twitter_friend_timeline'); 
     47    POE::Kernel->post( ircd => 'join_channel', self->config->{channel}, self->alias ); 
     48    self->yield('read_twitter_friend_timeline'); 
    5149} 
    5250 
    5351event send_message => sub { 
    54     my $self = shift; 
    55     my ($status,) = get_args(@_); 
     52    my ($status,) = get_args; 
    5653 
    5754    my $req = HTTP::Request::Common::POST( 
    58         $self->config->{apiurl} . '/update.json', 
     55        self->config->{apiurl} . '/update.json', 
    5956        [ status => encode('utf-8',$status) ], 
    6057    );   
    61     $req->authorization_basic($self->config->{twitter}->{username}, $self->config->{twitter}->{password}); 
     58    $req->authorization_basic(self->config->{twitter}->{username}, self->config->{twitter}->{password}); 
    6259 
    63     POE::Kernel->post($self->http_alias => request => 'http_response', $req); 
     60    POE::Kernel->post(self->http_alias => request => 'http_response', $req); 
    6461}; 
    6562 
    6663event read_twitter_friend_timeline => sub { 
    67     my $self = shift; 
    68  
    6964    debug "read twitter"; 
    7065 
    71     my $uri = URI->new($self->config->{apiurl} . '/friends_timeline.json'); 
    72     $uri->query_form(since => HTTP::Date::time2str($self->since)) if $self->since; 
    73     $self->since(time); 
     66    my $uri = URI->new(self->config->{apiurl} . '/friends_timeline.json'); 
     67    $uri->query_form(since => HTTP::Date::time2str(self->since)) if self->since; 
     68    self->since(time); 
    7469 
    7570    my $req = HTTP::Request->new(GET => $uri); 
    76     $req->authorization_basic($self->config->{twitter}->{username}, $self->config->{twitter}->{password}); 
     71    $req->authorization_basic(self->config->{twitter}->{username}, self->config->{twitter}->{password}); 
    7772 
    78     POE::Kernel->post($self->http_alias => request => 'http_response', $req); 
     73    POE::Kernel->post(self->http_alias => request => 'http_response', $req); 
    7974}; 
    8075 
    8176event http_response => sub { 
    82     my $self = shift; 
    83     my ($request_packet, $response_packet) = get_args(@_); 
     77    my ($request_packet, $response_packet) = get_args; 
    8478 
    8579    my $request  = $request_packet->[0]; 
     
    8983    if ($uri =~ /update.json/) { 
    9084        unless ($response->is_success) { 
    91             $self->yield(response_error => $response); 
     85            self->yield(response_error => $response); 
    9286            return; 
    9387        } 
    94         $self->yield(update_success => $response); 
     88        self->yield(update_success => $response); 
    9589    } elsif ($uri =~ /friends_timeline.json/) { 
    96         $self->yield(friend_timeline_success => $response); 
     90        self->yield(friend_timeline_success => $response); 
    9791    } 
    9892}; 
    9993 
    10094event friend_timeline_success => sub { 
    101     my $self = shift; 
    102  
    10395    debug "get friend timeline"; 
    104     my ( $response, ) = get_args(@_); 
     96    my ( $response, ) = get_args; 
    10597 
    10698    if ( $response->is_success ) { 
     
    110102                ircd => publish_message =>  
    111103                    $line->{user}->{screen_name}, 
    112                     $self->config->{channel},  
     104                    self->config->{channel},  
    113105                    $line->{text}, 
    114106            ); 
     
    116108    } 
    117109 
    118     POE::Kernel->delay('read_twitter_friend_timeline', $self->config->{twitter}->{retry}); 
     110    POE::Kernel->delay('read_twitter_friend_timeline', self->config->{twitter}->{retry}); 
    119111}; 
    120112 
    121113event update_success => sub { 
    122     my $self = shift; 
    123  
    124     my ( $response, ) = get_args(@_); 
     114    my ( $response, ) = get_args; 
    125115 
    126116    if ( $response->is_success ) { 
    127117        my $ret = JSON::Any->jsonToObj($response->content); 
    128         POE::Kernel->post( ircd => publish_notice => $self->config->{channel}, $ret->{text} ); 
     118        POE::Kernel->post( ircd => publish_notice => self->config->{channel}, $ret->{text} ); 
    129119    } 
    130120}; 
  • lang/perl/XIRCD/trunk/lib/XIRCD/Component/Wassr.pm

    r11669 r11679  
    3131 
    3232sub START { 
    33     my $self = shift; 
    34  
    35     $self->alias('wassr'); 
     33    self->alias('wassr'); 
    3634 
    3735    debug "start wassr"; 
    3836 
    39     my ($username, $hostname) = split '@', $self->config->{jabber}->{username}; 
     37    my ($username, $hostname) = split '@', self->config->{jabber}->{username}; 
    4038 
    41     $self->jabber( 
     39    self->jabber( 
    4240        POE::Component::Jabber->new( 
    43             IP       => $self->config->{jabber}->{server}, 
    44             Port     => $self->config->{jabber}->{port} || 5222, 
     41            IP       => self->config->{jabber}->{server}, 
     42            Port     => self->config->{jabber}->{port} || 5222, 
    4543            Hostname => $hostname, 
    4644            Username => $username, 
    47             Password => $self->config->{jabber}->{password}, 
     45            Password => self->config->{jabber}->{password}, 
    4846            Alias    => 'jabber', 
    4947            States   => { 
     
    5654    ); 
    5755 
    58     POE::Kernel->post( ircd => 'join_channel', $self->config->{channel}, $self->alias ); 
     56    POE::Kernel->post( ircd => 'join_channel', self->config->{channel}, self->alias ); 
    5957    POE::Kernel->post( jabber => 'connect' ); 
    6058} 
    6159 
    6260event status_handler => sub { 
    63     my $self = shift; 
    64     my ($state,) = get_args(@_); 
     61    my ($state,) = get_args; 
    6562 
    6663    if ($state == +PCJ_INIT_FINISHED) { 
    6764        debug "init finished"; 
    68         $self->jid($self->jabber->jid); 
     65        self->jid(self->jabber->jid); 
    6966 
    7067        POE::Kernel->post(jabber => 'output_handler', POE::Filter::XML::Node->new('presence')); 
     
    7471 
    7572event input_handler => sub { 
    76     my $self = shift; 
    77     my ($node,) = get_args(@_); 
     73    my self = shift; 
     74    my ($node,) = get_args; 
    7875 
    7976    debug "recv:", $node->to_str; 
     
    8481        my ($nick, $text) = $body->data =~ /^([A-Za-z0-9_.-]+): (.*)/s; 
    8582        if ($nick && $text) { 
    86             POE::Kernel->post( ircd => 'publish_message', $nick, $self->config->{channel}, $text ); 
     83            POE::Kernel->post( ircd => 'publish_message', $nick, self->config->{channel}, $text ); 
    8784        } else { 
    88             POE::Kernel->post( ircd => 'publish_notice', $self->config->{channel}, $body->data ); 
     85            POE::Kernel->post( ircd => 'publish_notice', self->config->{channel}, $body->data ); 
    8986        } 
    9087    } 
     
    9289 
    9390event send_message => sub { 
    94     my $self = shift; 
    95     my ($message,) = get_args(@_); 
     91    my ($message,) = get_args; 
    9692 
    9793    my $node = POE::Filter::XML::Node->new('message'); 
    9894 
    9995    $node->attr('to', 'wassr-bot@wassr.jp'); 
    100     $node->attr('from', $self->{jid} ); 
     96    $node->attr('from', self->{jid} ); 
    10197    $node->attr('type', 'chat'); 
    10298    $node->insert_tag('body')->data( $message ); 
     
    108104 
    109105event error_handler => sub { 
    110     my $self = shift; 
    111     my ($error,) = get_args(@_); 
     106    my ($error,) = get_args; 
    112107 
    113108    if ( $error == +PCJ_SOCKETFAIL or $error == +PCJ_SOCKETDISCONNECT or $error == +PCJ_CONNECTFAIL ) {