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
, "authname",authname_parse
, 0, "User's current authname or false"); /* nick only */
182 registersearchterm(reg_nicksearch
, "authts",authts_parse
, 0, "User's Auth timestamp"); /* nick only */
183 registersearchterm(reg_nicksearch
, "ident",ident_parse
, 0, "User's current ident"); /* nick only */
184 registersearchterm(reg_nicksearch
, "host",host_parse
, 0, "User's host, allow \"host real\" to match real host"); /* nick only */
185 registersearchterm(reg_nicksearch
, "channel",channel_parse
, 0, "Valid Channel Name to match users against"); /* nick only */
186 registersearchterm(reg_nicksearch
, "timestamp",timestamp_parse
, 0, "User's Timestamp"); /* nick only */
187 registersearchterm(reg_nicksearch
, "country",country_parse
, 0, "2 letter country code (data source is geoip)"); /* nick only */
188 registersearchterm(reg_nicksearch
, "ip",ip_parse
, 0, "User's IP - ipv4 or ipv6 format as appropriate. Note: not 6to4"); /* nick only */
189 registersearchterm(reg_nicksearch
, "channels",channels_parse
, 0, "Channel Count"); /* nick only */
190 registersearchterm(reg_nicksearch
, "server",server_parse
, 0, "Server Name. Either (server string) or (match (server) string)"); /* nick only */
191 registersearchterm(reg_nicksearch
, "authid",authid_parse
, 0, "User's Auth ID"); /* nick only */
192 registersearchterm(reg_nicksearch
, "cidr",cidr_parse
, 0, "CIDR matching"); /* nick only */
194 /* Channel operations */
195 registersearchterm(reg_chansearch
, "exists",exists_parse
, 0, "Returns if channel exists on network. Note: newserv may store data on empty channels"); /* channel only */
196 registersearchterm(reg_chansearch
, "services",services_parse
, 0, ""); /* channel only */
197 registersearchterm(reg_chansearch
, "size",size_parse
, 0, "Channel user count"); /* channel only */
198 registersearchterm(reg_chansearch
, "name",name_parse
, 0, "Channel Name"); /* channel only */
199 registersearchterm(reg_chansearch
, "topic",topic_parse
, 0, "Channel topic"); /* channel only */
200 registersearchterm(reg_chansearch
, "oppct",oppct_parse
, 0, "Percentage Opped"); /* channel only */
201 registersearchterm(reg_chansearch
, "cumodecount",cumodecount_parse
, 0, "Count of users with given channel modes"); /* channel only */
202 registersearchterm(reg_chansearch
, "cumodepct",cumodepct_parse
, 0, "Percentage of users with given channel modes"); /* channel only */
203 registersearchterm(reg_chansearch
, "uniquehostpct",hostpct_parse
, 0, "uniquehost percent"); /* channel only */
204 registersearchterm(reg_chansearch
, "authedpct",authedpct_parse
, 0, "Percentage of authed users"); /* channel only */
205 registersearchterm(reg_chansearch
, "kick",kick_parse
, 0, "KICK users channels in newsearch result. Note: evaluation order"); /* channel only */
207 /* Nickname / channel operations */
208 registersearchterm(reg_chansearch
, "modes",modes_parse
, 0, "User Modes");
209 registersearchterm(reg_nicksearch
, "modes",modes_parse
, 0, "Channel Modes");
210 registersearchterm(reg_chansearch
, "nick",nick_parse
, 0, "Nickname");
211 registersearchterm(reg_nicksearch
, "nick",nick_parse
, 0, "Nickname");
213 /* Kill / gline parameters */
214 registersearchterm(reg_chansearch
,"kill",kill_parse
, 0, "KILL users in newsearch result. Note: evaluation order");
215 registersearchterm(reg_chansearch
,"gline",gline_parse
, 0, "GLINE users in newsearch result. Note: evaluation order");
216 registersearchterm(reg_nicksearch
,"kill",kill_parse
, 0, "KILL users in newsearch result. Note: evaluation order");
217 registersearchterm(reg_nicksearch
,"gline",gline_parse
, 0, "GLINE users in newsearch result. Note: evaluation order");
219 /* Iteration functionality */
220 registerglobalsearchterm("any",any_parse
, "usage: any (generatorfn x) (fn ... (var x) ...)");
221 registerglobalsearchterm("all",all_parse
, "usage: all (generatorfn x) (fn ... (var x) ...)");
222 registerglobalsearchterm("var",var_parse
, "usage: var variable");
224 /* Iterable functions */
225 registersearchterm(reg_nicksearch
, "channeliter",channeliter_parse
, 0, "Channel Iterable function - usage: (any (channeliter x) (match (var x) #twilight*))"); /* nick only */
226 registersearchterm(reg_chansearch
, "nickiter",nickiter_parse
, 0, "Nick Iterable function - usage: (any (nickiter x) (match (var x) nickname*))"); /* channel only */
228 /* Functions that work on strings?! */
229 registersearchterm(reg_nicksearch
, "cumodes", cumodes_parse
, 0, "usage: (cumodes (var x) <modes>)");
230 registersearchterm(reg_chansearch
, "cumodes", cumodes_parse
, 0, "usage: (cumodes (var x) <modes>)");
232 /* Notice functionality */
233 registersearchterm(reg_chansearch
,"notice",notice_parse
, 0, "NOTICE users in newsearch result. Note: evaluation order");
234 registersearchterm(reg_nicksearch
,"notice",notice_parse
, 0, "NOTICE users in newsearch result. Note: evaluation order");
236 /* Nick output filters */
237 regdisp(reg_nicksearch
,"default",printnick
, 0, "");
238 regdisp(reg_nicksearch
,"channels",printnick_channels
, 0, "include channels in output");
240 /* Channel output filters */
241 regdisp(reg_chansearch
,"default",printchannel
, 0, "");
242 regdisp(reg_chansearch
,"topic",printchannel_topic
, 0, "display channel topics");
243 regdisp(reg_chansearch
,"services",printchannel_services
, 0, "display services on channels");
245 /* Nick output filters */
246 regdisp(reg_usersearch
,"default",printuser
, 0, "");
251 searchList
*sl
, *psl
;
253 Command
*cmdlist
[100];
260 n
=getcommandlist(searchCmdTree
,cmdlist
,100);
262 deregistersearchterm( (searchCmd
*)cmdlist
[i
]->handler
, psl
->name
->content
, psl
->cmd
);
265 freesstring(psl
->name
);
271 deregistersearchcommand( reg_nicksearch
);
272 deregistersearchcommand( reg_chansearch
);
273 deregistersearchcommand( reg_usersearch
);
274 destroycommandtree( searchCmdTree
);
277 void registerglobalsearchterm(char *term
, parseFunc parsefunc
, char *help
) {
279 Command
*cmdlist
[100];
280 searchList
*sl
= malloc(sizeof(searchList
));
282 Error("newsearch", ERR_ERROR
, "malloc failed: registerglobalsearchterm");
287 sl
->name
= getsstring(term
, NSMAX_COMMAND_LEN
);
290 int len
=strlen(help
);
291 sl
->help
=(char *)malloc(len
+1);
293 Error("newsearch", ERR_ERROR
, "malloc failed: registerglobalsearchterm");
296 strncpy(sl
->help
, help
, len
);
297 sl
->help
[len
] = '\0';
303 if ( globalterms
!= NULL
) {
304 sl
->next
= globalterms
;
308 n
=getcommandlist(searchCmdTree
,cmdlist
,100);
310 /* maxparams is set to 1 to indicate a global term */
311 /* access level is set to 0 for all global terms */
312 addcommandexttotree( ((searchCmd
*)cmdlist
[i
]->handler
)->searchtree
,term
, 0, 1, (CommandHandler
) parsefunc
, help
);
316 void deregisterglobalsearchterm(char *term
, parseFunc parsefunc
) {
318 Command
*cmdlist
[100];
319 searchList
*sl
, *psl
=NULL
;
321 for (sl
=globalterms
; sl
; sl
=sl
->next
) {
322 if ( strcmp( sl
->name
->content
, term
) == 0 ) {
330 psl
->next
= sl
->next
;
333 n
=getcommandlist(searchCmdTree
,cmdlist
,100);
335 deletecommandfromtree( ((searchCmd
*)cmdlist
[i
]->handler
)->searchtree
, term
, (CommandHandler
) parsefunc
);
337 freesstring(sl
->name
);
342 void registersearchterm(searchCmd
*cmd
, char *term
, parseFunc parsefunc
, int level
, char *help
) {
343 /* NOTE: global terms are added to the tree elsewhere as we set maxparams to 1 to indicate global */
344 addcommandexttotree(cmd
->searchtree
, term
, level
, 0, (CommandHandler
) parsefunc
, help
);
347 void deregistersearchterm(searchCmd
*cmd
, char *term
, parseFunc parsefunc
) {
348 /* NOTE: global terms are removed from the tree within deregisterglobalsearchterm */
349 deletecommandfromtree(cmd
->searchtree
, term
, (CommandHandler
) parsefunc
);
352 static void controlwallwrapper(int level
, char *format
, ...) __attribute__ ((format (printf
, 2, 3)));
353 static void controlwallwrapper(int level
, char *format
, ...) {
357 va_start(ap
, format
);
358 vsnprintf(buf
, sizeof(buf
), format
, ap
);
359 controlwall(NO_OPER
, level
, "%s", buf
);
363 int parseopts(int cargc
, char **cargv
, int *arg
, int *limit
, void **subset
, void *display
, CommandTree
*sl
, replyFunc reply
, void *sender
) {
366 struct irc_in_addr sin
; unsigned char bits
;
368 if (*cargv
[0] == '-') {
372 for (ch
=cargv
[0]+1;*ch
;ch
++) {
376 reply(sender
,"Error: -l switch requires an argument (for help, see help <searchcmd>)");
379 *limit
=strtoul(cargv
[(*arg
)++],NULL
,10);
384 reply(sender
,"Error: -d switch requires an argument (for help, see help <searchcmd>)");
387 cmd
=findcommandintree(sl
, cargv
[*arg
],1);
389 reply(sender
,"Error: unknown output format %s (for help, see help <searchcmd>)",cargv
[*arg
]);
392 if ( !controlpermitted( cmd
->level
, sender
) ) {
393 reply(sender
,"Error: Access Denied for output format %s (for help, see help <searchcmd>)", cargv
[*arg
]);
396 *((void **)display
)=(void *)cmd
->handler
;
402 reply(sender
,"Error: -s switch requires an argument (for help, see help <searchcmd>)");
405 if (ipmask_parse(cargv
[*arg
], &sin
, &bits
) == 0) {
406 reply(sender
, "Error: Invalid CIDR mask supplied (for help, see help <searchcmd>)");
409 *subset
= (void *)refnode(iptree
, &sin
, bits
);
414 reply(sender
,"Unrecognised flag -%c. (for help, see help <searchcmd>)",*ch
);
423 void newsearch_ctxinit(searchCtx
*ctx
, searchParseFunc searchfn
, replyFunc replyfn
, wallFunc wallfn
, void *arg
, searchCmd
*cmd
, nick
*np
, void *displayfn
, int limit
) {
424 memset(ctx
, 0, sizeof(searchCtx
));
426 ctx
->reply
= replyfn
;
428 ctx
->parser
= searchfn
;
430 ctx
->searchcmd
= cmd
;
433 ctx
->displayfn
= displayfn
;
436 int do_nicksearch_real(replyFunc reply
, wallFunc wall
, void *source
, int cargc
, char **cargv
) {
437 nick
*sender
= source
;
440 NickDisplayFunc display
=defaultnickfn
;
442 #ifndef NEWSEARCH_NEWPARSER
444 struct searchNode
*search
;
450 reply( sender
, "Usage: [flags] <criteria>");
451 reply( sender
, "For help, see help nicksearch");
455 ret
= parseopts(cargc
, cargv
, &arg
, &limit
, NULL
, (void *)&display
, reg_nicksearch
->outputtree
, reply
, sender
);
460 reply(sender
,"No search terms - aborting.");
465 rejoinline(cargv
[arg
],cargc
-arg
);
468 #ifndef NEWSEARCH_NEWPARSER
469 newsearch_ctxinit(&ctx
, search_parse
, reply
, wall
, NULL
, reg_nicksearch
, sender
, display
, limit
);
470 if (!(search
= ctx
.parser(&ctx
, cargv
[arg
]))) {
471 reply(sender
,"Parse error: %s",parseError
);
475 nicksearch_exe(search
, &ctx
);
477 (search
->free
)(&ctx
, search
);
479 tree
= parse_string(reg_nicksearch
, cargv
[arg
]);
481 displaystrerror(reply
, sender
, cargv
[arg
]);
485 ast_nicksearch(tree
->root
, reply
, sender
, wall
, display
, NULL
, NULL
, limit
);
493 int do_nicksearch(void *source
, int cargc
, char **cargv
) {
494 return do_nicksearch_real(controlreply
, controlwallwrapper
, source
, cargc
, cargv
);
497 void nicksearch_exe(struct searchNode
*search
, searchCtx
*ctx
) {
500 unsigned int cmarker
;
501 unsigned int tchans
=0,uchans
=0;
503 nick
*np
, *sender
= ctx
->sender
;
504 senderNSExtern
= sender
;
505 NickDisplayFunc display
= ctx
->displayfn
;
506 int limit
= ctx
->limit
;
508 /* Get a marker value to mark "seen" channels for unique count */
509 cmarker
=nextchanmarker();
511 /* The top-level node needs to return a BOOL */
512 search
=coerceNode(ctx
, search
, RETURNTYPE_BOOL
);
514 for (i
=0;i
<NICKHASHSIZE
;i
++) {
515 for (np
=nicktable
[i
];np
;np
=np
->next
) {
516 if ((search
->exe
)(ctx
, search
, np
)) {
517 /* Add total channels */
518 tchans
+= np
->channels
->cursi
;
520 /* Check channels for uniqueness */
521 cs
=(channel
**)np
->channels
->content
;
522 for (j
=0;j
<np
->channels
->cursi
;j
++) {
523 if (cs
[j
]->index
->marker
!= cmarker
) {
524 cs
[j
]->index
->marker
=cmarker
;
530 display(ctx
, sender
, np
);
533 ctx
->reply(sender
, "--- More than %d matches, skipping the rest",limit
);
539 ctx
->reply(sender
,"--- End of list: %d matches; users were on %u channels (%u unique, %.1f average clones)",
540 matches
, tchans
, uchans
, (float)tchans
/uchans
);
543 int do_chansearch_real(replyFunc reply
, wallFunc wall
, void *source
, int cargc
, char **cargv
) {
544 nick
*sender
= source
;
547 ChanDisplayFunc display
=defaultchanfn
;
549 #ifndef NEWSEARCH_NEWPARSER
550 struct searchNode
*search
;
557 reply( sender
, "Usage: [flags] <criteria>");
558 reply( sender
, "For help, see help chansearch");
562 ret
= parseopts(cargc
, cargv
, &arg
, &limit
, NULL
, (void *)&display
, reg_chansearch
->outputtree
, reply
, sender
);
567 reply(sender
,"No search terms - aborting.");
572 rejoinline(cargv
[arg
],cargc
-arg
);
575 #ifndef NEWSEARCH_NEWPARSER
576 newsearch_ctxinit(&ctx
, search_parse
, reply
, wall
, NULL
, reg_chansearch
, sender
, display
, limit
);
577 if (!(search
= ctx
.parser(&ctx
, cargv
[arg
]))) {
578 reply(sender
,"Parse error: %s",parseError
);
582 chansearch_exe(search
, &ctx
);
584 (search
->free
)(&ctx
, search
);
586 tree
= parse_string(reg_chansearch
, cargv
[arg
]);
588 displaystrerror(reply
, sender
, cargv
[arg
]);
592 ast_chansearch(tree
->root
, reply
, sender
, wall
, display
, NULL
, NULL
, limit
);
600 int do_chansearch(void *source
, int cargc
, char **cargv
) {
601 return do_chansearch_real(controlreply
, controlwallwrapper
, source
, cargc
, cargv
);
604 void chansearch_exe(struct searchNode
*search
, searchCtx
*ctx
) {
608 nick
*sender
= ctx
->sender
;
609 senderNSExtern
= sender
;
610 ChanDisplayFunc display
= ctx
->displayfn
;
611 int limit
= ctx
->limit
;
613 search
=coerceNode(ctx
, search
, RETURNTYPE_BOOL
);
615 for (i
=0;i
<CHANNELHASHSIZE
;i
++) {
616 for (cip
=chantable
[i
];cip
;cip
=cip
->next
) {
617 if ((search
->exe
)(ctx
, search
, cip
)) {
619 display(ctx
, sender
, cip
);
621 ctx
->reply(sender
, "--- More than %d matches, skipping the rest",limit
);
627 ctx
->reply(sender
,"--- End of list: %d matches", matches
);
630 int do_usersearch_real(replyFunc reply
, wallFunc wall
, void *source
, int cargc
, char **cargv
) {
631 nick
*sender
= source
;
634 UserDisplayFunc display
=defaultuserfn
;
636 #ifndef NEWSEARCH_NEWPARSER
637 struct searchNode
*search
;
644 reply( sender
, "Usage: [flags] <criteria>");
645 reply( sender
, "For help, see help usersearch");
649 ret
= parseopts(cargc
, cargv
, &arg
, &limit
, NULL
, (void *)&display
, reg_usersearch
->outputtree
, reply
, sender
);
654 reply(sender
,"No search terms - aborting.");
659 rejoinline(cargv
[arg
],cargc
-arg
);
662 #ifndef NEWSEARCH_NEWPARSER
663 newsearch_ctxinit(&ctx
, search_parse
, reply
, wall
, NULL
, reg_usersearch
, sender
, display
, limit
);
665 if (!(search
= ctx
.parser(&ctx
, cargv
[arg
]))) {
666 reply(sender
,"Parse error: %s",parseError
);
670 usersearch_exe(search
, &ctx
);
672 (search
->free
)(&ctx
, search
);
674 tree
= parse_string(reg_usersearch
, cargv
[arg
]);
676 displaystrerror(reply
, sender
, cargv
[arg
]);
680 ast_usersearch(tree
->root
, reply
, sender
, wall
, display
, NULL
, NULL
, limit
);
688 int do_usersearch(void *source
, int cargc
, char **cargv
) {
689 return do_usersearch_real(controlreply
, controlwallwrapper
, source
, cargc
, cargv
);
692 void usersearch_exe(struct searchNode
*search
, searchCtx
*ctx
) {
696 nick
*sender
= ctx
->sender
;
697 int limit
= ctx
->limit
;
698 UserDisplayFunc display
= ctx
->displayfn
;
699 senderNSExtern
= sender
;
701 search
=coerceNode(ctx
, search
, RETURNTYPE_BOOL
);
703 for (i
=0;i
<AUTHNAMEHASHSIZE
;i
++) {
704 for (aup
=authnametable
[i
];aup
;aup
=aup
->next
) {
705 if ((search
->exe
)(ctx
, search
, aup
)) {
707 display(ctx
, sender
, aup
);
709 ctx
->reply(sender
, "--- More than %d matches, skipping the rest",limit
);
715 ctx
->reply(sender
,"--- End of list: %d matches", matches
);
718 /* Free a coerce node */
719 void free_coerce(searchCtx
*ctx
, struct searchNode
*thenode
) {
720 struct coercedata
*cd
=thenode
->localdata
;
722 cd
->child
->free(ctx
, cd
->child
);
723 free(thenode
->localdata
);
727 /* Free a coerce node with a stringbuf allocated */
728 void free_coercestring(searchCtx
*ctx
, struct searchNode
*thenode
) {
729 free(((struct coercedata
*)thenode
->localdata
)->u
.stringbuf
);
730 free_coerce(ctx
, thenode
);
733 /* exe_tostr_null: return the constant string */
734 void *exe_tostr_null(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
735 struct coercedata
*cd
=thenode
->localdata
;
737 return cd
->u
.stringbuf
;
740 /* exe_val_null: return the constant value */
741 void *exe_val_null(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
742 struct coercedata
*cd
=thenode
->localdata
;
744 return (void *)cd
->u
.val
;
747 /* Lots of very dull type conversion functions */
748 void *exe_inttostr(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
749 struct coercedata
*cd
=thenode
->localdata
;
751 sprintf(cd
->u
.stringbuf
, "%lu", (unsigned long)(cd
->child
->exe
)(ctx
, cd
->child
, theinput
));
753 return cd
->u
.stringbuf
;
756 void *exe_booltostr(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
757 struct coercedata
*cd
=thenode
->localdata
;
759 if ((cd
->child
->exe
)(ctx
, cd
->child
, theinput
)) {
760 sprintf(cd
->u
.stringbuf
,"1");
762 cd
->u
.stringbuf
[0]='\0';
765 return cd
->u
.stringbuf
;
768 void *exe_strtoint(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
769 struct coercedata
*cd
=thenode
->localdata
;
771 return (void *)strtoul((cd
->child
->exe
)(ctx
,cd
->child
,theinput
),NULL
,10);
774 void *exe_booltoint(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
775 struct coercedata
*cd
=thenode
->localdata
;
777 /* Don't need to do anything */
778 return (cd
->child
->exe
)(ctx
, cd
->child
, theinput
);
781 void *exe_strtobool(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
782 struct coercedata
*cd
=thenode
->localdata
;
783 char *ch
=(cd
->child
->exe
)(ctx
, cd
->child
, theinput
);
785 if (!ch
|| *ch
=='\0' || (*ch
=='0' && ch
[1]=='\0')) {
792 void *exe_inttobool(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
793 struct coercedata
*cd
=thenode
->localdata
;
795 if ((cd
->child
->exe
)(ctx
, cd
->child
, theinput
)) {
802 struct searchNode
*coerceNode(searchCtx
*ctx
, struct searchNode
*thenode
, int type
) {
803 struct searchNode
*anode
;
804 struct coercedata
*cd
;
806 /* You can't coerce a NULL */
810 /* No effort required to coerce to the same type */
811 if (type
==(thenode
->returntype
& RETURNTYPE_TYPE
))
814 anode
=(struct searchNode
*)malloc(sizeof(struct searchNode
));
815 anode
->localdata
=cd
=(struct coercedata
*)malloc(sizeof(struct coercedata
));
817 anode
->returntype
=type
; /* We'll return what they want, always */
818 anode
->free
=free_coerce
;
821 case RETURNTYPE_STRING
:
822 /* For a string we'll need a buffer */
823 /* A 64-bit number prints out to 20 digits, this leaves some slack */
824 cd
->u
.stringbuf
=malloc(25);
825 anode
->free
=free_coercestring
;
827 switch(thenode
->returntype
& RETURNTYPE_TYPE
) {
830 if (thenode
->returntype
& RETURNTYPE_CONST
) {
831 /* Constant node: sort it out now */
832 sprintf(cd
->u
.stringbuf
, "%lu", (unsigned long)thenode
->exe(ctx
, thenode
, NULL
));
833 anode
->exe
=exe_tostr_null
;
834 anode
->returntype
|= RETURNTYPE_CONST
;
837 anode
->exe
=exe_inttostr
;
841 case RETURNTYPE_BOOL
:
842 if (thenode
->returntype
& RETURNTYPE_CONST
) {
843 /* Constant bool value */
844 if (thenode
->exe(ctx
, thenode
,NULL
)) {
846 sprintf(cd
->u
.stringbuf
, "1");
848 cd
->u
.stringbuf
[0] = '\0';
850 anode
->exe
=exe_tostr_null
;
851 anode
->returntype
|= RETURNTYPE_CONST
;
853 /* Variable bool value */
854 anode
->exe
=exe_booltostr
;
862 switch (thenode
->returntype
& RETURNTYPE_TYPE
) {
863 case RETURNTYPE_STRING
:
864 if (thenode
->returntype
& RETURNTYPE_CONST
) {
865 cd
->u
.val
=strtoul((thenode
->exe
)(ctx
, thenode
, NULL
), NULL
, 10);
866 anode
->exe
=exe_val_null
;
867 anode
->returntype
|= RETURNTYPE_CONST
;
869 anode
->exe
=exe_strtoint
;
874 case RETURNTYPE_BOOL
:
875 if (thenode
->returntype
& RETURNTYPE_CONST
) {
876 if ((thenode
->exe
)(ctx
, thenode
,NULL
))
881 anode
->exe
=exe_val_null
;
882 anode
->returntype
|= RETURNTYPE_CONST
;
884 anode
->exe
=exe_booltoint
;
891 case RETURNTYPE_BOOL
:
893 switch (thenode
->returntype
& RETURNTYPE_TYPE
) {
894 case RETURNTYPE_STRING
:
895 if (thenode
->returntype
& RETURNTYPE_CONST
) {
896 char *rv
=(char *)((thenode
->exe
)(ctx
, thenode
, NULL
));
897 if (!rv
|| *rv
=='\0' || (*rv
=='0' && rv
[1]=='\0'))
902 anode
->exe
=exe_val_null
;
903 anode
->returntype
|= RETURNTYPE_CONST
;
905 anode
->exe
=exe_strtobool
;
911 if (thenode
->returntype
& RETURNTYPE_CONST
) {
912 if ((thenode
->exe
)(ctx
, thenode
,NULL
))
917 anode
->exe
=exe_val_null
;
918 anode
->returntype
|= RETURNTYPE_CONST
;
920 anode
->exe
=exe_inttobool
;
930 /* Literals always return constant strings... */
931 void *literal_exe(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
932 if (thenode
->localdata
)
933 return ((sstring
*)thenode
->localdata
)->content
;
938 void literal_free(searchCtx
*ctx
, struct searchNode
*thenode
) {
939 freesstring(thenode
->localdata
);
943 static int unescape(char *input
, char *output
, size_t buflen
) {
948 for (ch
=input
;*ch
;ch
++) {
949 if(ch
- input
>= buflen
) {
950 parseError
="Buffer overflow";
955 if (*(ch
-1) != '\"') {
956 parseError
="Quote mismatch";
965 for (ch
=input
;*ch
;ch
++) {
966 if(ch
- input
>= buflen
) {
967 parseError
="Buffer overflow";
974 } else if (*ch
=='\\') {
985 struct searchNode
*search_parse(searchCtx
*ctx
, char *cinput
) {
986 /* OK, we need to split the input into chunks on spaces and brackets.. */
987 char *argvector
[100];
994 struct searchNode
*thenode
;
996 strlcpy(inputb
, cinput
, sizeof(inputb
));
999 /* If it starts with a bracket, it's a function call.. */
1001 /* Skip past string */
1002 for (ch
=input
;*ch
;ch
++);
1003 if (*(ch
-1) != ')') {
1004 parseError
= "Bracket mismatch!";
1010 /* Split further args */
1011 i
=-1; /* i = -1 BoW, 0 = inword, 1 = bracket nest depth */
1012 j
=0; /* j = current arg */
1016 for (ch
=input
;*ch
;ch
++) {
1017 /*printf("i: %d j: %d e: %d q: %d ch: '%c'\n", i, j, e, q, *ch);*/
1022 } else if (*ch
!= ' ') {
1026 } else if (*ch
=='\"') {
1035 } else if (*ch
=='\"') {
1041 } else if (*ch
=='\"') {
1043 } else if (*ch
==' ') {
1046 if(j
>= (sizeof(argvector
) / sizeof(*argvector
))) {
1047 parseError
= "Too many arguments";
1055 } else if (*ch
=='\"') {
1057 } else if (*ch
=='(') {
1059 } else if (*ch
==')') {
1066 parseError
= "Bracket mismatch!";
1070 if (*(ch
-1) == 0) /* if the last character was a space */
1071 j
--; /* remove an argument */
1073 /* for(k=1;k<=j;k++)
1074 if(!unescape(argvector[k], argvector[k], sizeof(inputb)))
1078 if (!(cmd
=findcommandintree(ctx
->searchcmd
->searchtree
,argvector
[0],1))) {
1079 parseError
= "Unknown command (for valid command list, see help <searchcmd>)";
1082 if (!controlpermitted(cmd
->level
, ctx
->sender
)) {
1083 parseError
= "Access denied (for valid command list, see help <searchcmd>)";
1086 return ((parseFunc
)cmd
->handler
)(ctx
, j
, argvector
+1);
1091 /* slug: disabled now we unescape during the main parse stage */
1092 if(!unescape(input
, thestring
, sizeof(thestring
)))
1095 if (!(thenode
=(struct searchNode
*)malloc(sizeof(struct searchNode
)))) {
1096 parseError
= "malloc: could not allocate memory for this search.";
1100 thenode
->localdata
= getsstring(thestring
,512);
1101 thenode
->returntype
= RETURNTYPE_CONST
| RETURNTYPE_STRING
;
1102 thenode
->exe
= literal_exe
;
1103 thenode
->free
= literal_free
;
1109 void nssnprintf(char *buf
, size_t size
, const char *format
, nick
*np
) {
1122 for(p
=format
;*p
;p
++) {
1124 if(!sbaddchar(&b
, *p
))
1132 if(!sbaddchar(&b
, *p
))
1140 c
= np
->nick
; break;
1142 c
= np
->ident
; break;
1144 c
= np
->host
->name
->content
; break;
1146 snprintf(hostbuf
, sizeof(hostbuf
), "%s", IPtostr(np
->p_ipaddr
));
1150 snprintf(hostbuf
, sizeof(hostbuf
), "%s!%s@%s", np
->nick
, np
->ident
, IPtostr(np
->p_ipaddr
));
1154 c
= "(bad format specifier)";
1157 if(!sbaddstr(&b
, c
))
1169 static char *var_tochar(searchCtx
*ctx
, char *arg
, searchNode
**variable
) {
1170 *variable
= ctx
->parser(ctx
, arg
);
1171 if (!(*variable
= coerceNode(ctx
, *variable
, RETURNTYPE_STRING
)))
1174 if(!((*variable
)->returntype
& RETURNTYPE_CONST
)) {
1175 parseError
= "only constant variables allowed";
1176 ((*variable
)->free
)(ctx
, *variable
);
1180 return (char *)((*variable
)->exe
)(ctx
, *variable
, NULL
);
1183 void free_val_null(searchCtx
*ctx
, struct searchNode
*thenode
) {
1186 struct searchVariable
*var_register(searchCtx
*ctx
, char *arg
, int type
) {
1187 searchNode
*variable
;
1188 struct searchVariable
*us
;
1192 if(ctx
->lastvar
>= MAX_VARIABLES
) {
1193 parseError
= "Maximum number of variables reached";
1197 us
= &ctx
->vars
[ctx
->lastvar
];
1199 var
= var_tochar(ctx
, arg
, &variable
);
1203 strlcpy(us
->name
, var
, sizeof(us
->name
));
1204 (variable
->free
)(ctx
, variable
);
1206 for(i
=0;i
<ctx
->lastvar
;i
++) {
1207 if(!strcmp(us
->name
, ctx
->vars
[i
].name
)) {
1208 parseError
= "variable name already in use";
1214 us
->data
.returntype
= type
;
1215 us
->data
.localdata
= &us
->cdata
;
1216 us
->data
.exe
= exe_val_null
;
1217 us
->data
.free
= free_val_null
;
1219 us
->cdata
.child
= NULL
;
1223 searchNode
*var_get(searchCtx
*ctx
, char *arg
) {
1224 searchNode
*variable
, *found
= NULL
;
1226 char *var
= var_tochar(ctx
, arg
, &variable
);
1230 for(i
=0;i
<ctx
->lastvar
;i
++) {
1231 if(!strcmp(var
, ctx
->vars
[i
].name
)) {
1232 found
= &ctx
->vars
[i
].data
;
1236 (variable
->free
)(ctx
, variable
);
1239 parseError
= "variable not found";
1243 void var_setstr(struct searchVariable
*v
, char *data
) {
1244 v
->cdata
.u
.stringbuf
= data
;
1247 #ifdef NEWSEARCH_NEWPARSER
1248 void displaystrerror(replyFunc reply
, nick
*np
, const char *input
) {
1251 if((parseStrErrorPos
>= 0) && (parseStrErrorPos
< sizeof(buf
) - 3)) {
1254 for(i
=0;i
<parseStrErrorPos
;i
++)
1260 reply(np
, "%s", input
);
1261 reply(np
, "%s", buf
);
1264 reply(np
, "Parse error: %s", parseStrError
);
1268 struct searchNode
*argtoconststr(char *command
, searchCtx
*ctx
, char *arg
, char **p
) {
1269 struct searchNode
*c
;
1270 static char errorbuf
[512];
1272 c
= ctx
->parser(ctx
, arg
);
1273 if (!(c
= coerceNode(ctx
, c
, RETURNTYPE_STRING
))) {
1274 snprintf(errorbuf
, sizeof(errorbuf
), "%s: unable to coerce argument to string", command
);
1275 parseError
= errorbuf
;
1279 if (!(c
->returntype
& RETURNTYPE_CONST
)) {
1280 snprintf(errorbuf
, sizeof(errorbuf
), "%s: constant argument required", command
);
1281 parseError
= errorbuf
;
1287 *p
= (char *)(c
->exe
)(ctx
, c
, NULL
);