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 (*(ServerInstance->chanlist))[name.c_str()] = this;
24 strlcpy(this->name, name.c_str(), CHANMAX);
25 this->created = ts ? ts : ServerInstance->Time(true);
26 this->age = this->created;
31 *topic = *setby = *key = 0;
32 maxbans = topicset = limit = 0;
36 void Channel::SetMode(char mode,bool mode_on)
38 modes[mode-65] = mode_on;
40 this->SetModeParam(mode,"",false);
44 void Channel::SetModeParam(char mode,const char* parameter,bool mode_on)
46 CustomModeList::iterator n = custom_mode_params.find(mode);
50 if (n == custom_mode_params.end())
51 custom_mode_params[mode] = strdup(parameter);
55 if (n != custom_mode_params.end())
58 custom_mode_params.erase(n);
63 bool Channel::IsModeSet(char mode)
65 return modes[mode-65];
68 std::string Channel::GetModeParameter(char mode)
75 return ConvToStr(this->limit);
77 CustomModeList::iterator n = custom_mode_params.find(mode);
78 if (n != custom_mode_params.end())
85 long Channel::GetUserCounter()
87 return (this->internal_userlist.size());
90 void Channel::AddUser(User* user)
92 internal_userlist[user] = user->nick;
95 unsigned long Channel::DelUser(User* user)
97 CUListIter a = internal_userlist.find(user);
99 if (a != internal_userlist.end())
101 internal_userlist.erase(a);
102 /* And tidy any others... */
104 DelHalfoppedUser(user);
108 return internal_userlist.size();
111 bool Channel::HasUser(User* user)
113 return (internal_userlist.find(user) != internal_userlist.end());
116 void Channel::AddOppedUser(User* user)
118 internal_op_userlist[user] = user->nick;
121 void Channel::DelOppedUser(User* user)
123 CUListIter a = internal_op_userlist.find(user);
124 if (a != internal_op_userlist.end())
126 internal_op_userlist.erase(a);
131 void Channel::AddHalfoppedUser(User* user)
133 internal_halfop_userlist[user] = user->nick;
136 void Channel::DelHalfoppedUser(User* user)
138 CUListIter a = internal_halfop_userlist.find(user);
140 if (a != internal_halfop_userlist.end())
142 internal_halfop_userlist.erase(a);
146 void Channel::AddVoicedUser(User* user)
148 internal_voice_userlist[user] = user->nick;
151 void Channel::DelVoicedUser(User* user)
153 CUListIter a = internal_voice_userlist.find(user);
155 if (a != internal_voice_userlist.end())
157 internal_voice_userlist.erase(a);
161 CUList* Channel::GetUsers()
163 return &internal_userlist;
166 CUList* Channel::GetOppedUsers()
168 return &internal_op_userlist;
171 CUList* Channel::GetHalfoppedUsers()
173 return &internal_halfop_userlist;
176 CUList* Channel::GetVoicedUsers()
178 return &internal_voice_userlist;
181 void Channel::SetDefaultModes()
183 irc::spacesepstream list(ServerInstance->Config->DefaultModes);
185 std::string parameter;
187 list.GetToken(modeseq);
189 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
191 ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
194 if (mode->GetNumParams(true))
195 list.GetToken(parameter);
199 mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, this, parameter, true);
205 * add a channel to a user, creating the record for it if needed and linking
206 * it to the user record
208 Channel* Channel::JoinUser(InspIRCd* Instance, User *user, const char* cn, bool override, const char* key, bool bursting, time_t TS)
219 * We don't restrict the number of channels that remote users or users that are override-joining may be in.
220 * We restrict local users to MaxChans channels.
221 * We restrict local operators to OperMaxChans channels.
222 * This is a lot more logical than how it was formerly. -- w00t
224 if (IS_LOCAL(user) && !override)
226 if (user->GetMaxChans())
228 if (user->chans.size() >= user->GetMaxChans())
230 user->WriteServ("405 %s %s :You are on too many channels",user->nick, cn);
238 if (user->chans.size() >= Instance->Config->OperMaxChans)
240 user->WriteServ("405 %s %s :You are on too many channels",user->nick, cn);
246 if (user->chans.size() >= Instance->Config->MaxChans)
248 user->WriteServ("405 %s %s :You are on too many channels",user->nick, cn);
255 strlcpy(cname, cn, CHANMAX);
256 Ptr = Instance->FindChan(cname);
261 * Fix: desync bug was here, don't set @ on remote users - spanningtree handles their permissions. bug #358. -- w00t
266 Instance->Log(DEBUG,"*** BUG *** Channel::JoinUser called for REMOTE user '%s' on channel '%s' but no TS given!", user->nick, cn);
273 if (IS_LOCAL(user) && override == false)
276 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,NULL,cname,privs));
281 Ptr = new Channel(Instance, cname, TS);
285 /* Already on the channel */
286 if (Ptr->HasUser(user))
290 * remote users are allowed us to bypass channel modes
291 * and bans (used by servers)
293 if (IS_LOCAL(user) && override == false)
296 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname,privs));
301 else if (MOD_RESULT == 0)
306 FOREACH_RESULT_I(Instance,I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
309 if ((!key) || strcmp(key,Ptr->key))
311 user->WriteServ("475 %s %s :Cannot join channel (Incorrect channel key)",user->nick, Ptr->name);
316 if (Ptr->IsModeSet('i'))
319 FOREACH_RESULT_I(Instance,I_OnCheckInvite,OnCheckInvite(user, Ptr));
322 if (!user->IsInvited(Ptr->name))
324 user->WriteServ("473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
328 user->RemoveInvite(Ptr->name);
333 FOREACH_RESULT_I(Instance,I_OnCheckLimit,OnCheckLimit(user, Ptr));
336 if (Ptr->GetUserCounter() >= Ptr->limit)
338 user->WriteServ("471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
343 if (Ptr->bans.size())
345 if (Ptr->IsBanned(user))
347 user->WriteServ("474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
355 /* As spotted by jilles, dont bother to set this on remote users */
356 if (IS_LOCAL(user) && Ptr->GetUserCounter() == 1)
357 Ptr->SetDefaultModes();
359 return Channel::ForceChan(Instance, Ptr, user, privs, bursting);
362 Channel* Channel::ForceChan(InspIRCd* Instance, Channel* Ptr, User* user, const std::string &privs, bool bursting)
364 std::string nick = user->nick;
369 /* Just in case they have no permissions */
370 user->chans[Ptr] = 0;
372 for (std::string::const_iterator x = privs.begin(); x != privs.end(); x++)
374 const char status = *x;
375 ModeHandler* mh = Instance->Modes->FindPrefix(status);
378 Ptr->SetPrefix(user, status, mh->GetPrefixRank(), true);
379 /* Make sure that the mode handler knows this mode was now set */
380 mh->OnModeChange(Instance->FakeClient, Instance->FakeClient, Ptr, nick, true);
382 switch (mh->GetPrefix())
384 /* These logic ops are SAFE IN THIS CASE
385 * because if the entry doesnt exist,
386 * addressing operator[] creates it.
387 * If they do exist, it points to it.
388 * At all other times where we dont want
389 * to create an item if it doesnt exist, we
390 * must stick to ::find().
393 user->chans[Ptr] |= UCMODE_OP;
396 user->chans[Ptr] |= UCMODE_HOP;
399 user->chans[Ptr] |= UCMODE_VOICE;
405 FOREACH_MOD_I(Instance,I_OnUserJoin,OnUserJoin(user, Ptr, bursting, silent));
408 Ptr->WriteChannel(user,"JOIN :%s",Ptr->name);
410 /* Theyre not the first ones in here, make sure everyone else sees the modes we gave the user */
411 std::string ms = Instance->Modes->ModeString(user, Ptr);
412 if ((Ptr->GetUserCounter() > 1) && (ms.length()))
413 Ptr->WriteAllExceptSender(user, true, 0, "MODE %s +%s", Ptr->name, ms.c_str());
415 /* Major improvement by Brain - we dont need to be calculating all this pointlessly for remote users */
420 user->WriteServ("332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
421 user->WriteServ("333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
425 FOREACH_MOD_I(Instance,I_OnPostJoin,OnPostJoin(user, Ptr));
429 bool Channel::IsBanned(User* user)
433 FOREACH_RESULT(I_OnCheckBan,OnCheckBan(user, this));
436 snprintf(mask, MAXBUF, "%s!%s@%s", user->nick, user->ident, user->GetIPString());
437 for (BanList::iterator i = this->bans.begin(); i != this->bans.end(); i++)
439 /* This allows CIDR ban matching
441 * Full masked host Full unmasked host IP with/without CIDR
443 if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match(mask, i->data, true)))
453 * remove a channel from a users record, and return the number of users left.
454 * Therefore, if this function returns 0 the caller should delete the Channel.
456 long Channel::PartUser(User *user, const char* reason)
461 return this->GetUserCounter();
463 UCListIter i = user->chans.find(this);
464 if (i != user->chans.end())
466 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, reason ? reason : "", silent));
469 this->WriteChannel(user, "PART %s%s%s", this->name, reason ? " :" : "", reason ? reason : "");
471 user->chans.erase(i);
472 this->RemoveAllPrefixes(user);
475 if (!this->DelUser(user)) /* if there are no users left on the channel... */
477 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
478 /* kill the record */
479 if (iter != ServerInstance->chanlist->end())
481 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
482 ServerInstance->chanlist->erase(iter);
487 return this->GetUserCounter();
490 long Channel::ServerKickUser(User* user, const char* reason, bool triggerevents)
494 if (!user || !reason)
495 return this->GetUserCounter();
499 if (!this->HasUser(user))
502 return this->GetUserCounter();
508 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL, user, this, reason, silent));
511 UCListIter i = user->chans.find(this);
512 if (i != user->chans.end())
515 this->WriteChannelWithServ(ServerInstance->Config->ServerName, "KICK %s %s :%s", this->name, user->nick, reason);
517 user->chans.erase(i);
518 this->RemoveAllPrefixes(user);
521 if (!this->DelUser(user))
523 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
524 /* kill the record */
525 if (iter != ServerInstance->chanlist->end())
527 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
528 ServerInstance->chanlist->erase(iter);
533 return this->GetUserCounter();
536 long Channel::KickUser(User *src, User *user, const char* reason)
540 if (!src || !user || !reason)
541 return this->GetUserCounter();
545 if (!this->HasUser(user))
547 src->WriteServ("441 %s %s %s :They are not on that channel",src->nick, user->nick, this->name);
548 return this->GetUserCounter();
550 if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
552 src->WriteServ("482 %s %s :Only a u-line may kick a u-line from a channel.",src->nick, this->name);
553 return this->GetUserCounter();
557 if (!ServerInstance->ULine(src->server))
560 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,this,reason));
562 return this->GetUserCounter();
564 /* Set to -1 by OnUserPreKick if explicit allow was set */
565 if (MOD_RESULT != -1)
567 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,this,AC_KICK));
568 if ((MOD_RESULT == ACR_DENY) && (!ServerInstance->ULine(src->server)))
569 return this->GetUserCounter();
571 if ((MOD_RESULT == ACR_DEFAULT) || (!ServerInstance->ULine(src->server)))
573 int them = this->GetStatus(src);
574 int us = this->GetStatus(user);
575 if ((them < STATUS_HOP) || (them < us))
577 src->WriteServ("482 %s %s :You must be a channel %soperator",src->nick, this->name, them == STATUS_HOP ? "" : "half-");
578 return this->GetUserCounter();
584 FOREACH_MOD(I_OnUserKick,OnUserKick(src, user, this, reason, silent));
586 UCListIter i = user->chans.find(this);
587 if (i != user->chans.end())
589 /* zap it from the channel list of the user */
591 this->WriteChannel(src, "KICK %s %s :%s", this->name, user->nick, reason);
593 user->chans.erase(i);
594 this->RemoveAllPrefixes(user);
597 if (!this->DelUser(user))
598 /* if there are no users left on the channel */
600 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
602 /* kill the record */
603 if (iter != ServerInstance->chanlist->end())
605 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
606 ServerInstance->chanlist->erase(iter);
611 return this->GetUserCounter();
614 void Channel::WriteChannel(User* user, char* text, ...)
616 char textbuffer[MAXBUF];
622 va_start(argsPtr, text);
623 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
626 this->WriteChannel(user, std::string(textbuffer));
629 void Channel::WriteChannel(User* user, const std::string &text)
631 CUList *ulist = this->GetUsers();
637 snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());
638 std::string out = tb;
640 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
642 if (IS_LOCAL(i->first))
643 i->first->Write(out);
647 void Channel::WriteChannelWithServ(const char* ServName, const char* text, ...)
649 char textbuffer[MAXBUF];
655 va_start(argsPtr, text);
656 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
659 this->WriteChannelWithServ(ServName, std::string(textbuffer));
662 void Channel::WriteChannelWithServ(const char* ServName, const std::string &text)
664 CUList *ulist = this->GetUsers();
667 snprintf(tb,MAXBUF,":%s %s",ServName ? ServName : ServerInstance->Config->ServerName, text.c_str());
668 std::string out = tb;
670 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
672 if (IS_LOCAL(i->first))
673 i->first->Write(out);
677 /* write formatted text from a source user to all users on a channel except
678 * for the sender (for privmsg etc) */
679 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, char* text, ...)
681 char textbuffer[MAXBUF];
687 va_start(argsPtr, text);
688 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
691 this->WriteAllExceptSender(user, serversource, status, std::string(textbuffer));
694 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, char* text, ...)
696 char textbuffer[MAXBUF];
702 va_start(argsPtr, text);
703 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
706 this->WriteAllExcept(user, serversource, status, except_list, std::string(textbuffer));
709 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string &text)
717 ulist = this->GetOppedUsers();
720 ulist = this->GetHalfoppedUsers();
723 ulist = this->GetVoicedUsers();
726 ulist = this->GetUsers();
730 snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());
731 std::string out = tb;
733 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
735 if ((IS_LOCAL(i->first)) && (except_list.find(i->first) == except_list.end()))
738 i->first->WriteServ(text);
740 i->first->Write(out);
745 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const std::string& text)
748 except_list[user] = user->nick;
749 this->WriteAllExcept(user, serversource, status, except_list, std::string(text));
753 * return a count of the users on a specific channel accounting for
754 * invisible users who won't increase the count. e.g. for /LIST
756 int Channel::CountInvisible()
759 CUList *ulist= this->GetUsers();
760 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
762 if (!(i->first->IsModeSet('i')))
769 char* Channel::ChanModes(bool showkey)
771 static char scratch[MAXBUF];
772 static char sparam[MAXBUF];
773 char* offset = scratch;
774 std::string extparam;
779 /* This was still iterating up to 190, Channel::modes is only 64 elements -- Om */
780 for(int n = 0; n < 64; n++)
789 extparam = (showkey ? this->key : "<key>");
792 extparam = ConvToStr(this->limit);
800 /* We know these have no parameters */
803 extparam = this->GetModeParameter(n + 65);
806 if (!extparam.empty())
808 charlcat(sparam,' ',MAXBUF);
809 strlcat(sparam,extparam.c_str(),MAXBUF);
814 /* Null terminate scratch */
816 strlcat(scratch,sparam,MAXBUF);
820 /* compile a userlist of a channel into a string, each nick seperated by
821 * spaces and op, voice etc status shown as @ and +, and send it to 'user'
823 void Channel::UserList(User *user, CUList *ulist)
832 FOREACH_RESULT(I_OnUserList,OnUserList(user, this, ulist));
835 if (MOD_RESULT != -1)
837 if ((this->IsModeSet('s')) && (!this->HasUser(user)))
839 user->WriteServ("401 %s %s :No such nick/channel",user->nick, this->name);
844 dlen = curlen = snprintf(list,MAXBUF,"353 %s %c %s :", user->nick, this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=', this->name);
847 char* ptr = list + dlen;
850 ulist = this->GetUsers();
852 /* Improvement by Brain - this doesnt change in value, so why was it inside
855 bool has_user = this->HasUser(user);
857 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
859 if ((!has_user) && (i->first->IsModeSet('i')))
862 * user is +i, and source not on the channel, does not show
868 if (i->first->Visibility && !i->first->Visibility->VisibleTo(user))
871 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", this->GetPrefixChar(i->first), i->second.c_str());
872 /* OnUserList can change this - reset it back to normal */
873 i->second = i->first->nick;
880 if (curlen > (480-NICKMAX))
882 /* list overflowed into multiple numerics */
883 user->WriteServ(std::string(list));
885 /* reset our lengths */
886 dlen = curlen = snprintf(list,MAXBUF,"353 %s %c %s :", user->nick, this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=', this->name);
894 /* if whats left in the list isnt empty, send it */
897 user->WriteServ(std::string(list));
900 user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, this->name);
903 long Channel::GetMaxBans()
905 /* Return the cached value if there is one */
907 return this->maxbans;
909 /* If there isnt one, we have to do some O(n) hax to find it the first time. (ick) */
910 for (std::map<std::string,int>::iterator n = ServerInstance->Config->maxbans.begin(); n != ServerInstance->Config->maxbans.end(); n++)
912 if (match(this->name,n->first.c_str()))
914 this->maxbans = n->second;
919 /* Screw it, just return the default of 64 */
921 return this->maxbans;
924 void Channel::ResetMaxBans()
929 /* returns the status character for a given user on a channel, e.g. @ for op,
930 * % for halfop etc. If the user has several modes set, the highest mode
931 * the user has must be returned.
933 const char* Channel::GetPrefixChar(User *user)
935 static char pf[2] = {0, 0};
937 prefixlist::iterator n = prefixes.find(user);
938 if (n != prefixes.end())
940 if (n->second.size())
942 /* If the user has any prefixes, their highest prefix
943 * will always be at the head of the list, as the list is
944 * sorted in rank order highest first (see SetPrefix()
947 *pf = n->second.begin()->first;
956 const char* Channel::GetAllPrefixChars(User* user)
958 static char prefix[MAXBUF];
962 prefixlist::iterator n = prefixes.find(user);
963 if (n != prefixes.end())
965 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
967 prefix[ctr++] = x->first;
976 unsigned int Channel::GetPrefixValue(User* user)
978 prefixlist::iterator n = prefixes.find(user);
979 if (n != prefixes.end())
981 if (n->second.size())
982 return n->second.begin()->second;
987 int Channel::GetStatusFlags(User *user)
989 UCListIter i = user->chans.find(this);
990 if (i != user->chans.end())
997 int Channel::GetStatus(User *user)
999 if (ServerInstance->ULine(user->server))
1002 UCListIter i = user->chans.find(this);
1003 if (i != user->chans.end())
1005 if ((i->second & UCMODE_OP) > 0)
1009 if ((i->second & UCMODE_HOP) > 0)
1013 if ((i->second & UCMODE_VOICE) > 0)
1015 return STATUS_VOICE;
1017 return STATUS_NORMAL;
1019 return STATUS_NORMAL;
1022 void Channel::SetPrefix(User* user, char prefix, unsigned int prefix_value, bool adding)
1024 prefixlist::iterator n = prefixes.find(user);
1025 prefixtype pfx = std::make_pair(prefix,prefix_value);
1028 if (n != prefixes.end())
1030 if (std::find(n->second.begin(), n->second.end(), pfx) == n->second.end())
1032 n->second.push_back(pfx);
1033 /* We must keep prefixes in rank order, largest first.
1034 * This is for two reasons, firstly because x-chat *ass-u-me's* this
1035 * state, and secondly it turns out to be a benefit to us later.
1036 * See above in GetPrefix().
1038 std::sort(n->second.begin(), n->second.end(), ModeParser::PrefixComparison);
1045 prefixes.insert(std::make_pair<User*,pfxcontainer>(user, one));
1050 if (n != prefixes.end())
1052 pfxcontainer::iterator x = std::find(n->second.begin(), n->second.end(), pfx);
1053 if (x != n->second.end())
1059 void Channel::RemoveAllPrefixes(User* user)
1061 prefixlist::iterator n = prefixes.find(user);
1062 if (n != prefixes.end())