X-Git-Url: http://git.ieval.ro/?a=blobdiff_plain;f=lib%2FApp%2FFonBot%2FPlugin%2FIRC.pm;fp=lib%2FApp%2FFonBot%2FPlugin%2FIRC.pm;h=ec5279e8cf17fbf2572142c7481b471f138ddf7e;hb=962dff7b76ec32cd68e0e96b8d3cc6d1f9759d00;hp=e4eaf01e55e424dfb2ffe92cdd49e03881436f17;hpb=7be318a6bf7a8f960c4192f52e7e5c8763d002ee;p=app-fonbot-daemon.git diff --git a/lib/App/FonBot/Plugin/IRC.pm b/lib/App/FonBot/Plugin/IRC.pm index e4eaf01..ec5279e 100644 --- a/lib/App/FonBot/Plugin/IRC.pm +++ b/lib/App/FonBot/Plugin/IRC.pm @@ -22,128 +22,128 @@ use App::FonBot::Plugin::Common; my %selves; sub init{ - my ($ns)=@_; + my ($ns)=@_; - my $self=$ns->new; - $self->{log}->info("initializing $ns"); - tie my %nick_to_username, DB_File => "nick_to_username-$ns.db"; - $self->{nick_to_username}=\%nick_to_username; - $selves{$ns}=$self + my $self=$ns->new; + $self->{log}->info("initializing $ns"); + tie my %nick_to_username, DB_File => "nick_to_username-$ns.db"; + $self->{nick_to_username}=\%nick_to_username; + $selves{$ns}=$self } sub fini{ - my ($ns)=@_; + my ($ns)=@_; - $selves{$ns}->{log}->info("finishing $ns"); - $selves{$ns}->{irc}->yield(shutdown => "finishing $ns") if defined $selves{$ns}->{irc}; - untie $selves{$ns}->{nick_to_username}; - POE::Kernel->post($selves{$ns}->{session} => 'shutdown'); - delete $selves{$ns} + $selves{$ns}->{log}->info("finishing $ns"); + $selves{$ns}->{irc}->yield(shutdown => "finishing $ns") if defined $selves{$ns}->{irc}; + untie $selves{$ns}->{nick_to_username}; + POE::Kernel->post($selves{$ns}->{session} => 'shutdown'); + delete $selves{$ns} } ################################################## sub new{ - my ($ns)=@_; + my ($ns)=@_; - my $self = { - prefix => {}, - log => Log::Log4perl->get_logger($ns), - }; + my $self = { + prefix => {}, + log => Log::Log4perl->get_logger($ns), + }; - bless $self, $ns; + bless $self, $ns; - $self->{session} = POE::Session->create( - object_states => [ $self => [ '_start', 'send_message', 'irc_001', 'irc_msg', 'irc_public', 'shutdown' ] ], - ); + $self->{session} = POE::Session->create( + object_states => [ $self => [ '_start', 'send_message', 'irc_001', 'irc_msg', 'irc_public', 'shutdown' ] ], + ); - $self + $self } sub irc_msg{ - my ($from, $msg, $self)=@_[ARG0,ARG2,OBJECT]; - my $nick=parse_user $from; + my ($from, $msg, $self)=@_[ARG0,ARG2,OBJECT]; + my $nick=parse_user $from; - my $username=$self->{nick_to_username}{$from}; - my $address=$_[KERNEL]->alias_list; - $address.=" $nick"; + my $username=$self->{nick_to_username}{$from}; + my $address=$_[KERNEL]->alias_list; + $address.=" $nick"; - chomp $msg; - my @args=shellwords $msg; - my $cmd=shift @args; + chomp $msg; + my @args=shellwords $msg; + my $cmd=shift @args; - given($cmd){ - when(/^myid$/i){ - $self->{irc}->yield(privmsg => $nick, $from); - } + given($cmd){ + when(/^myid$/i){ + $self->{irc}->yield(privmsg => $nick, $from); + } - when(/^login$/i) { - my ($user, $pass) = @args; + when(/^login$/i) { + my ($user, $pass) = @args; - eval { pwcheck $user, $pass }; + eval { pwcheck $user, $pass }; - if ($@) { - $self->{log}->debug("Login for $user failed"); - $self->{irc}->yield(privmsg => $nick, 'Bad username/password combination'); - } else { - $self->{log}->debug("Login for $user succeded"); - $self->{nick_to_username}{$from} = $user; - $self->{irc}->yield(privmsg => $nick, "Logged in as $user"); - } - } + if ($@) { + $self->{log}->debug("Login for $user failed"); + $self->{irc}->yield(privmsg => $nick, 'Bad username/password combination'); + } else { + $self->{log}->debug("Login for $user succeded"); + $self->{nick_to_username}{$from} = $user; + $self->{irc}->yield(privmsg => $nick, "Logged in as $user"); + } + } - when(/^logout$/i){ - delete $self->{nick_to_username}{$from}; - } + when(/^logout$/i){ + delete $self->{nick_to_username}{$from}; + } - when(/^prefix$/i){ - if (defined $username) { - $self->{prefix}{$username} = [@args]; - } else { - $self->{irc}->yield(privmsg => $nick, 'You are not logged in. Say "login your_username your_password" (where your_username and your_password are your login credentials) to login.'); - } - } - - when(/^noprefix$/i){ - if (defined $username) { - delete $self->{prefix}{$username} - } else { - $self->{irc}->yield(privmsg => $nick, 'You are not logged in. Say "login your_username your_password" (where your_username and your_password are your login credentials) to login.'); - } - } - - default { - if (defined $username) { - $ok_user_addresses{"$username $address"}=1; - $self->{log}->debug("Command $cmd @args from $username"); - if (exists $self->{prefix}{$username}) { - sendmsg $username, undef, $address, @{$self->{prefix}{$username}}, $cmd, @args; - } else { - sendmsg $username, undef, $address, $cmd, @args; + when(/^prefix$/i){ + if (defined $username) { + $self->{prefix}{$username} = [@args]; + } else { + $self->{irc}->yield(privmsg => $nick, 'You are not logged in. Say "login your_username your_password" (where your_username and your_password are your login credentials) to login.'); + } } - } else { - $self->{irc}->yield(privmsg => $nick, 'You are not logged in. Say "login your_username your_password" (where your_username and your_password are your login credentials) to login.'); - } - } - } + when(/^noprefix$/i){ + if (defined $username) { + delete $self->{prefix}{$username} + } else { + $self->{irc}->yield(privmsg => $nick, 'You are not logged in. Say "login your_username your_password" (where your_username and your_password are your login credentials) to login.'); + } + } + + default { + if (defined $username) { + $ok_user_addresses{"$username $address"}=1; + $self->{log}->debug("Command $cmd @args from $username"); + if (exists $self->{prefix}{$username}) { + sendmsg $username, undef, $address, @{$self->{prefix}{$username}}, $cmd, @args; + } else { + sendmsg $username, undef, $address, $cmd, @args; + } + } else { + $self->{irc}->yield(privmsg => $nick, 'You are not logged in. Say "login your_username your_password" (where your_username and your_password are your login credentials) to login.'); + } + } + + } } sub irc_public{ - # Do nothing + # Do nothing } sub irc_001{ - # Do nothing + # Do nothing } sub send_message{ - my ($self, $address, $content)=@_[OBJECT, ARG0, ARG1]; - $self->{irc}->yield(privmsg => $address, $_) for map {unpack '(A400)*'} split "\n", $content + my ($self, $address, $content)=@_[OBJECT, ARG0, ARG1]; + $self->{irc}->yield(privmsg => $address, $_) for map {unpack '(A400)*'} split "\n", $content } sub shutdown{ - $_[KERNEL]->alias_remove($_) for $_[KERNEL]->alias_list; + $_[KERNEL]->alias_remove($_) for $_[KERNEL]->alias_list; } sub _start { ... }