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"
19 CommandTree
*searchCmdTree
;
20 searchList
*globalterms
= NULL
;
22 int do_nicksearch(void *source
, int cargc
, char **cargv
);
23 int do_chansearch(void *source
, int cargc
, char **cargv
);
24 int do_usersearch(void *source
, int cargc
, char **cargv
);
26 void printnick_channels(searchCtx
*, nick
*, nick
*);
27 void printchannel(searchCtx
*, nick
*, chanindex
*);
28 void printchannel_topic(searchCtx
*, nick
*, chanindex
*);
29 void printchannel_services(searchCtx
*, nick
*, chanindex
*);
31 UserDisplayFunc defaultuserfn
= printuser
;
32 NickDisplayFunc defaultnickfn
= printnick
;
33 ChanDisplayFunc defaultchanfn
= printchannel
;
35 searchCmd
*reg_nicksearch
, *reg_chansearch
, *reg_usersearch
;
36 void displaycommandhelp(nick
*, Command
*);
37 void displaystrerror(replyFunc
, nick
*, const char *);
39 searchCmd
*registersearchcommand(char *name
, int level
, CommandHandler cmd
, void *defaultdisplayfunc
) {
43 registercontrolhelpfunccmd(name
, NO_OPER
,4, cmd
, &displaycommandhelp
);
44 acmd
=(struct searchCmd
*)malloc(sizeof(struct searchCmd
));
46 Error("newsearch", ERR_ERROR
, "malloc failed: registersearchcommand");
51 acmd
->name
= getsstring( name
, NSMAX_COMMAND_LEN
);
52 acmd
->outputtree
= newcommandtree();
53 acmd
->searchtree
= newcommandtree();
55 addcommandtotree(searchCmdTree
, name
, 0, 0, (CommandHandler
)acmd
);
57 for (sl
=globalterms
; sl
; sl
=sl
->next
) {
58 addcommandexttotree(acmd
->searchtree
, sl
->name
->content
, 0, 1, (CommandHandler
)sl
->cmd
, sl
->help
);
64 void deregistersearchcommand(searchCmd
*scmd
) {
65 deregistercontrolcmd(scmd
->name
->content
, (CommandHandler
)scmd
->handler
);
66 destroycommandtree(scmd
->outputtree
);
67 destroycommandtree(scmd
->searchtree
);
68 deletecommandfromtree(searchCmdTree
, scmd
->name
->content
, (CommandHandler
) scmd
);
69 freesstring(scmd
->name
);
73 void displaycommandhelp(nick
*np
, Command
*cmd
) {
75 Command
*cmdlist
[100], *acmdlist
[100];
78 n
=getcommandlist(searchCmdTree
,cmdlist
,100);
80 /* note: we may want to only deregister a command if we've already registered it, for now, try de-registering new commands anyway */
81 if ( ((searchCmd
*)cmdlist
[i
]->handler
)->handler
!= cmd
->handler
)
83 acmd
= ((searchCmd
*)(cmdlist
[i
]->handler
));
85 controlreply(np
, "Usage: [flags] <criteria>\n");
86 controlreply(np
, "Flags:\n");
87 controlreply(np
, " -l int : Limit number of rows of results\n");
88 controlreply(np
, " -d string : a valid output format for the results\n");
89 controlreply(np
, " -s subset : ipmask subset of network to search (only for node search)\n");
90 controlreply(np
, " \n");
91 controlreply(np
, "Available Output Formats:\n");
93 m
=getcommandlist(acmd
->outputtree
,acmdlist
,100);
95 if ( controlpermitted( acmdlist
[j
]->level
, np
) ) {
96 char *help
=(char *)acmdlist
[j
]->ext
;
97 if ( help
&& help
[0] != '\0')
98 controlreply(np
, "%-10s: %s\n", acmdlist
[j
]->command
->content
, help
);
100 controlreply(np
, "%s\n", acmdlist
[j
]->command
->content
);
104 controlreply(np
, " \n");
105 controlreply(np
, "Available Global Commands and Operators:\n" );
106 m
=getcommandlist(acmd
->searchtree
,acmdlist
,100);
108 if ( acmdlist
[j
]->maxparams
) {
109 char *help
=(char *)acmdlist
[j
]->ext
;
110 if ( help
&& help
[0] != '\0')
111 controlreply(np
, "%-10s: %s\n", acmdlist
[j
]->command
->content
, help
);
113 controlreply(np
, "%s\n", acmdlist
[j
]->command
->content
);
117 controlreply(np
, " \n");
118 controlreply(np
, "Available Commands and Operators for %s:\n", acmd
->name
->content
);
120 m
=getcommandlist(acmd
->searchtree
,acmdlist
,100);
122 if ( !acmdlist
[j
]->maxparams
&& controlpermitted( acmdlist
[j
]->level
, np
) ) {
123 char *help
=(char *)acmdlist
[j
]->ext
;
124 if ( help
&& help
[0] != '\0')
125 controlreply(np
, "%-10s: %s\n", acmdlist
[j
]->command
->content
, help
);
127 controlreply(np
, "%s\n", acmdlist
[j
]->command
->content
);
133 void regdisp( searchCmd
*cmd
, const char *name
, void *handler
, int level
, char *help
) {
134 addcommandexttotree(cmd
->outputtree
, name
, level
, 0, (CommandHandler
) handler
, (char *)help
);
137 void unregdisp( searchCmd
*cmd
, const char *name
, void *handler
) {
138 deletecommandfromtree(cmd
->outputtree
, name
, (CommandHandler
) handler
);
141 void *findcommandinlist( searchList
*sl
, char *name
){
143 if(strcmp(sl
->name
->content
,name
) == 0 ) {
151 const char *parseError
;
152 /* used for *_free functions that need to warn users of certain things
153 i.e. hitting too many users in a (kill) or (gline) */
154 nick
*senderNSExtern
;
157 searchCmdTree
=newcommandtree();
159 reg_nicksearch
= (searchCmd
*)registersearchcommand("nicksearch",NO_OPER
,&do_nicksearch
, printnick
);
160 reg_chansearch
= (searchCmd
*)registersearchcommand("chansearch",NO_OPER
,&do_chansearch
, printchannel
);
161 reg_usersearch
= (searchCmd
*)registersearchcommand("usersearch",NO_OPER
,&do_usersearch
, printuser
);
163 /* Boolean operations */
164 registerglobalsearchterm("and",and_parse
, "usage: (and (X) (X))" );
165 registerglobalsearchterm("not",not_parse
, "usage: (not (X))");
166 registerglobalsearchterm("or",or_parse
, "usage: (or (X) (X))" );
168 registerglobalsearchterm("eq",eq_parse
, "usage: (eq (X) Y)");
170 registerglobalsearchterm("lt",lt_parse
, "usage: (lt (X) int)");
171 registerglobalsearchterm("gt",gt_parse
, "usage: (gt (X) int)");
173 /* String operations */
174 registerglobalsearchterm("match",match_parse
, "usage: (match (X) string)");
175 registerglobalsearchterm("regex",regex_parse
, "usage: (regex (X) string)");
176 registerglobalsearchterm("length",length_parse
, "usage: (length string)");
178 /* Nickname operations */
179 registersearchterm(reg_nicksearch
, "hostmask",hostmask_parse
, 0, "The user's nick!user@host; \"hostmask real\" returns nick!user@host\rreal"); /* nick only */
180 registersearchterm(reg_nicksearch
, "realname",realname_parse
, 0, "User's current realname"); /* nick only */
181 registersearchterm(reg_nicksearch
, "away",away_parse
, 0, "User's current away message"); /* nick only */
182 registersearchterm(reg_nicksearch
, "authname",authname_parse
, 0, "User's current authname or false"); /* nick only */
183 registersearchterm(reg_nicksearch
, "authts",authts_parse
, 0, "User's Auth timestamp"); /* nick only */
184 registersearchterm(reg_nicksearch
, "ident",ident_parse
, 0, "User's current ident"); /* nick only */
185 registersearchterm(reg_nicksearch
, "host",host_parse
, 0, "User's host, allow \"host real\" to match real host"); /* nick only */
186 registersearchterm(reg_nicksearch
, "channel",channel_parse
, 0, "Valid Channel Name to match users against"); /* nick only */
187 registersearchterm(reg_nicksearch
, "timestamp",timestamp_parse
, 0, "User's Timestamp"); /* nick only */
188 registersearchterm(reg_nicksearch
, "country",country_parse
, 0, "2 letter country code (data source is geoip)"); /* nick only */
189 registersearchterm(reg_nicksearch
, "ip",ip_parse
, 0, "User's IP - ipv4 or ipv6 format as appropriate. Note: not 6to4"); /* nick only */
190 registersearchterm(reg_nicksearch
, "channels",channels_parse
, 0, "Channel Count"); /* nick only */
191 registersearchterm(reg_nicksearch
, "server",server_parse
, 0, "Server Name. Either (server string) or (match (server) string)"); /* nick only */
192 registersearchterm(reg_nicksearch
, "authid",authid_parse
, 0, "User's Auth ID"); /* nick only */
193 registersearchterm(reg_nicksearch
, "cidr",cidr_parse
, 0, "CIDR matching"); /* nick only */
194 registersearchterm(reg_nicksearch
, "ipvsix",ipv6_parse
, 0, "IPv6 user"); /* nick only */
196 /* Channel operations */
197 registersearchterm(reg_chansearch
, "exists",exists_parse
, 0, "Returns if channel exists on network. Note: newserv may store data on empty channels"); /* channel only */
198 registersearchterm(reg_chansearch
, "services",services_parse
, 0, ""); /* channel only */
199 registersearchterm(reg_chansearch
, "size",size_parse
, 0, "Channel user count"); /* channel only */
200 registersearchterm(reg_chansearch
, "name",name_parse
, 0, "Channel Name"); /* channel only */
201 registersearchterm(reg_chansearch
, "topic",topic_parse
, 0, "Channel topic"); /* channel only */
202 registersearchterm(reg_chansearch
, "oppct",oppct_parse
, 0, "Percentage Opped"); /* channel only */
203 registersearchterm(reg_chansearch
, "cumodecount",cumodecount_parse
, 0, "Count of users with given channel modes"); /* channel only */
204 registersearchterm(reg_chansearch
, "cumodepct",cumodepct_parse
, 0, "Percentage of users with given channel modes"); /* channel only */
205 registersearchterm(reg_chansearch
, "uniquehostpct",hostpct_parse
, 0, "uniquehost percent"); /* channel only */
206 registersearchterm(reg_chansearch
, "authedpct",authedpct_parse
, 0, "Percentage of authed users"); /* channel only */
207 registersearchterm(reg_chansearch
, "kick",kick_parse
, 0, "KICK users channels in newsearch result. Note: evaluation order"); /* channel only */
209 /* Nickname / channel operations */
210 registersearchterm(reg_chansearch
, "modes",modes_parse
, 0, "User Modes");
211 registersearchterm(reg_nicksearch
, "modes",modes_parse
, 0, "Channel Modes");
212 registersearchterm(reg_chansearch
, "nick",nick_parse
, 0, "Nickname");
213 registersearchterm(reg_nicksearch
, "nick",nick_parse
, 0, "Nickname");
215 /* Kill / gline parameters */
216 registersearchterm(reg_chansearch
,"kill",kill_parse
, 0, "KILL users in newsearch result. Note: evaluation order");
217 registersearchterm(reg_chansearch
,"gline",gline_parse
, 0, "GLINE users in newsearch result. Note: evaluation order");
218 registersearchterm(reg_nicksearch
,"kill",kill_parse
, 0, "KILL users in newsearch result. Note: evaluation order");
219 registersearchterm(reg_nicksearch
,"gline",gline_parse
, 0, "GLINE users in newsearch result. Note: evaluation order");
221 /* Iteration functionality */
222 registerglobalsearchterm("any",any_parse
, "usage: any (generatorfn x) (fn ... (var x) ...)");
223 registerglobalsearchterm("all",all_parse
, "usage: all (generatorfn x) (fn ... (var x) ...)");
224 registerglobalsearchterm("var",var_parse
, "usage: var variable");
226 /* Iterable functions */
227 registersearchterm(reg_nicksearch
, "channeliter",channeliter_parse
, 0, "Channel Iterable function - usage: (any (channeliter x) (match (var x) #twilight*))"); /* nick only */
228 registersearchterm(reg_chansearch
, "nickiter",nickiter_parse
, 0, "Nick Iterable function - usage: (any (nickiter x) (match (var x) nickname*))"); /* channel only */
230 /* Functions that work on strings?! */
231 registersearchterm(reg_nicksearch
, "cumodes", cumodes_parse
, 0, "usage: (cumodes (var x) <modes>)");
232 registersearchterm(reg_chansearch
, "cumodes", cumodes_parse
, 0, "usage: (cumodes (var x) <modes>)");
234 /* Notice functionality */
235 registersearchterm(reg_chansearch
,"notice",notice_parse
, 0, "NOTICE users in newsearch result. Note: evaluation order");
236 registersearchterm(reg_nicksearch
,"notice",notice_parse
, 0, "NOTICE users in newsearch result. Note: evaluation order");
238 /* Nick output filters */
239 regdisp(reg_nicksearch
,"default",printnick
, 0, "");
240 regdisp(reg_nicksearch
,"channels",printnick_channels
, 0, "include channels in output");
242 /* Channel output filters */
243 regdisp(reg_chansearch
,"default",printchannel
, 0, "");
244 regdisp(reg_chansearch
,"topic",printchannel_topic
, 0, "display channel topics");
245 regdisp(reg_chansearch
,"services",printchannel_services
, 0, "display services on channels");
247 /* Nick output filters */
248 regdisp(reg_usersearch
,"default",printuser
, 0, "");
253 searchList
*sl
, *psl
;
255 Command
*cmdlist
[100];
262 n
=getcommandlist(searchCmdTree
,cmdlist
,100);
264 deregistersearchterm( (searchCmd
*)cmdlist
[i
]->handler
, psl
->name
->content
, psl
->cmd
);
267 freesstring(psl
->name
);
273 deregistersearchcommand( reg_nicksearch
);
274 deregistersearchcommand( reg_chansearch
);
275 deregistersearchcommand( reg_usersearch
);
276 destroycommandtree( searchCmdTree
);
279 void registerglobalsearchterm(char *term
, parseFunc parsefunc
, char *help
) {
281 Command
*cmdlist
[100];
282 searchList
*sl
= malloc(sizeof(searchList
));
284 Error("newsearch", ERR_ERROR
, "malloc failed: registerglobalsearchterm");
289 sl
->name
= getsstring(term
, NSMAX_COMMAND_LEN
);
292 int len
=strlen(help
);
293 sl
->help
=(char *)malloc(len
+1);
295 Error("newsearch", ERR_ERROR
, "malloc failed: registerglobalsearchterm");
298 strncpy(sl
->help
, help
, len
);
299 sl
->help
[len
] = '\0';
305 if ( globalterms
!= NULL
) {
306 sl
->next
= globalterms
;
310 n
=getcommandlist(searchCmdTree
,cmdlist
,100);
312 /* maxparams is set to 1 to indicate a global term */
313 /* access level is set to 0 for all global terms */
314 addcommandexttotree( ((searchCmd
*)cmdlist
[i
]->handler
)->searchtree
,term
, 0, 1, (CommandHandler
) parsefunc
, help
);
318 void deregisterglobalsearchterm(char *term
, parseFunc parsefunc
) {
320 Command
*cmdlist
[100];
321 searchList
*sl
, *psl
=NULL
;
323 for (sl
=globalterms
; sl
; sl
=sl
->next
) {
324 if ( strcmp( sl
->name
->content
, term
) == 0 ) {
332 psl
->next
= sl
->next
;
335 n
=getcommandlist(searchCmdTree
,cmdlist
,100);
337 deletecommandfromtree( ((searchCmd
*)cmdlist
[i
]->handler
)->searchtree
, term
, (CommandHandler
) parsefunc
);
339 freesstring(sl
->name
);
344 void registersearchterm(searchCmd
*cmd
, char *term
, parseFunc parsefunc
, int level
, char *help
) {
345 /* NOTE: global terms are added to the tree elsewhere as we set maxparams to 1 to indicate global */
346 addcommandexttotree(cmd
->searchtree
, term
, level
, 0, (CommandHandler
) parsefunc
, help
);
349 void deregistersearchterm(searchCmd
*cmd
, char *term
, parseFunc parsefunc
) {
350 /* NOTE: global terms are removed from the tree within deregisterglobalsearchterm */
351 deletecommandfromtree(cmd
->searchtree
, term
, (CommandHandler
) parsefunc
);
354 static void controlwallwrapper(int level
, char *format
, ...) __attribute__ ((format (printf
, 2, 3)));
355 static void controlwallwrapper(int level
, char *format
, ...) {
359 va_start(ap
, format
);
360 vsnprintf(buf
, sizeof(buf
), format
, ap
);
361 controlwall(NO_OPER
, level
, "%s", buf
);
365 int parseopts(int cargc
, char **cargv
, int *arg
, int *limit
, void **subset
, void *display
, CommandTree
*sl
, replyFunc reply
, void *sender
) {
368 struct irc_in_addr sin
; unsigned char bits
;
370 if (*cargv
[0] == '-') {
374 for (ch
=cargv
[0]+1;*ch
;ch
++) {
378 reply(sender
,"Error: -l switch requires an argument (for help, see help <searchcmd>)");
381 *limit
=strtoul(cargv
[(*arg
)++],NULL
,10);
386 reply(sender
,"Error: -d switch requires an argument (for help, see help <searchcmd>)");
389 cmd
=findcommandintree(sl
, cargv
[*arg
],1);
391 reply(sender
,"Error: unknown output format %s (for help, see help <searchcmd>)",cargv
[*arg
]);
394 if ( !controlpermitted( cmd
->level
, sender
) ) {
395 reply(sender
,"Error: Access Denied for output format %s (for help, see help <searchcmd>)", cargv
[*arg
]);
398 *((void **)display
)=(void *)cmd
->handler
;
403 if (subset
== NULL
) {
404 reply(sender
,"Error: -s switch not supported for this search.");
408 reply(sender
,"Error: -s switch requires an argument (for help, see help <searchcmd>)");
411 if (ipmask_parse(cargv
[*arg
], &sin
, &bits
) == 0) {
412 reply(sender
, "Error: Invalid CIDR mask supplied (for help, see help <searchcmd>)");
415 *subset
= (void *)refnode(iptree
, &sin
, bits
);
420 reply(sender
,"Unrecognised flag -%c. (for help, see help <searchcmd>)",*ch
);
429 void newsearch_ctxinit(searchCtx
*ctx
, searchParseFunc searchfn
, replyFunc replyfn
, wallFunc wallfn
, void *arg
, searchCmd
*cmd
, nick
*np
, void *displayfn
, int limit
) {
430 memset(ctx
, 0, sizeof(searchCtx
));
432 ctx
->reply
= replyfn
;
434 ctx
->parser
= searchfn
;
436 ctx
->searchcmd
= cmd
;
439 ctx
->displayfn
= displayfn
;
442 int do_nicksearch_real(replyFunc reply
, wallFunc wall
, void *source
, int cargc
, char **cargv
) {
443 nick
*sender
= source
;
446 NickDisplayFunc display
=defaultnickfn
;
451 reply( sender
, "Usage: [flags] <criteria>");
452 reply( sender
, "For help, see help nicksearch");
456 ret
= parseopts(cargc
, cargv
, &arg
, &limit
, NULL
, (void *)&display
, reg_nicksearch
->outputtree
, reply
, sender
);
461 reply(sender
,"No search terms - aborting.");
466 rejoinline(cargv
[arg
],cargc
-arg
);
469 tree
= parse_string(reg_nicksearch
, cargv
[arg
]);
471 displaystrerror(reply
, sender
, cargv
[arg
]);
475 ast_nicksearch(tree
->root
, reply
, sender
, wall
, display
, NULL
, NULL
, limit
);
482 int do_nicksearch(void *source
, int cargc
, char **cargv
) {
483 return do_nicksearch_real(controlreply
, controlwallwrapper
, source
, cargc
, cargv
);
486 void nicksearch_exe(struct searchNode
*search
, searchCtx
*ctx
) {
489 unsigned int cmarker
;
490 unsigned int tchans
=0,uchans
=0;
492 nick
*np
, *sender
= ctx
->sender
;
493 senderNSExtern
= sender
;
494 NickDisplayFunc display
= ctx
->displayfn
;
495 int limit
= ctx
->limit
;
497 /* Get a marker value to mark "seen" channels for unique count */
498 cmarker
=nextchanmarker();
500 /* The top-level node needs to return a BOOL */
501 search
=coerceNode(ctx
, search
, RETURNTYPE_BOOL
);
503 for (i
=0;i
<NICKHASHSIZE
;i
++) {
504 for (np
=nicktable
[i
];np
;np
=np
->next
) {
505 if ((search
->exe
)(ctx
, search
, np
)) {
506 /* Add total channels */
507 tchans
+= np
->channels
->cursi
;
509 /* Check channels for uniqueness */
510 cs
=(channel
**)np
->channels
->content
;
511 for (j
=0;j
<np
->channels
->cursi
;j
++) {
512 if (cs
[j
]->index
->marker
!= cmarker
) {
513 cs
[j
]->index
->marker
=cmarker
;
519 display(ctx
, sender
, np
);
522 ctx
->reply(sender
, "--- More than %d matches, skipping the rest",limit
);
528 ctx
->reply(sender
,"--- End of list: %d matches; users were on %u channels (%u unique, %.1f average clones)",
529 matches
, tchans
, uchans
, (float)tchans
/uchans
);
532 int do_chansearch_real(replyFunc reply
, wallFunc wall
, void *source
, int cargc
, char **cargv
) {
533 nick
*sender
= source
;
536 ChanDisplayFunc display
=defaultchanfn
;
541 reply( sender
, "Usage: [flags] <criteria>");
542 reply( sender
, "For help, see help chansearch");
546 ret
= parseopts(cargc
, cargv
, &arg
, &limit
, NULL
, (void *)&display
, reg_chansearch
->outputtree
, reply
, sender
);
551 reply(sender
,"No search terms - aborting.");
556 rejoinline(cargv
[arg
],cargc
-arg
);
559 tree
= parse_string(reg_chansearch
, cargv
[arg
]);
561 displaystrerror(reply
, sender
, cargv
[arg
]);
565 ast_chansearch(tree
->root
, reply
, sender
, wall
, display
, NULL
, NULL
, limit
);
572 int do_chansearch(void *source
, int cargc
, char **cargv
) {
573 return do_chansearch_real(controlreply
, controlwallwrapper
, source
, cargc
, cargv
);
576 void chansearch_exe(struct searchNode
*search
, searchCtx
*ctx
) {
580 nick
*sender
= ctx
->sender
;
581 senderNSExtern
= sender
;
582 ChanDisplayFunc display
= ctx
->displayfn
;
583 int limit
= ctx
->limit
;
585 search
=coerceNode(ctx
, search
, RETURNTYPE_BOOL
);
587 for (i
=0;i
<CHANNELHASHSIZE
;i
++) {
588 for (cip
=chantable
[i
];cip
;cip
=cip
->next
) {
589 if ((search
->exe
)(ctx
, search
, cip
)) {
591 display(ctx
, sender
, cip
);
593 ctx
->reply(sender
, "--- More than %d matches, skipping the rest",limit
);
599 ctx
->reply(sender
,"--- End of list: %d matches", matches
);
602 int do_usersearch_real(replyFunc reply
, wallFunc wall
, void *source
, int cargc
, char **cargv
) {
603 nick
*sender
= source
;
606 UserDisplayFunc display
=defaultuserfn
;
611 reply( sender
, "Usage: [flags] <criteria>");
612 reply( sender
, "For help, see help usersearch");
616 ret
= parseopts(cargc
, cargv
, &arg
, &limit
, NULL
, (void *)&display
, reg_usersearch
->outputtree
, reply
, sender
);
621 reply(sender
,"No search terms - aborting.");
626 rejoinline(cargv
[arg
],cargc
-arg
);
629 tree
= parse_string(reg_usersearch
, cargv
[arg
]);
631 displaystrerror(reply
, sender
, cargv
[arg
]);
635 ast_usersearch(tree
->root
, reply
, sender
, wall
, display
, NULL
, NULL
, limit
);
642 int do_usersearch(void *source
, int cargc
, char **cargv
) {
643 return do_usersearch_real(controlreply
, controlwallwrapper
, source
, cargc
, cargv
);
646 void usersearch_exe(struct searchNode
*search
, searchCtx
*ctx
) {
650 nick
*sender
= ctx
->sender
;
651 int limit
= ctx
->limit
;
652 UserDisplayFunc display
= ctx
->displayfn
;
653 senderNSExtern
= sender
;
655 search
=coerceNode(ctx
, search
, RETURNTYPE_BOOL
);
657 for (i
=0;i
<AUTHNAMEHASHSIZE
;i
++) {
658 for (aup
=authnametable
[i
];aup
;aup
=aup
->next
) {
659 if ((search
->exe
)(ctx
, search
, aup
)) {
661 display(ctx
, sender
, aup
);
663 ctx
->reply(sender
, "--- More than %d matches, skipping the rest",limit
);
669 ctx
->reply(sender
,"--- End of list: %d matches", matches
);
672 /* Free a coerce node */
673 void free_coerce(searchCtx
*ctx
, struct searchNode
*thenode
) {
674 struct coercedata
*cd
=thenode
->localdata
;
676 cd
->child
->free(ctx
, cd
->child
);
677 free(thenode
->localdata
);
681 /* Free a coerce node with a stringbuf allocated */
682 void free_coercestring(searchCtx
*ctx
, struct searchNode
*thenode
) {
683 free(((struct coercedata
*)thenode
->localdata
)->u
.stringbuf
);
684 free_coerce(ctx
, thenode
);
687 /* exe_tostr_null: return the constant string */
688 void *exe_tostr_null(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
689 struct coercedata
*cd
=thenode
->localdata
;
691 return cd
->u
.stringbuf
;
694 /* exe_val_null: return the constant value */
695 void *exe_val_null(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
696 struct coercedata
*cd
=thenode
->localdata
;
698 return (void *)cd
->u
.val
;
701 /* Lots of very dull type conversion functions */
702 void *exe_inttostr(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
703 struct coercedata
*cd
=thenode
->localdata
;
705 sprintf(cd
->u
.stringbuf
, "%lu", (unsigned long)(cd
->child
->exe
)(ctx
, cd
->child
, theinput
));
707 return cd
->u
.stringbuf
;
710 void *exe_booltostr(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
711 struct coercedata
*cd
=thenode
->localdata
;
713 if ((cd
->child
->exe
)(ctx
, cd
->child
, theinput
)) {
714 sprintf(cd
->u
.stringbuf
,"1");
716 cd
->u
.stringbuf
[0]='\0';
719 return cd
->u
.stringbuf
;
722 void *exe_strtoint(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
723 struct coercedata
*cd
=thenode
->localdata
;
725 return (void *)strtoul((cd
->child
->exe
)(ctx
,cd
->child
,theinput
),NULL
,10);
728 void *exe_booltoint(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
729 struct coercedata
*cd
=thenode
->localdata
;
731 /* Don't need to do anything */
732 return (cd
->child
->exe
)(ctx
, cd
->child
, theinput
);
735 void *exe_strtobool(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
736 struct coercedata
*cd
=thenode
->localdata
;
737 char *ch
=(cd
->child
->exe
)(ctx
, cd
->child
, theinput
);
739 if (!ch
|| *ch
=='\0' || (*ch
=='0' && ch
[1]=='\0')) {
746 void *exe_inttobool(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
747 struct coercedata
*cd
=thenode
->localdata
;
749 if ((cd
->child
->exe
)(ctx
, cd
->child
, theinput
)) {
756 struct searchNode
*coerceNode(searchCtx
*ctx
, struct searchNode
*thenode
, int type
) {
757 struct searchNode
*anode
;
758 struct coercedata
*cd
;
760 /* You can't coerce a NULL */
764 /* No effort required to coerce to the same type */
765 if (type
==(thenode
->returntype
& RETURNTYPE_TYPE
))
768 anode
=(struct searchNode
*)malloc(sizeof(struct searchNode
));
769 anode
->localdata
=cd
=(struct coercedata
*)malloc(sizeof(struct coercedata
));
771 anode
->returntype
=type
; /* We'll return what they want, always */
772 anode
->free
=free_coerce
;
775 case RETURNTYPE_STRING
:
776 /* For a string we'll need a buffer */
777 /* A 64-bit number prints out to 20 digits, this leaves some slack */
778 cd
->u
.stringbuf
=malloc(25);
779 anode
->free
=free_coercestring
;
781 switch(thenode
->returntype
& RETURNTYPE_TYPE
) {
784 if (thenode
->returntype
& RETURNTYPE_CONST
) {
785 /* Constant node: sort it out now */
786 sprintf(cd
->u
.stringbuf
, "%lu", (unsigned long)thenode
->exe(ctx
, thenode
, NULL
));
787 anode
->exe
=exe_tostr_null
;
788 anode
->returntype
|= RETURNTYPE_CONST
;
791 anode
->exe
=exe_inttostr
;
795 case RETURNTYPE_BOOL
:
796 if (thenode
->returntype
& RETURNTYPE_CONST
) {
797 /* Constant bool value */
798 if (thenode
->exe(ctx
, thenode
,NULL
)) {
800 sprintf(cd
->u
.stringbuf
, "1");
802 cd
->u
.stringbuf
[0] = '\0';
804 anode
->exe
=exe_tostr_null
;
805 anode
->returntype
|= RETURNTYPE_CONST
;
807 /* Variable bool value */
808 anode
->exe
=exe_booltostr
;
816 switch (thenode
->returntype
& RETURNTYPE_TYPE
) {
817 case RETURNTYPE_STRING
:
818 if (thenode
->returntype
& RETURNTYPE_CONST
) {
819 cd
->u
.val
=strtoul((thenode
->exe
)(ctx
, thenode
, NULL
), NULL
, 10);
820 anode
->exe
=exe_val_null
;
821 anode
->returntype
|= RETURNTYPE_CONST
;
823 anode
->exe
=exe_strtoint
;
828 case RETURNTYPE_BOOL
:
829 if (thenode
->returntype
& RETURNTYPE_CONST
) {
830 if ((thenode
->exe
)(ctx
, thenode
,NULL
))
835 anode
->exe
=exe_val_null
;
836 anode
->returntype
|= RETURNTYPE_CONST
;
838 anode
->exe
=exe_booltoint
;
845 case RETURNTYPE_BOOL
:
847 switch (thenode
->returntype
& RETURNTYPE_TYPE
) {
848 case RETURNTYPE_STRING
:
849 if (thenode
->returntype
& RETURNTYPE_CONST
) {
850 char *rv
=(char *)((thenode
->exe
)(ctx
, thenode
, NULL
));
851 if (!rv
|| *rv
=='\0' || (*rv
=='0' && rv
[1]=='\0'))
856 anode
->exe
=exe_val_null
;
857 anode
->returntype
|= RETURNTYPE_CONST
;
859 anode
->exe
=exe_strtobool
;
865 if (thenode
->returntype
& RETURNTYPE_CONST
) {
866 if ((thenode
->exe
)(ctx
, thenode
,NULL
))
871 anode
->exe
=exe_val_null
;
872 anode
->returntype
|= RETURNTYPE_CONST
;
874 anode
->exe
=exe_inttobool
;
884 /* Literals always return constant strings... */
885 void *literal_exe(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
886 if (thenode
->localdata
)
887 return ((sstring
*)thenode
->localdata
)->content
;
892 void literal_free(searchCtx
*ctx
, struct searchNode
*thenode
) {
893 freesstring(thenode
->localdata
);
897 void nssnprintf(char *buf
, size_t size
, const char *format
, nick
*np
) {
910 for(p
=format
;*p
;p
++) {
912 if(!sbaddchar(&b
, *p
))
920 if(!sbaddchar(&b
, *p
))
930 c
= np
->ident
; break;
932 c
= np
->host
->name
->content
; break;
934 snprintf(hostbuf
, sizeof(hostbuf
), "%s", IPtostr(np
->p_ipaddr
));
938 snprintf(hostbuf
, sizeof(hostbuf
), "%s!%s@%s", np
->nick
, np
->ident
, IPtostr(np
->p_ipaddr
));
942 c
= "(bad format specifier)";
957 static char *var_tochar(searchCtx
*ctx
, char *arg
, searchNode
**variable
) {
958 *variable
= ctx
->parser(ctx
, arg
);
959 if (!(*variable
= coerceNode(ctx
, *variable
, RETURNTYPE_STRING
)))
962 if(!((*variable
)->returntype
& RETURNTYPE_CONST
)) {
963 parseError
= "only constant variables allowed";
964 ((*variable
)->free
)(ctx
, *variable
);
968 return (char *)((*variable
)->exe
)(ctx
, *variable
, NULL
);
971 void free_val_null(searchCtx
*ctx
, struct searchNode
*thenode
) {
974 struct searchVariable
*var_register(searchCtx
*ctx
, char *arg
, int type
) {
975 searchNode
*variable
;
976 struct searchVariable
*us
;
980 if(ctx
->lastvar
>= MAX_VARIABLES
) {
981 parseError
= "Maximum number of variables reached";
985 us
= &ctx
->vars
[ctx
->lastvar
];
987 var
= var_tochar(ctx
, arg
, &variable
);
991 strlcpy(us
->name
, var
, sizeof(us
->name
));
992 (variable
->free
)(ctx
, variable
);
994 for(i
=0;i
<ctx
->lastvar
;i
++) {
995 if(!strcmp(us
->name
, ctx
->vars
[i
].name
)) {
996 parseError
= "variable name already in use";
1002 us
->data
.returntype
= type
;
1003 us
->data
.localdata
= &us
->cdata
;
1004 us
->data
.exe
= exe_val_null
;
1005 us
->data
.free
= free_val_null
;
1007 us
->cdata
.child
= NULL
;
1011 searchNode
*var_get(searchCtx
*ctx
, char *arg
) {
1012 searchNode
*variable
, *found
= NULL
;
1014 char *var
= var_tochar(ctx
, arg
, &variable
);
1018 for(i
=0;i
<ctx
->lastvar
;i
++) {
1019 if(!strcmp(var
, ctx
->vars
[i
].name
)) {
1020 found
= &ctx
->vars
[i
].data
;
1024 (variable
->free
)(ctx
, variable
);
1027 parseError
= "variable not found";
1031 void var_setstr(struct searchVariable
*v
, char *data
) {
1032 v
->cdata
.u
.stringbuf
= data
;
1035 void displaystrerror(replyFunc reply
, nick
*np
, const char *input
) {
1038 if((parseStrErrorPos
>= 0) && (parseStrErrorPos
< sizeof(buf
) - 3)) {
1041 for(i
=0;i
<parseStrErrorPos
;i
++)
1047 reply(np
, "%s", input
);
1048 reply(np
, "%s", buf
);
1051 reply(np
, "Parse error: %s", parseStrError
);
1054 struct searchNode
*argtoconststr(char *command
, searchCtx
*ctx
, char *arg
, char **p
) {
1055 struct searchNode
*c
;
1056 static char errorbuf
[512];
1058 c
= ctx
->parser(ctx
, arg
);
1059 if (!(c
= coerceNode(ctx
, c
, RETURNTYPE_STRING
))) {
1060 snprintf(errorbuf
, sizeof(errorbuf
), "%s: unable to coerce argument to string", command
);
1061 parseError
= errorbuf
;
1065 if (!(c
->returntype
& RETURNTYPE_CONST
)) {
1066 snprintf(errorbuf
, sizeof(errorbuf
), "%s: constant argument required", command
);
1067 parseError
= errorbuf
;
1073 *p
= (char *)(c
->exe
)(ctx
, c
, NULL
);