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