-/* +------------------------------------+\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) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
+ * Copyright (C) 2006-2008 Robin Burchell <robin+git@viroteck.net>
+ * Copyright (C) 2006, 2008 Oliver Lupton <oliverlupton@gmail.com>
+ * Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
+ * Copyright (C) 2003-2008 Craig Edwards <craigedwards@brainbox.cc>
+ * Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
+ * Copyright (C) 2007 Dennis Friis <peavey@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/>.
+ */
+
+
+/* $Core */
+
+#include "inspircd.h"
+#include "listmode.h"
+#include <cstdarg>
+#include "mode.h"
+
+static ModeReference ban(NULL, "ban");
+
+Channel::Channel(const std::string &cname, time_t ts)
+{
+ chan_hash::iterator findchan = ServerInstance->chanlist->find(cname);
+ if (findchan != ServerInstance->chanlist->end())
+ throw CoreException("Cannot create duplicate channel " + cname);
+
+ (*(ServerInstance->chanlist))[cname.c_str()] = this;
+ this->name.assign(cname, 0, ServerInstance->Config->Limits.ChanMax);
+ this->age = ts ? ts : ServerInstance->Time();
+
+ topicset = 0;
+ modes.reset();
+}
+
+void Channel::SetMode(char mode,bool mode_on)
+{
+ modes[mode-65] = mode_on;
+}
+
+void Channel::SetMode(ModeHandler* mh, bool on)
+{
+ modes[mh->GetModeChar() - 65] = on;
+}
+
+void Channel::SetModeParam(char mode, const std::string& parameter)
+{
+ CustomModeList::iterator n = custom_mode_params.find(mode);
+ // always erase, even if changing, so that the map gets the new value
+ if (n != custom_mode_params.end())
+ custom_mode_params.erase(n);
+ if (parameter.empty())
+ {
+ modes[mode-65] = false;
+ }
+ else
+ {
+ custom_mode_params[mode] = parameter;
+ modes[mode-65] = true;
+ }
+}
+
+void Channel::SetModeParam(ModeHandler* mode, const std::string& parameter)
+{
+ SetModeParam(mode->GetModeChar(), parameter);
+}
+
+std::string Channel::GetModeParameter(char mode)
+{
+ CustomModeList::iterator n = custom_mode_params.find(mode);
+ if (n != custom_mode_params.end())
+ return n->second;
+ return "";
+}
+
+std::string Channel::GetModeParameter(ModeHandler* mode)
+{
+ CustomModeList::iterator n = custom_mode_params.find(mode->GetModeChar());
+ if (n != custom_mode_params.end())
+ return n->second;
+ return "";
+}
+
+int Channel::SetTopic(User *u, std::string &ntopic, bool forceset)
+{
+ if (!u)
+ u = ServerInstance->FakeClient;
+ if (IS_LOCAL(u) && !forceset)
+ {
+ ModResult res;
+ FIRST_MOD_RESULT(OnPreTopicChange, res, (u,this,ntopic));
+
+ if (res == MOD_RES_DENY)
+ return CMD_FAILURE;
+ if (res != MOD_RES_ALLOW)
+ {
+ if (!this->HasUser(u))
+ {
+ u->WriteNumeric(442, "%s %s :You're not on that channel!",u->nick.c_str(), this->name.c_str());
+ return CMD_FAILURE;
+ }
+ if (IsModeSet('t') && !ServerInstance->OnCheckExemption(u,this,"topiclock").check(GetPrefixValue(u) >= HALFOP_VALUE))
+ {
+ 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());
+ return CMD_FAILURE;
+ }
+ }
+ }
+
+ this->topic.assign(ntopic, 0, ServerInstance->Config->Limits.MaxTopic);
+ if (u)
+ {
+ this->setby.assign(ServerInstance->Config->FullHostInTopic ? u->GetFullHost() : u->nick, 0, 128);
+ this->WriteChannel(u, "TOPIC %s :%s", this->name.c_str(), this->topic.c_str());
+ }
+ else
+ {
+ this->setby.assign(ServerInstance->Config->ServerName);
+ this->WriteChannelWithServ(ServerInstance->Config->ServerName, "TOPIC %s :%s", this->name.c_str(), this->topic.c_str());
+ }
+
+ this->topicset = ServerInstance->Time();
+
+ FOREACH_MOD(I_OnPostTopicChange,OnPostTopicChange(u, this, this->topic));
+
+ return CMD_SUCCESS;
+}
+
+long Channel::GetUserCounter()
+{
+ return userlist.size();
+}
+
+Membership* Channel::AddUser(User* user)
+{
+ Membership*& memb = userlist[user];
+ if (memb)
+ return NULL;
+
+ memb = new Membership(user, this);
+ return memb;
+}
+
+void Channel::DelUser(User* user)
+{
+ UserMembIter a = userlist.find(user);
+
+ if (a != userlist.end())
+ {
+ a->second->cull();
+ delete a->second;
+ userlist.erase(a);
+ }
+
+ if (userlist.empty())
+ {
+ ModResult res;
+ FIRST_MOD_RESULT(OnChannelPreDelete, res, (this));
+ if (res == MOD_RES_DENY)
+ return;
+ 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);
+ }
+
+ ClearInvites();
+ ServerInstance->GlobalCulls.AddItem(this);
+ }
+}
+
+bool Channel::HasUser(User* user)
+{
+ return (userlist.find(user) != userlist.end());
+}
+
+Membership* Channel::GetUser(User* user)
+{
+ UserMembIter i = userlist.find(user);
+ if (i == userlist.end())
+ return NULL;
+ return i->second;
+}
+
+const UserMembList* Channel::GetUsers()
+{
+ return &userlist;
+}
+
+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->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
+ */
+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 MaxChans channels.
+ * We restrict local operators to OperMaxChans channels.
+ * This is a lot more logical than how it was formerly. -- w00t
+ */
+ if (!override)
+ {
+ if (user->HasPrivPermission("channels/high-join-limit"))
+ {
+ if (user->chans.size() >= ServerInstance->Config->OperMaxChans)
+ {
+ user->WriteNumeric(ERR_TOOMANYCHANNELS, "%s %s :You are on too many channels",user->nick.c_str(), cname.c_str());
+ return NULL;
+ }
+ }
+ else
+ {
+ unsigned int maxchans = user->GetClass()->maxchans;
+ if (!maxchans)
+ maxchans = ServerInstance->Config->MaxChans;
+ if (user->chans.size() >= maxchans)
+ {
+ user->WriteNumeric(ERR_TOOMANYCHANNELS, "%s %s :You are on too many channels",user->nick.c_str(), cname.c_str());
+ 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 = "o";
+
+ 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 modes +k, +i, +l and bans,
+ // in this order.
+ // If a module explicitly allowed the join (by returning MOD_RES_ALLOW),
+ // then this entire section is skipped
+ if (MOD_RESULT == MOD_RES_PASSTHRU)
+ {
+ std::string ckey = chan->GetModeParameter('k');
+ bool invited = user->IsInvited(chan);
+ bool can_bypass = ServerInstance->Config->InvBypassModes && invited;
+
+ if (!ckey.empty())
+ {
+ FIRST_MOD_RESULT(OnCheckKey, MOD_RESULT, (user, chan, key));
+ if (!MOD_RESULT.check((ckey == key) || can_bypass))
+ {
+ // If no key provided, or key is not the right one, and can't bypass +k (not invited or option not enabled)
+ user->WriteNumeric(ERR_BADCHANNELKEY, "%s %s :Cannot join channel (Incorrect channel key)",user->nick.c_str(), chan->name.c_str());
+ return NULL;
+ }
+ }
+
+ if (chan->IsModeSet('i'))
+ {
+ FIRST_MOD_RESULT(OnCheckInvite, MOD_RESULT, (user, chan));
+ if (!MOD_RESULT.check(invited))
+ {
+ user->WriteNumeric(ERR_INVITEONLYCHAN, "%s %s :Cannot join channel (Invite only)",user->nick.c_str(), chan->name.c_str());
+ return NULL;
+ }
+ }
+
+ std::string limit = chan->GetModeParameter('l');
+ if (!limit.empty())
+ {
+ FIRST_MOD_RESULT(OnCheckLimit, MOD_RESULT, (user, chan));
+ if (!MOD_RESULT.check((chan->GetUserCounter() < atol(limit.c_str()) || can_bypass)))
+ {
+ user->WriteNumeric(ERR_CHANNELISFULL, "%s %s :Cannot join channel (Channel is full)",user->nick.c_str(), chan->name.c_str());
+ return NULL;
+ }
+ }
+
+ if (chan->IsBanned(user) && !can_bypass)
+ {
+ user->WriteNumeric(ERR_BANNEDFROMCHAN, "%s %s :Cannot join channel (You're banned)",user->nick.c_str(), chan->name.c_str());
+ return NULL;
+ }
+
+ /*
+ * If the user has invites for this channel, remove them now
+ * after a successful join so they don't build up.
+ */
+ if (invited)
+ {
+ user->RemoveInvite(chan);
+ }
+ }
+ }
+ }
+
+ // 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;
+}
+
+void Channel::ForceJoin(User* user, const std::string* privs, bool bursting, bool created_by_local)
+{
+ Membership* memb = this->AddUser(user);
+ if (!memb)
+ return; // Already on the channel
+
+ if (IS_SERVER(user))
+ {
+ ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "Attempted to join server user " + user->uuid + " to channel " + this->name);
+ return;
+ }
+
+ user->chans.insert(this);
+
+ if (privs)
+ {
+ // If the user was granted prefix modes (in the OnUserPreJoin hook, or he's a
+ // remote user and his own server set the modes), then set them internally now
+ memb->modes = *privs;
+ for (std::string::const_iterator i = privs->begin(); i != privs->end(); ++i)
+ {
+ ModeHandler* mh = ServerInstance->Modes->FindMode(*i, MODETYPE_CHANNEL);
+ if (mh)
+ {
+ std::string nick = user->nick;
+ /* Set, and make sure that the mode handler knows this mode was now set */
+ this->SetPrefix(user, mh->GetModeChar(), true);
+ mh->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, 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(I_OnUserJoin,OnUserJoin(memb, bursting, created_by_local, except_list));
+
+ this->WriteAllExcept(user, false, 0, except_list, "JOIN :%s", this->name.c_str());
+
+ /* Theyre not the first ones in here, make sure everyone else sees the modes we gave the user */
+ if ((GetUserCounter() > 1) && (!memb->modes.empty()))
+ {
+ std::string ms = memb->modes;
+ for(unsigned int i=0; i < memb->modes.length(); i++)
+ ms.append(" ").append(user->nick);
+
+ except_list.insert(user);
+ this->WriteAllExcept(user, !ServerInstance->Config->CycleHostsFromUser, 0, except_list, "MODE %s +%s", this->name.c_str(), ms.c_str());
+ }
+
+ if (IS_LOCAL(user))
+ {
+ if (this->topicset)
+ {
+ user->WriteNumeric(RPL_TOPIC, "%s %s :%s", user->nick.c_str(), this->name.c_str(), this->topic.c_str());
+ user->WriteNumeric(RPL_TOPICTIME, "%s %s %s %lu", user->nick.c_str(), this->name.c_str(), this->setby.c_str(), (unsigned long)this->topicset);
+ }
+ this->UserList(user);
+ }
+
+ FOREACH_MOD(I_OnPostJoin,OnPostJoin(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);
+ 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;
+
+ char tomatch[MAXBUF];
+ snprintf(tomatch, MAXBUF, "%s!%s", user->nick.c_str(), user->ident.c_str());
+ std::string prefix = mask.substr(0, at);
+ if (InspIRCd::Match(tomatch, prefix, NULL))
+ {
+ std::string suffix = mask.substr(at + 1);
+ if (InspIRCd::Match(user->host, suffix, NULL) ||
+ InspIRCd::Match(user->dhost, 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);
+ 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 MOD_RES_DENY;
+ }
+ }
+ return MOD_RES_PASSTHRU;
+}
+
+/* Channel::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 Channel.
+ */
+void Channel::PartUser(User *user, std::string &reason)
+{
+ if (!user)
+ return;
+
+ Membership* memb = GetUser(user);
+
+ if (memb)
+ {
+ CUList except_list;
+ FOREACH_MOD(I_OnUserPart,OnUserPart(memb, reason, except_list));
+
+ WriteAllExcept(user, false, 0, except_list, "PART %s%s%s", this->name.c_str(), reason.empty() ? "" : " :", reason.c_str());
+
+ user->chans.erase(this);
+ this->RemoveAllPrefixes(user);
+ }
+
+ this->DelUser(user);
+}
+
+void Channel::KickUser(User *src, User *user, const char* reason)
+{
+ if (!src || !user || !reason)
+ return;
+
+ Membership* memb = GetUser(user);
+ if (IS_LOCAL(src))
+ {
+ if (!memb)
+ {
+ 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());
+ return;
+ }
+ if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
+ {
+ 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());
+ return;
+ }
+
+ ModResult res;
+ if (ServerInstance->ULine(src->server))
+ res = MOD_RES_ALLOW;
+ else
+ FIRST_MOD_RESULT(OnUserPreKick, res, (src,memb,reason));
+
+ if (res == MOD_RES_DENY)
+ return;
+
+ if (res == MOD_RES_PASSTHRU)
+ {
+ unsigned int them = this->GetPrefixValue(src);
+ unsigned int req = HALFOP_VALUE;
+ for (std::string::size_type i = 0; i < memb->modes.length(); i++)
+ {
+ ModeHandler* mh = ServerInstance->Modes->FindMode(memb->modes[i], MODETYPE_CHANNEL);
+ if (mh && mh->GetLevelRequired() > req)
+ req = mh->GetLevelRequired();
+ }
+
+ if (them < req)
+ {
+ src->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s %s :You must be a channel %soperator",
+ src->nick.c_str(), this->name.c_str(), req > HALFOP_VALUE ? "" : "half-");
+ return;
+ }
+ }
+ }
+
+ if (memb)
+ {
+ CUList except_list;
+ FOREACH_MOD(I_OnUserKick,OnUserKick(src, memb, reason, except_list));
+
+ WriteAllExcept(src, false, 0, except_list, "KICK %s %s :%s", name.c_str(), user->nick.c_str(), reason);
+
+ user->chans.erase(this);
+ this->RemoveAllPrefixes(user);
+ }
+
+ this->DelUser(user);
+}
+
+void Channel::WriteChannel(User* user, const 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 Channel::WriteChannel(User* user, const std::string &text)
+{
+ char tb[MAXBUF];
+
+ if (!user)
+ return;
+
+ snprintf(tb,MAXBUF,":%s %s", user->GetFullHost().c_str(), text.c_str());
+ std::string out = tb;
+
+ for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
+ {
+ if (IS_LOCAL(i->first))
+ i->first->Write(out);
+ }
+}
+
+void Channel::WriteChannelWithServ(const std::string& 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 Channel::WriteChannelWithServ(const std::string& ServName, const std::string &text)
+{
+ char tb[MAXBUF];
+
+ snprintf(tb,MAXBUF,":%s %s", ServName.empty() ? ServerInstance->Config->ServerName.c_str() : ServName.c_str(), text.c_str());
+ std::string out = tb;
+
+ for (UserMembIter i = userlist.begin(); i != userlist.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 Channel::WriteAllExceptSender(User* user, bool serversource, char status, 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->WriteAllExceptSender(user, serversource, status, std::string(textbuffer));
+}
+
+void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const char* text, ...)
+{
+ char textbuffer[MAXBUF];
+ va_list argsPtr;
+
+ if (!text)
+ return;
+
+ int offset = snprintf(textbuffer,MAXBUF,":%s ", serversource ? ServerInstance->Config->ServerName.c_str() : user->GetFullHost().c_str());
+
+ va_start(argsPtr, text);
+ vsnprintf(textbuffer + offset, MAXBUF - offset, text, argsPtr);
+ va_end(argsPtr);
+
+ this->RawWriteAllExcept(user, serversource, status, except_list, std::string(textbuffer));
+}
+
+void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string &text)
+{
+ char tb[MAXBUF];
+
+ snprintf(tb,MAXBUF,":%s %s", serversource ? ServerInstance->Config->ServerName.c_str() : user->GetFullHost().c_str(), text.c_str());
+ std::string out = tb;
+
+ this->RawWriteAllExcept(user, serversource, status, except_list, std::string(tb));
+}
+
+void Channel::RawWriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string &out)
+{
+ unsigned int minrank = 0;
+ if (status)
+ {
+ ModeHandler* mh = ServerInstance->Modes->FindPrefix(status);
+ if (mh)
+ minrank = mh->GetPrefixRank();
+ }
+ for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
+ {
+ if (IS_LOCAL(i->first) && (except_list.find(i->first) == except_list.end()))
+ {
+ /* User doesn't have the status we're after */
+ if (minrank && i->second->getRank() < minrank)
+ continue;
+
+ i->first->Write(out);
+ }
+ }
+}
+
+void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const std::string& text)
+{
+ CUList except_list;
+ except_list.insert(user);
+ 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 Channel::CountInvisible()
+{
+ int count = 0;
+ for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
+ {
+ if (!i->first->quitting && !i->first->IsModeSet('i'))
+ count++;
+ }
+
+ return count;
+}
+
+char* Channel::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, Channel::modes is only 64 elements -- Om */
+ for(int n = 0; n < 64; n++)
+ {
+ if(this->modes[n])
+ {
+ *offset++ = n + 65;
+ extparam.clear();
+ if (n == 'k' - 65 && !showkey)
+ {
+ extparam = "<key>";
+ }
+ else
+ {
+ extparam = this->GetModeParameter(n + 65);
+ }
+ 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 Channel::UserList(User *user)
+{
+ char list[MAXBUF];
+ size_t dlen, curlen;
+
+ if (!IS_LOCAL(user))
+ return;
+
+ if (this->IsModeSet('s') && !this->HasUser(user) && !user->HasPrivPermission("channels/auspex"))
+ {
+ user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nick/channel",user->nick.c_str(), this->name.c_str());
+ return;
+ }
+
+ dlen = curlen = snprintf(list,MAXBUF,"%s %c %s :", user->nick.c_str(), this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=', this->name.c_str());
+
+ int numusers = 0;
+ char* ptr = list + dlen;
+
+ /* Improvement by Brain - this doesnt change in value, so why was it inside
+ * the loop?
+ */
+ bool has_user = this->HasUser(user);
+
+ for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
+ {
+ if (i->first->quitting)
+ continue;
+ if ((!has_user) && (i->first->IsModeSet('i')))
+ {
+ /*
+ * user is +i, and source not on the channel, does not show
+ * nick in NAMES list
+ */
+ continue;
+ }
+
+ std::string prefixlist = this->GetPrefixChar(i->first);
+ std::string nick = i->first->nick;
+
+ FOREACH_MOD(I_OnNamesListItem, OnNamesListItem(user, i->second, prefixlist, nick));
+
+ /* Nick was nuked, a module wants us to skip it */
+ if (nick.empty())
+ continue;
+
+ size_t ptrlen = 0;
+
+ if (curlen + prefixlist.length() + nick.length() + 1 > 480)
+ {
+ /* list overflowed into multiple numerics */
+ user->WriteNumeric(RPL_NAMREPLY, std::string(list));
+
+ /* reset our lengths */
+ dlen = curlen = snprintf(list,MAXBUF,"%s %c %s :", user->nick.c_str(), this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=', this->name.c_str());
+ ptr = list + dlen;
+
+ numusers = 0;
+ }
+
+ ptrlen = snprintf(ptr, MAXBUF, "%s%s ", prefixlist.c_str(), nick.c_str());
+
+ curlen += ptrlen;
+ ptr += ptrlen;
+
+ numusers++;
+ }
+
+ /* if whats left in the list isnt empty, send it */
+ if (numusers)
+ {
+ user->WriteNumeric(RPL_NAMREPLY, std::string(list));
+ }
+
+ user->WriteNumeric(RPL_ENDOFNAMES, "%s %s :End of /NAMES list.", user->nick.c_str(), this->name.c_str());
+}
+
+/* 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* Channel::GetPrefixChar(User *user)
+{
+ static char pf[2] = {0, 0};
+ *pf = 0;
+ unsigned int bestrank = 0;
+
+ UserMembIter m = userlist.find(user);
+ if (m != userlist.end())
+ {
+ for(unsigned int i=0; i < m->second->modes.length(); i++)
+ {
+ char mchar = m->second->modes[i];
+ ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
+ if (mh && mh->GetPrefixRank() > bestrank && mh->GetPrefix())
+ {
+ bestrank = mh->GetPrefixRank();
+ pf[0] = mh->GetPrefix();
+ }
+ }
+ }
+ return pf;
+}
+
+unsigned int Membership::getRank()
+{
+ char mchar = modes.c_str()[0];
+ unsigned int rv = 0;
+ if (mchar)
+ {
+ ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
+ if (mh)
+ rv = mh->GetPrefixRank();
+ }
+ return rv;
+}
+
+const char* Channel::GetAllPrefixChars(User* user)
+{
+ static char prefix[64];
+ int ctr = 0;
+
+ UserMembIter m = userlist.find(user);
+ if (m != userlist.end())
+ {
+ for(unsigned int i=0; i < m->second->modes.length(); i++)
+ {
+ char mchar = m->second->modes[i];
+ ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
+ if (mh && mh->GetPrefix())
+ prefix[ctr++] = mh->GetPrefix();
+ }
+ }
+ prefix[ctr] = 0;
+
+ return prefix;
+}
+
+unsigned int Channel::GetPrefixValue(User* user)
+{
+ UserMembIter m = userlist.find(user);
+ if (m == userlist.end())
+ return 0;
+ return m->second->getRank();
+}
+
+bool Channel::SetPrefix(User* user, char prefix, bool adding)
+{
+ ModeHandler* delta_mh = ServerInstance->Modes->FindMode(prefix, MODETYPE_CHANNEL);
+ if (!delta_mh)
+ return false;
+ UserMembIter m = userlist.find(user);
+ if (m == userlist.end())
+ return false;
+ for(unsigned int i=0; i < m->second->modes.length(); i++)
+ {
+ char mchar = m->second->modes[i];
+ ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
+ if (mh && mh->GetPrefixRank() <= delta_mh->GetPrefixRank())
+ {
+ m->second->modes =
+ m->second->modes.substr(0,i) +
+ (adding ? std::string(1, prefix) : "") +
+ m->second->modes.substr(mchar == prefix ? i+1 : i);
+ return adding != (mchar == prefix);
+ }
+ }
+ if (adding)
+ m->second->modes += std::string(1, prefix);
+ return adding;
+}
+
+void Channel::RemoveAllPrefixes(User* user)
+{
+ UserMembIter m = userlist.find(user);
+ if (m != userlist.end())
+ {
+ m->second->modes.clear();
+ }
+}
+
+void Invitation::Create(Channel* c, LocalUser* u, time_t timeout)
+{
+ if ((timeout != 0) && (ServerInstance->Time() >= timeout))
+ // Expired, don't bother
+ return;
+
+ ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Create chan=%s user=%s", c->name.c_str(), u->uuid.c_str());
+
+ Invitation* inv = Invitation::Find(c, u, false);
+ if (inv)
+ {
+ if ((inv->expiry == 0) || (inv->expiry > timeout))
+ return;
+ inv->expiry = timeout;
+ ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Create changed expiry in existing invitation %p", (void*) inv);
+ }
+ else
+ {
+ inv = new Invitation(c, u, timeout);
+ c->invites.push_back(inv);
+ u->invites.push_back(inv);
+ ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Create created new invitation %p", (void*) inv);
+ }
+}
+
+Invitation* Invitation::Find(Channel* c, LocalUser* u, bool check_expired)
+{
+ 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);
+ if (!u || u->invites.empty())
+ return NULL;
+
+ InviteList locallist;
+ locallist.swap(u->invites);
+
+ Invitation* result = NULL;
+ for (InviteList::iterator i = locallist.begin(); i != locallist.end(); )
+ {
+ Invitation* inv = *i;
+ if ((check_expired) && (inv->expiry != 0) && (inv->expiry <= ServerInstance->Time()))
+ {
+ /* Expired invite, remove it. */
+ std::string expiration = ServerInstance->TimeString(inv->expiry);
+ ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Find ecountered expired entry: %p expired %s", (void*) inv, expiration.c_str());
+ i = locallist.erase(i);
+ inv->cull();
+ delete inv;
+ }
+ else
+ {
+ /* Is it what we're searching for? */
+ if (inv->chan == c)
+ {
+ result = inv;
+ break;
+ }
+ ++i;
+ }
+ }
+
+ locallist.swap(u->invites);
+ ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Find result=%p", (void*) result);
+ return result;
+}
+
+Invitation::~Invitation()
+{
+ // Remove this entry from both lists
+ InviteList::iterator it = std::find(chan->invites.begin(), chan->invites.end(), this);
+ if (it != chan->invites.end())
+ chan->invites.erase(it);
+ it = std::find(user->invites.begin(), user->invites.end(), this);
+ if (it != user->invites.end())
+ user->invites.erase(it);
+}
+
+void InviteBase::ClearInvites()
+{
+ ServerInstance->Logs->Log("INVITEBASE", LOG_DEBUG, "InviteBase::ClearInvites %p", (void*) this);
+ InviteList locallist;
+ locallist.swap(invites);
+ for (InviteList::const_iterator i = locallist.begin(); i != locallist.end(); ++i)
+ {
+ (*i)->cull();
+ delete *i;
+ }
+}