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, bool bursting, 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 Ptr = Channel::CreateChannel(Instance, cname, TS);
278 /* Already on the channel */
279 if (Ptr->HasUser(user))
283 * remote users are allowed us to bypass channel modes
284 * and bans (used by servers)
286 if (IS_LOCAL(user) && override == false)
289 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname,privs));
294 else if (MOD_RESULT == 0)
299 FOREACH_RESULT_I(Instance,I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
302 if ((!key) || strcmp(key,Ptr->key))
304 user->WriteServ("475 %s %s :Cannot join channel (Incorrect channel key)",user->nick, Ptr->name);
309 if (Ptr->IsModeSet('i'))
312 FOREACH_RESULT_I(Instance,I_OnCheckInvite,OnCheckInvite(user, Ptr));
315 if (!user->IsInvited(Ptr->name))
317 user->WriteServ("473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
321 user->RemoveInvite(Ptr->name);
326 FOREACH_RESULT_I(Instance,I_OnCheckLimit,OnCheckLimit(user, Ptr));
329 if (Ptr->GetUserCounter() >= Ptr->limit)
331 user->WriteServ("471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
336 if (Ptr->bans.size())
338 if (Ptr->IsBanned(user))
340 user->WriteServ("474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
348 /* As spotted by jilles, dont bother to set this on remote users */
349 if (IS_LOCAL(user) && Ptr->GetUserCounter() == 1)
350 Ptr->SetDefaultModes();
352 return Channel::ForceChan(Instance, Ptr, user, privs, bursting);
355 Channel *Channel::CreateChannel(InspIRCd *ServerInstance, const std::string &name, time_t ts)
357 /* create a new one */
358 Channel *c = new Channel(ServerInstance);
359 (*(ServerInstance->chanlist))[name.c_str()] = c;
361 strlcpy(c->name, name.c_str(), CHANMAX);
362 c->created = ts ? ts : ServerInstance->Time();
367 Channel* Channel::ForceChan(InspIRCd* Instance, Channel* Ptr, User* user, const std::string &privs, bool bursting)
369 std::string nick = user->nick;
374 /* Just in case they have no permissions */
375 user->chans[Ptr] = 0;
377 for (std::string::const_iterator x = privs.begin(); x != privs.end(); x++)
379 const char status = *x;
380 ModeHandler* mh = Instance->Modes->FindPrefix(status);
383 Ptr->SetPrefix(user, status, mh->GetPrefixRank(), true);
384 /* Make sure that the mode handler knows this mode was now set */
385 mh->OnModeChange(Instance->FakeClient, Instance->FakeClient, Ptr, nick, true);
387 switch (mh->GetPrefix())
389 /* These logic ops are SAFE IN THIS CASE
390 * because if the entry doesnt exist,
391 * addressing operator[] creates it.
392 * If they do exist, it points to it.
393 * At all other times where we dont want
394 * to create an item if it doesnt exist, we
395 * must stick to ::find().
398 user->chans[Ptr] |= UCMODE_OP;
401 user->chans[Ptr] |= UCMODE_HOP;
404 user->chans[Ptr] |= UCMODE_VOICE;
410 FOREACH_MOD_I(Instance,I_OnUserJoin,OnUserJoin(user, Ptr, bursting, silent));
413 Ptr->WriteChannel(user,"JOIN :%s",Ptr->name);
415 /* Theyre not the first ones in here, make sure everyone else sees the modes we gave the user */
416 std::string ms = Instance->Modes->ModeString(user, Ptr);
417 if ((Ptr->GetUserCounter() > 1) && (ms.length()))
418 Ptr->WriteAllExceptSender(user, true, 0, "MODE %s +%s", Ptr->name, ms.c_str());
420 /* Major improvement by Brain - we dont need to be calculating all this pointlessly for remote users */
425 user->WriteServ("332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
426 user->WriteServ("333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
430 FOREACH_MOD_I(Instance,I_OnPostJoin,OnPostJoin(user, Ptr));
434 bool Channel::IsBanned(User* user)
438 FOREACH_RESULT(I_OnCheckBan,OnCheckBan(user, this));
441 snprintf(mask, MAXBUF, "%s!%s@%s", user->nick, user->ident, user->GetIPString());
442 for (BanList::iterator i = this->bans.begin(); i != this->bans.end(); i++)
444 /* This allows CIDR ban matching
446 * Full masked host Full unmasked host IP with/without CIDR
448 if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match(mask, i->data, true)))
458 * remove a channel from a users record, and return the number of users left.
459 * Therefore, if this function returns 0 the caller should delete the Channel.
461 long Channel::PartUser(User *user, const char* reason)
466 return this->GetUserCounter();
468 UCListIter i = user->chans.find(this);
469 if (i != user->chans.end())
471 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, reason ? reason : "", silent));
474 this->WriteChannel(user, "PART %s%s%s", this->name, reason ? " :" : "", reason ? reason : "");
476 user->chans.erase(i);
477 this->RemoveAllPrefixes(user);
480 if (!this->DelUser(user)) /* if there are no users left on the channel... */
482 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
483 /* kill the record */
484 if (iter != ServerInstance->chanlist->end())
486 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
487 ServerInstance->chanlist->erase(iter);
492 return this->GetUserCounter();
495 long Channel::ServerKickUser(User* user, const char* reason, bool triggerevents)
499 if (!user || !reason)
500 return this->GetUserCounter();
504 if (!this->HasUser(user))
507 return this->GetUserCounter();
513 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL, user, this, reason, silent));
516 UCListIter i = user->chans.find(this);
517 if (i != user->chans.end())
520 this->WriteChannelWithServ(ServerInstance->Config->ServerName, "KICK %s %s :%s", this->name, user->nick, reason);
522 user->chans.erase(i);
523 this->RemoveAllPrefixes(user);
526 if (!this->DelUser(user))
528 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
529 /* kill the record */
530 if (iter != ServerInstance->chanlist->end())
532 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
533 ServerInstance->chanlist->erase(iter);
538 return this->GetUserCounter();
541 long Channel::KickUser(User *src, User *user, const char* reason)
545 if (!src || !user || !reason)
546 return this->GetUserCounter();
550 if (!this->HasUser(user))
552 src->WriteServ("441 %s %s %s :They are not on that channel",src->nick, user->nick, this->name);
553 return this->GetUserCounter();
555 if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
557 src->WriteServ("482 %s %s :Only a u-line may kick a u-line from a channel.",src->nick, this->name);
558 return this->GetUserCounter();
562 if (!ServerInstance->ULine(src->server))
565 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,this,reason));
567 return this->GetUserCounter();
569 /* Set to -1 by OnUserPreKick if explicit allow was set */
570 if (MOD_RESULT != -1)
572 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,this,AC_KICK));
573 if ((MOD_RESULT == ACR_DENY) && (!ServerInstance->ULine(src->server)))
574 return this->GetUserCounter();
576 if ((MOD_RESULT == ACR_DEFAULT) || (!ServerInstance->ULine(src->server)))
578 int them = this->GetStatus(src);
579 int us = this->GetStatus(user);
580 if ((them < STATUS_HOP) || (them < us))
582 src->WriteServ("482 %s %s :You must be a channel %soperator",src->nick, this->name, them == STATUS_HOP ? "" : "half-");
583 return this->GetUserCounter();
589 FOREACH_MOD(I_OnUserKick,OnUserKick(src, user, this, reason, silent));
591 UCListIter i = user->chans.find(this);
592 if (i != user->chans.end())
594 /* zap it from the channel list of the user */
596 this->WriteChannel(src, "KICK %s %s :%s", this->name, user->nick, reason);
598 user->chans.erase(i);
599 this->RemoveAllPrefixes(user);
602 if (!this->DelUser(user))
603 /* if there are no users left on the channel */
605 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
607 /* kill the record */
608 if (iter != ServerInstance->chanlist->end())
610 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
611 ServerInstance->chanlist->erase(iter);
616 return this->GetUserCounter();
619 void Channel::WriteChannel(User* user, char* text, ...)
621 char textbuffer[MAXBUF];
627 va_start(argsPtr, text);
628 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
631 this->WriteChannel(user, std::string(textbuffer));
634 void Channel::WriteChannel(User* user, const std::string &text)
636 CUList *ulist = this->GetUsers();
642 snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());
643 std::string out = tb;
645 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
647 if (IS_LOCAL(i->first))
648 i->first->Write(out);
652 void Channel::WriteChannelWithServ(const char* ServName, const char* text, ...)
654 char textbuffer[MAXBUF];
660 va_start(argsPtr, text);
661 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
664 this->WriteChannelWithServ(ServName, std::string(textbuffer));
667 void Channel::WriteChannelWithServ(const char* ServName, const std::string &text)
669 CUList *ulist = this->GetUsers();
672 snprintf(tb,MAXBUF,":%s %s",ServName ? ServName : ServerInstance->Config->ServerName, text.c_str());
673 std::string out = tb;
675 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
677 if (IS_LOCAL(i->first))
678 i->first->Write(out);
682 /* write formatted text from a source user to all users on a channel except
683 * for the sender (for privmsg etc) */
684 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, char* text, ...)
686 char textbuffer[MAXBUF];
692 va_start(argsPtr, text);
693 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
696 this->WriteAllExceptSender(user, serversource, status, std::string(textbuffer));
699 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, char* text, ...)
701 char textbuffer[MAXBUF];
707 va_start(argsPtr, text);
708 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
711 this->WriteAllExcept(user, serversource, status, except_list, std::string(textbuffer));
714 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string &text)
722 ulist = this->GetOppedUsers();
725 ulist = this->GetHalfoppedUsers();
728 ulist = this->GetVoicedUsers();
731 ulist = this->GetUsers();
735 snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());
736 std::string out = tb;
738 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
740 if ((IS_LOCAL(i->first)) && (except_list.find(i->first) == except_list.end()))
743 i->first->WriteServ(text);
745 i->first->Write(out);
750 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const std::string& text)
753 except_list[user] = user->nick;
754 this->WriteAllExcept(user, serversource, status, except_list, std::string(text));
758 * return a count of the users on a specific channel accounting for
759 * invisible users who won't increase the count. e.g. for /LIST
761 int Channel::CountInvisible()
764 CUList *ulist= this->GetUsers();
765 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
767 if (!(i->first->IsModeSet('i')))
774 char* Channel::ChanModes(bool showkey)
776 static char scratch[MAXBUF];
777 static char sparam[MAXBUF];
778 char* offset = scratch;
779 std::string extparam;
784 /* This was still iterating up to 190, Channel::modes is only 64 elements -- Om */
785 for(int n = 0; n < 64; n++)
794 extparam = (showkey ? this->key : "<key>");
797 extparam = ConvToStr(this->limit);
805 /* We know these have no parameters */
808 extparam = this->GetModeParameter(n + 65);
811 if (!extparam.empty())
813 charlcat(sparam,' ',MAXBUF);
814 strlcat(sparam,extparam.c_str(),MAXBUF);
819 /* Null terminate scratch */
821 strlcat(scratch,sparam,MAXBUF);
825 /* compile a userlist of a channel into a string, each nick seperated by
826 * spaces and op, voice etc status shown as @ and +, and send it to 'user'
828 void Channel::UserList(User *user, CUList *ulist)
837 FOREACH_RESULT(I_OnUserList,OnUserList(user, this, ulist));
840 if (MOD_RESULT != -1)
842 if ((this->IsModeSet('s')) && (!this->HasUser(user)))
844 user->WriteServ("401 %s %s :No such nick/channel",user->nick, this->name);
849 dlen = curlen = snprintf(list,MAXBUF,"353 %s %c %s :", user->nick, this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=', 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 %c %s :", user->nick, this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=', 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 Channel::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 Channel::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* Channel::GetPrefixChar(User *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* Channel::GetAllPrefixChars(User* 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 Channel::GetPrefixValue(User* 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 Channel::GetStatusFlags(User *user)
994 UCListIter i = user->chans.find(this);
995 if (i != user->chans.end())
1002 int Channel::GetStatus(User *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 Channel::SetPrefix(User* 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<User*,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 Channel::RemoveAllPrefixes(User* user)
1066 prefixlist::iterator n = prefixes.find(user);
1067 if (n != prefixes.end())