]> jfr.im git - irc/quakenet/newserv.git/commitdiff
Added (+ enabled) uniquehostpct functionality in newsearch's chansearch
authorIan Barker <redacted>
Mon, 4 Jun 2007 21:38:45 +0000 (22:38 +0100)
committerIan Barker <redacted>
Mon, 4 Jun 2007 21:38:45 +0000 (22:38 +0100)
newsearch/Makefile
newsearch/newsearch.c
newsearch/newsearch.h
newsearch/ns-hostpct.c [new file with mode: 0644]

index 060be9a5949e9be21307a1dc4191dea5e08ae7f3..541cd9f2bb2b601e05a705b422ab70a6b210e33e 100644 (file)
@@ -2,5 +2,5 @@
 .PHONY: all
 all: newsearch.so
 
-newsearch.so: newsearch.o ns-not.o ns-and.o ns-or.o ns-eq.o ns-match.o ns-hostmask.o ns-realname.o ns-modes.o ns-nick.o ns-ident.o ns-regex.o ns-host.o ns-channel.o ns-lt.o ns-gt.o ns-timestamp.o ns-country.o ns-authname.o ns-ip.o ns-kill.o ns-gline.o ns-exists.o ns-services.o ns-size.o ns-namelen.o ns-name.o ns-topic.o ns-oppct.o
+newsearch.so: newsearch.o ns-not.o ns-and.o ns-or.o ns-eq.o ns-match.o ns-hostmask.o ns-realname.o ns-modes.o ns-nick.o ns-ident.o ns-regex.o ns-host.o ns-channel.o ns-lt.o ns-gt.o ns-timestamp.o ns-country.o ns-authname.o ns-ip.o ns-kill.o ns-gline.o ns-exists.o ns-services.o ns-size.o ns-namelen.o ns-name.o ns-topic.o ns-oppct.o ns-hostpct.o
        ld -shared -Bdynamic $(LIBPCRE) -o $@ $^
index ae8059eba414587130aced79a8a4b41941607f13..c294088f3f8a20c447d9b1bf6e9022c5a1809aab 100644 (file)
@@ -64,7 +64,7 @@ void _init() {
   registersearchterm("name",name_parse);
   registersearchterm("topic",topic_parse);
   registersearchterm("oppct",oppct_parse);
-  //registersearchterm("hostpct",hostpct_parse);
+  registersearchterm("uniquehostpct",hostpct_parse);
   //registersearchterm("authedpct",authedpct_parse);
 
   /* Nickname / channel operations */
index 02dbb928ffa5f07d234e65e2a725072816ef6717..25d8e08cd2b5e76b781764cb1c729e6e7343f2e9 100644 (file)
@@ -61,7 +61,7 @@ struct searchNode *namelen_parse(int type, int argc, char **argv);
 struct searchNode *name_parse(int type, int argc, char **argv);
 struct searchNode *topic_parse(int type, int argc, char **argv);
 struct searchNode *oppct_parse(int type, int argc, char **argv);
-//struct searchNode *hostpct_parse(int type, int argc, char **argv);
+struct searchNode *hostpct_parse(int type, int argc, char **argv);
 //struct searchNode *authedpct_parse(int type, int argc, char **argv);
 
 
diff --git a/newsearch/ns-hostpct.c b/newsearch/ns-hostpct.c
new file mode 100644 (file)
index 0000000..88c95e3
--- /dev/null
@@ -0,0 +1,93 @@
+/*
+ * hostpct functionality 
+ */
+
+#include "newsearch.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+
+struct hostpct_localdata {
+  long pct;
+};
+
+void *hostpct_exe(struct searchNode *thenode, int type, void *theinput);
+void hostpct_free(struct searchNode *thenode);
+
+struct searchNode *hostpct_parse(int type, int argc, char **argv) {
+  struct hostpct_localdata *localdata;
+  struct searchNode *thenode;
+
+  if (type != SEARCHTYPE_CHANNEL) {
+    parseError = "uniquehostpct: this function is only valid for channel searches.";
+    return NULL;
+  }
+
+  if (argc!=1) {
+    parseError="uniquehostpct: usage: (uniquehostpct number)";
+    return NULL;
+  }
+
+  if (!(localdata=(struct hostpct_localdata *)malloc(sizeof(struct hostpct_localdata)))) {
+    parseError = "malloc: could not allocate memory for this search.";
+    return NULL;
+  }
+
+  localdata->pct = strtoul(argv[0],NULL,10);
+
+  if (!(thenode=(struct searchNode *)malloc(sizeof(struct searchNode)))) {
+    /* couldn't malloc() memory for thenode, so free localdata to avoid leakage */
+    parseError = "malloc: could not allocate memory for this search.";
+    free(localdata);
+    return NULL;
+  }
+
+  thenode->returntype = RETURNTYPE_BOOL;
+  thenode->localdata = localdata;
+  thenode->exe = hostpct_exe;
+  thenode->free = hostpct_free;
+
+  return thenode;
+}
+
+void *hostpct_exe(struct searchNode *thenode, int type, void *theinput) {
+  int hostreq;
+  int i;
+  unsigned int marker;
+  nick *np;
+  chanindex *cip = (chanindex *)theinput;
+  struct hostpct_localdata *localdata;
+
+  localdata = thenode->localdata;
+  
+  if (cip->channel==NULL)
+    return falseval(type);
+  
+  marker=nexthostmarker();
+  
+  hostreq=(cip->channel->users->totalusers * localdata->pct) / 100;
+  
+  for (i=0;i<cip->channel->users->hashsize;i++) {
+    if (cip->channel->users->content[i]==nouser)
+      continue;
+      
+    if (!(np=getnickbynumeric(cip->channel->users->content[i])))
+      continue;
+
+    if (np->host->marker!=marker) {
+      /* new unique host */
+      if (--hostreq <= 0) {
+        return trueval(type);
+      }
+      np->host->marker=marker;
+    }
+  }
+  
+  return falseval(type);
+}
+
+void hostpct_free(struct searchNode *thenode) {
+  free(thenode->localdata);
+  free(thenode);
+}
+