]>
Commit | Line | Data |
---|---|---|
3193eda0 CP |
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" | |
cc3768c4 | 8 | #include "../geoip/geoip.h" |
87698d77 | 9 | #include "../lib/version.h" |
3193eda0 CP |
10 | |
11 | #include "library.h" | |
12 | #include "nterfacer_control.h" | |
13 | ||
70b0a4e5 | 14 | MODULE_VERSION(""); |
87698d77 | 15 | |
cc3768c4 | 16 | int country_nickext = -1; |
3193eda0 | 17 | |
4e4920a4 | 18 | static struct handler *hl = NULL, *hl2 = NULL; |
3193eda0 | 19 | |
3193eda0 CP |
20 | int handle_countrytotals(struct rline *li, int argc, char **argv); |
21 | int handle_countrywhois(struct rline *li, int argc, char **argv); | |
096ef9d2 | 22 | int GeoIP_country_info_by_id(int id, char **two, char **three, char **full); |
3193eda0 | 23 | |
3193eda0 | 24 | void _init(void) { |
3193eda0 CP |
25 | if(!n_node) { |
26 | Error("nterfacer_country", ERR_ERROR, "Unable to register country as nterfacer_control isn't loaded!"); | |
27 | return; | |
28 | } | |
29 | ||
cc3768c4 | 30 | country_nickext = findnickext("geoip"); |
3193eda0 CP |
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); | |
3193eda0 CP |
36 | } |
37 | ||
38 | void _fini(void) { | |
3193eda0 CP |
39 | if(country_nickext == -1) |
40 | return; | |
41 | ||
3193eda0 CP |
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++) | |
cc3768c4 | 51 | if(ri_append(li, "%d", geoip_totals[i]) == BF_OVER) |
3193eda0 CP |
52 | return ri_error(li, BF_OVER, "Buffer overflow."); |
53 | ||
54 | return ri_final(li); | |
55 | } | |
56 | ||
3193eda0 CP |
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 | ||
cc3768c4 | 65 | result = (int)((long)np->exts[country_nickext]); |
3193eda0 CP |
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 |