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);
178 std::string modeseq = list.GetToken();
179 std::string parameter;
180 userrec* dummyuser = new userrec(ServerInstance);
181 dummyuser->SetFd(FD_MAGIC_NUMBER);
183 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
185 ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
188 if (mode->GetNumParams(true))
189 parameter = list.GetToken().c_str();
193 mode->OnModeChange(dummyuser, dummyuser, this, parameter, true);
201 * add a channel to a user, creating the record for it if needed and linking
202 * it to the user record
204 chanrec* chanrec::JoinUser(InspIRCd* Instance, userrec *user, const char* cn, bool override, const char* key, time_t TS)
215 * We don't restrict the number of channels that remote users or users that are override-joining may be in.
216 * We restrict local users to MaxChans channels.
217 * We restrict local operators to OperMaxChans channels.
218 * This is a lot more logical than how it was formerly. -- w00t
220 if (IS_LOCAL(user) && !override)
224 if (user->chans.size() >= Instance->Config->OperMaxChans)
226 user->WriteServ("405 %s %s :You are on too many channels",user->nick, cn);
232 if (user->chans.size() >= Instance->Config->MaxChans)
234 user->WriteServ("405 %s %s :You are on too many channels",user->nick, cn);
240 strlcpy(cname, cn, CHANMAX);
241 Ptr = Instance->FindChan(cname);
246 * Fix: desync bug was here, don't set @ on remote users - spanningtree handles their permissions. bug #358. -- w00t
251 Instance->Log(DEBUG,"*** BUG *** chanrec::JoinUser called for REMOTE user '%s' on channel '%s' but no TS given!", user->nick, cn);
258 if (IS_LOCAL(user) && override == false)
261 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,NULL,cname,privs));
266 /* create a new one */
267 Ptr = new chanrec(Instance);
268 (*(Instance->chanlist))[cname] = Ptr;
270 strlcpy(Ptr->name, cname,CHANMAX);
272 /* As spotted by jilles, dont bother to set this on remote users */
274 Ptr->SetDefaultModes();
276 Ptr->created = TS ? TS : Instance->Time();
277 Ptr->age = Ptr->created;
284 /* Already on the channel */
285 if (Ptr->HasUser(user))
289 * remote users are allowed us to bypass channel modes
290 * and bans (used by servers)
292 if (IS_LOCAL(user) && override == false)
295 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname,privs));
300 else if (MOD_RESULT == 0)
305 FOREACH_RESULT_I(Instance,I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
308 if ((!key) || strcmp(key,Ptr->key))
310 user->WriteServ("475 %s %s :Cannot join channel (Incorrect channel key)",user->nick, Ptr->name);
315 if (Ptr->IsModeSet('i'))
318 FOREACH_RESULT_I(Instance,I_OnCheckInvite,OnCheckInvite(user, Ptr));
321 if (!user->IsInvited(Ptr->name))
323 user->WriteServ("473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
327 user->RemoveInvite(Ptr->name);
332 FOREACH_RESULT_I(Instance,I_OnCheckLimit,OnCheckLimit(user, Ptr));
335 if (Ptr->GetUserCounter() >= Ptr->limit)
337 user->WriteServ("471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
342 if (Ptr->bans.size())
344 if (Ptr->IsBanned(user))
346 user->WriteServ("474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
354 return chanrec::ForceChan(Instance, Ptr, user, privs);
357 chanrec* chanrec::ForceChan(InspIRCd* Instance, chanrec* Ptr, userrec* user, const std::string &privs)
359 userrec* dummyuser = new userrec(Instance);
360 std::string nick = user->nick;
363 dummyuser->SetFd(FD_MAGIC_NUMBER);
366 /* Just in case they have no permissions */
367 user->chans[Ptr] = 0;
369 for (std::string::const_iterator x = privs.begin(); x != privs.end(); x++)
371 const char status = *x;
372 ModeHandler* mh = Instance->Modes->FindPrefix(status);
375 Ptr->SetPrefix(user, status, mh->GetPrefixRank(), true);
376 /* Make sure that the mode handler knows this mode was now set */
377 mh->OnModeChange(dummyuser, dummyuser, Ptr, nick, true);
379 switch (mh->GetPrefix())
381 /* These logic ops are SAFE IN THIS CASE
382 * because if the entry doesnt exist,
383 * addressing operator[] creates it.
384 * If they do exist, it points to it.
385 * At all other times where we dont want
386 * to create an item if it doesnt exist, we
387 * must stick to ::find().
390 user->chans[Ptr] |= UCMODE_OP;
393 user->chans[Ptr] |= UCMODE_HOP;
396 user->chans[Ptr] |= UCMODE_VOICE;
404 FOREACH_MOD_I(Instance,I_OnUserJoin,OnUserJoin(user, Ptr, silent));
407 Ptr->WriteChannel(user,"JOIN :%s",Ptr->name);
409 /* Theyre not the first ones in here, make sure everyone else sees the modes we gave the user */
410 std::string ms = Instance->Modes->ModeString(user, Ptr);
411 if ((Ptr->GetUserCounter() > 1) && (ms.length()))
412 Ptr->WriteAllExceptSender(user, true, 0, "MODE %s +%s", Ptr->name, ms.c_str());
414 /* Major improvement by Brain - we dont need to be calculating all this pointlessly for remote users */
419 user->WriteServ("332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
420 user->WriteServ("333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
424 FOREACH_MOD_I(Instance,I_OnPostJoin,OnPostJoin(user, Ptr));
428 bool chanrec::IsBanned(userrec* user)
432 FOREACH_RESULT(I_OnCheckBan,OnCheckBan(user, this));
435 snprintf(mask, MAXBUF, "%s!%s@%s", user->nick, user->ident, user->GetIPString());
436 for (BanList::iterator i = this->bans.begin(); i != this->bans.end(); i++)
438 /* This allows CIDR ban matching
440 * Full masked host Full unmasked host IP with/without CIDR
442 if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match(mask, i->data, true)))
452 * remove a channel from a users record, and return the number of users left.
453 * Therefore, if this function returns 0 the caller should delete the chanrec.
455 long chanrec::PartUser(userrec *user, const char* reason)
460 return this->GetUserCounter();
462 UCListIter i = user->chans.find(this);
463 if (i != user->chans.end())
465 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, reason ? reason : "", silent));
468 this->WriteChannel(user, "PART %s%s%s", this->name, reason ? " :" : "", reason ? reason : "");
470 user->chans.erase(i);
471 this->RemoveAllPrefixes(user);
474 if (!this->DelUser(user)) /* if there are no users left on the channel... */
476 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
477 /* kill the record */
478 if (iter != ServerInstance->chanlist->end())
480 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
481 ServerInstance->chanlist->erase(iter);
486 return this->GetUserCounter();
489 long chanrec::ServerKickUser(userrec* user, const char* reason, bool triggerevents)
493 if (!user || !reason)
494 return this->GetUserCounter();
498 if (!this->HasUser(user))
501 return this->GetUserCounter();
507 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL, user, this, reason, silent));
510 UCListIter i = user->chans.find(this);
511 if (i != user->chans.end())
514 this->WriteChannelWithServ(ServerInstance->Config->ServerName, "KICK %s %s :%s", this->name, user->nick, reason);
516 user->chans.erase(i);
517 this->RemoveAllPrefixes(user);
520 if (!this->DelUser(user))
522 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
523 /* kill the record */
524 if (iter != ServerInstance->chanlist->end())
526 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
527 ServerInstance->chanlist->erase(iter);
532 return this->GetUserCounter();
535 long chanrec::KickUser(userrec *src, userrec *user, const char* reason)
539 if (!src || !user || !reason)
540 return this->GetUserCounter();
544 if (!this->HasUser(user))
546 src->WriteServ("441 %s %s %s :They are not on that channel",src->nick, user->nick, this->name);
547 return this->GetUserCounter();
549 if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
551 src->WriteServ("482 %s %s :Only a u-line may kick a u-line from a channel.",src->nick, this->name);
552 return this->GetUserCounter();
556 if (!ServerInstance->ULine(src->server))
559 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,this,reason));
561 return this->GetUserCounter();
563 /* Set to -1 by OnUserPreKick if explicit allow was set */
564 if (MOD_RESULT != -1)
566 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,this,AC_KICK));
567 if ((MOD_RESULT == ACR_DENY) && (!ServerInstance->ULine(src->server)))
568 return this->GetUserCounter();
570 if ((MOD_RESULT == ACR_DEFAULT) || (!ServerInstance->ULine(src->server)))
572 int them = this->GetStatus(src);
573 int us = this->GetStatus(user);
574 if ((them < STATUS_HOP) || (them < us))
576 src->WriteServ("482 %s %s :You must be a channel %soperator",src->nick, this->name, them == STATUS_HOP ? "" : "half-");
577 return this->GetUserCounter();
583 FOREACH_MOD(I_OnUserKick,OnUserKick(src, user, this, reason, silent));
585 UCListIter i = user->chans.find(this);
586 if (i != user->chans.end())
588 /* zap it from the channel list of the user */
590 this->WriteChannel(src, "KICK %s %s :%s", this->name, user->nick, reason);
592 user->chans.erase(i);
593 this->RemoveAllPrefixes(user);
596 if (!this->DelUser(user))
597 /* if there are no users left on the channel */
599 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
601 /* kill the record */
602 if (iter != ServerInstance->chanlist->end())
604 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
605 ServerInstance->chanlist->erase(iter);
610 return this->GetUserCounter();
613 void chanrec::WriteChannel(userrec* user, char* text, ...)
615 char textbuffer[MAXBUF];
621 va_start(argsPtr, text);
622 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
625 this->WriteChannel(user, std::string(textbuffer));
628 void chanrec::WriteChannel(userrec* user, const std::string &text)
630 CUList *ulist = this->GetUsers();
636 snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());
637 std::string out = tb;
639 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
641 if (IS_LOCAL(i->first))
642 i->first->Write(out);
646 void chanrec::WriteChannelWithServ(const char* ServName, const char* text, ...)
648 char textbuffer[MAXBUF];
654 va_start(argsPtr, text);
655 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
658 this->WriteChannelWithServ(ServName, std::string(textbuffer));
661 void chanrec::WriteChannelWithServ(const char* ServName, const std::string &text)
663 CUList *ulist = this->GetUsers();
666 snprintf(tb,MAXBUF,":%s %s",ServName ? ServName : ServerInstance->Config->ServerName, text.c_str());
667 std::string out = tb;
669 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
671 if (IS_LOCAL(i->first))
672 i->first->Write(out);
676 /* write formatted text from a source user to all users on a channel except
677 * for the sender (for privmsg etc) */
678 void chanrec::WriteAllExceptSender(userrec* user, bool serversource, char status, char* text, ...)
680 char textbuffer[MAXBUF];
686 va_start(argsPtr, text);
687 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
690 this->WriteAllExceptSender(user, serversource, status, std::string(textbuffer));
693 void chanrec::WriteAllExcept(userrec* user, bool serversource, char status, CUList &except_list, char* text, ...)
695 char textbuffer[MAXBUF];
701 va_start(argsPtr, text);
702 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
705 this->WriteAllExcept(user, serversource, status, except_list, std::string(textbuffer));
708 void chanrec::WriteAllExcept(userrec* user, bool serversource, char status, CUList &except_list, const std::string &text)
716 ulist = this->GetOppedUsers();
719 ulist = this->GetHalfoppedUsers();
722 ulist = this->GetVoicedUsers();
725 ulist = this->GetUsers();
729 snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());
730 std::string out = tb;
732 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
734 if ((IS_LOCAL(i->first)) && (except_list.find(i->first) == except_list.end()))
737 i->first->WriteServ(text);
739 i->first->Write(out);
744 void chanrec::WriteAllExceptSender(userrec* user, bool serversource, char status, const std::string& text)
747 except_list[user] = user->nick;
748 this->WriteAllExcept(user, serversource, status, except_list, std::string(text));
752 * return a count of the users on a specific channel accounting for
753 * invisible users who won't increase the count. e.g. for /LIST
755 int chanrec::CountInvisible()
758 CUList *ulist= this->GetUsers();
759 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
761 if (!(i->first->IsModeSet('i')))
768 char* chanrec::ChanModes(bool showkey)
770 static char scratch[MAXBUF];
771 static char sparam[MAXBUF];
772 char* offset = scratch;
773 std::string extparam;
778 /* This was still iterating up to 190, chanrec::modes is only 64 elements -- Om */
779 for(int n = 0; n < 64; n++)
788 extparam = (showkey ? this->key : "<key>");
791 extparam = ConvToStr(this->limit);
799 /* We know these have no parameters */
802 extparam = this->GetModeParameter(n + 65);
805 if (!extparam.empty())
807 charlcat(sparam,' ',MAXBUF);
808 strlcat(sparam,extparam.c_str(),MAXBUF);
813 /* Null terminate scratch */
815 strlcat(scratch,sparam,MAXBUF);
819 /* compile a userlist of a channel into a string, each nick seperated by
820 * spaces and op, voice etc status shown as @ and +, and send it to 'user'
822 void chanrec::UserList(userrec *user, CUList *ulist)
831 FOREACH_RESULT(I_OnUserList,OnUserList(user, this, ulist));
835 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
838 char* ptr = list + dlen;
841 ulist = this->GetUsers();
843 /* Improvement by Brain - this doesnt change in value, so why was it inside
846 bool has_user = this->HasUser(user);
848 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
850 if ((!has_user) && (i->first->IsModeSet('i')))
853 * user is +i, and source not on the channel, does not show
859 if (i->first->Visibility && !i->first->Visibility->VisibleTo(user))
862 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", this->GetPrefixChar(i->first), i->second.c_str());
863 /* OnUserList can change this - reset it back to normal */
864 i->second = i->first->nick;
871 if (curlen > (480-NICKMAX))
873 /* list overflowed into multiple numerics */
874 user->WriteServ(std::string(list));
876 /* reset our lengths */
877 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
885 /* if whats left in the list isnt empty, send it */
888 user->WriteServ(std::string(list));
891 user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, this->name);
894 long chanrec::GetMaxBans()
896 /* Return the cached value if there is one */
898 return this->maxbans;
900 /* If there isnt one, we have to do some O(n) hax to find it the first time. (ick) */
901 for (std::map<std::string,int>::iterator n = ServerInstance->Config->maxbans.begin(); n != ServerInstance->Config->maxbans.end(); n++)
903 if (match(this->name,n->first.c_str()))
905 this->maxbans = n->second;
910 /* Screw it, just return the default of 64 */
912 return this->maxbans;
915 void chanrec::ResetMaxBans()
920 /* returns the status character for a given user on a channel, e.g. @ for op,
921 * % for halfop etc. If the user has several modes set, the highest mode
922 * the user has must be returned.
924 const char* chanrec::GetPrefixChar(userrec *user)
926 static char pf[2] = {0, 0};
928 prefixlist::iterator n = prefixes.find(user);
929 if (n != prefixes.end())
931 if (n->second.size())
933 /* If the user has any prefixes, their highest prefix
934 * will always be at the head of the list, as the list is
935 * sorted in rank order highest first (see SetPrefix()
938 *pf = n->second.begin()->first;
947 const char* chanrec::GetAllPrefixChars(userrec* user)
949 static char prefix[MAXBUF];
953 prefixlist::iterator n = prefixes.find(user);
954 if (n != prefixes.end())
956 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
958 prefix[ctr++] = x->first;
967 unsigned int chanrec::GetPrefixValue(userrec* user)
969 prefixlist::iterator n = prefixes.find(user);
970 if (n != prefixes.end())
972 if (n->second.size())
973 return n->second.begin()->second;
978 int chanrec::GetStatusFlags(userrec *user)
980 UCListIter i = user->chans.find(this);
981 if (i != user->chans.end())
988 int chanrec::GetStatus(userrec *user)
990 if (ServerInstance->ULine(user->server))
993 UCListIter i = user->chans.find(this);
994 if (i != user->chans.end())
996 if ((i->second & UCMODE_OP) > 0)
1000 if ((i->second & UCMODE_HOP) > 0)
1004 if ((i->second & UCMODE_VOICE) > 0)
1006 return STATUS_VOICE;
1008 return STATUS_NORMAL;
1010 return STATUS_NORMAL;
1013 void chanrec::SetPrefix(userrec* user, char prefix, unsigned int prefix_value, bool adding)
1015 prefixlist::iterator n = prefixes.find(user);
1016 prefixtype pfx = std::make_pair(prefix,prefix_value);
1019 if (n != prefixes.end())
1021 if (std::find(n->second.begin(), n->second.end(), pfx) == n->second.end())
1023 n->second.push_back(pfx);
1024 /* We must keep prefixes in rank order, largest first.
1025 * This is for two reasons, firstly because x-chat *ass-u-me's* this
1026 * state, and secondly it turns out to be a benefit to us later.
1027 * See above in GetPrefix().
1029 std::sort(n->second.begin(), n->second.end(), ModeParser::PrefixComparison);
1036 prefixes.insert(std::make_pair<userrec*,pfxcontainer>(user, one));
1041 if (n != prefixes.end())
1043 pfxcontainer::iterator x = std::find(n->second.begin(), n->second.end(), pfx);
1044 if (x != n->second.end())
1050 void chanrec::RemoveAllPrefixes(userrec* user)
1052 prefixlist::iterator n = prefixes.find(user);
1053 if (n != prefixes.end())