86e7af844898cca6e156a30a54a438bff2affd3d
[ircu2.10.12-pk.git] / ircd / m_kick.c
1 /*
2  * IRC - Internet Relay Chat, ircd/m_kick.c
3  * Copyright (C) 1990 Jarkko Oikarinen and
4  *                    University of Oulu, Computing Center
5  *
6  * See file AUTHORS in IRC package for additional names of
7  * the programmers.
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 1, or (at your option)
12  * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  * $Id$
24  */
25
26 /*
27  * m_functions execute protocol messages on this server:
28  *
29  *    cptr    is always NON-NULL, pointing to a *LOCAL* client
30  *            structure (with an open socket connected!). This
31  *            identifies the physical socket where the message
32  *            originated (or which caused the m_function to be
33  *            executed--some m_functions may call others...).
34  *
35  *    sptr    is the source of the message, defined by the
36  *            prefix part of the message if present. If not
37  *            or prefix not found, then sptr==cptr.
38  *
39  *            (!IsServer(cptr)) => (cptr == sptr), because
40  *            prefixes are taken *only* from servers...
41  *
42  *            (IsServer(cptr))
43  *                    (sptr == cptr) => the message didn't
44  *                    have the prefix.
45  *
46  *                    (sptr != cptr && IsServer(sptr) means
47  *                    the prefix specified servername. (?)
48  *
49  *                    (sptr != cptr && !IsServer(sptr) means
50  *                    that message originated from a remote
51  *                    user (not local).
52  *
53  *            combining
54  *
55  *            (!IsServer(sptr)) means that, sptr can safely
56  *            taken as defining the target structure of the
57  *            message in this server.
58  *
59  *    *Always* true (if 'parse' and others are working correct):
60  *
61  *    1)      sptr->from == cptr  (note: cptr->from == cptr)
62  *
63  *    2)      MyConnect(sptr) <=> sptr == cptr (e.g. sptr
64  *            *cannot* be a local connection, unless it's
65  *            actually cptr!). [MyConnect(x) should probably
66  *            be defined as (x == x->from) --msa ]
67  *
68  *    parc    number of variable parameter strings (if zero,
69  *            parv is allowed to be NULL)
70  *
71  *    parv    a NULL terminated list of parameter pointers,
72  *
73  *                    parv[0], sender (prefix string), if not present
74  *                            this points to an empty string.
75  *                    parv[1]...parv[parc-1]
76  *                            pointers to additional parameters
77  *                    parv[parc] == NULL, *always*
78  *
79  *            note:   it is guaranteed that parv[0]..parv[parc-1] are all
80  *                    non-NULL pointers.
81  */
82 #include "config.h"
83
84 #if 0
85 /*
86  * No need to include handlers.h here the signatures must match
87  * and we don't need to force a rebuild of all the handlers everytime
88  * we add a new one to the list. --Bleep
89  */
90 #include "handlers.h"
91 #endif /* 0 */
92 #include "channel.h"
93 #include "client.h"
94 #include "hash.h"
95 #include "ircd.h"
96 #include "ircd_reply.h"
97 #include "ircd_string.h"
98 #include "msg.h"
99 #include "numeric.h"
100 #include "numnicks.h"
101 #include "send.h"
102
103 #include <assert.h>
104
105 /*
106  * m_kick - generic message handler
107  *
108  * parv[0] = sender prefix
109  * parv[1] = channel
110  * parv[2] = client to kick
111  * parv[parc-1] = kick comment
112  */
113 int m_kick(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
114 {
115   struct Client *who;
116   struct Channel *chptr;
117   struct Membership *member = 0;
118   char *name, *comment;
119
120   cli_flags(sptr) &= ~FLAGS_TS8;
121
122   if (parc < 3 || *parv[1] == '\0')
123     return need_more_params(sptr, "KICK");
124
125   name = parv[1];
126
127   /* simple checks */
128   if (!(chptr = get_channel(sptr, name, CGT_NO_CREATE)))
129     return send_reply(sptr, ERR_NOSUCHCHANNEL, name);
130
131   if (!is_chan_op(sptr, chptr) || IsModelessChannel(name))
132     return send_reply(sptr, ERR_CHANOPRIVSNEEDED, name);
133
134   if (!(who = find_chasing(sptr, parv[2], 0)))
135     return 0; /* find_chasing sends the reply for us */
136
137   /* Don't allow the channel service to be kicked */
138   if (IsChannelService(who))
139     return send_reply(sptr, ERR_ISCHANSERVICE, cli_name(who), chptr->chname);
140
141   /* Prevent kicking opers from local channels -DM- */
142   if (IsLocalChannel(chptr->chname) && HasPriv(who, PRIV_DEOP_LCHAN))
143     return send_reply(sptr, ERR_ISOPERLCHAN, cli_name(who), chptr->chname);
144
145   /* check if kicked user is actually on the channel */
146   if (!(member = find_member_link(chptr, who)) || IsZombie(member))
147     return send_reply(sptr, ERR_USERNOTINCHANNEL, cli_name(who), chptr->chname);
148
149   /* We rely on ircd_snprintf to truncate the comment */
150   comment = EmptyString(parv[parc - 1]) ? parv[0] : parv[parc - 1];
151
152   if (!IsLocalChannel(name))
153     sendcmdto_serv_butone(sptr, CMD_KICK, cptr, "%H %C :%s", chptr, who,
154                           comment);
155
156   sendcmdto_channel_butserv(sptr, CMD_KICK, chptr, "%H %C :%s", chptr, who,
157                             comment);
158
159   make_zombie(member, who, cptr, sptr, chptr);
160
161   return 0;
162 }
163
164 /*
165  * ms_kick - server message handler
166  *
167  * parv[0] = sender prefix
168  * parv[1] = channel
169  * parv[2] = client to kick
170  * parv[parc-1] = kick comment
171  */
172 int ms_kick(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
173 {
174   struct Client *who;
175   struct Channel *chptr;
176   struct Membership *member = 0, *sptr_link = 0;
177   char *name, *comment;
178
179   cli_flags(sptr) &= ~FLAGS_TS8;
180
181   if (parc < 3 || *parv[1] == '\0')
182     return need_more_params(sptr, "KICK");
183
184   name = parv[1];
185   comment = parv[parc - 1];
186
187   /* figure out who gets kicked from what */
188   if (IsLocalChannel(name) ||
189       !(chptr = get_channel(sptr, name, CGT_NO_CREATE)) ||
190       !(who = findNUser(parv[2])))
191     return 0;
192
193   /* We go ahead and pass on the KICK for users not on the channel */
194   if (!(member = find_member_link(chptr, who)) || IsZombie(member))
195     member = 0;
196
197   /* Send HACK notice, but not for servers in BURST */
198   if (IsServer(sptr) && !IsBurstOrBurstAck(sptr))
199     sendto_opmask_butone(0, SNO_HACK4, "HACK: %C KICK %H %C %s", sptr, chptr,
200                          who, comment);
201
202   /* Unless someone accepted it downstream (or the user isn't on the channel
203    * here), if kicker is not on channel, or if kicker is not a channel
204    * operator, bounce the kick
205    */
206   if (!IsServer(sptr) && member && cli_from(who) != cptr &&
207       (!(sptr_link = find_member_link(chptr, sptr)) || !IsChanOp(sptr_link))) {
208     sendto_opmask_butone(0, SNO_HACK2, "HACK: %C KICK %H %C %s", sptr, chptr,
209                          who, comment);
210
211     sendcmdto_one(who, CMD_JOIN, cptr, "%H", chptr);
212
213     /* Reop/revoice member */
214     if (IsChanOp(member) || HasVoice(member)) {
215       struct ModeBuf mbuf;
216
217       modebuf_init(&mbuf, sptr, cptr, chptr,
218                    (MODEBUF_DEST_SERVER |  /* Send mode to a server */
219                     MODEBUF_DEST_DEOP   |  /* Deop the source */
220                     MODEBUF_DEST_BOUNCE)); /* And bounce the MODE */
221
222       if (IsChanOp(member))
223         modebuf_mode_client(&mbuf, MODE_DEL | MODE_CHANOP, who);
224       if (HasVoice(member))
225         modebuf_mode_client(&mbuf, MODE_DEL | MODE_VOICE, who);
226
227       modebuf_flush(&mbuf);
228     }
229   } else {
230     /* Propagate kick... */
231     sendcmdto_serv_butone(sptr, CMD_KICK, cptr, "%H %C :%s", chptr, who,
232                           comment);
233
234     if (member) { /* and tell the channel about it */
235       sendcmdto_channel_butserv(sptr, CMD_KICK, chptr, "%H %C :%s", chptr, who,
236                                 comment);
237
238       make_zombie(member, who, cptr, sptr, chptr);
239     }
240   }
241
242   return 0;
243 }