Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/dotfiles
Path: blob/master/roles/users/files/irssi/scripts/usercount.pl
245 views
1
use strict;
2
use Irssi 20040119.2359 ();
3
use vars qw($VERSION %IRSSI);
4
$VERSION = "1.20";
5
%IRSSI = (
6
authors => 'David Leadbeater, Timo Sirainen, Georg Lukas',
7
contact => '[email protected], [email protected], [email protected]',
8
name => 'usercount',
9
description => 'Adds a usercount for a channel as a statusbar item',
10
sbitems => 'usercount',
11
license => 'GNU GPLv2 or later',
12
url => 'http://irssi.dgl.cx/',
13
changes => 'Only show halfops if server supports them',
14
);
15
16
# Once you have loaded this script run the following command:
17
# /statusbar window add usercount
18
# You can also add -alignment left|right option
19
20
# Settings:
21
# /toggle usercount_show_zero to show item even when there are no users
22
# /toggle usercount_show_ircops (default off)
23
# /toggle usercount_show_halfops (default on)
24
25
# you can customize the look of this item from theme file:
26
# sb_usercount = "{sb %_$0%_ nicks ($1-)}";
27
# sb_uc_ircops = "%_*%_$*";
28
# sb_uc_ops = "%_@%_$*";
29
# sb_uc_halfops = "%_%%%_$*";
30
# sb_uc_voices = "%_+%_$*";
31
# sb_uc_normal = "$*";
32
# sb_uc_space = " ";
33
34
35
use Irssi::TextUI;
36
37
my ($ircops, $ops, $halfops, $voices, $normal, $total);
38
my ($timeout_tag, $recalc);
39
40
# Called to make the status bar item
41
sub usercount {
42
my ($item, $get_size_only) = @_;
43
my $wi = !Irssi::active_win() ? undef : Irssi::active_win()->{active};
44
45
if(!ref $wi || $wi->{type} ne "CHANNEL") { # only works on channels
46
return unless ref $item;
47
$item->{min_size} = $item->{max_size} = 0;
48
return;
49
}
50
51
if ($recalc) {
52
$recalc = 0;
53
calc_users($wi);
54
}
55
56
my $theme = Irssi::current_theme();
57
my $format = $theme->format_expand("{sb_usercount}");
58
if ($format) {
59
# use theme-specific look
60
my $ircopstr = $theme->format_expand("{sb_uc_ircops $ircops}",
61
Irssi::EXPAND_FLAG_IGNORE_EMPTY);
62
my $opstr = $theme->format_expand("{sb_uc_ops $ops}",
63
Irssi::EXPAND_FLAG_IGNORE_EMPTY);
64
my $halfopstr = $theme->format_expand("{sb_uc_halfops $halfops}",
65
Irssi::EXPAND_FLAG_IGNORE_EMPTY);
66
my $voicestr = $theme->format_expand("{sb_uc_voices $voices}",
67
Irssi::EXPAND_FLAG_IGNORE_EMPTY);
68
my $normalstr = $theme->format_expand("{sb_uc_normal $normal}",
69
Irssi::EXPAND_FLAG_IGNORE_EMPTY);
70
my $space = $theme->format_expand('{sb_uc_space}',
71
Irssi::EXPAND_FLAG_IGNORE_EMPTY);
72
$space = " " unless $space;
73
74
my $str = "";
75
$str .= $ircopstr.$space if defined $ircops;
76
$str .= $opstr.$space if defined $ops;
77
$str .= $halfopstr.$space if defined $halfops;
78
$str .= $voicestr.$space if defined $voices;
79
$str .= $normalstr.$space if defined $normal;
80
$str =~ s/\Q$space\E$//;
81
82
$format = $theme->format_expand("{sb_usercount $total $str}",
83
Irssi::EXPAND_FLAG_IGNORE_REPLACES);
84
} else {
85
# use the default look
86
$format = "{sb \%_$total\%_ nicks \%c(\%n";
87
$format .= '*'.$ircops.' ' if (defined $ircops);
88
$format .= '@'.$ops.' ' if (defined $ops);
89
$format .= '%%'.$halfops.' ' if (defined $halfops);
90
$format .= "+$voices " if (defined $voices);
91
$format .= "$normal " if (defined $normal);
92
$format =~ s/ $//;
93
$format .= "\%c)}";
94
}
95
96
$item->default_handler($get_size_only, $format, undef, 1);
97
}
98
99
sub calc_users() {
100
my $channel = shift;
101
my $server = $channel->{server};
102
103
$ircops = $ops = $halfops = $voices = $normal = 0;
104
for ($channel->nicks()) {
105
if ($_->{serverop}) {
106
$ircops++;
107
}
108
109
if ($_->{op}) {
110
$ops++;
111
} elsif ($_->{halfop}) {
112
$halfops++;
113
} elsif ($_->{voice}) {
114
$voices++;
115
} else {
116
$normal++;
117
}
118
}
119
120
$total = $ops+$halfops+$voices+$normal;
121
122
if (!Irssi::settings_get_bool('usercount_show_zero')) {
123
$ircops = undef if ($ircops == 0);
124
$ops = undef if ($ops == 0);
125
$halfops = undef if ($halfops == 0);
126
$voices = undef if ($voices == 0);
127
$normal = undef if ($normal == 0);
128
}
129
130
# Server doesn't support halfops?
131
if($server->can('isupport') && $server->isupport("PREFIX") !~ /\%/) {
132
$halfops = undef;
133
} else {
134
$halfops = undef unless Irssi::settings_get_bool('usercount_show_halfops');
135
}
136
137
$ircops = undef unless Irssi::settings_get_bool('usercount_show_ircops');
138
}
139
140
sub refresh {
141
if ($timeout_tag > 0) {
142
Irssi::timeout_remove($timeout_tag);
143
$timeout_tag = 0;
144
}
145
Irssi::statusbar_items_redraw('usercount');
146
}
147
148
sub refresh_check {
149
my $channel = shift;
150
my $wi = ref Irssi::active_win() ? Irssi::active_win()->{active} : 0;
151
152
return unless ref $wi && ref $channel;
153
return if $wi->{name} ne $channel->{name};
154
return if $wi->{server}->{tag} ne $channel->{server}->{tag};
155
156
# don't refresh immediately, or we'll end up refreshing
157
# a lot around netsplits
158
$recalc = 1;
159
Irssi::timeout_remove($timeout_tag) if ($timeout_tag > 0);
160
$timeout_tag = Irssi::timeout_add(500, 'refresh', undef);
161
}
162
163
sub refresh_recalc {
164
$recalc = 1;
165
refresh();
166
}
167
168
$recalc = 1;
169
$timeout_tag = 0;
170
171
Irssi::settings_add_bool('usercount', 'usercount_show_zero', 1);
172
Irssi::settings_add_bool('usercount', 'usercount_show_ircops', 0);
173
Irssi::settings_add_bool('usercount', 'usercount_show_halfops', 1);
174
175
Irssi::statusbar_item_register('usercount', undef, 'usercount');
176
Irssi::statusbars_recreate_items();
177
178
Irssi::signal_add_last('nicklist new', 'refresh_check');
179
Irssi::signal_add_last('nicklist remove', 'refresh_check');
180
Irssi::signal_add_last('nick mode changed', 'refresh_check');
181
Irssi::signal_add_last('setup changed', 'refresh_recalc');
182
Irssi::signal_add_last('window changed', 'refresh_recalc');
183
Irssi::signal_add_last('window item changed', 'refresh_recalc');
184
185
186