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 modes[mh->GetId()] = on;
46 void Channel::SetTopic(User* u, const std::string& ntopic, time_t topicts, const std::string* setter)
48 // Send a TOPIC message to the channel only if the new topic text differs
49 if (this->topic != ntopic)
52 ClientProtocol::Messages::Topic topicmsg(u, this, this->topic);
53 Write(ServerInstance->GetRFCEvents().topic, topicmsg);
56 // Always update setter and set time
58 setter = ServerInstance->Config->FullHostInTopic ? &u->GetFullHost() : &u->nick;
59 this->setby.assign(*setter, 0, ServerInstance->Config->Limits.GetMaxMask());
60 this->topicset = topicts;
62 FOREACH_MOD(OnPostTopicChange, (u, this, this->topic));
65 Membership* Channel::AddUser(User* user)
67 std::pair<MemberMap::iterator, bool> ret = userlist.insert(std::make_pair(user, insp::aligned_storage<Membership>()));
71 Membership* memb = new(ret.first->second) Membership(user, this);
75 void Channel::DelUser(User* user)
77 MemberMap::iterator it = userlist.find(user);
78 if (it != userlist.end())
82 void Channel::CheckDestroy()
84 if (!userlist.empty())
88 FIRST_MOD_RESULT(OnChannelPreDelete, res, (this));
89 if (res == MOD_RES_DENY)
92 // If the channel isn't in chanlist then it is already in the cull list, don't add it again
93 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
94 if ((iter == ServerInstance->chanlist.end()) || (iter->second != this))
97 FOREACH_MOD(OnChannelDelete, (this));
98 ServerInstance->chanlist.erase(iter);
99 ServerInstance->GlobalCulls.AddItem(this);
102 void Channel::DelUser(const MemberMap::iterator& membiter)
104 Membership* memb = membiter->second;
107 userlist.erase(membiter);
109 // If this channel became empty then it should be removed
113 Membership* Channel::GetUser(User* user)
115 MemberMap::iterator i = userlist.find(user);
116 if (i == userlist.end())
121 void Channel::SetDefaultModes()
123 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "SetDefaultModes %s",
124 ServerInstance->Config->DefaultModes.c_str());
125 irc::spacesepstream list(ServerInstance->Config->DefaultModes);
127 std::string parameter;
129 list.GetToken(modeseq);
131 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
133 ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
136 if (mode->IsPrefixMode())
139 if (mode->NeedsParam(true))
141 list.GetToken(parameter);
142 // If the parameter begins with a ':' then it's invalid
143 if (parameter.c_str()[0] == ':')
149 if ((mode->NeedsParam(true)) && (parameter.empty()))
152 mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, this, parameter, true);
158 * add a channel to a user, creating the record for it if needed and linking
159 * it to the user record
161 Channel* Channel::JoinUser(LocalUser* user, std::string cname, bool override, const std::string& key)
163 if (user->registered != REG_ALL)
165 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "Attempted to join unregistered user " + user->uuid + " to channel " + cname);
170 * We don't restrict the number of channels that remote users or users that are override-joining may be in.
171 * We restrict local users to <connect:maxchans> channels.
172 * We restrict local operators to <oper:maxchans> channels.
173 * This is a lot more logical than how it was formerly. -- w00t
177 unsigned int maxchans = user->GetClass()->maxchans;
180 unsigned int opermaxchans = ConvToInt(user->oper->getConfig("maxchans"));
181 // If not set, use 2.0's <channels:opers>, if that's not set either, use limit from CC
182 if (!opermaxchans && user->HasPrivPermission("channels/high-join-limit"))
183 opermaxchans = ServerInstance->Config->OperMaxChans;
185 maxchans = opermaxchans;
187 if (user->chans.size() >= maxchans)
189 user->WriteNumeric(ERR_TOOMANYCHANNELS, cname, "You are on too many channels");
194 // Crop channel name if it's too long
195 if (cname.length() > ServerInstance->Config->Limits.ChanMax)
196 cname.resize(ServerInstance->Config->Limits.ChanMax);
198 Channel* chan = ServerInstance->FindChan(cname);
199 bool created_by_local = (chan == NULL); // Flag that will be passed to modules in the OnUserJoin() hook later
200 std::string privs; // Prefix mode(letter)s to give to the joining user
204 privs = ServerInstance->Config->DefaultModes.substr(0, ServerInstance->Config->DefaultModes.find(' '));
206 if (override == false)
208 // Ask the modules whether they're ok with the join, pass NULL as Channel* as the channel is yet to be created
209 ModResult MOD_RESULT;
210 FIRST_MOD_RESULT(OnUserPreJoin, MOD_RESULT, (user, NULL, cname, privs, key));
211 if (MOD_RESULT == MOD_RES_DENY)
212 return NULL; // A module wasn't happy with the join, abort
215 chan = new Channel(cname, ServerInstance->Time());
216 // Set the default modes on the channel (<options:defaultmodes>)
217 chan->SetDefaultModes();
221 /* Already on the channel */
222 if (chan->HasUser(user))
225 if (override == false)
227 ModResult MOD_RESULT;
228 FIRST_MOD_RESULT(OnUserPreJoin, MOD_RESULT, (user, chan, cname, privs, key));
230 // A module explicitly denied the join and (hopefully) generated a message
231 // describing the situation, so we may stop here without sending anything
232 if (MOD_RESULT == MOD_RES_DENY)
235 // If no module returned MOD_RES_DENY or MOD_RES_ALLOW (which is the case
236 // most of the time) then proceed to check channel bans.
238 // If a module explicitly allowed the join (by returning MOD_RES_ALLOW),
239 // then this entire section is skipped
240 if (MOD_RESULT == MOD_RES_PASSTHRU)
242 if (chan->IsBanned(user))
244 user->WriteNumeric(ERR_BANNEDFROMCHAN, chan->name, "Cannot join channel (You're banned)");
251 // We figured that this join is allowed and also created the
252 // channel if it didn't exist before, now do the actual join
253 chan->ForceJoin(user, &privs, false, created_by_local);
257 Membership* Channel::ForceJoin(User* user, const std::string* privs, bool bursting, bool created_by_local)
261 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "Attempted to join server user " + user->uuid + " to channel " + this->name);
265 Membership* memb = this->AddUser(user);
267 return NULL; // Already on the channel
269 user->chans.push_front(memb);
273 // If the user was granted prefix modes (in the OnUserPreJoin hook, or he's a
274 // remote user and his own server set the modes), then set them internally now
275 for (std::string::const_iterator i = privs->begin(); i != privs->end(); ++i)
277 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(*i);
280 std::string nick = user->nick;
281 // Set the mode on the user
282 mh->OnModeChange(ServerInstance->FakeClient, NULL, this, nick, true);
287 // 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
289 FOREACH_MOD(OnUserJoin, (memb, bursting, created_by_local, except_list));
291 ClientProtocol::Events::Join joinevent(memb);
292 this->Write(joinevent, 0, except_list);
294 FOREACH_MOD(OnPostJoin, (memb));
298 bool Channel::IsBanned(User* user)
301 FIRST_MOD_RESULT(OnCheckChannelBan, result, (user, this));
303 if (result != MOD_RES_PASSTHRU)
304 return (result == MOD_RES_DENY);
306 ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
310 const ListModeBase::ModeList* bans = banlm->GetList(this);
313 for (ListModeBase::ModeList::const_iterator it = bans->begin(); it != bans->end(); it++)
315 if (CheckBan(user, it->mask))
322 bool Channel::CheckBan(User* user, const std::string& mask)
325 FIRST_MOD_RESULT(OnCheckBan, result, (user, this, mask));
326 if (result != MOD_RES_PASSTHRU)
327 return (result == MOD_RES_DENY);
329 // extbans were handled above, if this is one it obviously didn't match
330 if ((mask.length() <= 2) || (mask[1] == ':'))
333 std::string::size_type at = mask.find('@');
334 if (at == std::string::npos)
337 const std::string nickIdent = user->nick + "!" + user->ident;
338 std::string prefix(mask, 0, at);
339 if (InspIRCd::Match(nickIdent, prefix, NULL))
341 std::string suffix(mask, at + 1);
342 if (InspIRCd::Match(user->GetRealHost(), suffix, NULL) ||
343 InspIRCd::Match(user->GetDisplayedHost(), suffix, NULL) ||
344 InspIRCd::MatchCIDR(user->GetIPString(), suffix, NULL))
350 ModResult Channel::GetExtBanStatus(User *user, char type)
353 FIRST_MOD_RESULT(OnExtBanCheck, rv, (user, this, type));
354 if (rv != MOD_RES_PASSTHRU)
357 ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
359 return MOD_RES_PASSTHRU;
361 const ListModeBase::ModeList* bans = banlm->GetList(this);
364 for (ListModeBase::ModeList::const_iterator it = bans->begin(); it != bans->end(); ++it)
366 if (it->mask.length() <= 2 || it->mask[0] != type || it->mask[1] != ':')
369 if (CheckBan(user, it->mask.substr(2)))
373 return MOD_RES_PASSTHRU;
377 * Remove a channel from a users record, remove the reference to the Membership object
378 * from the channel and destroy it.
380 bool Channel::PartUser(User* user, std::string& reason)
382 MemberMap::iterator membiter = userlist.find(user);
384 if (membiter == userlist.end())
387 Membership* memb = membiter->second;
389 FOREACH_MOD(OnUserPart, (memb, reason, except_list));
391 ClientProtocol::Messages::Part partmsg(memb, reason);
392 Write(ServerInstance->GetRFCEvents().part, partmsg, 0, except_list);
394 // Remove this channel from the user's chanlist
395 user->chans.erase(memb);
396 // Remove the Membership from this channel's userlist and destroy it
397 this->DelUser(membiter);
402 void Channel::KickUser(User* src, const MemberMap::iterator& victimiter, const std::string& reason)
404 Membership* memb = victimiter->second;
406 FOREACH_MOD(OnUserKick, (src, memb, reason, except_list));
408 ClientProtocol::Messages::Kick kickmsg(src, memb, reason);
409 Write(ServerInstance->GetRFCEvents().kick, kickmsg, 0, except_list);
411 memb->user->chans.erase(memb);
412 this->DelUser(victimiter);
415 void Channel::Write(ClientProtocol::Event& protoev, char status, const CUList& except_list)
417 unsigned int minrank = 0;
420 PrefixMode* mh = ServerInstance->Modes->FindPrefix(status);
422 minrank = mh->GetPrefixRank();
424 for (MemberMap::iterator i = userlist.begin(); i != userlist.end(); i++)
426 LocalUser* user = IS_LOCAL(i->first);
427 if ((user) && (!except_list.count(user)))
429 /* User doesn't have the status we're after */
430 if (minrank && i->second->getRank() < minrank)
438 const char* Channel::ChanModes(bool showkey)
440 static std::string scratch;
445 /* This was still iterating up to 190, Channel::modes is only 64 elements -- Om */
446 for(int n = 0; n < 64; n++)
448 ModeHandler* mh = ServerInstance->Modes->FindMode(n + 65, MODETYPE_CHANNEL);
449 if (mh && IsModeSet(mh))
451 scratch.push_back(n + 65);
453 ParamModeBase* pm = mh->IsParameterMode();
457 if (n == 'k' - 65 && !showkey)
464 pm->GetParameter(this, sparam);
470 return scratch.c_str();
473 void Channel::WriteNotice(const std::string& text)
475 ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, ServerInstance->FakeClient, this, text, MSG_NOTICE);
476 Write(ServerInstance->GetRFCEvents().privmsg, privmsg);
479 /* returns the status character for a given user on a channel, e.g. @ for op,
480 * % for halfop etc. If the user has several modes set, the highest mode
481 * the user has must be returned.
483 char Membership::GetPrefixChar() const
486 unsigned int bestrank = 0;
488 for (std::string::const_iterator i = modes.begin(); i != modes.end(); ++i)
490 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(*i);
491 if (mh && mh->GetPrefixRank() > bestrank && mh->GetPrefix())
493 bestrank = mh->GetPrefixRank();
494 pf = mh->GetPrefix();
500 unsigned int Membership::getRank()
502 char mchar = modes.c_str()[0];
506 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(mchar);
508 rv = mh->GetPrefixRank();
513 std::string Membership::GetAllPrefixChars() const
516 for (std::string::const_iterator i = modes.begin(); i != modes.end(); ++i)
518 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(*i);
519 if (mh && mh->GetPrefix())
520 ret.push_back(mh->GetPrefix());
526 unsigned int Channel::GetPrefixValue(User* user)
528 MemberMap::iterator m = userlist.find(user);
529 if (m == userlist.end())
531 return m->second->getRank();
534 bool Membership::SetPrefix(PrefixMode* delta_mh, bool adding)
536 char prefix = delta_mh->GetModeChar();
537 for (unsigned int i = 0; i < modes.length(); i++)
539 char mchar = modes[i];
540 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(mchar);
541 if (mh && mh->GetPrefixRank() <= delta_mh->GetPrefixRank())
543 modes = modes.substr(0,i) +
544 (adding ? std::string(1, prefix) : "") +
545 modes.substr(mchar == prefix ? i+1 : i);
546 return adding != (mchar == prefix);
550 modes.push_back(prefix);
555 void Membership::WriteNotice(const std::string& text) const
557 LocalUser* const localuser = IS_LOCAL(user);
561 ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, ServerInstance->FakeClient, this->chan, text, MSG_NOTICE);
562 localuser->Send(ServerInstance->GetRFCEvents().privmsg, privmsg);