html/hostname.pl
1 # $Id: hostname.pl,v 1.8 2002/07/04 13:18:02 jylefort Exp $
2
3 use Irssi 20020121.2020 ();
4 $VERSION = "1.01";
5 %IRSSI = (
6 authors => 'Jean-Yves Lefort',
7 contact => 'jylefort\@brutele.be, decadix on IRCNet',
8 name => 'hostname',
9 description => 'Adds a /HOSTNAME command; it will list all IP addresses on all interfaces found on your machine, resolve them, and allow you to choose one easily',
10 license => 'BSD',
11 url => 'http://void.adminz.be/irssi.shtml',
12 changed => '$Date: 2002/07/04 13:18:02 $ ',
13 );
14
15 # description:
16 #
17 # hostname.pl will add a /HOSTNAME command similar to the one that can
18 # be found in BitchX.
19 #
20 # /HOSTNAME will list all IP addresses of your system, and resolve them.
21 # /HOSTNAME <index> will switch to the selected IP address.
22 #
23 # The IP addresses are collected by running ifconfig and parsing
24 # the output. It has been tested on the following systems:
25 #
26 # FreeBSD 4.4-RELEASE
27 # FreeBSD 4.5-RELEASE
28 # NetBSD 1.5.2
29 # Linux 2.4.16
30 # IRIX 6.5
31 # OSF/1 4.0
32 # SunOS 5.8
33 #
34 # It will probably work on any recent version of the following systems:
35 #
36 # FreeBSD
37 # NetBSD
38 # OpenBSD
39 # Linux
40 # IRIX
41 # OSF/1 / Tru64
42 # SunOS / Solaris
43 #
44 # It may or may not work on other systems / versions, but it will not
45 # work on the following pieces of crap:
46 #
47 # M$-DO$ all versions
48 # Windoze all versions
49 #
50 # You'll also need to have the module Socket6.pm installed, the address
51 # resolution needs it; on FreeBSD it can be installed easily by typing
52 # cd /usr/ports/net/p5-Socket6 && make install
53 #
54 # /format's:
55 #
56 # hostname
57 #
58 # $0 index number
59 # $1 IP address
60 # $2 hostname
61 #
62 # new theme abstracts:
63 #
64 # Insert the following in the abstracts section of your theme file:
65 #
66 # index = "[$*]";
67 # ip = "%g$*%n";
68 # hostname = "{comment $*}";
69 #
70 # usage:
71 #
72 # /HOSTNAME [<index>]
73 #
74 # Without arguments, display the list of IP addresses and resolve them.
75 #
76 # With a numerical argument, set the hostname setting to the IP
77 # address matching that index in the list.
78 #
79 # acknowledgements:
80 #
81 # The following people have sent the ifconfig output of their system:
82 # darix, plett, zur
83 #
84 # changes:
85 #
86 # 2002-07-04 release 1.01
87 # * command_bind uses a reference instead of a string
88 #
89 # 2002-04-25 release 1.00
90 # * increased version number
91 #
92 # 2002-02-02 release 0.02
93 # * reads ifconfig output one line at a time
94 # * excluded too many IP addresses in result: fixed
95 # * much '2' today ;)
96 #
97 # 2002-02-01 initial release
98
99 use strict;
100 use Socket;
101 use Socket6;
102
103 my %addresses;
104
105 sub hostname {
106 my ($args, $server, $item) = @_;
107
108 get_addresses();
109 if ($args) {
110 set_address($args);
111 } else {
112 print_addresses();
113 }
114 }
115
116 sub get_addresses {
117 Irssi::print("Resolving IP addresses...");
118 %addresses = ();
119 open(IFCONFIG, "ifconfig|");
120 while (<IFCONFIG>) {
121 $addresses{$2} = resolve($2)
122 if (/(inet addr:|inet6 addr: |inet |inet6 )([0-9a-f.:]*)/
123 && ! ($2 =~ /^(127\.0\.0\.1|::1|fe80:.*)$/));
124 }
125 close(IFCONFIG);
126 }
127
128 sub print_addresses {
129 my $i = 0;
130 Irssi::printformat(MSGLEVEL_CRAP, "hostname", ++$i, $_, $addresses{$_})
131 foreach (keys %addresses);
132 }
133
134 sub set_address {
135 my ($index, $i) = (shift, 0);
136 foreach (keys %addresses) {
137 if (++$i == $index) {
138 Irssi::print("Hostname set to $_");
139 Irssi::command("^SET HOSTNAME $_");
140 return;
141 }
142 }
143 Irssi::print("Hostname #$index not found", MSGLEVEL_CLIENTERROR);
144 }
145
146 sub resolve {
147 my $ip = shift;
148 my @res = getaddrinfo($ip, 0, AF_UNSPEC, SOCK_STREAM);
149 my ($name, $port) = getnameinfo($res[3]);
150 return $name;
151 }
152
153 Irssi::theme_register(['hostname',
154 '{index $0} {ip $[20]1} {hostname $[39]2}']);
155
156 Irssi::command_bind("hostname", \&hostname);