#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <stdint.h>
-#include "../control/control.h" /* controlreply() */
+#include "../control/control.h"
#include "../irc/irc.h" /* irc_send() */
#include "../lib/irc_string.h" /* IPtostr(), longtoduration(), durationtolong() */
#include "../lib/strlfunc.h"
+#include "../core/schedule.h"
+#include "../glines/glines.h"
/* used for *_free functions that need to warn users of certain things
i.e. hitting too many users in a (kill) or (gline) - declared in newsearch.c */
extern nick *senderNSExtern;
static const char *defaultreason = "You (%u) have been g-lined for violating our terms of service";
+static int defaultmindelay = 15;
+static int defaultvariance = 15;
-void *gline_exe(struct searchNode *thenode, void *theinput);
-void gline_free(struct searchNode *thenode);
+void *gline_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput);
+void gline_free(searchCtx *ctx, struct searchNode *thenode);
struct gline_localdata {
unsigned int marker;
unsigned int duration;
int count;
- int type;
char reason[NSMAX_REASON_LEN];
+ int mindelay;
+ int maxdelay;
};
-struct searchNode *gline_parse(int type, int argc, char **argv) {
+struct searchNode *common_gline_parse(searchCtx *ctx, int argc, char **argv, int withdelay) {
struct gline_localdata *localdata;
struct searchNode *thenode;
- int len;
- char *p;
if (!(localdata = (struct gline_localdata *) malloc(sizeof(struct gline_localdata)))) {
parseError = "malloc: could not allocate memory for this search.";
return NULL;
}
localdata->count = 0;
- localdata->type = type;
- if (type == SEARCHTYPE_CHANNEL)
+ if (ctx->searchcmd == reg_chansearch)
localdata->marker = nextchanmarker();
- else
+ else if (ctx->searchcmd == reg_nicksearch)
localdata->marker = nextnickmarker();
+ else if (ctx->searchcmd == reg_whowassearch)
+ localdata->marker = nextwhowasmarker();
+ else {
+ free(localdata);
+ parseError = "gline: invalid search type";
+ return NULL;
+ }
- switch (argc) {
- case 0:
+ /* default duration, default reason */
+ if(argc == 0) {
localdata->duration = NSGLINE_DURATION;
strlcpy(localdata->reason, defaultreason, sizeof(localdata->reason));
- break;
-
- case 1:
- if (strchr(argv[0], ' ') == NULL) { /* duration specified */
- localdata->duration = durationtolong(argv[0]);
- /* error checking on gline duration */
- if (localdata->duration == 0)
- localdata->duration = NSGLINE_DURATION;
- strlcpy(localdata->reason, defaultreason, sizeof(localdata->reason));
+ } else if(argc > (withdelay ? 4 : 2)) {
+ free(localdata);
+ parseError = "gline: invalid number of arguments";
+ return NULL;
+ } else {
+ char *argzerop, *reasonp, *durationp, *mindelayp, *maxdelayp;
+ struct searchNode *durationsn, *reasonsn, *mindelaysn, *maxdelaysn, *argzerosn;
+
+ if (!(argzerosn=argtoconststr("gline", ctx, argv[0], &argzerop))) {
+ free(localdata);
+ return NULL;
}
- else { /* reason specified */
- localdata->duration = NSGLINE_DURATION;
- p = argv[0];
- if(*p == '\"')
- p++;
- len = strlcpy(localdata->reason, p, sizeof(localdata->reason));
- if(len >= sizeof(localdata->reason)) {
- localdata->reason[sizeof(localdata->reason)-1] = '\0';
+ if(argc == 1) {
+ durationp = reasonp = mindelayp = maxdelayp = NULL;
+ durationsn = reasonsn = mindelaysn = maxdelaysn = NULL;
+
+ /* if we have a space it's a reason */
+ if(strchr(argzerop, ' ')) {
+ reasonsn = argzerosn;
+ reasonp = argzerop;
} else {
- localdata->reason[len-1] = '\0';
+ durationsn = argzerosn;
+ durationp = argzerop;
+ }
+ } else {
+ durationsn = argzerosn;
+ durationp = argzerop;
+
+ if (!(reasonsn=argtoconststr("gline", ctx, argv[1], &reasonp))) {
+ durationsn->free(ctx, durationsn);
+ free(localdata);
+ return NULL;
+ }
+
+ if (withdelay) {
+ mindelayp = maxdelayp = NULL;
+ mindelaysn = maxdelaysn = NULL;
+
+ if (argc > 2 && !(mindelaysn=argtoconststr("gline", ctx, argv[2], &mindelayp))) {
+ mindelaysn->free(ctx, mindelaysn);
+ free(localdata);
+ return NULL;
+ }
+
+ if (argc > 3 && !(maxdelaysn=argtoconststr("gline", ctx, argv[3], &maxdelayp))) {
+ maxdelaysn->free(ctx, maxdelaysn);
+ free(localdata);
+ return NULL;
+ }
}
}
- break;
+
+ if(!reasonp) {
+ strlcpy(localdata->reason, defaultreason, sizeof(localdata->reason));
+ } else {
+ strlcpy(localdata->reason, reasonp, sizeof(localdata->reason));
+ reasonsn->free(ctx, reasonsn);
+ }
- case 2:
- localdata->duration = durationtolong(argv[0]);
- /* error checking on gline duration */
- if (localdata->duration == 0)
+ if(!durationp) {
localdata->duration = NSGLINE_DURATION;
+ } else {
+ localdata->duration = durationtolong(durationp);
+ durationsn->free(ctx, durationsn);
+
+ if (localdata->duration == 0) {
+ parseError = "gline duration invalid.";
+ free(localdata);
+ return NULL;
+ }
+ }
- p = argv[1];
- if(*p == '\"')
- p++;
- len = strlcpy(localdata->reason, p, sizeof(localdata->reason));
- if(len >= sizeof(localdata->reason)) {
- localdata->reason[sizeof(localdata->reason)-1] = '\0';
+ if (withdelay) {
+ if(!mindelayp) {
+ localdata->mindelay = defaultmindelay;
+ } else {
+ localdata->mindelay = durationtolong(mindelayp);
+ mindelaysn->free(ctx, mindelaysn);
+
+ if (localdata->mindelay == 0) {
+ parseError = "gline mindelay invalid.";
+ free(localdata);
+ return NULL;
+ }
+ }
+
+ if(!maxdelayp) {
+ localdata->maxdelay = localdata->mindelay + defaultvariance;
+ } else {
+ localdata->maxdelay = durationtolong(maxdelayp);
+ maxdelaysn->free(ctx, maxdelaysn);
+
+ if (localdata->maxdelay == 0) {
+ parseError = "gline maxdelay invalid.";
+ free(localdata);
+ return NULL;
+ }
+ }
} else {
- localdata->reason[len-1] = '\0';
+ localdata->mindelay = 0;
+ localdata->maxdelay = 0;
}
- break;
- default:
- free(localdata);
- parseError = "gline: invalid number of arguments";
- return NULL;
+ if (localdata->mindelay < 0 || localdata->maxdelay < localdata->mindelay) {
+ parseError = "gline delay invalid.";
+ free(localdata);
+ return NULL;
+ }
}
if (!(thenode=(struct searchNode *)malloc(sizeof (struct searchNode)))) {
return thenode;
}
-void *gline_exe(struct searchNode *thenode, void *theinput) {
+struct searchNode *gline_parse(searchCtx *ctx, int argc, char **argv) {
+ return common_gline_parse(ctx, argc, argv, 0);
+}
+
+struct searchNode *delaygline_parse(searchCtx *ctx, int argc, char **argv) {
+ return common_gline_parse(ctx, argc, argv, 1);
+}
+
+void *gline_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
struct gline_localdata *localdata;
nick *np;
chanindex *cip;
+ whowas *ww;
localdata = thenode->localdata;
- if (localdata->type == SEARCHTYPE_CHANNEL) {
+ if (ctx->searchcmd == reg_chansearch) {
cip = (chanindex *)theinput;
cip->marker = localdata->marker;
- localdata->count += cip->channel->users->totalusers;
+ if (cip->channel != NULL)
+ localdata->count += cip->channel->users->totalusers;
}
else {
np = (nick *)theinput;
- np->marker = localdata->marker;
+ if (ctx->searchcmd == reg_nicksearch)
+ np->marker = localdata->marker;
+ else {
+ ww = (whowas *)np->next;
+ ww->marker = localdata->marker;
+ }
localdata->count++;
}
return (void *)1;
}
-void gline_free(struct searchNode *thenode) {
+static int glineuser(glinebuf *gbuf, nick *np, struct gline_localdata *localdata, time_t ti) {
+ char msgbuf[512];
+ if (!IsOper(np) && !IsService(np) && !IsXOper(np)) {
+ nssnprintf(msgbuf, sizeof(msgbuf), localdata->reason, np);
+ glinebufaddbynick(gbuf, np, 0, "newsearch", msgbuf, getnettime() + localdata->duration, getnettime(), getnettime() + localdata->duration);
+ return 1;
+ }
+
+ return 0;
+}
+
+static void commitglines(void *arg) {
+ glinebuf *gbuf = (glinebuf *)arg;
+ glinebufcommit(gbuf, 1);
+ free(gbuf);
+}
+
+void gline_free(searchCtx *ctx, struct searchNode *thenode) {
struct gline_localdata *localdata;
nick *np, *nnp;
chanindex *cip, *ncip;
- int i, j, safe=0;
- char msgbuf[512];
+ whowas *ww;
+ int i, j, hits, safe=0, delay;
+ time_t ti = time(NULL);
+ glinebuf *gbuf;
localdata = thenode->localdata;
if (localdata->count > NSMAX_GLINE_LIMIT) {
/* need to warn the user that they have just tried to twat half the network ... */
- controlreply(senderNSExtern, "Warning: your pattern matches too many users (%d) - nothing done.", localdata->count);
+ ctx->reply(senderNSExtern, "Warning: your pattern matches too many users (%d) - nothing done.", localdata->count);
free(localdata);
free(thenode);
return;
}
- if (localdata->type == SEARCHTYPE_CHANNEL) {
+ gbuf = malloc(sizeof(glinebuf));
+ glinebufinit(gbuf, 0);
+
+ if (ctx->searchcmd == reg_chansearch) {
for (i=0;i<CHANNELHASHSIZE;i++) {
for (cip=chantable[i];cip;cip=ncip) {
ncip = cip->next;
continue;
if ((np=getnickbynumeric(cip->channel->users->content[j]))) {
- if (!IsOper(np) && !IsService(np) && !IsXOper(np)) {
- nssnprintf(msgbuf, sizeof(msgbuf), localdata->reason, np);
- if (np->host->clonecount <= NSMAX_GLINE_CLONES)
- irc_send("%s GL * +*@%s %u :%s", mynumeric->content, IPtostr(np->p_ipaddr), localdata->duration, msgbuf);
- else
- irc_send("%s GL * +%s@%s %u :%s", mynumeric->content, np->ident, IPtostr(np->p_ipaddr), localdata->duration, msgbuf);
- }
- else
+ if(!glineuser(gbuf, np, localdata, ti))
safe++;
}
}
}
}
}
- }
- else {
+ } else if (ctx->searchcmd == reg_nicksearch) {
for (i=0;i<NICKHASHSIZE;i++) {
for (np=nicktable[i];np;np=nnp) {
nnp = np->next;
if (np->marker == localdata->marker) {
- if (!IsOper(np) && !IsService(np) && !IsXOper(np)) {
- nssnprintf(msgbuf, sizeof(msgbuf), localdata->reason, np);
- if (np->host->clonecount <= NSMAX_GLINE_CLONES)
- irc_send("%s GL * +*@%s %u :%s", mynumeric->content, IPtostr(np->p_ipaddr), localdata->duration, msgbuf);
- else
- irc_send("%s GL * +%s@%s %u :%s", mynumeric->content, np->ident, IPtostr(np->p_ipaddr), localdata->duration, msgbuf);
- }
- else
- safe++;
+ if(!glineuser(gbuf, np, localdata, ti))
+ safe++;
}
}
}
+ } else {
+ for (i = whowasoffset; i < whowasoffset + whowasmax; i++) {
+ ww = &whowasrecs[i % whowasmax];
+
+ if (ww->type == WHOWAS_UNUSED)
+ continue;
+
+ if (ww->marker == localdata->marker) {
+ if(!glineuser(gbuf, &ww->nick, localdata, ti))
+ safe++;
+ }
+ }
}
+
+ glinebufcounthits(gbuf, &hits, NULL);
+
+ delay = (rand() % (localdata->maxdelay + 1 - localdata->mindelay)) + localdata->mindelay;
+
+ if (delay > 0) {
+ scheduleoneshot(time(NULL) + delay, commitglines, gbuf);
+ } else {
+ commitglines(gbuf);
+ }
+
if (safe)
- controlreply(senderNSExtern, "Warning: your pattern matched privileged users (%d in total) - these have not been touched.", safe);
+ ctx->reply(senderNSExtern, "Warning: your pattern matched privileged users (%d in total) - these have not been touched.", safe);
/* notify opers of the action */
- controlwall(NO_OPER, NL_GLINES, "%s/%s glined %d %s via %s for %s [%d untouched].", senderNSExtern->nick, senderNSExtern->authname, (localdata->count - safe),
- (localdata->count - safe) != 1 ? "users" : "user", (localdata->type == SEARCHTYPE_CHANNEL) ? "chansearch" : "nicksearch", longtoduration(localdata->duration, 1), safe);
+ ctx->wall(NL_GLINES, "%s/%s glined %d %s via %s for %s [%d untouched].", senderNSExtern->nick, senderNSExtern->authname, (localdata->count - safe),
+ (localdata->count - safe) != 1 ? "users" : "user", (ctx->searchcmd == reg_chansearch) ? "chansearch" : "nicksearch", longtoduration(localdata->duration, 1), safe);
free(localdata);
free(thenode);
}