]>
Commit | Line | Data |
---|---|---|
4278cc14 IB |
1 | /* |
2 | * GLINE functionality | |
3 | */ | |
4 | ||
5 | #include "newsearch.h" | |
6 | ||
7 | #include <stdio.h> | |
8 | #include <stdlib.h> | |
96429168 | 9 | #include <string.h> |
4278cc14 | 10 | |
0da2a4ae | 11 | #include "../control/control.h" |
4278cc14 | 12 | #include "../irc/irc.h" /* irc_send() */ |
f16cabe4 | 13 | #include "../lib/irc_string.h" /* IPtostr(), longtoduration(), durationtolong() */ |
2ba836f2 | 14 | #include "../lib/strlfunc.h" |
4278cc14 IB |
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 */ | |
219d27f1 | 18 | extern nick *senderNSExtern; |
2ba836f2 | 19 | static const char *defaultreason = "You (%u) have been g-lined for violating our terms of service"; |
4278cc14 | 20 | |
c8be5183 CP |
21 | void *gline_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput); |
22 | void gline_free(searchCtx *ctx, struct searchNode *thenode); | |
4278cc14 IB |
23 | |
24 | struct gline_localdata { | |
25 | unsigned int marker; | |
f16cabe4 | 26 | unsigned int duration; |
4278cc14 | 27 | int count; |
8e257015 | 28 | int type; |
96429168 | 29 | char reason[NSMAX_REASON_LEN]; |
4278cc14 IB |
30 | }; |
31 | ||
c8be5183 | 32 | struct searchNode *gline_parse(searchCtx *ctx, int type, int argc, char **argv) { |
4278cc14 IB |
33 | struct gline_localdata *localdata; |
34 | struct searchNode *thenode; | |
96429168 | 35 | int len; |
2ba836f2 | 36 | char *p; |
4278cc14 | 37 | |
9ce4f0be IB |
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 | } | |
4278cc14 | 42 | localdata->count = 0; |
8e257015 IB |
43 | localdata->type = type; |
44 | if (type == SEARCHTYPE_CHANNEL) | |
45 | localdata->marker = nextchanmarker(); | |
46 | else | |
47 | localdata->marker = nextnickmarker(); | |
4278cc14 | 48 | |
96429168 IB |
49 | switch (argc) { |
50 | case 0: | |
f16cabe4 | 51 | localdata->duration = NSGLINE_DURATION; |
2ba836f2 | 52 | strlcpy(localdata->reason, defaultreason, sizeof(localdata->reason)); |
96429168 IB |
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; | |
2ba836f2 | 61 | strlcpy(localdata->reason, defaultreason, sizeof(localdata->reason)); |
96429168 IB |
62 | } |
63 | else { /* reason specified */ | |
64 | localdata->duration = NSGLINE_DURATION; | |
2ba836f2 CP |
65 | |
66 | p = argv[0]; | |
67 | if(*p == '\"') | |
7759d816 | 68 | p++; |
2ba836f2 CP |
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 | } | |
96429168 IB |
75 | } |
76 | break; | |
77 | ||
78 | case 2: | |
f16cabe4 IB |
79 | localdata->duration = durationtolong(argv[0]); |
80 | /* error checking on gline duration */ | |
81 | if (localdata->duration == 0) | |
82 | localdata->duration = NSGLINE_DURATION; | |
96429168 | 83 | |
2ba836f2 CP |
84 | p = argv[1]; |
85 | if(*p == '\"') | |
5b417a78 | 86 | p++; |
2ba836f2 CP |
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; | |
96429168 IB |
95 | default: |
96 | free(localdata); | |
97 | parseError = "gline: invalid number of arguments"; | |
98 | return NULL; | |
f16cabe4 IB |
99 | } |
100 | ||
9ce4f0be IB |
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 | } | |
4278cc14 IB |
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 | ||
c8be5183 | 116 | void *gline_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) { |
4278cc14 | 117 | struct gline_localdata *localdata; |
8e257015 IB |
118 | nick *np; |
119 | chanindex *cip; | |
4278cc14 IB |
120 | |
121 | localdata = thenode->localdata; | |
122 | ||
8e257015 IB |
123 | if (localdata->type == SEARCHTYPE_CHANNEL) { |
124 | cip = (chanindex *)theinput; | |
125 | cip->marker = localdata->marker; | |
1545f345 | 126 | localdata->count += cip->channel->users->totalusers; |
8e257015 IB |
127 | } |
128 | else { | |
129 | np = (nick *)theinput; | |
130 | np->marker = localdata->marker; | |
131 | localdata->count++; | |
132 | } | |
4278cc14 | 133 | |
c7f7a584 | 134 | return (void *)1; |
4278cc14 IB |
135 | } |
136 | ||
c8be5183 | 137 | void gline_free(searchCtx *ctx, struct searchNode *thenode) { |
4278cc14 IB |
138 | struct gline_localdata *localdata; |
139 | nick *np, *nnp; | |
8e257015 IB |
140 | chanindex *cip, *ncip; |
141 | int i, j, safe=0; | |
2ba836f2 | 142 | char msgbuf[512]; |
aace33dc | 143 | time_t ti = time(NULL); |
4278cc14 IB |
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 ... */ | |
0da2a4ae | 149 | ctx->reply(senderNSExtern, "Warning: your pattern matches too many users (%d) - nothing done.", localdata->count); |
4278cc14 IB |
150 | free(localdata); |
151 | free(thenode); | |
152 | return; | |
153 | } | |
154 | ||
8e257015 IB |
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)) { | |
2ba836f2 | 166 | nssnprintf(msgbuf, sizeof(msgbuf), localdata->reason, np); |
8e257015 | 167 | if (np->host->clonecount <= NSMAX_GLINE_CLONES) |
aace33dc | 168 | irc_send("%s GL * +*@%s %u %d :%s", mynumeric->content, IPtostr(np->p_ipaddr), localdata->duration, ti, msgbuf); |
8e257015 | 169 | else |
aace33dc | 170 | irc_send("%s GL * +%s@%s %u %d :%s", mynumeric->content, np->ident, IPtostr(np->p_ipaddr), localdata->duration, ti, msgbuf); |
2ba836f2 CP |
171 | } |
172 | else | |
173 | safe++; | |
8e257015 IB |
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)) { | |
2ba836f2 | 186 | nssnprintf(msgbuf, sizeof(msgbuf), localdata->reason, np); |
8e257015 | 187 | if (np->host->clonecount <= NSMAX_GLINE_CLONES) |
aace33dc | 188 | irc_send("%s GL * +*@%s %u %d :%s", mynumeric->content, IPtostr(np->p_ipaddr), localdata->duration, ti, msgbuf); |
8e257015 | 189 | else |
aace33dc | 190 | irc_send("%s GL * +%s@%s %u %d :%s", mynumeric->content, np->ident, IPtostr(np->p_ipaddr), localdata->duration, ti, msgbuf); |
8e257015 | 191 | } |
4278cc14 | 192 | else |
8e257015 | 193 | safe++; |
4278cc14 | 194 | } |
4278cc14 IB |
195 | } |
196 | } | |
197 | } | |
198 | if (safe) | |
0da2a4ae | 199 | ctx->reply(senderNSExtern, "Warning: your pattern matched privileged users (%d in total) - these have not been touched.", safe); |
f16cabe4 | 200 | /* notify opers of the action */ |
0da2a4ae | 201 | ctx->wall(NL_GLINES, "%s/%s glined %d %s via %s for %s [%d untouched].", senderNSExtern->nick, senderNSExtern->authname, (localdata->count - safe), |
8e257015 | 202 | (localdata->count - safe) != 1 ? "users" : "user", (localdata->type == SEARCHTYPE_CHANNEL) ? "chansearch" : "nicksearch", longtoduration(localdata->duration, 1), safe); |
4278cc14 IB |
203 | free(localdata); |
204 | free(thenode); | |
205 | } |