]> jfr.im git - irc/quakenet/newserv.git/blame - newsearch/ns-kill.c
SearchCtx should contain 'type' - this is to make life easier when defining new searc...
[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;
96429168 32 int len;
4278cc14 33
9ce4f0be
IB
34 if (!(localdata = (struct kill_localdata *) malloc(sizeof(struct kill_localdata)))) {
35 parseError = "malloc: could not allocate memory for this search.";
36 return NULL;
37 }
4278cc14 38 localdata->count = 0;
f33f3f52 39 if (ctx->type == SEARCHTYPE_CHANNEL)
8e257015
IB
40 localdata->marker = nextchanmarker();
41 else
42 localdata->marker = nextnickmarker();
4278cc14 43
96429168 44 if (argc==1) {
2ba836f2
CP
45 char *p = argv[0];
46 if(*p == '\"')
7759d816 47 p++;
2ba836f2
CP
48 len = strlcpy(localdata->reason, p, sizeof(localdata->reason));
49 if(len >= sizeof(localdata->reason)) {
50 localdata->reason[sizeof(localdata->reason)-1] = '\0';
51 } else {
52 localdata->reason[len-1] = '\0';
53 }
96429168
IB
54 }
55 else
2ba836f2 56 strlcpy(localdata->reason, defaultreason, sizeof(localdata->reason));
96429168 57
9ce4f0be
IB
58 if (!(thenode=(struct searchNode *)malloc(sizeof (struct searchNode)))) {
59 /* couldn't malloc() memory for thenode, so free localdata to avoid leakage */
60 parseError = "malloc: could not allocate memory for this search.";
61 free(localdata);
62 return NULL;
63 }
4278cc14
IB
64
65 thenode->returntype = RETURNTYPE_BOOL;
66 thenode->localdata = localdata;
67 thenode->exe = kill_exe;
68 thenode->free = kill_free;
69
70 return thenode;
71}
72
c8be5183 73void *kill_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
4278cc14 74 struct kill_localdata *localdata;
8e257015
IB
75 nick *np;
76 chanindex *cip;
4278cc14
IB
77
78 localdata = thenode->localdata;
79
f33f3f52 80 if (ctx->type == SEARCHTYPE_CHANNEL) {
8e257015
IB
81 cip = (chanindex *)theinput;
82 cip->marker = localdata->marker;
1545f345 83 localdata->count += cip->channel->users->totalusers;
8e257015
IB
84 }
85 else {
86 np = (nick *)theinput;
87 np->marker = localdata->marker;
88 localdata->count++;
89 }
4278cc14 90
c7f7a584 91 return (void *)1;
4278cc14
IB
92}
93
c8be5183 94void kill_free(searchCtx *ctx, struct searchNode *thenode) {
4278cc14
IB
95 struct kill_localdata *localdata;
96 nick *np, *nnp;
82b7019b 97 chanindex *cip;
8e257015 98 int i, j, safe=0;
42458fa5 99 unsigned int nickmarker;
2ba836f2 100 char msgbuf[512];
4278cc14
IB
101
102 localdata = thenode->localdata;
103
104 if (localdata->count > NSMAX_KILL_LIMIT) {
105 /* need to warn the user that they have just tried to twat half the network ... */
0da2a4ae 106 ctx->reply(senderNSExtern, "Warning: your pattern matches too many users (%d) - nothing done.", localdata->count);
4278cc14
IB
107 free(localdata);
108 free(thenode);
109 return;
110 }
111
82b7019b 112 /* For channel searches, mark up all the nicks in the relevant channels first */
f33f3f52 113 if (ctx->type == SEARCHTYPE_CHANNEL) {
42458fa5 114 nickmarker=nextnickmarker();
8e257015 115 for (i=0;i<CHANNELHASHSIZE;i++) {
82b7019b 116 for (cip=chantable[i];cip;cip=cip->next) {
117 /* Skip empty and non-matching channels */
118 if (!cip->channel || cip->marker != localdata->marker)
119 continue;
120
121 for (j=0;j<cip->channel->users->hashsize;j++) {
122 if (cip->channel->users->content[j]==nouser)
123 continue;
124
125 if ((np=getnickbynumeric(cip->channel->users->content[j])))
126 np->marker=nickmarker;
2ba836f2 127 }
42458fa5
P
128 }
129 }
82b7019b 130 } else {
131 /* For nick searches they're already marked, pick up the saved value */
132 nickmarker=localdata->marker;
8e257015 133 }
82b7019b 134
135 /* Now do the actual kills */
136 for (i=0;i<NICKHASHSIZE;i++) {
137 for (np=nicktable[i];np;np=nnp) {
138 nnp = np->next;
139
140 if (np->marker != nickmarker)
141 continue;
142
143 if (IsOper(np) || IsService(np) || IsXOper(np)) {
144 safe++;
145 continue;
4278cc14 146 }
82b7019b 147
148 nssnprintf(msgbuf, sizeof(msgbuf), localdata->reason, np);
149 killuser(NULL, np, "%s", msgbuf);
4278cc14
IB
150 }
151 }
82b7019b 152
4278cc14 153 if (safe)
0da2a4ae 154 ctx->reply(senderNSExtern, "Warning: your pattern matched privileged users (%d in total) - these have not been touched.", safe);
f16cabe4 155 /* notify opers of the action */
0da2a4ae 156 ctx->wall(NL_KICKKILLS, "%s/%s killed %d %s via %s [%d untouched].", senderNSExtern->nick, senderNSExtern->authname, (localdata->count - safe),
f33f3f52 157 (localdata->count - safe) != 1 ? "users" : "user", (ctx->type == SEARCHTYPE_CHANNEL) ? "chansearch" : "nicksearch", safe);
4278cc14
IB
158 free(localdata);
159 free(thenode);
160}