]>
Commit | Line | Data |
---|---|---|
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, *countrysn; | |
22 | GeoIP_LookupCode l; | |
23 | long target; | |
24 | char *p; | |
25 | ||
26 | if (argc<1) { | |
27 | parseError = "country: usage: country <country 2 character ISO code>"; | |
28 | return NULL; | |
29 | } | |
30 | ||
31 | ext = findnickext("geoip"); | |
32 | if(ext == -1) { | |
33 | parseError = "country: Geoip module not loaded."; | |
34 | return NULL; | |
35 | } | |
36 | ||
37 | l = ndlsym("geoip", "geoip_lookupcode"); | |
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 | ||
49 | if(target == -1) { | |
50 | parseError = "country: invalid country."; | |
51 | return NULL; | |
52 | } | |
53 | ||
54 | if (!(thenode=(struct searchNode *)malloc(sizeof (struct searchNode)))) { | |
55 | parseError = "malloc: could not allocate memory for this search."; | |
56 | return NULL; | |
57 | } | |
58 | ||
59 | thenode->returntype = RETURNTYPE_BOOL; | |
60 | thenode->localdata = (void *)target; | |
61 | thenode->exe = country_exe; | |
62 | thenode->free = country_free; | |
63 | ||
64 | return thenode; | |
65 | } | |
66 | ||
67 | void *country_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) { | |
68 | nick *np = (nick *)theinput; | |
69 | long country = (long)thenode->localdata, rc = (long)np->exts[ext]; | |
70 | ||
71 | if(country == rc) | |
72 | return (void *)1; | |
73 | ||
74 | return (void *)0; | |
75 | } | |
76 | ||
77 | void country_free(searchCtx *ctx, struct searchNode *thenode) { | |
78 | free(thenode); | |
79 | } |