-
Notifications
You must be signed in to change notification settings - Fork 234
/
mh_sbuserinfo.pl
450 lines (388 loc) · 14.8 KB
/
mh_sbuserinfo.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
##############################################################################
#
# mh_sbuserinfo.pl v1.06 (20170424)
#
# Copyright (c) 2015-2017 Michael Hansen
#
# Permission to use, copy, modify, and distribute this software
# for any purpose with or without fee is hereby granted, provided
# that the above copyright notice and this permission notice
# appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
# WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
# THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
##############################################################################
#
# statusbar item that shows user count (opers, ops, halfops, voice and total) and limit info (with opless/limit warning) in channels
#
# displays in the statusbar the number of users and the limit of the channel,
# with several settings for finetuning:
#
# default settings: [Users: <users>(*<users_oper>:@<users_op>:+<users_voice>:<users_rest>)/<limit>(<limitusers>)]
# "/<limit>(<limitusers>)" will only show when there is a limit set.
# "(<limitusers>)" shows the difference between the limit and current
# users (this can be negative if the limit is lower than users)
#
# (if you do not already have another script running a periodical who on channels,
# you will need one (autowho.pl from irssi.org for example). otherwise oper/away
# counts will not update correctly (if you do not use oper/away details you can ignore
# this))
#
# setting mh_sbuserinfo_format_group_begin (default '(') and
# setting mh_sbuserinfo_format_group_end' (default ')'); change the characters grouping
# details
#
# setting mh_sbuserinfo_format_sep (default ':'): change the : seperator to another string
#
# setting mh_sbuserinfo_format_div (default '/'): change the / divider to another string
#
# setting mh_sbuserinfo_show_prefix (default 'Users: '): set/unset the prefix
# in the window item
#
# setting mh_sbuserinfo_show_details (default ON): enable/disable showing a
# detailed breakout of users into opers, ops, halfops, voice and normal
# (further customisable with _show_details_*)
#
# setting mh_sbuserinfo_show_details_mode (default ON): enable/disable
# prefixing opers, ops, halfops and voice with *@%+ when details are enabled
#
# setting mh_sbuserinfo_format_mode_away (default 'z'),
# setting mh_sbuserinfo_format_mode_oper (default '*'),
# setting mh_sbuserinfo_format_mode_op (default '@'),
# setting mh_sbuserinfo_format_mode_ho (default '%%'),
# setting mh_sbuserinfo_format_mode_vo (default '+') and
# setting mh_sbuserinfo_format_mode_other (default ''): change the mode prefix
# for each of away, oper, op, halfdop, voice and others
#
# setting mh_sbuserinfo_show_details_halfop (default OFF): enable/disable
# showing halfops when details are enabled
#
# setting mh_sbuserinfo_show_details_oper (default ON): enable/disable
# showing opers when details are enabled
#
# setting mh_sbuserinfo_show_details_away (default ON): enable/disable
# showing users away when details are enabled
#
# setting mh_sbuserinfo_show_details_difference (default ON): enable/disable
# showing the "(<limitusers>)"
#
# setting mh_sbuserinfo_show_warning_opless' (default ON): change the colour
# of "<users_op>" if channel is opless
#
# setting mh_sbuserinfo_show_warning_limit (default ON): change the colour
# of "<limit>" if channel is above, at or close to the limited amount of users
#
# setting mh_sbuserinfo_show_warning_limit_percent (default 0): number in
# percent (0-100) of users relative to the limit before a limit warning is
# triggered (if set to 0 see mh_sbuserinfo_show_warning_limit_difference)
#
# setting mh_sbuserinfo_show_warning_limit_difference (default 5): when
# mh_sbuserinfo_show_warning_limit_percent is 0, use this absolute value
# as the difference warning trigger instead of percentage
#
# setting mh_sbuserinfo_warning_format (default '%Y'): the colour used for
# warnings. see http://www.irssi.org/documentation/formats
#
# to configure irssi to show the new statusbar item in a default irssi
# installation type '/statusbar window add -after window_empty mh_sbuserinfo'.
# see '/help statusbar' for more details and do not forget to '/save'
#
# history:
#
# v1.06 (20170424)
# added 'sbitems' to irssi header for better scriptassist.pl support (github issue #1)
# added _show_details_away/_format_mode_away and supporting code
# some description and documentation changes
#
# v1.05 (20161106)
# added setting _show_details_oper and supporting code
# added setting _format_sep and supportingf code
# added setting _format_div and supporting code
# added setting _group_begin and _format_group_end and supporting code
# added setting _format_mode_oper, _format_mode_op, _format_mode_ho, _format_mode_vo and _format_mode_other, and supporting code
# added settting _show_warning_limit_difference and supporting code (changing _show_warning_limit_percent behavior)
# changed default of _show_warning_limit_percent from 95 to 0
#
# v1.04 (20151225)
# added setting _show_details_difference and supporting code
# changed _show_warning_limit_percent default from 90 to 95
# added changed field to irssi header
# added a few comments
#
# v1.03 (20151201)
# added setting _show_prefix and supporting code
# changed setting _show_details_mode default to ON
# updated documentation
#
# v1.02 (20151127)
# only show item when channel is synced
# cleaned out redundant code
#
# v1.01 (20151127)
# call statusbar_redraw directly in signals
# now using elsif
# removed timeout on load
#
# v1.00 (20151126)
# initial release
#
use v5.14.2;
use strict;
##############################################################################
#
# irssi head
#
##############################################################################
use Irssi 20100403;
use Irssi::TextUI;
our $VERSION = '1.06';
our %IRSSI =
(
'name' => 'mh_sbuserinfo',
'description' => 'statusbar item that shows user count (opers, ops, halfops, voice and total) and limit info (with opless/limit warning) in channels',
'license' => 'BSD',
'authors' => 'Michael Hansen',
'contact' => 'mh on IRCnet #help',
'url' => 'http://scripts.irssi.org / https://github.com/mh-source/irssi-scripts',
'changed' => 'Mon Apr 24 09:34:36 CEST 2017',
'sbitems' => 'mh_sbuserinfo',
);
##############################################################################
#
# script functions
#
##############################################################################
sub statusbar_redraw
{
my ($channel) = @_;
if (ref($channel) eq 'Irssi::Irc::Channel')
{
my $window = Irssi::active_win();
my $channelactive = $window->{'active'};
if (ref($channelactive) eq 'Irssi::Irc::Channel')
{
#
# only redraw if triggered by active channel
#
if (lc($channelactive->{'server'}->{'tag'}) eq lc($channel->{'server'}->{'tag'}))
{
if (lc($channel->{'name'}) eq lc($channelactive->{'name'}))
{
if ($channelactive->{'synced'})
{
Irssi::statusbar_items_redraw('mh_sbuserinfo');
}
}
}
}
}
}
##############################################################################
#
# irssi signal handlers
#
##############################################################################
sub signal_setup_changed_last
{
statusbar_redraw(Irssi::active_win->{'active'});
}
sub signal_window_changed_last
{
my ($window, $window_old) = @_;
statusbar_redraw($window->{'active'});
}
##############################################################################
#
# statusbar item handlers
#
##############################################################################
sub statusbar_userinfo
{
my ($statusbaritem, $get_size_only) = @_;
my $format = '';
my $window = Irssi::active_win();
my $channel = $window->{'active'};
if (ref($channel) eq 'Irssi::Irc::Channel')
{
if ($channel->{'synced'})
{
$format = Irssi::settings_get_str('mh_sbuserinfo_show_prefix');
my $users = 0;
my $users_op = 0;
my $users_ho = 0;
my $users_vo = 0;
my $users_oper = 0;
my $users_gone = 0;
my $warning_format = Irssi::settings_get_str('mh_sbuserinfo_warning_format');
for my $nick ($channel->nicks())
{
$users++;
if ($nick->{'op'})
{
$users_op++;
} elsif ($nick->{'halfop'})
{
$users_ho++;
} elsif ($nick->{'voice'})
{
$users_vo++;
}
if ($nick->{'serverop'})
{
$users_oper++;
}
if ($nick->{'gone'})
{
$users_gone++;
}
}
$format .= $users;
my $format_sep = Irssi::settings_get_str('mh_sbuserinfo_format_sep');
my $format_div = Irssi::settings_get_str('mh_sbuserinfo_format_div');
my $format_group_begin = Irssi::settings_get_str('mh_sbuserinfo_format_group_begin');
my $format_group_end = Irssi::settings_get_str('mh_sbuserinfo_format_group_end');
if (Irssi::settings_get_bool('mh_sbuserinfo_show_details'))
{
$format .= $format_group_begin;
my $showmode = Irssi::settings_get_bool('mh_sbuserinfo_show_details_mode');
if (Irssi::settings_get_bool('mh_sbuserinfo_show_details_away'))
{
if ($showmode)
{
$format .= Irssi::settings_get_str('mh_sbuserinfo_format_mode_away');
}
$format .= $users_gone . $format_sep
}
if (Irssi::settings_get_bool('mh_sbuserinfo_show_details_oper'))
{
if ($showmode)
{
$format .= Irssi::settings_get_str('mh_sbuserinfo_format_mode_oper');
}
$format .= $users_oper . $format_sep
}
if (Irssi::settings_get_bool('mh_sbuserinfo_show_warning_opless') and (not $users_op))
{
$format .= $warning_format;
}
if ($showmode)
{
$format .= Irssi::settings_get_str('mh_sbuserinfo_format_mode_op');
}
$format .= $users_op . '%n' . $format_sep;
if (Irssi::settings_get_bool('mh_sbuserinfo_show_details_halfop'))
{
#
# add halfops to ops so users calculation below matches
#
$users_op += $users_ho;
if ($showmode)
{
$format .= Irssi::settings_get_str('mh_sbuserinfo_format_mode_ho');
}
$format .= $users_ho . $format_sep;
}
if ($showmode)
{
$format .= Irssi::settings_get_str('mh_sbuserinfo_format_mode_vo');;
}
$format .= $users_vo . $format_sep;
if ($showmode)
{
$format .= Irssi::settings_get_str('mh_sbuserinfo_format_mode_other');;
}
$format .= ($users - ($users_op + $users_vo)) . $format_group_end;
}
my $limit = $channel->{'limit'};
if ($limit)
{
$format .= $format_div;
if (Irssi::settings_get_bool('mh_sbuserinfo_show_warning_limit'))
{
my $setting_percent = Irssi::settings_get_int('mh_sbuserinfo_show_warning_limit_percent');
if ($setting_percent > 100)
{
$setting_percent = 100;
} elsif ($setting_percent < 0)
{
$setting_percent = 0;
}
if ($setting_percent)
{
my $percent = int(($users / $limit) * 100);
if ($percent >= $setting_percent)
{
$format .= $warning_format;
}
} else
{
my $setting_percent = Irssi::settings_get_int('mh_sbuserinfo_show_warning_limit_difference');
my $difference = ($limit - $users);
if ($setting_percent < 0)
{
$setting_percent = 0;
}
if ($difference < $setting_percent)
{
$format .= $warning_format;
}
}
}
if (Irssi::settings_get_bool('mh_sbuserinfo_show_details_difference'))
{
$limit .= $format_group_begin . ($limit - $users) . $format_group_end;
}
$format .= $limit . '%n';
}
}
}
$statusbaritem->default_handler($get_size_only, '{sb ' . $format . '}', '', 0);
}
##############################################################################
#
# script on load
#
##############################################################################
Irssi::settings_add_bool('mh_sbuserinfo', 'mh_sbuserinfo_show_details', 1);
Irssi::settings_add_bool('mh_sbuserinfo', 'mh_sbuserinfo_show_details_mode', 1);
Irssi::settings_add_bool('mh_sbuserinfo', 'mh_sbuserinfo_show_details_halfop', 0);
Irssi::settings_add_bool('mh_sbuserinfo', 'mh_sbuserinfo_show_warning_opless', 1);
Irssi::settings_add_bool('mh_sbuserinfo', 'mh_sbuserinfo_show_warning_limit', 1);
Irssi::settings_add_int( 'mh_sbuserinfo', 'mh_sbuserinfo_show_warning_limit_percent', 0);
Irssi::settings_add_int( 'mh_sbuserinfo', 'mh_sbuserinfo_show_warning_limit_difference', 5);
Irssi::settings_add_str( 'mh_sbuserinfo', 'mh_sbuserinfo_warning_format', '%Y');
Irssi::settings_add_str( 'mh_sbuserinfo', 'mh_sbuserinfo_show_prefix', 'Users: ');
Irssi::settings_add_bool('mh_sbuserinfo', 'mh_sbuserinfo_show_details_difference', 1);
Irssi::settings_add_bool('mh_sbuserinfo', 'mh_sbuserinfo_show_details_oper', 1);
Irssi::settings_add_bool('mh_sbuserinfo', 'mh_sbuserinfo_show_details_away', 1);
Irssi::settings_add_str( 'mh_sbuserinfo', 'mh_sbuserinfo_format_sep', ':');
Irssi::settings_add_str( 'mh_sbuserinfo', 'mh_sbuserinfo_format_div', '/');
Irssi::settings_add_str( 'mh_sbuserinfo', 'mh_sbuserinfo_format_group_begin', '(');
Irssi::settings_add_str( 'mh_sbuserinfo', 'mh_sbuserinfo_format_group_end', ')');
Irssi::settings_add_str( 'mh_sbuserinfo', 'mh_sbuserinfo_format_mode_oper', '*');
Irssi::settings_add_str( 'mh_sbuserinfo', 'mh_sbuserinfo_format_mode_away', 'z');
Irssi::settings_add_str( 'mh_sbuserinfo', 'mh_sbuserinfo_format_mode_op', '@');
Irssi::settings_add_str( 'mh_sbuserinfo', 'mh_sbuserinfo_format_mode_ho', '%%');
Irssi::settings_add_str( 'mh_sbuserinfo', 'mh_sbuserinfo_format_mode_vo', '+');
Irssi::settings_add_str( 'mh_sbuserinfo', 'mh_sbuserinfo_format_mode_other', '');
Irssi::signal_add_last('channel sync', 'statusbar_redraw');
Irssi::signal_add_last('channel mode changed', 'statusbar_redraw');
Irssi::signal_add_last('nick mode changed', 'statusbar_redraw');
Irssi::signal_add_last('nicklist new', 'statusbar_redraw');
Irssi::signal_add_last('nicklist remove', 'statusbar_redraw');
Irssi::signal_add_last('setup changed', 'signal_setup_changed_last');
Irssi::signal_add_last('window changed', 'signal_window_changed_last');
Irssi::statusbar_item_register('mh_sbuserinfo', '', 'statusbar_userinfo');
1;
##############################################################################
#
# eof mh_sbuserinfo.pl
#
##############################################################################