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