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 * ---------------------------------------------------
19 chanrec::chanrec(InspIRCd* Instance) : ServerInstance(Instance)
21 *name = *topic = *setby = *key = 0;
22 maxbans = created = topicset = limit = 0;
24 age = ServerInstance->Time(true);
27 void chanrec::SetMode(char mode,bool mode_on)
29 modes[mode-65] = mode_on;
31 this->SetModeParam(mode,"",false);
35 void chanrec::SetModeParam(char mode,const char* parameter,bool mode_on)
37 CustomModeList::iterator n = custom_mode_params.find(mode);
41 if (n == custom_mode_params.end())
42 custom_mode_params[mode] = strdup(parameter);
46 if (n != custom_mode_params.end())
49 custom_mode_params.erase(n);
54 bool chanrec::IsModeSet(char mode)
56 return modes[mode-65];
59 std::string chanrec::GetModeParameter(char mode)
66 return ConvToStr(this->limit);
68 CustomModeList::iterator n = custom_mode_params.find(mode);
69 if (n != custom_mode_params.end())
76 long chanrec::GetUserCounter()
78 return (this->internal_userlist.size());
81 void chanrec::AddUser(userrec* user)
83 internal_userlist[user] = user->nick;
86 unsigned long chanrec::DelUser(userrec* user)
88 CUListIter a = internal_userlist.find(user);
90 if (a != internal_userlist.end())
92 internal_userlist.erase(a);
93 /* And tidy any others... */
95 DelHalfoppedUser(user);
99 return internal_userlist.size();
102 bool chanrec::HasUser(userrec* user)
104 return (internal_userlist.find(user) != internal_userlist.end());
107 void chanrec::AddOppedUser(userrec* user)
109 internal_op_userlist[user] = user->nick;
112 void chanrec::DelOppedUser(userrec* user)
114 CUListIter a = internal_op_userlist.find(user);
115 if (a != internal_op_userlist.end())
117 internal_op_userlist.erase(a);
122 void chanrec::AddHalfoppedUser(userrec* user)
124 internal_halfop_userlist[user] = user->nick;
127 void chanrec::DelHalfoppedUser(userrec* user)
129 CUListIter a = internal_halfop_userlist.find(user);
131 if (a != internal_halfop_userlist.end())
133 internal_halfop_userlist.erase(a);
137 void chanrec::AddVoicedUser(userrec* user)
139 internal_voice_userlist[user] = user->nick;
142 void chanrec::DelVoicedUser(userrec* user)
144 CUListIter a = internal_voice_userlist.find(user);
146 if (a != internal_voice_userlist.end())
148 internal_voice_userlist.erase(a);
152 CUList* chanrec::GetUsers()
154 return &internal_userlist;
157 CUList* chanrec::GetOppedUsers()
159 return &internal_op_userlist;
162 CUList* chanrec::GetHalfoppedUsers()
164 return &internal_halfop_userlist;
167 CUList* chanrec::GetVoicedUsers()
169 return &internal_voice_userlist;
172 void chanrec::SetDefaultModes()
174 irc::spacesepstream list(ServerInstance->Config->DefaultModes);
176 std::string parameter;
178 list.GetToken(modeseq);
180 userrec* dummyuser = new userrec(ServerInstance);
181 dummyuser->SetFd(FD_MAGIC_NUMBER);
183 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
185 ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
188 if (mode->GetNumParams(true))
189 list.GetToken(parameter);
193 mode->OnModeChange(dummyuser, dummyuser, this, parameter, true);
201 * add a channel to a user, creating the record for it if needed and linking
202 * it to the user record
204 chanrec* chanrec::JoinUser(InspIRCd* Instance, userrec *user, const char* cn, bool override, const char* key, time_t TS)
215 * We don't restrict the number of channels that remote users or users that are override-joining may be in.
216 * We restrict local users to MaxChans channels.
217 * We restrict local operators to OperMaxChans channels.
218 * This is a lot more logical than how it was formerly. -- w00t
220 if (IS_LOCAL(user) && !override)
222 if (user->GetMaxChans())
224 if (user->chans.size() >= user->GetMaxChans())
226 user->WriteServ("405 %s %s :You are on too many channels",user->nick, cn);
234 if (user->chans.size() >= Instance->Config->OperMaxChans)
236 user->WriteServ("405 %s %s :You are on too many channels",user->nick, cn);
242 if (user->chans.size() >= Instance->Config->MaxChans)
244 user->WriteServ("405 %s %s :You are on too many channels",user->nick, cn);
251 strlcpy(cname, cn, CHANMAX);
252 Ptr = Instance->FindChan(cname);
257 * Fix: desync bug was here, don't set @ on remote users - spanningtree handles their permissions. bug #358. -- w00t
262 Instance->Log(DEBUG,"*** BUG *** chanrec::JoinUser called for REMOTE user '%s' on channel '%s' but no TS given!", user->nick, cn);
269 if (IS_LOCAL(user) && override == false)
272 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,NULL,cname,privs));
277 /* create a new one */
278 Ptr = new chanrec(Instance);
279 (*(Instance->chanlist))[cname] = Ptr;
281 strlcpy(Ptr->name, cname,CHANMAX);
283 /* As spotted by jilles, dont bother to set this on remote users */
285 Ptr->SetDefaultModes();
287 Ptr->created = TS ? TS : Instance->Time();
288 Ptr->age = Ptr->created;
295 /* Already on the channel */
296 if (Ptr->HasUser(user))
300 * remote users are allowed us to bypass channel modes
301 * and bans (used by servers)
303 if (IS_LOCAL(user) && override == false)
306 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname,privs));
311 else if (MOD_RESULT == 0)
316 FOREACH_RESULT_I(Instance,I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
319 if ((!key) || strcmp(key,Ptr->key))
321 user->WriteServ("475 %s %s :Cannot join channel (Incorrect channel key)",user->nick, Ptr->name);
326 if (Ptr->IsModeSet('i'))
329 FOREACH_RESULT_I(Instance,I_OnCheckInvite,OnCheckInvite(user, Ptr));
332 if (!user->IsInvited(Ptr->name))
334 user->WriteServ("473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
338 user->RemoveInvite(Ptr->name);
343 FOREACH_RESULT_I(Instance,I_OnCheckLimit,OnCheckLimit(user, Ptr));
346 if (Ptr->GetUserCounter() >= Ptr->limit)
348 user->WriteServ("471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
353 if (Ptr->bans.size())
355 if (Ptr->IsBanned(user))
357 user->WriteServ("474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
365 return chanrec::ForceChan(Instance, Ptr, user, privs);
368 chanrec* chanrec::ForceChan(InspIRCd* Instance, chanrec* Ptr, userrec* user, const std::string &privs)
370 userrec* dummyuser = new userrec(Instance);
371 std::string nick = user->nick;
374 dummyuser->SetFd(FD_MAGIC_NUMBER);
377 /* Just in case they have no permissions */
378 user->chans[Ptr] = 0;
380 for (std::string::const_iterator x = privs.begin(); x != privs.end(); x++)
382 const char status = *x;
383 ModeHandler* mh = Instance->Modes->FindPrefix(status);
386 Ptr->SetPrefix(user, status, mh->GetPrefixRank(), true);
387 /* Make sure that the mode handler knows this mode was now set */
388 mh->OnModeChange(dummyuser, dummyuser, Ptr, nick, true);
390 switch (mh->GetPrefix())
392 /* These logic ops are SAFE IN THIS CASE
393 * because if the entry doesnt exist,
394 * addressing operator[] creates it.
395 * If they do exist, it points to it.
396 * At all other times where we dont want
397 * to create an item if it doesnt exist, we
398 * must stick to ::find().
401 user->chans[Ptr] |= UCMODE_OP;
404 user->chans[Ptr] |= UCMODE_HOP;
407 user->chans[Ptr] |= UCMODE_VOICE;
415 FOREACH_MOD_I(Instance,I_OnUserJoin,OnUserJoin(user, Ptr, silent));
418 Ptr->WriteChannel(user,"JOIN :%s",Ptr->name);
420 /* Theyre not the first ones in here, make sure everyone else sees the modes we gave the user */
421 std::string ms = Instance->Modes->ModeString(user, Ptr);
422 if ((Ptr->GetUserCounter() > 1) && (ms.length()))
423 Ptr->WriteAllExceptSender(user, true, 0, "MODE %s +%s", Ptr->name, ms.c_str());
425 /* Major improvement by Brain - we dont need to be calculating all this pointlessly for remote users */
430 user->WriteServ("332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
431 user->WriteServ("333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
435 FOREACH_MOD_I(Instance,I_OnPostJoin,OnPostJoin(user, Ptr));
439 bool chanrec::IsBanned(userrec* user)
443 FOREACH_RESULT(I_OnCheckBan,OnCheckBan(user, this));
446 snprintf(mask, MAXBUF, "%s!%s@%s", user->nick, user->ident, user->GetIPString());
447 for (BanList::iterator i = this->bans.begin(); i != this->bans.end(); i++)
449 /* This allows CIDR ban matching
451 * Full masked host Full unmasked host IP with/without CIDR
453 if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match(mask, i->data, true)))
463 * remove a channel from a users record, and return the number of users left.
464 * Therefore, if this function returns 0 the caller should delete the chanrec.
466 long chanrec::PartUser(userrec *user, const char* reason)
471 return this->GetUserCounter();
473 UCListIter i = user->chans.find(this);
474 if (i != user->chans.end())
476 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, reason ? reason : "", silent));
479 this->WriteChannel(user, "PART %s%s%s", this->name, reason ? " :" : "", reason ? reason : "");
481 user->chans.erase(i);
482 this->RemoveAllPrefixes(user);
485 if (!this->DelUser(user)) /* if there are no users left on the channel... */
487 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
488 /* kill the record */
489 if (iter != ServerInstance->chanlist->end())
491 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
492 ServerInstance->chanlist->erase(iter);
497 return this->GetUserCounter();
500 long chanrec::ServerKickUser(userrec* user, const char* reason, bool triggerevents)
504 if (!user || !reason)
505 return this->GetUserCounter();
509 if (!this->HasUser(user))
512 return this->GetUserCounter();
518 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL, user, this, reason, silent));
521 UCListIter i = user->chans.find(this);
522 if (i != user->chans.end())
525 this->WriteChannelWithServ(ServerInstance->Config->ServerName, "KICK %s %s :%s", this->name, user->nick, reason);
527 user->chans.erase(i);
528 this->RemoveAllPrefixes(user);
531 if (!this->DelUser(user))
533 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
534 /* kill the record */
535 if (iter != ServerInstance->chanlist->end())
537 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
538 ServerInstance->chanlist->erase(iter);
543 return this->GetUserCounter();
546 long chanrec::KickUser(userrec *src, userrec *user, const char* reason)
550 if (!src || !user || !reason)
551 return this->GetUserCounter();
555 if (!this->HasUser(user))
557 src->WriteServ("441 %s %s %s :They are not on that channel",src->nick, user->nick, this->name);
558 return this->GetUserCounter();
560 if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
562 src->WriteServ("482 %s %s :Only a u-line may kick a u-line from a channel.",src->nick, this->name);
563 return this->GetUserCounter();
567 if (!ServerInstance->ULine(src->server))
570 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,this,reason));
572 return this->GetUserCounter();
574 /* Set to -1 by OnUserPreKick if explicit allow was set */
575 if (MOD_RESULT != -1)
577 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,this,AC_KICK));
578 if ((MOD_RESULT == ACR_DENY) && (!ServerInstance->ULine(src->server)))
579 return this->GetUserCounter();
581 if ((MOD_RESULT == ACR_DEFAULT) || (!ServerInstance->ULine(src->server)))
583 int them = this->GetStatus(src);
584 int us = this->GetStatus(user);
585 if ((them < STATUS_HOP) || (them < us))
587 src->WriteServ("482 %s %s :You must be a channel %soperator",src->nick, this->name, them == STATUS_HOP ? "" : "half-");
588 return this->GetUserCounter();
594 FOREACH_MOD(I_OnUserKick,OnUserKick(src, user, this, reason, silent));
596 UCListIter i = user->chans.find(this);
597 if (i != user->chans.end())
599 /* zap it from the channel list of the user */
601 this->WriteChannel(src, "KICK %s %s :%s", this->name, user->nick, reason);
603 user->chans.erase(i);
604 this->RemoveAllPrefixes(user);
607 if (!this->DelUser(user))
608 /* if there are no users left on the channel */
610 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
612 /* kill the record */
613 if (iter != ServerInstance->chanlist->end())
615 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
616 ServerInstance->chanlist->erase(iter);
621 return this->GetUserCounter();
624 void chanrec::WriteChannel(userrec* user, char* text, ...)
626 char textbuffer[MAXBUF];
632 va_start(argsPtr, text);
633 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
636 this->WriteChannel(user, std::string(textbuffer));
639 void chanrec::WriteChannel(userrec* user, const std::string &text)
641 CUList *ulist = this->GetUsers();
647 snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());
648 std::string out = tb;
650 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
652 if (IS_LOCAL(i->first))
653 i->first->Write(out);
657 void chanrec::WriteChannelWithServ(const char* ServName, const char* text, ...)
659 char textbuffer[MAXBUF];
665 va_start(argsPtr, text);
666 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
669 this->WriteChannelWithServ(ServName, std::string(textbuffer));
672 void chanrec::WriteChannelWithServ(const char* ServName, const std::string &text)
674 CUList *ulist = this->GetUsers();
677 snprintf(tb,MAXBUF,":%s %s",ServName ? ServName : ServerInstance->Config->ServerName, text.c_str());
678 std::string out = tb;
680 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
682 if (IS_LOCAL(i->first))
683 i->first->Write(out);
687 /* write formatted text from a source user to all users on a channel except
688 * for the sender (for privmsg etc) */
689 void chanrec::WriteAllExceptSender(userrec* user, bool serversource, char status, char* text, ...)
691 char textbuffer[MAXBUF];
697 va_start(argsPtr, text);
698 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
701 this->WriteAllExceptSender(user, serversource, status, std::string(textbuffer));
704 void chanrec::WriteAllExcept(userrec* user, bool serversource, char status, CUList &except_list, char* text, ...)
706 char textbuffer[MAXBUF];
712 va_start(argsPtr, text);
713 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
716 this->WriteAllExcept(user, serversource, status, except_list, std::string(textbuffer));
719 void chanrec::WriteAllExcept(userrec* user, bool serversource, char status, CUList &except_list, const std::string &text)
727 ulist = this->GetOppedUsers();
730 ulist = this->GetHalfoppedUsers();
733 ulist = this->GetVoicedUsers();
736 ulist = this->GetUsers();
740 snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());
741 std::string out = tb;
743 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
745 if ((IS_LOCAL(i->first)) && (except_list.find(i->first) == except_list.end()))
748 i->first->WriteServ(text);
750 i->first->Write(out);
755 void chanrec::WriteAllExceptSender(userrec* user, bool serversource, char status, const std::string& text)
758 except_list[user] = user->nick;
759 this->WriteAllExcept(user, serversource, status, except_list, std::string(text));
763 * return a count of the users on a specific channel accounting for
764 * invisible users who won't increase the count. e.g. for /LIST
766 int chanrec::CountInvisible()
769 CUList *ulist= this->GetUsers();
770 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
772 if (!(i->first->IsModeSet('i')))
779 char* chanrec::ChanModes(bool showkey)
781 static char scratch[MAXBUF];
782 static char sparam[MAXBUF];
783 char* offset = scratch;
784 std::string extparam;
789 /* This was still iterating up to 190, chanrec::modes is only 64 elements -- Om */
790 for(int n = 0; n < 64; n++)
799 extparam = (showkey ? this->key : "<key>");
802 extparam = ConvToStr(this->limit);
810 /* We know these have no parameters */
813 extparam = this->GetModeParameter(n + 65);
816 if (!extparam.empty())
818 charlcat(sparam,' ',MAXBUF);
819 strlcat(sparam,extparam.c_str(),MAXBUF);
824 /* Null terminate scratch */
826 strlcat(scratch,sparam,MAXBUF);
830 /* compile a userlist of a channel into a string, each nick seperated by
831 * spaces and op, voice etc status shown as @ and +, and send it to 'user'
833 void chanrec::UserList(userrec *user, CUList *ulist)
842 FOREACH_RESULT(I_OnUserList,OnUserList(user, this, ulist));
846 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
849 char* ptr = list + dlen;
852 ulist = this->GetUsers();
854 /* Improvement by Brain - this doesnt change in value, so why was it inside
857 bool has_user = this->HasUser(user);
859 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
861 if ((!has_user) && (i->first->IsModeSet('i')))
864 * user is +i, and source not on the channel, does not show
870 if (i->first->Visibility && !i->first->Visibility->VisibleTo(user))
873 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", this->GetPrefixChar(i->first), i->second.c_str());
874 /* OnUserList can change this - reset it back to normal */
875 i->second = i->first->nick;
882 if (curlen > (480-NICKMAX))
884 /* list overflowed into multiple numerics */
885 user->WriteServ(std::string(list));
887 /* reset our lengths */
888 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
896 /* if whats left in the list isnt empty, send it */
899 user->WriteServ(std::string(list));
902 user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, this->name);
905 long chanrec::GetMaxBans()
907 /* Return the cached value if there is one */
909 return this->maxbans;
911 /* If there isnt one, we have to do some O(n) hax to find it the first time. (ick) */
912 for (std::map<std::string,int>::iterator n = ServerInstance->Config->maxbans.begin(); n != ServerInstance->Config->maxbans.end(); n++)
914 if (match(this->name,n->first.c_str()))
916 this->maxbans = n->second;
921 /* Screw it, just return the default of 64 */
923 return this->maxbans;
926 void chanrec::ResetMaxBans()
931 /* returns the status character for a given user on a channel, e.g. @ for op,
932 * % for halfop etc. If the user has several modes set, the highest mode
933 * the user has must be returned.
935 const char* chanrec::GetPrefixChar(userrec *user)
937 static char pf[2] = {0, 0};
939 prefixlist::iterator n = prefixes.find(user);
940 if (n != prefixes.end())
942 if (n->second.size())
944 /* If the user has any prefixes, their highest prefix
945 * will always be at the head of the list, as the list is
946 * sorted in rank order highest first (see SetPrefix()
949 *pf = n->second.begin()->first;
958 const char* chanrec::GetAllPrefixChars(userrec* user)
960 static char prefix[MAXBUF];
964 prefixlist::iterator n = prefixes.find(user);
965 if (n != prefixes.end())
967 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
969 prefix[ctr++] = x->first;
978 unsigned int chanrec::GetPrefixValue(userrec* user)
980 prefixlist::iterator n = prefixes.find(user);
981 if (n != prefixes.end())
983 if (n->second.size())
984 return n->second.begin()->second;
989 int chanrec::GetStatusFlags(userrec *user)
991 UCListIter i = user->chans.find(this);
992 if (i != user->chans.end())
999 int chanrec::GetStatus(userrec *user)
1001 if (ServerInstance->ULine(user->server))
1004 UCListIter i = user->chans.find(this);
1005 if (i != user->chans.end())
1007 if ((i->second & UCMODE_OP) > 0)
1011 if ((i->second & UCMODE_HOP) > 0)
1015 if ((i->second & UCMODE_VOICE) > 0)
1017 return STATUS_VOICE;
1019 return STATUS_NORMAL;
1021 return STATUS_NORMAL;
1024 void chanrec::SetPrefix(userrec* user, char prefix, unsigned int prefix_value, bool adding)
1026 prefixlist::iterator n = prefixes.find(user);
1027 prefixtype pfx = std::make_pair(prefix,prefix_value);
1030 if (n != prefixes.end())
1032 if (std::find(n->second.begin(), n->second.end(), pfx) == n->second.end())
1034 n->second.push_back(pfx);
1035 /* We must keep prefixes in rank order, largest first.
1036 * This is for two reasons, firstly because x-chat *ass-u-me's* this
1037 * state, and secondly it turns out to be a benefit to us later.
1038 * See above in GetPrefix().
1040 std::sort(n->second.begin(), n->second.end(), ModeParser::PrefixComparison);
1047 prefixes.insert(std::make_pair<userrec*,pfxcontainer>(user, one));
1052 if (n != prefixes.end())
1054 pfxcontainer::iterator x = std::find(n->second.begin(), n->second.end(), pfx);
1055 if (x != n->second.end())
1061 void chanrec::RemoveAllPrefixes(userrec* user)
1063 prefixlist::iterator n = prefixes.find(user);
1064 if (n != prefixes.end())