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"
18 CommandTree
*searchCmdTree
;
19 searchList
*globalterms
= NULL
;
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
);
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
*);
30 UserDisplayFunc defaultuserfn
= printuser
;
31 NickDisplayFunc defaultnickfn
= printnick
;
32 ChanDisplayFunc defaultchanfn
= printchannel
;
34 searchCmd
*reg_nicksearch
, *reg_chansearch
, *reg_usersearch
;
35 void displaycommandhelp(nick
*, Command
*);
37 searchCmd
*registersearchcommand(char *name
, int level
, CommandHandler cmd
, void *defaultdisplayfunc
) {
41 registercontrolhelpfunccmd(name
, NO_OPER
,4, cmd
, &displaycommandhelp
);
42 acmd
=(struct searchCmd
*)malloc(sizeof(struct searchCmd
));
44 Error("newsearch", ERR_ERROR
, "malloc failed: registersearchcommand");
49 acmd
->name
= getsstring( name
, NSMAX_COMMAND_LEN
);
50 acmd
->outputtree
= newcommandtree();
51 acmd
->searchtree
= newcommandtree();
53 addcommandtotree(searchCmdTree
, name
, 0, 0, (CommandHandler
)acmd
);
55 for (sl
=globalterms
; sl
; sl
=sl
->next
) {
56 registersearchterm( acmd
, sl
->name
->content
, sl
->cmd
, 0, sl
->help
);
62 void deregistersearchcommand(searchCmd
*scmd
) {
63 deregistercontrolcmd(scmd
->name
->content
, (CommandHandler
)scmd
->handler
);
64 destroycommandtree(scmd
->outputtree
);
65 destroycommandtree(scmd
->searchtree
);
66 freesstring(scmd
->name
);
70 void displaycommandhelp(nick
*np
, Command
*cmd
) {
72 Command
*cmdlist
[100], *acmdlist
[100];
75 n
=getcommandlist(searchCmdTree
,cmdlist
,100);
77 /* note: we may want to only deregister a command if we've already registered it, for now, try de-registering new commands anyway */
78 if ( ((searchCmd
*)cmdlist
[i
]->handler
)->handler
!= cmd
->handler
)
80 acmd
= ((searchCmd
*)(cmdlist
[i
]->handler
));
82 controlreply(np
, "Usage: [flags] <criteria>\n");
83 controlreply(np
, "Flags:\n");
84 controlreply(np
, " -l int : Limit number of rows of results\n");
85 controlreply(np
, " -d string : a valid output format for the results\n");
86 controlreply(np
, " -s subset : ipmask subset of network to search (only for node search)\n");
87 controlreply(np
, " \n");
88 controlreply(np
, "Available Output Formats:\n");
90 m
=getcommandlist(acmd
->outputtree
,acmdlist
,100);
92 if ( controlpermitted( acmdlist
[j
]->level
, np
) ) {
93 char *help
=(char *)acmdlist
[j
]->ext
;
94 if ( help
&& help
[0] != '\0')
95 controlreply(np
, "%-10s: %s\n", acmdlist
[j
]->command
->content
, help
);
97 controlreply(np
, "%s\n", acmdlist
[j
]->command
->content
);
101 controlreply(np
, " \n");
102 controlreply(np
, "Available Global Search Terms:\n" );
103 m
=getcommandlist(acmd
->searchtree
,acmdlist
,100);
105 if ( acmdlist
[j
]->maxparams
) {
106 char *help
=(char *)acmdlist
[j
]->ext
;
107 if ( help
&& help
[0] != '\0')
108 controlreply(np
, "%-10s: %s\n", acmdlist
[j
]->command
->content
, help
);
110 controlreply(np
, "%s\n", acmdlist
[j
]->command
->content
);
114 controlreply(np
, " \n");
115 controlreply(np
, "Available Search Terms for %s:\n", acmd
->name
->content
);
117 m
=getcommandlist(acmd
->searchtree
,acmdlist
,100);
119 if ( !acmdlist
[j
]->maxparams
&& controlpermitted( acmdlist
[j
]->level
, np
) ) {
120 char *help
=(char *)acmdlist
[j
]->ext
;
121 if ( help
&& help
[0] != '\0')
122 controlreply(np
, "%-10s: %s\n", acmdlist
[j
]->command
->content
, help
);
124 controlreply(np
, "%s\n", acmdlist
[j
]->command
->content
);
130 void regdisp( searchCmd
*cmd
, const char *name
, void *handler
, int level
, char *help
) {
131 addcommandexttotree(cmd
->outputtree
, name
, level
, 0, (CommandHandler
) handler
, (char *)help
);
134 void unregdisp( searchCmd
*cmd
, const char *name
, void *handler
) {
135 deletecommandfromtree(cmd
->outputtree
, name
, (CommandHandler
) handler
);
138 void *findcommandinlist( searchList
*sl
, char *name
){
140 if(strcmp(sl
->name
->content
,name
) == 0 ) {
148 const char *parseError
;
149 /* used for *_free functions that need to warn users of certain things
150 i.e. hitting too many users in a (kill) or (gline) */
151 nick
*senderNSExtern
;
154 searchCmdTree
=newcommandtree();
156 reg_nicksearch
= (searchCmd
*)registersearchcommand("nicksearch",NO_OPER
,&do_nicksearch
, printnick
);
157 reg_chansearch
= (searchCmd
*)registersearchcommand("chansearch",NO_OPER
,&do_chansearch
, printchannel
);
158 reg_usersearch
= (searchCmd
*)registersearchcommand("usersearch",NO_OPER
,&do_usersearch
, printuser
);
160 /* Boolean operations */
161 registerglobalsearchterm("and",and_parse
, "usage: (and (X) (X))" );
162 registerglobalsearchterm("not",not_parse
, "usage: (not (X))");
163 registerglobalsearchterm("or",or_parse
, "usage: (or (X) (X))" );
165 registerglobalsearchterm("eq",eq_parse
, "usage: (eq (X) Y)");
167 registerglobalsearchterm("lt",lt_parse
, "usage: (lt (X) int)");
168 registerglobalsearchterm("gt",gt_parse
, "usage: (gt (X) int)");
170 /* String operations */
171 registerglobalsearchterm("match",match_parse
, "usage: (match (X) string)");
172 registerglobalsearchterm("regex",regex_parse
, "usage: (regex (X) string)");
173 registerglobalsearchterm("length",length_parse
, "usage: (length string)");
175 /* Nickname operations */
176 registersearchterm(reg_nicksearch
, "hostmask",hostmask_parse
, 0, "Users host mask, allow \"hostmask real\" to match realhost"); /* nick only */
177 registersearchterm(reg_nicksearch
, "realname",realname_parse
, 0, "Users current realname"); /* nick only */
178 registersearchterm(reg_nicksearch
, "authname",authname_parse
, 0, "Users current authname or false"); /* nick only */
179 registersearchterm(reg_nicksearch
, "authts",authts_parse
, 0, "Users Auth timestamp"); /* nick only */
180 registersearchterm(reg_nicksearch
, "ident",ident_parse
, 0, "Users current ident"); /* nick only */
181 registersearchterm(reg_nicksearch
, "host",host_parse
, 0, "Users host, allow \"host real\" to match real host"); /* nick only */
182 registersearchterm(reg_nicksearch
, "channel",channel_parse
, 0, "Valid Channel Name to match users against"); /* nick only */
183 registersearchterm(reg_nicksearch
, "timestamp",timestamp_parse
, 0, "Users Timestamp"); /* nick only */
184 registersearchterm(reg_nicksearch
, "country",country_parse
, 0, "2 letter country code (data source is geoip)"); /* nick only */
185 registersearchterm(reg_nicksearch
, "ip",ip_parse
, 0, "Users IP - ipv4 or ipv6 format as appropriate. Note: not 6to4"); /* nick only */
186 registersearchterm(reg_nicksearch
, "channels",channels_parse
, 0, "Channel Count"); /* nick only */
187 registersearchterm(reg_nicksearch
, "server",server_parse
, 0, "Server Name. Either (server string) or (match (server) string)"); /* nick only */
188 registersearchterm(reg_nicksearch
, "authid",authid_parse
, 0, "Users Auth ID"); /* nick only */
190 /* Channel operations */
191 registersearchterm(reg_chansearch
, "exists",exists_parse
, 0, "Returns if channel exists on network. Note: newserv may store data on empty channels"); /* channel only */
192 registersearchterm(reg_chansearch
, "services",services_parse
, 0, ""); /* channel only */
193 registersearchterm(reg_chansearch
, "size",size_parse
, 0, "Channel user count"); /* channel only */
194 registersearchterm(reg_chansearch
, "name",name_parse
, 0, "Channel Name"); /* channel only */
195 registersearchterm(reg_chansearch
, "topic",topic_parse
, 0, "Channel topic"); /* channel only */
196 registersearchterm(reg_chansearch
, "oppct",oppct_parse
, 0, "Percentage Opped"); /* channel only */
197 registersearchterm(reg_chansearch
, "uniquehostpct",hostpct_parse
, 0, "uniquehost percent"); /* channel only */
198 registersearchterm(reg_chansearch
, "authedpct",authedpct_parse
, 0, "Percentage of authed users"); /* channel only */
199 registersearchterm(reg_chansearch
, "kick",kick_parse
, 0, "KICK users channels in newsearch result. Note: evaluation order"); /* channel only */
201 /* Nickname / channel operations */
202 registersearchterm(reg_chansearch
, "modes",modes_parse
, 0, "User Modes");
203 registersearchterm(reg_nicksearch
, "modes",modes_parse
, 0, "Channel Modes");
204 registersearchterm(reg_chansearch
, "nick",nick_parse
, 0, "Nickname");
205 registersearchterm(reg_nicksearch
, "nick",nick_parse
, 0, "Nickname");
207 /* Kill / gline parameters */
208 registersearchterm(reg_chansearch
,"kill",kill_parse
, 0, "KILL users in newsearch result. Note: evaluation order");
209 registersearchterm(reg_chansearch
,"gline",gline_parse
, 0, "GLINE users in newsearch result. Note: evaluation order");
210 registersearchterm(reg_nicksearch
,"kill",kill_parse
, 0, "KILL users in newsearch result. Note: evaluation order");
211 registersearchterm(reg_nicksearch
,"gline",gline_parse
, 0, "GLINE users in newsearch result. Note: evaluation order");
213 /* Iteration functionality */
214 registerglobalsearchterm("any",any_parse
, "usage: any (generatorfn x) (fn ... (var x) ...)");
215 registerglobalsearchterm("all",all_parse
, "usage: all (generatorfn x) (fn ... (var x) ...)");
216 registerglobalsearchterm("var",var_parse
, "usage: var variable");
218 /* Iterable functions */
219 registersearchterm(reg_nicksearch
, "channeliter",channeliter_parse
, 0, "Channel Iterable function - usage: (any (channeliter x) (match (var x) #twilight*))"); /* nick only */
221 /* Functions that work on strings?! */
222 registersearchterm(reg_nicksearch
, "cumodes", cumodes_parse
, 0, "usage: (cumodes (var x) <modes>)");
224 /* Notice functionality */
225 registersearchterm(reg_chansearch
,"notice",notice_parse
, 0, "NOTICE users in newsearch result. Note: evaluation order");
226 registersearchterm(reg_nicksearch
,"notice",notice_parse
, 0, "NOTICE users in newsearch result. Note: evaluation order");
228 /* Nick output filters */
229 regdisp(reg_nicksearch
,"default",printnick
, 0, "");
230 regdisp(reg_nicksearch
,"channels",printnick_channels
, 0, "include channels in output");
232 /* Channel output filters */
233 regdisp(reg_chansearch
,"default",printchannel
, 0, "");
234 regdisp(reg_chansearch
,"topic",printchannel_topic
, 0, "display channel topics");
235 regdisp(reg_chansearch
,"services",printchannel_services
, 0, "display services on channels");
237 /* Nick output filters */
238 regdisp(reg_usersearch
,"default",printuser
, 0, "");
243 searchList
*sl
, *psl
;
245 Command
*cmdlist
[100];
252 n
=getcommandlist(searchCmdTree
,cmdlist
,100);
254 deregistersearchterm( (searchCmd
*)cmdlist
[i
]->handler
, psl
->name
->content
, psl
->cmd
);
257 freesstring(psl
->name
);
263 deregistersearchcommand( reg_nicksearch
);
264 deregistersearchcommand( reg_chansearch
);
265 deregistersearchcommand( reg_usersearch
);
266 destroycommandtree( searchCmdTree
);
269 void registerglobalsearchterm(char *term
, parseFunc parsefunc
, char *help
) {
271 Command
*cmdlist
[100];
272 searchList
*sl
= malloc(sizeof(searchList
));
274 Error("newsearch", ERR_ERROR
, "malloc failed: registerglobalsearchterm");
279 sl
->name
= getsstring(term
, NSMAX_COMMAND_LEN
);
282 int len
=strlen(help
);
283 sl
->help
=(char *)malloc(len
+1);
285 Error("newsearch", ERR_ERROR
, "malloc failed: registerglobalsearchterm");
288 strncpy(sl
->help
, help
, len
);
289 sl
->help
[len
] = '\0';
295 if ( globalterms
!= NULL
) {
296 sl
->next
= globalterms
;
300 n
=getcommandlist(searchCmdTree
,cmdlist
,100);
302 /* maxparams is set to 1 to indicate a global term */
303 /* access level is set to 0 for all global terms */
304 addcommandexttotree( ((searchCmd
*)cmdlist
[i
]->handler
)->searchtree
,term
, 0, 1, (CommandHandler
) parsefunc
, help
);
308 void deregisterglobalsearchterm(char *term
, parseFunc parsefunc
) {
310 Command
*cmdlist
[100];
311 searchList
*sl
, *psl
=NULL
;
313 for (sl
=globalterms
; sl
; sl
=sl
->next
) {
314 if ( strcmp( sl
->name
->content
, term
) == 0 ) {
322 psl
->next
= sl
->next
;
325 n
=getcommandlist(searchCmdTree
,cmdlist
,100);
327 deletecommandfromtree( ((searchCmd
*)cmdlist
[i
]->handler
)->searchtree
, term
, (CommandHandler
) parsefunc
);
329 freesstring(sl
->name
);
334 void registersearchterm(searchCmd
*cmd
, char *term
, parseFunc parsefunc
, int level
, char *help
) {
335 /* NOTE: global terms are added to the tree elsewhere as we set maxparams to 1 to indicate global */
336 addcommandexttotree(cmd
->searchtree
, term
, level
, 0, (CommandHandler
) parsefunc
, help
);
339 void deregistersearchterm(searchCmd
*cmd
, char *term
, parseFunc parsefunc
) {
340 /* NOTE: global terms are removed from the tree within deregisterglobalsearchterm */
341 deletecommandfromtree(cmd
->searchtree
, term
, (CommandHandler
) parsefunc
);
344 static void controlwallwrapper(int level
, char *format
, ...) __attribute__ ((format (printf
, 2, 3)));
345 static void controlwallwrapper(int level
, char *format
, ...) {
349 va_start(ap
, format
);
350 vsnprintf(buf
, sizeof(buf
), format
, ap
);
351 controlwall(NO_OPER
, level
, "%s", buf
);
355 int parseopts(int cargc
, char **cargv
, int *arg
, int *limit
, void **subset
, void *display
, CommandTree
*sl
, replyFunc reply
, void *sender
) {
358 struct irc_in_addr sin
; unsigned char bits
;
360 if (*cargv
[0] == '-') {
364 for (ch
=cargv
[0]+1;*ch
;ch
++) {
368 reply(sender
,"Error: -l switch requires an argument (for help, see help <searchcmd>)");
371 *limit
=strtoul(cargv
[(*arg
)++],NULL
,10);
376 reply(sender
,"Error: -d switch requires an argument (for help, see help <searchcmd>)");
379 cmd
=findcommandintree(sl
, cargv
[*arg
],1);
381 reply(sender
,"Error: unknown output format %s (for help, see help <searchcmd>)",cargv
[*arg
]);
384 if ( !controlpermitted( cmd
->level
, sender
) ) {
385 reply(sender
,"Error: Access Denied for output format %s (for help, see help <searchcmd>)", cargv
[*arg
]);
388 *((void **)display
)=(void *)cmd
->handler
;
394 reply(sender
,"Error: -s switch requires an argument (for help, see help <searchcmd>)");
397 if (ipmask_parse(cargv
[*arg
], &sin
, &bits
) == 0) {
398 reply(sender
, "Error: Invalid CIDR mask supplied (for help, see help <searchcmd>)");
401 *subset
= (void *)refnode(iptree
, &sin
, bits
);
406 reply(sender
,"Unrecognised flag -%c. (for help, see help <searchcmd>)",*ch
);
415 void newsearch_ctxinit(searchCtx
*ctx
, searchParseFunc searchfn
, replyFunc replyfn
, wallFunc wallfn
, void *arg
, searchCmd
*cmd
, nick
*np
) {
416 memset(ctx
, 0, sizeof(searchCtx
));
418 ctx
->reply
= replyfn
;
420 ctx
->parser
= searchfn
;
422 ctx
->searchcmd
= cmd
;
426 int do_nicksearch_real(replyFunc reply
, wallFunc wall
, void *source
, int cargc
, char **cargv
) {
427 nick
*sender
= source
;
428 struct searchNode
*search
;
431 NickDisplayFunc display
=defaultnickfn
;
436 reply( sender
, "Usage: [flags] <criteria>");
437 reply( sender
, "For help, see help nicksearch");
441 ret
= parseopts(cargc
, cargv
, &arg
, &limit
, NULL
, (void *)&display
, reg_nicksearch
->outputtree
, reply
, sender
);
446 reply(sender
,"No search terms - aborting.");
451 rejoinline(cargv
[arg
],cargc
-arg
);
454 newsearch_ctxinit(&ctx
, search_parse
, reply
, wall
, NULL
, reg_nicksearch
, sender
);
456 if (!(search
= ctx
.parser(&ctx
, cargv
[arg
]))) {
457 reply(sender
,"Parse error: %s",parseError
);
461 nicksearch_exe(search
, &ctx
, sender
, display
, limit
);
463 (search
->free
)(&ctx
, search
);
468 int do_nicksearch(void *source
, int cargc
, char **cargv
) {
469 return do_nicksearch_real(controlreply
, controlwallwrapper
, source
, cargc
, cargv
);
472 void nicksearch_exe(struct searchNode
*search
, searchCtx
*ctx
, nick
*sender
, NickDisplayFunc display
, int limit
) {
475 unsigned int cmarker
;
476 unsigned int tchans
=0,uchans
=0;
479 senderNSExtern
= sender
;
481 /* Get a marker value to mark "seen" channels for unique count */
482 cmarker
=nextchanmarker();
484 /* The top-level node needs to return a BOOL */
485 search
=coerceNode(ctx
, search
, RETURNTYPE_BOOL
);
487 for (i
=0;i
<NICKHASHSIZE
;i
++) {
488 for (np
=nicktable
[i
];np
;np
=np
->next
) {
489 if ((search
->exe
)(ctx
, search
, np
)) {
490 /* Add total channels */
491 tchans
+= np
->channels
->cursi
;
493 /* Check channels for uniqueness */
494 cs
=(channel
**)np
->channels
->content
;
495 for (j
=0;j
<np
->channels
->cursi
;j
++) {
496 if (cs
[j
]->index
->marker
!= cmarker
) {
497 cs
[j
]->index
->marker
=cmarker
;
503 display(ctx
, sender
, np
);
506 ctx
->reply(sender
, "--- More than %d matches, skipping the rest",limit
);
512 ctx
->reply(sender
,"--- End of list: %d matches; users were on %u channels (%u unique, %.1f average clones)",
513 matches
, tchans
, uchans
, (float)tchans
/uchans
);
516 int do_chansearch_real(replyFunc reply
, wallFunc wall
, void *source
, int cargc
, char **cargv
) {
517 nick
*sender
= source
;
518 struct searchNode
*search
;
521 ChanDisplayFunc display
=defaultchanfn
;
526 reply( sender
, "Usage: [flags] <criteria>");
527 reply( sender
, "For help, see help chansearch");
531 ret
= parseopts(cargc
, cargv
, &arg
, &limit
, NULL
, (void *)&display
, reg_chansearch
->outputtree
, reply
, sender
);
536 reply(sender
,"No search terms - aborting.");
541 rejoinline(cargv
[arg
],cargc
-arg
);
544 newsearch_ctxinit(&ctx
, search_parse
, reply
, wall
, NULL
, reg_chansearch
, sender
);
545 if (!(search
= ctx
.parser(&ctx
, cargv
[arg
]))) {
546 reply(sender
,"Parse error: %s",parseError
);
550 chansearch_exe(search
, &ctx
, sender
, display
, limit
);
552 (search
->free
)(&ctx
, search
);
557 int do_chansearch(void *source
, int cargc
, char **cargv
) {
558 return do_chansearch_real(controlreply
, controlwallwrapper
, source
, cargc
, cargv
);
561 void chansearch_exe(struct searchNode
*search
, searchCtx
*ctx
, nick
*sender
, ChanDisplayFunc display
, int limit
) {
565 senderNSExtern
= sender
;
567 search
=coerceNode(ctx
, search
, RETURNTYPE_BOOL
);
569 for (i
=0;i
<CHANNELHASHSIZE
;i
++) {
570 for (cip
=chantable
[i
];cip
;cip
=cip
->next
) {
571 if ((search
->exe
)(ctx
, search
, cip
)) {
573 display(ctx
, sender
, cip
);
575 ctx
->reply(sender
, "--- More than %d matches, skipping the rest",limit
);
581 ctx
->reply(sender
,"--- End of list: %d matches", matches
);
584 int do_usersearch_real(replyFunc reply
, wallFunc wall
, void *source
, int cargc
, char **cargv
) {
585 nick
*sender
= source
;
586 struct searchNode
*search
;
589 UserDisplayFunc display
=defaultuserfn
;
594 reply( sender
, "Usage: [flags] <criteria>");
595 reply( sender
, "For help, see help usersearch");
599 ret
= parseopts(cargc
, cargv
, &arg
, &limit
, NULL
, (void *)&display
, reg_usersearch
->outputtree
, reply
, sender
);
604 reply(sender
,"No search terms - aborting.");
609 rejoinline(cargv
[arg
],cargc
-arg
);
612 newsearch_ctxinit(&ctx
, search_parse
, reply
, wall
, NULL
, reg_usersearch
, sender
);
614 if (!(search
= ctx
.parser(&ctx
, cargv
[arg
]))) {
615 reply(sender
,"Parse error: %s",parseError
);
619 usersearch_exe(search
, &ctx
, sender
, display
, limit
);
621 (search
->free
)(&ctx
, search
);
626 int do_usersearch(void *source
, int cargc
, char **cargv
) {
627 return do_usersearch_real(controlreply
, controlwallwrapper
, source
, cargc
, cargv
);
630 void usersearch_exe(struct searchNode
*search
, searchCtx
*ctx
, nick
*sender
, UserDisplayFunc display
, int limit
) {
634 senderNSExtern
= sender
;
636 search
=coerceNode(ctx
, search
, RETURNTYPE_BOOL
);
638 for (i
=0;i
<AUTHNAMEHASHSIZE
;i
++) {
639 for (aup
=authnametable
[i
];aup
;aup
=aup
->next
) {
640 if ((search
->exe
)(ctx
, search
, aup
)) {
642 display(ctx
, sender
, aup
);
644 ctx
->reply(sender
, "--- More than %d matches, skipping the rest",limit
);
650 ctx
->reply(sender
,"--- End of list: %d matches", matches
);
653 /* Free a coerce node */
654 void free_coerce(searchCtx
*ctx
, struct searchNode
*thenode
) {
655 struct coercedata
*cd
=thenode
->localdata
;
657 cd
->child
->free(ctx
, cd
->child
);
658 free(thenode
->localdata
);
662 /* Free a coerce node with a stringbuf allocated */
663 void free_coercestring(searchCtx
*ctx
, struct searchNode
*thenode
) {
664 free(((struct coercedata
*)thenode
->localdata
)->u
.stringbuf
);
665 free_coerce(ctx
, thenode
);
668 /* exe_tostr_null: return the constant string */
669 void *exe_tostr_null(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
670 struct coercedata
*cd
=thenode
->localdata
;
672 return cd
->u
.stringbuf
;
675 /* exe_val_null: return the constant value */
676 void *exe_val_null(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
677 struct coercedata
*cd
=thenode
->localdata
;
679 return (void *)cd
->u
.val
;
682 /* Lots of very dull type conversion functions */
683 void *exe_inttostr(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
684 struct coercedata
*cd
=thenode
->localdata
;
686 sprintf(cd
->u
.stringbuf
, "%lu", (unsigned long)(cd
->child
->exe
)(ctx
, cd
->child
, theinput
));
688 return cd
->u
.stringbuf
;
691 void *exe_booltostr(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
692 struct coercedata
*cd
=thenode
->localdata
;
694 if ((cd
->child
->exe
)(ctx
, cd
->child
, theinput
)) {
695 sprintf(cd
->u
.stringbuf
,"1");
697 cd
->u
.stringbuf
[0]='\0';
700 return cd
->u
.stringbuf
;
703 void *exe_strtoint(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
704 struct coercedata
*cd
=thenode
->localdata
;
706 return (void *)strtoul((cd
->child
->exe
)(ctx
,cd
->child
,theinput
),NULL
,10);
709 void *exe_booltoint(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
710 struct coercedata
*cd
=thenode
->localdata
;
712 /* Don't need to do anything */
713 return (cd
->child
->exe
)(ctx
, cd
->child
, theinput
);
716 void *exe_strtobool(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
717 struct coercedata
*cd
=thenode
->localdata
;
718 char *ch
=(cd
->child
->exe
)(ctx
, cd
->child
, theinput
);
720 if (!ch
|| *ch
=='\0' || (*ch
=='0' && ch
[1]=='\0')) {
727 void *exe_inttobool(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
728 struct coercedata
*cd
=thenode
->localdata
;
730 if ((cd
->child
->exe
)(ctx
, cd
->child
, theinput
)) {
737 struct searchNode
*coerceNode(searchCtx
*ctx
, struct searchNode
*thenode
, int type
) {
738 struct searchNode
*anode
;
739 struct coercedata
*cd
;
741 /* You can't coerce a NULL */
745 /* No effort required to coerce to the same type */
746 if (type
==(thenode
->returntype
& RETURNTYPE_TYPE
))
749 anode
=(struct searchNode
*)malloc(sizeof(struct searchNode
));
750 anode
->localdata
=cd
=(struct coercedata
*)malloc(sizeof(struct coercedata
));
752 anode
->returntype
=type
; /* We'll return what they want, always */
753 anode
->free
=free_coerce
;
756 case RETURNTYPE_STRING
:
757 /* For a string we'll need a buffer */
758 /* A 64-bit number prints out to 20 digits, this leaves some slack */
759 cd
->u
.stringbuf
=malloc(25);
760 anode
->free
=free_coercestring
;
762 switch(thenode
->returntype
& RETURNTYPE_TYPE
) {
765 if (thenode
->returntype
& RETURNTYPE_CONST
) {
766 /* Constant node: sort it out now */
767 sprintf(cd
->u
.stringbuf
, "%lu", (unsigned long)thenode
->exe(ctx
, thenode
, NULL
));
768 anode
->exe
=exe_tostr_null
;
769 anode
->returntype
|= RETURNTYPE_CONST
;
772 anode
->exe
=exe_inttostr
;
776 case RETURNTYPE_BOOL
:
777 if (thenode
->returntype
& RETURNTYPE_CONST
) {
778 /* Constant bool value */
779 if (thenode
->exe(ctx
, thenode
,NULL
)) {
781 sprintf(cd
->u
.stringbuf
, "1");
783 cd
->u
.stringbuf
[0] = '\0';
785 anode
->exe
=exe_tostr_null
;
786 anode
->returntype
|= RETURNTYPE_CONST
;
788 /* Variable bool value */
789 anode
->exe
=exe_booltostr
;
797 switch (thenode
->returntype
& RETURNTYPE_TYPE
) {
798 case RETURNTYPE_STRING
:
799 if (thenode
->returntype
& RETURNTYPE_CONST
) {
800 cd
->u
.val
=strtoul((thenode
->exe
)(ctx
, thenode
, NULL
), NULL
, 10);
801 anode
->exe
=exe_val_null
;
802 anode
->returntype
|= RETURNTYPE_CONST
;
804 anode
->exe
=exe_strtoint
;
809 case RETURNTYPE_BOOL
:
810 if (thenode
->returntype
& RETURNTYPE_CONST
) {
811 if ((thenode
->exe
)(ctx
, thenode
,NULL
))
816 anode
->exe
=exe_val_null
;
817 anode
->returntype
|= RETURNTYPE_CONST
;
819 anode
->exe
=exe_booltoint
;
826 case RETURNTYPE_BOOL
:
828 switch (thenode
->returntype
& RETURNTYPE_TYPE
) {
829 case RETURNTYPE_STRING
:
830 if (thenode
->returntype
& RETURNTYPE_CONST
) {
831 char *rv
=(char *)((thenode
->exe
)(ctx
, thenode
, NULL
));
832 if (!rv
|| *rv
=='\0' || (*rv
=='0' && rv
[1]=='\0'))
837 anode
->exe
=exe_val_null
;
838 anode
->returntype
|= RETURNTYPE_CONST
;
840 anode
->exe
=exe_strtobool
;
846 if (thenode
->returntype
& RETURNTYPE_CONST
) {
847 if ((thenode
->exe
)(ctx
, thenode
,NULL
))
852 anode
->exe
=exe_val_null
;
853 anode
->returntype
|= RETURNTYPE_CONST
;
855 anode
->exe
=exe_inttobool
;
865 /* Literals always return constant strings... */
866 void *literal_exe(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
867 if (thenode
->localdata
)
868 return ((sstring
*)thenode
->localdata
)->content
;
873 void literal_free(searchCtx
*ctx
, struct searchNode
*thenode
) {
874 freesstring(thenode
->localdata
);
879 * Given an input string, return a searchNode.
882 struct searchNode
*search_parse(searchCtx
*ctx
, char *input
) {
883 /* OK, we need to split the input into chunks on spaces and brackets.. */
884 char *argvector
[100];
889 struct searchNode
*thenode
;
891 /* If it starts with a bracket, it's a function call.. */
893 /* Skip past string */
894 for (ch
=input
;*ch
;ch
++);
895 if (*(ch
-1) != ')') {
896 parseError
= "Bracket mismatch!";
902 /* Split further args */
903 i
=-1; /* i = -1 BoW, 0 = inword, 1 = bracket nest depth */
904 j
=0; /* j = current arg */
908 for (ch
=input
;*ch
;ch
++) {
913 } else if (*ch
!= ' ') {
917 } else if (*ch
=='\"') {
929 } else if (*ch
=='\"') {
931 } else if (*ch
==' ') {
934 if(j
>= (sizeof(argvector
) / sizeof(*argvector
))) {
935 parseError
= "Too many arguments";
943 } else if (*ch
=='\"') {
945 } else if (*ch
=='(') {
947 } else if (*ch
==')') {
954 parseError
= "Bracket mismatch!";
958 if (*(ch
-1) == 0) /* if the last character was a space */
959 j
--; /* remove an argument */
961 if (!(cmd
=findcommandintree(ctx
->searchcmd
->searchtree
,argvector
[0],1))) {
962 parseError
= "Unknown command (for valid command list, see help <searchcmd>)";
965 if ( !controlpermitted( cmd
->level
, ctx
->sender
) ) {
966 parseError
= "Access Denied (for valid command list, see help <searchcmd>)";
969 return ((parseFunc
)cmd
->handler
)(ctx
, j
, argvector
+1);
974 for (ch
=input
;*ch
;ch
++);
976 if (*(ch
-1) != '\"') {
977 parseError
="Quote mismatch";
986 for (ch
=input
;*ch
;ch
++) {
990 } else if (*ch
=='\\') {
998 if (!(thenode
=(struct searchNode
*)malloc(sizeof(struct searchNode
)))) {
999 parseError
= "malloc: could not allocate memory for this search.";
1003 thenode
->localdata
= getsstring(thestring
,512);
1004 thenode
->returntype
= RETURNTYPE_CONST
| RETURNTYPE_STRING
;
1005 thenode
->exe
= literal_exe
;
1006 thenode
->free
= literal_free
;
1012 void nssnprintf(char *buf
, size_t size
, const char *format
, nick
*np
) {
1025 for(p
=format
;*p
;p
++) {
1027 if(!sbaddchar(&b
, *p
))
1035 if(!sbaddchar(&b
, *p
))
1043 c
= np
->nick
; break;
1045 c
= np
->ident
; break;
1047 c
= np
->host
->name
->content
; break;
1049 snprintf(hostbuf
, sizeof(hostbuf
), "%s", IPtostr(np
->p_ipaddr
));
1053 snprintf(hostbuf
, sizeof(hostbuf
), "%s!%s@%s", np
->nick
, np
->ident
, IPtostr(np
->p_ipaddr
));
1057 c
= "(bad format specifier)";
1060 if(!sbaddstr(&b
, c
))
1072 static char *var_tochar(searchCtx
*ctx
, char *arg
, searchNode
**variable
) {
1073 *variable
= ctx
->parser(ctx
, arg
);
1074 if (!(*variable
= coerceNode(ctx
, *variable
, RETURNTYPE_STRING
)))
1077 if(!((*variable
)->returntype
& RETURNTYPE_CONST
)) {
1078 parseError
= "only constant variables allowed";
1079 ((*variable
)->free
)(ctx
, *variable
);
1083 return (char *)((*variable
)->exe
)(ctx
, *variable
, NULL
);
1086 void free_val_null(searchCtx
*ctx
, struct searchNode
*thenode
) {
1089 struct searchVariable
*var_register(searchCtx
*ctx
, char *arg
, int type
) {
1090 searchNode
*variable
;
1091 struct searchVariable
*us
;
1095 if(ctx
->lastvar
>= MAX_VARIABLES
) {
1096 parseError
= "Maximum number of variables reached";
1100 us
= &ctx
->vars
[ctx
->lastvar
];
1102 var
= var_tochar(ctx
, arg
, &variable
);
1106 strlcpy(us
->name
, var
, sizeof(us
->name
));
1107 (variable
->free
)(ctx
, variable
);
1109 for(i
=0;i
<ctx
->lastvar
;i
++) {
1110 if(!strcmp(us
->name
, ctx
->vars
[i
].name
)) {
1111 parseError
= "variable name already in use";
1117 us
->data
.returntype
= type
;
1118 us
->data
.localdata
= &us
->cdata
;
1119 us
->data
.exe
= exe_val_null
;
1120 us
->data
.free
= free_val_null
;
1122 us
->cdata
.child
= NULL
;
1126 searchNode
*var_get(searchCtx
*ctx
, char *arg
) {
1127 searchNode
*variable
, *found
= NULL
;
1129 char *var
= var_tochar(ctx
, arg
, &variable
);
1133 for(i
=0;i
<ctx
->lastvar
;i
++) {
1134 if(!strcmp(var
, ctx
->vars
[i
].name
)) {
1135 found
= &ctx
->vars
[i
].data
;
1139 (variable
->free
)(ctx
, variable
);
1142 parseError
= "variable not found";
1146 void var_setstr(struct searchVariable
*v
, char *data
) {
1147 v
->cdata
.u
.stringbuf
= data
;