43ef8f5d84cdfa3e71e0ce5353748b5a6953d4b7
[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 #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 "list.h"
99 #include "msg.h"
100 #include "numeric.h"
101 #include "numnicks.h"
102 #include "s_user.h"
103 #include "send.h"
104 #include "struct.h"
105
106 #include <assert.h>
107
108 /*
109  * m_invite - generic message handler
110  *
111  *   parv[0] - sender prefix
112  *   parv[1] - user to invite
113  *   parv[2] - channel name
114  *
115  * - INVITE now is accepted only if who does it is chanop (this of course
116  *   implies that channel must exist and he must be on it).
117  *
118  * - On the other side it IS processed even if channel is NOT invite only
119  *   leaving room for other enhancements like inviting banned ppl.  -- Nemesi
120  *
121  * - Invite with no parameters now lists the channels you are invited to.
122  *                                                         - Isomer 23 Oct 99
123  */
124 int m_invite(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
125 {
126   struct Client *acptr;
127   struct Channel *chptr;
128   
129   if (parc < 2 ) { 
130     /*
131      * list the channels you have an invite to.
132      */
133     struct SLink *lp;
134     for (lp = cli_user(sptr)->invited; lp; lp = lp->next)
135       send_reply(cptr, RPL_INVITELIST, lp->value.chptr->chname);
136     send_reply(cptr, RPL_ENDOFINVITELIST);
137     return 0;
138   }
139   
140   if (parc < 3 || EmptyString(parv[2]))
141     return need_more_params(sptr, "INVITE");
142
143   if (!(acptr = FindUser(parv[1]))) {
144     send_reply(sptr, ERR_NOSUCHNICK, parv[1]);
145     return 0;
146   }
147
148   if (is_silenced(sptr, acptr))
149     return 0;
150
151   clean_channelname(parv[2]);
152
153   if (!IsChannelPrefix(*parv[2]))
154     return 0;
155
156   if (!(chptr = FindChannel(parv[2]))) {
157     if (IsModelessChannel(parv[2]) || IsLocalChannel(parv[2])) {
158       send_reply(sptr, ERR_NOTONCHANNEL, parv[2]);
159       return 0;
160     }
161
162     /* Do not disallow to invite to non-existant #channels, otherwise they
163        would simply first be created, causing only MORE bandwidth usage. */
164
165     if (check_target_limit(sptr, acptr, cli_name(acptr), 0))
166       return 0;
167
168     send_reply(sptr, RPL_INVITING, cli_name(acptr), parv[2]);
169
170     if (cli_user(acptr)->away)
171       send_reply(sptr, RPL_AWAY, cli_name(acptr), cli_user(acptr)->away);
172
173     sendcmdto_one(sptr, CMD_INVITE, acptr, "%s :%s", cli_name(acptr), parv[2]);
174
175     return 0;
176   }
177
178   if (!find_channel_member(sptr, chptr)) {
179     send_reply(sptr, ERR_NOTONCHANNEL, chptr->chname);
180     return 0;
181   }
182
183   if (find_channel_member(acptr, chptr)) {
184     send_reply(sptr, ERR_USERONCHANNEL, cli_name(acptr), chptr->chname);
185     return 0;
186   }
187
188   if (!is_chan_op(sptr, chptr)) {
189     send_reply(sptr, ERR_CHANOPRIVSNEEDED, chptr->chname);
190     return 0;
191   }
192
193   /* If we get here, it was a VALID and meaningful INVITE */
194
195   if (check_target_limit(sptr, acptr, cli_name(acptr), 0))
196     return 0;
197
198   send_reply(sptr, RPL_INVITING, cli_name(acptr), chptr->chname);
199
200   if (cli_user(acptr)->away)
201     send_reply(sptr, RPL_AWAY, cli_name(acptr), cli_user(acptr)->away);
202
203   if (MyConnect(acptr))
204     add_invite(acptr, chptr);
205
206   sendcmdto_one(sptr, CMD_INVITE, acptr, "%s :%H", cli_name(acptr), chptr);
207
208   return 0;
209 }
210
211 /*
212  * ms_invite - server message handler
213  *
214  *   parv[0] - sender prefix
215  *   parv[1] - user to invite
216  *   parv[2] - channel name
217  *
218  * - INVITE now is accepted only if who does it is chanop (this of course
219  *   implies that channel must exist and he must be on it).
220  *
221  * - On the other side it IS processed even if channel is NOT invite only
222  *   leaving room for other enhancements like inviting banned ppl.  -- Nemesi
223  *
224  * - Invite with no parameters now lists the channels you are invited to.
225  *                                                         - Isomer 23 Oct 99
226  */
227 int ms_invite(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
228 {
229   struct Client *acptr;
230   struct Channel *chptr;
231   
232   if (IsServer(sptr)) {
233     /*
234      * this will blow up if we get an invite from a server
235      * we look for channel membership in sptr below. 
236      */
237     return protocol_violation(sptr,"Server attempting to invite");
238   }
239   if (parc < 3 || EmptyString(parv[2])) {
240     /*
241      * should have been handled upstream, ignore it.
242      */
243     return need_more_params(sptr,"INVITE");
244   }
245   if ('#' != *parv[2]) {
246     /*
247      * should not be sent
248      */
249     return protocol_violation(sptr, "Invite to a non-standard channel %s",parv[2]);
250   }
251   if (!(acptr = FindUser(parv[1]))) {
252     send_reply(sptr, ERR_NOSUCHNICK, parv[1]);
253     return 0;
254   }
255   if (!MyUser(acptr)) {
256     /*
257      * just relay the message
258      */
259     sendcmdto_one(sptr, CMD_INVITE, acptr, "%s :%s", cli_name(acptr), parv[2]);
260     return 0;
261   }
262
263   if (is_silenced(sptr, acptr))
264     return 0;
265
266   if (!(chptr = FindChannel(parv[2]))) {
267     /*
268      * allow invites to non existant channels, bleah
269      * avoid JOIN, INVITE, PART abuse
270      */
271     sendcmdto_one(sptr, CMD_INVITE, acptr, "%C :%s", acptr, parv[2]);
272     return 0;
273   }
274
275   if (!find_channel_member(sptr, chptr)) {
276     send_reply(sptr, ERR_NOTONCHANNEL, chptr->chname);
277     return 0;
278   }
279   if (find_channel_member(acptr, chptr)) {
280     send_reply(sptr, ERR_USERONCHANNEL, cli_name(acptr), chptr->chname);
281     return 0;
282   }
283   add_invite(acptr, chptr);
284   sendcmdto_one(sptr, CMD_INVITE, acptr, "%s :%H", cli_name(acptr), chptr);
285   return 0;
286 }
287
288
289 #if 0
290 /*
291  * m_invite
292  *
293  *   parv[0] - sender prefix
294  *   parv[1] - user to invite
295  *   parv[2] - channel name
296  *
297  * - INVITE now is accepted only if who does it is chanop (this of course
298  *   implies that channel must exist and he must be on it).
299  *
300  * - On the other side it IS processed even if channel is NOT invite only
301  *   leaving room for other enhancements like inviting banned ppl.  -- Nemesi
302  *
303  * - Invite with no parameters now lists the channels you are invited to.
304  *                                                         - Isomer 23 Oct 99
305  */
306 int m_invite(struct Client* cptr, struct Client *sptr, int parc, char *parv[])
307 {
308   struct Client *acptr;
309   struct Channel *chptr;
310   
311   if (parc < 2 ) { 
312     /*
313      * list the channels you have an invite to.
314      */
315     struct SLink *lp;
316     for (lp = sptr->user->invited; lp; lp = lp->next)
317       sendto_one(cptr, rpl_str(RPL_INVITELIST), me.name, cptr->name, /* XXX DEAD */
318                 lp->value.chptr->chname);
319     sendto_one(cptr, rpl_str(RPL_ENDOFINVITELIST), me.name, cptr->name); /* XXX DEAD */
320     return 0;
321   }
322
323   if (parc < 3 || *parv[2] == '\0')
324     return need_more_params(sptr, "INVITE");
325
326   if (!(acptr = FindUser(parv[1])))
327   {
328     sendto_one(sptr, err_str(ERR_NOSUCHNICK), me.name, parv[0], parv[1]); /* XXX DEAD */
329     return 0;
330   }
331
332   if (is_silenced(sptr, acptr))
333     return 0;
334
335   if (MyUser(sptr))
336     clean_channelname(parv[2]);
337   else if (IsLocalChannel(parv[2]))
338     return 0;
339
340   if (*parv[2] == '0' || !IsChannelName(parv[2]))
341     return 0;
342
343   if (!(chptr = FindChannel(parv[2])))
344   {
345     if (IsModelessChannel(parv[2]) || IsLocalChannel(parv[2]))
346     {
347       sendto_one(sptr, err_str(ERR_NOTONCHANNEL), me.name, parv[0], parv[2]); /* XXX DEAD */
348       return 0;
349     }
350
351     /* Do not disallow to invite to non-existant #channels, otherwise they
352        would simply first be created, causing only MORE bandwidth usage. */
353     if (MyConnect(sptr))
354     {
355       if (check_target_limit(sptr, acptr, acptr->name, 0))
356         return 0;
357
358       sendto_one(sptr, rpl_str(RPL_INVITING), me.name, parv[0], /* XXX DEAD */
359           acptr->name, parv[2]);
360
361       if (acptr->user->away)
362         sendto_one(sptr, rpl_str(RPL_AWAY), me.name, parv[0], /* XXX DEAD */
363             acptr->name, acptr->user->away);
364     }
365
366     sendto_prefix_one(acptr, sptr, ":%s INVITE %s :%s", parv[0], /* XXX DEAD */
367         acptr->name, parv[2]);
368
369     return 0;
370   }
371
372   if (!find_channel_member(sptr, chptr))
373   {
374     sendto_one(sptr, err_str(ERR_NOTONCHANNEL), me.name, parv[0], /* XXX DEAD */
375         chptr->chname);
376     return 0;
377   }
378
379   if (find_channel_member(acptr, chptr))
380   {
381     sendto_one(sptr, err_str(ERR_USERONCHANNEL), /* XXX DEAD */
382         me.name, parv[0], acptr->name, chptr->chname);
383     return 0;
384   }
385
386   if (MyConnect(sptr))
387   {
388     if (!is_chan_op(sptr, chptr))
389     {
390       sendto_one(sptr, err_str(ERR_CHANOPRIVSNEEDED), /* XXX DEAD */
391           me.name, parv[0], chptr->chname);
392       return 0;
393     }
394
395     /* If we get here, it was a VALID and meaningful INVITE */
396
397     if (check_target_limit(sptr, acptr, acptr->name, 0))
398       return 0;
399
400     sendto_one(sptr, rpl_str(RPL_INVITING), me.name, parv[0], /* XXX DEAD */
401         acptr->name, chptr->chname);
402
403     if (acptr->user->away)
404       sendto_one(sptr, rpl_str(RPL_AWAY), me.name, parv[0], /* XXX DEAD */
405           acptr->name, acptr->user->away);
406   }
407
408   if (MyConnect(acptr)) {
409     add_invite(acptr, chptr);
410   sendto_prefix_one(acptr, sptr, ":%s INVITE %s :%s", parv[0], /* XXX DEAD */
411       acptr->name, chptr->chname);
412   }
413   else
414     sendto_highprot_butone(acptr, 10, "%s%s " TOK_INVITE " %s :%s", /* XXX DEAD */
415        NumNick(sptr), acptr->name, chptr->chname);
416
417   return 0;
418 }
419 #endif /* 0 */
420