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 this->WriteChannel(u, "TOPIC %s :%s", this->name.c_str(), this->topic.c_str());
55 // Always update setter and set time
57 setter = ServerInstance->Config->FullHostInTopic ? &u->GetFullHost() : &u->nick;
58 this->setby.assign(*setter, 0, ServerInstance->Config->Limits.GetMaxMask());
59 this->topicset = topicts;
61 FOREACH_MOD(OnPostTopicChange, (u, this, this->topic));
64 Membership* Channel::AddUser(User* user)
66 std::pair<MemberMap::iterator, bool> ret = userlist.insert(std::make_pair(user, insp::aligned_storage<Membership>()));
70 Membership* memb = new(ret.first->second) Membership(user, this);
74 void Channel::DelUser(User* user)
76 MemberMap::iterator it = userlist.find(user);
77 if (it != userlist.end())
81 void Channel::CheckDestroy()
83 if (!userlist.empty())
87 FIRST_MOD_RESULT(OnChannelPreDelete, res, (this));
88 if (res == MOD_RES_DENY)
91 // If the channel isn't in chanlist then it is already in the cull list, don't add it again
92 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
93 if ((iter == ServerInstance->chanlist.end()) || (iter->second != this))
96 FOREACH_MOD(OnChannelDelete, (this));
97 ServerInstance->chanlist.erase(iter);
98 ServerInstance->GlobalCulls.AddItem(this);
101 void Channel::DelUser(const MemberMap::iterator& membiter)
103 Membership* memb = membiter->second;
106 userlist.erase(membiter);
108 // If this channel became empty then it should be removed
112 Membership* Channel::GetUser(User* user)
114 MemberMap::iterator i = userlist.find(user);
115 if (i == userlist.end())
120 void Channel::SetDefaultModes()
122 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "SetDefaultModes %s",
123 ServerInstance->Config->DefaultModes.c_str());
124 irc::spacesepstream list(ServerInstance->Config->DefaultModes);
126 std::string parameter;
128 list.GetToken(modeseq);
130 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
132 ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
135 if (mode->IsPrefixMode())
138 if (mode->NeedsParam(true))
140 list.GetToken(parameter);
141 // If the parameter begins with a ':' then it's invalid
142 if (parameter.c_str()[0] == ':')
148 if ((mode->NeedsParam(true)) && (parameter.empty()))
151 mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, this, parameter, true);
157 * add a channel to a user, creating the record for it if needed and linking
158 * it to the user record
160 Channel* Channel::JoinUser(LocalUser* user, std::string cname, bool override, const std::string& key)
162 if (user->registered != REG_ALL)
164 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "Attempted to join unregistered user " + user->uuid + " to channel " + cname);
169 * We don't restrict the number of channels that remote users or users that are override-joining may be in.
170 * We restrict local users to <connect:maxchans> channels.
171 * We restrict local operators to <oper:maxchans> channels.
172 * This is a lot more logical than how it was formerly. -- w00t
176 unsigned int maxchans = user->GetClass()->maxchans;
179 unsigned int opermaxchans = ConvToInt(user->oper->getConfig("maxchans"));
180 // If not set, use 2.0's <channels:opers>, if that's not set either, use limit from CC
181 if (!opermaxchans && user->HasPrivPermission("channels/high-join-limit"))
182 opermaxchans = ServerInstance->Config->OperMaxChans;
184 maxchans = opermaxchans;
186 if (user->chans.size() >= maxchans)
188 user->WriteNumeric(ERR_TOOMANYCHANNELS, cname, "You are on too many channels");
193 // Crop channel name if it's too long
194 if (cname.length() > ServerInstance->Config->Limits.ChanMax)
195 cname.resize(ServerInstance->Config->Limits.ChanMax);
197 Channel* chan = ServerInstance->FindChan(cname);
198 bool created_by_local = (chan == NULL); // Flag that will be passed to modules in the OnUserJoin() hook later
199 std::string privs; // Prefix mode(letter)s to give to the joining user
203 privs = ServerInstance->Config->DefaultModes.substr(0, ServerInstance->Config->DefaultModes.find(' '));
205 if (override == false)
207 // Ask the modules whether they're ok with the join, pass NULL as Channel* as the channel is yet to be created
208 ModResult MOD_RESULT;
209 FIRST_MOD_RESULT(OnUserPreJoin, MOD_RESULT, (user, NULL, cname, privs, key));
210 if (MOD_RESULT == MOD_RES_DENY)
211 return NULL; // A module wasn't happy with the join, abort
214 chan = new Channel(cname, ServerInstance->Time());
215 // Set the default modes on the channel (<options:defaultmodes>)
216 chan->SetDefaultModes();
220 /* Already on the channel */
221 if (chan->HasUser(user))
224 if (override == false)
226 ModResult MOD_RESULT;
227 FIRST_MOD_RESULT(OnUserPreJoin, MOD_RESULT, (user, chan, cname, privs, key));
229 // A module explicitly denied the join and (hopefully) generated a message
230 // describing the situation, so we may stop here without sending anything
231 if (MOD_RESULT == MOD_RES_DENY)
234 // If no module returned MOD_RES_DENY or MOD_RES_ALLOW (which is the case
235 // most of the time) then proceed to check channel bans.
237 // If a module explicitly allowed the join (by returning MOD_RES_ALLOW),
238 // then this entire section is skipped
239 if (MOD_RESULT == MOD_RES_PASSTHRU)
241 if (chan->IsBanned(user))
243 user->WriteNumeric(ERR_BANNEDFROMCHAN, chan->name, "Cannot join channel (You're banned)");
250 // We figured that this join is allowed and also created the
251 // channel if it didn't exist before, now do the actual join
252 chan->ForceJoin(user, &privs, false, created_by_local);
256 Membership* Channel::ForceJoin(User* user, const std::string* privs, bool bursting, bool created_by_local)
260 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "Attempted to join server user " + user->uuid + " to channel " + this->name);
264 Membership* memb = this->AddUser(user);
266 return NULL; // Already on the channel
268 user->chans.push_front(memb);
272 // If the user was granted prefix modes (in the OnUserPreJoin hook, or he's a
273 // remote user and his own server set the modes), then set them internally now
274 for (std::string::const_iterator i = privs->begin(); i != privs->end(); ++i)
276 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(*i);
279 std::string nick = user->nick;
280 // Set the mode on the user
281 mh->OnModeChange(ServerInstance->FakeClient, NULL, this, nick, true);
286 // 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
288 FOREACH_MOD(OnUserJoin, (memb, bursting, created_by_local, except_list));
290 this->WriteAllExcept(user, false, 0, except_list, "JOIN :%s", this->name.c_str());
292 /* Theyre not the first ones in here, make sure everyone else sees the modes we gave the user */
293 if ((GetUserCounter() > 1) && (!memb->modes.empty()))
295 std::string ms = memb->modes;
296 for(unsigned int i=0; i < memb->modes.length(); i++)
297 ms.append(" ").append(user->nick);
299 except_list.insert(user);
300 this->WriteAllExcept(user, !ServerInstance->Config->CycleHostsFromUser, 0, except_list, "MODE %s +%s", this->name.c_str(), ms.c_str());
303 FOREACH_MOD(OnPostJoin, (memb));
307 bool Channel::IsBanned(User* user)
310 FIRST_MOD_RESULT(OnCheckChannelBan, result, (user, this));
312 if (result != MOD_RES_PASSTHRU)
313 return (result == MOD_RES_DENY);
315 ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
319 const ListModeBase::ModeList* bans = banlm->GetList(this);
322 for (ListModeBase::ModeList::const_iterator it = bans->begin(); it != bans->end(); it++)
324 if (CheckBan(user, it->mask))
331 bool Channel::CheckBan(User* user, const std::string& mask)
334 FIRST_MOD_RESULT(OnCheckBan, result, (user, this, mask));
335 if (result != MOD_RES_PASSTHRU)
336 return (result == MOD_RES_DENY);
338 // extbans were handled above, if this is one it obviously didn't match
339 if ((mask.length() <= 2) || (mask[1] == ':'))
342 std::string::size_type at = mask.find('@');
343 if (at == std::string::npos)
346 const std::string nickIdent = user->nick + "!" + user->ident;
347 std::string prefix(mask, 0, at);
348 if (InspIRCd::Match(nickIdent, prefix, NULL))
350 std::string suffix(mask, at + 1);
351 if (InspIRCd::Match(user->GetRealHost(), suffix, NULL) ||
352 InspIRCd::Match(user->GetDisplayedHost(), suffix, NULL) ||
353 InspIRCd::MatchCIDR(user->GetIPString(), suffix, NULL))
359 ModResult Channel::GetExtBanStatus(User *user, char type)
362 FIRST_MOD_RESULT(OnExtBanCheck, rv, (user, this, type));
363 if (rv != MOD_RES_PASSTHRU)
366 ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
368 return MOD_RES_PASSTHRU;
370 const ListModeBase::ModeList* bans = banlm->GetList(this);
373 for (ListModeBase::ModeList::const_iterator it = bans->begin(); it != bans->end(); ++it)
375 if (it->mask.length() <= 2 || it->mask[0] != type || it->mask[1] != ':')
378 if (CheckBan(user, it->mask.substr(2)))
382 return MOD_RES_PASSTHRU;
386 * Remove a channel from a users record, remove the reference to the Membership object
387 * from the channel and destroy it.
389 bool Channel::PartUser(User* user, std::string& reason)
391 MemberMap::iterator membiter = userlist.find(user);
393 if (membiter == userlist.end())
396 Membership* memb = membiter->second;
398 FOREACH_MOD(OnUserPart, (memb, reason, except_list));
400 WriteAllExcept(user, false, 0, except_list, "PART %s%s%s", this->name.c_str(), reason.empty() ? "" : " :", reason.c_str());
402 // Remove this channel from the user's chanlist
403 user->chans.erase(memb);
404 // Remove the Membership from this channel's userlist and destroy it
405 this->DelUser(membiter);
410 void Channel::KickUser(User* src, const MemberMap::iterator& victimiter, const std::string& reason)
412 Membership* memb = victimiter->second;
414 FOREACH_MOD(OnUserKick, (src, memb, reason, except_list));
416 User* victim = memb->user;
417 WriteAllExcept(src, false, 0, except_list, "KICK %s %s :%s", name.c_str(), victim->nick.c_str(), reason.c_str());
419 victim->chans.erase(memb);
420 this->DelUser(victimiter);
423 void Channel::WriteChannel(User* user, const char* text, ...)
425 std::string textbuffer;
426 VAFORMAT(textbuffer, text, text);
427 this->WriteChannel(user, textbuffer);
430 void Channel::WriteChannel(User* user, const std::string &text)
432 const std::string message = ":" + user->GetFullHost() + " " + text;
434 for (MemberMap::iterator i = userlist.begin(); i != userlist.end(); i++)
436 if (IS_LOCAL(i->first))
437 i->first->Write(message);
441 void Channel::WriteChannelWithServ(const std::string& ServName, const char* text, ...)
443 std::string textbuffer;
444 VAFORMAT(textbuffer, text, text);
445 this->WriteChannelWithServ(ServName, textbuffer);
448 void Channel::WriteChannelWithServ(const std::string& ServName, const std::string &text)
450 const std::string message = ":" + (ServName.empty() ? ServerInstance->Config->ServerName : ServName) + " " + text;
452 for (MemberMap::iterator i = userlist.begin(); i != userlist.end(); i++)
454 if (IS_LOCAL(i->first))
455 i->first->Write(message);
459 /* write formatted text from a source user to all users on a channel except
460 * for the sender (for privmsg etc) */
461 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const char* text, ...)
463 std::string textbuffer;
464 VAFORMAT(textbuffer, text, text);
465 this->WriteAllExceptSender(user, serversource, status, textbuffer);
468 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const char* text, ...)
470 std::string textbuffer;
471 VAFORMAT(textbuffer, text, text);
472 textbuffer = ":" + (serversource ? ServerInstance->Config->ServerName : user->GetFullHost()) + " " + textbuffer;
473 this->RawWriteAllExcept(user, serversource, status, except_list, textbuffer);
476 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string &text)
478 const std::string message = ":" + (serversource ? ServerInstance->Config->ServerName : user->GetFullHost()) + " " + text;
479 this->RawWriteAllExcept(user, serversource, status, except_list, message);
482 void Channel::RawWriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string &out)
484 unsigned int minrank = 0;
487 PrefixMode* mh = ServerInstance->Modes->FindPrefix(status);
489 minrank = mh->GetPrefixRank();
491 for (MemberMap::iterator i = userlist.begin(); i != userlist.end(); i++)
493 if (IS_LOCAL(i->first) && (except_list.find(i->first) == except_list.end()))
495 /* User doesn't have the status we're after */
496 if (minrank && i->second->getRank() < minrank)
499 i->first->Write(out);
504 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const std::string& text)
507 except_list.insert(user);
508 this->WriteAllExcept(user, serversource, status, except_list, std::string(text));
511 const char* Channel::ChanModes(bool showkey)
513 static std::string scratch;
518 /* This was still iterating up to 190, Channel::modes is only 64 elements -- Om */
519 for(int n = 0; n < 64; n++)
521 ModeHandler* mh = ServerInstance->Modes->FindMode(n + 65, MODETYPE_CHANNEL);
522 if (mh && IsModeSet(mh))
524 scratch.push_back(n + 65);
526 ParamModeBase* pm = mh->IsParameterMode();
530 if (n == 'k' - 65 && !showkey)
537 pm->GetParameter(this, sparam);
543 return scratch.c_str();
546 void Channel::WriteNotice(const std::string& text)
548 std::string rawmsg = "NOTICE ";
549 rawmsg.append(this->name).append(" :").append(text);
550 WriteChannelWithServ(ServerInstance->Config->ServerName, rawmsg);
553 /* returns the status character for a given user on a channel, e.g. @ for op,
554 * % for halfop etc. If the user has several modes set, the highest mode
555 * the user has must be returned.
557 char Membership::GetPrefixChar() const
560 unsigned int bestrank = 0;
562 for (std::string::const_iterator i = modes.begin(); i != modes.end(); ++i)
564 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(*i);
565 if (mh && mh->GetPrefixRank() > bestrank && mh->GetPrefix())
567 bestrank = mh->GetPrefixRank();
568 pf = mh->GetPrefix();
574 unsigned int Membership::getRank()
576 char mchar = modes.c_str()[0];
580 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(mchar);
582 rv = mh->GetPrefixRank();
587 std::string Membership::GetAllPrefixChars() const
590 for (std::string::const_iterator i = modes.begin(); i != modes.end(); ++i)
592 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(*i);
593 if (mh && mh->GetPrefix())
594 ret.push_back(mh->GetPrefix());
600 unsigned int Channel::GetPrefixValue(User* user)
602 MemberMap::iterator m = userlist.find(user);
603 if (m == userlist.end())
605 return m->second->getRank();
608 bool Membership::SetPrefix(PrefixMode* delta_mh, bool adding)
610 char prefix = delta_mh->GetModeChar();
611 for (unsigned int i = 0; i < modes.length(); i++)
613 char mchar = modes[i];
614 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(mchar);
615 if (mh && mh->GetPrefixRank() <= delta_mh->GetPrefixRank())
617 modes = modes.substr(0,i) +
618 (adding ? std::string(1, prefix) : "") +
619 modes.substr(mchar == prefix ? i+1 : i);
620 return adding != (mchar == prefix);
624 modes.push_back(prefix);
629 void Membership::WriteNotice(const std::string& text) const
631 std::string rawmsg = "NOTICE ";
632 rawmsg.append(chan->name).append(" :").append(text);
633 user->WriteServ(rawmsg);