]> jfr.im git - irc/quakenet/newserv.git/blob - newsearch/ns-country.c
SearchCtx should contain 'type' - this is to make life easier when defining new searc...
[irc/quakenet/newserv.git] / newsearch / ns-country.c
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"
11 #include "../lib/irc_string.h"
12 #include "../geoip/geoip.h"
13 #include "../core/modules.h"
14
15 void *country_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput);
16 void country_free(searchCtx *ctx, struct searchNode *thenode);
17
18 int ext;
19
20 struct searchNode *country_parse(searchCtx *ctx, int argc, char **argv) {
21 struct searchNode *thenode;
22 GeoIP_LookupCode l;
23 long target;
24
25 if (ctx->type != SEARCHTYPE_NICK) {
26 parseError = "country: this function is only valid for nick searches.";
27 return NULL;
28 }
29
30 if (argc<1) {
31 parseError = "country: usage: country <country 2 character ISO code>";
32 return NULL;
33 }
34
35 ext = findnickext("geoip");
36 if(ext == -1) {
37 parseError = "country: Geoip module not loaded.";
38 return NULL;
39 }
40
41 l = ndlsym("geoip", "geoip_lookupcode");
42 target = l(argv[0]);
43 if(target == -1) {
44 parseError = "country: invalid country.";
45 return NULL;
46 }
47
48 if (!(thenode=(struct searchNode *)malloc(sizeof (struct searchNode)))) {
49 parseError = "malloc: could not allocate memory for this search.";
50 return NULL;
51 }
52
53 thenode->returntype = RETURNTYPE_BOOL;
54 thenode->localdata = (void *)target;
55 thenode->exe = country_exe;
56 thenode->free = country_free;
57
58 return thenode;
59 }
60
61 void *country_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
62 nick *np = (nick *)theinput;
63 long country = (long)thenode->localdata, rc = (long)np->exts[ext];
64
65 if(country == rc)
66 return (void *)1;
67
68 return (void *)0;
69 }
70
71 void country_free(searchCtx *ctx, struct searchNode *thenode) {
72 free(thenode);
73 }