Changeset 2362 for lang/perl/OtohaChan
- Timestamp:
- 12/03/07 22:24:33 (5 years ago)
- Files:
-
- 1 modified
-
lang/perl/OtohaChan/trunk/otohachan.pl (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/perl/OtohaChan/trunk/otohachan.pl
r2356 r2362 5 5 use POE qw(Component::IRC); 6 6 use YAML; 7 use IO::File; 7 8 8 9 my $config = YAML::LoadFile("config.yaml"); 9 10 my @channels = @{$config->{channels}}; 11 my $regexp_filename = "regexp.yaml"; 12 my $regexp_patterns = YAML::LoadFile($regexp_filename); 10 13 11 14 my $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} || "", 16 19 ) or die "Oh noooo! $!"; 17 20 … … 44 47 my ( $kernel, $sender, $who, $where, $what ) = 45 48 @_[ KERNEL, SENDER, ARG0, ARG1, ARG2 ]; 46 $kernel->post( $sender => privmsg => $_ => "\x031 4$what\x03" )49 $kernel->post( $sender => privmsg => $_ => "\x0313$what\x03" ) 47 50 for @channels; 48 51 undef; … … 53 56 @_[ KERNEL, SENDER, ARG0, ARG1, ARG2 ]; 54 57 my $nick = ( split /!/, $who )[0]; 58 55 59 my $channel = $where->[0]; 60 if ( $what =~ /^oregexp (.*) (.*)/ ) { 61 _regexp_regist($2,$1); 62 } 56 63 if ( $what =~ /OtohaChan/i ) { 57 64 $kernel->post( 58 $sender => privmsg => $channel => "\x031 4ホットホット\x03" );65 $sender => privmsg => $channel => "\x0313ホットホット\x03" ); 59 66 } 67 my $str = _regexp_conv($what); 68 $kernel->post( 69 $sender => privmsg => $channel => "\x0313$str\x03") if $str; 70 60 71 undef; 72 } 73 74 sub _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 85 sub _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 94 sub _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); 61 104 } 62 105
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)