]>
jfr.im git - irc/quakenet/newserv.git/blob - geoip/geoip.c
3 Copyright (C) 2004-2006 Chris Porter.
6 #include "../nick/nick.h"
7 #include "../core/error.h"
8 #include "../core/config.h"
9 #include "../core/hooks.h"
10 #include "../control/control.h"
11 #include "../lib/version.h"
13 #include "libGeoIP/GeoIP.h"
16 #warning This module is GPL'ed. Load/link at your peril.
20 int geoip_totals[COUNTRY_MAX + 1];
21 int geoip_nickext = -1;
24 void geoip_setupuser(nick *np);
26 void geoip_new_nick(int hook, void *args);
27 void geoip_quit(int hook, void *args);
28 void geoip_whois_handler(int hooknum, void *arg);
35 filename = getcopyconfigitem("geoip", "db", "GeoIP.dat", 256);
36 gi = GeoIP_new(GEOIP_MEMORY_CACHE, filename->content);
37 freesstring(filename);
42 geoip_nickext = registernickext("geoip");
43 if(geoip_nickext == -1)
44 return; /* PPA: registerchanext produces an error, however the module should stop loading */
46 memset(geoip_totals, 0, sizeof(geoip_totals));
48 for(i=0;i<NICKHASHSIZE;i++)
49 for(np=nicktable[i];np;np=np->next)
52 registerhook(HOOK_NICK_LOSTNICK, &geoip_quit);
53 registerhook(HOOK_NICK_NEWNICK, &geoip_new_nick);
54 registerhook(HOOK_CONTROL_WHOISREQUEST, &geoip_whois_handler);
61 if(geoip_nickext == -1)
64 releasenickext(geoip_nickext);
66 deregisterhook(HOOK_NICK_NEWNICK, &geoip_new_nick);
67 deregisterhook(HOOK_NICK_LOSTNICK, &geoip_quit);
68 deregisterhook(HOOK_CONTROL_WHOISREQUEST, &geoip_whois_handler);
71 void geoip_setupuser(nick *np) {
72 int country = GeoIP_id_by_ipnum(gi, np->ipaddress);
73 if((country < COUNTRY_MIN) || (country > COUNTRY_MAX))
76 geoip_totals[country]++;
77 np->exts[geoip_nickext] = (void *)(long)country;
80 void geoip_new_nick(int hook, void *args) {
81 geoip_setupuser((nick *)args);
84 void geoip_quit(int hook, void *args) {
86 nick *np = (nick *)args;
88 item = (int)((long)np->exts[geoip_nickext]);
90 if((item < COUNTRY_MIN) || (item > COUNTRY_MAX))
96 void geoip_whois_handler(int hooknum, void *arg) {
98 char message[512], *longcountry, *shortcountry, *shortcountry3;
99 nick *np = (nick *)arg;
104 item = (int)((long)np->exts[geoip_nickext]);
105 if((item < COUNTRY_MIN) || (item > COUNTRY_MAX))
108 if(GeoIP_country_info_by_id(item, &shortcountry, &shortcountry3, &longcountry)) {
109 snprintf(message, sizeof(message), "Country : %s (%s)", shortcountry, longcountry);
110 triggerhook(HOOK_CONTROL_WHOISREPLY, message);