]> jfr.im git - irc/quakenet/newserv.git/blob - newsearch/ns-kill.c
6f8b474c73b832b89e6462a7b6bb3171b8cf7bee
[irc/quakenet/newserv.git] / newsearch / ns-kill.c
1 /*
2 * KILL functionality
3 */
4
5 #include "newsearch.h"
6
7 #include <stdio.h>
8 #include <stdlib.h>
9
10 #include "../control/control.h"
11 #include "../localuser/localuser.h" /* killuser() */
12 #include "../lib/irc_string.h" /* IPtostr() */
13 #include "../lib/strlfunc.h"
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 */
17 extern nick *senderNSExtern;
18
19 void *kill_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput);
20 void kill_free(searchCtx *ctx, struct searchNode *thenode);
21 static const char *defaultreason = "You (%n) have been disconnected for violating our terms of service";
22
23 struct kill_localdata {
24 unsigned int marker;
25 int count;
26 char reason[NSMAX_REASON_LEN];
27 };
28
29 struct searchNode *kill_parse(searchCtx *ctx, int argc, char **argv) {
30 struct kill_localdata *localdata;
31 struct searchNode *thenode;
32 int len;
33
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 }
38 localdata->count = 0;
39 if (ctx->type == SEARCHTYPE_CHANNEL)
40 localdata->marker = nextchanmarker();
41 else
42 localdata->marker = nextnickmarker();
43
44 if (argc==1) {
45 char *p = argv[0];
46 if(*p == '\"')
47 p++;
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 }
54 }
55 else
56 strlcpy(localdata->reason, defaultreason, sizeof(localdata->reason));
57
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 }
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
73 void *kill_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
74 struct kill_localdata *localdata;
75 nick *np;
76 chanindex *cip;
77
78 localdata = thenode->localdata;
79
80 if (ctx->type == SEARCHTYPE_CHANNEL) {
81 cip = (chanindex *)theinput;
82 cip->marker = localdata->marker;
83 localdata->count += cip->channel->users->totalusers;
84 }
85 else {
86 np = (nick *)theinput;
87 np->marker = localdata->marker;
88 localdata->count++;
89 }
90
91 return (void *)1;
92 }
93
94 void kill_free(searchCtx *ctx, struct searchNode *thenode) {
95 struct kill_localdata *localdata;
96 nick *np, *nnp;
97 chanindex *cip;
98 int i, j, safe=0;
99 unsigned int nickmarker;
100 char msgbuf[512];
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 ... */
106 ctx->reply(senderNSExtern, "Warning: your pattern matches too many users (%d) - nothing done.", localdata->count);
107 free(localdata);
108 free(thenode);
109 return;
110 }
111
112 /* For channel searches, mark up all the nicks in the relevant channels first */
113 if (ctx->type == SEARCHTYPE_CHANNEL) {
114 nickmarker=nextnickmarker();
115 for (i=0;i<CHANNELHASHSIZE;i++) {
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;
127 }
128 }
129 }
130 } else {
131 /* For nick searches they're already marked, pick up the saved value */
132 nickmarker=localdata->marker;
133 }
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;
146 }
147
148 nssnprintf(msgbuf, sizeof(msgbuf), localdata->reason, np);
149 killuser(NULL, np, "%s", msgbuf);
150 }
151 }
152
153 if (safe)
154 ctx->reply(senderNSExtern, "Warning: your pattern matched privileged users (%d in total) - these have not been touched.", safe);
155 /* notify opers of the action */
156 ctx->wall(NL_KICKKILLS, "%s/%s killed %d %s via %s [%d untouched].", senderNSExtern->nick, senderNSExtern->authname, (localdata->count - safe),
157 (localdata->count - safe) != 1 ? "users" : "user", (ctx->type == SEARCHTYPE_CHANNEL) ? "chansearch" : "nicksearch", safe);
158 free(localdata);
159 free(thenode);
160 }