irssi 显IP插件

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

#QQWry.Dat放~/.irssi下,脚本改名叫wry.pl 放~/.irssi/scripts/,然后链接到~/.irssi/scripts/autorun
# include $whois_decip somewhere in your /FORMAT whois
# 作者 x11
# ikk- 改过

use Irssi 20011207;
use strict;
use vars qw($VERSION %IRSSI);
use Text::Iconv;
#use IP::QQWry;

$VERSION = "1.0";

%IRSSI = (
   authors => "x11",
   contact => "x11.vip\@gmail.org",
   name => "wry",
   description => "Print the locality of clients when they join/part channels, and whois.",
   license => "GPLv2 or later"
);

my $wrydata = Irssi::get_irssi_dir() . "/QQWry.Dat";
if (not -e $wrydata) {
  Irssi::print "can't find qqwry.dat, quit ";
  return;
}

sub wry2 {
  my ($host) =@_;

  $host =~ /.*\@(.*)$/;
  my $ip = $1;
  return '' if not $ip;
  return 'mibbit' if $ip =~ /mibbit\./;#不是Ip
  return 'WEB' if $ip == '59.36.101.19';
  $ip =~ s/\.\w*$/.1/ if $ip =~ /(\d{1,3}\.){3}\w{5,9}/ ; #替换 linuxfire 后ip
    
  if ($ip =~ /[a-zA-Z]/) { #取域名的IP host
    $ip =~ /^([\w\.\-\d]*)/;
    $ip = $1;
    
    return '' if $ip !~ /\w\./;#没有.; 至少要 xx.com
    my $tmp = qx{host $ip | head -n 1};
    $tmp =~ /((\d{1,3}\.){3}\d+)$/ ;
    
    $ip = $1;

    return '' if $ip !~ /\d\./;#不是Ip
  }

  my ($ipbegin,$ipend, $ipData1, $ipData2, $DataSeek ,$ipFlag);
  my ($ipAddr1,$ipAddr2);

  my @ip=split(/\./,$ip);

  my $ipNum;

  if ($ip[3]) {
    $ipNum = $ip[0]*16777216+$ip[1]*65536+$ip[2]*256+$ip[3];
  } else {
    $ipNum = $ip[0]*16777216+$ip[1]*65536+$ip[2]*256;
  }

  open(FILE,"$wrydata");
  binmode(FILE);
  sysread(FILE,$ipbegin,4);
  sysread(FILE,$ipend,4);
  $ipbegin=unpack("L",$ipbegin);
  $ipend=unpack("L",$ipend);
  my $ipAllNum = ($ipend-$ipbegin)/7+1;

  my $BeginNum=0;
  my $EndNum=$ipAllNum;

  Bgn:
  my $Middle= int(($EndNum+$BeginNum)/2);

  seek(FILE,$ipbegin+7*$Middle,0);
  read(FILE,$ipData1,4);
  my $ip1num=unpack("L",$ipData1);
  if ($ip1num > $ipNum) {
    $EndNum=$Middle;
    goto Bgn;
  }

  read(FILE,$DataSeek,3);
  $DataSeek=unpack("L",$DataSeek."\0");
  seek(FILE,$DataSeek,0);
  read(FILE,$ipData2,4);
  my $ip2num=unpack("L",$ipData2);
  if ($ip2num < $ipNum) {
    goto nd if ($Middle==$BeginNum);
    $BeginNum=$Middle;
    goto Bgn;
  }

  $/="\0";
  read(FILE,$ipFlag,1);
  if ($ipFlag eq "\1") {
    my $ipSeek;
    read(FILE,$ipSeek,3);
    $ipSeek = unpack("L",$ipSeek."\0");
    seek(FILE,$ipSeek,0);
    read(FILE,$ipFlag,1);
  }
  if ($ipFlag eq "\2") {
    my $AddrSeek;
    read(FILE,$AddrSeek,3);
    read(FILE,$ipFlag,1);
    if($ipFlag eq "\2") {
      my $AddrSeek2;
      read(FILE,$AddrSeek2,3);
      $AddrSeek2 = unpack("L",$AddrSeek2."\0");
      seek(FILE,$AddrSeek2,0);
    } else {
      seek(FILE,-1,1);
    }
    $ipAddr2=<FILE>;
    $AddrSeek = unpack("L",$AddrSeek."\0");
    seek(FILE,$AddrSeek,0);
    $ipAddr1=<FILE>;
  } else {
    seek(FILE,-1,1);
    $ipAddr1=<FILE>;
    read(FILE,$ipFlag,1);
    if($ipFlag eq "\2") {
      my $AddrSeek2;
      read(FILE,$AddrSeek2,3);
      $AddrSeek2 = unpack("L",$AddrSeek2."\0");
      seek(FILE,$AddrSeek2,0);
    } else {
      seek(FILE,-1,1);
    }
    $ipAddr2=<FILE>;
  }

  nd:
  close(FILE);
#$/="\n";
  my $iconv = Text::Iconv->new("gbk", "utf8");
  chomp($ipAddr1);
#chomp($ipAddr2);
#$ipAddr2 =~ s/\s*CZ88\.NET//;
#my $locality = $iconv->convert($ipAddr1.$ipAddr2);
  my $locality = $iconv->convert($ipAddr1);
  $locality =~ s/IANA//gi;
  return $locality;
}#wr y2

sub wry {
  my ($host) =@_;

  $host =~ /.*\@(.*)$/;
  my $ip = $1;
  $ip =~ s/\.\w*$/.1/;
  return '' if not $ip;

  my $qqwry = IP::QQWry->new($wrydata);
  my $iconv = Text::Iconv->new("gbk", "utf8");
  my $locality = $qqwry->query($ip);
  $qqwry->clear();
  $locality = $iconv->convert($locality) or $locality = '';
  $locality =~ s/IANA//gi;
  return $locality;
}

sub client_part {
  my ($server, $channame, $nick, $host) = @_;
  return if $host =~ /admin.linuxfire.com.cn$/;
  #print $channame , ' part ' , $nick ;
  my $locality = wry2($host);

  $channame =~ s/^://;
  my $channel = $server->channel_find($channame);
  #$channel->printformat(MSGLEVEL_PARTS, 'part_linuxfire', $nick, $host, $locality, $channel->{name});
  Irssi::signal_stop();
  return 0;
}

sub client_join {
  my ($server, $channame, $nick, $host) = @_;
  return if $host =~ /\@netadmin.linuxfire.com.cn$/;
  #print $channame , ' join ' , $nick ;
  my $locality = wry2($host);

  $channame =~ s/^://;
  my $channel = $server->channel_find($channame);
  $channel->printformat(MSGLEVEL_JOINS, 'join_linuxfire', $nick, $host, $locality, $channel->{name});
  Irssi::signal_stop();
  return 0;  
}

sub client_quit {
  my ($server, $nick, $host, $reason) = @_;
  return if $host =~ /admin.linuxfire.com.cn$/;
  #print ' quit ' , $nick ;

  my $locality = wry2($host);
  for my $channel (Irssi::channels()) {
  if ($channel->nick_find($nick)) {
    $channel->printformat(MSGLEVEL_QUITS, 'quit_linuxfire', $nick, $host, $locality, $reason);
  }
  }

  Irssi::signal_stop();
  return 0;
}

sub cmd_wry {
  my ($nick) = @_;
  my $channel = Irssi::active_win()->{active};
  $nick =~ s/\s//g;
  if (not $nick) {
    $channel->print("/wry nickname");
    return 0;
  }

  my $nickinfo = $channel->nick_find($nick);
  if ($nickinfo) {
  my $locality = wry2($nickinfo->{host});
  if ($locality) {
    $channel->print("$nick($nickinfo->{host}) is from $locality");
  }
  else {
    $channel->print("$nick($nickinfo->{host}) is from ");
  }
  }
  else {
    $channel->print("no such nick: $nick");
  }
  return 0;
}

Irssi::theme_register([
'join_linuxfire', '=> {hilight $2} {channick_hilight $0} {chanhost_hilight $1} has joined {channel $3}',
'part_linuxfire', '<= $2 {channick $0} {chanhost $1} has left {channel $3}',
'quit_linuxfire', '<= $2 {channick $0} {chanhost $1} has quit [$3]',
]);

#Irssi::expando_create('whois_decip', \&expando_decip, { 'event 311' => 'None' } );
#Irssi::signal_add_first('event 311', 'whois_signal');
Irssi::signal_add('message join', 'client_join');
Irssi::signal_add('message part', 'client_part');
Irssi::signal_add('message quit', 'client_quit');
Irssi::command_bind('wry', 'cmd_wry');