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