3 Copyright (C) 2004-2006 Chris Porter.
6 #include "../nick/nick.h"
7 #include "../core/error.h"
8 #include "../core/config.h"
9 #include "../core/hooks.h"
10 #include "../control/control.h"
11 #include "../lib/version.h"
15 #include "libGeoIP/GeoIP.h"
20 int geoip_totals
[COUNTRY_MAX
+ 1];
21 static int geoip_nickext
= -1;
22 static GeoIP
*gi
= NULL
;
24 static void geoip_setupuser(nick
*np
);
26 static void geoip_new_nick(int hook
, void *args
);
27 static void geoip_quit(int hook
, void *args
);
28 static void geoip_whois_handler(int hooknum
, void *arg
);
35 filename
= getcopyconfigitem("geoip", "db", "GeoIP.dat", 256);
36 gi
= GeoIP_open(filename
->content
, GEOIP_MEMORY_CACHE
);
37 freesstring(filename
);
42 geoip_nickext
= registernickext("geoip");
43 if(geoip_nickext
== -1)
44 return; /* PPA: registerchanext produces an error, however the module should stop loading */
46 memset(geoip_totals
, 0, sizeof(geoip_totals
));
48 for(i
=0;i
<NICKHASHSIZE
;i
++)
49 for(np
=nicktable
[i
];np
;np
=np
->next
)
52 registerhook(HOOK_NICK_LOSTNICK
, &geoip_quit
);
53 registerhook(HOOK_NICK_NEWNICK
, &geoip_new_nick
);
54 registerhook(HOOK_CONTROL_WHOISREQUEST
, &geoip_whois_handler
);
61 if(geoip_nickext
== -1)
64 releasenickext(geoip_nickext
);
66 deregisterhook(HOOK_NICK_NEWNICK
, &geoip_new_nick
);
67 deregisterhook(HOOK_NICK_LOSTNICK
, &geoip_quit
);
68 deregisterhook(HOOK_CONTROL_WHOISREQUEST
, &geoip_whois_handler
);
71 static void geoip_setupuser(nick
*np
) {
72 if (!irc_in_addr_is_ipv4(&np
->ipaddress
))
73 return; /* geoip only supports ipv4 */
75 unsigned int ip
= irc_in_addr_v4_to_int(&np
->ipaddress
);
76 int country
= GeoIP_id_by_ipnum(gi
, ip
);
77 if((country
< COUNTRY_MIN
) || (country
> COUNTRY_MAX
))
80 geoip_totals
[country
]++;
81 np
->exts
[geoip_nickext
] = (void *)(long)country
;
84 static void geoip_new_nick(int hook
, void *args
) {
85 geoip_setupuser((nick
*)args
);
88 static void geoip_quit(int hook
, void *args
) {
90 nick
*np
= (nick
*)args
;
92 item
= (int)((long)np
->exts
[geoip_nickext
]);
94 if((item
< COUNTRY_MIN
) || (item
> COUNTRY_MAX
))
100 static void geoip_whois_handler(int hooknum
, void *arg
) {
103 const char *longcountry
, *shortcountry
;
104 nick
*np
= (nick
*)arg
;
109 item
= (int)((long)np
->exts
[geoip_nickext
]);
110 if((item
< COUNTRY_MIN
) || (item
> COUNTRY_MAX
))
113 shortcountry
= GeoIP_code_by_id(item
);
114 longcountry
= GeoIP_name_by_id(item
);
116 if(shortcountry
&& longcountry
) {
117 snprintf(message
, sizeof(message
), "Country : %s (%s)", shortcountry
, longcountry
);
118 triggerhook(HOOK_CONTROL_WHOISREPLY
, message
);
122 int geoip_lookupcode(char *code
) {
124 for(i
=COUNTRY_MIN
;i
<COUNTRY_MAX
;i
++)
125 if(!strcasecmp(code
, GeoIP_country_code
[i
]))