]> jfr.im git - irc/quakenet/newserv.git/blame - newsearch/newsearch.c
newsearch changes to support addition of trust_search/patriciasearch
[irc/quakenet/newserv.git] / newsearch / newsearch.c
CommitLineData
c86edd1d 1#include <stdio.h>
0da2a4ae 2#include <stdarg.h>
b697c21c 3#include <string.h>
c86edd1d
Q
4
5#include "../irc/irc_config.h"
c86edd1d
Q
6#include "../lib/irc_string.h"
7#include "../parser/parser.h"
8#include "../control/control.h"
9#include "../lib/splitline.h"
87698d77 10#include "../lib/version.h"
ba8a65c4 11#include "../lib/stringbuf.h"
b697c21c 12#include "../lib/strlfunc.h"
a92bb8e1
P
13#include "../lib/array.h"
14#include "newsearch.h"
87698d77 15
70b0a4e5 16MODULE_VERSION("");
c86edd1d 17
a92bb8e1
P
18CommandTree *searchCmdTree;
19searchList *globalterms = NULL;
c86edd1d
Q
20
21int do_nicksearch(void *source, int cargc, char **cargv);
56a84a81 22int do_chansearch(void *source, int cargc, char **cargv);
e8ad630b 23int do_usersearch(void *source, int cargc, char **cargv);
d00af236 24
3d2bf13b
CP
25void printnick_channels(searchCtx *, nick *, nick *);
26void printchannel(searchCtx *, nick *, chanindex *);
27void printchannel_topic(searchCtx *, nick *, chanindex *);
28void printchannel_services(searchCtx *, nick *, chanindex *);
c86edd1d 29
55dd7529
CP
30UserDisplayFunc defaultuserfn = printuser;
31NickDisplayFunc defaultnickfn = printnick;
32ChanDisplayFunc defaultchanfn = printchannel;
33
a92bb8e1 34searchCmd *reg_nicksearch, *reg_chansearch, *reg_usersearch;
c86edd1d 35
a92bb8e1
P
36searchCmd *registersearchcommand(char *name, int level, CommandHandler cmd, void *defaultdisplayfunc) {
37 searchCmd *acmd;
38 searchList *sl;
73cfc302 39
a92bb8e1
P
40 registercontrolhelpcmd(name, NO_OPER,4, cmd, "Usage: <criteria\nSearches with the given criteria");
41
42 acmd=(struct searchCmd *)malloc(sizeof(struct searchCmd));
73cfc302 43
a92bb8e1
P
44 acmd->handler = cmd;
45
46 acmd->name = getsstring( name, NSMAX_COMMAND_LEN);
47 acmd->outputtree = newcommandtree();
48 acmd->searchtree = newcommandtree();
73cfc302 49
a92bb8e1
P
50 addcommandtotree(searchCmdTree, name, 0, 0, (CommandHandler)acmd);
51
52 sl = globalterms;
53 while (sl) {
54 registersearchterm( acmd, sl->name->content, sl->cmd);
55 sl = sl->next;
56 }
57
58 return acmd;
73cfc302 59}
60
a92bb8e1
P
61void deregistersearchcommand(searchCmd *scmd) {
62 deregistercontrolcmd(scmd->name->content, (CommandHandler)scmd->handler);
63 destroycommandtree(scmd->outputtree);
64 destroycommandtree(scmd->searchtree);
65 freesstring(scmd->name);
66 free(scmd);
e8ad630b
CP
67}
68
a92bb8e1
P
69void regdisp( searchCmd *cmd, const char *name, void *handler) {
70 addcommandtotree(cmd->outputtree, name, 0, 0, (CommandHandler) handler);
71}
72
73void unregdisp( searchCmd *cmd, const char *name, void *handler ) {
74 deletecommandfromtree(cmd->outputtree, name, (CommandHandler) handler);
e8ad630b
CP
75}
76
a92bb8e1
P
77void *findcommandinlist( searchList *sl, char *name){
78 while(sl) {
79 if(strcmp(sl->name->content,name) == 0 ) {
80 return sl;
81 }
82 sl = sl->next;
83 }
84 return NULL;
85}
86
c86edd1d 87const char *parseError;
16cc6e32
IB
88/* used for *_free functions that need to warn users of certain things
89 i.e. hitting too many users in a (kill) or (gline) */
219d27f1 90nick *senderNSExtern;
c86edd1d
Q
91
92void _init() {
a92bb8e1
P
93 searchCmdTree=newcommandtree();
94
95 reg_nicksearch = (searchCmd *)registersearchcommand("nicksearch",NO_OPER,&do_nicksearch, printnick);
96 reg_chansearch = (searchCmd *)registersearchcommand("chansearch",NO_OPER,&do_chansearch, printchannel);
97 reg_usersearch = (searchCmd *)registersearchcommand("usersearch",NO_OPER,&do_usersearch, printuser);
c86edd1d
Q
98
99 /* Boolean operations */
a92bb8e1
P
100 registerglobalsearchterm("and",and_parse);
101 registerglobalsearchterm("not",not_parse);
102 registerglobalsearchterm("or",or_parse);
f1903ace 103
a92bb8e1 104 registerglobalsearchterm("eq",eq_parse);
f1903ace 105
a92bb8e1
P
106 registerglobalsearchterm("lt",lt_parse);
107 registerglobalsearchterm("gt",gt_parse);
c86edd1d
Q
108
109 /* String operations */
a92bb8e1
P
110 registerglobalsearchterm("match",match_parse);
111 registerglobalsearchterm("regex",regex_parse);
112 registerglobalsearchterm("length",length_parse);
c7f7a584 113
c86edd1d 114 /* Nickname operations */
a92bb8e1
P
115 registersearchterm(reg_nicksearch, "hostmask",hostmask_parse); /* nick only */
116 registersearchterm(reg_nicksearch, "realname",realname_parse); /* nick only */
117 registersearchterm(reg_nicksearch, "authname",authname_parse); /* nick only */
118 registersearchterm(reg_nicksearch, "authts",authts_parse); /* nick only */
119 registersearchterm(reg_nicksearch, "ident",ident_parse); /* nick only */
120 registersearchterm(reg_nicksearch, "host",host_parse); /* nick only */
121 registersearchterm(reg_nicksearch, "channel",channel_parse); /* nick only */
122 registersearchterm(reg_nicksearch, "timestamp",timestamp_parse); /* nick only */
123 registersearchterm(reg_nicksearch, "country",country_parse); /* nick only */
124 registersearchterm(reg_nicksearch, "ip",ip_parse); /* nick only */
125 registersearchterm(reg_nicksearch, "channels",channels_parse); /* nick only */
126 registersearchterm(reg_nicksearch, "server",server_parse); /* nick only */
127 registersearchterm(reg_nicksearch, "authid",authid_parse); /* nick only */
c86edd1d 128
56a84a81 129 /* Channel operations */
a92bb8e1
P
130 registersearchterm(reg_chansearch, "exists",exists_parse); /* channel only */
131 registersearchterm(reg_chansearch, "services",services_parse); /* channel only */
132 registersearchterm(reg_chansearch, "size",size_parse); /* channel only */
133 registersearchterm(reg_chansearch, "name",name_parse); /* channel only */
134 registersearchterm(reg_chansearch, "topic",topic_parse); /* channel only */
135 registersearchterm(reg_chansearch, "oppct",oppct_parse); /* channel only */
136 registersearchterm(reg_chansearch, "uniquehostpct",hostpct_parse); /* channel only */
137 registersearchterm(reg_chansearch, "authedpct",authedpct_parse); /* channel only */
138 registersearchterm(reg_chansearch, "kick",kick_parse); /* channel only */
56a84a81 139
c86edd1d 140 /* Nickname / channel operations */
a92bb8e1
P
141 registersearchterm(reg_chansearch, "modes",modes_parse);
142 registersearchterm(reg_nicksearch, "modes",modes_parse);
143 registersearchterm(reg_chansearch, "nick",nick_parse);
144 registersearchterm(reg_nicksearch, "nick",nick_parse);
c86edd1d 145
16cc6e32 146 /* Kill / gline parameters */
a92bb8e1
P
147 registersearchterm(reg_chansearch,"kill",kill_parse);
148 registersearchterm(reg_chansearch,"gline",gline_parse);
149 registersearchterm(reg_nicksearch,"kill",kill_parse);
150 registersearchterm(reg_nicksearch,"gline",gline_parse);
4622c762 151
b697c21c 152 /* Iteration functionality */
a92bb8e1
P
153 registerglobalsearchterm("any",any_parse);
154 registerglobalsearchterm("all",all_parse);
155 registerglobalsearchterm("var",var_parse);
b697c21c
CP
156
157 /* Iterable functions */
a92bb8e1 158 registersearchterm(reg_nicksearch, "channeliter",channeliter_parse); /* nick only */
b697c21c 159
4622c762 160 /* Notice functionality */
a92bb8e1
P
161 registersearchterm(reg_chansearch,"notice",notice_parse);
162 registersearchterm(reg_nicksearch,"notice",notice_parse);
163
73cfc302 164 /* Nick output filters */
a92bb8e1
P
165 regdisp(reg_nicksearch,"default",printnick);
166 regdisp(reg_nicksearch,"channels",printnick_channels);
d00af236 167
73cfc302 168 /* Channel output filters */
a92bb8e1
P
169 regdisp(reg_chansearch,"default",printchannel);
170 regdisp(reg_chansearch,"topic",printchannel_topic);
171 regdisp(reg_chansearch,"services",printchannel_services);
16cc6e32 172
e8ad630b 173 /* Nick output filters */
a92bb8e1
P
174 regdisp(reg_usersearch,"default",printuser);
175
c86edd1d
Q
176}
177
178void _fini() {
a92bb8e1
P
179 searchList *sl, *psl;
180 int i,n;
181 Command *cmdlist[100];
182
183 sl = globalterms;
184 while (sl) {
185 psl = sl;
186 sl = sl->next;
187
188 n=getcommandlist(searchCmdTree,cmdlist,100);
189 for(i=0;i<n;i++) {
190 deregistersearchterm( (searchCmd *)cmdlist[i]->handler, psl->name->content, psl->cmd);
191 }
192
193 freesstring(psl->name);
194 free(psl);
195
196 }
197
198 deregistersearchcommand( reg_nicksearch );
199 deregistersearchcommand( reg_chansearch );
200 deregistersearchcommand( reg_usersearch );
201 destroycommandtree( searchCmdTree );
202}
203
204void registerglobalsearchterm(char *term, parseFunc parsefunc) {
205 searchList *sl = malloc(sizeof(searchList));
206 int i,n;
207 Command *cmdlist[100];
208
209 sl->cmd = parsefunc;
210 sl->name = getsstring(term, NSMAX_COMMAND_LEN);
211 sl->next = NULL;
212
213 if ( globalterms != NULL ) {
214 sl->next = globalterms;
215 }
216 globalterms = sl;
217
218 n=getcommandlist(searchCmdTree,cmdlist,100);
219 for(i=0;i<n;i++) {
220 registersearchterm( (searchCmd *)cmdlist[i]->handler,term, parsefunc);
221 }
222}
223
224void deregisterglobalsearchterm(char *term, parseFunc parsefunc) {
225 int i,n;
226 Command *cmdlist[100];
227 searchList *sl, *psl=NULL;
228
229 sl = globalterms;
230 while (sl) {
231 if ( strcmp( sl->name->content, term) == 0 ) {
232 break;
233 }
234 psl = sl;
235 sl = sl->next;
236 }
237
238 if (sl) {
239 if( psl ) {
240 psl->next = sl->next;
241 }
242
243 n=getcommandlist(searchCmdTree,cmdlist,100);
244 for(i=0;i<n;i++) {
245 deregistersearchterm( (searchCmd *)cmdlist[i]->handler, term, parsefunc);
246 }
247 freesstring(sl->name);
248 free(sl);
249 }
c86edd1d
Q
250}
251
a92bb8e1
P
252void registersearchterm(searchCmd *cmd, char *term, parseFunc parsefunc) {
253 addcommandtotree(cmd->searchtree, term, 0, 0, (CommandHandler) parsefunc);
c86edd1d
Q
254}
255
a92bb8e1
P
256void deregistersearchterm(searchCmd *cmd, char *term, parseFunc parsefunc) {
257 deletecommandfromtree(cmd->searchtree, term, (CommandHandler) parsefunc);
c86edd1d
Q
258}
259
0da2a4ae
CP
260static void controlwallwrapper(int level, char *format, ...) {
261 char buf[1024];
262 va_list ap;
263
264 va_start(ap, format);
265 vsnprintf(buf, sizeof(buf), format, ap);
266 controlwall(NO_OPER, level, "%s", buf);
267 va_end(ap);
268}
269
a92bb8e1 270int parseopts(int cargc, char **cargv, int *arg, int *limit, void **subset, void **display, CommandTree *sl, replyFunc reply, void *sender) {
c86edd1d 271 char *ch;
a92bb8e1
P
272 Command *cmd;
273 struct irc_in_addr sin; unsigned char bits;
274
c86edd1d
Q
275 if (*cargv[0] == '-') {
276 /* options */
e8ad630b 277 (*arg)++;
c86edd1d
Q
278
279 for (ch=cargv[0]+1;*ch;ch++) {
280 switch(*ch) {
281 case 'l':
e8ad630b 282 if (cargc<*arg) {
0da2a4ae 283 reply(sender,"Error: -l switch requires an argument");
38cee035 284 return CMD_USAGE;
c86edd1d 285 }
e8ad630b 286 *limit=strtoul(cargv[(*arg)++],NULL,10);
c86edd1d
Q
287 break;
288
73cfc302 289 case 'd':
e8ad630b 290 if (cargc<*arg) {
0da2a4ae 291 reply(sender,"Error: -d switch requires an argument");
73cfc302 292 return CMD_USAGE;
293 }
a92bb8e1 294 cmd=findcommandintree(sl, cargv[*arg],1);
73cfc302 295 if (!cmd) {
e8ad630b 296 reply(sender,"Error: unknown output format %s",cargv[*arg]);
73cfc302 297 return CMD_USAGE;
298 }
e8ad630b
CP
299 *display=(void *)cmd->handler;
300 (*arg)++;
73cfc302 301 break;
a92bb8e1
P
302
303 case 's':
304 if (cargc<*arg) {
305 reply(sender,"Error: -s switch requires an argument");
306 return CMD_USAGE;
307 }
308 if (ipmask_parse(cargv[*arg], &sin, &bits) == 0) {
309 reply(sender, "Error: Invalid CIDR mask supplied");
310 return CMD_USAGE;
311 }
312 *subset = (void *)refnode(iptree, &sin, bits);
313 (*arg)++;
314 break;
73cfc302 315
c86edd1d 316 default:
0da2a4ae 317 reply(sender,"Unrecognised flag -%c.",*ch);
c86edd1d
Q
318 }
319 }
320 }
321
e8ad630b
CP
322 return CMD_OK;
323}
324
a92bb8e1 325void newsearch_ctxinit(searchCtx *ctx, searchParseFunc searchfn, replyFunc replyfn, wallFunc wallfn, void *arg, searchCmd *cmd) {
b697c21c
CP
326 memset(ctx, 0, sizeof(searchCtx));
327
328 ctx->reply = replyfn;
329 ctx->wall = wallfn;
330 ctx->parser = searchfn;
331 ctx->arg = arg;
a92bb8e1 332 ctx->searchcmd = cmd;
b697c21c
CP
333}
334
e8ad630b 335int do_nicksearch_real(replyFunc reply, wallFunc wall, void *source, int cargc, char **cargv) {
b697c21c 336 nick *sender = source;
e8ad630b
CP
337 struct searchNode *search;
338 int limit=500;
339 int arg=0;
55dd7529 340 NickDisplayFunc display=defaultnickfn;
e8ad630b
CP
341 searchCtx ctx;
342 int ret;
343
344 if (cargc<1)
345 return CMD_USAGE;
346
a92bb8e1 347 ret = parseopts(cargc, cargv, &arg, &limit, NULL, (void **)&display, reg_nicksearch->outputtree, reply, sender);
e8ad630b
CP
348 if(ret != CMD_OK)
349 return ret;
350
c86edd1d 351 if (arg>=cargc) {
4156103f 352 reply(sender,"No search terms - aborting.");
c86edd1d
Q
353 return CMD_ERROR;
354 }
355
356 if (arg<(cargc-1)) {
357 rejoinline(cargv[arg],cargc-arg);
358 }
c8be5183 359
a92bb8e1 360 newsearch_ctxinit(&ctx, search_parse, reply, wall, NULL, reg_nicksearch);
c8be5183 361
f33f3f52 362 if (!(search = ctx.parser(&ctx, cargv[arg]))) {
0da2a4ae 363 reply(sender,"Parse error: %s",parseError);
c86edd1d
Q
364 return CMD_ERROR;
365 }
c2c414be 366
c8be5183
CP
367 nicksearch_exe(search, &ctx, sender, display, limit);
368
369 (search->free)(&ctx, search);
c2c414be
CP
370
371 return CMD_OK;
372}
373
0da2a4ae
CP
374int do_nicksearch(void *source, int cargc, char **cargv) {
375 return do_nicksearch_real(controlreply, controlwallwrapper, source, cargc, cargv);
376}
377
c8be5183 378void nicksearch_exe(struct searchNode *search, searchCtx *ctx, nick *sender, NickDisplayFunc display, int limit) {
c2c414be
CP
379 int i, j;
380 int matches = 0;
381 unsigned int cmarker;
382 unsigned int tchans=0,uchans=0;
383 struct channel **cs;
384 nick *np;
b697c21c
CP
385 senderNSExtern = sender;
386
7c02e111 387 /* Get a marker value to mark "seen" channels for unique count */
388 cmarker=nextchanmarker();
389
c7f7a584 390 /* The top-level node needs to return a BOOL */
c8be5183 391 search=coerceNode(ctx, search, RETURNTYPE_BOOL);
c7f7a584 392
c86edd1d
Q
393 for (i=0;i<NICKHASHSIZE;i++) {
394 for (np=nicktable[i];np;np=np->next) {
c8be5183 395 if ((search->exe)(ctx, search, np)) {
7c02e111 396 /* Add total channels */
397 tchans += np->channels->cursi;
398
399 /* Check channels for uniqueness */
400 cs=(channel **)np->channels->content;
401 for (j=0;j<np->channels->cursi;j++) {
402 if (cs[j]->index->marker != cmarker) {
403 cs[j]->index->marker=cmarker;
404 uchans++;
405 }
406 }
407
c86edd1d 408 if (matches<limit)
3d2bf13b 409 display(ctx, sender, np);
7c02e111 410
c86edd1d 411 if (matches==limit)
c8be5183 412 ctx->reply(sender, "--- More than %d matches, skipping the rest",limit);
c86edd1d
Q
413 matches++;
414 }
415 }
416 }
417
c8be5183 418 ctx->reply(sender,"--- End of list: %d matches; users were on %u channels (%u unique, %.1f average clones)",
7c02e111 419 matches, tchans, uchans, (float)tchans/uchans);
c86edd1d
Q
420}
421
0da2a4ae 422int do_chansearch_real(replyFunc reply, wallFunc wall, void *source, int cargc, char **cargv) {
b697c21c 423 nick *sender = source;
56a84a81 424 struct searchNode *search;
c2c414be 425 int limit=500;
56a84a81 426 int arg=0;
55dd7529 427 ChanDisplayFunc display=defaultchanfn;
c8be5183 428 searchCtx ctx;
e8ad630b 429 int ret;
56a84a81
IB
430
431 if (cargc<1)
432 return CMD_USAGE;
433
a92bb8e1 434 ret = parseopts(cargc, cargv, &arg, &limit, NULL, (void **)&display, reg_chansearch->outputtree, reply, sender);
e8ad630b
CP
435 if(ret != CMD_OK)
436 return ret;
56a84a81
IB
437
438 if (arg>=cargc) {
4156103f 439 reply(sender,"No search terms - aborting.");
56a84a81
IB
440 return CMD_ERROR;
441 }
442
443 if (arg<(cargc-1)) {
444 rejoinline(cargv[arg],cargc-arg);
445 }
c2c414be 446
a92bb8e1 447 newsearch_ctxinit(&ctx, search_parse, reply, wall, NULL, reg_chansearch);
f33f3f52 448 if (!(search = ctx.parser(&ctx, cargv[arg]))) {
0da2a4ae 449 reply(sender,"Parse error: %s",parseError);
56a84a81
IB
450 return CMD_ERROR;
451 }
c7f7a584 452
c8be5183 453 chansearch_exe(search, &ctx, sender, display, limit);
c2c414be 454
c8be5183 455 (search->free)(&ctx, search);
c2c414be
CP
456
457 return CMD_OK;
458}
459
0da2a4ae
CP
460int do_chansearch(void *source, int cargc, char **cargv) {
461 return do_chansearch_real(controlreply, controlwallwrapper, source, cargc, cargv);
462}
463
c8be5183 464void chansearch_exe(struct searchNode *search, searchCtx *ctx, nick *sender, ChanDisplayFunc display, int limit) {
c2c414be
CP
465 int i;
466 chanindex *cip;
467 int matches = 0;
b697c21c 468 senderNSExtern = sender;
c2c414be 469
c8be5183 470 search=coerceNode(ctx, search, RETURNTYPE_BOOL);
56a84a81
IB
471
472 for (i=0;i<CHANNELHASHSIZE;i++) {
473 for (cip=chantable[i];cip;cip=cip->next) {
c8be5183 474 if ((search->exe)(ctx, search, cip)) {
56a84a81 475 if (matches<limit)
3d2bf13b 476 display(ctx, sender, cip);
56a84a81 477 if (matches==limit)
c8be5183 478 ctx->reply(sender, "--- More than %d matches, skipping the rest",limit);
56a84a81
IB
479 matches++;
480 }
481 }
482 }
483
c8be5183 484 ctx->reply(sender,"--- End of list: %d matches", matches);
56a84a81
IB
485}
486
e8ad630b 487int do_usersearch_real(replyFunc reply, wallFunc wall, void *source, int cargc, char **cargv) {
b697c21c 488 nick *sender = source;
e8ad630b
CP
489 struct searchNode *search;
490 int limit=500;
491 int arg=0;
55dd7529 492 UserDisplayFunc display=defaultuserfn;
e8ad630b
CP
493 searchCtx ctx;
494 int ret;
495
496 if (cargc<1)
497 return CMD_USAGE;
498
a92bb8e1 499 ret = parseopts(cargc, cargv, &arg, &limit, NULL, (void **)&display, reg_usersearch->outputtree, reply, sender);
e8ad630b
CP
500 if(ret != CMD_OK)
501 return ret;
502
503 if (arg>=cargc) {
4156103f 504 reply(sender,"No search terms - aborting.");
e8ad630b
CP
505 return CMD_ERROR;
506 }
507
508 if (arg<(cargc-1)) {
509 rejoinline(cargv[arg],cargc-arg);
510 }
511
a92bb8e1 512 newsearch_ctxinit(&ctx, search_parse, reply, wall, NULL, reg_usersearch);
f33f3f52 513 if (!(search = ctx.parser(&ctx, cargv[arg]))) {
e8ad630b
CP
514 reply(sender,"Parse error: %s",parseError);
515 return CMD_ERROR;
516 }
517
518 usersearch_exe(search, &ctx, sender, display, limit);
519
520 (search->free)(&ctx, search);
521
522 return CMD_OK;
523}
524
525int do_usersearch(void *source, int cargc, char **cargv) {
526 return do_usersearch_real(controlreply, controlwallwrapper, source, cargc, cargv);
527}
528
529void usersearch_exe(struct searchNode *search, searchCtx *ctx, nick *sender, UserDisplayFunc display, int limit) {
530 int i;
531 authname *aup;
532 int matches = 0;
b697c21c 533 senderNSExtern = sender;
e8ad630b
CP
534
535 search=coerceNode(ctx, search, RETURNTYPE_BOOL);
536
537 for (i=0;i<AUTHNAMEHASHSIZE;i++) {
538 for (aup=authnametable[i];aup;aup=aup->next) {
539 if ((search->exe)(ctx, search, aup)) {
540 if (matches<limit)
541 display(ctx, sender, aup);
542 if (matches==limit)
543 ctx->reply(sender, "--- More than %d matches, skipping the rest",limit);
544 matches++;
545 }
546 }
547 }
548
549 ctx->reply(sender,"--- End of list: %d matches", matches);
550}
551
c7f7a584 552/* Free a coerce node */
c8be5183 553void free_coerce(searchCtx *ctx, struct searchNode *thenode) {
c7f7a584 554 struct coercedata *cd=thenode->localdata;
555
c8be5183 556 cd->child->free(ctx, cd->child);
c7f7a584 557 free(thenode->localdata);
558 free(thenode);
559}
c86edd1d 560
c7f7a584 561/* Free a coerce node with a stringbuf allocated */
c8be5183 562void free_coercestring(searchCtx *ctx, struct searchNode *thenode) {
c7f7a584 563 free(((struct coercedata *)thenode->localdata)->u.stringbuf);
c8be5183 564 free_coerce(ctx, thenode);
c7f7a584 565}
566
567/* exe_tostr_null: return the constant string */
c8be5183 568void *exe_tostr_null(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
c7f7a584 569 struct coercedata *cd=thenode->localdata;
570
571 return cd->u.stringbuf;
572}
573
574/* exe_val_null: return the constant value */
c8be5183 575void *exe_val_null(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
c7f7a584 576 struct coercedata *cd=thenode->localdata;
577
578 return (void *)cd->u.val;
579}
580
581/* Lots of very dull type conversion functions */
c8be5183 582void *exe_inttostr(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
c7f7a584 583 struct coercedata *cd=thenode->localdata;
584
c8be5183 585 sprintf(cd->u.stringbuf, "%lu", (unsigned long)(cd->child->exe)(ctx, cd->child, theinput));
c7f7a584 586
587 return cd->u.stringbuf;
588}
589
c8be5183 590void *exe_booltostr(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
c7f7a584 591 struct coercedata *cd=thenode->localdata;
592
c8be5183 593 if ((cd->child->exe)(ctx, cd->child, theinput)) {
c7f7a584 594 sprintf(cd->u.stringbuf,"1");
595 } else {
596 cd->u.stringbuf[0]='\0';
597 }
598
599 return cd->u.stringbuf;
600}
c86edd1d 601
c8be5183 602void *exe_strtoint(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
c7f7a584 603 struct coercedata *cd=thenode->localdata;
604
c8be5183 605 return (void *)strtoul((cd->child->exe)(ctx,cd->child,theinput),NULL,10);
c7f7a584 606}
607
c8be5183 608void *exe_booltoint(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
c7f7a584 609 struct coercedata *cd=thenode->localdata;
610
611 /* Don't need to do anything */
c8be5183 612 return (cd->child->exe)(ctx, cd->child, theinput);
c7f7a584 613}
614
c8be5183 615void *exe_strtobool(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
c7f7a584 616 struct coercedata *cd=thenode->localdata;
c8be5183 617 char *ch=(cd->child->exe)(ctx, cd->child, theinput);
c7f7a584 618
619 if (!ch || *ch=='\0' || (*ch=='0' && ch[1]=='\0')) {
620 return (void *)0;
621 } else {
622 return (void *)1;
623 }
624}
625
c8be5183 626void *exe_inttobool(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
c7f7a584 627 struct coercedata *cd=thenode->localdata;
628
c8be5183 629 if ((cd->child->exe)(ctx, cd->child, theinput)) {
c7f7a584 630 return (void *)1;
631 } else {
632 return (void *)0;
633 }
634}
635
c8be5183 636struct searchNode *coerceNode(searchCtx *ctx, struct searchNode *thenode, int type) {
c7f7a584 637 struct searchNode *anode;
638 struct coercedata *cd;
639
640 /* You can't coerce a NULL */
641 if (!thenode)
642 return NULL;
643
644 /* No effort required to coerce to the same type */
645 if (type==(thenode->returntype & RETURNTYPE_TYPE))
646 return thenode;
647
648 anode=(struct searchNode *)malloc(sizeof(struct searchNode));
649 anode->localdata=cd=(struct coercedata *)malloc(sizeof(struct coercedata));
650 cd->child=thenode;
651 anode->returntype=type; /* We'll return what they want, always */
652 anode->free=free_coerce;
653
654 switch(type) {
655 case RETURNTYPE_STRING:
656 /* For a string we'll need a buffer */
657 /* A 64-bit number prints out to 20 digits, this leaves some slack */
658 cd->u.stringbuf=malloc(25);
659 anode->free=free_coercestring;
660
661 switch(thenode->returntype & RETURNTYPE_TYPE) {
662 default:
663 case RETURNTYPE_INT:
664 if (thenode->returntype & RETURNTYPE_CONST) {
665 /* Constant node: sort it out now */
c8be5183 666 sprintf(cd->u.stringbuf, "%lu", (unsigned long)thenode->exe(ctx, thenode, NULL));
c7f7a584 667 anode->exe=exe_tostr_null;
668 anode->returntype |= RETURNTYPE_CONST;
669 } else {
670 /* Variable data */
671 anode->exe=exe_inttostr;
672 }
673 break;
674
675 case RETURNTYPE_BOOL:
676 if (thenode->returntype & RETURNTYPE_CONST) {
677 /* Constant bool value */
c8be5183 678 if (thenode->exe(ctx, thenode,NULL)) {
c7f7a584 679 /* True! */
680 sprintf(cd->u.stringbuf, "1");
681 } else {
682 cd->u.stringbuf[0] = '\0';
683 }
684 anode->exe=exe_tostr_null;
685 anode->returntype |= RETURNTYPE_CONST;
686 } else {
687 /* Variable bool value */
688 anode->exe=exe_booltostr;
689 }
690 break;
691 }
692 break;
693
694 case RETURNTYPE_INT:
695 /* we want an int */
696 switch (thenode->returntype & RETURNTYPE_TYPE) {
697 case RETURNTYPE_STRING:
698 if (thenode->returntype & RETURNTYPE_CONST) {
c8be5183 699 cd->u.val=strtoul((thenode->exe)(ctx, thenode, NULL), NULL, 10);
c7f7a584 700 anode->exe=exe_val_null;
701 anode->returntype |= RETURNTYPE_CONST;
702 } else {
703 anode->exe=exe_strtoint;
704 }
705 break;
706
707 default:
708 case RETURNTYPE_BOOL:
709 if (thenode->returntype & RETURNTYPE_CONST) {
c8be5183 710 if ((thenode->exe)(ctx, thenode,NULL))
c7f7a584 711 cd->u.val=1;
712 else
713 cd->u.val=0;
714
715 anode->exe=exe_val_null;
716 anode->returntype |= RETURNTYPE_CONST;
717 } else {
718 anode->exe=exe_booltoint;
719 }
720 break;
721 }
722 break;
723
724 default:
725 case RETURNTYPE_BOOL:
726 /* we want a bool */
727 switch (thenode->returntype & RETURNTYPE_TYPE) {
728 case RETURNTYPE_STRING:
729 if (thenode->returntype & RETURNTYPE_CONST) {
c8be5183 730 char *rv=(char *)((thenode->exe)(ctx, thenode, NULL));
c7f7a584 731 if (!rv || *rv=='\0' || (*rv=='0' && rv[1]=='\0'))
732 cd->u.val=0;
733 else
734 cd->u.val=1;
735
736 anode->exe=exe_val_null;
737 anode->returntype |= RETURNTYPE_CONST;
738 } else {
739 anode->exe=exe_strtobool;
740 }
741 break;
742
743 default:
744 case RETURNTYPE_INT:
745 if (thenode->returntype & RETURNTYPE_CONST) {
c8be5183 746 if ((thenode->exe)(ctx, thenode,NULL))
c7f7a584 747 cd->u.val=1;
748 else
749 cd->u.val=0;
750
751 anode->exe=exe_val_null;
752 anode->returntype |= RETURNTYPE_CONST;
753 } else {
754 anode->exe=exe_inttobool;
755 }
756 break;
757 }
758 break;
759 }
760
761 return anode;
762}
763
764/* Literals always return constant strings... */
c8be5183 765void *literal_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
2e2a8c6a
P
766 if (thenode->localdata)
767 return ((sstring *)thenode->localdata)->content;
768 else
769 return "";
c7f7a584 770}
771
c8be5183 772void literal_free(searchCtx *ctx, struct searchNode *thenode) {
c7f7a584 773 freesstring(thenode->localdata);
774 free(thenode);
775}
c86edd1d
Q
776
777/* search_parse:
778 * Given an input string, return a searchNode.
779 */
780
f33f3f52 781struct searchNode *search_parse(searchCtx *ctx, char *input) {
c86edd1d
Q
782 /* OK, we need to split the input into chunks on spaces and brackets.. */
783 char *argvector[100];
1bf9cda6 784 char thestring[500];
c7f7a584 785 int i,j,q=0,e=0;
1bf9cda6 786 char *ch,*ch2;
c86edd1d
Q
787 struct Command *cmd;
788 struct searchNode *thenode;
c86edd1d
Q
789
790 /* If it starts with a bracket, it's a function call.. */
791 if (*input=='(') {
792 /* Skip past string */
793 for (ch=input;*ch;ch++);
794 if (*(ch-1) != ')') {
795 parseError = "Bracket mismatch!";
796 return NULL;
797 }
798 input++;
799 *(ch-1)='\0';
800
801 /* Split further args */
802 i=-1; /* i = -1 BoW, 0 = inword, 1 = bracket nest depth */
803 j=0; /* j = current arg */
1bf9cda6 804 e=0;
805 q=0;
a33e0d2b 806 argvector[0]="";
c86edd1d
Q
807 for (ch=input;*ch;ch++) {
808 if (i==-1) {
1bf9cda6 809 argvector[j]=ch;
810 if (*ch=='(') {
811 i=1;
812 } else if (*ch != ' ') {
813 i=0;
814 if (*ch=='\\') {
815 e=1;
816 } else if (*ch=='\"') {
817 q=1;
818 }
819 }
820 } else if (e==1) {
821 e=0;
822 } else if (q==1) {
823 if (*ch=='\"')
824 q=0;
c86edd1d 825 } else if (i==0) {
1bf9cda6 826 if (*ch=='\\') {
827 e=1;
828 } else if (*ch=='\"') {
829 q=1;
830 } else if (*ch==' ') {
831 *ch='\0';
832 j++;
4cd6347c
P
833 if(j >= (sizeof(argvector) / sizeof(*argvector))) {
834 parseError = "Too many arguments";
835 return NULL;
836 }
1bf9cda6 837 i=-1;
838 }
c86edd1d 839 } else {
1bf9cda6 840 if (*ch=='\\') {
841 e=1;
842 } else if (*ch=='\"') {
843 q=1;
844 } else if (*ch=='(') {
845 i++;
846 } else if (*ch==')') {
847 i--;
848 }
c86edd1d
Q
849 }
850 }
851
852 if (i>0) {
853 parseError = "Bracket mismatch!";
854 return NULL;
855 }
4cd6347c
P
856
857 if (*(ch-1) == 0) /* if the last character was a space */
858 j--; /* remove an argument */
c86edd1d 859
a92bb8e1 860 if (!(cmd=findcommandintree(ctx->searchcmd->searchtree,argvector[0],1))) {
c86edd1d
Q
861 parseError = "Unknown command";
862 return NULL;
863 } else {
f33f3f52 864 return ((parseFunc)cmd->handler)(ctx, j, argvector+1);
c86edd1d
Q
865 }
866 } else {
867 /* Literal */
1bf9cda6 868 if (*input=='\"') {
869 for (ch=input;*ch;ch++);
870
871 if (*(ch-1) != '\"') {
872 parseError="Quote mismatch";
873 return NULL;
874 }
875
876 *(ch-1)='\0';
877 input++;
878 }
879
880 ch2=thestring;
881 for (ch=input;*ch;ch++) {
882 if (e) {
883 e=0;
884 *ch2++=*ch;
885 } else if (*ch=='\\') {
886 e=1;
887 } else {
888 *ch2++=*ch;
889 }
890 }
891 *ch2='\0';
892
9ce4f0be
IB
893 if (!(thenode=(struct searchNode *)malloc(sizeof(struct searchNode)))) {
894 parseError = "malloc: could not allocate memory for this search.";
895 return NULL;
1bf9cda6 896 }
897
c7f7a584 898 thenode->localdata = getsstring(thestring,512);
c86edd1d
Q
899 thenode->returntype = RETURNTYPE_CONST | RETURNTYPE_STRING;
900 thenode->exe = literal_exe;
901 thenode->free = literal_free;
902
903 return thenode;
904 }
905}
2ba836f2 906
2ba836f2 907void nssnprintf(char *buf, size_t size, const char *format, nick *np) {
ba8a65c4 908 StringBuf b;
2ba836f2
CP
909 const char *p;
910 char *c;
911 char hostbuf[512];
912
913 if(size == 0)
914 return;
915
916 b.buf = buf;
917 b.capacity = size;
918 b.len = 0;
919
920 for(p=format;*p;p++) {
921 if(*p != '%') {
ba8a65c4 922 if(!sbaddchar(&b, *p))
2ba836f2
CP
923 break;
924 continue;
925 }
926 p++;
927 if(*p == '\0')
928 break;
929 if(*p == '%') {
ba8a65c4 930 if(!sbaddchar(&b, *p))
2ba836f2
CP
931 break;
932 continue;
933 }
934
935 c = NULL;
936 switch(*p) {
937 case 'n':
938 c = np->nick; break;
939 case 'i':
940 c = np->ident; break;
941 case 'h':
942 c = np->host->name->content; break;
943 case 'I':
944 snprintf(hostbuf, sizeof(hostbuf), "%s", IPtostr(np->p_ipaddr));
945 c = hostbuf;
946 break;
947 case 'u':
948 snprintf(hostbuf, sizeof(hostbuf), "%s!%s@%s", np->nick, np->ident, IPtostr(np->p_ipaddr));
949 c = hostbuf;
950 break;
951 default:
952 c = "(bad format specifier)";
953 }
954 if(c)
ba8a65c4 955 if(!sbaddstr(&b, c))
2ba836f2
CP
956 break;
957 }
958
0be0b2d0 959 sbterminate(&b);
2ba836f2
CP
960
961 /* not required */
962 /*
963 buf[size-1] = '\0';
964 */
965}
966
f33f3f52
P
967static char *var_tochar(searchCtx *ctx, char *arg, searchNode **variable) {
968 *variable = ctx->parser(ctx, arg);
b697c21c
CP
969 if (!(*variable = coerceNode(ctx, *variable, RETURNTYPE_STRING)))
970 return NULL;
971
972 if(!((*variable)->returntype & RETURNTYPE_CONST)) {
973 parseError = "only constant variables allowed";
974 ((*variable)->free)(ctx, *variable);
975 return NULL;
976 }
977
978 return (char *)((*variable)->exe)(ctx, *variable, NULL);
979}
980
981void free_val_null(searchCtx *ctx, struct searchNode *thenode) {
982}
983
f33f3f52 984struct searchVariable *var_register(searchCtx *ctx, char *arg, int type) {
b697c21c
CP
985 searchNode *variable;
986 struct searchVariable *us;
987 char *var;
988 int i;
989
990 if(ctx->lastvar >= MAX_VARIABLES) {
991 parseError = "Maximum number of variables reached";
992 return NULL;
993 }
994
995 us = &ctx->vars[ctx->lastvar];
996
f33f3f52 997 var = var_tochar(ctx, arg, &variable);
b697c21c
CP
998 if(!var)
999 return NULL;
1000
1001 strlcpy(us->name, var, sizeof(us->name));
1002 (variable->free)(ctx, variable);
1003
1004 for(i=0;i<ctx->lastvar;i++) {
1005 if(!strcmp(us->name, ctx->vars[i].name)) {
1006 parseError = "variable name already in use";
1007 return NULL;
1008 }
1009 }
1010
1011 ctx->lastvar++;
1012 us->data.returntype = type;
1013 us->data.localdata = &us->cdata;
1014 us->data.exe = exe_val_null;
1015 us->data.free = free_val_null;
1016
1017 us->cdata.child = NULL;
1018 return us;
1019}
1020
f33f3f52 1021searchNode *var_get(searchCtx *ctx, char *arg) {
b697c21c
CP
1022 searchNode *variable, *found = NULL;
1023 int i;
f33f3f52 1024 char *var = var_tochar(ctx, arg, &variable);
b697c21c
CP
1025 if(!var)
1026 return NULL;
1027
1028 for(i=0;i<ctx->lastvar;i++) {
1029 if(!strcmp(var, ctx->vars[i].name)) {
1030 found = &ctx->vars[i].data;
1031 break;
1032 }
1033 }
1034 (variable->free)(ctx, variable);
1035
1036 if(!found)
1037 parseError = "variable not found";
1038 return found;
1039}
1040
1041void var_setstr(struct searchVariable *v, char *data) {
1042 v->cdata.u.stringbuf = data;
1043}