summaryrefslogtreecommitdiff
path: root/scripts/usercount.pl
diff options
context:
space:
mode:
authorTimo Sirainen <cras@irssi.org>2002-03-10 21:33:46 +0000
committercras <cras@dbcabf3a-b0e7-0310-adc4-f8d773084564>2002-03-10 21:33:46 +0000
commit02c9b7d1cf9d6863a871b5133f4dabb68f1101b7 (patch)
tree887ad4f86261354f2439f9cdbcfcc6c24d7d8cd1 /scripts/usercount.pl
parent0b7ebb6adb03df18f79612d7df77cd3cce1ed0c7 (diff)
downloadirssi-02c9b7d1cf9d6863a871b5133f4dabb68f1101b7.zip
new scripts
git-svn-id: http://svn.irssi.org/repos/irssi/trunk@2571 dbcabf3a-b0e7-0310-adc4-f8d773084564
Diffstat (limited to 'scripts/usercount.pl')
-rw-r--r--scripts/usercount.pl160
1 files changed, 160 insertions, 0 deletions
diff --git a/scripts/usercount.pl b/scripts/usercount.pl
new file mode 100644
index 00000000..fbf9d7fb
--- /dev/null
+++ b/scripts/usercount.pl
@@ -0,0 +1,160 @@
+use Irssi 20020101.0250 ();
+$VERSION = "1.14";
+%IRSSI = (
+ authors => 'David Leadbeater, Timo Sirainen, Georg Lukas',
+ contact => 'dgl@dgl.cx, tss@iki.fi, georg@boerde.de',
+ name => 'usercount',
+ description => 'Adds a usercount for a channel as a status bar item',
+ license => 'GNU GPLv2 or later',
+ url => 'http://irssi.dgl.yi.org/',
+);
+
+# Add usercount = { }; to your statusbar config
+# /set usercount_show_zero on or off to show users when 0 users of that type
+# /set usercount_show_ircops (default off)
+# /set usercount_show_halfops (default on)
+
+# you can customize the look of this item from theme file:
+# sb_usercount = "{sb %_$0%_ nicks ($1-)}";
+# sb_uc_ircops = "%_*%_$*";
+# sb_uc_ops = "%_@%_$*";
+# sb_uc_halfops = "%_%%%_$*";
+# sb_uc_voices = "%_+%_$*";
+# sb_uc_normal = "$*";
+# sb_uc_space = " ";
+
+
+use strict;
+use Irssi::TextUI;
+
+my ($ircops, $ops, $halfops, $voices, $normal, $total);
+my ($timeout_tag, $recalc);
+
+# Called to make the status bar item
+sub usercount {
+ my ($item, $get_size_only) = @_;
+ my $wi = !Irssi::active_win() ? undef : Irssi::active_win()->{active};
+
+ if(!$wi || $wi->{type} ne "CHANNEL") { # only works on channels
+ $item->{min_size} = $item->{max_size} = 0;
+ return;
+ }
+
+ if ($recalc) {
+ $recalc = 0;
+ calc_users($wi);
+ }
+
+ my $theme = Irssi::current_theme();
+ my $format = $theme->format_expand("{sb_usercount}");
+ if ($format) {
+ # use theme-specific look
+ my $ircopstr = $theme->format_expand("{sb_uc_ircops $ircops}", Irssi::EXPAND_FLAG_IGNORE_EMPTY);
+ my $opstr = $theme->format_expand("{sb_uc_ops $ops}", Irssi::EXPAND_FLAG_IGNORE_EMPTY);
+ my $halfopstr = $theme->format_expand("{sb_uc_halfops $halfops}", Irssi::EXPAND_FLAG_IGNORE_EMPTY);
+ my $voicestr = $theme->format_expand("{sb_uc_voices $voices}", Irssi::EXPAND_FLAG_IGNORE_EMPTY);
+ my $normalstr = $theme->format_expand("{sb_uc_normal $normal}", Irssi::EXPAND_FLAG_IGNORE_EMPTY);
+ my $space = $theme->format_expand('{sb_uc_space}', Irssi::EXPAND_FLAG_IGNORE_EMPTY);
+ $space = " " unless $space;
+
+ my $str = "";
+ $str .= $ircopstr.$space if defined $ircops;
+ $str .= $opstr.$space if defined $ops;
+ $str .= $halfopstr.$space if defined $halfops;
+ $str .= $voicestr.$space if defined $voices;
+ $str .= $normalstr.$space if defined $normal;
+ $str =~ s/\Q$space\E$//;
+
+ $format = $theme->format_expand("{sb_usercount $total $str}",
+ Irssi::EXPAND_FLAG_IGNORE_REPLACES);
+ } else {
+ # use the default look
+ $format = "{sb \%_$total\%_ nicks \%c(\%n";
+ $format .= '*'.$ircops.' ' if (defined $ircops);
+ $format .= '@'.$ops.' ' if (defined $ops);
+ $format .= '%%'.$halfops.' ' if (defined $halfops);
+ $format .= "+$voices " if (defined $voices);
+ $format .= "$normal " if (defined $normal);
+ $format =~ s/ $//;
+ $format .= "\%c)}";
+ }
+
+ $item->default_handler($get_size_only, $format, undef, 1);
+}
+
+sub calc_users() {
+ my $channel = shift;
+ my $server = $channel->{server};
+
+ $ircops = $ops = $halfops = $voices = $normal = 0;
+ for ($channel->nicks()) {
+ if ($_->{serverop}) {
+ $ircops++;
+ }
+
+ if ($_->{op}) {
+ $ops++;
+ } elsif ($_->{halfop}) {
+ $halfops++;
+ } elsif ($_->{voice}) {
+ $voices++;
+ } else {
+ $normal++;
+ }
+ }
+
+ $total = $ops+$halfops+$voices+$normal;
+ if (!Irssi::settings_get_bool('usercount_show_zero')) {
+ $ircops = undef if ($ircops == 0);
+ $ops = undef if ($ops == 0);
+ $halfops = undef if ($halfops == 0);
+ $voices = undef if ($voices == 0);
+ $normal = undef if ($normal == 0);
+ }
+ $halfops = undef unless Irssi::settings_get_bool('usercount_show_halfops');
+ $ircops = undef unless Irssi::settings_get_bool('usercount_show_ircops');
+}
+
+sub refresh {
+ if ($timeout_tag > 0) {
+ Irssi::timeout_remove($timeout_tag);
+ $timeout_tag = 0;
+ }
+ Irssi::statusbar_items_redraw('usercount');
+}
+
+sub refresh_check {
+ my $channel = shift;
+ my $wi = Irssi::active_win()->{active};
+ return if $wi->{name} ne $channel->{name};
+ return if $wi->{server}->{tag} ne $channel->{server}->{tag};
+
+ # don't refresh immediately, or we'll end up refreshing
+ # a lot around netsplits
+ $recalc = 1;
+ Irssi::timeout_remove($timeout_tag) if ($timeout_tag > 0);
+ $timeout_tag = Irssi::timeout_add(500, 'refresh', undef);
+}
+
+sub refresh_recalc {
+ $recalc = 1;
+ refresh();
+}
+
+$recalc = 1;
+$timeout_tag = 0;
+
+Irssi::settings_add_bool('misc', 'usercount_show_zero', 1);
+Irssi::settings_add_bool('misc', 'usercount_show_ircops', 0);
+Irssi::settings_add_bool('misc', 'usercount_show_halfops', 1);
+
+Irssi::statusbar_item_register('usercount', undef, 'usercount');
+Irssi::statusbars_recreate_items();
+
+Irssi::signal_add_last('nicklist new', 'refresh_check');
+Irssi::signal_add_last('nicklist remove', 'refresh_check');
+Irssi::signal_add_last('nick mode changed', 'refresh_check');
+Irssi::signal_add_last('setup changed', 'refresh_recalc');
+Irssi::signal_add_last('window changed', 'refresh_recalc');
+Irssi::signal_add_last('window item changed', 'refresh_recalc');
+