-
#include <stdio.h>
-#include "newsearch.h"
+#include <stdarg.h>
+#include <string.h>
#include "../irc/irc_config.h"
#include "../lib/irc_string.h"
#include "../control/control.h"
#include "../lib/splitline.h"
#include "../lib/version.h"
+#include "../lib/stringbuf.h"
+#include "../lib/strlfunc.h"
+#include "../lib/array.h"
+#include "newsearch.h"
MODULE_VERSION("");
-CommandTree *searchTree;
+CommandTree *searchCmdTree;
+searchList *globalterms = NULL;
int do_nicksearch(void *source, int cargc, char **cargv);
int do_chansearch(void *source, int cargc, char **cargv);
-struct searchNode *search_parse(int type, char *input);
+int do_usersearch(void *source, int cargc, char **cargv);
+
+void printnick_channels(searchCtx *, nick *, nick *);
+void printchannel(searchCtx *, nick *, chanindex *);
+void printchannel_topic(searchCtx *, nick *, chanindex *);
+void printchannel_services(searchCtx *, nick *, chanindex *);
+
+UserDisplayFunc defaultuserfn = printuser;
+NickDisplayFunc defaultnickfn = printnick;
+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;
+
+ 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);
+ acmd->outputtree = newcommandtree();
+ acmd->searchtree = newcommandtree();
+
+ addcommandtotree(searchCmdTree, name, 0, 0, (CommandHandler)acmd);
+
+ for (sl=globalterms; sl; sl=sl->next) {
+ registersearchterm( acmd, sl->name->content, sl->cmd, 0, sl->help);
+ }
+
+ return acmd;
+}
+
+void deregistersearchcommand(searchCmd *scmd) {
+ deregistercontrolcmd(scmd->name->content, (CommandHandler)scmd->handler);
+ destroycommandtree(scmd->outputtree);
+ destroycommandtree(scmd->searchtree);
+ freesstring(scmd->name);
+ free(scmd);
+}
+
+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 ) {
+ deletecommandfromtree(cmd->outputtree, name, (CommandHandler) handler);
+}
-void registersearchterm(char *term, parseFunc parsefunc);
-void deregistersearchterm(char *term, parseFunc parsefunc);
+void *findcommandinlist( searchList *sl, char *name){
+ while (sl) {
+ if(strcmp(sl->name->content,name) == 0 ) {
+ return sl;
+ }
+ sl=sl->next;
+ }
+ return NULL;
+}
const char *parseError;
/* used for *_free functions that need to warn users of certain things
nick *senderNSExtern;
void _init() {
- searchTree=newcommandtree();
+ searchCmdTree=newcommandtree();
+
+ reg_nicksearch = (searchCmd *)registersearchcommand("nicksearch",NO_OPER,&do_nicksearch, printnick);
+ reg_chansearch = (searchCmd *)registersearchcommand("chansearch",NO_OPER,&do_chansearch, printchannel);
+ reg_usersearch = (searchCmd *)registersearchcommand("usersearch",NO_OPER,&do_usersearch, printuser);
/* Boolean operations */
- registersearchterm("and",and_parse);
- registersearchterm("not",not_parse);
- registersearchterm("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))" );
- registersearchterm("eq",eq_parse);
+ registerglobalsearchterm("eq",eq_parse, "usage: (eq (X) Y)");
- registersearchterm("lt",lt_parse);
- registersearchterm("gt",gt_parse);
+ registerglobalsearchterm("lt",lt_parse, "usage: (lt (X) int)");
+ registerglobalsearchterm("gt",gt_parse, "usage: (gt (X) int)");
/* String operations */
- registersearchterm("match",match_parse);
- registersearchterm("regex",regex_parse);
- registersearchterm("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("hostmask",hostmask_parse);
- registersearchterm("realname",realname_parse);
- registersearchterm("authname",authname_parse);
- registersearchterm("authts",authts_parse);
- registersearchterm("ident",ident_parse);
- registersearchterm("host",host_parse);
- registersearchterm("channel",channel_parse);
- registersearchterm("timestamp",timestamp_parse);
- registersearchterm("country",country_parse);
- registersearchterm("ip",ip_parse);
+ 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("exists",exists_parse);
- registersearchterm("services",services_parse);
- registersearchterm("size",size_parse);
- registersearchterm("name",name_parse);
- registersearchterm("topic",topic_parse);
- registersearchterm("oppct",oppct_parse);
- registersearchterm("uniquehostpct",hostpct_parse);
- registersearchterm("authedpct",authedpct_parse);
- registersearchterm("kick",kick_parse);
+ 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("modes",modes_parse);
- registersearchterm("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("kill",kill_parse);
- registersearchterm("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");
- registercontrolhelpcmd("nicksearch",NO_OPER,4,do_nicksearch, "Usage: nicksearch <criteria>\nSearches for nicknames with the given criteria.");
- registercontrolhelpcmd("chansearch",NO_OPER,4,do_chansearch, "Usage: chansearch <criteria>\nSearches for channels with the given criteria.");
+ /* Iteration functionality */
+ 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, 0, "Channel Iterable function - usage: (any (channeliter x) (match (var x) #twilight*))"); /* nick only */
+
+ /* Notice functionality */
+ 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, 0, "");
+ regdisp(reg_nicksearch,"channels",printnick_channels, 0, "include channels in output");
+
+ /* Channel output filters */
+ 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, 0, "");
+
}
void _fini() {
- destroycommandtree(searchTree);
- deregistercontrolcmd("nicksearch", do_nicksearch);
- deregistercontrolcmd("chansearch", do_chansearch);
-}
+ searchList *sl, *psl;
+ int i,n;
+ Command *cmdlist[100];
-void registersearchterm(char *term, parseFunc parsefunc) {
- addcommandtotree(searchTree, term, 0, 0, (CommandHandler) parsefunc);
-}
+ sl=globalterms;
+ while (sl) {
+ psl = sl;
+ sl=sl->next;
-void deregistersearchterm(char *term, parseFunc parsefunc) {
- deletecommandfromtree(searchTree, term, (CommandHandler) parsefunc);
-}
+ n=getcommandlist(searchCmdTree,cmdlist,100);
+ for(i=0;i<n;i++) {
+ deregistersearchterm( (searchCmd *)cmdlist[i]->handler, psl->name->content, psl->cmd);
+ }
-void printnick(nick *sender, nick *np) {
- char hostbuf[HOSTLEN+NICKLEN+USERLEN+4];
+ freesstring(psl->name);
+ if (psl->help)
+ free (psl->help);
+ free(psl);
+ }
- controlreply(sender,"%s [%s] (%s) (%s)",visiblehostmask(np,hostbuf),
- IPtostr(np->p_ipaddr), printflags(np->umodes, umodeflags), np->realname->name->content);
+ deregistersearchcommand( reg_nicksearch );
+ deregistersearchcommand( reg_chansearch );
+ deregistersearchcommand( reg_usersearch );
+ destroycommandtree( searchCmdTree );
}
-void printchannel(nick *sender, chanindex *cip) {
- /* shamelessly stolen from (now defunct) chansearch.c */
- int i;
- int op,voice,peon;
- int oper,service,hosts;
- nick *np;
- chanuserhash *cuhp;
- unsigned int marker;
-
- op=voice=peon=oper=service=hosts=0;
- marker=nexthostmarker();
-
- if (cip->channel==NULL) {
- controlreply(sender,"[ Channel currently empty ] %s",cip->name->content);
+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 {
- cuhp=cip->channel->users;
- for (i=0;i<cuhp->hashsize;i++) {
- if (cuhp->content[i]!=nouser) {
- if (cuhp->content[i]&CUMODE_OP) {
- op++;
- } else if (cuhp->content[i]&CUMODE_VOICE) {
- voice++;
- } else {
- peon++;
- }
- if ((np=getnickbynumeric(cuhp->content[i]&CU_NUMERICMASK))!=NULL) {
- if (IsOper(np)) {
- oper++;
- }
- if (IsService(np)) {
- service++;
- }
- if (np->host->marker!=marker) {
- np->host->marker=marker;
- hosts++;
- }
- }
- }
+ sl->help=NULL;
+ }
+
+
+ if ( globalterms != NULL ) {
+ sl->next = globalterms;
+ }
+ globalterms = sl;
+
+ n=getcommandlist(searchCmdTree,cmdlist,100);
+ for(i=0;i<n;i++) {
+ /* 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);
+ }
+}
+
+void deregisterglobalsearchterm(char *term, parseFunc parsefunc) {
+ int i,n;
+ Command *cmdlist[100];
+ searchList *sl, *psl=NULL;
+
+ for (sl=globalterms; sl; sl=sl->next) {
+ if ( strcmp( sl->name->content, term) == 0 ) {
+ break;
+ }
+ psl = sl;
+ }
+
+ if (sl) {
+ if( psl ) {
+ psl->next = sl->next;
+ }
+
+ n=getcommandlist(searchCmdTree,cmdlist,100);
+ for(i=0;i<n;i++) {
+ deletecommandfromtree( ((searchCmd *)cmdlist[i]->handler)->searchtree, term, (CommandHandler) parsefunc);
}
- controlreply(sender,"[ %4dU %4d@ %4d+ %4d %4d* %4dk %4dH ] %s (%s)",cuhp->totalusers,op,voice,peon,oper,
- service,hosts,cip->name->content, printflags(cip->channel->flags, cmodeflags));
+ freesstring(sl->name);
+ free(sl);
}
}
-int do_nicksearch(void *source, int cargc, char **cargv) {
- nick *sender = senderNSExtern = source, *np;
- int i;
- struct searchNode *search;
- int limit=500,matches=0;
- char *ch;
- int arg=0;
+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);
+}
- if (cargc<1)
- return CMD_USAGE;
-
+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;
+
+ va_start(ap, format);
+ vsnprintf(buf, sizeof(buf), format, ap);
+ controlwall(NO_OPER, level, "%s", buf);
+ va_end(ap);
+}
+
+int parseopts(int cargc, char **cargv, int *arg, int *limit, void **subset, void **display, CommandTree *sl, replyFunc reply, void *sender) {
+ char *ch;
+ Command *cmd;
+ struct irc_in_addr sin; unsigned char bits;
+
if (*cargv[0] == '-') {
/* options */
- arg++;
+ (*arg)++;
for (ch=cargv[0]+1;*ch;ch++) {
switch(*ch) {
case 'l':
- if (cargc<arg) {
- controlreply(sender,"Error: -l switch requires an argument");
- return CMD_USAGE;
+ if (cargc<*arg) {
+ reply(sender,"Error: -l switch requires an argument (for help, see help <searchcmd>)");
+ return CMD_ERROR;
}
- limit=strtoul(cargv[arg++],NULL,10);
+ *limit=strtoul(cargv[(*arg)++],NULL,10);
break;
+ case 'd':
+ if (cargc<*arg) {
+ 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 (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)++;
+ break;
+
+ case 's':
+ if (cargc<*arg) {
+ 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 (for help, see help <searchcmd>)");
+ return CMD_ERROR;
+ }
+ *subset = (void *)refnode(iptree, &sin, bits);
+ (*arg)++;
+ break;
+
default:
- controlreply(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, nick *np) {
+ memset(ctx, 0, sizeof(searchCtx));
+
+ ctx->reply = replyfn;
+ ctx->wall = wallfn;
+ 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) {
+ nick *sender = source;
+ struct searchNode *search;
+ int limit=500;
+ int arg=0;
+ NickDisplayFunc display=defaultnickfn;
+ searchCtx ctx;
+ int ret;
+
+ 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;
+
if (arg>=cargc) {
- controlreply(sender,"No search terms - aborting.");
+ reply(sender,"No search terms - aborting.");
return CMD_ERROR;
}
if (arg<(cargc-1)) {
rejoinline(cargv[arg],cargc-arg);
}
-
- if (!(search = search_parse(SEARCHTYPE_NICK, cargv[arg]))) {
- controlreply(sender,"Parse error: %s",parseError);
+
+ newsearch_ctxinit(&ctx, search_parse, reply, wall, NULL, reg_nicksearch, sender);
+
+ if (!(search = ctx.parser(&ctx, cargv[arg]))) {
+ reply(sender,"Parse error: %s",parseError);
return CMD_ERROR;
}
+
+ nicksearch_exe(search, &ctx, sender, display, limit);
+
+ (search->free)(&ctx, search);
+
+ return CMD_OK;
+}
+
+int do_nicksearch(void *source, int cargc, char **cargv) {
+ return do_nicksearch_real(controlreply, controlwallwrapper, source, cargc, cargv);
+}
+
+void nicksearch_exe(struct searchNode *search, searchCtx *ctx, nick *sender, NickDisplayFunc display, int limit) {
+ int i, j;
+ int matches = 0;
+ unsigned int cmarker;
+ unsigned int tchans=0,uchans=0;
+ struct channel **cs;
+ nick *np;
+ senderNSExtern = sender;
+
+ /* Get a marker value to mark "seen" channels for unique count */
+ cmarker=nextchanmarker();
/* The top-level node needs to return a BOOL */
- search=coerceNode(search, RETURNTYPE_BOOL);
+ search=coerceNode(ctx, search, RETURNTYPE_BOOL);
for (i=0;i<NICKHASHSIZE;i++) {
for (np=nicktable[i];np;np=np->next) {
- if ((search->exe)(search, np)) {
+ if ((search->exe)(ctx, search, np)) {
+ /* Add total channels */
+ tchans += np->channels->cursi;
+
+ /* Check channels for uniqueness */
+ cs=(channel **)np->channels->content;
+ for (j=0;j<np->channels->cursi;j++) {
+ if (cs[j]->index->marker != cmarker) {
+ cs[j]->index->marker=cmarker;
+ uchans++;
+ }
+ }
+
if (matches<limit)
- printnick(sender, np);
+ display(ctx, sender, np);
+
if (matches==limit)
- controlreply(sender, "--- More than %d matches, skipping the rest",limit);
+ ctx->reply(sender, "--- More than %d matches, skipping the rest",limit);
matches++;
}
}
}
- (search->free)(search);
-
- controlreply(sender,"--- End of list: %d matches", matches);
-
- return CMD_OK;
+ ctx->reply(sender,"--- End of list: %d matches; users were on %u channels (%u unique, %.1f average clones)",
+ matches, tchans, uchans, (float)tchans/uchans);
}
-int do_chansearch(void *source, int cargc, char **cargv) {
- nick *sender = senderNSExtern = source;
- chanindex *cip;
- int i;
+int do_chansearch_real(replyFunc reply, wallFunc wall, void *source, int cargc, char **cargv) {
+ nick *sender = source;
struct searchNode *search;
- int limit=500,matches=0;
- char *ch;
+ int limit=500;
int arg=0;
+ ChanDisplayFunc display=defaultchanfn;
+ searchCtx ctx;
+ int ret;
- if (cargc<1)
- return CMD_USAGE;
-
- if (*cargv[0] == '-') {
- /* options */
- arg++;
-
- for (ch=cargv[0]+1;*ch;ch++) {
- switch(*ch) {
- case 'l':
- if (cargc<arg) {
- controlreply(sender,"Error: -l switch requires an argument");
- return CMD_USAGE;
- }
- limit=strtoul(cargv[arg++],NULL,10);
- break;
-
- default:
- controlreply(sender,"Unrecognised flag -%c.",*ch);
- }
- }
+ 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)
+ return ret;
if (arg>=cargc) {
- controlreply(sender,"No search terms - aborting.");
+ reply(sender,"No search terms - aborting.");
return CMD_ERROR;
}
if (arg<(cargc-1)) {
rejoinline(cargv[arg],cargc-arg);
}
-
- if (!(search = search_parse(SEARCHTYPE_CHANNEL, cargv[arg]))) {
- controlreply(sender,"Parse error: %s",parseError);
+
+ 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;
}
- search=coerceNode(search, RETURNTYPE_BOOL);
+ chansearch_exe(search, &ctx, sender, display, limit);
+
+ (search->free)(&ctx, search);
+
+ return CMD_OK;
+}
+
+int do_chansearch(void *source, int cargc, char **cargv) {
+ return do_chansearch_real(controlreply, controlwallwrapper, source, cargc, cargv);
+}
+
+void chansearch_exe(struct searchNode *search, searchCtx *ctx, nick *sender, ChanDisplayFunc display, int limit) {
+ int i;
+ chanindex *cip;
+ int matches = 0;
+ senderNSExtern = sender;
+
+ search=coerceNode(ctx, search, RETURNTYPE_BOOL);
for (i=0;i<CHANNELHASHSIZE;i++) {
for (cip=chantable[i];cip;cip=cip->next) {
- if ((search->exe)(search, cip)) {
+ if ((search->exe)(ctx, search, cip)) {
if (matches<limit)
- printchannel(sender, cip);
+ display(ctx, sender, cip);
if (matches==limit)
- controlreply(sender, "--- More than %d matches, skipping the rest",limit);
+ ctx->reply(sender, "--- More than %d matches, skipping the rest",limit);
matches++;
}
}
}
- (search->free)(search);
+ ctx->reply(sender,"--- End of list: %d matches", matches);
+}
+
+int do_usersearch_real(replyFunc reply, wallFunc wall, void *source, int cargc, char **cargv) {
+ nick *sender = source;
+ struct searchNode *search;
+ int limit=500;
+ int arg=0;
+ UserDisplayFunc display=defaultuserfn;
+ searchCtx ctx;
+ int ret;
- controlreply(sender,"--- End of list: %d matches", matches);
+ 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;
+
+ if (arg>=cargc) {
+ reply(sender,"No search terms - aborting.");
+ return CMD_ERROR;
+ }
+
+ if (arg<(cargc-1)) {
+ rejoinline(cargv[arg],cargc-arg);
+ }
+
+ 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;
+ }
+
+ usersearch_exe(search, &ctx, sender, display, limit);
+
+ (search->free)(&ctx, search);
return CMD_OK;
}
-struct coercedata {
- struct searchNode *child;
- union {
- char *stringbuf;
- unsigned long val;
- } u;
-};
+int do_usersearch(void *source, int cargc, char **cargv) {
+ return do_usersearch_real(controlreply, controlwallwrapper, source, cargc, cargv);
+}
+
+void usersearch_exe(struct searchNode *search, searchCtx *ctx, nick *sender, UserDisplayFunc display, int limit) {
+ int i;
+ authname *aup;
+ int matches = 0;
+ senderNSExtern = sender;
+
+ search=coerceNode(ctx, search, RETURNTYPE_BOOL);
+
+ for (i=0;i<AUTHNAMEHASHSIZE;i++) {
+ for (aup=authnametable[i];aup;aup=aup->next) {
+ if ((search->exe)(ctx, search, aup)) {
+ if (matches<limit)
+ display(ctx, sender, aup);
+ if (matches==limit)
+ ctx->reply(sender, "--- More than %d matches, skipping the rest",limit);
+ matches++;
+ }
+ }
+ }
+
+ ctx->reply(sender,"--- End of list: %d matches", matches);
+}
/* Free a coerce node */
-void free_coerce(struct searchNode *thenode) {
+void free_coerce(searchCtx *ctx, struct searchNode *thenode) {
struct coercedata *cd=thenode->localdata;
- cd->child->free(cd->child);
+ cd->child->free(ctx, cd->child);
free(thenode->localdata);
free(thenode);
}
/* Free a coerce node with a stringbuf allocated */
-void free_coercestring(struct searchNode *thenode) {
+void free_coercestring(searchCtx *ctx, struct searchNode *thenode) {
free(((struct coercedata *)thenode->localdata)->u.stringbuf);
- free_coerce(thenode);
+ free_coerce(ctx, thenode);
}
/* exe_tostr_null: return the constant string */
-void *exe_tostr_null(struct searchNode *thenode, void *theinput) {
+void *exe_tostr_null(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
struct coercedata *cd=thenode->localdata;
return cd->u.stringbuf;
}
/* exe_val_null: return the constant value */
-void *exe_val_null(struct searchNode *thenode, void *theinput) {
+void *exe_val_null(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
struct coercedata *cd=thenode->localdata;
return (void *)cd->u.val;
}
/* Lots of very dull type conversion functions */
-void *exe_inttostr(struct searchNode *thenode, void *theinput) {
+void *exe_inttostr(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
struct coercedata *cd=thenode->localdata;
- sprintf(cd->u.stringbuf, "%lu", (unsigned long)(cd->child->exe)(cd->child, theinput));
+ sprintf(cd->u.stringbuf, "%lu", (unsigned long)(cd->child->exe)(ctx, cd->child, theinput));
return cd->u.stringbuf;
}
-void *exe_booltostr(struct searchNode *thenode, void *theinput) {
+void *exe_booltostr(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
struct coercedata *cd=thenode->localdata;
- if ((cd->child->exe)(cd->child, theinput)) {
+ if ((cd->child->exe)(ctx, cd->child, theinput)) {
sprintf(cd->u.stringbuf,"1");
} else {
cd->u.stringbuf[0]='\0';
return cd->u.stringbuf;
}
-void *exe_strtoint(struct searchNode *thenode, void *theinput) {
+void *exe_strtoint(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
struct coercedata *cd=thenode->localdata;
- return (void *)strtoul((cd->child->exe)(cd->child,theinput),NULL,10);
+ return (void *)strtoul((cd->child->exe)(ctx,cd->child,theinput),NULL,10);
}
-void *exe_booltoint(struct searchNode *thenode, void *theinput) {
+void *exe_booltoint(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
struct coercedata *cd=thenode->localdata;
/* Don't need to do anything */
- return (cd->child->exe)(cd->child, theinput);
+ return (cd->child->exe)(ctx, cd->child, theinput);
}
-void *exe_strtobool(struct searchNode *thenode, void *theinput) {
+void *exe_strtobool(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
struct coercedata *cd=thenode->localdata;
- char *ch=(cd->child->exe)(cd->child, theinput);
+ char *ch=(cd->child->exe)(ctx, cd->child, theinput);
if (!ch || *ch=='\0' || (*ch=='0' && ch[1]=='\0')) {
return (void *)0;
}
}
-void *exe_inttobool(struct searchNode *thenode, void *theinput) {
+void *exe_inttobool(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
struct coercedata *cd=thenode->localdata;
- if ((cd->child->exe)(cd->child, theinput)) {
+ if ((cd->child->exe)(ctx, cd->child, theinput)) {
return (void *)1;
} else {
return (void *)0;
}
}
-struct searchNode *coerceNode(struct searchNode *thenode, int type) {
+struct searchNode *coerceNode(searchCtx *ctx, struct searchNode *thenode, int type) {
struct searchNode *anode;
struct coercedata *cd;
case RETURNTYPE_INT:
if (thenode->returntype & RETURNTYPE_CONST) {
/* Constant node: sort it out now */
- sprintf(cd->u.stringbuf, "%lu", (unsigned long)thenode->exe(thenode, NULL));
+ sprintf(cd->u.stringbuf, "%lu", (unsigned long)thenode->exe(ctx, thenode, NULL));
anode->exe=exe_tostr_null;
anode->returntype |= RETURNTYPE_CONST;
} else {
case RETURNTYPE_BOOL:
if (thenode->returntype & RETURNTYPE_CONST) {
/* Constant bool value */
- if (thenode->exe(thenode,NULL)) {
+ if (thenode->exe(ctx, thenode,NULL)) {
/* True! */
sprintf(cd->u.stringbuf, "1");
} else {
switch (thenode->returntype & RETURNTYPE_TYPE) {
case RETURNTYPE_STRING:
if (thenode->returntype & RETURNTYPE_CONST) {
- cd->u.val=strtoul((thenode->exe)(thenode, NULL), NULL, 10);
+ cd->u.val=strtoul((thenode->exe)(ctx, thenode, NULL), NULL, 10);
anode->exe=exe_val_null;
anode->returntype |= RETURNTYPE_CONST;
} else {
default:
case RETURNTYPE_BOOL:
if (thenode->returntype & RETURNTYPE_CONST) {
- if ((thenode->exe)(thenode,NULL))
+ if ((thenode->exe)(ctx, thenode,NULL))
cd->u.val=1;
else
cd->u.val=0;
switch (thenode->returntype & RETURNTYPE_TYPE) {
case RETURNTYPE_STRING:
if (thenode->returntype & RETURNTYPE_CONST) {
- char *rv=(char *)((thenode->exe)(thenode, NULL));
+ char *rv=(char *)((thenode->exe)(ctx, thenode, NULL));
if (!rv || *rv=='\0' || (*rv=='0' && rv[1]=='\0'))
cd->u.val=0;
else
default:
case RETURNTYPE_INT:
if (thenode->returntype & RETURNTYPE_CONST) {
- if ((thenode->exe)(thenode,NULL))
+ if ((thenode->exe)(ctx, thenode,NULL))
cd->u.val=1;
else
cd->u.val=0;
}
/* Literals always return constant strings... */
-void *literal_exe(struct searchNode *thenode, void *theinput) {
- return ((sstring *)thenode->localdata)->content;
+void *literal_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
+ if (thenode->localdata)
+ return ((sstring *)thenode->localdata)->content;
+ else
+ return "";
}
-void literal_free(struct searchNode *thenode) {
+void literal_free(searchCtx *ctx, struct searchNode *thenode) {
freesstring(thenode->localdata);
free(thenode);
}
* Given an input string, return a searchNode.
*/
-struct searchNode *search_parse(int type, char *input) {
+struct searchNode *search_parse(searchCtx *ctx, char *input) {
/* OK, we need to split the input into chunks on spaces and brackets.. */
char *argvector[100];
char thestring[500];
if (*(ch-1) == 0) /* if the last character was a space */
j--; /* remove an argument */
- if (!(cmd=findcommandintree(searchTree,argvector[0],1))) {
- parseError = "Unknown command";
+ if (!(cmd=findcommandintree(ctx->searchcmd->searchtree,argvector[0],1))) {
+ parseError = "Unknown command (for valid command list, see help <searchcmd>)";
return NULL;
} else {
- return ((parseFunc)cmd->handler)(type, j, argvector+1);
+ 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 {
/* Literal */
return thenode;
}
}
+
+void nssnprintf(char *buf, size_t size, const char *format, nick *np) {
+ StringBuf b;
+ const char *p;
+ char *c;
+ char hostbuf[512];
+
+ if(size == 0)
+ return;
+
+ b.buf = buf;
+ b.capacity = size;
+ b.len = 0;
+
+ for(p=format;*p;p++) {
+ if(*p != '%') {
+ if(!sbaddchar(&b, *p))
+ break;
+ continue;
+ }
+ p++;
+ if(*p == '\0')
+ break;
+ if(*p == '%') {
+ if(!sbaddchar(&b, *p))
+ break;
+ continue;
+ }
+
+ c = NULL;
+ switch(*p) {
+ case 'n':
+ c = np->nick; break;
+ case 'i':
+ c = np->ident; break;
+ case 'h':
+ c = np->host->name->content; break;
+ case 'I':
+ snprintf(hostbuf, sizeof(hostbuf), "%s", IPtostr(np->p_ipaddr));
+ c = hostbuf;
+ break;
+ case 'u':
+ snprintf(hostbuf, sizeof(hostbuf), "%s!%s@%s", np->nick, np->ident, IPtostr(np->p_ipaddr));
+ c = hostbuf;
+ break;
+ default:
+ c = "(bad format specifier)";
+ }
+ if(c)
+ if(!sbaddstr(&b, c))
+ break;
+ }
+
+ sbterminate(&b);
+
+ /* not required */
+ /*
+ buf[size-1] = '\0';
+ */
+}
+
+static char *var_tochar(searchCtx *ctx, char *arg, searchNode **variable) {
+ *variable = ctx->parser(ctx, arg);
+ if (!(*variable = coerceNode(ctx, *variable, RETURNTYPE_STRING)))
+ return NULL;
+
+ if(!((*variable)->returntype & RETURNTYPE_CONST)) {
+ parseError = "only constant variables allowed";
+ ((*variable)->free)(ctx, *variable);
+ return NULL;
+ }
+
+ return (char *)((*variable)->exe)(ctx, *variable, NULL);
+}
+
+void free_val_null(searchCtx *ctx, struct searchNode *thenode) {
+}
+
+struct searchVariable *var_register(searchCtx *ctx, char *arg, int type) {
+ searchNode *variable;
+ struct searchVariable *us;
+ char *var;
+ int i;
+
+ if(ctx->lastvar >= MAX_VARIABLES) {
+ parseError = "Maximum number of variables reached";
+ return NULL;
+ }
+
+ us = &ctx->vars[ctx->lastvar];
+
+ var = var_tochar(ctx, arg, &variable);
+ if(!var)
+ return NULL;
+
+ strlcpy(us->name, var, sizeof(us->name));
+ (variable->free)(ctx, variable);
+
+ for(i=0;i<ctx->lastvar;i++) {
+ if(!strcmp(us->name, ctx->vars[i].name)) {
+ parseError = "variable name already in use";
+ return NULL;
+ }
+ }
+
+ ctx->lastvar++;
+ us->data.returntype = type;
+ us->data.localdata = &us->cdata;
+ us->data.exe = exe_val_null;
+ us->data.free = free_val_null;
+
+ us->cdata.child = NULL;
+ return us;
+}
+
+searchNode *var_get(searchCtx *ctx, char *arg) {
+ searchNode *variable, *found = NULL;
+ int i;
+ char *var = var_tochar(ctx, arg, &variable);
+ if(!var)
+ return NULL;
+
+ for(i=0;i<ctx->lastvar;i++) {
+ if(!strcmp(var, ctx->vars[i].name)) {
+ found = &ctx->vars[i].data;
+ break;
+ }
+ }
+ (variable->free)(ctx, variable);
+
+ if(!found)
+ parseError = "variable not found";
+ return found;
+}
+
+void var_setstr(struct searchVariable *v, char *data) {
+ v->cdata.u.stringbuf = data;
+}