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"
18 #warning This module is GPLed. Load/link at your peril.
22 int geoip_totals
[COUNTRY_MAX
+ 1];
23 int geoip_nickext
= -1;
26 void geoip_setupuser(nick
*np
);
28 void geoip_new_nick(int hook
, void *args
);
29 void geoip_quit(int hook
, void *args
);
30 void geoip_whois_handler(int hooknum
, void *arg
);
37 filename
= getcopyconfigitem("geoip", "db", "GeoIP.dat", 256);
38 gi
= GeoIP_new(GEOIP_MEMORY_CACHE
, filename
->content
);
39 freesstring(filename
);
44 geoip_nickext
= registernickext("geoip");
45 if(geoip_nickext
== -1)
46 return; /* PPA: registerchanext produces an error, however the module should stop loading */
48 memset(geoip_totals
, 0, sizeof(geoip_totals
));
50 for(i
=0;i
<NICKHASHSIZE
;i
++)
51 for(np
=nicktable
[i
];np
;np
=np
->next
)
54 registerhook(HOOK_NICK_LOSTNICK
, &geoip_quit
);
55 registerhook(HOOK_NICK_NEWNICK
, &geoip_new_nick
);
56 registerhook(HOOK_CONTROL_WHOISREQUEST
, &geoip_whois_handler
);
63 if(geoip_nickext
== -1)
66 releasenickext(geoip_nickext
);
68 deregisterhook(HOOK_NICK_NEWNICK
, &geoip_new_nick
);
69 deregisterhook(HOOK_NICK_LOSTNICK
, &geoip_quit
);
70 deregisterhook(HOOK_CONTROL_WHOISREQUEST
, &geoip_whois_handler
);
73 void geoip_setupuser(nick
*np
) {
74 if (!irc_in_addr_is_ipv4(&np
->p_ipaddr
))
75 return; /* geoip only supports ipv4 */
77 unsigned int ip
= irc_in_addr_v4_to_int(&np
->p_ipaddr
);
78 int country
= GeoIP_id_by_ipnum(gi
, ip
);
79 if((country
< COUNTRY_MIN
) || (country
> COUNTRY_MAX
))
82 geoip_totals
[country
]++;
83 np
->exts
[geoip_nickext
] = (void *)(long)country
;
86 void geoip_new_nick(int hook
, void *args
) {
87 geoip_setupuser((nick
*)args
);
90 void geoip_quit(int hook
, void *args
) {
92 nick
*np
= (nick
*)args
;
94 item
= (int)((long)np
->exts
[geoip_nickext
]);
96 if((item
< COUNTRY_MIN
) || (item
> COUNTRY_MAX
))
102 void geoip_whois_handler(int hooknum
, void *arg
) {
104 char message
[512], *longcountry
, *shortcountry
, *shortcountry3
;
105 nick
*np
= (nick
*)arg
;
110 item
= (int)((long)np
->exts
[geoip_nickext
]);
111 if((item
< COUNTRY_MIN
) || (item
> COUNTRY_MAX
))
114 if(GeoIP_country_info_by_id(item
, &shortcountry
, &shortcountry3
, &longcountry
)) {
115 snprintf(message
, sizeof(message
), "Country : %s (%s)", shortcountry
, longcountry
);
116 triggerhook(HOOK_CONTROL_WHOISREPLY
, message
);
120 int geoip_lookupcode(char *code
) {
122 for(i
=COUNTRY_MIN
;i
<COUNTRY_MAX
;i
++)
123 if(!strcasecmp(code
, GeoIP_country_code
[i
]))