Author: beware
[ircu2.10.12-pk.git] / ircd / m_join.c
1 /*
2  * IRC - Internet Relay Chat, ircd/m_join.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 "gline.h"
87 #include "hash.h"
88 #include "ircd.h"
89 #include "ircd_chattr.h"
90 #include "ircd_features.h"
91 #include "ircd_reply.h"
92 #include "ircd_string.h"
93 #include "msg.h"
94 #include "numeric.h"
95 #include "numnicks.h"
96 #include "s_debug.h"
97 #include "s_user.h"
98 #include "send.h"
99
100 #include <assert.h>
101 #include <stdlib.h>
102 #include <string.h>
103
104 /*
105  * Helper function to find last 0 in a comma-separated list of
106  * channel names.
107  */
108 static char *
109 last0(char *chanlist)
110 {
111   char *p;
112
113   for (p = chanlist; p[0]; p++) /* find last "JOIN 0" */
114     if (p[0] == '0' && (p[1] == ',' || p[1] == '\0' || !IsChannelChar(p[1]))) {
115       chanlist = p; /* we'll start parsing here */
116
117       if (!p[1]) /* hit the end */
118         break;
119
120       p++;
121     } else {
122       while (p[0] != ',' && p[0] != '\0') /* skip past channel name */
123         p++;
124
125       if (!p[0]) /* hit the end */
126         break;
127     }
128
129   return chanlist;
130 }
131
132 /*
133  * Helper function to perform a JOIN 0 if needed; returns 0 if channel
134  * name is not 0, else removes user from all channels and returns 1.
135  */
136 static int
137 join0(struct JoinBuf *join, struct Client *cptr, struct Client *sptr,
138       char *chan)
139 {
140   struct Membership *member;
141   struct JoinBuf part;
142
143   /* is it a JOIN 0? */
144   if (chan[0] != '0' || chan[1] != '\0')
145     return 0;
146   
147   joinbuf_join(join, 0, 0); /* join special channel 0 */
148
149   /* leave all channels */
150   joinbuf_init(&part, sptr, cptr, JOINBUF_TYPE_PARTALL,
151                "Left all channels", 0);
152
153   while ((member = cli_user(sptr)->channel))
154     joinbuf_join(&part, member->channel,
155                  IsZombie(member) ? CHFL_ZOMBIE : 0);
156
157   joinbuf_flush(&part);
158
159   return 1;
160 }
161
162 /*
163  * m_join - generic message handler
164  */
165 int m_join(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
166 {
167   struct Channel *chptr;
168   struct JoinBuf join;
169   struct JoinBuf create;
170   struct ModeBuf mbuf;
171   struct Gline *gline;
172   unsigned int flags = 0;
173   int i, j, k = 0;
174   char *p = 0;
175   char *chanlist;
176   char *name;
177   char *keys;
178
179   if (parc < 2 || *parv[1] == '\0')
180     return need_more_params(sptr, "JOIN");
181
182   joinbuf_init(&join, sptr, cptr, JOINBUF_TYPE_JOIN, 0, 0);
183   joinbuf_init(&create, sptr, cptr, JOINBUF_TYPE_CREATE, 0, TStime());
184
185   chanlist = last0(parv[1]); /* find last "JOIN 0" */
186
187   keys = parv[2]; /* remember where keys are */
188
189   for (name = ircd_strtok(&p, chanlist, ","); name;
190        name = ircd_strtok(&p, 0, ",")) {
191     clean_channelname(name);
192
193     if (join0(&join, cptr, sptr, name)) /* did client do a JOIN 0? */
194       continue;
195
196     if (!IsChannelName(name))
197     {
198       /* bad channel name */
199       send_reply(sptr, ERR_NOSUCHCHANNEL, name);
200       continue;
201     }
202
203     /* This checks if the channel contains control codes and rejects em
204      * until they are gone, then we will do it otherwise - *SOB Mode*
205      */
206     for (k = 0, j = 0; name[j]; j++)
207       if (IsCntrl(name[j]))
208         k++;
209     if (k > 0)
210     {
211       send_reply(sptr, ERR_NOSUCHCHANNEL, name);
212       continue;
213     }
214
215     /* BADCHANed channel */
216     if ((gline = gline_find(name, GLINE_BADCHAN | GLINE_EXACT)) &&
217         GlineIsActive(gline) && !IsAnOper(sptr)) {
218       send_reply(sptr, ERR_BANNEDFROMCHAN, name);
219       continue;
220     }
221
222     if ((chptr = FindChannel(name)))
223     {
224       if (find_member_link(chptr, sptr))
225         continue; /* already on channel */
226
227       flags = CHFL_DEOPPED;
228     }
229     else
230       flags = CHFL_CHANOP;
231
232     /* disallow creating local channels */
233     if ((name[0] == '&') && !chptr && !feature_bool(FEAT_LOCAL_CHANNELS)) {
234         send_reply(sptr, ERR_NOSUCHCHANNEL, name);
235         continue;
236     }
237
238     if (cli_user(sptr)->joined >= feature_int(FEAT_MAXCHANNELSPERUSER) &&
239         !HasPriv(sptr, PRIV_CHAN_LIMIT)) {
240       send_reply(sptr, ERR_TOOMANYCHANNELS, chptr ? chptr->chname : name);
241       break; /* no point processing the other channels */
242     }
243
244     if (chptr) {
245       int is_level0_op = 0;
246       if (!BadPtr(keys) && *chptr->mode.apass) {
247         /* Don't use compall for the apass, only a single key is allowed.
248            Test Apass first in case someone set Apass and upass equal. */
249         if (strcmp(chptr->mode.apass, keys) == 0) {
250           is_level0_op = 1;
251           flags &= ~CHFL_DEOPPED;
252           flags |= CHFL_CHANOP | CHFL_CHANNEL_MANAGER;
253         }
254         else if (*chptr->mode.upass && strcmp(chptr->mode.upass, keys) == 0) {
255           is_level0_op = 1;
256           flags &= ~CHFL_DEOPPED;
257           flags |= CHFL_CHANOP;
258         }
259       }
260       if (check_target_limit(sptr, chptr, chptr->chname, 0))
261         continue; /* exceeded target limit */
262       else if (!is_level0_op && (i = can_join(sptr, chptr, keys))) {
263         if (i > MAGIC_OPER_OVERRIDE)
264         { /* oper overrode mode */
265           switch (i - MAGIC_OPER_OVERRIDE)
266           {
267           case ERR_CHANNELISFULL: /* figure out which mode */
268             i = 'l';
269             break;
270
271           case ERR_INVITEONLYCHAN:
272             i = 'i';
273             break;
274
275           case ERR_BANNEDFROMCHAN:
276             i = 'b';
277             break;
278
279           case ERR_BADCHANNELKEY:
280             i = 'k';
281             break;
282
283           case ERR_NEEDREGGEDNICK:
284             i = 'r';
285             break;
286
287           default:
288             i = '?';
289             break;
290           }
291
292           /* send accountability notice */
293           sendto_opmask_butone(0, SNO_HACK4, "OPER JOIN: %C JOIN %H "
294                                "(overriding +%c)", sptr, chptr, i);
295         }
296         else
297         {
298           send_reply(sptr, i, chptr->chname);
299           continue;
300         }
301       } /* else if ((i = can_join(sptr, chptr, keys))) */
302
303       joinbuf_join(&join, chptr, flags);
304       if (is_level0_op)
305       {
306         joinbuf_flush(&join);
307         modebuf_init(&mbuf, &me, cptr, chptr,
308             MODEBUF_DEST_CHANNEL |              /* Send mode to channel */
309             MODEBUF_DEST_SERVER);               /* And send it to the other servers */
310         modebuf_mode_client(&mbuf,
311             MODE_ADD | MODE_CHANOP, sptr);      /* Give ops to the level0 op */
312         modebuf_flush(&mbuf);
313       }
314     } else if (!(chptr = get_channel(sptr, name, CGT_CREATE)))
315       continue; /* couldn't get channel */
316     else if (check_target_limit(sptr, chptr, chptr->chname, 1))
317     {
318       /* Note: check_target_limit will only ever return 0 here */
319       chptr->members = 0;
320       destruct_channel(chptr); /* created it... */
321       continue;
322     }
323     else
324       joinbuf_join(&create, chptr, flags | CHFL_CHANNEL_MANAGER);
325
326     del_invite(sptr, chptr);
327
328     if (chptr->topic[0]) {
329       send_reply(sptr, RPL_TOPIC, chptr->chname, chptr->topic);
330       send_reply(sptr, RPL_TOPICWHOTIME, chptr->chname, chptr->topic_nick,
331                  chptr->topic_time);
332     }
333
334     do_names(sptr, chptr, NAMES_ALL|NAMES_EON); /* send /names list */
335   }
336
337   joinbuf_flush(&join); /* must be first, if there's a JOIN 0 */
338   joinbuf_flush(&create);
339
340   return 0;
341 }
342
343 /*
344  * ms_join - server message handler
345  */
346 int ms_join(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
347 {
348   struct Membership *member;
349   struct Channel *chptr;
350   struct JoinBuf join;
351   unsigned int flags = 0;
352   time_t creation = 0;
353   char *p = 0;
354   char *chanlist;
355   char *name;
356
357   if (IsServer(sptr))
358   {
359     return protocol_violation(cptr,
360                               "%s tried to JOIN %s, duh!",
361                               cli_name(sptr),
362                               (parc < 2 || *parv[1] == '\0') ? "a channel" :
363                                                                parv[1]
364                               );
365   }
366
367   if (parc < 2 || *parv[1] == '\0')
368     return need_more_params(sptr, "JOIN");
369
370   if (parc > 2 && parv[2])
371     creation = atoi(parv[2]);
372
373   joinbuf_init(&join, sptr, cptr, JOINBUF_TYPE_JOIN, 0, 0);
374
375   chanlist = last0(parv[1]); /* find last "JOIN 0" */
376
377   for (name = ircd_strtok(&p, chanlist, ","); name;
378        name = ircd_strtok(&p, 0, ",")) {
379     clean_channelname(name);
380
381     if (join0(&join, cptr, sptr, name)) /* did client do a JOIN 0? */
382       continue;
383
384     if (IsLocalChannel(name) || !IsChannelName(name))
385     {
386       protocol_violation(cptr, "%s tried to join %s", cli_name(sptr), name);
387       continue;
388     }
389
390     if (!(chptr = FindChannel(name)))
391     {
392       /* No channel exists, so create one */
393       if (!(chptr = get_channel(sptr, name, CGT_CREATE)))
394       {
395         protocol_violation(sptr,"couldn't get channel %s for %s",
396                            name,cli_name(sptr));
397         continue;
398       }
399       flags = CHFL_DEOPPED | (HasFlag(sptr, FLAG_TS8) ? CHFL_SERVOPOK : 0);
400
401       /* when the network is 2.10.11+ then remove MAGIC_REMOTE_JOIN_TS */ 
402       chptr->creationtime = creation ? creation : MAGIC_REMOTE_JOIN_TS;
403     }
404     else { /* We have a valid channel? */
405       if ((member = find_member_link(chptr, sptr)))
406       {
407         /* It is impossible to get here --Run */
408         if (!IsZombie(member)) /* already on channel */
409           continue;
410
411         flags = member->status & (CHFL_DEOPPED | CHFL_SERVOPOK);
412         remove_user_from_channel(sptr, chptr);
413         chptr = FindChannel(name);
414       }
415       else
416         flags = CHFL_DEOPPED | (HasFlag(sptr, FLAG_TS8) ? CHFL_SERVOPOK : 0);
417       /* Always copy the timestamp when it is older, that is the only way to
418          ensure network-wide synchronization of creation times. */
419       if (creation && creation < chptr->creationtime)
420         chptr->creationtime = creation;
421     } 
422
423     joinbuf_join(&join, chptr, flags);
424   }
425
426   joinbuf_flush(&join); /* flush joins... */
427
428   return 0;
429 }