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;
180 maxchans = ServerInstance->Config->MaxChans;
183 unsigned int opermaxchans = ConvToNum<unsigned int>(user->oper->getConfig("maxchans"));
184 // If not set, use 2.0's <channels:opers>, if that's not set either, use limit from CC
185 if (!opermaxchans && user->HasPrivPermission("channels/high-join-limit"))
186 opermaxchans = ServerInstance->Config->OperMaxChans;
188 maxchans = opermaxchans;
190 if (user->chans.size() >= maxchans)
192 user->WriteNumeric(ERR_TOOMANYCHANNELS, cname, "You are on too many channels");
197 // Crop channel name if it's too long
198 if (cname.length() > ServerInstance->Config->Limits.ChanMax)
199 cname.resize(ServerInstance->Config->Limits.ChanMax);
201 Channel* chan = ServerInstance->FindChan(cname);
202 bool created_by_local = (chan == NULL); // Flag that will be passed to modules in the OnUserJoin() hook later
203 std::string privs; // Prefix mode(letter)s to give to the joining user
207 privs = ServerInstance->Config->DefaultModes.substr(0, ServerInstance->Config->DefaultModes.find(' '));
209 if (override == false)
211 // Ask the modules whether they're ok with the join, pass NULL as Channel* as the channel is yet to be created
212 ModResult MOD_RESULT;
213 FIRST_MOD_RESULT(OnUserPreJoin, MOD_RESULT, (user, NULL, cname, privs, key));
214 if (MOD_RESULT == MOD_RES_DENY)
215 return NULL; // A module wasn't happy with the join, abort
218 chan = new Channel(cname, ServerInstance->Time());
219 // Set the default modes on the channel (<options:defaultmodes>)
220 chan->SetDefaultModes();
224 /* Already on the channel */
225 if (chan->HasUser(user))
228 if (override == false)
230 ModResult MOD_RESULT;
231 FIRST_MOD_RESULT(OnUserPreJoin, MOD_RESULT, (user, chan, cname, privs, key));
233 // A module explicitly denied the join and (hopefully) generated a message
234 // describing the situation, so we may stop here without sending anything
235 if (MOD_RESULT == MOD_RES_DENY)
238 // If no module returned MOD_RES_DENY or MOD_RES_ALLOW (which is the case
239 // most of the time) then proceed to check channel bans.
241 // If a module explicitly allowed the join (by returning MOD_RES_ALLOW),
242 // then this entire section is skipped
243 if (MOD_RESULT == MOD_RES_PASSTHRU)
245 if (chan->IsBanned(user))
247 user->WriteNumeric(ERR_BANNEDFROMCHAN, chan->name, "Cannot join channel (you're banned)");
254 // We figured that this join is allowed and also created the
255 // channel if it didn't exist before, now do the actual join
256 chan->ForceJoin(user, &privs, false, created_by_local);
260 Membership* Channel::ForceJoin(User* user, const std::string* privs, bool bursting, bool created_by_local)
264 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "Attempted to join server user " + user->uuid + " to channel " + this->name);
268 Membership* memb = this->AddUser(user);
270 return NULL; // Already on the channel
272 user->chans.push_front(memb);
276 // If the user was granted prefix modes (in the OnUserPreJoin hook, or they're a
277 // remote user and their own server set the modes), then set them internally now
278 for (std::string::const_iterator i = privs->begin(); i != privs->end(); ++i)
280 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(*i);
283 std::string nick = user->nick;
284 // Set the mode on the user
285 mh->OnModeChange(ServerInstance->FakeClient, NULL, this, nick, true);
290 // 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
292 FOREACH_MOD(OnUserJoin, (memb, bursting, created_by_local, except_list));
294 ClientProtocol::Events::Join joinevent(memb);
295 this->Write(joinevent, 0, except_list);
297 FOREACH_MOD(OnPostJoin, (memb));
301 bool Channel::IsBanned(User* user)
304 FIRST_MOD_RESULT(OnCheckChannelBan, result, (user, this));
306 if (result != MOD_RES_PASSTHRU)
307 return (result == MOD_RES_DENY);
309 ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
313 const ListModeBase::ModeList* bans = banlm->GetList(this);
316 for (ListModeBase::ModeList::const_iterator it = bans->begin(); it != bans->end(); it++)
318 if (CheckBan(user, it->mask))
325 bool Channel::CheckBan(User* user, const std::string& mask)
328 FIRST_MOD_RESULT(OnCheckBan, result, (user, this, mask));
329 if (result != MOD_RES_PASSTHRU)
330 return (result == MOD_RES_DENY);
332 // extbans were handled above, if this is one it obviously didn't match
333 if ((mask.length() <= 2) || (mask[1] == ':'))
336 std::string::size_type at = mask.find('@');
337 if (at == std::string::npos)
340 const std::string nickIdent = user->nick + "!" + user->ident;
341 std::string prefix(mask, 0, at);
342 if (InspIRCd::Match(nickIdent, prefix, NULL))
344 std::string suffix(mask, at + 1);
345 if (InspIRCd::Match(user->GetRealHost(), suffix, NULL) ||
346 InspIRCd::Match(user->GetDisplayedHost(), suffix, NULL) ||
347 InspIRCd::MatchCIDR(user->GetIPString(), suffix, NULL))
353 ModResult Channel::GetExtBanStatus(User *user, char type)
356 FIRST_MOD_RESULT(OnExtBanCheck, rv, (user, this, type));
357 if (rv != MOD_RES_PASSTHRU)
360 ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
362 return MOD_RES_PASSTHRU;
364 const ListModeBase::ModeList* bans = banlm->GetList(this);
367 for (ListModeBase::ModeList::const_iterator it = bans->begin(); it != bans->end(); ++it)
369 if (it->mask.length() <= 2 || it->mask[0] != type || it->mask[1] != ':')
372 if (CheckBan(user, it->mask.substr(2)))
376 return MOD_RES_PASSTHRU;
380 * Remove a channel from a users record, remove the reference to the Membership object
381 * from the channel and destroy it.
383 bool Channel::PartUser(User* user, std::string& reason)
385 MemberMap::iterator membiter = userlist.find(user);
387 if (membiter == userlist.end())
390 Membership* memb = membiter->second;
392 FOREACH_MOD(OnUserPart, (memb, reason, except_list));
394 ClientProtocol::Messages::Part partmsg(memb, reason);
395 Write(ServerInstance->GetRFCEvents().part, partmsg, 0, except_list);
397 // Remove this channel from the user's chanlist
398 user->chans.erase(memb);
399 // Remove the Membership from this channel's userlist and destroy it
400 this->DelUser(membiter);
405 void Channel::KickUser(User* src, const MemberMap::iterator& victimiter, const std::string& reason)
407 Membership* memb = victimiter->second;
409 FOREACH_MOD(OnUserKick, (src, memb, reason, except_list));
411 ClientProtocol::Messages::Kick kickmsg(src, memb, reason);
412 Write(ServerInstance->GetRFCEvents().kick, kickmsg, 0, except_list);
414 memb->user->chans.erase(memb);
415 this->DelUser(victimiter);
418 void Channel::Write(ClientProtocol::Event& protoev, char status, const CUList& except_list)
420 unsigned int minrank = 0;
423 PrefixMode* mh = ServerInstance->Modes->FindPrefix(status);
425 minrank = mh->GetPrefixRank();
427 for (MemberMap::iterator i = userlist.begin(); i != userlist.end(); i++)
429 LocalUser* user = IS_LOCAL(i->first);
430 if ((user) && (!except_list.count(user)))
432 /* User doesn't have the status we're after */
433 if (minrank && i->second->getRank() < minrank)
441 const char* Channel::ChanModes(bool showsecret)
443 static std::string scratch;
448 /* This was still iterating up to 190, Channel::modes is only 64 elements -- Om */
449 for(int n = 0; n < 64; n++)
451 ModeHandler* mh = ServerInstance->Modes->FindMode(n + 65, MODETYPE_CHANNEL);
452 if (mh && IsModeSet(mh))
454 scratch.push_back(n + 65);
456 ParamModeBase* pm = mh->IsParameterMode();
460 if (pm->IsParameterSecret() && !showsecret)
462 sparam += " <" + pm->name + ">";
467 pm->GetParameter(this, sparam);
473 return scratch.c_str();
476 void Channel::WriteNotice(const std::string& text, char status)
478 ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, ServerInstance->FakeClient, this, text, MSG_NOTICE, status);
479 Write(ServerInstance->GetRFCEvents().privmsg, privmsg);
480 ServerInstance->PI->SendMessage(this, status, text, MSG_NOTICE);
483 /* returns the status character for a given user on a channel, e.g. @ for op,
484 * % for halfop etc. If the user has several modes set, the highest mode
485 * the user has must be returned.
487 char Membership::GetPrefixChar() const
490 unsigned int bestrank = 0;
492 for (std::string::const_iterator i = modes.begin(); i != modes.end(); ++i)
494 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(*i);
495 if (mh && mh->GetPrefixRank() > bestrank && mh->GetPrefix())
497 bestrank = mh->GetPrefixRank();
498 pf = mh->GetPrefix();
504 unsigned int Membership::getRank()
506 char mchar = modes.c_str()[0];
510 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(mchar);
512 rv = mh->GetPrefixRank();
517 std::string Membership::GetAllPrefixChars() const
520 for (std::string::const_iterator i = modes.begin(); i != modes.end(); ++i)
522 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(*i);
523 if (mh && mh->GetPrefix())
524 ret.push_back(mh->GetPrefix());
530 unsigned int Channel::GetPrefixValue(User* user)
532 MemberMap::iterator m = userlist.find(user);
533 if (m == userlist.end())
535 return m->second->getRank();
538 bool Membership::SetPrefix(PrefixMode* delta_mh, bool adding)
540 char prefix = delta_mh->GetModeChar();
541 for (unsigned int i = 0; i < modes.length(); i++)
543 char mchar = modes[i];
544 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(mchar);
545 if (mh && mh->GetPrefixRank() <= delta_mh->GetPrefixRank())
547 modes = modes.substr(0,i) +
548 (adding ? std::string(1, prefix) : "") +
549 modes.substr(mchar == prefix ? i+1 : i);
550 return adding != (mchar == prefix);
554 modes.push_back(prefix);
559 void Membership::WriteNotice(const std::string& text) const
561 LocalUser* const localuser = IS_LOCAL(user);
565 ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, ServerInstance->FakeClient, this->chan, text, MSG_NOTICE);
566 localuser->Send(ServerInstance->GetRFCEvents().privmsg, privmsg);