]> jfr.im git - irc/quakenet/newserv.git/blob - nterface/nterfacer_country.c
Sigh
[irc/quakenet/newserv.git] / nterface / 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("$Id: nterfacer_country.c 663 2006-05-16 17:27:36Z newserv $")
15
16 int country_nickext = -1;
17
18 struct handler *hl = NULL, *hl2 = NULL;
19
20 int handle_countrytotals(struct rline *li, int argc, char **argv);
21 int handle_countrywhois(struct rline *li, int argc, char **argv);
22 int GeoIP_country_info_by_id(int id, char **two, char **three, char **full);
23
24 void _init(void) {
25 if(!n_node) {
26 Error("nterfacer_country", ERR_ERROR, "Unable to register country as nterfacer_control isn't loaded!");
27 return;
28 }
29
30 country_nickext = findnickext("geoip");
31 if(country_nickext == -1)
32 return; /* PPA: registerchanext produces an error, however the module should stop loading */
33
34 hl = register_handler(n_node, "countrytotals", 0, handle_countrytotals);
35 hl2 = register_handler(n_node, "countrywhois", 1, handle_countrywhois);
36 }
37
38 void _fini(void) {
39 if(country_nickext == -1)
40 return;
41
42 if(hl2)
43 deregister_handler(hl2);
44 if(hl)
45 deregister_handler(hl);
46 }
47
48 int handle_countrytotals(struct rline *li, int argc, char **argv) {
49 int i;
50 for(i=COUNTRY_MIN;i<=COUNTRY_MAX;i++)
51 if(ri_append(li, "%d", geoip_totals[i]) == BF_OVER)
52 return ri_error(li, BF_OVER, "Buffer overflow.");
53
54 return ri_final(li);
55 }
56
57 int handle_countrywhois(struct rline *li, int argc, char **argv) {
58 int result;
59 char *longcountry, *shortcountry, *shortcountry3;
60 nick *np = getnickbynick(argv[0]);
61
62 if(!np)
63 return ri_error(li, ERR_TARGET_NOT_FOUND, "User not online");
64
65 result = (int)((long)np->exts[country_nickext]);
66 if((result < COUNTRY_MIN) || (result > COUNTRY_MAX))
67 result = -1;
68
69 if(!GeoIP_country_info_by_id(result, &shortcountry, &shortcountry3, &longcountry))
70 result = -1;
71
72 ri_append(li, "%d", result);
73
74 if(result == -1) {
75 ri_append(li, "%s", "!!");
76 ri_append(li, "%s", "!!");
77 ri_append(li, "%s", "Error");
78 } else {
79 ri_append(li, "%s", shortcountry);
80 ri_append(li, "%s", shortcountry3);
81 ri_append(li, "%s", longcountry);
82 }
83
84 return ri_final(li);
85 }
86