2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2017 B00mX0r <b00mx0r@aureus.pw>
5 * Copyright (C) 2013-2014, 2016-2020 Sadie Powell <sadie@witchery.services>
6 * Copyright (C) 2013 Adam <Adam@anope.org>
7 * Copyright (C) 2012-2016, 2018 Attila Molnar <attilamolnar@hush.com>
8 * Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
9 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
10 * Copyright (C) 2007-2009 Robin Burchell <robin+git@viroteck.net>
11 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
12 * Copyright (C) 2006-2008, 2010 Craig Edwards <brain@inspircd.org>
14 * This file is part of InspIRCd. InspIRCd is free software: you can
15 * redistribute it and/or modify it under the terms of the GNU General Public
16 * License as published by the Free Software Foundation, version 2.
18 * This program is distributed in the hope that it will be useful, but WITHOUT
19 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 ChanModeReference ban(NULL, "ban");
36 Channel::Channel(const std::string &cname, time_t ts)
37 : name(cname), age(ts), topicset(0)
39 if (!ServerInstance->chanlist.insert(std::make_pair(cname, this)).second)
40 throw CoreException("Cannot create duplicate channel " + cname);
43 void Channel::SetMode(ModeHandler* mh, bool on)
45 if (mh && mh->GetId() != ModeParser::MODEID_MAX)
46 modes[mh->GetId()] = on;
49 void Channel::SetTopic(User* u, const std::string& ntopic, time_t topicts, const std::string* setter)
51 // Send a TOPIC message to the channel only if the new topic text differs
52 if (this->topic != ntopic)
55 ClientProtocol::Messages::Topic topicmsg(u, this, this->topic);
56 Write(ServerInstance->GetRFCEvents().topic, topicmsg);
59 // Always update setter and set time
61 setter = ServerInstance->Config->FullHostInTopic ? &u->GetFullHost() : &u->nick;
62 this->setby.assign(*setter, 0, ServerInstance->Config->Limits.GetMaxMask());
63 this->topicset = topicts;
65 FOREACH_MOD(OnPostTopicChange, (u, this, this->topic));
68 Membership* Channel::AddUser(User* user)
70 std::pair<MemberMap::iterator, bool> ret = userlist.insert(std::make_pair(user, insp::aligned_storage<Membership>()));
74 Membership* memb = new(ret.first->second) Membership(user, this);
78 void Channel::DelUser(User* user)
80 MemberMap::iterator it = userlist.find(user);
81 if (it != userlist.end())
85 void Channel::CheckDestroy()
87 if (!userlist.empty())
91 FIRST_MOD_RESULT(OnChannelPreDelete, res, (this));
92 if (res == MOD_RES_DENY)
95 // If the channel isn't in chanlist then it is already in the cull list, don't add it again
96 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
97 if ((iter == ServerInstance->chanlist.end()) || (iter->second != this))
100 FOREACH_MOD(OnChannelDelete, (this));
101 ServerInstance->chanlist.erase(iter);
102 ServerInstance->GlobalCulls.AddItem(this);
105 void Channel::DelUser(const MemberMap::iterator& membiter)
107 Membership* memb = membiter->second;
110 userlist.erase(membiter);
112 // If this channel became empty then it should be removed
116 Membership* Channel::GetUser(User* user)
118 MemberMap::iterator i = userlist.find(user);
119 if (i == userlist.end())
124 void Channel::SetDefaultModes()
126 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "SetDefaultModes %s",
127 ServerInstance->Config->DefaultModes.c_str());
128 irc::spacesepstream list(ServerInstance->Config->DefaultModes);
130 std::string parameter;
132 list.GetToken(modeseq);
134 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
136 ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
139 if (mode->IsPrefixMode())
142 if (mode->NeedsParam(true))
144 list.GetToken(parameter);
145 // If the parameter begins with a ':' then it's invalid
146 if (parameter.c_str()[0] == ':')
152 if ((mode->NeedsParam(true)) && (parameter.empty()))
155 mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, this, parameter, true);
161 * add a channel to a user, creating the record for it if needed and linking
162 * it to the user record
164 Channel* Channel::JoinUser(LocalUser* user, std::string cname, bool override, const std::string& key)
166 if (user->registered != REG_ALL)
168 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "Attempted to join unregistered user " + user->uuid + " to channel " + cname);
173 * We don't restrict the number of channels that remote users or users that are override-joining may be in.
174 * We restrict local users to <connect:maxchans> channels.
175 * We restrict local operators to <oper:maxchans> channels.
176 * This is a lot more logical than how it was formerly. -- w00t
180 unsigned int maxchans = user->GetClass()->maxchans;
182 maxchans = ServerInstance->Config->MaxChans;
185 unsigned int opermaxchans = ConvToNum<unsigned int>(user->oper->getConfig("maxchans"));
186 // If not set, use 2.0's <channels:opers>, if that's not set either, use limit from CC
187 if (!opermaxchans && user->HasPrivPermission("channels/high-join-limit"))
188 opermaxchans = ServerInstance->Config->OperMaxChans;
190 maxchans = opermaxchans;
192 if (user->chans.size() >= maxchans)
194 user->WriteNumeric(ERR_TOOMANYCHANNELS, cname, "You are on too many channels");
199 // Crop channel name if it's too long
200 if (cname.length() > ServerInstance->Config->Limits.ChanMax)
201 cname.resize(ServerInstance->Config->Limits.ChanMax);
203 Channel* chan = ServerInstance->FindChan(cname);
204 bool created_by_local = (chan == NULL); // Flag that will be passed to modules in the OnUserJoin() hook later
205 std::string privs; // Prefix mode(letter)s to give to the joining user
209 privs = ServerInstance->Config->DefaultModes.substr(0, ServerInstance->Config->DefaultModes.find(' '));
211 if (override == false)
213 // Ask the modules whether they're ok with the join, pass NULL as Channel* as the channel is yet to be created
214 ModResult MOD_RESULT;
215 FIRST_MOD_RESULT(OnUserPreJoin, MOD_RESULT, (user, NULL, cname, privs, key));
216 if (MOD_RESULT == MOD_RES_DENY)
217 return NULL; // A module wasn't happy with the join, abort
220 chan = new Channel(cname, ServerInstance->Time());
221 // Set the default modes on the channel (<options:defaultmodes>)
222 chan->SetDefaultModes();
226 /* Already on the channel */
227 if (chan->HasUser(user))
230 if (override == false)
232 ModResult MOD_RESULT;
233 FIRST_MOD_RESULT(OnUserPreJoin, MOD_RESULT, (user, chan, cname, privs, key));
235 // A module explicitly denied the join and (hopefully) generated a message
236 // describing the situation, so we may stop here without sending anything
237 if (MOD_RESULT == MOD_RES_DENY)
240 // If no module returned MOD_RES_DENY or MOD_RES_ALLOW (which is the case
241 // most of the time) then proceed to check channel bans.
243 // If a module explicitly allowed the join (by returning MOD_RES_ALLOW),
244 // then this entire section is skipped
245 if (MOD_RESULT == MOD_RES_PASSTHRU)
247 if (chan->IsBanned(user))
249 user->WriteNumeric(ERR_BANNEDFROMCHAN, chan->name, "Cannot join channel (you're banned)");
256 // We figured that this join is allowed and also created the
257 // channel if it didn't exist before, now do the actual join
258 chan->ForceJoin(user, &privs, false, created_by_local);
262 Membership* Channel::ForceJoin(User* user, const std::string* privs, bool bursting, bool created_by_local)
266 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "Attempted to join server user " + user->uuid + " to channel " + this->name);
270 Membership* memb = this->AddUser(user);
272 return NULL; // Already on the channel
274 user->chans.push_front(memb);
278 // If the user was granted prefix modes (in the OnUserPreJoin hook, or they're a
279 // remote user and their own server set the modes), then set them internally now
280 for (std::string::const_iterator i = privs->begin(); i != privs->end(); ++i)
282 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(*i);
285 std::string nick = user->nick;
286 // Set the mode on the user
287 mh->OnModeChange(ServerInstance->FakeClient, NULL, this, nick, true);
292 // Tell modules about this join, they have the chance now to populate except_list with users we won't send the JOIN (and possibly MODE) to
294 FOREACH_MOD(OnUserJoin, (memb, bursting, created_by_local, except_list));
296 ClientProtocol::Events::Join joinevent(memb);
297 this->Write(joinevent, 0, except_list);
299 FOREACH_MOD(OnPostJoin, (memb));
303 bool Channel::IsBanned(User* user)
306 FIRST_MOD_RESULT(OnCheckChannelBan, result, (user, this));
308 if (result != MOD_RES_PASSTHRU)
309 return (result == MOD_RES_DENY);
311 ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
315 const ListModeBase::ModeList* bans = banlm->GetList(this);
318 for (ListModeBase::ModeList::const_iterator it = bans->begin(); it != bans->end(); it++)
320 if (CheckBan(user, it->mask))
327 bool Channel::CheckBan(User* user, const std::string& mask)
330 FIRST_MOD_RESULT(OnCheckBan, result, (user, this, mask));
331 if (result != MOD_RES_PASSTHRU)
332 return (result == MOD_RES_DENY);
334 // extbans were handled above, if this is one it obviously didn't match
335 if ((mask.length() <= 2) || (mask[1] == ':'))
338 std::string::size_type at = mask.find('@');
339 if (at == std::string::npos)
342 const std::string nickIdent = user->nick + "!" + user->ident;
343 std::string prefix(mask, 0, at);
344 if (InspIRCd::Match(nickIdent, prefix, NULL))
346 std::string suffix(mask, at + 1);
347 if (InspIRCd::Match(user->GetRealHost(), suffix, NULL) ||
348 InspIRCd::Match(user->GetDisplayedHost(), suffix, NULL) ||
349 InspIRCd::MatchCIDR(user->GetIPString(), suffix, NULL))
355 ModResult Channel::GetExtBanStatus(User *user, char type)
358 FIRST_MOD_RESULT(OnExtBanCheck, rv, (user, this, type));
359 if (rv != MOD_RES_PASSTHRU)
362 ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
364 return MOD_RES_PASSTHRU;
366 const ListModeBase::ModeList* bans = banlm->GetList(this);
369 for (ListModeBase::ModeList::const_iterator it = bans->begin(); it != bans->end(); ++it)
371 if (it->mask.length() <= 2 || it->mask[0] != type || it->mask[1] != ':')
374 if (CheckBan(user, it->mask.substr(2)))
378 return MOD_RES_PASSTHRU;
382 * Remove a channel from a users record, remove the reference to the Membership object
383 * from the channel and destroy it.
385 bool Channel::PartUser(User* user, std::string& reason)
387 MemberMap::iterator membiter = userlist.find(user);
389 if (membiter == userlist.end())
392 Membership* memb = membiter->second;
394 FOREACH_MOD(OnUserPart, (memb, reason, except_list));
396 ClientProtocol::Messages::Part partmsg(memb, reason);
397 Write(ServerInstance->GetRFCEvents().part, partmsg, 0, except_list);
399 // Remove this channel from the user's chanlist
400 user->chans.erase(memb);
401 // Remove the Membership from this channel's userlist and destroy it
402 this->DelUser(membiter);
407 void Channel::KickUser(User* src, const MemberMap::iterator& victimiter, const std::string& reason)
409 Membership* memb = victimiter->second;
411 FOREACH_MOD(OnUserKick, (src, memb, reason, except_list));
413 ClientProtocol::Messages::Kick kickmsg(src, memb, reason);
414 Write(ServerInstance->GetRFCEvents().kick, kickmsg, 0, except_list);
416 memb->user->chans.erase(memb);
417 this->DelUser(victimiter);
420 void Channel::Write(ClientProtocol::Event& protoev, char status, const CUList& except_list)
422 unsigned int minrank = 0;
425 PrefixMode* mh = ServerInstance->Modes->FindPrefix(status);
427 minrank = mh->GetPrefixRank();
429 for (MemberMap::iterator i = userlist.begin(); i != userlist.end(); i++)
431 LocalUser* user = IS_LOCAL(i->first);
432 if ((user) && (!except_list.count(user)))
434 /* User doesn't have the status we're after */
435 if (minrank && i->second->getRank() < minrank)
443 const char* Channel::ChanModes(bool showsecret)
445 static std::string scratch;
450 /* This was still iterating up to 190, Channel::modes is only 64 elements -- Om */
451 for(int n = 0; n < 64; n++)
453 ModeHandler* mh = ServerInstance->Modes->FindMode(n + 65, MODETYPE_CHANNEL);
454 if (mh && IsModeSet(mh))
456 scratch.push_back(n + 65);
458 ParamModeBase* pm = mh->IsParameterMode();
462 if (pm->IsParameterSecret() && !showsecret)
464 sparam += " <" + pm->name + ">";
469 pm->GetParameter(this, sparam);
475 return scratch.c_str();
478 void Channel::WriteNotice(const std::string& text, char status)
480 ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, ServerInstance->FakeClient, this, text, MSG_NOTICE, status);
481 Write(ServerInstance->GetRFCEvents().privmsg, privmsg);
484 void Channel::WriteRemoteNotice(const std::string& text, char status)
486 WriteNotice(text, status);
487 ServerInstance->PI->SendMessage(this, status, text, MSG_NOTICE);
490 /* returns the status character for a given user on a channel, e.g. @ for op,
491 * % for halfop etc. If the user has several modes set, the highest mode
492 * the user has must be returned.
494 char Membership::GetPrefixChar() const
497 unsigned int bestrank = 0;
499 for (std::string::const_iterator i = modes.begin(); i != modes.end(); ++i)
501 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(*i);
502 if (mh && mh->GetPrefixRank() > bestrank && mh->GetPrefix())
504 bestrank = mh->GetPrefixRank();
505 pf = mh->GetPrefix();
511 unsigned int Membership::getRank()
513 char mchar = modes.c_str()[0];
517 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(mchar);
519 rv = mh->GetPrefixRank();
524 std::string Membership::GetAllPrefixChars() const
527 for (std::string::const_iterator i = modes.begin(); i != modes.end(); ++i)
529 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(*i);
530 if (mh && mh->GetPrefix())
531 ret.push_back(mh->GetPrefix());
537 unsigned int Channel::GetPrefixValue(User* user)
539 MemberMap::iterator m = userlist.find(user);
540 if (m == userlist.end())
542 return m->second->getRank();
545 bool Membership::SetPrefix(PrefixMode* delta_mh, bool adding)
547 char prefix = delta_mh->GetModeChar();
548 for (unsigned int i = 0; i < modes.length(); i++)
550 char mchar = modes[i];
551 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(mchar);
552 if (mh && mh->GetPrefixRank() <= delta_mh->GetPrefixRank())
554 modes = modes.substr(0,i) +
555 (adding ? std::string(1, prefix) : "") +
556 modes.substr(mchar == prefix ? i+1 : i);
557 return adding != (mchar == prefix);
561 modes.push_back(prefix);
566 void Membership::WriteNotice(const std::string& text) const
568 LocalUser* const localuser = IS_LOCAL(user);
572 ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, ServerInstance->FakeClient, this->chan, text, MSG_NOTICE);
573 localuser->Send(ServerInstance->GetRFCEvents().privmsg, privmsg);