-/* +------------------------------------+\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
+/* +------------------------------------+
+ * | Inspire Internet Relay Chat Daemon |
+ * +------------------------------------+
+ *
+ * InspIRCd: (C) 2002-2007 InspIRCd Development Team
+ * See: http://www.inspircd.org/wiki/index.php/Credits
+ *
+ * This program is free but copyrighted software; see
+ * the file COPYING for details.
+ *
+ * ---------------------------------------------------
+ */
+
+#include "inspircd.h"
+#include <stdarg.h>
+#include "wildcard.h"
+#include "mode.h"
+
+chanrec::chanrec(InspIRCd* Instance) : ServerInstance(Instance)
+{
+ *name = *topic = *setby = *key = 0;
+ maxbans = created = topicset = limit = 0;
+ memset(&modes,0,64);
+ age = ServerInstance->Time(true);
+}
+
+void chanrec::SetMode(char mode,bool mode_on)
+{
+ modes[mode-65] = mode_on;
+ if (!mode_on)
+ this->SetModeParam(mode,"",false);
+}
+
+
+void chanrec::SetModeParam(char mode,const char* parameter,bool mode_on)
+{
+ CustomModeList::iterator n = custom_mode_params.find(mode);
+
+ if (mode_on)
+ {
+ if (n == custom_mode_params.end())
+ custom_mode_params[mode] = strdup(parameter);
+ }
+ else
+ {
+ if (n != custom_mode_params.end())
+ {
+ free(n->second);
+ custom_mode_params.erase(n);
+ }
+ }
+}
+
+bool chanrec::IsModeSet(char mode)
+{
+ return modes[mode-65];
+}
+
+std::string chanrec::GetModeParameter(char mode)
+{
+ switch (mode)
+ {
+ case 'k':
+ return this->key;
+ case 'l':
+ return ConvToStr(this->limit);
+ default:
+ CustomModeList::iterator n = custom_mode_params.find(mode);
+ if (n != custom_mode_params.end())
+ return n->second;
+ return "";
+ break;
+ }
+}
+
+long chanrec::GetUserCounter()
+{
+ return (this->internal_userlist.size());
+}
+
+void chanrec::AddUser(userrec* user)
+{
+ internal_userlist[user] = user->nick;
+}
+
+unsigned long chanrec::DelUser(userrec* user)
+{
+ CUListIter a = internal_userlist.find(user);
+
+ if (a != internal_userlist.end())
+ {
+ internal_userlist.erase(a);
+ /* And tidy any others... */
+ DelOppedUser(user);
+ DelHalfoppedUser(user);
+ DelVoicedUser(user);
+ }
+
+ return internal_userlist.size();
+}
+
+bool chanrec::HasUser(userrec* user)
+{
+ return (internal_userlist.find(user) != internal_userlist.end());
+}
+
+void chanrec::AddOppedUser(userrec* user)
+{
+ internal_op_userlist[user] = user->nick;
+}
+
+void chanrec::DelOppedUser(userrec* user)
+{
+ CUListIter a = internal_op_userlist.find(user);
+ if (a != internal_op_userlist.end())
+ {
+ internal_op_userlist.erase(a);
+ return;
+ }
+}
+
+void chanrec::AddHalfoppedUser(userrec* user)
+{
+ internal_halfop_userlist[user] = user->nick;
+}
+
+void chanrec::DelHalfoppedUser(userrec* user)
+{
+ CUListIter a = internal_halfop_userlist.find(user);
+
+ if (a != internal_halfop_userlist.end())
+ {
+ internal_halfop_userlist.erase(a);
+ }
+}
+
+void chanrec::AddVoicedUser(userrec* user)
+{
+ internal_voice_userlist[user] = user->nick;
+}
+
+void chanrec::DelVoicedUser(userrec* user)
+{
+ CUListIter a = internal_voice_userlist.find(user);
+
+ if (a != internal_voice_userlist.end())
+ {
+ internal_voice_userlist.erase(a);
+ }
+}
+
+CUList* chanrec::GetUsers()
+{
+ return &internal_userlist;
+}
+
+CUList* chanrec::GetOppedUsers()
+{
+ return &internal_op_userlist;
+}
+
+CUList* chanrec::GetHalfoppedUsers()
+{
+ return &internal_halfop_userlist;
+}
+
+CUList* chanrec::GetVoicedUsers()
+{
+ return &internal_voice_userlist;
+}
+
+void chanrec::SetDefaultModes()
+{
+ 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->GetNumParams(true))
+ list.GetToken(parameter);
+ else
+ parameter.clear();
+
+ 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
+ */
+chanrec* chanrec::JoinUser(InspIRCd* Instance, userrec *user, const char* cn, bool override, const char* key, time_t TS)
+{
+ if (!user || !cn)
+ return NULL;
+
+ char cname[MAXBUF];
+ int MOD_RESULT = 0;
+ std::string privs;
+ chanrec *Ptr;
+
+ /*
+ * 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 MaxChans channels.
+ * We restrict local operators to OperMaxChans channels.
+ * This is a lot more logical than how it was formerly. -- w00t
+ */
+ if (IS_LOCAL(user) && !override)
+ {
+ if (user->GetMaxChans())
+ {
+ if (user->chans.size() >= user->GetMaxChans())
+ {
+ user->WriteServ("405 %s %s :You are on too many channels",user->nick, cn);
+ return NULL;
+ }
+ }
+ else
+ {
+ if (IS_OPER(user))
+ {
+ if (user->chans.size() >= Instance->Config->OperMaxChans)
+ {
+ user->WriteServ("405 %s %s :You are on too many channels",user->nick, cn);
+ return NULL;
+ }
+ }
+ else
+ {
+ if (user->chans.size() >= Instance->Config->MaxChans)
+ {
+ user->WriteServ("405 %s %s :You are on too many channels",user->nick, cn);
+ return NULL;
+ }
+ }
+ }
+ }
+
+ strlcpy(cname, cn, CHANMAX);
+ Ptr = Instance->FindChan(cname);
+
+ if (!Ptr)
+ {
+ /*
+ * Fix: desync bug was here, don't set @ on remote users - spanningtree handles their permissions. bug #358. -- w00t
+ */
+ if (!IS_LOCAL(user))
+ {
+ if (!TS)
+ Instance->Log(DEBUG,"*** BUG *** chanrec::JoinUser called for REMOTE user '%s' on channel '%s' but no TS given!", user->nick, cn);
+ }
+ else
+ {
+ privs = "@";
+ }
+
+ if (IS_LOCAL(user) && override == false)
+ {
+ MOD_RESULT = 0;
+ FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,NULL,cname,privs));
+ if (MOD_RESULT == 1)
+ return NULL;
+ }
+
+ /* create a new one */
+ Ptr = new chanrec(Instance);
+ (*(Instance->chanlist))[cname] = Ptr;
+
+ strlcpy(Ptr->name, cname,CHANMAX);
+
+ /* As spotted by jilles, dont bother to set this on remote users */
+ if (IS_LOCAL(user))
+ Ptr->SetDefaultModes();
+
+ Ptr->created = TS ? TS : Instance->Time();
+ Ptr->age = Ptr->created;
+ *Ptr->topic = 0;
+ *Ptr->setby = 0;
+ Ptr->topicset = 0;
+ }
+ else
+ {
+ /* Already on the channel */
+ if (Ptr->HasUser(user))
+ return NULL;
+
+ /*
+ * remote users are allowed us to bypass channel modes
+ * and bans (used by servers)
+ */
+ if (IS_LOCAL(user) && override == false)
+ {
+ MOD_RESULT = 0;
+ FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname,privs));
+ if (MOD_RESULT == 1)
+ {
+ return NULL;
+ }
+ else if (MOD_RESULT == 0)
+ {
+ if (*Ptr->key)
+ {
+ MOD_RESULT = 0;
+ FOREACH_RESULT_I(Instance,I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
+ if (!MOD_RESULT)
+ {
+ if ((!key) || strcmp(key,Ptr->key))
+ {
+ user->WriteServ("475 %s %s :Cannot join channel (Incorrect channel key)",user->nick, Ptr->name);
+ return NULL;
+ }
+ }
+ }
+ if (Ptr->IsModeSet('i'))
+ {
+ MOD_RESULT = 0;
+ FOREACH_RESULT_I(Instance,I_OnCheckInvite,OnCheckInvite(user, Ptr));
+ if (!MOD_RESULT)
+ {
+ if (!user->IsInvited(Ptr->name))
+ {
+ user->WriteServ("473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
+ return NULL;
+ }
+ }
+ user->RemoveInvite(Ptr->name);
+ }
+ if (Ptr->limit)
+ {
+ MOD_RESULT = 0;
+ FOREACH_RESULT_I(Instance,I_OnCheckLimit,OnCheckLimit(user, Ptr));
+ if (!MOD_RESULT)
+ {
+ if (Ptr->GetUserCounter() >= Ptr->limit)
+ {
+ user->WriteServ("471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
+ return NULL;
+ }
+ }
+ }
+ if (Ptr->bans.size())
+ {
+ if (Ptr->IsBanned(user))
+ {
+ user->WriteServ("474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
+ return NULL;
+ }
+ }
+ }
+ }
+ }
+
+ return chanrec::ForceChan(Instance, Ptr, user, privs);
+}
+
+chanrec* chanrec::ForceChan(InspIRCd* Instance, chanrec* Ptr, userrec* user, const std::string &privs)
+{
+ std::string nick = user->nick;
+ bool silent = false;
+
+ Ptr->AddUser(user);
+
+ /* Just in case they have no permissions */
+ user->chans[Ptr] = 0;
+
+ for (std::string::const_iterator x = privs.begin(); x != privs.end(); x++)
+ {
+ const char status = *x;
+ ModeHandler* mh = Instance->Modes->FindPrefix(status);
+ if (mh)
+ {
+ Ptr->SetPrefix(user, status, mh->GetPrefixRank(), true);
+ /* Make sure that the mode handler knows this mode was now set */
+ mh->OnModeChange(Instance->FakeClient, Instance->FakeClient, Ptr, nick, true);
+
+ switch (mh->GetPrefix())
+ {
+ /* These logic ops are SAFE IN THIS CASE
+ * because if the entry doesnt exist,
+ * addressing operator[] creates it.
+ * If they do exist, it points to it.
+ * At all other times where we dont want
+ * to create an item if it doesnt exist, we
+ * must stick to ::find().
+ */
+ case '@':
+ user->chans[Ptr] |= UCMODE_OP;
+ break;
+ case '%':
+ user->chans[Ptr] |= UCMODE_HOP;
+ break;
+ case '+':
+ user->chans[Ptr] |= UCMODE_VOICE;
+ break;
+ }
+ }
+ }
+
+ FOREACH_MOD_I(Instance,I_OnUserJoin,OnUserJoin(user, Ptr, silent));
+
+ if (!silent)
+ Ptr->WriteChannel(user,"JOIN :%s",Ptr->name);
+
+ /* Theyre not the first ones in here, make sure everyone else sees the modes we gave the user */
+ std::string ms = Instance->Modes->ModeString(user, Ptr);
+ if ((Ptr->GetUserCounter() > 1) && (ms.length()))
+ Ptr->WriteAllExceptSender(user, true, 0, "MODE %s +%s", Ptr->name, ms.c_str());
+
+ /* Major improvement by Brain - we dont need to be calculating all this pointlessly for remote users */
+ if (IS_LOCAL(user))
+ {
+ if (Ptr->topicset)
+ {
+ user->WriteServ("332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
+ user->WriteServ("333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
+ }
+ Ptr->UserList(user);
+ }
+ FOREACH_MOD_I(Instance,I_OnPostJoin,OnPostJoin(user, Ptr));
+ return Ptr;
+}
+
+bool chanrec::IsBanned(userrec* user)
+{
+ char mask[MAXBUF];
+ int MOD_RESULT = 0;
+ FOREACH_RESULT(I_OnCheckBan,OnCheckBan(user, this));
+ if (!MOD_RESULT)
+ {
+ snprintf(mask, MAXBUF, "%s!%s@%s", user->nick, user->ident, user->GetIPString());
+ for (BanList::iterator i = this->bans.begin(); i != this->bans.end(); i++)
+ {
+ /* This allows CIDR ban matching
+ *
+ * Full masked host Full unmasked host IP with/without CIDR
+ */
+ if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match(mask, i->data, true)))
+ {
+ return true;
+ }
+ }
+ }
+ return false;
+}
+
+/* chanrec::PartUser
+ * remove a channel from a users record, and return the number of users left.
+ * Therefore, if this function returns 0 the caller should delete the chanrec.
+ */
+long chanrec::PartUser(userrec *user, const char* reason)
+{
+ bool silent = false;
+
+ if (!user)
+ return this->GetUserCounter();
+
+ UCListIter i = user->chans.find(this);
+ if (i != user->chans.end())
+ {
+ FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, reason ? reason : "", silent));
+
+ if (!silent)
+ this->WriteChannel(user, "PART %s%s%s", this->name, reason ? " :" : "", reason ? reason : "");
+
+ user->chans.erase(i);
+ this->RemoveAllPrefixes(user);
+ }
+
+ if (!this->DelUser(user)) /* if there are no users left on the channel... */
+ {
+ chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
+ /* kill the record */
+ if (iter != ServerInstance->chanlist->end())
+ {
+ FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
+ ServerInstance->chanlist->erase(iter);
+ }
+ return 0;
+ }
+
+ return this->GetUserCounter();
+}
+
+long chanrec::ServerKickUser(userrec* user, const char* reason, bool triggerevents)
+{
+ bool silent = false;
+
+ if (!user || !reason)
+ return this->GetUserCounter();
+
+ if (IS_LOCAL(user))
+ {
+ if (!this->HasUser(user))
+ {
+ /* Not on channel */
+ return this->GetUserCounter();
+ }
+ }
+
+ if (triggerevents)
+ {
+ FOREACH_MOD(I_OnUserKick,OnUserKick(NULL, user, this, reason, silent));
+ }
+
+ UCListIter i = user->chans.find(this);
+ if (i != user->chans.end())
+ {
+ if (!silent)
+ this->WriteChannelWithServ(ServerInstance->Config->ServerName, "KICK %s %s :%s", this->name, user->nick, reason);
+
+ user->chans.erase(i);
+ this->RemoveAllPrefixes(user);
+ }
+
+ if (!this->DelUser(user))
+ {
+ chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
+ /* kill the record */
+ if (iter != ServerInstance->chanlist->end())
+ {
+ FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
+ ServerInstance->chanlist->erase(iter);
+ }
+ return 0;
+ }
+
+ return this->GetUserCounter();
+}
+
+long chanrec::KickUser(userrec *src, userrec *user, const char* reason)
+{
+ bool silent = false;
+
+ if (!src || !user || !reason)
+ return this->GetUserCounter();
+
+ if (IS_LOCAL(src))
+ {
+ if (!this->HasUser(user))
+ {
+ src->WriteServ("441 %s %s %s :They are not on that channel",src->nick, user->nick, this->name);
+ return this->GetUserCounter();
+ }
+ if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
+ {
+ src->WriteServ("482 %s %s :Only a u-line may kick a u-line from a channel.",src->nick, this->name);
+ return this->GetUserCounter();
+ }
+ int MOD_RESULT = 0;
+
+ if (!ServerInstance->ULine(src->server))
+ {
+ MOD_RESULT = 0;
+ FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,this,reason));
+ if (MOD_RESULT == 1)
+ return this->GetUserCounter();
+ }
+ /* Set to -1 by OnUserPreKick if explicit allow was set */
+ if (MOD_RESULT != -1)
+ {
+ FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,this,AC_KICK));
+ if ((MOD_RESULT == ACR_DENY) && (!ServerInstance->ULine(src->server)))
+ return this->GetUserCounter();
+
+ if ((MOD_RESULT == ACR_DEFAULT) || (!ServerInstance->ULine(src->server)))
+ {
+ int them = this->GetStatus(src);
+ int us = this->GetStatus(user);
+ if ((them < STATUS_HOP) || (them < us))
+ {
+ src->WriteServ("482 %s %s :You must be a channel %soperator",src->nick, this->name, them == STATUS_HOP ? "" : "half-");
+ return this->GetUserCounter();
+ }
+ }
+ }
+ }
+
+ FOREACH_MOD(I_OnUserKick,OnUserKick(src, user, this, reason, silent));
+
+ UCListIter i = user->chans.find(this);
+ if (i != user->chans.end())
+ {
+ /* zap it from the channel list of the user */
+ if (!silent)
+ this->WriteChannel(src, "KICK %s %s :%s", this->name, user->nick, reason);
+
+ user->chans.erase(i);
+ this->RemoveAllPrefixes(user);
+ }
+
+ if (!this->DelUser(user))
+ /* if there are no users left on the channel */
+ {
+ chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
+
+ /* kill the record */
+ if (iter != ServerInstance->chanlist->end())
+ {
+ FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
+ ServerInstance->chanlist->erase(iter);
+ }
+ return 0;
+ }
+
+ return this->GetUserCounter();
+}
+
+void chanrec::WriteChannel(userrec* user, char* text, ...)
+{
+ char textbuffer[MAXBUF];
+ va_list argsPtr;
+
+ if (!user || !text)
+ return;
+
+ va_start(argsPtr, text);
+ vsnprintf(textbuffer, MAXBUF, text, argsPtr);
+ va_end(argsPtr);
+
+ this->WriteChannel(user, std::string(textbuffer));
+}
+
+void chanrec::WriteChannel(userrec* user, const std::string &text)
+{
+ CUList *ulist = this->GetUsers();
+ char tb[MAXBUF];
+
+ if (!user)
+ return;
+
+ snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());
+ std::string out = tb;
+
+ for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
+ {
+ if (IS_LOCAL(i->first))
+ i->first->Write(out);
+ }
+}
+
+void chanrec::WriteChannelWithServ(const char* ServName, const char* text, ...)
+{
+ char textbuffer[MAXBUF];
+ va_list argsPtr;
+
+ if (!text)
+ return;
+
+ va_start(argsPtr, text);
+ vsnprintf(textbuffer, MAXBUF, text, argsPtr);
+ va_end(argsPtr);
+
+ this->WriteChannelWithServ(ServName, std::string(textbuffer));
+}
+
+void chanrec::WriteChannelWithServ(const char* ServName, const std::string &text)
+{
+ CUList *ulist = this->GetUsers();
+ char tb[MAXBUF];
+
+ snprintf(tb,MAXBUF,":%s %s",ServName ? ServName : ServerInstance->Config->ServerName, text.c_str());
+ std::string out = tb;
+
+ for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
+ {
+ if (IS_LOCAL(i->first))
+ i->first->Write(out);
+ }
+}
+
+/* write formatted text from a source user to all users on a channel except
+ * for the sender (for privmsg etc) */
+void chanrec::WriteAllExceptSender(userrec* user, bool serversource, char status, char* text, ...)
+{
+ char textbuffer[MAXBUF];
+ va_list argsPtr;
+
+ if (!text)
+ return;
+
+ va_start(argsPtr, text);
+ vsnprintf(textbuffer, MAXBUF, text, argsPtr);
+ va_end(argsPtr);
+
+ this->WriteAllExceptSender(user, serversource, status, std::string(textbuffer));
+}
+
+void chanrec::WriteAllExcept(userrec* user, bool serversource, char status, CUList &except_list, char* text, ...)
+{
+ char textbuffer[MAXBUF];
+ va_list argsPtr;
+
+ if (!text)
+ return;
+
+ va_start(argsPtr, text);
+ vsnprintf(textbuffer, MAXBUF, text, argsPtr);
+ va_end(argsPtr);
+
+ this->WriteAllExcept(user, serversource, status, except_list, std::string(textbuffer));
+}
+
+void chanrec::WriteAllExcept(userrec* user, bool serversource, char status, CUList &except_list, const std::string &text)
+{
+ CUList *ulist;
+ char tb[MAXBUF];
+
+ switch (status)
+ {
+ case '@':
+ ulist = this->GetOppedUsers();
+ break;
+ case '%':
+ ulist = this->GetHalfoppedUsers();
+ break;
+ case '+':
+ ulist = this->GetVoicedUsers();
+ break;
+ default:
+ ulist = this->GetUsers();
+ break;
+ }
+
+ snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());
+ std::string out = tb;
+
+ for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
+ {
+ if ((IS_LOCAL(i->first)) && (except_list.find(i->first) == except_list.end()))
+ {
+ if (serversource)
+ i->first->WriteServ(text);
+ else
+ i->first->Write(out);
+ }
+ }
+}
+
+void chanrec::WriteAllExceptSender(userrec* user, bool serversource, char status, const std::string& text)
+{
+ CUList except_list;
+ except_list[user] = user->nick;
+ this->WriteAllExcept(user, serversource, status, except_list, std::string(text));
+}
+
+/*
+ * return a count of the users on a specific channel accounting for
+ * invisible users who won't increase the count. e.g. for /LIST
+ */
+int chanrec::CountInvisible()
+{
+ int count = 0;
+ CUList *ulist= this->GetUsers();
+ for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
+ {
+ if (!(i->first->IsModeSet('i')))
+ count++;
+ }
+
+ return count;
+}
+
+char* chanrec::ChanModes(bool showkey)
+{
+ static char scratch[MAXBUF];
+ static char sparam[MAXBUF];
+ char* offset = scratch;
+ std::string extparam;
+
+ *scratch = '\0';
+ *sparam = '\0';
+
+ /* This was still iterating up to 190, chanrec::modes is only 64 elements -- Om */
+ for(int n = 0; n < 64; n++)
+ {
+ if(this->modes[n])
+ {
+ *offset++ = n + 65;
+ extparam.clear();
+ switch (n)
+ {
+ case CM_KEY:
+ extparam = (showkey ? this->key : "<key>");
+ break;
+ case CM_LIMIT:
+ extparam = ConvToStr(this->limit);
+ break;
+ case CM_NOEXTERNAL:
+ case CM_TOPICLOCK:
+ case CM_INVITEONLY:
+ case CM_MODERATED:
+ case CM_SECRET:
+ case CM_PRIVATE:
+ /* We know these have no parameters */
+ break;
+ default:
+ extparam = this->GetModeParameter(n + 65);
+ break;
+ }
+ if (!extparam.empty())
+ {
+ charlcat(sparam,' ',MAXBUF);
+ strlcat(sparam,extparam.c_str(),MAXBUF);
+ }
+ }
+ }
+
+ /* Null terminate scratch */
+ *offset = '\0';
+ strlcat(scratch,sparam,MAXBUF);
+ return scratch;
+}
+
+/* compile a userlist of a channel into a string, each nick seperated by
+ * spaces and op, voice etc status shown as @ and +, and send it to 'user'
+ */
+void chanrec::UserList(userrec *user, CUList *ulist)
+{
+ char list[MAXBUF];
+ size_t dlen, curlen;
+ int MOD_RESULT = 0;
+
+ if (!IS_LOCAL(user))
+ return;
+
+ FOREACH_RESULT(I_OnUserList,OnUserList(user, this, ulist));
+ if (MOD_RESULT == 1)
+ return;
+
+ dlen = curlen = snprintf(list,MAXBUF,"353 %s %c %s :", user->nick, this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=', this->name);
+
+ int numusers = 0;
+ char* ptr = list + dlen;
+
+ if (!ulist)
+ ulist = this->GetUsers();
+
+ /* Improvement by Brain - this doesnt change in value, so why was it inside
+ * the loop?
+ */
+ bool has_user = this->HasUser(user);
+
+ for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
+ {
+ if ((!has_user) && (i->first->IsModeSet('i')))
+ {
+ /*
+ * user is +i, and source not on the channel, does not show
+ * nick in NAMES list
+ */
+ continue;
+ }
+
+ if (i->first->Visibility && !i->first->Visibility->VisibleTo(user))
+ continue;
+
+ size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", this->GetPrefixChar(i->first), i->second.c_str());
+ /* OnUserList can change this - reset it back to normal */
+ i->second = i->first->nick;
+
+ curlen += ptrlen;
+ ptr += ptrlen;
+
+ numusers++;
+
+ if (curlen > (480-NICKMAX))
+ {
+ /* list overflowed into multiple numerics */
+ user->WriteServ(std::string(list));
+
+ /* reset our lengths */
+ dlen = curlen = snprintf(list,MAXBUF,"353 %s %c %s :", user->nick, this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=', this->name);
+ ptr = list + dlen;
+
+ ptrlen = 0;
+ numusers = 0;
+ }
+ }
+
+ /* if whats left in the list isnt empty, send it */
+ if (numusers)
+ {
+ user->WriteServ(std::string(list));
+ }
+
+ user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, this->name);
+}
+
+long chanrec::GetMaxBans()
+{
+ /* Return the cached value if there is one */
+ if (this->maxbans)
+ return this->maxbans;
+
+ /* If there isnt one, we have to do some O(n) hax to find it the first time. (ick) */
+ for (std::map<std::string,int>::iterator n = ServerInstance->Config->maxbans.begin(); n != ServerInstance->Config->maxbans.end(); n++)
+ {
+ if (match(this->name,n->first.c_str()))
+ {
+ this->maxbans = n->second;
+ return n->second;
+ }
+ }
+
+ /* Screw it, just return the default of 64 */
+ this->maxbans = 64;
+ return this->maxbans;
+}
+
+void chanrec::ResetMaxBans()
+{
+ this->maxbans = 0;
+}
+
+/* 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.
+ */
+const char* chanrec::GetPrefixChar(userrec *user)
+{
+ static char pf[2] = {0, 0};
+
+ prefixlist::iterator n = prefixes.find(user);
+ if (n != prefixes.end())
+ {
+ if (n->second.size())
+ {
+ /* If the user has any prefixes, their highest prefix
+ * will always be at the head of the list, as the list is
+ * sorted in rank order highest first (see SetPrefix()
+ * for reasons why)
+ */
+ *pf = n->second.begin()->first;
+ return pf;
+ }
+ }
+
+ *pf = 0;
+ return pf;
+}
+
+const char* chanrec::GetAllPrefixChars(userrec* user)
+{
+ static char prefix[MAXBUF];
+ int ctr = 0;
+ *prefix = 0;
+
+ prefixlist::iterator n = prefixes.find(user);
+ if (n != prefixes.end())
+ {
+ for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
+ {
+ prefix[ctr++] = x->first;
+ }
+ }
+
+ prefix[ctr] = 0;
+
+ return prefix;
+}
+
+unsigned int chanrec::GetPrefixValue(userrec* user)
+{
+ prefixlist::iterator n = prefixes.find(user);
+ if (n != prefixes.end())
+ {
+ if (n->second.size())
+ return n->second.begin()->second;
+ }
+ return 0;
+}
+
+int chanrec::GetStatusFlags(userrec *user)
+{
+ UCListIter i = user->chans.find(this);
+ if (i != user->chans.end())
+ {
+ return i->second;
+ }
+ return 0;
+}
+
+int chanrec::GetStatus(userrec *user)
+{
+ if (ServerInstance->ULine(user->server))
+ return STATUS_OP;
+
+ UCListIter i = user->chans.find(this);
+ if (i != user->chans.end())
+ {
+ if ((i->second & UCMODE_OP) > 0)
+ {
+ return STATUS_OP;
+ }
+ if ((i->second & UCMODE_HOP) > 0)
+ {
+ return STATUS_HOP;
+ }
+ if ((i->second & UCMODE_VOICE) > 0)
+ {
+ return STATUS_VOICE;
+ }
+ return STATUS_NORMAL;
+ }
+ return STATUS_NORMAL;
+}
+
+void chanrec::SetPrefix(userrec* user, char prefix, unsigned int prefix_value, bool adding)
+{
+ prefixlist::iterator n = prefixes.find(user);
+ prefixtype pfx = std::make_pair(prefix,prefix_value);
+ if (adding)
+ {
+ if (n != prefixes.end())
+ {
+ if (std::find(n->second.begin(), n->second.end(), pfx) == n->second.end())
+ {
+ n->second.push_back(pfx);
+ /* We must keep prefixes in rank order, largest first.
+ * This is for two reasons, firstly because x-chat *ass-u-me's* this
+ * state, and secondly it turns out to be a benefit to us later.
+ * See above in GetPrefix().
+ */
+ std::sort(n->second.begin(), n->second.end(), ModeParser::PrefixComparison);
+ }
+ }
+ else
+ {
+ pfxcontainer one;
+ one.push_back(pfx);
+ prefixes.insert(std::make_pair<userrec*,pfxcontainer>(user, one));
+ }
+ }
+ else
+ {
+ if (n != prefixes.end())
+ {
+ pfxcontainer::iterator x = std::find(n->second.begin(), n->second.end(), pfx);
+ if (x != n->second.end())
+ n->second.erase(x);
+ }
+ }
+}
+
+void chanrec::RemoveAllPrefixes(userrec* user)
+{
+ prefixlist::iterator n = prefixes.find(user);
+ if (n != prefixes.end())
+ {
+ prefixes.erase(n);
+ }
+}
+