]> jfr.im git - irc/quakenet/newserv.git/blame - newsearch/newsearch.c
NEWSEARCH: check for NULL subset pointer in parseopts.
[irc/quakenet/newserv.git] / newsearch / newsearch.c
CommitLineData
c86edd1d 1#include <stdio.h>
0da2a4ae 2#include <stdarg.h>
b697c21c 3#include <string.h>
c86edd1d
Q
4
5#include "../irc/irc_config.h"
c86edd1d
Q
6#include "../lib/irc_string.h"
7#include "../parser/parser.h"
8#include "../control/control.h"
9#include "../lib/splitline.h"
87698d77 10#include "../lib/version.h"
ba8a65c4 11#include "../lib/stringbuf.h"
b697c21c 12#include "../lib/strlfunc.h"
a92bb8e1
P
13#include "../lib/array.h"
14#include "newsearch.h"
ffc11f03 15#include "parser.h"
87698d77 16
70b0a4e5 17MODULE_VERSION("");
c86edd1d 18
a92bb8e1
P
19CommandTree *searchCmdTree;
20searchList *globalterms = NULL;
c86edd1d
Q
21
22int do_nicksearch(void *source, int cargc, char **cargv);
56a84a81 23int do_chansearch(void *source, int cargc, char **cargv);
e8ad630b 24int do_usersearch(void *source, int cargc, char **cargv);
d00af236 25
3d2bf13b
CP
26void printnick_channels(searchCtx *, nick *, nick *);
27void printchannel(searchCtx *, nick *, chanindex *);
28void printchannel_topic(searchCtx *, nick *, chanindex *);
29void printchannel_services(searchCtx *, nick *, chanindex *);
c86edd1d 30
55dd7529
CP
31UserDisplayFunc defaultuserfn = printuser;
32NickDisplayFunc defaultnickfn = printnick;
33ChanDisplayFunc defaultchanfn = printchannel;
34
a92bb8e1 35searchCmd *reg_nicksearch, *reg_chansearch, *reg_usersearch;
e8ff7b61 36void displaycommandhelp(nick *, Command *);
9b276ca6 37void displaystrerror(replyFunc, nick *, const char *);
c86edd1d 38
a92bb8e1
P
39searchCmd *registersearchcommand(char *name, int level, CommandHandler cmd, void *defaultdisplayfunc) {
40 searchCmd *acmd;
41 searchList *sl;
73cfc302 42
e8ff7b61 43 registercontrolhelpfunccmd(name, NO_OPER,4, cmd, &displaycommandhelp);
a92bb8e1 44 acmd=(struct searchCmd *)malloc(sizeof(struct searchCmd));
e8ff7b61
P
45 if (!acmd) {
46 Error("newsearch", ERR_ERROR, "malloc failed: registersearchcommand");
47 return NULL;
48 }
a92bb8e1
P
49 acmd->handler = cmd;
50
51 acmd->name = getsstring( name, NSMAX_COMMAND_LEN);
52 acmd->outputtree = newcommandtree();
53 acmd->searchtree = newcommandtree();
73cfc302 54
a92bb8e1
P
55 addcommandtotree(searchCmdTree, name, 0, 0, (CommandHandler)acmd);
56
e8ff7b61 57 for (sl=globalterms; sl; sl=sl->next) {
1188d606 58 addcommandexttotree(acmd->searchtree, sl->name->content, 0, 1, (CommandHandler)sl->cmd, sl->help);
a92bb8e1
P
59 }
60
61 return acmd;
73cfc302 62}
63
a92bb8e1
P
64void deregistersearchcommand(searchCmd *scmd) {
65 deregistercontrolcmd(scmd->name->content, (CommandHandler)scmd->handler);
66 destroycommandtree(scmd->outputtree);
67 destroycommandtree(scmd->searchtree);
1188d606 68 deletecommandfromtree(searchCmdTree, scmd->name->content, (CommandHandler) scmd);
a92bb8e1
P
69 freesstring(scmd->name);
70 free(scmd);
e8ad630b
CP
71}
72
e8ff7b61
P
73void 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");
7a8355d1 105 controlreply(np, "Available Global Commands and Operators:\n" );
e8ff7b61
P
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");
7a8355d1 118 controlreply(np, "Available Commands and Operators for %s:\n", acmd->name->content);
e8ff7b61
P
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
133void regdisp( searchCmd *cmd, const char *name, void *handler, int level, char *help) {
134 addcommandexttotree(cmd->outputtree, name, level, 0, (CommandHandler) handler, (char *)help);
a92bb8e1
P
135}
136
137void unregdisp( searchCmd *cmd, const char *name, void *handler ) {
138 deletecommandfromtree(cmd->outputtree, name, (CommandHandler) handler);
e8ad630b
CP
139}
140
a92bb8e1 141void *findcommandinlist( searchList *sl, char *name){
e8ff7b61 142 while (sl) {
a92bb8e1
P
143 if(strcmp(sl->name->content,name) == 0 ) {
144 return sl;
145 }
e8ff7b61 146 sl=sl->next;
a92bb8e1
P
147 }
148 return NULL;
149}
150
c86edd1d 151const char *parseError;
16cc6e32
IB
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) */
219d27f1 154nick *senderNSExtern;
c86edd1d
Q
155
156void _init() {
a92bb8e1
P
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);
c86edd1d
Q
162
163 /* Boolean operations */
e8ff7b61
P
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))" );
f1903ace 167
e8ff7b61 168 registerglobalsearchterm("eq",eq_parse, "usage: (eq (X) Y)");
f1903ace 169
e8ff7b61
P
170 registerglobalsearchterm("lt",lt_parse, "usage: (lt (X) int)");
171 registerglobalsearchterm("gt",gt_parse, "usage: (gt (X) int)");
c86edd1d
Q
172
173 /* String operations */
e8ff7b61
P
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)");
c7f7a584 177
c86edd1d 178 /* Nickname operations */
7a8355d1
P
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 */
d369fba6 185 registersearchterm(reg_nicksearch, "channel",channel_parse, 0, "Valid Channel Name to match users against"); /* nick only */
7a8355d1 186 registersearchterm(reg_nicksearch, "timestamp",timestamp_parse, 0, "User's Timestamp"); /* nick only */
e8ff7b61 187 registersearchterm(reg_nicksearch, "country",country_parse, 0, "2 letter country code (data source is geoip)"); /* nick only */
7a8355d1 188 registersearchterm(reg_nicksearch, "ip",ip_parse, 0, "User's IP - ipv4 or ipv6 format as appropriate. Note: not 6to4"); /* nick only */
d369fba6
P
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 */
7a8355d1 191 registersearchterm(reg_nicksearch, "authid",authid_parse, 0, "User's Auth ID"); /* nick only */
8a959859 192 registersearchterm(reg_nicksearch, "cidr",cidr_parse, 0, "CIDR matching"); /* nick only */
e3b5fea4 193 registersearchterm(reg_nicksearch, "ipvsix",ipv6_parse, 0, "IPv6 user"); /* nick only */
c86edd1d 194
56a84a81 195 /* Channel operations */
d369fba6 196 registersearchterm(reg_chansearch, "exists",exists_parse, 0, "Returns if channel exists on network. Note: newserv may store data on empty channels"); /* channel only */
e8ff7b61 197 registersearchterm(reg_chansearch, "services",services_parse, 0, ""); /* channel only */
d369fba6
P
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 */
de8c57b4
C
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 */
d369fba6
P
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 */
56a84a81 207
c86edd1d 208 /* Nickname / channel operations */
d369fba6
P
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");
c86edd1d 213
16cc6e32 214 /* Kill / gline parameters */
d369fba6
P
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");
4622c762 219
b697c21c 220 /* Iteration functionality */
d369fba6
P
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");
b697c21c
CP
224
225 /* Iterable functions */
d369fba6 226 registersearchterm(reg_nicksearch, "channeliter",channeliter_parse, 0, "Channel Iterable function - usage: (any (channeliter x) (match (var x) #twilight*))"); /* nick only */
cb857218 227 registersearchterm(reg_chansearch, "nickiter",nickiter_parse, 0, "Nick Iterable function - usage: (any (nickiter x) (match (var x) nickname*))"); /* channel only */
90334b4c
CP
228
229 /* Functions that work on strings?! */
230 registersearchterm(reg_nicksearch, "cumodes", cumodes_parse, 0, "usage: (cumodes (var x) <modes>)");
31b4db90 231 registersearchterm(reg_chansearch, "cumodes", cumodes_parse, 0, "usage: (cumodes (var x) <modes>)");
90334b4c 232
4622c762 233 /* Notice functionality */
d369fba6
P
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");
a92bb8e1 236
73cfc302 237 /* Nick output filters */
e8ff7b61
P
238 regdisp(reg_nicksearch,"default",printnick, 0, "");
239 regdisp(reg_nicksearch,"channels",printnick_channels, 0, "include channels in output");
d00af236 240
73cfc302 241 /* Channel output filters */
e8ff7b61
P
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");
16cc6e32 245
e8ad630b 246 /* Nick output filters */
e8ff7b61 247 regdisp(reg_usersearch,"default",printuser, 0, "");
a92bb8e1 248
c86edd1d
Q
249}
250
251void _fini() {
a92bb8e1
P
252 searchList *sl, *psl;
253 int i,n;
254 Command *cmdlist[100];
255
e8ff7b61 256 sl=globalterms;
a92bb8e1
P
257 while (sl) {
258 psl = sl;
e8ff7b61 259 sl=sl->next;
a92bb8e1
P
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);
e8ff7b61
P
267 if (psl->help)
268 free (psl->help);
a92bb8e1 269 free(psl);
a92bb8e1
P
270 }
271
272 deregistersearchcommand( reg_nicksearch );
273 deregistersearchcommand( reg_chansearch );
274 deregistersearchcommand( reg_usersearch );
275 destroycommandtree( searchCmdTree );
276}
277
e8ff7b61 278void registerglobalsearchterm(char *term, parseFunc parsefunc, char *help) {
a92bb8e1
P
279 int i,n;
280 Command *cmdlist[100];
e8ff7b61
P
281 searchList *sl = malloc(sizeof(searchList));
282 if (!sl) {
283 Error("newsearch", ERR_ERROR, "malloc failed: registerglobalsearchterm");
284 return;
285 }
a92bb8e1
P
286
287 sl->cmd = parsefunc;
288 sl->name = getsstring(term, NSMAX_COMMAND_LEN);
289 sl->next = NULL;
e8ff7b61
P
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
a92bb8e1
P
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++) {
e8ff7b61
P
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);
a92bb8e1
P
314 }
315}
316
317void deregisterglobalsearchterm(char *term, parseFunc parsefunc) {
318 int i,n;
319 Command *cmdlist[100];
320 searchList *sl, *psl=NULL;
321
e8ff7b61 322 for (sl=globalterms; sl; sl=sl->next) {
a92bb8e1
P
323 if ( strcmp( sl->name->content, term) == 0 ) {
324 break;
325 }
326 psl = sl;
a92bb8e1
P
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++) {
e8ff7b61 336 deletecommandfromtree( ((searchCmd *)cmdlist[i]->handler)->searchtree, term, (CommandHandler) parsefunc);
a92bb8e1
P
337 }
338 freesstring(sl->name);
339 free(sl);
340 }
c86edd1d
Q
341}
342
e8ff7b61
P
343void 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);
c86edd1d
Q
346}
347
a92bb8e1 348void deregistersearchterm(searchCmd *cmd, char *term, parseFunc parsefunc) {
e8ff7b61 349 /* NOTE: global terms are removed from the tree within deregisterglobalsearchterm */
a92bb8e1 350 deletecommandfromtree(cmd->searchtree, term, (CommandHandler) parsefunc);
c86edd1d
Q
351}
352
bb4b25ee 353static void controlwallwrapper(int level, char *format, ...) __attribute__ ((format (printf, 2, 3)));
0da2a4ae
CP
354static 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
6cb1b086 364int parseopts(int cargc, char **cargv, int *arg, int *limit, void **subset, void *display, CommandTree *sl, replyFunc reply, void *sender) {
c86edd1d 365 char *ch;
a92bb8e1
P
366 Command *cmd;
367 struct irc_in_addr sin; unsigned char bits;
368
c86edd1d
Q
369 if (*cargv[0] == '-') {
370 /* options */
e8ad630b 371 (*arg)++;
c86edd1d
Q
372
373 for (ch=cargv[0]+1;*ch;ch++) {
374 switch(*ch) {
375 case 'l':
e8ad630b 376 if (cargc<*arg) {
e8ff7b61
P
377 reply(sender,"Error: -l switch requires an argument (for help, see help <searchcmd>)");
378 return CMD_ERROR;
c86edd1d 379 }
e8ad630b 380 *limit=strtoul(cargv[(*arg)++],NULL,10);
c86edd1d
Q
381 break;
382
73cfc302 383 case 'd':
e8ad630b 384 if (cargc<*arg) {
e8ff7b61
P
385 reply(sender,"Error: -d switch requires an argument (for help, see help <searchcmd>)");
386 return CMD_ERROR;
73cfc302 387 }
a92bb8e1 388 cmd=findcommandintree(sl, cargv[*arg],1);
73cfc302 389 if (!cmd) {
e8ff7b61
P
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;
73cfc302 396 }
6cb1b086 397 *((void **)display)=(void *)cmd->handler;
e8ad630b 398 (*arg)++;
73cfc302 399 break;
a92bb8e1
P
400
401 case 's':
97d26602
CP
402 if (subset == NULL) {
403 reply(sender,"Error: -s switch not supported for this search.");
404 return CMD_ERROR;
405 }
a92bb8e1 406 if (cargc<*arg) {
e8ff7b61
P
407 reply(sender,"Error: -s switch requires an argument (for help, see help <searchcmd>)");
408 return CMD_ERROR;
a92bb8e1
P
409 }
410 if (ipmask_parse(cargv[*arg], &sin, &bits) == 0) {
e8ff7b61
P
411 reply(sender, "Error: Invalid CIDR mask supplied (for help, see help <searchcmd>)");
412 return CMD_ERROR;
a92bb8e1
P
413 }
414 *subset = (void *)refnode(iptree, &sin, bits);
415 (*arg)++;
416 break;
73cfc302 417
c86edd1d 418 default:
e8ff7b61
P
419 reply(sender,"Unrecognised flag -%c. (for help, see help <searchcmd>)",*ch);
420 return CMD_ERROR;
c86edd1d
Q
421 }
422 }
423 }
424
e8ad630b
CP
425 return CMD_OK;
426}
427
6b2202d0 428void newsearch_ctxinit(searchCtx *ctx, searchParseFunc searchfn, replyFunc replyfn, wallFunc wallfn, void *arg, searchCmd *cmd, nick *np, void *displayfn, int limit) {
b697c21c
CP
429 memset(ctx, 0, sizeof(searchCtx));
430
431 ctx->reply = replyfn;
432 ctx->wall = wallfn;
433 ctx->parser = searchfn;
434 ctx->arg = arg;
a92bb8e1 435 ctx->searchcmd = cmd;
e8ff7b61 436 ctx->sender = np;
6b2202d0
CP
437 ctx->limit = limit;
438 ctx->displayfn = displayfn;
b697c21c
CP
439}
440
e8ad630b 441int do_nicksearch_real(replyFunc reply, wallFunc wall, void *source, int cargc, char **cargv) {
b697c21c 442 nick *sender = source;
e8ad630b
CP
443 int limit=500;
444 int arg=0;
55dd7529 445 NickDisplayFunc display=defaultnickfn;
e8ad630b 446 int ret;
ffc11f03
CP
447#ifndef NEWSEARCH_NEWPARSER
448 searchCtx ctx;
449 struct searchNode *search;
450#else
451 parsertree *tree;
452#endif
e8ad630b 453
e8ff7b61
P
454 if (cargc<1) {
455 reply( sender, "Usage: [flags] <criteria>");
456 reply( sender, "For help, see help nicksearch");
457 return CMD_OK;
458 }
459
6cb1b086 460 ret = parseopts(cargc, cargv, &arg, &limit, NULL, (void *)&display, reg_nicksearch->outputtree, reply, sender);
e8ad630b
CP
461 if(ret != CMD_OK)
462 return ret;
463
c86edd1d 464 if (arg>=cargc) {
4156103f 465 reply(sender,"No search terms - aborting.");
c86edd1d
Q
466 return CMD_ERROR;
467 }
468
469 if (arg<(cargc-1)) {
470 rejoinline(cargv[arg],cargc-arg);
471 }
c8be5183 472
ffc11f03 473#ifndef NEWSEARCH_NEWPARSER
6b2202d0 474 newsearch_ctxinit(&ctx, search_parse, reply, wall, NULL, reg_nicksearch, sender, display, limit);
f33f3f52 475 if (!(search = ctx.parser(&ctx, cargv[arg]))) {
0da2a4ae 476 reply(sender,"Parse error: %s",parseError);
c86edd1d
Q
477 return CMD_ERROR;
478 }
c2c414be 479
6b2202d0 480 nicksearch_exe(search, &ctx);
c8be5183
CP
481
482 (search->free)(&ctx, search);
ffc11f03
CP
483#else
484 tree = parse_string(reg_nicksearch, cargv[arg]);
485 if(!tree) {
9b276ca6 486 displaystrerror(reply, sender, cargv[arg]);
ffc11f03
CP
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
c2c414be
CP
494
495 return CMD_OK;
496}
497
0da2a4ae
CP
498int do_nicksearch(void *source, int cargc, char **cargv) {
499 return do_nicksearch_real(controlreply, controlwallwrapper, source, cargc, cargv);
500}
501
6b2202d0 502void nicksearch_exe(struct searchNode *search, searchCtx *ctx) {
c2c414be
CP
503 int i, j;
504 int matches = 0;
505 unsigned int cmarker;
506 unsigned int tchans=0,uchans=0;
507 struct channel **cs;
6b2202d0 508 nick *np, *sender = ctx->sender;
b697c21c 509 senderNSExtern = sender;
6b2202d0
CP
510 NickDisplayFunc display = ctx->displayfn;
511 int limit = ctx->limit;
512
7c02e111 513 /* Get a marker value to mark "seen" channels for unique count */
514 cmarker=nextchanmarker();
515
c7f7a584 516 /* The top-level node needs to return a BOOL */
c8be5183 517 search=coerceNode(ctx, search, RETURNTYPE_BOOL);
c7f7a584 518
c86edd1d
Q
519 for (i=0;i<NICKHASHSIZE;i++) {
520 for (np=nicktable[i];np;np=np->next) {
c8be5183 521 if ((search->exe)(ctx, search, np)) {
7c02e111 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
c86edd1d 534 if (matches<limit)
3d2bf13b 535 display(ctx, sender, np);
7c02e111 536
c86edd1d 537 if (matches==limit)
c8be5183 538 ctx->reply(sender, "--- More than %d matches, skipping the rest",limit);
c86edd1d
Q
539 matches++;
540 }
541 }
542 }
543
c8be5183 544 ctx->reply(sender,"--- End of list: %d matches; users were on %u channels (%u unique, %.1f average clones)",
7c02e111 545 matches, tchans, uchans, (float)tchans/uchans);
c86edd1d
Q
546}
547
0da2a4ae 548int do_chansearch_real(replyFunc reply, wallFunc wall, void *source, int cargc, char **cargv) {
b697c21c 549 nick *sender = source;
c2c414be 550 int limit=500;
56a84a81 551 int arg=0;
55dd7529 552 ChanDisplayFunc display=defaultchanfn;
e8ad630b 553 int ret;
ffc11f03
CP
554#ifndef NEWSEARCH_NEWPARSER
555 struct searchNode *search;
556 searchCtx ctx;
557#else
558 parsertree *tree;
559#endif
56a84a81 560
e8ff7b61
P
561 if (cargc<1) {
562 reply( sender, "Usage: [flags] <criteria>");
563 reply( sender, "For help, see help chansearch");
564 return CMD_OK;
565 }
56a84a81 566
6cb1b086 567 ret = parseopts(cargc, cargv, &arg, &limit, NULL, (void *)&display, reg_chansearch->outputtree, reply, sender);
e8ad630b
CP
568 if(ret != CMD_OK)
569 return ret;
56a84a81
IB
570
571 if (arg>=cargc) {
4156103f 572 reply(sender,"No search terms - aborting.");
56a84a81
IB
573 return CMD_ERROR;
574 }
575
576 if (arg<(cargc-1)) {
577 rejoinline(cargv[arg],cargc-arg);
578 }
c2c414be 579
ffc11f03 580#ifndef NEWSEARCH_NEWPARSER
6b2202d0 581 newsearch_ctxinit(&ctx, search_parse, reply, wall, NULL, reg_chansearch, sender, display, limit);
f33f3f52 582 if (!(search = ctx.parser(&ctx, cargv[arg]))) {
0da2a4ae 583 reply(sender,"Parse error: %s",parseError);
56a84a81
IB
584 return CMD_ERROR;
585 }
c7f7a584 586
6b2202d0 587 chansearch_exe(search, &ctx);
c2c414be 588
c8be5183 589 (search->free)(&ctx, search);
ffc11f03
CP
590#else
591 tree = parse_string(reg_chansearch, cargv[arg]);
592 if(!tree) {
9b276ca6 593 displaystrerror(reply, sender, cargv[arg]);
ffc11f03
CP
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
c2c414be
CP
601
602 return CMD_OK;
603}
604
0da2a4ae
CP
605int do_chansearch(void *source, int cargc, char **cargv) {
606 return do_chansearch_real(controlreply, controlwallwrapper, source, cargc, cargv);
607}
608
6b2202d0 609void chansearch_exe(struct searchNode *search, searchCtx *ctx) {
c2c414be
CP
610 int i;
611 chanindex *cip;
612 int matches = 0;
6b2202d0 613 nick *sender = ctx->sender;
b697c21c 614 senderNSExtern = sender;
6b2202d0
CP
615 ChanDisplayFunc display = ctx->displayfn;
616 int limit = ctx->limit;
c2c414be 617
c8be5183 618 search=coerceNode(ctx, search, RETURNTYPE_BOOL);
56a84a81
IB
619
620 for (i=0;i<CHANNELHASHSIZE;i++) {
621 for (cip=chantable[i];cip;cip=cip->next) {
c8be5183 622 if ((search->exe)(ctx, search, cip)) {
56a84a81 623 if (matches<limit)
3d2bf13b 624 display(ctx, sender, cip);
56a84a81 625 if (matches==limit)
c8be5183 626 ctx->reply(sender, "--- More than %d matches, skipping the rest",limit);
56a84a81
IB
627 matches++;
628 }
629 }
630 }
631
c8be5183 632 ctx->reply(sender,"--- End of list: %d matches", matches);
56a84a81
IB
633}
634
e8ad630b 635int do_usersearch_real(replyFunc reply, wallFunc wall, void *source, int cargc, char **cargv) {
b697c21c 636 nick *sender = source;
e8ad630b
CP
637 int limit=500;
638 int arg=0;
55dd7529 639 UserDisplayFunc display=defaultuserfn;
e8ad630b 640 int ret;
ffc11f03
CP
641#ifndef NEWSEARCH_NEWPARSER
642 struct searchNode *search;
643 searchCtx ctx;
644#else
645 parsertree *tree;
646#endif
e8ad630b 647
e8ff7b61
P
648 if (cargc<1) {
649 reply( sender, "Usage: [flags] <criteria>");
650 reply( sender, "For help, see help usersearch");
651 return CMD_OK;
652 }
653
6cb1b086 654 ret = parseopts(cargc, cargv, &arg, &limit, NULL, (void *)&display, reg_usersearch->outputtree, reply, sender);
e8ad630b
CP
655 if(ret != CMD_OK)
656 return ret;
657
658 if (arg>=cargc) {
4156103f 659 reply(sender,"No search terms - aborting.");
e8ad630b
CP
660 return CMD_ERROR;
661 }
662
663 if (arg<(cargc-1)) {
664 rejoinline(cargv[arg],cargc-arg);
665 }
666
ffc11f03 667#ifndef NEWSEARCH_NEWPARSER
6b2202d0 668 newsearch_ctxinit(&ctx, search_parse, reply, wall, NULL, reg_usersearch, sender, display, limit);
e8ff7b61 669
f33f3f52 670 if (!(search = ctx.parser(&ctx, cargv[arg]))) {
e8ad630b
CP
671 reply(sender,"Parse error: %s",parseError);
672 return CMD_ERROR;
673 }
674
6b2202d0 675 usersearch_exe(search, &ctx);
e8ad630b
CP
676
677 (search->free)(&ctx, search);
ffc11f03
CP
678#else
679 tree = parse_string(reg_usersearch, cargv[arg]);
680 if(!tree) {
9b276ca6 681 displaystrerror(reply, sender, cargv[arg]);
ffc11f03
CP
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
e8ad630b
CP
689
690 return CMD_OK;
691}
692
693int do_usersearch(void *source, int cargc, char **cargv) {
694 return do_usersearch_real(controlreply, controlwallwrapper, source, cargc, cargv);
695}
696
6b2202d0 697void usersearch_exe(struct searchNode *search, searchCtx *ctx) {
e8ad630b
CP
698 int i;
699 authname *aup;
700 int matches = 0;
6b2202d0
CP
701 nick *sender = ctx->sender;
702 int limit = ctx->limit;
703 UserDisplayFunc display = ctx->displayfn;
b697c21c 704 senderNSExtern = sender;
6b2202d0 705
e8ad630b
CP
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
c7f7a584 723/* Free a coerce node */
c8be5183 724void free_coerce(searchCtx *ctx, struct searchNode *thenode) {
c7f7a584 725 struct coercedata *cd=thenode->localdata;
726
c8be5183 727 cd->child->free(ctx, cd->child);
c7f7a584 728 free(thenode->localdata);
729 free(thenode);
730}
c86edd1d 731
c7f7a584 732/* Free a coerce node with a stringbuf allocated */
c8be5183 733void free_coercestring(searchCtx *ctx, struct searchNode *thenode) {
c7f7a584 734 free(((struct coercedata *)thenode->localdata)->u.stringbuf);
c8be5183 735 free_coerce(ctx, thenode);
c7f7a584 736}
737
738/* exe_tostr_null: return the constant string */
c8be5183 739void *exe_tostr_null(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
c7f7a584 740 struct coercedata *cd=thenode->localdata;
741
742 return cd->u.stringbuf;
743}
744
745/* exe_val_null: return the constant value */
c8be5183 746void *exe_val_null(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
c7f7a584 747 struct coercedata *cd=thenode->localdata;
748
749 return (void *)cd->u.val;
750}
751
752/* Lots of very dull type conversion functions */
c8be5183 753void *exe_inttostr(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
c7f7a584 754 struct coercedata *cd=thenode->localdata;
755
c8be5183 756 sprintf(cd->u.stringbuf, "%lu", (unsigned long)(cd->child->exe)(ctx, cd->child, theinput));
c7f7a584 757
758 return cd->u.stringbuf;
759}
760
c8be5183 761void *exe_booltostr(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
c7f7a584 762 struct coercedata *cd=thenode->localdata;
763
c8be5183 764 if ((cd->child->exe)(ctx, cd->child, theinput)) {
c7f7a584 765 sprintf(cd->u.stringbuf,"1");
766 } else {
767 cd->u.stringbuf[0]='\0';
768 }
769
770 return cd->u.stringbuf;
771}
c86edd1d 772
c8be5183 773void *exe_strtoint(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
c7f7a584 774 struct coercedata *cd=thenode->localdata;
775
c8be5183 776 return (void *)strtoul((cd->child->exe)(ctx,cd->child,theinput),NULL,10);
c7f7a584 777}
778
c8be5183 779void *exe_booltoint(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
c7f7a584 780 struct coercedata *cd=thenode->localdata;
781
782 /* Don't need to do anything */
c8be5183 783 return (cd->child->exe)(ctx, cd->child, theinput);
c7f7a584 784}
785
c8be5183 786void *exe_strtobool(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
c7f7a584 787 struct coercedata *cd=thenode->localdata;
c8be5183 788 char *ch=(cd->child->exe)(ctx, cd->child, theinput);
c7f7a584 789
790 if (!ch || *ch=='\0' || (*ch=='0' && ch[1]=='\0')) {
791 return (void *)0;
792 } else {
793 return (void *)1;
794 }
795}
796
c8be5183 797void *exe_inttobool(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
c7f7a584 798 struct coercedata *cd=thenode->localdata;
799
c8be5183 800 if ((cd->child->exe)(ctx, cd->child, theinput)) {
c7f7a584 801 return (void *)1;
802 } else {
803 return (void *)0;
804 }
805}
806
c8be5183 807struct searchNode *coerceNode(searchCtx *ctx, struct searchNode *thenode, int type) {
c7f7a584 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 */
c8be5183 837 sprintf(cd->u.stringbuf, "%lu", (unsigned long)thenode->exe(ctx, thenode, NULL));
c7f7a584 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 */
c8be5183 849 if (thenode->exe(ctx, thenode,NULL)) {
c7f7a584 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) {
c8be5183 870 cd->u.val=strtoul((thenode->exe)(ctx, thenode, NULL), NULL, 10);
c7f7a584 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) {
c8be5183 881 if ((thenode->exe)(ctx, thenode,NULL))
c7f7a584 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) {
c8be5183 901 char *rv=(char *)((thenode->exe)(ctx, thenode, NULL));
c7f7a584 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) {
c8be5183 917 if ((thenode->exe)(ctx, thenode,NULL))
c7f7a584 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... */
c8be5183 936void *literal_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
2e2a8c6a
P
937 if (thenode->localdata)
938 return ((sstring *)thenode->localdata)->content;
939 else
940 return "";
c7f7a584 941}
942
c8be5183 943void literal_free(searchCtx *ctx, struct searchNode *thenode) {
c7f7a584 944 freesstring(thenode->localdata);
945 free(thenode);
946}
c86edd1d 947
ffe23bc1
CP
948static 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
990struct searchNode *search_parse(searchCtx *ctx, char *cinput) {
c86edd1d
Q
991 /* OK, we need to split the input into chunks on spaces and brackets.. */
992 char *argvector[100];
ffe23bc1
CP
993 char inputb[1024];
994 char *input;
1bf9cda6 995 char thestring[500];
e88c37d8 996 int i,j,q=0,e=0;
ffe23bc1 997 char *ch;
c86edd1d
Q
998 struct Command *cmd;
999 struct searchNode *thenode;
c86edd1d 1000
ffe23bc1
CP
1001 strlcpy(inputb, cinput, sizeof(inputb));
1002 input = inputb;
1003
c86edd1d
Q
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 */
1bf9cda6 1018 e=0;
1019 q=0;
a33e0d2b 1020 argvector[0]="";
c86edd1d 1021 for (ch=input;*ch;ch++) {
25ee628a 1022 /*printf("i: %d j: %d e: %d q: %d ch: '%c'\n", i, j, e, q, *ch);*/
c86edd1d 1023 if (i==-1) {
1bf9cda6 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) {
25ee628a
CP
1038 if (*ch=='\\') {
1039 e=1;
1040 } else if (*ch=='\"') {
1041 q=0;
1042 }
c86edd1d 1043 } else if (i==0) {
1bf9cda6 1044 if (*ch=='\\') {
1045 e=1;
1046 } else if (*ch=='\"') {
1047 q=1;
1048 } else if (*ch==' ') {
1049 *ch='\0';
1050 j++;
4cd6347c
P
1051 if(j >= (sizeof(argvector) / sizeof(*argvector))) {
1052 parseError = "Too many arguments";
1053 return NULL;
1054 }
1bf9cda6 1055 i=-1;
1056 }
c86edd1d 1057 } else {
1bf9cda6 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 }
c86edd1d
Q
1067 }
1068 }
1069
1070 if (i>0) {
1071 parseError = "Bracket mismatch!";
1072 return NULL;
1073 }
4cd6347c
P
1074
1075 if (*(ch-1) == 0) /* if the last character was a space */
1076 j--; /* remove an argument */
c86edd1d 1077
f58cb0de 1078/* for(k=1;k<=j;k++)
ffe23bc1
CP
1079 if(!unescape(argvector[k], argvector[k], sizeof(inputb)))
1080 return NULL;
f58cb0de 1081*/
25ee628a 1082
a92bb8e1 1083 if (!(cmd=findcommandintree(ctx->searchcmd->searchtree,argvector[0],1))) {
e8ff7b61 1084 parseError = "Unknown command (for valid command list, see help <searchcmd>)";
c86edd1d
Q
1085 return NULL;
1086 } else {
ffe23bc1
CP
1087 if (!controlpermitted(cmd->level, ctx->sender)) {
1088 parseError = "Access denied (for valid command list, see help <searchcmd>)";
e8ff7b61
P
1089 return NULL;
1090 }
f33f3f52 1091 return ((parseFunc)cmd->handler)(ctx, j, argvector+1);
c86edd1d
Q
1092 }
1093 } else {
1094 /* Literal */
1bf9cda6 1095
ffe23bc1 1096 /* slug: disabled now we unescape during the main parse stage */
ffe23bc1
CP
1097 if(!unescape(input, thestring, sizeof(thestring)))
1098 return NULL;
ffe23bc1 1099
9ce4f0be
IB
1100 if (!(thenode=(struct searchNode *)malloc(sizeof(struct searchNode)))) {
1101 parseError = "malloc: could not allocate memory for this search.";
1102 return NULL;
1bf9cda6 1103 }
1104
2db45047 1105 thenode->localdata = getsstring(thestring,512);
c86edd1d
Q
1106 thenode->returntype = RETURNTYPE_CONST | RETURNTYPE_STRING;
1107 thenode->exe = literal_exe;
1108 thenode->free = literal_free;
1109
1110 return thenode;
1111 }
1112}
2ba836f2 1113
2ba836f2 1114void nssnprintf(char *buf, size_t size, const char *format, nick *np) {
ba8a65c4 1115 StringBuf b;
2ba836f2
CP
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 != '%') {
ba8a65c4 1129 if(!sbaddchar(&b, *p))
2ba836f2
CP
1130 break;
1131 continue;
1132 }
1133 p++;
1134 if(*p == '\0')
1135 break;
1136 if(*p == '%') {
ba8a65c4 1137 if(!sbaddchar(&b, *p))
2ba836f2
CP
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)
ba8a65c4 1162 if(!sbaddstr(&b, c))
2ba836f2
CP
1163 break;
1164 }
1165
0be0b2d0 1166 sbterminate(&b);
2ba836f2
CP
1167
1168 /* not required */
1169 /*
1170 buf[size-1] = '\0';
1171 */
1172}
1173
f33f3f52
P
1174static char *var_tochar(searchCtx *ctx, char *arg, searchNode **variable) {
1175 *variable = ctx->parser(ctx, arg);
b697c21c
CP
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
1188void free_val_null(searchCtx *ctx, struct searchNode *thenode) {
1189}
1190
f33f3f52 1191struct searchVariable *var_register(searchCtx *ctx, char *arg, int type) {
b697c21c
CP
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
f33f3f52 1204 var = var_tochar(ctx, arg, &variable);
b697c21c
CP
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
f33f3f52 1228searchNode *var_get(searchCtx *ctx, char *arg) {
b697c21c
CP
1229 searchNode *variable, *found = NULL;
1230 int i;
f33f3f52 1231 char *var = var_tochar(ctx, arg, &variable);
b697c21c
CP
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
1248void var_setstr(struct searchVariable *v, char *data) {
1249 v->cdata.u.stringbuf = data;
1250}
9b276ca6 1251
53eff0a9 1252#ifdef NEWSEARCH_NEWPARSER
9b276ca6
CP
1253void 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}
53eff0a9 1271#endif
31686847
CP
1272
1273struct 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}