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