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