]>
jfr.im git - irc/SurrealServices/srsv.git/blob - branches/erry-devel/modules/country.pm
1 # This file is part of SurrealServices.
3 # SurrealServices is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # SurrealServices is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with SurrealServices; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 # Copyright tabris@surrealchat.net (C) 2005
22 use SrSv
::MySQL
'$dbh';
23 use SrSv
::Process
::Init
;
24 use SrSv
::IRCd
::Event
'addhandler';
25 use SrSv
::IRCd
::State
'initial_synced';
29 use SrSv
::Shared
qw(%unwhois);
31 use SrSv
::User
qw(get_user_id);
33 addhandler
('USERIP', undef, undef, 'userip');
34 addhandler
('NICKCONN', undef, undef, 'nickconn');
36 our ($get_ip_country, $get_ip_country_aton, $get_user_country);
39 $get_ip_country = $dbh->prepare_cached("SELECT country FROM country WHERE
40 ? BETWEEN low AND high");
41 $get_ip_country_aton = $dbh->prepare_cached("SELECT country FROM country WHERE
42 INET_ATON(?) BETWEEN low AND high");
43 $get_user_country = $dbh->prepare_cached("SELECT country FROM country, user WHERE
44 user.ip BETWEEN low AND high and user.id=?");
47 sub get_ip_country
($) {
50 $get_ip_country->execute($ip);
51 my ($country) = $get_ip_country->fetchrow_array();
52 $get_ip_country->finish();
57 sub get_ip_country_aton
($) {
58 # IP is expected to be a dotted quad string!
61 $get_ip_country_aton->execute($ip);
62 my ($country) = $get_ip_country_aton->fetchrow_array();
63 $get_ip_country_aton->finish();
64 #my ($country)= $dbh->selectrow_array(
65 # "SELECT `country` FROM `country` WHERE `low` < INET_ATON('$ip') AND `high` > INET_ATON('$ip')");
71 sub get_user_country
($) {
72 # Preferred to use this if you have a $user hash and you've set the IP.
73 # it should return undef in the case of user.ip == 0
74 # do check this case in the caller before assuming the return value is valid.
77 $get_user_country->execute(get_user_id
($user));
78 my ($country) = $get_user_country->fetchrow_array();
79 $get_user_country->finish();
84 sub get_country_long
($) {
85 # I'd prefer that this be used by the callers of get_user_country()
86 # If they need the long country name,
87 # they can use country::get_country_long(country::get_user_country($user))
88 # that way the get_{user,ip}_country functions get back an easily parsed value.
90 $country = uc $country;
92 my $cname = $core::ccode
{$country};
93 $country .= " ($cname)" if $cname;
95 return $country if $cname;
99 sub get_user_country_long
($) {
101 return get_country_long
(get_user_country
($user));
105 my ($rnick, $time, $ident, $host, $vhost, $server, $modes, $gecos, $ip, $svsstamp) = @_[0,2..4,8,5,7,9,10,6];
106 if(initial_synced
() && !$svsstamp) {
108 wlog
($main::rsnick
, LOG_INFO
(), "\002$rnick\002 is connecting from ".
109 get_country_long
(get_ip_country_aton
($ip)));
112 $unwhois{lc $rnick} = 1;
115 # we already depend on services being up for our SQL,
116 # thus we know a USERIP will be sent.
117 # However this IS avoidable if we make our own SQL connection
118 # but would then require an additional %config and configfile
123 my($src, $nick, $ip) = @_;
125 return unless($unwhois{lc $nick});
126 return unless($ip =~ /^\d{1,3}(\.\d{1,3}){3}$/);
128 wlog
($main::rsnick
, LOG_INFO
(), "\002$nick\002 is connecting from ".
129 get_country_long
(get_ip_country_aton
($ip)));
130 delete $unwhois{lc $nick};
135 sub end
() { %unwhois = undef(); }