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 if (!ServerInstance->chanlist->insert(std::make_pair(cname, this)).second)
35 throw CoreException("Cannot create duplicate channel " + cname);
38 this->age = ts ? ts : ServerInstance->Time();
40 maxbans = topicset = 0;
44 void Channel::SetMode(char mode,bool mode_on)
46 modes[mode-65] = mode_on;
49 void Channel::SetMode(ModeHandler* mh, bool on)
51 modes[mh->GetModeChar() - 65] = on;
54 void Channel::SetModeParam(char mode, const std::string& parameter)
56 CustomModeList::iterator n = custom_mode_params.find(mode);
57 // always erase, even if changing, so that the map gets the new value
58 if (n != custom_mode_params.end())
59 custom_mode_params.erase(n);
60 if (parameter.empty())
62 modes[mode-65] = false;
66 custom_mode_params[mode] = parameter;
67 modes[mode-65] = true;
71 void Channel::SetModeParam(ModeHandler* mode, const std::string& parameter)
73 SetModeParam(mode->GetModeChar(), parameter);
76 std::string Channel::GetModeParameter(char mode)
78 CustomModeList::iterator n = custom_mode_params.find(mode);
79 if (n != custom_mode_params.end())
84 std::string Channel::GetModeParameter(ModeHandler* mode)
86 CustomModeList::iterator n = custom_mode_params.find(mode->GetModeChar());
87 if (n != custom_mode_params.end())
92 int Channel::SetTopic(User *u, std::string &ntopic, bool forceset)
95 u = ServerInstance->FakeClient;
96 if (IS_LOCAL(u) && !forceset)
99 FIRST_MOD_RESULT(OnPreTopicChange, res, (u,this,ntopic));
101 if (res == MOD_RES_DENY)
103 if (res != MOD_RES_ALLOW)
105 if (!this->HasUser(u))
107 u->WriteNumeric(442, "%s %s :You're not on that channel!",u->nick.c_str(), this->name.c_str());
110 if (IsModeSet('t') && !ServerInstance->OnCheckExemption(u,this,"topiclock").check(GetPrefixValue(u) >= HALFOP_VALUE))
112 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());
118 this->topic.assign(ntopic, 0, ServerInstance->Config->Limits.MaxTopic);
119 this->setby.assign(ServerInstance->Config->FullHostInTopic ? u->GetFullHost() : u->nick, 0, 128);
120 this->WriteChannel(u, "TOPIC %s :%s", this->name.c_str(), this->topic.c_str());
122 this->topicset = ServerInstance->Time();
124 FOREACH_MOD(I_OnPostTopicChange,OnPostTopicChange(u, this, this->topic));
129 long Channel::GetUserCounter()
131 return userlist.size();
134 Membership* Channel::AddUser(User* user)
136 Membership* memb = new Membership(user, this);
137 userlist[user] = memb;
141 void Channel::DelUser(User* user)
143 UserMembIter a = userlist.find(user);
145 if (a != userlist.end())
152 if (userlist.empty())
155 FIRST_MOD_RESULT(OnChannelPreDelete, res, (this));
156 if (res == MOD_RES_DENY)
158 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
159 /* kill the record */
160 if (iter != ServerInstance->chanlist->end())
162 FOREACH_MOD(I_OnChannelDelete, OnChannelDelete(this));
163 ServerInstance->chanlist->erase(iter);
167 ServerInstance->GlobalCulls.AddItem(this);
171 bool Channel::HasUser(User* user)
173 return (userlist.find(user) != userlist.end());
176 Membership* Channel::GetUser(User* user)
178 UserMembIter i = userlist.find(user);
179 if (i == userlist.end())
184 const UserMembList* Channel::GetUsers()
189 void Channel::SetDefaultModes()
191 ServerInstance->Logs->Log("CHANNELS", DEBUG, "SetDefaultModes %s",
192 ServerInstance->Config->DefaultModes.c_str());
193 irc::spacesepstream list(ServerInstance->Config->DefaultModes);
195 std::string parameter;
197 list.GetToken(modeseq);
199 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
201 ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
204 if (mode->GetNumParams(true))
205 list.GetToken(parameter);
209 if ((mode->GetNumParams(true)) && (parameter.empty()))
212 mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, this, parameter, true);
218 * add a channel to a user, creating the record for it if needed and linking
219 * it to the user record
221 Channel* Channel::JoinUser(User *user, const char* cn, bool override, const char* key, bool bursting, time_t TS)
223 // Fix: unregistered users could be joined using /SAJOIN
224 if (!user || !cn || user->registered != REG_ALL)
231 * We don't restrict the number of channels that remote users or users that are override-joining may be in.
232 * We restrict local users to MaxChans channels.
233 * We restrict local operators to OperMaxChans channels.
234 * This is a lot more logical than how it was formerly. -- w00t
236 if (IS_LOCAL(user) && !override)
238 if (user->HasPrivPermission("channels/high-join-limit"))
240 if (user->chans.size() >= ServerInstance->Config->OperMaxChans)
242 user->WriteNumeric(ERR_TOOMANYCHANNELS, "%s %s :You are on too many channels",user->nick.c_str(), cn);
248 unsigned int maxchans = user->GetClass()->maxchans;
250 maxchans = ServerInstance->Config->MaxChans;
251 if (user->chans.size() >= maxchans)
253 user->WriteNumeric(ERR_TOOMANYCHANNELS, "%s %s :You are on too many channels",user->nick.c_str(), cn);
260 cname.assign(std::string(cn), 0, ServerInstance->Config->Limits.ChanMax);
261 Ptr = ServerInstance->FindChan(cname);
262 bool created_by_local = false;
267 * Fix: desync bug was here, don't set @ on remote users - spanningtree handles their permissions. bug #358. -- w00t
272 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);
277 created_by_local = true;
280 if (IS_LOCAL(user) && override == false)
282 ModResult MOD_RESULT;
283 FIRST_MOD_RESULT(OnUserPreJoin, MOD_RESULT, (user, NULL, cname.c_str(), privs, key ? key : ""));
284 if (MOD_RESULT == MOD_RES_DENY)
288 Ptr = new Channel(cname, TS);
292 /* Already on the channel */
293 if (Ptr->HasUser(user))
297 * remote users are allowed us to bypass channel modes
298 * and bans (used by servers)
300 if (IS_LOCAL(user) && override == false)
302 ModResult MOD_RESULT;
303 FIRST_MOD_RESULT(OnUserPreJoin, MOD_RESULT, (user, Ptr, cname.c_str(), privs, key ? key : ""));
304 if (MOD_RESULT == MOD_RES_DENY)
308 else if (MOD_RESULT == MOD_RES_PASSTHRU)
310 std::string ckey = Ptr->GetModeParameter('k');
311 bool invited = IS_LOCAL(user)->IsInvited(Ptr->name.c_str());
312 bool can_bypass = ServerInstance->Config->InvBypassModes && invited;
316 FIRST_MOD_RESULT(OnCheckKey, MOD_RESULT, (user, Ptr, key ? key : ""));
317 if (!MOD_RESULT.check((key && ckey == key) || can_bypass))
319 // If no key provided, or key is not the right one, and can't bypass +k (not invited or option not enabled)
320 user->WriteNumeric(ERR_BADCHANNELKEY, "%s %s :Cannot join channel (Incorrect channel key)",user->nick.c_str(), Ptr->name.c_str());
325 if (Ptr->IsModeSet('i'))
327 FIRST_MOD_RESULT(OnCheckInvite, MOD_RESULT, (user, Ptr));
328 if (!MOD_RESULT.check(invited))
330 user->WriteNumeric(ERR_INVITEONLYCHAN, "%s %s :Cannot join channel (Invite only)",user->nick.c_str(), Ptr->name.c_str());
335 std::string limit = Ptr->GetModeParameter('l');
338 FIRST_MOD_RESULT(OnCheckLimit, MOD_RESULT, (user, Ptr));
339 if (!MOD_RESULT.check((Ptr->GetUserCounter() < atol(limit.c_str()) || can_bypass)))
341 user->WriteNumeric(ERR_CHANNELISFULL, "%s %s :Cannot join channel (Channel is full)",user->nick.c_str(), Ptr->name.c_str());
346 if (Ptr->IsBanned(user) && !can_bypass)
348 user->WriteNumeric(ERR_BANNEDFROMCHAN, "%s %s :Cannot join channel (You're banned)",user->nick.c_str(), Ptr->name.c_str());
353 * If the user has invites for this channel, remove them now
354 * after a successful join so they don't build up.
358 IS_LOCAL(user)->RemoveInvite(Ptr->name.c_str());
364 if (created_by_local)
366 /* As spotted by jilles, dont bother to set this on remote users */
367 Ptr->SetDefaultModes();
370 return Channel::ForceChan(Ptr, user, privs, bursting, created_by_local);
373 Channel* Channel::ForceChan(Channel* Ptr, User* user, const std::string &privs, bool bursting, bool created)
375 std::string nick = user->nick;
377 Membership* memb = Ptr->AddUser(user);
378 user->chans.insert(Ptr);
380 for (std::string::const_iterator x = privs.begin(); x != privs.end(); x++)
382 const char status = *x;
383 ModeHandler* mh = ServerInstance->Modes->FindMode(status, MODETYPE_CHANNEL);
386 /* Set, and make sure that the mode handler knows this mode was now set */
387 Ptr->SetPrefix(user, mh->GetModeChar(), true);
388 mh->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, Ptr, nick, true);
393 FOREACH_MOD(I_OnUserJoin,OnUserJoin(memb, bursting, created, except_list));
395 Ptr->WriteAllExcept(user, false, 0, except_list, "JOIN :%s", Ptr->name.c_str());
397 /* Theyre not the first ones in here, make sure everyone else sees the modes we gave the user */
398 if ((Ptr->GetUserCounter() > 1) && (!memb->modes.empty()))
400 std::string ms = memb->modes;
401 for(unsigned int i=0; i < memb->modes.length(); i++)
402 ms.append(" ").append(user->nick);
404 except_list.insert(user);
405 Ptr->WriteAllExcept(user, !ServerInstance->Config->CycleHostsFromUser, 0, except_list, "MODE %s +%s", Ptr->name.c_str(), ms.c_str());
412 user->WriteNumeric(RPL_TOPIC, "%s %s :%s", user->nick.c_str(), Ptr->name.c_str(), Ptr->topic.c_str());
413 user->WriteNumeric(RPL_TOPICTIME, "%s %s %s %lu", user->nick.c_str(), Ptr->name.c_str(), Ptr->setby.c_str(), (unsigned long)Ptr->topicset);
417 FOREACH_MOD(I_OnPostJoin,OnPostJoin(memb));
421 bool Channel::IsBanned(User* user)
424 FIRST_MOD_RESULT(OnCheckChannelBan, result, (user, this));
426 if (result != MOD_RES_PASSTHRU)
427 return (result == MOD_RES_DENY);
429 for (BanList::iterator i = this->bans.begin(); i != this->bans.end(); i++)
431 if (CheckBan(user, i->data))
437 bool Channel::CheckBan(User* user, const std::string& mask)
440 FIRST_MOD_RESULT(OnCheckBan, result, (user, this, mask));
441 if (result != MOD_RES_PASSTHRU)
442 return (result == MOD_RES_DENY);
444 // extbans were handled above, if this is one it obviously didn't match
445 if ((mask.length() <= 2) || (mask[1] == ':'))
448 std::string::size_type at = mask.find('@');
449 if (at == std::string::npos)
452 char tomatch[MAXBUF];
453 snprintf(tomatch, MAXBUF, "%s!%s", user->nick.c_str(), user->ident.c_str());
454 std::string prefix = mask.substr(0, at);
455 if (InspIRCd::Match(tomatch, prefix, NULL))
457 std::string suffix = mask.substr(at + 1);
458 if (InspIRCd::Match(user->host, suffix, NULL) ||
459 InspIRCd::Match(user->dhost, suffix, NULL) ||
460 InspIRCd::MatchCIDR(user->GetIPString(), suffix, NULL))
466 ModResult Channel::GetExtBanStatus(User *user, char type)
469 FIRST_MOD_RESULT(OnExtBanCheck, rv, (user, this, type));
470 if (rv != MOD_RES_PASSTHRU)
472 for (BanList::iterator i = this->bans.begin(); i != this->bans.end(); i++)
474 if (i->data[0] == type && i->data[1] == ':')
476 std::string val = i->data.substr(2);
477 if (CheckBan(user, val))
481 return MOD_RES_PASSTHRU;
485 * remove a channel from a users record, and return the number of users left.
486 * Therefore, if this function returns 0 the caller should delete the Channel.
488 void Channel::PartUser(User *user, std::string &reason)
493 Membership* memb = GetUser(user);
498 FOREACH_MOD(I_OnUserPart,OnUserPart(memb, reason, except_list));
500 WriteAllExcept(user, false, 0, except_list, "PART %s%s%s", this->name.c_str(), reason.empty() ? "" : " :", reason.c_str());
502 user->chans.erase(this);
503 this->RemoveAllPrefixes(user);
509 void Channel::KickUser(User *src, User *user, const char* reason)
511 if (!src || !user || !reason)
514 Membership* memb = GetUser(user);
519 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());
522 if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
524 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());
529 if (ServerInstance->ULine(src->server))
532 FIRST_MOD_RESULT(OnUserPreKick, res, (src,memb,reason));
534 if (res == MOD_RES_DENY)
537 if (res == MOD_RES_PASSTHRU)
539 unsigned int them = this->GetPrefixValue(src);
540 unsigned int req = HALFOP_VALUE;
541 for (std::string::size_type i = 0; i < memb->modes.length(); i++)
543 ModeHandler* mh = ServerInstance->Modes->FindMode(memb->modes[i], MODETYPE_CHANNEL);
544 if (mh && mh->GetLevelRequired() > req)
545 req = mh->GetLevelRequired();
550 src->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s %s :You must be a channel %soperator",
551 src->nick.c_str(), this->name.c_str(), req > HALFOP_VALUE ? "" : "half-");
560 FOREACH_MOD(I_OnUserKick,OnUserKick(src, memb, reason, except_list));
562 WriteAllExcept(src, false, 0, except_list, "KICK %s %s :%s", name.c_str(), user->nick.c_str(), reason);
564 user->chans.erase(this);
565 this->RemoveAllPrefixes(user);
571 void Channel::WriteChannel(User* user, const char* text, ...)
573 char textbuffer[MAXBUF];
579 va_start(argsPtr, text);
580 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
583 this->WriteChannel(user, std::string(textbuffer));
586 void Channel::WriteChannel(User* user, const std::string &text)
593 snprintf(tb,MAXBUF,":%s %s", user->GetFullHost().c_str(), text.c_str());
594 std::string out = tb;
596 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
598 if (IS_LOCAL(i->first))
599 i->first->Write(out);
603 void Channel::WriteChannelWithServ(const std::string& ServName, const char* text, ...)
605 char textbuffer[MAXBUF];
611 va_start(argsPtr, text);
612 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
615 this->WriteChannelWithServ(ServName, std::string(textbuffer));
618 void Channel::WriteChannelWithServ(const std::string& ServName, const std::string &text)
622 snprintf(tb,MAXBUF,":%s %s", ServName.empty() ? ServerInstance->Config->ServerName.c_str() : ServName.c_str(), text.c_str());
623 std::string out = tb;
625 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
627 if (IS_LOCAL(i->first))
628 i->first->Write(out);
632 /* write formatted text from a source user to all users on a channel except
633 * for the sender (for privmsg etc) */
634 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const char* text, ...)
636 char textbuffer[MAXBUF];
642 va_start(argsPtr, text);
643 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
646 this->WriteAllExceptSender(user, serversource, status, std::string(textbuffer));
649 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const char* text, ...)
651 char textbuffer[MAXBUF];
657 int offset = snprintf(textbuffer,MAXBUF,":%s ", serversource ? ServerInstance->Config->ServerName.c_str() : user->GetFullHost().c_str());
659 va_start(argsPtr, text);
660 vsnprintf(textbuffer + offset, MAXBUF - offset, text, argsPtr);
663 this->RawWriteAllExcept(user, serversource, status, except_list, std::string(textbuffer));
666 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string &text)
670 snprintf(tb,MAXBUF,":%s %s", serversource ? ServerInstance->Config->ServerName.c_str() : user->GetFullHost().c_str(), text.c_str());
672 this->RawWriteAllExcept(user, serversource, status, except_list, std::string(tb));
675 void Channel::RawWriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string &out)
677 unsigned int minrank = 0;
680 ModeHandler* mh = ServerInstance->Modes->FindPrefix(status);
682 minrank = mh->GetPrefixRank();
684 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
686 if (IS_LOCAL(i->first) && (except_list.find(i->first) == except_list.end()))
688 /* User doesn't have the status we're after */
689 if (minrank && i->second->getRank() < minrank)
692 i->first->Write(out);
697 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const std::string& text)
700 except_list.insert(user);
701 this->WriteAllExcept(user, serversource, status, except_list, std::string(text));
705 * return a count of the users on a specific channel accounting for
706 * invisible users who won't increase the count. e.g. for /LIST
708 int Channel::CountInvisible()
711 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
713 if (!i->first->quitting && !i->first->IsModeSet('i'))
720 char* Channel::ChanModes(bool showkey)
722 static char scratch[MAXBUF];
723 static char sparam[MAXBUF];
724 char* offset = scratch;
725 std::string extparam;
730 /* This was still iterating up to 190, Channel::modes is only 64 elements -- Om */
731 for(int n = 0; n < 64; n++)
737 if (n == 'k' - 65 && !showkey)
743 extparam = this->GetModeParameter(n + 65);
745 if (!extparam.empty())
747 charlcat(sparam,' ',MAXBUF);
748 strlcat(sparam,extparam.c_str(),MAXBUF);
753 /* Null terminate scratch */
755 strlcat(scratch,sparam,MAXBUF);
759 /* compile a userlist of a channel into a string, each nick seperated by
760 * spaces and op, voice etc status shown as @ and +, and send it to 'user'
762 void Channel::UserList(User *user)
767 bool has_privs = user->HasPrivPermission("channels/auspex");
769 if (this->IsModeSet('s') && !this->HasUser(user) && !has_privs)
771 user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nick/channel",user->nick.c_str(), this->name.c_str());
775 std::string list = user->nick;
777 list.push_back(this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=');
779 list.append(this->name).append(" :");
780 std::string::size_type pos = list.size();
782 bool has_one = false;
784 /* Improvement by Brain - this doesnt change in value, so why was it inside
787 bool has_user = this->HasUser(user);
789 const size_t maxlen = MAXBUF - 10 - ServerInstance->Config->ServerName.size();
790 std::string prefixlist;
792 for (UserMembIter i = userlist.begin(); i != userlist.end(); ++i)
794 if (i->first->quitting)
796 if ((!has_user) && (i->first->IsModeSet('i')) && (!has_privs))
799 * user is +i, and source not on the channel, does not show
805 prefixlist = this->GetPrefixChar(i->first);
806 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 */
814 if (list.size() + prefixlist.length() + nick.length() + 1 > maxlen)
816 /* list overflowed into multiple numerics */
817 user->WriteNumeric(RPL_NAMREPLY, list);
819 // Erase all nicks, keep the constant part
824 list.append(prefixlist).append(nick).push_back(' ');
829 /* if whats left in the list isnt empty, send it */
832 user->WriteNumeric(RPL_NAMREPLY, list);
835 user->WriteNumeric(RPL_ENDOFNAMES, "%s %s :End of /NAMES list.", user->nick.c_str(), this->name.c_str());
838 long Channel::GetMaxBans()
840 /* Return the cached value if there is one */
842 return this->maxbans;
844 /* If there isnt one, we have to do some O(n) hax to find it the first time. (ick) */
845 for (std::map<std::string,int>::iterator n = ServerInstance->Config->maxbans.begin(); n != ServerInstance->Config->maxbans.end(); n++)
847 if (InspIRCd::Match(this->name, n->first, NULL))
849 this->maxbans = n->second;
854 /* Screw it, just return the default of 64 */
856 return this->maxbans;
859 void Channel::ResetMaxBans()
864 /* returns the status character for a given user on a channel, e.g. @ for op,
865 * % for halfop etc. If the user has several modes set, the highest mode
866 * the user has must be returned.
868 const char* Channel::GetPrefixChar(User *user)
870 static char pf[2] = {0, 0};
872 unsigned int bestrank = 0;
874 UserMembIter m = userlist.find(user);
875 if (m != userlist.end())
877 for(unsigned int i=0; i < m->second->modes.length(); i++)
879 char mchar = m->second->modes[i];
880 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
881 if (mh && mh->GetPrefixRank() > bestrank && mh->GetPrefix())
883 bestrank = mh->GetPrefixRank();
884 pf[0] = mh->GetPrefix();
891 unsigned int Membership::getRank()
893 char mchar = modes.c_str()[0];
897 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
899 rv = mh->GetPrefixRank();
904 const char* Channel::GetAllPrefixChars(User* user)
906 static char prefix[64];
909 UserMembIter m = userlist.find(user);
910 if (m != userlist.end())
912 for(unsigned int i=0; i < m->second->modes.length(); i++)
914 char mchar = m->second->modes[i];
915 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
916 if (mh && mh->GetPrefix())
917 prefix[ctr++] = mh->GetPrefix();
925 unsigned int Channel::GetPrefixValue(User* user)
927 UserMembIter m = userlist.find(user);
928 if (m == userlist.end())
930 return m->second->getRank();
933 bool Channel::SetPrefix(User* user, char prefix, bool adding)
935 ModeHandler* delta_mh = ServerInstance->Modes->FindMode(prefix, MODETYPE_CHANNEL);
938 UserMembIter m = userlist.find(user);
939 if (m == userlist.end())
941 for(unsigned int i=0; i < m->second->modes.length(); i++)
943 char mchar = m->second->modes[i];
944 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
945 if (mh && mh->GetPrefixRank() <= delta_mh->GetPrefixRank())
948 m->second->modes.substr(0,i) +
949 (adding ? std::string(1, prefix) : "") +
950 m->second->modes.substr(mchar == prefix ? i+1 : i);
951 return adding != (mchar == prefix);
955 m->second->modes += std::string(1, prefix);
959 void Channel::RemoveAllPrefixes(User* user)
961 UserMembIter m = userlist.find(user);
962 if (m != userlist.end())
964 m->second->modes.clear();
968 void Invitation::Create(Channel* c, LocalUser* u, time_t timeout)
970 if ((timeout != 0) && (ServerInstance->Time() >= timeout))
971 // Expired, don't bother
974 ServerInstance->Logs->Log("INVITATION", DEBUG, "Invitation::Create chan=%s user=%s", c->name.c_str(), u->uuid.c_str());
976 Invitation* inv = Invitation::Find(c, u, false);
979 if ((inv->expiry == 0) || (inv->expiry > timeout))
981 inv->expiry = timeout;
982 ServerInstance->Logs->Log("INVITATION", DEBUG, "Invitation::Create changed expiry in existing invitation %p", (void*) inv);
986 inv = new Invitation(c, u, timeout);
987 c->invites.push_back(inv);
988 u->invites.push_back(inv);
989 ServerInstance->Logs->Log("INVITATION", DEBUG, "Invitation::Create created new invitation %p", (void*) inv);
993 Invitation* Invitation::Find(Channel* c, LocalUser* u, bool check_expired)
995 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);
996 if (!u || u->invites.empty())
999 InviteList locallist;
1000 locallist.swap(u->invites);
1002 Invitation* result = NULL;
1003 for (InviteList::iterator i = locallist.begin(); i != locallist.end(); )
1005 Invitation* inv = *i;
1006 if ((check_expired) && (inv->expiry != 0) && (inv->expiry <= ServerInstance->Time()))
1008 /* Expired invite, remove it. */
1009 std::string expiration = ServerInstance->TimeString(inv->expiry);
1010 ServerInstance->Logs->Log("INVITATION", DEBUG, "Invitation::Find ecountered expired entry: %p expired %s", (void*) inv, expiration.c_str());
1011 i = locallist.erase(i);
1017 /* Is it what we're searching for? */
1027 locallist.swap(u->invites);
1028 ServerInstance->Logs->Log("INVITATION", DEBUG, "Invitation::Find result=%p", (void*) result);
1032 Invitation::~Invitation()
1034 // Remove this entry from both lists
1035 InviteList::iterator it = std::find(chan->invites.begin(), chan->invites.end(), this);
1036 if (it != chan->invites.end())
1037 chan->invites.erase(it);
1038 it = std::find(user->invites.begin(), user->invites.end(), this);
1039 if (it != user->invites.end())
1040 user->invites.erase(it);
1043 void InviteBase::ClearInvites()
1045 ServerInstance->Logs->Log("INVITEBASE", DEBUG, "InviteBase::ClearInvites %p", (void*) this);
1046 InviteList locallist;
1047 locallist.swap(invites);
1048 for (InviteList::const_iterator i = locallist.begin(); i != locallist.end(); ++i)