a2946e081aebbd686257ffcdff3be1e2380cc103
[ircu2.10.12-pk.git] / ircd / m_clearmode.c
1 /*
2  * IRC - Internet Relay Chat, ircd/m_tmpl.c
3  * Copyright (C) 1990 Jarkko Oikarinen and
4  *                    University of Oulu, Computing Center
5  * Copyright (C) 2000 Kevin L. Mitchell <klmitch@mit.edu>
6  *
7  * See file AUTHORS in IRC package for additional names of
8  * the programmers.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 1, or (at your option)
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  *
24  * $Id$
25  */
26
27 /*
28  * m_functions execute protocol messages on this server:
29  *
30  *    cptr    is always NON-NULL, pointing to a *LOCAL* client
31  *            structure (with an open socket connected!). This
32  *            identifies the physical socket where the message
33  *            originated (or which caused the m_function to be
34  *            executed--some m_functions may call others...).
35  *
36  *    sptr    is the source of the message, defined by the
37  *            prefix part of the message if present. If not
38  *            or prefix not found, then sptr==cptr.
39  *
40  *            (!IsServer(cptr)) => (cptr == sptr), because
41  *            prefixes are taken *only* from servers...
42  *
43  *            (IsServer(cptr))
44  *                    (sptr == cptr) => the message didn't
45  *                    have the prefix.
46  *
47  *                    (sptr != cptr && IsServer(sptr) means
48  *                    the prefix specified servername. (?)
49  *
50  *                    (sptr != cptr && !IsServer(sptr) means
51  *                    that message originated from a remote
52  *                    user (not local).
53  *
54  *            combining
55  *
56  *            (!IsServer(sptr)) means that, sptr can safely
57  *            taken as defining the target structure of the
58  *            message in this server.
59  *
60  *    *Always* true (if 'parse' and others are working correct):
61  *
62  *    1)      sptr->from == cptr  (note: cptr->from == cptr)
63  *
64  *    2)      MyConnect(sptr) <=> sptr == cptr (e.g. sptr
65  *            *cannot* be a local connection, unless it's
66  *            actually cptr!). [MyConnect(x) should probably
67  *            be defined as (x == x->from) --msa ]
68  *
69  *    parc    number of variable parameter strings (if zero,
70  *            parv is allowed to be NULL)
71  *
72  *    parv    a NULL terminated list of parameter pointers,
73  *
74  *                    parv[0], sender (prefix string), if not present
75  *                            this points to an empty string.
76  *                    parv[1]...parv[parc-1]
77  *                            pointers to additional parameters
78  *                    parv[parc] == NULL, *always*
79  *
80  *            note:   it is guaranteed that parv[0]..parv[parc-1] are all
81  *                    non-NULL pointers.
82  */
83 #if 0
84 /*
85  * No need to include handlers.h here the signatures must match
86  * and we don't need to force a rebuild of all the handlers everytime
87  * we add a new one to the list. --Bleep
88  */
89 #include "handlers.h"
90 #endif /* 0 */
91 #include "client.h"
92 #include "channel.h"
93 #include "hash.h"
94 #include "ircd.h"
95 #include "ircd_alloc.h"
96 #include "ircd_reply.h"
97 #include "ircd_string.h"
98 #include "list.h"
99 #include "msg.h"
100 #include "numeric.h"
101 #include "numnicks.h"
102 #include "send.h"
103 #include "support.h"
104
105 #include <assert.h>
106
107 /*
108  * do_clearmode(struct Client *cptr, struct Client *sptr,
109  *              struct Channel *chptr, char *control)
110  *
111  * This is the function that actually clears the channel modes.
112  */
113 static int
114 do_clearmode(struct Client *cptr, struct Client *sptr, struct Channel *chptr,
115              char *control)
116 {
117   static int flags[] = {
118     MODE_CHANOP,        'o',
119     MODE_VOICE,         'v',
120     MODE_PRIVATE,       'p',
121     MODE_SECRET,        's',
122     MODE_MODERATED,     'm',
123     MODE_TOPICLIMIT,    't',
124     MODE_INVITEONLY,    'i',
125     MODE_NOPRIVMSGS,    'n',
126     MODE_KEY,           'k',
127     MODE_BAN,           'b',
128     MODE_LIMIT,         'l',
129     0x0, 0x0
130   };
131   int *flag_p;
132   unsigned int del_mode = 0;
133   char control_buf[20];
134   int control_buf_i = 0;
135   struct ModeBuf mbuf;
136   struct SLink *link, *next;
137   struct Membership *member;
138
139   /* Um...yeah, like it's supposed to have any modes at all. */
140   if (IsModelessChannel(chptr->chname))
141     return 0;
142
143   /* Ok, so what are we supposed to get rid of? */
144   for (; *control; control++) {
145     for (flag_p = flags; flag_p[0]; flag_p += 2)
146       if (*control == flag_p[1]) {
147         del_mode |= flag_p[0];
148         break;
149       }
150   }
151
152   if (!del_mode)
153     return 0; /* nothing to remove; ho hum. */
154
155   modebuf_init(&mbuf, sptr, cptr, chptr,
156                (MODEBUF_DEST_CHANNEL | /* Send MODE to channel */
157                 MODEBUF_DEST_OPMODE  | /* Treat it like an OPMODE */
158                 MODEBUF_DEST_HACK4));  /* Generate a HACK(4) notice */
159
160   modebuf_mode(&mbuf, MODE_DEL | (del_mode & chptr->mode.mode));
161   chptr->mode.mode &= ~del_mode; /* and of course actually delete them */
162
163   /* If we're removing invite, remove all the invites */
164   if (del_mode & MODE_INVITEONLY)
165     mode_invite_clear(chptr);
166
167   /*
168    * If we're removing the key, note that; note that we can't clear
169    * the key until after modebuf_* are done with it
170    */
171   if (del_mode & MODE_KEY && *chptr->mode.key)
172     modebuf_mode_string(&mbuf, MODE_DEL | MODE_KEY, chptr->mode.key, 0);
173
174   /* If we're removing the limit, note that and clear the limit */
175   if (del_mode & MODE_LIMIT && chptr->mode.limit) {
176     modebuf_mode_uint(&mbuf, MODE_DEL | MODE_LIMIT, chptr->mode.limit);
177     chptr->mode.limit = 0; /* not referenced, so safe */
178   }
179
180   /*
181    * Go through and mark the bans for deletion; note that we can't
182    * free them until after modebuf_* are done with them
183    */
184   if (del_mode & MODE_BAN) {
185     for (link = chptr->banlist; link; link = next) {
186       next = link->next;
187
188       modebuf_mode_string(&mbuf, MODE_DEL | MODE_BAN, /* delete ban */
189                           link->value.ban.banstr, 1);
190
191       MyFree(link->value.ban.who); /* free up who string */
192       free_link(link); /* and of course the link itself */
193     }
194
195     chptr->banlist = 0;
196   }
197
198   /* Deal with users on the channel */
199   if (del_mode & (MODE_BAN | MODE_CHANOP | MODE_VOICE))
200     for (member = chptr->members; member; member = member->next_member) {
201       if (IsZombie(member)) /* we ignore zombies */
202         continue;
203
204       if (del_mode & MODE_BAN) /* If we cleared bans, clear the valid flags */
205         ClearBanValid(member);
206
207       /* Drop channel operator status */
208       if (IsChanOp(member) && del_mode & MODE_CHANOP) {
209         modebuf_mode_client(&mbuf, MODE_DEL | MODE_CHANOP, member->user);
210         member->status &= ~CHFL_CHANOP;
211       }
212
213       /* Drop voice */
214       if (HasVoice(member) && del_mode & MODE_VOICE) {
215         modebuf_mode_client(&mbuf, MODE_DEL | MODE_VOICE, member->user);
216         member->status &= ~CHFL_VOICE;
217       }
218     }
219
220   /* And flush the modes to the channel */
221   modebuf_flush(&mbuf);
222
223   /* Finally, we can clear the key... */
224   if (del_mode & MODE_KEY)
225     chptr->mode.key[0] = '\0';
226
227 #ifndef OPATH
228   /* Don't propagate CLEARMODE if it's a local channel */
229   if (IsLocalChannel(chptr->chname))
230     return 0;
231 #endif
232
233   /* Ok, build control string again */
234   for (flag_p = flags; flag_p[0]; flag_p += 2)
235     if (del_mode & flag_p[0])
236       control_buf[control_buf_i++] = flag_p[1];
237
238   control_buf[control_buf_i] = '\0';
239
240 #ifdef OPATH
241   write_log(OPATH, "%Tu %#C CLEARMODE %H %s\n", TStime(), sptr, chptr,
242             control_buf);
243 #endif
244
245   /* Then send it */
246   if (!IsLocalChannel(chptr->chname))
247     sendcmdto_serv_butone(sptr, CMD_CLEARMODE, cptr, "%H %s", chptr,
248                           control_buf);
249
250   return 0;
251 }
252
253 /*
254  * ms_clearmode - server message handler
255  *
256  * parv[0] = Send prefix
257  * parv[1] = Channel name
258  * parv[2] = Control string
259  */
260 int
261 ms_clearmode(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
262 {
263   struct Channel *chptr;
264
265   if (parc < 3)
266     return need_more_params(sptr, "CLEARMODE");
267
268   if (!IsPrivileged(sptr))
269     return send_reply(sptr, ERR_NOPRIVILEGES);
270
271   if (!IsChannelName(parv[1]) || IsLocalChannel(parv[1]) ||
272       !(chptr = FindChannel(parv[1])))
273     return send_reply(sptr, ERR_NOSUCHCHANNEL, parv[1]);
274
275   return do_clearmode(cptr, sptr, chptr, parv[2]);
276 }
277
278 /*
279  * mo_clearmode - oper message handler
280  *
281  * parv[0] = Send prefix
282  * parv[1] = Channel name
283  * parv[2] = Control string
284  */
285 int
286 mo_clearmode(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
287 {
288 #ifndef CONFIG_OPERCMDS
289   return send_reply(sptr, ERR_DISABLED, "CLEARMODE");
290 #else
291   struct Channel *chptr;
292   char *control = "ovpsmikbl"; /* default control string */
293
294   if (parc < 2)
295     return need_more_params(sptr, "CLEARMODE");
296
297   if (parc > 2)
298     control = parv[2];
299
300   clean_channelname(parv[1]);
301
302   if (!IsOper(sptr) && !IsLocalChannel(parv[1]))
303     return send_reply(sptr, ERR_NOPRIVILEGES);
304
305   if (!IsChannelName(parv[1]) || !(chptr = FindChannel(parv[1])))
306     return send_reply(sptr, ERR_NOSUCHCHANNEL, parv[1]);
307
308   return do_clearmode(cptr, sptr, chptr, control);
309 #endif /* CONFIG_OPERCMDS */
310 }