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 * ---------------------------------------------------
14 /* $Core: libIRCDchannels */
21 Channel::Channel(InspIRCd* Instance) : ServerInstance(Instance)
23 *name = *topic = *setby = *key = 0;
24 maxbans = created = topicset = limit = 0;
26 age = ServerInstance->Time(true);
29 void Channel::SetMode(char mode,bool mode_on)
31 modes[mode-65] = mode_on;
33 this->SetModeParam(mode,"",false);
37 void Channel::SetModeParam(char mode,const char* parameter,bool mode_on)
39 CustomModeList::iterator n = custom_mode_params.find(mode);
43 if (n == custom_mode_params.end())
44 custom_mode_params[mode] = strdup(parameter);
48 if (n != custom_mode_params.end())
51 custom_mode_params.erase(n);
56 bool Channel::IsModeSet(char mode)
58 return modes[mode-65];
61 std::string Channel::GetModeParameter(char mode)
68 return ConvToStr(this->limit);
70 CustomModeList::iterator n = custom_mode_params.find(mode);
71 if (n != custom_mode_params.end())
78 long Channel::GetUserCounter()
80 return (this->internal_userlist.size());
83 void Channel::AddUser(User* user)
85 internal_userlist[user] = user->nick;
88 unsigned long Channel::DelUser(User* user)
90 CUListIter a = internal_userlist.find(user);
92 if (a != internal_userlist.end())
94 internal_userlist.erase(a);
95 /* And tidy any others... */
97 DelHalfoppedUser(user);
101 return internal_userlist.size();
104 bool Channel::HasUser(User* user)
106 return (internal_userlist.find(user) != internal_userlist.end());
109 void Channel::AddOppedUser(User* user)
111 internal_op_userlist[user] = user->nick;
114 void Channel::DelOppedUser(User* user)
116 CUListIter a = internal_op_userlist.find(user);
117 if (a != internal_op_userlist.end())
119 internal_op_userlist.erase(a);
124 void Channel::AddHalfoppedUser(User* user)
126 internal_halfop_userlist[user] = user->nick;
129 void Channel::DelHalfoppedUser(User* user)
131 CUListIter a = internal_halfop_userlist.find(user);
133 if (a != internal_halfop_userlist.end())
135 internal_halfop_userlist.erase(a);
139 void Channel::AddVoicedUser(User* user)
141 internal_voice_userlist[user] = user->nick;
144 void Channel::DelVoicedUser(User* user)
146 CUListIter a = internal_voice_userlist.find(user);
148 if (a != internal_voice_userlist.end())
150 internal_voice_userlist.erase(a);
154 CUList* Channel::GetUsers()
156 return &internal_userlist;
159 CUList* Channel::GetOppedUsers()
161 return &internal_op_userlist;
164 CUList* Channel::GetHalfoppedUsers()
166 return &internal_halfop_userlist;
169 CUList* Channel::GetVoicedUsers()
171 return &internal_voice_userlist;
174 void Channel::SetDefaultModes()
176 irc::spacesepstream list(ServerInstance->Config->DefaultModes);
178 std::string parameter;
180 list.GetToken(modeseq);
182 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
184 ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
187 if (mode->GetNumParams(true))
188 list.GetToken(parameter);
192 mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, this, parameter, true);
198 * add a channel to a user, creating the record for it if needed and linking
199 * it to the user record
201 Channel* Channel::JoinUser(InspIRCd* Instance, User *user, const char* cn, bool override, const char* key, time_t TS)
212 * We don't restrict the number of channels that remote users or users that are override-joining may be in.
213 * We restrict local users to MaxChans channels.
214 * We restrict local operators to OperMaxChans channels.
215 * This is a lot more logical than how it was formerly. -- w00t
217 if (IS_LOCAL(user) && !override)
219 if (user->GetMaxChans())
221 if (user->chans.size() >= user->GetMaxChans())
223 user->WriteServ("405 %s %s :You are on too many channels",user->nick, cn);
231 if (user->chans.size() >= Instance->Config->OperMaxChans)
233 user->WriteServ("405 %s %s :You are on too many channels",user->nick, cn);
239 if (user->chans.size() >= Instance->Config->MaxChans)
241 user->WriteServ("405 %s %s :You are on too many channels",user->nick, cn);
248 strlcpy(cname, cn, CHANMAX);
249 Ptr = Instance->FindChan(cname);
254 * Fix: desync bug was here, don't set @ on remote users - spanningtree handles their permissions. bug #358. -- w00t
259 Instance->Log(DEBUG,"*** BUG *** Channel::JoinUser called for REMOTE user '%s' on channel '%s' but no TS given!", user->nick, cn);
266 if (IS_LOCAL(user) && override == false)
269 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,NULL,cname,privs));
274 /* create a new one */
275 Ptr = new Channel(Instance);
276 (*(Instance->chanlist))[cname] = Ptr;
278 strlcpy(Ptr->name, cname,CHANMAX);
280 /* As spotted by jilles, dont bother to set this on remote users */
282 Ptr->SetDefaultModes();
284 Ptr->created = TS ? TS : Instance->Time();
285 Ptr->age = Ptr->created;
292 /* Already on the channel */
293 if (Ptr->HasUser(user))
297 * remote users are allowed us to bypass channel modes
298 * and bans (used by servers)
300 if (IS_LOCAL(user) && override == false)
303 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname,privs));
308 else if (MOD_RESULT == 0)
313 FOREACH_RESULT_I(Instance,I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
316 if ((!key) || strcmp(key,Ptr->key))
318 user->WriteServ("475 %s %s :Cannot join channel (Incorrect channel key)",user->nick, Ptr->name);
323 if (Ptr->IsModeSet('i'))
326 FOREACH_RESULT_I(Instance,I_OnCheckInvite,OnCheckInvite(user, Ptr));
329 if (!user->IsInvited(Ptr->name))
331 user->WriteServ("473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
335 user->RemoveInvite(Ptr->name);
340 FOREACH_RESULT_I(Instance,I_OnCheckLimit,OnCheckLimit(user, Ptr));
343 if (Ptr->GetUserCounter() >= Ptr->limit)
345 user->WriteServ("471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
350 if (Ptr->bans.size())
352 if (Ptr->IsBanned(user))
354 user->WriteServ("474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
362 return Channel::ForceChan(Instance, Ptr, user, privs);
365 Channel* Channel::ForceChan(InspIRCd* Instance, Channel* Ptr, User* user, const std::string &privs)
367 std::string nick = user->nick;
372 /* Just in case they have no permissions */
373 user->chans[Ptr] = 0;
375 for (std::string::const_iterator x = privs.begin(); x != privs.end(); x++)
377 const char status = *x;
378 ModeHandler* mh = Instance->Modes->FindPrefix(status);
381 Ptr->SetPrefix(user, status, mh->GetPrefixRank(), true);
382 /* Make sure that the mode handler knows this mode was now set */
383 mh->OnModeChange(Instance->FakeClient, Instance->FakeClient, Ptr, nick, true);
385 switch (mh->GetPrefix())
387 /* These logic ops are SAFE IN THIS CASE
388 * because if the entry doesnt exist,
389 * addressing operator[] creates it.
390 * If they do exist, it points to it.
391 * At all other times where we dont want
392 * to create an item if it doesnt exist, we
393 * must stick to ::find().
396 user->chans[Ptr] |= UCMODE_OP;
399 user->chans[Ptr] |= UCMODE_HOP;
402 user->chans[Ptr] |= UCMODE_VOICE;
408 FOREACH_MOD_I(Instance,I_OnUserJoin,OnUserJoin(user, Ptr, silent));
411 Ptr->WriteChannel(user,"JOIN :%s",Ptr->name);
413 /* Theyre not the first ones in here, make sure everyone else sees the modes we gave the user */
414 std::string ms = Instance->Modes->ModeString(user, Ptr);
415 if ((Ptr->GetUserCounter() > 1) && (ms.length()))
416 Ptr->WriteAllExceptSender(user, true, 0, "MODE %s +%s", Ptr->name, ms.c_str());
418 /* Major improvement by Brain - we dont need to be calculating all this pointlessly for remote users */
423 user->WriteServ("332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
424 user->WriteServ("333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
428 FOREACH_MOD_I(Instance,I_OnPostJoin,OnPostJoin(user, Ptr));
432 bool Channel::IsBanned(User* user)
436 FOREACH_RESULT(I_OnCheckBan,OnCheckBan(user, this));
439 snprintf(mask, MAXBUF, "%s!%s@%s", user->nick, user->ident, user->GetIPString());
440 for (BanList::iterator i = this->bans.begin(); i != this->bans.end(); i++)
442 /* This allows CIDR ban matching
444 * Full masked host Full unmasked host IP with/without CIDR
446 if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match(mask, i->data, true)))
456 * remove a channel from a users record, and return the number of users left.
457 * Therefore, if this function returns 0 the caller should delete the Channel.
459 long Channel::PartUser(User *user, const char* reason)
464 return this->GetUserCounter();
466 UCListIter i = user->chans.find(this);
467 if (i != user->chans.end())
469 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, reason ? reason : "", silent));
472 this->WriteChannel(user, "PART %s%s%s", this->name, reason ? " :" : "", reason ? reason : "");
474 user->chans.erase(i);
475 this->RemoveAllPrefixes(user);
478 if (!this->DelUser(user)) /* if there are no users left on the channel... */
480 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
481 /* kill the record */
482 if (iter != ServerInstance->chanlist->end())
484 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
485 ServerInstance->chanlist->erase(iter);
490 return this->GetUserCounter();
493 long Channel::ServerKickUser(User* user, const char* reason, bool triggerevents)
497 if (!user || !reason)
498 return this->GetUserCounter();
502 if (!this->HasUser(user))
505 return this->GetUserCounter();
511 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL, user, this, reason, silent));
514 UCListIter i = user->chans.find(this);
515 if (i != user->chans.end())
518 this->WriteChannelWithServ(ServerInstance->Config->ServerName, "KICK %s %s :%s", this->name, user->nick, reason);
520 user->chans.erase(i);
521 this->RemoveAllPrefixes(user);
524 if (!this->DelUser(user))
526 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
527 /* kill the record */
528 if (iter != ServerInstance->chanlist->end())
530 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
531 ServerInstance->chanlist->erase(iter);
536 return this->GetUserCounter();
539 long Channel::KickUser(User *src, User *user, const char* reason)
543 if (!src || !user || !reason)
544 return this->GetUserCounter();
548 if (!this->HasUser(user))
550 src->WriteServ("441 %s %s %s :They are not on that channel",src->nick, user->nick, this->name);
551 return this->GetUserCounter();
553 if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
555 src->WriteServ("482 %s %s :Only a u-line may kick a u-line from a channel.",src->nick, this->name);
556 return this->GetUserCounter();
560 if (!ServerInstance->ULine(src->server))
563 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,this,reason));
565 return this->GetUserCounter();
567 /* Set to -1 by OnUserPreKick if explicit allow was set */
568 if (MOD_RESULT != -1)
570 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,this,AC_KICK));
571 if ((MOD_RESULT == ACR_DENY) && (!ServerInstance->ULine(src->server)))
572 return this->GetUserCounter();
574 if ((MOD_RESULT == ACR_DEFAULT) || (!ServerInstance->ULine(src->server)))
576 int them = this->GetStatus(src);
577 int us = this->GetStatus(user);
578 if ((them < STATUS_HOP) || (them < us))
580 src->WriteServ("482 %s %s :You must be a channel %soperator",src->nick, this->name, them == STATUS_HOP ? "" : "half-");
581 return this->GetUserCounter();
587 FOREACH_MOD(I_OnUserKick,OnUserKick(src, user, this, reason, silent));
589 UCListIter i = user->chans.find(this);
590 if (i != user->chans.end())
592 /* zap it from the channel list of the user */
594 this->WriteChannel(src, "KICK %s %s :%s", this->name, user->nick, reason);
596 user->chans.erase(i);
597 this->RemoveAllPrefixes(user);
600 if (!this->DelUser(user))
601 /* if there are no users left on the channel */
603 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
605 /* kill the record */
606 if (iter != ServerInstance->chanlist->end())
608 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
609 ServerInstance->chanlist->erase(iter);
614 return this->GetUserCounter();
617 void Channel::WriteChannel(User* user, char* text, ...)
619 char textbuffer[MAXBUF];
625 va_start(argsPtr, text);
626 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
629 this->WriteChannel(user, std::string(textbuffer));
632 void Channel::WriteChannel(User* user, const std::string &text)
634 CUList *ulist = this->GetUsers();
640 snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());
641 std::string out = tb;
643 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
645 if (IS_LOCAL(i->first))
646 i->first->Write(out);
650 void Channel::WriteChannelWithServ(const char* ServName, const char* text, ...)
652 char textbuffer[MAXBUF];
658 va_start(argsPtr, text);
659 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
662 this->WriteChannelWithServ(ServName, std::string(textbuffer));
665 void Channel::WriteChannelWithServ(const char* ServName, const std::string &text)
667 CUList *ulist = this->GetUsers();
670 snprintf(tb,MAXBUF,":%s %s",ServName ? ServName : ServerInstance->Config->ServerName, text.c_str());
671 std::string out = tb;
673 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
675 if (IS_LOCAL(i->first))
676 i->first->Write(out);
680 /* write formatted text from a source user to all users on a channel except
681 * for the sender (for privmsg etc) */
682 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, char* text, ...)
684 char textbuffer[MAXBUF];
690 va_start(argsPtr, text);
691 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
694 this->WriteAllExceptSender(user, serversource, status, std::string(textbuffer));
697 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, char* text, ...)
699 char textbuffer[MAXBUF];
705 va_start(argsPtr, text);
706 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
709 this->WriteAllExcept(user, serversource, status, except_list, std::string(textbuffer));
712 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string &text)
720 ulist = this->GetOppedUsers();
723 ulist = this->GetHalfoppedUsers();
726 ulist = this->GetVoicedUsers();
729 ulist = this->GetUsers();
733 snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());
734 std::string out = tb;
736 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
738 if ((IS_LOCAL(i->first)) && (except_list.find(i->first) == except_list.end()))
741 i->first->WriteServ(text);
743 i->first->Write(out);
748 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const std::string& text)
751 except_list[user] = user->nick;
752 this->WriteAllExcept(user, serversource, status, except_list, std::string(text));
756 * return a count of the users on a specific channel accounting for
757 * invisible users who won't increase the count. e.g. for /LIST
759 int Channel::CountInvisible()
762 CUList *ulist= this->GetUsers();
763 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
765 if (!(i->first->IsModeSet('i')))
772 char* Channel::ChanModes(bool showkey)
774 static char scratch[MAXBUF];
775 static char sparam[MAXBUF];
776 char* offset = scratch;
777 std::string extparam;
782 /* This was still iterating up to 190, Channel::modes is only 64 elements -- Om */
783 for(int n = 0; n < 64; n++)
792 extparam = (showkey ? this->key : "<key>");
795 extparam = ConvToStr(this->limit);
803 /* We know these have no parameters */
806 extparam = this->GetModeParameter(n + 65);
809 if (!extparam.empty())
811 charlcat(sparam,' ',MAXBUF);
812 strlcat(sparam,extparam.c_str(),MAXBUF);
817 /* Null terminate scratch */
819 strlcat(scratch,sparam,MAXBUF);
823 /* compile a userlist of a channel into a string, each nick seperated by
824 * spaces and op, voice etc status shown as @ and +, and send it to 'user'
826 void Channel::UserList(User *user, CUList *ulist)
835 FOREACH_RESULT(I_OnUserList,OnUserList(user, this, ulist));
839 dlen = curlen = snprintf(list,MAXBUF,"353 %s %c %s :", user->nick, this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=', this->name);
842 char* ptr = list + dlen;
845 ulist = this->GetUsers();
847 /* Improvement by Brain - this doesnt change in value, so why was it inside
850 bool has_user = this->HasUser(user);
852 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
854 if ((!has_user) && (i->first->IsModeSet('i')))
857 * user is +i, and source not on the channel, does not show
863 if (i->first->Visibility && !i->first->Visibility->VisibleTo(user))
866 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", this->GetPrefixChar(i->first), i->second.c_str());
867 /* OnUserList can change this - reset it back to normal */
868 i->second = i->first->nick;
875 if (curlen > (480-NICKMAX))
877 /* list overflowed into multiple numerics */
878 user->WriteServ(std::string(list));
880 /* reset our lengths */
881 dlen = curlen = snprintf(list,MAXBUF,"353 %s %c %s :", user->nick, this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=', this->name);
889 /* if whats left in the list isnt empty, send it */
892 user->WriteServ(std::string(list));
895 user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, this->name);
898 long Channel::GetMaxBans()
900 /* Return the cached value if there is one */
902 return this->maxbans;
904 /* If there isnt one, we have to do some O(n) hax to find it the first time. (ick) */
905 for (std::map<std::string,int>::iterator n = ServerInstance->Config->maxbans.begin(); n != ServerInstance->Config->maxbans.end(); n++)
907 if (match(this->name,n->first.c_str()))
909 this->maxbans = n->second;
914 /* Screw it, just return the default of 64 */
916 return this->maxbans;
919 void Channel::ResetMaxBans()
924 /* returns the status character for a given user on a channel, e.g. @ for op,
925 * % for halfop etc. If the user has several modes set, the highest mode
926 * the user has must be returned.
928 const char* Channel::GetPrefixChar(User *user)
930 static char pf[2] = {0, 0};
932 prefixlist::iterator n = prefixes.find(user);
933 if (n != prefixes.end())
935 if (n->second.size())
937 /* If the user has any prefixes, their highest prefix
938 * will always be at the head of the list, as the list is
939 * sorted in rank order highest first (see SetPrefix()
942 *pf = n->second.begin()->first;
951 const char* Channel::GetAllPrefixChars(User* user)
953 static char prefix[MAXBUF];
957 prefixlist::iterator n = prefixes.find(user);
958 if (n != prefixes.end())
960 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
962 prefix[ctr++] = x->first;
971 unsigned int Channel::GetPrefixValue(User* user)
973 prefixlist::iterator n = prefixes.find(user);
974 if (n != prefixes.end())
976 if (n->second.size())
977 return n->second.begin()->second;
982 int Channel::GetStatusFlags(User *user)
984 UCListIter i = user->chans.find(this);
985 if (i != user->chans.end())
992 int Channel::GetStatus(User *user)
994 if (ServerInstance->ULine(user->server))
997 UCListIter i = user->chans.find(this);
998 if (i != user->chans.end())
1000 if ((i->second & UCMODE_OP) > 0)
1004 if ((i->second & UCMODE_HOP) > 0)
1008 if ((i->second & UCMODE_VOICE) > 0)
1010 return STATUS_VOICE;
1012 return STATUS_NORMAL;
1014 return STATUS_NORMAL;
1017 void Channel::SetPrefix(User* user, char prefix, unsigned int prefix_value, bool adding)
1019 prefixlist::iterator n = prefixes.find(user);
1020 prefixtype pfx = std::make_pair(prefix,prefix_value);
1023 if (n != prefixes.end())
1025 if (std::find(n->second.begin(), n->second.end(), pfx) == n->second.end())
1027 n->second.push_back(pfx);
1028 /* We must keep prefixes in rank order, largest first.
1029 * This is for two reasons, firstly because x-chat *ass-u-me's* this
1030 * state, and secondly it turns out to be a benefit to us later.
1031 * See above in GetPrefix().
1033 std::sort(n->second.begin(), n->second.end(), ModeParser::PrefixComparison);
1040 prefixes.insert(std::make_pair<User*,pfxcontainer>(user, one));
1045 if (n != prefixes.end())
1047 pfxcontainer::iterator x = std::find(n->second.begin(), n->second.end(), pfx);
1048 if (x != n->second.end())
1054 void Channel::RemoveAllPrefixes(User* user)
1056 prefixlist::iterator n = prefixes.find(user);
1057 if (n != prefixes.end())