From f2deae219d47ed62ff870731acad7831263562d7 Mon Sep 17 00:00:00 2001 From: Elijah Perrault Date: Fri, 18 Mar 2011 14:34:02 -0600 Subject: [PATCH] Started State::IRC, and moved chanusers to it. --- doc/CHANGELOG | 2 ++ lib/API/Std.pm | 8 ++--- lib/Core/IRC.pm | 6 ++-- lib/Core/IRC/Users.pm | 10 +++---- lib/Proto/IRC.pm | 70 +++++++++++-------------------------------- lib/State/IRC.pm | 49 ++++++++++++++++++++++++++++++ 6 files changed, 81 insertions(+), 64 deletions(-) create mode 100644 lib/State/IRC.pm diff --git a/doc/CHANGELOG b/doc/CHANGELOG index 321e219..6e6d678 100644 --- a/doc/CHANGELOG +++ b/doc/CHANGELOG @@ -3,6 +3,8 @@ Auto IRC Bot 3.0: Change Log 3.0 Indev =============================================================================== + * Moved chanusers to State::IRC. + * Created State::IRC. * UNO now keeps game duration and cards played count. * Added a LOLCAT module for translating English to LOLCAT. * EightBall module rewritten. diff --git a/lib/API/Std.pm b/lib/API/Std.pm index 3ef0bdc..1b4e058 100644 --- a/lib/API/Std.pm +++ b/lib/API/Std.pm @@ -375,15 +375,15 @@ sub match_user { my $svr = $ulhp{net}[0]; if (defined $Auto::SOCKET{$svr}) { if ($ccnm eq 'CURRENT' and defined $user{chan}) { - if (defined $Proto::IRC::chanusers{$svr}{$user{chan}}{$user{nick}}) { - if ($Proto::IRC::chanusers{$svr}{$user{chan}}{$user{nick}} =~ m/($ccst)/sm) { return $userkey; } ## no critic qw(RegularExpressions::RequireExtendedFormatting) + if (defined $State::IRC::chanusers{$svr}{$user{chan}}{$user{nick}}) { + if ($State::IRC::chanusers{$svr}{$user{chan}}{$user{nick}} =~ m/($ccst)/sm) { return $userkey; } ## no critic qw(RegularExpressions::RequireExtendedFormatting) } } else { foreach my $bcj (keys %{ $Proto::IRC::botchans{$svr} }) { if (API::IRC::match_mask($bcj, $ccnm)) { - if (defined $Proto::IRC::chanusers{$svr}{$bcj}{$user{nick}}) { - if ($Proto::IRC::chanusers{$svr}{$bcj}{$user{nick}} =~ m/($ccst)/sm) { return $userkey; } ## no critic qw(RegularExpressions::RequireExtendedFormatting) + if (defined $State::IRC::chanusers{$svr}{$bcj}{$user{nick}}) { + if ($State::IRC::chanusers{$svr}{$bcj}{$user{nick}} =~ m/($ccst)/sm) { return $userkey; } ## no critic qw(RegularExpressions::RequireExtendedFormatting) } } } diff --git a/lib/Core/IRC.pm b/lib/Core/IRC.pm index 37c0ae1..f3502f9 100644 --- a/lib/Core/IRC.pm +++ b/lib/Core/IRC.pm @@ -32,8 +32,8 @@ hook_add("on_quit", "quit_update_chanusers", sub { my %src = %{ $src }; # Delete the user from all channels. - foreach my $ccu (keys %{ $Proto::IRC::chanusers{$src{svr}}}) { - if (defined $Proto::IRC::chanusers{$src{svr}}{$ccu}{$src{nick}}) { delete $Proto::IRC::chanusers{$src{svr}}{$ccu}{$src{nick}}; } + foreach my $ccu (keys %{ $State::IRC::chanusers{$src{svr}}}) { + if (defined $State::IRC::chanusers{$src{svr}}{$ccu}{$src{nick}}) { delete $State::IRC::chanusers{$src{svr}}{$ccu}{$src{nick}}; } } return 1; @@ -196,7 +196,7 @@ hook_add('on_disconnect', 'core.irc.deldata', sub { if (defined $Proto::IRC::got_001{$svr}) { delete $Proto::IRC::got_001{$svr} } if (defined $Proto::IRC::botinfo{$svr}) { delete $Proto::IRC::botinfo{$svr} } if (defined $Proto::IRC::botchans{$svr}) { delete $Proto::IRC::botchans{$svr} } - if (defined $Proto::IRC::chanusers{$svr}) { delete $Proto::IRC::chanusers{$svr} } + if (defined $State::IRC::chanusers{$svr}) { delete $State::IRC::chanusers{$svr} } if (defined $Proto::IRC::csprefix{$svr}) { delete $Proto::IRC::csprefix{$svr} } if (defined $Proto::IRC::chanmodes{$svr}) { delete $Proto::IRC::chanmodes{$svr} } if (defined $Proto::IRC::cap{$svr}) { delete $Proto::IRC::cap{$svr} } diff --git a/lib/Core/IRC/Users.pm b/lib/Core/IRC/Users.pm index baf7c41..b840310 100644 --- a/lib/Core/IRC/Users.pm +++ b/lib/Core/IRC/Users.pm @@ -29,7 +29,7 @@ hook_add('on_namesreply', 'ircusers.names', sub { my ($svr, $chan, undef) = @_; # Iterate through the users for this channel. - foreach (keys %{$Proto::IRC::chanusers{$svr}{$chan}}) { + foreach (keys %{$State::IRC::chanusers{$svr}{$chan}}) { # Check if the user already exists. if (!$users{$svr}{$_}) { # They do not; WHO them. @@ -77,9 +77,9 @@ hook_add('on_kick', 'ircusers.onkick', sub { if ($users{$src->{svr}}{lc $user}) { # Figure out if the user is in any other channel we're in. my $ri = 0; - foreach my $chan (keys %{$Proto::IRC::chanusers{$src->{svr}}}) { + foreach my $chan (keys %{$State::IRC::chanusers{$src->{svr}}}) { if ($chan ne $kchan) { - if (defined $Proto::IRC::chanusers{$src->{svr}}{$chan}{lc $user}) { $ri++; last; } + if (defined $State::IRC::chanusers{$src->{svr}}{$chan}{lc $user}) { $ri++; last; } } } if (!$ri) { @@ -100,9 +100,9 @@ hook_add('on_part', 'ircusers.onpart', sub { if ($users{$src->{svr}}{lc $src->{nick}}) { # Figure out if the user is in any other channel we're in. my $ri = 0; - foreach my $chan (keys %{$Proto::IRC::chanusers{$src->{svr}}}) { + foreach my $chan (keys %{$State::IRC::chanusers{$src->{svr}}}) { if ($chan ne $pchan) { - if (defined $Proto::IRC::chanusers{$src->{svr}}{$chan}{lc $src->{nick}}) { $ri++; last; } + if (defined $State::IRC::chanusers{$src->{svr}}{$chan}{lc $src->{nick}}) { $ri++; last; } } } if (!$ri) { diff --git a/lib/Proto/IRC.pm b/lib/Proto/IRC.pm index 47a32d1..1850986 100644 --- a/lib/Proto/IRC.pm +++ b/lib/Proto/IRC.pm @@ -38,7 +38,7 @@ our %RAWC = ( ); # Variables for various functions. -our (%got_001, %botinfo, %botchans, %csprefix, %chanusers, %chanmodes, %cap); +our (%got_001, %botinfo, %botchans, %csprefix, %chanmodes, %cap); # Events. API::Std::event_add('on_capack'); @@ -171,43 +171,9 @@ sub num353 { # Get rid of the colon. $ex[5] =~ s/^://xsm; - # Delete the old chanusers hash if it exists. - if (defined $chanusers{$svr}{$ex[4]}) { delete $chanusers{$svr}{$ex[4]} } - # Iterate through each user. - for (5..$#ex) { - my $fi = 0; - PFITER: foreach my $spfx (keys %{ $csprefix{$svr} }) { - # Check if the user has status in the channel. - if (substr($ex[$_], 0, 1) eq $csprefix{$svr}{$spfx}) { - # He/she does. Lets set that. - if (defined $chanusers{$svr}{$ex[4]}{lc $ex[$_]}) { - # If the user has multiple statuses. - $chanusers{$svr}{$ex[4]}{lc substr $ex[$_], 1} = $chanusers{$svr}{$ex[4]}{lc $ex[$_]}.$spfx; - delete $chanusers{$svr}{$ex[4]}{lc $ex[$_]}; - } - else { - # Or not. - $chanusers{$svr}{$ex[4]}{lc substr $ex[$_], 1} = $spfx; - } - $fi = 1; - $ex[$_] = substr $ex[$_], 1; - } - } - # Check if there's still a prefix. - foreach my $spfx (keys %{$csprefix{$svr}}) { - if (substr($ex[$_], 0, 1) eq $csprefix{$svr}{$spfx}) { goto 'PFITER' } - } - # They had status, so go to the next user. - next if $fi; - # They didn't, set them as a normal user. - if (!defined $chanusers{$svr}{$ex[4]}{lc $ex[$_]}) { - $chanusers{$svr}{$ex[4]}{lc $ex[$_]} = 1; - } - } - # Trigger on_namesreply. API::Std::event_run('on_namesreply', ($svr, $ex[4], @ex[5..$#ex])); - + return 1; } @@ -392,7 +358,7 @@ sub cjoin { } else { # It isn't. Update chanusers and trigger on_rcjoin. - $chanusers{$svr}{lc $chan}{$src{nick}} = 1; + $State::IRC::chanusers{$svr}{lc $chan}{$src{nick}} = 1; $src{svr} = $svr; API::Std::event_run("on_rcjoin", (\%src, $chan)); } @@ -408,7 +374,7 @@ sub kick { $src{svr} = $svr; # Update chanusers. - delete $chanusers{$svr}{$ex[2]}{$ex[3]} if defined $chanusers{$svr}{$ex[2]}{$ex[3]}; + delete $State::IRC::chanusers{$svr}{$ex[2]}{$ex[3]} if defined $State::IRC::chanusers{$svr}{$ex[2]}{$ex[3]}; # Set $msg to the kick message. my $msg = 0; @@ -440,7 +406,7 @@ sub kick { } else { # We weren't. Update chanusers and trigger on_kick. - if (defined $chanusers{$svr}{$ex[2]}{$ex[3]}) { delete $chanusers{$svr}{$ex[2]}{$ex[3]}; } + if (defined $State::IRC::chanusers{$svr}{$ex[2]}{$ex[3]}) { delete $State::IRC::chanusers{$svr}{$ex[2]}{$ex[3]}; } API::Std::event_run("on_kick", (\%src, $ex[2], $ex[3], $msg)); } @@ -499,26 +465,26 @@ sub mode { # It is a status mode, lets parse changes. my $user = shift(@ex); - if (defined $chanusers{$svr}{$chan}{$user}) { + if (defined $State::IRC::chanusers{$svr}{$chan}{$user}) { if ($op == 1) { - if ($chanusers{$svr}{$chan}{$user} eq 1) { - $chanusers{$svr}{$chan}{$user} = $maf; + if ($State::IRC::chanusers{$svr}{$chan}{$user} eq 1) { + $State::IRC::chanusers{$svr}{$chan}{$user} = $maf; } else { - $chanusers{$svr}{$chan}{$user} .= $maf; + $State::IRC::chanusers{$svr}{$chan}{$user} .= $maf; } } elsif ($op == 2) { - if (length($chanusers{$svr}{$chan}{$user}) == 1) { - $chanusers{$svr}{$chan}{$user} = 1; + if (length($State::IRC::chanusers{$svr}{$chan}{$user}) == 1) { + $State::IRC::chanusers{$svr}{$chan}{$user} = 1; } else { - $chanusers{$svr}{$chan}{$user} =~ s/($maf)//gxsm; + $State::IRC::chanusers{$svr}{$chan}{$user} =~ s/($maf)//gxsm; } } } else { - $chanusers{$svr}{$chan}{$user} = $maf; + $State::IRC::chanusers{$svr}{$chan}{$user} = $maf; } } else { @@ -560,10 +526,10 @@ sub nick { } else { # It isn't. Update chanusers and trigger on_nick. - foreach my $chk (keys %{ $chanusers{$svr} }) { - if (defined $chanusers{$svr}{$chk}{$src{nick}}) { - $chanusers{$svr}{$chk}{$nex} = $chanusers{$svr}{$chk}{$src{nick}}; - delete $chanusers{$svr}{$chk}{$src{nick}}; + foreach my $chk (keys %{ $State::IRC::chanusers{$svr} }) { + if (defined $State::IRC::chanusers{$svr}{$chk}{$src{nick}}) { + $State::IRC::chanusers{$svr}{$chk}{$nex} = $State::IRC::chanusers{$svr}{$chk}{$src{nick}}; + delete $State::IRC::chanusers{$svr}{$chk}{$src{nick}}; } } API::Std::event_run("on_nick", (\%src, $nex)); @@ -608,7 +574,7 @@ sub part { } else { # Delete them from chanusers. - delete $chanusers{$svr}{$ex[2]}{$src{nick}} if defined $chanusers{$svr}{$ex[2]}{$src{nick}}; + delete $State::IRC::chanusers{$svr}{$ex[2]}{$src{nick}} if defined $State::IRC::chanusers{$svr}{$ex[2]}{$src{nick}}; # Set $msg to the part message. my $msg = 0; diff --git a/lib/State/IRC.pm b/lib/State/IRC.pm new file mode 100644 index 0000000..856da97 --- /dev/null +++ b/lib/State/IRC.pm @@ -0,0 +1,49 @@ +# lib/State/IRC.pm - IRC state data. +# Copyright (C) 2010-2011 Xelhua Development Group, et al. +# This program is free software; rights to this code are stated in doc/LICENSE. +package State::IRC; +use strict; +use warnings; +use API::Std qw(hook_add); +our (%chanusers); + +# Create on_namesreply hook. +hook_add('on_namesreply', 'state.irc.names', sub { + my ($svr, $chan, @data) = @_; + + # Delete the old chanusers hash if it exists. + if (defined $chanusers{$svr}{$chan}) { delete $chanusers{$svr}{$chan} } + # Iterate through each user. + for (1..$#data) { + my $fi = 0; + PFITER: foreach my $spfx (keys %{ $Proto::IRC::csprefix{$svr} }) { + # Check if the user has status in the channel. + if (substr($data[$_], 0, 1) eq $Proto::IRC::csprefix{$svr}{$spfx}) { + # He/she does. Lets set that. + if (defined $chanusers{$svr}{$chan}{lc $data[$_]}) { + # If the user has multiple statuses. + $chanusers{$svr}{$chan}{lc substr $data[$_], 1} = $chanusers{$svr}{$chan}{lc $data[$_]}.$spfx; + delete $chanusers{$svr}{$chan}{lc $data[$_]}; + } + else { + # Or not. + $chanusers{$svr}{$chan}{lc substr $data[$_], 1} = $spfx; + } + $fi = 1; + $data[$_] = substr $data[$_], 1; + } + } + # Check if there's still a prefix. + foreach my $spfx (keys %{$Proto::IRC::csprefix{$svr}}) { + if (substr($data[$_], 0, 1) eq $Proto::IRC::csprefix{$svr}{$spfx}) { goto 'PFITER' } + } + # They had status, so go to the next user. + next if $fi; + # They didn't, set them as a normal user. + if (!defined $chanusers{$svr}{$chan}{lc $data[$_]}) { + $chanusers{$svr}{$chan}{lc $data[$_]} = 1; + } + } + + return 1; +});