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)
98 u = ServerInstance->FakeClient;
99 if (IS_LOCAL(u) && !forceset)
102 FIRST_MOD_RESULT(OnPreTopicChange, res, (u,this,ntopic));
104 if (res == MOD_RES_DENY)
106 if (res != MOD_RES_ALLOW)
108 if (!this->HasUser(u))
110 u->WriteNumeric(442, "%s %s :You're not on that channel!",u->nick.c_str(), this->name.c_str());
113 if (IsModeSet('t') && !ServerInstance->OnCheckExemption(u,this,"topiclock").check(GetPrefixValue(u) >= HALFOP_VALUE))
115 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());
121 this->topic.assign(ntopic, 0, ServerInstance->Config->Limits.MaxTopic);
124 this->setby.assign(ServerInstance->Config->FullHostInTopic ? u->GetFullHost() : u->nick, 0, 128);
125 this->WriteChannel(u, "TOPIC %s :%s", this->name.c_str(), this->topic.c_str());
129 this->setby.assign(ServerInstance->Config->ServerName);
130 this->WriteChannelWithServ(ServerInstance->Config->ServerName, "TOPIC %s :%s", this->name.c_str(), this->topic.c_str());
133 this->topicset = ServerInstance->Time();
135 FOREACH_MOD(I_OnPostTopicChange,OnPostTopicChange(u, this, this->topic));
140 long Channel::GetUserCounter()
142 return userlist.size();
145 Membership* Channel::AddUser(User* user)
147 Membership*& memb = userlist[user];
151 memb = new Membership(user, this);
155 void Channel::DelUser(User* user)
157 UserMembIter a = userlist.find(user);
159 if (a != userlist.end())
166 if (userlist.empty())
169 FIRST_MOD_RESULT(OnChannelPreDelete, res, (this));
170 if (res == MOD_RES_DENY)
172 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
173 /* kill the record */
174 if (iter != ServerInstance->chanlist->end())
176 FOREACH_MOD(I_OnChannelDelete, OnChannelDelete(this));
177 ServerInstance->chanlist->erase(iter);
181 ServerInstance->GlobalCulls.AddItem(this);
185 bool Channel::HasUser(User* user)
187 return (userlist.find(user) != userlist.end());
190 Membership* Channel::GetUser(User* user)
192 UserMembIter i = userlist.find(user);
193 if (i == userlist.end())
198 const UserMembList* Channel::GetUsers()
203 void Channel::SetDefaultModes()
205 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "SetDefaultModes %s",
206 ServerInstance->Config->DefaultModes.c_str());
207 irc::spacesepstream list(ServerInstance->Config->DefaultModes);
209 std::string parameter;
211 list.GetToken(modeseq);
213 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
215 ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
218 if (mode->GetNumParams(true))
219 list.GetToken(parameter);
223 mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, this, parameter, true);
229 * add a channel to a user, creating the record for it if needed and linking
230 * it to the user record
232 Channel* Channel::JoinUser(LocalUser* user, std::string cname, bool override, const std::string& key)
234 if (user->registered != REG_ALL)
236 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "Attempted to join unregistered user " + user->uuid + " to channel " + cname);
241 * We don't restrict the number of channels that remote users or users that are override-joining may be in.
242 * We restrict local users to MaxChans channels.
243 * We restrict local operators to OperMaxChans channels.
244 * This is a lot more logical than how it was formerly. -- w00t
248 if (user->HasPrivPermission("channels/high-join-limit"))
250 if (user->chans.size() >= ServerInstance->Config->OperMaxChans)
252 user->WriteNumeric(ERR_TOOMANYCHANNELS, "%s %s :You are on too many channels",user->nick.c_str(), cname.c_str());
258 unsigned int maxchans = user->GetClass()->maxchans;
260 maxchans = ServerInstance->Config->MaxChans;
261 if (user->chans.size() >= maxchans)
263 user->WriteNumeric(ERR_TOOMANYCHANNELS, "%s %s :You are on too many channels",user->nick.c_str(), cname.c_str());
269 // Crop channel name if it's too long
270 if (cname.length() > ServerInstance->Config->Limits.ChanMax)
271 cname.resize(ServerInstance->Config->Limits.ChanMax);
273 Channel* chan = ServerInstance->FindChan(cname);
274 bool created_by_local = (chan == NULL); // Flag that will be passed to modules in the OnUserJoin() hook later
275 std::string privs; // Prefix mode(letter)s to give to the joining user
281 if (override == false)
283 // Ask the modules whether they're ok with the join, pass NULL as Channel* as the channel is yet to be created
284 ModResult MOD_RESULT;
285 FIRST_MOD_RESULT(OnUserPreJoin, MOD_RESULT, (user, NULL, cname, privs, key));
286 if (MOD_RESULT == MOD_RES_DENY)
287 return NULL; // A module wasn't happy with the join, abort
290 chan = new Channel(cname, ServerInstance->Time());
291 // Set the default modes on the channel (<options:defaultmodes>)
292 chan->SetDefaultModes();
296 /* Already on the channel */
297 if (chan->HasUser(user))
300 if (override == false)
302 ModResult MOD_RESULT;
303 FIRST_MOD_RESULT(OnUserPreJoin, MOD_RESULT, (user, chan, cname, privs, key));
305 // A module explicitly denied the join and (hopefully) generated a message
306 // describing the situation, so we may stop here without sending anything
307 if (MOD_RESULT == MOD_RES_DENY)
310 // If no module returned MOD_RES_DENY or MOD_RES_ALLOW (which is the case
311 // most of the time) then proceed to check channel modes +k, +i, +l and bans,
313 // If a module explicitly allowed the join (by returning MOD_RES_ALLOW),
314 // then this entire section is skipped
315 if (MOD_RESULT == MOD_RES_PASSTHRU)
317 std::string ckey = chan->GetModeParameter('k');
318 bool invited = user->IsInvited(chan);
319 bool can_bypass = ServerInstance->Config->InvBypassModes && invited;
323 FIRST_MOD_RESULT(OnCheckKey, MOD_RESULT, (user, chan, key));
324 if (!MOD_RESULT.check((ckey == key) || can_bypass))
326 // If no key provided, or key is not the right one, and can't bypass +k (not invited or option not enabled)
327 user->WriteNumeric(ERR_BADCHANNELKEY, "%s %s :Cannot join channel (Incorrect channel key)",user->nick.c_str(), chan->name.c_str());
332 if (chan->IsModeSet('i'))
334 FIRST_MOD_RESULT(OnCheckInvite, MOD_RESULT, (user, chan));
335 if (!MOD_RESULT.check(invited))
337 user->WriteNumeric(ERR_INVITEONLYCHAN, "%s %s :Cannot join channel (Invite only)",user->nick.c_str(), chan->name.c_str());
342 std::string limit = chan->GetModeParameter('l');
345 FIRST_MOD_RESULT(OnCheckLimit, MOD_RESULT, (user, chan));
346 if (!MOD_RESULT.check((chan->GetUserCounter() < atol(limit.c_str()) || can_bypass)))
348 user->WriteNumeric(ERR_CHANNELISFULL, "%s %s :Cannot join channel (Channel is full)",user->nick.c_str(), chan->name.c_str());
353 if (chan->IsBanned(user) && !can_bypass)
355 user->WriteNumeric(ERR_BANNEDFROMCHAN, "%s %s :Cannot join channel (You're banned)",user->nick.c_str(), chan->name.c_str());
360 * If the user has invites for this channel, remove them now
361 * after a successful join so they don't build up.
365 user->RemoveInvite(chan);
371 // We figured that this join is allowed and also created the
372 // channel if it didn't exist before, now do the actual join
373 chan->ForceJoin(user, &privs, false, created_by_local);
377 void Channel::ForceJoin(User* user, const std::string* privs, bool bursting, bool created_by_local)
379 Membership* memb = this->AddUser(user);
381 return; // Already on the channel
385 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "Attempted to join server user " + user->uuid + " to channel " + this->name);
389 user->chans.insert(this);
393 // If the user was granted prefix modes (in the OnUserPreJoin hook, or he's a
394 // remote user and his own server set the modes), then set them internally now
395 memb->modes = *privs;
396 for (std::string::const_iterator i = privs->begin(); i != privs->end(); ++i)
398 ModeHandler* mh = ServerInstance->Modes->FindMode(*i, MODETYPE_CHANNEL);
401 std::string nick = user->nick;
402 /* Set, and make sure that the mode handler knows this mode was now set */
403 this->SetPrefix(user, mh->GetModeChar(), true);
404 mh->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, this, nick, true);
409 // 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
411 FOREACH_MOD(I_OnUserJoin,OnUserJoin(memb, bursting, created_by_local, except_list));
413 this->WriteAllExcept(user, false, 0, except_list, "JOIN :%s", this->name.c_str());
415 /* Theyre not the first ones in here, make sure everyone else sees the modes we gave the user */
416 if ((GetUserCounter() > 1) && (!memb->modes.empty()))
418 std::string ms = memb->modes;
419 for(unsigned int i=0; i < memb->modes.length(); i++)
420 ms.append(" ").append(user->nick);
422 except_list.insert(user);
423 this->WriteAllExcept(user, !ServerInstance->Config->CycleHostsFromUser, 0, except_list, "MODE %s +%s", this->name.c_str(), ms.c_str());
430 user->WriteNumeric(RPL_TOPIC, "%s %s :%s", user->nick.c_str(), this->name.c_str(), this->topic.c_str());
431 user->WriteNumeric(RPL_TOPICTIME, "%s %s %s %lu", user->nick.c_str(), this->name.c_str(), this->setby.c_str(), (unsigned long)this->topicset);
433 this->UserList(user);
436 FOREACH_MOD(I_OnPostJoin,OnPostJoin(memb));
439 bool Channel::IsBanned(User* user)
442 FIRST_MOD_RESULT(OnCheckChannelBan, result, (user, this));
444 if (result != MOD_RES_PASSTHRU)
445 return (result == MOD_RES_DENY);
447 ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
448 const ListModeBase::ModeList* bans = banlm->GetList(this);
451 for (ListModeBase::ModeList::const_iterator it = bans->begin(); it != bans->end(); it++)
453 if (CheckBan(user, it->mask))
460 bool Channel::CheckBan(User* user, const std::string& mask)
463 FIRST_MOD_RESULT(OnCheckBan, result, (user, this, mask));
464 if (result != MOD_RES_PASSTHRU)
465 return (result == MOD_RES_DENY);
467 // extbans were handled above, if this is one it obviously didn't match
468 if ((mask.length() <= 2) || (mask[1] == ':'))
471 std::string::size_type at = mask.find('@');
472 if (at == std::string::npos)
475 const std::string nickIdent = user->nick + "!" + user->ident;
476 std::string prefix = mask.substr(0, at);
477 if (InspIRCd::Match(nickIdent, prefix, NULL))
479 std::string suffix = mask.substr(at + 1);
480 if (InspIRCd::Match(user->host, suffix, NULL) ||
481 InspIRCd::Match(user->dhost, suffix, NULL) ||
482 InspIRCd::MatchCIDR(user->GetIPString(), suffix, NULL))
488 ModResult Channel::GetExtBanStatus(User *user, char type)
491 FIRST_MOD_RESULT(OnExtBanCheck, rv, (user, this, type));
492 if (rv != MOD_RES_PASSTHRU)
495 ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
496 const ListModeBase::ModeList* bans = banlm->GetList(this);
500 for (ListModeBase::ModeList::const_iterator it = bans->begin(); it != bans->end(); ++it)
502 if (CheckBan(user, it->mask))
506 return MOD_RES_PASSTHRU;
510 * remove a channel from a users record, and return the number of users left.
511 * Therefore, if this function returns 0 the caller should delete the Channel.
513 void Channel::PartUser(User *user, std::string &reason)
518 Membership* memb = GetUser(user);
523 FOREACH_MOD(I_OnUserPart,OnUserPart(memb, reason, except_list));
525 WriteAllExcept(user, false, 0, except_list, "PART %s%s%s", this->name.c_str(), reason.empty() ? "" : " :", reason.c_str());
527 user->chans.erase(this);
528 this->RemoveAllPrefixes(user);
534 void Channel::KickUser(User *src, User *user, const std::string& reason)
539 Membership* memb = GetUser(user);
544 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());
547 if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
549 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());
554 if (ServerInstance->ULine(src->server))
557 FIRST_MOD_RESULT(OnUserPreKick, res, (src,memb,reason));
559 if (res == MOD_RES_DENY)
562 if (res == MOD_RES_PASSTHRU)
564 unsigned int them = this->GetPrefixValue(src);
565 unsigned int req = HALFOP_VALUE;
566 for (std::string::size_type i = 0; i < memb->modes.length(); i++)
568 ModeHandler* mh = ServerInstance->Modes->FindMode(memb->modes[i], MODETYPE_CHANNEL);
569 if (mh && mh->GetLevelRequired() > req)
570 req = mh->GetLevelRequired();
575 src->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s %s :You must be a channel %soperator",
576 src->nick.c_str(), this->name.c_str(), req > HALFOP_VALUE ? "" : "half-");
585 FOREACH_MOD(I_OnUserKick,OnUserKick(src, memb, reason, except_list));
587 WriteAllExcept(src, false, 0, except_list, "KICK %s %s :%s", name.c_str(), user->nick.c_str(), reason.c_str());
589 user->chans.erase(this);
590 this->RemoveAllPrefixes(user);
596 void Channel::WriteChannel(User* user, const char* text, ...)
598 char textbuffer[MAXBUF];
604 va_start(argsPtr, text);
605 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
608 this->WriteChannel(user, std::string(textbuffer));
611 void Channel::WriteChannel(User* user, const std::string &text)
616 const std::string message = ":" + user->GetFullHost() + " " + text;
618 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
620 if (IS_LOCAL(i->first))
621 i->first->Write(message);
625 void Channel::WriteChannelWithServ(const std::string& ServName, const char* text, ...)
627 char textbuffer[MAXBUF];
633 va_start(argsPtr, text);
634 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
637 this->WriteChannelWithServ(ServName, std::string(textbuffer));
640 void Channel::WriteChannelWithServ(const std::string& ServName, const std::string &text)
642 const std::string message = ":" + (ServName.empty() ? ServerInstance->Config->ServerName : ServName) + " " + text;
644 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
646 if (IS_LOCAL(i->first))
647 i->first->Write(message);
651 /* write formatted text from a source user to all users on a channel except
652 * for the sender (for privmsg etc) */
653 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const char* text, ...)
655 char textbuffer[MAXBUF];
661 va_start(argsPtr, text);
662 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
665 this->WriteAllExceptSender(user, serversource, status, std::string(textbuffer));
668 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const char* text, ...)
670 char textbuffer[MAXBUF];
676 int offset = snprintf(textbuffer,MAXBUF,":%s ", serversource ? ServerInstance->Config->ServerName.c_str() : user->GetFullHost().c_str());
678 va_start(argsPtr, text);
679 vsnprintf(textbuffer + offset, MAXBUF - offset, text, argsPtr);
682 this->RawWriteAllExcept(user, serversource, status, except_list, std::string(textbuffer));
685 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string &text)
687 const std::string message = ":" + (serversource ? ServerInstance->Config->ServerName : user->GetFullHost()) + " " + text;
688 this->RawWriteAllExcept(user, serversource, status, except_list, message);
691 void Channel::RawWriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string &out)
693 unsigned int minrank = 0;
696 ModeHandler* mh = ServerInstance->Modes->FindPrefix(status);
698 minrank = mh->GetPrefixRank();
700 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
702 if (IS_LOCAL(i->first) && (except_list.find(i->first) == except_list.end()))
704 /* User doesn't have the status we're after */
705 if (minrank && i->second->getRank() < minrank)
708 i->first->Write(out);
713 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const std::string& text)
716 except_list.insert(user);
717 this->WriteAllExcept(user, serversource, status, except_list, std::string(text));
721 * return a count of the users on a specific channel accounting for
722 * invisible users who won't increase the count. e.g. for /LIST
724 int Channel::CountInvisible()
727 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
729 if (!i->first->quitting && !i->first->IsModeSet('i'))
736 const char* Channel::ChanModes(bool showkey)
738 static std::string scratch;
743 /* This was still iterating up to 190, Channel::modes is only 64 elements -- Om */
744 for(int n = 0; n < 64; n++)
748 scratch.push_back(n + 65);
749 if (n == 'k' - 65 && !showkey)
755 const std::string param = this->GetModeParameter(n + 65);
766 return scratch.c_str();
769 /* compile a userlist of a channel into a string, each nick seperated by
770 * spaces and op, voice etc status shown as @ and +, and send it to 'user'
772 void Channel::UserList(User *user)
780 if (this->IsModeSet('s') && !this->HasUser(user) && !user->HasPrivPermission("channels/auspex"))
782 user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nick/channel",user->nick.c_str(), this->name.c_str());
786 dlen = curlen = snprintf(list,MAXBUF,"%s %c %s :", user->nick.c_str(), this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=', this->name.c_str());
789 char* ptr = list + dlen;
791 /* Improvement by Brain - this doesnt change in value, so why was it inside
794 bool has_user = this->HasUser(user);
796 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
798 if (i->first->quitting)
800 if ((!has_user) && (i->first->IsModeSet('i')))
803 * user is +i, and source not on the channel, does not show
809 std::string prefixlist = this->GetPrefixChar(i->first);
810 std::string nick = i->first->nick;
812 FOREACH_MOD(I_OnNamesListItem, OnNamesListItem(user, i->second, prefixlist, nick));
814 /* Nick was nuked, a module wants us to skip it */
820 if (curlen + prefixlist.length() + nick.length() + 1 > 480)
822 /* list overflowed into multiple numerics */
823 user->WriteNumeric(RPL_NAMREPLY, std::string(list));
825 /* reset our lengths */
826 dlen = curlen = snprintf(list,MAXBUF,"%s %c %s :", user->nick.c_str(), this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=', this->name.c_str());
832 ptrlen = snprintf(ptr, MAXBUF, "%s%s ", prefixlist.c_str(), nick.c_str());
840 /* if whats left in the list isnt empty, send it */
843 user->WriteNumeric(RPL_NAMREPLY, std::string(list));
846 user->WriteNumeric(RPL_ENDOFNAMES, "%s %s :End of /NAMES list.", user->nick.c_str(), this->name.c_str());
849 /* returns the status character for a given user on a channel, e.g. @ for op,
850 * % for halfop etc. If the user has several modes set, the highest mode
851 * the user has must be returned.
853 const char* Channel::GetPrefixChar(User *user)
855 static char pf[2] = {0, 0};
857 unsigned int bestrank = 0;
859 UserMembIter m = userlist.find(user);
860 if (m != userlist.end())
862 for(unsigned int i=0; i < m->second->modes.length(); i++)
864 char mchar = m->second->modes[i];
865 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
866 if (mh && mh->GetPrefixRank() > bestrank && mh->GetPrefix())
868 bestrank = mh->GetPrefixRank();
869 pf[0] = mh->GetPrefix();
876 unsigned int Membership::getRank()
878 char mchar = modes.c_str()[0];
882 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
884 rv = mh->GetPrefixRank();
889 const char* Channel::GetAllPrefixChars(User* user)
891 static char prefix[64];
894 UserMembIter m = userlist.find(user);
895 if (m != userlist.end())
897 for(unsigned int i=0; i < m->second->modes.length(); i++)
899 char mchar = m->second->modes[i];
900 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
901 if (mh && mh->GetPrefix())
902 prefix[ctr++] = mh->GetPrefix();
910 unsigned int Channel::GetPrefixValue(User* user)
912 UserMembIter m = userlist.find(user);
913 if (m == userlist.end())
915 return m->second->getRank();
918 bool Channel::SetPrefix(User* user, char prefix, bool adding)
920 ModeHandler* delta_mh = ServerInstance->Modes->FindMode(prefix, MODETYPE_CHANNEL);
923 UserMembIter m = userlist.find(user);
924 if (m == userlist.end())
926 for(unsigned int i=0; i < m->second->modes.length(); i++)
928 char mchar = m->second->modes[i];
929 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
930 if (mh && mh->GetPrefixRank() <= delta_mh->GetPrefixRank())
933 m->second->modes.substr(0,i) +
934 (adding ? std::string(1, prefix) : "") +
935 m->second->modes.substr(mchar == prefix ? i+1 : i);
936 return adding != (mchar == prefix);
940 m->second->modes += std::string(1, prefix);
944 void Channel::RemoveAllPrefixes(User* user)
946 UserMembIter m = userlist.find(user);
947 if (m != userlist.end())
949 m->second->modes.clear();
953 void Invitation::Create(Channel* c, LocalUser* u, time_t timeout)
955 if ((timeout != 0) && (ServerInstance->Time() >= timeout))
956 // Expired, don't bother
959 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Create chan=%s user=%s", c->name.c_str(), u->uuid.c_str());
961 Invitation* inv = Invitation::Find(c, u, false);
964 if ((inv->expiry == 0) || (inv->expiry > timeout))
966 inv->expiry = timeout;
967 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Create changed expiry in existing invitation %p", (void*) inv);
971 inv = new Invitation(c, u, timeout);
972 c->invites.push_back(inv);
973 u->invites.push_back(inv);
974 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Create created new invitation %p", (void*) inv);
978 Invitation* Invitation::Find(Channel* c, LocalUser* u, bool check_expired)
980 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);
981 if (!u || u->invites.empty())
984 InviteList locallist;
985 locallist.swap(u->invites);
987 Invitation* result = NULL;
988 for (InviteList::iterator i = locallist.begin(); i != locallist.end(); )
990 Invitation* inv = *i;
991 if ((check_expired) && (inv->expiry != 0) && (inv->expiry <= ServerInstance->Time()))
993 /* Expired invite, remove it. */
994 std::string expiration = ServerInstance->TimeString(inv->expiry);
995 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Find ecountered expired entry: %p expired %s", (void*) inv, expiration.c_str());
996 i = locallist.erase(i);
1002 /* Is it what we're searching for? */
1012 locallist.swap(u->invites);
1013 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Find result=%p", (void*) result);
1017 Invitation::~Invitation()
1019 // Remove this entry from both lists
1020 InviteList::iterator it = std::find(chan->invites.begin(), chan->invites.end(), this);
1021 if (it != chan->invites.end())
1022 chan->invites.erase(it);
1023 it = std::find(user->invites.begin(), user->invites.end(), this);
1024 if (it != user->invites.end())
1025 user->invites.erase(it);
1028 void InviteBase::ClearInvites()
1030 ServerInstance->Logs->Log("INVITEBASE", LOG_DEBUG, "InviteBase::ClearInvites %p", (void*) this);
1031 InviteList locallist;
1032 locallist.swap(invites);
1033 for (InviteList::const_iterator i = locallist.begin(); i != locallist.end(); ++i)