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/>.
32 Channel::Channel(const std::string &cname, time_t ts)
34 chan_hash::iterator findchan = ServerInstance->chanlist->find(cname);
35 if (findchan != ServerInstance->chanlist->end())
36 throw CoreException("Cannot create duplicate channel " + cname);
38 (*(ServerInstance->chanlist))[cname.c_str()] = this;
39 this->name.assign(cname, 0, ServerInstance->Config->Limits.ChanMax);
40 this->age = ts ? ts : ServerInstance->Time();
42 maxbans = topicset = 0;
46 void Channel::SetMode(char mode,bool mode_on)
48 modes[mode-65] = mode_on;
51 void Channel::SetMode(ModeHandler* mh, bool on)
53 modes[mh->GetModeChar() - 65] = on;
56 void Channel::SetModeParam(char mode, const std::string& parameter)
58 CustomModeList::iterator n = custom_mode_params.find(mode);
59 // always erase, even if changing, so that the map gets the new value
60 if (n != custom_mode_params.end())
61 custom_mode_params.erase(n);
62 if (parameter.empty())
64 modes[mode-65] = false;
68 custom_mode_params[mode] = parameter;
69 modes[mode-65] = true;
73 void Channel::SetModeParam(ModeHandler* mode, const std::string& parameter)
75 SetModeParam(mode->GetModeChar(), parameter);
78 std::string Channel::GetModeParameter(char mode)
80 CustomModeList::iterator n = custom_mode_params.find(mode);
81 if (n != custom_mode_params.end())
86 std::string Channel::GetModeParameter(ModeHandler* mode)
88 CustomModeList::iterator n = custom_mode_params.find(mode->GetModeChar());
89 if (n != custom_mode_params.end())
94 int Channel::SetTopic(User *u, std::string &ntopic, bool forceset)
97 u = ServerInstance->FakeClient;
98 if (IS_LOCAL(u) && !forceset)
101 FIRST_MOD_RESULT(OnPreTopicChange, res, (u,this,ntopic));
103 if (res == MOD_RES_DENY)
105 if (res != MOD_RES_ALLOW)
107 if (!this->HasUser(u))
109 u->WriteNumeric(442, "%s %s :You're not on that channel!",u->nick.c_str(), this->name.c_str());
112 if (IsModeSet('t') && !ServerInstance->OnCheckExemption(u,this,"topiclock").check(GetPrefixValue(u) >= HALFOP_VALUE))
114 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());
120 this->topic.assign(ntopic, 0, ServerInstance->Config->Limits.MaxTopic);
123 this->setby.assign(ServerInstance->Config->FullHostInTopic ? u->GetFullHost() : u->nick, 0, 128);
124 this->WriteChannel(u, "TOPIC %s :%s", this->name.c_str(), this->topic.c_str());
128 this->setby.assign(ServerInstance->Config->ServerName);
129 this->WriteChannelWithServ(ServerInstance->Config->ServerName, "TOPIC %s :%s", this->name.c_str(), this->topic.c_str());
132 this->topicset = ServerInstance->Time();
134 FOREACH_MOD(I_OnPostTopicChange,OnPostTopicChange(u, this, this->topic));
139 long Channel::GetUserCounter()
141 return userlist.size();
144 Membership* Channel::AddUser(User* user)
146 Membership* memb = new Membership(user, this);
147 userlist[user] = memb;
151 void Channel::DelUser(User* user)
153 UserMembIter a = userlist.find(user);
155 if (a != userlist.end())
162 if (userlist.empty())
165 FIRST_MOD_RESULT(OnChannelPreDelete, res, (this));
166 if (res == MOD_RES_DENY)
168 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
169 /* kill the record */
170 if (iter != ServerInstance->chanlist->end())
172 FOREACH_MOD(I_OnChannelDelete, OnChannelDelete(this));
173 ServerInstance->chanlist->erase(iter);
177 ServerInstance->GlobalCulls.AddItem(this);
181 bool Channel::HasUser(User* user)
183 return (userlist.find(user) != userlist.end());
186 Membership* Channel::GetUser(User* user)
188 UserMembIter i = userlist.find(user);
189 if (i == userlist.end())
194 const UserMembList* Channel::GetUsers()
199 void Channel::SetDefaultModes()
201 ServerInstance->Logs->Log("CHANNELS", DEBUG, "SetDefaultModes %s",
202 ServerInstance->Config->DefaultModes.c_str());
203 irc::spacesepstream list(ServerInstance->Config->DefaultModes);
205 std::string parameter;
207 list.GetToken(modeseq);
209 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
211 ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
214 if (mode->GetNumParams(true))
215 list.GetToken(parameter);
219 mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, this, parameter, true);
225 * add a channel to a user, creating the record for it if needed and linking
226 * it to the user record
228 Channel* Channel::JoinUser(User *user, const char* cn, bool override, const char* key, bool bursting, time_t TS)
230 // Fix: unregistered users could be joined using /SAJOIN
231 if (!user || !cn || user->registered != REG_ALL)
239 * We don't restrict the number of channels that remote users or users that are override-joining may be in.
240 * We restrict local users to MaxChans channels.
241 * We restrict local operators to OperMaxChans channels.
242 * This is a lot more logical than how it was formerly. -- w00t
244 if (IS_LOCAL(user) && !override)
246 if (user->HasPrivPermission("channels/high-join-limit"))
248 if (user->chans.size() >= ServerInstance->Config->OperMaxChans)
250 user->WriteNumeric(ERR_TOOMANYCHANNELS, "%s %s :You are on too many channels",user->nick.c_str(), cn);
256 unsigned int maxchans = user->GetClass()->maxchans;
258 maxchans = ServerInstance->Config->MaxChans;
259 if (user->chans.size() >= maxchans)
261 user->WriteNumeric(ERR_TOOMANYCHANNELS, "%s %s :You are on too many channels",user->nick.c_str(), cn);
267 strlcpy(cname, cn, ServerInstance->Config->Limits.ChanMax);
268 Ptr = ServerInstance->FindChan(cname);
269 bool created_by_local = false;
274 * Fix: desync bug was here, don't set @ on remote users - spanningtree handles their permissions. bug #358. -- w00t
279 ServerInstance->Logs->Log("CHANNELS",DEBUG,"*** BUG *** Channel::JoinUser called for REMOTE user '%s' on channel '%s' but no TS given!", user->nick.c_str(), cn);
284 created_by_local = true;
287 if (IS_LOCAL(user) && override == false)
289 ModResult MOD_RESULT;
290 FIRST_MOD_RESULT(OnUserPreJoin, MOD_RESULT, (user, NULL, cname, privs, key ? key : ""));
291 if (MOD_RESULT == MOD_RES_DENY)
295 Ptr = new Channel(cname, TS);
299 /* Already on the channel */
300 if (Ptr->HasUser(user))
304 * remote users are allowed us to bypass channel modes
305 * and bans (used by servers)
307 if (IS_LOCAL(user) && override == false)
309 ModResult MOD_RESULT;
310 FIRST_MOD_RESULT(OnUserPreJoin, MOD_RESULT, (user, Ptr, cname, privs, key ? key : ""));
311 if (MOD_RESULT == MOD_RES_DENY)
315 else if (MOD_RESULT == MOD_RES_PASSTHRU)
317 std::string ckey = Ptr->GetModeParameter('k');
318 bool invited = IS_LOCAL(user)->IsInvited(Ptr->name.c_str());
319 bool can_bypass = ServerInstance->Config->InvBypassModes && invited;
323 FIRST_MOD_RESULT(OnCheckKey, MOD_RESULT, (user, Ptr, key ? key : ""));
324 if (!MOD_RESULT.check((key && 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(), Ptr->name.c_str());
332 if (Ptr->IsModeSet('i'))
334 FIRST_MOD_RESULT(OnCheckInvite, MOD_RESULT, (user, Ptr));
335 if (!MOD_RESULT.check(invited))
337 user->WriteNumeric(ERR_INVITEONLYCHAN, "%s %s :Cannot join channel (Invite only)",user->nick.c_str(), Ptr->name.c_str());
342 std::string limit = Ptr->GetModeParameter('l');
345 FIRST_MOD_RESULT(OnCheckLimit, MOD_RESULT, (user, Ptr));
346 if (!MOD_RESULT.check((Ptr->GetUserCounter() < atol(limit.c_str()) || can_bypass)))
348 user->WriteNumeric(ERR_CHANNELISFULL, "%s %s :Cannot join channel (Channel is full)",user->nick.c_str(), Ptr->name.c_str());
353 if (Ptr->IsBanned(user) && !can_bypass)
355 user->WriteNumeric(ERR_BANNEDFROMCHAN, "%s %s :Cannot join channel (You're banned)",user->nick.c_str(), Ptr->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 IS_LOCAL(user)->RemoveInvite(Ptr->name.c_str());
371 if (created_by_local)
373 /* As spotted by jilles, dont bother to set this on remote users */
374 Ptr->SetDefaultModes();
377 return Channel::ForceChan(Ptr, user, privs, bursting, created_by_local);
380 Channel* Channel::ForceChan(Channel* Ptr, User* user, const std::string &privs, bool bursting, bool created)
382 std::string nick = user->nick;
384 Membership* memb = Ptr->AddUser(user);
385 user->chans.insert(Ptr);
387 for (std::string::const_iterator x = privs.begin(); x != privs.end(); x++)
389 const char status = *x;
390 ModeHandler* mh = ServerInstance->Modes->FindMode(status, MODETYPE_CHANNEL);
393 /* Set, and make sure that the mode handler knows this mode was now set */
394 Ptr->SetPrefix(user, mh->GetModeChar(), true);
395 mh->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, Ptr, nick, true);
400 FOREACH_MOD(I_OnUserJoin,OnUserJoin(memb, bursting, created, except_list));
402 Ptr->WriteAllExcept(user, false, 0, except_list, "JOIN :%s", Ptr->name.c_str());
404 /* Theyre not the first ones in here, make sure everyone else sees the modes we gave the user */
405 if ((Ptr->GetUserCounter() > 1) && (!memb->modes.empty()))
407 std::string ms = memb->modes;
408 for(unsigned int i=0; i < memb->modes.length(); i++)
409 ms.append(" ").append(user->nick);
411 except_list.insert(user);
412 Ptr->WriteAllExcept(user, !ServerInstance->Config->CycleHostsFromUser, 0, except_list, "MODE %s +%s", Ptr->name.c_str(), ms.c_str());
419 user->WriteNumeric(RPL_TOPIC, "%s %s :%s", user->nick.c_str(), Ptr->name.c_str(), Ptr->topic.c_str());
420 user->WriteNumeric(RPL_TOPICTIME, "%s %s %s %lu", user->nick.c_str(), Ptr->name.c_str(), Ptr->setby.c_str(), (unsigned long)Ptr->topicset);
424 FOREACH_MOD(I_OnPostJoin,OnPostJoin(memb));
428 bool Channel::IsBanned(User* user)
431 FIRST_MOD_RESULT(OnCheckChannelBan, result, (user, this));
433 if (result != MOD_RES_PASSTHRU)
434 return (result == MOD_RES_DENY);
436 for (BanList::iterator i = this->bans.begin(); i != this->bans.end(); i++)
438 if (CheckBan(user, i->data))
444 bool Channel::CheckBan(User* user, const std::string& mask)
447 FIRST_MOD_RESULT(OnCheckBan, result, (user, this, mask));
448 if (result != MOD_RES_PASSTHRU)
449 return (result == MOD_RES_DENY);
451 // extbans were handled above, if this is one it obviously didn't match
455 std::string::size_type at = mask.find('@');
456 if (at == std::string::npos)
459 char tomatch[MAXBUF];
460 snprintf(tomatch, MAXBUF, "%s!%s", user->nick.c_str(), user->ident.c_str());
461 std::string prefix = mask.substr(0, at);
462 if (InspIRCd::Match(tomatch, 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)
479 for (BanList::iterator i = this->bans.begin(); i != this->bans.end(); i++)
481 if (i->data[0] == type && i->data[1] == ':')
483 std::string val = i->data.substr(2);
484 if (CheckBan(user, val))
488 return MOD_RES_PASSTHRU;
492 * remove a channel from a users record, and return the number of users left.
493 * Therefore, if this function returns 0 the caller should delete the Channel.
495 void Channel::PartUser(User *user, std::string &reason)
500 Membership* memb = GetUser(user);
505 FOREACH_MOD(I_OnUserPart,OnUserPart(memb, reason, except_list));
507 WriteAllExcept(user, false, 0, except_list, "PART %s%s%s", this->name.c_str(), reason.empty() ? "" : " :", reason.c_str());
509 user->chans.erase(this);
510 this->RemoveAllPrefixes(user);
516 void Channel::KickUser(User *src, User *user, const char* reason)
518 if (!src || !user || !reason)
521 Membership* memb = GetUser(user);
526 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());
529 if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
531 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());
536 if (ServerInstance->ULine(src->server))
539 FIRST_MOD_RESULT(OnUserPreKick, res, (src,memb,reason));
541 if (res == MOD_RES_DENY)
544 if (res == MOD_RES_PASSTHRU)
546 unsigned int them = this->GetPrefixValue(src);
547 unsigned int req = HALFOP_VALUE;
548 for (std::string::size_type i = 0; i < memb->modes.length(); i++)
550 ModeHandler* mh = ServerInstance->Modes->FindMode(memb->modes[i], MODETYPE_CHANNEL);
551 if (mh && mh->GetLevelRequired() > req)
552 req = mh->GetLevelRequired();
557 src->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s %s :You must be a channel %soperator",
558 src->nick.c_str(), this->name.c_str(), req > HALFOP_VALUE ? "" : "half-");
567 FOREACH_MOD(I_OnUserKick,OnUserKick(src, memb, reason, except_list));
569 WriteAllExcept(src, false, 0, except_list, "KICK %s %s :%s", name.c_str(), user->nick.c_str(), reason);
571 user->chans.erase(this);
572 this->RemoveAllPrefixes(user);
578 void Channel::WriteChannel(User* user, const char* text, ...)
580 char textbuffer[MAXBUF];
586 va_start(argsPtr, text);
587 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
590 this->WriteChannel(user, std::string(textbuffer));
593 void Channel::WriteChannel(User* user, const std::string &text)
600 snprintf(tb,MAXBUF,":%s %s", user->GetFullHost().c_str(), text.c_str());
601 std::string out = tb;
603 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
605 if (IS_LOCAL(i->first))
606 i->first->Write(out);
610 void Channel::WriteChannelWithServ(const std::string& ServName, const char* text, ...)
612 char textbuffer[MAXBUF];
618 va_start(argsPtr, text);
619 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
622 this->WriteChannelWithServ(ServName, std::string(textbuffer));
625 void Channel::WriteChannelWithServ(const std::string& ServName, const std::string &text)
629 snprintf(tb,MAXBUF,":%s %s", ServName.empty() ? ServerInstance->Config->ServerName.c_str() : ServName.c_str(), text.c_str());
630 std::string out = tb;
632 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
634 if (IS_LOCAL(i->first))
635 i->first->Write(out);
639 /* write formatted text from a source user to all users on a channel except
640 * for the sender (for privmsg etc) */
641 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const char* text, ...)
643 char textbuffer[MAXBUF];
649 va_start(argsPtr, text);
650 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
653 this->WriteAllExceptSender(user, serversource, status, std::string(textbuffer));
656 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const char* text, ...)
658 char textbuffer[MAXBUF];
664 int offset = snprintf(textbuffer,MAXBUF,":%s ", serversource ? ServerInstance->Config->ServerName.c_str() : user->GetFullHost().c_str());
666 va_start(argsPtr, text);
667 vsnprintf(textbuffer + offset, MAXBUF - offset, text, argsPtr);
670 this->RawWriteAllExcept(user, serversource, status, except_list, std::string(textbuffer));
673 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string &text)
677 snprintf(tb,MAXBUF,":%s %s", serversource ? ServerInstance->Config->ServerName.c_str() : user->GetFullHost().c_str(), text.c_str());
678 std::string out = tb;
680 this->RawWriteAllExcept(user, serversource, status, except_list, std::string(tb));
683 void Channel::RawWriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string &out)
685 unsigned int minrank = 0;
688 ModeHandler* mh = ServerInstance->Modes->FindPrefix(status);
690 minrank = mh->GetPrefixRank();
692 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
694 if (IS_LOCAL(i->first) && (except_list.find(i->first) == except_list.end()))
696 /* User doesn't have the status we're after */
697 if (minrank && i->second->getRank() < minrank)
700 i->first->Write(out);
705 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const std::string& text)
708 except_list.insert(user);
709 this->WriteAllExcept(user, serversource, status, except_list, std::string(text));
713 * return a count of the users on a specific channel accounting for
714 * invisible users who won't increase the count. e.g. for /LIST
716 int Channel::CountInvisible()
719 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
721 if (!(i->first->IsModeSet('i')))
728 char* Channel::ChanModes(bool showkey)
730 static char scratch[MAXBUF];
731 static char sparam[MAXBUF];
732 char* offset = scratch;
733 std::string extparam;
738 /* This was still iterating up to 190, Channel::modes is only 64 elements -- Om */
739 for(int n = 0; n < 64; n++)
745 if (n == 'k' - 65 && !showkey)
751 extparam = this->GetModeParameter(n + 65);
753 if (!extparam.empty())
755 charlcat(sparam,' ',MAXBUF);
756 strlcat(sparam,extparam.c_str(),MAXBUF);
761 /* Null terminate scratch */
763 strlcat(scratch,sparam,MAXBUF);
767 /* compile a userlist of a channel into a string, each nick seperated by
768 * spaces and op, voice etc status shown as @ and +, and send it to 'user'
770 void Channel::UserList(User *user)
778 if (this->IsModeSet('s') && !this->HasUser(user) && !user->HasPrivPermission("channels/auspex"))
780 user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nick/channel",user->nick.c_str(), this->name.c_str());
784 dlen = curlen = snprintf(list,MAXBUF,"%s %c %s :", user->nick.c_str(), this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=', this->name.c_str());
787 char* ptr = list + dlen;
789 /* Improvement by Brain - this doesnt change in value, so why was it inside
792 bool has_user = this->HasUser(user);
794 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
796 if ((!has_user) && (i->first->IsModeSet('i')))
799 * user is +i, and source not on the channel, does not show
805 std::string prefixlist = this->GetPrefixChar(i->first);
806 std::string nick = i->first->nick;
808 FOREACH_MOD(I_OnNamesListItem, OnNamesListItem(user, i->second, prefixlist, nick));
810 /* Nick was nuked, a module wants us to skip it */
816 if (curlen + prefixlist.length() + nick.length() + 1 > 480)
818 /* list overflowed into multiple numerics */
819 user->WriteNumeric(RPL_NAMREPLY, std::string(list));
821 /* reset our lengths */
822 dlen = curlen = snprintf(list,MAXBUF,"%s %c %s :", user->nick.c_str(), this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=', this->name.c_str());
828 ptrlen = snprintf(ptr, MAXBUF, "%s%s ", prefixlist.c_str(), nick.c_str());
836 /* if whats left in the list isnt empty, send it */
839 user->WriteNumeric(RPL_NAMREPLY, std::string(list));
842 user->WriteNumeric(RPL_ENDOFNAMES, "%s %s :End of /NAMES list.", user->nick.c_str(), this->name.c_str());
845 long Channel::GetMaxBans()
847 /* Return the cached value if there is one */
849 return this->maxbans;
851 /* If there isnt one, we have to do some O(n) hax to find it the first time. (ick) */
852 for (std::map<std::string,int>::iterator n = ServerInstance->Config->maxbans.begin(); n != ServerInstance->Config->maxbans.end(); n++)
854 if (InspIRCd::Match(this->name, n->first, NULL))
856 this->maxbans = n->second;
861 /* Screw it, just return the default of 64 */
863 return this->maxbans;
866 void Channel::ResetMaxBans()
871 /* returns the status character for a given user on a channel, e.g. @ for op,
872 * % for halfop etc. If the user has several modes set, the highest mode
873 * the user has must be returned.
875 const char* Channel::GetPrefixChar(User *user)
877 static char pf[2] = {0, 0};
879 unsigned int bestrank = 0;
881 UserMembIter m = userlist.find(user);
882 if (m != userlist.end())
884 for(unsigned int i=0; i < m->second->modes.length(); i++)
886 char mchar = m->second->modes[i];
887 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
888 if (mh && mh->GetPrefixRank() > bestrank && mh->GetPrefix())
890 bestrank = mh->GetPrefixRank();
891 pf[0] = mh->GetPrefix();
898 unsigned int Membership::getRank()
900 char mchar = modes.c_str()[0];
904 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
906 rv = mh->GetPrefixRank();
911 const char* Channel::GetAllPrefixChars(User* user)
913 static char prefix[64];
916 UserMembIter m = userlist.find(user);
917 if (m != userlist.end())
919 for(unsigned int i=0; i < m->second->modes.length(); i++)
921 char mchar = m->second->modes[i];
922 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
923 if (mh && mh->GetPrefix())
924 prefix[ctr++] = mh->GetPrefix();
932 unsigned int Channel::GetPrefixValue(User* user)
934 UserMembIter m = userlist.find(user);
935 if (m == userlist.end())
937 return m->second->getRank();
940 bool Channel::SetPrefix(User* user, char prefix, bool adding)
942 ModeHandler* delta_mh = ServerInstance->Modes->FindMode(prefix, MODETYPE_CHANNEL);
945 UserMembIter m = userlist.find(user);
946 if (m == userlist.end())
948 for(unsigned int i=0; i < m->second->modes.length(); i++)
950 char mchar = m->second->modes[i];
951 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
952 if (mh && mh->GetPrefixRank() <= delta_mh->GetPrefixRank())
955 m->second->modes.substr(0,i) +
956 (adding ? std::string(1, prefix) : "") +
957 m->second->modes.substr(mchar == prefix ? i+1 : i);
958 return adding != (mchar == prefix);
962 m->second->modes += std::string(1, prefix);
966 void Channel::RemoveAllPrefixes(User* user)
968 UserMembIter m = userlist.find(user);
969 if (m != userlist.end())
971 m->second->modes.clear();
975 void Invitation::Create(Channel* c, LocalUser* u, time_t timeout)
977 if ((timeout != 0) && (ServerInstance->Time() >= timeout))
978 // Expired, don't bother
981 ServerInstance->Logs->Log("INVITATION", DEBUG, "Invitation::Create chan=%s user=%s", c->name.c_str(), u->uuid.c_str());
983 Invitation* inv = Invitation::Find(c, u, false);
986 if ((inv->expiry == 0) || (inv->expiry > timeout))
988 inv->expiry = timeout;
989 ServerInstance->Logs->Log("INVITATION", DEBUG, "Invitation::Create changed expiry in existing invitation %p", (void*) inv);
993 inv = new Invitation(c, u, timeout);
994 c->invites.push_back(inv);
995 u->invites.push_back(inv);
996 ServerInstance->Logs->Log("INVITATION", DEBUG, "Invitation::Create created new invitation %p", (void*) inv);
1000 Invitation* Invitation::Find(Channel* c, LocalUser* u, bool check_expired)
1002 ServerInstance->Logs->Log("INVITATION", DEBUG, "Invitation::Find chan=%s user=%s check_expired=%d", c ? c->name.c_str() : "NULL", u ? u->uuid.c_str() : "NULL", check_expired);
1003 if (!u || u->invites.empty())
1006 InviteList locallist;
1007 locallist.swap(u->invites);
1009 Invitation* result = NULL;
1010 for (InviteList::iterator i = locallist.begin(); i != locallist.end(); )
1012 Invitation* inv = *i;
1013 if ((check_expired) && (inv->expiry != 0) && (inv->expiry <= ServerInstance->Time()))
1015 /* Expired invite, remove it. */
1016 ServerInstance->Logs->Log("INVITATION", DEBUG, "Invitation::Find ecountered expired entry: %p timed out at %lu", (void*) inv, inv->expiry);
1017 i = locallist.erase(i);
1023 /* Is it what we're searching for? */
1033 locallist.swap(u->invites);
1034 ServerInstance->Logs->Log("INVITATION", DEBUG, "Invitation::Find result=%p", (void*) result);
1038 Invitation::~Invitation()
1040 // Remove this entry from both lists
1041 InviteList::iterator it = std::find(chan->invites.begin(), chan->invites.end(), this);
1042 if (it != chan->invites.end())
1043 chan->invites.erase(it);
1044 it = std::find(user->invites.begin(), user->invites.end(), this);
1045 if (it != user->invites.end())
1046 user->invites.erase(it);
1049 void InviteBase::ClearInvites()
1051 ServerInstance->Logs->Log("INVITEBASE", DEBUG, "InviteBase::ClearInvites %p", (void*) this);
1052 InviteList locallist;
1053 locallist.swap(invites);
1054 for (InviteList::const_iterator i = locallist.begin(); i != locallist.end(); ++i)