]> jfr.im git - irc/quakenet/newserv.git/blob - patriciasearch/patriciasearch.c
update patricia search for ast + add ipvsix command
[irc/quakenet/newserv.git] / patriciasearch / patriciasearch.c
1 #include <stdio.h>
2 #include <stdarg.h>
3 #include <string.h>
4
5 #include "../control/control.h"
6 #include "../irc/irc_config.h"
7 #include "../lib/irc_string.h"
8 #include "../parser/parser.h"
9 #include "../lib/splitline.h"
10 #include "../lib/version.h"
11 #include "../lib/stringbuf.h"
12 #include "../lib/strlfunc.h"
13 #include "patriciasearch.h"
14
15 searchCmd *reg_nodesearch;
16
17 int do_pnodesearch(void *source, int cargc, char **cargv);
18
19 NodeDisplayFunc defaultpnodefn = printnode;
20
21 void _init() {
22 reg_nodesearch = (searchCmd *)registersearchcommand("nodesearch",NO_OPER,do_pnodesearch, printnode);
23
24 registersearchterm(reg_nodesearch, "users", ps_users_parse, 0, "");
25 registersearchterm(reg_nodesearch, "nick", ps_nick_parse, 0, "");
26 registersearchterm(reg_nodesearch, "ipvsix", ps_ipv6_parse, 0, "");
27 }
28
29 void _fini() {
30 deregistersearchcommand( reg_nodesearch );
31 }
32
33 static void controlwallwrapper(int level, char *format, ...) {
34 char buf[1024];
35 va_list ap;
36
37 va_start(ap, format);
38 vsnprintf(buf, sizeof(buf), format, ap);
39 controlwall(NO_OPER, level, "%s", buf);
40 va_end(ap);
41 }
42
43 int do_pnodesearch_real(replyFunc reply, wallFunc wall, void *source, int cargc, char **cargv) {
44 nick *sender = source;
45 int limit=500;
46 int arg=0;
47 NodeDisplayFunc display=defaultpnodefn;
48 int ret;
49 patricia_node_t *subset = iptree->head;
50 parsertree *tree;
51
52 if (cargc<1) {
53 reply( sender, "Usage: [flags] <criteria>");
54 reply( sender, "For help, see help nicksearch");
55 return CMD_OK;
56 }
57
58 ret = parseopts(cargc, cargv, &arg, &limit, (void *)&subset, (void *)&display, reg_nodesearch->outputtree, reply, sender);
59 if(ret != CMD_OK)
60 return ret;
61
62 if (arg>=cargc) {
63 reply(sender,"No search terms - aborting.");
64 return CMD_ERROR;
65 }
66
67 if (arg<(cargc-1)) {
68 rejoinline(cargv[arg],cargc-arg);
69 }
70
71 tree = parse_string(reg_nodesearch, cargv[arg]);
72 if(!tree) {
73 displaystrerror(reply, sender, cargv[arg]);
74 return CMD_ERROR;
75 }
76
77 ast_nodesearch(tree->root, reply, sender, wall, display, NULL, NULL, limit);
78
79 parse_free(tree);
80
81 return CMD_OK;
82 }
83
84 int do_pnodesearch(void *source, int cargc, char **cargv) {
85 return do_pnodesearch_real(controlreply, controlwallwrapper, source, cargc, cargv);
86 }
87
88 void pnodesearch_exe(struct searchNode *search, searchCtx *ctx, patricia_node_t *subset) {
89 int matches = 0;
90 patricia_node_t *node;
91 nick *sender = ctx->sender;
92 senderNSExtern = sender;
93 int limit = ctx->limit;
94 NodeDisplayFunc display = ctx->displayfn;
95
96 /* Get a marker value to mark "seen" channels for unique count */
97 //nmarker=nextnodemarker();
98
99 /* The top-level node needs to return a BOOL */
100 search=coerceNode(ctx, search, RETURNTYPE_BOOL);
101
102 PATRICIA_WALK(subset, node) {
103 if ((search->exe)(ctx, search, node)) {
104 if (matches<limit)
105 display(ctx, sender, node);
106
107 if (matches==limit)
108 ctx->reply(sender, "--- More than %d matches, skipping the rest",limit);
109 matches++;
110 }
111 }
112 PATRICIA_WALK_END;
113
114 ctx->reply(sender,"--- End of list: %d matches",
115 matches);
116 }
117
118