]>
Commit | Line | Data |
---|---|---|
1 | #include <stdio.h> | |
2 | #include <stdarg.h> | |
3 | #include <string.h> | |
4 | ||
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" | |
15 | #include "parser.h" | |
16 | ||
17 | MODULE_VERSION(""); | |
18 | ||
19 | CommandTree *searchCmdTree; | |
20 | searchList *globalterms = NULL; | |
21 | ||
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 | ||
26 | void printnick_channels(searchCtx *, nick *, nick *); | |
27 | void printchannel(searchCtx *, nick *, chanindex *); | |
28 | void printchannel_topic(searchCtx *, nick *, chanindex *); | |
29 | void printchannel_services(searchCtx *, nick *, chanindex *); | |
30 | ||
31 | UserDisplayFunc defaultuserfn = printuser; | |
32 | NickDisplayFunc defaultnickfn = printnick; | |
33 | ChanDisplayFunc defaultchanfn = printchannel; | |
34 | ||
35 | searchCmd *reg_nicksearch, *reg_chansearch, *reg_usersearch; | |
36 | void displaycommandhelp(nick *, Command *); | |
37 | void displaystrerror(replyFunc, nick *, const char *); | |
38 | ||
39 | searchCmd *registersearchcommand(char *name, int level, CommandHandler cmd, void *defaultdisplayfunc) { | |
40 | searchCmd *acmd; | |
41 | searchList *sl; | |
42 | ||
43 | registercontrolhelpfunccmd(name, NO_OPER,4, cmd, &displaycommandhelp); | |
44 | acmd=(struct searchCmd *)malloc(sizeof(struct searchCmd)); | |
45 | if (!acmd) { | |
46 | Error("newsearch", ERR_ERROR, "malloc failed: registersearchcommand"); | |
47 | return NULL; | |
48 | } | |
49 | acmd->handler = cmd; | |
50 | ||
51 | acmd->name = getsstring( name, NSMAX_COMMAND_LEN); | |
52 | acmd->outputtree = newcommandtree(); | |
53 | acmd->searchtree = newcommandtree(); | |
54 | ||
55 | addcommandtotree(searchCmdTree, name, 0, 0, (CommandHandler)acmd); | |
56 | ||
57 | for (sl=globalterms; sl; sl=sl->next) { | |
58 | addcommandexttotree(acmd->searchtree, sl->name->content, 0, 1, (CommandHandler)sl->cmd, sl->help); | |
59 | } | |
60 | ||
61 | return acmd; | |
62 | } | |
63 | ||
64 | void deregistersearchcommand(searchCmd *scmd) { | |
65 | deregistercontrolcmd(scmd->name->content, (CommandHandler)scmd->handler); | |
66 | destroycommandtree(scmd->outputtree); | |
67 | destroycommandtree(scmd->searchtree); | |
68 | deletecommandfromtree(searchCmdTree, scmd->name->content, (CommandHandler) scmd); | |
69 | freesstring(scmd->name); | |
70 | free(scmd); | |
71 | } | |
72 | ||
73 | void displaycommandhelp(nick *np, Command *cmd) { | |
74 | int i,n,j,m; | |
75 | Command *cmdlist[100], *acmdlist[100]; | |
76 | searchCmd *acmd; | |
77 | ||
78 | n=getcommandlist(searchCmdTree,cmdlist,100); | |
79 | for(i=0;i<n;i++) { | |
80 | /* note: we may want to only deregister a command if we've already registered it, for now, try de-registering new commands anyway */ | |
81 | if ( ((searchCmd *)cmdlist[i]->handler)->handler != cmd->handler ) | |
82 | continue; | |
83 | acmd = ((searchCmd *)(cmdlist[i]->handler)); | |
84 | ||
85 | controlreply(np, "Usage: [flags] <criteria>\n"); | |
86 | controlreply(np, "Flags:\n"); | |
87 | controlreply(np, " -l int : Limit number of rows of results\n"); | |
88 | controlreply(np, " -d string : a valid output format for the results\n"); | |
89 | controlreply(np, " -s subset : ipmask subset of network to search (only for node search)\n"); | |
90 | controlreply(np, " \n"); | |
91 | controlreply(np, "Available Output Formats:\n"); | |
92 | ||
93 | m=getcommandlist(acmd->outputtree,acmdlist,100); | |
94 | for(j=0;j<m;j++) { | |
95 | if ( controlpermitted( acmdlist[j]->level, np) ) { | |
96 | char *help=(char *)acmdlist[j]->ext; | |
97 | if ( help && help[0] != '\0') | |
98 | controlreply(np, "%-10s: %s\n", acmdlist[j]->command->content, help); | |
99 | else | |
100 | controlreply(np, "%s\n", acmdlist[j]->command->content ); | |
101 | } | |
102 | } | |
103 | ||
104 | controlreply(np, " \n"); | |
105 | controlreply(np, "Available Global Commands and Operators:\n" ); | |
106 | m=getcommandlist(acmd->searchtree,acmdlist,100); | |
107 | for(j=0;j<m;j++) { | |
108 | if ( acmdlist[j]->maxparams) { | |
109 | char *help=(char *)acmdlist[j]->ext; | |
110 | if ( help && help[0] != '\0') | |
111 | controlreply(np, "%-10s: %s\n", acmdlist[j]->command->content, help ); | |
112 | else | |
113 | controlreply(np, "%s\n", acmdlist[j]->command->content ); | |
114 | } | |
115 | } | |
116 | ||
117 | controlreply(np, " \n"); | |
118 | controlreply(np, "Available Commands and Operators for %s:\n", acmd->name->content); | |
119 | ||
120 | m=getcommandlist(acmd->searchtree,acmdlist,100); | |
121 | for(j=0;j<m;j++) { | |
122 | if ( !acmdlist[j]->maxparams && controlpermitted( acmdlist[j]->level, np) ) { | |
123 | char *help=(char *)acmdlist[j]->ext; | |
124 | if ( help && help[0] != '\0') | |
125 | controlreply(np, "%-10s: %s\n", acmdlist[j]->command->content, help ); | |
126 | else | |
127 | controlreply(np, "%s\n", acmdlist[j]->command->content ); | |
128 | } | |
129 | } | |
130 | } | |
131 | } | |
132 | ||
133 | void regdisp( searchCmd *cmd, const char *name, void *handler, int level, char *help) { | |
134 | addcommandexttotree(cmd->outputtree, name, level, 0, (CommandHandler) handler, (char *)help); | |
135 | } | |
136 | ||
137 | void unregdisp( searchCmd *cmd, const char *name, void *handler ) { | |
138 | deletecommandfromtree(cmd->outputtree, name, (CommandHandler) handler); | |
139 | } | |
140 | ||
141 | void *findcommandinlist( searchList *sl, char *name){ | |
142 | while (sl) { | |
143 | if(strcmp(sl->name->content,name) == 0 ) { | |
144 | return sl; | |
145 | } | |
146 | sl=sl->next; | |
147 | } | |
148 | return NULL; | |
149 | } | |
150 | ||
151 | const char *parseError; | |
152 | /* used for *_free functions that need to warn users of certain things | |
153 | i.e. hitting too many users in a (kill) or (gline) */ | |
154 | nick *senderNSExtern; | |
155 | ||
156 | void _init() { | |
157 | searchCmdTree=newcommandtree(); | |
158 | ||
159 | reg_nicksearch = (searchCmd *)registersearchcommand("nicksearch",NO_OPER,&do_nicksearch, printnick); | |
160 | reg_chansearch = (searchCmd *)registersearchcommand("chansearch",NO_OPER,&do_chansearch, printchannel); | |
161 | reg_usersearch = (searchCmd *)registersearchcommand("usersearch",NO_OPER,&do_usersearch, printuser); | |
162 | ||
163 | /* Boolean operations */ | |
164 | registerglobalsearchterm("and",and_parse, "usage: (and (X) (X))" ); | |
165 | registerglobalsearchterm("not",not_parse, "usage: (not (X))"); | |
166 | registerglobalsearchterm("or",or_parse, "usage: (or (X) (X))" ); | |
167 | ||
168 | registerglobalsearchterm("eq",eq_parse, "usage: (eq (X) Y)"); | |
169 | ||
170 | registerglobalsearchterm("lt",lt_parse, "usage: (lt (X) int)"); | |
171 | registerglobalsearchterm("gt",gt_parse, "usage: (gt (X) int)"); | |
172 | ||
173 | /* String operations */ | |
174 | registerglobalsearchterm("match",match_parse, "usage: (match (X) string)"); | |
175 | registerglobalsearchterm("regex",regex_parse, "usage: (regex (X) string)"); | |
176 | registerglobalsearchterm("length",length_parse, "usage: (length string)"); | |
177 | ||
178 | /* Nickname operations */ | |
179 | registersearchterm(reg_nicksearch, "hostmask",hostmask_parse, 0, "The user's nick!user@host; \"hostmask real\" returns nick!user@host\rreal"); /* nick only */ | |
180 | registersearchterm(reg_nicksearch, "realname",realname_parse, 0, "User's current realname"); /* nick only */ | |
181 | registersearchterm(reg_nicksearch, "authname",authname_parse, 0, "User's current authname or false"); /* nick only */ | |
182 | registersearchterm(reg_nicksearch, "authts",authts_parse, 0, "User's Auth timestamp"); /* nick only */ | |
183 | registersearchterm(reg_nicksearch, "ident",ident_parse, 0, "User's current ident"); /* nick only */ | |
184 | registersearchterm(reg_nicksearch, "host",host_parse, 0, "User's host, allow \"host real\" to match real host"); /* nick only */ | |
185 | registersearchterm(reg_nicksearch, "channel",channel_parse, 0, "Valid Channel Name to match users against"); /* nick only */ | |
186 | registersearchterm(reg_nicksearch, "timestamp",timestamp_parse, 0, "User's Timestamp"); /* nick only */ | |
187 | registersearchterm(reg_nicksearch, "country",country_parse, 0, "2 letter country code (data source is geoip)"); /* nick only */ | |
188 | registersearchterm(reg_nicksearch, "ip",ip_parse, 0, "User's IP - ipv4 or ipv6 format as appropriate. Note: not 6to4"); /* nick only */ | |
189 | registersearchterm(reg_nicksearch, "channels",channels_parse, 0, "Channel Count"); /* nick only */ | |
190 | registersearchterm(reg_nicksearch, "server",server_parse, 0, "Server Name. Either (server string) or (match (server) string)"); /* nick only */ | |
191 | registersearchterm(reg_nicksearch, "authid",authid_parse, 0, "User's Auth ID"); /* nick only */ | |
192 | registersearchterm(reg_nicksearch, "cidr",cidr_parse, 0, "CIDR matching"); /* nick only */ | |
193 | registersearchterm(reg_nicksearch, "ipvsix",ipv6_parse, 0, "IPv6 user"); /* nick only */ | |
194 | ||
195 | /* Channel operations */ | |
196 | registersearchterm(reg_chansearch, "exists",exists_parse, 0, "Returns if channel exists on network. Note: newserv may store data on empty channels"); /* channel only */ | |
197 | registersearchterm(reg_chansearch, "services",services_parse, 0, ""); /* channel only */ | |
198 | registersearchterm(reg_chansearch, "size",size_parse, 0, "Channel user count"); /* channel only */ | |
199 | registersearchterm(reg_chansearch, "name",name_parse, 0, "Channel Name"); /* channel only */ | |
200 | registersearchterm(reg_chansearch, "topic",topic_parse, 0, "Channel topic"); /* channel only */ | |
201 | registersearchterm(reg_chansearch, "oppct",oppct_parse, 0, "Percentage Opped"); /* channel only */ | |
202 | registersearchterm(reg_chansearch, "cumodecount",cumodecount_parse, 0, "Count of users with given channel modes"); /* channel only */ | |
203 | registersearchterm(reg_chansearch, "cumodepct",cumodepct_parse, 0, "Percentage of users with given channel modes"); /* channel only */ | |
204 | registersearchterm(reg_chansearch, "uniquehostpct",hostpct_parse, 0, "uniquehost percent"); /* channel only */ | |
205 | registersearchterm(reg_chansearch, "authedpct",authedpct_parse, 0, "Percentage of authed users"); /* channel only */ | |
206 | registersearchterm(reg_chansearch, "kick",kick_parse, 0, "KICK users channels in newsearch result. Note: evaluation order"); /* channel only */ | |
207 | ||
208 | /* Nickname / channel operations */ | |
209 | registersearchterm(reg_chansearch, "modes",modes_parse, 0, "User Modes"); | |
210 | registersearchterm(reg_nicksearch, "modes",modes_parse, 0, "Channel Modes"); | |
211 | registersearchterm(reg_chansearch, "nick",nick_parse, 0, "Nickname"); | |
212 | registersearchterm(reg_nicksearch, "nick",nick_parse, 0, "Nickname"); | |
213 | ||
214 | /* Kill / gline parameters */ | |
215 | registersearchterm(reg_chansearch,"kill",kill_parse, 0, "KILL users in newsearch result. Note: evaluation order"); | |
216 | registersearchterm(reg_chansearch,"gline",gline_parse, 0, "GLINE users in newsearch result. Note: evaluation order"); | |
217 | registersearchterm(reg_nicksearch,"kill",kill_parse, 0, "KILL users in newsearch result. Note: evaluation order"); | |
218 | registersearchterm(reg_nicksearch,"gline",gline_parse, 0, "GLINE users in newsearch result. Note: evaluation order"); | |
219 | ||
220 | /* Iteration functionality */ | |
221 | registerglobalsearchterm("any",any_parse, "usage: any (generatorfn x) (fn ... (var x) ...)"); | |
222 | registerglobalsearchterm("all",all_parse, "usage: all (generatorfn x) (fn ... (var x) ...)"); | |
223 | registerglobalsearchterm("var",var_parse, "usage: var variable"); | |
224 | ||
225 | /* Iterable functions */ | |
226 | registersearchterm(reg_nicksearch, "channeliter",channeliter_parse, 0, "Channel Iterable function - usage: (any (channeliter x) (match (var x) #twilight*))"); /* nick only */ | |
227 | registersearchterm(reg_chansearch, "nickiter",nickiter_parse, 0, "Nick Iterable function - usage: (any (nickiter x) (match (var x) nickname*))"); /* channel only */ | |
228 | ||
229 | /* Functions that work on strings?! */ | |
230 | registersearchterm(reg_nicksearch, "cumodes", cumodes_parse, 0, "usage: (cumodes (var x) <modes>)"); | |
231 | registersearchterm(reg_chansearch, "cumodes", cumodes_parse, 0, "usage: (cumodes (var x) <modes>)"); | |
232 | ||
233 | /* Notice functionality */ | |
234 | registersearchterm(reg_chansearch,"notice",notice_parse, 0, "NOTICE users in newsearch result. Note: evaluation order"); | |
235 | registersearchterm(reg_nicksearch,"notice",notice_parse, 0, "NOTICE users in newsearch result. Note: evaluation order"); | |
236 | ||
237 | /* Nick output filters */ | |
238 | regdisp(reg_nicksearch,"default",printnick, 0, ""); | |
239 | regdisp(reg_nicksearch,"channels",printnick_channels, 0, "include channels in output"); | |
240 | ||
241 | /* Channel output filters */ | |
242 | regdisp(reg_chansearch,"default",printchannel, 0, ""); | |
243 | regdisp(reg_chansearch,"topic",printchannel_topic, 0, "display channel topics"); | |
244 | regdisp(reg_chansearch,"services",printchannel_services, 0, "display services on channels"); | |
245 | ||
246 | /* Nick output filters */ | |
247 | regdisp(reg_usersearch,"default",printuser, 0, ""); | |
248 | ||
249 | } | |
250 | ||
251 | void _fini() { | |
252 | searchList *sl, *psl; | |
253 | int i,n; | |
254 | Command *cmdlist[100]; | |
255 | ||
256 | sl=globalterms; | |
257 | while (sl) { | |
258 | psl = sl; | |
259 | sl=sl->next; | |
260 | ||
261 | n=getcommandlist(searchCmdTree,cmdlist,100); | |
262 | for(i=0;i<n;i++) { | |
263 | deregistersearchterm( (searchCmd *)cmdlist[i]->handler, psl->name->content, psl->cmd); | |
264 | } | |
265 | ||
266 | freesstring(psl->name); | |
267 | if (psl->help) | |
268 | free (psl->help); | |
269 | free(psl); | |
270 | } | |
271 | ||
272 | deregistersearchcommand( reg_nicksearch ); | |
273 | deregistersearchcommand( reg_chansearch ); | |
274 | deregistersearchcommand( reg_usersearch ); | |
275 | destroycommandtree( searchCmdTree ); | |
276 | } | |
277 | ||
278 | void registerglobalsearchterm(char *term, parseFunc parsefunc, char *help) { | |
279 | int i,n; | |
280 | Command *cmdlist[100]; | |
281 | searchList *sl = malloc(sizeof(searchList)); | |
282 | if (!sl) { | |
283 | Error("newsearch", ERR_ERROR, "malloc failed: registerglobalsearchterm"); | |
284 | return; | |
285 | } | |
286 | ||
287 | sl->cmd = parsefunc; | |
288 | sl->name = getsstring(term, NSMAX_COMMAND_LEN); | |
289 | sl->next = NULL; | |
290 | if (help) { | |
291 | int len=strlen(help); | |
292 | sl->help=(char *)malloc(len+1); | |
293 | if(!sl->help) { | |
294 | Error("newsearch", ERR_ERROR, "malloc failed: registerglobalsearchterm"); | |
295 | return; | |
296 | } | |
297 | strncpy(sl->help, help, len); | |
298 | sl->help[len] = '\0'; | |
299 | } else { | |
300 | sl->help=NULL; | |
301 | } | |
302 | ||
303 | ||
304 | if ( globalterms != NULL ) { | |
305 | sl->next = globalterms; | |
306 | } | |
307 | globalterms = sl; | |
308 | ||
309 | n=getcommandlist(searchCmdTree,cmdlist,100); | |
310 | for(i=0;i<n;i++) { | |
311 | /* maxparams is set to 1 to indicate a global term */ | |
312 | /* access level is set to 0 for all global terms */ | |
313 | addcommandexttotree( ((searchCmd *)cmdlist[i]->handler)->searchtree,term, 0, 1, (CommandHandler) parsefunc, help); | |
314 | } | |
315 | } | |
316 | ||
317 | void deregisterglobalsearchterm(char *term, parseFunc parsefunc) { | |
318 | int i,n; | |
319 | Command *cmdlist[100]; | |
320 | searchList *sl, *psl=NULL; | |
321 | ||
322 | for (sl=globalterms; sl; sl=sl->next) { | |
323 | if ( strcmp( sl->name->content, term) == 0 ) { | |
324 | break; | |
325 | } | |
326 | psl = sl; | |
327 | } | |
328 | ||
329 | if (sl) { | |
330 | if( psl ) { | |
331 | psl->next = sl->next; | |
332 | } | |
333 | ||
334 | n=getcommandlist(searchCmdTree,cmdlist,100); | |
335 | for(i=0;i<n;i++) { | |
336 | deletecommandfromtree( ((searchCmd *)cmdlist[i]->handler)->searchtree, term, (CommandHandler) parsefunc); | |
337 | } | |
338 | freesstring(sl->name); | |
339 | free(sl); | |
340 | } | |
341 | } | |
342 | ||
343 | void registersearchterm(searchCmd *cmd, char *term, parseFunc parsefunc, int level, char *help) { | |
344 | /* NOTE: global terms are added to the tree elsewhere as we set maxparams to 1 to indicate global */ | |
345 | addcommandexttotree(cmd->searchtree, term, level, 0, (CommandHandler) parsefunc, help); | |
346 | } | |
347 | ||
348 | void deregistersearchterm(searchCmd *cmd, char *term, parseFunc parsefunc) { | |
349 | /* NOTE: global terms are removed from the tree within deregisterglobalsearchterm */ | |
350 | deletecommandfromtree(cmd->searchtree, term, (CommandHandler) parsefunc); | |
351 | } | |
352 | ||
353 | static void controlwallwrapper(int level, char *format, ...) __attribute__ ((format (printf, 2, 3))); | |
354 | static void controlwallwrapper(int level, char *format, ...) { | |
355 | char buf[1024]; | |
356 | va_list ap; | |
357 | ||
358 | va_start(ap, format); | |
359 | vsnprintf(buf, sizeof(buf), format, ap); | |
360 | controlwall(NO_OPER, level, "%s", buf); | |
361 | va_end(ap); | |
362 | } | |
363 | ||
364 | int parseopts(int cargc, char **cargv, int *arg, int *limit, void **subset, void *display, CommandTree *sl, replyFunc reply, void *sender) { | |
365 | char *ch; | |
366 | Command *cmd; | |
367 | struct irc_in_addr sin; unsigned char bits; | |
368 | ||
369 | if (*cargv[0] == '-') { | |
370 | /* options */ | |
371 | (*arg)++; | |
372 | ||
373 | for (ch=cargv[0]+1;*ch;ch++) { | |
374 | switch(*ch) { | |
375 | case 'l': | |
376 | if (cargc<*arg) { | |
377 | reply(sender,"Error: -l switch requires an argument (for help, see help <searchcmd>)"); | |
378 | return CMD_ERROR; | |
379 | } | |
380 | *limit=strtoul(cargv[(*arg)++],NULL,10); | |
381 | break; | |
382 | ||
383 | case 'd': | |
384 | if (cargc<*arg) { | |
385 | reply(sender,"Error: -d switch requires an argument (for help, see help <searchcmd>)"); | |
386 | return CMD_ERROR; | |
387 | } | |
388 | cmd=findcommandintree(sl, cargv[*arg],1); | |
389 | if (!cmd) { | |
390 | reply(sender,"Error: unknown output format %s (for help, see help <searchcmd>)",cargv[*arg]); | |
391 | return CMD_ERROR; | |
392 | } | |
393 | if ( !controlpermitted( cmd->level, sender) ) { | |
394 | reply(sender,"Error: Access Denied for output format %s (for help, see help <searchcmd>)", cargv[*arg]); | |
395 | return CMD_ERROR; | |
396 | } | |
397 | *((void **)display)=(void *)cmd->handler; | |
398 | (*arg)++; | |
399 | break; | |
400 | ||
401 | case 's': | |
402 | if (subset == NULL) { | |
403 | reply(sender,"Error: -s switch not supported for this search."); | |
404 | return CMD_ERROR; | |
405 | } | |
406 | if (cargc<*arg) { | |
407 | reply(sender,"Error: -s switch requires an argument (for help, see help <searchcmd>)"); | |
408 | return CMD_ERROR; | |
409 | } | |
410 | if (ipmask_parse(cargv[*arg], &sin, &bits) == 0) { | |
411 | reply(sender, "Error: Invalid CIDR mask supplied (for help, see help <searchcmd>)"); | |
412 | return CMD_ERROR; | |
413 | } | |
414 | *subset = (void *)refnode(iptree, &sin, bits); | |
415 | (*arg)++; | |
416 | break; | |
417 | ||
418 | default: | |
419 | reply(sender,"Unrecognised flag -%c. (for help, see help <searchcmd>)",*ch); | |
420 | return CMD_ERROR; | |
421 | } | |
422 | } | |
423 | } | |
424 | ||
425 | return CMD_OK; | |
426 | } | |
427 | ||
428 | void newsearch_ctxinit(searchCtx *ctx, searchParseFunc searchfn, replyFunc replyfn, wallFunc wallfn, void *arg, searchCmd *cmd, nick *np, void *displayfn, int limit) { | |
429 | memset(ctx, 0, sizeof(searchCtx)); | |
430 | ||
431 | ctx->reply = replyfn; | |
432 | ctx->wall = wallfn; | |
433 | ctx->parser = searchfn; | |
434 | ctx->arg = arg; | |
435 | ctx->searchcmd = cmd; | |
436 | ctx->sender = np; | |
437 | ctx->limit = limit; | |
438 | ctx->displayfn = displayfn; | |
439 | } | |
440 | ||
441 | int do_nicksearch_real(replyFunc reply, wallFunc wall, void *source, int cargc, char **cargv) { | |
442 | nick *sender = source; | |
443 | int limit=500; | |
444 | int arg=0; | |
445 | NickDisplayFunc display=defaultnickfn; | |
446 | int ret; | |
447 | #ifndef NEWSEARCH_NEWPARSER | |
448 | searchCtx ctx; | |
449 | struct searchNode *search; | |
450 | #else | |
451 | parsertree *tree; | |
452 | #endif | |
453 | ||
454 | if (cargc<1) { | |
455 | reply( sender, "Usage: [flags] <criteria>"); | |
456 | reply( sender, "For help, see help nicksearch"); | |
457 | return CMD_OK; | |
458 | } | |
459 | ||
460 | ret = parseopts(cargc, cargv, &arg, &limit, NULL, (void *)&display, reg_nicksearch->outputtree, reply, sender); | |
461 | if(ret != CMD_OK) | |
462 | return ret; | |
463 | ||
464 | if (arg>=cargc) { | |
465 | reply(sender,"No search terms - aborting."); | |
466 | return CMD_ERROR; | |
467 | } | |
468 | ||
469 | if (arg<(cargc-1)) { | |
470 | rejoinline(cargv[arg],cargc-arg); | |
471 | } | |
472 | ||
473 | #ifndef NEWSEARCH_NEWPARSER | |
474 | newsearch_ctxinit(&ctx, search_parse, reply, wall, NULL, reg_nicksearch, sender, display, limit); | |
475 | if (!(search = ctx.parser(&ctx, cargv[arg]))) { | |
476 | reply(sender,"Parse error: %s",parseError); | |
477 | return CMD_ERROR; | |
478 | } | |
479 | ||
480 | nicksearch_exe(search, &ctx); | |
481 | ||
482 | (search->free)(&ctx, search); | |
483 | #else | |
484 | tree = parse_string(reg_nicksearch, cargv[arg]); | |
485 | if(!tree) { | |
486 | displaystrerror(reply, sender, cargv[arg]); | |
487 | return CMD_ERROR; | |
488 | } | |
489 | ||
490 | ast_nicksearch(tree->root, reply, sender, wall, display, NULL, NULL, limit); | |
491 | ||
492 | parse_free(tree); | |
493 | #endif | |
494 | ||
495 | return CMD_OK; | |
496 | } | |
497 | ||
498 | int do_nicksearch(void *source, int cargc, char **cargv) { | |
499 | return do_nicksearch_real(controlreply, controlwallwrapper, source, cargc, cargv); | |
500 | } | |
501 | ||
502 | void nicksearch_exe(struct searchNode *search, searchCtx *ctx) { | |
503 | int i, j; | |
504 | int matches = 0; | |
505 | unsigned int cmarker; | |
506 | unsigned int tchans=0,uchans=0; | |
507 | struct channel **cs; | |
508 | nick *np, *sender = ctx->sender; | |
509 | senderNSExtern = sender; | |
510 | NickDisplayFunc display = ctx->displayfn; | |
511 | int limit = ctx->limit; | |
512 | ||
513 | /* Get a marker value to mark "seen" channels for unique count */ | |
514 | cmarker=nextchanmarker(); | |
515 | ||
516 | /* The top-level node needs to return a BOOL */ | |
517 | search=coerceNode(ctx, search, RETURNTYPE_BOOL); | |
518 | ||
519 | for (i=0;i<NICKHASHSIZE;i++) { | |
520 | for (np=nicktable[i];np;np=np->next) { | |
521 | if ((search->exe)(ctx, search, np)) { | |
522 | /* Add total channels */ | |
523 | tchans += np->channels->cursi; | |
524 | ||
525 | /* Check channels for uniqueness */ | |
526 | cs=(channel **)np->channels->content; | |
527 | for (j=0;j<np->channels->cursi;j++) { | |
528 | if (cs[j]->index->marker != cmarker) { | |
529 | cs[j]->index->marker=cmarker; | |
530 | uchans++; | |
531 | } | |
532 | } | |
533 | ||
534 | if (matches<limit) | |
535 | display(ctx, sender, np); | |
536 | ||
537 | if (matches==limit) | |
538 | ctx->reply(sender, "--- More than %d matches, skipping the rest",limit); | |
539 | matches++; | |
540 | } | |
541 | } | |
542 | } | |
543 | ||
544 | ctx->reply(sender,"--- End of list: %d matches; users were on %u channels (%u unique, %.1f average clones)", | |
545 | matches, tchans, uchans, (float)tchans/uchans); | |
546 | } | |
547 | ||
548 | int do_chansearch_real(replyFunc reply, wallFunc wall, void *source, int cargc, char **cargv) { | |
549 | nick *sender = source; | |
550 | int limit=500; | |
551 | int arg=0; | |
552 | ChanDisplayFunc display=defaultchanfn; | |
553 | int ret; | |
554 | #ifndef NEWSEARCH_NEWPARSER | |
555 | struct searchNode *search; | |
556 | searchCtx ctx; | |
557 | #else | |
558 | parsertree *tree; | |
559 | #endif | |
560 | ||
561 | if (cargc<1) { | |
562 | reply( sender, "Usage: [flags] <criteria>"); | |
563 | reply( sender, "For help, see help chansearch"); | |
564 | return CMD_OK; | |
565 | } | |
566 | ||
567 | ret = parseopts(cargc, cargv, &arg, &limit, NULL, (void *)&display, reg_chansearch->outputtree, reply, sender); | |
568 | if(ret != CMD_OK) | |
569 | return ret; | |
570 | ||
571 | if (arg>=cargc) { | |
572 | reply(sender,"No search terms - aborting."); | |
573 | return CMD_ERROR; | |
574 | } | |
575 | ||
576 | if (arg<(cargc-1)) { | |
577 | rejoinline(cargv[arg],cargc-arg); | |
578 | } | |
579 | ||
580 | #ifndef NEWSEARCH_NEWPARSER | |
581 | newsearch_ctxinit(&ctx, search_parse, reply, wall, NULL, reg_chansearch, sender, display, limit); | |
582 | if (!(search = ctx.parser(&ctx, cargv[arg]))) { | |
583 | reply(sender,"Parse error: %s",parseError); | |
584 | return CMD_ERROR; | |
585 | } | |
586 | ||
587 | chansearch_exe(search, &ctx); | |
588 | ||
589 | (search->free)(&ctx, search); | |
590 | #else | |
591 | tree = parse_string(reg_chansearch, cargv[arg]); | |
592 | if(!tree) { | |
593 | displaystrerror(reply, sender, cargv[arg]); | |
594 | return CMD_ERROR; | |
595 | } | |
596 | ||
597 | ast_chansearch(tree->root, reply, sender, wall, display, NULL, NULL, limit); | |
598 | ||
599 | parse_free(tree); | |
600 | #endif | |
601 | ||
602 | return CMD_OK; | |
603 | } | |
604 | ||
605 | int do_chansearch(void *source, int cargc, char **cargv) { | |
606 | return do_chansearch_real(controlreply, controlwallwrapper, source, cargc, cargv); | |
607 | } | |
608 | ||
609 | void chansearch_exe(struct searchNode *search, searchCtx *ctx) { | |
610 | int i; | |
611 | chanindex *cip; | |
612 | int matches = 0; | |
613 | nick *sender = ctx->sender; | |
614 | senderNSExtern = sender; | |
615 | ChanDisplayFunc display = ctx->displayfn; | |
616 | int limit = ctx->limit; | |
617 | ||
618 | search=coerceNode(ctx, search, RETURNTYPE_BOOL); | |
619 | ||
620 | for (i=0;i<CHANNELHASHSIZE;i++) { | |
621 | for (cip=chantable[i];cip;cip=cip->next) { | |
622 | if ((search->exe)(ctx, search, cip)) { | |
623 | if (matches<limit) | |
624 | display(ctx, sender, cip); | |
625 | if (matches==limit) | |
626 | ctx->reply(sender, "--- More than %d matches, skipping the rest",limit); | |
627 | matches++; | |
628 | } | |
629 | } | |
630 | } | |
631 | ||
632 | ctx->reply(sender,"--- End of list: %d matches", matches); | |
633 | } | |
634 | ||
635 | int do_usersearch_real(replyFunc reply, wallFunc wall, void *source, int cargc, char **cargv) { | |
636 | nick *sender = source; | |
637 | int limit=500; | |
638 | int arg=0; | |
639 | UserDisplayFunc display=defaultuserfn; | |
640 | int ret; | |
641 | #ifndef NEWSEARCH_NEWPARSER | |
642 | struct searchNode *search; | |
643 | searchCtx ctx; | |
644 | #else | |
645 | parsertree *tree; | |
646 | #endif | |
647 | ||
648 | if (cargc<1) { | |
649 | reply( sender, "Usage: [flags] <criteria>"); | |
650 | reply( sender, "For help, see help usersearch"); | |
651 | return CMD_OK; | |
652 | } | |
653 | ||
654 | ret = parseopts(cargc, cargv, &arg, &limit, NULL, (void *)&display, reg_usersearch->outputtree, reply, sender); | |
655 | if(ret != CMD_OK) | |
656 | return ret; | |
657 | ||
658 | if (arg>=cargc) { | |
659 | reply(sender,"No search terms - aborting."); | |
660 | return CMD_ERROR; | |
661 | } | |
662 | ||
663 | if (arg<(cargc-1)) { | |
664 | rejoinline(cargv[arg],cargc-arg); | |
665 | } | |
666 | ||
667 | #ifndef NEWSEARCH_NEWPARSER | |
668 | newsearch_ctxinit(&ctx, search_parse, reply, wall, NULL, reg_usersearch, sender, display, limit); | |
669 | ||
670 | if (!(search = ctx.parser(&ctx, cargv[arg]))) { | |
671 | reply(sender,"Parse error: %s",parseError); | |
672 | return CMD_ERROR; | |
673 | } | |
674 | ||
675 | usersearch_exe(search, &ctx); | |
676 | ||
677 | (search->free)(&ctx, search); | |
678 | #else | |
679 | tree = parse_string(reg_usersearch, cargv[arg]); | |
680 | if(!tree) { | |
681 | displaystrerror(reply, sender, cargv[arg]); | |
682 | return CMD_ERROR; | |
683 | } | |
684 | ||
685 | ast_usersearch(tree->root, reply, sender, wall, display, NULL, NULL, limit); | |
686 | ||
687 | parse_free(tree); | |
688 | #endif | |
689 | ||
690 | return CMD_OK; | |
691 | } | |
692 | ||
693 | int do_usersearch(void *source, int cargc, char **cargv) { | |
694 | return do_usersearch_real(controlreply, controlwallwrapper, source, cargc, cargv); | |
695 | } | |
696 | ||
697 | void usersearch_exe(struct searchNode *search, searchCtx *ctx) { | |
698 | int i; | |
699 | authname *aup; | |
700 | int matches = 0; | |
701 | nick *sender = ctx->sender; | |
702 | int limit = ctx->limit; | |
703 | UserDisplayFunc display = ctx->displayfn; | |
704 | senderNSExtern = sender; | |
705 | ||
706 | search=coerceNode(ctx, search, RETURNTYPE_BOOL); | |
707 | ||
708 | for (i=0;i<AUTHNAMEHASHSIZE;i++) { | |
709 | for (aup=authnametable[i];aup;aup=aup->next) { | |
710 | if ((search->exe)(ctx, search, aup)) { | |
711 | if (matches<limit) | |
712 | display(ctx, sender, aup); | |
713 | if (matches==limit) | |
714 | ctx->reply(sender, "--- More than %d matches, skipping the rest",limit); | |
715 | matches++; | |
716 | } | |
717 | } | |
718 | } | |
719 | ||
720 | ctx->reply(sender,"--- End of list: %d matches", matches); | |
721 | } | |
722 | ||
723 | /* Free a coerce node */ | |
724 | void free_coerce(searchCtx *ctx, struct searchNode *thenode) { | |
725 | struct coercedata *cd=thenode->localdata; | |
726 | ||
727 | cd->child->free(ctx, cd->child); | |
728 | free(thenode->localdata); | |
729 | free(thenode); | |
730 | } | |
731 | ||
732 | /* Free a coerce node with a stringbuf allocated */ | |
733 | void free_coercestring(searchCtx *ctx, struct searchNode *thenode) { | |
734 | free(((struct coercedata *)thenode->localdata)->u.stringbuf); | |
735 | free_coerce(ctx, thenode); | |
736 | } | |
737 | ||
738 | /* exe_tostr_null: return the constant string */ | |
739 | void *exe_tostr_null(searchCtx *ctx, struct searchNode *thenode, void *theinput) { | |
740 | struct coercedata *cd=thenode->localdata; | |
741 | ||
742 | return cd->u.stringbuf; | |
743 | } | |
744 | ||
745 | /* exe_val_null: return the constant value */ | |
746 | void *exe_val_null(searchCtx *ctx, struct searchNode *thenode, void *theinput) { | |
747 | struct coercedata *cd=thenode->localdata; | |
748 | ||
749 | return (void *)cd->u.val; | |
750 | } | |
751 | ||
752 | /* Lots of very dull type conversion functions */ | |
753 | void *exe_inttostr(searchCtx *ctx, struct searchNode *thenode, void *theinput) { | |
754 | struct coercedata *cd=thenode->localdata; | |
755 | ||
756 | sprintf(cd->u.stringbuf, "%lu", (unsigned long)(cd->child->exe)(ctx, cd->child, theinput)); | |
757 | ||
758 | return cd->u.stringbuf; | |
759 | } | |
760 | ||
761 | void *exe_booltostr(searchCtx *ctx, struct searchNode *thenode, void *theinput) { | |
762 | struct coercedata *cd=thenode->localdata; | |
763 | ||
764 | if ((cd->child->exe)(ctx, cd->child, theinput)) { | |
765 | sprintf(cd->u.stringbuf,"1"); | |
766 | } else { | |
767 | cd->u.stringbuf[0]='\0'; | |
768 | } | |
769 | ||
770 | return cd->u.stringbuf; | |
771 | } | |
772 | ||
773 | void *exe_strtoint(searchCtx *ctx, struct searchNode *thenode, void *theinput) { | |
774 | struct coercedata *cd=thenode->localdata; | |
775 | ||
776 | return (void *)strtoul((cd->child->exe)(ctx,cd->child,theinput),NULL,10); | |
777 | } | |
778 | ||
779 | void *exe_booltoint(searchCtx *ctx, struct searchNode *thenode, void *theinput) { | |
780 | struct coercedata *cd=thenode->localdata; | |
781 | ||
782 | /* Don't need to do anything */ | |
783 | return (cd->child->exe)(ctx, cd->child, theinput); | |
784 | } | |
785 | ||
786 | void *exe_strtobool(searchCtx *ctx, struct searchNode *thenode, void *theinput) { | |
787 | struct coercedata *cd=thenode->localdata; | |
788 | char *ch=(cd->child->exe)(ctx, cd->child, theinput); | |
789 | ||
790 | if (!ch || *ch=='\0' || (*ch=='0' && ch[1]=='\0')) { | |
791 | return (void *)0; | |
792 | } else { | |
793 | return (void *)1; | |
794 | } | |
795 | } | |
796 | ||
797 | void *exe_inttobool(searchCtx *ctx, struct searchNode *thenode, void *theinput) { | |
798 | struct coercedata *cd=thenode->localdata; | |
799 | ||
800 | if ((cd->child->exe)(ctx, cd->child, theinput)) { | |
801 | return (void *)1; | |
802 | } else { | |
803 | return (void *)0; | |
804 | } | |
805 | } | |
806 | ||
807 | struct searchNode *coerceNode(searchCtx *ctx, struct searchNode *thenode, int type) { | |
808 | struct searchNode *anode; | |
809 | struct coercedata *cd; | |
810 | ||
811 | /* You can't coerce a NULL */ | |
812 | if (!thenode) | |
813 | return NULL; | |
814 | ||
815 | /* No effort required to coerce to the same type */ | |
816 | if (type==(thenode->returntype & RETURNTYPE_TYPE)) | |
817 | return thenode; | |
818 | ||
819 | anode=(struct searchNode *)malloc(sizeof(struct searchNode)); | |
820 | anode->localdata=cd=(struct coercedata *)malloc(sizeof(struct coercedata)); | |
821 | cd->child=thenode; | |
822 | anode->returntype=type; /* We'll return what they want, always */ | |
823 | anode->free=free_coerce; | |
824 | ||
825 | switch(type) { | |
826 | case RETURNTYPE_STRING: | |
827 | /* For a string we'll need a buffer */ | |
828 | /* A 64-bit number prints out to 20 digits, this leaves some slack */ | |
829 | cd->u.stringbuf=malloc(25); | |
830 | anode->free=free_coercestring; | |
831 | ||
832 | switch(thenode->returntype & RETURNTYPE_TYPE) { | |
833 | default: | |
834 | case RETURNTYPE_INT: | |
835 | if (thenode->returntype & RETURNTYPE_CONST) { | |
836 | /* Constant node: sort it out now */ | |
837 | sprintf(cd->u.stringbuf, "%lu", (unsigned long)thenode->exe(ctx, thenode, NULL)); | |
838 | anode->exe=exe_tostr_null; | |
839 | anode->returntype |= RETURNTYPE_CONST; | |
840 | } else { | |
841 | /* Variable data */ | |
842 | anode->exe=exe_inttostr; | |
843 | } | |
844 | break; | |
845 | ||
846 | case RETURNTYPE_BOOL: | |
847 | if (thenode->returntype & RETURNTYPE_CONST) { | |
848 | /* Constant bool value */ | |
849 | if (thenode->exe(ctx, thenode,NULL)) { | |
850 | /* True! */ | |
851 | sprintf(cd->u.stringbuf, "1"); | |
852 | } else { | |
853 | cd->u.stringbuf[0] = '\0'; | |
854 | } | |
855 | anode->exe=exe_tostr_null; | |
856 | anode->returntype |= RETURNTYPE_CONST; | |
857 | } else { | |
858 | /* Variable bool value */ | |
859 | anode->exe=exe_booltostr; | |
860 | } | |
861 | break; | |
862 | } | |
863 | break; | |
864 | ||
865 | case RETURNTYPE_INT: | |
866 | /* we want an int */ | |
867 | switch (thenode->returntype & RETURNTYPE_TYPE) { | |
868 | case RETURNTYPE_STRING: | |
869 | if (thenode->returntype & RETURNTYPE_CONST) { | |
870 | cd->u.val=strtoul((thenode->exe)(ctx, thenode, NULL), NULL, 10); | |
871 | anode->exe=exe_val_null; | |
872 | anode->returntype |= RETURNTYPE_CONST; | |
873 | } else { | |
874 | anode->exe=exe_strtoint; | |
875 | } | |
876 | break; | |
877 | ||
878 | default: | |
879 | case RETURNTYPE_BOOL: | |
880 | if (thenode->returntype & RETURNTYPE_CONST) { | |
881 | if ((thenode->exe)(ctx, thenode,NULL)) | |
882 | cd->u.val=1; | |
883 | else | |
884 | cd->u.val=0; | |
885 | ||
886 | anode->exe=exe_val_null; | |
887 | anode->returntype |= RETURNTYPE_CONST; | |
888 | } else { | |
889 | anode->exe=exe_booltoint; | |
890 | } | |
891 | break; | |
892 | } | |
893 | break; | |
894 | ||
895 | default: | |
896 | case RETURNTYPE_BOOL: | |
897 | /* we want a bool */ | |
898 | switch (thenode->returntype & RETURNTYPE_TYPE) { | |
899 | case RETURNTYPE_STRING: | |
900 | if (thenode->returntype & RETURNTYPE_CONST) { | |
901 | char *rv=(char *)((thenode->exe)(ctx, thenode, NULL)); | |
902 | if (!rv || *rv=='\0' || (*rv=='0' && rv[1]=='\0')) | |
903 | cd->u.val=0; | |
904 | else | |
905 | cd->u.val=1; | |
906 | ||
907 | anode->exe=exe_val_null; | |
908 | anode->returntype |= RETURNTYPE_CONST; | |
909 | } else { | |
910 | anode->exe=exe_strtobool; | |
911 | } | |
912 | break; | |
913 | ||
914 | default: | |
915 | case RETURNTYPE_INT: | |
916 | if (thenode->returntype & RETURNTYPE_CONST) { | |
917 | if ((thenode->exe)(ctx, thenode,NULL)) | |
918 | cd->u.val=1; | |
919 | else | |
920 | cd->u.val=0; | |
921 | ||
922 | anode->exe=exe_val_null; | |
923 | anode->returntype |= RETURNTYPE_CONST; | |
924 | } else { | |
925 | anode->exe=exe_inttobool; | |
926 | } | |
927 | break; | |
928 | } | |
929 | break; | |
930 | } | |
931 | ||
932 | return anode; | |
933 | } | |
934 | ||
935 | /* Literals always return constant strings... */ | |
936 | void *literal_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) { | |
937 | if (thenode->localdata) | |
938 | return ((sstring *)thenode->localdata)->content; | |
939 | else | |
940 | return ""; | |
941 | } | |
942 | ||
943 | void literal_free(searchCtx *ctx, struct searchNode *thenode) { | |
944 | freesstring(thenode->localdata); | |
945 | free(thenode); | |
946 | } | |
947 | ||
948 | static int unescape(char *input, char *output, size_t buflen) { | |
949 | char *ch, *ch2; | |
950 | int e=0; | |
951 | ||
952 | if (*input=='\"') { | |
953 | for (ch=input;*ch;ch++) { | |
954 | if(ch - input >= buflen) { | |
955 | parseError="Buffer overflow"; | |
956 | return 0; | |
957 | } | |
958 | } | |
959 | ||
960 | if (*(ch-1) != '\"') { | |
961 | parseError="Quote mismatch"; | |
962 | return 0; | |
963 | } | |
964 | ||
965 | *(ch-1)='\0'; | |
966 | input++; | |
967 | } | |
968 | ||
969 | ch2=output; | |
970 | for (ch=input;*ch;ch++) { | |
971 | if(ch - input >= buflen) { | |
972 | parseError="Buffer overflow"; | |
973 | return 0; | |
974 | } | |
975 | ||
976 | if (e) { | |
977 | e=0; | |
978 | *ch2++=*ch; | |
979 | } else if (*ch=='\\') { | |
980 | e=1; | |
981 | } else { | |
982 | *ch2++=*ch; | |
983 | } | |
984 | } | |
985 | *ch2='\0'; | |
986 | ||
987 | return 1; | |
988 | } | |
989 | ||
990 | struct searchNode *search_parse(searchCtx *ctx, char *cinput) { | |
991 | /* OK, we need to split the input into chunks on spaces and brackets.. */ | |
992 | char *argvector[100]; | |
993 | char inputb[1024]; | |
994 | char *input; | |
995 | char thestring[500]; | |
996 | int i,j,q=0,e=0; | |
997 | char *ch; | |
998 | struct Command *cmd; | |
999 | struct searchNode *thenode; | |
1000 | ||
1001 | strlcpy(inputb, cinput, sizeof(inputb)); | |
1002 | input = inputb; | |
1003 | ||
1004 | /* If it starts with a bracket, it's a function call.. */ | |
1005 | if (*input=='(') { | |
1006 | /* Skip past string */ | |
1007 | for (ch=input;*ch;ch++); | |
1008 | if (*(ch-1) != ')') { | |
1009 | parseError = "Bracket mismatch!"; | |
1010 | return NULL; | |
1011 | } | |
1012 | input++; | |
1013 | *(ch-1)='\0'; | |
1014 | ||
1015 | /* Split further args */ | |
1016 | i=-1; /* i = -1 BoW, 0 = inword, 1 = bracket nest depth */ | |
1017 | j=0; /* j = current arg */ | |
1018 | e=0; | |
1019 | q=0; | |
1020 | argvector[0]=""; | |
1021 | for (ch=input;*ch;ch++) { | |
1022 | /*printf("i: %d j: %d e: %d q: %d ch: '%c'\n", i, j, e, q, *ch);*/ | |
1023 | if (i==-1) { | |
1024 | argvector[j]=ch; | |
1025 | if (*ch=='(') { | |
1026 | i=1; | |
1027 | } else if (*ch != ' ') { | |
1028 | i=0; | |
1029 | if (*ch=='\\') { | |
1030 | e=1; | |
1031 | } else if (*ch=='\"') { | |
1032 | q=1; | |
1033 | } | |
1034 | } | |
1035 | } else if (e==1) { | |
1036 | e=0; | |
1037 | } else if (q==1) { | |
1038 | if (*ch=='\\') { | |
1039 | e=1; | |
1040 | } else if (*ch=='\"') { | |
1041 | q=0; | |
1042 | } | |
1043 | } else if (i==0) { | |
1044 | if (*ch=='\\') { | |
1045 | e=1; | |
1046 | } else if (*ch=='\"') { | |
1047 | q=1; | |
1048 | } else if (*ch==' ') { | |
1049 | *ch='\0'; | |
1050 | j++; | |
1051 | if(j >= (sizeof(argvector) / sizeof(*argvector))) { | |
1052 | parseError = "Too many arguments"; | |
1053 | return NULL; | |
1054 | } | |
1055 | i=-1; | |
1056 | } | |
1057 | } else { | |
1058 | if (*ch=='\\') { | |
1059 | e=1; | |
1060 | } else if (*ch=='\"') { | |
1061 | q=1; | |
1062 | } else if (*ch=='(') { | |
1063 | i++; | |
1064 | } else if (*ch==')') { | |
1065 | i--; | |
1066 | } | |
1067 | } | |
1068 | } | |
1069 | ||
1070 | if (i>0) { | |
1071 | parseError = "Bracket mismatch!"; | |
1072 | return NULL; | |
1073 | } | |
1074 | ||
1075 | if (*(ch-1) == 0) /* if the last character was a space */ | |
1076 | j--; /* remove an argument */ | |
1077 | ||
1078 | /* for(k=1;k<=j;k++) | |
1079 | if(!unescape(argvector[k], argvector[k], sizeof(inputb))) | |
1080 | return NULL; | |
1081 | */ | |
1082 | ||
1083 | if (!(cmd=findcommandintree(ctx->searchcmd->searchtree,argvector[0],1))) { | |
1084 | parseError = "Unknown command (for valid command list, see help <searchcmd>)"; | |
1085 | return NULL; | |
1086 | } else { | |
1087 | if (!controlpermitted(cmd->level, ctx->sender)) { | |
1088 | parseError = "Access denied (for valid command list, see help <searchcmd>)"; | |
1089 | return NULL; | |
1090 | } | |
1091 | return ((parseFunc)cmd->handler)(ctx, j, argvector+1); | |
1092 | } | |
1093 | } else { | |
1094 | /* Literal */ | |
1095 | ||
1096 | /* slug: disabled now we unescape during the main parse stage */ | |
1097 | if(!unescape(input, thestring, sizeof(thestring))) | |
1098 | return NULL; | |
1099 | ||
1100 | if (!(thenode=(struct searchNode *)malloc(sizeof(struct searchNode)))) { | |
1101 | parseError = "malloc: could not allocate memory for this search."; | |
1102 | return NULL; | |
1103 | } | |
1104 | ||
1105 | thenode->localdata = getsstring(thestring,512); | |
1106 | thenode->returntype = RETURNTYPE_CONST | RETURNTYPE_STRING; | |
1107 | thenode->exe = literal_exe; | |
1108 | thenode->free = literal_free; | |
1109 | ||
1110 | return thenode; | |
1111 | } | |
1112 | } | |
1113 | ||
1114 | void nssnprintf(char *buf, size_t size, const char *format, nick *np) { | |
1115 | StringBuf b; | |
1116 | const char *p; | |
1117 | char *c; | |
1118 | char hostbuf[512]; | |
1119 | ||
1120 | if(size == 0) | |
1121 | return; | |
1122 | ||
1123 | b.buf = buf; | |
1124 | b.capacity = size; | |
1125 | b.len = 0; | |
1126 | ||
1127 | for(p=format;*p;p++) { | |
1128 | if(*p != '%') { | |
1129 | if(!sbaddchar(&b, *p)) | |
1130 | break; | |
1131 | continue; | |
1132 | } | |
1133 | p++; | |
1134 | if(*p == '\0') | |
1135 | break; | |
1136 | if(*p == '%') { | |
1137 | if(!sbaddchar(&b, *p)) | |
1138 | break; | |
1139 | continue; | |
1140 | } | |
1141 | ||
1142 | c = NULL; | |
1143 | switch(*p) { | |
1144 | case 'n': | |
1145 | c = np->nick; break; | |
1146 | case 'i': | |
1147 | c = np->ident; break; | |
1148 | case 'h': | |
1149 | c = np->host->name->content; break; | |
1150 | case 'I': | |
1151 | snprintf(hostbuf, sizeof(hostbuf), "%s", IPtostr(np->p_ipaddr)); | |
1152 | c = hostbuf; | |
1153 | break; | |
1154 | case 'u': | |
1155 | snprintf(hostbuf, sizeof(hostbuf), "%s!%s@%s", np->nick, np->ident, IPtostr(np->p_ipaddr)); | |
1156 | c = hostbuf; | |
1157 | break; | |
1158 | default: | |
1159 | c = "(bad format specifier)"; | |
1160 | } | |
1161 | if(c) | |
1162 | if(!sbaddstr(&b, c)) | |
1163 | break; | |
1164 | } | |
1165 | ||
1166 | sbterminate(&b); | |
1167 | ||
1168 | /* not required */ | |
1169 | /* | |
1170 | buf[size-1] = '\0'; | |
1171 | */ | |
1172 | } | |
1173 | ||
1174 | static char *var_tochar(searchCtx *ctx, char *arg, searchNode **variable) { | |
1175 | *variable = ctx->parser(ctx, arg); | |
1176 | if (!(*variable = coerceNode(ctx, *variable, RETURNTYPE_STRING))) | |
1177 | return NULL; | |
1178 | ||
1179 | if(!((*variable)->returntype & RETURNTYPE_CONST)) { | |
1180 | parseError = "only constant variables allowed"; | |
1181 | ((*variable)->free)(ctx, *variable); | |
1182 | return NULL; | |
1183 | } | |
1184 | ||
1185 | return (char *)((*variable)->exe)(ctx, *variable, NULL); | |
1186 | } | |
1187 | ||
1188 | void free_val_null(searchCtx *ctx, struct searchNode *thenode) { | |
1189 | } | |
1190 | ||
1191 | struct searchVariable *var_register(searchCtx *ctx, char *arg, int type) { | |
1192 | searchNode *variable; | |
1193 | struct searchVariable *us; | |
1194 | char *var; | |
1195 | int i; | |
1196 | ||
1197 | if(ctx->lastvar >= MAX_VARIABLES) { | |
1198 | parseError = "Maximum number of variables reached"; | |
1199 | return NULL; | |
1200 | } | |
1201 | ||
1202 | us = &ctx->vars[ctx->lastvar]; | |
1203 | ||
1204 | var = var_tochar(ctx, arg, &variable); | |
1205 | if(!var) | |
1206 | return NULL; | |
1207 | ||
1208 | strlcpy(us->name, var, sizeof(us->name)); | |
1209 | (variable->free)(ctx, variable); | |
1210 | ||
1211 | for(i=0;i<ctx->lastvar;i++) { | |
1212 | if(!strcmp(us->name, ctx->vars[i].name)) { | |
1213 | parseError = "variable name already in use"; | |
1214 | return NULL; | |
1215 | } | |
1216 | } | |
1217 | ||
1218 | ctx->lastvar++; | |
1219 | us->data.returntype = type; | |
1220 | us->data.localdata = &us->cdata; | |
1221 | us->data.exe = exe_val_null; | |
1222 | us->data.free = free_val_null; | |
1223 | ||
1224 | us->cdata.child = NULL; | |
1225 | return us; | |
1226 | } | |
1227 | ||
1228 | searchNode *var_get(searchCtx *ctx, char *arg) { | |
1229 | searchNode *variable, *found = NULL; | |
1230 | int i; | |
1231 | char *var = var_tochar(ctx, arg, &variable); | |
1232 | if(!var) | |
1233 | return NULL; | |
1234 | ||
1235 | for(i=0;i<ctx->lastvar;i++) { | |
1236 | if(!strcmp(var, ctx->vars[i].name)) { | |
1237 | found = &ctx->vars[i].data; | |
1238 | break; | |
1239 | } | |
1240 | } | |
1241 | (variable->free)(ctx, variable); | |
1242 | ||
1243 | if(!found) | |
1244 | parseError = "variable not found"; | |
1245 | return found; | |
1246 | } | |
1247 | ||
1248 | void var_setstr(struct searchVariable *v, char *data) { | |
1249 | v->cdata.u.stringbuf = data; | |
1250 | } | |
1251 | ||
1252 | #ifdef NEWSEARCH_NEWPARSER | |
1253 | void displaystrerror(replyFunc reply, nick *np, const char *input) { | |
1254 | char buf[515]; | |
1255 | ||
1256 | if((parseStrErrorPos >= 0) && (parseStrErrorPos < sizeof(buf) - 3)) { | |
1257 | int i; | |
1258 | ||
1259 | for(i=0;i<parseStrErrorPos;i++) | |
1260 | buf[i] = ' '; | |
1261 | ||
1262 | buf[i++] = '^'; | |
1263 | buf[i] = '\0'; | |
1264 | ||
1265 | reply(np, "%s", input); | |
1266 | reply(np, "%s", buf); | |
1267 | } | |
1268 | ||
1269 | reply(np, "Parse error: %s", parseStrError); | |
1270 | } | |
1271 | #endif | |
1272 | ||
1273 | struct searchNode *argtoconststr(char *command, searchCtx *ctx, char *arg, char **p) { | |
1274 | struct searchNode *c; | |
1275 | static char errorbuf[512]; | |
1276 | ||
1277 | c = ctx->parser(ctx, arg); | |
1278 | if (!(c = coerceNode(ctx, c, RETURNTYPE_STRING))) { | |
1279 | snprintf(errorbuf, sizeof(errorbuf), "%s: unable to coerce argument to string", command); | |
1280 | parseError = errorbuf; | |
1281 | return NULL; | |
1282 | } | |
1283 | ||
1284 | if (!(c->returntype & RETURNTYPE_CONST)) { | |
1285 | snprintf(errorbuf, sizeof(errorbuf), "%s: constant argument required", command); | |
1286 | parseError = errorbuf; | |
1287 | (c->free)(ctx, c); | |
1288 | return NULL; | |
1289 | } | |
1290 | ||
1291 | if(p) | |
1292 | *p = (char *)(c->exe)(ctx, c, NULL); | |
1293 | ||
1294 | return c; | |
1295 | } |