6 #include "../irc/irc_config.h"
7 #include "../lib/irc_string.h"
8 #include "../parser/parser.h"
9 #include "../control/control.h"
10 #include "../lib/splitline.h"
11 #include "../lib/version.h"
12 #include "../lib/stringbuf.h"
13 #include "../lib/strlfunc.h"
14 #include "../lib/array.h"
15 #include "newsearch.h"
20 CommandTree
*searchCmdTree
;
21 searchList
*globalterms
= NULL
;
23 int do_nicksearch(void *source
, int cargc
, char **cargv
);
24 int do_chansearch(void *source
, int cargc
, char **cargv
);
25 int do_usersearch(void *source
, int cargc
, char **cargv
);
26 int do_whowassearch(void *source
, int cargc
, char **cargv
);
28 void printnick_channels(searchCtx
*, nick
*, nick
*);
29 void printchannel(searchCtx
*, nick
*, chanindex
*);
30 void printchannel_topic(searchCtx
*, nick
*, chanindex
*);
31 void printchannel_services(searchCtx
*, nick
*, chanindex
*);
33 UserDisplayFunc defaultuserfn
= printuser
;
34 NickDisplayFunc defaultnickfn
= printnick
;
35 ChanDisplayFunc defaultchanfn
= printchannel
;
36 WhowasDisplayFunc defaultwhowasfn
= printwhowas
;
38 searchCmd
*reg_nicksearch
, *reg_chansearch
, *reg_usersearch
, *reg_whowassearch
;
39 void displaycommandhelp(nick
*, Command
*);
40 void displaystrerror(replyFunc
, nick
*, const char *);
42 searchCmd
*registersearchcommand(char *name
, int level
, CommandHandler cmd
, void *defaultdisplayfunc
) {
46 registercontrolhelpfunccmd(name
, NO_OPER
,4, cmd
, &displaycommandhelp
);
47 acmd
=(struct searchCmd
*)malloc(sizeof(struct searchCmd
));
49 Error("newsearch", ERR_ERROR
, "malloc failed: registersearchcommand");
54 acmd
->name
= getsstring( name
, NSMAX_COMMAND_LEN
);
55 acmd
->outputtree
= newcommandtree();
56 acmd
->searchtree
= newcommandtree();
58 addcommandtotree(searchCmdTree
, name
, 0, 0, (CommandHandler
)acmd
);
60 for (sl
=globalterms
; sl
; sl
=sl
->next
) {
61 addcommandexttotree(acmd
->searchtree
, sl
->name
->content
, 0, 1, (CommandHandler
)sl
->cmd
, sl
->help
);
67 void deregistersearchcommand(searchCmd
*scmd
) {
68 deregistercontrolcmd(scmd
->name
->content
, (CommandHandler
)scmd
->handler
);
69 destroycommandtree(scmd
->outputtree
);
70 destroycommandtree(scmd
->searchtree
);
71 deletecommandfromtree(searchCmdTree
, scmd
->name
->content
, (CommandHandler
) scmd
);
72 freesstring(scmd
->name
);
76 void displaycommandhelp(nick
*np
, Command
*cmd
) {
78 Command
*cmdlist
[100], *acmdlist
[100];
81 n
=getcommandlist(searchCmdTree
,cmdlist
,100);
83 /* note: we may want to only deregister a command if we've already registered it, for now, try de-registering new commands anyway */
84 if ( ((searchCmd
*)cmdlist
[i
]->handler
)->handler
!= cmd
->handler
)
86 acmd
= ((searchCmd
*)(cmdlist
[i
]->handler
));
88 controlreply(np
, "Usage: [flags] <criteria>\n");
89 controlreply(np
, "Flags:\n");
90 controlreply(np
, " -l int : Limit number of rows of results\n");
91 controlreply(np
, " -d string : a valid output format for the results\n");
92 controlreply(np
, " -s subset : ipmask subset of network to search (only for node search)\n");
93 controlreply(np
, " \n");
94 controlreply(np
, "Available Output Formats:\n");
96 m
=getcommandlist(acmd
->outputtree
,acmdlist
,100);
98 if ( controlpermitted( acmdlist
[j
]->level
, np
) ) {
99 char *help
=(char *)acmdlist
[j
]->ext
;
100 if ( help
&& help
[0] != '\0')
101 controlreply(np
, "%-10s: %s\n", acmdlist
[j
]->command
->content
, help
);
103 controlreply(np
, "%s\n", acmdlist
[j
]->command
->content
);
107 controlreply(np
, " \n");
108 controlreply(np
, "Available Global Commands and Operators:\n" );
109 m
=getcommandlist(acmd
->searchtree
,acmdlist
,100);
111 if ( acmdlist
[j
]->maxparams
) {
112 char *help
=(char *)acmdlist
[j
]->ext
;
113 if ( help
&& help
[0] != '\0')
114 controlreply(np
, "%-10s: %s\n", acmdlist
[j
]->command
->content
, help
);
116 controlreply(np
, "%s\n", acmdlist
[j
]->command
->content
);
120 controlreply(np
, " \n");
121 controlreply(np
, "Available Commands and Operators for %s:\n", acmd
->name
->content
);
123 m
=getcommandlist(acmd
->searchtree
,acmdlist
,100);
125 if ( !acmdlist
[j
]->maxparams
&& controlpermitted( acmdlist
[j
]->level
, np
) ) {
126 char *help
=(char *)acmdlist
[j
]->ext
;
127 if ( help
&& help
[0] != '\0')
128 controlreply(np
, "%-10s: %s\n", acmdlist
[j
]->command
->content
, help
);
130 controlreply(np
, "%s\n", acmdlist
[j
]->command
->content
);
136 void regdisp( searchCmd
*cmd
, const char *name
, void *handler
, int level
, char *help
) {
137 addcommandexttotree(cmd
->outputtree
, name
, level
, 0, (CommandHandler
) handler
, (char *)help
);
140 void unregdisp( searchCmd
*cmd
, const char *name
, void *handler
) {
141 deletecommandfromtree(cmd
->outputtree
, name
, (CommandHandler
) handler
);
144 void *findcommandinlist( searchList
*sl
, char *name
){
146 if(strcmp(sl
->name
->content
,name
) == 0 ) {
154 const char *parseError
;
155 /* used for *_free functions that need to warn users of certain things
156 i.e. hitting too many users in a (kill) or (gline) */
157 nick
*senderNSExtern
;
160 searchCmdTree
=newcommandtree();
162 reg_nicksearch
= (searchCmd
*)registersearchcommand("nicksearch",NO_OPER
,&do_nicksearch
, printnick
);
163 reg_chansearch
= (searchCmd
*)registersearchcommand("chansearch",NO_OPER
,&do_chansearch
, printchannel
);
164 reg_usersearch
= (searchCmd
*)registersearchcommand("usersearch",NO_OPER
,&do_usersearch
, printuser
);
165 reg_whowassearch
= (searchCmd
*)registersearchcommand("whowassearch",NO_OPER
,&do_whowassearch
, printwhowas
);
167 /* Boolean operations */
168 registerglobalsearchterm("and",and_parse
, "usage: (and (X) (X))" );
169 registerglobalsearchterm("not",not_parse
, "usage: (not (X))");
170 registerglobalsearchterm("or",or_parse
, "usage: (or (X) (X))" );
172 registerglobalsearchterm("eq",eq_parse
, "usage: (eq (X) Y)");
174 registerglobalsearchterm("lt",lt_parse
, "usage: (lt (X) int)");
175 registerglobalsearchterm("gt",gt_parse
, "usage: (gt (X) int)");
177 /* String operations */
178 registerglobalsearchterm("match",match_parse
, "usage: (match (X) string)");
179 registerglobalsearchterm("regex",regex_parse
, "usage: (regex (X) string)");
180 registerglobalsearchterm("length",length_parse
, "usage: (length string)");
182 /* Nickname operations */
183 registersearchterm(reg_nicksearch
, "hostmask",hostmask_parse
, 0, "The user's nick!user@host; \"hostmask real\" returns nick!user@host\rreal");
184 registersearchterm(reg_whowassearch
, "hostmask",hostmask_parse
, 0, "The user's nick!user@host; \"hostmask real\" returns nick!user@host\rreal");
186 registersearchterm(reg_nicksearch
, "realname",realname_parse
, 0, "User's current realname"); /* nick only */
187 registersearchterm(reg_whowassearch
, "realname",realname_parse
, 0, "User's current realname"); /* nick only */
189 registersearchterm(reg_nicksearch
, "away",away_parse
, 0, "User's current away message");
190 registersearchterm(reg_whowassearch
, "away",away_parse
, 0, "User's current away message");
191 registersearchterm(reg_nicksearch
, "authname",authname_parse
, 0, "User's current authname or false");
192 registersearchterm(reg_whowassearch
, "authname",authname_parse
, 0, "User's current authname or false");
193 registersearchterm(reg_nicksearch
, "authts",authts_parse
, 0, "User's Auth timestamp");
194 registersearchterm(reg_whowassearch
, "authts",authts_parse
, 0, "User's Auth timestamp");
195 registersearchterm(reg_nicksearch
, "ident",ident_parse
, 0, "User's current ident");
196 registersearchterm(reg_whowassearch
, "ident",ident_parse
, 0, "User's current ident");
197 registersearchterm(reg_nicksearch
, "host",host_parse
, 0, "User's host, allow \"host real\" to match real host");
198 registersearchterm(reg_whowassearch
, "host",host_parse
, 0, "User's host, allow \"host real\" to match real host");
199 registersearchterm(reg_nicksearch
, "channel",channel_parse
, 0, "Valid Channel Name to match users against");
200 registersearchterm(reg_whowassearch
, "channel",channel_parse
, 0, "Valid Channel Name to match users against");
201 registersearchterm(reg_nicksearch
, "timestamp",timestamp_parse
, 0, "User's Timestamp");
202 registersearchterm(reg_whowassearch
, "timestamp",timestamp_parse
, 0, "User's Timestamp");
203 registersearchterm(reg_nicksearch
, "country",country_parse
, 0, "2 letter country code (data source is geoip)");
204 registersearchterm(reg_nicksearch
, "ip",ip_parse
, 0, "User's IP - ipv4 or ipv6 format as appropriate. Note: not 6to4");
205 registersearchterm(reg_whowassearch
, "ip",ip_parse
, 0, "User's IP - ipv4 or ipv6 format as appropriate. Note: not 6to4");
206 registersearchterm(reg_nicksearch
, "channels",channels_parse
, 0, "Channel Count"); /* nick only */
207 registersearchterm(reg_nicksearch
, "server",server_parse
, 0, "Server Name. Either (server string) or (match (server) string)");
208 registersearchterm(reg_whowassearch
, "server",server_parse
, 0, "Server Name. Either (server string) or (match (server) string)");
209 registersearchterm(reg_whowassearch
, "server",server_parse
, 0, "Server Name. Either (server string) or (match (server) string)");
210 registersearchterm(reg_nicksearch
, "authid",authid_parse
, 0, "User's Auth ID");
211 registersearchterm(reg_whowassearch
, "authid",authid_parse
, 0, "User's Auth ID");
212 registersearchterm(reg_nicksearch
, "cidr",cidr_parse
, 0, "CIDR matching");
213 registersearchterm(reg_whowassearch
, "cidr",cidr_parse
, 0, "CIDR matching");
214 registersearchterm(reg_nicksearch
, "ipvsix",ipv6_parse
, 0, "IPv6 user");
215 registersearchterm(reg_whowassearch
, "ipvsix",ipv6_parse
, 0, "IPv6 user");
216 registersearchterm(reg_nicksearch
, "message",message_parse
, 0, "Last message");
218 /* Whowas operations */
219 registersearchterm(reg_whowassearch
, "quit",quit_parse
, 0, "User quit");
220 registersearchterm(reg_whowassearch
, "killed",killed_parse
, 0, "User was killed");
221 registersearchterm(reg_whowassearch
, "renamed",renamed_parse
, 0, "User changed nick");
222 registersearchterm(reg_whowassearch
, "age",age_parse
, 0, "Whowas record age in seconds");
223 registersearchterm(reg_whowassearch
, "newnick",newnick_parse
, 0, "New nick (for rename whowas records)");
224 registersearchterm(reg_whowassearch
, "reason",reason_parse
, 0, "Quit/kill reason");
226 /* Channel operations */
227 registersearchterm(reg_chansearch
, "exists",exists_parse
, 0, "Returns if channel exists on network. Note: newserv may store data on empty channels"); /* channel only */
228 registersearchterm(reg_chansearch
, "services",services_parse
, 0, ""); /* channel only */
229 registersearchterm(reg_chansearch
, "size",size_parse
, 0, "Channel user count"); /* channel only */
230 registersearchterm(reg_chansearch
, "name",name_parse
, 0, "Channel Name"); /* channel only */
231 registersearchterm(reg_chansearch
, "topic",topic_parse
, 0, "Channel topic"); /* channel only */
232 registersearchterm(reg_chansearch
, "oppct",oppct_parse
, 0, "Percentage Opped"); /* channel only */
233 registersearchterm(reg_chansearch
, "cumodecount",cumodecount_parse
, 0, "Count of users with given channel modes"); /* channel only */
234 registersearchterm(reg_chansearch
, "cumodepct",cumodepct_parse
, 0, "Percentage of users with given channel modes"); /* channel only */
235 registersearchterm(reg_chansearch
, "uniquehostpct",hostpct_parse
, 0, "uniquehost percent"); /* channel only */
236 registersearchterm(reg_chansearch
, "authedpct",authedpct_parse
, 0, "Percentage of authed users"); /* channel only */
237 registersearchterm(reg_chansearch
, "kick",kick_parse
, 0, "KICK users channels in newsearch result. Note: evaluation order"); /* channel only */
239 /* Nickname / channel operations */
240 registersearchterm(reg_chansearch
, "modes",modes_parse
, 0, "Channel Modes");
241 registersearchterm(reg_nicksearch
, "modes",modes_parse
, 0, "User Modes");
242 registersearchterm(reg_whowassearch
, "modes",modes_parse
, 0, "User Modes");
243 registersearchterm(reg_chansearch
, "nick",nick_parse
, 0, "Nickname");
244 registersearchterm(reg_nicksearch
, "nick",nick_parse
, 0, "Nickname");
245 registersearchterm(reg_whowassearch
, "nick",nick_parse
, 0, "Nickname");
247 /* Kill / gline parameters */
248 registersearchterm(reg_chansearch
,"kill",kill_parse
, 0, "KILL users in newsearch result. Note: evaluation order");
249 registersearchterm(reg_chansearch
,"gline",gline_parse
, 0, "GLINE users in newsearch result. Note: evaluation order");
250 registersearchterm(reg_nicksearch
,"kill",kill_parse
, 0, "KILL users in newsearch result. Note: evaluation order");
251 registersearchterm(reg_nicksearch
,"gline",gline_parse
, 0, "GLINE users in newsearch result. Note: evaluation order");
252 registersearchterm(reg_whowassearch
,"gline",gline_parse
, 0, "GLINE users in newsearch result. Note: evaluation order");
254 /* Iteration functionality */
255 registerglobalsearchterm("any",any_parse
, "usage: any (generatorfn x) (fn ... (var x) ...)");
256 registerglobalsearchterm("all",all_parse
, "usage: all (generatorfn x) (fn ... (var x) ...)");
257 registerglobalsearchterm("var",var_parse
, "usage: var variable");
259 /* Iterable functions */
260 registersearchterm(reg_nicksearch
, "channeliter",channeliter_parse
, 0, "Channel Iterable function - usage: (any (channeliter x) (match (var x) #twilight*))"); /* nick only */
261 registersearchterm(reg_chansearch
, "nickiter",nickiter_parse
, 0, "Nick Iterable function - usage: (any (nickiter x) (match (var x) nickname*))"); /* channel only */
263 /* Functions that work on strings?! */
264 registersearchterm(reg_nicksearch
, "cumodes", cumodes_parse
, 0, "usage: (cumodes (var x) <modes>)");
265 registersearchterm(reg_chansearch
, "cumodes", cumodes_parse
, 0, "usage: (cumodes (var x) <modes>)");
267 /* Notice functionality */
268 registersearchterm(reg_chansearch
,"notice",notice_parse
, 0, "NOTICE users in newsearch result. Note: evaluation order");
269 registersearchterm(reg_nicksearch
,"notice",notice_parse
, 0, "NOTICE users in newsearch result. Note: evaluation order");
271 /* Nick output filters */
272 regdisp(reg_nicksearch
,"default",printnick
, 0, "");
273 regdisp(reg_nicksearch
,"channels",printnick_channels
, 0, "include channels in output");
275 /* Channel output filters */
276 regdisp(reg_chansearch
,"default",printchannel
, 0, "");
277 regdisp(reg_chansearch
,"topic",printchannel_topic
, 0, "display channel topics");
278 regdisp(reg_chansearch
,"services",printchannel_services
, 0, "display services on channels");
280 /* Nick output filters */
281 regdisp(reg_usersearch
,"default",printuser
, 0, "");
286 searchList
*sl
, *psl
;
288 Command
*cmdlist
[100];
295 n
=getcommandlist(searchCmdTree
,cmdlist
,100);
297 deregistersearchterm( (searchCmd
*)cmdlist
[i
]->handler
, psl
->name
->content
, psl
->cmd
);
300 freesstring(psl
->name
);
306 deregistersearchcommand( reg_nicksearch
);
307 deregistersearchcommand( reg_chansearch
);
308 deregistersearchcommand( reg_usersearch
);
309 destroycommandtree( searchCmdTree
);
312 void registerglobalsearchterm(char *term
, parseFunc parsefunc
, char *help
) {
314 Command
*cmdlist
[100];
315 searchList
*sl
= malloc(sizeof(searchList
));
317 Error("newsearch", ERR_ERROR
, "malloc failed: registerglobalsearchterm");
322 sl
->name
= getsstring(term
, NSMAX_COMMAND_LEN
);
325 int len
=strlen(help
);
326 sl
->help
=(char *)malloc(len
+1);
328 Error("newsearch", ERR_ERROR
, "malloc failed: registerglobalsearchterm");
331 strncpy(sl
->help
, help
, len
);
332 sl
->help
[len
] = '\0';
338 if ( globalterms
!= NULL
) {
339 sl
->next
= globalterms
;
343 n
=getcommandlist(searchCmdTree
,cmdlist
,100);
345 /* maxparams is set to 1 to indicate a global term */
346 /* access level is set to 0 for all global terms */
347 addcommandexttotree( ((searchCmd
*)cmdlist
[i
]->handler
)->searchtree
,term
, 0, 1, (CommandHandler
) parsefunc
, help
);
351 void deregisterglobalsearchterm(char *term
, parseFunc parsefunc
) {
353 Command
*cmdlist
[100];
354 searchList
*sl
, *psl
=NULL
;
356 for (sl
=globalterms
; sl
; sl
=sl
->next
) {
357 if ( strcmp( sl
->name
->content
, term
) == 0 ) {
365 psl
->next
= sl
->next
;
368 n
=getcommandlist(searchCmdTree
,cmdlist
,100);
370 deletecommandfromtree( ((searchCmd
*)cmdlist
[i
]->handler
)->searchtree
, term
, (CommandHandler
) parsefunc
);
372 freesstring(sl
->name
);
377 void registersearchterm(searchCmd
*cmd
, char *term
, parseFunc parsefunc
, int level
, char *help
) {
378 /* NOTE: global terms are added to the tree elsewhere as we set maxparams to 1 to indicate global */
379 addcommandexttotree(cmd
->searchtree
, term
, level
, 0, (CommandHandler
) parsefunc
, help
);
382 void deregistersearchterm(searchCmd
*cmd
, char *term
, parseFunc parsefunc
) {
383 /* NOTE: global terms are removed from the tree within deregisterglobalsearchterm */
384 deletecommandfromtree(cmd
->searchtree
, term
, (CommandHandler
) parsefunc
);
387 static void controlwallwrapper(int level
, char *format
, ...) __attribute__ ((format (printf
, 2, 3)));
388 static void controlwallwrapper(int level
, char *format
, ...) {
392 va_start(ap
, format
);
393 vsnprintf(buf
, sizeof(buf
), format
, ap
);
394 controlwall(NO_OPER
, level
, "%s", buf
);
398 int parseopts(int cargc
, char **cargv
, int *arg
, int *limit
, void **subset
, void *display
, CommandTree
*sl
, replyFunc reply
, void *sender
) {
401 struct irc_in_addr sin
; unsigned char bits
;
403 if (*cargv
[0] == '-') {
407 for (ch
=cargv
[0]+1;*ch
;ch
++) {
411 reply(sender
,"Error: -l switch requires an argument (for help, see help <searchcmd>)");
414 *limit
=strtoul(cargv
[(*arg
)++],NULL
,10);
419 reply(sender
,"Error: -d switch requires an argument (for help, see help <searchcmd>)");
422 cmd
=findcommandintree(sl
, cargv
[*arg
],1);
424 reply(sender
,"Error: unknown output format %s (for help, see help <searchcmd>)",cargv
[*arg
]);
427 if ( !controlpermitted( cmd
->level
, sender
) ) {
428 reply(sender
,"Error: Access Denied for output format %s (for help, see help <searchcmd>)", cargv
[*arg
]);
431 *((void **)display
)=(void *)cmd
->handler
;
436 if (subset
== NULL
) {
437 reply(sender
,"Error: -s switch not supported for this search.");
441 reply(sender
,"Error: -s switch requires an argument (for help, see help <searchcmd>)");
444 if (ipmask_parse(cargv
[*arg
], &sin
, &bits
) == 0) {
445 reply(sender
, "Error: Invalid CIDR mask supplied (for help, see help <searchcmd>)");
448 *subset
= (void *)refnode(iptree
, &sin
, bits
);
453 reply(sender
,"Unrecognised flag -%c. (for help, see help <searchcmd>)",*ch
);
462 void newsearch_ctxinit(searchCtx
*ctx
, searchParseFunc searchfn
, replyFunc replyfn
, wallFunc wallfn
, void *arg
, searchCmd
*cmd
, nick
*np
, void *displayfn
, int limit
, array
*targets
) {
463 memset(ctx
, 0, sizeof(searchCtx
));
465 ctx
->reply
= replyfn
;
467 ctx
->parser
= searchfn
;
469 ctx
->searchcmd
= cmd
;
472 ctx
->targets
= targets
;
473 ctx
->displayfn
= displayfn
;
476 int do_nicksearch_real(replyFunc reply
, wallFunc wall
, void *source
, int cargc
, char **cargv
) {
477 nick
*sender
= source
;
480 NickDisplayFunc display
=defaultnickfn
;
485 reply( sender
, "Usage: [flags] <criteria>");
486 reply( sender
, "For help, see help nicksearch");
490 ret
= parseopts(cargc
, cargv
, &arg
, &limit
, NULL
, (void *)&display
, reg_nicksearch
->outputtree
, reply
, sender
);
495 reply(sender
,"No search terms - aborting.");
500 rejoinline(cargv
[arg
],cargc
-arg
);
503 tree
= parse_string(reg_nicksearch
, cargv
[arg
]);
505 displaystrerror(reply
, sender
, cargv
[arg
]);
509 ast_nicksearch(tree
->root
, reply
, sender
, wall
, display
, NULL
, NULL
, limit
, NULL
);
516 int do_nicksearch(void *source
, int cargc
, char **cargv
) {
517 return do_nicksearch_real(controlreply
, controlwallwrapper
, source
, cargc
, cargv
);
520 void nicksearch_exe(struct searchNode
*search
, searchCtx
*ctx
) {
523 unsigned int cmarker
;
524 unsigned int tchans
=0,uchans
=0;
526 nick
*np
, *sender
= ctx
->sender
;
527 senderNSExtern
= sender
;
528 NickDisplayFunc display
= ctx
->displayfn
;
529 int limit
= ctx
->limit
;
531 /* Get a marker value to mark "seen" channels for unique count */
532 cmarker
=nextchanmarker();
534 /* The top-level node needs to return a BOOL */
535 search
=coerceNode(ctx
, search
, RETURNTYPE_BOOL
);
537 for (i
=0;i
<NICKHASHSIZE
;i
++) {
538 for (np
=nicktable
[i
], k
= 0;ctx
->targets
? (k
< ctx
->targets
->cursi
) : (np
!= NULL
);np
=np
->next
, k
++) {
540 np
= ((nick
**)ctx
->targets
->content
)[k
];
545 if ((search
->exe
)(ctx
, search
, np
)) {
546 /* Add total channels */
547 tchans
+= np
->channels
->cursi
;
549 /* Check channels for uniqueness */
550 cs
=(channel
**)np
->channels
->content
;
551 for (j
=0;j
<np
->channels
->cursi
;j
++) {
552 if (cs
[j
]->index
->marker
!= cmarker
) {
553 cs
[j
]->index
->marker
=cmarker
;
559 display(ctx
, sender
, np
);
562 ctx
->reply(sender
, "--- More than %d matches, skipping the rest",limit
);
571 ctx
->reply(sender
,"--- End of list: %d matches; users were on %u channels (%u unique, %.1f average clones)",
572 matches
, tchans
, uchans
, (float)tchans
/uchans
);
575 int do_whowassearch_real(replyFunc reply
, wallFunc wall
, void *source
, int cargc
, char **cargv
) {
576 nick
*sender
= source
;
579 WhowasDisplayFunc display
=defaultwhowasfn
;
584 reply( sender
, "Usage: [flags] <criteria>");
585 reply( sender
, "For help, see help whowassearch");
589 ret
= parseopts(cargc
, cargv
, &arg
, &limit
, NULL
, (void *)&display
, reg_whowassearch
->outputtree
, reply
, sender
);
594 reply(sender
,"No search terms - aborting.");
599 rejoinline(cargv
[arg
],cargc
-arg
);
602 tree
= parse_string(reg_whowassearch
, cargv
[arg
]);
604 displaystrerror(reply
, sender
, cargv
[arg
]);
608 ast_whowassearch(tree
->root
, reply
, sender
, wall
, display
, NULL
, NULL
, limit
, NULL
);
615 int do_whowassearch(void *source
, int cargc
, char **cargv
) {
616 return do_whowassearch_real(controlreply
, controlwallwrapper
, source
, cargc
, cargv
);
619 void whowassearch_exe(struct searchNode
*search
, searchCtx
*ctx
) {
622 nick
*sender
= ctx
->sender
;
623 senderNSExtern
= sender
;
624 WhowasDisplayFunc display
= ctx
->displayfn
;
625 int limit
= ctx
->limit
;
627 assert(!ctx
->targets
);
629 /* The top-level node needs to return a BOOL */
630 search
=coerceNode(ctx
, search
, RETURNTYPE_BOOL
);
632 for (i
= whowasoffset
; i
< whowasoffset
+ WW_MAXENTRIES
; i
++) {
633 ww
= &whowasrecs
[i
% WW_MAXENTRIES
];
635 if (ww
->type
== WHOWAS_UNUSED
)
638 /* Note: We're passing the nick to the filter function. The original
639 * whowas record is in the nick's ->next field. */
640 if ((search
->exe
)(ctx
, search
, &ww
->nick
)) {
642 display(ctx
, sender
, ww
);
645 ctx
->reply(sender
, "--- More than %d matches, skipping the rest",limit
);
650 ctx
->reply(sender
,"--- End of list: %d matches", matches
);
653 int do_chansearch_real(replyFunc reply
, wallFunc wall
, void *source
, int cargc
, char **cargv
) {
654 nick
*sender
= source
;
657 ChanDisplayFunc display
=defaultchanfn
;
662 reply( sender
, "Usage: [flags] <criteria>");
663 reply( sender
, "For help, see help chansearch");
667 ret
= parseopts(cargc
, cargv
, &arg
, &limit
, NULL
, (void *)&display
, reg_chansearch
->outputtree
, reply
, sender
);
672 reply(sender
,"No search terms - aborting.");
677 rejoinline(cargv
[arg
],cargc
-arg
);
680 tree
= parse_string(reg_chansearch
, cargv
[arg
]);
682 displaystrerror(reply
, sender
, cargv
[arg
]);
686 ast_chansearch(tree
->root
, reply
, sender
, wall
, display
, NULL
, NULL
, limit
, NULL
);
693 int do_chansearch(void *source
, int cargc
, char **cargv
) {
694 return do_chansearch_real(controlreply
, controlwallwrapper
, source
, cargc
, cargv
);
697 void chansearch_exe(struct searchNode
*search
, searchCtx
*ctx
) {
701 nick
*sender
= ctx
->sender
;
702 senderNSExtern
= sender
;
703 ChanDisplayFunc display
= ctx
->displayfn
;
704 int limit
= ctx
->limit
;
706 assert(!ctx
->targets
);
708 search
=coerceNode(ctx
, search
, RETURNTYPE_BOOL
);
710 for (i
=0;i
<CHANNELHASHSIZE
;i
++) {
711 for (cip
=chantable
[i
];cip
;cip
=cip
->next
) {
712 if ((search
->exe
)(ctx
, search
, cip
)) {
714 display(ctx
, sender
, cip
);
716 ctx
->reply(sender
, "--- More than %d matches, skipping the rest",limit
);
722 ctx
->reply(sender
,"--- End of list: %d matches", matches
);
725 int do_usersearch_real(replyFunc reply
, wallFunc wall
, void *source
, int cargc
, char **cargv
) {
726 nick
*sender
= source
;
729 UserDisplayFunc display
=defaultuserfn
;
734 reply( sender
, "Usage: [flags] <criteria>");
735 reply( sender
, "For help, see help usersearch");
739 ret
= parseopts(cargc
, cargv
, &arg
, &limit
, NULL
, (void *)&display
, reg_usersearch
->outputtree
, reply
, sender
);
744 reply(sender
,"No search terms - aborting.");
749 rejoinline(cargv
[arg
],cargc
-arg
);
752 tree
= parse_string(reg_usersearch
, cargv
[arg
]);
754 displaystrerror(reply
, sender
, cargv
[arg
]);
758 ast_usersearch(tree
->root
, reply
, sender
, wall
, display
, NULL
, NULL
, limit
, NULL
);
765 int do_usersearch(void *source
, int cargc
, char **cargv
) {
766 return do_usersearch_real(controlreply
, controlwallwrapper
, source
, cargc
, cargv
);
769 void usersearch_exe(struct searchNode
*search
, searchCtx
*ctx
) {
773 nick
*sender
= ctx
->sender
;
774 int limit
= ctx
->limit
;
775 UserDisplayFunc display
= ctx
->displayfn
;
776 senderNSExtern
= sender
;
778 assert(!ctx
->targets
);
780 search
=coerceNode(ctx
, search
, RETURNTYPE_BOOL
);
782 for (i
=0;i
<AUTHNAMEHASHSIZE
;i
++) {
783 for (aup
=authnametable
[i
];aup
;aup
=aup
->next
) {
784 if ((search
->exe
)(ctx
, search
, aup
)) {
786 display(ctx
, sender
, aup
);
788 ctx
->reply(sender
, "--- More than %d matches, skipping the rest",limit
);
794 ctx
->reply(sender
,"--- End of list: %d matches", matches
);
797 /* Free a coerce node */
798 void free_coerce(searchCtx
*ctx
, struct searchNode
*thenode
) {
799 struct coercedata
*cd
=thenode
->localdata
;
801 cd
->child
->free(ctx
, cd
->child
);
802 free(thenode
->localdata
);
806 /* Free a coerce node with a stringbuf allocated */
807 void free_coercestring(searchCtx
*ctx
, struct searchNode
*thenode
) {
808 free(((struct coercedata
*)thenode
->localdata
)->u
.stringbuf
);
809 free_coerce(ctx
, thenode
);
812 /* exe_tostr_null: return the constant string */
813 void *exe_tostr_null(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
814 struct coercedata
*cd
=thenode
->localdata
;
816 return cd
->u
.stringbuf
;
819 /* exe_val_null: return the constant value */
820 void *exe_val_null(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
821 struct coercedata
*cd
=thenode
->localdata
;
823 return (void *)cd
->u
.val
;
826 /* Lots of very dull type conversion functions */
827 void *exe_inttostr(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
828 struct coercedata
*cd
=thenode
->localdata
;
830 sprintf(cd
->u
.stringbuf
, "%lu", (unsigned long)(cd
->child
->exe
)(ctx
, cd
->child
, theinput
));
832 return cd
->u
.stringbuf
;
835 void *exe_booltostr(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
836 struct coercedata
*cd
=thenode
->localdata
;
838 if ((cd
->child
->exe
)(ctx
, cd
->child
, theinput
)) {
839 sprintf(cd
->u
.stringbuf
,"1");
841 cd
->u
.stringbuf
[0]='\0';
844 return cd
->u
.stringbuf
;
847 void *exe_strtoint(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
848 struct coercedata
*cd
=thenode
->localdata
;
850 return (void *)strtoul((cd
->child
->exe
)(ctx
,cd
->child
,theinput
),NULL
,10);
853 void *exe_booltoint(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
854 struct coercedata
*cd
=thenode
->localdata
;
856 /* Don't need to do anything */
857 return (cd
->child
->exe
)(ctx
, cd
->child
, theinput
);
860 void *exe_strtobool(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
861 struct coercedata
*cd
=thenode
->localdata
;
862 char *ch
=(cd
->child
->exe
)(ctx
, cd
->child
, theinput
);
864 if (!ch
|| *ch
=='\0' || (*ch
=='0' && ch
[1]=='\0')) {
871 void *exe_inttobool(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
872 struct coercedata
*cd
=thenode
->localdata
;
874 if ((cd
->child
->exe
)(ctx
, cd
->child
, theinput
)) {
881 struct searchNode
*coerceNode(searchCtx
*ctx
, struct searchNode
*thenode
, int type
) {
882 struct searchNode
*anode
;
883 struct coercedata
*cd
;
885 /* You can't coerce a NULL */
889 /* No effort required to coerce to the same type */
890 if (type
==(thenode
->returntype
& RETURNTYPE_TYPE
))
893 anode
=(struct searchNode
*)malloc(sizeof(struct searchNode
));
894 anode
->localdata
=cd
=(struct coercedata
*)malloc(sizeof(struct coercedata
));
896 anode
->returntype
=type
; /* We'll return what they want, always */
897 anode
->free
=free_coerce
;
900 case RETURNTYPE_STRING
:
901 /* For a string we'll need a buffer */
902 /* A 64-bit number prints out to 20 digits, this leaves some slack */
903 cd
->u
.stringbuf
=malloc(25);
904 anode
->free
=free_coercestring
;
906 switch(thenode
->returntype
& RETURNTYPE_TYPE
) {
909 if (thenode
->returntype
& RETURNTYPE_CONST
) {
910 /* Constant node: sort it out now */
911 sprintf(cd
->u
.stringbuf
, "%lu", (unsigned long)thenode
->exe(ctx
, thenode
, NULL
));
912 anode
->exe
=exe_tostr_null
;
913 anode
->returntype
|= RETURNTYPE_CONST
;
916 anode
->exe
=exe_inttostr
;
920 case RETURNTYPE_BOOL
:
921 if (thenode
->returntype
& RETURNTYPE_CONST
) {
922 /* Constant bool value */
923 if (thenode
->exe(ctx
, thenode
,NULL
)) {
925 sprintf(cd
->u
.stringbuf
, "1");
927 cd
->u
.stringbuf
[0] = '\0';
929 anode
->exe
=exe_tostr_null
;
930 anode
->returntype
|= RETURNTYPE_CONST
;
932 /* Variable bool value */
933 anode
->exe
=exe_booltostr
;
941 switch (thenode
->returntype
& RETURNTYPE_TYPE
) {
942 case RETURNTYPE_STRING
:
943 if (thenode
->returntype
& RETURNTYPE_CONST
) {
944 cd
->u
.val
=strtoul((thenode
->exe
)(ctx
, thenode
, NULL
), NULL
, 10);
945 anode
->exe
=exe_val_null
;
946 anode
->returntype
|= RETURNTYPE_CONST
;
948 anode
->exe
=exe_strtoint
;
953 case RETURNTYPE_BOOL
:
954 if (thenode
->returntype
& RETURNTYPE_CONST
) {
955 if ((thenode
->exe
)(ctx
, thenode
,NULL
))
960 anode
->exe
=exe_val_null
;
961 anode
->returntype
|= RETURNTYPE_CONST
;
963 anode
->exe
=exe_booltoint
;
970 case RETURNTYPE_BOOL
:
972 switch (thenode
->returntype
& RETURNTYPE_TYPE
) {
973 case RETURNTYPE_STRING
:
974 if (thenode
->returntype
& RETURNTYPE_CONST
) {
975 char *rv
=(char *)((thenode
->exe
)(ctx
, thenode
, NULL
));
976 if (!rv
|| *rv
=='\0' || (*rv
=='0' && rv
[1]=='\0'))
981 anode
->exe
=exe_val_null
;
982 anode
->returntype
|= RETURNTYPE_CONST
;
984 anode
->exe
=exe_strtobool
;
990 if (thenode
->returntype
& RETURNTYPE_CONST
) {
991 if ((thenode
->exe
)(ctx
, thenode
,NULL
))
996 anode
->exe
=exe_val_null
;
997 anode
->returntype
|= RETURNTYPE_CONST
;
999 anode
->exe
=exe_inttobool
;
1009 /* Literals always return constant strings... */
1010 void *literal_exe(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
1011 if (thenode
->localdata
)
1012 return ((sstring
*)thenode
->localdata
)->content
;
1017 void literal_free(searchCtx
*ctx
, struct searchNode
*thenode
) {
1018 freesstring(thenode
->localdata
);
1022 void nssnprintf(char *buf
, size_t size
, const char *format
, nick
*np
) {
1035 for(p
=format
;*p
;p
++) {
1037 if(!sbaddchar(&b
, *p
))
1045 if(!sbaddchar(&b
, *p
))
1053 c
= np
->nick
; break;
1055 c
= np
->ident
; break;
1057 c
= np
->host
->name
->content
; break;
1059 snprintf(hostbuf
, sizeof(hostbuf
), "%s", IPtostr(np
->ipaddress
));
1063 snprintf(hostbuf
, sizeof(hostbuf
), "%s!%s@%s", np
->nick
, np
->ident
, IPtostr(np
->ipaddress
));
1067 c
= "(bad format specifier)";
1070 if(!sbaddstr(&b
, c
))
1082 static char *var_tochar(searchCtx
*ctx
, char *arg
, searchNode
**variable
) {
1083 *variable
= ctx
->parser(ctx
, arg
);
1084 if (!(*variable
= coerceNode(ctx
, *variable
, RETURNTYPE_STRING
)))
1087 if(!((*variable
)->returntype
& RETURNTYPE_CONST
)) {
1088 parseError
= "only constant variables allowed";
1089 ((*variable
)->free
)(ctx
, *variable
);
1093 return (char *)((*variable
)->exe
)(ctx
, *variable
, NULL
);
1096 void free_val_null(searchCtx
*ctx
, struct searchNode
*thenode
) {
1099 struct searchVariable
*var_register(searchCtx
*ctx
, char *arg
, int type
) {
1100 searchNode
*variable
;
1101 struct searchVariable
*us
;
1105 if(ctx
->lastvar
>= MAX_VARIABLES
) {
1106 parseError
= "Maximum number of variables reached";
1110 us
= &ctx
->vars
[ctx
->lastvar
];
1112 var
= var_tochar(ctx
, arg
, &variable
);
1116 strlcpy(us
->name
, var
, sizeof(us
->name
));
1117 (variable
->free
)(ctx
, variable
);
1119 for(i
=0;i
<ctx
->lastvar
;i
++) {
1120 if(!strcmp(us
->name
, ctx
->vars
[i
].name
)) {
1121 parseError
= "variable name already in use";
1127 us
->data
.returntype
= type
;
1128 us
->data
.localdata
= &us
->cdata
;
1129 us
->data
.exe
= exe_val_null
;
1130 us
->data
.free
= free_val_null
;
1132 us
->cdata
.child
= NULL
;
1136 searchNode
*var_get(searchCtx
*ctx
, char *arg
) {
1137 searchNode
*variable
, *found
= NULL
;
1139 char *var
= var_tochar(ctx
, arg
, &variable
);
1143 for(i
=0;i
<ctx
->lastvar
;i
++) {
1144 if(!strcmp(var
, ctx
->vars
[i
].name
)) {
1145 found
= &ctx
->vars
[i
].data
;
1149 (variable
->free
)(ctx
, variable
);
1152 parseError
= "variable not found";
1156 void var_setstr(struct searchVariable
*v
, char *data
) {
1157 v
->cdata
.u
.stringbuf
= data
;
1160 void displaystrerror(replyFunc reply
, nick
*np
, const char *input
) {
1163 if((parseStrErrorPos
>= 0) && (parseStrErrorPos
< sizeof(buf
) - 3)) {
1166 for(i
=0;i
<parseStrErrorPos
;i
++)
1172 reply(np
, "%s", input
);
1173 reply(np
, "%s", buf
);
1176 reply(np
, "Parse error: %s", parseStrError
);
1179 struct searchNode
*argtoconststr(char *command
, searchCtx
*ctx
, char *arg
, char **p
) {
1180 struct searchNode
*c
;
1181 static char errorbuf
[512];
1183 c
= ctx
->parser(ctx
, arg
);
1184 if (!(c
= coerceNode(ctx
, c
, RETURNTYPE_STRING
))) {
1185 snprintf(errorbuf
, sizeof(errorbuf
), "%s: unable to coerce argument to string", command
);
1186 parseError
= errorbuf
;
1190 if (!(c
->returntype
& RETURNTYPE_CONST
)) {
1191 snprintf(errorbuf
, sizeof(errorbuf
), "%s: constant argument required", command
);
1192 parseError
= errorbuf
;
1198 *p
= (char *)(c
->exe
)(ctx
, c
, NULL
);