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