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 chan_hash::iterator findchan = ServerInstance->chanlist->find(cname);
38 if (findchan != ServerInstance->chanlist->end())
39 throw CoreException("Cannot create duplicate channel " + cname);
41 (*(ServerInstance->chanlist))[cname.c_str()] = this;
42 this->name.assign(cname, 0, ServerInstance->Config->Limits.ChanMax);
43 this->age = ts ? ts : ServerInstance->Time();
49 void Channel::SetMode(char mode,bool mode_on)
51 modes[mode-65] = mode_on;
54 void Channel::SetMode(ModeHandler* mh, bool on)
56 modes[mh->GetModeChar() - 65] = on;
59 void Channel::SetModeParam(char mode, const std::string& parameter)
61 CustomModeList::iterator n = custom_mode_params.find(mode);
62 // always erase, even if changing, so that the map gets the new value
63 if (n != custom_mode_params.end())
64 custom_mode_params.erase(n);
65 if (parameter.empty())
67 modes[mode-65] = false;
71 custom_mode_params[mode] = parameter;
72 modes[mode-65] = true;
76 void Channel::SetModeParam(ModeHandler* mode, const std::string& parameter)
78 SetModeParam(mode->GetModeChar(), parameter);
81 std::string Channel::GetModeParameter(char mode)
83 CustomModeList::iterator n = custom_mode_params.find(mode);
84 if (n != custom_mode_params.end())
89 std::string Channel::GetModeParameter(ModeHandler* mode)
91 CustomModeList::iterator n = custom_mode_params.find(mode->GetModeChar());
92 if (n != custom_mode_params.end())
97 int Channel::SetTopic(User *u, std::string &ntopic, bool forceset)
100 u = ServerInstance->FakeClient;
101 if (IS_LOCAL(u) && !forceset)
104 FIRST_MOD_RESULT(OnPreTopicChange, res, (u,this,ntopic));
106 if (res == MOD_RES_DENY)
108 if (res != MOD_RES_ALLOW)
110 if (!this->HasUser(u))
112 u->WriteNumeric(442, "%s %s :You're not on that channel!",u->nick.c_str(), this->name.c_str());
115 if (IsModeSet('t') && !ServerInstance->OnCheckExemption(u,this,"topiclock").check(GetPrefixValue(u) >= HALFOP_VALUE))
117 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());
123 this->topic.assign(ntopic, 0, ServerInstance->Config->Limits.MaxTopic);
126 this->setby.assign(ServerInstance->Config->FullHostInTopic ? u->GetFullHost() : u->nick, 0, 128);
127 this->WriteChannel(u, "TOPIC %s :%s", this->name.c_str(), this->topic.c_str());
131 this->setby.assign(ServerInstance->Config->ServerName);
132 this->WriteChannelWithServ(ServerInstance->Config->ServerName, "TOPIC %s :%s", this->name.c_str(), this->topic.c_str());
135 this->topicset = ServerInstance->Time();
137 FOREACH_MOD(I_OnPostTopicChange,OnPostTopicChange(u, this, this->topic));
142 long Channel::GetUserCounter()
144 return userlist.size();
147 Membership* Channel::AddUser(User* user)
149 Membership*& memb = userlist[user];
153 memb = new Membership(user, this);
157 void Channel::DelUser(User* user)
159 UserMembIter a = userlist.find(user);
161 if (a != userlist.end())
168 if (userlist.empty())
171 FIRST_MOD_RESULT(OnChannelPreDelete, res, (this));
172 if (res == MOD_RES_DENY)
174 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
175 /* kill the record */
176 if (iter != ServerInstance->chanlist->end())
178 FOREACH_MOD(I_OnChannelDelete, OnChannelDelete(this));
179 ServerInstance->chanlist->erase(iter);
183 ServerInstance->GlobalCulls.AddItem(this);
187 bool Channel::HasUser(User* user)
189 return (userlist.find(user) != userlist.end());
192 Membership* Channel::GetUser(User* user)
194 UserMembIter i = userlist.find(user);
195 if (i == userlist.end())
200 const UserMembList* Channel::GetUsers()
205 void Channel::SetDefaultModes()
207 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "SetDefaultModes %s",
208 ServerInstance->Config->DefaultModes.c_str());
209 irc::spacesepstream list(ServerInstance->Config->DefaultModes);
211 std::string parameter;
213 list.GetToken(modeseq);
215 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
217 ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
220 if (mode->GetNumParams(true))
221 list.GetToken(parameter);
225 mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, this, parameter, true);
231 * add a channel to a user, creating the record for it if needed and linking
232 * it to the user record
234 Channel* Channel::JoinUser(LocalUser* user, std::string cname, bool override, const std::string& key)
236 if (user->registered != REG_ALL)
238 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "Attempted to join unregistered user " + user->uuid + " to channel " + cname);
243 * We don't restrict the number of channels that remote users or users that are override-joining may be in.
244 * We restrict local users to MaxChans channels.
245 * We restrict local operators to OperMaxChans channels.
246 * This is a lot more logical than how it was formerly. -- w00t
250 if (user->HasPrivPermission("channels/high-join-limit"))
252 if (user->chans.size() >= ServerInstance->Config->OperMaxChans)
254 user->WriteNumeric(ERR_TOOMANYCHANNELS, "%s %s :You are on too many channels",user->nick.c_str(), cname.c_str());
260 unsigned int maxchans = user->GetClass()->maxchans;
262 maxchans = ServerInstance->Config->MaxChans;
263 if (user->chans.size() >= maxchans)
265 user->WriteNumeric(ERR_TOOMANYCHANNELS, "%s %s :You are on too many channels",user->nick.c_str(), cname.c_str());
271 // Crop channel name if it's too long
272 if (cname.length() > ServerInstance->Config->Limits.ChanMax)
273 cname.resize(ServerInstance->Config->Limits.ChanMax);
275 Channel* chan = ServerInstance->FindChan(cname);
276 bool created_by_local = (chan == NULL); // Flag that will be passed to modules in the OnUserJoin() hook later
277 std::string privs; // Prefix mode(letter)s to give to the joining user
283 if (override == false)
285 // Ask the modules whether they're ok with the join, pass NULL as Channel* as the channel is yet to be created
286 ModResult MOD_RESULT;
287 FIRST_MOD_RESULT(OnUserPreJoin, MOD_RESULT, (user, NULL, cname, privs, key));
288 if (MOD_RESULT == MOD_RES_DENY)
289 return NULL; // A module wasn't happy with the join, abort
292 chan = new Channel(cname, ServerInstance->Time());
293 // Set the default modes on the channel (<options:defaultmodes>)
294 chan->SetDefaultModes();
298 /* Already on the channel */
299 if (chan->HasUser(user))
302 if (override == false)
304 ModResult MOD_RESULT;
305 FIRST_MOD_RESULT(OnUserPreJoin, MOD_RESULT, (user, chan, cname, privs, key));
307 // A module explicitly denied the join and (hopefully) generated a message
308 // describing the situation, so we may stop here without sending anything
309 if (MOD_RESULT == MOD_RES_DENY)
312 // If no module returned MOD_RES_DENY or MOD_RES_ALLOW (which is the case
313 // most of the time) then proceed to check channel modes +k, +i, +l and bans,
315 // If a module explicitly allowed the join (by returning MOD_RES_ALLOW),
316 // then this entire section is skipped
317 if (MOD_RESULT == MOD_RES_PASSTHRU)
319 std::string ckey = chan->GetModeParameter('k');
320 bool invited = user->IsInvited(chan);
321 bool can_bypass = ServerInstance->Config->InvBypassModes && invited;
325 FIRST_MOD_RESULT(OnCheckKey, MOD_RESULT, (user, chan, key));
326 if (!MOD_RESULT.check((ckey == key) || can_bypass))
328 // If no key provided, or key is not the right one, and can't bypass +k (not invited or option not enabled)
329 user->WriteNumeric(ERR_BADCHANNELKEY, "%s %s :Cannot join channel (Incorrect channel key)",user->nick.c_str(), chan->name.c_str());
334 if (chan->IsModeSet('i'))
336 FIRST_MOD_RESULT(OnCheckInvite, MOD_RESULT, (user, chan));
337 if (!MOD_RESULT.check(invited))
339 user->WriteNumeric(ERR_INVITEONLYCHAN, "%s %s :Cannot join channel (Invite only)",user->nick.c_str(), chan->name.c_str());
344 std::string limit = chan->GetModeParameter('l');
347 FIRST_MOD_RESULT(OnCheckLimit, MOD_RESULT, (user, chan));
348 if (!MOD_RESULT.check((chan->GetUserCounter() < atol(limit.c_str()) || can_bypass)))
350 user->WriteNumeric(ERR_CHANNELISFULL, "%s %s :Cannot join channel (Channel is full)",user->nick.c_str(), chan->name.c_str());
355 if (chan->IsBanned(user) && !can_bypass)
357 user->WriteNumeric(ERR_BANNEDFROMCHAN, "%s %s :Cannot join channel (You're banned)",user->nick.c_str(), chan->name.c_str());
362 * If the user has invites for this channel, remove them now
363 * after a successful join so they don't build up.
367 user->RemoveInvite(chan);
373 // We figured that this join is allowed and also created the
374 // channel if it didn't exist before, now do the actual join
375 chan->ForceJoin(user, &privs, false, created_by_local);
379 void Channel::ForceJoin(User* user, const std::string* privs, bool bursting, bool created_by_local)
381 Membership* memb = this->AddUser(user);
383 return; // Already on the channel
387 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "Attempted to join server user " + user->uuid + " to channel " + this->name);
391 user->chans.insert(this);
395 // If the user was granted prefix modes (in the OnUserPreJoin hook, or he's a
396 // remote user and his own server set the modes), then set them internally now
397 memb->modes = *privs;
398 for (std::string::const_iterator i = privs->begin(); i != privs->end(); ++i)
400 ModeHandler* mh = ServerInstance->Modes->FindMode(*i, MODETYPE_CHANNEL);
403 std::string nick = user->nick;
404 /* Set, and make sure that the mode handler knows this mode was now set */
405 this->SetPrefix(user, mh->GetModeChar(), true);
406 mh->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, this, nick, true);
411 // 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
413 FOREACH_MOD(I_OnUserJoin,OnUserJoin(memb, bursting, created_by_local, except_list));
415 this->WriteAllExcept(user, false, 0, except_list, "JOIN :%s", this->name.c_str());
417 /* Theyre not the first ones in here, make sure everyone else sees the modes we gave the user */
418 if ((GetUserCounter() > 1) && (!memb->modes.empty()))
420 std::string ms = memb->modes;
421 for(unsigned int i=0; i < memb->modes.length(); i++)
422 ms.append(" ").append(user->nick);
424 except_list.insert(user);
425 this->WriteAllExcept(user, !ServerInstance->Config->CycleHostsFromUser, 0, except_list, "MODE %s +%s", this->name.c_str(), ms.c_str());
432 user->WriteNumeric(RPL_TOPIC, "%s %s :%s", user->nick.c_str(), this->name.c_str(), this->topic.c_str());
433 user->WriteNumeric(RPL_TOPICTIME, "%s %s %s %lu", user->nick.c_str(), this->name.c_str(), this->setby.c_str(), (unsigned long)this->topicset);
435 this->UserList(user);
438 FOREACH_MOD(I_OnPostJoin,OnPostJoin(memb));
441 bool Channel::IsBanned(User* user)
444 FIRST_MOD_RESULT(OnCheckChannelBan, result, (user, this));
446 if (result != MOD_RES_PASSTHRU)
447 return (result == MOD_RES_DENY);
449 ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
450 const ListModeBase::ModeList* bans = banlm->GetList(this);
453 for (ListModeBase::ModeList::const_iterator it = bans->begin(); it != bans->end(); it++)
455 if (CheckBan(user, it->mask))
462 bool Channel::CheckBan(User* user, const std::string& mask)
465 FIRST_MOD_RESULT(OnCheckBan, result, (user, this, mask));
466 if (result != MOD_RES_PASSTHRU)
467 return (result == MOD_RES_DENY);
469 // extbans were handled above, if this is one it obviously didn't match
470 if ((mask.length() <= 2) || (mask[1] == ':'))
473 std::string::size_type at = mask.find('@');
474 if (at == std::string::npos)
477 char tomatch[MAXBUF];
478 snprintf(tomatch, MAXBUF, "%s!%s", user->nick.c_str(), user->ident.c_str());
479 std::string prefix = mask.substr(0, at);
480 if (InspIRCd::Match(tomatch, prefix, NULL))
482 std::string suffix = mask.substr(at + 1);
483 if (InspIRCd::Match(user->host, suffix, NULL) ||
484 InspIRCd::Match(user->dhost, suffix, NULL) ||
485 InspIRCd::MatchCIDR(user->GetIPString(), suffix, NULL))
491 ModResult Channel::GetExtBanStatus(User *user, char type)
494 FIRST_MOD_RESULT(OnExtBanCheck, rv, (user, this, type));
495 if (rv != MOD_RES_PASSTHRU)
498 ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
499 const ListModeBase::ModeList* bans = banlm->GetList(this);
503 for (ListModeBase::ModeList::const_iterator it = bans->begin(); it != bans->end(); ++it)
505 if (CheckBan(user, it->mask))
509 return MOD_RES_PASSTHRU;
513 * remove a channel from a users record, and return the number of users left.
514 * Therefore, if this function returns 0 the caller should delete the Channel.
516 void Channel::PartUser(User *user, std::string &reason)
521 Membership* memb = GetUser(user);
526 FOREACH_MOD(I_OnUserPart,OnUserPart(memb, reason, except_list));
528 WriteAllExcept(user, false, 0, except_list, "PART %s%s%s", this->name.c_str(), reason.empty() ? "" : " :", reason.c_str());
530 user->chans.erase(this);
531 this->RemoveAllPrefixes(user);
537 void Channel::KickUser(User *src, User *user, const char* reason)
539 if (!src || !user || !reason)
542 Membership* memb = GetUser(user);
547 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());
550 if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
552 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());
557 if (ServerInstance->ULine(src->server))
560 FIRST_MOD_RESULT(OnUserPreKick, res, (src,memb,reason));
562 if (res == MOD_RES_DENY)
565 if (res == MOD_RES_PASSTHRU)
567 unsigned int them = this->GetPrefixValue(src);
568 unsigned int req = HALFOP_VALUE;
569 for (std::string::size_type i = 0; i < memb->modes.length(); i++)
571 ModeHandler* mh = ServerInstance->Modes->FindMode(memb->modes[i], MODETYPE_CHANNEL);
572 if (mh && mh->GetLevelRequired() > req)
573 req = mh->GetLevelRequired();
578 src->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s %s :You must be a channel %soperator",
579 src->nick.c_str(), this->name.c_str(), req > HALFOP_VALUE ? "" : "half-");
588 FOREACH_MOD(I_OnUserKick,OnUserKick(src, memb, reason, except_list));
590 WriteAllExcept(src, false, 0, except_list, "KICK %s %s :%s", name.c_str(), user->nick.c_str(), reason);
592 user->chans.erase(this);
593 this->RemoveAllPrefixes(user);
599 void Channel::WriteChannel(User* user, const char* text, ...)
601 char textbuffer[MAXBUF];
607 va_start(argsPtr, text);
608 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
611 this->WriteChannel(user, std::string(textbuffer));
614 void Channel::WriteChannel(User* user, const std::string &text)
621 snprintf(tb,MAXBUF,":%s %s", user->GetFullHost().c_str(), text.c_str());
622 std::string out = tb;
624 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
626 if (IS_LOCAL(i->first))
627 i->first->Write(out);
631 void Channel::WriteChannelWithServ(const std::string& ServName, const char* text, ...)
633 char textbuffer[MAXBUF];
639 va_start(argsPtr, text);
640 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
643 this->WriteChannelWithServ(ServName, std::string(textbuffer));
646 void Channel::WriteChannelWithServ(const std::string& ServName, const std::string &text)
650 snprintf(tb,MAXBUF,":%s %s", ServName.empty() ? ServerInstance->Config->ServerName.c_str() : ServName.c_str(), text.c_str());
651 std::string out = tb;
653 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
655 if (IS_LOCAL(i->first))
656 i->first->Write(out);
660 /* write formatted text from a source user to all users on a channel except
661 * for the sender (for privmsg etc) */
662 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const char* text, ...)
664 char textbuffer[MAXBUF];
670 va_start(argsPtr, text);
671 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
674 this->WriteAllExceptSender(user, serversource, status, std::string(textbuffer));
677 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const char* text, ...)
679 char textbuffer[MAXBUF];
685 int offset = snprintf(textbuffer,MAXBUF,":%s ", serversource ? ServerInstance->Config->ServerName.c_str() : user->GetFullHost().c_str());
687 va_start(argsPtr, text);
688 vsnprintf(textbuffer + offset, MAXBUF - offset, text, argsPtr);
691 this->RawWriteAllExcept(user, serversource, status, except_list, std::string(textbuffer));
694 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string &text)
698 snprintf(tb,MAXBUF,":%s %s", serversource ? ServerInstance->Config->ServerName.c_str() : user->GetFullHost().c_str(), text.c_str());
699 std::string out = tb;
701 this->RawWriteAllExcept(user, serversource, status, except_list, std::string(tb));
704 void Channel::RawWriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string &out)
706 unsigned int minrank = 0;
709 ModeHandler* mh = ServerInstance->Modes->FindPrefix(status);
711 minrank = mh->GetPrefixRank();
713 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
715 if (IS_LOCAL(i->first) && (except_list.find(i->first) == except_list.end()))
717 /* User doesn't have the status we're after */
718 if (minrank && i->second->getRank() < minrank)
721 i->first->Write(out);
726 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const std::string& text)
729 except_list.insert(user);
730 this->WriteAllExcept(user, serversource, status, except_list, std::string(text));
734 * return a count of the users on a specific channel accounting for
735 * invisible users who won't increase the count. e.g. for /LIST
737 int Channel::CountInvisible()
740 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
742 if (!i->first->quitting && !i->first->IsModeSet('i'))
749 char* Channel::ChanModes(bool showkey)
751 static char scratch[MAXBUF];
752 static char sparam[MAXBUF];
753 char* offset = scratch;
754 std::string extparam;
759 /* This was still iterating up to 190, Channel::modes is only 64 elements -- Om */
760 for(int n = 0; n < 64; n++)
766 if (n == 'k' - 65 && !showkey)
772 extparam = this->GetModeParameter(n + 65);
774 if (!extparam.empty())
776 charlcat(sparam,' ',MAXBUF);
777 strlcat(sparam,extparam.c_str(),MAXBUF);
782 /* Null terminate scratch */
784 strlcat(scratch,sparam,MAXBUF);
788 /* compile a userlist of a channel into a string, each nick seperated by
789 * spaces and op, voice etc status shown as @ and +, and send it to 'user'
791 void Channel::UserList(User *user)
799 if (this->IsModeSet('s') && !this->HasUser(user) && !user->HasPrivPermission("channels/auspex"))
801 user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nick/channel",user->nick.c_str(), this->name.c_str());
805 dlen = curlen = snprintf(list,MAXBUF,"%s %c %s :", user->nick.c_str(), this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=', this->name.c_str());
808 char* ptr = list + dlen;
810 /* Improvement by Brain - this doesnt change in value, so why was it inside
813 bool has_user = this->HasUser(user);
815 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
817 if (i->first->quitting)
819 if ((!has_user) && (i->first->IsModeSet('i')))
822 * user is +i, and source not on the channel, does not show
828 std::string prefixlist = this->GetPrefixChar(i->first);
829 std::string nick = i->first->nick;
831 FOREACH_MOD(I_OnNamesListItem, OnNamesListItem(user, i->second, prefixlist, nick));
833 /* Nick was nuked, a module wants us to skip it */
839 if (curlen + prefixlist.length() + nick.length() + 1 > 480)
841 /* list overflowed into multiple numerics */
842 user->WriteNumeric(RPL_NAMREPLY, std::string(list));
844 /* reset our lengths */
845 dlen = curlen = snprintf(list,MAXBUF,"%s %c %s :", user->nick.c_str(), this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=', this->name.c_str());
851 ptrlen = snprintf(ptr, MAXBUF, "%s%s ", prefixlist.c_str(), nick.c_str());
859 /* if whats left in the list isnt empty, send it */
862 user->WriteNumeric(RPL_NAMREPLY, std::string(list));
865 user->WriteNumeric(RPL_ENDOFNAMES, "%s %s :End of /NAMES list.", user->nick.c_str(), this->name.c_str());
868 /* returns the status character for a given user on a channel, e.g. @ for op,
869 * % for halfop etc. If the user has several modes set, the highest mode
870 * the user has must be returned.
872 const char* Channel::GetPrefixChar(User *user)
874 static char pf[2] = {0, 0};
876 unsigned int bestrank = 0;
878 UserMembIter m = userlist.find(user);
879 if (m != userlist.end())
881 for(unsigned int i=0; i < m->second->modes.length(); i++)
883 char mchar = m->second->modes[i];
884 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
885 if (mh && mh->GetPrefixRank() > bestrank && mh->GetPrefix())
887 bestrank = mh->GetPrefixRank();
888 pf[0] = mh->GetPrefix();
895 unsigned int Membership::getRank()
897 char mchar = modes.c_str()[0];
901 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
903 rv = mh->GetPrefixRank();
908 const char* Channel::GetAllPrefixChars(User* user)
910 static char prefix[64];
913 UserMembIter m = userlist.find(user);
914 if (m != userlist.end())
916 for(unsigned int i=0; i < m->second->modes.length(); i++)
918 char mchar = m->second->modes[i];
919 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
920 if (mh && mh->GetPrefix())
921 prefix[ctr++] = mh->GetPrefix();
929 unsigned int Channel::GetPrefixValue(User* user)
931 UserMembIter m = userlist.find(user);
932 if (m == userlist.end())
934 return m->second->getRank();
937 bool Channel::SetPrefix(User* user, char prefix, bool adding)
939 ModeHandler* delta_mh = ServerInstance->Modes->FindMode(prefix, MODETYPE_CHANNEL);
942 UserMembIter m = userlist.find(user);
943 if (m == userlist.end())
945 for(unsigned int i=0; i < m->second->modes.length(); i++)
947 char mchar = m->second->modes[i];
948 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
949 if (mh && mh->GetPrefixRank() <= delta_mh->GetPrefixRank())
952 m->second->modes.substr(0,i) +
953 (adding ? std::string(1, prefix) : "") +
954 m->second->modes.substr(mchar == prefix ? i+1 : i);
955 return adding != (mchar == prefix);
959 m->second->modes += std::string(1, prefix);
963 void Channel::RemoveAllPrefixes(User* user)
965 UserMembIter m = userlist.find(user);
966 if (m != userlist.end())
968 m->second->modes.clear();
972 void Invitation::Create(Channel* c, LocalUser* u, time_t timeout)
974 if ((timeout != 0) && (ServerInstance->Time() >= timeout))
975 // Expired, don't bother
978 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Create chan=%s user=%s", c->name.c_str(), u->uuid.c_str());
980 Invitation* inv = Invitation::Find(c, u, false);
983 if ((inv->expiry == 0) || (inv->expiry > timeout))
985 inv->expiry = timeout;
986 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Create changed expiry in existing invitation %p", (void*) inv);
990 inv = new Invitation(c, u, timeout);
991 c->invites.push_back(inv);
992 u->invites.push_back(inv);
993 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Create created new invitation %p", (void*) inv);
997 Invitation* Invitation::Find(Channel* c, LocalUser* u, bool check_expired)
999 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);
1000 if (!u || u->invites.empty())
1003 InviteList locallist;
1004 locallist.swap(u->invites);
1006 Invitation* result = NULL;
1007 for (InviteList::iterator i = locallist.begin(); i != locallist.end(); )
1009 Invitation* inv = *i;
1010 if ((check_expired) && (inv->expiry != 0) && (inv->expiry <= ServerInstance->Time()))
1012 /* Expired invite, remove it. */
1013 std::string expiration = ServerInstance->TimeString(inv->expiry);
1014 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Find ecountered expired entry: %p expired %s", (void*) inv, expiration.c_str());
1015 i = locallist.erase(i);
1021 /* Is it what we're searching for? */
1031 locallist.swap(u->invites);
1032 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Find result=%p", (void*) result);
1036 Invitation::~Invitation()
1038 // Remove this entry from both lists
1039 InviteList::iterator it = std::find(chan->invites.begin(), chan->invites.end(), this);
1040 if (it != chan->invites.end())
1041 chan->invites.erase(it);
1042 it = std::find(user->invites.begin(), user->invites.end(), this);
1043 if (it != user->invites.end())
1044 user->invites.erase(it);
1047 void InviteBase::ClearInvites()
1049 ServerInstance->Logs->Log("INVITEBASE", LOG_DEBUG, "InviteBase::ClearInvites %p", (void*) this);
1050 InviteList locallist;
1051 locallist.swap(invites);
1052 for (InviteList::const_iterator i = locallist.begin(); i != locallist.end(); ++i)