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
);
25 int do_whowassearch(void *source
, int cargc
, char **cargv
);
27 void printnick_channels(searchCtx
*, nick
*, nick
*);
28 void printchannel(searchCtx
*, nick
*, chanindex
*);
29 void printchannel_topic(searchCtx
*, nick
*, chanindex
*);
30 void printchannel_services(searchCtx
*, nick
*, chanindex
*);
32 UserDisplayFunc defaultuserfn
= printuser
;
33 NickDisplayFunc defaultnickfn
= printnick
;
34 ChanDisplayFunc defaultchanfn
= printchannel
;
35 WhowasDisplayFunc defaultwhowasfn
= printwhowas
;
37 searchCmd
*reg_nicksearch
, *reg_chansearch
, *reg_usersearch
, *reg_whowassearch
;
38 void displaycommandhelp(nick
*, Command
*);
39 void displaystrerror(replyFunc
, nick
*, const char *);
41 searchCmd
*registersearchcommand(char *name
, int level
, CommandHandler cmd
, void *defaultdisplayfunc
) {
45 registercontrolhelpfunccmd(name
, NO_OPER
,4, cmd
, &displaycommandhelp
);
46 acmd
=(struct searchCmd
*)malloc(sizeof(struct searchCmd
));
48 Error("newsearch", ERR_ERROR
, "malloc failed: registersearchcommand");
53 acmd
->name
= getsstring( name
, NSMAX_COMMAND_LEN
);
54 acmd
->outputtree
= newcommandtree();
55 acmd
->searchtree
= newcommandtree();
57 addcommandtotree(searchCmdTree
, name
, 0, 0, (CommandHandler
)acmd
);
59 for (sl
=globalterms
; sl
; sl
=sl
->next
) {
60 addcommandexttotree(acmd
->searchtree
, sl
->name
->content
, 0, 1, (CommandHandler
)sl
->cmd
, sl
->help
);
66 void deregistersearchcommand(searchCmd
*scmd
) {
67 deregistercontrolcmd(scmd
->name
->content
, (CommandHandler
)scmd
->handler
);
68 destroycommandtree(scmd
->outputtree
);
69 destroycommandtree(scmd
->searchtree
);
70 deletecommandfromtree(searchCmdTree
, scmd
->name
->content
, (CommandHandler
) scmd
);
71 freesstring(scmd
->name
);
75 void displaycommandhelp(nick
*np
, Command
*cmd
) {
77 Command
*cmdlist
[100], *acmdlist
[100];
80 n
=getcommandlist(searchCmdTree
,cmdlist
,100);
82 /* note: we may want to only deregister a command if we've already registered it, for now, try de-registering new commands anyway */
83 if ( ((searchCmd
*)cmdlist
[i
]->handler
)->handler
!= cmd
->handler
)
85 acmd
= ((searchCmd
*)(cmdlist
[i
]->handler
));
87 controlreply(np
, "Usage: [flags] <criteria>\n");
88 controlreply(np
, "Flags:\n");
89 controlreply(np
, " -l int : Limit number of rows of results\n");
90 controlreply(np
, " -d string : a valid output format for the results\n");
91 controlreply(np
, " -s subset : ipmask subset of network to search (only for node search)\n");
92 controlreply(np
, " \n");
93 controlreply(np
, "Available Output Formats:\n");
95 m
=getcommandlist(acmd
->outputtree
,acmdlist
,100);
97 if ( controlpermitted( acmdlist
[j
]->level
, np
) ) {
98 char *help
=(char *)acmdlist
[j
]->ext
;
99 if ( help
&& help
[0] != '\0')
100 controlreply(np
, "%-10s: %s\n", acmdlist
[j
]->command
->content
, help
);
102 controlreply(np
, "%s\n", acmdlist
[j
]->command
->content
);
106 controlreply(np
, " \n");
107 controlreply(np
, "Available Global Commands and Operators:\n" );
108 m
=getcommandlist(acmd
->searchtree
,acmdlist
,100);
110 if ( acmdlist
[j
]->maxparams
) {
111 char *help
=(char *)acmdlist
[j
]->ext
;
112 if ( help
&& help
[0] != '\0')
113 controlreply(np
, "%-10s: %s\n", acmdlist
[j
]->command
->content
, help
);
115 controlreply(np
, "%s\n", acmdlist
[j
]->command
->content
);
119 controlreply(np
, " \n");
120 controlreply(np
, "Available Commands and Operators for %s:\n", acmd
->name
->content
);
122 m
=getcommandlist(acmd
->searchtree
,acmdlist
,100);
124 if ( !acmdlist
[j
]->maxparams
&& controlpermitted( acmdlist
[j
]->level
, np
) ) {
125 char *help
=(char *)acmdlist
[j
]->ext
;
126 if ( help
&& help
[0] != '\0')
127 controlreply(np
, "%-10s: %s\n", acmdlist
[j
]->command
->content
, help
);
129 controlreply(np
, "%s\n", acmdlist
[j
]->command
->content
);
135 void regdisp( searchCmd
*cmd
, const char *name
, void *handler
, int level
, char *help
) {
136 addcommandexttotree(cmd
->outputtree
, name
, level
, 0, (CommandHandler
) handler
, (char *)help
);
139 void unregdisp( searchCmd
*cmd
, const char *name
, void *handler
) {
140 deletecommandfromtree(cmd
->outputtree
, name
, (CommandHandler
) handler
);
143 void *findcommandinlist( searchList
*sl
, char *name
){
145 if(strcmp(sl
->name
->content
,name
) == 0 ) {
153 const char *parseError
;
154 /* used for *_free functions that need to warn users of certain things
155 i.e. hitting too many users in a (kill) or (gline) */
156 nick
*senderNSExtern
;
159 searchCmdTree
=newcommandtree();
161 reg_nicksearch
= (searchCmd
*)registersearchcommand("nicksearch",NO_OPER
,&do_nicksearch
, printnick
);
162 reg_chansearch
= (searchCmd
*)registersearchcommand("chansearch",NO_OPER
,&do_chansearch
, printchannel
);
163 reg_usersearch
= (searchCmd
*)registersearchcommand("usersearch",NO_OPER
,&do_usersearch
, printuser
);
164 reg_whowassearch
= (searchCmd
*)registersearchcommand("whowassearch",NO_OPER
,&do_whowassearch
, printwhowas
);
166 /* Boolean operations */
167 registerglobalsearchterm("and",and_parse
, "usage: (and (X) (X))" );
168 registerglobalsearchterm("not",not_parse
, "usage: (not (X))");
169 registerglobalsearchterm("or",or_parse
, "usage: (or (X) (X))" );
171 registerglobalsearchterm("eq",eq_parse
, "usage: (eq (X) Y)");
173 registerglobalsearchterm("lt",lt_parse
, "usage: (lt (X) int)");
174 registerglobalsearchterm("gt",gt_parse
, "usage: (gt (X) int)");
176 /* String operations */
177 registerglobalsearchterm("match",match_parse
, "usage: (match (X) string)");
178 registerglobalsearchterm("regex",regex_parse
, "usage: (regex (X) string)");
179 registerglobalsearchterm("length",length_parse
, "usage: (length string)");
181 /* Nickname operations */
182 registersearchterm(reg_nicksearch
, "hostmask",hostmask_parse
, 0, "The user's nick!user@host; \"hostmask real\" returns nick!user@host\rreal");
183 registersearchterm(reg_whowassearch
, "hostmask",hostmask_parse
, 0, "The user's nick!user@host; \"hostmask real\" returns nick!user@host\rreal");
185 registersearchterm(reg_nicksearch
, "realname",realname_parse
, 0, "User's current realname"); /* nick only */
186 registersearchterm(reg_whowassearch
, "realname",realname_parse
, 0, "User's current realname"); /* nick only */
188 registersearchterm(reg_nicksearch
, "away",away_parse
, 0, "User's current away message");
189 registersearchterm(reg_whowassearch
, "away",away_parse
, 0, "User's current away message");
190 registersearchterm(reg_nicksearch
, "authname",authname_parse
, 0, "User's current authname or false");
191 registersearchterm(reg_whowassearch
, "authname",authname_parse
, 0, "User's current authname or false");
192 registersearchterm(reg_nicksearch
, "authts",authts_parse
, 0, "User's Auth timestamp");
193 registersearchterm(reg_whowassearch
, "authts",authts_parse
, 0, "User's Auth timestamp");
194 registersearchterm(reg_nicksearch
, "ident",ident_parse
, 0, "User's current ident");
195 registersearchterm(reg_whowassearch
, "ident",ident_parse
, 0, "User's current ident");
196 registersearchterm(reg_nicksearch
, "host",host_parse
, 0, "User's host, allow \"host real\" to match real host");
197 registersearchterm(reg_whowassearch
, "host",host_parse
, 0, "User's host, allow \"host real\" to match real host");
198 registersearchterm(reg_nicksearch
, "channel",channel_parse
, 0, "Valid Channel Name to match users against"); /* nick only */
199 registersearchterm(reg_nicksearch
, "timestamp",timestamp_parse
, 0, "User's Timestamp");
200 registersearchterm(reg_whowassearch
, "timestamp",timestamp_parse
, 0, "User's Timestamp");
201 registersearchterm(reg_nicksearch
, "country",country_parse
, 0, "2 letter country code (data source is geoip)");
202 registersearchterm(reg_nicksearch
, "ip",ip_parse
, 0, "User's IP - ipv4 or ipv6 format as appropriate. Note: not 6to4");
203 registersearchterm(reg_whowassearch
, "ip",ip_parse
, 0, "User's IP - ipv4 or ipv6 format as appropriate. Note: not 6to4");
204 registersearchterm(reg_nicksearch
, "channels",channels_parse
, 0, "Channel Count"); /* nick only */
205 registersearchterm(reg_nicksearch
, "server",server_parse
, 0, "Server Name. Either (server string) or (match (server) string)");
206 registersearchterm(reg_whowassearch
, "server",server_parse
, 0, "Server Name. Either (server string) or (match (server) string)");
207 registersearchterm(reg_whowassearch
, "server",server_parse
, 0, "Server Name. Either (server string) or (match (server) string)");
208 registersearchterm(reg_nicksearch
, "authid",authid_parse
, 0, "User's Auth ID");
209 registersearchterm(reg_whowassearch
, "authid",authid_parse
, 0, "User's Auth ID");
210 registersearchterm(reg_nicksearch
, "cidr",cidr_parse
, 0, "CIDR matching");
211 registersearchterm(reg_whowassearch
, "cidr",cidr_parse
, 0, "CIDR matching");
212 registersearchterm(reg_nicksearch
, "ipvsix",ipv6_parse
, 0, "IPv6 user");
213 registersearchterm(reg_whowassearch
, "ipvsix",ipv6_parse
, 0, "IPv6 user");
215 /* Whowas operations */
216 registersearchterm(reg_whowassearch
, "quit",quit_parse
, 0, "User quit");
217 registersearchterm(reg_whowassearch
, "killed",killed_parse
, 0, "User was killed");
218 registersearchterm(reg_whowassearch
, "renamed",renamed_parse
, 0, "User changed nick");
219 registersearchterm(reg_whowassearch
, "age",age_parse
, 0, "Whowas record age in seconds");
220 registersearchterm(reg_whowassearch
, "newnick",newnick_parse
, 0, "New nick (for rename whowas records)");
221 registersearchterm(reg_whowassearch
, "reason",reason_parse
, 0, "Quit/kill reason");
223 /* Channel operations */
224 registersearchterm(reg_chansearch
, "exists",exists_parse
, 0, "Returns if channel exists on network. Note: newserv may store data on empty channels"); /* channel only */
225 registersearchterm(reg_chansearch
, "services",services_parse
, 0, ""); /* channel only */
226 registersearchterm(reg_chansearch
, "size",size_parse
, 0, "Channel user count"); /* channel only */
227 registersearchterm(reg_chansearch
, "name",name_parse
, 0, "Channel Name"); /* channel only */
228 registersearchterm(reg_chansearch
, "topic",topic_parse
, 0, "Channel topic"); /* channel only */
229 registersearchterm(reg_chansearch
, "oppct",oppct_parse
, 0, "Percentage Opped"); /* channel only */
230 registersearchterm(reg_chansearch
, "cumodecount",cumodecount_parse
, 0, "Count of users with given channel modes"); /* channel only */
231 registersearchterm(reg_chansearch
, "cumodepct",cumodepct_parse
, 0, "Percentage of users with given channel modes"); /* channel only */
232 registersearchterm(reg_chansearch
, "uniquehostpct",hostpct_parse
, 0, "uniquehost percent"); /* channel only */
233 registersearchterm(reg_chansearch
, "authedpct",authedpct_parse
, 0, "Percentage of authed users"); /* channel only */
234 registersearchterm(reg_chansearch
, "kick",kick_parse
, 0, "KICK users channels in newsearch result. Note: evaluation order"); /* channel only */
236 /* Nickname / channel operations */
237 registersearchterm(reg_chansearch
, "modes",modes_parse
, 0, "Channel Modes");
238 registersearchterm(reg_nicksearch
, "modes",modes_parse
, 0, "User Modes");
239 registersearchterm(reg_whowassearch
, "modes",modes_parse
, 0, "User Modes");
240 registersearchterm(reg_chansearch
, "nick",nick_parse
, 0, "Nickname");
241 registersearchterm(reg_nicksearch
, "nick",nick_parse
, 0, "Nickname");
242 registersearchterm(reg_whowassearch
, "nick",nick_parse
, 0, "Nickname");
244 /* Kill / gline parameters */
245 registersearchterm(reg_chansearch
,"kill",kill_parse
, 0, "KILL users in newsearch result. Note: evaluation order");
246 registersearchterm(reg_chansearch
,"gline",gline_parse
, 0, "GLINE users in newsearch result. Note: evaluation order");
247 registersearchterm(reg_nicksearch
,"kill",kill_parse
, 0, "KILL users in newsearch result. Note: evaluation order");
248 registersearchterm(reg_nicksearch
,"gline",gline_parse
, 0, "GLINE users in newsearch result. Note: evaluation order");
249 registersearchterm(reg_whowassearch
,"gline",gline_parse
, 0, "GLINE users in newsearch result. Note: evaluation order");
251 /* Iteration functionality */
252 registerglobalsearchterm("any",any_parse
, "usage: any (generatorfn x) (fn ... (var x) ...)");
253 registerglobalsearchterm("all",all_parse
, "usage: all (generatorfn x) (fn ... (var x) ...)");
254 registerglobalsearchterm("var",var_parse
, "usage: var variable");
256 /* Iterable functions */
257 registersearchterm(reg_nicksearch
, "channeliter",channeliter_parse
, 0, "Channel Iterable function - usage: (any (channeliter x) (match (var x) #twilight*))"); /* nick only */
258 registersearchterm(reg_chansearch
, "nickiter",nickiter_parse
, 0, "Nick Iterable function - usage: (any (nickiter x) (match (var x) nickname*))"); /* channel only */
260 /* Functions that work on strings?! */
261 registersearchterm(reg_nicksearch
, "cumodes", cumodes_parse
, 0, "usage: (cumodes (var x) <modes>)");
262 registersearchterm(reg_chansearch
, "cumodes", cumodes_parse
, 0, "usage: (cumodes (var x) <modes>)");
264 /* Notice functionality */
265 registersearchterm(reg_chansearch
,"notice",notice_parse
, 0, "NOTICE users in newsearch result. Note: evaluation order");
266 registersearchterm(reg_nicksearch
,"notice",notice_parse
, 0, "NOTICE users in newsearch result. Note: evaluation order");
268 /* Nick output filters */
269 regdisp(reg_nicksearch
,"default",printnick
, 0, "");
270 regdisp(reg_nicksearch
,"channels",printnick_channels
, 0, "include channels in output");
272 /* Channel output filters */
273 regdisp(reg_chansearch
,"default",printchannel
, 0, "");
274 regdisp(reg_chansearch
,"topic",printchannel_topic
, 0, "display channel topics");
275 regdisp(reg_chansearch
,"services",printchannel_services
, 0, "display services on channels");
277 /* Nick output filters */
278 regdisp(reg_usersearch
,"default",printuser
, 0, "");
283 searchList
*sl
, *psl
;
285 Command
*cmdlist
[100];
292 n
=getcommandlist(searchCmdTree
,cmdlist
,100);
294 deregistersearchterm( (searchCmd
*)cmdlist
[i
]->handler
, psl
->name
->content
, psl
->cmd
);
297 freesstring(psl
->name
);
303 deregistersearchcommand( reg_nicksearch
);
304 deregistersearchcommand( reg_chansearch
);
305 deregistersearchcommand( reg_usersearch
);
306 destroycommandtree( searchCmdTree
);
309 void registerglobalsearchterm(char *term
, parseFunc parsefunc
, char *help
) {
311 Command
*cmdlist
[100];
312 searchList
*sl
= malloc(sizeof(searchList
));
314 Error("newsearch", ERR_ERROR
, "malloc failed: registerglobalsearchterm");
319 sl
->name
= getsstring(term
, NSMAX_COMMAND_LEN
);
322 int len
=strlen(help
);
323 sl
->help
=(char *)malloc(len
+1);
325 Error("newsearch", ERR_ERROR
, "malloc failed: registerglobalsearchterm");
328 strncpy(sl
->help
, help
, len
);
329 sl
->help
[len
] = '\0';
335 if ( globalterms
!= NULL
) {
336 sl
->next
= globalterms
;
340 n
=getcommandlist(searchCmdTree
,cmdlist
,100);
342 /* maxparams is set to 1 to indicate a global term */
343 /* access level is set to 0 for all global terms */
344 addcommandexttotree( ((searchCmd
*)cmdlist
[i
]->handler
)->searchtree
,term
, 0, 1, (CommandHandler
) parsefunc
, help
);
348 void deregisterglobalsearchterm(char *term
, parseFunc parsefunc
) {
350 Command
*cmdlist
[100];
351 searchList
*sl
, *psl
=NULL
;
353 for (sl
=globalterms
; sl
; sl
=sl
->next
) {
354 if ( strcmp( sl
->name
->content
, term
) == 0 ) {
362 psl
->next
= sl
->next
;
365 n
=getcommandlist(searchCmdTree
,cmdlist
,100);
367 deletecommandfromtree( ((searchCmd
*)cmdlist
[i
]->handler
)->searchtree
, term
, (CommandHandler
) parsefunc
);
369 freesstring(sl
->name
);
374 void registersearchterm(searchCmd
*cmd
, char *term
, parseFunc parsefunc
, int level
, char *help
) {
375 /* NOTE: global terms are added to the tree elsewhere as we set maxparams to 1 to indicate global */
376 addcommandexttotree(cmd
->searchtree
, term
, level
, 0, (CommandHandler
) parsefunc
, help
);
379 void deregistersearchterm(searchCmd
*cmd
, char *term
, parseFunc parsefunc
) {
380 /* NOTE: global terms are removed from the tree within deregisterglobalsearchterm */
381 deletecommandfromtree(cmd
->searchtree
, term
, (CommandHandler
) parsefunc
);
384 static void controlwallwrapper(int level
, char *format
, ...) __attribute__ ((format (printf
, 2, 3)));
385 static void controlwallwrapper(int level
, char *format
, ...) {
389 va_start(ap
, format
);
390 vsnprintf(buf
, sizeof(buf
), format
, ap
);
391 controlwall(NO_OPER
, level
, "%s", buf
);
395 int parseopts(int cargc
, char **cargv
, int *arg
, int *limit
, void **subset
, void *display
, CommandTree
*sl
, replyFunc reply
, void *sender
) {
398 struct irc_in_addr sin
; unsigned char bits
;
400 if (*cargv
[0] == '-') {
404 for (ch
=cargv
[0]+1;*ch
;ch
++) {
408 reply(sender
,"Error: -l switch requires an argument (for help, see help <searchcmd>)");
411 *limit
=strtoul(cargv
[(*arg
)++],NULL
,10);
416 reply(sender
,"Error: -d switch requires an argument (for help, see help <searchcmd>)");
419 cmd
=findcommandintree(sl
, cargv
[*arg
],1);
421 reply(sender
,"Error: unknown output format %s (for help, see help <searchcmd>)",cargv
[*arg
]);
424 if ( !controlpermitted( cmd
->level
, sender
) ) {
425 reply(sender
,"Error: Access Denied for output format %s (for help, see help <searchcmd>)", cargv
[*arg
]);
428 *((void **)display
)=(void *)cmd
->handler
;
433 if (subset
== NULL
) {
434 reply(sender
,"Error: -s switch not supported for this search.");
438 reply(sender
,"Error: -s switch requires an argument (for help, see help <searchcmd>)");
441 if (ipmask_parse(cargv
[*arg
], &sin
, &bits
) == 0) {
442 reply(sender
, "Error: Invalid CIDR mask supplied (for help, see help <searchcmd>)");
445 *subset
= (void *)refnode(iptree
, &sin
, bits
);
450 reply(sender
,"Unrecognised flag -%c. (for help, see help <searchcmd>)",*ch
);
459 void newsearch_ctxinit(searchCtx
*ctx
, searchParseFunc searchfn
, replyFunc replyfn
, wallFunc wallfn
, void *arg
, searchCmd
*cmd
, nick
*np
, void *displayfn
, int limit
) {
460 memset(ctx
, 0, sizeof(searchCtx
));
462 ctx
->reply
= replyfn
;
464 ctx
->parser
= searchfn
;
466 ctx
->searchcmd
= cmd
;
469 ctx
->displayfn
= displayfn
;
472 int do_nicksearch_real(replyFunc reply
, wallFunc wall
, void *source
, int cargc
, char **cargv
) {
473 nick
*sender
= source
;
476 NickDisplayFunc display
=defaultnickfn
;
481 reply( sender
, "Usage: [flags] <criteria>");
482 reply( sender
, "For help, see help nicksearch");
486 ret
= parseopts(cargc
, cargv
, &arg
, &limit
, NULL
, (void *)&display
, reg_nicksearch
->outputtree
, reply
, sender
);
491 reply(sender
,"No search terms - aborting.");
496 rejoinline(cargv
[arg
],cargc
-arg
);
499 tree
= parse_string(reg_nicksearch
, cargv
[arg
]);
501 displaystrerror(reply
, sender
, cargv
[arg
]);
505 ast_nicksearch(tree
->root
, reply
, sender
, wall
, display
, NULL
, NULL
, limit
);
512 int do_nicksearch(void *source
, int cargc
, char **cargv
) {
513 return do_nicksearch_real(controlreply
, controlwallwrapper
, source
, cargc
, cargv
);
516 void nicksearch_exe(struct searchNode
*search
, searchCtx
*ctx
) {
519 unsigned int cmarker
;
520 unsigned int tchans
=0,uchans
=0;
522 nick
*np
, *sender
= ctx
->sender
;
523 senderNSExtern
= sender
;
524 NickDisplayFunc display
= ctx
->displayfn
;
525 int limit
= ctx
->limit
;
527 /* Get a marker value to mark "seen" channels for unique count */
528 cmarker
=nextchanmarker();
530 /* The top-level node needs to return a BOOL */
531 search
=coerceNode(ctx
, search
, RETURNTYPE_BOOL
);
533 for (i
=0;i
<NICKHASHSIZE
;i
++) {
534 for (np
=nicktable
[i
];np
;np
=np
->next
) {
535 if ((search
->exe
)(ctx
, search
, np
)) {
536 /* Add total channels */
537 tchans
+= np
->channels
->cursi
;
539 /* Check channels for uniqueness */
540 cs
=(channel
**)np
->channels
->content
;
541 for (j
=0;j
<np
->channels
->cursi
;j
++) {
542 if (cs
[j
]->index
->marker
!= cmarker
) {
543 cs
[j
]->index
->marker
=cmarker
;
549 display(ctx
, sender
, np
);
552 ctx
->reply(sender
, "--- More than %d matches, skipping the rest",limit
);
558 ctx
->reply(sender
,"--- End of list: %d matches; users were on %u channels (%u unique, %.1f average clones)",
559 matches
, tchans
, uchans
, (float)tchans
/uchans
);
562 int do_whowassearch_real(replyFunc reply
, wallFunc wall
, void *source
, int cargc
, char **cargv
) {
563 nick
*sender
= source
;
566 WhowasDisplayFunc display
=defaultwhowasfn
;
571 reply( sender
, "Usage: [flags] <criteria>");
572 reply( sender
, "For help, see help whowassearch");
576 ret
= parseopts(cargc
, cargv
, &arg
, &limit
, NULL
, (void *)&display
, reg_whowassearch
->outputtree
, reply
, sender
);
581 reply(sender
,"No search terms - aborting.");
586 rejoinline(cargv
[arg
],cargc
-arg
);
589 tree
= parse_string(reg_whowassearch
, cargv
[arg
]);
591 displaystrerror(reply
, sender
, cargv
[arg
]);
595 ast_whowassearch(tree
->root
, reply
, sender
, wall
, display
, NULL
, NULL
, limit
);
602 int do_whowassearch(void *source
, int cargc
, char **cargv
) {
603 return do_whowassearch_real(controlreply
, controlwallwrapper
, source
, cargc
, cargv
);
606 void whowassearch_exe(struct searchNode
*search
, searchCtx
*ctx
) {
609 nick
*sender
= ctx
->sender
;
610 senderNSExtern
= sender
;
611 WhowasDisplayFunc display
= ctx
->displayfn
;
612 int limit
= ctx
->limit
;
614 /* The top-level node needs to return a BOOL */
615 search
=coerceNode(ctx
, search
, RETURNTYPE_BOOL
);
617 for (i
= whowasoffset
; i
< whowasoffset
+ WW_MAXENTRIES
; i
++) {
618 ww
= &whowasrecs
[i
% WW_MAXENTRIES
];
620 if (ww
->type
== WHOWAS_UNUSED
)
623 /* Note: We're passing the nick to the filter function. The original
624 * whowas record is in the nick's ->next field. */
625 if ((search
->exe
)(ctx
, search
, &ww
->nick
)) {
627 display(ctx
, sender
, ww
);
630 ctx
->reply(sender
, "--- More than %d matches, skipping the rest",limit
);
635 ctx
->reply(sender
,"--- End of list: %d matches", matches
);
638 int do_chansearch_real(replyFunc reply
, wallFunc wall
, void *source
, int cargc
, char **cargv
) {
639 nick
*sender
= source
;
642 ChanDisplayFunc display
=defaultchanfn
;
647 reply( sender
, "Usage: [flags] <criteria>");
648 reply( sender
, "For help, see help chansearch");
652 ret
= parseopts(cargc
, cargv
, &arg
, &limit
, NULL
, (void *)&display
, reg_chansearch
->outputtree
, reply
, sender
);
657 reply(sender
,"No search terms - aborting.");
662 rejoinline(cargv
[arg
],cargc
-arg
);
665 tree
= parse_string(reg_chansearch
, cargv
[arg
]);
667 displaystrerror(reply
, sender
, cargv
[arg
]);
671 ast_chansearch(tree
->root
, reply
, sender
, wall
, display
, NULL
, NULL
, limit
);
678 int do_chansearch(void *source
, int cargc
, char **cargv
) {
679 return do_chansearch_real(controlreply
, controlwallwrapper
, source
, cargc
, cargv
);
682 void chansearch_exe(struct searchNode
*search
, searchCtx
*ctx
) {
686 nick
*sender
= ctx
->sender
;
687 senderNSExtern
= sender
;
688 ChanDisplayFunc display
= ctx
->displayfn
;
689 int limit
= ctx
->limit
;
691 search
=coerceNode(ctx
, search
, RETURNTYPE_BOOL
);
693 for (i
=0;i
<CHANNELHASHSIZE
;i
++) {
694 for (cip
=chantable
[i
];cip
;cip
=cip
->next
) {
695 if ((search
->exe
)(ctx
, search
, cip
)) {
697 display(ctx
, sender
, cip
);
699 ctx
->reply(sender
, "--- More than %d matches, skipping the rest",limit
);
705 ctx
->reply(sender
,"--- End of list: %d matches", matches
);
708 int do_usersearch_real(replyFunc reply
, wallFunc wall
, void *source
, int cargc
, char **cargv
) {
709 nick
*sender
= source
;
712 UserDisplayFunc display
=defaultuserfn
;
717 reply( sender
, "Usage: [flags] <criteria>");
718 reply( sender
, "For help, see help usersearch");
722 ret
= parseopts(cargc
, cargv
, &arg
, &limit
, NULL
, (void *)&display
, reg_usersearch
->outputtree
, reply
, sender
);
727 reply(sender
,"No search terms - aborting.");
732 rejoinline(cargv
[arg
],cargc
-arg
);
735 tree
= parse_string(reg_usersearch
, cargv
[arg
]);
737 displaystrerror(reply
, sender
, cargv
[arg
]);
741 ast_usersearch(tree
->root
, reply
, sender
, wall
, display
, NULL
, NULL
, limit
);
748 int do_usersearch(void *source
, int cargc
, char **cargv
) {
749 return do_usersearch_real(controlreply
, controlwallwrapper
, source
, cargc
, cargv
);
752 void usersearch_exe(struct searchNode
*search
, searchCtx
*ctx
) {
756 nick
*sender
= ctx
->sender
;
757 int limit
= ctx
->limit
;
758 UserDisplayFunc display
= ctx
->displayfn
;
759 senderNSExtern
= sender
;
761 search
=coerceNode(ctx
, search
, RETURNTYPE_BOOL
);
763 for (i
=0;i
<AUTHNAMEHASHSIZE
;i
++) {
764 for (aup
=authnametable
[i
];aup
;aup
=aup
->next
) {
765 if ((search
->exe
)(ctx
, search
, aup
)) {
767 display(ctx
, sender
, aup
);
769 ctx
->reply(sender
, "--- More than %d matches, skipping the rest",limit
);
775 ctx
->reply(sender
,"--- End of list: %d matches", matches
);
778 /* Free a coerce node */
779 void free_coerce(searchCtx
*ctx
, struct searchNode
*thenode
) {
780 struct coercedata
*cd
=thenode
->localdata
;
782 cd
->child
->free(ctx
, cd
->child
);
783 free(thenode
->localdata
);
787 /* Free a coerce node with a stringbuf allocated */
788 void free_coercestring(searchCtx
*ctx
, struct searchNode
*thenode
) {
789 free(((struct coercedata
*)thenode
->localdata
)->u
.stringbuf
);
790 free_coerce(ctx
, thenode
);
793 /* exe_tostr_null: return the constant string */
794 void *exe_tostr_null(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
795 struct coercedata
*cd
=thenode
->localdata
;
797 return cd
->u
.stringbuf
;
800 /* exe_val_null: return the constant value */
801 void *exe_val_null(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
802 struct coercedata
*cd
=thenode
->localdata
;
804 return (void *)cd
->u
.val
;
807 /* Lots of very dull type conversion functions */
808 void *exe_inttostr(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
809 struct coercedata
*cd
=thenode
->localdata
;
811 sprintf(cd
->u
.stringbuf
, "%lu", (unsigned long)(cd
->child
->exe
)(ctx
, cd
->child
, theinput
));
813 return cd
->u
.stringbuf
;
816 void *exe_booltostr(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
817 struct coercedata
*cd
=thenode
->localdata
;
819 if ((cd
->child
->exe
)(ctx
, cd
->child
, theinput
)) {
820 sprintf(cd
->u
.stringbuf
,"1");
822 cd
->u
.stringbuf
[0]='\0';
825 return cd
->u
.stringbuf
;
828 void *exe_strtoint(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
829 struct coercedata
*cd
=thenode
->localdata
;
831 return (void *)strtoul((cd
->child
->exe
)(ctx
,cd
->child
,theinput
),NULL
,10);
834 void *exe_booltoint(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
835 struct coercedata
*cd
=thenode
->localdata
;
837 /* Don't need to do anything */
838 return (cd
->child
->exe
)(ctx
, cd
->child
, theinput
);
841 void *exe_strtobool(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
842 struct coercedata
*cd
=thenode
->localdata
;
843 char *ch
=(cd
->child
->exe
)(ctx
, cd
->child
, theinput
);
845 if (!ch
|| *ch
=='\0' || (*ch
=='0' && ch
[1]=='\0')) {
852 void *exe_inttobool(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
853 struct coercedata
*cd
=thenode
->localdata
;
855 if ((cd
->child
->exe
)(ctx
, cd
->child
, theinput
)) {
862 struct searchNode
*coerceNode(searchCtx
*ctx
, struct searchNode
*thenode
, int type
) {
863 struct searchNode
*anode
;
864 struct coercedata
*cd
;
866 /* You can't coerce a NULL */
870 /* No effort required to coerce to the same type */
871 if (type
==(thenode
->returntype
& RETURNTYPE_TYPE
))
874 anode
=(struct searchNode
*)malloc(sizeof(struct searchNode
));
875 anode
->localdata
=cd
=(struct coercedata
*)malloc(sizeof(struct coercedata
));
877 anode
->returntype
=type
; /* We'll return what they want, always */
878 anode
->free
=free_coerce
;
881 case RETURNTYPE_STRING
:
882 /* For a string we'll need a buffer */
883 /* A 64-bit number prints out to 20 digits, this leaves some slack */
884 cd
->u
.stringbuf
=malloc(25);
885 anode
->free
=free_coercestring
;
887 switch(thenode
->returntype
& RETURNTYPE_TYPE
) {
890 if (thenode
->returntype
& RETURNTYPE_CONST
) {
891 /* Constant node: sort it out now */
892 sprintf(cd
->u
.stringbuf
, "%lu", (unsigned long)thenode
->exe(ctx
, thenode
, NULL
));
893 anode
->exe
=exe_tostr_null
;
894 anode
->returntype
|= RETURNTYPE_CONST
;
897 anode
->exe
=exe_inttostr
;
901 case RETURNTYPE_BOOL
:
902 if (thenode
->returntype
& RETURNTYPE_CONST
) {
903 /* Constant bool value */
904 if (thenode
->exe(ctx
, thenode
,NULL
)) {
906 sprintf(cd
->u
.stringbuf
, "1");
908 cd
->u
.stringbuf
[0] = '\0';
910 anode
->exe
=exe_tostr_null
;
911 anode
->returntype
|= RETURNTYPE_CONST
;
913 /* Variable bool value */
914 anode
->exe
=exe_booltostr
;
922 switch (thenode
->returntype
& RETURNTYPE_TYPE
) {
923 case RETURNTYPE_STRING
:
924 if (thenode
->returntype
& RETURNTYPE_CONST
) {
925 cd
->u
.val
=strtoul((thenode
->exe
)(ctx
, thenode
, NULL
), NULL
, 10);
926 anode
->exe
=exe_val_null
;
927 anode
->returntype
|= RETURNTYPE_CONST
;
929 anode
->exe
=exe_strtoint
;
934 case RETURNTYPE_BOOL
:
935 if (thenode
->returntype
& RETURNTYPE_CONST
) {
936 if ((thenode
->exe
)(ctx
, thenode
,NULL
))
941 anode
->exe
=exe_val_null
;
942 anode
->returntype
|= RETURNTYPE_CONST
;
944 anode
->exe
=exe_booltoint
;
951 case RETURNTYPE_BOOL
:
953 switch (thenode
->returntype
& RETURNTYPE_TYPE
) {
954 case RETURNTYPE_STRING
:
955 if (thenode
->returntype
& RETURNTYPE_CONST
) {
956 char *rv
=(char *)((thenode
->exe
)(ctx
, thenode
, NULL
));
957 if (!rv
|| *rv
=='\0' || (*rv
=='0' && rv
[1]=='\0'))
962 anode
->exe
=exe_val_null
;
963 anode
->returntype
|= RETURNTYPE_CONST
;
965 anode
->exe
=exe_strtobool
;
971 if (thenode
->returntype
& RETURNTYPE_CONST
) {
972 if ((thenode
->exe
)(ctx
, thenode
,NULL
))
977 anode
->exe
=exe_val_null
;
978 anode
->returntype
|= RETURNTYPE_CONST
;
980 anode
->exe
=exe_inttobool
;
990 /* Literals always return constant strings... */
991 void *literal_exe(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
992 if (thenode
->localdata
)
993 return ((sstring
*)thenode
->localdata
)->content
;
998 void literal_free(searchCtx
*ctx
, struct searchNode
*thenode
) {
999 freesstring(thenode
->localdata
);
1003 void nssnprintf(char *buf
, size_t size
, const char *format
, nick
*np
) {
1016 for(p
=format
;*p
;p
++) {
1018 if(!sbaddchar(&b
, *p
))
1026 if(!sbaddchar(&b
, *p
))
1034 c
= np
->nick
; break;
1036 c
= np
->ident
; break;
1038 c
= np
->host
->name
->content
; break;
1040 snprintf(hostbuf
, sizeof(hostbuf
), "%s", IPtostr(np
->ipaddress
));
1044 snprintf(hostbuf
, sizeof(hostbuf
), "%s!%s@%s", np
->nick
, np
->ident
, IPtostr(np
->ipaddress
));
1048 c
= "(bad format specifier)";
1051 if(!sbaddstr(&b
, c
))
1063 static char *var_tochar(searchCtx
*ctx
, char *arg
, searchNode
**variable
) {
1064 *variable
= ctx
->parser(ctx
, arg
);
1065 if (!(*variable
= coerceNode(ctx
, *variable
, RETURNTYPE_STRING
)))
1068 if(!((*variable
)->returntype
& RETURNTYPE_CONST
)) {
1069 parseError
= "only constant variables allowed";
1070 ((*variable
)->free
)(ctx
, *variable
);
1074 return (char *)((*variable
)->exe
)(ctx
, *variable
, NULL
);
1077 void free_val_null(searchCtx
*ctx
, struct searchNode
*thenode
) {
1080 struct searchVariable
*var_register(searchCtx
*ctx
, char *arg
, int type
) {
1081 searchNode
*variable
;
1082 struct searchVariable
*us
;
1086 if(ctx
->lastvar
>= MAX_VARIABLES
) {
1087 parseError
= "Maximum number of variables reached";
1091 us
= &ctx
->vars
[ctx
->lastvar
];
1093 var
= var_tochar(ctx
, arg
, &variable
);
1097 strlcpy(us
->name
, var
, sizeof(us
->name
));
1098 (variable
->free
)(ctx
, variable
);
1100 for(i
=0;i
<ctx
->lastvar
;i
++) {
1101 if(!strcmp(us
->name
, ctx
->vars
[i
].name
)) {
1102 parseError
= "variable name already in use";
1108 us
->data
.returntype
= type
;
1109 us
->data
.localdata
= &us
->cdata
;
1110 us
->data
.exe
= exe_val_null
;
1111 us
->data
.free
= free_val_null
;
1113 us
->cdata
.child
= NULL
;
1117 searchNode
*var_get(searchCtx
*ctx
, char *arg
) {
1118 searchNode
*variable
, *found
= NULL
;
1120 char *var
= var_tochar(ctx
, arg
, &variable
);
1124 for(i
=0;i
<ctx
->lastvar
;i
++) {
1125 if(!strcmp(var
, ctx
->vars
[i
].name
)) {
1126 found
= &ctx
->vars
[i
].data
;
1130 (variable
->free
)(ctx
, variable
);
1133 parseError
= "variable not found";
1137 void var_setstr(struct searchVariable
*v
, char *data
) {
1138 v
->cdata
.u
.stringbuf
= data
;
1141 void displaystrerror(replyFunc reply
, nick
*np
, const char *input
) {
1144 if((parseStrErrorPos
>= 0) && (parseStrErrorPos
< sizeof(buf
) - 3)) {
1147 for(i
=0;i
<parseStrErrorPos
;i
++)
1153 reply(np
, "%s", input
);
1154 reply(np
, "%s", buf
);
1157 reply(np
, "Parse error: %s", parseStrError
);
1160 struct searchNode
*argtoconststr(char *command
, searchCtx
*ctx
, char *arg
, char **p
) {
1161 struct searchNode
*c
;
1162 static char errorbuf
[512];
1164 c
= ctx
->parser(ctx
, arg
);
1165 if (!(c
= coerceNode(ctx
, c
, RETURNTYPE_STRING
))) {
1166 snprintf(errorbuf
, sizeof(errorbuf
), "%s: unable to coerce argument to string", command
);
1167 parseError
= errorbuf
;
1171 if (!(c
->returntype
& RETURNTYPE_CONST
)) {
1172 snprintf(errorbuf
, sizeof(errorbuf
), "%s: constant argument required", command
);
1173 parseError
= errorbuf
;
1179 *p
= (char *)(c
->exe
)(ctx
, c
, NULL
);