1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 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, const std::string &cname, time_t ts) : ServerInstance(Instance)
23 chan_hash::iterator findchan = ServerInstance->chanlist->find(name);
24 if (findchan != Instance->chanlist->end())
25 throw CoreException("Cannot create duplicate channel " + cname);
27 (*(ServerInstance->chanlist))[cname.c_str()] = this;
28 strlcpy(this->name, cname.c_str(), CHANMAX);
29 this->created = ts ? ts : ServerInstance->Time();
30 this->age = this->created;
35 *topic = *setby = *key = 0;
36 maxbans = topicset = limit = 0;
40 void Channel::SetMode(char mode,bool mode_on)
42 modes[mode-65] = mode_on;
44 this->SetModeParam(mode,"",false);
48 void Channel::SetModeParam(char mode,const char* parameter,bool mode_on)
50 CustomModeList::iterator n = custom_mode_params.find(mode);
54 if (n == custom_mode_params.end())
55 custom_mode_params[mode] = strdup(parameter);
59 if (n != custom_mode_params.end())
62 custom_mode_params.erase(n);
67 bool Channel::IsModeSet(char mode)
69 return modes[mode-65];
72 std::string Channel::GetModeParameter(char mode)
79 return ConvToStr(this->limit);
81 CustomModeList::iterator n = custom_mode_params.find(mode);
82 if (n != custom_mode_params.end())
89 long Channel::GetUserCounter()
91 return (this->internal_userlist.size());
94 void Channel::AddUser(User* user)
96 internal_userlist[user] = user->nick;
99 unsigned long Channel::DelUser(User* user)
101 CUListIter a = internal_userlist.find(user);
103 if (a != internal_userlist.end())
105 internal_userlist.erase(a);
106 /* And tidy any others... */
108 DelHalfoppedUser(user);
112 return internal_userlist.size();
115 bool Channel::HasUser(User* user)
117 return (internal_userlist.find(user) != internal_userlist.end());
120 void Channel::AddOppedUser(User* user)
122 internal_op_userlist[user] = user->nick;
125 void Channel::DelOppedUser(User* user)
127 CUListIter a = internal_op_userlist.find(user);
128 if (a != internal_op_userlist.end())
130 internal_op_userlist.erase(a);
135 void Channel::AddHalfoppedUser(User* user)
137 internal_halfop_userlist[user] = user->nick;
140 void Channel::DelHalfoppedUser(User* user)
142 CUListIter a = internal_halfop_userlist.find(user);
144 if (a != internal_halfop_userlist.end())
146 internal_halfop_userlist.erase(a);
150 void Channel::AddVoicedUser(User* user)
152 internal_voice_userlist[user] = user->nick;
155 void Channel::DelVoicedUser(User* user)
157 CUListIter a = internal_voice_userlist.find(user);
159 if (a != internal_voice_userlist.end())
161 internal_voice_userlist.erase(a);
165 CUList* Channel::GetUsers()
167 return &internal_userlist;
170 CUList* Channel::GetOppedUsers()
172 return &internal_op_userlist;
175 CUList* Channel::GetHalfoppedUsers()
177 return &internal_halfop_userlist;
180 CUList* Channel::GetVoicedUsers()
182 return &internal_voice_userlist;
185 void Channel::SetDefaultModes()
187 irc::spacesepstream list(ServerInstance->Config->DefaultModes);
189 std::string parameter;
191 list.GetToken(modeseq);
193 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
195 ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
198 if (mode->GetNumParams(true))
199 list.GetToken(parameter);
203 mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, this, parameter, true);
209 * add a channel to a user, creating the record for it if needed and linking
210 * it to the user record
212 Channel* Channel::JoinUser(InspIRCd* Instance, User *user, const char* cn, bool override, const char* key, bool bursting, time_t TS)
223 * We don't restrict the number of channels that remote users or users that are override-joining may be in.
224 * We restrict local users to MaxChans channels.
225 * We restrict local operators to OperMaxChans channels.
226 * This is a lot more logical than how it was formerly. -- w00t
228 if (IS_LOCAL(user) && !override)
230 if (user->GetMaxChans())
232 if (user->chans.size() >= user->GetMaxChans())
234 user->WriteServ("405 %s %s :You are on too many channels",user->nick, cn);
242 if (user->chans.size() >= Instance->Config->OperMaxChans)
244 user->WriteServ("405 %s %s :You are on too many channels",user->nick, cn);
250 if (user->chans.size() >= Instance->Config->MaxChans)
252 user->WriteServ("405 %s %s :You are on too many channels",user->nick, cn);
259 strlcpy(cname, cn, CHANMAX);
260 Ptr = Instance->FindChan(cname);
265 * Fix: desync bug was here, don't set @ on remote users - spanningtree handles their permissions. bug #358. -- w00t
270 Instance->Logs->Log("CHANNEL",DEBUG,"*** BUG *** Channel::JoinUser called for REMOTE user '%s' on channel '%s' but no TS given!", user->nick, cn);
277 if (IS_LOCAL(user) && override == false)
280 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,NULL,cname,privs));
285 Ptr = new Channel(Instance, cname, TS);
289 /* Already on the channel */
290 if (Ptr->HasUser(user))
294 * remote users are allowed us to bypass channel modes
295 * and bans (used by servers)
297 if (IS_LOCAL(user) && override == false)
300 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname,privs));
305 else if (MOD_RESULT == 0)
310 FOREACH_RESULT_I(Instance,I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
313 if ((!key) || strcmp(key,Ptr->key))
315 user->WriteServ("475 %s %s :Cannot join channel (Incorrect channel key)",user->nick, Ptr->name);
320 if (Ptr->IsModeSet('i'))
323 FOREACH_RESULT_I(Instance,I_OnCheckInvite,OnCheckInvite(user, Ptr));
326 if (!user->IsInvited(Ptr->name))
328 user->WriteServ("473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
332 user->RemoveInvite(Ptr->name);
337 FOREACH_RESULT_I(Instance,I_OnCheckLimit,OnCheckLimit(user, Ptr));
340 if (Ptr->GetUserCounter() >= Ptr->limit)
342 user->WriteServ("471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
347 if (Ptr->bans.size())
349 if (Ptr->IsBanned(user))
351 user->WriteServ("474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
359 /* As spotted by jilles, dont bother to set this on remote users */
360 if (IS_LOCAL(user) && Ptr->GetUserCounter() == 1)
361 Ptr->SetDefaultModes();
363 return Channel::ForceChan(Instance, Ptr, user, privs, bursting);
366 Channel* Channel::ForceChan(InspIRCd* Instance, Channel* Ptr, User* user, const std::string &privs, bool bursting)
368 std::string nick = user->nick;
373 /* Just in case they have no permissions */
374 user->chans[Ptr] = 0;
376 for (std::string::const_iterator x = privs.begin(); x != privs.end(); x++)
378 const char status = *x;
379 ModeHandler* mh = Instance->Modes->FindPrefix(status);
382 /* Set, and make sure that the mode handler knows this mode was now set */
383 Ptr->SetPrefix(user, status, mh->GetPrefixRank(), true);
384 mh->OnModeChange(Instance->FakeClient, Instance->FakeClient, Ptr, nick, true);
386 switch (mh->GetPrefix())
388 /* These logic ops are SAFE IN THIS CASE because if the entry doesnt exist,
389 * addressing operator[] creates it. If they do exist, it points to it.
390 * At all other times where we dont want to create an item if it doesnt exist, we
391 * must stick to ::find().
394 user->chans[Ptr] |= UCMODE_OP;
397 user->chans[Ptr] |= UCMODE_HOP;
400 user->chans[Ptr] |= UCMODE_VOICE;
406 FOREACH_MOD_I(Instance,I_OnUserJoin,OnUserJoin(user, Ptr, bursting, silent));
409 Ptr->WriteChannel(user,"JOIN :%s",Ptr->name);
411 /* Theyre not the first ones in here, make sure everyone else sees the modes we gave the user */
412 std::string ms = Instance->Modes->ModeString(user, Ptr);
413 if ((Ptr->GetUserCounter() > 1) && (ms.length()))
414 Ptr->WriteAllExceptSender(user, true, 0, "MODE %s +%s", Ptr->name, ms.c_str());
416 /* Major improvement by Brain - we dont need to be calculating all this pointlessly for remote users */
421 user->WriteServ("332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
422 user->WriteServ("333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
426 FOREACH_MOD_I(Instance,I_OnPostJoin,OnPostJoin(user, Ptr));
430 bool Channel::IsBanned(User* user)
434 FOREACH_RESULT(I_OnCheckBan,OnCheckBan(user, this));
437 snprintf(mask, MAXBUF, "%s!%s@%s", user->nick, user->ident, user->GetIPString());
438 for (BanList::iterator i = this->bans.begin(); i != this->bans.end(); i++)
440 /* This allows CIDR ban matching
442 * Full masked host Full unmasked host IP with/without CIDR
444 if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match(mask, i->data, true)))
454 * remove a channel from a users record, and return the number of users left.
455 * Therefore, if this function returns 0 the caller should delete the Channel.
457 long Channel::PartUser(User *user, const char* reason)
462 return this->GetUserCounter();
464 UCListIter i = user->chans.find(this);
465 if (i != user->chans.end())
467 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, reason ? reason : "", silent));
470 this->WriteChannel(user, "PART %s%s%s", this->name, reason ? " :" : "", reason ? reason : "");
472 user->chans.erase(i);
473 this->RemoveAllPrefixes(user);
476 if (!this->DelUser(user)) /* if there are no users left on the channel... */
478 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
479 /* kill the record */
480 if (iter != ServerInstance->chanlist->end())
483 FOREACH_RESULT_I(ServerInstance,I_OnChannelPreDelete, OnChannelPreDelete(this));
485 return 1; // delete halted by module
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())
533 FOREACH_RESULT_I(ServerInstance,I_OnChannelPreDelete, OnChannelPreDelete(this));
535 return 1; // delete halted by module
536 FOREACH_MOD(I_OnChannelDelete, OnChannelDelete(this));
537 ServerInstance->chanlist->erase(iter);
542 return this->GetUserCounter();
545 long Channel::KickUser(User *src, User *user, const char* reason)
549 if (!src || !user || !reason)
550 return this->GetUserCounter();
554 if (!this->HasUser(user))
556 src->WriteServ("441 %s %s %s :They are not on that channel",src->nick, user->nick, this->name);
557 return this->GetUserCounter();
559 if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
561 src->WriteServ("482 %s %s :Only a u-line may kick a u-line from a channel.",src->nick, this->name);
562 return this->GetUserCounter();
566 if (!ServerInstance->ULine(src->server))
569 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,this,reason));
571 return this->GetUserCounter();
573 /* Set to -1 by OnUserPreKick if explicit allow was set */
574 if (MOD_RESULT != -1)
576 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,this,AC_KICK));
577 if ((MOD_RESULT == ACR_DENY) && (!ServerInstance->ULine(src->server)))
578 return this->GetUserCounter();
580 if ((MOD_RESULT == ACR_DEFAULT) || (!ServerInstance->ULine(src->server)))
582 int them = this->GetStatus(src);
583 int us = this->GetStatus(user);
584 if ((them < STATUS_HOP) || (them < us))
586 src->WriteServ("482 %s %s :You must be a channel %soperator",src->nick, this->name, them == STATUS_HOP ? "" : "half-");
587 return this->GetUserCounter();
593 FOREACH_MOD(I_OnUserKick,OnUserKick(src, user, this, reason, silent));
595 UCListIter i = user->chans.find(this);
596 if (i != user->chans.end())
598 /* zap it from the channel list of the user */
600 this->WriteChannel(src, "KICK %s %s :%s", this->name, user->nick, reason);
602 user->chans.erase(i);
603 this->RemoveAllPrefixes(user);
606 if (!this->DelUser(user))
607 /* if there are no users left on the channel */
609 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
611 /* kill the record */
612 if (iter != ServerInstance->chanlist->end())
615 FOREACH_RESULT_I(ServerInstance,I_OnChannelPreDelete, OnChannelPreDelete(this));
617 return 1; // delete halted by module
618 FOREACH_MOD(I_OnChannelDelete, OnChannelDelete(this));
619 ServerInstance->chanlist->erase(iter);
624 return this->GetUserCounter();
627 void Channel::WriteChannel(User* user, const 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 Channel::WriteChannel(User* 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 Channel::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 Channel::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 Channel::WriteAllExceptSender(User* user, bool serversource, char status, const 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 Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const 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 Channel::WriteAllExcept(User* 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 Channel::WriteAllExceptSender(User* 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 Channel::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* Channel::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, Channel::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 Channel::UserList(User *user, CUList *ulist)
845 FOREACH_RESULT(I_OnUserList,OnUserList(user, this, ulist));
848 if (MOD_RESULT != -1)
850 if ((this->IsModeSet('s')) && (!this->HasUser(user)))
852 user->WriteServ("401 %s %s :No such nick/channel",user->nick, this->name);
857 dlen = curlen = snprintf(list,MAXBUF,"353 %s %c %s :", user->nick, this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=', this->name);
860 char* ptr = list + dlen;
863 ulist = this->GetUsers();
865 /* Improvement by Brain - this doesnt change in value, so why was it inside
868 bool has_user = this->HasUser(user);
870 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
872 if ((!has_user) && (i->first->IsModeSet('i')))
875 * user is +i, and source not on the channel, does not show
881 if (i->first->Visibility && !i->first->Visibility->VisibleTo(user))
884 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", this->GetPrefixChar(i->first), i->second.c_str());
885 /* OnUserList can change this - reset it back to normal */
886 i->second = i->first->nick;
893 if (curlen > (480-NICKMAX))
895 /* list overflowed into multiple numerics */
896 user->WriteServ(std::string(list));
898 /* reset our lengths */
899 dlen = curlen = snprintf(list,MAXBUF,"353 %s %c %s :", user->nick, this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=', this->name);
907 /* if whats left in the list isnt empty, send it */
910 user->WriteServ(std::string(list));
913 user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, this->name);
916 long Channel::GetMaxBans()
918 /* Return the cached value if there is one */
920 return this->maxbans;
922 /* If there isnt one, we have to do some O(n) hax to find it the first time. (ick) */
923 for (std::map<std::string,int>::iterator n = ServerInstance->Config->maxbans.begin(); n != ServerInstance->Config->maxbans.end(); n++)
925 if (match(this->name,n->first.c_str()))
927 this->maxbans = n->second;
932 /* Screw it, just return the default of 64 */
934 return this->maxbans;
937 void Channel::ResetMaxBans()
942 /* returns the status character for a given user on a channel, e.g. @ for op,
943 * % for halfop etc. If the user has several modes set, the highest mode
944 * the user has must be returned.
946 const char* Channel::GetPrefixChar(User *user)
948 static char pf[2] = {0, 0};
950 prefixlist::iterator n = prefixes.find(user);
951 if (n != prefixes.end())
953 if (n->second.size())
955 /* If the user has any prefixes, their highest prefix
956 * will always be at the head of the list, as the list is
957 * sorted in rank order highest first (see SetPrefix()
960 *pf = n->second.begin()->first;
969 const char* Channel::GetAllPrefixChars(User* user)
971 static char prefix[MAXBUF];
975 prefixlist::iterator n = prefixes.find(user);
976 if (n != prefixes.end())
978 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
980 prefix[ctr++] = x->first;
989 unsigned int Channel::GetPrefixValue(User* user)
991 prefixlist::iterator n = prefixes.find(user);
992 if (n != prefixes.end())
994 if (n->second.size())
995 return n->second.begin()->second;
1000 int Channel::GetStatusFlags(User *user)
1002 UCListIter i = user->chans.find(this);
1003 if (i != user->chans.end())
1010 int Channel::GetStatus(User *user)
1012 if (ServerInstance->ULine(user->server))
1015 UCListIter i = user->chans.find(this);
1016 if (i != user->chans.end())
1018 if ((i->second & UCMODE_OP) > 0)
1022 if ((i->second & UCMODE_HOP) > 0)
1026 if ((i->second & UCMODE_VOICE) > 0)
1028 return STATUS_VOICE;
1030 return STATUS_NORMAL;
1032 return STATUS_NORMAL;
1035 void Channel::SetPrefix(User* user, char prefix, unsigned int prefix_value, bool adding)
1037 prefixlist::iterator n = prefixes.find(user);
1038 prefixtype pfx = std::make_pair(prefix,prefix_value);
1041 if (n != prefixes.end())
1043 if (std::find(n->second.begin(), n->second.end(), pfx) == n->second.end())
1045 n->second.push_back(pfx);
1046 /* We must keep prefixes in rank order, largest first.
1047 * This is for two reasons, firstly because x-chat *ass-u-me's* this
1048 * state, and secondly it turns out to be a benefit to us later.
1049 * See above in GetPrefix().
1051 std::sort(n->second.begin(), n->second.end(), ModeParser::PrefixComparison);
1058 prefixes.insert(std::make_pair<User*,pfxcontainer>(user, one));
1063 if (n != prefixes.end())
1065 pfxcontainer::iterator x = std::find(n->second.begin(), n->second.end(), pfx);
1066 if (x != n->second.end())
1072 void Channel::RemoveAllPrefixes(User* user)
1074 prefixlist::iterator n = prefixes.find(user);
1075 if (n != prefixes.end())