html/fcountry.pl
1 # Print the country name in /WHOIS replies
2 # /FCOUNTRY <URI|IP> prints where a URI or IP is hosted.
3 # Installation: Add $whois_fcountry somewhere in your /FORMAT whois line
4
5 ######
6 # Copyright (c) 2008 Stefan Jakobs
7 #
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License
10 # as published by the Free Software Foundation; either version 2
11 # of the License, or (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 #
23 #####################################################################
24
25 use strict;
26 use Irssi 20021028;
27 use IP::Country::Fast;
28 use Geography::Countries;
29
30 use vars qw($VERSION %IRSSI);
31 $VERSION = "1.0.0";
32 %IRSSI = (
33 authors => "Stefan Jakobs",
34 contact => "stefan\.jakobs\@rus\.uni-stuttgart\.de",
35 name => "fast_country",
36 description => "Print the country name in /WHOIS replies",
37 license => "GPLv2",
38 changed => "Son May 11 20:20: CET 2008",
39 modules => "IP::Country::Fast Geography::Countries",
40 commands => "fcountry"
41 );
42
43 my $last_country = "";
44 my $short_country = "";
45 my $reg = IP::Country::Fast->new();
46
47 sub show_help() {
48 my $help = $IRSSI{name} ." " .$VERSION ."
49 Add \$whois_fcountry somewhere in your \'/FORMAT whois\' line
50
51 /fcountry <URI|IP>
52 prints were the specified URI or IP is hosted
53 /fcountry help
54 prints this message
55 ";
56 my $text = '';
57 foreach (split(/\n/, $help)) {
58 $_ =~ s/^\/(.*)$/%9\/$1%9/;
59 $text .= $_."\n";
60 }
61 print CLIENTCRAP "\n" .$text;
62 }
63
64 sub sig_whois {
65 my ($server, $data, $nick, $host) = @_;
66 my ($me, $nick, $user, $host) = split(" ", $data);
67
68 $short_country = $reg->inet_atocc($host);
69 if ($short_country) { $last_country = country $short_country; }
70 else { $last_country = ""; }
71 }
72
73 sub expando_whois_country {
74 if (!$short_country) {
75 return ">unknown<";
76 } else {
77 return $last_country ."\(" .$short_country ."\)";
78 }
79 }
80
81 sub cmd_country {
82 my $url_ip = lc shift;
83 if ($url_ip eq 'help') {
84 show_help();
85 } elsif ($url_ip eq "") {
86 Irssi::print("USAGE: /FCOUNTRY <URL|IP>");
87 } else {
88 my $short = $reg->inet_atocc($url_ip);
89 if (!$short) {
90 Irssi::print("Unknown country origin: $url_ip");
91 } else {
92 my $name = country $short;
93 if (!$name) { Irssi::print("$url_ip is hosted in $short"); }
94 else { Irssi::print("$url_ip is hosted in $name \($short\)"); }
95 }
96 }
97 }
98
99 Irssi::command_bind('fcountry', \&cmd_country);
100 Irssi::signal_add_first('event 311', \&sig_whois);
101 Irssi::expando_create('whois_fcountry', \&expando_whois_country,
102 { 'event 311' => 'None' } );
103 print CLIENTCRAP "%B>>%n ".$IRSSI{name}." ".$VERSION." loaded: \'/fcountry help\' for help"