Author: Kev <klmitch@mit.edu>
[ircu2.10.12-pk.git] / ircd / m_invite.c
1 /*
2  * IRC - Internet Relay Chat, ircd/m_invite.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 #include "channel.h"
85 #include "client.h"
86 #include "hash.h"
87 #include "ircd.h"
88 #include "ircd_reply.h"
89 #include "ircd_string.h"
90 #include "list.h"
91 #include "msg.h"
92 #include "numeric.h"
93 #include "numnicks.h"
94 #include "s_user.h"
95 #include "send.h"
96 #include "struct.h"
97
98 #include <assert.h>
99
100 /*
101  * m_invite - generic message handler
102  *
103  *   parv[0] - sender prefix
104  *   parv[1] - user to invite
105  *   parv[2] - channel name
106  *
107  * - INVITE now is accepted only if who does it is chanop (this of course
108  *   implies that channel must exist and he must be on it).
109  *
110  * - On the other side it IS processed even if channel is NOT invite only
111  *   leaving room for other enhancements like inviting banned ppl.  -- Nemesi
112  *
113  * - Invite with no parameters now lists the channels you are invited to.
114  *                                                         - Isomer 23 Oct 99
115  */
116 int m_invite(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
117 {
118   struct Client *acptr;
119   struct Channel *chptr;
120   
121   if (parc < 2 ) { 
122     /*
123      * list the channels you have an invite to.
124      */
125     struct SLink *lp;
126     for (lp = cli_user(sptr)->invited; lp; lp = lp->next)
127       send_reply(cptr, RPL_INVITELIST, lp->value.chptr->chname);
128     send_reply(cptr, RPL_ENDOFINVITELIST);
129     return 0;
130   }
131   
132   if (parc < 3 || EmptyString(parv[2]))
133     return need_more_params(sptr, "INVITE");
134
135   if (!(acptr = FindUser(parv[1]))) {
136     send_reply(sptr, ERR_NOSUCHNICK, parv[1]);
137     return 0;
138   }
139
140   if (is_silenced(sptr, acptr))
141     return 0;
142
143   clean_channelname(parv[2]);
144
145   if (!IsChannelPrefix(*parv[2]))
146     return 0;
147
148   if (!(chptr = FindChannel(parv[2]))) {
149     if (IsModelessChannel(parv[2]) || IsLocalChannel(parv[2])) {
150       send_reply(sptr, ERR_NOTONCHANNEL, parv[2]);
151       return 0;
152     }
153
154     /* Do not disallow to invite to non-existant #channels, otherwise they
155        would simply first be created, causing only MORE bandwidth usage. */
156
157     if (check_target_limit(sptr, acptr, cli_name(acptr), 0))
158       return 0;
159
160     send_reply(sptr, RPL_INVITING, cli_name(acptr), parv[2]);
161
162     if (cli_user(acptr)->away)
163       send_reply(sptr, RPL_AWAY, cli_name(acptr), cli_user(acptr)->away);
164
165     sendcmdto_one(sptr, CMD_INVITE, acptr, "%s :%s", cli_name(acptr), parv[2]);
166
167     return 0;
168   }
169
170   if (!find_channel_member(sptr, chptr)) {
171     send_reply(sptr, ERR_NOTONCHANNEL, chptr->chname);
172     return 0;
173   }
174
175   if (find_channel_member(acptr, chptr)) {
176     send_reply(sptr, ERR_USERONCHANNEL, cli_name(acptr), chptr->chname);
177     return 0;
178   }
179
180   if (!is_chan_op(sptr, chptr)) {
181     send_reply(sptr, ERR_CHANOPRIVSNEEDED, chptr->chname);
182     return 0;
183   }
184
185   /* If we get here, it was a VALID and meaningful INVITE */
186
187   if (check_target_limit(sptr, acptr, cli_name(acptr), 0))
188     return 0;
189
190   send_reply(sptr, RPL_INVITING, cli_name(acptr), chptr->chname);
191
192   if (cli_user(acptr)->away)
193     send_reply(sptr, RPL_AWAY, cli_name(acptr), cli_user(acptr)->away);
194
195   if (MyConnect(acptr))
196     add_invite(acptr, chptr);
197
198   sendcmdto_one(sptr, CMD_INVITE, acptr, "%s :%H", cli_name(acptr), chptr);
199
200   return 0;
201 }
202
203 /*
204  * ms_invite - server message handler
205  *
206  *   parv[0] - sender prefix
207  *   parv[1] - user to invite
208  *   parv[2] - channel name
209  *
210  * - INVITE now is accepted only if who does it is chanop (this of course
211  *   implies that channel must exist and he must be on it).
212  *
213  * - On the other side it IS processed even if channel is NOT invite only
214  *   leaving room for other enhancements like inviting banned ppl.  -- Nemesi
215  *
216  * - Invite with no parameters now lists the channels you are invited to.
217  *                                                         - Isomer 23 Oct 99
218  */
219 int ms_invite(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
220 {
221   struct Client *acptr;
222   struct Channel *chptr;
223   
224   if (IsServer(sptr)) {
225     /*
226      * this will blow up if we get an invite from a server
227      * we look for channel membership in sptr below. 
228      */
229     return protocol_violation(sptr,"Server attempting to invite");
230   }
231   if (parc < 3 || EmptyString(parv[2])) {
232     /*
233      * should have been handled upstream, ignore it.
234      */
235     protocol_violation(sptr,"Too few arguments to invite");
236     return need_more_params(sptr,"INVITE");
237   }
238   if ('#' != *parv[2]) {
239     /*
240      * should not be sent
241      */
242     return protocol_violation(sptr, "Invite to a non-standard channel %s",parv[2]);
243   }
244   if (!(acptr = FindUser(parv[1]))) {
245     send_reply(sptr, ERR_NOSUCHNICK, parv[1]);
246     return 0;
247   }
248   if (!MyUser(acptr)) {
249     /*
250      * just relay the message
251      */
252     sendcmdto_one(sptr, CMD_INVITE, acptr, "%s :%s", cli_name(acptr), parv[2]);
253     return 0;
254   }
255
256   if (is_silenced(sptr, acptr))
257     return 0;
258
259   if (!(chptr = FindChannel(parv[2]))) {
260     /*
261      * allow invites to non existant channels, bleah
262      * avoid JOIN, INVITE, PART abuse
263      */
264     sendcmdto_one(sptr, CMD_INVITE, acptr, "%C :%s", acptr, parv[2]);
265     return 0;
266   }
267
268   if (!find_channel_member(sptr, chptr)) {
269     send_reply(sptr, ERR_NOTONCHANNEL, chptr->chname);
270     return 0;
271   }
272   if (find_channel_member(acptr, chptr)) {
273     send_reply(sptr, ERR_USERONCHANNEL, cli_name(acptr), chptr->chname);
274     return 0;
275   }
276   add_invite(acptr, chptr);
277   sendcmdto_one(sptr, CMD_INVITE, acptr, "%s :%H", cli_name(acptr), chptr);
278   return 0;
279 }
280
281