]> jfr.im git - irc/quakenet/newserv.git/blob - nterfacer/nterfacer_country.c
Merge pull request #132 from retropc/lua_country
[irc/quakenet/newserv.git] / nterfacer / nterfacer_country.c
1 /*
2 nterfacer newserv geoip country module
3 Copyright (C) 2004 Chris Porter.
4 */
5
6 #include "../nick/nick.h"
7 #include "../core/error.h"
8 #include "../geoip/geoip.h"
9 #include "../lib/version.h"
10
11 #include "library.h"
12 #include "nterfacer_control.h"
13
14 MODULE_VERSION("");
15
16 static struct handler *hl, *hl2;
17
18 static int handle_countrytotals(struct rline *li, int argc, char **argv) {
19 for(struct country *c = geoip_next(NULL); c; c = geoip_next(c))
20 if(ri_append(li, "%s:%d", geoip_code(c), geoip_total(c)) == BF_OVER)
21 return ri_error(li, BF_OVER, "Buffer overflow.");
22
23 return ri_final(li);
24 }
25
26 static int handle_countrywhois(struct rline *li, int argc, char **argv) {
27 nick *np = getnickbynick(argv[0]);
28
29 if(!np)
30 return ri_error(li, ERR_TARGET_NOT_FOUND, "User not online");
31
32 struct country *c = geoip_lookup_nick(np);
33 if(!c)
34 return ri_error(li, ERR_UNKNOWN_ERROR, "Country lookup for user failed");
35
36 ri_append(li, "%s", geoip_code(c));
37 ri_append(li, "%s", geoip_name(c));
38
39 return ri_final(li);
40 }
41
42 void _init(void) {
43 if(!n_node) {
44 Error("nterfacer_country", ERR_ERROR, "Unable to register country as nterfacer_control isn't loaded!");
45 return;
46 }
47
48 hl = register_handler(n_node, "countrytotals", 0, handle_countrytotals);
49 hl2 = register_handler(n_node, "countrywhois", 1, handle_countrywhois);
50 }
51
52 void _fini(void) {
53 if(hl2)
54 deregister_handler(hl2);
55 if(hl)
56 deregister_handler(hl);
57 }