]> jfr.im git - irc/quakenet/newserv.git/blob - geoip/geoip.c
oops - forgot to hg merge slugs changes before
[irc/quakenet/newserv.git] / geoip / geoip.c
1 /*
2 Geoip module
3 Copyright (C) 2004-2006 Chris Porter.
4 */
5
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"
12
13 #include "libGeoIP/GeoIP.h"
14 #include "geoip.h"
15
16 #warning This module is GPL'ed. Load/link at your peril.
17
18 MODULE_VERSION("");
19
20 int geoip_totals[COUNTRY_MAX + 1];
21 int geoip_nickext = -1;
22 GeoIP *gi = NULL;
23
24 void geoip_setupuser(nick *np);
25
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);
29
30 void _init(void) {
31 int i;
32 nick *np;
33 sstring *filename;
34
35 filename = getcopyconfigitem("geoip", "db", "GeoIP.dat", 256);
36 gi = GeoIP_new(GEOIP_MEMORY_CACHE, filename->content);
37 freesstring(filename);
38
39 if(!gi)
40 return;
41
42 geoip_nickext = registernickext("geoip");
43 if(geoip_nickext == -1)
44 return; /* PPA: registerchanext produces an error, however the module should stop loading */
45
46 memset(geoip_totals, 0, sizeof(geoip_totals));
47
48 for(i=0;i<NICKHASHSIZE;i++)
49 for(np=nicktable[i];np;np=np->next)
50 geoip_setupuser(np);
51
52 registerhook(HOOK_NICK_LOSTNICK, &geoip_quit);
53 registerhook(HOOK_NICK_NEWNICK, &geoip_new_nick);
54 registerhook(HOOK_CONTROL_WHOISREQUEST, &geoip_whois_handler);
55 }
56
57 void _fini(void) {
58 if(gi)
59 GeoIP_delete(gi);
60
61 if(geoip_nickext == -1)
62 return;
63
64 releasenickext(geoip_nickext);
65
66 deregisterhook(HOOK_NICK_NEWNICK, &geoip_new_nick);
67 deregisterhook(HOOK_NICK_LOSTNICK, &geoip_quit);
68 deregisterhook(HOOK_CONTROL_WHOISREQUEST, &geoip_whois_handler);
69 }
70
71 void geoip_setupuser(nick *np) {
72 int country = GeoIP_id_by_ipnum(gi, np->ipaddress);
73 if((country < COUNTRY_MIN) || (country > COUNTRY_MAX))
74 return;
75
76 geoip_totals[country]++;
77 np->exts[geoip_nickext] = (void *)(long)country;
78 }
79
80 void geoip_new_nick(int hook, void *args) {
81 geoip_setupuser((nick *)args);
82 }
83
84 void geoip_quit(int hook, void *args) {
85 int item;
86 nick *np = (nick *)args;
87
88 item = (int)((long)np->exts[geoip_nickext]);
89
90 if((item < COUNTRY_MIN) || (item > COUNTRY_MAX))
91 return;
92
93 geoip_totals[item]--;
94 }
95
96 void geoip_whois_handler(int hooknum, void *arg) {
97 int item;
98 char message[512], *longcountry, *shortcountry, *shortcountry3;
99 nick *np = (nick *)arg;
100
101 if(!np)
102 return;
103
104 item = (int)((long)np->exts[geoip_nickext]);
105 if((item < COUNTRY_MIN) || (item > COUNTRY_MAX))
106 return;
107
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);
111 }
112 }
113