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