]> jfr.im git - irc/quakenet/newserv.git/blame - newsearch/ns-country.c
LUA: port luadb to dbapi2 to drop postgres dependency
[irc/quakenet/newserv.git] / newsearch / ns-country.c
CommitLineData
c433958f
CP
1/*
2 * COUNTRY functionality
3 */
4
5#include "newsearch.h"
6
7#include <stdio.h>
8#include <stdlib.h>
9
10#include "../irc/irc_config.h"
c433958f 11#include "../lib/irc_string.h"
d1306c7a
CP
12#include "../geoip/geoip.h"
13#include "../core/modules.h"
c433958f 14
c8be5183
CP
15void *country_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput);
16void country_free(searchCtx *ctx, struct searchNode *thenode);
c433958f
CP
17
18int ext;
19
f33f3f52 20struct searchNode *country_parse(searchCtx *ctx, int argc, char **argv) {
31686847 21 struct searchNode *thenode, *countrysn;
d1306c7a 22 GeoIP_LookupCode l;
c54295ef 23 long target;
31686847
CP
24 char *p;
25
d1306c7a
CP
26 if (argc<1) {
27 parseError = "country: usage: country <country 2 character ISO code>";
28 return NULL;
29 }
30
c433958f
CP
31 ext = findnickext("geoip");
32 if(ext == -1) {
33 parseError = "country: Geoip module not loaded.";
34 return NULL;
35 }
36
d1306c7a 37 l = ndlsym("geoip", "geoip_lookupcode");
31686847
CP
38 if(!l) {
39 parseError = "unable to lookup geoip function pointer";
40 return NULL;
41 }
42
43 if (!(countrysn=argtoconststr("country", ctx, argv[0], &p)))
44 return NULL;
45
46 target = l(p);
47 countrysn->free(ctx, countrysn);
48
d1306c7a
CP
49 if(target == -1) {
50 parseError = "country: invalid country.";
51 return NULL;
52 }
c433958f 53
9ce4f0be
IB
54 if (!(thenode=(struct searchNode *)malloc(sizeof (struct searchNode)))) {
55 parseError = "malloc: could not allocate memory for this search.";
56 return NULL;
57 }
c433958f 58
d1306c7a
CP
59 thenode->returntype = RETURNTYPE_BOOL;
60 thenode->localdata = (void *)target;
c433958f
CP
61 thenode->exe = country_exe;
62 thenode->free = country_free;
63
64 return thenode;
65}
66
c8be5183 67void *country_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
c433958f 68 nick *np = (nick *)theinput;
c54295ef 69 long country = (long)thenode->localdata, rc = (long)np->exts[ext];
d1306c7a
CP
70
71 if(country == rc)
72 return (void *)1;
c433958f 73
d1306c7a 74 return (void *)0;
c433958f
CP
75}
76
c8be5183 77void country_free(searchCtx *ctx, struct searchNode *thenode) {
c433958f
CP
78 free(thenode);
79}