2 * IRC - Internet Relay Chat, ircd/m_join.c
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Computing Center
6 * See file AUTHORS in IRC package for additional names of
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)
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.
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.
27 * m_functions execute protocol messages on this server:
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...).
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.
39 * (!IsServer(cptr)) => (cptr == sptr), because
40 * prefixes are taken *only* from servers...
43 * (sptr == cptr) => the message didn't
46 * (sptr != cptr && IsServer(sptr) means
47 * the prefix specified servername. (?)
49 * (sptr != cptr && !IsServer(sptr) means
50 * that message originated from a remote
55 * (!IsServer(sptr)) means that, sptr can safely
56 * taken as defining the target structure of the
57 * message in this server.
59 * *Always* true (if 'parse' and others are working correct):
61 * 1) sptr->from == cptr (note: cptr->from == cptr)
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 ]
68 * parc number of variable parameter strings (if zero,
69 * parv is allowed to be NULL)
71 * parv a NULL terminated list of parameter pointers,
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*
79 * note: it is guaranteed that parv[0]..parv[parc-1] are all
89 #include "ircd_chattr.h"
90 #include "ircd_features.h"
91 #include "ircd_reply.h"
92 #include "ircd_string.h"
105 * Helper function to find last 0 in a comma-separated list of
109 last0(char *chanlist)
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 */
117 if (!p[1]) /* hit the end */
122 while (p[0] != ',' && p[0] != '\0') /* skip past channel name */
125 if (!p[0]) /* hit the end */
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.
137 join0(struct JoinBuf *join, struct Client *cptr, struct Client *sptr,
140 struct Membership *member;
143 /* is it a JOIN 0? */
144 if (chan[0] != '0' || chan[1] != '\0')
147 joinbuf_join(join, 0, 0); /* join special channel 0 */
149 /* leave all channels */
150 joinbuf_init(&part, sptr, cptr, JOINBUF_TYPE_PARTALL,
151 "Left all channels", 0);
153 while ((member = cli_user(sptr)->channel))
154 joinbuf_join(&part, member->channel,
155 IsZombie(member) ? CHFL_ZOMBIE :
156 IsDelayedJoin(member) ? CHFL_DELAYED :
159 joinbuf_flush(&part);
165 * m_join - generic message handler
167 int m_join(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
169 struct Channel *chptr;
171 struct JoinBuf create;
174 unsigned int flags = 0;
181 if (parc < 2 || *parv[1] == '\0')
182 return need_more_params(sptr, "JOIN");
184 joinbuf_init(&join, sptr, cptr, JOINBUF_TYPE_JOIN, 0, 0);
185 joinbuf_init(&create, sptr, cptr, JOINBUF_TYPE_CREATE, 0, TStime());
187 chanlist = last0(parv[1]); /* find last "JOIN 0" */
189 keys = parv[2]; /* remember where keys are */
191 for (name = ircd_strtok(&p, chanlist, ","); name;
192 name = ircd_strtok(&p, 0, ",")) {
193 clean_channelname(name);
195 if (join0(&join, cptr, sptr, name)) /* did client do a JOIN 0? */
198 if (!IsChannelName(name))
200 /* bad channel name */
201 send_reply(sptr, ERR_NOSUCHCHANNEL, name);
205 /* This checks if the channel contains control codes and rejects em
206 * until they are gone, then we will do it otherwise - *SOB Mode*
208 for (k = 0, j = 0; name[j]; j++)
209 if (IsCntrl(name[j]))
213 send_reply(sptr, ERR_NOSUCHCHANNEL, name);
217 /* BADCHANed channel */
218 if ((gline = gline_find(name, GLINE_BADCHAN | GLINE_EXACT)) &&
219 GlineIsActive(gline) && !IsAnOper(sptr)) {
220 send_reply(sptr, ERR_BANNEDFROMCHAN, name);
224 if ((chptr = FindChannel(name)))
226 if (find_member_link(chptr, sptr))
227 continue; /* already on channel */
229 flags = CHFL_DEOPPED;
234 /* disallow creating local channels */
235 if ((name[0] == '&') && !chptr && !feature_bool(FEAT_LOCAL_CHANNELS)) {
236 send_reply(sptr, ERR_NOSUCHCHANNEL, name);
240 if (cli_user(sptr)->joined >= feature_int(FEAT_MAXCHANNELSPERUSER) &&
241 !HasPriv(sptr, PRIV_CHAN_LIMIT)) {
242 send_reply(sptr, ERR_TOOMANYCHANNELS, chptr ? chptr->chname : name);
243 break; /* no point processing the other channels */
247 int is_level0_op = 0;
248 if (!BadPtr(keys) && *chptr->mode.apass) {
249 /* Don't use compall for the apass, only a single key is allowed.
250 Test Apass first in case someone set Apass and upass equal. */
251 if (strcmp(chptr->mode.apass, keys) == 0) {
253 flags &= ~CHFL_DEOPPED;
254 flags |= CHFL_CHANOP | CHFL_CHANNEL_MANAGER;
256 else if (*chptr->mode.upass && strcmp(chptr->mode.upass, keys) == 0) {
258 flags &= ~CHFL_DEOPPED;
259 flags |= CHFL_CHANOP;
262 if (check_target_limit(sptr, chptr, chptr->chname, 0))
263 continue; /* exceeded target limit */
264 else if (!is_level0_op && (i = can_join(sptr, chptr, keys))) {
265 if (i > MAGIC_OPER_OVERRIDE)
266 { /* oper overrode mode */
267 switch (i - MAGIC_OPER_OVERRIDE)
269 case ERR_CHANNELISFULL: /* figure out which mode */
273 case ERR_INVITEONLYCHAN:
277 case ERR_BANNEDFROMCHAN:
281 case ERR_BADCHANNELKEY:
285 case ERR_NEEDREGGEDNICK:
294 /* send accountability notice */
295 sendto_opmask_butone(0, SNO_HACK4, "OPER JOIN: %C JOIN %H "
296 "(overriding +%c)", sptr, chptr, i);
300 send_reply(sptr, i, chptr->chname);
303 } /* else if ((i = can_join(sptr, chptr, keys))) */
305 joinbuf_join(&join, chptr, flags);
308 joinbuf_flush(&join);
309 modebuf_init(&mbuf, &me, cptr, chptr,
310 MODEBUF_DEST_CHANNEL | /* Send mode to channel */
311 MODEBUF_DEST_SERVER); /* And send it to the other servers */
312 modebuf_mode_client(&mbuf,
313 MODE_ADD | MODE_CHANOP, sptr); /* Give ops to the level0 op */
314 modebuf_flush(&mbuf);
316 } else if (!(chptr = get_channel(sptr, name, CGT_CREATE)))
317 continue; /* couldn't get channel */
318 else if (check_target_limit(sptr, chptr, chptr->chname, 1))
320 /* Note: check_target_limit will only ever return 0 here */
322 destruct_channel(chptr); /* created it... */
326 joinbuf_join(&create, chptr, flags | CHFL_CHANNEL_MANAGER);
328 del_invite(sptr, chptr);
330 if (chptr->topic[0]) {
331 send_reply(sptr, RPL_TOPIC, chptr->chname, chptr->topic);
332 send_reply(sptr, RPL_TOPICWHOTIME, chptr->chname, chptr->topic_nick,
336 do_names(sptr, chptr, NAMES_ALL|NAMES_EON); /* send /names list */
339 joinbuf_flush(&join); /* must be first, if there's a JOIN 0 */
340 joinbuf_flush(&create);
346 * ms_join - server message handler
348 int ms_join(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
350 struct Membership *member;
351 struct Channel *chptr;
353 unsigned int flags = 0;
361 return protocol_violation(cptr,
362 "%s tried to JOIN %s, duh!",
364 (parc < 2 || *parv[1] == '\0') ? "a channel" :
369 if (parc < 2 || *parv[1] == '\0')
370 return need_more_params(sptr, "JOIN");
372 if (parc > 2 && parv[2])
373 creation = atoi(parv[2]);
375 joinbuf_init(&join, sptr, cptr, JOINBUF_TYPE_JOIN, 0, 0);
377 chanlist = last0(parv[1]); /* find last "JOIN 0" */
379 for (name = ircd_strtok(&p, chanlist, ","); name;
380 name = ircd_strtok(&p, 0, ",")) {
381 clean_channelname(name);
383 if (join0(&join, cptr, sptr, name)) /* did client do a JOIN 0? */
386 if (IsLocalChannel(name) || !IsChannelName(name))
388 protocol_violation(cptr, "%s tried to join %s", cli_name(sptr), name);
392 if (!(chptr = FindChannel(name)))
394 /* No channel exists, so create one */
395 if (!(chptr = get_channel(sptr, name, CGT_CREATE)))
397 protocol_violation(sptr,"couldn't get channel %s for %s",
398 name,cli_name(sptr));
401 flags = CHFL_DEOPPED | (HasFlag(sptr, FLAG_TS8) ? CHFL_SERVOPOK : 0);
403 /* when the network is 2.10.11+ then remove MAGIC_REMOTE_JOIN_TS */
404 chptr->creationtime = creation ? creation : MAGIC_REMOTE_JOIN_TS;
406 else { /* We have a valid channel? */
407 if ((member = find_member_link(chptr, sptr)))
409 /* It is impossible to get here --Run */
410 if (!IsZombie(member)) /* already on channel */
413 flags = member->status & (CHFL_DEOPPED | CHFL_SERVOPOK);
414 remove_user_from_channel(sptr, chptr);
415 chptr = FindChannel(name);
418 flags = CHFL_DEOPPED | (HasFlag(sptr, FLAG_TS8) ? CHFL_SERVOPOK : 0);
419 /* Always copy the timestamp when it is older, that is the only way to
420 ensure network-wide synchronization of creation times. */
421 if (creation && creation < chptr->creationtime)
422 chptr->creationtime = creation;
425 joinbuf_join(&join, chptr, flags);
428 joinbuf_flush(&join); /* flush joins... */