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, const std::string &name, 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 " + name);
27 (*(ServerInstance->chanlist))[name.c_str()] = this;
28 strlcpy(this->name, name.c_str(), CHANMAX);
29 this->created = ts ? ts : ServerInstance->Time(true);
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->Log(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 Ptr->SetPrefix(user, status, mh->GetPrefixRank(), true);
383 /* Make sure that the mode handler knows this mode was now set */
384 mh->OnModeChange(Instance->FakeClient, Instance->FakeClient, Ptr, nick, true);
386 switch (mh->GetPrefix())
388 /* These logic ops are SAFE IN THIS CASE
389 * because if the entry doesnt exist,
390 * addressing operator[] creates it.
391 * If they do exist, it points to it.
392 * At all other times where we dont want
393 * to create an item if it doesnt exist, we
394 * must stick to ::find().
397 user->chans[Ptr] |= UCMODE_OP;
400 user->chans[Ptr] |= UCMODE_HOP;
403 user->chans[Ptr] |= UCMODE_VOICE;
409 FOREACH_MOD_I(Instance,I_OnUserJoin,OnUserJoin(user, Ptr, bursting, silent));
412 Ptr->WriteChannel(user,"JOIN :%s",Ptr->name);
414 /* Theyre not the first ones in here, make sure everyone else sees the modes we gave the user */
415 std::string ms = Instance->Modes->ModeString(user, Ptr);
416 if ((Ptr->GetUserCounter() > 1) && (ms.length()))
417 Ptr->WriteAllExceptSender(user, true, 0, "MODE %s +%s", Ptr->name, ms.c_str());
419 /* Major improvement by Brain - we dont need to be calculating all this pointlessly for remote users */
424 user->WriteServ("332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
425 user->WriteServ("333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
429 FOREACH_MOD_I(Instance,I_OnPostJoin,OnPostJoin(user, Ptr));
433 bool Channel::IsBanned(User* user)
437 FOREACH_RESULT(I_OnCheckBan,OnCheckBan(user, this));
440 snprintf(mask, MAXBUF, "%s!%s@%s", user->nick, user->ident, user->GetIPString());
441 for (BanList::iterator i = this->bans.begin(); i != this->bans.end(); i++)
443 /* This allows CIDR ban matching
445 * Full masked host Full unmasked host IP with/without CIDR
447 if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match(mask, i->data, true)))
457 * remove a channel from a users record, and return the number of users left.
458 * Therefore, if this function returns 0 the caller should delete the Channel.
460 long Channel::PartUser(User *user, const char* reason)
465 return this->GetUserCounter();
467 UCListIter i = user->chans.find(this);
468 if (i != user->chans.end())
470 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, reason ? reason : "", silent));
473 this->WriteChannel(user, "PART %s%s%s", this->name, reason ? " :" : "", reason ? reason : "");
475 user->chans.erase(i);
476 this->RemoveAllPrefixes(user);
479 if (!this->DelUser(user)) /* if there are no users left on the channel... */
481 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
482 /* kill the record */
483 if (iter != ServerInstance->chanlist->end())
486 FOREACH_RESULT_I(ServerInstance,I_OnChannelPreDelete, OnChannelPreDelete(this));
488 return 1; // delete halted by module
489 FOREACH_MOD(I_OnChannelDelete, OnChannelDelete(this));
490 ServerInstance->chanlist->erase(iter);
495 return this->GetUserCounter();
498 long Channel::ServerKickUser(User* user, const char* reason, bool triggerevents)
502 if (!user || !reason)
503 return this->GetUserCounter();
507 if (!this->HasUser(user))
510 return this->GetUserCounter();
516 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL, user, this, reason, silent));
519 UCListIter i = user->chans.find(this);
520 if (i != user->chans.end())
523 this->WriteChannelWithServ(ServerInstance->Config->ServerName, "KICK %s %s :%s", this->name, user->nick, reason);
525 user->chans.erase(i);
526 this->RemoveAllPrefixes(user);
529 if (!this->DelUser(user))
531 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
532 /* kill the record */
533 if (iter != ServerInstance->chanlist->end())
536 FOREACH_RESULT_I(ServerInstance,I_OnChannelPreDelete, OnChannelPreDelete(this));
538 return 1; // delete halted by module
539 FOREACH_MOD(I_OnChannelDelete, OnChannelDelete(this));
540 ServerInstance->chanlist->erase(iter);
545 return this->GetUserCounter();
548 long Channel::KickUser(User *src, User *user, const char* reason)
552 if (!src || !user || !reason)
553 return this->GetUserCounter();
557 if (!this->HasUser(user))
559 src->WriteServ("441 %s %s %s :They are not on that channel",src->nick, user->nick, this->name);
560 return this->GetUserCounter();
562 if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
564 src->WriteServ("482 %s %s :Only a u-line may kick a u-line from a channel.",src->nick, this->name);
565 return this->GetUserCounter();
569 if (!ServerInstance->ULine(src->server))
572 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,this,reason));
574 return this->GetUserCounter();
576 /* Set to -1 by OnUserPreKick if explicit allow was set */
577 if (MOD_RESULT != -1)
579 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,this,AC_KICK));
580 if ((MOD_RESULT == ACR_DENY) && (!ServerInstance->ULine(src->server)))
581 return this->GetUserCounter();
583 if ((MOD_RESULT == ACR_DEFAULT) || (!ServerInstance->ULine(src->server)))
585 int them = this->GetStatus(src);
586 int us = this->GetStatus(user);
587 if ((them < STATUS_HOP) || (them < us))
589 src->WriteServ("482 %s %s :You must be a channel %soperator",src->nick, this->name, them == STATUS_HOP ? "" : "half-");
590 return this->GetUserCounter();
596 FOREACH_MOD(I_OnUserKick,OnUserKick(src, user, this, reason, silent));
598 UCListIter i = user->chans.find(this);
599 if (i != user->chans.end())
601 /* zap it from the channel list of the user */
603 this->WriteChannel(src, "KICK %s %s :%s", this->name, user->nick, reason);
605 user->chans.erase(i);
606 this->RemoveAllPrefixes(user);
609 if (!this->DelUser(user))
610 /* if there are no users left on the channel */
612 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
614 /* kill the record */
615 if (iter != ServerInstance->chanlist->end())
618 FOREACH_RESULT_I(ServerInstance,I_OnChannelPreDelete, OnChannelPreDelete(this));
620 return 1; // delete halted by module
621 FOREACH_MOD(I_OnChannelDelete, OnChannelDelete(this));
622 ServerInstance->chanlist->erase(iter);
627 return this->GetUserCounter();
630 void Channel::WriteChannel(User* user, char* text, ...)
632 char textbuffer[MAXBUF];
638 va_start(argsPtr, text);
639 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
642 this->WriteChannel(user, std::string(textbuffer));
645 void Channel::WriteChannel(User* user, const std::string &text)
647 CUList *ulist = this->GetUsers();
653 snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());
654 std::string out = tb;
656 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
658 if (IS_LOCAL(i->first))
659 i->first->Write(out);
663 void Channel::WriteChannelWithServ(const char* ServName, const char* text, ...)
665 char textbuffer[MAXBUF];
671 va_start(argsPtr, text);
672 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
675 this->WriteChannelWithServ(ServName, std::string(textbuffer));
678 void Channel::WriteChannelWithServ(const char* ServName, const std::string &text)
680 CUList *ulist = this->GetUsers();
683 snprintf(tb,MAXBUF,":%s %s",ServName ? ServName : ServerInstance->Config->ServerName, text.c_str());
684 std::string out = tb;
686 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
688 if (IS_LOCAL(i->first))
689 i->first->Write(out);
693 /* write formatted text from a source user to all users on a channel except
694 * for the sender (for privmsg etc) */
695 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, char* text, ...)
697 char textbuffer[MAXBUF];
703 va_start(argsPtr, text);
704 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
707 this->WriteAllExceptSender(user, serversource, status, std::string(textbuffer));
710 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, char* text, ...)
712 char textbuffer[MAXBUF];
718 va_start(argsPtr, text);
719 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
722 this->WriteAllExcept(user, serversource, status, except_list, std::string(textbuffer));
725 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string &text)
733 ulist = this->GetOppedUsers();
736 ulist = this->GetHalfoppedUsers();
739 ulist = this->GetVoicedUsers();
742 ulist = this->GetUsers();
746 snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());
747 std::string out = tb;
749 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
751 if ((IS_LOCAL(i->first)) && (except_list.find(i->first) == except_list.end()))
754 i->first->WriteServ(text);
756 i->first->Write(out);
761 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const std::string& text)
764 except_list[user] = user->nick;
765 this->WriteAllExcept(user, serversource, status, except_list, std::string(text));
769 * return a count of the users on a specific channel accounting for
770 * invisible users who won't increase the count. e.g. for /LIST
772 int Channel::CountInvisible()
775 CUList *ulist= this->GetUsers();
776 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
778 if (!(i->first->IsModeSet('i')))
785 char* Channel::ChanModes(bool showkey)
787 static char scratch[MAXBUF];
788 static char sparam[MAXBUF];
789 char* offset = scratch;
790 std::string extparam;
795 /* This was still iterating up to 190, Channel::modes is only 64 elements -- Om */
796 for(int n = 0; n < 64; n++)
805 extparam = (showkey ? this->key : "<key>");
808 extparam = ConvToStr(this->limit);
816 /* We know these have no parameters */
819 extparam = this->GetModeParameter(n + 65);
822 if (!extparam.empty())
824 charlcat(sparam,' ',MAXBUF);
825 strlcat(sparam,extparam.c_str(),MAXBUF);
830 /* Null terminate scratch */
832 strlcat(scratch,sparam,MAXBUF);
836 /* compile a userlist of a channel into a string, each nick seperated by
837 * spaces and op, voice etc status shown as @ and +, and send it to 'user'
839 void Channel::UserList(User *user, CUList *ulist)
848 FOREACH_RESULT(I_OnUserList,OnUserList(user, this, ulist));
851 if (MOD_RESULT != -1)
853 if ((this->IsModeSet('s')) && (!this->HasUser(user)))
855 user->WriteServ("401 %s %s :No such nick/channel",user->nick, this->name);
860 dlen = curlen = snprintf(list,MAXBUF,"353 %s %c %s :", user->nick, this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=', this->name);
863 char* ptr = list + dlen;
866 ulist = this->GetUsers();
868 /* Improvement by Brain - this doesnt change in value, so why was it inside
871 bool has_user = this->HasUser(user);
873 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
875 if ((!has_user) && (i->first->IsModeSet('i')))
878 * user is +i, and source not on the channel, does not show
884 if (i->first->Visibility && !i->first->Visibility->VisibleTo(user))
887 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", this->GetPrefixChar(i->first), i->second.c_str());
888 /* OnUserList can change this - reset it back to normal */
889 i->second = i->first->nick;
896 if (curlen > (480-NICKMAX))
898 /* list overflowed into multiple numerics */
899 user->WriteServ(std::string(list));
901 /* reset our lengths */
902 dlen = curlen = snprintf(list,MAXBUF,"353 %s %c %s :", user->nick, this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=', this->name);
910 /* if whats left in the list isnt empty, send it */
913 user->WriteServ(std::string(list));
916 user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, this->name);
919 long Channel::GetMaxBans()
921 /* Return the cached value if there is one */
923 return this->maxbans;
925 /* If there isnt one, we have to do some O(n) hax to find it the first time. (ick) */
926 for (std::map<std::string,int>::iterator n = ServerInstance->Config->maxbans.begin(); n != ServerInstance->Config->maxbans.end(); n++)
928 if (match(this->name,n->first.c_str()))
930 this->maxbans = n->second;
935 /* Screw it, just return the default of 64 */
937 return this->maxbans;
940 void Channel::ResetMaxBans()
945 /* returns the status character for a given user on a channel, e.g. @ for op,
946 * % for halfop etc. If the user has several modes set, the highest mode
947 * the user has must be returned.
949 const char* Channel::GetPrefixChar(User *user)
951 static char pf[2] = {0, 0};
953 prefixlist::iterator n = prefixes.find(user);
954 if (n != prefixes.end())
956 if (n->second.size())
958 /* If the user has any prefixes, their highest prefix
959 * will always be at the head of the list, as the list is
960 * sorted in rank order highest first (see SetPrefix()
963 *pf = n->second.begin()->first;
972 const char* Channel::GetAllPrefixChars(User* user)
974 static char prefix[MAXBUF];
978 prefixlist::iterator n = prefixes.find(user);
979 if (n != prefixes.end())
981 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
983 prefix[ctr++] = x->first;
992 unsigned int Channel::GetPrefixValue(User* user)
994 prefixlist::iterator n = prefixes.find(user);
995 if (n != prefixes.end())
997 if (n->second.size())
998 return n->second.begin()->second;
1003 int Channel::GetStatusFlags(User *user)
1005 UCListIter i = user->chans.find(this);
1006 if (i != user->chans.end())
1013 int Channel::GetStatus(User *user)
1015 if (ServerInstance->ULine(user->server))
1018 UCListIter i = user->chans.find(this);
1019 if (i != user->chans.end())
1021 if ((i->second & UCMODE_OP) > 0)
1025 if ((i->second & UCMODE_HOP) > 0)
1029 if ((i->second & UCMODE_VOICE) > 0)
1031 return STATUS_VOICE;
1033 return STATUS_NORMAL;
1035 return STATUS_NORMAL;
1038 void Channel::SetPrefix(User* user, char prefix, unsigned int prefix_value, bool adding)
1040 prefixlist::iterator n = prefixes.find(user);
1041 prefixtype pfx = std::make_pair(prefix,prefix_value);
1044 if (n != prefixes.end())
1046 if (std::find(n->second.begin(), n->second.end(), pfx) == n->second.end())
1048 n->second.push_back(pfx);
1049 /* We must keep prefixes in rank order, largest first.
1050 * This is for two reasons, firstly because x-chat *ass-u-me's* this
1051 * state, and secondly it turns out to be a benefit to us later.
1052 * See above in GetPrefix().
1054 std::sort(n->second.begin(), n->second.end(), ModeParser::PrefixComparison);
1061 prefixes.insert(std::make_pair<User*,pfxcontainer>(user, one));
1066 if (n != prefixes.end())
1068 pfxcontainer::iterator x = std::find(n->second.begin(), n->second.end(), pfx);
1069 if (x != n->second.end())
1075 void Channel::RemoveAllPrefixes(User* user)
1077 prefixlist::iterator n = prefixes.find(user);
1078 if (n != prefixes.end())