]> git.vpit.fr Git - perl/scripts/xchat.git/blob - opers.pl
Sanitize opers.pl
[perl/scripts/xchat.git] / opers.pl
1 package Xchat::VPIT::Opers;
2
3 use strict;
4 use warnings;
5
6 use Xchat qw/:all/;
7
8 use lib get_info 'xchatdir';
9 use Xchat::XPI;
10
11 our $VERSION = '0.03';
12
13 my ($ph, %opers);
14
15 sub print_op { print $ph "\002$_[0]\002 ($_[1]) \002is an IRC operator\n"; 1 }
16
17 hook_server '352', sub {
18  return EAT_NONE unless 0 <= rindex $_[0][8], '*';
19  my $serv = get_info 'server';
20  my $nick = $_[0][7];
21  if (!$opers{$serv}{$nick}) {
22   $opers{$serv}{$nick} = 1;
23   my $host = $_[0][4].'@'.$_[0][5];
24   my %chans;
25   push @{$chans{$_->{type}}}, $_ for grep $_->{server} eq $serv,
26                                       get_list 'channels';
27   my $oldctxt = get_context;
28   my $onachan = grep $_, map {
29        set_context $_->{context}
30    and user_info $nick
31    and print_op $nick, $host
32   } @{$chans{2}};
33   do {
34        set_context $_->{context}
35    and print_op $nick, $host
36   } for ($onachan ? () : @{$chans{1}}),
37                          grep !nickcmp($_->{channel}, $nick), @{$chans{3}};
38   set_context $oldctxt;
39  }
40  return EAT_NONE;
41 };
42
43 hook_print 'Disconnected', sub {
44  my %servers = map { $_->{server} => 1 }
45                 grep { $_->{flags} & 9 && not $_->{flags} & 2 }
46                  get_list 'channels';
47  delete $opers{$_} for grep { !$servers{$_} } keys %opers;
48  return EAT_NONE;
49 };
50
51 hook_server 'QUIT', sub {
52  my ($nick) = $_[0][0] =~ /^:([^!]+)!/;
53  my $serv = get_info 'server';
54  delete $opers{$serv}{$nick};
55  return EAT_NONE;
56 };
57
58 sub nick_cb {
59  my ($old, $new) = @{$_[0]};
60  my $ops = $opers{get_info 'server'};
61  if ($ops && $ops->{$old}) {
62   $ops->{$new} = 1;
63   delete $ops->{$old};
64  }
65  return EAT_NONE;
66 }
67
68 hook_print $_, \&nick_cb for ('Change Nick', 'Your Nick Changing');
69
70 hook_command 'OPSCAN', sub {
71  my $serv = get_info 'server';
72  if (!$opers{$serv}) {
73   print $ph "No information for this network yet\n";
74   return EAT_ALL;
75  }
76  my ($chan, $isnet) = (get_info('channel'), 0);
77  for (get_list 'channels') {
78   if ($_->{type} eq 1 && $_->{channel} eq $chan) {
79    $isnet = 1;
80    last;
81   }
82  }
83  my ($name, @ops) = ($isnet) ? ('network', keys %{$opers{$serv}})
84                              : ('channel', map $_->{nick},
85                                             grep $opers{$serv}{$_->{nick}},
86                                              get_list 'users');
87  if (@ops) {
88   print $ph 'IRC operators on this '.$name." : \002@ops\n";
89  } else {
90   print $ph 'No IRC operators on this '.$name."\n";
91  }
92  return EAT_ALL;
93 }, {
94  help_text => 'OPSCAN, scan for IRC operators in the current channel'
95 };
96
97 $ph = new Xchat::XPI name   => 'Operators',
98                      tag    => 'Opers',
99                      desc   => 'IRC operators scanner',
100                      author => 'Vincent Pit (VPIT)',
101                      email  => 'perl@profvince.com',
102                      url    => 'http://www.profvince.com',
103                      unload => sub { undef %opers; };
104
105 1;