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 MODULE_VERSION("$Id: geoip.c 663 2006-05-16 17:27:36Z newserv $")
18 int geoip_totals
[COUNTRY_MAX
+ 1];
19 int geoip_nickext
= -1;
22 void geoip_setupuser(nick
*np
);
24 void geoip_new_nick(int hook
, void *args
);
25 void geoip_quit(int hook
, void *args
);
26 void geoip_whois_handler(int hooknum
, void *arg
);
33 filename
= getcopyconfigitem("geoip", "db", "GeoIP.dat", 256);
34 gi
= GeoIP_new(GEOIP_MEMORY_CACHE
, filename
->content
);
35 freesstring(filename
);
40 geoip_nickext
= registernickext("geoip");
41 if(geoip_nickext
== -1)
42 return; /* PPA: registerchanext produces an error, however the module should stop loading */
44 memset(geoip_totals
, 0, sizeof(geoip_totals
));
46 for(i
=0;i
<NICKHASHSIZE
;i
++)
47 for(np
=nicktable
[i
];np
;np
=np
->next
)
50 registerhook(HOOK_NICK_LOSTNICK
, &geoip_quit
);
51 registerhook(HOOK_NICK_NEWNICK
, &geoip_new_nick
);
52 registerhook(HOOK_CONTROL_WHOISREQUEST
, &geoip_whois_handler
);
59 if(geoip_nickext
== -1)
62 releasenickext(geoip_nickext
);
64 deregisterhook(HOOK_NICK_NEWNICK
, &geoip_new_nick
);
65 deregisterhook(HOOK_NICK_LOSTNICK
, &geoip_quit
);
66 deregisterhook(HOOK_CONTROL_WHOISREQUEST
, &geoip_whois_handler
);
69 void geoip_setupuser(nick
*np
) {
70 int country
= GeoIP_id_by_ipnum(gi
, np
->ipaddress
);
71 if((country
< COUNTRY_MIN
) || (country
> COUNTRY_MAX
))
74 geoip_totals
[country
]++;
75 np
->exts
[geoip_nickext
] = (void *)(long)country
;
78 void geoip_new_nick(int hook
, void *args
) {
79 geoip_setupuser((nick
*)args
);
82 void geoip_quit(int hook
, void *args
) {
84 nick
*np
= (nick
*)args
;
86 item
= (int)((long)np
->exts
[geoip_nickext
]);
88 if((item
< COUNTRY_MIN
) || (item
> COUNTRY_MAX
))
94 void geoip_whois_handler(int hooknum
, void *arg
) {
96 char message
[512], *longcountry
, *shortcountry
, *shortcountry3
;
97 nick
*np
= (nick
*)arg
;
102 item
= (int)((long)np
->exts
[geoip_nickext
]);
103 if((item
< COUNTRY_MIN
) || (item
> COUNTRY_MAX
))
106 if(GeoIP_country_info_by_id(item
, &shortcountry
, &shortcountry3
, &longcountry
)) {
107 snprintf(message
, sizeof(message
), "Country : %s (%s)", shortcountry
, longcountry
);
108 triggerhook(HOOK_CONTROL_WHOISREPLY
, message
);