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->WriteNumeric(405, "%s %s :You are on too many channels",user->nick, cn);
242 if (user->chans.size() >= Instance->Config->OperMaxChans)
244 user->WriteNumeric(405, "%s %s :You are on too many channels",user->nick, cn);
250 if (user->chans.size() >= Instance->Config->MaxChans)
252 user->WriteNumeric(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->WriteNumeric(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->WriteNumeric(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->WriteNumeric(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->WriteNumeric(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->WriteNumeric(332, "%s %s :%s", user->nick, Ptr->name, Ptr->topic);
422 user->WriteNumeric(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->WriteNumeric(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->WriteNumeric(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->WriteNumeric(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)
724 CUList *ulist = this->GetUsers();
727 snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());
728 std::string out = tb;
730 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
732 if ((IS_LOCAL(i->first)) && (except_list.find(i->first) == except_list.end()))
734 /* User doesnt have the status we're after */
735 if (status && !strchr(this->GetAllPrefixChars(i->first), status))
739 i->first->WriteServ(text);
741 i->first->Write(out);
746 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const std::string& text)
749 except_list[user] = user->nick;
750 this->WriteAllExcept(user, serversource, status, except_list, std::string(text));
754 * return a count of the users on a specific channel accounting for
755 * invisible users who won't increase the count. e.g. for /LIST
757 int Channel::CountInvisible()
760 CUList *ulist= this->GetUsers();
761 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
763 if (!(i->first->IsModeSet('i')))
770 char* Channel::ChanModes(bool showkey)
772 static char scratch[MAXBUF];
773 static char sparam[MAXBUF];
774 char* offset = scratch;
775 std::string extparam;
780 /* This was still iterating up to 190, Channel::modes is only 64 elements -- Om */
781 for(int n = 0; n < 64; n++)
790 extparam = (showkey ? this->key : "<key>");
793 extparam = ConvToStr(this->limit);
801 /* We know these have no parameters */
804 extparam = this->GetModeParameter(n + 65);
807 if (!extparam.empty())
809 charlcat(sparam,' ',MAXBUF);
810 strlcat(sparam,extparam.c_str(),MAXBUF);
815 /* Null terminate scratch */
817 strlcat(scratch,sparam,MAXBUF);
821 /* compile a userlist of a channel into a string, each nick seperated by
822 * spaces and op, voice etc status shown as @ and +, and send it to 'user'
824 void Channel::UserList(User *user, CUList *ulist)
829 bool call_modules = true;
834 FOREACH_RESULT(I_OnUserList,OnUserList(user, this, ulist));
836 call_modules = false;
838 if (MOD_RESULT != -1)
840 if ((this->IsModeSet('s')) && (!this->HasUser(user)))
842 user->WriteNumeric(401, "%s %s :No such nick/channel",user->nick, this->name);
847 dlen = curlen = snprintf(list,MAXBUF,"%s %c %s :", user->nick, this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=', this->name);
850 char* ptr = list + dlen;
853 ulist = this->GetUsers();
855 /* Improvement by Brain - this doesnt change in value, so why was it inside
858 bool has_user = this->HasUser(user);
860 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
862 if ((!has_user) && (i->first->IsModeSet('i')))
865 * user is +i, and source not on the channel, does not show
871 if (i->first->Visibility && !i->first->Visibility->VisibleTo(user))
874 std::string prefixlist = this->GetPrefixChar(i->first);
875 std::string nick = i->first->nick;
879 FOREACH_MOD(I_OnNamesListItem, OnNamesListItem(user, i->first, this, prefixlist, nick));
881 /* Nick was nuked, a module wants us to skip it */
888 if (curlen > (480-NICKMAX))
890 /* list overflowed into multiple numerics */
891 user->WriteServ(std::string(list));
893 /* reset our lengths */
894 dlen = curlen = snprintf(list,MAXBUF,"%s %c %s :", user->nick, this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=', this->name);
901 ptrlen = snprintf(ptr, MAXBUF, "%s%s ", prefixlist.c_str(), nick.c_str());
909 /* if whats left in the list isnt empty, send it */
912 user->WriteNumeric(353,std::string(list));
915 user->WriteNumeric(366, "%s %s :End of /NAMES list.", user->nick, this->name);
918 long Channel::GetMaxBans()
920 /* Return the cached value if there is one */
922 return this->maxbans;
924 /* If there isnt one, we have to do some O(n) hax to find it the first time. (ick) */
925 for (std::map<std::string,int>::iterator n = ServerInstance->Config->maxbans.begin(); n != ServerInstance->Config->maxbans.end(); n++)
927 if (match(this->name,n->first.c_str()))
929 this->maxbans = n->second;
934 /* Screw it, just return the default of 64 */
936 return this->maxbans;
939 void Channel::ResetMaxBans()
944 /* returns the status character for a given user on a channel, e.g. @ for op,
945 * % for halfop etc. If the user has several modes set, the highest mode
946 * the user has must be returned.
948 const char* Channel::GetPrefixChar(User *user)
950 static char pf[2] = {0, 0};
952 prefixlist::iterator n = prefixes.find(user);
953 if (n != prefixes.end())
955 if (n->second.size())
957 /* If the user has any prefixes, their highest prefix
958 * will always be at the head of the list, as the list is
959 * sorted in rank order highest first (see SetPrefix()
962 *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())