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"); /* nick only */
200 registersearchterm(reg_nicksearch
, "timestamp",timestamp_parse
, 0, "User's Timestamp");
201 registersearchterm(reg_whowassearch
, "timestamp",timestamp_parse
, 0, "User's Timestamp");
202 registersearchterm(reg_nicksearch
, "country",country_parse
, 0, "2 letter country code (data source is geoip)");
203 registersearchterm(reg_nicksearch
, "ip",ip_parse
, 0, "User's IP - ipv4 or ipv6 format as appropriate. Note: not 6to4");
204 registersearchterm(reg_whowassearch
, "ip",ip_parse
, 0, "User's IP - ipv4 or ipv6 format as appropriate. Note: not 6to4");
205 registersearchterm(reg_nicksearch
, "channels",channels_parse
, 0, "Channel Count"); /* nick only */
206 registersearchterm(reg_nicksearch
, "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_whowassearch
, "server",server_parse
, 0, "Server Name. Either (server string) or (match (server) string)");
209 registersearchterm(reg_nicksearch
, "authid",authid_parse
, 0, "User's Auth ID");
210 registersearchterm(reg_whowassearch
, "authid",authid_parse
, 0, "User's Auth ID");
211 registersearchterm(reg_nicksearch
, "cidr",cidr_parse
, 0, "CIDR matching");
212 registersearchterm(reg_whowassearch
, "cidr",cidr_parse
, 0, "CIDR matching");
213 registersearchterm(reg_nicksearch
, "ipvsix",ipv6_parse
, 0, "IPv6 user");
214 registersearchterm(reg_whowassearch
, "ipvsix",ipv6_parse
, 0, "IPv6 user");
215 registersearchterm(reg_nicksearch
, "message",message_parse
, 0, "Last message");
217 /* Whowas operations */
218 registersearchterm(reg_whowassearch
, "quit",quit_parse
, 0, "User quit");
219 registersearchterm(reg_whowassearch
, "killed",killed_parse
, 0, "User was killed");
220 registersearchterm(reg_whowassearch
, "renamed",renamed_parse
, 0, "User changed nick");
221 registersearchterm(reg_whowassearch
, "age",age_parse
, 0, "Whowas record age in seconds");
222 registersearchterm(reg_whowassearch
, "newnick",newnick_parse
, 0, "New nick (for rename whowas records)");
223 registersearchterm(reg_whowassearch
, "reason",reason_parse
, 0, "Quit/kill reason");
225 /* Channel operations */
226 registersearchterm(reg_chansearch
, "exists",exists_parse
, 0, "Returns if channel exists on network. Note: newserv may store data on empty channels"); /* channel only */
227 registersearchterm(reg_chansearch
, "services",services_parse
, 0, ""); /* channel only */
228 registersearchterm(reg_chansearch
, "size",size_parse
, 0, "Channel user count"); /* channel only */
229 registersearchterm(reg_chansearch
, "name",name_parse
, 0, "Channel Name"); /* channel only */
230 registersearchterm(reg_chansearch
, "topic",topic_parse
, 0, "Channel topic"); /* channel only */
231 registersearchterm(reg_chansearch
, "oppct",oppct_parse
, 0, "Percentage Opped"); /* channel only */
232 registersearchterm(reg_chansearch
, "cumodecount",cumodecount_parse
, 0, "Count of users with given channel modes"); /* channel only */
233 registersearchterm(reg_chansearch
, "cumodepct",cumodepct_parse
, 0, "Percentage of users with given channel modes"); /* channel only */
234 registersearchterm(reg_chansearch
, "uniquehostpct",hostpct_parse
, 0, "uniquehost percent"); /* channel only */
235 registersearchterm(reg_chansearch
, "authedpct",authedpct_parse
, 0, "Percentage of authed users"); /* channel only */
236 registersearchterm(reg_chansearch
, "kick",kick_parse
, 0, "KICK users channels in newsearch result. Note: evaluation order"); /* channel only */
238 /* Nickname / channel operations */
239 registersearchterm(reg_chansearch
, "modes",modes_parse
, 0, "Channel Modes");
240 registersearchterm(reg_nicksearch
, "modes",modes_parse
, 0, "User Modes");
241 registersearchterm(reg_whowassearch
, "modes",modes_parse
, 0, "User Modes");
242 registersearchterm(reg_chansearch
, "nick",nick_parse
, 0, "Nickname");
243 registersearchterm(reg_nicksearch
, "nick",nick_parse
, 0, "Nickname");
244 registersearchterm(reg_whowassearch
, "nick",nick_parse
, 0, "Nickname");
246 /* Kill / gline parameters */
247 registersearchterm(reg_chansearch
,"kill",kill_parse
, 0, "KILL users in newsearch result. Note: evaluation order");
248 registersearchterm(reg_chansearch
,"gline",gline_parse
, 0, "GLINE users in newsearch result. Note: evaluation order");
249 registersearchterm(reg_nicksearch
,"kill",kill_parse
, 0, "KILL users in newsearch result. Note: evaluation order");
250 registersearchterm(reg_nicksearch
,"gline",gline_parse
, 0, "GLINE users in newsearch result. Note: evaluation order");
251 registersearchterm(reg_whowassearch
,"gline",gline_parse
, 0, "GLINE users in newsearch result. Note: evaluation order");
253 /* Iteration functionality */
254 registerglobalsearchterm("any",any_parse
, "usage: any (generatorfn x) (fn ... (var x) ...)");
255 registerglobalsearchterm("all",all_parse
, "usage: all (generatorfn x) (fn ... (var x) ...)");
256 registerglobalsearchterm("var",var_parse
, "usage: var variable");
258 /* Iterable functions */
259 registersearchterm(reg_nicksearch
, "channeliter",channeliter_parse
, 0, "Channel Iterable function - usage: (any (channeliter x) (match (var x) #twilight*))"); /* nick only */
260 registersearchterm(reg_chansearch
, "nickiter",nickiter_parse
, 0, "Nick Iterable function - usage: (any (nickiter x) (match (var x) nickname*))"); /* channel only */
262 /* Functions that work on strings?! */
263 registersearchterm(reg_nicksearch
, "cumodes", cumodes_parse
, 0, "usage: (cumodes (var x) <modes>)");
264 registersearchterm(reg_chansearch
, "cumodes", cumodes_parse
, 0, "usage: (cumodes (var x) <modes>)");
266 /* Notice functionality */
267 registersearchterm(reg_chansearch
,"notice",notice_parse
, 0, "NOTICE users in newsearch result. Note: evaluation order");
268 registersearchterm(reg_nicksearch
,"notice",notice_parse
, 0, "NOTICE users in newsearch result. Note: evaluation order");
270 /* Nick output filters */
271 regdisp(reg_nicksearch
,"default",printnick
, 0, "");
272 regdisp(reg_nicksearch
,"channels",printnick_channels
, 0, "include channels in output");
274 /* Channel output filters */
275 regdisp(reg_chansearch
,"default",printchannel
, 0, "");
276 regdisp(reg_chansearch
,"topic",printchannel_topic
, 0, "display channel topics");
277 regdisp(reg_chansearch
,"services",printchannel_services
, 0, "display services on channels");
279 /* Nick output filters */
280 regdisp(reg_usersearch
,"default",printuser
, 0, "");
285 searchList
*sl
, *psl
;
287 Command
*cmdlist
[100];
294 n
=getcommandlist(searchCmdTree
,cmdlist
,100);
296 deregistersearchterm( (searchCmd
*)cmdlist
[i
]->handler
, psl
->name
->content
, psl
->cmd
);
299 freesstring(psl
->name
);
305 deregistersearchcommand( reg_nicksearch
);
306 deregistersearchcommand( reg_chansearch
);
307 deregistersearchcommand( reg_usersearch
);
308 destroycommandtree( searchCmdTree
);
311 void registerglobalsearchterm(char *term
, parseFunc parsefunc
, char *help
) {
313 Command
*cmdlist
[100];
314 searchList
*sl
= malloc(sizeof(searchList
));
316 Error("newsearch", ERR_ERROR
, "malloc failed: registerglobalsearchterm");
321 sl
->name
= getsstring(term
, NSMAX_COMMAND_LEN
);
324 int len
=strlen(help
);
325 sl
->help
=(char *)malloc(len
+1);
327 Error("newsearch", ERR_ERROR
, "malloc failed: registerglobalsearchterm");
330 strncpy(sl
->help
, help
, len
);
331 sl
->help
[len
] = '\0';
337 if ( globalterms
!= NULL
) {
338 sl
->next
= globalterms
;
342 n
=getcommandlist(searchCmdTree
,cmdlist
,100);
344 /* maxparams is set to 1 to indicate a global term */
345 /* access level is set to 0 for all global terms */
346 addcommandexttotree( ((searchCmd
*)cmdlist
[i
]->handler
)->searchtree
,term
, 0, 1, (CommandHandler
) parsefunc
, help
);
350 void deregisterglobalsearchterm(char *term
, parseFunc parsefunc
) {
352 Command
*cmdlist
[100];
353 searchList
*sl
, *psl
=NULL
;
355 for (sl
=globalterms
; sl
; sl
=sl
->next
) {
356 if ( strcmp( sl
->name
->content
, term
) == 0 ) {
364 psl
->next
= sl
->next
;
367 n
=getcommandlist(searchCmdTree
,cmdlist
,100);
369 deletecommandfromtree( ((searchCmd
*)cmdlist
[i
]->handler
)->searchtree
, term
, (CommandHandler
) parsefunc
);
371 freesstring(sl
->name
);
376 void registersearchterm(searchCmd
*cmd
, char *term
, parseFunc parsefunc
, int level
, char *help
) {
377 /* NOTE: global terms are added to the tree elsewhere as we set maxparams to 1 to indicate global */
378 addcommandexttotree(cmd
->searchtree
, term
, level
, 0, (CommandHandler
) parsefunc
, help
);
381 void deregistersearchterm(searchCmd
*cmd
, char *term
, parseFunc parsefunc
) {
382 /* NOTE: global terms are removed from the tree within deregisterglobalsearchterm */
383 deletecommandfromtree(cmd
->searchtree
, term
, (CommandHandler
) parsefunc
);
386 static void controlwallwrapper(int level
, char *format
, ...) __attribute__ ((format (printf
, 2, 3)));
387 static void controlwallwrapper(int level
, char *format
, ...) {
391 va_start(ap
, format
);
392 vsnprintf(buf
, sizeof(buf
), format
, ap
);
393 controlwall(NO_OPER
, level
, "%s", buf
);
397 int parseopts(int cargc
, char **cargv
, int *arg
, int *limit
, void **subset
, void *display
, CommandTree
*sl
, replyFunc reply
, void *sender
) {
400 struct irc_in_addr sin
; unsigned char bits
;
402 if (*cargv
[0] == '-') {
406 for (ch
=cargv
[0]+1;*ch
;ch
++) {
410 reply(sender
,"Error: -l switch requires an argument (for help, see help <searchcmd>)");
413 *limit
=strtoul(cargv
[(*arg
)++],NULL
,10);
418 reply(sender
,"Error: -d switch requires an argument (for help, see help <searchcmd>)");
421 cmd
=findcommandintree(sl
, cargv
[*arg
],1);
423 reply(sender
,"Error: unknown output format %s (for help, see help <searchcmd>)",cargv
[*arg
]);
426 if ( !controlpermitted( cmd
->level
, sender
) ) {
427 reply(sender
,"Error: Access Denied for output format %s (for help, see help <searchcmd>)", cargv
[*arg
]);
430 *((void **)display
)=(void *)cmd
->handler
;
435 if (subset
== NULL
) {
436 reply(sender
,"Error: -s switch not supported for this search.");
440 reply(sender
,"Error: -s switch requires an argument (for help, see help <searchcmd>)");
443 if (ipmask_parse(cargv
[*arg
], &sin
, &bits
) == 0) {
444 reply(sender
, "Error: Invalid CIDR mask supplied (for help, see help <searchcmd>)");
447 *subset
= (void *)refnode(iptree
, &sin
, bits
);
452 reply(sender
,"Unrecognised flag -%c. (for help, see help <searchcmd>)",*ch
);
461 void newsearch_ctxinit(searchCtx
*ctx
, searchParseFunc searchfn
, replyFunc replyfn
, wallFunc wallfn
, void *arg
, searchCmd
*cmd
, nick
*np
, void *displayfn
, int limit
, array
*targets
) {
462 memset(ctx
, 0, sizeof(searchCtx
));
464 ctx
->reply
= replyfn
;
466 ctx
->parser
= searchfn
;
468 ctx
->searchcmd
= cmd
;
471 ctx
->targets
= targets
;
472 ctx
->displayfn
= displayfn
;
475 int do_nicksearch_real(replyFunc reply
, wallFunc wall
, void *source
, int cargc
, char **cargv
) {
476 nick
*sender
= source
;
479 NickDisplayFunc display
=defaultnickfn
;
484 reply( sender
, "Usage: [flags] <criteria>");
485 reply( sender
, "For help, see help nicksearch");
489 ret
= parseopts(cargc
, cargv
, &arg
, &limit
, NULL
, (void *)&display
, reg_nicksearch
->outputtree
, reply
, sender
);
494 reply(sender
,"No search terms - aborting.");
499 rejoinline(cargv
[arg
],cargc
-arg
);
502 tree
= parse_string(reg_nicksearch
, cargv
[arg
]);
504 displaystrerror(reply
, sender
, cargv
[arg
]);
508 ast_nicksearch(tree
->root
, reply
, sender
, wall
, display
, NULL
, NULL
, limit
, NULL
);
515 int do_nicksearch(void *source
, int cargc
, char **cargv
) {
516 return do_nicksearch_real(controlreply
, controlwallwrapper
, source
, cargc
, cargv
);
519 void nicksearch_exe(struct searchNode
*search
, searchCtx
*ctx
) {
522 unsigned int cmarker
;
523 unsigned int tchans
=0,uchans
=0;
525 nick
*np
, *sender
= ctx
->sender
;
526 senderNSExtern
= sender
;
527 NickDisplayFunc display
= ctx
->displayfn
;
528 int limit
= ctx
->limit
;
530 /* Get a marker value to mark "seen" channels for unique count */
531 cmarker
=nextchanmarker();
533 /* The top-level node needs to return a BOOL */
534 search
=coerceNode(ctx
, search
, RETURNTYPE_BOOL
);
536 for (i
=0;i
<NICKHASHSIZE
;i
++) {
537 for (np
=nicktable
[i
], k
= 0;ctx
->targets
? (k
< ctx
->targets
->cursi
) : (np
!= NULL
);np
=np
->next
, k
++) {
539 np
= ((nick
**)ctx
->targets
->content
)[k
];
544 if ((search
->exe
)(ctx
, search
, np
)) {
545 /* Add total channels */
546 tchans
+= np
->channels
->cursi
;
548 /* Check channels for uniqueness */
549 cs
=(channel
**)np
->channels
->content
;
550 for (j
=0;j
<np
->channels
->cursi
;j
++) {
551 if (cs
[j
]->index
->marker
!= cmarker
) {
552 cs
[j
]->index
->marker
=cmarker
;
558 display(ctx
, sender
, np
);
561 ctx
->reply(sender
, "--- More than %d matches, skipping the rest",limit
);
570 ctx
->reply(sender
,"--- End of list: %d matches; users were on %u channels (%u unique, %.1f average clones)",
571 matches
, tchans
, uchans
, (float)tchans
/uchans
);
574 int do_whowassearch_real(replyFunc reply
, wallFunc wall
, void *source
, int cargc
, char **cargv
) {
575 nick
*sender
= source
;
578 WhowasDisplayFunc display
=defaultwhowasfn
;
583 reply( sender
, "Usage: [flags] <criteria>");
584 reply( sender
, "For help, see help whowassearch");
588 ret
= parseopts(cargc
, cargv
, &arg
, &limit
, NULL
, (void *)&display
, reg_whowassearch
->outputtree
, reply
, sender
);
593 reply(sender
,"No search terms - aborting.");
598 rejoinline(cargv
[arg
],cargc
-arg
);
601 tree
= parse_string(reg_whowassearch
, cargv
[arg
]);
603 displaystrerror(reply
, sender
, cargv
[arg
]);
607 ast_whowassearch(tree
->root
, reply
, sender
, wall
, display
, NULL
, NULL
, limit
, NULL
);
614 int do_whowassearch(void *source
, int cargc
, char **cargv
) {
615 return do_whowassearch_real(controlreply
, controlwallwrapper
, source
, cargc
, cargv
);
618 void whowassearch_exe(struct searchNode
*search
, searchCtx
*ctx
) {
621 nick
*sender
= ctx
->sender
;
622 senderNSExtern
= sender
;
623 WhowasDisplayFunc display
= ctx
->displayfn
;
624 int limit
= ctx
->limit
;
626 assert(!ctx
->targets
);
628 /* The top-level node needs to return a BOOL */
629 search
=coerceNode(ctx
, search
, RETURNTYPE_BOOL
);
631 for (i
= whowasoffset
; i
< whowasoffset
+ WW_MAXENTRIES
; i
++) {
632 ww
= &whowasrecs
[i
% WW_MAXENTRIES
];
634 if (ww
->type
== WHOWAS_UNUSED
)
637 /* Note: We're passing the nick to the filter function. The original
638 * whowas record is in the nick's ->next field. */
639 if ((search
->exe
)(ctx
, search
, &ww
->nick
)) {
641 display(ctx
, sender
, ww
);
644 ctx
->reply(sender
, "--- More than %d matches, skipping the rest",limit
);
649 ctx
->reply(sender
,"--- End of list: %d matches", matches
);
652 int do_chansearch_real(replyFunc reply
, wallFunc wall
, void *source
, int cargc
, char **cargv
) {
653 nick
*sender
= source
;
656 ChanDisplayFunc display
=defaultchanfn
;
661 reply( sender
, "Usage: [flags] <criteria>");
662 reply( sender
, "For help, see help chansearch");
666 ret
= parseopts(cargc
, cargv
, &arg
, &limit
, NULL
, (void *)&display
, reg_chansearch
->outputtree
, reply
, sender
);
671 reply(sender
,"No search terms - aborting.");
676 rejoinline(cargv
[arg
],cargc
-arg
);
679 tree
= parse_string(reg_chansearch
, cargv
[arg
]);
681 displaystrerror(reply
, sender
, cargv
[arg
]);
685 ast_chansearch(tree
->root
, reply
, sender
, wall
, display
, NULL
, NULL
, limit
, NULL
);
692 int do_chansearch(void *source
, int cargc
, char **cargv
) {
693 return do_chansearch_real(controlreply
, controlwallwrapper
, source
, cargc
, cargv
);
696 void chansearch_exe(struct searchNode
*search
, searchCtx
*ctx
) {
700 nick
*sender
= ctx
->sender
;
701 senderNSExtern
= sender
;
702 ChanDisplayFunc display
= ctx
->displayfn
;
703 int limit
= ctx
->limit
;
705 assert(!ctx
->targets
);
707 search
=coerceNode(ctx
, search
, RETURNTYPE_BOOL
);
709 for (i
=0;i
<CHANNELHASHSIZE
;i
++) {
710 for (cip
=chantable
[i
];cip
;cip
=cip
->next
) {
711 if ((search
->exe
)(ctx
, search
, cip
)) {
713 display(ctx
, sender
, cip
);
715 ctx
->reply(sender
, "--- More than %d matches, skipping the rest",limit
);
721 ctx
->reply(sender
,"--- End of list: %d matches", matches
);
724 int do_usersearch_real(replyFunc reply
, wallFunc wall
, void *source
, int cargc
, char **cargv
) {
725 nick
*sender
= source
;
728 UserDisplayFunc display
=defaultuserfn
;
733 reply( sender
, "Usage: [flags] <criteria>");
734 reply( sender
, "For help, see help usersearch");
738 ret
= parseopts(cargc
, cargv
, &arg
, &limit
, NULL
, (void *)&display
, reg_usersearch
->outputtree
, reply
, sender
);
743 reply(sender
,"No search terms - aborting.");
748 rejoinline(cargv
[arg
],cargc
-arg
);
751 tree
= parse_string(reg_usersearch
, cargv
[arg
]);
753 displaystrerror(reply
, sender
, cargv
[arg
]);
757 ast_usersearch(tree
->root
, reply
, sender
, wall
, display
, NULL
, NULL
, limit
, NULL
);
764 int do_usersearch(void *source
, int cargc
, char **cargv
) {
765 return do_usersearch_real(controlreply
, controlwallwrapper
, source
, cargc
, cargv
);
768 void usersearch_exe(struct searchNode
*search
, searchCtx
*ctx
) {
772 nick
*sender
= ctx
->sender
;
773 int limit
= ctx
->limit
;
774 UserDisplayFunc display
= ctx
->displayfn
;
775 senderNSExtern
= sender
;
777 assert(!ctx
->targets
);
779 search
=coerceNode(ctx
, search
, RETURNTYPE_BOOL
);
781 for (i
=0;i
<AUTHNAMEHASHSIZE
;i
++) {
782 for (aup
=authnametable
[i
];aup
;aup
=aup
->next
) {
783 if ((search
->exe
)(ctx
, search
, aup
)) {
785 display(ctx
, sender
, aup
);
787 ctx
->reply(sender
, "--- More than %d matches, skipping the rest",limit
);
793 ctx
->reply(sender
,"--- End of list: %d matches", matches
);
796 /* Free a coerce node */
797 void free_coerce(searchCtx
*ctx
, struct searchNode
*thenode
) {
798 struct coercedata
*cd
=thenode
->localdata
;
800 cd
->child
->free(ctx
, cd
->child
);
801 free(thenode
->localdata
);
805 /* Free a coerce node with a stringbuf allocated */
806 void free_coercestring(searchCtx
*ctx
, struct searchNode
*thenode
) {
807 free(((struct coercedata
*)thenode
->localdata
)->u
.stringbuf
);
808 free_coerce(ctx
, thenode
);
811 /* exe_tostr_null: return the constant string */
812 void *exe_tostr_null(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
813 struct coercedata
*cd
=thenode
->localdata
;
815 return cd
->u
.stringbuf
;
818 /* exe_val_null: return the constant value */
819 void *exe_val_null(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
820 struct coercedata
*cd
=thenode
->localdata
;
822 return (void *)cd
->u
.val
;
825 /* Lots of very dull type conversion functions */
826 void *exe_inttostr(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
827 struct coercedata
*cd
=thenode
->localdata
;
829 sprintf(cd
->u
.stringbuf
, "%lu", (unsigned long)(cd
->child
->exe
)(ctx
, cd
->child
, theinput
));
831 return cd
->u
.stringbuf
;
834 void *exe_booltostr(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
835 struct coercedata
*cd
=thenode
->localdata
;
837 if ((cd
->child
->exe
)(ctx
, cd
->child
, theinput
)) {
838 sprintf(cd
->u
.stringbuf
,"1");
840 cd
->u
.stringbuf
[0]='\0';
843 return cd
->u
.stringbuf
;
846 void *exe_strtoint(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
847 struct coercedata
*cd
=thenode
->localdata
;
849 return (void *)strtoul((cd
->child
->exe
)(ctx
,cd
->child
,theinput
),NULL
,10);
852 void *exe_booltoint(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
853 struct coercedata
*cd
=thenode
->localdata
;
855 /* Don't need to do anything */
856 return (cd
->child
->exe
)(ctx
, cd
->child
, theinput
);
859 void *exe_strtobool(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
860 struct coercedata
*cd
=thenode
->localdata
;
861 char *ch
=(cd
->child
->exe
)(ctx
, cd
->child
, theinput
);
863 if (!ch
|| *ch
=='\0' || (*ch
=='0' && ch
[1]=='\0')) {
870 void *exe_inttobool(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
871 struct coercedata
*cd
=thenode
->localdata
;
873 if ((cd
->child
->exe
)(ctx
, cd
->child
, theinput
)) {
880 struct searchNode
*coerceNode(searchCtx
*ctx
, struct searchNode
*thenode
, int type
) {
881 struct searchNode
*anode
;
882 struct coercedata
*cd
;
884 /* You can't coerce a NULL */
888 /* No effort required to coerce to the same type */
889 if (type
==(thenode
->returntype
& RETURNTYPE_TYPE
))
892 anode
=(struct searchNode
*)malloc(sizeof(struct searchNode
));
893 anode
->localdata
=cd
=(struct coercedata
*)malloc(sizeof(struct coercedata
));
895 anode
->returntype
=type
; /* We'll return what they want, always */
896 anode
->free
=free_coerce
;
899 case RETURNTYPE_STRING
:
900 /* For a string we'll need a buffer */
901 /* A 64-bit number prints out to 20 digits, this leaves some slack */
902 cd
->u
.stringbuf
=malloc(25);
903 anode
->free
=free_coercestring
;
905 switch(thenode
->returntype
& RETURNTYPE_TYPE
) {
908 if (thenode
->returntype
& RETURNTYPE_CONST
) {
909 /* Constant node: sort it out now */
910 sprintf(cd
->u
.stringbuf
, "%lu", (unsigned long)thenode
->exe(ctx
, thenode
, NULL
));
911 anode
->exe
=exe_tostr_null
;
912 anode
->returntype
|= RETURNTYPE_CONST
;
915 anode
->exe
=exe_inttostr
;
919 case RETURNTYPE_BOOL
:
920 if (thenode
->returntype
& RETURNTYPE_CONST
) {
921 /* Constant bool value */
922 if (thenode
->exe(ctx
, thenode
,NULL
)) {
924 sprintf(cd
->u
.stringbuf
, "1");
926 cd
->u
.stringbuf
[0] = '\0';
928 anode
->exe
=exe_tostr_null
;
929 anode
->returntype
|= RETURNTYPE_CONST
;
931 /* Variable bool value */
932 anode
->exe
=exe_booltostr
;
940 switch (thenode
->returntype
& RETURNTYPE_TYPE
) {
941 case RETURNTYPE_STRING
:
942 if (thenode
->returntype
& RETURNTYPE_CONST
) {
943 cd
->u
.val
=strtoul((thenode
->exe
)(ctx
, thenode
, NULL
), NULL
, 10);
944 anode
->exe
=exe_val_null
;
945 anode
->returntype
|= RETURNTYPE_CONST
;
947 anode
->exe
=exe_strtoint
;
952 case RETURNTYPE_BOOL
:
953 if (thenode
->returntype
& RETURNTYPE_CONST
) {
954 if ((thenode
->exe
)(ctx
, thenode
,NULL
))
959 anode
->exe
=exe_val_null
;
960 anode
->returntype
|= RETURNTYPE_CONST
;
962 anode
->exe
=exe_booltoint
;
969 case RETURNTYPE_BOOL
:
971 switch (thenode
->returntype
& RETURNTYPE_TYPE
) {
972 case RETURNTYPE_STRING
:
973 if (thenode
->returntype
& RETURNTYPE_CONST
) {
974 char *rv
=(char *)((thenode
->exe
)(ctx
, thenode
, NULL
));
975 if (!rv
|| *rv
=='\0' || (*rv
=='0' && rv
[1]=='\0'))
980 anode
->exe
=exe_val_null
;
981 anode
->returntype
|= RETURNTYPE_CONST
;
983 anode
->exe
=exe_strtobool
;
989 if (thenode
->returntype
& RETURNTYPE_CONST
) {
990 if ((thenode
->exe
)(ctx
, thenode
,NULL
))
995 anode
->exe
=exe_val_null
;
996 anode
->returntype
|= RETURNTYPE_CONST
;
998 anode
->exe
=exe_inttobool
;
1008 /* Literals always return constant strings... */
1009 void *literal_exe(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
1010 if (thenode
->localdata
)
1011 return ((sstring
*)thenode
->localdata
)->content
;
1016 void literal_free(searchCtx
*ctx
, struct searchNode
*thenode
) {
1017 freesstring(thenode
->localdata
);
1021 void nssnprintf(char *buf
, size_t size
, const char *format
, nick
*np
) {
1034 for(p
=format
;*p
;p
++) {
1036 if(!sbaddchar(&b
, *p
))
1044 if(!sbaddchar(&b
, *p
))
1052 c
= np
->nick
; break;
1054 c
= np
->ident
; break;
1056 c
= np
->host
->name
->content
; break;
1058 snprintf(hostbuf
, sizeof(hostbuf
), "%s", IPtostr(np
->ipaddress
));
1062 snprintf(hostbuf
, sizeof(hostbuf
), "%s!%s@%s", np
->nick
, np
->ident
, IPtostr(np
->ipaddress
));
1066 c
= "(bad format specifier)";
1069 if(!sbaddstr(&b
, c
))
1081 static char *var_tochar(searchCtx
*ctx
, char *arg
, searchNode
**variable
) {
1082 *variable
= ctx
->parser(ctx
, arg
);
1083 if (!(*variable
= coerceNode(ctx
, *variable
, RETURNTYPE_STRING
)))
1086 if(!((*variable
)->returntype
& RETURNTYPE_CONST
)) {
1087 parseError
= "only constant variables allowed";
1088 ((*variable
)->free
)(ctx
, *variable
);
1092 return (char *)((*variable
)->exe
)(ctx
, *variable
, NULL
);
1095 void free_val_null(searchCtx
*ctx
, struct searchNode
*thenode
) {
1098 struct searchVariable
*var_register(searchCtx
*ctx
, char *arg
, int type
) {
1099 searchNode
*variable
;
1100 struct searchVariable
*us
;
1104 if(ctx
->lastvar
>= MAX_VARIABLES
) {
1105 parseError
= "Maximum number of variables reached";
1109 us
= &ctx
->vars
[ctx
->lastvar
];
1111 var
= var_tochar(ctx
, arg
, &variable
);
1115 strlcpy(us
->name
, var
, sizeof(us
->name
));
1116 (variable
->free
)(ctx
, variable
);
1118 for(i
=0;i
<ctx
->lastvar
;i
++) {
1119 if(!strcmp(us
->name
, ctx
->vars
[i
].name
)) {
1120 parseError
= "variable name already in use";
1126 us
->data
.returntype
= type
;
1127 us
->data
.localdata
= &us
->cdata
;
1128 us
->data
.exe
= exe_val_null
;
1129 us
->data
.free
= free_val_null
;
1131 us
->cdata
.child
= NULL
;
1135 searchNode
*var_get(searchCtx
*ctx
, char *arg
) {
1136 searchNode
*variable
, *found
= NULL
;
1138 char *var
= var_tochar(ctx
, arg
, &variable
);
1142 for(i
=0;i
<ctx
->lastvar
;i
++) {
1143 if(!strcmp(var
, ctx
->vars
[i
].name
)) {
1144 found
= &ctx
->vars
[i
].data
;
1148 (variable
->free
)(ctx
, variable
);
1151 parseError
= "variable not found";
1155 void var_setstr(struct searchVariable
*v
, char *data
) {
1156 v
->cdata
.u
.stringbuf
= data
;
1159 void displaystrerror(replyFunc reply
, nick
*np
, const char *input
) {
1162 if((parseStrErrorPos
>= 0) && (parseStrErrorPos
< sizeof(buf
) - 3)) {
1165 for(i
=0;i
<parseStrErrorPos
;i
++)
1171 reply(np
, "%s", input
);
1172 reply(np
, "%s", buf
);
1175 reply(np
, "Parse error: %s", parseStrError
);
1178 struct searchNode
*argtoconststr(char *command
, searchCtx
*ctx
, char *arg
, char **p
) {
1179 struct searchNode
*c
;
1180 static char errorbuf
[512];
1182 c
= ctx
->parser(ctx
, arg
);
1183 if (!(c
= coerceNode(ctx
, c
, RETURNTYPE_STRING
))) {
1184 snprintf(errorbuf
, sizeof(errorbuf
), "%s: unable to coerce argument to string", command
);
1185 parseError
= errorbuf
;
1189 if (!(c
->returntype
& RETURNTYPE_CONST
)) {
1190 snprintf(errorbuf
, sizeof(errorbuf
), "%s: constant argument required", command
);
1191 parseError
= errorbuf
;
1197 *p
= (char *)(c
->exe
)(ctx
, c
, NULL
);