]> jfr.im git - irc/freenode/solanum.git/blob - modules/core/m_mode.c
chmode: end the grace period more intelligently (#84)
[irc/freenode/solanum.git] / modules / core / m_mode.c
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * m_mode.c: Sets a user or channel mode.
4 *
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
23 */
24
25 #include "stdinc.h"
26 #include "channel.h"
27 #include "client.h"
28 #include "hash.h"
29 #include "match.h"
30 #include "ircd.h"
31 #include "numeric.h"
32 #include "s_user.h"
33 #include "s_conf.h"
34 #include "s_serv.h"
35 #include "logger.h"
36 #include "send.h"
37 #include "msg.h"
38 #include "parse.h"
39 #include "modules.h"
40 #include "packet.h"
41 #include "s_newconf.h"
42
43 static const char mode_desc[] =
44 "Provides the MODE and MLOCK client and server commands, and TS6 server-to-server TMODE and BMASK commands";
45
46 static void m_mode(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
47 static void ms_mode(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
48 static void ms_tmode(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
49 static void ms_mlock(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
50 static void ms_bmask(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
51
52 struct Message mode_msgtab = {
53 "MODE", 0, 0, 0, 0,
54 {mg_unreg, {m_mode, 2}, {m_mode, 3}, {ms_mode, 3}, mg_ignore, {m_mode, 2}}
55 };
56 struct Message tmode_msgtab = {
57 "TMODE", 0, 0, 0, 0,
58 {mg_ignore, mg_ignore, {ms_tmode, 4}, {ms_tmode, 4}, mg_ignore, mg_ignore}
59 };
60 struct Message mlock_msgtab = {
61 "MLOCK", 0, 0, 0, 0,
62 {mg_ignore, mg_ignore, {ms_mlock, 3}, {ms_mlock, 3}, mg_ignore, mg_ignore}
63 };
64 struct Message bmask_msgtab = {
65 "BMASK", 0, 0, 0, 0,
66 {mg_ignore, mg_ignore, mg_ignore, {ms_bmask, 5}, mg_ignore, mg_ignore}
67 };
68
69 mapi_clist_av1 mode_clist[] = { &mode_msgtab, &tmode_msgtab, &mlock_msgtab, &bmask_msgtab, NULL };
70
71 DECLARE_MODULE_AV2(mode, NULL, NULL, mode_clist, NULL, NULL, NULL, NULL, mode_desc);
72
73 /*
74 * m_mode - MODE command handler
75 * parv[1] - channel
76 */
77 static void
78 m_mode(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
79 {
80 struct Channel *chptr = NULL;
81 struct membership *msptr;
82 int n = 2;
83 const char *dest;
84 int operspy = 0;
85
86 dest = parv[1];
87
88 if(IsOperSpy(source_p) && *dest == '!')
89 {
90 dest++;
91 operspy = 1;
92
93 if(EmptyString(dest))
94 {
95 sendto_one(source_p, form_str(ERR_NEEDMOREPARAMS),
96 me.name, source_p->name, "MODE");
97 return;
98 }
99 }
100
101 /* Now, try to find the channel in question */
102 if(!IsChanPrefix(*dest))
103 {
104 /* if here, it has to be a non-channel name */
105 user_mode(client_p, source_p, parc, parv);
106 return;
107 }
108
109 if(!check_channel_name(dest))
110 {
111 sendto_one_numeric(source_p, ERR_BADCHANNAME, form_str(ERR_BADCHANNAME), parv[1]);
112 return;
113 }
114
115 chptr = find_channel(dest);
116
117 if(chptr == NULL)
118 {
119 sendto_one_numeric(source_p, ERR_NOSUCHCHANNEL,
120 form_str(ERR_NOSUCHCHANNEL), parv[1]);
121 return;
122 }
123
124 /* Now know the channel exists */
125 if(parc < n + 1)
126 {
127 if(operspy)
128 report_operspy(source_p, "MODE", chptr->chname);
129
130 sendto_one(source_p, form_str(RPL_CHANNELMODEIS),
131 me.name, source_p->name, parv[1],
132 operspy ? channel_modes(chptr, &me) : channel_modes(chptr, source_p));
133
134 sendto_one(source_p, form_str(RPL_CREATIONTIME),
135 me.name, source_p->name, parv[1], chptr->channelts);
136 }
137 else
138 {
139 msptr = find_channel_membership(chptr, source_p);
140
141 set_channel_mode(client_p, source_p, chptr, msptr, parc - n, parv + n);
142 }
143 }
144
145 static void
146 ms_mode(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
147 {
148 struct Channel *chptr;
149
150 chptr = find_channel(parv[1]);
151
152 if(chptr == NULL)
153 {
154 sendto_one_numeric(source_p, ERR_NOSUCHCHANNEL,
155 form_str(ERR_NOSUCHCHANNEL), parv[1]);
156 return;
157 }
158
159 set_channel_mode(client_p, source_p, chptr, NULL, parc - 2, parv + 2);
160 }
161
162 static void
163 ms_tmode(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
164 {
165 struct Channel *chptr = NULL;
166 struct membership *msptr;
167
168 /* Now, try to find the channel in question */
169 if(!IsChanPrefix(parv[2][0]) || !check_channel_name(parv[2]))
170 {
171 sendto_one_numeric(source_p, ERR_BADCHANNAME, form_str(ERR_BADCHANNAME), parv[2]);
172 return;
173 }
174
175 chptr = find_channel(parv[2]);
176
177 if(chptr == NULL)
178 {
179 sendto_one_numeric(source_p, ERR_NOSUCHCHANNEL,
180 form_str(ERR_NOSUCHCHANNEL), parv[2]);
181 return;
182 }
183
184 /* TS is higher, drop it. */
185 if(atol(parv[1]) > chptr->channelts)
186 return;
187
188 if(IsServer(source_p))
189 {
190 set_channel_mode(client_p, source_p, chptr, NULL, parc - 3, parv + 3);
191 }
192 else
193 {
194 msptr = find_channel_membership(chptr, source_p);
195
196 set_channel_mode(client_p, source_p, chptr, msptr, parc - 3, parv + 3);
197 }
198 }
199
200 static void
201 ms_mlock(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
202 {
203 struct Channel *chptr = NULL;
204
205 /* Now, try to find the channel in question */
206 if(!IsChanPrefix(parv[2][0]) || !check_channel_name(parv[2]))
207 {
208 sendto_one_numeric(source_p, ERR_BADCHANNAME, form_str(ERR_BADCHANNAME), parv[2]);
209 return;
210 }
211
212 chptr = find_channel(parv[2]);
213
214 if(chptr == NULL)
215 {
216 sendto_one_numeric(source_p, ERR_NOSUCHCHANNEL,
217 form_str(ERR_NOSUCHCHANNEL), parv[2]);
218 return;
219 }
220
221 /* TS is higher, drop it. */
222 if(atol(parv[1]) > chptr->channelts)
223 return;
224
225 if(IsServer(source_p))
226 set_channel_mlock(client_p, source_p, chptr, parv[3], true);
227 }
228
229 static void
230 possibly_remove_lower_forward(struct Client *fakesource_p, int mems,
231 struct Channel *chptr, rb_dlink_list *banlist, int mchar,
232 const char *mask, const char *forward)
233 {
234 struct Ban *actualBan;
235 rb_dlink_node *ptr;
236
237 RB_DLINK_FOREACH(ptr, banlist->head)
238 {
239 actualBan = ptr->data;
240 if(!irccmp(actualBan->banstr, mask) &&
241 (actualBan->forward == NULL ||
242 irccmp(actualBan->forward, forward) < 0))
243 {
244 sendto_channel_local(fakesource_p, mems, chptr, ":%s MODE %s -%c %s%s%s",
245 fakesource_p->name,
246 chptr->chname,
247 mchar,
248 actualBan->banstr,
249 actualBan->forward ? "$" : "",
250 actualBan->forward ? actualBan->forward : "");
251 rb_dlinkDelete(&actualBan->node, banlist);
252 free_ban(actualBan);
253 return;
254 }
255 }
256 }
257
258 static void
259 ms_bmask(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
260 {
261 static char modebuf[BUFSIZE];
262 static char parabuf[BUFSIZE];
263 struct Channel *chptr;
264 rb_dlink_list *banlist;
265 char *s, *forward;
266 char *t;
267 char *mbuf;
268 char *pbuf;
269 long mode_type;
270 int mlen;
271 int plen = 0;
272 int tlen;
273 int arglen;
274 int modecount = 0;
275 int needcap = NOCAPS;
276 int mems;
277 struct Client *fakesource_p;
278
279 if(!IsChanPrefix(parv[2][0]) || !check_channel_name(parv[2]))
280 return;
281
282 if((chptr = find_channel(parv[2])) == NULL)
283 return;
284
285 /* TS is higher, drop it. */
286 if(atol(parv[1]) > chptr->channelts)
287 return;
288
289 switch (parv[3][0])
290 {
291 case 'b':
292 banlist = &chptr->banlist;
293 mode_type = CHFL_BAN;
294 mems = ALL_MEMBERS;
295 break;
296
297 case 'e':
298 banlist = &chptr->exceptlist;
299 mode_type = CHFL_EXCEPTION;
300 needcap = CAP_EX;
301 mems = ONLY_CHANOPS;
302 break;
303
304 case 'I':
305 banlist = &chptr->invexlist;
306 mode_type = CHFL_INVEX;
307 needcap = CAP_IE;
308 mems = ONLY_CHANOPS;
309 break;
310
311 case 'q':
312 banlist = &chptr->quietlist;
313 mode_type = CHFL_QUIET;
314 mems = ALL_MEMBERS;
315 break;
316
317 /* maybe we should just blindly propagate this? */
318 default:
319 return;
320 }
321
322 parabuf[0] = '\0';
323 s = LOCAL_COPY(parv[4]);
324
325 /* Hide connecting server on netburst -- jilles */
326 if (ConfigServerHide.flatten_links && !HasSentEob(source_p))
327 fakesource_p = &me;
328 else
329 fakesource_p = source_p;
330 mlen = sprintf(modebuf, ":%s MODE %s +", fakesource_p->name, chptr->chname);
331 mbuf = modebuf + mlen;
332 pbuf = parabuf;
333
334 while(*s == ' ')
335 s++;
336
337 /* next char isnt a space, point t to the next one */
338 if((t = strchr(s, ' ')) != NULL)
339 {
340 *t++ = '\0';
341
342 /* double spaces will break the parser */
343 while(*t == ' ')
344 t++;
345 }
346
347 /* couldve skipped spaces and got nothing.. */
348 while(!EmptyString(s))
349 {
350 /* ban with a leading ':' -- this will break the protocol */
351 if(*s == ':')
352 goto nextban;
353
354 tlen = strlen(s);
355
356 /* I dont even want to begin parsing this.. */
357 if(tlen > MODEBUFLEN)
358 break;
359
360 if((forward = strchr(s+1, '$')) != NULL)
361 {
362 *forward++ = '\0';
363 if(*forward == '\0')
364 tlen--, forward = NULL;
365 else
366 possibly_remove_lower_forward(fakesource_p,
367 mems, chptr, banlist,
368 parv[3][0], s, forward);
369 }
370
371 if(add_id(fakesource_p, chptr, s, forward, banlist, mode_type))
372 {
373 /* this new one wont fit.. */
374 if(mlen + MAXMODEPARAMS + plen + tlen > BUFSIZE - 5 ||
375 modecount >= MAXMODEPARAMS)
376 {
377 *mbuf = '\0';
378 *(pbuf - 1) = '\0';
379 sendto_channel_local(fakesource_p, mems, chptr, "%s %s", modebuf, parabuf);
380
381 mbuf = modebuf + mlen;
382 pbuf = parabuf;
383 plen = modecount = 0;
384 }
385
386 if (forward != NULL)
387 forward[-1] = '$';
388
389 *mbuf++ = parv[3][0];
390 arglen = sprintf(pbuf, "%s ", s);
391 pbuf += arglen;
392 plen += arglen;
393 modecount++;
394 }
395
396 nextban:
397 s = t;
398
399 if(s != NULL)
400 {
401 if((t = strchr(s, ' ')) != NULL)
402 {
403 *t++ = '\0';
404
405 while(*t == ' ')
406 t++;
407 }
408 }
409 }
410
411 if(modecount)
412 {
413 *mbuf = '\0';
414 *(pbuf - 1) = '\0';
415 sendto_channel_local(fakesource_p, mems, chptr, "%s %s", modebuf, parabuf);
416 }
417
418 sendto_server(client_p, chptr, CAP_TS6 | needcap, NOCAPS, ":%s BMASK %ld %s %s :%s",
419 source_p->id, (long) chptr->channelts, chptr->chname, parv[3], parv[4]);
420 }