1# /CHANSHARE - display people who are in more than one channel with you
2# for irssi 0.7.98
3#
4# /CHANSHARE [ircnets ...] [#channels ...]
5#
6# This program is free software; you can redistribute it and/or
7# modify it under the terms of the GNU General Public License
8# as published by the Free Software Foundation; either version 2
9# of the License, or (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19#
20# Version 0.1 - Timo Sirainen tss@iki.fi
21#	Initial stalker.pl
22# Version 0.2 - Chad Armstrong chad@analogself.com
23#	Added multiserver support
24#	Added keying by nick AND hostname. "nick (fw.corp.com)"
25#	Prints to current active window now.
26# Version 0.21 - Timo Sirainen tss@iki.fi
27#       Removed printing to active window - if you want it, remove your
28#       status window.
29# Version 0.3 - Timo Sirainen tss@iki.fi
30#       Supports for limiting searches only to specified ircnets and
31#       channels. Some cleanups..
32
33use strict;
34use Irssi;
35use vars qw($VERSION %IRSSI);
36$VERSION = "0.3";
37%IRSSI = (
38    authors	=> "Timo \'cras\' Sirainen",
39    contact	=> "tss\@iki.fi",
40    name	=> "chan share",
41    description	=> "/CHANSHARE - display people who are in more than one channel with you",
42    license	=> "Public Domain",
43    url		=> "http://irssi.org/",
44    changed	=> "2002-03-04T22:47+0100",
45    changes	=> "v0.3 - Timo Sirainen tss\@iki.fi: Supports for limiting searches only to specified ircnets and channels. Some cleanups.."
46);
47
48sub cmd_chanshare {
49  my ($data, $server, $channel) = @_;
50  my (%channicks, @show_channels, @show_ircnets);
51
52  # get list of channels and ircnets
53  @show_channels = ();
54  @show_ircnets = ();
55  foreach my $arg (split(" ", $data)) {
56    if ($server->ischannel($arg)) {
57      push @show_channels, $arg;
58    } else {
59      push @show_ircnets, $arg;
60    }
61  }
62
63  my @checkservers = ();
64  if (scalar(@show_ircnets) == 0) {
65    # check from all servers
66    @checkservers = Irssi::servers();
67  } else {
68    # check from specified ircnets
69    foreach my $s (Irssi::servers()) {
70      foreach my $n (@show_ircnets) {
71	if ($s->{chatnet} eq $n) {
72	  push @checkservers, $s;
73	  last;
74	}
75      }
76    }
77  }
78
79  foreach my $s (@checkservers) {
80    my $mynick = $s->{nick};
81    foreach my $channel ($s->channels()) {
82      foreach my $nick ($channel->nicks()) {
83	my ($user, $host) = split(/@/, $nick->{host});
84	my $nickhost = $nick->{nick}." ($host)";
85	my @list = ();
86	next if ($nick->{nick} eq $mynick);
87
88	@list = @{$channicks{$nickhost}} if (@{$channicks{$nickhost}});
89#	Irssi::print($nickhost);
90	push @list, $channel->{name};
91	$channicks{$nickhost} = [@list];
92      }
93    }
94  }
95
96  Irssi::print("Nicks of those who share your #channels:");
97  foreach my $nick (keys %channicks) {
98    my @channels = @{$channicks{$nick}};
99    if (@channels > 1) {
100      my $chanstr = "";
101      my $ok = scalar(@show_channels) == 0;
102      foreach $channel (@channels) {
103	if (!$ok) {
104	  # check the show_channels list..
105	  foreach my $c (@show_channels) {
106	    if ($channel eq $c) {
107	      $ok = 1;
108	      last;
109	    }
110	  }
111	}
112	$chanstr .= "$channel ";
113      }
114      Irssi::print("$chanstr : $nick") if ($ok);
115    }
116  }
117}
118
119Irssi::command_bind('chanshare', 'cmd_chanshare');
120