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 clean_channelname(name);
139 if (!IsChannelName(name))
141 /* bad channel name */
142 send_reply(sptr, ERR_NOSUCHCHANNEL, name);
146 if (cli_user(sptr)->joined >= feature_int(FEAT_MAXCHANNELSPERUSER)
147 && !HasPriv(sptr, PRIV_CHAN_LIMIT)) {
148 send_reply(sptr, ERR_TOOMANYCHANNELS, name);
149 break; /* no point processing the other channels */
152 /* BADCHANed channel */
153 if ((gline = gline_find(name, GLINE_BADCHAN | GLINE_EXACT)) &&
154 GlineIsActive(gline) && !IsAnOper(sptr)) {
155 send_reply(sptr, ERR_BANNEDFROMCHAN, name);
159 if (!(chptr = FindChannel(name))) {
160 if ((name[0] == '&') && !feature_bool(FEAT_LOCAL_CHANNELS)) {
161 send_reply(sptr, ERR_NOSUCHCHANNEL, name);
165 if (!(chptr = get_channel(sptr, name, CGT_CREATE)))
168 /* Try to add the new channel as a recent target for the user. */
169 if (check_target_limit(sptr, chptr, chptr->chname, 1)) {
171 destruct_channel(chptr);
175 joinbuf_join(&create, chptr, CHFL_CHANOP | CHFL_CHANNEL_MANAGER);
176 } else if (find_member_link(chptr, sptr)) {
177 continue; /* already on channel */
178 } else if (check_target_limit(sptr, chptr, chptr->chname, 0)) {
181 int flags = CHFL_DEOPPED;
184 /* Check target change limits. */
186 /* Check Apass/Upass -- since we only ever look at a single
187 * "key" per channel now, this hampers brute force attacks. */
188 if (key && !strcmp(key, chptr->mode.apass))
189 flags = CHFL_CHANOP | CHFL_CHANNEL_MANAGER;
190 else if (key && !strcmp(key, chptr->mode.upass))
192 else if (IsInvited(sptr, chptr)) {
193 /* Invites bypass these other checks. */
194 } else if (chptr->mode.mode & MODE_INVITEONLY)
195 err = ERR_INVITEONLYCHAN;
196 else if (chptr->mode.limit && (chptr->users >= chptr->mode.limit))
197 err = ERR_CHANNELISFULL;
198 else if ((chptr->mode.mode & MODE_REGONLY) && !IsAccount(sptr))
199 err = ERR_NEEDREGGEDNICK;
200 else if (find_ban(sptr, chptr->banlist))
201 err = ERR_BANNEDFROMCHAN;
202 else if (*chptr->mode.key && (!key || strcmp(key, chptr->mode.key)))
203 err = ERR_BADCHANNELKEY;
205 /* An oper with WALK_LCHAN privilege can join a local channel
206 * he otherwise could not join by using "OVERRIDE" as the key.
207 * This will generate a HACK(4) notice, but fails if the oper
208 * could normally join the channel. */
209 if (IsLocalChannel(chptr->chname)
210 && HasPriv(sptr, PRIV_WALK_LCHAN)
211 && !(flags & CHFL_CHANOP)
212 && key && !strcmp(key, "OVERRIDE")
213 && strcmp(chptr->mode.key, "OVERRIDE"))
217 send_reply(sptr, ERR_DONTCHEAT, chptr->chname);
219 case ERR_INVITEONLYCHAN: err = 'i'; break;
220 case ERR_CHANNELISFULL: err = 'l'; break;
221 case ERR_BANNEDFROMCHAN: err = 'b'; break;
222 case ERR_BADCHANNELKEY: err = 'k'; break;
223 case ERR_NEEDREGGEDNICK: err = 'r'; break;
224 default: err = '?'; break;
226 /* send accountability notice */
227 sendto_opmask_butone(0, SNO_HACK4, "OPER JOIN: %C JOIN %H "
228 "(overriding +%c)", sptr, chptr, err);
232 /* Is there some reason the user may not join? */
234 send_reply(sptr, err, chptr->chname);
238 joinbuf_join(&join, chptr, flags);
241 del_invite(sptr, chptr);
243 if (chptr->topic[0]) {
244 send_reply(sptr, RPL_TOPIC, chptr->chname, chptr->topic);
245 send_reply(sptr, RPL_TOPICWHOTIME, chptr->chname, chptr->topic_nick,
249 do_names(sptr, chptr, NAMES_ALL|NAMES_EON); /* send /names list */
252 joinbuf_flush(&join); /* must be first, if there's a JOIN 0 */
253 joinbuf_flush(&create);
258 /** Handle a JOIN message from a server connection.
259 * See @ref m_functions for discussion of the arguments.
260 * @param[in] cptr Client that sent us the message.
261 * @param[in] sptr Original source of message.
262 * @param[in] parc Number of arguments.
263 * @param[in] parv Argument vector.
265 int ms_join(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
267 struct Membership *member;
268 struct Channel *chptr;
278 return protocol_violation(cptr,
279 "%s tried to JOIN %s, duh!",
281 (parc < 2 || *parv[1] == '\0') ? "a channel" :
286 if (parc < 2 || *parv[1] == '\0')
287 return need_more_params(sptr, "JOIN");
289 if (parc > 2 && parv[2])
290 creation = atoi(parv[2]);
292 joinbuf_init(&join, sptr, cptr, JOINBUF_TYPE_JOIN, 0, 0);
294 chanlist = last0(cptr, sptr, parv[1]); /* find last "JOIN 0" */
296 for (name = ircd_strtok(&p, chanlist, ","); name;
297 name = ircd_strtok(&p, 0, ",")) {
299 if (name[0] == '0' && name[1] == ':')
301 flags = CHFL_CHANOP | CHFL_CHANNEL_MANAGER;
304 else if (name[0] == '1' && name[1] == ':')
310 flags = CHFL_DEOPPED;
312 if (IsLocalChannel(name) || !IsChannelName(name))
314 protocol_violation(cptr, "%s tried to join %s", cli_name(sptr), name);
318 if (!(chptr = FindChannel(name)))
320 /* No channel exists, so create one */
321 if (!(chptr = get_channel(sptr, name, CGT_CREATE)))
323 protocol_violation(sptr,"couldn't get channel %s for %s",
324 name,cli_name(sptr));
327 flags |= HasFlag(sptr, FLAG_TS8) ? CHFL_SERVOPOK : 0;
329 /* when the network is 2.10.11+ then remove MAGIC_REMOTE_JOIN_TS */
330 chptr->creationtime = creation ? creation : MAGIC_REMOTE_JOIN_TS;
332 else { /* We have a valid channel? */
333 if ((member = find_member_link(chptr, sptr)))
335 /* It is impossible to get here --Run */
336 if (!IsZombie(member)) /* already on channel */
339 flags = member->status & (CHFL_DEOPPED | CHFL_SERVOPOK);
340 remove_user_from_channel(sptr, chptr);
341 chptr = FindChannel(name);
344 flags |= HasFlag(sptr, FLAG_TS8) ? CHFL_SERVOPOK : 0;
345 /* Always copy the timestamp when it is older, that is the only way to
346 ensure network-wide synchronization of creation times. */
347 if (creation && creation < chptr->creationtime)
348 chptr->creationtime = creation;
351 joinbuf_join(&join, chptr, flags);
354 joinbuf_flush(&join); /* flush joins... */