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