2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2006-2008 Robin Burchell <robin+git@viroteck.net>
6 * Copyright (C) 2006, 2008 Oliver Lupton <oliverlupton@gmail.com>
7 * Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
8 * Copyright (C) 2003-2008 Craig Edwards <craigedwards@brainbox.cc>
9 * Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
10 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
12 * This file is part of InspIRCd. InspIRCd is free software: you can
13 * redistribute it and/or modify it under the terms of the GNU General Public
14 * License as published by the Free Software Foundation, version 2.
16 * This program is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 ChanModeReference ban(NULL, "ban");
34 Channel::Channel(const std::string &cname, time_t ts)
35 : name(cname), age(ts), topicset(0)
37 if (!ServerInstance->chanlist.insert(std::make_pair(cname, this)).second)
38 throw CoreException("Cannot create duplicate channel " + cname);
41 void Channel::SetMode(ModeHandler* mh, bool on)
43 if (mh && mh->GetId() != ModeParser::MODEID_MAX)
44 modes[mh->GetId()] = on;
47 void Channel::SetTopic(User* u, const std::string& ntopic, time_t topicts, const std::string* setter)
49 // Send a TOPIC message to the channel only if the new topic text differs
50 if (this->topic != ntopic)
53 ClientProtocol::Messages::Topic topicmsg(u, this, this->topic);
54 Write(ServerInstance->GetRFCEvents().topic, topicmsg);
57 // Always update setter and set time
59 setter = ServerInstance->Config->FullHostInTopic ? &u->GetFullHost() : &u->nick;
60 this->setby.assign(*setter, 0, ServerInstance->Config->Limits.GetMaxMask());
61 this->topicset = topicts;
63 FOREACH_MOD(OnPostTopicChange, (u, this, this->topic));
66 Membership* Channel::AddUser(User* user)
68 std::pair<MemberMap::iterator, bool> ret = userlist.insert(std::make_pair(user, insp::aligned_storage<Membership>()));
72 Membership* memb = new(ret.first->second) Membership(user, this);
76 void Channel::DelUser(User* user)
78 MemberMap::iterator it = userlist.find(user);
79 if (it != userlist.end())
83 void Channel::CheckDestroy()
85 if (!userlist.empty())
89 FIRST_MOD_RESULT(OnChannelPreDelete, res, (this));
90 if (res == MOD_RES_DENY)
93 // If the channel isn't in chanlist then it is already in the cull list, don't add it again
94 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
95 if ((iter == ServerInstance->chanlist.end()) || (iter->second != this))
98 FOREACH_MOD(OnChannelDelete, (this));
99 ServerInstance->chanlist.erase(iter);
100 ServerInstance->GlobalCulls.AddItem(this);
103 void Channel::DelUser(const MemberMap::iterator& membiter)
105 Membership* memb = membiter->second;
108 userlist.erase(membiter);
110 // If this channel became empty then it should be removed
114 Membership* Channel::GetUser(User* user)
116 MemberMap::iterator i = userlist.find(user);
117 if (i == userlist.end())
122 void Channel::SetDefaultModes()
124 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "SetDefaultModes %s",
125 ServerInstance->Config->DefaultModes.c_str());
126 irc::spacesepstream list(ServerInstance->Config->DefaultModes);
128 std::string parameter;
130 list.GetToken(modeseq);
132 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
134 ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
137 if (mode->IsPrefixMode())
140 if (mode->NeedsParam(true))
142 list.GetToken(parameter);
143 // If the parameter begins with a ':' then it's invalid
144 if (parameter.c_str()[0] == ':')
150 if ((mode->NeedsParam(true)) && (parameter.empty()))
153 mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, this, parameter, true);
159 * add a channel to a user, creating the record for it if needed and linking
160 * it to the user record
162 Channel* Channel::JoinUser(LocalUser* user, std::string cname, bool override, const std::string& key)
164 if (user->registered != REG_ALL)
166 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "Attempted to join unregistered user " + user->uuid + " to channel " + cname);
171 * We don't restrict the number of channels that remote users or users that are override-joining may be in.
172 * We restrict local users to <connect:maxchans> channels.
173 * We restrict local operators to <oper:maxchans> channels.
174 * This is a lot more logical than how it was formerly. -- w00t
178 unsigned int maxchans = user->GetClass()->maxchans;
181 unsigned int opermaxchans = ConvToNum<unsigned int>(user->oper->getConfig("maxchans"));
182 // If not set, use 2.0's <channels:opers>, if that's not set either, use limit from CC
183 if (!opermaxchans && user->HasPrivPermission("channels/high-join-limit"))
184 opermaxchans = ServerInstance->Config->OperMaxChans;
186 maxchans = opermaxchans;
188 if (user->chans.size() >= maxchans)
190 user->WriteNumeric(ERR_TOOMANYCHANNELS, cname, "You are on too many channels");
195 // Crop channel name if it's too long
196 if (cname.length() > ServerInstance->Config->Limits.ChanMax)
197 cname.resize(ServerInstance->Config->Limits.ChanMax);
199 Channel* chan = ServerInstance->FindChan(cname);
200 bool created_by_local = (chan == NULL); // Flag that will be passed to modules in the OnUserJoin() hook later
201 std::string privs; // Prefix mode(letter)s to give to the joining user
205 privs = ServerInstance->Config->DefaultModes.substr(0, ServerInstance->Config->DefaultModes.find(' '));
207 if (override == false)
209 // Ask the modules whether they're ok with the join, pass NULL as Channel* as the channel is yet to be created
210 ModResult MOD_RESULT;
211 FIRST_MOD_RESULT(OnUserPreJoin, MOD_RESULT, (user, NULL, cname, privs, key));
212 if (MOD_RESULT == MOD_RES_DENY)
213 return NULL; // A module wasn't happy with the join, abort
216 chan = new Channel(cname, ServerInstance->Time());
217 // Set the default modes on the channel (<options:defaultmodes>)
218 chan->SetDefaultModes();
222 /* Already on the channel */
223 if (chan->HasUser(user))
226 if (override == false)
228 ModResult MOD_RESULT;
229 FIRST_MOD_RESULT(OnUserPreJoin, MOD_RESULT, (user, chan, cname, privs, key));
231 // A module explicitly denied the join and (hopefully) generated a message
232 // describing the situation, so we may stop here without sending anything
233 if (MOD_RESULT == MOD_RES_DENY)
236 // If no module returned MOD_RES_DENY or MOD_RES_ALLOW (which is the case
237 // most of the time) then proceed to check channel bans.
239 // If a module explicitly allowed the join (by returning MOD_RES_ALLOW),
240 // then this entire section is skipped
241 if (MOD_RESULT == MOD_RES_PASSTHRU)
243 if (chan->IsBanned(user))
245 user->WriteNumeric(ERR_BANNEDFROMCHAN, chan->name, "Cannot join channel (you're banned)");
252 // We figured that this join is allowed and also created the
253 // channel if it didn't exist before, now do the actual join
254 chan->ForceJoin(user, &privs, false, created_by_local);
258 Membership* Channel::ForceJoin(User* user, const std::string* privs, bool bursting, bool created_by_local)
262 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "Attempted to join server user " + user->uuid + " to channel " + this->name);
266 Membership* memb = this->AddUser(user);
268 return NULL; // Already on the channel
270 user->chans.push_front(memb);
274 // If the user was granted prefix modes (in the OnUserPreJoin hook, or they're a
275 // remote user and their own server set the modes), then set them internally now
276 for (std::string::const_iterator i = privs->begin(); i != privs->end(); ++i)
278 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(*i);
281 std::string nick = user->nick;
282 // Set the mode on the user
283 mh->OnModeChange(ServerInstance->FakeClient, NULL, this, nick, true);
288 // 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
290 FOREACH_MOD(OnUserJoin, (memb, bursting, created_by_local, except_list));
292 ClientProtocol::Events::Join joinevent(memb);
293 this->Write(joinevent, 0, except_list);
295 FOREACH_MOD(OnPostJoin, (memb));
299 bool Channel::IsBanned(User* user)
302 FIRST_MOD_RESULT(OnCheckChannelBan, result, (user, this));
304 if (result != MOD_RES_PASSTHRU)
305 return (result == MOD_RES_DENY);
307 ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
311 const ListModeBase::ModeList* bans = banlm->GetList(this);
314 for (ListModeBase::ModeList::const_iterator it = bans->begin(); it != bans->end(); it++)
316 if (CheckBan(user, it->mask))
323 bool Channel::CheckBan(User* user, const std::string& mask)
326 FIRST_MOD_RESULT(OnCheckBan, result, (user, this, mask));
327 if (result != MOD_RES_PASSTHRU)
328 return (result == MOD_RES_DENY);
330 // extbans were handled above, if this is one it obviously didn't match
331 if ((mask.length() <= 2) || (mask[1] == ':'))
334 std::string::size_type at = mask.find('@');
335 if (at == std::string::npos)
338 const std::string nickIdent = user->nick + "!" + user->ident;
339 std::string prefix(mask, 0, at);
340 if (InspIRCd::Match(nickIdent, prefix, NULL))
342 std::string suffix(mask, at + 1);
343 if (InspIRCd::Match(user->GetRealHost(), suffix, NULL) ||
344 InspIRCd::Match(user->GetDisplayedHost(), suffix, NULL) ||
345 InspIRCd::MatchCIDR(user->GetIPString(), suffix, NULL))
351 ModResult Channel::GetExtBanStatus(User *user, char type)
354 FIRST_MOD_RESULT(OnExtBanCheck, rv, (user, this, type));
355 if (rv != MOD_RES_PASSTHRU)
358 ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
360 return MOD_RES_PASSTHRU;
362 const ListModeBase::ModeList* bans = banlm->GetList(this);
365 for (ListModeBase::ModeList::const_iterator it = bans->begin(); it != bans->end(); ++it)
367 if (it->mask.length() <= 2 || it->mask[0] != type || it->mask[1] != ':')
370 if (CheckBan(user, it->mask.substr(2)))
374 return MOD_RES_PASSTHRU;
378 * Remove a channel from a users record, remove the reference to the Membership object
379 * from the channel and destroy it.
381 bool Channel::PartUser(User* user, std::string& reason)
383 MemberMap::iterator membiter = userlist.find(user);
385 if (membiter == userlist.end())
388 Membership* memb = membiter->second;
390 FOREACH_MOD(OnUserPart, (memb, reason, except_list));
392 ClientProtocol::Messages::Part partmsg(memb, reason);
393 Write(ServerInstance->GetRFCEvents().part, partmsg, 0, except_list);
395 // Remove this channel from the user's chanlist
396 user->chans.erase(memb);
397 // Remove the Membership from this channel's userlist and destroy it
398 this->DelUser(membiter);
403 void Channel::KickUser(User* src, const MemberMap::iterator& victimiter, const std::string& reason)
405 Membership* memb = victimiter->second;
407 FOREACH_MOD(OnUserKick, (src, memb, reason, except_list));
409 ClientProtocol::Messages::Kick kickmsg(src, memb, reason);
410 Write(ServerInstance->GetRFCEvents().kick, kickmsg, 0, except_list);
412 memb->user->chans.erase(memb);
413 this->DelUser(victimiter);
416 void Channel::Write(ClientProtocol::Event& protoev, char status, const CUList& except_list)
418 unsigned int minrank = 0;
421 PrefixMode* mh = ServerInstance->Modes->FindPrefix(status);
423 minrank = mh->GetPrefixRank();
425 for (MemberMap::iterator i = userlist.begin(); i != userlist.end(); i++)
427 LocalUser* user = IS_LOCAL(i->first);
428 if ((user) && (!except_list.count(user)))
430 /* User doesn't have the status we're after */
431 if (minrank && i->second->getRank() < minrank)
439 const char* Channel::ChanModes(bool showsecret)
441 static std::string scratch;
446 /* This was still iterating up to 190, Channel::modes is only 64 elements -- Om */
447 for(int n = 0; n < 64; n++)
449 ModeHandler* mh = ServerInstance->Modes->FindMode(n + 65, MODETYPE_CHANNEL);
450 if (mh && IsModeSet(mh))
452 scratch.push_back(n + 65);
454 ParamModeBase* pm = mh->IsParameterMode();
458 if (pm->IsParameterSecret() && !showsecret)
460 sparam += " <" + pm->name + ">";
465 pm->GetParameter(this, sparam);
471 return scratch.c_str();
474 void Channel::WriteNotice(const std::string& text)
476 ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, ServerInstance->FakeClient, this, text, MSG_NOTICE);
477 Write(ServerInstance->GetRFCEvents().privmsg, privmsg);
480 /* returns the status character for a given user on a channel, e.g. @ for op,
481 * % for halfop etc. If the user has several modes set, the highest mode
482 * the user has must be returned.
484 char Membership::GetPrefixChar() const
487 unsigned int bestrank = 0;
489 for (std::string::const_iterator i = modes.begin(); i != modes.end(); ++i)
491 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(*i);
492 if (mh && mh->GetPrefixRank() > bestrank && mh->GetPrefix())
494 bestrank = mh->GetPrefixRank();
495 pf = mh->GetPrefix();
501 unsigned int Membership::getRank()
503 char mchar = modes.c_str()[0];
507 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(mchar);
509 rv = mh->GetPrefixRank();
514 std::string Membership::GetAllPrefixChars() const
517 for (std::string::const_iterator i = modes.begin(); i != modes.end(); ++i)
519 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(*i);
520 if (mh && mh->GetPrefix())
521 ret.push_back(mh->GetPrefix());
527 unsigned int Channel::GetPrefixValue(User* user)
529 MemberMap::iterator m = userlist.find(user);
530 if (m == userlist.end())
532 return m->second->getRank();
535 bool Membership::SetPrefix(PrefixMode* delta_mh, bool adding)
537 char prefix = delta_mh->GetModeChar();
538 for (unsigned int i = 0; i < modes.length(); i++)
540 char mchar = modes[i];
541 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(mchar);
542 if (mh && mh->GetPrefixRank() <= delta_mh->GetPrefixRank())
544 modes = modes.substr(0,i) +
545 (adding ? std::string(1, prefix) : "") +
546 modes.substr(mchar == prefix ? i+1 : i);
547 return adding != (mchar == prefix);
551 modes.push_back(prefix);
556 void Membership::WriteNotice(const std::string& text) const
558 LocalUser* const localuser = IS_LOCAL(user);
562 ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, ServerInstance->FakeClient, this->chan, text, MSG_NOTICE);
563 localuser->Send(ServerInstance->GetRFCEvents().privmsg, privmsg);