ChanDisplayFunc defaultchanfn = printchannel;
searchCmd *reg_nicksearch, *reg_chansearch, *reg_usersearch;
+void displaycommandhelp(nick *, Command *);
searchCmd *registersearchcommand(char *name, int level, CommandHandler cmd, void *defaultdisplayfunc) {
searchCmd *acmd;
searchList *sl;
- registercontrolhelpcmd(name, NO_OPER,4, cmd, "Usage: <criteria\nSearches with the given criteria");
-
+ registercontrolhelpfunccmd(name, NO_OPER,4, cmd, &displaycommandhelp);
acmd=(struct searchCmd *)malloc(sizeof(struct searchCmd));
-
+ if (!acmd) {
+ Error("newsearch", ERR_ERROR, "malloc failed: registersearchcommand");
+ return NULL;
+ }
acmd->handler = cmd;
acmd->name = getsstring( name, NSMAX_COMMAND_LEN);
addcommandtotree(searchCmdTree, name, 0, 0, (CommandHandler)acmd);
- sl = globalterms;
- while (sl) {
- registersearchterm( acmd, sl->name->content, sl->cmd);
- sl = sl->next;
+ for (sl=globalterms; sl; sl=sl->next) {
+ registersearchterm( acmd, sl->name->content, sl->cmd, 0, sl->help);
}
return acmd;
free(scmd);
}
-void regdisp( searchCmd *cmd, const char *name, void *handler) {
- addcommandtotree(cmd->outputtree, name, 0, 0, (CommandHandler) handler);
+void displaycommandhelp(nick *np, Command *cmd) {
+ int i,n,j,m;
+ Command *cmdlist[100], *acmdlist[100];
+ searchCmd *acmd;
+
+ n=getcommandlist(searchCmdTree,cmdlist,100);
+ for(i=0;i<n;i++) {
+ /* note: we may want to only deregister a command if we've already registered it, for now, try de-registering new commands anyway */
+ if ( ((searchCmd *)cmdlist[i]->handler)->handler != cmd->handler )
+ continue;
+ acmd = ((searchCmd *)(cmdlist[i]->handler));
+
+ controlreply(np, "Usage: [flags] <criteria>\n");
+ controlreply(np, "Flags:\n");
+ controlreply(np, " -l int : Limit number of rows of results\n");
+ controlreply(np, " -d string : a valid output format for the results\n");
+ controlreply(np, " -s subset : ipmask subset of network to search (only for node search)\n");
+ controlreply(np, " \n");
+ controlreply(np, "Available Output Formats:\n");
+
+ m=getcommandlist(acmd->outputtree,acmdlist,100);
+ for(j=0;j<m;j++) {
+ if ( controlpermitted( acmdlist[j]->level, np) ) {
+ char *help=(char *)acmdlist[j]->ext;
+ if ( help && help[0] != '\0')
+ controlreply(np, "%-10s: %s\n", acmdlist[j]->command->content, help);
+ else
+ controlreply(np, "%s\n", acmdlist[j]->command->content );
+ }
+ }
+
+ controlreply(np, " \n");
+ controlreply(np, "Available Global Commands and Operators:\n" );
+ m=getcommandlist(acmd->searchtree,acmdlist,100);
+ for(j=0;j<m;j++) {
+ if ( acmdlist[j]->maxparams) {
+ char *help=(char *)acmdlist[j]->ext;
+ if ( help && help[0] != '\0')
+ controlreply(np, "%-10s: %s\n", acmdlist[j]->command->content, help );
+ else
+ controlreply(np, "%s\n", acmdlist[j]->command->content );
+ }
+ }
+
+ controlreply(np, " \n");
+ controlreply(np, "Available Commands and Operators for %s:\n", acmd->name->content);
+
+ m=getcommandlist(acmd->searchtree,acmdlist,100);
+ for(j=0;j<m;j++) {
+ if ( !acmdlist[j]->maxparams && controlpermitted( acmdlist[j]->level, np) ) {
+ char *help=(char *)acmdlist[j]->ext;
+ if ( help && help[0] != '\0')
+ controlreply(np, "%-10s: %s\n", acmdlist[j]->command->content, help );
+ else
+ controlreply(np, "%s\n", acmdlist[j]->command->content );
+ }
+ }
+ }
+}
+
+void regdisp( searchCmd *cmd, const char *name, void *handler, int level, char *help) {
+ addcommandexttotree(cmd->outputtree, name, level, 0, (CommandHandler) handler, (char *)help);
}
void unregdisp( searchCmd *cmd, const char *name, void *handler ) {
}
void *findcommandinlist( searchList *sl, char *name){
- while(sl) {
+ while (sl) {
if(strcmp(sl->name->content,name) == 0 ) {
return sl;
}
- sl = sl->next;
+ sl=sl->next;
}
return NULL;
}
reg_usersearch = (searchCmd *)registersearchcommand("usersearch",NO_OPER,&do_usersearch, printuser);
/* Boolean operations */
- registerglobalsearchterm("and",and_parse);
- registerglobalsearchterm("not",not_parse);
- registerglobalsearchterm("or",or_parse);
+ registerglobalsearchterm("and",and_parse, "usage: (and (X) (X))" );
+ registerglobalsearchterm("not",not_parse, "usage: (not (X))");
+ registerglobalsearchterm("or",or_parse, "usage: (or (X) (X))" );
- registerglobalsearchterm("eq",eq_parse);
+ registerglobalsearchterm("eq",eq_parse, "usage: (eq (X) Y)");
- registerglobalsearchterm("lt",lt_parse);
- registerglobalsearchterm("gt",gt_parse);
+ registerglobalsearchterm("lt",lt_parse, "usage: (lt (X) int)");
+ registerglobalsearchterm("gt",gt_parse, "usage: (gt (X) int)");
/* String operations */
- registerglobalsearchterm("match",match_parse);
- registerglobalsearchterm("regex",regex_parse);
- registerglobalsearchterm("length",length_parse);
+ registerglobalsearchterm("match",match_parse, "usage: (match (X) string)");
+ registerglobalsearchterm("regex",regex_parse, "usage: (regex (X) string)");
+ registerglobalsearchterm("length",length_parse, "usage: (length string)");
/* Nickname operations */
- registersearchterm(reg_nicksearch, "hostmask",hostmask_parse); /* nick only */
- registersearchterm(reg_nicksearch, "realname",realname_parse); /* nick only */
- registersearchterm(reg_nicksearch, "authname",authname_parse); /* nick only */
- registersearchterm(reg_nicksearch, "authts",authts_parse); /* nick only */
- registersearchterm(reg_nicksearch, "ident",ident_parse); /* nick only */
- registersearchterm(reg_nicksearch, "host",host_parse); /* nick only */
- registersearchterm(reg_nicksearch, "channel",channel_parse); /* nick only */
- registersearchterm(reg_nicksearch, "timestamp",timestamp_parse); /* nick only */
- registersearchterm(reg_nicksearch, "country",country_parse); /* nick only */
- registersearchterm(reg_nicksearch, "ip",ip_parse); /* nick only */
- registersearchterm(reg_nicksearch, "channels",channels_parse); /* nick only */
- registersearchterm(reg_nicksearch, "server",server_parse); /* nick only */
- registersearchterm(reg_nicksearch, "authid",authid_parse); /* nick only */
+ registersearchterm(reg_nicksearch, "hostmask",hostmask_parse, 0, "The user's nick!user@host; \"hostmask real\" returns nick!user@host\rreal"); /* nick only */
+ registersearchterm(reg_nicksearch, "realname",realname_parse, 0, "User's current realname"); /* nick only */
+ registersearchterm(reg_nicksearch, "authname",authname_parse, 0, "User's current authname or false"); /* nick only */
+ registersearchterm(reg_nicksearch, "authts",authts_parse, 0, "User's Auth timestamp"); /* nick only */
+ registersearchterm(reg_nicksearch, "ident",ident_parse, 0, "User's current ident"); /* nick only */
+ registersearchterm(reg_nicksearch, "host",host_parse, 0, "User's host, allow \"host real\" to match real host"); /* nick only */
+ registersearchterm(reg_nicksearch, "channel",channel_parse, 0, "Valid Channel Name to match users against"); /* nick only */
+ registersearchterm(reg_nicksearch, "timestamp",timestamp_parse, 0, "User's Timestamp"); /* nick only */
+ registersearchterm(reg_nicksearch, "country",country_parse, 0, "2 letter country code (data source is geoip)"); /* nick only */
+ registersearchterm(reg_nicksearch, "ip",ip_parse, 0, "User's IP - ipv4 or ipv6 format as appropriate. Note: not 6to4"); /* nick only */
+ registersearchterm(reg_nicksearch, "channels",channels_parse, 0, "Channel Count"); /* nick only */
+ registersearchterm(reg_nicksearch, "server",server_parse, 0, "Server Name. Either (server string) or (match (server) string)"); /* nick only */
+ registersearchterm(reg_nicksearch, "authid",authid_parse, 0, "User's Auth ID"); /* nick only */
/* Channel operations */
- registersearchterm(reg_chansearch, "exists",exists_parse); /* channel only */
- registersearchterm(reg_chansearch, "services",services_parse); /* channel only */
- registersearchterm(reg_chansearch, "size",size_parse); /* channel only */
- registersearchterm(reg_chansearch, "name",name_parse); /* channel only */
- registersearchterm(reg_chansearch, "topic",topic_parse); /* channel only */
- registersearchterm(reg_chansearch, "oppct",oppct_parse); /* channel only */
- registersearchterm(reg_chansearch, "uniquehostpct",hostpct_parse); /* channel only */
- registersearchterm(reg_chansearch, "authedpct",authedpct_parse); /* channel only */
- registersearchterm(reg_chansearch, "kick",kick_parse); /* channel only */
+ registersearchterm(reg_chansearch, "exists",exists_parse, 0, "Returns if channel exists on network. Note: newserv may store data on empty channels"); /* channel only */
+ registersearchterm(reg_chansearch, "services",services_parse, 0, ""); /* channel only */
+ registersearchterm(reg_chansearch, "size",size_parse, 0, "Channel user count"); /* channel only */
+ registersearchterm(reg_chansearch, "name",name_parse, 0, "Channel Name"); /* channel only */
+ registersearchterm(reg_chansearch, "topic",topic_parse, 0, "Channel topic"); /* channel only */
+ registersearchterm(reg_chansearch, "oppct",oppct_parse, 0, "Percentage Opped"); /* channel only */
+ registersearchterm(reg_chansearch, "uniquehostpct",hostpct_parse, 0, "uniquehost percent"); /* channel only */
+ registersearchterm(reg_chansearch, "authedpct",authedpct_parse, 0, "Percentage of authed users"); /* channel only */
+ registersearchterm(reg_chansearch, "kick",kick_parse, 0, "KICK users channels in newsearch result. Note: evaluation order"); /* channel only */
/* Nickname / channel operations */
- registersearchterm(reg_chansearch, "modes",modes_parse);
- registersearchterm(reg_nicksearch, "modes",modes_parse);
- registersearchterm(reg_chansearch, "nick",nick_parse);
- registersearchterm(reg_nicksearch, "nick",nick_parse);
+ registersearchterm(reg_chansearch, "modes",modes_parse, 0, "User Modes");
+ registersearchterm(reg_nicksearch, "modes",modes_parse, 0, "Channel Modes");
+ registersearchterm(reg_chansearch, "nick",nick_parse, 0, "Nickname");
+ registersearchterm(reg_nicksearch, "nick",nick_parse, 0, "Nickname");
/* Kill / gline parameters */
- registersearchterm(reg_chansearch,"kill",kill_parse);
- registersearchterm(reg_chansearch,"gline",gline_parse);
- registersearchterm(reg_nicksearch,"kill",kill_parse);
- registersearchterm(reg_nicksearch,"gline",gline_parse);
+ registersearchterm(reg_chansearch,"kill",kill_parse, 0, "KILL users in newsearch result. Note: evaluation order");
+ registersearchterm(reg_chansearch,"gline",gline_parse, 0, "GLINE users in newsearch result. Note: evaluation order");
+ registersearchterm(reg_nicksearch,"kill",kill_parse, 0, "KILL users in newsearch result. Note: evaluation order");
+ registersearchterm(reg_nicksearch,"gline",gline_parse, 0, "GLINE users in newsearch result. Note: evaluation order");
/* Iteration functionality */
- registerglobalsearchterm("any",any_parse);
- registerglobalsearchterm("all",all_parse);
- registerglobalsearchterm("var",var_parse);
+ registerglobalsearchterm("any",any_parse, "usage: any (generatorfn x) (fn ... (var x) ...)");
+ registerglobalsearchterm("all",all_parse, "usage: all (generatorfn x) (fn ... (var x) ...)");
+ registerglobalsearchterm("var",var_parse, "usage: var variable");
/* Iterable functions */
- registersearchterm(reg_nicksearch, "channeliter",channeliter_parse); /* nick only */
+ registersearchterm(reg_nicksearch, "channeliter",channeliter_parse, 0, "Channel Iterable function - usage: (any (channeliter x) (match (var x) #twilight*))"); /* nick only */
/* Notice functionality */
- registersearchterm(reg_chansearch,"notice",notice_parse);
- registersearchterm(reg_nicksearch,"notice",notice_parse);
+ registersearchterm(reg_chansearch,"notice",notice_parse, 0, "NOTICE users in newsearch result. Note: evaluation order");
+ registersearchterm(reg_nicksearch,"notice",notice_parse, 0, "NOTICE users in newsearch result. Note: evaluation order");
/* Nick output filters */
- regdisp(reg_nicksearch,"default",printnick);
- regdisp(reg_nicksearch,"channels",printnick_channels);
+ regdisp(reg_nicksearch,"default",printnick, 0, "");
+ regdisp(reg_nicksearch,"channels",printnick_channels, 0, "include channels in output");
/* Channel output filters */
- regdisp(reg_chansearch,"default",printchannel);
- regdisp(reg_chansearch,"topic",printchannel_topic);
- regdisp(reg_chansearch,"services",printchannel_services);
+ regdisp(reg_chansearch,"default",printchannel, 0, "");
+ regdisp(reg_chansearch,"topic",printchannel_topic, 0, "display channel topics");
+ regdisp(reg_chansearch,"services",printchannel_services, 0, "display services on channels");
/* Nick output filters */
- regdisp(reg_usersearch,"default",printuser);
+ regdisp(reg_usersearch,"default",printuser, 0, "");
}
int i,n;
Command *cmdlist[100];
- sl = globalterms;
+ sl=globalterms;
while (sl) {
psl = sl;
- sl = sl->next;
+ sl=sl->next;
n=getcommandlist(searchCmdTree,cmdlist,100);
for(i=0;i<n;i++) {
}
freesstring(psl->name);
+ if (psl->help)
+ free (psl->help);
free(psl);
-
}
deregistersearchcommand( reg_nicksearch );
destroycommandtree( searchCmdTree );
}
-void registerglobalsearchterm(char *term, parseFunc parsefunc) {
- searchList *sl = malloc(sizeof(searchList));
+void registerglobalsearchterm(char *term, parseFunc parsefunc, char *help) {
int i,n;
Command *cmdlist[100];
+ searchList *sl = malloc(sizeof(searchList));
+ if (!sl) {
+ Error("newsearch", ERR_ERROR, "malloc failed: registerglobalsearchterm");
+ return;
+ }
sl->cmd = parsefunc;
sl->name = getsstring(term, NSMAX_COMMAND_LEN);
sl->next = NULL;
+ if (help) {
+ int len=strlen(help);
+ sl->help=(char *)malloc(len+1);
+ if(!sl->help) {
+ Error("newsearch", ERR_ERROR, "malloc failed: registerglobalsearchterm");
+ return;
+ }
+ strncpy(sl->help, help, len);
+ sl->help[len] = '\0';
+ } else {
+ sl->help=NULL;
+ }
+
if ( globalterms != NULL ) {
sl->next = globalterms;
n=getcommandlist(searchCmdTree,cmdlist,100);
for(i=0;i<n;i++) {
- registersearchterm( (searchCmd *)cmdlist[i]->handler,term, parsefunc);
+ /* maxparams is set to 1 to indicate a global term */
+ /* access level is set to 0 for all global terms */
+ addcommandexttotree( ((searchCmd *)cmdlist[i]->handler)->searchtree,term, 0, 1, (CommandHandler) parsefunc, help);
}
}
Command *cmdlist[100];
searchList *sl, *psl=NULL;
- sl = globalterms;
- while (sl) {
+ for (sl=globalterms; sl; sl=sl->next) {
if ( strcmp( sl->name->content, term) == 0 ) {
break;
}
psl = sl;
- sl = sl->next;
}
if (sl) {
n=getcommandlist(searchCmdTree,cmdlist,100);
for(i=0;i<n;i++) {
- deregistersearchterm( (searchCmd *)cmdlist[i]->handler, term, parsefunc);
+ deletecommandfromtree( ((searchCmd *)cmdlist[i]->handler)->searchtree, term, (CommandHandler) parsefunc);
}
freesstring(sl->name);
free(sl);
}
}
-void registersearchterm(searchCmd *cmd, char *term, parseFunc parsefunc) {
- addcommandtotree(cmd->searchtree, term, 0, 0, (CommandHandler) parsefunc);
+void registersearchterm(searchCmd *cmd, char *term, parseFunc parsefunc, int level, char *help) {
+ /* NOTE: global terms are added to the tree elsewhere as we set maxparams to 1 to indicate global */
+ addcommandexttotree(cmd->searchtree, term, level, 0, (CommandHandler) parsefunc, help);
}
void deregistersearchterm(searchCmd *cmd, char *term, parseFunc parsefunc) {
+ /* NOTE: global terms are removed from the tree within deregisterglobalsearchterm */
deletecommandfromtree(cmd->searchtree, term, (CommandHandler) parsefunc);
}
+static void controlwallwrapper(int level, char *format, ...) __attribute__ ((format (printf, 2, 3)));
static void controlwallwrapper(int level, char *format, ...) {
char buf[1024];
va_list ap;
switch(*ch) {
case 'l':
if (cargc<*arg) {
- reply(sender,"Error: -l switch requires an argument");
- return CMD_USAGE;
+ reply(sender,"Error: -l switch requires an argument (for help, see help <searchcmd>)");
+ return CMD_ERROR;
}
*limit=strtoul(cargv[(*arg)++],NULL,10);
break;
case 'd':
if (cargc<*arg) {
- reply(sender,"Error: -d switch requires an argument");
- return CMD_USAGE;
+ reply(sender,"Error: -d switch requires an argument (for help, see help <searchcmd>)");
+ return CMD_ERROR;
}
cmd=findcommandintree(sl, cargv[*arg],1);
if (!cmd) {
- reply(sender,"Error: unknown output format %s",cargv[*arg]);
- return CMD_USAGE;
+ reply(sender,"Error: unknown output format %s (for help, see help <searchcmd>)",cargv[*arg]);
+ return CMD_ERROR;
+ }
+ if ( !controlpermitted( cmd->level, sender) ) {
+ reply(sender,"Error: Access Denied for output format %s (for help, see help <searchcmd>)", cargv[*arg]);
+ return CMD_ERROR;
}
*display=(void *)cmd->handler;
(*arg)++;
case 's':
if (cargc<*arg) {
- reply(sender,"Error: -s switch requires an argument");
- return CMD_USAGE;
+ reply(sender,"Error: -s switch requires an argument (for help, see help <searchcmd>)");
+ return CMD_ERROR;
}
if (ipmask_parse(cargv[*arg], &sin, &bits) == 0) {
- reply(sender, "Error: Invalid CIDR mask supplied");
- return CMD_USAGE;
+ reply(sender, "Error: Invalid CIDR mask supplied (for help, see help <searchcmd>)");
+ return CMD_ERROR;
}
*subset = (void *)refnode(iptree, &sin, bits);
(*arg)++;
break;
default:
- reply(sender,"Unrecognised flag -%c.",*ch);
+ reply(sender,"Unrecognised flag -%c. (for help, see help <searchcmd>)",*ch);
+ return CMD_ERROR;
}
}
}
return CMD_OK;
}
-void newsearch_ctxinit(searchCtx *ctx, searchParseFunc searchfn, replyFunc replyfn, wallFunc wallfn, void *arg, searchCmd *cmd) {
+void newsearch_ctxinit(searchCtx *ctx, searchParseFunc searchfn, replyFunc replyfn, wallFunc wallfn, void *arg, searchCmd *cmd, nick *np) {
memset(ctx, 0, sizeof(searchCtx));
ctx->reply = replyfn;
ctx->parser = searchfn;
ctx->arg = arg;
ctx->searchcmd = cmd;
+ ctx->sender = np;
}
int do_nicksearch_real(replyFunc reply, wallFunc wall, void *source, int cargc, char **cargv) {
searchCtx ctx;
int ret;
- if (cargc<1)
- return CMD_USAGE;
-
+ if (cargc<1) {
+ reply( sender, "Usage: [flags] <criteria>");
+ reply( sender, "For help, see help nicksearch");
+ return CMD_OK;
+ }
+
ret = parseopts(cargc, cargv, &arg, &limit, NULL, (void **)&display, reg_nicksearch->outputtree, reply, sender);
if(ret != CMD_OK)
return ret;
rejoinline(cargv[arg],cargc-arg);
}
- newsearch_ctxinit(&ctx, search_parse, reply, wall, NULL, reg_nicksearch);
+ newsearch_ctxinit(&ctx, search_parse, reply, wall, NULL, reg_nicksearch, sender);
if (!(search = ctx.parser(&ctx, cargv[arg]))) {
reply(sender,"Parse error: %s",parseError);
searchCtx ctx;
int ret;
- if (cargc<1)
- return CMD_USAGE;
+ if (cargc<1) {
+ reply( sender, "Usage: [flags] <criteria>");
+ reply( sender, "For help, see help chansearch");
+ return CMD_OK;
+ }
ret = parseopts(cargc, cargv, &arg, &limit, NULL, (void **)&display, reg_chansearch->outputtree, reply, sender);
if(ret != CMD_OK)
rejoinline(cargv[arg],cargc-arg);
}
- newsearch_ctxinit(&ctx, search_parse, reply, wall, NULL, reg_chansearch);
+ newsearch_ctxinit(&ctx, search_parse, reply, wall, NULL, reg_chansearch, sender);
if (!(search = ctx.parser(&ctx, cargv[arg]))) {
reply(sender,"Parse error: %s",parseError);
return CMD_ERROR;
searchCtx ctx;
int ret;
- if (cargc<1)
- return CMD_USAGE;
-
+ if (cargc<1) {
+ reply( sender, "Usage: [flags] <criteria>");
+ reply( sender, "For help, see help usersearch");
+ return CMD_OK;
+ }
+
ret = parseopts(cargc, cargv, &arg, &limit, NULL, (void **)&display, reg_usersearch->outputtree, reply, sender);
if(ret != CMD_OK)
return ret;
rejoinline(cargv[arg],cargc-arg);
}
- newsearch_ctxinit(&ctx, search_parse, reply, wall, NULL, reg_usersearch);
+ newsearch_ctxinit(&ctx, search_parse, reply, wall, NULL, reg_usersearch, sender);
+
if (!(search = ctx.parser(&ctx, cargv[arg]))) {
reply(sender,"Parse error: %s",parseError);
return CMD_ERROR;
j--; /* remove an argument */
if (!(cmd=findcommandintree(ctx->searchcmd->searchtree,argvector[0],1))) {
- parseError = "Unknown command";
+ parseError = "Unknown command (for valid command list, see help <searchcmd>)";
return NULL;
} else {
+ if ( !controlpermitted( cmd->level, ctx->sender) ) {
+ parseError = "Access Denied (for valid command list, see help <searchcmd>)";
+ return NULL;
+ }
return ((parseFunc)cmd->handler)(ctx, j, argvector+1);
}
} else {