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