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