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");
217 registersearchterm(reg_nicksearch
, "age",age_parse
, 0, "Nick record age in seconds");
218 registersearchterm(reg_whowassearch
, "age",age_parse
, 0, "Whowas record age in seconds");
221 /* Whowas operations */
222 registersearchterm(reg_whowassearch
, "quit",quit_parse
, 0, "User quit");
223 registersearchterm(reg_whowassearch
, "killed",killed_parse
, 0, "User was killed");
224 registersearchterm(reg_whowassearch
, "renamed",renamed_parse
, 0, "User changed nick");
225 registersearchterm(reg_whowassearch
, "newnick",newnick_parse
, 0, "New nick (for rename whowas records)");
226 registersearchterm(reg_whowassearch
, "reason",reason_parse
, 0, "Quit/kill reason");
228 /* Channel operations */
229 registersearchterm(reg_chansearch
, "exists",exists_parse
, 0, "Returns if channel exists on network. Note: newserv may store data on empty channels"); /* channel only */
230 registersearchterm(reg_chansearch
, "services",services_parse
, 0, ""); /* channel only */
231 registersearchterm(reg_chansearch
, "size",size_parse
, 0, "Channel user count"); /* channel only */
232 registersearchterm(reg_chansearch
, "name",name_parse
, 0, "Channel Name"); /* channel only */
233 registersearchterm(reg_chansearch
, "topic",topic_parse
, 0, "Channel topic"); /* channel only */
234 registersearchterm(reg_chansearch
, "oppct",oppct_parse
, 0, "Percentage Opped"); /* channel only */
235 registersearchterm(reg_chansearch
, "cumodecount",cumodecount_parse
, 0, "Count of users with given channel modes"); /* channel only */
236 registersearchterm(reg_chansearch
, "cumodepct",cumodepct_parse
, 0, "Percentage of users with given channel modes"); /* channel only */
237 registersearchterm(reg_chansearch
, "uniquehostpct",hostpct_parse
, 0, "uniquehost percent"); /* channel only */
238 registersearchterm(reg_chansearch
, "authedpct",authedpct_parse
, 0, "Percentage of authed users"); /* channel only */
239 registersearchterm(reg_chansearch
, "kick",kick_parse
, 0, "KICK users channels in newsearch result. Note: evaluation order"); /* channel only */
241 /* Nickname / channel operations */
242 registersearchterm(reg_chansearch
, "modes",modes_parse
, 0, "Channel Modes");
243 registersearchterm(reg_nicksearch
, "modes",modes_parse
, 0, "User Modes");
244 registersearchterm(reg_whowassearch
, "modes",modes_parse
, 0, "User Modes");
245 registersearchterm(reg_chansearch
, "nick",nick_parse
, 0, "Nickname");
246 registersearchterm(reg_nicksearch
, "nick",nick_parse
, 0, "Nickname");
247 registersearchterm(reg_whowassearch
, "nick",nick_parse
, 0, "Nickname");
249 /* Kill / gline parameters */
250 registersearchterm(reg_chansearch
,"kill",kill_parse
, 0, "KILL users in newsearch result. Note: evaluation order");
251 registersearchterm(reg_chansearch
,"gline",gline_parse
, 0, "GLINE users in newsearch result. Note: evaluation order");
252 registersearchterm(reg_nicksearch
,"kill",kill_parse
, 0, "KILL users in newsearch result. Note: evaluation order");
253 registersearchterm(reg_nicksearch
,"gline",gline_parse
, 0, "GLINE users in newsearch result. Note: evaluation order");
254 registersearchterm(reg_whowassearch
,"gline",gline_parse
, 0, "GLINE users in newsearch result. Note: evaluation order");
256 /* Iteration functionality */
257 registerglobalsearchterm("any",any_parse
, "usage: any (generatorfn x) (fn ... (var x) ...)");
258 registerglobalsearchterm("all",all_parse
, "usage: all (generatorfn x) (fn ... (var x) ...)");
259 registerglobalsearchterm("var",var_parse
, "usage: var variable");
261 /* Iterable functions */
262 registersearchterm(reg_nicksearch
, "channeliter",channeliter_parse
, 0, "Channel Iterable function - usage: (any (channeliter x) (match (var x) #twilight*))"); /* nick only */
263 registersearchterm(reg_chansearch
, "nickiter",nickiter_parse
, 0, "Nick Iterable function - usage: (any (nickiter x) (match (var x) nickname*))"); /* channel only */
265 /* Functions that work on strings?! */
266 registersearchterm(reg_nicksearch
, "cumodes", cumodes_parse
, 0, "usage: (cumodes (var x) <modes>)");
267 registersearchterm(reg_chansearch
, "cumodes", cumodes_parse
, 0, "usage: (cumodes (var x) <modes>)");
269 /* Notice functionality */
270 registersearchterm(reg_chansearch
,"notice",notice_parse
, 0, "NOTICE users in newsearch result. Note: evaluation order");
271 registersearchterm(reg_nicksearch
,"notice",notice_parse
, 0, "NOTICE users in newsearch result. Note: evaluation order");
273 /* Nick output filters */
274 regdisp(reg_nicksearch
,"default",printnick
, 0, "");
275 regdisp(reg_nicksearch
,"channels",printnick_channels
, 0, "include channels in output");
277 /* Channel output filters */
278 regdisp(reg_chansearch
,"default",printchannel
, 0, "");
279 regdisp(reg_chansearch
,"topic",printchannel_topic
, 0, "display channel topics");
280 regdisp(reg_chansearch
,"services",printchannel_services
, 0, "display services on channels");
282 /* Nick output filters */
283 regdisp(reg_usersearch
,"default",printuser
, 0, "");
288 searchList
*sl
, *psl
;
290 Command
*cmdlist
[100];
297 n
=getcommandlist(searchCmdTree
,cmdlist
,100);
299 deregistersearchterm( (searchCmd
*)cmdlist
[i
]->handler
, psl
->name
->content
, psl
->cmd
);
302 freesstring(psl
->name
);
308 deregistersearchcommand( reg_nicksearch
);
309 deregistersearchcommand( reg_chansearch
);
310 deregistersearchcommand( reg_usersearch
);
311 deregistersearchcommand( reg_whowassearch
);
312 destroycommandtree( searchCmdTree
);
315 void registerglobalsearchterm(char *term
, parseFunc parsefunc
, char *help
) {
317 Command
*cmdlist
[100];
318 searchList
*sl
= malloc(sizeof(searchList
));
320 Error("newsearch", ERR_ERROR
, "malloc failed: registerglobalsearchterm");
325 sl
->name
= getsstring(term
, NSMAX_COMMAND_LEN
);
328 int len
=strlen(help
);
329 sl
->help
=(char *)malloc(len
+1);
331 freesstring(sl
->name
);
333 Error("newsearch", ERR_ERROR
, "malloc failed: registerglobalsearchterm");
336 strncpy(sl
->help
, help
, len
);
337 sl
->help
[len
] = '\0';
343 if ( globalterms
!= NULL
) {
344 sl
->next
= globalterms
;
348 n
=getcommandlist(searchCmdTree
,cmdlist
,100);
350 /* maxparams is set to 1 to indicate a global term */
351 /* access level is set to 0 for all global terms */
352 addcommandexttotree( ((searchCmd
*)cmdlist
[i
]->handler
)->searchtree
,term
, 0, 1, (CommandHandler
) parsefunc
, help
);
356 void deregisterglobalsearchterm(char *term
, parseFunc parsefunc
) {
358 Command
*cmdlist
[100];
359 searchList
*sl
, *psl
=NULL
;
361 for (sl
=globalterms
; sl
; sl
=sl
->next
) {
362 if ( strcmp( sl
->name
->content
, term
) == 0 ) {
370 psl
->next
= sl
->next
;
373 n
=getcommandlist(searchCmdTree
,cmdlist
,100);
375 deletecommandfromtree( ((searchCmd
*)cmdlist
[i
]->handler
)->searchtree
, term
, (CommandHandler
) parsefunc
);
377 freesstring(sl
->name
);
382 void registersearchterm(searchCmd
*cmd
, char *term
, parseFunc parsefunc
, int level
, char *help
) {
383 /* NOTE: global terms are added to the tree elsewhere as we set maxparams to 1 to indicate global */
384 addcommandexttotree(cmd
->searchtree
, term
, level
, 0, (CommandHandler
) parsefunc
, help
);
387 void deregistersearchterm(searchCmd
*cmd
, char *term
, parseFunc parsefunc
) {
388 /* NOTE: global terms are removed from the tree within deregisterglobalsearchterm */
389 deletecommandfromtree(cmd
->searchtree
, term
, (CommandHandler
) parsefunc
);
392 static void controlwallwrapper(int level
, char *format
, ...) __attribute__ ((format (printf
, 2, 3)));
393 static void controlwallwrapper(int level
, char *format
, ...) {
397 va_start(ap
, format
);
398 vsnprintf(buf
, sizeof(buf
), format
, ap
);
399 controlwall(NO_OPER
, level
, "%s", buf
);
403 int parseopts(int cargc
, char **cargv
, int *arg
, int *limit
, void **subset
, void *display
, CommandTree
*sl
, replyFunc reply
, void *sender
) {
406 struct irc_in_addr sin
; unsigned char bits
;
408 if (*cargv
[0] == '-') {
412 for (ch
=cargv
[0]+1;*ch
;ch
++) {
416 reply(sender
,"Error: -l switch requires an argument (for help, see help <searchcmd>)");
419 *limit
=strtoul(cargv
[(*arg
)++],NULL
,10);
424 reply(sender
,"Error: -d switch requires an argument (for help, see help <searchcmd>)");
427 cmd
=findcommandintree(sl
, cargv
[*arg
],1);
429 reply(sender
,"Error: unknown output format %s (for help, see help <searchcmd>)",cargv
[*arg
]);
432 if ( !controlpermitted( cmd
->level
, sender
) ) {
433 reply(sender
,"Error: Access Denied for output format %s (for help, see help <searchcmd>)", cargv
[*arg
]);
436 *((void **)display
)=(void *)cmd
->handler
;
441 if (subset
== NULL
) {
442 reply(sender
,"Error: -s switch not supported for this search.");
446 reply(sender
,"Error: -s switch requires an argument (for help, see help <searchcmd>)");
449 if (ipmask_parse(cargv
[*arg
], &sin
, &bits
) == 0) {
450 reply(sender
, "Error: Invalid CIDR mask supplied (for help, see help <searchcmd>)");
453 *subset
= (void *)refnode(iptree
, &sin
, bits
);
458 reply(sender
,"Unrecognised flag -%c. (for help, see help <searchcmd>)",*ch
);
467 void newsearch_ctxinit(searchCtx
*ctx
, searchParseFunc searchfn
, replyFunc replyfn
, wallFunc wallfn
, void *arg
, searchCmd
*cmd
, nick
*np
, void *displayfn
, int limit
, array
*targets
) {
468 memset(ctx
, 0, sizeof(searchCtx
));
470 ctx
->reply
= replyfn
;
472 ctx
->parser
= searchfn
;
474 ctx
->searchcmd
= cmd
;
477 ctx
->targets
= targets
;
478 ctx
->displayfn
= displayfn
;
481 int do_nicksearch_real(replyFunc reply
, wallFunc wall
, void *source
, int cargc
, char **cargv
) {
482 nick
*sender
= source
;
485 NickDisplayFunc display
=defaultnickfn
;
490 reply( sender
, "Usage: [flags] <criteria>");
491 reply( sender
, "For help, see help nicksearch");
495 ret
= parseopts(cargc
, cargv
, &arg
, &limit
, NULL
, (void *)&display
, reg_nicksearch
->outputtree
, reply
, sender
);
500 reply(sender
,"No search terms - aborting.");
505 rejoinline(cargv
[arg
],cargc
-arg
);
508 tree
= parse_string(reg_nicksearch
, cargv
[arg
]);
510 displaystrerror(reply
, sender
, cargv
[arg
]);
514 ast_nicksearch(tree
->root
, reply
, sender
, wall
, display
, NULL
, NULL
, limit
, NULL
);
521 int do_nicksearch(void *source
, int cargc
, char **cargv
) {
522 return do_nicksearch_real(controlreply
, controlwallwrapper
, source
, cargc
, cargv
);
525 void nicksearch_exe(struct searchNode
*search
, searchCtx
*ctx
) {
528 unsigned int cmarker
;
529 unsigned int tchans
=0,uchans
=0;
531 nick
*np
, *sender
= ctx
->sender
;
532 senderNSExtern
= sender
;
533 NickDisplayFunc display
= ctx
->displayfn
;
534 int limit
= ctx
->limit
;
536 /* Get a marker value to mark "seen" channels for unique count */
537 cmarker
=nextchanmarker();
539 /* The top-level node needs to return a BOOL */
540 search
=coerceNode(ctx
, search
, RETURNTYPE_BOOL
);
542 for (i
=0;i
<NICKHASHSIZE
;i
++) {
543 for (np
=nicktable
[i
], k
= 0;ctx
->targets
? (k
< ctx
->targets
->cursi
) : (np
!= NULL
);np
=np
->next
, k
++) {
545 np
= ((nick
**)ctx
->targets
->content
)[k
];
550 if ((search
->exe
)(ctx
, search
, np
)) {
551 /* Add total channels */
552 tchans
+= np
->channels
->cursi
;
554 /* Check channels for uniqueness */
555 cs
=(channel
**)np
->channels
->content
;
556 for (j
=0;j
<np
->channels
->cursi
;j
++) {
557 if (cs
[j
]->index
->marker
!= cmarker
) {
558 cs
[j
]->index
->marker
=cmarker
;
564 display(ctx
, sender
, np
);
567 ctx
->reply(sender
, "--- More than %d matches, skipping the rest",limit
);
576 ctx
->reply(sender
,"--- End of list: %d matches; users were on %u channels (%u unique, %.1f average clones)",
577 matches
, tchans
, uchans
, (float)tchans
/uchans
);
580 int do_whowassearch_real(replyFunc reply
, wallFunc wall
, void *source
, int cargc
, char **cargv
) {
581 nick
*sender
= source
;
584 WhowasDisplayFunc display
=defaultwhowasfn
;
589 reply( sender
, "Usage: [flags] <criteria>");
590 reply( sender
, "For help, see help whowassearch");
594 ret
= parseopts(cargc
, cargv
, &arg
, &limit
, NULL
, (void *)&display
, reg_whowassearch
->outputtree
, reply
, sender
);
599 reply(sender
,"No search terms - aborting.");
604 rejoinline(cargv
[arg
],cargc
-arg
);
607 tree
= parse_string(reg_whowassearch
, cargv
[arg
]);
609 displaystrerror(reply
, sender
, cargv
[arg
]);
613 ast_whowassearch(tree
->root
, reply
, sender
, wall
, display
, NULL
, NULL
, limit
, NULL
);
620 int do_whowassearch(void *source
, int cargc
, char **cargv
) {
621 return do_whowassearch_real(controlreply
, controlwallwrapper
, source
, cargc
, cargv
);
624 void whowassearch_exe(struct searchNode
*search
, searchCtx
*ctx
) {
627 nick
*sender
= ctx
->sender
;
628 senderNSExtern
= sender
;
629 WhowasDisplayFunc display
= ctx
->displayfn
;
630 int limit
= ctx
->limit
;
632 assert(!ctx
->targets
);
634 /* The top-level node needs to return a BOOL */
635 search
=coerceNode(ctx
, search
, RETURNTYPE_BOOL
);
637 for (i
= whowasoffset
; i
< whowasoffset
+ WW_MAXENTRIES
; i
++) {
638 ww
= &whowasrecs
[i
% WW_MAXENTRIES
];
640 if (ww
->type
== WHOWAS_UNUSED
)
643 /* Note: We're passing the nick to the filter function. The original
644 * whowas record is in the nick's ->next field. */
645 if ((search
->exe
)(ctx
, search
, &ww
->nick
)) {
647 display(ctx
, sender
, ww
);
650 ctx
->reply(sender
, "--- More than %d matches, skipping the rest",limit
);
655 ctx
->reply(sender
,"--- End of list: %d matches", matches
);
658 int do_chansearch_real(replyFunc reply
, wallFunc wall
, void *source
, int cargc
, char **cargv
) {
659 nick
*sender
= source
;
662 ChanDisplayFunc display
=defaultchanfn
;
667 reply( sender
, "Usage: [flags] <criteria>");
668 reply( sender
, "For help, see help chansearch");
672 ret
= parseopts(cargc
, cargv
, &arg
, &limit
, NULL
, (void *)&display
, reg_chansearch
->outputtree
, reply
, sender
);
677 reply(sender
,"No search terms - aborting.");
682 rejoinline(cargv
[arg
],cargc
-arg
);
685 tree
= parse_string(reg_chansearch
, cargv
[arg
]);
687 displaystrerror(reply
, sender
, cargv
[arg
]);
691 ast_chansearch(tree
->root
, reply
, sender
, wall
, display
, NULL
, NULL
, limit
, NULL
);
698 int do_chansearch(void *source
, int cargc
, char **cargv
) {
699 return do_chansearch_real(controlreply
, controlwallwrapper
, source
, cargc
, cargv
);
702 void chansearch_exe(struct searchNode
*search
, searchCtx
*ctx
) {
706 nick
*sender
= ctx
->sender
;
707 senderNSExtern
= sender
;
708 ChanDisplayFunc display
= ctx
->displayfn
;
709 int limit
= ctx
->limit
;
711 assert(!ctx
->targets
);
713 search
=coerceNode(ctx
, search
, RETURNTYPE_BOOL
);
715 for (i
=0;i
<CHANNELHASHSIZE
;i
++) {
716 for (cip
=chantable
[i
];cip
;cip
=cip
->next
) {
717 if ((search
->exe
)(ctx
, search
, cip
)) {
719 display(ctx
, sender
, cip
);
721 ctx
->reply(sender
, "--- More than %d matches, skipping the rest",limit
);
727 ctx
->reply(sender
,"--- End of list: %d matches", matches
);
730 int do_usersearch_real(replyFunc reply
, wallFunc wall
, void *source
, int cargc
, char **cargv
) {
731 nick
*sender
= source
;
734 UserDisplayFunc display
=defaultuserfn
;
739 reply( sender
, "Usage: [flags] <criteria>");
740 reply( sender
, "For help, see help usersearch");
744 ret
= parseopts(cargc
, cargv
, &arg
, &limit
, NULL
, (void *)&display
, reg_usersearch
->outputtree
, reply
, sender
);
749 reply(sender
,"No search terms - aborting.");
754 rejoinline(cargv
[arg
],cargc
-arg
);
757 tree
= parse_string(reg_usersearch
, cargv
[arg
]);
759 displaystrerror(reply
, sender
, cargv
[arg
]);
763 ast_usersearch(tree
->root
, reply
, sender
, wall
, display
, NULL
, NULL
, limit
, NULL
);
770 int do_usersearch(void *source
, int cargc
, char **cargv
) {
771 return do_usersearch_real(controlreply
, controlwallwrapper
, source
, cargc
, cargv
);
774 void usersearch_exe(struct searchNode
*search
, searchCtx
*ctx
) {
778 nick
*sender
= ctx
->sender
;
779 int limit
= ctx
->limit
;
780 UserDisplayFunc display
= ctx
->displayfn
;
781 senderNSExtern
= sender
;
783 assert(!ctx
->targets
);
785 search
=coerceNode(ctx
, search
, RETURNTYPE_BOOL
);
787 for (i
=0;i
<AUTHNAMEHASHSIZE
;i
++) {
788 for (aup
=authnametable
[i
];aup
;aup
=aup
->next
) {
789 if ((search
->exe
)(ctx
, search
, aup
)) {
791 display(ctx
, sender
, aup
);
793 ctx
->reply(sender
, "--- More than %d matches, skipping the rest",limit
);
799 ctx
->reply(sender
,"--- End of list: %d matches", matches
);
802 /* Free a coerce node */
803 void free_coerce(searchCtx
*ctx
, struct searchNode
*thenode
) {
804 struct coercedata
*cd
=thenode
->localdata
;
806 cd
->child
->free(ctx
, cd
->child
);
807 free(thenode
->localdata
);
811 /* Free a coerce node with a stringbuf allocated */
812 void free_coercestring(searchCtx
*ctx
, struct searchNode
*thenode
) {
813 free(((struct coercedata
*)thenode
->localdata
)->u
.stringbuf
);
814 free_coerce(ctx
, thenode
);
817 /* exe_tostr_null: return the constant string */
818 void *exe_tostr_null(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
819 struct coercedata
*cd
=thenode
->localdata
;
821 return cd
->u
.stringbuf
;
824 /* exe_val_null: return the constant value */
825 void *exe_val_null(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
826 struct coercedata
*cd
=thenode
->localdata
;
828 return (void *)cd
->u
.val
;
831 /* Lots of very dull type conversion functions */
832 void *exe_inttostr(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
833 struct coercedata
*cd
=thenode
->localdata
;
835 sprintf(cd
->u
.stringbuf
, "%lu", (unsigned long)(cd
->child
->exe
)(ctx
, cd
->child
, theinput
));
837 return cd
->u
.stringbuf
;
840 void *exe_booltostr(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
841 struct coercedata
*cd
=thenode
->localdata
;
843 if ((cd
->child
->exe
)(ctx
, cd
->child
, theinput
)) {
844 sprintf(cd
->u
.stringbuf
,"1");
846 cd
->u
.stringbuf
[0]='\0';
849 return cd
->u
.stringbuf
;
852 void *exe_strtoint(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
853 struct coercedata
*cd
=thenode
->localdata
;
855 return (void *)strtoul((cd
->child
->exe
)(ctx
,cd
->child
,theinput
),NULL
,10);
858 void *exe_booltoint(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
859 struct coercedata
*cd
=thenode
->localdata
;
861 /* Don't need to do anything */
862 return (cd
->child
->exe
)(ctx
, cd
->child
, theinput
);
865 void *exe_strtobool(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
866 struct coercedata
*cd
=thenode
->localdata
;
867 char *ch
=(cd
->child
->exe
)(ctx
, cd
->child
, theinput
);
869 if (!ch
|| *ch
=='\0' || (*ch
=='0' && ch
[1]=='\0')) {
876 void *exe_inttobool(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
877 struct coercedata
*cd
=thenode
->localdata
;
879 if ((cd
->child
->exe
)(ctx
, cd
->child
, theinput
)) {
886 struct searchNode
*coerceNode(searchCtx
*ctx
, struct searchNode
*thenode
, int type
) {
887 struct searchNode
*anode
;
888 struct coercedata
*cd
;
890 /* You can't coerce a NULL */
894 /* No effort required to coerce to the same type */
895 if (type
==(thenode
->returntype
& RETURNTYPE_TYPE
))
898 anode
=(struct searchNode
*)malloc(sizeof(struct searchNode
));
899 anode
->localdata
=cd
=(struct coercedata
*)malloc(sizeof(struct coercedata
));
901 anode
->returntype
=type
; /* We'll return what they want, always */
902 anode
->free
=free_coerce
;
905 case RETURNTYPE_STRING
:
906 /* For a string we'll need a buffer */
907 /* A 64-bit number prints out to 20 digits, this leaves some slack */
908 cd
->u
.stringbuf
=malloc(25);
909 anode
->free
=free_coercestring
;
911 switch(thenode
->returntype
& RETURNTYPE_TYPE
) {
914 if (thenode
->returntype
& RETURNTYPE_CONST
) {
915 /* Constant node: sort it out now */
916 sprintf(cd
->u
.stringbuf
, "%lu", (unsigned long)thenode
->exe(ctx
, thenode
, NULL
));
917 anode
->exe
=exe_tostr_null
;
918 anode
->returntype
|= RETURNTYPE_CONST
;
921 anode
->exe
=exe_inttostr
;
925 case RETURNTYPE_BOOL
:
926 if (thenode
->returntype
& RETURNTYPE_CONST
) {
927 /* Constant bool value */
928 if (thenode
->exe(ctx
, thenode
,NULL
)) {
930 sprintf(cd
->u
.stringbuf
, "1");
932 cd
->u
.stringbuf
[0] = '\0';
934 anode
->exe
=exe_tostr_null
;
935 anode
->returntype
|= RETURNTYPE_CONST
;
937 /* Variable bool value */
938 anode
->exe
=exe_booltostr
;
946 switch (thenode
->returntype
& RETURNTYPE_TYPE
) {
947 case RETURNTYPE_STRING
:
948 if (thenode
->returntype
& RETURNTYPE_CONST
) {
949 cd
->u
.val
=strtoul((thenode
->exe
)(ctx
, thenode
, NULL
), NULL
, 10);
950 anode
->exe
=exe_val_null
;
951 anode
->returntype
|= RETURNTYPE_CONST
;
953 anode
->exe
=exe_strtoint
;
958 case RETURNTYPE_BOOL
:
959 if (thenode
->returntype
& RETURNTYPE_CONST
) {
960 if ((thenode
->exe
)(ctx
, thenode
,NULL
))
965 anode
->exe
=exe_val_null
;
966 anode
->returntype
|= RETURNTYPE_CONST
;
968 anode
->exe
=exe_booltoint
;
975 case RETURNTYPE_BOOL
:
977 switch (thenode
->returntype
& RETURNTYPE_TYPE
) {
978 case RETURNTYPE_STRING
:
979 if (thenode
->returntype
& RETURNTYPE_CONST
) {
980 char *rv
=(char *)((thenode
->exe
)(ctx
, thenode
, NULL
));
981 if (!rv
|| *rv
=='\0' || (*rv
=='0' && rv
[1]=='\0'))
986 anode
->exe
=exe_val_null
;
987 anode
->returntype
|= RETURNTYPE_CONST
;
989 anode
->exe
=exe_strtobool
;
995 if (thenode
->returntype
& RETURNTYPE_CONST
) {
996 if ((thenode
->exe
)(ctx
, thenode
,NULL
))
1001 anode
->exe
=exe_val_null
;
1002 anode
->returntype
|= RETURNTYPE_CONST
;
1004 anode
->exe
=exe_inttobool
;
1014 /* Literals always return constant strings... */
1015 void *literal_exe(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
1016 if (thenode
->localdata
)
1017 return ((sstring
*)thenode
->localdata
)->content
;
1022 void literal_free(searchCtx
*ctx
, struct searchNode
*thenode
) {
1023 freesstring(thenode
->localdata
);
1027 void nssnprintf(char *buf
, size_t size
, const char *format
, nick
*np
) {
1040 for(p
=format
;*p
;p
++) {
1042 if(!sbaddchar(&b
, *p
))
1050 if(!sbaddchar(&b
, *p
))
1058 c
= np
->nick
; break;
1060 c
= np
->ident
; break;
1062 c
= np
->host
->name
->content
; break;
1064 snprintf(hostbuf
, sizeof(hostbuf
), "%s", IPtostr(np
->ipaddress
));
1068 snprintf(hostbuf
, sizeof(hostbuf
), "%s!%s@%s", np
->nick
, np
->ident
, IPtostr(np
->ipaddress
));
1072 c
= "(bad format specifier)";
1075 if(!sbaddstr(&b
, c
))
1087 static char *var_tochar(searchCtx
*ctx
, char *arg
, searchNode
**variable
) {
1088 *variable
= ctx
->parser(ctx
, arg
);
1089 if (!(*variable
= coerceNode(ctx
, *variable
, RETURNTYPE_STRING
)))
1092 if(!((*variable
)->returntype
& RETURNTYPE_CONST
)) {
1093 parseError
= "only constant variables allowed";
1094 ((*variable
)->free
)(ctx
, *variable
);
1098 return (char *)((*variable
)->exe
)(ctx
, *variable
, NULL
);
1101 void free_val_null(searchCtx
*ctx
, struct searchNode
*thenode
) {
1104 struct searchVariable
*var_register(searchCtx
*ctx
, char *arg
, int type
) {
1105 searchNode
*variable
;
1106 struct searchVariable
*us
;
1110 if(ctx
->lastvar
>= MAX_VARIABLES
) {
1111 parseError
= "Maximum number of variables reached";
1115 us
= &ctx
->vars
[ctx
->lastvar
];
1117 var
= var_tochar(ctx
, arg
, &variable
);
1121 strlcpy(us
->name
, var
, sizeof(us
->name
));
1122 (variable
->free
)(ctx
, variable
);
1124 for(i
=0;i
<ctx
->lastvar
;i
++) {
1125 if(!strcmp(us
->name
, ctx
->vars
[i
].name
)) {
1126 parseError
= "variable name already in use";
1132 us
->data
.returntype
= type
;
1133 us
->data
.localdata
= &us
->cdata
;
1134 us
->data
.exe
= exe_val_null
;
1135 us
->data
.free
= free_val_null
;
1137 us
->cdata
.child
= NULL
;
1141 searchNode
*var_get(searchCtx
*ctx
, char *arg
) {
1142 searchNode
*variable
, *found
= NULL
;
1144 char *var
= var_tochar(ctx
, arg
, &variable
);
1148 for(i
=0;i
<ctx
->lastvar
;i
++) {
1149 if(!strcmp(var
, ctx
->vars
[i
].name
)) {
1150 found
= &ctx
->vars
[i
].data
;
1154 (variable
->free
)(ctx
, variable
);
1157 parseError
= "variable not found";
1161 void var_setstr(struct searchVariable
*v
, char *data
) {
1162 v
->cdata
.u
.stringbuf
= data
;
1165 void displaystrerror(replyFunc reply
, nick
*np
, const char *input
) {
1168 if((parseStrErrorPos
>= 0) && (parseStrErrorPos
< sizeof(buf
) - 3)) {
1171 for(i
=0;i
<parseStrErrorPos
;i
++)
1177 reply(np
, "%s", input
);
1178 reply(np
, "%s", buf
);
1181 reply(np
, "Parse error: %s", parseStrError
);
1184 struct searchNode
*argtoconststr(char *command
, searchCtx
*ctx
, char *arg
, char **p
) {
1185 struct searchNode
*c
;
1186 static char errorbuf
[512];
1188 c
= ctx
->parser(ctx
, arg
);
1189 if (!(c
= coerceNode(ctx
, c
, RETURNTYPE_STRING
))) {
1190 snprintf(errorbuf
, sizeof(errorbuf
), "%s: unable to coerce argument to string", command
);
1191 parseError
= errorbuf
;
1195 if (!(c
->returntype
& RETURNTYPE_CONST
)) {
1196 snprintf(errorbuf
, sizeof(errorbuf
), "%s: constant argument required", command
);
1197 parseError
= errorbuf
;
1203 *p
= (char *)(c
->exe
)(ctx
, c
, NULL
);