1#!/usr/local/bin/perl -w
2
3# chops.pl: Simulates BitchX's /chops and /nops commands
4# prints list with nickname and userhost
5#
6# Written by Jakub Jankowski <shasta@atn.pl>
7# for irssi 0.7.98.CVS
8#
9# todo:
10#  - enhance the look of the script
11#
12# sample /chops output:
13# [11:36:33] -!- Irssi: Information about chanops on #irssi
14# [11:36:33] -!- Irssi: [nick]    [hostmask]
15# [11:36:33] -!- Irssi: shasta    shasta@quasimodo.olsztyn.tpsa.pl
16# [11:36:34] -!- Irssi: cras      cras@xmunkki.org
17# [11:36:34] -!- Irssi: fuchs     fox@wh8043.stw.uni-rostock.de
18# [11:36:34] -!- Irssi: End of listing
19#
20# sample /nops output:
21# [11:40:34] -!- Irssi: Information about non-ops on #irssi
22# [11:40:34] -!- Irssi: [nick]    [hostmask]
23# [11:40:34] -!- Irssi: globe_    ~globe@ui20i21hel.dial.kolumbus.fi
24# [11:40:34] -!- Irssi: shastaBX  shasta@thorn.kanal.olsztyn.pl
25# [11:40:34] -!- Irssi: End of listing
26
27use strict;
28use vars qw($VERSION %IRSSI);
29
30$VERSION = "20020223";
31%IRSSI = (
32    authors     => 'Jakub Jankowski',
33    contact     => 'shasta@atn.pl',
34    name        => 'chops',
35    description => 'Simulates BitchX\'s /CHOPS and /NOPS commands.',
36    license     => 'GNU GPLv2 or later',
37    url         => 'http://irssi.atn.pl/',
38);
39
40use Irssi;
41use Irssi::Irc;
42
43Irssi::theme_register([
44	'chops_nochan', 'You are not on a channel',
45	'chops_notsynced', 'Channel $0 is not fully synchronized yet',
46	'chops_noone', 'There are no $0 to list',
47	'chops_start', 'Information about $0 on $1',
48	'chops_end', 'End of listing',
49	'chops_header', '[nick]    [hostmask]',
50	'chops_line', '$[!9]0 $[!50]1'
51]);
52
53sub cmd_chops {
54	my ($data, $server, $channel) = @_;
55	my @chanops = ();
56
57	# if we're not on a channel, print appropriate message and return
58	if (!$channel) {
59		Irssi::printformat(MSGLEVEL_CLIENTNOTICE, 'chops_nochan');
60		return;
61	}
62
63	# if channel is not fully synced yet, print appropriate message and return
64	if (!$channel->{synced}) {
65		Irssi::printformat(MSGLEVEL_CLIENTNOTICE, 'chops_notsynced', $channel->{name});
66		return;
67	}
68
69	# gather all opped people into an array
70	foreach my $nick ($channel->nicks()) {
71		push(@chanops, $nick) if ($nick->{op});
72	}
73
74	# if there are no chanops, print appropriate message and return
75	if (scalar(@chanops) < 1) {
76		Irssi::printformat(MSGLEVEL_CLIENTNOTICE, 'chops_noone', "chanops");
77		return;
78	}
79
80	# print a starting message
81	Irssi::printformat(MSGLEVEL_CLIENTNOTICE, 'chops_start', "chanops", $channel->{name});
82
83	# print listing header
84	Irssi::printformat(MSGLEVEL_CLIENTNOTICE, 'chops_header');
85
86	# print every chanop's nick, status (gone/here), userhost and hopcount
87	foreach my $nick (@chanops) {
88		my $userhost = $nick->{host};
89		# if user's host is longer than 50 characters, cut it to 47 to fit in column
90		$userhost = substr($userhost, 0, 47) if (length($userhost) > 50);
91		Irssi::printformat(MSGLEVEL_CLIENTNOTICE, 'chops_line', $nick->{nick}, $userhost);
92	}
93
94	# print listing footer
95	Irssi::printformat(MSGLEVEL_CLIENTNOTICE, 'chops_end');
96}
97
98sub cmd_nops {
99	my ($data, $server, $channel) = @_;
100	my @nonops = ();
101
102	# if we're not on a channel, print appropriate message and return
103	if (!$channel) {
104		Irssi::printformat(MSGLEVEL_CLIENTNOTICE, 'chops_nochan');
105		return;
106	}
107
108	# if channel is not fully synced yet, print appropriate message and return
109	if (!$channel->{synced}) {
110		Irssi::printformat(MSGLEVEL_CLIENTNOTICE, 'chops_notsynced', $channel->{name});
111		return;
112	}
113
114	# gather all not opped people into an array
115	foreach my $nick ($channel->nicks()) {
116		push(@nonops, $nick) if (!$nick->{op});
117	}
118
119	# if there are only chanops, print appropriate message and return
120	if (scalar(@nonops) < 1) {
121		Irssi::printformat(MSGLEVEL_CLIENTNOTICE, 'chops_noone', "non-ops");
122		return;
123	}
124
125	# print a starting message
126	Irssi::printformat(MSGLEVEL_CLIENTNOTICE, 'chops_start', "non-ops", $channel->{name});
127
128	# print listing header
129	Irssi::printformat(MSGLEVEL_CLIENTNOTICE, 'chops_header');
130
131	# print every chanop's nick, status (gone/here), userhost and hopcount
132	foreach my $nick (@nonops) {
133		my $userhost = $nick->{host};
134		# if user's host is longer than 50 characters, cut it to 47 to fit in column
135		$userhost = substr($userhost, 0, 47) if (length($userhost) > 50);
136		Irssi::printformat(MSGLEVEL_CLIENTNOTICE, 'chops_line', $nick->{nick}, $userhost);
137	}
138
139	# print listing footer
140	Irssi::printformat(MSGLEVEL_CLIENTNOTICE, 'chops_end');
141}
142
143Irssi::command_bind("chops", "cmd_chops");
144Irssi::command_bind("nops", "cmd_nops");
145