html/whois_hexip.pl


   1 #!/usr/bin/perl
   2 # whois_hexip irssi module
   3 # written by Michael Kowalchuk <michael_kowalchuk@umanitoba.ca>
   4 #
   5 # Every time a WHOIS or WHOWAS is run, this script checks the
   6 # ident and realname for a hex encoded IP address, then decodes
   7 # it, optionally reverses it, and adds it to the printed WHOIS
   8 # result. Useful for looking at CGI::IRC clients.
   9 #
  10 # dns lookups are blocking, so if you find them to be slow, disable
  11 # lookups with the whois_hexip_lookup option.
  12 #
  13 # usage:
  14 #
  15 # put this script into your autorun directory and/or load it with
  16 #  /SCRIPT LOAD whois_hexip
  17 #
  18 # there is 1 setting:
  19 #  /set whois_hexip_lookup ON/OFF
  20 #
  21 # lookup means attempt to get the hostname; if it is off you will
  22 # only get the IP address
  23 #
  24 #
  25 # changes:
  26 #  24.12.2005 fix msg levels so the hexip won't end up in the wrong win
  27 #  10.10.2005 allowed idents to start with ~
  28 #  07.09.2005 limit to 32 bit numbers
  29 #  28.07.2005 changed realname matching to look for 0 or 1 non-word
  30 #             characters on either side of the hexadecimal number
  31 #             for less false-positives
  32 #  13.07.2005 added max_realname_nonhex
  33 #  12.07.2005 initial release
  34 #
  35 
  36 use strict;
  37 use Irssi;
  38 use Socket;
  39 
  40 
  41 use vars qw($VERSION %IRSSI);
  42 
  43 $VERSION = "1.4";
  44 %IRSSI = (
  45     authors     => "Michael Kowalchuk",
  46     contact     => "michael_kowalchuk\@umanitoba.ca",
  47     name        => "whois_hexip",
  48     description => "Every time a WHOIS or WHOWAS is run, this script checks the ident and realname for a hex encoded IP address, then decodes it, reverses it, and adds it to the printed WHOIS/WHOWAS result. Useful for looking at CGI::IRC clients.",
  49     license     => "MIT",
  50     url         => "http://home.cc.umanitoba.ca/~umkowa17/junk/whois_hexip.pl",
  51     changed     => "12.24.2005",
  52 );
  53 
  54 my $hexreg = "[A-Fa-f0-9]+";
  55 
  56 sub event_server_event {
  57 	my ($server, $text) = @_;
  58 
  59 	# Look up the ident and whois
  60 	my @items = split(/ /,$text);
  61 
  62 	my $ident = $items[2];
  63 	$ident =~ s/^~//;
  64 	
  65 	# CGI::IRC can put the IP in the WHOIS too!  Thanks mef
  66 	my $whois = join(" ",  @items[5 .. @items] );
  67 	$whois =~ s/^://; # Remove the initial :
  68 	$whois =~ s/\s+$//; # and any trailing whitespace
  69 
  70 	# Set $num to whatever string holds the hex ip, with
  71 	# priority given to the ident
  72 	my @numarray = undef;
  73 	@numarray = ($ident =~ /^\~?($hexreg)$/ );
  74 	@numarray = ($whois =~ /^($hexreg)$/) if @numarray eq 0;
  75 	@numarray = ($whois =~ /^[^\w]($hexreg)[^\w]$/) if @numarray eq 0;
  76 
  77 	my $num = $numarray[0] if @numarray gt 0;	
  78 
  79 	if( $num and length($num) <= 8 ) {
  80 		my $ip = inet_aton(hex $num);
  81 		my $display = gethostbyaddr($ip, AF_INET)
  82 			if Irssi::settings_get_bool($IRSSI{'name'}."_lookup");
  83 
  84 		# If there's a DNS timeout rather than an NXDOMAIN,
  85 		# you get an empty string rather than undefined
  86 		if( not defined $display or ( $display eq "" )  ) {
  87 			$display = inet_ntoa($ip)
  88 		}
  89 	
  90 		$server->printformat($items[1], MSGLEVEL_CRAP, $IRSSI{'name'},
  91 						$items[1], $display );
  92 	}
  93 }
  94 
  95 
  96 Irssi::theme_register([$IRSSI{'name'} => '{whois hexip %|$1}']);
  97 
  98 Irssi::signal_add_last('event 311', 'event_server_event'); # WHOIS
  99 Irssi::signal_add_last('event 314', 'event_server_event'); # WHOWAS
 100 
 101 Irssi::settings_add_bool('misc', $IRSSI{'name'} . '_lookup', 1);
 102