Author: Bleep <tomh@inxpress.net>
[ircu2.10.12-pk.git] / ircd / m_part.c
1 /*
2  * IRC - Internet Relay Chat, ircd/m_part.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 "numeric.h"
97 #include "numnicks.h"
98 #include "send.h"
99
100 #include <assert.h>
101 #include <string.h>
102
103 /*
104  * m_part - generic message handler
105  *
106  * parv[0] = sender prefix
107  * parv[1] = channel
108  * parv[parc - 1] = comment
109  */
110 int m_part(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
111 {
112   struct Channel* chptr;
113   struct Membership* member;
114   char*           p = 0;
115   char*           name;
116   char            pbuf[BUFSIZE];
117   char*           comment = (parc > 2 && !EmptyString(parv[parc - 1])) ? parv[parc - 1] : 0;
118
119   *pbuf = '\0';                 /* Initialize the part buffer... -Kev */
120
121   sptr->flags &= ~FLAGS_TS8;
122
123   if (parc < 2 || parv[1][0] == '\0')
124     return need_more_params(sptr, "PART");
125
126   for (; (name = ircd_strtok(&p, parv[1], ",")); parv[1] = 0)
127   {
128     chptr = get_channel(sptr, name, CGT_NO_CREATE);
129     if (!chptr) {
130       sendto_one(sptr, err_str(ERR_NOSUCHCHANNEL), me.name, parv[0], name);
131       continue;
132     }
133     if (*name == '&' && !MyUser(sptr))
134       continue;
135     /*
136      * Do not use find_channel_member here: zombies must be able to part too
137      */
138     if (!(member = find_member_link(chptr, sptr)))
139     {
140       /* Normal to get when our client did a kick
141        * for a remote client (who sends back a PART),
142        * so check for remote client or not --Run
143        */
144       if (MyUser(sptr))
145         sendto_one(sptr, err_str(ERR_NOTONCHANNEL), me.name, parv[0],
146             chptr->chname);
147       continue;
148     }
149     /* Recreate the /part list for sending to servers */
150     if (*name != '&')
151     {
152       if (*pbuf)
153         strcat(pbuf, ",");
154       strcat(pbuf, name);
155     }
156     if (IsZombie(member)
157         || !member_can_send_to_channel(member))  /* Returns 1 if we CAN send */
158       comment = 0;
159     /* Send part to all clients */
160     if (!IsZombie(member))
161     {
162       if (comment)
163         sendto_channel_butserv(chptr, sptr, PartFmt2, parv[0], chptr->chname,
164                                comment);
165       else
166         sendto_channel_butserv(chptr, sptr, PartFmt1, parv[0], chptr->chname);
167     }
168     else if (MyUser(sptr))
169     {
170       if (comment)
171         sendto_one(sptr, PartFmt2, parv[0], chptr->chname, comment);
172       else
173         sendto_one(sptr, PartFmt1, parv[0], chptr->chname);
174     }
175     remove_user_from_channel(sptr, chptr);
176   }
177   /* Send out the parts to all servers... -Kev */
178   if (*pbuf)
179   {
180     if (comment)
181       sendto_serv_butone(cptr, PartFmt2serv, NumNick(sptr), pbuf, comment);
182     else
183       sendto_serv_butone(cptr, PartFmt1serv, NumNick(sptr), pbuf);
184   }
185   return 0;
186 }
187
188 /*
189  * ms_part - server message handler
190  *
191  * parv[0] = sender prefix
192  * parv[1] = channel
193  * parv[parc - 1] = comment
194  */
195 int ms_part(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
196 {
197   struct Channel* chptr;
198   struct Membership* member;
199   char*           p = 0;
200   char*           name;
201   char            pbuf[BUFSIZE];
202   char*           comment = (parc > 2 && !EmptyString(parv[parc - 1])) ? parv[parc - 1] : 0;
203
204   *pbuf = '\0';                 /* Initialize the part buffer... -Kev */
205
206   sptr->flags &= ~FLAGS_TS8;
207
208   if (parc < 2 || parv[1][0] == '\0')
209     return need_more_params(sptr, "PART");
210
211   for (; (name = ircd_strtok(&p, parv[1], ",")); parv[1] = 0)
212   {
213     chptr = get_channel(sptr, name, CGT_NO_CREATE);
214     if (!chptr) {
215       sendto_one(sptr, err_str(ERR_NOSUCHCHANNEL), me.name, parv[0], name);
216       continue;
217     }
218     if (*name == '&' && !MyUser(sptr))
219       continue;
220     /*
221      * Do not use find_channel_member here: zombies must be able to part too
222      */
223     if (!(member = find_member_link(chptr, sptr)))
224     {
225       /* Normal to get when our client did a kick
226        * for a remote client (who sends back a PART),
227        * so check for remote client or not --Run
228        */
229       if (MyUser(sptr))
230         sendto_one(sptr, err_str(ERR_NOTONCHANNEL), me.name, parv[0],
231             chptr->chname);
232       continue;
233     }
234     /* Recreate the /part list for sending to servers */
235     if (*name != '&')
236     {
237       if (*pbuf)
238         strcat(pbuf, ",");
239       strcat(pbuf, name);
240     }
241     if (IsZombie(member)
242         || !member_can_send_to_channel(member))  /* Returns 1 if we CAN send */
243       comment = 0;
244     /* Send part to all clients */
245     if (!IsZombie(member))
246     {
247       if (comment)
248         sendto_channel_butserv(chptr, sptr, PartFmt2, parv[0], chptr->chname,
249                                comment);
250       else
251         sendto_channel_butserv(chptr, sptr, PartFmt1, parv[0], chptr->chname);
252     }
253     else if (MyUser(sptr))
254     {
255       if (comment)
256         sendto_one(sptr, PartFmt2, parv[0], chptr->chname, comment);
257       else
258         sendto_one(sptr, PartFmt1, parv[0], chptr->chname);
259     }
260     remove_user_from_channel(sptr, chptr);
261   }
262   /* Send out the parts to all servers... -Kev */
263   if (*pbuf)
264   {
265     if (comment)
266       sendto_serv_butone(cptr, PartFmt2serv, NumNick(sptr), pbuf, comment);
267     else
268       sendto_serv_butone(cptr, PartFmt1serv, NumNick(sptr), pbuf);
269   }
270   return 0;
271 }
272
273 #if 0
274 /*
275  * m_part
276  *
277  * parv[0] = sender prefix
278  * parv[1] = channel
279  * parv[parc - 1] = comment
280  */
281 int m_part(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
282 {
283   struct Channel* chptr;
284   struct Membership* member;
285   char*           p = 0;
286   char*           name;
287   char            pbuf[BUFSIZE];
288   char*           comment = (parc > 2 && !EmptyString(parv[parc - 1])) ? parv[parc - 1] : 0;
289
290   *pbuf = '\0';                 /* Initialize the part buffer... -Kev */
291
292   sptr->flags &= ~FLAGS_TS8;
293
294   if (parc < 2 || parv[1][0] == '\0')
295     return need_more_params(sptr, "PART");
296
297   for (; (name = ircd_strtok(&p, parv[1], ",")); parv[1] = 0)
298   {
299     chptr = get_channel(sptr, name, CGT_NO_CREATE);
300     if (!chptr) {
301       sendto_one(sptr, err_str(ERR_NOSUCHCHANNEL), me.name, parv[0], name);
302       continue;
303     }
304     if (*name == '&' && !MyUser(sptr))
305       continue;
306     /*
307      * Do not use find_channel_member here: zombies must be able to part too
308      */
309     if (!(member = find_member_link(chptr, sptr)))
310     {
311       /* Normal to get when our client did a kick
312        * for a remote client (who sends back a PART),
313        * so check for remote client or not --Run
314        */
315       if (MyUser(sptr))
316         sendto_one(sptr, err_str(ERR_NOTONCHANNEL), me.name, parv[0],
317             chptr->chname);
318       continue;
319     }
320     /* Recreate the /part list for sending to servers */
321     if (*name != '&')
322     {
323       if (*pbuf)
324         strcat(pbuf, ",");
325       strcat(pbuf, name);
326     }
327     if (IsZombie(member)
328         || !member_can_send_to_channel(member))  /* Returns 1 if we CAN send */
329       comment = 0;
330     /* Send part to all clients */
331     if (!IsZombie(member))
332     {
333       if (comment)
334         sendto_channel_butserv(chptr, sptr, PartFmt2, parv[0], chptr->chname,
335                                comment);
336       else
337         sendto_channel_butserv(chptr, sptr, PartFmt1, parv[0], chptr->chname);
338     }
339     else if (MyUser(sptr))
340     {
341       if (comment)
342         sendto_one(sptr, PartFmt2, parv[0], chptr->chname, comment);
343       else
344         sendto_one(sptr, PartFmt1, parv[0], chptr->chname);
345     }
346     remove_user_from_channel(sptr, chptr);
347   }
348   /* Send out the parts to all servers... -Kev */
349   if (*pbuf)
350   {
351     if (comment)
352       sendto_serv_butone(cptr, PartFmt2serv, NumNick(sptr), pbuf, comment);
353     else
354       sendto_serv_butone(cptr, PartFmt1serv, NumNick(sptr), pbuf);
355   }
356   return 0;
357 }
358 #endif /* 0 */