]> jfr.im git - irc/quakenet/newserv.git/blame - newsearch/ns-kill.c
merge
[irc/quakenet/newserv.git] / newsearch / ns-kill.c
CommitLineData
4278cc14
IB
1/*
2 * KILL functionality
3 */
4
5#include "newsearch.h"
6
7#include <stdio.h>
8#include <stdlib.h>
9
0da2a4ae 10#include "../control/control.h"
4278cc14
IB
11#include "../localuser/localuser.h" /* killuser() */
12#include "../lib/irc_string.h" /* IPtostr() */
2ba836f2 13#include "../lib/strlfunc.h"
4278cc14
IB
14
15/* used for *_free functions that need to warn users of certain things
16 i.e. hitting too many users in a (kill) or (gline) - declared in newsearch.c */
219d27f1 17extern nick *senderNSExtern;
4278cc14 18
c8be5183
CP
19void *kill_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput);
20void kill_free(searchCtx *ctx, struct searchNode *thenode);
a2646788 21static const char *defaultreason = "You (%n) have been disconnected for violating our terms of service";
4278cc14
IB
22
23struct kill_localdata {
24 unsigned int marker;
25 int count;
96429168 26 char reason[NSMAX_REASON_LEN];
4278cc14
IB
27};
28
f33f3f52 29struct searchNode *kill_parse(searchCtx *ctx, int argc, char **argv) {
4278cc14
IB
30 struct kill_localdata *localdata;
31 struct searchNode *thenode;
32
9ce4f0be
IB
33 if (!(localdata = (struct kill_localdata *) malloc(sizeof(struct kill_localdata)))) {
34 parseError = "malloc: could not allocate memory for this search.";
35 return NULL;
36 }
4278cc14 37 localdata->count = 0;
a92bb8e1 38 if (ctx->searchcmd == reg_chansearch)
8e257015 39 localdata->marker = nextchanmarker();
a92bb8e1 40 else if (ctx->searchcmd == reg_nicksearch)
8e257015 41 localdata->marker = nextnickmarker();
a92bb8e1 42 else {
31686847 43 free(localdata);
a92bb8e1
P
44 parseError = "kill: invalid search type";
45 return NULL;
46 }
4278cc14 47
31686847
CP
48 if (argc==1) {
49 struct searchNode *reasonstr;
50 char *p;
51 if (!(reasonstr=argtoconststr("kill", ctx, argv[0], &p))) {
52 free(localdata);
53 return NULL;
54 }
55
56 strlcpy(localdata->reason, p, sizeof(localdata->reason));
57 reasonstr->free(ctx, reasonstr);
58 } else {
2ba836f2 59 strlcpy(localdata->reason, defaultreason, sizeof(localdata->reason));
31686847
CP
60 }
61
9ce4f0be
IB
62 if (!(thenode=(struct searchNode *)malloc(sizeof (struct searchNode)))) {
63 /* couldn't malloc() memory for thenode, so free localdata to avoid leakage */
64 parseError = "malloc: could not allocate memory for this search.";
65 free(localdata);
66 return NULL;
67 }
4278cc14
IB
68
69 thenode->returntype = RETURNTYPE_BOOL;
70 thenode->localdata = localdata;
71 thenode->exe = kill_exe;
72 thenode->free = kill_free;
73
74 return thenode;
75}
76
c8be5183 77void *kill_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
4278cc14 78 struct kill_localdata *localdata;
8e257015
IB
79 nick *np;
80 chanindex *cip;
4278cc14
IB
81
82 localdata = thenode->localdata;
83
a92bb8e1 84 if (ctx->searchcmd == reg_chansearch) {
8e257015
IB
85 cip = (chanindex *)theinput;
86 cip->marker = localdata->marker;
1545f345 87 localdata->count += cip->channel->users->totalusers;
a92bb8e1 88 } else {
8e257015
IB
89 np = (nick *)theinput;
90 np->marker = localdata->marker;
91 localdata->count++;
92 }
4278cc14 93
c7f7a584 94 return (void *)1;
4278cc14
IB
95}
96
c8be5183 97void kill_free(searchCtx *ctx, struct searchNode *thenode) {
4278cc14
IB
98 struct kill_localdata *localdata;
99 nick *np, *nnp;
82b7019b 100 chanindex *cip;
8e257015 101 int i, j, safe=0;
42458fa5 102 unsigned int nickmarker;
2ba836f2 103 char msgbuf[512];
4278cc14
IB
104
105 localdata = thenode->localdata;
106
107 if (localdata->count > NSMAX_KILL_LIMIT) {
108 /* need to warn the user that they have just tried to twat half the network ... */
0da2a4ae 109 ctx->reply(senderNSExtern, "Warning: your pattern matches too many users (%d) - nothing done.", localdata->count);
4278cc14
IB
110 free(localdata);
111 free(thenode);
112 return;
113 }
114
82b7019b 115 /* For channel searches, mark up all the nicks in the relevant channels first */
a92bb8e1 116 if (ctx->searchcmd == reg_chansearch) {
42458fa5 117 nickmarker=nextnickmarker();
8e257015 118 for (i=0;i<CHANNELHASHSIZE;i++) {
82b7019b 119 for (cip=chantable[i];cip;cip=cip->next) {
120 /* Skip empty and non-matching channels */
121 if (!cip->channel || cip->marker != localdata->marker)
122 continue;
123
124 for (j=0;j<cip->channel->users->hashsize;j++) {
125 if (cip->channel->users->content[j]==nouser)
126 continue;
127
128 if ((np=getnickbynumeric(cip->channel->users->content[j])))
129 np->marker=nickmarker;
2ba836f2 130 }
42458fa5
P
131 }
132 }
82b7019b 133 } else {
134 /* For nick searches they're already marked, pick up the saved value */
135 nickmarker=localdata->marker;
8e257015 136 }
82b7019b 137
138 /* Now do the actual kills */
139 for (i=0;i<NICKHASHSIZE;i++) {
140 for (np=nicktable[i];np;np=nnp) {
141 nnp = np->next;
142
143 if (np->marker != nickmarker)
144 continue;
145
146 if (IsOper(np) || IsService(np) || IsXOper(np)) {
147 safe++;
148 continue;
4278cc14 149 }
82b7019b 150
151 nssnprintf(msgbuf, sizeof(msgbuf), localdata->reason, np);
152 killuser(NULL, np, "%s", msgbuf);
4278cc14
IB
153 }
154 }
82b7019b 155
4278cc14 156 if (safe)
0da2a4ae 157 ctx->reply(senderNSExtern, "Warning: your pattern matched privileged users (%d in total) - these have not been touched.", safe);
f16cabe4 158 /* notify opers of the action */
0da2a4ae 159 ctx->wall(NL_KICKKILLS, "%s/%s killed %d %s via %s [%d untouched].", senderNSExtern->nick, senderNSExtern->authname, (localdata->count - safe),
a92bb8e1 160 (localdata->count - safe) != 1 ? "users" : "user", (ctx->searchcmd == reg_chansearch) ? "chansearch" : "nicksearch", safe);
4278cc14
IB
161 free(localdata);
162 free(thenode);
163}