Changeset 2362 for lang/perl/OtohaChan

Show
Ignore:
Timestamp:
12/03/07 22:24:33 (5 years ago)
Author:
yusukebe
Message:

add regexp

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/OtohaChan/trunk/otohachan.pl

    r2356 r2362  
    55use POE qw(Component::IRC); 
    66use YAML; 
     7use IO::File; 
    78 
    89my $config = YAML::LoadFile("config.yaml"); 
    910my @channels = @{$config->{channels}}; 
     11my $regexp_filename = "regexp.yaml"; 
     12my $regexp_patterns = YAML::LoadFile($regexp_filename); 
    1013 
    1114my $irc = POE::Component::IRC->spawn( 
    12     nick     => $config->{nickname}, 
    13     server   => $config->{ircserver}, 
    14     port     => $config->{port}, 
    15     password => $config->{password}, 
     15    nick     => $config->{nickname} || "OtohaChan", 
     16    server   => $config->{ircserver} || "irc.freenode.net", 
     17    port     => $config->{port} || "6667", 
     18    password => $config->{password} || "", 
    1619) or die "Oh noooo! $!"; 
    1720 
     
    4447    my ( $kernel, $sender, $who, $where, $what ) = 
    4548      @_[ KERNEL, SENDER, ARG0, ARG1, ARG2 ]; 
    46     $kernel->post( $sender => privmsg => $_ => "\x0314$what\x03" ) 
     49    $kernel->post( $sender => privmsg => $_ => "\x0313$what\x03" ) 
    4750      for @channels; 
    4851    undef; 
     
    5356      @_[ KERNEL, SENDER, ARG0, ARG1, ARG2 ]; 
    5457    my $nick = ( split /!/, $who )[0]; 
     58 
    5559    my $channel = $where->[0]; 
     60    if ( $what =~ /^oregexp (.*) (.*)/ ) { 
     61        _regexp_regist($2,$1); 
     62    } 
    5663    if ( $what =~ /OtohaChan/i ) { 
    5764        $kernel->post( 
    58             $sender => privmsg => $channel => "\x0314ホットホット\x03" ); 
     65            $sender => privmsg => $channel => "\x0313ホットホット\x03" ); 
    5966    } 
     67    my $str = _regexp_conv($what); 
     68    $kernel->post( 
     69                  $sender => privmsg => $channel => "\x0313$str\x03") if $str; 
     70 
    6071    undef; 
     72} 
     73 
     74sub _regexp_conv { 
     75    my ($str) = @_; 
     76    foreach my $pattern (@$regexp_patterns) { 
     77        if ( $str =~ /$pattern->{match}/ ) { 
     78            $str =~ s/$pattern->{match}/$pattern->{after}/; 
     79            return $str; 
     80        } 
     81    } 
     82    return 0; 
     83} 
     84 
     85sub _regexp_regist { 
     86    my ($match, $after) = @_; 
     87    return if grep { $_->{match} eq $match } @$regexp_patterns; 
     88    my $io = IO::File->new($regexp_filename, "a"); 
     89    print $io Dump([{ match=> $match, after=> $after}]); 
     90    $io->close; 
     91    $regexp_patterns = YAML::LoadFile($regexp_filename); 
     92} 
     93 
     94sub _regexp_unregist { 
     95    my ($match) = @_; 
     96    my @new_patterns; 
     97    foreach (grep { $_->{match} ne $match } @$regexp_patterns){ 
     98        push(@new_patterns, $_); 
     99    } 
     100    my $io = IO::File->new($regexp_filename, "w"); 
     101    print $io Dump( @new_patterns ); 
     102    $io->close; 
     103    $regexp_patterns = YAML::LoadFile($regexp_filename); 
    61104} 
    62105