]>
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 | ||
16 | MODULE_VERSION(""); | |
17 | ||
18 | CommandTree *searchCmdTree; | |
19 | searchList *globalterms = NULL; | |
20 | ||
21 | int do_nicksearch(void *source, int cargc, char **cargv); | |
22 | int do_chansearch(void *source, int cargc, char **cargv); | |
23 | int do_usersearch(void *source, int cargc, char **cargv); | |
24 | ||
25 | void printnick_channels(searchCtx *, nick *, nick *); | |
26 | void printchannel(searchCtx *, nick *, chanindex *); | |
27 | void printchannel_topic(searchCtx *, nick *, chanindex *); | |
28 | void printchannel_services(searchCtx *, nick *, chanindex *); | |
29 | ||
30 | UserDisplayFunc defaultuserfn = printuser; | |
31 | NickDisplayFunc defaultnickfn = printnick; | |
32 | ChanDisplayFunc defaultchanfn = printchannel; | |
33 | ||
34 | searchCmd *reg_nicksearch, *reg_chansearch, *reg_usersearch; | |
35 | ||
36 | searchCmd *registersearchcommand(char *name, int level, CommandHandler cmd, void *defaultdisplayfunc) { | |
37 | searchCmd *acmd; | |
38 | searchList *sl; | |
39 | ||
40 | registercontrolhelpcmd(name, NO_OPER,4, cmd, "Usage: <criteria\nSearches with the given criteria"); | |
41 | ||
42 | acmd=(struct searchCmd *)malloc(sizeof(struct searchCmd)); | |
43 | ||
44 | acmd->handler = cmd; | |
45 | ||
46 | acmd->name = getsstring( name, NSMAX_COMMAND_LEN); | |
47 | acmd->outputtree = newcommandtree(); | |
48 | acmd->searchtree = newcommandtree(); | |
49 | ||
50 | addcommandtotree(searchCmdTree, name, 0, 0, (CommandHandler)acmd); | |
51 | ||
52 | sl = globalterms; | |
53 | while (sl) { | |
54 | registersearchterm( acmd, sl->name->content, sl->cmd); | |
55 | sl = sl->next; | |
56 | } | |
57 | ||
58 | return acmd; | |
59 | } | |
60 | ||
61 | void deregistersearchcommand(searchCmd *scmd) { | |
62 | deregistercontrolcmd(scmd->name->content, (CommandHandler)scmd->handler); | |
63 | destroycommandtree(scmd->outputtree); | |
64 | destroycommandtree(scmd->searchtree); | |
65 | freesstring(scmd->name); | |
66 | free(scmd); | |
67 | } | |
68 | ||
69 | void regdisp( searchCmd *cmd, const char *name, void *handler) { | |
70 | addcommandtotree(cmd->outputtree, name, 0, 0, (CommandHandler) handler); | |
71 | } | |
72 | ||
73 | void unregdisp( searchCmd *cmd, const char *name, void *handler ) { | |
74 | deletecommandfromtree(cmd->outputtree, name, (CommandHandler) handler); | |
75 | } | |
76 | ||
77 | void *findcommandinlist( searchList *sl, char *name){ | |
78 | while(sl) { | |
79 | if(strcmp(sl->name->content,name) == 0 ) { | |
80 | return sl; | |
81 | } | |
82 | sl = sl->next; | |
83 | } | |
84 | return NULL; | |
85 | } | |
86 | ||
87 | const char *parseError; | |
88 | /* used for *_free functions that need to warn users of certain things | |
89 | i.e. hitting too many users in a (kill) or (gline) */ | |
90 | nick *senderNSExtern; | |
91 | ||
92 | void _init() { | |
93 | searchCmdTree=newcommandtree(); | |
94 | ||
95 | reg_nicksearch = (searchCmd *)registersearchcommand("nicksearch",NO_OPER,&do_nicksearch, printnick); | |
96 | reg_chansearch = (searchCmd *)registersearchcommand("chansearch",NO_OPER,&do_chansearch, printchannel); | |
97 | reg_usersearch = (searchCmd *)registersearchcommand("usersearch",NO_OPER,&do_usersearch, printuser); | |
98 | ||
99 | /* Boolean operations */ | |
100 | registerglobalsearchterm("and",and_parse); | |
101 | registerglobalsearchterm("not",not_parse); | |
102 | registerglobalsearchterm("or",or_parse); | |
103 | ||
104 | registerglobalsearchterm("eq",eq_parse); | |
105 | ||
106 | registerglobalsearchterm("lt",lt_parse); | |
107 | registerglobalsearchterm("gt",gt_parse); | |
108 | ||
109 | /* String operations */ | |
110 | registerglobalsearchterm("match",match_parse); | |
111 | registerglobalsearchterm("regex",regex_parse); | |
112 | registerglobalsearchterm("length",length_parse); | |
113 | ||
114 | /* Nickname operations */ | |
115 | registersearchterm(reg_nicksearch, "hostmask",hostmask_parse); /* nick only */ | |
116 | registersearchterm(reg_nicksearch, "realname",realname_parse); /* nick only */ | |
117 | registersearchterm(reg_nicksearch, "authname",authname_parse); /* nick only */ | |
118 | registersearchterm(reg_nicksearch, "authts",authts_parse); /* nick only */ | |
119 | registersearchterm(reg_nicksearch, "ident",ident_parse); /* nick only */ | |
120 | registersearchterm(reg_nicksearch, "host",host_parse); /* nick only */ | |
121 | registersearchterm(reg_nicksearch, "channel",channel_parse); /* nick only */ | |
122 | registersearchterm(reg_nicksearch, "timestamp",timestamp_parse); /* nick only */ | |
123 | registersearchterm(reg_nicksearch, "country",country_parse); /* nick only */ | |
124 | registersearchterm(reg_nicksearch, "ip",ip_parse); /* nick only */ | |
125 | registersearchterm(reg_nicksearch, "channels",channels_parse); /* nick only */ | |
126 | registersearchterm(reg_nicksearch, "server",server_parse); /* nick only */ | |
127 | registersearchterm(reg_nicksearch, "authid",authid_parse); /* nick only */ | |
128 | ||
129 | /* Channel operations */ | |
130 | registersearchterm(reg_chansearch, "exists",exists_parse); /* channel only */ | |
131 | registersearchterm(reg_chansearch, "services",services_parse); /* channel only */ | |
132 | registersearchterm(reg_chansearch, "size",size_parse); /* channel only */ | |
133 | registersearchterm(reg_chansearch, "name",name_parse); /* channel only */ | |
134 | registersearchterm(reg_chansearch, "topic",topic_parse); /* channel only */ | |
135 | registersearchterm(reg_chansearch, "oppct",oppct_parse); /* channel only */ | |
136 | registersearchterm(reg_chansearch, "uniquehostpct",hostpct_parse); /* channel only */ | |
137 | registersearchterm(reg_chansearch, "authedpct",authedpct_parse); /* channel only */ | |
138 | registersearchterm(reg_chansearch, "kick",kick_parse); /* channel only */ | |
139 | ||
140 | /* Nickname / channel operations */ | |
141 | registersearchterm(reg_chansearch, "modes",modes_parse); | |
142 | registersearchterm(reg_nicksearch, "modes",modes_parse); | |
143 | registersearchterm(reg_chansearch, "nick",nick_parse); | |
144 | registersearchterm(reg_nicksearch, "nick",nick_parse); | |
145 | ||
146 | /* Kill / gline parameters */ | |
147 | registersearchterm(reg_chansearch,"kill",kill_parse); | |
148 | registersearchterm(reg_chansearch,"gline",gline_parse); | |
149 | registersearchterm(reg_nicksearch,"kill",kill_parse); | |
150 | registersearchterm(reg_nicksearch,"gline",gline_parse); | |
151 | ||
152 | /* Iteration functionality */ | |
153 | registerglobalsearchterm("any",any_parse); | |
154 | registerglobalsearchterm("all",all_parse); | |
155 | registerglobalsearchterm("var",var_parse); | |
156 | ||
157 | /* Iterable functions */ | |
158 | registersearchterm(reg_nicksearch, "channeliter",channeliter_parse); /* nick only */ | |
159 | ||
160 | /* Notice functionality */ | |
161 | registersearchterm(reg_chansearch,"notice",notice_parse); | |
162 | registersearchterm(reg_nicksearch,"notice",notice_parse); | |
163 | ||
164 | /* Nick output filters */ | |
165 | regdisp(reg_nicksearch,"default",printnick); | |
166 | regdisp(reg_nicksearch,"channels",printnick_channels); | |
167 | ||
168 | /* Channel output filters */ | |
169 | regdisp(reg_chansearch,"default",printchannel); | |
170 | regdisp(reg_chansearch,"topic",printchannel_topic); | |
171 | regdisp(reg_chansearch,"services",printchannel_services); | |
172 | ||
173 | /* Nick output filters */ | |
174 | regdisp(reg_usersearch,"default",printuser); | |
175 | ||
176 | } | |
177 | ||
178 | void _fini() { | |
179 | searchList *sl, *psl; | |
180 | int i,n; | |
181 | Command *cmdlist[100]; | |
182 | ||
183 | sl = globalterms; | |
184 | while (sl) { | |
185 | psl = sl; | |
186 | sl = sl->next; | |
187 | ||
188 | n=getcommandlist(searchCmdTree,cmdlist,100); | |
189 | for(i=0;i<n;i++) { | |
190 | deregistersearchterm( (searchCmd *)cmdlist[i]->handler, psl->name->content, psl->cmd); | |
191 | } | |
192 | ||
193 | freesstring(psl->name); | |
194 | free(psl); | |
195 | ||
196 | } | |
197 | ||
198 | deregistersearchcommand( reg_nicksearch ); | |
199 | deregistersearchcommand( reg_chansearch ); | |
200 | deregistersearchcommand( reg_usersearch ); | |
201 | destroycommandtree( searchCmdTree ); | |
202 | } | |
203 | ||
204 | void registerglobalsearchterm(char *term, parseFunc parsefunc) { | |
205 | searchList *sl = malloc(sizeof(searchList)); | |
206 | int i,n; | |
207 | Command *cmdlist[100]; | |
208 | ||
209 | sl->cmd = parsefunc; | |
210 | sl->name = getsstring(term, NSMAX_COMMAND_LEN); | |
211 | sl->next = NULL; | |
212 | ||
213 | if ( globalterms != NULL ) { | |
214 | sl->next = globalterms; | |
215 | } | |
216 | globalterms = sl; | |
217 | ||
218 | n=getcommandlist(searchCmdTree,cmdlist,100); | |
219 | for(i=0;i<n;i++) { | |
220 | registersearchterm( (searchCmd *)cmdlist[i]->handler,term, parsefunc); | |
221 | } | |
222 | } | |
223 | ||
224 | void deregisterglobalsearchterm(char *term, parseFunc parsefunc) { | |
225 | int i,n; | |
226 | Command *cmdlist[100]; | |
227 | searchList *sl, *psl=NULL; | |
228 | ||
229 | sl = globalterms; | |
230 | while (sl) { | |
231 | if ( strcmp( sl->name->content, term) == 0 ) { | |
232 | break; | |
233 | } | |
234 | psl = sl; | |
235 | sl = sl->next; | |
236 | } | |
237 | ||
238 | if (sl) { | |
239 | if( psl ) { | |
240 | psl->next = sl->next; | |
241 | } | |
242 | ||
243 | n=getcommandlist(searchCmdTree,cmdlist,100); | |
244 | for(i=0;i<n;i++) { | |
245 | deregistersearchterm( (searchCmd *)cmdlist[i]->handler, term, parsefunc); | |
246 | } | |
247 | freesstring(sl->name); | |
248 | free(sl); | |
249 | } | |
250 | } | |
251 | ||
252 | void registersearchterm(searchCmd *cmd, char *term, parseFunc parsefunc) { | |
253 | addcommandtotree(cmd->searchtree, term, 0, 0, (CommandHandler) parsefunc); | |
254 | } | |
255 | ||
256 | void deregistersearchterm(searchCmd *cmd, char *term, parseFunc parsefunc) { | |
257 | deletecommandfromtree(cmd->searchtree, term, (CommandHandler) parsefunc); | |
258 | } | |
259 | ||
260 | static void controlwallwrapper(int level, char *format, ...) { | |
261 | char buf[1024]; | |
262 | va_list ap; | |
263 | ||
264 | va_start(ap, format); | |
265 | vsnprintf(buf, sizeof(buf), format, ap); | |
266 | controlwall(NO_OPER, level, "%s", buf); | |
267 | va_end(ap); | |
268 | } | |
269 | ||
270 | int parseopts(int cargc, char **cargv, int *arg, int *limit, void **subset, void **display, CommandTree *sl, replyFunc reply, void *sender) { | |
271 | char *ch; | |
272 | Command *cmd; | |
273 | struct irc_in_addr sin; unsigned char bits; | |
274 | ||
275 | if (*cargv[0] == '-') { | |
276 | /* options */ | |
277 | (*arg)++; | |
278 | ||
279 | for (ch=cargv[0]+1;*ch;ch++) { | |
280 | switch(*ch) { | |
281 | case 'l': | |
282 | if (cargc<*arg) { | |
283 | reply(sender,"Error: -l switch requires an argument"); | |
284 | return CMD_USAGE; | |
285 | } | |
286 | *limit=strtoul(cargv[(*arg)++],NULL,10); | |
287 | break; | |
288 | ||
289 | case 'd': | |
290 | if (cargc<*arg) { | |
291 | reply(sender,"Error: -d switch requires an argument"); | |
292 | return CMD_USAGE; | |
293 | } | |
294 | cmd=findcommandintree(sl, cargv[*arg],1); | |
295 | if (!cmd) { | |
296 | reply(sender,"Error: unknown output format %s",cargv[*arg]); | |
297 | return CMD_USAGE; | |
298 | } | |
299 | *display=(void *)cmd->handler; | |
300 | (*arg)++; | |
301 | break; | |
302 | ||
303 | case 's': | |
304 | if (cargc<*arg) { | |
305 | reply(sender,"Error: -s switch requires an argument"); | |
306 | return CMD_USAGE; | |
307 | } | |
308 | if (ipmask_parse(cargv[*arg], &sin, &bits) == 0) { | |
309 | reply(sender, "Error: Invalid CIDR mask supplied"); | |
310 | return CMD_USAGE; | |
311 | } | |
312 | *subset = (void *)refnode(iptree, &sin, bits); | |
313 | (*arg)++; | |
314 | break; | |
315 | ||
316 | default: | |
317 | reply(sender,"Unrecognised flag -%c.",*ch); | |
318 | } | |
319 | } | |
320 | } | |
321 | ||
322 | return CMD_OK; | |
323 | } | |
324 | ||
325 | void newsearch_ctxinit(searchCtx *ctx, searchParseFunc searchfn, replyFunc replyfn, wallFunc wallfn, void *arg, searchCmd *cmd) { | |
326 | memset(ctx, 0, sizeof(searchCtx)); | |
327 | ||
328 | ctx->reply = replyfn; | |
329 | ctx->wall = wallfn; | |
330 | ctx->parser = searchfn; | |
331 | ctx->arg = arg; | |
332 | ctx->searchcmd = cmd; | |
333 | } | |
334 | ||
335 | int do_nicksearch_real(replyFunc reply, wallFunc wall, void *source, int cargc, char **cargv) { | |
336 | nick *sender = source; | |
337 | struct searchNode *search; | |
338 | int limit=500; | |
339 | int arg=0; | |
340 | NickDisplayFunc display=defaultnickfn; | |
341 | searchCtx ctx; | |
342 | int ret; | |
343 | ||
344 | if (cargc<1) | |
345 | return CMD_USAGE; | |
346 | ||
347 | ret = parseopts(cargc, cargv, &arg, &limit, NULL, (void **)&display, reg_nicksearch->outputtree, reply, sender); | |
348 | if(ret != CMD_OK) | |
349 | return ret; | |
350 | ||
351 | if (arg>=cargc) { | |
352 | reply(sender,"No search terms - aborting."); | |
353 | return CMD_ERROR; | |
354 | } | |
355 | ||
356 | if (arg<(cargc-1)) { | |
357 | rejoinline(cargv[arg],cargc-arg); | |
358 | } | |
359 | ||
360 | newsearch_ctxinit(&ctx, search_parse, reply, wall, NULL, reg_nicksearch); | |
361 | ||
362 | if (!(search = ctx.parser(&ctx, cargv[arg]))) { | |
363 | reply(sender,"Parse error: %s",parseError); | |
364 | return CMD_ERROR; | |
365 | } | |
366 | ||
367 | nicksearch_exe(search, &ctx, sender, display, limit); | |
368 | ||
369 | (search->free)(&ctx, search); | |
370 | ||
371 | return CMD_OK; | |
372 | } | |
373 | ||
374 | int do_nicksearch(void *source, int cargc, char **cargv) { | |
375 | return do_nicksearch_real(controlreply, controlwallwrapper, source, cargc, cargv); | |
376 | } | |
377 | ||
378 | void nicksearch_exe(struct searchNode *search, searchCtx *ctx, nick *sender, NickDisplayFunc display, int limit) { | |
379 | int i, j; | |
380 | int matches = 0; | |
381 | unsigned int cmarker; | |
382 | unsigned int tchans=0,uchans=0; | |
383 | struct channel **cs; | |
384 | nick *np; | |
385 | senderNSExtern = sender; | |
386 | ||
387 | /* Get a marker value to mark "seen" channels for unique count */ | |
388 | cmarker=nextchanmarker(); | |
389 | ||
390 | /* The top-level node needs to return a BOOL */ | |
391 | search=coerceNode(ctx, search, RETURNTYPE_BOOL); | |
392 | ||
393 | for (i=0;i<NICKHASHSIZE;i++) { | |
394 | for (np=nicktable[i];np;np=np->next) { | |
395 | if ((search->exe)(ctx, search, np)) { | |
396 | /* Add total channels */ | |
397 | tchans += np->channels->cursi; | |
398 | ||
399 | /* Check channels for uniqueness */ | |
400 | cs=(channel **)np->channels->content; | |
401 | for (j=0;j<np->channels->cursi;j++) { | |
402 | if (cs[j]->index->marker != cmarker) { | |
403 | cs[j]->index->marker=cmarker; | |
404 | uchans++; | |
405 | } | |
406 | } | |
407 | ||
408 | if (matches<limit) | |
409 | display(ctx, sender, np); | |
410 | ||
411 | if (matches==limit) | |
412 | ctx->reply(sender, "--- More than %d matches, skipping the rest",limit); | |
413 | matches++; | |
414 | } | |
415 | } | |
416 | } | |
417 | ||
418 | ctx->reply(sender,"--- End of list: %d matches; users were on %u channels (%u unique, %.1f average clones)", | |
419 | matches, tchans, uchans, (float)tchans/uchans); | |
420 | } | |
421 | ||
422 | int do_chansearch_real(replyFunc reply, wallFunc wall, void *source, int cargc, char **cargv) { | |
423 | nick *sender = source; | |
424 | struct searchNode *search; | |
425 | int limit=500; | |
426 | int arg=0; | |
427 | ChanDisplayFunc display=defaultchanfn; | |
428 | searchCtx ctx; | |
429 | int ret; | |
430 | ||
431 | if (cargc<1) | |
432 | return CMD_USAGE; | |
433 | ||
434 | ret = parseopts(cargc, cargv, &arg, &limit, NULL, (void **)&display, reg_chansearch->outputtree, reply, sender); | |
435 | if(ret != CMD_OK) | |
436 | return ret; | |
437 | ||
438 | if (arg>=cargc) { | |
439 | reply(sender,"No search terms - aborting."); | |
440 | return CMD_ERROR; | |
441 | } | |
442 | ||
443 | if (arg<(cargc-1)) { | |
444 | rejoinline(cargv[arg],cargc-arg); | |
445 | } | |
446 | ||
447 | newsearch_ctxinit(&ctx, search_parse, reply, wall, NULL, reg_chansearch); | |
448 | if (!(search = ctx.parser(&ctx, cargv[arg]))) { | |
449 | reply(sender,"Parse error: %s",parseError); | |
450 | return CMD_ERROR; | |
451 | } | |
452 | ||
453 | chansearch_exe(search, &ctx, sender, display, limit); | |
454 | ||
455 | (search->free)(&ctx, search); | |
456 | ||
457 | return CMD_OK; | |
458 | } | |
459 | ||
460 | int do_chansearch(void *source, int cargc, char **cargv) { | |
461 | return do_chansearch_real(controlreply, controlwallwrapper, source, cargc, cargv); | |
462 | } | |
463 | ||
464 | void chansearch_exe(struct searchNode *search, searchCtx *ctx, nick *sender, ChanDisplayFunc display, int limit) { | |
465 | int i; | |
466 | chanindex *cip; | |
467 | int matches = 0; | |
468 | senderNSExtern = sender; | |
469 | ||
470 | search=coerceNode(ctx, search, RETURNTYPE_BOOL); | |
471 | ||
472 | for (i=0;i<CHANNELHASHSIZE;i++) { | |
473 | for (cip=chantable[i];cip;cip=cip->next) { | |
474 | if ((search->exe)(ctx, search, cip)) { | |
475 | if (matches<limit) | |
476 | display(ctx, sender, cip); | |
477 | if (matches==limit) | |
478 | ctx->reply(sender, "--- More than %d matches, skipping the rest",limit); | |
479 | matches++; | |
480 | } | |
481 | } | |
482 | } | |
483 | ||
484 | ctx->reply(sender,"--- End of list: %d matches", matches); | |
485 | } | |
486 | ||
487 | int do_usersearch_real(replyFunc reply, wallFunc wall, void *source, int cargc, char **cargv) { | |
488 | nick *sender = source; | |
489 | struct searchNode *search; | |
490 | int limit=500; | |
491 | int arg=0; | |
492 | UserDisplayFunc display=defaultuserfn; | |
493 | searchCtx ctx; | |
494 | int ret; | |
495 | ||
496 | if (cargc<1) | |
497 | return CMD_USAGE; | |
498 | ||
499 | ret = parseopts(cargc, cargv, &arg, &limit, NULL, (void **)&display, reg_usersearch->outputtree, reply, sender); | |
500 | if(ret != CMD_OK) | |
501 | return ret; | |
502 | ||
503 | if (arg>=cargc) { | |
504 | reply(sender,"No search terms - aborting."); | |
505 | return CMD_ERROR; | |
506 | } | |
507 | ||
508 | if (arg<(cargc-1)) { | |
509 | rejoinline(cargv[arg],cargc-arg); | |
510 | } | |
511 | ||
512 | newsearch_ctxinit(&ctx, search_parse, reply, wall, NULL, reg_usersearch); | |
513 | if (!(search = ctx.parser(&ctx, cargv[arg]))) { | |
514 | reply(sender,"Parse error: %s",parseError); | |
515 | return CMD_ERROR; | |
516 | } | |
517 | ||
518 | usersearch_exe(search, &ctx, sender, display, limit); | |
519 | ||
520 | (search->free)(&ctx, search); | |
521 | ||
522 | return CMD_OK; | |
523 | } | |
524 | ||
525 | int do_usersearch(void *source, int cargc, char **cargv) { | |
526 | return do_usersearch_real(controlreply, controlwallwrapper, source, cargc, cargv); | |
527 | } | |
528 | ||
529 | void usersearch_exe(struct searchNode *search, searchCtx *ctx, nick *sender, UserDisplayFunc display, int limit) { | |
530 | int i; | |
531 | authname *aup; | |
532 | int matches = 0; | |
533 | senderNSExtern = sender; | |
534 | ||
535 | search=coerceNode(ctx, search, RETURNTYPE_BOOL); | |
536 | ||
537 | for (i=0;i<AUTHNAMEHASHSIZE;i++) { | |
538 | for (aup=authnametable[i];aup;aup=aup->next) { | |
539 | if ((search->exe)(ctx, search, aup)) { | |
540 | if (matches<limit) | |
541 | display(ctx, sender, aup); | |
542 | if (matches==limit) | |
543 | ctx->reply(sender, "--- More than %d matches, skipping the rest",limit); | |
544 | matches++; | |
545 | } | |
546 | } | |
547 | } | |
548 | ||
549 | ctx->reply(sender,"--- End of list: %d matches", matches); | |
550 | } | |
551 | ||
552 | /* Free a coerce node */ | |
553 | void free_coerce(searchCtx *ctx, struct searchNode *thenode) { | |
554 | struct coercedata *cd=thenode->localdata; | |
555 | ||
556 | cd->child->free(ctx, cd->child); | |
557 | free(thenode->localdata); | |
558 | free(thenode); | |
559 | } | |
560 | ||
561 | /* Free a coerce node with a stringbuf allocated */ | |
562 | void free_coercestring(searchCtx *ctx, struct searchNode *thenode) { | |
563 | free(((struct coercedata *)thenode->localdata)->u.stringbuf); | |
564 | free_coerce(ctx, thenode); | |
565 | } | |
566 | ||
567 | /* exe_tostr_null: return the constant string */ | |
568 | void *exe_tostr_null(searchCtx *ctx, struct searchNode *thenode, void *theinput) { | |
569 | struct coercedata *cd=thenode->localdata; | |
570 | ||
571 | return cd->u.stringbuf; | |
572 | } | |
573 | ||
574 | /* exe_val_null: return the constant value */ | |
575 | void *exe_val_null(searchCtx *ctx, struct searchNode *thenode, void *theinput) { | |
576 | struct coercedata *cd=thenode->localdata; | |
577 | ||
578 | return (void *)cd->u.val; | |
579 | } | |
580 | ||
581 | /* Lots of very dull type conversion functions */ | |
582 | void *exe_inttostr(searchCtx *ctx, struct searchNode *thenode, void *theinput) { | |
583 | struct coercedata *cd=thenode->localdata; | |
584 | ||
585 | sprintf(cd->u.stringbuf, "%lu", (unsigned long)(cd->child->exe)(ctx, cd->child, theinput)); | |
586 | ||
587 | return cd->u.stringbuf; | |
588 | } | |
589 | ||
590 | void *exe_booltostr(searchCtx *ctx, struct searchNode *thenode, void *theinput) { | |
591 | struct coercedata *cd=thenode->localdata; | |
592 | ||
593 | if ((cd->child->exe)(ctx, cd->child, theinput)) { | |
594 | sprintf(cd->u.stringbuf,"1"); | |
595 | } else { | |
596 | cd->u.stringbuf[0]='\0'; | |
597 | } | |
598 | ||
599 | return cd->u.stringbuf; | |
600 | } | |
601 | ||
602 | void *exe_strtoint(searchCtx *ctx, struct searchNode *thenode, void *theinput) { | |
603 | struct coercedata *cd=thenode->localdata; | |
604 | ||
605 | return (void *)strtoul((cd->child->exe)(ctx,cd->child,theinput),NULL,10); | |
606 | } | |
607 | ||
608 | void *exe_booltoint(searchCtx *ctx, struct searchNode *thenode, void *theinput) { | |
609 | struct coercedata *cd=thenode->localdata; | |
610 | ||
611 | /* Don't need to do anything */ | |
612 | return (cd->child->exe)(ctx, cd->child, theinput); | |
613 | } | |
614 | ||
615 | void *exe_strtobool(searchCtx *ctx, struct searchNode *thenode, void *theinput) { | |
616 | struct coercedata *cd=thenode->localdata; | |
617 | char *ch=(cd->child->exe)(ctx, cd->child, theinput); | |
618 | ||
619 | if (!ch || *ch=='\0' || (*ch=='0' && ch[1]=='\0')) { | |
620 | return (void *)0; | |
621 | } else { | |
622 | return (void *)1; | |
623 | } | |
624 | } | |
625 | ||
626 | void *exe_inttobool(searchCtx *ctx, struct searchNode *thenode, void *theinput) { | |
627 | struct coercedata *cd=thenode->localdata; | |
628 | ||
629 | if ((cd->child->exe)(ctx, cd->child, theinput)) { | |
630 | return (void *)1; | |
631 | } else { | |
632 | return (void *)0; | |
633 | } | |
634 | } | |
635 | ||
636 | struct searchNode *coerceNode(searchCtx *ctx, struct searchNode *thenode, int type) { | |
637 | struct searchNode *anode; | |
638 | struct coercedata *cd; | |
639 | ||
640 | /* You can't coerce a NULL */ | |
641 | if (!thenode) | |
642 | return NULL; | |
643 | ||
644 | /* No effort required to coerce to the same type */ | |
645 | if (type==(thenode->returntype & RETURNTYPE_TYPE)) | |
646 | return thenode; | |
647 | ||
648 | anode=(struct searchNode *)malloc(sizeof(struct searchNode)); | |
649 | anode->localdata=cd=(struct coercedata *)malloc(sizeof(struct coercedata)); | |
650 | cd->child=thenode; | |
651 | anode->returntype=type; /* We'll return what they want, always */ | |
652 | anode->free=free_coerce; | |
653 | ||
654 | switch(type) { | |
655 | case RETURNTYPE_STRING: | |
656 | /* For a string we'll need a buffer */ | |
657 | /* A 64-bit number prints out to 20 digits, this leaves some slack */ | |
658 | cd->u.stringbuf=malloc(25); | |
659 | anode->free=free_coercestring; | |
660 | ||
661 | switch(thenode->returntype & RETURNTYPE_TYPE) { | |
662 | default: | |
663 | case RETURNTYPE_INT: | |
664 | if (thenode->returntype & RETURNTYPE_CONST) { | |
665 | /* Constant node: sort it out now */ | |
666 | sprintf(cd->u.stringbuf, "%lu", (unsigned long)thenode->exe(ctx, thenode, NULL)); | |
667 | anode->exe=exe_tostr_null; | |
668 | anode->returntype |= RETURNTYPE_CONST; | |
669 | } else { | |
670 | /* Variable data */ | |
671 | anode->exe=exe_inttostr; | |
672 | } | |
673 | break; | |
674 | ||
675 | case RETURNTYPE_BOOL: | |
676 | if (thenode->returntype & RETURNTYPE_CONST) { | |
677 | /* Constant bool value */ | |
678 | if (thenode->exe(ctx, thenode,NULL)) { | |
679 | /* True! */ | |
680 | sprintf(cd->u.stringbuf, "1"); | |
681 | } else { | |
682 | cd->u.stringbuf[0] = '\0'; | |
683 | } | |
684 | anode->exe=exe_tostr_null; | |
685 | anode->returntype |= RETURNTYPE_CONST; | |
686 | } else { | |
687 | /* Variable bool value */ | |
688 | anode->exe=exe_booltostr; | |
689 | } | |
690 | break; | |
691 | } | |
692 | break; | |
693 | ||
694 | case RETURNTYPE_INT: | |
695 | /* we want an int */ | |
696 | switch (thenode->returntype & RETURNTYPE_TYPE) { | |
697 | case RETURNTYPE_STRING: | |
698 | if (thenode->returntype & RETURNTYPE_CONST) { | |
699 | cd->u.val=strtoul((thenode->exe)(ctx, thenode, NULL), NULL, 10); | |
700 | anode->exe=exe_val_null; | |
701 | anode->returntype |= RETURNTYPE_CONST; | |
702 | } else { | |
703 | anode->exe=exe_strtoint; | |
704 | } | |
705 | break; | |
706 | ||
707 | default: | |
708 | case RETURNTYPE_BOOL: | |
709 | if (thenode->returntype & RETURNTYPE_CONST) { | |
710 | if ((thenode->exe)(ctx, thenode,NULL)) | |
711 | cd->u.val=1; | |
712 | else | |
713 | cd->u.val=0; | |
714 | ||
715 | anode->exe=exe_val_null; | |
716 | anode->returntype |= RETURNTYPE_CONST; | |
717 | } else { | |
718 | anode->exe=exe_booltoint; | |
719 | } | |
720 | break; | |
721 | } | |
722 | break; | |
723 | ||
724 | default: | |
725 | case RETURNTYPE_BOOL: | |
726 | /* we want a bool */ | |
727 | switch (thenode->returntype & RETURNTYPE_TYPE) { | |
728 | case RETURNTYPE_STRING: | |
729 | if (thenode->returntype & RETURNTYPE_CONST) { | |
730 | char *rv=(char *)((thenode->exe)(ctx, thenode, NULL)); | |
731 | if (!rv || *rv=='\0' || (*rv=='0' && rv[1]=='\0')) | |
732 | cd->u.val=0; | |
733 | else | |
734 | cd->u.val=1; | |
735 | ||
736 | anode->exe=exe_val_null; | |
737 | anode->returntype |= RETURNTYPE_CONST; | |
738 | } else { | |
739 | anode->exe=exe_strtobool; | |
740 | } | |
741 | break; | |
742 | ||
743 | default: | |
744 | case RETURNTYPE_INT: | |
745 | if (thenode->returntype & RETURNTYPE_CONST) { | |
746 | if ((thenode->exe)(ctx, thenode,NULL)) | |
747 | cd->u.val=1; | |
748 | else | |
749 | cd->u.val=0; | |
750 | ||
751 | anode->exe=exe_val_null; | |
752 | anode->returntype |= RETURNTYPE_CONST; | |
753 | } else { | |
754 | anode->exe=exe_inttobool; | |
755 | } | |
756 | break; | |
757 | } | |
758 | break; | |
759 | } | |
760 | ||
761 | return anode; | |
762 | } | |
763 | ||
764 | /* Literals always return constant strings... */ | |
765 | void *literal_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) { | |
766 | if (thenode->localdata) | |
767 | return ((sstring *)thenode->localdata)->content; | |
768 | else | |
769 | return ""; | |
770 | } | |
771 | ||
772 | void literal_free(searchCtx *ctx, struct searchNode *thenode) { | |
773 | freesstring(thenode->localdata); | |
774 | free(thenode); | |
775 | } | |
776 | ||
777 | /* search_parse: | |
778 | * Given an input string, return a searchNode. | |
779 | */ | |
780 | ||
781 | struct searchNode *search_parse(searchCtx *ctx, char *input) { | |
782 | /* OK, we need to split the input into chunks on spaces and brackets.. */ | |
783 | char *argvector[100]; | |
784 | char thestring[500]; | |
785 | int i,j,q=0,e=0; | |
786 | char *ch,*ch2; | |
787 | struct Command *cmd; | |
788 | struct searchNode *thenode; | |
789 | ||
790 | /* If it starts with a bracket, it's a function call.. */ | |
791 | if (*input=='(') { | |
792 | /* Skip past string */ | |
793 | for (ch=input;*ch;ch++); | |
794 | if (*(ch-1) != ')') { | |
795 | parseError = "Bracket mismatch!"; | |
796 | return NULL; | |
797 | } | |
798 | input++; | |
799 | *(ch-1)='\0'; | |
800 | ||
801 | /* Split further args */ | |
802 | i=-1; /* i = -1 BoW, 0 = inword, 1 = bracket nest depth */ | |
803 | j=0; /* j = current arg */ | |
804 | e=0; | |
805 | q=0; | |
806 | argvector[0]=""; | |
807 | for (ch=input;*ch;ch++) { | |
808 | if (i==-1) { | |
809 | argvector[j]=ch; | |
810 | if (*ch=='(') { | |
811 | i=1; | |
812 | } else if (*ch != ' ') { | |
813 | i=0; | |
814 | if (*ch=='\\') { | |
815 | e=1; | |
816 | } else if (*ch=='\"') { | |
817 | q=1; | |
818 | } | |
819 | } | |
820 | } else if (e==1) { | |
821 | e=0; | |
822 | } else if (q==1) { | |
823 | if (*ch=='\"') | |
824 | q=0; | |
825 | } else if (i==0) { | |
826 | if (*ch=='\\') { | |
827 | e=1; | |
828 | } else if (*ch=='\"') { | |
829 | q=1; | |
830 | } else if (*ch==' ') { | |
831 | *ch='\0'; | |
832 | j++; | |
833 | if(j >= (sizeof(argvector) / sizeof(*argvector))) { | |
834 | parseError = "Too many arguments"; | |
835 | return NULL; | |
836 | } | |
837 | i=-1; | |
838 | } | |
839 | } else { | |
840 | if (*ch=='\\') { | |
841 | e=1; | |
842 | } else if (*ch=='\"') { | |
843 | q=1; | |
844 | } else if (*ch=='(') { | |
845 | i++; | |
846 | } else if (*ch==')') { | |
847 | i--; | |
848 | } | |
849 | } | |
850 | } | |
851 | ||
852 | if (i>0) { | |
853 | parseError = "Bracket mismatch!"; | |
854 | return NULL; | |
855 | } | |
856 | ||
857 | if (*(ch-1) == 0) /* if the last character was a space */ | |
858 | j--; /* remove an argument */ | |
859 | ||
860 | if (!(cmd=findcommandintree(ctx->searchcmd->searchtree,argvector[0],1))) { | |
861 | parseError = "Unknown command"; | |
862 | return NULL; | |
863 | } else { | |
864 | return ((parseFunc)cmd->handler)(ctx, j, argvector+1); | |
865 | } | |
866 | } else { | |
867 | /* Literal */ | |
868 | if (*input=='\"') { | |
869 | for (ch=input;*ch;ch++); | |
870 | ||
871 | if (*(ch-1) != '\"') { | |
872 | parseError="Quote mismatch"; | |
873 | return NULL; | |
874 | } | |
875 | ||
876 | *(ch-1)='\0'; | |
877 | input++; | |
878 | } | |
879 | ||
880 | ch2=thestring; | |
881 | for (ch=input;*ch;ch++) { | |
882 | if (e) { | |
883 | e=0; | |
884 | *ch2++=*ch; | |
885 | } else if (*ch=='\\') { | |
886 | e=1; | |
887 | } else { | |
888 | *ch2++=*ch; | |
889 | } | |
890 | } | |
891 | *ch2='\0'; | |
892 | ||
893 | if (!(thenode=(struct searchNode *)malloc(sizeof(struct searchNode)))) { | |
894 | parseError = "malloc: could not allocate memory for this search."; | |
895 | return NULL; | |
896 | } | |
897 | ||
898 | thenode->localdata = getsstring(thestring,512); | |
899 | thenode->returntype = RETURNTYPE_CONST | RETURNTYPE_STRING; | |
900 | thenode->exe = literal_exe; | |
901 | thenode->free = literal_free; | |
902 | ||
903 | return thenode; | |
904 | } | |
905 | } | |
906 | ||
907 | void nssnprintf(char *buf, size_t size, const char *format, nick *np) { | |
908 | StringBuf b; | |
909 | const char *p; | |
910 | char *c; | |
911 | char hostbuf[512]; | |
912 | ||
913 | if(size == 0) | |
914 | return; | |
915 | ||
916 | b.buf = buf; | |
917 | b.capacity = size; | |
918 | b.len = 0; | |
919 | ||
920 | for(p=format;*p;p++) { | |
921 | if(*p != '%') { | |
922 | if(!sbaddchar(&b, *p)) | |
923 | break; | |
924 | continue; | |
925 | } | |
926 | p++; | |
927 | if(*p == '\0') | |
928 | break; | |
929 | if(*p == '%') { | |
930 | if(!sbaddchar(&b, *p)) | |
931 | break; | |
932 | continue; | |
933 | } | |
934 | ||
935 | c = NULL; | |
936 | switch(*p) { | |
937 | case 'n': | |
938 | c = np->nick; break; | |
939 | case 'i': | |
940 | c = np->ident; break; | |
941 | case 'h': | |
942 | c = np->host->name->content; break; | |
943 | case 'I': | |
944 | snprintf(hostbuf, sizeof(hostbuf), "%s", IPtostr(np->p_ipaddr)); | |
945 | c = hostbuf; | |
946 | break; | |
947 | case 'u': | |
948 | snprintf(hostbuf, sizeof(hostbuf), "%s!%s@%s", np->nick, np->ident, IPtostr(np->p_ipaddr)); | |
949 | c = hostbuf; | |
950 | break; | |
951 | default: | |
952 | c = "(bad format specifier)"; | |
953 | } | |
954 | if(c) | |
955 | if(!sbaddstr(&b, c)) | |
956 | break; | |
957 | } | |
958 | ||
959 | sbterminate(&b); | |
960 | ||
961 | /* not required */ | |
962 | /* | |
963 | buf[size-1] = '\0'; | |
964 | */ | |
965 | } | |
966 | ||
967 | static char *var_tochar(searchCtx *ctx, char *arg, searchNode **variable) { | |
968 | *variable = ctx->parser(ctx, arg); | |
969 | if (!(*variable = coerceNode(ctx, *variable, RETURNTYPE_STRING))) | |
970 | return NULL; | |
971 | ||
972 | if(!((*variable)->returntype & RETURNTYPE_CONST)) { | |
973 | parseError = "only constant variables allowed"; | |
974 | ((*variable)->free)(ctx, *variable); | |
975 | return NULL; | |
976 | } | |
977 | ||
978 | return (char *)((*variable)->exe)(ctx, *variable, NULL); | |
979 | } | |
980 | ||
981 | void free_val_null(searchCtx *ctx, struct searchNode *thenode) { | |
982 | } | |
983 | ||
984 | struct searchVariable *var_register(searchCtx *ctx, char *arg, int type) { | |
985 | searchNode *variable; | |
986 | struct searchVariable *us; | |
987 | char *var; | |
988 | int i; | |
989 | ||
990 | if(ctx->lastvar >= MAX_VARIABLES) { | |
991 | parseError = "Maximum number of variables reached"; | |
992 | return NULL; | |
993 | } | |
994 | ||
995 | us = &ctx->vars[ctx->lastvar]; | |
996 | ||
997 | var = var_tochar(ctx, arg, &variable); | |
998 | if(!var) | |
999 | return NULL; | |
1000 | ||
1001 | strlcpy(us->name, var, sizeof(us->name)); | |
1002 | (variable->free)(ctx, variable); | |
1003 | ||
1004 | for(i=0;i<ctx->lastvar;i++) { | |
1005 | if(!strcmp(us->name, ctx->vars[i].name)) { | |
1006 | parseError = "variable name already in use"; | |
1007 | return NULL; | |
1008 | } | |
1009 | } | |
1010 | ||
1011 | ctx->lastvar++; | |
1012 | us->data.returntype = type; | |
1013 | us->data.localdata = &us->cdata; | |
1014 | us->data.exe = exe_val_null; | |
1015 | us->data.free = free_val_null; | |
1016 | ||
1017 | us->cdata.child = NULL; | |
1018 | return us; | |
1019 | } | |
1020 | ||
1021 | searchNode *var_get(searchCtx *ctx, char *arg) { | |
1022 | searchNode *variable, *found = NULL; | |
1023 | int i; | |
1024 | char *var = var_tochar(ctx, arg, &variable); | |
1025 | if(!var) | |
1026 | return NULL; | |
1027 | ||
1028 | for(i=0;i<ctx->lastvar;i++) { | |
1029 | if(!strcmp(var, ctx->vars[i].name)) { | |
1030 | found = &ctx->vars[i].data; | |
1031 | break; | |
1032 | } | |
1033 | } | |
1034 | (variable->free)(ctx, variable); | |
1035 | ||
1036 | if(!found) | |
1037 | parseError = "variable not found"; | |
1038 | return found; | |
1039 | } | |
1040 | ||
1041 | void var_setstr(struct searchVariable *v, char *data) { | |
1042 | v->cdata.u.stringbuf = data; | |
1043 | } |