Path: blob/master/roles/users/files/irssi/scripts/usercount.pl
245 views
use strict;1use Irssi 20040119.2359 ();2use vars qw($VERSION %IRSSI);3$VERSION = "1.20";4%IRSSI = (5authors => 'David Leadbeater, Timo Sirainen, Georg Lukas',6contact => '[email protected], [email protected], [email protected]',7name => 'usercount',8description => 'Adds a usercount for a channel as a statusbar item',9sbitems => 'usercount',10license => 'GNU GPLv2 or later',11url => 'http://irssi.dgl.cx/',12changes => 'Only show halfops if server supports them',13);1415# Once you have loaded this script run the following command:16# /statusbar window add usercount17# You can also add -alignment left|right option1819# Settings:20# /toggle usercount_show_zero to show item even when there are no users21# /toggle usercount_show_ircops (default off)22# /toggle usercount_show_halfops (default on)2324# you can customize the look of this item from theme file:25# sb_usercount = "{sb %_$0%_ nicks ($1-)}";26# sb_uc_ircops = "%_*%_$*";27# sb_uc_ops = "%_@%_$*";28# sb_uc_halfops = "%_%%%_$*";29# sb_uc_voices = "%_+%_$*";30# sb_uc_normal = "$*";31# sb_uc_space = " ";323334use Irssi::TextUI;3536my ($ircops, $ops, $halfops, $voices, $normal, $total);37my ($timeout_tag, $recalc);3839# Called to make the status bar item40sub usercount {41my ($item, $get_size_only) = @_;42my $wi = !Irssi::active_win() ? undef : Irssi::active_win()->{active};4344if(!ref $wi || $wi->{type} ne "CHANNEL") { # only works on channels45return unless ref $item;46$item->{min_size} = $item->{max_size} = 0;47return;48}4950if ($recalc) {51$recalc = 0;52calc_users($wi);53}5455my $theme = Irssi::current_theme();56my $format = $theme->format_expand("{sb_usercount}");57if ($format) {58# use theme-specific look59my $ircopstr = $theme->format_expand("{sb_uc_ircops $ircops}",60Irssi::EXPAND_FLAG_IGNORE_EMPTY);61my $opstr = $theme->format_expand("{sb_uc_ops $ops}",62Irssi::EXPAND_FLAG_IGNORE_EMPTY);63my $halfopstr = $theme->format_expand("{sb_uc_halfops $halfops}",64Irssi::EXPAND_FLAG_IGNORE_EMPTY);65my $voicestr = $theme->format_expand("{sb_uc_voices $voices}",66Irssi::EXPAND_FLAG_IGNORE_EMPTY);67my $normalstr = $theme->format_expand("{sb_uc_normal $normal}",68Irssi::EXPAND_FLAG_IGNORE_EMPTY);69my $space = $theme->format_expand('{sb_uc_space}',70Irssi::EXPAND_FLAG_IGNORE_EMPTY);71$space = " " unless $space;7273my $str = "";74$str .= $ircopstr.$space if defined $ircops;75$str .= $opstr.$space if defined $ops;76$str .= $halfopstr.$space if defined $halfops;77$str .= $voicestr.$space if defined $voices;78$str .= $normalstr.$space if defined $normal;79$str =~ s/\Q$space\E$//;8081$format = $theme->format_expand("{sb_usercount $total $str}",82Irssi::EXPAND_FLAG_IGNORE_REPLACES);83} else {84# use the default look85$format = "{sb \%_$total\%_ nicks \%c(\%n";86$format .= '*'.$ircops.' ' if (defined $ircops);87$format .= '@'.$ops.' ' if (defined $ops);88$format .= '%%'.$halfops.' ' if (defined $halfops);89$format .= "+$voices " if (defined $voices);90$format .= "$normal " if (defined $normal);91$format =~ s/ $//;92$format .= "\%c)}";93}9495$item->default_handler($get_size_only, $format, undef, 1);96}9798sub calc_users() {99my $channel = shift;100my $server = $channel->{server};101102$ircops = $ops = $halfops = $voices = $normal = 0;103for ($channel->nicks()) {104if ($_->{serverop}) {105$ircops++;106}107108if ($_->{op}) {109$ops++;110} elsif ($_->{halfop}) {111$halfops++;112} elsif ($_->{voice}) {113$voices++;114} else {115$normal++;116}117}118119$total = $ops+$halfops+$voices+$normal;120121if (!Irssi::settings_get_bool('usercount_show_zero')) {122$ircops = undef if ($ircops == 0);123$ops = undef if ($ops == 0);124$halfops = undef if ($halfops == 0);125$voices = undef if ($voices == 0);126$normal = undef if ($normal == 0);127}128129# Server doesn't support halfops?130if($server->can('isupport') && $server->isupport("PREFIX") !~ /\%/) {131$halfops = undef;132} else {133$halfops = undef unless Irssi::settings_get_bool('usercount_show_halfops');134}135136$ircops = undef unless Irssi::settings_get_bool('usercount_show_ircops');137}138139sub refresh {140if ($timeout_tag > 0) {141Irssi::timeout_remove($timeout_tag);142$timeout_tag = 0;143}144Irssi::statusbar_items_redraw('usercount');145}146147sub refresh_check {148my $channel = shift;149my $wi = ref Irssi::active_win() ? Irssi::active_win()->{active} : 0;150151return unless ref $wi && ref $channel;152return if $wi->{name} ne $channel->{name};153return if $wi->{server}->{tag} ne $channel->{server}->{tag};154155# don't refresh immediately, or we'll end up refreshing156# a lot around netsplits157$recalc = 1;158Irssi::timeout_remove($timeout_tag) if ($timeout_tag > 0);159$timeout_tag = Irssi::timeout_add(500, 'refresh', undef);160}161162sub refresh_recalc {163$recalc = 1;164refresh();165}166167$recalc = 1;168$timeout_tag = 0;169170Irssi::settings_add_bool('usercount', 'usercount_show_zero', 1);171Irssi::settings_add_bool('usercount', 'usercount_show_ircops', 0);172Irssi::settings_add_bool('usercount', 'usercount_show_halfops', 1);173174Irssi::statusbar_item_register('usercount', undef, 'usercount');175Irssi::statusbars_recreate_items();176177Irssi::signal_add_last('nicklist new', 'refresh_check');178Irssi::signal_add_last('nicklist remove', 'refresh_check');179Irssi::signal_add_last('nick mode changed', 'refresh_check');180Irssi::signal_add_last('setup changed', 'refresh_recalc');181Irssi::signal_add_last('window changed', 'refresh_recalc');182Irssi::signal_add_last('window item changed', 'refresh_recalc');183184185186