]>
Commit | Line | Data |
---|---|---|
cc3768c4 CP |
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 | ||
b2182a68 | 12 | #include "libGeoIP/GeoIP.h" |
cc3768c4 CP |
13 | #include "geoip.h" |
14 | ||
15 | int geoip_totals[COUNTRY_MAX + 1]; | |
16 | int geoip_nickext = -1; | |
17 | GeoIP *gi = NULL; | |
18 | ||
19 | void geoip_setupuser(nick *np); | |
20 | ||
21 | void geoip_new_nick(int hook, void *args); | |
22 | void geoip_quit(int hook, void *args); | |
23 | void geoip_whois_handler(int hooknum, void *arg); | |
24 | ||
25 | void _init(void) { | |
26 | int i; | |
27 | nick *np; | |
28 | sstring *filename; | |
29 | ||
30 | filename = getcopyconfigitem("geoip", "db", "GeoIP.dat", 256); | |
31 | gi = GeoIP_new(GEOIP_MEMORY_CACHE, filename->content); | |
32 | freesstring(filename); | |
33 | ||
34 | if(!gi) | |
35 | return; | |
36 | ||
37 | geoip_nickext = registernickext("geoip"); | |
38 | if(geoip_nickext == -1) | |
39 | return; /* PPA: registerchanext produces an error, however the module should stop loading */ | |
40 | ||
41 | memset(geoip_totals, 0, sizeof(geoip_totals)); | |
42 | ||
43 | for(i=0;i<NICKHASHSIZE;i++) | |
44 | for(np=nicktable[i];np;np=np->next) | |
45 | geoip_setupuser(np); | |
46 | ||
47 | registerhook(HOOK_NICK_LOSTNICK, &geoip_quit); | |
48 | registerhook(HOOK_NICK_NEWNICK, &geoip_new_nick); | |
49 | registerhook(HOOK_CONTROL_WHOISREQUEST, &geoip_whois_handler); | |
50 | } | |
51 | ||
52 | void _fini(void) { | |
53 | if(gi) | |
54 | GeoIP_delete(gi); | |
55 | ||
56 | if(geoip_nickext == -1) | |
57 | return; | |
58 | ||
59 | releasenickext(geoip_nickext); | |
60 | ||
61 | deregisterhook(HOOK_NICK_NEWNICK, &geoip_new_nick); | |
62 | deregisterhook(HOOK_NICK_LOSTNICK, &geoip_quit); | |
63 | deregisterhook(HOOK_CONTROL_WHOISREQUEST, &geoip_whois_handler); | |
64 | } | |
65 | ||
66 | void geoip_setupuser(nick *np) { | |
67 | int country = GeoIP_id_by_ipnum(gi, np->ipaddress); | |
68 | if((country < COUNTRY_MIN) || (country > COUNTRY_MAX)) | |
69 | return; | |
70 | ||
71 | geoip_totals[country]++; | |
72 | np->exts[geoip_nickext] = (void *)(long)country; | |
73 | } | |
74 | ||
75 | void geoip_new_nick(int hook, void *args) { | |
76 | geoip_setupuser((nick *)args); | |
77 | } | |
78 | ||
79 | void geoip_quit(int hook, void *args) { | |
80 | int item; | |
81 | nick *np = (nick *)args; | |
82 | ||
83 | item = (int)((long)np->exts[geoip_nickext]); | |
84 | ||
85 | if((item < COUNTRY_MIN) || (item > COUNTRY_MAX)) | |
86 | return; | |
87 | ||
88 | geoip_totals[item]--; | |
89 | } | |
90 | ||
91 | void geoip_whois_handler(int hooknum, void *arg) { | |
92 | int item; | |
93 | char message[512], *longcountry, *shortcountry, *shortcountry3; | |
94 | nick *np = (nick *)arg; | |
95 | ||
96 | if(!np) | |
97 | return; | |
98 | ||
99 | item = (int)((long)np->exts[geoip_nickext]); | |
100 | if((item < COUNTRY_MIN) || (item > COUNTRY_MAX)) | |
101 | return; | |
102 | ||
103 | if(GeoIP_country_info_by_id(item, &shortcountry, &shortcountry3, &longcountry)) { | |
104 | snprintf(message, sizeof(message), "Country : %s (%s)", shortcountry, longcountry); | |
105 | triggerhook(HOOK_CONTROL_WHOISREPLY, message); | |
106 | } | |
107 | } | |
108 |