summaryrefslogtreecommitdiffstats
path: root/scripts/chanshare.pl
blob: dbd4bbec53e1544545e9c220c2f93a2e1cad444b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# /CHANSHARE - display people who are in more than one channel with you
# for irssi 0.7.98
#
# /CHANSHARE [ircnets ...] [#channels ...]
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
# 
# Version 0.1 - Timo Sirainen tss@iki.fi
#	Initial stalker.pl 
# Version 0.2 - Chad Armstrong chad@analogself.com
#	Added multiserver support
#	Added keying by nick AND hostname. "nick (fw.corp.com)"
#	Prints to current active window now.
# Version 0.21 - Timo Sirainen tss@iki.fi
#       Removed printing to active window - if you want it, remove your
#       status window.
# Version 0.3 - Timo Sirainen tss@iki.fi
#       Supports for limiting searches only to specified ircnets and
#       channels. Some cleanups..
# Version 0.4 - bw1
#       bug fix

use strict;
use Irssi;
use vars qw($VERSION %IRSSI); 
$VERSION = "0.4";
%IRSSI = (
    authors	=> "Timo \'cras\' Sirainen",
    contact	=> "tss\@iki.fi",
    name	=> "chan share",
    description	=> "/CHANSHARE - display people who are in more than one channel with you",
    license	=> "Public Domain",
    url		=> "http://irssi.org/",
    changed	=> "2019-02-13",
);

sub cmd_chanshare {
  my ($data, $server, $channel) = @_;
  my (%channicks, @show_channels, @show_ircnets);

  # get list of channels and ircnets
  @show_channels = ();
  @show_ircnets = ();
  foreach my $arg (split(" ", $data)) {
    if ($server->ischannel($arg)) {
      push @show_channels, $arg;
    } else {
      push @show_ircnets, $arg;
    }
  }

  my @checkservers = ();
  if (scalar(@show_ircnets) == 0) {
    # check from all servers
    @checkservers = Irssi::servers();
  } else {
    # check from specified ircnets
    foreach my $s (Irssi::servers()) {
      foreach my $n (@show_ircnets) {
	if ($s->{chatnet} eq $n) {
	  push @checkservers, $s;
	  last;
	}
      }
    }
  }

  foreach my $s (@checkservers) {
    my $mynick = $s->{nick};
    foreach my $channel ($s->channels()) {
      foreach my $nick ($channel->nicks()) {
	my ($user, $host) = split(/@/, $nick->{host});
	my $nickhost = $nick->{nick}." ($host)";
	my @list = ();
	next if ($nick->{nick} eq $mynick);

	@list = @{$channicks{$nickhost}} if (exists $channicks{$nickhost});
#	Irssi::print($nickhost);
	push @list, $channel->{name};
	$channicks{$nickhost} = [@list];
      }
    }
  }

  Irssi::print("Nicks of those who share your #channels:");
  foreach my $nick (keys %channicks) {
    my @channels = @{$channicks{$nick}};
    if (@channels > 1) {
      my $chanstr = "";
      my $ok = scalar(@show_channels) == 0;
      foreach $channel (@channels) {
	if (!$ok) {
	  # check the show_channels list..
	  foreach my $c (@show_channels) {
	    if ($channel eq $c) {
	      $ok = 1;
	      last;
	    }
	  }
	}
	$chanstr .= "$channel ";
      }
      Irssi::print("$chanstr : $nick") if ($ok);
    }
  }
}

Irssi::command_bind('chanshare', 'cmd_chanshare');

# vim:set ts=8 sw=2 expandtab: