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