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