]>
jfr.im git - irc/quakenet/newserv.git/blob - nterface/nterfacer_country.c
f91893abdebc57288b7eee43e33e4c62c841ae7f
2 nterfacer newserv geoip country module
3 Copyright (C) 2004 Chris Porter.
6 #include "../nick/nick.h"
7 #include "../core/error.h"
8 #include "../core/config.h"
9 #include "../core/hooks.h"
12 #include "nterfacer_control.h"
14 #include "libGeoIP/GeoIP.h"
16 struct handler
*hl
= NULL
, *hl2
= NULL
;
19 #define COUNTRY_MAX 246
21 unsigned int totals
[COUNTRY_MAX
+ 1];
22 int country_nickext
= -1;
25 void country_setupuser(nick
*np
);
26 int handle_countrytotals(struct rline
*li
, int argc
, char **argv
);
27 int handle_countrywhois(struct rline
*li
, int argc
, char **argv
);
29 void country_new_nick(int hook
, void *args
);
30 void country_quit(int hook
, void *args
);
38 Error("nterfacer_country", ERR_ERROR
, "Unable to register country as nterfacer_control isn't loaded!");
42 filename
= getcopyconfigitem("nterfacer", "geoipdb", "GeoIP.dat", 256);
43 gi
= GeoIP_new(GEOIP_MEMORY_CACHE
, filename
->content
);
44 freesstring(filename
);
49 country_nickext
= registernickext("nterfacer_country");
50 if(country_nickext
== -1)
51 return; /* PPA: registerchanext produces an error, however the module should stop loading */
53 hl
= register_handler(n_node
, "countrytotals", 0, handle_countrytotals
);
54 hl2
= register_handler(n_node
, "countrywhois", 1, handle_countrywhois
);
55 memset(totals
, 0, sizeof(totals
));
57 for(i
=0;i
<NICKHASHSIZE
;i
++) {
58 for(np
=nicktable
[i
];np
;np
=np
->next
) {
59 country_setupuser(np
);
63 registerhook(HOOK_NICK_LOSTNICK
, &country_quit
);
64 registerhook(HOOK_NICK_NEWNICK
, &country_new_nick
);
71 if(country_nickext
== -1)
74 releasenickext(country_nickext
);
76 deregisterhook(HOOK_NICK_NEWNICK
, &country_new_nick
);
77 deregisterhook(HOOK_NICK_LOSTNICK
, &country_quit
);
80 deregister_handler(hl2
);
82 deregister_handler(hl
);
85 int handle_countrytotals(struct rline
*li
, int argc
, char **argv
) {
87 for(i
=COUNTRY_MIN
;i
<=COUNTRY_MAX
;i
++)
88 if(ri_append(li
, "%d", totals
[i
]) == BF_OVER
)
89 return ri_error(li
, BF_OVER
, "Buffer overflow.");
94 void country_setupuser(nick
*np
) {
95 int country
= GeoIP_id_by_ipnum(gi
, np
->ipaddress
);
96 if((country
< COUNTRY_MIN
) || (country
> COUNTRY_MAX
))
100 np
->exts
[country_nickext
] = (void *)&totals
[country
];
103 void country_new_nick(int hook
, void *args
) {
104 country_setupuser((nick
*)args
);
107 void country_quit(int hook
, void *args
) {
108 nick
*np
= (nick
*)args
;
111 if((item
= (unsigned int *)np
->exts
[country_nickext
]))
115 int handle_countrywhois(struct rline
*li
, int argc
, char **argv
) {
117 char *longcountry
, *shortcountry
, *shortcountry3
;
118 nick
*np
= getnickbynick(argv
[0]);
121 return ri_error(li
, ERR_TARGET_NOT_FOUND
, "User not online");
123 result
= GeoIP_id_by_ipnum(gi
, np
->ipaddress
);
124 if((result
< COUNTRY_MIN
) || (result
> COUNTRY_MAX
))
127 if(!GeoIP_country_info_by_id(result
, &shortcountry
, &shortcountry3
, &longcountry
))
130 ri_append(li
, "%d", result
);
133 ri_append(li
, "%s", "!!");
134 ri_append(li
, "%s", "!!");
135 ri_append(li
, "%s", "Error");
137 ri_append(li
, "%s", shortcountry
);
138 ri_append(li
, "%s", shortcountry3
);
139 ri_append(li
, "%s", longcountry
);