]>
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" | |
ffc11f03 | 15 | #include "parser.h" |
87698d77 | 16 | |
70b0a4e5 | 17 | MODULE_VERSION(""); |
c86edd1d | 18 | |
a92bb8e1 P |
19 | CommandTree *searchCmdTree; |
20 | searchList *globalterms = NULL; | |
c86edd1d Q |
21 | |
22 | int do_nicksearch(void *source, int cargc, char **cargv); | |
56a84a81 | 23 | int do_chansearch(void *source, int cargc, char **cargv); |
e8ad630b | 24 | int do_usersearch(void *source, int cargc, char **cargv); |
d00af236 | 25 | |
3d2bf13b CP |
26 | void printnick_channels(searchCtx *, nick *, nick *); |
27 | void printchannel(searchCtx *, nick *, chanindex *); | |
28 | void printchannel_topic(searchCtx *, nick *, chanindex *); | |
29 | void printchannel_services(searchCtx *, nick *, chanindex *); | |
c86edd1d | 30 | |
55dd7529 CP |
31 | UserDisplayFunc defaultuserfn = printuser; |
32 | NickDisplayFunc defaultnickfn = printnick; | |
33 | ChanDisplayFunc defaultchanfn = printchannel; | |
34 | ||
a92bb8e1 | 35 | searchCmd *reg_nicksearch, *reg_chansearch, *reg_usersearch; |
e8ff7b61 | 36 | void displaycommandhelp(nick *, Command *); |
9b276ca6 | 37 | void displaystrerror(replyFunc, nick *, const char *); |
c86edd1d | 38 | |
a92bb8e1 P |
39 | searchCmd *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 |
64 | void 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 |
73 | void displaycommandhelp(nick *np, Command *cmd) { |
74 | int i,n,j,m; | |
75 | Command *cmdlist[100], *acmdlist[100]; | |
76 | searchCmd *acmd; | |
77 | ||
78 | n=getcommandlist(searchCmdTree,cmdlist,100); | |
79 | for(i=0;i<n;i++) { | |
80 | /* note: we may want to only deregister a command if we've already registered it, for now, try de-registering new commands anyway */ | |
81 | if ( ((searchCmd *)cmdlist[i]->handler)->handler != cmd->handler ) | |
82 | continue; | |
83 | acmd = ((searchCmd *)(cmdlist[i]->handler)); | |
84 | ||
85 | controlreply(np, "Usage: [flags] <criteria>\n"); | |
86 | controlreply(np, "Flags:\n"); | |
87 | controlreply(np, " -l int : Limit number of rows of results\n"); | |
88 | controlreply(np, " -d string : a valid output format for the results\n"); | |
89 | controlreply(np, " -s subset : ipmask subset of network to search (only for node search)\n"); | |
90 | controlreply(np, " \n"); | |
91 | controlreply(np, "Available Output Formats:\n"); | |
92 | ||
93 | m=getcommandlist(acmd->outputtree,acmdlist,100); | |
94 | for(j=0;j<m;j++) { | |
95 | if ( controlpermitted( acmdlist[j]->level, np) ) { | |
96 | char *help=(char *)acmdlist[j]->ext; | |
97 | if ( help && help[0] != '\0') | |
98 | controlreply(np, "%-10s: %s\n", acmdlist[j]->command->content, help); | |
99 | else | |
100 | controlreply(np, "%s\n", acmdlist[j]->command->content ); | |
101 | } | |
102 | } | |
103 | ||
104 | controlreply(np, " \n"); | |
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 | ||
133 | void regdisp( searchCmd *cmd, const char *name, void *handler, int level, char *help) { | |
134 | addcommandexttotree(cmd->outputtree, name, level, 0, (CommandHandler) handler, (char *)help); | |
a92bb8e1 P |
135 | } |
136 | ||
137 | void unregdisp( searchCmd *cmd, const char *name, void *handler ) { | |
138 | deletecommandfromtree(cmd->outputtree, name, (CommandHandler) handler); | |
e8ad630b CP |
139 | } |
140 | ||
a92bb8e1 | 141 | void *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 | 151 | const 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 | 154 | nick *senderNSExtern; |
c86edd1d Q |
155 | |
156 | void _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 | ||
251 | void _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 | 278 | void 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 | ||
317 | void 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 |
343 | void registersearchterm(searchCmd *cmd, char *term, parseFunc parsefunc, int level, char *help) { |
344 | /* NOTE: global terms are added to the tree elsewhere as we set maxparams to 1 to indicate global */ | |
345 | addcommandexttotree(cmd->searchtree, term, level, 0, (CommandHandler) parsefunc, help); | |
c86edd1d Q |
346 | } |
347 | ||
a92bb8e1 | 348 | void 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 | 353 | static void controlwallwrapper(int level, char *format, ...) __attribute__ ((format (printf, 2, 3))); |
0da2a4ae CP |
354 | static void controlwallwrapper(int level, char *format, ...) { |
355 | char buf[1024]; | |
356 | va_list ap; | |
357 | ||
358 | va_start(ap, format); | |
359 | vsnprintf(buf, sizeof(buf), format, ap); | |
360 | controlwall(NO_OPER, level, "%s", buf); | |
361 | va_end(ap); | |
362 | } | |
363 | ||
6cb1b086 | 364 | int 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': | |
402 | if (cargc<*arg) { | |
e8ff7b61 P |
403 | reply(sender,"Error: -s switch requires an argument (for help, see help <searchcmd>)"); |
404 | return CMD_ERROR; | |
a92bb8e1 P |
405 | } |
406 | if (ipmask_parse(cargv[*arg], &sin, &bits) == 0) { | |
e8ff7b61 P |
407 | reply(sender, "Error: Invalid CIDR mask supplied (for help, see help <searchcmd>)"); |
408 | return CMD_ERROR; | |
a92bb8e1 P |
409 | } |
410 | *subset = (void *)refnode(iptree, &sin, bits); | |
411 | (*arg)++; | |
412 | break; | |
73cfc302 | 413 | |
c86edd1d | 414 | default: |
e8ff7b61 P |
415 | reply(sender,"Unrecognised flag -%c. (for help, see help <searchcmd>)",*ch); |
416 | return CMD_ERROR; | |
c86edd1d Q |
417 | } |
418 | } | |
419 | } | |
420 | ||
e8ad630b CP |
421 | return CMD_OK; |
422 | } | |
423 | ||
6b2202d0 | 424 | void newsearch_ctxinit(searchCtx *ctx, searchParseFunc searchfn, replyFunc replyfn, wallFunc wallfn, void *arg, searchCmd *cmd, nick *np, void *displayfn, int limit) { |
b697c21c CP |
425 | memset(ctx, 0, sizeof(searchCtx)); |
426 | ||
427 | ctx->reply = replyfn; | |
428 | ctx->wall = wallfn; | |
429 | ctx->parser = searchfn; | |
430 | ctx->arg = arg; | |
a92bb8e1 | 431 | ctx->searchcmd = cmd; |
e8ff7b61 | 432 | ctx->sender = np; |
6b2202d0 CP |
433 | ctx->limit = limit; |
434 | ctx->displayfn = displayfn; | |
b697c21c CP |
435 | } |
436 | ||
e8ad630b | 437 | int do_nicksearch_real(replyFunc reply, wallFunc wall, void *source, int cargc, char **cargv) { |
b697c21c | 438 | nick *sender = source; |
e8ad630b CP |
439 | int limit=500; |
440 | int arg=0; | |
55dd7529 | 441 | NickDisplayFunc display=defaultnickfn; |
e8ad630b | 442 | int ret; |
ffc11f03 CP |
443 | #ifndef NEWSEARCH_NEWPARSER |
444 | searchCtx ctx; | |
445 | struct searchNode *search; | |
446 | #else | |
447 | parsertree *tree; | |
448 | #endif | |
e8ad630b | 449 | |
e8ff7b61 P |
450 | if (cargc<1) { |
451 | reply( sender, "Usage: [flags] <criteria>"); | |
452 | reply( sender, "For help, see help nicksearch"); | |
453 | return CMD_OK; | |
454 | } | |
455 | ||
6cb1b086 | 456 | ret = parseopts(cargc, cargv, &arg, &limit, NULL, (void *)&display, reg_nicksearch->outputtree, reply, sender); |
e8ad630b CP |
457 | if(ret != CMD_OK) |
458 | return ret; | |
459 | ||
c86edd1d | 460 | if (arg>=cargc) { |
4156103f | 461 | reply(sender,"No search terms - aborting."); |
c86edd1d Q |
462 | return CMD_ERROR; |
463 | } | |
464 | ||
465 | if (arg<(cargc-1)) { | |
466 | rejoinline(cargv[arg],cargc-arg); | |
467 | } | |
c8be5183 | 468 | |
ffc11f03 | 469 | #ifndef NEWSEARCH_NEWPARSER |
6b2202d0 | 470 | newsearch_ctxinit(&ctx, search_parse, reply, wall, NULL, reg_nicksearch, sender, display, limit); |
f33f3f52 | 471 | if (!(search = ctx.parser(&ctx, cargv[arg]))) { |
0da2a4ae | 472 | reply(sender,"Parse error: %s",parseError); |
c86edd1d Q |
473 | return CMD_ERROR; |
474 | } | |
c2c414be | 475 | |
6b2202d0 | 476 | nicksearch_exe(search, &ctx); |
c8be5183 CP |
477 | |
478 | (search->free)(&ctx, search); | |
ffc11f03 CP |
479 | #else |
480 | tree = parse_string(reg_nicksearch, cargv[arg]); | |
481 | if(!tree) { | |
9b276ca6 | 482 | displaystrerror(reply, sender, cargv[arg]); |
ffc11f03 CP |
483 | return CMD_ERROR; |
484 | } | |
485 | ||
486 | ast_nicksearch(tree->root, reply, sender, wall, display, NULL, NULL, limit); | |
487 | ||
488 | parse_free(tree); | |
489 | #endif | |
c2c414be CP |
490 | |
491 | return CMD_OK; | |
492 | } | |
493 | ||
0da2a4ae CP |
494 | int do_nicksearch(void *source, int cargc, char **cargv) { |
495 | return do_nicksearch_real(controlreply, controlwallwrapper, source, cargc, cargv); | |
496 | } | |
497 | ||
6b2202d0 | 498 | void nicksearch_exe(struct searchNode *search, searchCtx *ctx) { |
c2c414be CP |
499 | int i, j; |
500 | int matches = 0; | |
501 | unsigned int cmarker; | |
502 | unsigned int tchans=0,uchans=0; | |
503 | struct channel **cs; | |
6b2202d0 | 504 | nick *np, *sender = ctx->sender; |
b697c21c | 505 | senderNSExtern = sender; |
6b2202d0 CP |
506 | NickDisplayFunc display = ctx->displayfn; |
507 | int limit = ctx->limit; | |
508 | ||
7c02e111 | 509 | /* Get a marker value to mark "seen" channels for unique count */ |
510 | cmarker=nextchanmarker(); | |
511 | ||
c7f7a584 | 512 | /* The top-level node needs to return a BOOL */ |
c8be5183 | 513 | search=coerceNode(ctx, search, RETURNTYPE_BOOL); |
c7f7a584 | 514 | |
c86edd1d Q |
515 | for (i=0;i<NICKHASHSIZE;i++) { |
516 | for (np=nicktable[i];np;np=np->next) { | |
c8be5183 | 517 | if ((search->exe)(ctx, search, np)) { |
7c02e111 | 518 | /* Add total channels */ |
519 | tchans += np->channels->cursi; | |
520 | ||
521 | /* Check channels for uniqueness */ | |
522 | cs=(channel **)np->channels->content; | |
523 | for (j=0;j<np->channels->cursi;j++) { | |
524 | if (cs[j]->index->marker != cmarker) { | |
525 | cs[j]->index->marker=cmarker; | |
526 | uchans++; | |
527 | } | |
528 | } | |
529 | ||
c86edd1d | 530 | if (matches<limit) |
3d2bf13b | 531 | display(ctx, sender, np); |
7c02e111 | 532 | |
c86edd1d | 533 | if (matches==limit) |
c8be5183 | 534 | ctx->reply(sender, "--- More than %d matches, skipping the rest",limit); |
c86edd1d Q |
535 | matches++; |
536 | } | |
537 | } | |
538 | } | |
539 | ||
c8be5183 | 540 | ctx->reply(sender,"--- End of list: %d matches; users were on %u channels (%u unique, %.1f average clones)", |
7c02e111 | 541 | matches, tchans, uchans, (float)tchans/uchans); |
c86edd1d Q |
542 | } |
543 | ||
0da2a4ae | 544 | int do_chansearch_real(replyFunc reply, wallFunc wall, void *source, int cargc, char **cargv) { |
b697c21c | 545 | nick *sender = source; |
c2c414be | 546 | int limit=500; |
56a84a81 | 547 | int arg=0; |
55dd7529 | 548 | ChanDisplayFunc display=defaultchanfn; |
e8ad630b | 549 | int ret; |
ffc11f03 CP |
550 | #ifndef NEWSEARCH_NEWPARSER |
551 | struct searchNode *search; | |
552 | searchCtx ctx; | |
553 | #else | |
554 | parsertree *tree; | |
555 | #endif | |
56a84a81 | 556 | |
e8ff7b61 P |
557 | if (cargc<1) { |
558 | reply( sender, "Usage: [flags] <criteria>"); | |
559 | reply( sender, "For help, see help chansearch"); | |
560 | return CMD_OK; | |
561 | } | |
56a84a81 | 562 | |
6cb1b086 | 563 | ret = parseopts(cargc, cargv, &arg, &limit, NULL, (void *)&display, reg_chansearch->outputtree, reply, sender); |
e8ad630b CP |
564 | if(ret != CMD_OK) |
565 | return ret; | |
56a84a81 IB |
566 | |
567 | if (arg>=cargc) { | |
4156103f | 568 | reply(sender,"No search terms - aborting."); |
56a84a81 IB |
569 | return CMD_ERROR; |
570 | } | |
571 | ||
572 | if (arg<(cargc-1)) { | |
573 | rejoinline(cargv[arg],cargc-arg); | |
574 | } | |
c2c414be | 575 | |
ffc11f03 | 576 | #ifndef NEWSEARCH_NEWPARSER |
6b2202d0 | 577 | newsearch_ctxinit(&ctx, search_parse, reply, wall, NULL, reg_chansearch, sender, display, limit); |
f33f3f52 | 578 | if (!(search = ctx.parser(&ctx, cargv[arg]))) { |
0da2a4ae | 579 | reply(sender,"Parse error: %s",parseError); |
56a84a81 IB |
580 | return CMD_ERROR; |
581 | } | |
c7f7a584 | 582 | |
6b2202d0 | 583 | chansearch_exe(search, &ctx); |
c2c414be | 584 | |
c8be5183 | 585 | (search->free)(&ctx, search); |
ffc11f03 CP |
586 | #else |
587 | tree = parse_string(reg_chansearch, cargv[arg]); | |
588 | if(!tree) { | |
9b276ca6 | 589 | displaystrerror(reply, sender, cargv[arg]); |
ffc11f03 CP |
590 | return CMD_ERROR; |
591 | } | |
592 | ||
593 | ast_chansearch(tree->root, reply, sender, wall, display, NULL, NULL, limit); | |
594 | ||
595 | parse_free(tree); | |
596 | #endif | |
c2c414be CP |
597 | |
598 | return CMD_OK; | |
599 | } | |
600 | ||
0da2a4ae CP |
601 | int do_chansearch(void *source, int cargc, char **cargv) { |
602 | return do_chansearch_real(controlreply, controlwallwrapper, source, cargc, cargv); | |
603 | } | |
604 | ||
6b2202d0 | 605 | void chansearch_exe(struct searchNode *search, searchCtx *ctx) { |
c2c414be CP |
606 | int i; |
607 | chanindex *cip; | |
608 | int matches = 0; | |
6b2202d0 | 609 | nick *sender = ctx->sender; |
b697c21c | 610 | senderNSExtern = sender; |
6b2202d0 CP |
611 | ChanDisplayFunc display = ctx->displayfn; |
612 | int limit = ctx->limit; | |
c2c414be | 613 | |
c8be5183 | 614 | search=coerceNode(ctx, search, RETURNTYPE_BOOL); |
56a84a81 IB |
615 | |
616 | for (i=0;i<CHANNELHASHSIZE;i++) { | |
617 | for (cip=chantable[i];cip;cip=cip->next) { | |
c8be5183 | 618 | if ((search->exe)(ctx, search, cip)) { |
56a84a81 | 619 | if (matches<limit) |
3d2bf13b | 620 | display(ctx, sender, cip); |
56a84a81 | 621 | if (matches==limit) |
c8be5183 | 622 | ctx->reply(sender, "--- More than %d matches, skipping the rest",limit); |
56a84a81 IB |
623 | matches++; |
624 | } | |
625 | } | |
626 | } | |
627 | ||
c8be5183 | 628 | ctx->reply(sender,"--- End of list: %d matches", matches); |
56a84a81 IB |
629 | } |
630 | ||
e8ad630b | 631 | int do_usersearch_real(replyFunc reply, wallFunc wall, void *source, int cargc, char **cargv) { |
b697c21c | 632 | nick *sender = source; |
e8ad630b CP |
633 | int limit=500; |
634 | int arg=0; | |
55dd7529 | 635 | UserDisplayFunc display=defaultuserfn; |
e8ad630b | 636 | int ret; |
ffc11f03 CP |
637 | #ifndef NEWSEARCH_NEWPARSER |
638 | struct searchNode *search; | |
639 | searchCtx ctx; | |
640 | #else | |
641 | parsertree *tree; | |
642 | #endif | |
e8ad630b | 643 | |
e8ff7b61 P |
644 | if (cargc<1) { |
645 | reply( sender, "Usage: [flags] <criteria>"); | |
646 | reply( sender, "For help, see help usersearch"); | |
647 | return CMD_OK; | |
648 | } | |
649 | ||
6cb1b086 | 650 | ret = parseopts(cargc, cargv, &arg, &limit, NULL, (void *)&display, reg_usersearch->outputtree, reply, sender); |
e8ad630b CP |
651 | if(ret != CMD_OK) |
652 | return ret; | |
653 | ||
654 | if (arg>=cargc) { | |
4156103f | 655 | reply(sender,"No search terms - aborting."); |
e8ad630b CP |
656 | return CMD_ERROR; |
657 | } | |
658 | ||
659 | if (arg<(cargc-1)) { | |
660 | rejoinline(cargv[arg],cargc-arg); | |
661 | } | |
662 | ||
ffc11f03 | 663 | #ifndef NEWSEARCH_NEWPARSER |
6b2202d0 | 664 | newsearch_ctxinit(&ctx, search_parse, reply, wall, NULL, reg_usersearch, sender, display, limit); |
e8ff7b61 | 665 | |
f33f3f52 | 666 | if (!(search = ctx.parser(&ctx, cargv[arg]))) { |
e8ad630b CP |
667 | reply(sender,"Parse error: %s",parseError); |
668 | return CMD_ERROR; | |
669 | } | |
670 | ||
6b2202d0 | 671 | usersearch_exe(search, &ctx); |
e8ad630b CP |
672 | |
673 | (search->free)(&ctx, search); | |
ffc11f03 CP |
674 | #else |
675 | tree = parse_string(reg_usersearch, cargv[arg]); | |
676 | if(!tree) { | |
9b276ca6 | 677 | displaystrerror(reply, sender, cargv[arg]); |
ffc11f03 CP |
678 | return CMD_ERROR; |
679 | } | |
680 | ||
681 | ast_usersearch(tree->root, reply, sender, wall, display, NULL, NULL, limit); | |
682 | ||
683 | parse_free(tree); | |
684 | #endif | |
e8ad630b CP |
685 | |
686 | return CMD_OK; | |
687 | } | |
688 | ||
689 | int do_usersearch(void *source, int cargc, char **cargv) { | |
690 | return do_usersearch_real(controlreply, controlwallwrapper, source, cargc, cargv); | |
691 | } | |
692 | ||
6b2202d0 | 693 | void usersearch_exe(struct searchNode *search, searchCtx *ctx) { |
e8ad630b CP |
694 | int i; |
695 | authname *aup; | |
696 | int matches = 0; | |
6b2202d0 CP |
697 | nick *sender = ctx->sender; |
698 | int limit = ctx->limit; | |
699 | UserDisplayFunc display = ctx->displayfn; | |
b697c21c | 700 | senderNSExtern = sender; |
6b2202d0 | 701 | |
e8ad630b CP |
702 | search=coerceNode(ctx, search, RETURNTYPE_BOOL); |
703 | ||
704 | for (i=0;i<AUTHNAMEHASHSIZE;i++) { | |
705 | for (aup=authnametable[i];aup;aup=aup->next) { | |
706 | if ((search->exe)(ctx, search, aup)) { | |
707 | if (matches<limit) | |
708 | display(ctx, sender, aup); | |
709 | if (matches==limit) | |
710 | ctx->reply(sender, "--- More than %d matches, skipping the rest",limit); | |
711 | matches++; | |
712 | } | |
713 | } | |
714 | } | |
715 | ||
716 | ctx->reply(sender,"--- End of list: %d matches", matches); | |
717 | } | |
718 | ||
c7f7a584 | 719 | /* Free a coerce node */ |
c8be5183 | 720 | void free_coerce(searchCtx *ctx, struct searchNode *thenode) { |
c7f7a584 | 721 | struct coercedata *cd=thenode->localdata; |
722 | ||
c8be5183 | 723 | cd->child->free(ctx, cd->child); |
c7f7a584 | 724 | free(thenode->localdata); |
725 | free(thenode); | |
726 | } | |
c86edd1d | 727 | |
c7f7a584 | 728 | /* Free a coerce node with a stringbuf allocated */ |
c8be5183 | 729 | void free_coercestring(searchCtx *ctx, struct searchNode *thenode) { |
c7f7a584 | 730 | free(((struct coercedata *)thenode->localdata)->u.stringbuf); |
c8be5183 | 731 | free_coerce(ctx, thenode); |
c7f7a584 | 732 | } |
733 | ||
734 | /* exe_tostr_null: return the constant string */ | |
c8be5183 | 735 | void *exe_tostr_null(searchCtx *ctx, struct searchNode *thenode, void *theinput) { |
c7f7a584 | 736 | struct coercedata *cd=thenode->localdata; |
737 | ||
738 | return cd->u.stringbuf; | |
739 | } | |
740 | ||
741 | /* exe_val_null: return the constant value */ | |
c8be5183 | 742 | void *exe_val_null(searchCtx *ctx, struct searchNode *thenode, void *theinput) { |
c7f7a584 | 743 | struct coercedata *cd=thenode->localdata; |
744 | ||
745 | return (void *)cd->u.val; | |
746 | } | |
747 | ||
748 | /* Lots of very dull type conversion functions */ | |
c8be5183 | 749 | void *exe_inttostr(searchCtx *ctx, struct searchNode *thenode, void *theinput) { |
c7f7a584 | 750 | struct coercedata *cd=thenode->localdata; |
751 | ||
c8be5183 | 752 | sprintf(cd->u.stringbuf, "%lu", (unsigned long)(cd->child->exe)(ctx, cd->child, theinput)); |
c7f7a584 | 753 | |
754 | return cd->u.stringbuf; | |
755 | } | |
756 | ||
c8be5183 | 757 | void *exe_booltostr(searchCtx *ctx, struct searchNode *thenode, void *theinput) { |
c7f7a584 | 758 | struct coercedata *cd=thenode->localdata; |
759 | ||
c8be5183 | 760 | if ((cd->child->exe)(ctx, cd->child, theinput)) { |
c7f7a584 | 761 | sprintf(cd->u.stringbuf,"1"); |
762 | } else { | |
763 | cd->u.stringbuf[0]='\0'; | |
764 | } | |
765 | ||
766 | return cd->u.stringbuf; | |
767 | } | |
c86edd1d | 768 | |
c8be5183 | 769 | void *exe_strtoint(searchCtx *ctx, struct searchNode *thenode, void *theinput) { |
c7f7a584 | 770 | struct coercedata *cd=thenode->localdata; |
771 | ||
c8be5183 | 772 | return (void *)strtoul((cd->child->exe)(ctx,cd->child,theinput),NULL,10); |
c7f7a584 | 773 | } |
774 | ||
c8be5183 | 775 | void *exe_booltoint(searchCtx *ctx, struct searchNode *thenode, void *theinput) { |
c7f7a584 | 776 | struct coercedata *cd=thenode->localdata; |
777 | ||
778 | /* Don't need to do anything */ | |
c8be5183 | 779 | return (cd->child->exe)(ctx, cd->child, theinput); |
c7f7a584 | 780 | } |
781 | ||
c8be5183 | 782 | void *exe_strtobool(searchCtx *ctx, struct searchNode *thenode, void *theinput) { |
c7f7a584 | 783 | struct coercedata *cd=thenode->localdata; |
c8be5183 | 784 | char *ch=(cd->child->exe)(ctx, cd->child, theinput); |
c7f7a584 | 785 | |
786 | if (!ch || *ch=='\0' || (*ch=='0' && ch[1]=='\0')) { | |
787 | return (void *)0; | |
788 | } else { | |
789 | return (void *)1; | |
790 | } | |
791 | } | |
792 | ||
c8be5183 | 793 | void *exe_inttobool(searchCtx *ctx, struct searchNode *thenode, void *theinput) { |
c7f7a584 | 794 | struct coercedata *cd=thenode->localdata; |
795 | ||
c8be5183 | 796 | if ((cd->child->exe)(ctx, cd->child, theinput)) { |
c7f7a584 | 797 | return (void *)1; |
798 | } else { | |
799 | return (void *)0; | |
800 | } | |
801 | } | |
802 | ||
c8be5183 | 803 | struct searchNode *coerceNode(searchCtx *ctx, struct searchNode *thenode, int type) { |
c7f7a584 | 804 | struct searchNode *anode; |
805 | struct coercedata *cd; | |
806 | ||
807 | /* You can't coerce a NULL */ | |
808 | if (!thenode) | |
809 | return NULL; | |
810 | ||
811 | /* No effort required to coerce to the same type */ | |
812 | if (type==(thenode->returntype & RETURNTYPE_TYPE)) | |
813 | return thenode; | |
814 | ||
815 | anode=(struct searchNode *)malloc(sizeof(struct searchNode)); | |
816 | anode->localdata=cd=(struct coercedata *)malloc(sizeof(struct coercedata)); | |
817 | cd->child=thenode; | |
818 | anode->returntype=type; /* We'll return what they want, always */ | |
819 | anode->free=free_coerce; | |
820 | ||
821 | switch(type) { | |
822 | case RETURNTYPE_STRING: | |
823 | /* For a string we'll need a buffer */ | |
824 | /* A 64-bit number prints out to 20 digits, this leaves some slack */ | |
825 | cd->u.stringbuf=malloc(25); | |
826 | anode->free=free_coercestring; | |
827 | ||
828 | switch(thenode->returntype & RETURNTYPE_TYPE) { | |
829 | default: | |
830 | case RETURNTYPE_INT: | |
831 | if (thenode->returntype & RETURNTYPE_CONST) { | |
832 | /* Constant node: sort it out now */ | |
c8be5183 | 833 | sprintf(cd->u.stringbuf, "%lu", (unsigned long)thenode->exe(ctx, thenode, NULL)); |
c7f7a584 | 834 | anode->exe=exe_tostr_null; |
835 | anode->returntype |= RETURNTYPE_CONST; | |
836 | } else { | |
837 | /* Variable data */ | |
838 | anode->exe=exe_inttostr; | |
839 | } | |
840 | break; | |
841 | ||
842 | case RETURNTYPE_BOOL: | |
843 | if (thenode->returntype & RETURNTYPE_CONST) { | |
844 | /* Constant bool value */ | |
c8be5183 | 845 | if (thenode->exe(ctx, thenode,NULL)) { |
c7f7a584 | 846 | /* True! */ |
847 | sprintf(cd->u.stringbuf, "1"); | |
848 | } else { | |
849 | cd->u.stringbuf[0] = '\0'; | |
850 | } | |
851 | anode->exe=exe_tostr_null; | |
852 | anode->returntype |= RETURNTYPE_CONST; | |
853 | } else { | |
854 | /* Variable bool value */ | |
855 | anode->exe=exe_booltostr; | |
856 | } | |
857 | break; | |
858 | } | |
859 | break; | |
860 | ||
861 | case RETURNTYPE_INT: | |
862 | /* we want an int */ | |
863 | switch (thenode->returntype & RETURNTYPE_TYPE) { | |
864 | case RETURNTYPE_STRING: | |
865 | if (thenode->returntype & RETURNTYPE_CONST) { | |
c8be5183 | 866 | cd->u.val=strtoul((thenode->exe)(ctx, thenode, NULL), NULL, 10); |
c7f7a584 | 867 | anode->exe=exe_val_null; |
868 | anode->returntype |= RETURNTYPE_CONST; | |
869 | } else { | |
870 | anode->exe=exe_strtoint; | |
871 | } | |
872 | break; | |
873 | ||
874 | default: | |
875 | case RETURNTYPE_BOOL: | |
876 | if (thenode->returntype & RETURNTYPE_CONST) { | |
c8be5183 | 877 | if ((thenode->exe)(ctx, thenode,NULL)) |
c7f7a584 | 878 | cd->u.val=1; |
879 | else | |
880 | cd->u.val=0; | |
881 | ||
882 | anode->exe=exe_val_null; | |
883 | anode->returntype |= RETURNTYPE_CONST; | |
884 | } else { | |
885 | anode->exe=exe_booltoint; | |
886 | } | |
887 | break; | |
888 | } | |
889 | break; | |
890 | ||
891 | default: | |
892 | case RETURNTYPE_BOOL: | |
893 | /* we want a bool */ | |
894 | switch (thenode->returntype & RETURNTYPE_TYPE) { | |
895 | case RETURNTYPE_STRING: | |
896 | if (thenode->returntype & RETURNTYPE_CONST) { | |
c8be5183 | 897 | char *rv=(char *)((thenode->exe)(ctx, thenode, NULL)); |
c7f7a584 | 898 | if (!rv || *rv=='\0' || (*rv=='0' && rv[1]=='\0')) |
899 | cd->u.val=0; | |
900 | else | |
901 | cd->u.val=1; | |
902 | ||
903 | anode->exe=exe_val_null; | |
904 | anode->returntype |= RETURNTYPE_CONST; | |
905 | } else { | |
906 | anode->exe=exe_strtobool; | |
907 | } | |
908 | break; | |
909 | ||
910 | default: | |
911 | case RETURNTYPE_INT: | |
912 | if (thenode->returntype & RETURNTYPE_CONST) { | |
c8be5183 | 913 | if ((thenode->exe)(ctx, thenode,NULL)) |
c7f7a584 | 914 | cd->u.val=1; |
915 | else | |
916 | cd->u.val=0; | |
917 | ||
918 | anode->exe=exe_val_null; | |
919 | anode->returntype |= RETURNTYPE_CONST; | |
920 | } else { | |
921 | anode->exe=exe_inttobool; | |
922 | } | |
923 | break; | |
924 | } | |
925 | break; | |
926 | } | |
927 | ||
928 | return anode; | |
929 | } | |
930 | ||
931 | /* Literals always return constant strings... */ | |
c8be5183 | 932 | void *literal_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) { |
2e2a8c6a P |
933 | if (thenode->localdata) |
934 | return ((sstring *)thenode->localdata)->content; | |
935 | else | |
936 | return ""; | |
c7f7a584 | 937 | } |
938 | ||
c8be5183 | 939 | void literal_free(searchCtx *ctx, struct searchNode *thenode) { |
c7f7a584 | 940 | freesstring(thenode->localdata); |
941 | free(thenode); | |
942 | } | |
c86edd1d | 943 | |
ffe23bc1 CP |
944 | static int unescape(char *input, char *output, size_t buflen) { |
945 | char *ch, *ch2; | |
946 | int e=0; | |
947 | ||
948 | if (*input=='\"') { | |
949 | for (ch=input;*ch;ch++) { | |
950 | if(ch - input >= buflen) { | |
951 | parseError="Buffer overflow"; | |
952 | return 0; | |
953 | } | |
954 | } | |
955 | ||
956 | if (*(ch-1) != '\"') { | |
957 | parseError="Quote mismatch"; | |
958 | return 0; | |
959 | } | |
960 | ||
961 | *(ch-1)='\0'; | |
962 | input++; | |
963 | } | |
964 | ||
965 | ch2=output; | |
966 | for (ch=input;*ch;ch++) { | |
967 | if(ch - input >= buflen) { | |
968 | parseError="Buffer overflow"; | |
969 | return 0; | |
970 | } | |
971 | ||
972 | if (e) { | |
973 | e=0; | |
974 | *ch2++=*ch; | |
975 | } else if (*ch=='\\') { | |
976 | e=1; | |
977 | } else { | |
978 | *ch2++=*ch; | |
979 | } | |
980 | } | |
981 | *ch2='\0'; | |
982 | ||
983 | return 1; | |
984 | } | |
985 | ||
986 | struct searchNode *search_parse(searchCtx *ctx, char *cinput) { | |
c86edd1d Q |
987 | /* OK, we need to split the input into chunks on spaces and brackets.. */ |
988 | char *argvector[100]; | |
ffe23bc1 CP |
989 | char inputb[1024]; |
990 | char *input; | |
1bf9cda6 | 991 | char thestring[500]; |
e88c37d8 | 992 | int i,j,q=0,e=0; |
ffe23bc1 | 993 | char *ch; |
c86edd1d Q |
994 | struct Command *cmd; |
995 | struct searchNode *thenode; | |
c86edd1d | 996 | |
ffe23bc1 CP |
997 | strlcpy(inputb, cinput, sizeof(inputb)); |
998 | input = inputb; | |
999 | ||
c86edd1d Q |
1000 | /* If it starts with a bracket, it's a function call.. */ |
1001 | if (*input=='(') { | |
1002 | /* Skip past string */ | |
1003 | for (ch=input;*ch;ch++); | |
1004 | if (*(ch-1) != ')') { | |
1005 | parseError = "Bracket mismatch!"; | |
1006 | return NULL; | |
1007 | } | |
1008 | input++; | |
1009 | *(ch-1)='\0'; | |
1010 | ||
1011 | /* Split further args */ | |
1012 | i=-1; /* i = -1 BoW, 0 = inword, 1 = bracket nest depth */ | |
1013 | j=0; /* j = current arg */ | |
1bf9cda6 | 1014 | e=0; |
1015 | q=0; | |
a33e0d2b | 1016 | argvector[0]=""; |
c86edd1d | 1017 | for (ch=input;*ch;ch++) { |
25ee628a | 1018 | /*printf("i: %d j: %d e: %d q: %d ch: '%c'\n", i, j, e, q, *ch);*/ |
c86edd1d | 1019 | if (i==-1) { |
1bf9cda6 | 1020 | argvector[j]=ch; |
1021 | if (*ch=='(') { | |
1022 | i=1; | |
1023 | } else if (*ch != ' ') { | |
1024 | i=0; | |
1025 | if (*ch=='\\') { | |
1026 | e=1; | |
1027 | } else if (*ch=='\"') { | |
1028 | q=1; | |
1029 | } | |
1030 | } | |
1031 | } else if (e==1) { | |
1032 | e=0; | |
1033 | } else if (q==1) { | |
25ee628a CP |
1034 | if (*ch=='\\') { |
1035 | e=1; | |
1036 | } else if (*ch=='\"') { | |
1037 | q=0; | |
1038 | } | |
c86edd1d | 1039 | } else if (i==0) { |
1bf9cda6 | 1040 | if (*ch=='\\') { |
1041 | e=1; | |
1042 | } else if (*ch=='\"') { | |
1043 | q=1; | |
1044 | } else if (*ch==' ') { | |
1045 | *ch='\0'; | |
1046 | j++; | |
4cd6347c P |
1047 | if(j >= (sizeof(argvector) / sizeof(*argvector))) { |
1048 | parseError = "Too many arguments"; | |
1049 | return NULL; | |
1050 | } | |
1bf9cda6 | 1051 | i=-1; |
1052 | } | |
c86edd1d | 1053 | } else { |
1bf9cda6 | 1054 | if (*ch=='\\') { |
1055 | e=1; | |
1056 | } else if (*ch=='\"') { | |
1057 | q=1; | |
1058 | } else if (*ch=='(') { | |
1059 | i++; | |
1060 | } else if (*ch==')') { | |
1061 | i--; | |
1062 | } | |
c86edd1d Q |
1063 | } |
1064 | } | |
1065 | ||
1066 | if (i>0) { | |
1067 | parseError = "Bracket mismatch!"; | |
1068 | return NULL; | |
1069 | } | |
4cd6347c P |
1070 | |
1071 | if (*(ch-1) == 0) /* if the last character was a space */ | |
1072 | j--; /* remove an argument */ | |
c86edd1d | 1073 | |
f58cb0de | 1074 | /* for(k=1;k<=j;k++) |
ffe23bc1 CP |
1075 | if(!unescape(argvector[k], argvector[k], sizeof(inputb))) |
1076 | return NULL; | |
f58cb0de | 1077 | */ |
25ee628a | 1078 | |
a92bb8e1 | 1079 | if (!(cmd=findcommandintree(ctx->searchcmd->searchtree,argvector[0],1))) { |
e8ff7b61 | 1080 | parseError = "Unknown command (for valid command list, see help <searchcmd>)"; |
c86edd1d Q |
1081 | return NULL; |
1082 | } else { | |
ffe23bc1 CP |
1083 | if (!controlpermitted(cmd->level, ctx->sender)) { |
1084 | parseError = "Access denied (for valid command list, see help <searchcmd>)"; | |
e8ff7b61 P |
1085 | return NULL; |
1086 | } | |
f33f3f52 | 1087 | return ((parseFunc)cmd->handler)(ctx, j, argvector+1); |
c86edd1d Q |
1088 | } |
1089 | } else { | |
1090 | /* Literal */ | |
1bf9cda6 | 1091 | |
ffe23bc1 | 1092 | /* slug: disabled now we unescape during the main parse stage */ |
ffe23bc1 CP |
1093 | if(!unescape(input, thestring, sizeof(thestring))) |
1094 | return NULL; | |
ffe23bc1 | 1095 | |
9ce4f0be IB |
1096 | if (!(thenode=(struct searchNode *)malloc(sizeof(struct searchNode)))) { |
1097 | parseError = "malloc: could not allocate memory for this search."; | |
1098 | return NULL; | |
1bf9cda6 | 1099 | } |
1100 | ||
2db45047 | 1101 | thenode->localdata = getsstring(thestring,512); |
c86edd1d Q |
1102 | thenode->returntype = RETURNTYPE_CONST | RETURNTYPE_STRING; |
1103 | thenode->exe = literal_exe; | |
1104 | thenode->free = literal_free; | |
1105 | ||
1106 | return thenode; | |
1107 | } | |
1108 | } | |
2ba836f2 | 1109 | |
2ba836f2 | 1110 | void nssnprintf(char *buf, size_t size, const char *format, nick *np) { |
ba8a65c4 | 1111 | StringBuf b; |
2ba836f2 CP |
1112 | const char *p; |
1113 | char *c; | |
1114 | char hostbuf[512]; | |
1115 | ||
1116 | if(size == 0) | |
1117 | return; | |
1118 | ||
1119 | b.buf = buf; | |
1120 | b.capacity = size; | |
1121 | b.len = 0; | |
1122 | ||
1123 | for(p=format;*p;p++) { | |
1124 | if(*p != '%') { | |
ba8a65c4 | 1125 | if(!sbaddchar(&b, *p)) |
2ba836f2 CP |
1126 | break; |
1127 | continue; | |
1128 | } | |
1129 | p++; | |
1130 | if(*p == '\0') | |
1131 | break; | |
1132 | if(*p == '%') { | |
ba8a65c4 | 1133 | if(!sbaddchar(&b, *p)) |
2ba836f2 CP |
1134 | break; |
1135 | continue; | |
1136 | } | |
1137 | ||
1138 | c = NULL; | |
1139 | switch(*p) { | |
1140 | case 'n': | |
1141 | c = np->nick; break; | |
1142 | case 'i': | |
1143 | c = np->ident; break; | |
1144 | case 'h': | |
1145 | c = np->host->name->content; break; | |
1146 | case 'I': | |
1147 | snprintf(hostbuf, sizeof(hostbuf), "%s", IPtostr(np->p_ipaddr)); | |
1148 | c = hostbuf; | |
1149 | break; | |
1150 | case 'u': | |
1151 | snprintf(hostbuf, sizeof(hostbuf), "%s!%s@%s", np->nick, np->ident, IPtostr(np->p_ipaddr)); | |
1152 | c = hostbuf; | |
1153 | break; | |
1154 | default: | |
1155 | c = "(bad format specifier)"; | |
1156 | } | |
1157 | if(c) | |
ba8a65c4 | 1158 | if(!sbaddstr(&b, c)) |
2ba836f2 CP |
1159 | break; |
1160 | } | |
1161 | ||
0be0b2d0 | 1162 | sbterminate(&b); |
2ba836f2 CP |
1163 | |
1164 | /* not required */ | |
1165 | /* | |
1166 | buf[size-1] = '\0'; | |
1167 | */ | |
1168 | } | |
1169 | ||
f33f3f52 P |
1170 | static char *var_tochar(searchCtx *ctx, char *arg, searchNode **variable) { |
1171 | *variable = ctx->parser(ctx, arg); | |
b697c21c CP |
1172 | if (!(*variable = coerceNode(ctx, *variable, RETURNTYPE_STRING))) |
1173 | return NULL; | |
1174 | ||
1175 | if(!((*variable)->returntype & RETURNTYPE_CONST)) { | |
1176 | parseError = "only constant variables allowed"; | |
1177 | ((*variable)->free)(ctx, *variable); | |
1178 | return NULL; | |
1179 | } | |
1180 | ||
1181 | return (char *)((*variable)->exe)(ctx, *variable, NULL); | |
1182 | } | |
1183 | ||
1184 | void free_val_null(searchCtx *ctx, struct searchNode *thenode) { | |
1185 | } | |
1186 | ||
f33f3f52 | 1187 | struct searchVariable *var_register(searchCtx *ctx, char *arg, int type) { |
b697c21c CP |
1188 | searchNode *variable; |
1189 | struct searchVariable *us; | |
1190 | char *var; | |
1191 | int i; | |
1192 | ||
1193 | if(ctx->lastvar >= MAX_VARIABLES) { | |
1194 | parseError = "Maximum number of variables reached"; | |
1195 | return NULL; | |
1196 | } | |
1197 | ||
1198 | us = &ctx->vars[ctx->lastvar]; | |
1199 | ||
f33f3f52 | 1200 | var = var_tochar(ctx, arg, &variable); |
b697c21c CP |
1201 | if(!var) |
1202 | return NULL; | |
1203 | ||
1204 | strlcpy(us->name, var, sizeof(us->name)); | |
1205 | (variable->free)(ctx, variable); | |
1206 | ||
1207 | for(i=0;i<ctx->lastvar;i++) { | |
1208 | if(!strcmp(us->name, ctx->vars[i].name)) { | |
1209 | parseError = "variable name already in use"; | |
1210 | return NULL; | |
1211 | } | |
1212 | } | |
1213 | ||
1214 | ctx->lastvar++; | |
1215 | us->data.returntype = type; | |
1216 | us->data.localdata = &us->cdata; | |
1217 | us->data.exe = exe_val_null; | |
1218 | us->data.free = free_val_null; | |
1219 | ||
1220 | us->cdata.child = NULL; | |
1221 | return us; | |
1222 | } | |
1223 | ||
f33f3f52 | 1224 | searchNode *var_get(searchCtx *ctx, char *arg) { |
b697c21c CP |
1225 | searchNode *variable, *found = NULL; |
1226 | int i; | |
f33f3f52 | 1227 | char *var = var_tochar(ctx, arg, &variable); |
b697c21c CP |
1228 | if(!var) |
1229 | return NULL; | |
1230 | ||
1231 | for(i=0;i<ctx->lastvar;i++) { | |
1232 | if(!strcmp(var, ctx->vars[i].name)) { | |
1233 | found = &ctx->vars[i].data; | |
1234 | break; | |
1235 | } | |
1236 | } | |
1237 | (variable->free)(ctx, variable); | |
1238 | ||
1239 | if(!found) | |
1240 | parseError = "variable not found"; | |
1241 | return found; | |
1242 | } | |
1243 | ||
1244 | void var_setstr(struct searchVariable *v, char *data) { | |
1245 | v->cdata.u.stringbuf = data; | |
1246 | } | |
9b276ca6 | 1247 | |
53eff0a9 | 1248 | #ifdef NEWSEARCH_NEWPARSER |
9b276ca6 CP |
1249 | void displaystrerror(replyFunc reply, nick *np, const char *input) { |
1250 | char buf[515]; | |
1251 | ||
1252 | if((parseStrErrorPos >= 0) && (parseStrErrorPos < sizeof(buf) - 3)) { | |
1253 | int i; | |
1254 | ||
1255 | for(i=0;i<parseStrErrorPos;i++) | |
1256 | buf[i] = ' '; | |
1257 | ||
1258 | buf[i++] = '^'; | |
1259 | buf[i] = '\0'; | |
1260 | ||
1261 | reply(np, "%s", input); | |
1262 | reply(np, "%s", buf); | |
1263 | } | |
1264 | ||
1265 | reply(np, "Parse error: %s", parseStrError); | |
1266 | } | |
53eff0a9 | 1267 | #endif |
31686847 CP |
1268 | |
1269 | struct searchNode *argtoconststr(char *command, searchCtx *ctx, char *arg, char **p) { | |
1270 | struct searchNode *c; | |
1271 | static char errorbuf[512]; | |
1272 | ||
1273 | c = ctx->parser(ctx, arg); | |
1274 | if (!(c = coerceNode(ctx, c, RETURNTYPE_STRING))) { | |
1275 | snprintf(errorbuf, sizeof(errorbuf), "%s: unable to coerce argument to string", command); | |
1276 | parseError = errorbuf; | |
1277 | return NULL; | |
1278 | } | |
1279 | ||
1280 | if (!(c->returntype & RETURNTYPE_CONST)) { | |
1281 | snprintf(errorbuf, sizeof(errorbuf), "%s: constant argument required", command); | |
1282 | parseError = errorbuf; | |
1283 | (c->free)(ctx, c); | |
1284 | return NULL; | |
1285 | } | |
1286 | ||
1287 | if(p) | |
1288 | *p = (char *)(c->exe)(ctx, c, NULL); | |
1289 | ||
1290 | return c; | |
1291 | } |