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 Commands and Operators:\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 Commands and Operators 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, "The user's nick!user@host; \"hostmask real\" returns nick!user@host\rreal"); /* nick only */
177 registersearchterm(reg_nicksearch
, "realname",realname_parse
, 0, "User's current realname"); /* nick only */
178 registersearchterm(reg_nicksearch
, "authname",authname_parse
, 0, "User's current authname or false"); /* nick only */
179 registersearchterm(reg_nicksearch
, "authts",authts_parse
, 0, "User's Auth timestamp"); /* nick only */
180 registersearchterm(reg_nicksearch
, "ident",ident_parse
, 0, "User's current ident"); /* nick only */
181 registersearchterm(reg_nicksearch
, "host",host_parse
, 0, "User's 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, "User's 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, "User's 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, "User's 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 /* Notice functionality */
222 registersearchterm(reg_chansearch
,"notice",notice_parse
, 0, "NOTICE users in newsearch result. Note: evaluation order");
223 registersearchterm(reg_nicksearch
,"notice",notice_parse
, 0, "NOTICE users in newsearch result. Note: evaluation order");
225 /* Nick output filters */
226 regdisp(reg_nicksearch
,"default",printnick
, 0, "");
227 regdisp(reg_nicksearch
,"channels",printnick_channels
, 0, "include channels in output");
229 /* Channel output filters */
230 regdisp(reg_chansearch
,"default",printchannel
, 0, "");
231 regdisp(reg_chansearch
,"topic",printchannel_topic
, 0, "display channel topics");
232 regdisp(reg_chansearch
,"services",printchannel_services
, 0, "display services on channels");
234 /* Nick output filters */
235 regdisp(reg_usersearch
,"default",printuser
, 0, "");
240 searchList
*sl
, *psl
;
242 Command
*cmdlist
[100];
249 n
=getcommandlist(searchCmdTree
,cmdlist
,100);
251 deregistersearchterm( (searchCmd
*)cmdlist
[i
]->handler
, psl
->name
->content
, psl
->cmd
);
254 freesstring(psl
->name
);
260 deregistersearchcommand( reg_nicksearch
);
261 deregistersearchcommand( reg_chansearch
);
262 deregistersearchcommand( reg_usersearch
);
263 destroycommandtree( searchCmdTree
);
266 void registerglobalsearchterm(char *term
, parseFunc parsefunc
, char *help
) {
268 Command
*cmdlist
[100];
269 searchList
*sl
= malloc(sizeof(searchList
));
271 Error("newsearch", ERR_ERROR
, "malloc failed: registerglobalsearchterm");
276 sl
->name
= getsstring(term
, NSMAX_COMMAND_LEN
);
279 int len
=strlen(help
);
280 sl
->help
=(char *)malloc(len
+1);
282 Error("newsearch", ERR_ERROR
, "malloc failed: registerglobalsearchterm");
285 strncpy(sl
->help
, help
, len
);
286 sl
->help
[len
] = '\0';
292 if ( globalterms
!= NULL
) {
293 sl
->next
= globalterms
;
297 n
=getcommandlist(searchCmdTree
,cmdlist
,100);
299 /* maxparams is set to 1 to indicate a global term */
300 /* access level is set to 0 for all global terms */
301 addcommandexttotree( ((searchCmd
*)cmdlist
[i
]->handler
)->searchtree
,term
, 0, 1, (CommandHandler
) parsefunc
, help
);
305 void deregisterglobalsearchterm(char *term
, parseFunc parsefunc
) {
307 Command
*cmdlist
[100];
308 searchList
*sl
, *psl
=NULL
;
310 for (sl
=globalterms
; sl
; sl
=sl
->next
) {
311 if ( strcmp( sl
->name
->content
, term
) == 0 ) {
319 psl
->next
= sl
->next
;
322 n
=getcommandlist(searchCmdTree
,cmdlist
,100);
324 deletecommandfromtree( ((searchCmd
*)cmdlist
[i
]->handler
)->searchtree
, term
, (CommandHandler
) parsefunc
);
326 freesstring(sl
->name
);
331 void registersearchterm(searchCmd
*cmd
, char *term
, parseFunc parsefunc
, int level
, char *help
) {
332 /* NOTE: global terms are added to the tree elsewhere as we set maxparams to 1 to indicate global */
333 addcommandexttotree(cmd
->searchtree
, term
, level
, 0, (CommandHandler
) parsefunc
, help
);
336 void deregistersearchterm(searchCmd
*cmd
, char *term
, parseFunc parsefunc
) {
337 /* NOTE: global terms are removed from the tree within deregisterglobalsearchterm */
338 deletecommandfromtree(cmd
->searchtree
, term
, (CommandHandler
) parsefunc
);
341 static void controlwallwrapper(int level
, char *format
, ...) __attribute__ ((format (printf
, 2, 3)));
342 static void controlwallwrapper(int level
, char *format
, ...) {
346 va_start(ap
, format
);
347 vsnprintf(buf
, sizeof(buf
), format
, ap
);
348 controlwall(NO_OPER
, level
, "%s", buf
);
352 int parseopts(int cargc
, char **cargv
, int *arg
, int *limit
, void **subset
, void **display
, CommandTree
*sl
, replyFunc reply
, void *sender
) {
355 struct irc_in_addr sin
; unsigned char bits
;
357 if (*cargv
[0] == '-') {
361 for (ch
=cargv
[0]+1;*ch
;ch
++) {
365 reply(sender
,"Error: -l switch requires an argument (for help, see help <searchcmd>)");
368 *limit
=strtoul(cargv
[(*arg
)++],NULL
,10);
373 reply(sender
,"Error: -d switch requires an argument (for help, see help <searchcmd>)");
376 cmd
=findcommandintree(sl
, cargv
[*arg
],1);
378 reply(sender
,"Error: unknown output format %s (for help, see help <searchcmd>)",cargv
[*arg
]);
381 if ( !controlpermitted( cmd
->level
, sender
) ) {
382 reply(sender
,"Error: Access Denied for output format %s (for help, see help <searchcmd>)", cargv
[*arg
]);
385 *display
=(void *)cmd
->handler
;
391 reply(sender
,"Error: -s switch requires an argument (for help, see help <searchcmd>)");
394 if (ipmask_parse(cargv
[*arg
], &sin
, &bits
) == 0) {
395 reply(sender
, "Error: Invalid CIDR mask supplied (for help, see help <searchcmd>)");
398 *subset
= (void *)refnode(iptree
, &sin
, bits
);
403 reply(sender
,"Unrecognised flag -%c. (for help, see help <searchcmd>)",*ch
);
412 void newsearch_ctxinit(searchCtx
*ctx
, searchParseFunc searchfn
, replyFunc replyfn
, wallFunc wallfn
, void *arg
, searchCmd
*cmd
, nick
*np
) {
413 memset(ctx
, 0, sizeof(searchCtx
));
415 ctx
->reply
= replyfn
;
417 ctx
->parser
= searchfn
;
419 ctx
->searchcmd
= cmd
;
423 int do_nicksearch_real(replyFunc reply
, wallFunc wall
, void *source
, int cargc
, char **cargv
) {
424 nick
*sender
= source
;
425 struct searchNode
*search
;
428 NickDisplayFunc display
=defaultnickfn
;
433 reply( sender
, "Usage: [flags] <criteria>");
434 reply( sender
, "For help, see help nicksearch");
438 ret
= parseopts(cargc
, cargv
, &arg
, &limit
, NULL
, (void **)&display
, reg_nicksearch
->outputtree
, reply
, sender
);
443 reply(sender
,"No search terms - aborting.");
448 rejoinline(cargv
[arg
],cargc
-arg
);
451 newsearch_ctxinit(&ctx
, search_parse
, reply
, wall
, NULL
, reg_nicksearch
, sender
);
453 if (!(search
= ctx
.parser(&ctx
, cargv
[arg
]))) {
454 reply(sender
,"Parse error: %s",parseError
);
458 nicksearch_exe(search
, &ctx
, sender
, display
, limit
);
460 (search
->free
)(&ctx
, search
);
465 int do_nicksearch(void *source
, int cargc
, char **cargv
) {
466 return do_nicksearch_real(controlreply
, controlwallwrapper
, source
, cargc
, cargv
);
469 void nicksearch_exe(struct searchNode
*search
, searchCtx
*ctx
, nick
*sender
, NickDisplayFunc display
, int limit
) {
472 unsigned int cmarker
;
473 unsigned int tchans
=0,uchans
=0;
476 senderNSExtern
= sender
;
478 /* Get a marker value to mark "seen" channels for unique count */
479 cmarker
=nextchanmarker();
481 /* The top-level node needs to return a BOOL */
482 search
=coerceNode(ctx
, search
, RETURNTYPE_BOOL
);
484 for (i
=0;i
<NICKHASHSIZE
;i
++) {
485 for (np
=nicktable
[i
];np
;np
=np
->next
) {
486 if ((search
->exe
)(ctx
, search
, np
)) {
487 /* Add total channels */
488 tchans
+= np
->channels
->cursi
;
490 /* Check channels for uniqueness */
491 cs
=(channel
**)np
->channels
->content
;
492 for (j
=0;j
<np
->channels
->cursi
;j
++) {
493 if (cs
[j
]->index
->marker
!= cmarker
) {
494 cs
[j
]->index
->marker
=cmarker
;
500 display(ctx
, sender
, np
);
503 ctx
->reply(sender
, "--- More than %d matches, skipping the rest",limit
);
509 ctx
->reply(sender
,"--- End of list: %d matches; users were on %u channels (%u unique, %.1f average clones)",
510 matches
, tchans
, uchans
, (float)tchans
/uchans
);
513 int do_chansearch_real(replyFunc reply
, wallFunc wall
, void *source
, int cargc
, char **cargv
) {
514 nick
*sender
= source
;
515 struct searchNode
*search
;
518 ChanDisplayFunc display
=defaultchanfn
;
523 reply( sender
, "Usage: [flags] <criteria>");
524 reply( sender
, "For help, see help chansearch");
528 ret
= parseopts(cargc
, cargv
, &arg
, &limit
, NULL
, (void **)&display
, reg_chansearch
->outputtree
, reply
, sender
);
533 reply(sender
,"No search terms - aborting.");
538 rejoinline(cargv
[arg
],cargc
-arg
);
541 newsearch_ctxinit(&ctx
, search_parse
, reply
, wall
, NULL
, reg_chansearch
, sender
);
542 if (!(search
= ctx
.parser(&ctx
, cargv
[arg
]))) {
543 reply(sender
,"Parse error: %s",parseError
);
547 chansearch_exe(search
, &ctx
, sender
, display
, limit
);
549 (search
->free
)(&ctx
, search
);
554 int do_chansearch(void *source
, int cargc
, char **cargv
) {
555 return do_chansearch_real(controlreply
, controlwallwrapper
, source
, cargc
, cargv
);
558 void chansearch_exe(struct searchNode
*search
, searchCtx
*ctx
, nick
*sender
, ChanDisplayFunc display
, int limit
) {
562 senderNSExtern
= sender
;
564 search
=coerceNode(ctx
, search
, RETURNTYPE_BOOL
);
566 for (i
=0;i
<CHANNELHASHSIZE
;i
++) {
567 for (cip
=chantable
[i
];cip
;cip
=cip
->next
) {
568 if ((search
->exe
)(ctx
, search
, cip
)) {
570 display(ctx
, sender
, cip
);
572 ctx
->reply(sender
, "--- More than %d matches, skipping the rest",limit
);
578 ctx
->reply(sender
,"--- End of list: %d matches", matches
);
581 int do_usersearch_real(replyFunc reply
, wallFunc wall
, void *source
, int cargc
, char **cargv
) {
582 nick
*sender
= source
;
583 struct searchNode
*search
;
586 UserDisplayFunc display
=defaultuserfn
;
591 reply( sender
, "Usage: [flags] <criteria>");
592 reply( sender
, "For help, see help usersearch");
596 ret
= parseopts(cargc
, cargv
, &arg
, &limit
, NULL
, (void **)&display
, reg_usersearch
->outputtree
, reply
, sender
);
601 reply(sender
,"No search terms - aborting.");
606 rejoinline(cargv
[arg
],cargc
-arg
);
609 newsearch_ctxinit(&ctx
, search_parse
, reply
, wall
, NULL
, reg_usersearch
, sender
);
611 if (!(search
= ctx
.parser(&ctx
, cargv
[arg
]))) {
612 reply(sender
,"Parse error: %s",parseError
);
616 usersearch_exe(search
, &ctx
, sender
, display
, limit
);
618 (search
->free
)(&ctx
, search
);
623 int do_usersearch(void *source
, int cargc
, char **cargv
) {
624 return do_usersearch_real(controlreply
, controlwallwrapper
, source
, cargc
, cargv
);
627 void usersearch_exe(struct searchNode
*search
, searchCtx
*ctx
, nick
*sender
, UserDisplayFunc display
, int limit
) {
631 senderNSExtern
= sender
;
633 search
=coerceNode(ctx
, search
, RETURNTYPE_BOOL
);
635 for (i
=0;i
<AUTHNAMEHASHSIZE
;i
++) {
636 for (aup
=authnametable
[i
];aup
;aup
=aup
->next
) {
637 if ((search
->exe
)(ctx
, search
, aup
)) {
639 display(ctx
, sender
, aup
);
641 ctx
->reply(sender
, "--- More than %d matches, skipping the rest",limit
);
647 ctx
->reply(sender
,"--- End of list: %d matches", matches
);
650 /* Free a coerce node */
651 void free_coerce(searchCtx
*ctx
, struct searchNode
*thenode
) {
652 struct coercedata
*cd
=thenode
->localdata
;
654 cd
->child
->free(ctx
, cd
->child
);
655 free(thenode
->localdata
);
659 /* Free a coerce node with a stringbuf allocated */
660 void free_coercestring(searchCtx
*ctx
, struct searchNode
*thenode
) {
661 free(((struct coercedata
*)thenode
->localdata
)->u
.stringbuf
);
662 free_coerce(ctx
, thenode
);
665 /* exe_tostr_null: return the constant string */
666 void *exe_tostr_null(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
667 struct coercedata
*cd
=thenode
->localdata
;
669 return cd
->u
.stringbuf
;
672 /* exe_val_null: return the constant value */
673 void *exe_val_null(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
674 struct coercedata
*cd
=thenode
->localdata
;
676 return (void *)cd
->u
.val
;
679 /* Lots of very dull type conversion functions */
680 void *exe_inttostr(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
681 struct coercedata
*cd
=thenode
->localdata
;
683 sprintf(cd
->u
.stringbuf
, "%lu", (unsigned long)(cd
->child
->exe
)(ctx
, cd
->child
, theinput
));
685 return cd
->u
.stringbuf
;
688 void *exe_booltostr(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
689 struct coercedata
*cd
=thenode
->localdata
;
691 if ((cd
->child
->exe
)(ctx
, cd
->child
, theinput
)) {
692 sprintf(cd
->u
.stringbuf
,"1");
694 cd
->u
.stringbuf
[0]='\0';
697 return cd
->u
.stringbuf
;
700 void *exe_strtoint(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
701 struct coercedata
*cd
=thenode
->localdata
;
703 return (void *)strtoul((cd
->child
->exe
)(ctx
,cd
->child
,theinput
),NULL
,10);
706 void *exe_booltoint(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
707 struct coercedata
*cd
=thenode
->localdata
;
709 /* Don't need to do anything */
710 return (cd
->child
->exe
)(ctx
, cd
->child
, theinput
);
713 void *exe_strtobool(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
714 struct coercedata
*cd
=thenode
->localdata
;
715 char *ch
=(cd
->child
->exe
)(ctx
, cd
->child
, theinput
);
717 if (!ch
|| *ch
=='\0' || (*ch
=='0' && ch
[1]=='\0')) {
724 void *exe_inttobool(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
725 struct coercedata
*cd
=thenode
->localdata
;
727 if ((cd
->child
->exe
)(ctx
, cd
->child
, theinput
)) {
734 struct searchNode
*coerceNode(searchCtx
*ctx
, struct searchNode
*thenode
, int type
) {
735 struct searchNode
*anode
;
736 struct coercedata
*cd
;
738 /* You can't coerce a NULL */
742 /* No effort required to coerce to the same type */
743 if (type
==(thenode
->returntype
& RETURNTYPE_TYPE
))
746 anode
=(struct searchNode
*)malloc(sizeof(struct searchNode
));
747 anode
->localdata
=cd
=(struct coercedata
*)malloc(sizeof(struct coercedata
));
749 anode
->returntype
=type
; /* We'll return what they want, always */
750 anode
->free
=free_coerce
;
753 case RETURNTYPE_STRING
:
754 /* For a string we'll need a buffer */
755 /* A 64-bit number prints out to 20 digits, this leaves some slack */
756 cd
->u
.stringbuf
=malloc(25);
757 anode
->free
=free_coercestring
;
759 switch(thenode
->returntype
& RETURNTYPE_TYPE
) {
762 if (thenode
->returntype
& RETURNTYPE_CONST
) {
763 /* Constant node: sort it out now */
764 sprintf(cd
->u
.stringbuf
, "%lu", (unsigned long)thenode
->exe(ctx
, thenode
, NULL
));
765 anode
->exe
=exe_tostr_null
;
766 anode
->returntype
|= RETURNTYPE_CONST
;
769 anode
->exe
=exe_inttostr
;
773 case RETURNTYPE_BOOL
:
774 if (thenode
->returntype
& RETURNTYPE_CONST
) {
775 /* Constant bool value */
776 if (thenode
->exe(ctx
, thenode
,NULL
)) {
778 sprintf(cd
->u
.stringbuf
, "1");
780 cd
->u
.stringbuf
[0] = '\0';
782 anode
->exe
=exe_tostr_null
;
783 anode
->returntype
|= RETURNTYPE_CONST
;
785 /* Variable bool value */
786 anode
->exe
=exe_booltostr
;
794 switch (thenode
->returntype
& RETURNTYPE_TYPE
) {
795 case RETURNTYPE_STRING
:
796 if (thenode
->returntype
& RETURNTYPE_CONST
) {
797 cd
->u
.val
=strtoul((thenode
->exe
)(ctx
, thenode
, NULL
), NULL
, 10);
798 anode
->exe
=exe_val_null
;
799 anode
->returntype
|= RETURNTYPE_CONST
;
801 anode
->exe
=exe_strtoint
;
806 case RETURNTYPE_BOOL
:
807 if (thenode
->returntype
& RETURNTYPE_CONST
) {
808 if ((thenode
->exe
)(ctx
, thenode
,NULL
))
813 anode
->exe
=exe_val_null
;
814 anode
->returntype
|= RETURNTYPE_CONST
;
816 anode
->exe
=exe_booltoint
;
823 case RETURNTYPE_BOOL
:
825 switch (thenode
->returntype
& RETURNTYPE_TYPE
) {
826 case RETURNTYPE_STRING
:
827 if (thenode
->returntype
& RETURNTYPE_CONST
) {
828 char *rv
=(char *)((thenode
->exe
)(ctx
, thenode
, NULL
));
829 if (!rv
|| *rv
=='\0' || (*rv
=='0' && rv
[1]=='\0'))
834 anode
->exe
=exe_val_null
;
835 anode
->returntype
|= RETURNTYPE_CONST
;
837 anode
->exe
=exe_strtobool
;
843 if (thenode
->returntype
& RETURNTYPE_CONST
) {
844 if ((thenode
->exe
)(ctx
, thenode
,NULL
))
849 anode
->exe
=exe_val_null
;
850 anode
->returntype
|= RETURNTYPE_CONST
;
852 anode
->exe
=exe_inttobool
;
862 /* Literals always return constant strings... */
863 void *literal_exe(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
864 if (thenode
->localdata
)
865 return ((sstring
*)thenode
->localdata
)->content
;
870 void literal_free(searchCtx
*ctx
, struct searchNode
*thenode
) {
871 freesstring(thenode
->localdata
);
876 * Given an input string, return a searchNode.
879 struct searchNode
*search_parse(searchCtx
*ctx
, char *input
) {
880 /* OK, we need to split the input into chunks on spaces and brackets.. */
881 char *argvector
[100];
886 struct searchNode
*thenode
;
888 /* If it starts with a bracket, it's a function call.. */
890 /* Skip past string */
891 for (ch
=input
;*ch
;ch
++);
892 if (*(ch
-1) != ')') {
893 parseError
= "Bracket mismatch!";
899 /* Split further args */
900 i
=-1; /* i = -1 BoW, 0 = inword, 1 = bracket nest depth */
901 j
=0; /* j = current arg */
905 for (ch
=input
;*ch
;ch
++) {
910 } else if (*ch
!= ' ') {
914 } else if (*ch
=='\"') {
926 } else if (*ch
=='\"') {
928 } else if (*ch
==' ') {
931 if(j
>= (sizeof(argvector
) / sizeof(*argvector
))) {
932 parseError
= "Too many arguments";
940 } else if (*ch
=='\"') {
942 } else if (*ch
=='(') {
944 } else if (*ch
==')') {
951 parseError
= "Bracket mismatch!";
955 if (*(ch
-1) == 0) /* if the last character was a space */
956 j
--; /* remove an argument */
958 if (!(cmd
=findcommandintree(ctx
->searchcmd
->searchtree
,argvector
[0],1))) {
959 parseError
= "Unknown command (for valid command list, see help <searchcmd>)";
962 if ( !controlpermitted( cmd
->level
, ctx
->sender
) ) {
963 parseError
= "Access Denied (for valid command list, see help <searchcmd>)";
966 return ((parseFunc
)cmd
->handler
)(ctx
, j
, argvector
+1);
971 for (ch
=input
;*ch
;ch
++);
973 if (*(ch
-1) != '\"') {
974 parseError
="Quote mismatch";
983 for (ch
=input
;*ch
;ch
++) {
987 } else if (*ch
=='\\') {
995 if (!(thenode
=(struct searchNode
*)malloc(sizeof(struct searchNode
)))) {
996 parseError
= "malloc: could not allocate memory for this search.";
1000 thenode
->localdata
= getsstring(thestring
,512);
1001 thenode
->returntype
= RETURNTYPE_CONST
| RETURNTYPE_STRING
;
1002 thenode
->exe
= literal_exe
;
1003 thenode
->free
= literal_free
;
1009 void nssnprintf(char *buf
, size_t size
, const char *format
, nick
*np
) {
1022 for(p
=format
;*p
;p
++) {
1024 if(!sbaddchar(&b
, *p
))
1032 if(!sbaddchar(&b
, *p
))
1040 c
= np
->nick
; break;
1042 c
= np
->ident
; break;
1044 c
= np
->host
->name
->content
; break;
1046 snprintf(hostbuf
, sizeof(hostbuf
), "%s", IPtostr(np
->p_ipaddr
));
1050 snprintf(hostbuf
, sizeof(hostbuf
), "%s!%s@%s", np
->nick
, np
->ident
, IPtostr(np
->p_ipaddr
));
1054 c
= "(bad format specifier)";
1057 if(!sbaddstr(&b
, c
))
1069 static char *var_tochar(searchCtx
*ctx
, char *arg
, searchNode
**variable
) {
1070 *variable
= ctx
->parser(ctx
, arg
);
1071 if (!(*variable
= coerceNode(ctx
, *variable
, RETURNTYPE_STRING
)))
1074 if(!((*variable
)->returntype
& RETURNTYPE_CONST
)) {
1075 parseError
= "only constant variables allowed";
1076 ((*variable
)->free
)(ctx
, *variable
);
1080 return (char *)((*variable
)->exe
)(ctx
, *variable
, NULL
);
1083 void free_val_null(searchCtx
*ctx
, struct searchNode
*thenode
) {
1086 struct searchVariable
*var_register(searchCtx
*ctx
, char *arg
, int type
) {
1087 searchNode
*variable
;
1088 struct searchVariable
*us
;
1092 if(ctx
->lastvar
>= MAX_VARIABLES
) {
1093 parseError
= "Maximum number of variables reached";
1097 us
= &ctx
->vars
[ctx
->lastvar
];
1099 var
= var_tochar(ctx
, arg
, &variable
);
1103 strlcpy(us
->name
, var
, sizeof(us
->name
));
1104 (variable
->free
)(ctx
, variable
);
1106 for(i
=0;i
<ctx
->lastvar
;i
++) {
1107 if(!strcmp(us
->name
, ctx
->vars
[i
].name
)) {
1108 parseError
= "variable name already in use";
1114 us
->data
.returntype
= type
;
1115 us
->data
.localdata
= &us
->cdata
;
1116 us
->data
.exe
= exe_val_null
;
1117 us
->data
.free
= free_val_null
;
1119 us
->cdata
.child
= NULL
;
1123 searchNode
*var_get(searchCtx
*ctx
, char *arg
) {
1124 searchNode
*variable
, *found
= NULL
;
1126 char *var
= var_tochar(ctx
, arg
, &variable
);
1130 for(i
=0;i
<ctx
->lastvar
;i
++) {
1131 if(!strcmp(var
, ctx
->vars
[i
].name
)) {
1132 found
= &ctx
->vars
[i
].data
;
1136 (variable
->free
)(ctx
, variable
);
1139 parseError
= "variable not found";
1143 void var_setstr(struct searchVariable
*v
, char *data
) {
1144 v
->cdata
.u
.stringbuf
= data
;