]> jfr.im git - irc/rqf/shadowircd.git/blob - modules/core/m_mode.c
Update NEWS.
[irc/rqf/shadowircd.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 * $Id: m_mode.c 1006 2006-03-09 15:32:14Z nenolod $
25 */
26
27 #include "stdinc.h"
28 #include "channel.h"
29 #include "client.h"
30 #include "hash.h"
31 #include "match.h"
32 #include "ircd.h"
33 #include "numeric.h"
34 #include "s_user.h"
35 #include "s_conf.h"
36 #include "s_serv.h"
37 #include "logger.h"
38 #include "send.h"
39 #include "msg.h"
40 #include "parse.h"
41 #include "modules.h"
42 #include "packet.h"
43 #include "s_newconf.h"
44
45 static int m_mode(struct Client *, struct Client *, int, const char **);
46 static int ms_mode(struct Client *, struct Client *, int, const char **);
47 static int ms_tmode(struct Client *, struct Client *, int, const char **);
48 static int ms_bmask(struct Client *, struct Client *, int, const char **);
49
50 struct Message mode_msgtab = {
51 "MODE", 0, 0, 0, MFLG_SLOW,
52 {mg_unreg, {m_mode, 2}, {m_mode, 3}, {ms_mode, 3}, mg_ignore, {m_mode, 2}}
53 };
54 struct Message tmode_msgtab = {
55 "TMODE", 0, 0, 0, MFLG_SLOW,
56 {mg_ignore, mg_ignore, {ms_tmode, 4}, {ms_tmode, 4}, mg_ignore, mg_ignore}
57 };
58 struct Message bmask_msgtab = {
59 "BMASK", 0, 0, 0, MFLG_SLOW,
60 {mg_ignore, mg_ignore, mg_ignore, {ms_bmask, 5}, mg_ignore, mg_ignore}
61 };
62
63 mapi_clist_av1 mode_clist[] = { &mode_msgtab, &tmode_msgtab, &bmask_msgtab, NULL };
64
65 DECLARE_MODULE_AV1(mode, NULL, NULL, mode_clist, NULL, NULL, "$Revision: 1006 $");
66
67 /*
68 * m_mode - MODE command handler
69 * parv[1] - channel
70 */
71 static int
72 m_mode(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
73 {
74 struct Channel *chptr = NULL;
75 struct membership *msptr;
76 int n = 2;
77 const char *dest;
78 int operspy = 0;
79
80 dest = parv[1];
81
82 if(IsOperSpy(source_p) && *dest == '!')
83 {
84 dest++;
85 operspy = 1;
86
87 if(EmptyString(dest))
88 {
89 sendto_one(source_p, form_str(ERR_NEEDMOREPARAMS),
90 me.name, source_p->name, "MODE");
91 return 0;
92 }
93 }
94
95 /* Now, try to find the channel in question */
96 if(!IsChanPrefix(*dest))
97 {
98 /* if here, it has to be a non-channel name */
99 user_mode(client_p, source_p, parc, parv);
100 return 0;
101 }
102
103 if(!check_channel_name(dest))
104 {
105 sendto_one_numeric(source_p, ERR_BADCHANNAME, form_str(ERR_BADCHANNAME), parv[1]);
106 return 0;
107 }
108
109 chptr = find_channel(dest);
110
111 if(chptr == NULL)
112 {
113 sendto_one_numeric(source_p, ERR_NOSUCHCHANNEL,
114 form_str(ERR_NOSUCHCHANNEL), parv[1]);
115 return 0;
116 }
117
118 /* Now know the channel exists */
119 if(parc < n + 1)
120 {
121 if(operspy)
122 report_operspy(source_p, "MODE", chptr->chname);
123
124 sendto_one(source_p, form_str(RPL_CHANNELMODEIS),
125 me.name, source_p->name, parv[1],
126 operspy ? channel_modes(chptr, &me) : channel_modes(chptr, source_p));
127
128 sendto_one(source_p, form_str(RPL_CREATIONTIME),
129 me.name, source_p->name, parv[1], chptr->channelts);
130 }
131 else
132 {
133 msptr = find_channel_membership(chptr, source_p);
134
135 /* Finish the flood grace period... */
136 if(MyClient(source_p) && !IsFloodDone(source_p))
137 {
138 if(!((parc == 3) && (parv[2][0] == 'b') && (parv[2][1] == '\0')))
139 flood_endgrace(source_p);
140 }
141
142 set_channel_mode(client_p, source_p, chptr, msptr, parc - n, parv + n);
143 }
144
145 return 0;
146 }
147
148 static int
149 ms_mode(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
150 {
151 struct Channel *chptr;
152
153 chptr = find_channel(parv[1]);
154
155 if(chptr == NULL)
156 {
157 sendto_one_numeric(source_p, ERR_NOSUCHCHANNEL,
158 form_str(ERR_NOSUCHCHANNEL), parv[1]);
159 return 0;
160 }
161
162 set_channel_mode(client_p, source_p, chptr, NULL, parc - 2, parv + 2);
163
164 return 0;
165 }
166
167 static int
168 ms_tmode(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
169 {
170 struct Channel *chptr = NULL;
171 struct membership *msptr;
172
173 /* Now, try to find the channel in question */
174 if(!IsChanPrefix(parv[2][0]) || !check_channel_name(parv[2]))
175 {
176 sendto_one_numeric(source_p, ERR_BADCHANNAME, form_str(ERR_BADCHANNAME), parv[2]);
177 return 0;
178 }
179
180 chptr = find_channel(parv[2]);
181
182 if(chptr == NULL)
183 {
184 sendto_one_numeric(source_p, ERR_NOSUCHCHANNEL,
185 form_str(ERR_NOSUCHCHANNEL), parv[2]);
186 return 0;
187 }
188
189 /* TS is higher, drop it. */
190 if(atol(parv[1]) > chptr->channelts)
191 return 0;
192
193 if(IsServer(source_p))
194 {
195 set_channel_mode(client_p, source_p, chptr, NULL, parc - 3, parv + 3);
196 }
197 else
198 {
199 msptr = find_channel_membership(chptr, source_p);
200
201 set_channel_mode(client_p, source_p, chptr, msptr, parc - 3, parv + 3);
202 }
203
204 return 0;
205 }
206
207 static int
208 ms_bmask(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
209 {
210 static char modebuf[BUFSIZE];
211 static char parabuf[BUFSIZE];
212 struct Channel *chptr;
213 rb_dlink_list *banlist;
214 const char *s;
215 char *t;
216 char *mbuf;
217 char *pbuf;
218 long mode_type;
219 int mlen;
220 int plen = 0;
221 int tlen;
222 int arglen;
223 int modecount = 0;
224 int needcap = NOCAPS;
225 int mems;
226 struct Client *fakesource_p;
227
228 if(!IsChanPrefix(parv[2][0]) || !check_channel_name(parv[2]))
229 return 0;
230
231 if((chptr = find_channel(parv[2])) == NULL)
232 return 0;
233
234 /* TS is higher, drop it. */
235 if(atol(parv[1]) > chptr->channelts)
236 return 0;
237
238 switch (parv[3][0])
239 {
240 case 'b':
241 banlist = &chptr->banlist;
242 mode_type = CHFL_BAN;
243 mems = ALL_MEMBERS;
244 break;
245
246 case 'e':
247 banlist = &chptr->exceptlist;
248 mode_type = CHFL_EXCEPTION;
249 needcap = CAP_EX;
250 mems = ONLY_CHANOPS;
251 break;
252
253 case 'I':
254 banlist = &chptr->invexlist;
255 mode_type = CHFL_INVEX;
256 needcap = CAP_IE;
257 mems = ONLY_CHANOPS;
258 break;
259
260 case 'q':
261 banlist = &chptr->quietlist;
262 mode_type = CHFL_QUIET;
263 mems = ALL_MEMBERS;
264 break;
265
266 /* maybe we should just blindly propagate this? */
267 default:
268 return 0;
269 }
270
271 parabuf[0] = '\0';
272 s = LOCAL_COPY(parv[4]);
273
274 /* Hide connecting server on netburst -- jilles */
275 if (ConfigServerHide.flatten_links && !HasSentEob(source_p))
276 fakesource_p = &me;
277 else
278 fakesource_p = source_p;
279 mlen = rb_sprintf(modebuf, ":%s MODE %s +", fakesource_p->name, chptr->chname);
280 mbuf = modebuf + mlen;
281 pbuf = parabuf;
282
283 while(*s == ' ')
284 s++;
285
286 /* next char isnt a space, point t to the next one */
287 if((t = strchr(s, ' ')) != NULL)
288 {
289 *t++ = '\0';
290
291 /* double spaces will break the parser */
292 while(*t == ' ')
293 t++;
294 }
295
296 /* couldve skipped spaces and got nothing.. */
297 while(!EmptyString(s))
298 {
299 /* ban with a leading ':' -- this will break the protocol */
300 if(*s == ':')
301 goto nextban;
302
303 tlen = strlen(s);
304
305 /* I dont even want to begin parsing this.. */
306 if(tlen > MODEBUFLEN)
307 break;
308
309 if(add_id(fakesource_p, chptr, s, banlist, mode_type))
310 {
311 /* this new one wont fit.. */
312 if(mlen + MAXMODEPARAMS + plen + tlen > BUFSIZE - 5 ||
313 modecount >= MAXMODEPARAMS)
314 {
315 *mbuf = '\0';
316 *(pbuf - 1) = '\0';
317 sendto_channel_local(mems, chptr, "%s %s", modebuf, parabuf);
318 sendto_server(client_p, chptr, needcap, CAP_TS6,
319 "%s %s", modebuf, parabuf);
320
321 mbuf = modebuf + mlen;
322 pbuf = parabuf;
323 plen = modecount = 0;
324 }
325
326 *mbuf++ = parv[3][0];
327 arglen = rb_sprintf(pbuf, "%s ", s);
328 pbuf += arglen;
329 plen += arglen;
330 modecount++;
331 }
332
333 nextban:
334 s = t;
335
336 if(s != NULL)
337 {
338 if((t = strchr(s, ' ')) != NULL)
339 {
340 *t++ = '\0';
341
342 while(*t == ' ')
343 t++;
344 }
345 }
346 }
347
348 if(modecount)
349 {
350 *mbuf = '\0';
351 *(pbuf - 1) = '\0';
352 sendto_channel_local(mems, chptr, "%s %s", modebuf, parabuf);
353 sendto_server(client_p, chptr, needcap, CAP_TS6, "%s %s", modebuf, parabuf);
354 }
355
356 sendto_server(client_p, chptr, CAP_TS6 | needcap, NOCAPS, ":%s BMASK %ld %s %s :%s",
357 source_p->id, (long) chptr->channelts, chptr->chname, parv[3], parv[4]);
358 return 0;
359 }
360