]> git.vpit.fr Git - perl/scripts/xchat.git/blobdiff - opers.pl
Make sure local_context() runs the code only when the context was found
[perl/scripts/xchat.git] / opers.pl
index 2500f83441e09f1bce7fb2d87c34efba16550ba5..f8d178d59e3d870318c0943976bf487fdc01036b 100755 (executable)
--- a/opers.pl
+++ b/opers.pl
@@ -3,69 +3,86 @@ package Xchat::VPIT::Opers;
 use strict;
 use warnings;
 
-use Xchat qw/:all/;
+use Xchat qw<:all>;
 
 use lib get_info 'xchatdir';
 use Xchat::XPI;
+use Xchat::XPI::Utils qw<irc_lc save_context>;
 
 our $VERSION = '0.03';
 
 my ($ph, %opers);
 
-sub print_op { print $ph "\002$_[0]\002 ($_[1]) \002is an IRC operator\n"; 1 }
+sub print_op { print $ph "\002$_[0]\002 ($_[1]) \002is an IRC operator\n" }
 
 hook_server '352', sub {
  return EAT_NONE unless 0 <= rindex $_[0][8], '*';
+
  my $serv = get_info 'server';
  my $nick = $_[0][7];
- if (!$opers{$serv}{$nick}) {
-  $opers{$serv}{$nick} = 1;
-  my $host = $_[0][4].'@'.$_[0][5];
-  my %chans;
-  push @{$chans{$_->{type}}}, $_ for grep $_->{server} eq $serv,
-                                      get_list 'channels';
-  my $oldctxt = get_context;
-  my $onachan = grep $_, map {
-       set_context $_->{context}
-   and user_info $nick
-   and print_op $nick, $host
-  } @{$chans{2}};
-  do {
-       set_context $_->{context}
-   and print_op $nick, $host
-  } for ($onachan ? () : @{$chans{1}}),
-                         grep !nickcmp($_->{channel}, $nick), @{$chans{3}};
-  set_context $oldctxt;
+ return EAT_NONE if $opers{$serv}{irc_lc($nick)};
+ $opers{$serv}{irc_lc($nick)} = $nick;
+
+ my $host = $_[0][4] . '@' . strip_code $_[0][5];
+ my %chans;
+ push @{$chans{$_->{type}}}, $_ for grep $_->{server} eq $serv,
+                                     get_list 'channels';
+
+ my $guard = save_context;
+
+ my $onachan;
+ for (@{$chans{2}}) {
+  set_context $_->{context} or next;
+  user_info   $nick         or next;
+  print_op    $nick, $host;
+  ++$onachan;
+ }
+
+ my @opchans;
+ @opchans = @{$chans{1}} unless $onachan;
+ push @opchans, grep !nickcmp($_->{channel}, $nick), @{$chans{3}};
+ for (@opchans) {
+  set_context $_->{context} or next;
+  print_op $nick, $host;
  }
+
  return EAT_NONE;
 };
 
 hook_print 'Disconnected', sub {
- my %servers = map { $_->{server} => 1 }
-                grep { $_->{flags} & 9 && not $_->{flags} & 2 }
-                 get_list 'channels';
- delete $opers{$_} for grep { !$servers{$_} } keys %opers;
+ my %servers;
+ $servers{$_->{server}} = 1 for grep +($_->{flags} & 0b1011) == 0b1001,
+                                 get_list 'channels';
+
+ delete @opers{grep !$servers{$_}, keys %opers};
+
  return EAT_NONE;
 };
 
 hook_server 'QUIT', sub {
  my ($nick) = $_[0][0] =~ /^:([^!]+)!/;
+
  my $serv = get_info 'server';
- delete $opers{$serv}{$nick};
+ delete $opers{$serv}{irc_lc($nick)};
+
  return EAT_NONE;
 };
 
 sub nick_cb {
  my ($old, $new) = @{$_[0]};
+
+ $old = irc_lc $old;
+
  my $ops = $opers{get_info 'server'};
- if ($ops && $ops->{$old}) {
-  $ops->{$new} = 1;
+ if ($ops and $ops->{$old}) {
+  $ops->{irc_lc($new)} = $new;
   delete $ops->{$old};
  }
+
  return EAT_NONE;
 }
 
-hook_print $_, \&nick_cb for ('Change Nick', 'Your Nick Changing');
+hook_print $_, \&nick_cb for 'Change Nick', 'Your Nick Changing';
 
 hook_command 'OPSCAN', sub {
  my $serv = get_info 'server';
@@ -73,33 +90,46 @@ hook_command 'OPSCAN', sub {
   print $ph "No information for this network yet\n";
   return EAT_ALL;
  }
- my ($chan, $isnet) = (get_info('channel'), 0);
+
+ my $chan  = get_info 'channel';
+ my $isnet = 0;
  for (get_list 'channels') {
-  if ($_->{type} eq 1 && $_->{channel} eq $chan) {
+  if ($_->{type} == 1 and $_->{channel} eq $chan) {
    $isnet = 1;
    last;
   }
  }
- my ($name, @ops) = ($isnet) ? ('network', keys %{$opers{$serv}})
-                             : ('channel', map $_->{nick},
-                                            grep $opers{$serv}{$_->{nick}},
-                                             get_list 'users');
+
+ my ($name, @ops);
+ if ($isnet) {
+  $name = 'network';
+  @ops  = values %{$opers{$serv}};
+ } else {
+  $name = 'channel';
+  @ops  = map $_->{nick},
+           grep $opers{$serv}{irc_lc($_->{nick})},
+            get_list 'users';
+ }
+
  if (@ops) {
-  print $ph 'IRC operators on this '.$name." : \002@ops\n";
+  print $ph "IRC operators on this $name : \002@ops\n";
  } else {
-  print $ph 'No IRC operators on this '.$name."\n";
+  print $ph "No IRC operators on this $name\n";
  }
+
  return EAT_ALL;
 }, {
  help_text => 'OPSCAN, scan for IRC operators in the current channel'
 };
 
-$ph = new Xchat::XPI name   => 'Operators',
-                     tag    => 'Opers',
-                     desc   => 'IRC operators scanner',
-                     author => 'Vincent Pit (VPIT)',
-                     email  => 'perl@profvince.com',
-                     url    => 'http://www.profvince.com',
-                     unload => sub { undef %opers; };
+$ph = Xchat::XPI->new(
+ name   => 'Operators',
+ tag    => 'Opers',
+ desc   => 'IRC operators scanner',
+ author => 'Vincent Pit (VPIT)',
+ email  => 'perl@profvince.com',
+ url    => 'http://www.profvince.com',
+ unload => sub { undef %opers },
+);
 
 1;