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