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/>.
33 static ModeReference ban(NULL, "ban");
35 Channel::Channel(const std::string &cname, time_t ts)
37 if (!ServerInstance->chanlist->insert(std::make_pair(cname, this)).second)
38 throw CoreException("Cannot create duplicate channel " + cname);
41 this->age = ts ? ts : ServerInstance->Time();
47 void Channel::SetMode(char mode,bool mode_on)
49 modes[mode-65] = mode_on;
52 void Channel::SetMode(ModeHandler* mh, bool on)
54 modes[mh->GetModeChar() - 65] = on;
57 void Channel::SetModeParam(char mode, const std::string& parameter)
59 CustomModeList::iterator n = custom_mode_params.find(mode);
60 // always erase, even if changing, so that the map gets the new value
61 if (n != custom_mode_params.end())
62 custom_mode_params.erase(n);
63 if (parameter.empty())
65 modes[mode-65] = false;
69 custom_mode_params[mode] = parameter;
70 modes[mode-65] = true;
74 void Channel::SetModeParam(ModeHandler* mode, const std::string& parameter)
76 SetModeParam(mode->GetModeChar(), parameter);
79 std::string Channel::GetModeParameter(char mode)
81 CustomModeList::iterator n = custom_mode_params.find(mode);
82 if (n != custom_mode_params.end())
87 std::string Channel::GetModeParameter(ModeHandler* mode)
89 CustomModeList::iterator n = custom_mode_params.find(mode->GetModeChar());
90 if (n != custom_mode_params.end())
95 int Channel::SetTopic(User *u, std::string &ntopic, bool forceset)
97 if (IS_LOCAL(u) && !forceset)
100 FIRST_MOD_RESULT(OnPreTopicChange, res, (u,this,ntopic));
102 if (res == MOD_RES_DENY)
104 if (res != MOD_RES_ALLOW)
106 if (!this->HasUser(u))
108 u->WriteNumeric(442, "%s %s :You're not on that channel!",u->nick.c_str(), this->name.c_str());
111 if (IsModeSet('t') && !ServerInstance->OnCheckExemption(u,this,"topiclock").check(GetPrefixValue(u) >= HALFOP_VALUE))
113 u->WriteNumeric(482, "%s %s :You do not have access to change the topic on this channel", u->nick.c_str(), this->name.c_str());
119 this->topic.assign(ntopic, 0, ServerInstance->Config->Limits.MaxTopic);
120 this->setby.assign(ServerInstance->Config->FullHostInTopic ? u->GetFullHost() : u->nick, 0, 128);
121 this->WriteChannel(u, "TOPIC %s :%s", this->name.c_str(), this->topic.c_str());
122 this->topicset = ServerInstance->Time();
124 FOREACH_MOD(I_OnPostTopicChange,OnPostTopicChange(u, this, this->topic));
129 Membership* Channel::AddUser(User* user)
131 Membership*& memb = userlist[user];
135 memb = new Membership(user, this);
139 void Channel::DelUser(User* user)
141 UserMembIter a = userlist.find(user);
143 if (a != userlist.end())
150 if (userlist.empty())
153 FIRST_MOD_RESULT(OnChannelPreDelete, res, (this));
154 if (res == MOD_RES_DENY)
156 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
157 /* kill the record */
158 if (iter != ServerInstance->chanlist->end())
160 FOREACH_MOD(I_OnChannelDelete, OnChannelDelete(this));
161 ServerInstance->chanlist->erase(iter);
165 ServerInstance->GlobalCulls.AddItem(this);
169 Membership* Channel::GetUser(User* user)
171 UserMembIter i = userlist.find(user);
172 if (i == userlist.end())
177 void Channel::SetDefaultModes()
179 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "SetDefaultModes %s",
180 ServerInstance->Config->DefaultModes.c_str());
181 irc::spacesepstream list(ServerInstance->Config->DefaultModes);
183 std::string parameter;
185 list.GetToken(modeseq);
187 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
189 ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
192 if (mode->GetNumParams(true))
193 list.GetToken(parameter);
197 mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, this, parameter, true);
203 * add a channel to a user, creating the record for it if needed and linking
204 * it to the user record
206 Channel* Channel::JoinUser(LocalUser* user, std::string cname, bool override, const std::string& key)
208 if (user->registered != REG_ALL)
210 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "Attempted to join unregistered user " + user->uuid + " to channel " + cname);
215 * We don't restrict the number of channels that remote users or users that are override-joining may be in.
216 * We restrict local users to MaxChans channels.
217 * We restrict local operators to OperMaxChans channels.
218 * This is a lot more logical than how it was formerly. -- w00t
222 if (user->HasPrivPermission("channels/high-join-limit"))
224 if (user->chans.size() >= ServerInstance->Config->OperMaxChans)
226 user->WriteNumeric(ERR_TOOMANYCHANNELS, "%s %s :You are on too many channels",user->nick.c_str(), cname.c_str());
232 unsigned int maxchans = user->GetClass()->maxchans;
234 maxchans = ServerInstance->Config->MaxChans;
235 if (user->chans.size() >= maxchans)
237 user->WriteNumeric(ERR_TOOMANYCHANNELS, "%s %s :You are on too many channels",user->nick.c_str(), cname.c_str());
243 // Crop channel name if it's too long
244 if (cname.length() > ServerInstance->Config->Limits.ChanMax)
245 cname.resize(ServerInstance->Config->Limits.ChanMax);
247 Channel* chan = ServerInstance->FindChan(cname);
248 bool created_by_local = (chan == NULL); // Flag that will be passed to modules in the OnUserJoin() hook later
249 std::string privs; // Prefix mode(letter)s to give to the joining user
255 if (override == false)
257 // Ask the modules whether they're ok with the join, pass NULL as Channel* as the channel is yet to be created
258 ModResult MOD_RESULT;
259 FIRST_MOD_RESULT(OnUserPreJoin, MOD_RESULT, (user, NULL, cname, privs, key));
260 if (MOD_RESULT == MOD_RES_DENY)
261 return NULL; // A module wasn't happy with the join, abort
264 chan = new Channel(cname, ServerInstance->Time());
265 // Set the default modes on the channel (<options:defaultmodes>)
266 chan->SetDefaultModes();
270 /* Already on the channel */
271 if (chan->HasUser(user))
274 if (override == false)
276 ModResult MOD_RESULT;
277 FIRST_MOD_RESULT(OnUserPreJoin, MOD_RESULT, (user, chan, cname, privs, key));
279 // A module explicitly denied the join and (hopefully) generated a message
280 // describing the situation, so we may stop here without sending anything
281 if (MOD_RESULT == MOD_RES_DENY)
284 // If no module returned MOD_RES_DENY or MOD_RES_ALLOW (which is the case
285 // most of the time) then proceed to check channel modes +k, +i, +l and bans,
287 // If a module explicitly allowed the join (by returning MOD_RES_ALLOW),
288 // then this entire section is skipped
289 if (MOD_RESULT == MOD_RES_PASSTHRU)
291 std::string ckey = chan->GetModeParameter('k');
292 bool invited = user->IsInvited(chan);
293 bool can_bypass = ServerInstance->Config->InvBypassModes && invited;
297 FIRST_MOD_RESULT(OnCheckKey, MOD_RESULT, (user, chan, key));
298 if (!MOD_RESULT.check((ckey == key) || can_bypass))
300 // If no key provided, or key is not the right one, and can't bypass +k (not invited or option not enabled)
301 user->WriteNumeric(ERR_BADCHANNELKEY, "%s %s :Cannot join channel (Incorrect channel key)",user->nick.c_str(), chan->name.c_str());
306 if (chan->IsModeSet('i'))
308 FIRST_MOD_RESULT(OnCheckInvite, MOD_RESULT, (user, chan));
309 if (!MOD_RESULT.check(invited))
311 user->WriteNumeric(ERR_INVITEONLYCHAN, "%s %s :Cannot join channel (Invite only)",user->nick.c_str(), chan->name.c_str());
316 std::string limit = chan->GetModeParameter('l');
319 FIRST_MOD_RESULT(OnCheckLimit, MOD_RESULT, (user, chan));
320 if (!MOD_RESULT.check((chan->GetUserCounter() < atol(limit.c_str()) || can_bypass)))
322 user->WriteNumeric(ERR_CHANNELISFULL, "%s %s :Cannot join channel (Channel is full)",user->nick.c_str(), chan->name.c_str());
327 if (chan->IsBanned(user) && !can_bypass)
329 user->WriteNumeric(ERR_BANNEDFROMCHAN, "%s %s :Cannot join channel (You're banned)",user->nick.c_str(), chan->name.c_str());
334 * If the user has invites for this channel, remove them now
335 * after a successful join so they don't build up.
339 user->RemoveInvite(chan);
345 // We figured that this join is allowed and also created the
346 // channel if it didn't exist before, now do the actual join
347 chan->ForceJoin(user, &privs, false, created_by_local);
351 void Channel::ForceJoin(User* user, const std::string* privs, bool bursting, bool created_by_local)
353 Membership* memb = this->AddUser(user);
355 return; // Already on the channel
359 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "Attempted to join server user " + user->uuid + " to channel " + this->name);
363 user->chans.insert(this);
367 // If the user was granted prefix modes (in the OnUserPreJoin hook, or he's a
368 // remote user and his own server set the modes), then set them internally now
369 memb->modes = *privs;
370 for (std::string::const_iterator i = privs->begin(); i != privs->end(); ++i)
372 ModeHandler* mh = ServerInstance->Modes->FindMode(*i, MODETYPE_CHANNEL);
375 std::string nick = user->nick;
376 /* Set, and make sure that the mode handler knows this mode was now set */
377 this->SetPrefix(user, mh->GetModeChar(), true);
378 mh->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, this, nick, true);
383 // 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
385 FOREACH_MOD(I_OnUserJoin,OnUserJoin(memb, bursting, created_by_local, except_list));
387 this->WriteAllExcept(user, false, 0, except_list, "JOIN :%s", this->name.c_str());
389 /* Theyre not the first ones in here, make sure everyone else sees the modes we gave the user */
390 if ((GetUserCounter() > 1) && (!memb->modes.empty()))
392 std::string ms = memb->modes;
393 for(unsigned int i=0; i < memb->modes.length(); i++)
394 ms.append(" ").append(user->nick);
396 except_list.insert(user);
397 this->WriteAllExcept(user, !ServerInstance->Config->CycleHostsFromUser, 0, except_list, "MODE %s +%s", this->name.c_str(), ms.c_str());
404 user->WriteNumeric(RPL_TOPIC, "%s %s :%s", user->nick.c_str(), this->name.c_str(), this->topic.c_str());
405 user->WriteNumeric(RPL_TOPICTIME, "%s %s %s %lu", user->nick.c_str(), this->name.c_str(), this->setby.c_str(), (unsigned long)this->topicset);
407 this->UserList(user);
410 FOREACH_MOD(I_OnPostJoin,OnPostJoin(memb));
413 bool Channel::IsBanned(User* user)
416 FIRST_MOD_RESULT(OnCheckChannelBan, result, (user, this));
418 if (result != MOD_RES_PASSTHRU)
419 return (result == MOD_RES_DENY);
421 ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
422 const ListModeBase::ModeList* bans = banlm->GetList(this);
425 for (ListModeBase::ModeList::const_iterator it = bans->begin(); it != bans->end(); it++)
427 if (CheckBan(user, it->mask))
434 bool Channel::CheckBan(User* user, const std::string& mask)
437 FIRST_MOD_RESULT(OnCheckBan, result, (user, this, mask));
438 if (result != MOD_RES_PASSTHRU)
439 return (result == MOD_RES_DENY);
441 // extbans were handled above, if this is one it obviously didn't match
442 if ((mask.length() <= 2) || (mask[1] == ':'))
445 std::string::size_type at = mask.find('@');
446 if (at == std::string::npos)
449 const std::string nickIdent = user->nick + "!" + user->ident;
450 std::string prefix = mask.substr(0, at);
451 if (InspIRCd::Match(nickIdent, prefix, NULL))
453 std::string suffix = mask.substr(at + 1);
454 if (InspIRCd::Match(user->host, suffix, NULL) ||
455 InspIRCd::Match(user->dhost, suffix, NULL) ||
456 InspIRCd::MatchCIDR(user->GetIPString(), suffix, NULL))
462 ModResult Channel::GetExtBanStatus(User *user, char type)
465 FIRST_MOD_RESULT(OnExtBanCheck, rv, (user, this, type));
466 if (rv != MOD_RES_PASSTHRU)
469 ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
470 const ListModeBase::ModeList* bans = banlm->GetList(this);
474 for (ListModeBase::ModeList::const_iterator it = bans->begin(); it != bans->end(); ++it)
476 if (CheckBan(user, it->mask))
480 return MOD_RES_PASSTHRU;
484 * remove a channel from a users record, and return the number of users left.
485 * Therefore, if this function returns 0 the caller should delete the Channel.
487 void Channel::PartUser(User *user, std::string &reason)
489 Membership* memb = GetUser(user);
494 FOREACH_MOD(I_OnUserPart,OnUserPart(memb, reason, except_list));
496 WriteAllExcept(user, false, 0, except_list, "PART %s%s%s", this->name.c_str(), reason.empty() ? "" : " :", reason.c_str());
498 user->chans.erase(this);
499 this->RemoveAllPrefixes(user);
505 void Channel::KickUser(User *src, User *user, const std::string& reason)
507 Membership* memb = GetUser(user);
512 src->WriteNumeric(ERR_USERNOTINCHANNEL, "%s %s %s :They are not on that channel",src->nick.c_str(), user->nick.c_str(), this->name.c_str());
515 if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
517 src->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s %s :Only a u-line may kick a u-line from a channel.",src->nick.c_str(), this->name.c_str());
522 if (ServerInstance->ULine(src->server))
525 FIRST_MOD_RESULT(OnUserPreKick, res, (src,memb,reason));
527 if (res == MOD_RES_DENY)
530 if (res == MOD_RES_PASSTHRU)
532 unsigned int them = this->GetPrefixValue(src);
533 unsigned int req = HALFOP_VALUE;
534 for (std::string::size_type i = 0; i < memb->modes.length(); i++)
536 ModeHandler* mh = ServerInstance->Modes->FindMode(memb->modes[i], MODETYPE_CHANNEL);
537 if (mh && mh->GetLevelRequired() > req)
538 req = mh->GetLevelRequired();
543 src->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s %s :You must be a channel %soperator",
544 src->nick.c_str(), this->name.c_str(), req > HALFOP_VALUE ? "" : "half-");
553 FOREACH_MOD(I_OnUserKick,OnUserKick(src, memb, reason, except_list));
555 WriteAllExcept(src, false, 0, except_list, "KICK %s %s :%s", name.c_str(), user->nick.c_str(), reason.c_str());
557 user->chans.erase(this);
558 this->RemoveAllPrefixes(user);
564 void Channel::WriteChannel(User* user, const char* text, ...)
566 std::string textbuffer;
567 VAFORMAT(textbuffer, text, text);
568 this->WriteChannel(user, textbuffer);
571 void Channel::WriteChannel(User* user, const std::string &text)
573 const std::string message = ":" + user->GetFullHost() + " " + text;
575 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
577 if (IS_LOCAL(i->first))
578 i->first->Write(message);
582 void Channel::WriteChannelWithServ(const std::string& ServName, const char* text, ...)
584 std::string textbuffer;
585 VAFORMAT(textbuffer, text, text);
586 this->WriteChannelWithServ(ServName, textbuffer);
589 void Channel::WriteChannelWithServ(const std::string& ServName, const std::string &text)
591 const std::string message = ":" + (ServName.empty() ? ServerInstance->Config->ServerName : ServName) + " " + text;
593 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
595 if (IS_LOCAL(i->first))
596 i->first->Write(message);
600 /* write formatted text from a source user to all users on a channel except
601 * for the sender (for privmsg etc) */
602 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const char* text, ...)
604 std::string textbuffer;
605 VAFORMAT(textbuffer, text, text);
606 this->WriteAllExceptSender(user, serversource, status, textbuffer);
609 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const char* text, ...)
611 std::string textbuffer;
612 VAFORMAT(textbuffer, text, text);
613 textbuffer = ":" + (serversource ? ServerInstance->Config->ServerName : user->GetFullHost()) + " " + textbuffer;
614 this->RawWriteAllExcept(user, serversource, status, except_list, textbuffer);
617 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string &text)
619 const std::string message = ":" + (serversource ? ServerInstance->Config->ServerName : user->GetFullHost()) + " " + text;
620 this->RawWriteAllExcept(user, serversource, status, except_list, message);
623 void Channel::RawWriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string &out)
625 unsigned int minrank = 0;
628 ModeHandler* mh = ServerInstance->Modes->FindPrefix(status);
630 minrank = mh->GetPrefixRank();
632 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
634 if (IS_LOCAL(i->first) && (except_list.find(i->first) == except_list.end()))
636 /* User doesn't have the status we're after */
637 if (minrank && i->second->getRank() < minrank)
640 i->first->Write(out);
645 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const std::string& text)
648 except_list.insert(user);
649 this->WriteAllExcept(user, serversource, status, except_list, std::string(text));
653 * return a count of the users on a specific channel accounting for
654 * invisible users who won't increase the count. e.g. for /LIST
656 int Channel::CountInvisible()
659 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
661 if (!i->first->quitting && !i->first->IsModeSet('i'))
668 const char* Channel::ChanModes(bool showkey)
670 static std::string scratch;
675 /* This was still iterating up to 190, Channel::modes is only 64 elements -- Om */
676 for(int n = 0; n < 64; n++)
680 scratch.push_back(n + 65);
681 if (n == 'k' - 65 && !showkey)
687 const std::string param = this->GetModeParameter(n + 65);
698 return scratch.c_str();
701 /* compile a userlist of a channel into a string, each nick seperated by
702 * spaces and op, voice etc status shown as @ and +, and send it to 'user'
704 void Channel::UserList(User *user)
712 if (this->IsModeSet('s') && !this->HasUser(user) && !user->HasPrivPermission("channels/auspex"))
714 user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nick/channel",user->nick.c_str(), this->name.c_str());
718 dlen = curlen = snprintf(list,MAXBUF,"%s %c %s :", user->nick.c_str(), this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=', this->name.c_str());
721 char* ptr = list + dlen;
723 /* Improvement by Brain - this doesnt change in value, so why was it inside
726 bool has_user = this->HasUser(user);
728 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
730 if (i->first->quitting)
732 if ((!has_user) && (i->first->IsModeSet('i')))
735 * user is +i, and source not on the channel, does not show
741 std::string prefixlist = this->GetPrefixChar(i->first);
742 std::string nick = i->first->nick;
744 FOREACH_MOD(I_OnNamesListItem, OnNamesListItem(user, i->second, prefixlist, nick));
746 /* Nick was nuked, a module wants us to skip it */
752 if (curlen + prefixlist.length() + nick.length() + 1 > 480)
754 /* list overflowed into multiple numerics */
755 user->WriteNumeric(RPL_NAMREPLY, std::string(list));
757 /* reset our lengths */
758 dlen = curlen = snprintf(list,MAXBUF,"%s %c %s :", user->nick.c_str(), this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=', this->name.c_str());
764 ptrlen = snprintf(ptr, MAXBUF, "%s%s ", prefixlist.c_str(), nick.c_str());
772 /* if whats left in the list isnt empty, send it */
775 user->WriteNumeric(RPL_NAMREPLY, std::string(list));
778 user->WriteNumeric(RPL_ENDOFNAMES, "%s %s :End of /NAMES list.", user->nick.c_str(), this->name.c_str());
781 /* returns the status character for a given user on a channel, e.g. @ for op,
782 * % for halfop etc. If the user has several modes set, the highest mode
783 * the user has must be returned.
785 const char* Channel::GetPrefixChar(User *user)
787 static char pf[2] = {0, 0};
789 unsigned int bestrank = 0;
791 UserMembIter m = userlist.find(user);
792 if (m != userlist.end())
794 for(unsigned int i=0; i < m->second->modes.length(); i++)
796 char mchar = m->second->modes[i];
797 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
798 if (mh && mh->GetPrefixRank() > bestrank && mh->GetPrefix())
800 bestrank = mh->GetPrefixRank();
801 pf[0] = mh->GetPrefix();
808 unsigned int Membership::getRank()
810 char mchar = modes.c_str()[0];
814 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
816 rv = mh->GetPrefixRank();
821 const char* Channel::GetAllPrefixChars(User* user)
823 static char prefix[64];
826 UserMembIter m = userlist.find(user);
827 if (m != userlist.end())
829 for(unsigned int i=0; i < m->second->modes.length(); i++)
831 char mchar = m->second->modes[i];
832 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
833 if (mh && mh->GetPrefix())
834 prefix[ctr++] = mh->GetPrefix();
842 unsigned int Channel::GetPrefixValue(User* user)
844 UserMembIter m = userlist.find(user);
845 if (m == userlist.end())
847 return m->second->getRank();
850 bool Channel::SetPrefix(User* user, char prefix, bool adding)
852 ModeHandler* delta_mh = ServerInstance->Modes->FindMode(prefix, MODETYPE_CHANNEL);
855 UserMembIter m = userlist.find(user);
856 if (m == userlist.end())
858 for(unsigned int i=0; i < m->second->modes.length(); i++)
860 char mchar = m->second->modes[i];
861 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
862 if (mh && mh->GetPrefixRank() <= delta_mh->GetPrefixRank())
865 m->second->modes.substr(0,i) +
866 (adding ? std::string(1, prefix) : "") +
867 m->second->modes.substr(mchar == prefix ? i+1 : i);
868 return adding != (mchar == prefix);
872 m->second->modes += std::string(1, prefix);
876 void Channel::RemoveAllPrefixes(User* user)
878 UserMembIter m = userlist.find(user);
879 if (m != userlist.end())
881 m->second->modes.clear();
885 void Invitation::Create(Channel* c, LocalUser* u, time_t timeout)
887 if ((timeout != 0) && (ServerInstance->Time() >= timeout))
888 // Expired, don't bother
891 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Create chan=%s user=%s", c->name.c_str(), u->uuid.c_str());
893 Invitation* inv = Invitation::Find(c, u, false);
896 if ((inv->expiry == 0) || (inv->expiry > timeout))
898 inv->expiry = timeout;
899 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Create changed expiry in existing invitation %p", (void*) inv);
903 inv = new Invitation(c, u, timeout);
904 c->invites.push_back(inv);
905 u->invites.push_back(inv);
906 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Create created new invitation %p", (void*) inv);
910 Invitation* Invitation::Find(Channel* c, LocalUser* u, bool check_expired)
912 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Find chan=%s user=%s check_expired=%d", c ? c->name.c_str() : "NULL", u ? u->uuid.c_str() : "NULL", check_expired);
913 if (!u || u->invites.empty())
916 InviteList locallist;
917 locallist.swap(u->invites);
919 Invitation* result = NULL;
920 for (InviteList::iterator i = locallist.begin(); i != locallist.end(); )
922 Invitation* inv = *i;
923 if ((check_expired) && (inv->expiry != 0) && (inv->expiry <= ServerInstance->Time()))
925 /* Expired invite, remove it. */
926 std::string expiration = ServerInstance->TimeString(inv->expiry);
927 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Find ecountered expired entry: %p expired %s", (void*) inv, expiration.c_str());
928 i = locallist.erase(i);
934 /* Is it what we're searching for? */
944 locallist.swap(u->invites);
945 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Find result=%p", (void*) result);
949 Invitation::~Invitation()
951 // Remove this entry from both lists
952 InviteList::iterator it = std::find(chan->invites.begin(), chan->invites.end(), this);
953 if (it != chan->invites.end())
954 chan->invites.erase(it);
955 it = std::find(user->invites.begin(), user->invites.end(), this);
956 if (it != user->invites.end())
957 user->invites.erase(it);
960 void InviteBase::ClearInvites()
962 ServerInstance->Logs->Log("INVITEBASE", LOG_DEBUG, "InviteBase::ClearInvites %p", (void*) this);
963 InviteList locallist;
964 locallist.swap(invites);
965 for (InviteList::const_iterator i = locallist.begin(); i != locallist.end(); ++i)