fixed ssl.c bug when ssl backend returns IO_BLOCKED but IO engine doesn't get informe...
[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: m_kick.c 1893 2008-11-18 03:18:46Z entrope $
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 #include "channel.h"
85 #include "client.h"
86 #include "hash.h"
87 #include "ircd.h"
88 #include "ircd_log.h"
89 #include "ircd_reply.h"
90 #include "ircd_string.h"
91 #include "msg.h"
92 #include "numeric.h"
93 #include "numnicks.h"
94 #include "send.h"
95 #include "ircd_features.h"
96
97 /* #include <assert.h> -- Now using assert in ircd_log.h */
98
99 /*
100  * m_kick - generic message handler
101  *
102  * parv[0] = sender prefix
103  * parv[1] = channel
104  * parv[2] = client to kick
105  * parv[parc-1] = kick comment
106  */
107 int m_kick(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
108 {
109   struct Client *who;
110   struct Channel *chptr;
111   struct Membership *member = 0;
112   struct Membership* member2;
113   char *name, *comment;
114
115   ClrFlag(sptr, FLAG_TS8);
116
117   if (parc < 3 || *parv[1] == '\0')
118     return need_more_params(sptr, "KICK");
119
120   name = parv[1];
121
122   /* simple checks */
123   if (!(chptr = get_channel(sptr, name, CGT_NO_CREATE)))
124     return send_reply(sptr, ERR_NOSUCHCHANNEL, name);
125
126   if (!(member2 = find_member_link(chptr, sptr)) || IsZombie(member2)
127       || (!IsChanOpOrHalfOp(member2) && !(IsNetServ(sptr) && IsSecurityServ(sptr) && IsChannelService(sptr))))
128     return send_reply(sptr, ERR_CHANOPRIVSNEEDED, name);
129
130   if (!(who = find_chasing(sptr, parv[2], 0)))
131     return 0; /* find_chasing sends the reply for us */
132
133   /* Don't allow the channel service to be kicked */
134   if (IsChannelService(who) && !IsXtraOp(sptr) && who != sptr)
135     return send_reply(sptr, ERR_ISCHANSERVICE, cli_name(who), chptr->chname);
136
137   /* Prevent kicking opers from local channels -DM- */
138   if (IsLocalChannel(chptr->chname) && HasPriv(who, PRIV_DEOP_LCHAN))
139     return send_reply(sptr, ERR_ISOPERLCHAN, cli_name(who), chptr->chname);
140
141   /* check if kicked user is actually on the channel */
142   if (!(member = find_member_link(chptr, who)) || IsZombie(member) || (IsInvisibleJoin(member) && IsDelayedJoin(member)))
143     return send_reply(sptr, ERR_USERNOTINCHANNEL, cli_name(who), chptr->chname);
144     
145   /* Don't allow oped users to be kicked by halfops */
146   if (IsChanOp(member) && !IsChanOp(member2) && who != sptr)
147     return send_reply(sptr, ERR_CHANOPRIVSNEEDED, name);
148
149   /* Don't allow to kick member with a higher op-level,
150    * or members with the same op-level unless both are MAXOPLEVEL.
151    */
152   if (OpLevel(member) < OpLevel(member2)
153       || (OpLevel(member) == OpLevel(member2)
154           && OpLevel(member) < MAXOPLEVEL))
155     return send_reply(sptr, ERR_NOTLOWEROPLEVEL, cli_name(who), chptr->chname,
156         OpLevel(member2), OpLevel(member), "kick",
157         OpLevel(member) == OpLevel(member2) ? "the same" : "a higher");
158
159   /* We rely on ircd_snprintf to truncate the comment */
160   comment = EmptyString(parv[parc - 1]) ? parv[0] : parv[parc - 1];
161
162   if (!IsLocalChannel(name))
163     sendcmdto_serv_butone(sptr, CMD_KICK, cptr, "%H %C :%s", chptr, who,
164                           comment);
165
166   if (IsDelayedJoin(member)) {
167     /* If it's a delayed join, only send the KICK to the person doing
168      * the kicking and the victim */
169     if (MyUser(who))
170       sendcmdto_one(sptr, CMD_KICK, who, "%H %C :%s", chptr, who, comment);
171     sendcmdto_one(who, CMD_JOIN, sptr, "%H", chptr);
172     sendcmdto_one(sptr, CMD_KICK, sptr, "%H %C :%s", chptr, who, comment);
173     CheckDelayedJoins(chptr);
174   } else if((member->channel->mode.mode & MODE_AUDITORIUM) && !IsVoicedOrOpped(member)) {
175     /* send the kick to ops only. */
176     if (MyUser(who))
177       sendcmdto_one(sptr, CMD_KICK, who, "%H %C :%s", chptr, who, comment);
178     sendcmdto_channel_butserv_butone(sptr, CMD_KICK, chptr, NULL, SKIP_NONOPS, "%H %C :%s", chptr, who, comment);
179   } else
180     sendcmdto_channel_butserv_butone(sptr, CMD_KICK, chptr, NULL, 0, "%H %C :%s", chptr, who, comment);
181
182   make_zombie(member, who, cptr, sptr, chptr);
183
184   return 0;
185 }
186
187 /*
188  * ms_kick - server message handler
189  *
190  * parv[0] = sender prefix
191  * parv[1] = channel
192  * parv[2] = client to kick
193  * parv[parc-1] = kick comment
194  */
195 int ms_kick(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
196 {
197   struct Client *who;
198   struct Channel *chptr;
199   struct Membership *member = 0, *sptr_link = 0;
200   char *name, *comment;
201
202   ClrFlag(sptr, FLAG_TS8);
203
204   if (parc < 3 || *parv[1] == '\0')
205     return need_more_params(sptr, "KICK");
206
207   name = parv[1];
208   comment = parv[parc - 1];
209
210   /* figure out who gets kicked from what */
211   if (IsLocalChannel(name) ||
212       !(chptr = get_channel(sptr, name, CGT_NO_CREATE)) ||
213       !(who = findNUser(parv[2])))
214     return 0;
215
216   /* We go ahead and pass on the KICK for users not on the channel */
217   member = find_member_link(chptr, who);
218   if(member && (IsInvisibleJoin(member) && IsDelayedJoin(member))) 
219     return 0;
220   if (member && IsZombie(member))
221   {
222     /* We might get a KICK from a zombie's own server because the user
223      * net-rode during a burst (which always generates a KICK) *and*
224      * was kicked via another server.  In that case, we must remove
225      * the user from the channel.
226      */
227     if (sptr == cli_user(who)->server)
228     {
229       remove_user_from_channel(who, chptr);
230     }
231     /* Otherwise, we treat zombies like they are not channel members. */
232     member = 0;
233   }
234
235   /* Send HACK notice, but not for servers in BURST */
236   /* 2002-10-17: Don't send HACK if the users local server is kicking them */
237   if (IsServer(sptr) &&
238       !IsBurstOrBurstAck(sptr) &&
239       sptr!=cli_user(who)->server)
240     sendto_opmask_butone(0, SNO_HACK4, "HACK: %C KICK %H %C %s", sptr, chptr,
241                          who, comment);
242
243   /* Unless someone accepted it downstream (or the user isn't on the channel
244    * here), if kicker is not on channel, or if kicker is not a channel
245    * operator, bounce the kick
246    */
247   if (!IsServer(sptr) && !IsChannelService(sptr) && member && cli_from(who) != cptr &&
248       (!(sptr_link = find_member_link(chptr, sptr)) || !IsChanOp(sptr_link))) {
249     sendto_opmask_butone(0, SNO_HACK2, "HACK: %C KICK %H %C %s", sptr, chptr,
250                          who, comment);
251
252     sendcmdto_one(who, CMD_JOIN, cptr, "%H", chptr);
253
254     /* Reop/revoice member */
255     if (IsChanOp(member) || HasVoice(member)) {
256       struct ModeBuf mbuf;
257
258       modebuf_init(&mbuf, sptr, cptr, chptr,
259                    (MODEBUF_DEST_SERVER |  /* Send mode to a server */
260                     MODEBUF_DEST_DEOP   |  /* Deop the source */
261                     MODEBUF_DEST_BOUNCE)); /* And bounce the MODE */
262
263       if (IsChanOp(member))
264         modebuf_mode_client(&mbuf, MODE_DEL | MODE_CHANOP, who, OpLevel(member));
265       if (HasVoice(member))
266         modebuf_mode_client(&mbuf, MODE_DEL | MODE_VOICE, who, MAXOPLEVEL + 1);
267
268       modebuf_flush(&mbuf);
269     }
270   } else {
271     /* Propagate kick... */
272     sendcmdto_serv_butone(sptr, CMD_KICK, cptr, "%H %C :%s", chptr, who,
273                           comment);
274
275     if (member) { /* and tell the channel about it */
276       if (IsDelayedJoin(member)) {
277         if (MyUser(who))
278           sendcmdto_one(IsServer(sptr) ? &his : sptr, CMD_KICK,
279                         who, "%H %C :%s", chptr, who, comment);
280       }  else if((member->channel->mode.mode & MODE_AUDITORIUM) && !IsVoicedOrOpped(member)) {
281         /* send the kick to ops only. */
282         if (MyUser(who))
283           sendcmdto_one(IsServer(sptr) ? &his : sptr, CMD_KICK, who, "%H %C :%s", chptr, who, comment);
284         sendcmdto_channel_butserv_butone(IsServer(sptr) ? &his : sptr, CMD_KICK, chptr, NULL, SKIP_NONOPS, "%H %C :%s", chptr, who, comment);
285       } else {
286         sendcmdto_channel_butserv_butone(IsServer(sptr) ? &his : sptr, CMD_KICK,
287                                          chptr, NULL, 0, "%H %C :%s", chptr, who,
288                                          comment);
289       }
290
291       make_zombie(member, who, cptr, sptr, chptr);
292     }
293   }
294
295   return 0;
296 }