]>
Commit | Line | Data |
---|---|---|
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 | ||
16 | typedef void (*TGDisplayFunc)(struct searchCtx *, nick *, trustgroup_t *); | |
17 | typedef void (*THDisplayFunc)(struct searchCtx *, nick *, trusthost_t *); | |
18 | ||
19 | int do_tgsearch(void *source, int cargc, char **cargv); | |
20 | int do_tgsearch_real(replyFunc reply, wallFunc wall, void *source, int cargc, char **cargv); | |
479e661f | 21 | void tgsearch_exe(struct searchNode *search, searchCtx *ctx); |
e2527cba P |
22 | int do_thsearch(void *source, int cargc, char **cargv); |
23 | int do_thsearch_real(replyFunc reply, wallFunc wall, void *source, int cargc, char **cargv); | |
24 | void thsearch_exe(struct searchNode *search, searchCtx *ctx, nick *sender, THDisplayFunc display, int limit, patricia_node_t *subset); | |
25 | ||
26 | searchCmd *reg_tgsearch; | |
27 | searchCmd *reg_thsearch; | |
28 | ||
29 | TGDisplayFunc defaulttgfn = printtg; | |
30 | THDisplayFunc defaultthfn = printth; | |
31 | ||
32 | void _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 | ||
41 | void _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 | ||
50 | static 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 | ||
60 | int do_tgsearch_real(replyFunc reply, wallFunc wall, void *source, int cargc, char **cargv) { | |
61 | nick *sender = senderNSExtern = source; | |
e2527cba P |
62 | int limit=500; |
63 | int arg=0; | |
64 | TGDisplayFunc display=defaulttgfn; | |
e2527cba P |
65 | int ret; |
66 | patricia_node_t *subset = iptree->head; | |
479e661f | 67 | parsertree *tree; |
e2527cba | 68 | |
479e661f P |
69 | if (cargc<1) { |
70 | reply( sender, "Usage: [flags] <criteria>"); | |
71 | reply( sender, "For help, see help nicksearch"); | |
72 | return CMD_OK; | |
73 | } | |
e2527cba P |
74 | |
75 | ret = parseopts(cargc, cargv, &arg, &limit, (void *)&subset, (void **)&display, reg_tgsearch->outputtree, reply, sender); | |
76 | if(ret != CMD_OK) | |
77 | return ret; | |
78 | ||
79 | if (arg>=cargc) { | |
80 | reply(sender,"No search terms - aborting."); | |
81 | return CMD_ERROR; | |
82 | } | |
83 | ||
84 | if (arg<(cargc-1)) { | |
85 | rejoinline(cargv[arg],cargc-arg); | |
86 | } | |
87 | ||
479e661f P |
88 | tree = parse_string(reg_tgsearch, cargv[arg]); |
89 | if(!tree) { | |
90 | displaystrerror(reply, sender, cargv[arg]); | |
e2527cba P |
91 | return CMD_ERROR; |
92 | } | |
93 | ||
479e661f | 94 | ast_tgsearch(tree->root, reply, sender, wall, display, NULL, NULL, limit); |
e2527cba | 95 | |
479e661f | 96 | parse_free(tree); |
e2527cba P |
97 | |
98 | return CMD_OK; | |
99 | } | |
100 | ||
101 | int do_tgsearch(void *source, int cargc, char **cargv) { | |
102 | return do_tgsearch_real(controlreply, controlwallwrapper, source, cargc, cargv); | |
103 | } | |
104 | ||
479e661f | 105 | void tgsearch_exe(struct searchNode *search, searchCtx *ctx) { |
e2527cba P |
106 | int matches = 0; |
107 | trustgroup_t *tg; | |
108 | int i; | |
479e661f P |
109 | nick *np, *sender = ctx->sender; |
110 | senderNSExtern = sender; | |
111 | TGDisplayFunc display = ctx->displayfn; | |
112 | int limit = ctx->limit; | |
e2527cba P |
113 | |
114 | /* Get a marker value to mark "seen" channels for unique count */ | |
115 | //nmarker=nextnodemarker(); | |
116 | ||
117 | /* The top-level node needs to return a BOOL */ | |
118 | search=coerceNode(ctx, search, RETURNTYPE_BOOL); | |
119 | ||
120 | for ( i = 0; i < TRUSTS_HASH_GROUPSIZE ; i++ ) { | |
121 | for ( tg = trustgroupidtable[i]; tg; tg = tg -> nextbyid ) { | |
122 | if ((search->exe)(ctx, search, tg)) { | |
123 | if (matches<limit) | |
124 | display(ctx, sender, tg); | |
125 | ||
126 | if (matches==limit) | |
127 | ctx->reply(sender, "--- More than %d matches, skipping the rest",limit); | |
128 | matches++; | |
129 | } | |
130 | } | |
131 | } | |
132 | ctx->reply(sender,"--- End of list: %d matches", | |
133 | matches); | |
134 | } | |
135 | ||
136 | int do_thsearch_real(replyFunc reply, wallFunc wall, void *source, int cargc, char **cargv) { | |
137 | nick *sender = senderNSExtern = source; | |
138 | struct searchNode *search; | |
139 | int limit=500; | |
140 | int arg=0; | |
141 | THDisplayFunc display=defaultthfn; | |
142 | searchCtx ctx; | |
143 | int ret; | |
144 | patricia_node_t *subset = iptree->head; | |
145 | ||
146 | if (cargc<1) | |
147 | return CMD_USAGE; | |
148 | ||
149 | ret = parseopts(cargc, cargv, &arg, &limit, (void *)&subset, (void **)&display, reg_tgsearch->outputtree, reply, sender); | |
150 | if(ret != CMD_OK) | |
151 | return ret; | |
152 | ||
153 | if (arg>=cargc) { | |
154 | reply(sender,"No search terms - aborting."); | |
155 | return CMD_ERROR; | |
156 | } | |
157 | ||
158 | if (arg<(cargc-1)) { | |
159 | rejoinline(cargv[arg],cargc-arg); | |
160 | } | |
161 | ||
162 | newsearch_ctxinit(&ctx, search_parse, reply, wall, NULL, reg_thsearch, sender, display, limit); | |
163 | ||
164 | if (!(search = ctx.parser(&ctx, cargv[arg]))) { | |
165 | reply(sender,"Parse error: %s",parseError); | |
166 | return CMD_ERROR; | |
167 | } | |
168 | ||
169 | thsearch_exe(search, &ctx, sender, display, limit, subset); | |
170 | ||
171 | (search->free)(&ctx, search); | |
172 | ||
173 | return CMD_OK; | |
174 | } | |
175 | ||
176 | int do_thsearch(void *source, int cargc, char **cargv) { | |
177 | return do_thsearch_real(controlreply, controlwallwrapper, source, cargc, cargv); | |
178 | } | |
179 | ||
180 | void thsearch_exe(struct searchNode *search, searchCtx *ctx, nick *sender, THDisplayFunc display, int limit, patricia_node_t *subset) { | |
181 | int matches = 0; | |
182 | trusthost_t *tgh; | |
183 | int i; | |
184 | ||
185 | /* Get a marker value to mark "seen" channels for unique count */ | |
186 | //nmarker=nextnodemarker(); | |
187 | ||
188 | /* The top-level node needs to return a BOOL */ | |
189 | search=coerceNode(ctx, search, RETURNTYPE_BOOL); | |
190 | ||
191 | for ( i = 0; i < TRUSTS_HASH_HOSTSIZE ; i++ ) { | |
192 | for ( tgh = trusthostidtable[i]; tgh; tgh = tgh -> nextbyid ) { | |
193 | if ((search->exe)(ctx, search, tgh)) { | |
194 | if (matches<limit) | |
195 | display(ctx, sender, tgh); | |
196 | ||
197 | if (matches==limit) | |
198 | ctx->reply(sender, "--- More than %d matches, skipping the rest",limit); | |
199 | matches++; | |
200 | } | |
201 | } | |
202 | } | |
203 | ctx->reply(sender,"--- End of list: %d matches", | |
204 | matches); | |
205 | } | |
206 |