More RCS keyword removal...
[seven-1.x.git] / modules / core / m_mode.c
blobee54b4559c1b7580af17f77aec0f839ba3e104ee
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * m_mode.c: Sets a user or channel mode.
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
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.
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.
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
25 #include "stdinc.h"
26 #include "tools.h"
27 #include "balloc.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"
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 **);
51 static 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}}
55 static 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}
59 static 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}
64 mapi_clist_av1 mode_clist[] = { &mode_msgtab, &tmode_msgtab, &bmask_msgtab, NULL };
66 DECLARE_MODULE_AV1(mode, NULL, NULL, mode_clist, NULL, NULL, "$Revision: 61 $");
69 * m_mode - MODE command handler
70 * parv[0] - sender
71 * parv[1] - channel
73 static int
74 m_mode(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
76 struct Channel *chptr = NULL;
77 struct membership *msptr;
78 int n = 2;
79 const char *dest;
80 int auspex = 0;
82 dest = parv[1];
84 if(IsAuspex(source_p))
86 auspex = 1;
88 if(EmptyString(dest))
90 sendto_one(source_p, form_str(ERR_NEEDMOREPARAMS),
91 me.name, source_p->name, "MODE");
92 return 0;
96 /* Now, try to find the channel in question */
97 if(!IsChanPrefix(*dest))
99 /* if here, it has to be a non-channel name */
100 user_mode(client_p, source_p, parc, parv);
101 return 0;
104 if(!check_channel_name(dest))
106 sendto_one_numeric(source_p, ERR_BADCHANNAME, form_str(ERR_BADCHANNAME), parv[1]);
107 return 0;
110 chptr = find_channel(dest);
112 if(chptr == NULL)
114 sendto_one_numeric(source_p, ERR_NOSUCHCHANNEL,
115 form_str(ERR_NOSUCHCHANNEL), parv[1]);
116 return 0;
119 /* Now know the channel exists */
120 if(parc < n + 1)
122 sendto_one(source_p, form_str(RPL_CHANNELMODEIS),
123 me.name, source_p->name, parv[1],
124 auspex ? channel_modes(chptr, &me) : channel_modes(chptr, source_p));
126 sendto_one(source_p, form_str(RPL_CREATIONTIME),
127 me.name, source_p->name, parv[1], chptr->channelts);
129 else
131 msptr = find_channel_membership(chptr, source_p);
133 if(is_deop(msptr))
134 return 0;
136 /* Finish the flood grace period... */
137 if(MyClient(source_p) && !IsFloodDone(source_p))
139 if(!((parc == 3) && (parv[2][0] == 'b') && (parv[2][1] == '\0')))
140 flood_endgrace(source_p);
143 set_channel_mode(client_p, source_p, chptr, msptr, parc - n, parv + n);
146 return 0;
149 static int
150 ms_mode(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
152 struct Channel *chptr;
154 chptr = find_channel(parv[1]);
156 if(chptr == NULL)
158 sendto_one_numeric(source_p, ERR_NOSUCHCHANNEL,
159 form_str(ERR_NOSUCHCHANNEL), parv[1]);
160 return 0;
163 set_channel_mode(client_p, source_p, chptr, NULL, parc - 2, parv + 2);
165 return 0;
168 static int
169 ms_tmode(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
171 struct Channel *chptr = NULL;
172 struct membership *msptr;
174 /* Now, try to find the channel in question */
175 if(!IsChanPrefix(parv[2][0]) || !check_channel_name(parv[2]))
177 sendto_one_numeric(source_p, ERR_BADCHANNAME, form_str(ERR_BADCHANNAME), parv[2]);
178 return 0;
181 chptr = find_channel(parv[2]);
183 if(chptr == NULL)
185 sendto_one_numeric(source_p, ERR_NOSUCHCHANNEL,
186 form_str(ERR_NOSUCHCHANNEL), parv[2]);
187 return 0;
190 /* TS is higher, drop it. */
191 if(atol(parv[1]) > chptr->channelts)
192 return 0;
194 if(IsServer(source_p))
196 set_channel_mode(client_p, source_p, chptr, NULL, parc - 3, parv + 3);
198 else
200 msptr = find_channel_membership(chptr, source_p);
202 /* this can still happen on a mixed ts network. */
203 if(is_deop(msptr))
204 return 0;
206 set_channel_mode(client_p, source_p, chptr, msptr, parc - 3, parv + 3);
209 return 0;
212 static int
213 ms_bmask(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
215 static char modebuf[BUFSIZE];
216 static char parabuf[BUFSIZE];
217 struct Channel *chptr;
218 dlink_list *banlist;
219 const char *s;
220 char *t;
221 char *mbuf;
222 char *pbuf;
223 long mode_type;
224 int mlen;
225 int plen = 0;
226 int tlen;
227 int arglen;
228 int modecount = 0;
229 int needcap = NOCAPS;
230 int mems;
231 struct Client *fakesource_p;
233 if(!IsChanPrefix(parv[2][0]) || !check_channel_name(parv[2]))
234 return 0;
236 if((chptr = find_channel(parv[2])) == NULL)
237 return 0;
239 /* TS is higher, drop it. */
240 if(atol(parv[1]) > chptr->channelts)
241 return 0;
243 switch (parv[3][0])
245 case 'b':
246 banlist = &chptr->banlist;
247 mode_type = CHFL_BAN;
248 mems = ALL_MEMBERS;
249 break;
251 case 'e':
252 banlist = &chptr->exceptlist;
253 mode_type = CHFL_EXCEPTION;
254 needcap = CAP_EX;
255 mems = ONLY_CHANOPS;
256 break;
258 case 'I':
259 banlist = &chptr->invexlist;
260 mode_type = CHFL_INVEX;
261 needcap = CAP_IE;
262 mems = ONLY_CHANOPS;
263 break;
265 case 'q':
266 banlist = &chptr->quietlist;
267 mode_type = CHFL_QUIET;
268 mems = ALL_MEMBERS;
269 break;
271 /* maybe we should just blindly propagate this? */
272 default:
273 return 0;
276 parabuf[0] = '\0';
277 s = LOCAL_COPY(parv[4]);
279 /* Hide connecting server on netburst -- jilles */
280 if (ConfigServerHide.flatten_links && !HasSentEob(source_p))
281 fakesource_p = &me;
282 else
283 fakesource_p = source_p;
284 mlen = ircsprintf(modebuf, ":%s MODE %s +", fakesource_p->name, chptr->chname);
285 mbuf = modebuf + mlen;
286 pbuf = parabuf;
288 while(*s == ' ')
289 s++;
291 /* next char isnt a space, point t to the next one */
292 if((t = strchr(s, ' ')) != NULL)
294 *t++ = '\0';
296 /* double spaces will break the parser */
297 while(*t == ' ')
298 t++;
301 /* couldve skipped spaces and got nothing.. */
302 while(!EmptyString(s))
304 /* ban with a leading ':' -- this will break the protocol */
305 if(*s == ':')
306 goto nextban;
308 tlen = strlen(s);
310 /* I dont even want to begin parsing this.. */
311 if(tlen > MODEBUFLEN)
312 break;
314 if(add_id(fakesource_p, chptr, s, banlist, mode_type))
316 /* this new one wont fit.. */
317 if(mlen + MAXMODEPARAMS + plen + tlen > BUFSIZE - 5 ||
318 modecount >= MAXMODEPARAMS)
320 *mbuf = '\0';
321 *(pbuf - 1) = '\0';
322 sendto_channel_local(mems, chptr, "%s %s", modebuf, parabuf);
323 sendto_server(client_p, chptr, needcap, CAP_TS6,
324 "%s %s", modebuf, parabuf);
326 mbuf = modebuf + mlen;
327 pbuf = parabuf;
328 plen = modecount = 0;
331 *mbuf++ = parv[3][0];
332 arglen = ircsprintf(pbuf, "%s ", s);
333 pbuf += arglen;
334 plen += arglen;
335 modecount++;
338 nextban:
339 s = t;
341 if(s != NULL)
343 if((t = strchr(s, ' ')) != NULL)
345 *t++ = '\0';
347 while(*t == ' ')
348 t++;
353 if(modecount)
355 *mbuf = '\0';
356 *(pbuf - 1) = '\0';
357 sendto_channel_local(mems, chptr, "%s %s", modebuf, parabuf);
358 sendto_server(client_p, chptr, needcap, CAP_TS6, "%s %s", modebuf, parabuf);
361 sendto_server(client_p, chptr, CAP_TS6 | needcap, NOCAPS, ":%s BMASK %ld %s %s :%s",
362 source_p->id, (long) chptr->channelts, chptr->chname, parv[3], parv[4]);
363 return 0;