# /OWNAGE [<output to channel>]
# shows how many channels you're joined and how many in them you're op, and
# how many nicks are in those channels (not including you)
use Irssi;
use strict;

sub cmd_ownage {
  # Get stuff
  my ($argumenter, $server, $witem) = @_;

  my $chans = 0;
  my $opchans = 0;
  my $nicks = 0;
  my $opnicks = 0;
  my $servs = 0;

  foreach my $ircserver (Irssi::servers()) {
    $servs++;
  }

  foreach my $channel (Irssi::channels()) {
    $chans++;
    if ($channel->{chanop}) {
      $opchans++;
      my @channicks = $channel->nicks();
      $nicks += (scalar @channicks)-1;
      
      $opnicks--; # don't count youself
      foreach my $nick (@channicks) {
	$opnicks++ if $nick->{op};
      }
    }
  }

  my $text = "has ops on \cB$opchans\cB out of \cB$chans\cB channels, is in the presence of \cB$nicks\cB other IRC users (of which \cB$opnicks\cB are channel operators) and is connected to \cB$servs\cB servers.";
  if ($witem && $witem->{type} eq "CHANNEL" || $witem->{type} eq "QUERY") {
    $witem->command("ACTION ".$witem->{name}." ".$text);
  } else {
    Irssi::print $text;
  }

#  if ($_[0]) {
#    Irssi::command("say $text");
#  } else {
#    Irssi::print $text;    
#  }
}

Irssi::command_bind('ownage', 'cmd_ownage');
