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