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.
33 #include "ircd_chattr.h"
34 #include "ircd_features.h"
36 #include "ircd_reply.h"
37 #include "ircd_string.h"
45 /* #include <assert.h> -- Now using assert in ircd_log.h */
49 /** Searches for and handles a 0 in a join list.
50 * @param[in] cptr Client that sent us the message.
51 * @param[in] sptr Original source of message.
52 * @param[in] chanlist List of channels to join.
53 * @return First token in \a chanlist after the final 0 entry, which
54 * may be its nul terminator (if the final entry is a 0 entry).
57 last0(struct Client *cptr, struct Client *sptr, char *chanlist)
62 for (p = chanlist; p[0]; p++) /* find last "JOIN 0" */
63 if (p[0] == '0' && (p[1] == ',' || p[1] == '\0' || !IsChannelChar(p[1]))) {
69 while (p[0] != ',' && p[0] != '\0') /* skip past channel name */
72 if (!p[0]) /* hit the end */
78 struct Membership *member;
80 joinbuf_init(&part, sptr, cptr, JOINBUF_TYPE_PARTALL,
81 "Left all channels", 0);
83 joinbuf_join(&part, 0, 0);
85 while ((member = cli_user(sptr)->channel))
86 joinbuf_join(&part, member->channel,
87 IsZombie(member) ? CHFL_ZOMBIE :
88 IsDelayedJoin(member) ? CHFL_DELAYED :
97 /** Handle a JOIN message from a client connection.
98 * See @ref m_functions for discussion of the arguments.
99 * @param[in] cptr Client that sent us the message.
100 * @param[in] sptr Original source of message.
101 * @param[in] parc Number of arguments.
102 * @param[in] parv Argument vector.
104 int m_join(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
106 struct Channel *chptr;
108 struct JoinBuf create;
115 if (parc < 2 || *parv[1] == '\0')
116 return need_more_params(sptr, "JOIN");
118 joinbuf_init(&join, sptr, cptr, JOINBUF_TYPE_JOIN, 0, 0);
119 joinbuf_init(&create, sptr, cptr, JOINBUF_TYPE_CREATE, 0, TStime());
121 chanlist = last0(cptr, sptr, parv[1]); /* find last "JOIN 0" */
123 keys = parv[2]; /* remember where keys are */
125 for (name = ircd_strtok(&p, chanlist, ","); name;
126 name = ircd_strtok(&p, 0, ",")) {
129 /* If we have any more keys, take the first for this channel. */
131 && (keys = strchr(key = keys, ',')))
134 /* Empty keys are the same as no keys. */
138 if (!IsChannelName(name) || !strIsIrcCh(name))
140 /* bad channel name */
141 send_reply(sptr, ERR_NOSUCHCHANNEL, name);
145 if (cli_user(sptr)->joined >= feature_int(FEAT_MAXCHANNELSPERUSER)
146 && !HasPriv(sptr, PRIV_CHAN_LIMIT)) {
147 send_reply(sptr, ERR_TOOMANYCHANNELS, name);
148 break; /* no point processing the other channels */
151 /* BADCHANed channel */
152 if ((gline = gline_find(name, GLINE_BADCHAN | GLINE_EXACT)) &&
153 GlineIsActive(gline) && !IsAnOper(sptr)) {
154 send_reply(sptr, ERR_BANNEDFROMCHAN, name);
158 if (!(chptr = FindChannel(name))) {
159 if ((name[0] == '&') && !feature_bool(FEAT_LOCAL_CHANNELS)) {
160 send_reply(sptr, ERR_NOSUCHCHANNEL, name);
164 if (!(chptr = get_channel(sptr, name, CGT_CREATE)))
167 /* Try to add the new channel as a recent target for the user. */
168 if (check_target_limit(sptr, chptr, chptr->chname, 1)) {
170 destruct_channel(chptr);
174 joinbuf_join(&create, chptr, CHFL_CHANOP | CHFL_CHANNEL_MANAGER);
175 } else if (find_member_link(chptr, sptr)) {
176 continue; /* already on channel */
177 } else if (check_target_limit(sptr, chptr, chptr->chname, 0)) {
180 int flags = CHFL_DEOPPED;
183 /* Check target change limits. */
185 /* Check Apass/Upass -- since we only ever look at a single
186 * "key" per channel now, this hampers brute force attacks. */
187 if (key && !strcmp(key, chptr->mode.apass))
188 flags = CHFL_CHANOP | CHFL_CHANNEL_MANAGER;
189 else if (key && !strcmp(key, chptr->mode.upass))
191 else if (IsInvited(sptr, chptr)) {
192 /* Invites bypass these other checks. */
193 } else if (chptr->mode.mode & MODE_INVITEONLY)
194 err = ERR_INVITEONLYCHAN;
195 else if (chptr->mode.limit && (chptr->users >= chptr->mode.limit))
196 err = ERR_CHANNELISFULL;
197 else if ((chptr->mode.mode & MODE_REGONLY) && !IsAccount(sptr))
198 err = ERR_NEEDREGGEDNICK;
199 else if (find_ban(sptr, chptr->banlist))
200 err = ERR_BANNEDFROMCHAN;
201 else if (*chptr->mode.key && (!key || strcmp(key, chptr->mode.key)))
202 err = ERR_BADCHANNELKEY;
204 /* An oper with WALK_LCHAN privilege can join a local channel
205 * he otherwise could not join by using "OVERRIDE" as the key.
206 * This will generate a HACK(4) notice, but fails if the oper
207 * could normally join the channel. */
208 if (IsLocalChannel(chptr->chname)
209 && HasPriv(sptr, PRIV_WALK_LCHAN)
210 && !(flags & CHFL_CHANOP)
211 && key && !strcmp(key, "OVERRIDE")
212 && strcmp(chptr->mode.key, "OVERRIDE"))
216 send_reply(sptr, ERR_DONTCHEAT, chptr->chname);
218 case ERR_INVITEONLYCHAN: err = 'i'; break;
219 case ERR_CHANNELISFULL: err = 'l'; break;
220 case ERR_BANNEDFROMCHAN: err = 'b'; break;
221 case ERR_BADCHANNELKEY: err = 'k'; break;
222 case ERR_NEEDREGGEDNICK: err = 'r'; break;
223 default: err = '?'; break;
225 /* send accountability notice */
226 sendto_opmask_butone(0, SNO_HACK4, "OPER JOIN: %C JOIN %H "
227 "(overriding +%c)", sptr, chptr, err);
231 /* Is there some reason the user may not join? */
233 send_reply(sptr, err, chptr->chname);
237 joinbuf_join(&join, chptr, flags);
240 del_invite(sptr, chptr);
242 if (chptr->topic[0]) {
243 send_reply(sptr, RPL_TOPIC, chptr->chname, chptr->topic);
244 send_reply(sptr, RPL_TOPICWHOTIME, chptr->chname, chptr->topic_nick,
248 do_names(sptr, chptr, NAMES_ALL|NAMES_EON); /* send /names list */
251 joinbuf_flush(&join); /* must be first, if there's a JOIN 0 */
252 joinbuf_flush(&create);
257 /** Handle a JOIN message from a server connection.
258 * See @ref m_functions for discussion of the arguments.
259 * @param[in] cptr Client that sent us the message.
260 * @param[in] sptr Original source of message.
261 * @param[in] parc Number of arguments.
262 * @param[in] parv Argument vector.
264 int ms_join(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
266 struct Membership *member;
267 struct Channel *chptr;
277 return protocol_violation(cptr,
278 "%s tried to JOIN %s, duh!",
280 (parc < 2 || *parv[1] == '\0') ? "a channel" :
285 if (parc < 2 || *parv[1] == '\0')
286 return need_more_params(sptr, "JOIN");
288 if (parc > 2 && parv[2])
289 creation = atoi(parv[2]);
291 joinbuf_init(&join, sptr, cptr, JOINBUF_TYPE_JOIN, 0, 0);
293 chanlist = last0(cptr, sptr, parv[1]); /* find last "JOIN 0" */
295 for (name = ircd_strtok(&p, chanlist, ","); name;
296 name = ircd_strtok(&p, 0, ",")) {
298 if (name[0] == '0' && name[1] == ':')
300 flags = CHFL_CHANOP | CHFL_CHANNEL_MANAGER;
303 else if (name[0] == '1' && name[1] == ':')
309 flags = CHFL_DEOPPED;
311 if (IsLocalChannel(name) || !IsChannelName(name))
313 protocol_violation(cptr, "%s tried to join %s", cli_name(sptr), name);
317 if (!(chptr = FindChannel(name)))
319 /* No channel exists, so create one */
320 if (!(chptr = get_channel(sptr, name, CGT_CREATE)))
322 protocol_violation(sptr,"couldn't get channel %s for %s",
323 name,cli_name(sptr));
326 flags |= HasFlag(sptr, FLAG_TS8) ? CHFL_SERVOPOK : 0;
328 /* when the network is 2.10.11+ then remove MAGIC_REMOTE_JOIN_TS */
329 chptr->creationtime = creation ? creation : MAGIC_REMOTE_JOIN_TS;
331 else { /* We have a valid channel? */
332 if ((member = find_member_link(chptr, sptr)))
334 /* It is impossible to get here --Run */
335 if (!IsZombie(member)) /* already on channel */
338 flags = member->status & (CHFL_DEOPPED | CHFL_SERVOPOK);
339 remove_user_from_channel(sptr, chptr);
340 chptr = FindChannel(name);
343 flags |= HasFlag(sptr, FLAG_TS8) ? CHFL_SERVOPOK : 0;
344 /* Always copy the timestamp when it is older, that is the only way to
345 ensure network-wide synchronization of creation times. */
346 if (creation && creation < chptr->creationtime)
347 chptr->creationtime = creation;
350 joinbuf_join(&join, chptr, flags);
353 joinbuf_flush(&join); /* flush joins... */