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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
# /DNS <nick>|<host>|<ip> ...
# version 2.1.1
#
# updated the script to fix a bug where the script would let
# a trailing whitespace go through (ex: tab completion)
# - inch <inch@stmpd.net>
use strict;
use Socket;
use POSIX;
use vars qw($VERSION %IRSSI);
$VERSION = "2.1.1";
%IRSSI = (
authors => "Timo \'cras\' Sirainen",
contact => "tss\@iki.fi",
name => "dns",
description => "/DNS <nick>|<host>|<ip> ...",
license => "Public Domain",
url => "http://irssi.org/",
changed => "2002-03-04T22:47+0100"
);
my (%resolve_hosts, %resolve_nicks, %resolve_print); # resolve queues
my $userhosts; # number of USERHOSTs currently waiting for reply
my $lookup_waiting; # 1 if we're waiting a reply for host lookup
# for the current host lookup
my ($print_server, $print_host, $print_name, @print_ips);
my ($input_skip_next, $input_query);
my $pipe_tag;
sub cmd_dns {
my ($nicks, $server) = @_;
return if !$nicks;
$nicks =~ s/\s+$//;
# get list of nicks/hosts we want to know
my $tag = !$server ? undef : $server->{tag};
my $ask_nicks = "";
my $print_error = 0;
foreach my $nick (split(" ", $nicks)) {
$nick = lc($nick);
if ($nick =~ /[\.:]/) {
# it's an IP or hostname
$resolve_hosts{$nick} = $tag;
} else {
# it's nick
if (!$print_error && (!$server || !$server->{connected})) {
$print_error = 1;
Irssi::print("Not connected to server");
} else {
$resolve_nicks{$nick} = 1;
$ask_nicks .= "$nick ";
}
}
}
if ($ask_nicks ne "") {
# send the USERHOST query
$userhosts++;
$server->redirect_event('userhost', 1, $ask_nicks, 0, 'redir dns failure', {
'event 302' => 'redir dns host',
'' => 'event empty' } );
$server->send_raw("USERHOST :$nicks");
}
# ask the IPs/hostnames immediately
host_lookup() if (!$lookup_waiting);
}
sub sig_failure {
Irssi::print("Error getting hostname for nick");
%resolve_nicks = () if (--$userhosts == 0);
}
sub sig_userhost {
my ($server, $data) = @_;
$data =~ s/^[^ ]* :?//;
my @hosts = split(/ +/, $data);
# move resolve_nicks -> resolve_hosts
foreach my $host (@hosts) {
if ($host =~ /^([^=\*]*)\*?=.(.*)@(.*)/) {
my $nick = lc($1);
my $user = $2;
$host = lc($3);
$resolve_hosts{$host} = $resolve_nicks{$nick};
delete $resolve_nicks{$nick};
$resolve_print{$host} = "[$nick!$user"."@"."$host]";
}
}
if (--$userhosts == 0 && %resolve_nicks) {
# unknown nicks - they didn't contain . or : so it can't be
# IP or hostname.
Irssi::print("Unknown nicks: ".join(' ', keys %resolve_nicks));
%resolve_nicks = ();
}
host_lookup() if (!$lookup_waiting);
}
sub host_lookup {
return if (!%resolve_hosts);
my ($host) = keys %resolve_hosts;
$print_server = $resolve_hosts{$host};
$print_host = undef;
$print_name = $resolve_print{$host};
@print_ips = ();
delete $resolve_hosts{$host};
delete $resolve_print{$host};
$input_query = $host;
$input_skip_next = 0;
# pipe is used to get the reply from child
my ($rh, $wh);
pipe($rh, $wh);
# non-blocking host lookups with fork()ing
my $pid = fork();
if (!defined($pid)) {
%resolve_hosts = ();
%resolve_print = ();
Irssi::print("Can't fork() - aborting");
close($rh); close($wh);
return;
}
$lookup_waiting++;
if ($pid > 0) {
# parent, wait for reply
close($wh);
Irssi::pidwait_add($pid);
$pipe_tag = Irssi::input_add(fileno($rh), INPUT_READ, \&pipe_input, $rh);
return;
}
my $text;
eval {
# child, do the lookup
my $name = "";
if ($host =~ /^[0-9\.]*$/) {
# ip -> host
$name = gethostbyaddr(inet_aton($host), AF_INET);
} else {
# host -> ip
my @addrs = gethostbyname($host);
if (@addrs) {
@addrs = map { inet_ntoa($_) } @addrs[4 .. $#addrs];
$name = join (" ", @addrs);
}
}
$print_name = $input_query if !$print_name;
if (!$name) {
$text = "No information for $print_name";
} else {
$text = "$print_name: $name";
}
};
$text = $! if (!$text);
eval {
# write the reply
print($wh $text);
close($wh);
};
POSIX::_exit(1);
}
sub pipe_input {
my $rh = shift;
my $text = <$rh>;
close($rh);
Irssi::input_remove($pipe_tag);
$pipe_tag = -1;
my $server = Irssi::server_find_tag($print_server);
if ($server) {
$server->print('', $text);
} else {
Irssi::print($text);
}
$lookup_waiting--;
host_lookup();
}
Irssi::command_bind('dns', 'cmd_dns');
Irssi::signal_add( {
'redir dns failure' => \&sig_failure,
'redir dns host' => \&sig_userhost } );
|