#include "../control/control.h"
#include "../lib/version.h"
+#include <strings.h>
+
#include "libGeoIP/GeoIP.h"
#include "geoip.h"
MODULE_VERSION("");
int geoip_totals[COUNTRY_MAX + 1];
-int geoip_nickext = -1;
-GeoIP *gi = NULL;
+static int geoip_nickext = -1;
+static GeoIP *gi = NULL;
-void geoip_setupuser(nick *np);
+static void geoip_setupuser(nick *np);
-void geoip_new_nick(int hook, void *args);
-void geoip_quit(int hook, void *args);
-void geoip_whois_handler(int hooknum, void *arg);
+static void geoip_new_nick(int hook, void *args);
+static void geoip_quit(int hook, void *args);
+static void geoip_whois_handler(int hooknum, void *arg);
void _init(void) {
int i;
sstring *filename;
filename = getcopyconfigitem("geoip", "db", "GeoIP.dat", 256);
- gi = GeoIP_new(GEOIP_MEMORY_CACHE, filename->content);
+ gi = GeoIP_open(filename->content, GEOIP_MEMORY_CACHE);
freesstring(filename);
if(!gi)
deregisterhook(HOOK_CONTROL_WHOISREQUEST, &geoip_whois_handler);
}
-void geoip_setupuser(nick *np) {
- int country = GeoIP_id_by_ipnum(gi, np->ipaddress);
+static void geoip_setupuser(nick *np) {
+ if (!irc_in_addr_is_ipv4(&np->ipaddress))
+ return; /* geoip only supports ipv4 */
+
+ unsigned int ip = irc_in_addr_v4_to_int(&np->ipaddress);
+ int country = GeoIP_id_by_ipnum(gi, ip);
if((country < COUNTRY_MIN) || (country > COUNTRY_MAX))
return;
np->exts[geoip_nickext] = (void *)(long)country;
}
-void geoip_new_nick(int hook, void *args) {
+static void geoip_new_nick(int hook, void *args) {
geoip_setupuser((nick *)args);
}
-void geoip_quit(int hook, void *args) {
+static void geoip_quit(int hook, void *args) {
int item;
nick *np = (nick *)args;
geoip_totals[item]--;
}
-void geoip_whois_handler(int hooknum, void *arg) {
+static void geoip_whois_handler(int hooknum, void *arg) {
int item;
- char message[512], *longcountry, *shortcountry, *shortcountry3;
+ char message[512];
+ const char *longcountry, *shortcountry;
nick *np = (nick *)arg;
if(!np)
if((item < COUNTRY_MIN) || (item > COUNTRY_MAX))
return;
- if(GeoIP_country_info_by_id(item, &shortcountry, &shortcountry3, &longcountry)) {
+ shortcountry = GeoIP_code_by_id(item);
+ longcountry = GeoIP_name_by_id(item);
+
+ if(shortcountry && longcountry) {
snprintf(message, sizeof(message), "Country : %s (%s)", shortcountry, longcountry);
triggerhook(HOOK_CONTROL_WHOISREPLY, message);
}
}
+int geoip_lookupcode(char *code) {
+ int i;
+ for(i=COUNTRY_MIN;i<COUNTRY_MAX;i++)
+ if(!strcasecmp(code, GeoIP_country_code[i]))
+ return i;
+
+ return -1;
+}