1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
16 #include "configreader.h"
22 chanrec::chanrec(InspIRCd* Instance) : ServerInstance(Instance)
24 *name = *topic = *setby = *key = 0;
25 maxbans = created = topicset = limit = 0;
27 age = ServerInstance->Time(true);
30 void chanrec::SetMode(char mode,bool mode_on)
32 modes[mode-65] = mode_on;
34 this->SetModeParam(mode,"",false);
38 void chanrec::SetModeParam(char mode,const char* parameter,bool mode_on)
40 CustomModeList::iterator n = custom_mode_params.find(mode);
44 if (n == custom_mode_params.end())
45 custom_mode_params[mode] = strdup(parameter);
49 if (n != custom_mode_params.end())
52 custom_mode_params.erase(n);
57 bool chanrec::IsModeSet(char mode)
59 return modes[mode-65];
62 std::string chanrec::GetModeParameter(char mode)
69 return ConvToStr(this->limit);
71 CustomModeList::iterator n = custom_mode_params.find(mode);
72 if (n != custom_mode_params.end())
79 long chanrec::GetUserCounter()
81 return (this->internal_userlist.size());
84 void chanrec::AddUser(userrec* user)
86 internal_userlist[user] = user->nick;
89 unsigned long chanrec::DelUser(userrec* user)
91 CUListIter a = internal_userlist.find(user);
93 if (a != internal_userlist.end())
95 internal_userlist.erase(a);
96 /* And tidy any others... */
98 DelHalfoppedUser(user);
102 return internal_userlist.size();
105 bool chanrec::HasUser(userrec* user)
107 return (internal_userlist.find(user) != internal_userlist.end());
110 void chanrec::AddOppedUser(userrec* user)
112 internal_op_userlist[user] = user->nick;
115 void chanrec::DelOppedUser(userrec* user)
117 CUListIter a = internal_op_userlist.find(user);
118 if (a != internal_op_userlist.end())
120 internal_op_userlist.erase(a);
125 void chanrec::AddHalfoppedUser(userrec* user)
127 internal_halfop_userlist[user] = user->nick;
130 void chanrec::DelHalfoppedUser(userrec* user)
132 CUListIter a = internal_halfop_userlist.find(user);
134 if (a != internal_halfop_userlist.end())
136 internal_halfop_userlist.erase(a);
140 void chanrec::AddVoicedUser(userrec* user)
142 internal_voice_userlist[user] = user->nick;
145 void chanrec::DelVoicedUser(userrec* user)
147 CUListIter a = internal_voice_userlist.find(user);
149 if (a != internal_voice_userlist.end())
151 internal_voice_userlist.erase(a);
155 CUList* chanrec::GetUsers()
157 return &internal_userlist;
160 CUList* chanrec::GetOppedUsers()
162 return &internal_op_userlist;
165 CUList* chanrec::GetHalfoppedUsers()
167 return &internal_halfop_userlist;
170 CUList* chanrec::GetVoicedUsers()
172 return &internal_voice_userlist;
175 void chanrec::SetDefaultModes()
177 irc::spacesepstream list(ServerInstance->Config->DefaultModes);
179 std::string parameter;
181 list.GetToken(modeseq);
183 userrec* dummyuser = new userrec(ServerInstance);
184 dummyuser->SetFd(FD_MAGIC_NUMBER);
186 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
188 ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
191 if (mode->GetNumParams(true))
192 list.GetToken(parameter);
196 mode->OnModeChange(dummyuser, dummyuser, this, parameter, true);
204 * add a channel to a user, creating the record for it if needed and linking
205 * it to the user record
207 chanrec* chanrec::JoinUser(InspIRCd* Instance, userrec *user, const char* cn, bool override, const char* key, time_t TS)
218 * We don't restrict the number of channels that remote users or users that are override-joining may be in.
219 * We restrict local users to MaxChans channels.
220 * We restrict local operators to OperMaxChans channels.
221 * This is a lot more logical than how it was formerly. -- w00t
223 if (IS_LOCAL(user) && !override)
225 if (user->GetMaxChans())
227 if (user->chans.size() >= user->GetMaxChans())
229 user->WriteServ("405 %s %s :You are on too many channels",user->nick, cn);
237 if (user->chans.size() >= Instance->Config->OperMaxChans)
239 user->WriteServ("405 %s %s :You are on too many channels",user->nick, cn);
245 if (user->chans.size() >= Instance->Config->MaxChans)
247 user->WriteServ("405 %s %s :You are on too many channels",user->nick, cn);
254 strlcpy(cname, cn, CHANMAX);
255 Ptr = Instance->FindChan(cname);
260 * Fix: desync bug was here, don't set @ on remote users - spanningtree handles their permissions. bug #358. -- w00t
265 Instance->Log(DEBUG,"*** BUG *** chanrec::JoinUser called for REMOTE user '%s' on channel '%s' but no TS given!", user->nick, cn);
272 if (IS_LOCAL(user) && override == false)
275 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,NULL,cname,privs));
280 /* create a new one */
281 Ptr = new chanrec(Instance);
282 (*(Instance->chanlist))[cname] = Ptr;
284 strlcpy(Ptr->name, cname,CHANMAX);
286 /* As spotted by jilles, dont bother to set this on remote users */
288 Ptr->SetDefaultModes();
290 Ptr->created = TS ? TS : Instance->Time();
291 Ptr->age = Ptr->created;
298 /* Already on the channel */
299 if (Ptr->HasUser(user))
303 * remote users are allowed us to bypass channel modes
304 * and bans (used by servers)
306 if (IS_LOCAL(user) && override == false)
309 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname,privs));
314 else if (MOD_RESULT == 0)
319 FOREACH_RESULT_I(Instance,I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
322 if ((!key) || strcmp(key,Ptr->key))
324 user->WriteServ("475 %s %s :Cannot join channel (Incorrect channel key)",user->nick, Ptr->name);
329 if (Ptr->IsModeSet('i'))
332 FOREACH_RESULT_I(Instance,I_OnCheckInvite,OnCheckInvite(user, Ptr));
335 if (!user->IsInvited(Ptr->name))
337 user->WriteServ("473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
341 user->RemoveInvite(Ptr->name);
346 FOREACH_RESULT_I(Instance,I_OnCheckLimit,OnCheckLimit(user, Ptr));
349 if (Ptr->GetUserCounter() >= Ptr->limit)
351 user->WriteServ("471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
356 if (Ptr->bans.size())
358 if (Ptr->IsBanned(user))
360 user->WriteServ("474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
368 return chanrec::ForceChan(Instance, Ptr, user, privs);
371 chanrec* chanrec::ForceChan(InspIRCd* Instance, chanrec* Ptr, userrec* user, const std::string &privs)
373 userrec* dummyuser = new userrec(Instance);
374 std::string nick = user->nick;
377 dummyuser->SetFd(FD_MAGIC_NUMBER);
380 /* Just in case they have no permissions */
381 user->chans[Ptr] = 0;
383 for (std::string::const_iterator x = privs.begin(); x != privs.end(); x++)
385 const char status = *x;
386 ModeHandler* mh = Instance->Modes->FindPrefix(status);
389 Ptr->SetPrefix(user, status, mh->GetPrefixRank(), true);
390 /* Make sure that the mode handler knows this mode was now set */
391 mh->OnModeChange(dummyuser, dummyuser, Ptr, nick, true);
393 switch (mh->GetPrefix())
395 /* These logic ops are SAFE IN THIS CASE
396 * because if the entry doesnt exist,
397 * addressing operator[] creates it.
398 * If they do exist, it points to it.
399 * At all other times where we dont want
400 * to create an item if it doesnt exist, we
401 * must stick to ::find().
404 user->chans[Ptr] |= UCMODE_OP;
407 user->chans[Ptr] |= UCMODE_HOP;
410 user->chans[Ptr] |= UCMODE_VOICE;
418 FOREACH_MOD_I(Instance,I_OnUserJoin,OnUserJoin(user, Ptr, silent));
421 Ptr->WriteChannel(user,"JOIN :%s",Ptr->name);
423 /* Theyre not the first ones in here, make sure everyone else sees the modes we gave the user */
424 std::string ms = Instance->Modes->ModeString(user, Ptr);
425 if ((Ptr->GetUserCounter() > 1) && (ms.length()))
426 Ptr->WriteAllExceptSender(user, true, 0, "MODE %s +%s", Ptr->name, ms.c_str());
428 /* Major improvement by Brain - we dont need to be calculating all this pointlessly for remote users */
433 user->WriteServ("332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
434 user->WriteServ("333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
438 FOREACH_MOD_I(Instance,I_OnPostJoin,OnPostJoin(user, Ptr));
442 bool chanrec::IsBanned(userrec* user)
446 FOREACH_RESULT(I_OnCheckBan,OnCheckBan(user, this));
449 snprintf(mask, MAXBUF, "%s!%s@%s", user->nick, user->ident, user->GetIPString());
450 for (BanList::iterator i = this->bans.begin(); i != this->bans.end(); i++)
452 /* This allows CIDR ban matching
454 * Full masked host Full unmasked host IP with/without CIDR
456 if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match(mask, i->data, true)))
466 * remove a channel from a users record, and return the number of users left.
467 * Therefore, if this function returns 0 the caller should delete the chanrec.
469 long chanrec::PartUser(userrec *user, const char* reason)
474 return this->GetUserCounter();
476 UCListIter i = user->chans.find(this);
477 if (i != user->chans.end())
479 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, reason ? reason : "", silent));
482 this->WriteChannel(user, "PART %s%s%s", this->name, reason ? " :" : "", reason ? reason : "");
484 user->chans.erase(i);
485 this->RemoveAllPrefixes(user);
488 if (!this->DelUser(user)) /* if there are no users left on the channel... */
490 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
491 /* kill the record */
492 if (iter != ServerInstance->chanlist->end())
494 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
495 ServerInstance->chanlist->erase(iter);
500 return this->GetUserCounter();
503 long chanrec::ServerKickUser(userrec* user, const char* reason, bool triggerevents)
507 if (!user || !reason)
508 return this->GetUserCounter();
512 if (!this->HasUser(user))
515 return this->GetUserCounter();
521 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL, user, this, reason, silent));
524 UCListIter i = user->chans.find(this);
525 if (i != user->chans.end())
528 this->WriteChannelWithServ(ServerInstance->Config->ServerName, "KICK %s %s :%s", this->name, user->nick, reason);
530 user->chans.erase(i);
531 this->RemoveAllPrefixes(user);
534 if (!this->DelUser(user))
536 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
537 /* kill the record */
538 if (iter != ServerInstance->chanlist->end())
540 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
541 ServerInstance->chanlist->erase(iter);
546 return this->GetUserCounter();
549 long chanrec::KickUser(userrec *src, userrec *user, const char* reason)
553 if (!src || !user || !reason)
554 return this->GetUserCounter();
558 if (!this->HasUser(user))
560 src->WriteServ("441 %s %s %s :They are not on that channel",src->nick, user->nick, this->name);
561 return this->GetUserCounter();
563 if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
565 src->WriteServ("482 %s %s :Only a u-line may kick a u-line from a channel.",src->nick, this->name);
566 return this->GetUserCounter();
570 if (!ServerInstance->ULine(src->server))
573 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,this,reason));
575 return this->GetUserCounter();
577 /* Set to -1 by OnUserPreKick if explicit allow was set */
578 if (MOD_RESULT != -1)
580 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,this,AC_KICK));
581 if ((MOD_RESULT == ACR_DENY) && (!ServerInstance->ULine(src->server)))
582 return this->GetUserCounter();
584 if ((MOD_RESULT == ACR_DEFAULT) || (!ServerInstance->ULine(src->server)))
586 int them = this->GetStatus(src);
587 int us = this->GetStatus(user);
588 if ((them < STATUS_HOP) || (them < us))
590 src->WriteServ("482 %s %s :You must be a channel %soperator",src->nick, this->name, them == STATUS_HOP ? "" : "half-");
591 return this->GetUserCounter();
597 FOREACH_MOD(I_OnUserKick,OnUserKick(src, user, this, reason, silent));
599 UCListIter i = user->chans.find(this);
600 if (i != user->chans.end())
602 /* zap it from the channel list of the user */
604 this->WriteChannel(src, "KICK %s %s :%s", this->name, user->nick, reason);
606 user->chans.erase(i);
607 this->RemoveAllPrefixes(user);
610 if (!this->DelUser(user))
611 /* if there are no users left on the channel */
613 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
615 /* kill the record */
616 if (iter != ServerInstance->chanlist->end())
618 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
619 ServerInstance->chanlist->erase(iter);
624 return this->GetUserCounter();
627 void chanrec::WriteChannel(userrec* user, char* text, ...)
629 char textbuffer[MAXBUF];
635 va_start(argsPtr, text);
636 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
639 this->WriteChannel(user, std::string(textbuffer));
642 void chanrec::WriteChannel(userrec* user, const std::string &text)
644 CUList *ulist = this->GetUsers();
650 snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());
651 std::string out = tb;
653 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
655 if (IS_LOCAL(i->first))
656 i->first->Write(out);
660 void chanrec::WriteChannelWithServ(const char* ServName, const char* text, ...)
662 char textbuffer[MAXBUF];
668 va_start(argsPtr, text);
669 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
672 this->WriteChannelWithServ(ServName, std::string(textbuffer));
675 void chanrec::WriteChannelWithServ(const char* ServName, const std::string &text)
677 CUList *ulist = this->GetUsers();
680 snprintf(tb,MAXBUF,":%s %s",ServName ? ServName : ServerInstance->Config->ServerName, text.c_str());
681 std::string out = tb;
683 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
685 if (IS_LOCAL(i->first))
686 i->first->Write(out);
690 /* write formatted text from a source user to all users on a channel except
691 * for the sender (for privmsg etc) */
692 void chanrec::WriteAllExceptSender(userrec* user, bool serversource, char status, char* text, ...)
694 char textbuffer[MAXBUF];
700 va_start(argsPtr, text);
701 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
704 this->WriteAllExceptSender(user, serversource, status, std::string(textbuffer));
707 void chanrec::WriteAllExcept(userrec* user, bool serversource, char status, CUList &except_list, char* text, ...)
709 char textbuffer[MAXBUF];
715 va_start(argsPtr, text);
716 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
719 this->WriteAllExcept(user, serversource, status, except_list, std::string(textbuffer));
722 void chanrec::WriteAllExcept(userrec* user, bool serversource, char status, CUList &except_list, const std::string &text)
730 ulist = this->GetOppedUsers();
733 ulist = this->GetHalfoppedUsers();
736 ulist = this->GetVoicedUsers();
739 ulist = this->GetUsers();
743 snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());
744 std::string out = tb;
746 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
748 if ((IS_LOCAL(i->first)) && (except_list.find(i->first) == except_list.end()))
751 i->first->WriteServ(text);
753 i->first->Write(out);
758 void chanrec::WriteAllExceptSender(userrec* user, bool serversource, char status, const std::string& text)
761 except_list[user] = user->nick;
762 this->WriteAllExcept(user, serversource, status, except_list, std::string(text));
766 * return a count of the users on a specific channel accounting for
767 * invisible users who won't increase the count. e.g. for /LIST
769 int chanrec::CountInvisible()
772 CUList *ulist= this->GetUsers();
773 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
775 if (!(i->first->IsModeSet('i')))
782 char* chanrec::ChanModes(bool showkey)
784 static char scratch[MAXBUF];
785 static char sparam[MAXBUF];
786 char* offset = scratch;
787 std::string extparam;
792 /* This was still iterating up to 190, chanrec::modes is only 64 elements -- Om */
793 for(int n = 0; n < 64; n++)
802 extparam = (showkey ? this->key : "<key>");
805 extparam = ConvToStr(this->limit);
813 /* We know these have no parameters */
816 extparam = this->GetModeParameter(n + 65);
819 if (!extparam.empty())
821 charlcat(sparam,' ',MAXBUF);
822 strlcat(sparam,extparam.c_str(),MAXBUF);
827 /* Null terminate scratch */
829 strlcat(scratch,sparam,MAXBUF);
833 /* compile a userlist of a channel into a string, each nick seperated by
834 * spaces and op, voice etc status shown as @ and +, and send it to 'user'
836 void chanrec::UserList(userrec *user, CUList *ulist)
845 FOREACH_RESULT(I_OnUserList,OnUserList(user, this, ulist));
849 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
852 char* ptr = list + dlen;
855 ulist = this->GetUsers();
857 /* Improvement by Brain - this doesnt change in value, so why was it inside
860 bool has_user = this->HasUser(user);
862 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
864 if ((!has_user) && (i->first->IsModeSet('i')))
867 * user is +i, and source not on the channel, does not show
873 if (i->first->Visibility && !i->first->Visibility->VisibleTo(user))
876 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", this->GetPrefixChar(i->first), i->second.c_str());
877 /* OnUserList can change this - reset it back to normal */
878 i->second = i->first->nick;
885 if (curlen > (480-NICKMAX))
887 /* list overflowed into multiple numerics */
888 user->WriteServ(std::string(list));
890 /* reset our lengths */
891 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
899 /* if whats left in the list isnt empty, send it */
902 user->WriteServ(std::string(list));
905 user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, this->name);
908 long chanrec::GetMaxBans()
910 /* Return the cached value if there is one */
912 return this->maxbans;
914 /* If there isnt one, we have to do some O(n) hax to find it the first time. (ick) */
915 for (std::map<std::string,int>::iterator n = ServerInstance->Config->maxbans.begin(); n != ServerInstance->Config->maxbans.end(); n++)
917 if (match(this->name,n->first.c_str()))
919 this->maxbans = n->second;
924 /* Screw it, just return the default of 64 */
926 return this->maxbans;
929 void chanrec::ResetMaxBans()
934 /* returns the status character for a given user on a channel, e.g. @ for op,
935 * % for halfop etc. If the user has several modes set, the highest mode
936 * the user has must be returned.
938 const char* chanrec::GetPrefixChar(userrec *user)
940 static char pf[2] = {0, 0};
942 prefixlist::iterator n = prefixes.find(user);
943 if (n != prefixes.end())
945 if (n->second.size())
947 /* If the user has any prefixes, their highest prefix
948 * will always be at the head of the list, as the list is
949 * sorted in rank order highest first (see SetPrefix()
952 *pf = n->second.begin()->first;
961 const char* chanrec::GetAllPrefixChars(userrec* user)
963 static char prefix[MAXBUF];
967 prefixlist::iterator n = prefixes.find(user);
968 if (n != prefixes.end())
970 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
972 prefix[ctr++] = x->first;
981 unsigned int chanrec::GetPrefixValue(userrec* user)
983 prefixlist::iterator n = prefixes.find(user);
984 if (n != prefixes.end())
986 if (n->second.size())
987 return n->second.begin()->second;
992 int chanrec::GetStatusFlags(userrec *user)
994 UCListIter i = user->chans.find(this);
995 if (i != user->chans.end())
1002 int chanrec::GetStatus(userrec *user)
1004 if (ServerInstance->ULine(user->server))
1007 UCListIter i = user->chans.find(this);
1008 if (i != user->chans.end())
1010 if ((i->second & UCMODE_OP) > 0)
1014 if ((i->second & UCMODE_HOP) > 0)
1018 if ((i->second & UCMODE_VOICE) > 0)
1020 return STATUS_VOICE;
1022 return STATUS_NORMAL;
1024 return STATUS_NORMAL;
1027 void chanrec::SetPrefix(userrec* user, char prefix, unsigned int prefix_value, bool adding)
1029 prefixlist::iterator n = prefixes.find(user);
1030 prefixtype pfx = std::make_pair(prefix,prefix_value);
1033 if (n != prefixes.end())
1035 if (std::find(n->second.begin(), n->second.end(), pfx) == n->second.end())
1037 n->second.push_back(pfx);
1038 /* We must keep prefixes in rank order, largest first.
1039 * This is for two reasons, firstly because x-chat *ass-u-me's* this
1040 * state, and secondly it turns out to be a benefit to us later.
1041 * See above in GetPrefix().
1043 std::sort(n->second.begin(), n->second.end(), ModeParser::PrefixComparison);
1050 prefixes.insert(std::make_pair<userrec*,pfxcontainer>(user, one));
1055 if (n != prefixes.end())
1057 pfxcontainer::iterator x = std::find(n->second.begin(), n->second.end(), pfx);
1058 if (x != n->second.end())
1064 void chanrec::RemoveAllPrefixes(userrec* user)
1066 prefixlist::iterator n = prefixes.find(user);
1067 if (n != prefixes.end())