]> jfr.im git - irc/quakenet/newserv.git/blame - trusts_search/trusts_search.c
update patricia search for ast + add ipvsix command
[irc/quakenet/newserv.git] / trusts_search / trusts_search.c
CommitLineData
e2527cba
P
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 "../trusts2/trusts.h"
14#include "trusts_search.h"
15
16typedef void (*TGDisplayFunc)(struct searchCtx *, nick *, trustgroup_t *);
17typedef void (*THDisplayFunc)(struct searchCtx *, nick *, trusthost_t *);
18
19int do_tgsearch(void *source, int cargc, char **cargv);
20int do_tgsearch_real(replyFunc reply, wallFunc wall, void *source, int cargc, char **cargv);
21void tgsearch_exe(struct searchNode *search, searchCtx *ctx, nick *sender, TGDisplayFunc display, int limit, patricia_node_t *subset);
22int do_thsearch(void *source, int cargc, char **cargv);
23int do_thsearch_real(replyFunc reply, wallFunc wall, void *source, int cargc, char **cargv);
24void thsearch_exe(struct searchNode *search, searchCtx *ctx, nick *sender, THDisplayFunc display, int limit, patricia_node_t *subset);
25
26searchCmd *reg_tgsearch;
27searchCmd *reg_thsearch;
28
29TGDisplayFunc defaulttgfn = printtg;
30THDisplayFunc defaultthfn = printth;
31
32void _init() {
33 reg_tgsearch = (searchCmd *)registersearchcommand("tgsearch",NO_OPER,do_tgsearch, printtg);
34 reg_thsearch = (searchCmd *)registersearchcommand("thsearch",NO_OPER,do_thsearch, printth);
35
36 regdisp(reg_tgsearch, "all", printtgfull, 0, "show trustgroup details, including hosts, excludes trust comments");
37 regdisp(reg_tgsearch, "default", printtg, 0, "displays trust group id");
38 regdisp(reg_thsearch, "default", printth, 0, "displays trust host id");
39}
40
41void _fini() {
42 unregdisp( reg_tgsearch, "all", printtgfull);
43 unregdisp(reg_tgsearch, "default", printtg);
44 unregdisp(reg_thsearch, "default", printth);
45
46 deregistersearchcommand( reg_tgsearch );
47 deregistersearchcommand( reg_thsearch );
48}
49
50static void controlwallwrapper(int level, char *format, ...) {
51 char buf[1024];
52 va_list ap;
53
54 va_start(ap, format);
55 vsnprintf(buf, sizeof(buf), format, ap);
56 controlwall(NO_OPER, level, "%s", buf);
57 va_end(ap);
58}
59
60int do_tgsearch_real(replyFunc reply, wallFunc wall, void *source, int cargc, char **cargv) {
61 nick *sender = senderNSExtern = source;
62 struct searchNode *search;
63 int limit=500;
64 int arg=0;
65 TGDisplayFunc display=defaulttgfn;
66 searchCtx ctx;
67 int ret;
68 patricia_node_t *subset = iptree->head;
69
70 if (cargc<1)
71 return CMD_USAGE;
72
73 ret = parseopts(cargc, cargv, &arg, &limit, (void *)&subset, (void **)&display, reg_tgsearch->outputtree, reply, sender);
74 if(ret != CMD_OK)
75 return ret;
76
77 if (arg>=cargc) {
78 reply(sender,"No search terms - aborting.");
79 return CMD_ERROR;
80 }
81
82 if (arg<(cargc-1)) {
83 rejoinline(cargv[arg],cargc-arg);
84 }
85
86 newsearch_ctxinit(&ctx, search_parse, reply, wall, NULL, reg_tgsearch, sender, display, limit);
87
88 if (!(search = ctx.parser(&ctx, cargv[arg]))) {
89 reply(sender,"Parse error: %s",parseError);
90 return CMD_ERROR;
91 }
92
93 tgsearch_exe(search, &ctx, sender, display, limit, subset);
94
95 (search->free)(&ctx, search);
96
97 return CMD_OK;
98}
99
100int do_tgsearch(void *source, int cargc, char **cargv) {
101 return do_tgsearch_real(controlreply, controlwallwrapper, source, cargc, cargv);
102}
103
104void tgsearch_exe(struct searchNode *search, searchCtx *ctx, nick *sender, TGDisplayFunc display, int limit, patricia_node_t *subset) {
105 int matches = 0;
106 trustgroup_t *tg;
107 int i;
108
109 /* Get a marker value to mark "seen" channels for unique count */
110 //nmarker=nextnodemarker();
111
112 /* The top-level node needs to return a BOOL */
113 search=coerceNode(ctx, search, RETURNTYPE_BOOL);
114
115 for ( i = 0; i < TRUSTS_HASH_GROUPSIZE ; i++ ) {
116 for ( tg = trustgroupidtable[i]; tg; tg = tg -> nextbyid ) {
117 if ((search->exe)(ctx, search, tg)) {
118 if (matches<limit)
119 display(ctx, sender, tg);
120
121 if (matches==limit)
122 ctx->reply(sender, "--- More than %d matches, skipping the rest",limit);
123 matches++;
124 }
125 }
126 }
127 ctx->reply(sender,"--- End of list: %d matches",
128 matches);
129}
130
131int do_thsearch_real(replyFunc reply, wallFunc wall, void *source, int cargc, char **cargv) {
132 nick *sender = senderNSExtern = source;
133 struct searchNode *search;
134 int limit=500;
135 int arg=0;
136 THDisplayFunc display=defaultthfn;
137 searchCtx ctx;
138 int ret;
139 patricia_node_t *subset = iptree->head;
140
141 if (cargc<1)
142 return CMD_USAGE;
143
144 ret = parseopts(cargc, cargv, &arg, &limit, (void *)&subset, (void **)&display, reg_tgsearch->outputtree, reply, sender);
145 if(ret != CMD_OK)
146 return ret;
147
148 if (arg>=cargc) {
149 reply(sender,"No search terms - aborting.");
150 return CMD_ERROR;
151 }
152
153 if (arg<(cargc-1)) {
154 rejoinline(cargv[arg],cargc-arg);
155 }
156
157 newsearch_ctxinit(&ctx, search_parse, reply, wall, NULL, reg_thsearch, sender, display, limit);
158
159 if (!(search = ctx.parser(&ctx, cargv[arg]))) {
160 reply(sender,"Parse error: %s",parseError);
161 return CMD_ERROR;
162 }
163
164 thsearch_exe(search, &ctx, sender, display, limit, subset);
165
166 (search->free)(&ctx, search);
167
168 return CMD_OK;
169}
170
171int do_thsearch(void *source, int cargc, char **cargv) {
172 return do_thsearch_real(controlreply, controlwallwrapper, source, cargc, cargv);
173}
174
175void thsearch_exe(struct searchNode *search, searchCtx *ctx, nick *sender, THDisplayFunc display, int limit, patricia_node_t *subset) {
176 int matches = 0;
177 trusthost_t *tgh;
178 int i;
179
180 /* Get a marker value to mark "seen" channels for unique count */
181 //nmarker=nextnodemarker();
182
183 /* The top-level node needs to return a BOOL */
184 search=coerceNode(ctx, search, RETURNTYPE_BOOL);
185
186 for ( i = 0; i < TRUSTS_HASH_HOSTSIZE ; i++ ) {
187 for ( tgh = trusthostidtable[i]; tgh; tgh = tgh -> nextbyid ) {
188 if ((search->exe)(ctx, search, tgh)) {
189 if (matches<limit)
190 display(ctx, sender, tgh);
191
192 if (matches==limit)
193 ctx->reply(sender, "--- More than %d matches, skipping the rest",limit);
194 matches++;
195 }
196 }
197 }
198 ctx->reply(sender,"--- End of list: %d matches",
199 matches);
200}
201