]> jfr.im git - irc/quakenet/newserv.git/blobdiff - newsearch/ns-country.c
Merge default into chanserv-live.
[irc/quakenet/newserv.git] / newsearch / ns-country.c
index d5a1a29a57c8130d97abe5c1f07c58369d12236b..3f8588270be54d0ea6d4b5857909208d0baa4c32 100644 (file)
@@ -9,17 +9,22 @@
 
 #include "../irc/irc_config.h"
 #include "../lib/irc_string.h"
+#include "../geoip/geoip.h"
+#include "../core/modules.h"
 
-void *country_exe(struct searchNode *thenode, void *theinput);
-void country_free(struct searchNode *thenode);
+void *country_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput);
+void country_free(searchCtx *ctx, struct searchNode *thenode);
 
 int ext;
 
-struct searchNode *country_parse(int type, int argc, char **argv) {
-  struct searchNode *thenode;
-
-  if (type != SEARCHTYPE_NICK) {
-    parseError = "country: this function is only valid for nick searches.";
+struct searchNode *country_parse(searchCtx *ctx, int argc, char **argv) {
+  struct searchNode *thenode, *countrysn;
+  GeoIP_LookupCode l;
+  long target;
+  char *p;
+  
+  if (argc<1) {
+    parseError = "country: usage: country <country 2 character ISO code>";
     return NULL;
   }
 
@@ -29,26 +34,46 @@ struct searchNode *country_parse(int type, int argc, char **argv) {
     return NULL;
   }
   
+  l = ndlsym("geoip", "geoip_lookupcode");
+  if(!l) {
+    parseError = "unable to lookup geoip function pointer";
+    return NULL;
+  }
+  
+  if (!(countrysn=argtoconststr("country", ctx, argv[0], &p)))
+    return NULL;
+    
+  target = l(p);
+  countrysn->free(ctx, countrysn);
+  
+  if(target == -1) {
+    parseError = "country: invalid country.";
+    return NULL;
+  }
 
   if (!(thenode=(struct searchNode *)malloc(sizeof (struct searchNode)))) {
     parseError = "malloc: could not allocate memory for this search.";
     return NULL;
   }
 
-  thenode->returntype = RETURNTYPE_INT;
-  thenode->localdata = NULL;
+  thenode->returntype = RETURNTYPE_BOOL;
+  thenode->localdata = (void *)target;
   thenode->exe = country_exe;
   thenode->free = country_free;
 
   return thenode;
 }
 
-void *country_exe(struct searchNode *thenode, void *theinput) {
+void *country_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
   nick *np = (nick *)theinput;
+  long country = (long)thenode->localdata, rc = (long)np->exts[ext];
+
+  if(country == rc)
+    return (void *)1;
 
-  return (void *)np->exts[ext];
+  return (void *)0;
 }
 
-void country_free(struct searchNode *thenode) {
+void country_free(searchCtx *ctx, struct searchNode *thenode) {
   free(thenode);
 }