1/* This file is part of Mailfromd.             -*- c -*-
2   Copyright (C) 2009-2021 Sergey Poznyakoff
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License as published by
6   the Free Software Foundation; either version 3, or (at your option)
7   any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU General Public License for more details.
13
14   You should have received a copy of the GNU General Public License
15   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17MF_BUILTIN_MODULE
18MF_COND(WITH_GEOIP)
19#include <GeoIP.h>
20
21MF_DEFUN(geoip_country_code_by_addr, STRING, STRING ip,
22	 OPTIONAL, NUMBER tlc)
23{
24	GeoIP *gi = GeoIP_new(GEOIP_STANDARD);
25	const char *ret;
26
27	MF_ASSERT(gi != NULL,
28		  mfe_failure,
29		  "GeoIP_new");
30	ret = MF_OPTVAL(tlc) ? GeoIP_country_code3_by_addr(gi, ip)
31		             : GeoIP_country_code_by_addr(gi, ip);
32	GeoIP_delete(gi);
33	MF_ASSERT(ret != NULL && strcmp(ret, "N/A") != 0,
34		  mfe_not_found,
35		  _("country code not found"));
36	MF_RETURN(ret);
37}
38END
39
40MF_DEFUN(geoip_country_code_by_name, STRING, STRING name,
41	 OPTIONAL, NUMBER tlc)
42{
43	GeoIP *gi = GeoIP_new(GEOIP_STANDARD);
44	const char *ret;
45
46	MF_ASSERT(gi != NULL,
47		  mfe_failure,
48		  "GeoIP_new");
49	ret = MF_OPTVAL(tlc) ? GeoIP_country_code3_by_name(gi, name)
50		             : GeoIP_country_code_by_name(gi, name);
51	GeoIP_delete(gi);
52	MF_ASSERT(ret != NULL && strcmp(ret, "N/A") != 0,
53		  mfe_not_found,
54		  _("country code not found"));
55	MF_RETURN(ret);
56}
57END
58
59