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 = new Membership(user, this);
150 userlist[user] = memb;
154 void Channel::DelUser(User* user)
156 UserMembIter a = userlist.find(user);
158 if (a != userlist.end())
165 if (userlist.empty())
168 FIRST_MOD_RESULT(OnChannelPreDelete, res, (this));
169 if (res == MOD_RES_DENY)
171 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
172 /* kill the record */
173 if (iter != ServerInstance->chanlist->end())
175 FOREACH_MOD(I_OnChannelDelete, OnChannelDelete(this));
176 ServerInstance->chanlist->erase(iter);
180 ServerInstance->GlobalCulls.AddItem(this);
184 bool Channel::HasUser(User* user)
186 return (userlist.find(user) != userlist.end());
189 Membership* Channel::GetUser(User* user)
191 UserMembIter i = userlist.find(user);
192 if (i == userlist.end())
197 const UserMembList* Channel::GetUsers()
202 void Channel::SetDefaultModes()
204 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "SetDefaultModes %s",
205 ServerInstance->Config->DefaultModes.c_str());
206 irc::spacesepstream list(ServerInstance->Config->DefaultModes);
208 std::string parameter;
210 list.GetToken(modeseq);
212 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
214 ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
217 if (mode->GetNumParams(true))
218 list.GetToken(parameter);
222 mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, this, parameter, true);
228 * add a channel to a user, creating the record for it if needed and linking
229 * it to the user record
231 Channel* Channel::JoinUser(User* user, std::string cname, bool override, const std::string& key, bool bursting, time_t TS)
233 // Fix: unregistered users could be joined using /SAJOIN
234 if (!user || user->registered != REG_ALL)
241 * We don't restrict the number of channels that remote users or users that are override-joining may be in.
242 * We restrict local users to MaxChans channels.
243 * We restrict local operators to OperMaxChans channels.
244 * This is a lot more logical than how it was formerly. -- w00t
246 if (IS_LOCAL(user) && !override)
248 if (user->HasPrivPermission("channels/high-join-limit"))
250 if (user->chans.size() >= ServerInstance->Config->OperMaxChans)
252 user->WriteNumeric(ERR_TOOMANYCHANNELS, "%s %s :You are on too many channels",user->nick.c_str(), cname.c_str());
258 unsigned int maxchans = user->GetClass()->maxchans;
260 maxchans = ServerInstance->Config->MaxChans;
261 if (user->chans.size() >= maxchans)
263 user->WriteNumeric(ERR_TOOMANYCHANNELS, "%s %s :You are on too many channels",user->nick.c_str(), cname.c_str());
269 if (cname.length() > ServerInstance->Config->Limits.ChanMax)
270 cname.resize(ServerInstance->Config->Limits.ChanMax);
272 Ptr = ServerInstance->FindChan(cname);
273 bool created_by_local = false;
278 * Fix: desync bug was here, don't set @ on remote users - spanningtree handles their permissions. bug #358. -- w00t
283 ServerInstance->Logs->Log("CHANNELS",LOG_DEBUG,"*** BUG *** Channel::JoinUser called for REMOTE user '%s' on channel '%s' but no TS given!", user->nick.c_str(), cname.c_str());
288 created_by_local = true;
291 if (IS_LOCAL(user) && override == false)
293 ModResult MOD_RESULT;
294 FIRST_MOD_RESULT(OnUserPreJoin, MOD_RESULT, (user, NULL, cname, privs, key));
295 if (MOD_RESULT == MOD_RES_DENY)
299 Ptr = new Channel(cname, TS);
303 /* Already on the channel */
304 if (Ptr->HasUser(user))
308 * remote users are allowed us to bypass channel modes
309 * and bans (used by servers)
311 if (IS_LOCAL(user) && override == false)
313 ModResult MOD_RESULT;
314 FIRST_MOD_RESULT(OnUserPreJoin, MOD_RESULT, (user, Ptr, cname, privs, key));
315 if (MOD_RESULT == MOD_RES_DENY)
319 else if (MOD_RESULT == MOD_RES_PASSTHRU)
321 std::string ckey = Ptr->GetModeParameter('k');
322 bool invited = IS_LOCAL(user)->IsInvited(Ptr->name.c_str());
323 bool can_bypass = ServerInstance->Config->InvBypassModes && invited;
327 FIRST_MOD_RESULT(OnCheckKey, MOD_RESULT, (user, Ptr, key));
328 if (!MOD_RESULT.check((ckey == key) || can_bypass))
330 // If no key provided, or key is not the right one, and can't bypass +k (not invited or option not enabled)
331 user->WriteNumeric(ERR_BADCHANNELKEY, "%s %s :Cannot join channel (Incorrect channel key)",user->nick.c_str(), Ptr->name.c_str());
336 if (Ptr->IsModeSet('i'))
338 FIRST_MOD_RESULT(OnCheckInvite, MOD_RESULT, (user, Ptr));
339 if (!MOD_RESULT.check(invited))
341 user->WriteNumeric(ERR_INVITEONLYCHAN, "%s %s :Cannot join channel (Invite only)",user->nick.c_str(), Ptr->name.c_str());
346 std::string limit = Ptr->GetModeParameter('l');
349 FIRST_MOD_RESULT(OnCheckLimit, MOD_RESULT, (user, Ptr));
350 if (!MOD_RESULT.check((Ptr->GetUserCounter() < atol(limit.c_str()) || can_bypass)))
352 user->WriteNumeric(ERR_CHANNELISFULL, "%s %s :Cannot join channel (Channel is full)",user->nick.c_str(), Ptr->name.c_str());
357 if (Ptr->IsBanned(user) && !can_bypass)
359 user->WriteNumeric(ERR_BANNEDFROMCHAN, "%s %s :Cannot join channel (You're banned)",user->nick.c_str(), Ptr->name.c_str());
364 * If the user has invites for this channel, remove them now
365 * after a successful join so they don't build up.
369 IS_LOCAL(user)->RemoveInvite(Ptr->name.c_str());
375 if (created_by_local)
377 /* As spotted by jilles, dont bother to set this on remote users */
378 Ptr->SetDefaultModes();
381 return Channel::ForceChan(Ptr, user, privs, bursting, created_by_local);
384 Channel* Channel::ForceChan(Channel* Ptr, User* user, const std::string &privs, bool bursting, bool created)
386 std::string nick = user->nick;
388 Membership* memb = Ptr->AddUser(user);
389 user->chans.insert(Ptr);
391 for (std::string::const_iterator x = privs.begin(); x != privs.end(); x++)
393 const char status = *x;
394 ModeHandler* mh = ServerInstance->Modes->FindMode(status, MODETYPE_CHANNEL);
397 /* Set, and make sure that the mode handler knows this mode was now set */
398 Ptr->SetPrefix(user, mh->GetModeChar(), true);
399 mh->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, Ptr, nick, true);
404 FOREACH_MOD(I_OnUserJoin,OnUserJoin(memb, bursting, created, except_list));
406 Ptr->WriteAllExcept(user, false, 0, except_list, "JOIN :%s", Ptr->name.c_str());
408 /* Theyre not the first ones in here, make sure everyone else sees the modes we gave the user */
409 if ((Ptr->GetUserCounter() > 1) && (!memb->modes.empty()))
411 std::string ms = memb->modes;
412 for(unsigned int i=0; i < memb->modes.length(); i++)
413 ms.append(" ").append(user->nick);
415 except_list.insert(user);
416 Ptr->WriteAllExcept(user, !ServerInstance->Config->CycleHostsFromUser, 0, except_list, "MODE %s +%s", Ptr->name.c_str(), ms.c_str());
423 user->WriteNumeric(RPL_TOPIC, "%s %s :%s", user->nick.c_str(), Ptr->name.c_str(), Ptr->topic.c_str());
424 user->WriteNumeric(RPL_TOPICTIME, "%s %s %s %lu", user->nick.c_str(), Ptr->name.c_str(), Ptr->setby.c_str(), (unsigned long)Ptr->topicset);
428 FOREACH_MOD(I_OnPostJoin,OnPostJoin(memb));
432 bool Channel::IsBanned(User* user)
435 FIRST_MOD_RESULT(OnCheckChannelBan, result, (user, this));
437 if (result != MOD_RES_PASSTHRU)
438 return (result == MOD_RES_DENY);
440 ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
441 const ListModeBase::ModeList* bans = banlm->GetList(this);
444 for (ListModeBase::ModeList::const_iterator it = bans->begin(); it != bans->end(); it++)
446 if (CheckBan(user, it->mask))
453 bool Channel::CheckBan(User* user, const std::string& mask)
456 FIRST_MOD_RESULT(OnCheckBan, result, (user, this, mask));
457 if (result != MOD_RES_PASSTHRU)
458 return (result == MOD_RES_DENY);
460 // extbans were handled above, if this is one it obviously didn't match
461 if ((mask.length() <= 2) || (mask[1] == ':'))
464 std::string::size_type at = mask.find('@');
465 if (at == std::string::npos)
468 char tomatch[MAXBUF];
469 snprintf(tomatch, MAXBUF, "%s!%s", user->nick.c_str(), user->ident.c_str());
470 std::string prefix = mask.substr(0, at);
471 if (InspIRCd::Match(tomatch, prefix, NULL))
473 std::string suffix = mask.substr(at + 1);
474 if (InspIRCd::Match(user->host, suffix, NULL) ||
475 InspIRCd::Match(user->dhost, suffix, NULL) ||
476 InspIRCd::MatchCIDR(user->GetIPString(), suffix, NULL))
482 ModResult Channel::GetExtBanStatus(User *user, char type)
485 FIRST_MOD_RESULT(OnExtBanCheck, rv, (user, this, type));
486 if (rv != MOD_RES_PASSTHRU)
489 ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
490 const ListModeBase::ModeList* bans = banlm->GetList(this);
494 for (ListModeBase::ModeList::const_iterator it = bans->begin(); it != bans->end(); ++it)
496 if (CheckBan(user, it->mask))
500 return MOD_RES_PASSTHRU;
504 * remove a channel from a users record, and return the number of users left.
505 * Therefore, if this function returns 0 the caller should delete the Channel.
507 void Channel::PartUser(User *user, std::string &reason)
512 Membership* memb = GetUser(user);
517 FOREACH_MOD(I_OnUserPart,OnUserPart(memb, reason, except_list));
519 WriteAllExcept(user, false, 0, except_list, "PART %s%s%s", this->name.c_str(), reason.empty() ? "" : " :", reason.c_str());
521 user->chans.erase(this);
522 this->RemoveAllPrefixes(user);
528 void Channel::KickUser(User *src, User *user, const char* reason)
530 if (!src || !user || !reason)
533 Membership* memb = GetUser(user);
538 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());
541 if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
543 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());
548 if (ServerInstance->ULine(src->server))
551 FIRST_MOD_RESULT(OnUserPreKick, res, (src,memb,reason));
553 if (res == MOD_RES_DENY)
556 if (res == MOD_RES_PASSTHRU)
558 unsigned int them = this->GetPrefixValue(src);
559 unsigned int req = HALFOP_VALUE;
560 for (std::string::size_type i = 0; i < memb->modes.length(); i++)
562 ModeHandler* mh = ServerInstance->Modes->FindMode(memb->modes[i], MODETYPE_CHANNEL);
563 if (mh && mh->GetLevelRequired() > req)
564 req = mh->GetLevelRequired();
569 src->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s %s :You must be a channel %soperator",
570 src->nick.c_str(), this->name.c_str(), req > HALFOP_VALUE ? "" : "half-");
579 FOREACH_MOD(I_OnUserKick,OnUserKick(src, memb, reason, except_list));
581 WriteAllExcept(src, false, 0, except_list, "KICK %s %s :%s", name.c_str(), user->nick.c_str(), reason);
583 user->chans.erase(this);
584 this->RemoveAllPrefixes(user);
590 void Channel::WriteChannel(User* user, const char* text, ...)
592 char textbuffer[MAXBUF];
598 va_start(argsPtr, text);
599 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
602 this->WriteChannel(user, std::string(textbuffer));
605 void Channel::WriteChannel(User* user, const std::string &text)
612 snprintf(tb,MAXBUF,":%s %s", user->GetFullHost().c_str(), text.c_str());
613 std::string out = tb;
615 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
617 if (IS_LOCAL(i->first))
618 i->first->Write(out);
622 void Channel::WriteChannelWithServ(const std::string& ServName, const char* text, ...)
624 char textbuffer[MAXBUF];
630 va_start(argsPtr, text);
631 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
634 this->WriteChannelWithServ(ServName, std::string(textbuffer));
637 void Channel::WriteChannelWithServ(const std::string& ServName, const std::string &text)
641 snprintf(tb,MAXBUF,":%s %s", ServName.empty() ? ServerInstance->Config->ServerName.c_str() : ServName.c_str(), text.c_str());
642 std::string out = tb;
644 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
646 if (IS_LOCAL(i->first))
647 i->first->Write(out);
651 /* write formatted text from a source user to all users on a channel except
652 * for the sender (for privmsg etc) */
653 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const char* text, ...)
655 char textbuffer[MAXBUF];
661 va_start(argsPtr, text);
662 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
665 this->WriteAllExceptSender(user, serversource, status, std::string(textbuffer));
668 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const char* text, ...)
670 char textbuffer[MAXBUF];
676 int offset = snprintf(textbuffer,MAXBUF,":%s ", serversource ? ServerInstance->Config->ServerName.c_str() : user->GetFullHost().c_str());
678 va_start(argsPtr, text);
679 vsnprintf(textbuffer + offset, MAXBUF - offset, text, argsPtr);
682 this->RawWriteAllExcept(user, serversource, status, except_list, std::string(textbuffer));
685 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string &text)
689 snprintf(tb,MAXBUF,":%s %s", serversource ? ServerInstance->Config->ServerName.c_str() : user->GetFullHost().c_str(), text.c_str());
690 std::string out = tb;
692 this->RawWriteAllExcept(user, serversource, status, except_list, std::string(tb));
695 void Channel::RawWriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string &out)
697 unsigned int minrank = 0;
700 ModeHandler* mh = ServerInstance->Modes->FindPrefix(status);
702 minrank = mh->GetPrefixRank();
704 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
706 if (IS_LOCAL(i->first) && (except_list.find(i->first) == except_list.end()))
708 /* User doesn't have the status we're after */
709 if (minrank && i->second->getRank() < minrank)
712 i->first->Write(out);
717 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const std::string& text)
720 except_list.insert(user);
721 this->WriteAllExcept(user, serversource, status, except_list, std::string(text));
725 * return a count of the users on a specific channel accounting for
726 * invisible users who won't increase the count. e.g. for /LIST
728 int Channel::CountInvisible()
731 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
733 if (!i->first->quitting && !i->first->IsModeSet('i'))
740 char* Channel::ChanModes(bool showkey)
742 static char scratch[MAXBUF];
743 static char sparam[MAXBUF];
744 char* offset = scratch;
745 std::string extparam;
750 /* This was still iterating up to 190, Channel::modes is only 64 elements -- Om */
751 for(int n = 0; n < 64; n++)
757 if (n == 'k' - 65 && !showkey)
763 extparam = this->GetModeParameter(n + 65);
765 if (!extparam.empty())
767 charlcat(sparam,' ',MAXBUF);
768 strlcat(sparam,extparam.c_str(),MAXBUF);
773 /* Null terminate scratch */
775 strlcat(scratch,sparam,MAXBUF);
779 /* compile a userlist of a channel into a string, each nick seperated by
780 * spaces and op, voice etc status shown as @ and +, and send it to 'user'
782 void Channel::UserList(User *user)
790 if (this->IsModeSet('s') && !this->HasUser(user) && !user->HasPrivPermission("channels/auspex"))
792 user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nick/channel",user->nick.c_str(), this->name.c_str());
796 dlen = curlen = snprintf(list,MAXBUF,"%s %c %s :", user->nick.c_str(), this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=', this->name.c_str());
799 char* ptr = list + dlen;
801 /* Improvement by Brain - this doesnt change in value, so why was it inside
804 bool has_user = this->HasUser(user);
806 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
808 if (i->first->quitting)
810 if ((!has_user) && (i->first->IsModeSet('i')))
813 * user is +i, and source not on the channel, does not show
819 std::string prefixlist = this->GetPrefixChar(i->first);
820 std::string nick = i->first->nick;
822 FOREACH_MOD(I_OnNamesListItem, OnNamesListItem(user, i->second, prefixlist, nick));
824 /* Nick was nuked, a module wants us to skip it */
830 if (curlen + prefixlist.length() + nick.length() + 1 > 480)
832 /* list overflowed into multiple numerics */
833 user->WriteNumeric(RPL_NAMREPLY, std::string(list));
835 /* reset our lengths */
836 dlen = curlen = snprintf(list,MAXBUF,"%s %c %s :", user->nick.c_str(), this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=', this->name.c_str());
842 ptrlen = snprintf(ptr, MAXBUF, "%s%s ", prefixlist.c_str(), nick.c_str());
850 /* if whats left in the list isnt empty, send it */
853 user->WriteNumeric(RPL_NAMREPLY, std::string(list));
856 user->WriteNumeric(RPL_ENDOFNAMES, "%s %s :End of /NAMES list.", user->nick.c_str(), this->name.c_str());
859 /* returns the status character for a given user on a channel, e.g. @ for op,
860 * % for halfop etc. If the user has several modes set, the highest mode
861 * the user has must be returned.
863 const char* Channel::GetPrefixChar(User *user)
865 static char pf[2] = {0, 0};
867 unsigned int bestrank = 0;
869 UserMembIter m = userlist.find(user);
870 if (m != userlist.end())
872 for(unsigned int i=0; i < m->second->modes.length(); i++)
874 char mchar = m->second->modes[i];
875 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
876 if (mh && mh->GetPrefixRank() > bestrank && mh->GetPrefix())
878 bestrank = mh->GetPrefixRank();
879 pf[0] = mh->GetPrefix();
886 unsigned int Membership::getRank()
888 char mchar = modes.c_str()[0];
892 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
894 rv = mh->GetPrefixRank();
899 const char* Channel::GetAllPrefixChars(User* user)
901 static char prefix[64];
904 UserMembIter m = userlist.find(user);
905 if (m != userlist.end())
907 for(unsigned int i=0; i < m->second->modes.length(); i++)
909 char mchar = m->second->modes[i];
910 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
911 if (mh && mh->GetPrefix())
912 prefix[ctr++] = mh->GetPrefix();
920 unsigned int Channel::GetPrefixValue(User* user)
922 UserMembIter m = userlist.find(user);
923 if (m == userlist.end())
925 return m->second->getRank();
928 bool Channel::SetPrefix(User* user, char prefix, bool adding)
930 ModeHandler* delta_mh = ServerInstance->Modes->FindMode(prefix, MODETYPE_CHANNEL);
933 UserMembIter m = userlist.find(user);
934 if (m == userlist.end())
936 for(unsigned int i=0; i < m->second->modes.length(); i++)
938 char mchar = m->second->modes[i];
939 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
940 if (mh && mh->GetPrefixRank() <= delta_mh->GetPrefixRank())
943 m->second->modes.substr(0,i) +
944 (adding ? std::string(1, prefix) : "") +
945 m->second->modes.substr(mchar == prefix ? i+1 : i);
946 return adding != (mchar == prefix);
950 m->second->modes += std::string(1, prefix);
954 void Channel::RemoveAllPrefixes(User* user)
956 UserMembIter m = userlist.find(user);
957 if (m != userlist.end())
959 m->second->modes.clear();
963 void Invitation::Create(Channel* c, LocalUser* u, time_t timeout)
965 if ((timeout != 0) && (ServerInstance->Time() >= timeout))
966 // Expired, don't bother
969 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Create chan=%s user=%s", c->name.c_str(), u->uuid.c_str());
971 Invitation* inv = Invitation::Find(c, u, false);
974 if ((inv->expiry == 0) || (inv->expiry > timeout))
976 inv->expiry = timeout;
977 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Create changed expiry in existing invitation %p", (void*) inv);
981 inv = new Invitation(c, u, timeout);
982 c->invites.push_back(inv);
983 u->invites.push_back(inv);
984 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Create created new invitation %p", (void*) inv);
988 Invitation* Invitation::Find(Channel* c, LocalUser* u, bool check_expired)
990 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);
991 if (!u || u->invites.empty())
994 InviteList locallist;
995 locallist.swap(u->invites);
997 Invitation* result = NULL;
998 for (InviteList::iterator i = locallist.begin(); i != locallist.end(); )
1000 Invitation* inv = *i;
1001 if ((check_expired) && (inv->expiry != 0) && (inv->expiry <= ServerInstance->Time()))
1003 /* Expired invite, remove it. */
1004 std::string expiration = ServerInstance->TimeString(inv->expiry);
1005 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Find ecountered expired entry: %p expired %s", (void*) inv, expiration.c_str());
1006 i = locallist.erase(i);
1012 /* Is it what we're searching for? */
1022 locallist.swap(u->invites);
1023 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Find result=%p", (void*) result);
1027 Invitation::~Invitation()
1029 // Remove this entry from both lists
1030 InviteList::iterator it = std::find(chan->invites.begin(), chan->invites.end(), this);
1031 if (it != chan->invites.end())
1032 chan->invites.erase(it);
1033 it = std::find(user->invites.begin(), user->invites.end(), this);
1034 if (it != user->invites.end())
1035 user->invites.erase(it);
1038 void InviteBase::ClearInvites()
1040 ServerInstance->Logs->Log("INVITEBASE", LOG_DEBUG, "InviteBase::ClearInvites %p", (void*) this);
1041 InviteList locallist;
1042 locallist.swap(invites);
1043 for (InviteList::const_iterator i = locallist.begin(); i != locallist.end(); ++i)