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 Membership* Channel::AddUser(User* user)
142 Membership*& memb = userlist[user];
146 memb = new Membership(user, this);
150 void Channel::DelUser(User* user)
152 UserMembIter a = userlist.find(user);
154 if (a != userlist.end())
161 if (userlist.empty())
164 FIRST_MOD_RESULT(OnChannelPreDelete, res, (this));
165 if (res == MOD_RES_DENY)
167 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
168 /* kill the record */
169 if (iter != ServerInstance->chanlist->end())
171 FOREACH_MOD(I_OnChannelDelete, OnChannelDelete(this));
172 ServerInstance->chanlist->erase(iter);
176 ServerInstance->GlobalCulls.AddItem(this);
180 Membership* Channel::GetUser(User* user)
182 UserMembIter i = userlist.find(user);
183 if (i == userlist.end())
188 void Channel::SetDefaultModes()
190 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "SetDefaultModes %s",
191 ServerInstance->Config->DefaultModes.c_str());
192 irc::spacesepstream list(ServerInstance->Config->DefaultModes);
194 std::string parameter;
196 list.GetToken(modeseq);
198 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
200 ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
203 if (mode->GetNumParams(true))
204 list.GetToken(parameter);
208 mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, this, parameter, true);
214 * add a channel to a user, creating the record for it if needed and linking
215 * it to the user record
217 Channel* Channel::JoinUser(LocalUser* user, std::string cname, bool override, const std::string& key)
219 if (user->registered != REG_ALL)
221 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "Attempted to join unregistered user " + user->uuid + " to channel " + cname);
226 * We don't restrict the number of channels that remote users or users that are override-joining may be in.
227 * We restrict local users to MaxChans channels.
228 * We restrict local operators to OperMaxChans channels.
229 * This is a lot more logical than how it was formerly. -- w00t
233 if (user->HasPrivPermission("channels/high-join-limit"))
235 if (user->chans.size() >= ServerInstance->Config->OperMaxChans)
237 user->WriteNumeric(ERR_TOOMANYCHANNELS, "%s %s :You are on too many channels",user->nick.c_str(), cname.c_str());
243 unsigned int maxchans = user->GetClass()->maxchans;
245 maxchans = ServerInstance->Config->MaxChans;
246 if (user->chans.size() >= maxchans)
248 user->WriteNumeric(ERR_TOOMANYCHANNELS, "%s %s :You are on too many channels",user->nick.c_str(), cname.c_str());
254 // Crop channel name if it's too long
255 if (cname.length() > ServerInstance->Config->Limits.ChanMax)
256 cname.resize(ServerInstance->Config->Limits.ChanMax);
258 Channel* chan = ServerInstance->FindChan(cname);
259 bool created_by_local = (chan == NULL); // Flag that will be passed to modules in the OnUserJoin() hook later
260 std::string privs; // Prefix mode(letter)s to give to the joining user
266 if (override == false)
268 // Ask the modules whether they're ok with the join, pass NULL as Channel* as the channel is yet to be created
269 ModResult MOD_RESULT;
270 FIRST_MOD_RESULT(OnUserPreJoin, MOD_RESULT, (user, NULL, cname, privs, key));
271 if (MOD_RESULT == MOD_RES_DENY)
272 return NULL; // A module wasn't happy with the join, abort
275 chan = new Channel(cname, ServerInstance->Time());
276 // Set the default modes on the channel (<options:defaultmodes>)
277 chan->SetDefaultModes();
281 /* Already on the channel */
282 if (chan->HasUser(user))
285 if (override == false)
287 ModResult MOD_RESULT;
288 FIRST_MOD_RESULT(OnUserPreJoin, MOD_RESULT, (user, chan, cname, privs, key));
290 // A module explicitly denied the join and (hopefully) generated a message
291 // describing the situation, so we may stop here without sending anything
292 if (MOD_RESULT == MOD_RES_DENY)
295 // If no module returned MOD_RES_DENY or MOD_RES_ALLOW (which is the case
296 // most of the time) then proceed to check channel modes +k, +i, +l and bans,
298 // If a module explicitly allowed the join (by returning MOD_RES_ALLOW),
299 // then this entire section is skipped
300 if (MOD_RESULT == MOD_RES_PASSTHRU)
302 std::string ckey = chan->GetModeParameter('k');
303 bool invited = user->IsInvited(chan);
304 bool can_bypass = ServerInstance->Config->InvBypassModes && invited;
308 FIRST_MOD_RESULT(OnCheckKey, MOD_RESULT, (user, chan, key));
309 if (!MOD_RESULT.check((ckey == key) || can_bypass))
311 // If no key provided, or key is not the right one, and can't bypass +k (not invited or option not enabled)
312 user->WriteNumeric(ERR_BADCHANNELKEY, "%s %s :Cannot join channel (Incorrect channel key)",user->nick.c_str(), chan->name.c_str());
317 if (chan->IsModeSet('i'))
319 FIRST_MOD_RESULT(OnCheckInvite, MOD_RESULT, (user, chan));
320 if (!MOD_RESULT.check(invited))
322 user->WriteNumeric(ERR_INVITEONLYCHAN, "%s %s :Cannot join channel (Invite only)",user->nick.c_str(), chan->name.c_str());
327 std::string limit = chan->GetModeParameter('l');
330 FIRST_MOD_RESULT(OnCheckLimit, MOD_RESULT, (user, chan));
331 if (!MOD_RESULT.check((chan->GetUserCounter() < atol(limit.c_str()) || can_bypass)))
333 user->WriteNumeric(ERR_CHANNELISFULL, "%s %s :Cannot join channel (Channel is full)",user->nick.c_str(), chan->name.c_str());
338 if (chan->IsBanned(user) && !can_bypass)
340 user->WriteNumeric(ERR_BANNEDFROMCHAN, "%s %s :Cannot join channel (You're banned)",user->nick.c_str(), chan->name.c_str());
345 * If the user has invites for this channel, remove them now
346 * after a successful join so they don't build up.
350 user->RemoveInvite(chan);
356 // We figured that this join is allowed and also created the
357 // channel if it didn't exist before, now do the actual join
358 chan->ForceJoin(user, &privs, false, created_by_local);
362 void Channel::ForceJoin(User* user, const std::string* privs, bool bursting, bool created_by_local)
364 Membership* memb = this->AddUser(user);
366 return; // Already on the channel
370 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "Attempted to join server user " + user->uuid + " to channel " + this->name);
374 user->chans.insert(this);
378 // If the user was granted prefix modes (in the OnUserPreJoin hook, or he's a
379 // remote user and his own server set the modes), then set them internally now
380 memb->modes = *privs;
381 for (std::string::const_iterator i = privs->begin(); i != privs->end(); ++i)
383 ModeHandler* mh = ServerInstance->Modes->FindMode(*i, MODETYPE_CHANNEL);
386 std::string nick = user->nick;
387 /* Set, and make sure that the mode handler knows this mode was now set */
388 this->SetPrefix(user, mh->GetModeChar(), true);
389 mh->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, this, nick, true);
394 // 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
396 FOREACH_MOD(I_OnUserJoin,OnUserJoin(memb, bursting, created_by_local, except_list));
398 this->WriteAllExcept(user, false, 0, except_list, "JOIN :%s", this->name.c_str());
400 /* Theyre not the first ones in here, make sure everyone else sees the modes we gave the user */
401 if ((GetUserCounter() > 1) && (!memb->modes.empty()))
403 std::string ms = memb->modes;
404 for(unsigned int i=0; i < memb->modes.length(); i++)
405 ms.append(" ").append(user->nick);
407 except_list.insert(user);
408 this->WriteAllExcept(user, !ServerInstance->Config->CycleHostsFromUser, 0, except_list, "MODE %s +%s", this->name.c_str(), ms.c_str());
415 user->WriteNumeric(RPL_TOPIC, "%s %s :%s", user->nick.c_str(), this->name.c_str(), this->topic.c_str());
416 user->WriteNumeric(RPL_TOPICTIME, "%s %s %s %lu", user->nick.c_str(), this->name.c_str(), this->setby.c_str(), (unsigned long)this->topicset);
418 this->UserList(user);
421 FOREACH_MOD(I_OnPostJoin,OnPostJoin(memb));
424 bool Channel::IsBanned(User* user)
427 FIRST_MOD_RESULT(OnCheckChannelBan, result, (user, this));
429 if (result != MOD_RES_PASSTHRU)
430 return (result == MOD_RES_DENY);
432 ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
433 const ListModeBase::ModeList* bans = banlm->GetList(this);
436 for (ListModeBase::ModeList::const_iterator it = bans->begin(); it != bans->end(); it++)
438 if (CheckBan(user, it->mask))
445 bool Channel::CheckBan(User* user, const std::string& mask)
448 FIRST_MOD_RESULT(OnCheckBan, result, (user, this, mask));
449 if (result != MOD_RES_PASSTHRU)
450 return (result == MOD_RES_DENY);
452 // extbans were handled above, if this is one it obviously didn't match
453 if ((mask.length() <= 2) || (mask[1] == ':'))
456 std::string::size_type at = mask.find('@');
457 if (at == std::string::npos)
460 const std::string nickIdent = user->nick + "!" + user->ident;
461 std::string prefix = mask.substr(0, at);
462 if (InspIRCd::Match(nickIdent, prefix, NULL))
464 std::string suffix = mask.substr(at + 1);
465 if (InspIRCd::Match(user->host, suffix, NULL) ||
466 InspIRCd::Match(user->dhost, suffix, NULL) ||
467 InspIRCd::MatchCIDR(user->GetIPString(), suffix, NULL))
473 ModResult Channel::GetExtBanStatus(User *user, char type)
476 FIRST_MOD_RESULT(OnExtBanCheck, rv, (user, this, type));
477 if (rv != MOD_RES_PASSTHRU)
480 ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
481 const ListModeBase::ModeList* bans = banlm->GetList(this);
485 for (ListModeBase::ModeList::const_iterator it = bans->begin(); it != bans->end(); ++it)
487 if (CheckBan(user, it->mask))
491 return MOD_RES_PASSTHRU;
495 * remove a channel from a users record, and return the number of users left.
496 * Therefore, if this function returns 0 the caller should delete the Channel.
498 void Channel::PartUser(User *user, std::string &reason)
503 Membership* memb = GetUser(user);
508 FOREACH_MOD(I_OnUserPart,OnUserPart(memb, reason, except_list));
510 WriteAllExcept(user, false, 0, except_list, "PART %s%s%s", this->name.c_str(), reason.empty() ? "" : " :", reason.c_str());
512 user->chans.erase(this);
513 this->RemoveAllPrefixes(user);
519 void Channel::KickUser(User *src, User *user, const std::string& reason)
524 Membership* memb = GetUser(user);
529 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());
532 if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
534 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());
539 if (ServerInstance->ULine(src->server))
542 FIRST_MOD_RESULT(OnUserPreKick, res, (src,memb,reason));
544 if (res == MOD_RES_DENY)
547 if (res == MOD_RES_PASSTHRU)
549 unsigned int them = this->GetPrefixValue(src);
550 unsigned int req = HALFOP_VALUE;
551 for (std::string::size_type i = 0; i < memb->modes.length(); i++)
553 ModeHandler* mh = ServerInstance->Modes->FindMode(memb->modes[i], MODETYPE_CHANNEL);
554 if (mh && mh->GetLevelRequired() > req)
555 req = mh->GetLevelRequired();
560 src->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s %s :You must be a channel %soperator",
561 src->nick.c_str(), this->name.c_str(), req > HALFOP_VALUE ? "" : "half-");
570 FOREACH_MOD(I_OnUserKick,OnUserKick(src, memb, reason, except_list));
572 WriteAllExcept(src, false, 0, except_list, "KICK %s %s :%s", name.c_str(), user->nick.c_str(), reason.c_str());
574 user->chans.erase(this);
575 this->RemoveAllPrefixes(user);
581 void Channel::WriteChannel(User* user, const char* text, ...)
586 std::string textbuffer;
587 VAFORMAT(textbuffer, text, text);
588 this->WriteChannel(user, textbuffer);
591 void Channel::WriteChannel(User* user, const std::string &text)
596 const std::string message = ":" + user->GetFullHost() + " " + text;
598 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
600 if (IS_LOCAL(i->first))
601 i->first->Write(message);
605 void Channel::WriteChannelWithServ(const std::string& ServName, const char* text, ...)
610 std::string textbuffer;
611 VAFORMAT(textbuffer, text, text);
612 this->WriteChannelWithServ(ServName, textbuffer);
615 void Channel::WriteChannelWithServ(const std::string& ServName, const std::string &text)
617 const std::string message = ":" + (ServName.empty() ? ServerInstance->Config->ServerName : ServName) + " " + text;
619 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
621 if (IS_LOCAL(i->first))
622 i->first->Write(message);
626 /* write formatted text from a source user to all users on a channel except
627 * for the sender (for privmsg etc) */
628 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const char* text, ...)
633 std::string textbuffer;
634 VAFORMAT(textbuffer, text, text);
635 this->WriteAllExceptSender(user, serversource, status, textbuffer);
638 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const char* text, ...)
643 std::string textbuffer;
644 VAFORMAT(textbuffer, text, text);
645 textbuffer = ":" + (serversource ? ServerInstance->Config->ServerName : user->GetFullHost()) + " " + textbuffer;
646 this->RawWriteAllExcept(user, serversource, status, except_list, textbuffer);
649 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string &text)
651 const std::string message = ":" + (serversource ? ServerInstance->Config->ServerName : user->GetFullHost()) + " " + text;
652 this->RawWriteAllExcept(user, serversource, status, except_list, message);
655 void Channel::RawWriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string &out)
657 unsigned int minrank = 0;
660 ModeHandler* mh = ServerInstance->Modes->FindPrefix(status);
662 minrank = mh->GetPrefixRank();
664 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
666 if (IS_LOCAL(i->first) && (except_list.find(i->first) == except_list.end()))
668 /* User doesn't have the status we're after */
669 if (minrank && i->second->getRank() < minrank)
672 i->first->Write(out);
677 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const std::string& text)
680 except_list.insert(user);
681 this->WriteAllExcept(user, serversource, status, except_list, std::string(text));
685 * return a count of the users on a specific channel accounting for
686 * invisible users who won't increase the count. e.g. for /LIST
688 int Channel::CountInvisible()
691 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
693 if (!i->first->quitting && !i->first->IsModeSet('i'))
700 const char* Channel::ChanModes(bool showkey)
702 static std::string scratch;
707 /* This was still iterating up to 190, Channel::modes is only 64 elements -- Om */
708 for(int n = 0; n < 64; n++)
712 scratch.push_back(n + 65);
713 if (n == 'k' - 65 && !showkey)
719 const std::string param = this->GetModeParameter(n + 65);
730 return scratch.c_str();
733 /* compile a userlist of a channel into a string, each nick seperated by
734 * spaces and op, voice etc status shown as @ and +, and send it to 'user'
736 void Channel::UserList(User *user)
744 if (this->IsModeSet('s') && !this->HasUser(user) && !user->HasPrivPermission("channels/auspex"))
746 user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nick/channel",user->nick.c_str(), this->name.c_str());
750 dlen = curlen = snprintf(list,MAXBUF,"%s %c %s :", user->nick.c_str(), this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=', this->name.c_str());
753 char* ptr = list + dlen;
755 /* Improvement by Brain - this doesnt change in value, so why was it inside
758 bool has_user = this->HasUser(user);
760 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
762 if (i->first->quitting)
764 if ((!has_user) && (i->first->IsModeSet('i')))
767 * user is +i, and source not on the channel, does not show
773 std::string prefixlist = this->GetPrefixChar(i->first);
774 std::string nick = i->first->nick;
776 FOREACH_MOD(I_OnNamesListItem, OnNamesListItem(user, i->second, prefixlist, nick));
778 /* Nick was nuked, a module wants us to skip it */
784 if (curlen + prefixlist.length() + nick.length() + 1 > 480)
786 /* list overflowed into multiple numerics */
787 user->WriteNumeric(RPL_NAMREPLY, std::string(list));
789 /* reset our lengths */
790 dlen = curlen = snprintf(list,MAXBUF,"%s %c %s :", user->nick.c_str(), this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=', this->name.c_str());
796 ptrlen = snprintf(ptr, MAXBUF, "%s%s ", prefixlist.c_str(), nick.c_str());
804 /* if whats left in the list isnt empty, send it */
807 user->WriteNumeric(RPL_NAMREPLY, std::string(list));
810 user->WriteNumeric(RPL_ENDOFNAMES, "%s %s :End of /NAMES list.", user->nick.c_str(), this->name.c_str());
813 /* returns the status character for a given user on a channel, e.g. @ for op,
814 * % for halfop etc. If the user has several modes set, the highest mode
815 * the user has must be returned.
817 const char* Channel::GetPrefixChar(User *user)
819 static char pf[2] = {0, 0};
821 unsigned int bestrank = 0;
823 UserMembIter m = userlist.find(user);
824 if (m != userlist.end())
826 for(unsigned int i=0; i < m->second->modes.length(); i++)
828 char mchar = m->second->modes[i];
829 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
830 if (mh && mh->GetPrefixRank() > bestrank && mh->GetPrefix())
832 bestrank = mh->GetPrefixRank();
833 pf[0] = mh->GetPrefix();
840 unsigned int Membership::getRank()
842 char mchar = modes.c_str()[0];
846 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
848 rv = mh->GetPrefixRank();
853 const char* Channel::GetAllPrefixChars(User* user)
855 static char prefix[64];
858 UserMembIter m = userlist.find(user);
859 if (m != userlist.end())
861 for(unsigned int i=0; i < m->second->modes.length(); i++)
863 char mchar = m->second->modes[i];
864 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
865 if (mh && mh->GetPrefix())
866 prefix[ctr++] = mh->GetPrefix();
874 unsigned int Channel::GetPrefixValue(User* user)
876 UserMembIter m = userlist.find(user);
877 if (m == userlist.end())
879 return m->second->getRank();
882 bool Channel::SetPrefix(User* user, char prefix, bool adding)
884 ModeHandler* delta_mh = ServerInstance->Modes->FindMode(prefix, MODETYPE_CHANNEL);
887 UserMembIter m = userlist.find(user);
888 if (m == userlist.end())
890 for(unsigned int i=0; i < m->second->modes.length(); i++)
892 char mchar = m->second->modes[i];
893 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
894 if (mh && mh->GetPrefixRank() <= delta_mh->GetPrefixRank())
897 m->second->modes.substr(0,i) +
898 (adding ? std::string(1, prefix) : "") +
899 m->second->modes.substr(mchar == prefix ? i+1 : i);
900 return adding != (mchar == prefix);
904 m->second->modes += std::string(1, prefix);
908 void Channel::RemoveAllPrefixes(User* user)
910 UserMembIter m = userlist.find(user);
911 if (m != userlist.end())
913 m->second->modes.clear();
917 void Invitation::Create(Channel* c, LocalUser* u, time_t timeout)
919 if ((timeout != 0) && (ServerInstance->Time() >= timeout))
920 // Expired, don't bother
923 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Create chan=%s user=%s", c->name.c_str(), u->uuid.c_str());
925 Invitation* inv = Invitation::Find(c, u, false);
928 if ((inv->expiry == 0) || (inv->expiry > timeout))
930 inv->expiry = timeout;
931 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Create changed expiry in existing invitation %p", (void*) inv);
935 inv = new Invitation(c, u, timeout);
936 c->invites.push_back(inv);
937 u->invites.push_back(inv);
938 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Create created new invitation %p", (void*) inv);
942 Invitation* Invitation::Find(Channel* c, LocalUser* u, bool check_expired)
944 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);
945 if (!u || u->invites.empty())
948 InviteList locallist;
949 locallist.swap(u->invites);
951 Invitation* result = NULL;
952 for (InviteList::iterator i = locallist.begin(); i != locallist.end(); )
954 Invitation* inv = *i;
955 if ((check_expired) && (inv->expiry != 0) && (inv->expiry <= ServerInstance->Time()))
957 /* Expired invite, remove it. */
958 std::string expiration = ServerInstance->TimeString(inv->expiry);
959 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Find ecountered expired entry: %p expired %s", (void*) inv, expiration.c_str());
960 i = locallist.erase(i);
966 /* Is it what we're searching for? */
976 locallist.swap(u->invites);
977 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Find result=%p", (void*) result);
981 Invitation::~Invitation()
983 // Remove this entry from both lists
984 InviteList::iterator it = std::find(chan->invites.begin(), chan->invites.end(), this);
985 if (it != chan->invites.end())
986 chan->invites.erase(it);
987 it = std::find(user->invites.begin(), user->invites.end(), this);
988 if (it != user->invites.end())
989 user->invites.erase(it);
992 void InviteBase::ClearInvites()
994 ServerInstance->Logs->Log("INVITEBASE", LOG_DEBUG, "InviteBase::ClearInvites %p", (void*) this);
995 InviteList locallist;
996 locallist.swap(invites);
997 for (InviteList::const_iterator i = locallist.begin(); i != locallist.end(); ++i)