-/* +------------------------------------+\r * | Inspire Internet Relay Chat Daemon |\r * +------------------------------------+\r *\r * InspIRCd: (C) 2002-2007 InspIRCd Development Team\r * See: http://www.inspircd.org/wiki/index.php/Credits\r *\r * This program is free but copyrighted software; see\r * the file COPYING for details.\r *\r * ---------------------------------------------------\r */\r\r#include "inspircd.h"\r#include <stdarg.h>\r#include "configreader.h"\r#include "users.h"\r#include "modules.h"\r#include "wildcard.h"\r#include "mode.h"\r\rchanrec::chanrec(InspIRCd* Instance) : ServerInstance(Instance)\r{\r *name = *topic = *setby = *key = 0;\r maxbans = created = topicset = limit = 0;\r memset(&modes,0,64);\r age = ServerInstance->Time(true);\r}\r\rvoid chanrec::SetMode(char mode,bool mode_on)\r{\r modes[mode-65] = mode_on;\r if (!mode_on)\r this->SetModeParam(mode,"",false);\r}\r\r\rvoid chanrec::SetModeParam(char mode,const char* parameter,bool mode_on)\r{\r CustomModeList::iterator n = custom_mode_params.find(mode); \r\r if (mode_on)\r {\r if (n == custom_mode_params.end())\r custom_mode_params[mode] = strdup(parameter);\r }\r else\r {\r if (n != custom_mode_params.end())\r {\r free(n->second);\r custom_mode_params.erase(n);\r }\r }\r}\r\rbool chanrec::IsModeSet(char mode)\r{\r return modes[mode-65];\r}\r\rstd::string chanrec::GetModeParameter(char mode)\r{\r switch (mode)\r {\r case 'k':\r return this->key;\r case 'l':\r return ConvToStr(this->limit);\r default:\r CustomModeList::iterator n = custom_mode_params.find(mode);\r if (n != custom_mode_params.end())\r return n->second;\r return "";\r break;\r }\r}\r\rlong chanrec::GetUserCounter()\r{\r return (this->internal_userlist.size());\r}\r\rvoid chanrec::AddUser(userrec* user)\r{\r internal_userlist[user] = user->nick;\r}\r\runsigned long chanrec::DelUser(userrec* user)\r{\r CUListIter a = internal_userlist.find(user);\r \r if (a != internal_userlist.end())\r {\r internal_userlist.erase(a);\r /* And tidy any others... */\r DelOppedUser(user);\r DelHalfoppedUser(user);\r DelVoicedUser(user);\r }\r \r return internal_userlist.size();\r}\r\rbool chanrec::HasUser(userrec* user)\r{\r return (internal_userlist.find(user) != internal_userlist.end());\r}\r\rvoid chanrec::AddOppedUser(userrec* user)\r{\r internal_op_userlist[user] = user->nick;\r}\r\rvoid chanrec::DelOppedUser(userrec* user)\r{\r CUListIter a = internal_op_userlist.find(user);\r if (a != internal_op_userlist.end())\r {\r internal_op_userlist.erase(a);\r return;\r }\r}\r\rvoid chanrec::AddHalfoppedUser(userrec* user)\r{\r internal_halfop_userlist[user] = user->nick;\r}\r\rvoid chanrec::DelHalfoppedUser(userrec* user)\r{\r CUListIter a = internal_halfop_userlist.find(user);\r\r if (a != internal_halfop_userlist.end())\r { \r internal_halfop_userlist.erase(a);\r }\r}\r\rvoid chanrec::AddVoicedUser(userrec* user)\r{\r internal_voice_userlist[user] = user->nick;\r}\r\rvoid chanrec::DelVoicedUser(userrec* user)\r{\r CUListIter a = internal_voice_userlist.find(user);\r \r if (a != internal_voice_userlist.end())\r {\r internal_voice_userlist.erase(a);\r }\r}\r\rCUList* chanrec::GetUsers()\r{\r return &internal_userlist;\r}\r\rCUList* chanrec::GetOppedUsers()\r{\r return &internal_op_userlist;\r}\r\rCUList* chanrec::GetHalfoppedUsers()\r{\r return &internal_halfop_userlist;\r}\r\rCUList* chanrec::GetVoicedUsers()\r{\r return &internal_voice_userlist;\r}\r\rvoid chanrec::SetDefaultModes()\r{\r irc::spacesepstream list(ServerInstance->Config->DefaultModes);\r std::string modeseq = list.GetToken();\r std::string parameter;\r userrec* dummyuser = new userrec(ServerInstance);\r dummyuser->SetFd(FD_MAGIC_NUMBER);\r\r for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)\r {\r ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);\r if (mode)\r {\r if (mode->GetNumParams(true))\r parameter = list.GetToken().c_str();\r else\r parameter.clear();\r\r mode->OnModeChange(dummyuser, dummyuser, this, parameter, true);\r }\r }\r\r delete dummyuser;\r}\r\r/* \r * add a channel to a user, creating the record for it if needed and linking\r * it to the user record \r */\rchanrec* chanrec::JoinUser(InspIRCd* Instance, userrec *user, const char* cn, bool override, const char* key, time_t TS)\r{\r if (!user || !cn)\r return NULL;\r\r bool new_channel = false;\r char cname[MAXBUF];\r int MOD_RESULT = 0;\r strlcpy(cname,cn,CHANMAX);\r\r std::string privs;\r\r chanrec* Ptr = Instance->FindChan(cname);\r\r if (!Ptr)\r {\r if ((!IS_LOCAL(user)) && (!TS))\r Instance->Log(DEBUG,"*** BUG *** chanrec::JoinUser called for REMOTE user '%s' on channel '%s' but no TS given!", user->nick, cn);\r\r privs = "@";\r\r if (IS_LOCAL(user) && override == false)\r {\r MOD_RESULT = 0;\r FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,NULL,cname,privs));\r if (MOD_RESULT == 1)\r return NULL;\r }\r\r /* create a new one */\r Ptr = new chanrec(Instance);\r (*(Instance->chanlist))[cname] = Ptr;\r\r strlcpy(Ptr->name, cname,CHANMAX);\r\r /* As spotted by jilles, dont bother to set this on remote users */\r if (IS_LOCAL(user))\r Ptr->SetDefaultModes();\r\r Ptr->created = TS ? TS : Instance->Time();\r Ptr->age = Ptr->created;\r *Ptr->topic = 0;\r *Ptr->setby = 0;\r Ptr->topicset = 0;\r new_channel = true;\r }\r else\r {\r /* Already on the channel */\r if (Ptr->HasUser(user))\r return NULL;\r\r /*\r * remote users are allowed us to bypass channel modes\r * and bans (used by servers)\r */\r if (IS_LOCAL(user) && override == false)\r {\r MOD_RESULT = 0;\r FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname,privs));\r if (MOD_RESULT == 1)\r {\r return NULL;\r }\r else if (MOD_RESULT == 0)\r {\r if (*Ptr->key)\r {\r MOD_RESULT = 0;\r FOREACH_RESULT_I(Instance,I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));\r if (!MOD_RESULT)\r {\r if ((!key) || strcmp(key,Ptr->key))\r {\r user->WriteServ("475 %s %s :Cannot join channel (Incorrect channel key)",user->nick, Ptr->name);\r return NULL;\r }\r }\r }\r if (Ptr->modes[CM_INVITEONLY])\r {\r MOD_RESULT = 0;\r FOREACH_RESULT_I(Instance,I_OnCheckInvite,OnCheckInvite(user, Ptr));\r if (!MOD_RESULT)\r {\r if (user->IsInvited(Ptr->name))\r {\r /* user was invited to channel */\r /* there may be an optional channel NOTICE here */\r }\r else\r {\r user->WriteServ("473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);\r return NULL;\r }\r }\r user->RemoveInvite(Ptr->name);\r }\r if (Ptr->limit)\r {\r MOD_RESULT = 0;\r FOREACH_RESULT_I(Instance,I_OnCheckLimit,OnCheckLimit(user, Ptr));\r if (!MOD_RESULT)\r {\r if (Ptr->GetUserCounter() >= Ptr->limit)\r {\r user->WriteServ("471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);\r return NULL;\r }\r }\r }\r if (Ptr->bans.size())\r {\r if (Ptr->IsBanned(user))\r {\r user->WriteServ("474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);\r return NULL;\r }\r }\r }\r }\r }\r\r /* NOTE: If the user is an oper here, we can extend their user->chans by up to\r * OperMaxchans. For remote users which are not bound by the channel limits,\r * we can extend infinitely. Otherwise, nope, youre restricted to MaxChans.\r */\r if (!IS_LOCAL(user) || override == true)\r {\r return chanrec::ForceChan(Instance, Ptr, user, privs);\r }\r else if (IS_OPER(user))\r {\r /* Oper allows extension up to the OperMaxchans value */\r if (user->chans.size() < Instance->Config->OperMaxChans)\r {\r return chanrec::ForceChan(Instance, Ptr, user, privs);\r }\r }\r else if (user->chans.size() < Instance->Config->MaxChans)\r {\r return chanrec::ForceChan(Instance, Ptr, user, privs);\r }\r\r\r user->WriteServ("405 %s %s :You are on too many channels",user->nick, cname);\r\r if (new_channel)\r {\r /* Things went seriously pear shaped, so take this away. bwahaha. */\r chan_hash::iterator n = Instance->chanlist->find(cname);\r if (n != Instance->chanlist->end())\r {\r Ptr->DelUser(user);\r DELETE(Ptr);\r Instance->chanlist->erase(n);\r }\r }\r\r return NULL;\r}\r\rchanrec* chanrec::ForceChan(InspIRCd* Instance, chanrec* Ptr, userrec* user, const std::string &privs)\r{\r userrec* dummyuser = new userrec(Instance);\r std::string nick = user->nick;\r bool silent = false;\r\r dummyuser->SetFd(FD_MAGIC_NUMBER);\r Ptr->AddUser(user);\r\r /* Just in case they have no permissions */\r user->chans[Ptr] = 0;\r\r for (std::string::const_iterator x = privs.begin(); x != privs.end(); x++)\r {\r const char status = *x;\r ModeHandler* mh = Instance->Modes->FindPrefix(status);\r if (mh)\r {\r Ptr->SetPrefix(user, status, mh->GetPrefixRank(), true);\r /* Make sure that the mode handler knows this mode was now set */\r mh->OnModeChange(dummyuser, dummyuser, Ptr, nick, true);\r\r switch (mh->GetPrefix())\r {\r /* These logic ops are SAFE IN THIS CASE\r * because if the entry doesnt exist,\r * addressing operator[] creates it.\r * If they do exist, it points to it.\r * At all other times where we dont want\r * to create an item if it doesnt exist, we\r * must stick to ::find().\r */\r case '@':\r user->chans[Ptr] |= UCMODE_OP;\r break;\r case '%':\r user->chans[Ptr] |= UCMODE_HOP;\r break;\r case '+':\r user->chans[Ptr] |= UCMODE_VOICE;\r break;\r }\r }\r }\r\r delete dummyuser;\r\r FOREACH_MOD_I(Instance,I_OnUserJoin,OnUserJoin(user, Ptr, silent));\r\r if (!silent)\r Ptr->WriteChannel(user,"JOIN :%s",Ptr->name);\r\r /* Theyre not the first ones in here, make sure everyone else sees the modes we gave the user */\r std::string ms = Instance->Modes->ModeString(user, Ptr);\r if ((Ptr->GetUserCounter() > 1) && (ms.length()))\r Ptr->WriteAllExceptSender(user, true, 0, "MODE %s +%s", Ptr->name, ms.c_str());\r\r /* Major improvement by Brain - we dont need to be calculating all this pointlessly for remote users */\r if (IS_LOCAL(user))\r {\r if (Ptr->topicset)\r {\r user->WriteServ("332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);\r user->WriteServ("333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);\r }\r Ptr->UserList(user);\r }\r FOREACH_MOD_I(Instance,I_OnPostJoin,OnPostJoin(user, Ptr));\r return Ptr;\r}\r\rbool chanrec::IsBanned(userrec* user)\r{\r char mask[MAXBUF];\r int MOD_RESULT = 0;\r FOREACH_RESULT(I_OnCheckBan,OnCheckBan(user, this));\r if (!MOD_RESULT)\r {\r snprintf(mask, MAXBUF, "%s!%s@%s", user->nick, user->ident, user->GetIPString());\r for (BanList::iterator i = this->bans.begin(); i != this->bans.end(); i++)\r {\r /* This allows CIDR ban matching\r * \r * Full masked host Full unmasked host IP with/without CIDR\r */\r if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match(mask, i->data, true)))\r {\r return true;\r }\r }\r }\r return false;\r}\r\r/* chanrec::PartUser\r * remove a channel from a users record, and return the number of users left.\r * Therefore, if this function returns 0 the caller should delete the chanrec.\r */\rlong chanrec::PartUser(userrec *user, const char* reason)\r{\r bool silent = false;\r\r if (!user)\r return this->GetUserCounter();\r\r UCListIter i = user->chans.find(this);\r if (i != user->chans.end())\r {\r FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, reason ? reason : "", silent));\r\r if (!silent)\r this->WriteChannel(user, "PART %s%s%s", this->name, reason ? " :" : "", reason ? reason : "");\r\r user->chans.erase(i);\r this->RemoveAllPrefixes(user);\r }\r\r if (!this->DelUser(user)) /* if there are no users left on the channel... */\r {\r chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);\r /* kill the record */\r if (iter != ServerInstance->chanlist->end())\r {\r FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));\r ServerInstance->chanlist->erase(iter);\r }\r return 0;\r }\r\r return this->GetUserCounter();\r}\r\rlong chanrec::ServerKickUser(userrec* user, const char* reason, bool triggerevents)\r{\r bool silent = false;\r\r if (!user || !reason)\r return this->GetUserCounter();\r\r if (IS_LOCAL(user))\r {\r if (!this->HasUser(user))\r {\r /* Not on channel */\r return this->GetUserCounter();\r }\r }\r\r if (triggerevents)\r {\r FOREACH_MOD(I_OnUserKick,OnUserKick(NULL, user, this, reason, silent));\r }\r\r UCListIter i = user->chans.find(this);\r if (i != user->chans.end())\r {\r if (!silent)\r this->WriteChannelWithServ(ServerInstance->Config->ServerName, "KICK %s %s :%s", this->name, user->nick, reason);\r\r user->chans.erase(i);\r this->RemoveAllPrefixes(user);\r }\r\r if (!this->DelUser(user))\r {\r chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);\r /* kill the record */\r if (iter != ServerInstance->chanlist->end())\r {\r FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));\r ServerInstance->chanlist->erase(iter);\r }\r return 0;\r }\r\r return this->GetUserCounter();\r}\r\rlong chanrec::KickUser(userrec *src, userrec *user, const char* reason)\r{\r bool silent = false;\r\r if (!src || !user || !reason)\r return this->GetUserCounter();\r\r if (IS_LOCAL(src))\r {\r if (!this->HasUser(user))\r {\r src->WriteServ("441 %s %s %s :They are not on that channel",src->nick, user->nick, this->name);\r return this->GetUserCounter();\r }\r if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))\r {\r src->WriteServ("482 %s %s :Only a u-line may kick a u-line from a channel.",src->nick, this->name);\r return this->GetUserCounter();\r }\r int MOD_RESULT = 0;\r\r if (!ServerInstance->ULine(src->server))\r {\r MOD_RESULT = 0;\r FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,this,reason));\r if (MOD_RESULT == 1)\r return this->GetUserCounter();\r }\r /* Set to -1 by OnUserPreKick if explicit allow was set */\r if (MOD_RESULT != -1)\r {\r FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,this,AC_KICK));\r if ((MOD_RESULT == ACR_DENY) && (!ServerInstance->ULine(src->server)))\r return this->GetUserCounter();\r \r if ((MOD_RESULT == ACR_DEFAULT) || (!ServerInstance->ULine(src->server)))\r {\r int them = this->GetStatus(src);\r int us = this->GetStatus(user);\r if ((them < STATUS_HOP) || (them < us))\r {\r src->WriteServ("482 %s %s :You must be a channel %soperator",src->nick, this->name, them == STATUS_HOP ? "" : "half-");\r return this->GetUserCounter();\r }\r }\r }\r }\r\r FOREACH_MOD(I_OnUserKick,OnUserKick(src, user, this, reason, silent));\r\r UCListIter i = user->chans.find(this);\r if (i != user->chans.end())\r {\r /* zap it from the channel list of the user */\r if (!silent)\r this->WriteChannel(src, "KICK %s %s :%s", this->name, user->nick, reason);\r\r user->chans.erase(i);\r this->RemoveAllPrefixes(user);\r }\r\r if (!this->DelUser(user))\r /* if there are no users left on the channel */\r {\r chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);\r\r /* kill the record */\r if (iter != ServerInstance->chanlist->end())\r {\r FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));\r ServerInstance->chanlist->erase(iter);\r }\r return 0;\r }\r\r return this->GetUserCounter();\r}\r\rvoid chanrec::WriteChannel(userrec* user, char* text, ...)\r{\r char textbuffer[MAXBUF];\r va_list argsPtr;\r\r if (!user || !text)\r return;\r\r va_start(argsPtr, text);\r vsnprintf(textbuffer, MAXBUF, text, argsPtr);\r va_end(argsPtr);\r\r this->WriteChannel(user, std::string(textbuffer));\r}\r\rvoid chanrec::WriteChannel(userrec* user, const std::string &text)\r{\r CUList *ulist = this->GetUsers();\r char tb[MAXBUF];\r\r if (!user)\r return;\r\r snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());\r std::string out = tb;\r\r for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)\r {\r if (IS_LOCAL(i->first))\r i->first->Write(out);\r }\r}\r\rvoid chanrec::WriteChannelWithServ(const char* ServName, const char* text, ...)\r{\r char textbuffer[MAXBUF];\r va_list argsPtr;\r\r if (!text)\r return;\r\r va_start(argsPtr, text);\r vsnprintf(textbuffer, MAXBUF, text, argsPtr);\r va_end(argsPtr);\r\r this->WriteChannelWithServ(ServName, std::string(textbuffer));\r}\r\rvoid chanrec::WriteChannelWithServ(const char* ServName, const std::string &text)\r{\r CUList *ulist = this->GetUsers();\r char tb[MAXBUF];\r\r snprintf(tb,MAXBUF,":%s %s",ServName ? ServName : ServerInstance->Config->ServerName, text.c_str());\r std::string out = tb;\r\r for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)\r {\r if (IS_LOCAL(i->first))\r i->first->Write(out);\r }\r}\r\r/* write formatted text from a source user to all users on a channel except\r * for the sender (for privmsg etc) */\rvoid chanrec::WriteAllExceptSender(userrec* user, bool serversource, char status, char* text, ...)\r{\r char textbuffer[MAXBUF];\r va_list argsPtr;\r\r if (!text)\r return;\r\r va_start(argsPtr, text);\r vsnprintf(textbuffer, MAXBUF, text, argsPtr);\r va_end(argsPtr);\r\r this->WriteAllExceptSender(user, serversource, status, std::string(textbuffer));\r}\r\rvoid chanrec::WriteAllExcept(userrec* user, bool serversource, char status, CUList &except_list, char* text, ...)\r{\r char textbuffer[MAXBUF];\r va_list argsPtr;\r\r if (!text)\r return;\r\r va_start(argsPtr, text);\r vsnprintf(textbuffer, MAXBUF, text, argsPtr);\r va_end(argsPtr);\r\r this->WriteAllExcept(user, serversource, status, except_list, std::string(textbuffer));\r}\r\rvoid chanrec::WriteAllExcept(userrec* user, bool serversource, char status, CUList &except_list, const std::string &text)\r{\r CUList *ulist;\r char tb[MAXBUF];\r\r switch (status)\r {\r case '@':\r ulist = this->GetOppedUsers();\r break;\r case '%':\r ulist = this->GetHalfoppedUsers();\r break;\r case '+':\r ulist = this->GetVoicedUsers();\r break;\r default:\r ulist = this->GetUsers();\r break;\r }\r\r snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());\r std::string out = tb;\r\r for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)\r {\r if ((IS_LOCAL(i->first)) && (except_list.find(i->first) == except_list.end()))\r {\r if (serversource)\r i->first->WriteServ(text);\r else\r i->first->Write(out);\r }\r }\r}\r\rvoid chanrec::WriteAllExceptSender(userrec* user, bool serversource, char status, const std::string& text)\r{\r CUList except_list;\r except_list[user] = user->nick;\r this->WriteAllExcept(user, serversource, status, except_list, std::string(text));\r}\r\r/*\r * return a count of the users on a specific channel accounting for\r * invisible users who won't increase the count. e.g. for /LIST\r */\rint chanrec::CountInvisible()\r{\r int count = 0;\r CUList *ulist= this->GetUsers();\r for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)\r {\r if (!(i->first->IsModeSet('i')))\r count++;\r }\r\r return count;\r}\r\rchar* chanrec::ChanModes(bool showkey)\r{\r static char scratch[MAXBUF];\r static char sparam[MAXBUF];\r char* offset = scratch;\r std::string extparam;\r\r *scratch = '\0';\r *sparam = '\0';\r\r /* This was still iterating up to 190, chanrec::modes is only 64 elements -- Om */\r for(int n = 0; n < 64; n++)\r {\r if(this->modes[n])\r {\r *offset++ = n + 65;\r extparam.clear();\r switch (n)\r {\r case CM_KEY:\r extparam = (showkey ? this->key : "<key>");\r break;\r case CM_LIMIT:\r extparam = ConvToStr(this->limit);\r break;\r case CM_NOEXTERNAL:\r case CM_TOPICLOCK:\r case CM_INVITEONLY:\r case CM_MODERATED:\r case CM_SECRET:\r case CM_PRIVATE:\r /* We know these have no parameters */\r break;\r default:\r extparam = this->GetModeParameter(n + 65);\r break;\r }\r if (!extparam.empty())\r {\r charlcat(sparam,' ',MAXBUF);\r strlcat(sparam,extparam.c_str(),MAXBUF);\r }\r }\r }\r\r /* Null terminate scratch */\r *offset = '\0';\r strlcat(scratch,sparam,MAXBUF);\r return scratch;\r}\r\r/* compile a userlist of a channel into a string, each nick seperated by\r * spaces and op, voice etc status shown as @ and +, and send it to 'user'\r */\rvoid chanrec::UserList(userrec *user, CUList *ulist)\r{\r char list[MAXBUF];\r size_t dlen, curlen;\r int MOD_RESULT = 0;\r\r if (!IS_LOCAL(user))\r return;\r\r FOREACH_RESULT(I_OnUserList,OnUserList(user, this, ulist));\r if (MOD_RESULT == 1)\r return;\r\r dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);\r\r int numusers = 0;\r char* ptr = list + dlen;\r\r if (!ulist)\r ulist = this->GetUsers();\r\r /* Improvement by Brain - this doesnt change in value, so why was it inside\r * the loop?\r */\r bool has_user = this->HasUser(user);\r\r for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)\r {\r if ((!has_user) && (i->first->modes[UM_INVISIBLE]))\r {\r /*\r * user is +i, and source not on the channel, does not show\r * nick in NAMES list\r */\r continue;\r }\r\r if (i->first->Visibility && !i->first->Visibility->VisibleTo(user))\r continue;\r\r size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", this->GetPrefixChar(i->first), i->second.c_str());\r /* OnUserList can change this - reset it back to normal */\r i->second = i->first->nick;\r\r curlen += ptrlen;\r ptr += ptrlen;\r\r numusers++;\r\r if (curlen > (480-NICKMAX))\r {\r /* list overflowed into multiple numerics */\r user->WriteServ(std::string(list));\r\r /* reset our lengths */\r dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);\r ptr = list + dlen;\r\r ptrlen = 0;\r numusers = 0;\r }\r }\r\r /* if whats left in the list isnt empty, send it */\r if (numusers)\r {\r user->WriteServ(std::string(list));\r }\r\r user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, this->name);\r}\r\rlong chanrec::GetMaxBans()\r{\r /* Return the cached value if there is one */\r if (this->maxbans)\r return this->maxbans;\r\r /* If there isnt one, we have to do some O(n) hax to find it the first time. (ick) */\r for (std::map<std::string,int>::iterator n = ServerInstance->Config->maxbans.begin(); n != ServerInstance->Config->maxbans.end(); n++)\r {\r if (match(this->name,n->first.c_str()))\r {\r this->maxbans = n->second;\r return n->second;\r }\r }\r\r /* Screw it, just return the default of 64 */\r this->maxbans = 64;\r return this->maxbans;\r}\r\rvoid chanrec::ResetMaxBans()\r{\r this->maxbans = 0;\r}\r\r/* returns the status character for a given user on a channel, e.g. @ for op,\r * % for halfop etc. If the user has several modes set, the highest mode\r * the user has must be returned.\r */\rconst char* chanrec::GetPrefixChar(userrec *user)\r{\r static char pf[2] = {0, 0};\r \r prefixlist::iterator n = prefixes.find(user);\r if (n != prefixes.end())\r {\r if (n->second.size())\r {\r /* If the user has any prefixes, their highest prefix\r * will always be at the head of the list, as the list is\r * sorted in rank order highest first (see SetPrefix()\r * for reasons why)\r */\r *pf = n->second.begin()->first;\r return pf;\r }\r }\r\r *pf = 0;\r return pf;\r}\r\rconst char* chanrec::GetAllPrefixChars(userrec* user)\r{\r static char prefix[MAXBUF];\r int ctr = 0;\r *prefix = 0;\r\r prefixlist::iterator n = prefixes.find(user);\r if (n != prefixes.end())\r {\r for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)\r {\r prefix[ctr++] = x->first;\r }\r }\r\r prefix[ctr] = 0;\r\r return prefix;\r}\r\runsigned int chanrec::GetPrefixValue(userrec* user)\r{\r prefixlist::iterator n = prefixes.find(user);\r if (n != prefixes.end())\r {\r if (n->second.size())\r return n->second.begin()->second;\r }\r return 0;\r}\r\rint chanrec::GetStatusFlags(userrec *user)\r{\r UCListIter i = user->chans.find(this);\r if (i != user->chans.end())\r {\r return i->second;\r }\r return 0;\r}\r\rint chanrec::GetStatus(userrec *user)\r{\r if (ServerInstance->ULine(user->server))\r return STATUS_OP;\r\r UCListIter i = user->chans.find(this);\r if (i != user->chans.end())\r {\r if ((i->second & UCMODE_OP) > 0)\r {\r return STATUS_OP;\r }\r if ((i->second & UCMODE_HOP) > 0)\r {\r return STATUS_HOP;\r }\r if ((i->second & UCMODE_VOICE) > 0)\r {\r return STATUS_VOICE;\r }\r return STATUS_NORMAL;\r }\r return STATUS_NORMAL;\r}\r\rvoid chanrec::SetPrefix(userrec* user, char prefix, unsigned int prefix_value, bool adding)\r{\r prefixlist::iterator n = prefixes.find(user);\r prefixtype pfx = std::make_pair(prefix,prefix_value);\r if (adding)\r {\r if (n != prefixes.end())\r {\r if (std::find(n->second.begin(), n->second.end(), pfx) == n->second.end())\r {\r n->second.push_back(pfx);\r /* We must keep prefixes in rank order, largest first.\r * This is for two reasons, firstly because x-chat *ass-u-me's* this\r * state, and secondly it turns out to be a benefit to us later.\r * See above in GetPrefix().\r */\r std::sort(n->second.begin(), n->second.end(), ModeParser::PrefixComparison);\r }\r }\r else\r {\r pfxcontainer one;\r one.push_back(pfx);\r prefixes.insert(std::make_pair<userrec*,pfxcontainer>(user, one));\r }\r }\r else\r {\r if (n != prefixes.end())\r {\r pfxcontainer::iterator x = std::find(n->second.begin(), n->second.end(), pfx);\r if (x != n->second.end())\r n->second.erase(x);\r }\r }\r}\r\rvoid chanrec::RemoveAllPrefixes(userrec* user)\r{\r prefixlist::iterator n = prefixes.find(user);\r if (n != prefixes.end())\r {\r prefixes.erase(n);\r }\r}\r\r
\ No newline at end of file
+/*
+ * InspIRCd -- Internet Relay Chat Daemon
+ *
+ * Copyright (C) 2017 B00mX0r <b00mx0r@aureus.pw>
+ * Copyright (C) 2013-2014, 2016-2020 Sadie Powell <sadie@witchery.services>
+ * Copyright (C) 2013 Adam <Adam@anope.org>
+ * Copyright (C) 2012-2016, 2018 Attila Molnar <attilamolnar@hush.com>
+ * Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
+ * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
+ * Copyright (C) 2007-2009 Robin Burchell <robin+git@viroteck.net>
+ * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
+ * Copyright (C) 2006-2008, 2010 Craig Edwards <brain@inspircd.org>
+ *
+ * This file is part of InspIRCd. InspIRCd is free software: you can
+ * redistribute it and/or modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation, version 2.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#include "inspircd.h"
+#include "listmode.h"
+
+namespace
+{
+ ChanModeReference ban(NULL, "ban");
+}
+
+Channel::Channel(const std::string &cname, time_t ts)
+ : name(cname), age(ts), topicset(0)
+{
+ if (!ServerInstance->chanlist.insert(std::make_pair(cname, this)).second)
+ throw CoreException("Cannot create duplicate channel " + cname);
+}
+
+void Channel::SetMode(ModeHandler* mh, bool on)
+{
+ if (mh && mh->GetId() != ModeParser::MODEID_MAX)
+ modes[mh->GetId()] = on;
+}
+
+void Channel::SetTopic(User* u, const std::string& ntopic, time_t topicts, const std::string* setter)
+{
+ // Send a TOPIC message to the channel only if the new topic text differs
+ if (this->topic != ntopic)
+ {
+ this->topic = ntopic;
+ ClientProtocol::Messages::Topic topicmsg(u, this, this->topic);
+ Write(ServerInstance->GetRFCEvents().topic, topicmsg);
+ }
+
+ // Always update setter and set time
+ if (!setter)
+ setter = ServerInstance->Config->FullHostInTopic ? &u->GetFullHost() : &u->nick;
+ this->setby.assign(*setter, 0, ServerInstance->Config->Limits.GetMaxMask());
+ this->topicset = topicts;
+
+ FOREACH_MOD(OnPostTopicChange, (u, this, this->topic));
+}
+
+Membership* Channel::AddUser(User* user)
+{
+ std::pair<MemberMap::iterator, bool> ret = userlist.insert(std::make_pair(user, insp::aligned_storage<Membership>()));
+ if (!ret.second)
+ return NULL;
+
+ Membership* memb = new(ret.first->second) Membership(user, this);
+ return memb;
+}
+
+void Channel::DelUser(User* user)
+{
+ MemberMap::iterator it = userlist.find(user);
+ if (it != userlist.end())
+ DelUser(it);
+}
+
+void Channel::CheckDestroy()
+{
+ if (!userlist.empty())
+ return;
+
+ ModResult res;
+ FIRST_MOD_RESULT(OnChannelPreDelete, res, (this));
+ if (res == MOD_RES_DENY)
+ return;
+
+ // If the channel isn't in chanlist then it is already in the cull list, don't add it again
+ chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
+ if ((iter == ServerInstance->chanlist.end()) || (iter->second != this))
+ return;
+
+ FOREACH_MOD(OnChannelDelete, (this));
+ ServerInstance->chanlist.erase(iter);
+ ServerInstance->GlobalCulls.AddItem(this);
+}
+
+void Channel::DelUser(const MemberMap::iterator& membiter)
+{
+ Membership* memb = membiter->second;
+ memb->cull();
+ memb->~Membership();
+ userlist.erase(membiter);
+
+ // If this channel became empty then it should be removed
+ CheckDestroy();
+}
+
+Membership* Channel::GetUser(User* user)
+{
+ MemberMap::iterator i = userlist.find(user);
+ if (i == userlist.end())
+ return NULL;
+ return i->second;
+}
+
+void Channel::SetDefaultModes()
+{
+ ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "SetDefaultModes %s",
+ ServerInstance->Config->DefaultModes.c_str());
+ irc::spacesepstream list(ServerInstance->Config->DefaultModes);
+ std::string modeseq;
+ std::string parameter;
+
+ list.GetToken(modeseq);
+
+ for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
+ {
+ ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
+ if (mode)
+ {
+ if (mode->IsPrefixMode())
+ continue;
+
+ if (mode->NeedsParam(true))
+ {
+ list.GetToken(parameter);
+ // If the parameter begins with a ':' then it's invalid
+ if (parameter.c_str()[0] == ':')
+ continue;
+ }
+ else
+ parameter.clear();
+
+ if ((mode->NeedsParam(true)) && (parameter.empty()))
+ continue;
+
+ mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, this, parameter, true);
+ }
+ }
+}
+
+/*
+ * add a channel to a user, creating the record for it if needed and linking
+ * it to the user record
+ */
+Channel* Channel::JoinUser(LocalUser* user, std::string cname, bool override, const std::string& key)
+{
+ if (user->registered != REG_ALL)
+ {
+ ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "Attempted to join unregistered user " + user->uuid + " to channel " + cname);
+ return NULL;
+ }
+
+ /*
+ * We don't restrict the number of channels that remote users or users that are override-joining may be in.
+ * We restrict local users to <connect:maxchans> channels.
+ * We restrict local operators to <oper:maxchans> channels.
+ * This is a lot more logical than how it was formerly. -- w00t
+ */
+ if (!override)
+ {
+ unsigned int maxchans = user->GetClass()->maxchans;
+ if (!maxchans)
+ maxchans = ServerInstance->Config->MaxChans;
+ if (user->IsOper())
+ {
+ unsigned int opermaxchans = ConvToNum<unsigned int>(user->oper->getConfig("maxchans"));
+ // If not set, use 2.0's <channels:opers>, if that's not set either, use limit from CC
+ if (!opermaxchans && user->HasPrivPermission("channels/high-join-limit"))
+ opermaxchans = ServerInstance->Config->OperMaxChans;
+ if (opermaxchans)
+ maxchans = opermaxchans;
+ }
+ if (user->chans.size() >= maxchans)
+ {
+ user->WriteNumeric(ERR_TOOMANYCHANNELS, cname, "You are on too many channels");
+ return NULL;
+ }
+ }
+
+ // Crop channel name if it's too long
+ if (cname.length() > ServerInstance->Config->Limits.ChanMax)
+ cname.resize(ServerInstance->Config->Limits.ChanMax);
+
+ Channel* chan = ServerInstance->FindChan(cname);
+ bool created_by_local = (chan == NULL); // Flag that will be passed to modules in the OnUserJoin() hook later
+ std::string privs; // Prefix mode(letter)s to give to the joining user
+
+ if (!chan)
+ {
+ privs = ServerInstance->Config->DefaultModes.substr(0, ServerInstance->Config->DefaultModes.find(' '));
+
+ if (override == false)
+ {
+ // Ask the modules whether they're ok with the join, pass NULL as Channel* as the channel is yet to be created
+ ModResult MOD_RESULT;
+ FIRST_MOD_RESULT(OnUserPreJoin, MOD_RESULT, (user, NULL, cname, privs, key));
+ if (MOD_RESULT == MOD_RES_DENY)
+ return NULL; // A module wasn't happy with the join, abort
+ }
+
+ chan = new Channel(cname, ServerInstance->Time());
+ // Set the default modes on the channel (<options:defaultmodes>)
+ chan->SetDefaultModes();
+ }
+ else
+ {
+ /* Already on the channel */
+ if (chan->HasUser(user))
+ return NULL;
+
+ if (override == false)
+ {
+ ModResult MOD_RESULT;
+ FIRST_MOD_RESULT(OnUserPreJoin, MOD_RESULT, (user, chan, cname, privs, key));
+
+ // A module explicitly denied the join and (hopefully) generated a message
+ // describing the situation, so we may stop here without sending anything
+ if (MOD_RESULT == MOD_RES_DENY)
+ return NULL;
+
+ // If no module returned MOD_RES_DENY or MOD_RES_ALLOW (which is the case
+ // most of the time) then proceed to check channel bans.
+ //
+ // If a module explicitly allowed the join (by returning MOD_RES_ALLOW),
+ // then this entire section is skipped
+ if (MOD_RESULT == MOD_RES_PASSTHRU)
+ {
+ if (chan->IsBanned(user))
+ {
+ user->WriteNumeric(ERR_BANNEDFROMCHAN, chan->name, "Cannot join channel (you're banned)");
+ return NULL;
+ }
+ }
+ }
+ }
+
+ // We figured that this join is allowed and also created the
+ // channel if it didn't exist before, now do the actual join
+ chan->ForceJoin(user, &privs, false, created_by_local);
+ return chan;
+}
+
+Membership* Channel::ForceJoin(User* user, const std::string* privs, bool bursting, bool created_by_local)
+{
+ if (IS_SERVER(user))
+ {
+ ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "Attempted to join server user " + user->uuid + " to channel " + this->name);
+ return NULL;
+ }
+
+ Membership* memb = this->AddUser(user);
+ if (!memb)
+ return NULL; // Already on the channel
+
+ user->chans.push_front(memb);
+
+ if (privs)
+ {
+ // If the user was granted prefix modes (in the OnUserPreJoin hook, or they're a
+ // remote user and their own server set the modes), then set them internally now
+ for (std::string::const_iterator i = privs->begin(); i != privs->end(); ++i)
+ {
+ PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(*i);
+ if (mh)
+ {
+ std::string nick = user->nick;
+ // Set the mode on the user
+ mh->OnModeChange(ServerInstance->FakeClient, NULL, this, nick, true);
+ }
+ }
+ }
+
+ // 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
+ CUList except_list;
+ FOREACH_MOD(OnUserJoin, (memb, bursting, created_by_local, except_list));
+
+ ClientProtocol::Events::Join joinevent(memb);
+ this->Write(joinevent, 0, except_list);
+
+ FOREACH_MOD(OnPostJoin, (memb));
+ return memb;
+}
+
+bool Channel::IsBanned(User* user)
+{
+ ModResult result;
+ FIRST_MOD_RESULT(OnCheckChannelBan, result, (user, this));
+
+ if (result != MOD_RES_PASSTHRU)
+ return (result == MOD_RES_DENY);
+
+ ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
+ if (!banlm)
+ return false;
+
+ const ListModeBase::ModeList* bans = banlm->GetList(this);
+ if (bans)
+ {
+ for (ListModeBase::ModeList::const_iterator it = bans->begin(); it != bans->end(); it++)
+ {
+ if (CheckBan(user, it->mask))
+ return true;
+ }
+ }
+ return false;
+}
+
+bool Channel::CheckBan(User* user, const std::string& mask)
+{
+ ModResult result;
+ FIRST_MOD_RESULT(OnCheckBan, result, (user, this, mask));
+ if (result != MOD_RES_PASSTHRU)
+ return (result == MOD_RES_DENY);
+
+ // extbans were handled above, if this is one it obviously didn't match
+ if ((mask.length() <= 2) || (mask[1] == ':'))
+ return false;
+
+ std::string::size_type at = mask.find('@');
+ if (at == std::string::npos)
+ return false;
+
+ const std::string nickIdent = user->nick + "!" + user->ident;
+ std::string prefix(mask, 0, at);
+ if (InspIRCd::Match(nickIdent, prefix, NULL))
+ {
+ std::string suffix(mask, at + 1);
+ if (InspIRCd::Match(user->GetRealHost(), suffix, NULL) ||
+ InspIRCd::Match(user->GetDisplayedHost(), suffix, NULL) ||
+ InspIRCd::MatchCIDR(user->GetIPString(), suffix, NULL))
+ return true;
+ }
+ return false;
+}
+
+ModResult Channel::GetExtBanStatus(User *user, char type)
+{
+ ModResult rv;
+ FIRST_MOD_RESULT(OnExtBanCheck, rv, (user, this, type));
+ if (rv != MOD_RES_PASSTHRU)
+ return rv;
+
+ ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
+ if (!banlm)
+ return MOD_RES_PASSTHRU;
+
+ const ListModeBase::ModeList* bans = banlm->GetList(this);
+ if (bans)
+ {
+ for (ListModeBase::ModeList::const_iterator it = bans->begin(); it != bans->end(); ++it)
+ {
+ if (it->mask.length() <= 2 || it->mask[0] != type || it->mask[1] != ':')
+ continue;
+
+ if (CheckBan(user, it->mask.substr(2)))
+ return MOD_RES_DENY;
+ }
+ }
+ return MOD_RES_PASSTHRU;
+}
+
+/* Channel::PartUser
+ * Remove a channel from a users record, remove the reference to the Membership object
+ * from the channel and destroy it.
+ */
+bool Channel::PartUser(User* user, std::string& reason)
+{
+ MemberMap::iterator membiter = userlist.find(user);
+
+ if (membiter == userlist.end())
+ return false;
+
+ Membership* memb = membiter->second;
+ CUList except_list;
+ FOREACH_MOD(OnUserPart, (memb, reason, except_list));
+
+ ClientProtocol::Messages::Part partmsg(memb, reason);
+ Write(ServerInstance->GetRFCEvents().part, partmsg, 0, except_list);
+
+ // Remove this channel from the user's chanlist
+ user->chans.erase(memb);
+ // Remove the Membership from this channel's userlist and destroy it
+ this->DelUser(membiter);
+
+ return true;
+}
+
+void Channel::KickUser(User* src, const MemberMap::iterator& victimiter, const std::string& reason)
+{
+ Membership* memb = victimiter->second;
+ CUList except_list;
+ FOREACH_MOD(OnUserKick, (src, memb, reason, except_list));
+
+ ClientProtocol::Messages::Kick kickmsg(src, memb, reason);
+ Write(ServerInstance->GetRFCEvents().kick, kickmsg, 0, except_list);
+
+ memb->user->chans.erase(memb);
+ this->DelUser(victimiter);
+}
+
+void Channel::Write(ClientProtocol::Event& protoev, char status, const CUList& except_list)
+{
+ unsigned int minrank = 0;
+ if (status)
+ {
+ PrefixMode* mh = ServerInstance->Modes->FindPrefix(status);
+ if (mh)
+ minrank = mh->GetPrefixRank();
+ }
+ for (MemberMap::iterator i = userlist.begin(); i != userlist.end(); i++)
+ {
+ LocalUser* user = IS_LOCAL(i->first);
+ if ((user) && (!except_list.count(user)))
+ {
+ /* User doesn't have the status we're after */
+ if (minrank && i->second->getRank() < minrank)
+ continue;
+
+ user->Send(protoev);
+ }
+ }
+}
+
+const char* Channel::ChanModes(bool showsecret)
+{
+ static std::string scratch;
+ std::string sparam;
+
+ scratch.clear();
+
+ /* This was still iterating up to 190, Channel::modes is only 64 elements -- Om */
+ for(int n = 0; n < 64; n++)
+ {
+ ModeHandler* mh = ServerInstance->Modes->FindMode(n + 65, MODETYPE_CHANNEL);
+ if (mh && IsModeSet(mh))
+ {
+ scratch.push_back(n + 65);
+
+ ParamModeBase* pm = mh->IsParameterMode();
+ if (!pm)
+ continue;
+
+ if (pm->IsParameterSecret() && !showsecret)
+ {
+ sparam += " <" + pm->name + ">";
+ }
+ else
+ {
+ sparam += ' ';
+ pm->GetParameter(this, sparam);
+ }
+ }
+ }
+
+ scratch += sparam;
+ return scratch.c_str();
+}
+
+void Channel::WriteNotice(const std::string& text, char status)
+{
+ ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, ServerInstance->FakeClient, this, text, MSG_NOTICE, status);
+ Write(ServerInstance->GetRFCEvents().privmsg, privmsg);
+}
+
+void Channel::WriteRemoteNotice(const std::string& text, char status)
+{
+ WriteNotice(text, status);
+ ServerInstance->PI->SendMessage(this, status, text, MSG_NOTICE);
+}
+
+/* returns the status character for a given user on a channel, e.g. @ for op,
+ * % for halfop etc. If the user has several modes set, the highest mode
+ * the user has must be returned.
+ */
+char Membership::GetPrefixChar() const
+{
+ char pf = 0;
+ unsigned int bestrank = 0;
+
+ for (std::string::const_iterator i = modes.begin(); i != modes.end(); ++i)
+ {
+ PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(*i);
+ if (mh && mh->GetPrefixRank() > bestrank && mh->GetPrefix())
+ {
+ bestrank = mh->GetPrefixRank();
+ pf = mh->GetPrefix();
+ }
+ }
+ return pf;
+}
+
+unsigned int Membership::getRank()
+{
+ char mchar = modes.c_str()[0];
+ unsigned int rv = 0;
+ if (mchar)
+ {
+ PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(mchar);
+ if (mh)
+ rv = mh->GetPrefixRank();
+ }
+ return rv;
+}
+
+std::string Membership::GetAllPrefixChars() const
+{
+ std::string ret;
+ for (std::string::const_iterator i = modes.begin(); i != modes.end(); ++i)
+ {
+ PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(*i);
+ if (mh && mh->GetPrefix())
+ ret.push_back(mh->GetPrefix());
+ }
+
+ return ret;
+}
+
+unsigned int Channel::GetPrefixValue(User* user)
+{
+ MemberMap::iterator m = userlist.find(user);
+ if (m == userlist.end())
+ return 0;
+ return m->second->getRank();
+}
+
+bool Membership::SetPrefix(PrefixMode* delta_mh, bool adding)
+{
+ char prefix = delta_mh->GetModeChar();
+ for (unsigned int i = 0; i < modes.length(); i++)
+ {
+ char mchar = modes[i];
+ PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(mchar);
+ if (mh && mh->GetPrefixRank() <= delta_mh->GetPrefixRank())
+ {
+ modes = modes.substr(0,i) +
+ (adding ? std::string(1, prefix) : "") +
+ modes.substr(mchar == prefix ? i+1 : i);
+ return adding != (mchar == prefix);
+ }
+ }
+ if (adding)
+ modes.push_back(prefix);
+ return adding;
+}
+
+
+void Membership::WriteNotice(const std::string& text) const
+{
+ LocalUser* const localuser = IS_LOCAL(user);
+ if (!localuser)
+ return;
+
+ ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, ServerInstance->FakeClient, this->chan, text, MSG_NOTICE);
+ localuser->Send(ServerInstance->GetRFCEvents().privmsg, privmsg);
+}