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