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