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