#include "local_scan.h" #include #include #include static GeoIP *gi=NULL; int ip2country(uschar **yield, int argc, uschar *argv[]) { uschar *s, *hname; char *p; if (argc != 1) { *yield = string_copy((uschar *)"Bad number of arguments"); return ERROR; } // Try first to get country name from tld (will be more accurated) if (sender_host_address && sender_host_name && *sender_host_name && !strcmp((char *)sender_host_address, (char *)argv[0])) hname = sender_host_name; else { hname = expand_string(string_sprintf("${lookup dnsdb{ptr=%s}{$value}{}}",argv[0])); } if (hname && (p = strrchr((char *)hname,'.'))!=NULL && strlen(p)==3) { p++; *yield = string_sprintf("%c%c",toupper(*p),toupper(*(p+1))); return OK; } if (!gi) { gi = GeoIP_new(GEOIP_STANDARD); if (!gi) { *yield = string_copy((uschar *)"Cannot initializa GeoIP"); return ERROR; } } s = (uschar *)GeoIP_country_code_by_addr(gi, (char *)argv[0]); if (!s) *yield = string_copy((uschar *)"--"); else *yield = string_copy(s); return OK; }