1use strict;
2use vars qw($VERSION %IRSSI);
3use Irssi 20010920.0000 ();
4$VERSION = "2.01";
5%IRSSI = (
6    authors     => 'From irssi source, modified by David Leadbeater (dg)',
7    name        => 'clones',
8    description => '/CLONES - Display clones in the active channel (with added options)',
9    license     => 'Same as Irssi',
10    url         => 'http://irssi.dgl.cx/',
11);
12
13sub cmd_clones {
14  my ($data, $server, $channel) = @_;
15
16  my $min = $data =~ /\d/ ? $data : Irssi::settings_get_int('clones_min_show');
17
18  if (!$channel || $channel->{type} ne 'CHANNEL') {
19    Irssi::print('No active channel in window');
20    return;
21  }
22
23  my %hostnames = {};
24  my $ident = Irssi::settings_get_bool('clones_host_only');
25
26  foreach my $nick ($channel->nicks()) {
27	my $hostname;
28	if($ident) {
29	   ($hostname = $nick->{host}) =~ s/^[^@]+@//;
30	}else{
31	   $hostname = $nick->{host};
32	}
33
34	$hostnames{$hostname} ||= [];
35	push( @{ $hostnames{$hostname} }, $nick->{nick});
36  }
37
38  my $count = 0;
39  foreach my $host (keys %hostnames) {
40	next unless ref($hostnames{$host}) eq 'ARRAY'; # sometimes a hash is here
41    my @clones = @{ $hostnames{$host} };
42    if (scalar @clones >= $min) {
43      $channel->print('Clones:') if ($count == 0);
44      $channel->print("$host: " . join(' ',@clones));
45      $count++;
46    }
47  }
48
49  $channel->print('No clones in channel') if ($count == 0);
50}
51
52Irssi::command_bind('clones', 'cmd_clones');
53Irssi::settings_add_bool('misc', 'clones_host_only', 1);
54Irssi::settings_add_int('misc', 'clones_min_show', 2);
55
56