]> jfr.im git - irc/quakenet/newserv.git/blob - newsearch/ns-gline.c
Merge.
[irc/quakenet/newserv.git] / newsearch / ns-gline.c
1 /*
2 * GLINE functionality
3 */
4
5 #include "newsearch.h"
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10
11 #include "../control/control.h"
12 #include "../irc/irc.h" /* irc_send() */
13 #include "../lib/irc_string.h" /* IPtostr(), longtoduration(), durationtolong() */
14 #include "../lib/strlfunc.h"
15
16 /* used for *_free functions that need to warn users of certain things
17 i.e. hitting too many users in a (kill) or (gline) - declared in newsearch.c */
18 extern nick *senderNSExtern;
19 static const char *defaultreason = "You (%u) have been g-lined for violating our terms of service";
20
21 void *gline_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput);
22 void gline_free(searchCtx *ctx, struct searchNode *thenode);
23
24 struct gline_localdata {
25 unsigned int marker;
26 unsigned int duration;
27 int count;
28 int type;
29 char reason[NSMAX_REASON_LEN];
30 };
31
32 struct searchNode *gline_parse(searchCtx *ctx, int type, int argc, char **argv) {
33 struct gline_localdata *localdata;
34 struct searchNode *thenode;
35 int len;
36 char *p;
37
38 if (!(localdata = (struct gline_localdata *) malloc(sizeof(struct gline_localdata)))) {
39 parseError = "malloc: could not allocate memory for this search.";
40 return NULL;
41 }
42 localdata->count = 0;
43 localdata->type = type;
44 if (type == SEARCHTYPE_CHANNEL)
45 localdata->marker = nextchanmarker();
46 else
47 localdata->marker = nextnickmarker();
48
49 switch (argc) {
50 case 0:
51 localdata->duration = NSGLINE_DURATION;
52 strlcpy(localdata->reason, defaultreason, sizeof(localdata->reason));
53 break;
54
55 case 1:
56 if (strchr(argv[0], ' ') == NULL) { /* duration specified */
57 localdata->duration = durationtolong(argv[0]);
58 /* error checking on gline duration */
59 if (localdata->duration == 0)
60 localdata->duration = NSGLINE_DURATION;
61 strlcpy(localdata->reason, defaultreason, sizeof(localdata->reason));
62 }
63 else { /* reason specified */
64 localdata->duration = NSGLINE_DURATION;
65
66 p = argv[0];
67 if(*p == '\"')
68 p++;
69 len = strlcpy(localdata->reason, p, sizeof(localdata->reason));
70 if(len >= sizeof(localdata->reason)) {
71 localdata->reason[sizeof(localdata->reason)-1] = '\0';
72 } else {
73 localdata->reason[len-1] = '\0';
74 }
75 }
76 break;
77
78 case 2:
79 localdata->duration = durationtolong(argv[0]);
80 /* error checking on gline duration */
81 if (localdata->duration == 0)
82 localdata->duration = NSGLINE_DURATION;
83
84 p = argv[1];
85 if(*p == '\"')
86 p++;
87 len = strlcpy(localdata->reason, p, sizeof(localdata->reason));
88 if(len >= sizeof(localdata->reason)) {
89 localdata->reason[sizeof(localdata->reason)-1] = '\0';
90 } else {
91 localdata->reason[len-1] = '\0';
92 }
93
94 break;
95 default:
96 free(localdata);
97 parseError = "gline: invalid number of arguments";
98 return NULL;
99 }
100
101 if (!(thenode=(struct searchNode *)malloc(sizeof (struct searchNode)))) {
102 /* couldn't malloc() memory for thenode, so free localdata to avoid leakage */
103 parseError = "malloc: could not allocate memory for this search.";
104 free(localdata);
105 return NULL;
106 }
107
108 thenode->returntype = RETURNTYPE_BOOL;
109 thenode->localdata = localdata;
110 thenode->exe = gline_exe;
111 thenode->free = gline_free;
112
113 return thenode;
114 }
115
116 void *gline_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
117 struct gline_localdata *localdata;
118 nick *np;
119 chanindex *cip;
120
121 localdata = thenode->localdata;
122
123 if (localdata->type == SEARCHTYPE_CHANNEL) {
124 cip = (chanindex *)theinput;
125 cip->marker = localdata->marker;
126 localdata->count += cip->channel->users->totalusers;
127 }
128 else {
129 np = (nick *)theinput;
130 np->marker = localdata->marker;
131 localdata->count++;
132 }
133
134 return (void *)1;
135 }
136
137 void gline_free(searchCtx *ctx, struct searchNode *thenode) {
138 struct gline_localdata *localdata;
139 nick *np, *nnp;
140 chanindex *cip, *ncip;
141 int i, j, safe=0;
142 char msgbuf[512];
143 time_t ti = time(NULL);
144
145 localdata = thenode->localdata;
146
147 if (localdata->count > NSMAX_GLINE_LIMIT) {
148 /* need to warn the user that they have just tried to twat half the network ... */
149 ctx->reply(senderNSExtern, "Warning: your pattern matches too many users (%d) - nothing done.", localdata->count);
150 free(localdata);
151 free(thenode);
152 return;
153 }
154
155 if (localdata->type == SEARCHTYPE_CHANNEL) {
156 for (i=0;i<CHANNELHASHSIZE;i++) {
157 for (cip=chantable[i];cip;cip=ncip) {
158 ncip = cip->next;
159 if (cip != NULL && cip->channel != NULL && cip->marker == localdata->marker) {
160 for (j=0;j<cip->channel->users->hashsize;j++) {
161 if (cip->channel->users->content[j]==nouser)
162 continue;
163
164 if ((np=getnickbynumeric(cip->channel->users->content[j]))) {
165 if (!IsOper(np) && !IsService(np) && !IsXOper(np)) {
166 nssnprintf(msgbuf, sizeof(msgbuf), localdata->reason, np);
167 if (np->host->clonecount <= NSMAX_GLINE_CLONES)
168 irc_send("%s GL * +*@%s %u %d :%s", mynumeric->content, IPtostr(np->p_ipaddr), localdata->duration, ti, msgbuf);
169 else
170 irc_send("%s GL * +%s@%s %u %d :%s", mynumeric->content, np->ident, IPtostr(np->p_ipaddr), localdata->duration, ti, msgbuf);
171 }
172 else
173 safe++;
174 }
175 }
176 }
177 }
178 }
179 }
180 else {
181 for (i=0;i<NICKHASHSIZE;i++) {
182 for (np=nicktable[i];np;np=nnp) {
183 nnp = np->next;
184 if (np->marker == localdata->marker) {
185 if (!IsOper(np) && !IsService(np) && !IsXOper(np)) {
186 nssnprintf(msgbuf, sizeof(msgbuf), localdata->reason, np);
187 if (np->host->clonecount <= NSMAX_GLINE_CLONES)
188 irc_send("%s GL * +*@%s %u %d :%s", mynumeric->content, IPtostr(np->p_ipaddr), localdata->duration, ti, msgbuf);
189 else
190 irc_send("%s GL * +%s@%s %u %d :%s", mynumeric->content, np->ident, IPtostr(np->p_ipaddr), localdata->duration, ti, msgbuf);
191 }
192 else
193 safe++;
194 }
195 }
196 }
197 }
198 if (safe)
199 ctx->reply(senderNSExtern, "Warning: your pattern matched privileged users (%d in total) - these have not been touched.", safe);
200 /* notify opers of the action */
201 ctx->wall(NL_GLINES, "%s/%s glined %d %s via %s for %s [%d untouched].", senderNSExtern->nick, senderNSExtern->authname, (localdata->count - safe),
202 (localdata->count - safe) != 1 ? "users" : "user", (localdata->type == SEARCHTYPE_CHANNEL) ? "chansearch" : "nicksearch", longtoduration(localdata->duration, 1), safe);
203 free(localdata);
204 free(thenode);
205 }