5 #include "../irc/irc_config.h"
6 #include "../lib/irc_string.h"
7 #include "../parser/parser.h"
8 #include "../control/control.h"
9 #include "../lib/splitline.h"
10 #include "../lib/version.h"
11 #include "../lib/stringbuf.h"
12 #include "../lib/strlfunc.h"
13 #include "../lib/array.h"
14 #include "newsearch.h"
19 CommandTree
*searchCmdTree
;
20 searchList
*globalterms
= NULL
;
22 int do_nicksearch(void *source
, int cargc
, char **cargv
);
23 int do_chansearch(void *source
, int cargc
, char **cargv
);
24 int do_usersearch(void *source
, int cargc
, char **cargv
);
25 int do_whowassearch(void *source
, int cargc
, char **cargv
);
27 void printnick_channels(searchCtx
*, nick
*, nick
*);
28 void printchannel(searchCtx
*, nick
*, chanindex
*);
29 void printchannel_topic(searchCtx
*, nick
*, chanindex
*);
30 void printchannel_services(searchCtx
*, nick
*, chanindex
*);
32 UserDisplayFunc defaultuserfn
= printuser
;
33 NickDisplayFunc defaultnickfn
= printnick
;
34 ChanDisplayFunc defaultchanfn
= printchannel
;
35 WhowasDisplayFunc defaultwhowasfn
= printwhowas
;
37 searchCmd
*reg_nicksearch
, *reg_chansearch
, *reg_usersearch
, *reg_whowassearch
;
38 void displaycommandhelp(nick
*, Command
*);
39 void displaystrerror(replyFunc
, nick
*, const char *);
41 searchCmd
*registersearchcommand(char *name
, int level
, CommandHandler cmd
, void *defaultdisplayfunc
) {
45 registercontrolhelpfunccmd(name
, NO_OPER
,4, cmd
, &displaycommandhelp
);
46 acmd
=(struct searchCmd
*)malloc(sizeof(struct searchCmd
));
48 Error("newsearch", ERR_ERROR
, "malloc failed: registersearchcommand");
53 acmd
->name
= getsstring( name
, NSMAX_COMMAND_LEN
);
54 acmd
->outputtree
= newcommandtree();
55 acmd
->searchtree
= newcommandtree();
57 addcommandtotree(searchCmdTree
, name
, 0, 0, (CommandHandler
)acmd
);
59 for (sl
=globalterms
; sl
; sl
=sl
->next
) {
60 addcommandexttotree(acmd
->searchtree
, sl
->name
->content
, 0, 1, (CommandHandler
)sl
->cmd
, sl
->help
);
66 void deregistersearchcommand(searchCmd
*scmd
) {
67 deregistercontrolcmd(scmd
->name
->content
, (CommandHandler
)scmd
->handler
);
68 destroycommandtree(scmd
->outputtree
);
69 destroycommandtree(scmd
->searchtree
);
70 deletecommandfromtree(searchCmdTree
, scmd
->name
->content
, (CommandHandler
) scmd
);
71 freesstring(scmd
->name
);
75 void displaycommandhelp(nick
*np
, Command
*cmd
) {
77 Command
*cmdlist
[100], *acmdlist
[100];
80 n
=getcommandlist(searchCmdTree
,cmdlist
,100);
82 /* note: we may want to only deregister a command if we've already registered it, for now, try de-registering new commands anyway */
83 if ( ((searchCmd
*)cmdlist
[i
]->handler
)->handler
!= cmd
->handler
)
85 acmd
= ((searchCmd
*)(cmdlist
[i
]->handler
));
87 controlreply(np
, "Usage: [flags] <criteria>\n");
88 controlreply(np
, "Flags:\n");
89 controlreply(np
, " -l int : Limit number of rows of results\n");
90 controlreply(np
, " -d string : a valid output format for the results\n");
91 controlreply(np
, " -s subset : ipmask subset of network to search (only for node search)\n");
92 controlreply(np
, " \n");
93 controlreply(np
, "Available Output Formats:\n");
95 m
=getcommandlist(acmd
->outputtree
,acmdlist
,100);
97 if ( controlpermitted( acmdlist
[j
]->level
, np
) ) {
98 char *help
=(char *)acmdlist
[j
]->ext
;
99 if ( help
&& help
[0] != '\0')
100 controlreply(np
, "%-10s: %s\n", acmdlist
[j
]->command
->content
, help
);
102 controlreply(np
, "%s\n", acmdlist
[j
]->command
->content
);
106 controlreply(np
, " \n");
107 controlreply(np
, "Available Global Commands and Operators:\n" );
108 m
=getcommandlist(acmd
->searchtree
,acmdlist
,100);
110 if ( acmdlist
[j
]->maxparams
) {
111 char *help
=(char *)acmdlist
[j
]->ext
;
112 if ( help
&& help
[0] != '\0')
113 controlreply(np
, "%-10s: %s\n", acmdlist
[j
]->command
->content
, help
);
115 controlreply(np
, "%s\n", acmdlist
[j
]->command
->content
);
119 controlreply(np
, " \n");
120 controlreply(np
, "Available Commands and Operators for %s:\n", acmd
->name
->content
);
122 m
=getcommandlist(acmd
->searchtree
,acmdlist
,100);
124 if ( !acmdlist
[j
]->maxparams
&& controlpermitted( acmdlist
[j
]->level
, np
) ) {
125 char *help
=(char *)acmdlist
[j
]->ext
;
126 if ( help
&& help
[0] != '\0')
127 controlreply(np
, "%-10s: %s\n", acmdlist
[j
]->command
->content
, help
);
129 controlreply(np
, "%s\n", acmdlist
[j
]->command
->content
);
135 void regdisp( searchCmd
*cmd
, const char *name
, void *handler
, int level
, char *help
) {
136 addcommandexttotree(cmd
->outputtree
, name
, level
, 0, (CommandHandler
) handler
, (char *)help
);
139 void unregdisp( searchCmd
*cmd
, const char *name
, void *handler
) {
140 deletecommandfromtree(cmd
->outputtree
, name
, (CommandHandler
) handler
);
143 void *findcommandinlist( searchList
*sl
, char *name
){
145 if(strcmp(sl
->name
->content
,name
) == 0 ) {
153 const char *parseError
;
154 /* used for *_free functions that need to warn users of certain things
155 i.e. hitting too many users in a (kill) or (gline) */
156 nick
*senderNSExtern
;
159 searchCmdTree
=newcommandtree();
161 reg_nicksearch
= (searchCmd
*)registersearchcommand("nicksearch",NO_OPER
,&do_nicksearch
, printnick
);
162 reg_chansearch
= (searchCmd
*)registersearchcommand("chansearch",NO_OPER
,&do_chansearch
, printchannel
);
163 reg_usersearch
= (searchCmd
*)registersearchcommand("usersearch",NO_OPER
,&do_usersearch
, printuser
);
164 reg_whowassearch
= (searchCmd
*)registersearchcommand("whowassearch",NO_OPER
,&do_whowassearch
, printwhowas
);
166 /* Boolean operations */
167 registerglobalsearchterm("and",and_parse
, "usage: (and (X) (X))" );
168 registerglobalsearchterm("not",not_parse
, "usage: (not (X))");
169 registerglobalsearchterm("or",or_parse
, "usage: (or (X) (X))" );
171 registerglobalsearchterm("eq",eq_parse
, "usage: (eq (X) Y)");
173 registerglobalsearchterm("lt",lt_parse
, "usage: (lt (X) int)");
174 registerglobalsearchterm("gt",gt_parse
, "usage: (gt (X) int)");
176 /* String operations */
177 registerglobalsearchterm("match",match_parse
, "usage: (match (X) string)");
178 registerglobalsearchterm("regex",regex_parse
, "usage: (regex (X) string)");
179 registerglobalsearchterm("length",length_parse
, "usage: (length string)");
181 /* Nickname operations */
182 registersearchterm(reg_nicksearch
, "hostmask",hostmask_parse
, 0, "The user's nick!user@host; \"hostmask real\" returns nick!user@host\rreal");
183 registersearchterm(reg_whowassearch
, "hostmask",hostmask_parse
, 0, "The user's nick!user@host; \"hostmask real\" returns nick!user@host\rreal");
185 registersearchterm(reg_nicksearch
, "realname",realname_parse
, 0, "User's current realname"); /* nick only */
186 registersearchterm(reg_whowassearch
, "realname",realname_parse
, 0, "User's current realname"); /* nick only */
188 registersearchterm(reg_nicksearch
, "away",away_parse
, 0, "User's current away message");
189 registersearchterm(reg_whowassearch
, "away",away_parse
, 0, "User's current away message");
190 registersearchterm(reg_nicksearch
, "authname",authname_parse
, 0, "User's current authname or false");
191 registersearchterm(reg_whowassearch
, "authname",authname_parse
, 0, "User's current authname or false");
192 registersearchterm(reg_nicksearch
, "authts",authts_parse
, 0, "User's Auth timestamp");
193 registersearchterm(reg_whowassearch
, "authts",authts_parse
, 0, "User's Auth timestamp");
194 registersearchterm(reg_nicksearch
, "ident",ident_parse
, 0, "User's current ident");
195 registersearchterm(reg_whowassearch
, "ident",ident_parse
, 0, "User's current ident");
196 registersearchterm(reg_nicksearch
, "host",host_parse
, 0, "User's host, allow \"host real\" to match real host");
197 registersearchterm(reg_whowassearch
, "host",host_parse
, 0, "User's host, allow \"host real\" to match real host");
198 registersearchterm(reg_nicksearch
, "channel",channel_parse
, 0, "Valid Channel Name to match users against"); /* nick only */
199 registersearchterm(reg_nicksearch
, "timestamp",timestamp_parse
, 0, "User's Timestamp");
200 registersearchterm(reg_whowassearch
, "timestamp",timestamp_parse
, 0, "User's Timestamp");
201 registersearchterm(reg_nicksearch
, "country",country_parse
, 0, "2 letter country code (data source is geoip)");
202 registersearchterm(reg_nicksearch
, "ip",ip_parse
, 0, "User's IP - ipv4 or ipv6 format as appropriate. Note: not 6to4");
203 registersearchterm(reg_whowassearch
, "ip",ip_parse
, 0, "User's IP - ipv4 or ipv6 format as appropriate. Note: not 6to4");
204 registersearchterm(reg_nicksearch
, "channels",channels_parse
, 0, "Channel Count"); /* nick only */
205 registersearchterm(reg_nicksearch
, "server",server_parse
, 0, "Server Name. Either (server string) or (match (server) string)");
206 registersearchterm(reg_whowassearch
, "server",server_parse
, 0, "Server Name. Either (server string) or (match (server) string)");
207 registersearchterm(reg_whowassearch
, "server",server_parse
, 0, "Server Name. Either (server string) or (match (server) string)");
208 registersearchterm(reg_nicksearch
, "authid",authid_parse
, 0, "User's Auth ID");
209 registersearchterm(reg_whowassearch
, "authid",authid_parse
, 0, "User's Auth ID");
210 registersearchterm(reg_nicksearch
, "cidr",cidr_parse
, 0, "CIDR matching");
211 registersearchterm(reg_whowassearch
, "cidr",cidr_parse
, 0, "CIDR matching");
212 registersearchterm(reg_nicksearch
, "ipvsix",ipv6_parse
, 0, "IPv6 user");
213 registersearchterm(reg_whowassearch
, "ipvsix",ipv6_parse
, 0, "IPv6 user");
214 registersearchterm(reg_nicksearch
, "message",message_parse
, 0, "Last message");
216 /* Whowas operations */
217 registersearchterm(reg_whowassearch
, "quit",quit_parse
, 0, "User quit");
218 registersearchterm(reg_whowassearch
, "killed",killed_parse
, 0, "User was killed");
219 registersearchterm(reg_whowassearch
, "renamed",renamed_parse
, 0, "User changed nick");
220 registersearchterm(reg_whowassearch
, "age",age_parse
, 0, "Whowas record age in seconds");
221 registersearchterm(reg_whowassearch
, "newnick",newnick_parse
, 0, "New nick (for rename whowas records)");
222 registersearchterm(reg_whowassearch
, "reason",reason_parse
, 0, "Quit/kill reason");
224 /* Channel operations */
225 registersearchterm(reg_chansearch
, "exists",exists_parse
, 0, "Returns if channel exists on network. Note: newserv may store data on empty channels"); /* channel only */
226 registersearchterm(reg_chansearch
, "services",services_parse
, 0, ""); /* channel only */
227 registersearchterm(reg_chansearch
, "size",size_parse
, 0, "Channel user count"); /* channel only */
228 registersearchterm(reg_chansearch
, "name",name_parse
, 0, "Channel Name"); /* channel only */
229 registersearchterm(reg_chansearch
, "topic",topic_parse
, 0, "Channel topic"); /* channel only */
230 registersearchterm(reg_chansearch
, "oppct",oppct_parse
, 0, "Percentage Opped"); /* channel only */
231 registersearchterm(reg_chansearch
, "cumodecount",cumodecount_parse
, 0, "Count of users with given channel modes"); /* channel only */
232 registersearchterm(reg_chansearch
, "cumodepct",cumodepct_parse
, 0, "Percentage of users with given channel modes"); /* channel only */
233 registersearchterm(reg_chansearch
, "uniquehostpct",hostpct_parse
, 0, "uniquehost percent"); /* channel only */
234 registersearchterm(reg_chansearch
, "authedpct",authedpct_parse
, 0, "Percentage of authed users"); /* channel only */
235 registersearchterm(reg_chansearch
, "kick",kick_parse
, 0, "KICK users channels in newsearch result. Note: evaluation order"); /* channel only */
237 /* Nickname / channel operations */
238 registersearchterm(reg_chansearch
, "modes",modes_parse
, 0, "Channel Modes");
239 registersearchterm(reg_nicksearch
, "modes",modes_parse
, 0, "User Modes");
240 registersearchterm(reg_whowassearch
, "modes",modes_parse
, 0, "User Modes");
241 registersearchterm(reg_chansearch
, "nick",nick_parse
, 0, "Nickname");
242 registersearchterm(reg_nicksearch
, "nick",nick_parse
, 0, "Nickname");
243 registersearchterm(reg_whowassearch
, "nick",nick_parse
, 0, "Nickname");
245 /* Kill / gline parameters */
246 registersearchterm(reg_chansearch
,"kill",kill_parse
, 0, "KILL users in newsearch result. Note: evaluation order");
247 registersearchterm(reg_chansearch
,"gline",gline_parse
, 0, "GLINE users in newsearch result. Note: evaluation order");
248 registersearchterm(reg_nicksearch
,"kill",kill_parse
, 0, "KILL users in newsearch result. Note: evaluation order");
249 registersearchterm(reg_nicksearch
,"gline",gline_parse
, 0, "GLINE users in newsearch result. Note: evaluation order");
250 registersearchterm(reg_whowassearch
,"gline",gline_parse
, 0, "GLINE users in newsearch result. Note: evaluation order");
252 /* Iteration functionality */
253 registerglobalsearchterm("any",any_parse
, "usage: any (generatorfn x) (fn ... (var x) ...)");
254 registerglobalsearchterm("all",all_parse
, "usage: all (generatorfn x) (fn ... (var x) ...)");
255 registerglobalsearchterm("var",var_parse
, "usage: var variable");
257 /* Iterable functions */
258 registersearchterm(reg_nicksearch
, "channeliter",channeliter_parse
, 0, "Channel Iterable function - usage: (any (channeliter x) (match (var x) #twilight*))"); /* nick only */
259 registersearchterm(reg_chansearch
, "nickiter",nickiter_parse
, 0, "Nick Iterable function - usage: (any (nickiter x) (match (var x) nickname*))"); /* channel only */
261 /* Functions that work on strings?! */
262 registersearchterm(reg_nicksearch
, "cumodes", cumodes_parse
, 0, "usage: (cumodes (var x) <modes>)");
263 registersearchterm(reg_chansearch
, "cumodes", cumodes_parse
, 0, "usage: (cumodes (var x) <modes>)");
265 /* Notice functionality */
266 registersearchterm(reg_chansearch
,"notice",notice_parse
, 0, "NOTICE users in newsearch result. Note: evaluation order");
267 registersearchterm(reg_nicksearch
,"notice",notice_parse
, 0, "NOTICE users in newsearch result. Note: evaluation order");
269 /* Nick output filters */
270 regdisp(reg_nicksearch
,"default",printnick
, 0, "");
271 regdisp(reg_nicksearch
,"channels",printnick_channels
, 0, "include channels in output");
273 /* Channel output filters */
274 regdisp(reg_chansearch
,"default",printchannel
, 0, "");
275 regdisp(reg_chansearch
,"topic",printchannel_topic
, 0, "display channel topics");
276 regdisp(reg_chansearch
,"services",printchannel_services
, 0, "display services on channels");
278 /* Nick output filters */
279 regdisp(reg_usersearch
,"default",printuser
, 0, "");
284 searchList
*sl
, *psl
;
286 Command
*cmdlist
[100];
293 n
=getcommandlist(searchCmdTree
,cmdlist
,100);
295 deregistersearchterm( (searchCmd
*)cmdlist
[i
]->handler
, psl
->name
->content
, psl
->cmd
);
298 freesstring(psl
->name
);
304 deregistersearchcommand( reg_nicksearch
);
305 deregistersearchcommand( reg_chansearch
);
306 deregistersearchcommand( reg_usersearch
);
307 destroycommandtree( searchCmdTree
);
310 void registerglobalsearchterm(char *term
, parseFunc parsefunc
, char *help
) {
312 Command
*cmdlist
[100];
313 searchList
*sl
= malloc(sizeof(searchList
));
315 Error("newsearch", ERR_ERROR
, "malloc failed: registerglobalsearchterm");
320 sl
->name
= getsstring(term
, NSMAX_COMMAND_LEN
);
323 int len
=strlen(help
);
324 sl
->help
=(char *)malloc(len
+1);
326 Error("newsearch", ERR_ERROR
, "malloc failed: registerglobalsearchterm");
329 strncpy(sl
->help
, help
, len
);
330 sl
->help
[len
] = '\0';
336 if ( globalterms
!= NULL
) {
337 sl
->next
= globalterms
;
341 n
=getcommandlist(searchCmdTree
,cmdlist
,100);
343 /* maxparams is set to 1 to indicate a global term */
344 /* access level is set to 0 for all global terms */
345 addcommandexttotree( ((searchCmd
*)cmdlist
[i
]->handler
)->searchtree
,term
, 0, 1, (CommandHandler
) parsefunc
, help
);
349 void deregisterglobalsearchterm(char *term
, parseFunc parsefunc
) {
351 Command
*cmdlist
[100];
352 searchList
*sl
, *psl
=NULL
;
354 for (sl
=globalterms
; sl
; sl
=sl
->next
) {
355 if ( strcmp( sl
->name
->content
, term
) == 0 ) {
363 psl
->next
= sl
->next
;
366 n
=getcommandlist(searchCmdTree
,cmdlist
,100);
368 deletecommandfromtree( ((searchCmd
*)cmdlist
[i
]->handler
)->searchtree
, term
, (CommandHandler
) parsefunc
);
370 freesstring(sl
->name
);
375 void registersearchterm(searchCmd
*cmd
, char *term
, parseFunc parsefunc
, int level
, char *help
) {
376 /* NOTE: global terms are added to the tree elsewhere as we set maxparams to 1 to indicate global */
377 addcommandexttotree(cmd
->searchtree
, term
, level
, 0, (CommandHandler
) parsefunc
, help
);
380 void deregistersearchterm(searchCmd
*cmd
, char *term
, parseFunc parsefunc
) {
381 /* NOTE: global terms are removed from the tree within deregisterglobalsearchterm */
382 deletecommandfromtree(cmd
->searchtree
, term
, (CommandHandler
) parsefunc
);
385 static void controlwallwrapper(int level
, char *format
, ...) __attribute__ ((format (printf
, 2, 3)));
386 static void controlwallwrapper(int level
, char *format
, ...) {
390 va_start(ap
, format
);
391 vsnprintf(buf
, sizeof(buf
), format
, ap
);
392 controlwall(NO_OPER
, level
, "%s", buf
);
396 int parseopts(int cargc
, char **cargv
, int *arg
, int *limit
, void **subset
, void *display
, CommandTree
*sl
, replyFunc reply
, void *sender
) {
399 struct irc_in_addr sin
; unsigned char bits
;
401 if (*cargv
[0] == '-') {
405 for (ch
=cargv
[0]+1;*ch
;ch
++) {
409 reply(sender
,"Error: -l switch requires an argument (for help, see help <searchcmd>)");
412 *limit
=strtoul(cargv
[(*arg
)++],NULL
,10);
417 reply(sender
,"Error: -d switch requires an argument (for help, see help <searchcmd>)");
420 cmd
=findcommandintree(sl
, cargv
[*arg
],1);
422 reply(sender
,"Error: unknown output format %s (for help, see help <searchcmd>)",cargv
[*arg
]);
425 if ( !controlpermitted( cmd
->level
, sender
) ) {
426 reply(sender
,"Error: Access Denied for output format %s (for help, see help <searchcmd>)", cargv
[*arg
]);
429 *((void **)display
)=(void *)cmd
->handler
;
434 if (subset
== NULL
) {
435 reply(sender
,"Error: -s switch not supported for this search.");
439 reply(sender
,"Error: -s switch requires an argument (for help, see help <searchcmd>)");
442 if (ipmask_parse(cargv
[*arg
], &sin
, &bits
) == 0) {
443 reply(sender
, "Error: Invalid CIDR mask supplied (for help, see help <searchcmd>)");
446 *subset
= (void *)refnode(iptree
, &sin
, bits
);
451 reply(sender
,"Unrecognised flag -%c. (for help, see help <searchcmd>)",*ch
);
460 void newsearch_ctxinit(searchCtx
*ctx
, searchParseFunc searchfn
, replyFunc replyfn
, wallFunc wallfn
, void *arg
, searchCmd
*cmd
, nick
*np
, void *displayfn
, int limit
, void *target
) {
461 memset(ctx
, 0, sizeof(searchCtx
));
463 ctx
->reply
= replyfn
;
465 ctx
->parser
= searchfn
;
467 ctx
->searchcmd
= cmd
;
470 ctx
->target
= target
;
471 ctx
->displayfn
= displayfn
;
474 int do_nicksearch_real(replyFunc reply
, wallFunc wall
, void *source
, int cargc
, char **cargv
) {
475 nick
*sender
= source
;
478 NickDisplayFunc display
=defaultnickfn
;
483 reply( sender
, "Usage: [flags] <criteria>");
484 reply( sender
, "For help, see help nicksearch");
488 ret
= parseopts(cargc
, cargv
, &arg
, &limit
, NULL
, (void *)&display
, reg_nicksearch
->outputtree
, reply
, sender
);
493 reply(sender
,"No search terms - aborting.");
498 rejoinline(cargv
[arg
],cargc
-arg
);
501 tree
= parse_string(reg_nicksearch
, cargv
[arg
]);
503 displaystrerror(reply
, sender
, cargv
[arg
]);
507 ast_nicksearch(tree
->root
, reply
, sender
, wall
, display
, NULL
, NULL
, limit
, NULL
);
514 int do_nicksearch(void *source
, int cargc
, char **cargv
) {
515 return do_nicksearch_real(controlreply
, controlwallwrapper
, source
, cargc
, cargv
);
518 void nicksearch_exe(struct searchNode
*search
, searchCtx
*ctx
) {
521 unsigned int cmarker
;
522 unsigned int tchans
=0,uchans
=0;
524 nick
*np
, *sender
= ctx
->sender
;
525 senderNSExtern
= sender
;
526 NickDisplayFunc display
= ctx
->displayfn
;
527 int limit
= ctx
->limit
;
529 /* Get a marker value to mark "seen" channels for unique count */
530 cmarker
=nextchanmarker();
532 /* The top-level node needs to return a BOOL */
533 search
=coerceNode(ctx
, search
, RETURNTYPE_BOOL
);
535 for (i
=0;i
<NICKHASHSIZE
;i
++) {
536 for (np
=ctx
->target
? ctx
->target
: nicktable
[i
];np
;np
=np
->next
) {
537 if ((search
->exe
)(ctx
, search
, np
)) {
538 /* Add total channels */
539 tchans
+= np
->channels
->cursi
;
541 /* Check channels for uniqueness */
542 cs
=(channel
**)np
->channels
->content
;
543 for (j
=0;j
<np
->channels
->cursi
;j
++) {
544 if (cs
[j
]->index
->marker
!= cmarker
) {
545 cs
[j
]->index
->marker
=cmarker
;
551 display(ctx
, sender
, np
);
554 ctx
->reply(sender
, "--- More than %d matches, skipping the rest",limit
);
564 ctx
->reply(sender
,"--- End of list: %d matches; users were on %u channels (%u unique, %.1f average clones)",
565 matches
, tchans
, uchans
, (float)tchans
/uchans
);
568 int do_whowassearch_real(replyFunc reply
, wallFunc wall
, void *source
, int cargc
, char **cargv
) {
569 nick
*sender
= source
;
572 WhowasDisplayFunc display
=defaultwhowasfn
;
577 reply( sender
, "Usage: [flags] <criteria>");
578 reply( sender
, "For help, see help whowassearch");
582 ret
= parseopts(cargc
, cargv
, &arg
, &limit
, NULL
, (void *)&display
, reg_whowassearch
->outputtree
, reply
, sender
);
587 reply(sender
,"No search terms - aborting.");
592 rejoinline(cargv
[arg
],cargc
-arg
);
595 tree
= parse_string(reg_whowassearch
, cargv
[arg
]);
597 displaystrerror(reply
, sender
, cargv
[arg
]);
601 ast_whowassearch(tree
->root
, reply
, sender
, wall
, display
, NULL
, NULL
, limit
, NULL
);
608 int do_whowassearch(void *source
, int cargc
, char **cargv
) {
609 return do_whowassearch_real(controlreply
, controlwallwrapper
, source
, cargc
, cargv
);
612 void whowassearch_exe(struct searchNode
*search
, searchCtx
*ctx
) {
615 nick
*sender
= ctx
->sender
;
616 senderNSExtern
= sender
;
617 WhowasDisplayFunc display
= ctx
->displayfn
;
618 int limit
= ctx
->limit
;
620 /* The top-level node needs to return a BOOL */
621 search
=coerceNode(ctx
, search
, RETURNTYPE_BOOL
);
623 for (i
= whowasoffset
; i
< whowasoffset
+ WW_MAXENTRIES
; i
++) {
627 ww
= &whowasrecs
[i
% WW_MAXENTRIES
];
629 if (ww
->type
== WHOWAS_UNUSED
)
633 /* Note: We're passing the nick to the filter function. The original
634 * whowas record is in the nick's ->next field. */
635 if ((search
->exe
)(ctx
, search
, &ww
->nick
)) {
637 display(ctx
, sender
, ww
);
640 ctx
->reply(sender
, "--- More than %d matches, skipping the rest",limit
);
648 ctx
->reply(sender
,"--- End of list: %d matches", matches
);
651 int do_chansearch_real(replyFunc reply
, wallFunc wall
, void *source
, int cargc
, char **cargv
) {
652 nick
*sender
= source
;
655 ChanDisplayFunc display
=defaultchanfn
;
660 reply( sender
, "Usage: [flags] <criteria>");
661 reply( sender
, "For help, see help chansearch");
665 ret
= parseopts(cargc
, cargv
, &arg
, &limit
, NULL
, (void *)&display
, reg_chansearch
->outputtree
, reply
, sender
);
670 reply(sender
,"No search terms - aborting.");
675 rejoinline(cargv
[arg
],cargc
-arg
);
678 tree
= parse_string(reg_chansearch
, cargv
[arg
]);
680 displaystrerror(reply
, sender
, cargv
[arg
]);
684 ast_chansearch(tree
->root
, reply
, sender
, wall
, display
, NULL
, NULL
, limit
, NULL
);
691 int do_chansearch(void *source
, int cargc
, char **cargv
) {
692 return do_chansearch_real(controlreply
, controlwallwrapper
, source
, cargc
, cargv
);
695 void chansearch_exe(struct searchNode
*search
, searchCtx
*ctx
) {
699 nick
*sender
= ctx
->sender
;
700 senderNSExtern
= sender
;
701 ChanDisplayFunc display
= ctx
->displayfn
;
702 int limit
= ctx
->limit
;
704 search
=coerceNode(ctx
, search
, RETURNTYPE_BOOL
);
706 for (i
=0;i
<CHANNELHASHSIZE
;i
++) {
707 for (cip
=ctx
->target
? ctx
->target
: chantable
[i
];cip
;cip
=cip
->next
) {
708 if ((search
->exe
)(ctx
, search
, cip
)) {
710 display(ctx
, sender
, cip
);
712 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 search
=coerceNode(ctx
, search
, RETURNTYPE_BOOL
);
780 for (i
=0;i
<AUTHNAMEHASHSIZE
;i
++) {
781 for (aup
=ctx
->target
? ctx
->target
: authnametable
[i
];aup
;aup
=aup
->next
) {
782 if ((search
->exe
)(ctx
, search
, aup
)) {
784 display(ctx
, sender
, aup
);
786 ctx
->reply(sender
, "--- More than %d matches, skipping the rest",limit
);
796 ctx
->reply(sender
,"--- End of list: %d matches", matches
);
799 /* Free a coerce node */
800 void free_coerce(searchCtx
*ctx
, struct searchNode
*thenode
) {
801 struct coercedata
*cd
=thenode
->localdata
;
803 cd
->child
->free(ctx
, cd
->child
);
804 free(thenode
->localdata
);
808 /* Free a coerce node with a stringbuf allocated */
809 void free_coercestring(searchCtx
*ctx
, struct searchNode
*thenode
) {
810 free(((struct coercedata
*)thenode
->localdata
)->u
.stringbuf
);
811 free_coerce(ctx
, thenode
);
814 /* exe_tostr_null: return the constant string */
815 void *exe_tostr_null(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
816 struct coercedata
*cd
=thenode
->localdata
;
818 return cd
->u
.stringbuf
;
821 /* exe_val_null: return the constant value */
822 void *exe_val_null(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
823 struct coercedata
*cd
=thenode
->localdata
;
825 return (void *)cd
->u
.val
;
828 /* Lots of very dull type conversion functions */
829 void *exe_inttostr(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
830 struct coercedata
*cd
=thenode
->localdata
;
832 sprintf(cd
->u
.stringbuf
, "%lu", (unsigned long)(cd
->child
->exe
)(ctx
, cd
->child
, theinput
));
834 return cd
->u
.stringbuf
;
837 void *exe_booltostr(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
838 struct coercedata
*cd
=thenode
->localdata
;
840 if ((cd
->child
->exe
)(ctx
, cd
->child
, theinput
)) {
841 sprintf(cd
->u
.stringbuf
,"1");
843 cd
->u
.stringbuf
[0]='\0';
846 return cd
->u
.stringbuf
;
849 void *exe_strtoint(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
850 struct coercedata
*cd
=thenode
->localdata
;
852 return (void *)strtoul((cd
->child
->exe
)(ctx
,cd
->child
,theinput
),NULL
,10);
855 void *exe_booltoint(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
856 struct coercedata
*cd
=thenode
->localdata
;
858 /* Don't need to do anything */
859 return (cd
->child
->exe
)(ctx
, cd
->child
, theinput
);
862 void *exe_strtobool(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
863 struct coercedata
*cd
=thenode
->localdata
;
864 char *ch
=(cd
->child
->exe
)(ctx
, cd
->child
, theinput
);
866 if (!ch
|| *ch
=='\0' || (*ch
=='0' && ch
[1]=='\0')) {
873 void *exe_inttobool(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
874 struct coercedata
*cd
=thenode
->localdata
;
876 if ((cd
->child
->exe
)(ctx
, cd
->child
, theinput
)) {
883 struct searchNode
*coerceNode(searchCtx
*ctx
, struct searchNode
*thenode
, int type
) {
884 struct searchNode
*anode
;
885 struct coercedata
*cd
;
887 /* You can't coerce a NULL */
891 /* No effort required to coerce to the same type */
892 if (type
==(thenode
->returntype
& RETURNTYPE_TYPE
))
895 anode
=(struct searchNode
*)malloc(sizeof(struct searchNode
));
896 anode
->localdata
=cd
=(struct coercedata
*)malloc(sizeof(struct coercedata
));
898 anode
->returntype
=type
; /* We'll return what they want, always */
899 anode
->free
=free_coerce
;
902 case RETURNTYPE_STRING
:
903 /* For a string we'll need a buffer */
904 /* A 64-bit number prints out to 20 digits, this leaves some slack */
905 cd
->u
.stringbuf
=malloc(25);
906 anode
->free
=free_coercestring
;
908 switch(thenode
->returntype
& RETURNTYPE_TYPE
) {
911 if (thenode
->returntype
& RETURNTYPE_CONST
) {
912 /* Constant node: sort it out now */
913 sprintf(cd
->u
.stringbuf
, "%lu", (unsigned long)thenode
->exe(ctx
, thenode
, NULL
));
914 anode
->exe
=exe_tostr_null
;
915 anode
->returntype
|= RETURNTYPE_CONST
;
918 anode
->exe
=exe_inttostr
;
922 case RETURNTYPE_BOOL
:
923 if (thenode
->returntype
& RETURNTYPE_CONST
) {
924 /* Constant bool value */
925 if (thenode
->exe(ctx
, thenode
,NULL
)) {
927 sprintf(cd
->u
.stringbuf
, "1");
929 cd
->u
.stringbuf
[0] = '\0';
931 anode
->exe
=exe_tostr_null
;
932 anode
->returntype
|= RETURNTYPE_CONST
;
934 /* Variable bool value */
935 anode
->exe
=exe_booltostr
;
943 switch (thenode
->returntype
& RETURNTYPE_TYPE
) {
944 case RETURNTYPE_STRING
:
945 if (thenode
->returntype
& RETURNTYPE_CONST
) {
946 cd
->u
.val
=strtoul((thenode
->exe
)(ctx
, thenode
, NULL
), NULL
, 10);
947 anode
->exe
=exe_val_null
;
948 anode
->returntype
|= RETURNTYPE_CONST
;
950 anode
->exe
=exe_strtoint
;
955 case RETURNTYPE_BOOL
:
956 if (thenode
->returntype
& RETURNTYPE_CONST
) {
957 if ((thenode
->exe
)(ctx
, thenode
,NULL
))
962 anode
->exe
=exe_val_null
;
963 anode
->returntype
|= RETURNTYPE_CONST
;
965 anode
->exe
=exe_booltoint
;
972 case RETURNTYPE_BOOL
:
974 switch (thenode
->returntype
& RETURNTYPE_TYPE
) {
975 case RETURNTYPE_STRING
:
976 if (thenode
->returntype
& RETURNTYPE_CONST
) {
977 char *rv
=(char *)((thenode
->exe
)(ctx
, thenode
, NULL
));
978 if (!rv
|| *rv
=='\0' || (*rv
=='0' && rv
[1]=='\0'))
983 anode
->exe
=exe_val_null
;
984 anode
->returntype
|= RETURNTYPE_CONST
;
986 anode
->exe
=exe_strtobool
;
992 if (thenode
->returntype
& RETURNTYPE_CONST
) {
993 if ((thenode
->exe
)(ctx
, thenode
,NULL
))
998 anode
->exe
=exe_val_null
;
999 anode
->returntype
|= RETURNTYPE_CONST
;
1001 anode
->exe
=exe_inttobool
;
1011 /* Literals always return constant strings... */
1012 void *literal_exe(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
1013 if (thenode
->localdata
)
1014 return ((sstring
*)thenode
->localdata
)->content
;
1019 void literal_free(searchCtx
*ctx
, struct searchNode
*thenode
) {
1020 freesstring(thenode
->localdata
);
1024 void nssnprintf(char *buf
, size_t size
, const char *format
, nick
*np
) {
1037 for(p
=format
;*p
;p
++) {
1039 if(!sbaddchar(&b
, *p
))
1047 if(!sbaddchar(&b
, *p
))
1055 c
= np
->nick
; break;
1057 c
= np
->ident
; break;
1059 c
= np
->host
->name
->content
; break;
1061 snprintf(hostbuf
, sizeof(hostbuf
), "%s", IPtostr(np
->ipaddress
));
1065 snprintf(hostbuf
, sizeof(hostbuf
), "%s!%s@%s", np
->nick
, np
->ident
, IPtostr(np
->ipaddress
));
1069 c
= "(bad format specifier)";
1072 if(!sbaddstr(&b
, c
))
1084 static char *var_tochar(searchCtx
*ctx
, char *arg
, searchNode
**variable
) {
1085 *variable
= ctx
->parser(ctx
, arg
);
1086 if (!(*variable
= coerceNode(ctx
, *variable
, RETURNTYPE_STRING
)))
1089 if(!((*variable
)->returntype
& RETURNTYPE_CONST
)) {
1090 parseError
= "only constant variables allowed";
1091 ((*variable
)->free
)(ctx
, *variable
);
1095 return (char *)((*variable
)->exe
)(ctx
, *variable
, NULL
);
1098 void free_val_null(searchCtx
*ctx
, struct searchNode
*thenode
) {
1101 struct searchVariable
*var_register(searchCtx
*ctx
, char *arg
, int type
) {
1102 searchNode
*variable
;
1103 struct searchVariable
*us
;
1107 if(ctx
->lastvar
>= MAX_VARIABLES
) {
1108 parseError
= "Maximum number of variables reached";
1112 us
= &ctx
->vars
[ctx
->lastvar
];
1114 var
= var_tochar(ctx
, arg
, &variable
);
1118 strlcpy(us
->name
, var
, sizeof(us
->name
));
1119 (variable
->free
)(ctx
, variable
);
1121 for(i
=0;i
<ctx
->lastvar
;i
++) {
1122 if(!strcmp(us
->name
, ctx
->vars
[i
].name
)) {
1123 parseError
= "variable name already in use";
1129 us
->data
.returntype
= type
;
1130 us
->data
.localdata
= &us
->cdata
;
1131 us
->data
.exe
= exe_val_null
;
1132 us
->data
.free
= free_val_null
;
1134 us
->cdata
.child
= NULL
;
1138 searchNode
*var_get(searchCtx
*ctx
, char *arg
) {
1139 searchNode
*variable
, *found
= NULL
;
1141 char *var
= var_tochar(ctx
, arg
, &variable
);
1145 for(i
=0;i
<ctx
->lastvar
;i
++) {
1146 if(!strcmp(var
, ctx
->vars
[i
].name
)) {
1147 found
= &ctx
->vars
[i
].data
;
1151 (variable
->free
)(ctx
, variable
);
1154 parseError
= "variable not found";
1158 void var_setstr(struct searchVariable
*v
, char *data
) {
1159 v
->cdata
.u
.stringbuf
= data
;
1162 void displaystrerror(replyFunc reply
, nick
*np
, const char *input
) {
1165 if((parseStrErrorPos
>= 0) && (parseStrErrorPos
< sizeof(buf
) - 3)) {
1168 for(i
=0;i
<parseStrErrorPos
;i
++)
1174 reply(np
, "%s", input
);
1175 reply(np
, "%s", buf
);
1178 reply(np
, "Parse error: %s", parseStrError
);
1181 struct searchNode
*argtoconststr(char *command
, searchCtx
*ctx
, char *arg
, char **p
) {
1182 struct searchNode
*c
;
1183 static char errorbuf
[512];
1185 c
= ctx
->parser(ctx
, arg
);
1186 if (!(c
= coerceNode(ctx
, c
, RETURNTYPE_STRING
))) {
1187 snprintf(errorbuf
, sizeof(errorbuf
), "%s: unable to coerce argument to string", command
);
1188 parseError
= errorbuf
;
1192 if (!(c
->returntype
& RETURNTYPE_CONST
)) {
1193 snprintf(errorbuf
, sizeof(errorbuf
), "%s: constant argument required", command
);
1194 parseError
= errorbuf
;
1200 *p
= (char *)(c
->exe
)(ctx
, c
, NULL
);