1use Irssi 20020101.0250 ();
2$VERSION = "2";
3%IRSSI = (
4    authors     => 'David Leadbeater',
5    contact     => 'dgl@dgl.cx',
6    name        => 'servercomplete',
7    description => 'Tab complete servers and userhosts (irc. -> irc server, user@ -> user@host). Useful for lazy ircops for /squit and so on :)',
8    license     => 'GNU GPLv2 or later',
9    url         => 'http://irssi.dgl.yi.org/',
10);
11
12use strict;
13my %servers;
14
15sub sig_complete {
16  my ($complist, $window, $word, $linestart, $want_space) = @_;
17  my $tag = $window->{active_server}->{tag};
18
19  if($word =~ /[!*@]/) {
20	 my $wi = Irssi::active_win()->{active};
21	 return unless ref $wi and $wi->{type} eq 'CHANNEL';
22	 my $server = $wi->{server};
23	 return unless ref $server;
24
25	 my($nick,$ident,$host) = ('','','');
26
27	 $nick = $1 if $word =~ /([^!]+)!/ && $1;
28	 $ident = $1 if $word !~ /!$/ && $word =~ /!?([^@]+)(@|$)/ && $1;
29	 $host = $1 if $word =~ /@(.*)$/ && $1;
30
31	 for my $n ($wi->nicks()) {
32		next if not_wild($nick) and $n->{nick} !~ /^\Q$nick\E/i;
33
34		my($user,$addr) = split(/@/, $n->{host});
35
36		next if not_wild($ident) and $user !~ /^\Q$ident\E/i;
37		next if not_wild($host) and $addr !~ /^\Q$host\E/i;
38
39		if($word =~ /!/) {
40		   push @$complist, get_match($n->{nick}, $nick) . '!' . get_match($user, $ident) . '@' . get_match($addr,$host);
41		}else{
42		   push @$complist, get_match($user, $ident) . '@' . get_match($addr,$host);
43		}
44	 }
45  }
46
47  return unless $servers{$tag};
48  for (keys %{$servers{$tag}}) {
49	 push @$complist, $_ if /^\Q$word\E/;
50  }
51}
52
53sub get_match {
54   my($match, $thing) = @_;
55   return $thing eq '*' ? '*' : $match;
56}
57
58sub not_wild {
59   return 0 if($_[0] eq '*' || $_[0] eq '');
60   1;
61}
62
63sub add_server {
64   my($tag,$data,$offset) = @_;
65   $servers{$tag}{(split(/ /,$data))[$offset]} = 1;
66}
67
68Irssi::signal_add_last('complete word', 'sig_complete');
69
70Irssi::signal_add('event 352', sub {
71   my($server,$data) = @_;
72   add_server($server->{tag}, $data, 4);
73} );
74
75Irssi::signal_add('event 312', sub {
76   my($server,$data) = @_;
77   add_server($server->{tag}, $data, 2);
78} );
79
80Irssi::signal_add('event 364', sub {
81   my($server,$data) = @_;
82   add_server($server->{tag}, $data, 1);
83   add_server($server->{tag}, $data, 2);
84} );
85
86