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 * ---------------------------------------------------
16 #include "configreader.h"
22 chanrec::chanrec(InspIRCd* Instance) : ServerInstance(Instance)
24 *name = *topic = *setby = *key = 0;
25 maxbans = created = topicset = limit = 0;
27 age = ServerInstance->Time(true);
30 void chanrec::SetMode(char mode,bool mode_on)
32 modes[mode-65] = mode_on;
34 this->SetModeParam(mode,"",false);
38 void chanrec::SetModeParam(char mode,const char* parameter,bool mode_on)
40 CustomModeList::iterator n = custom_mode_params.find(mode);
44 if (n == custom_mode_params.end())
45 custom_mode_params[mode] = strdup(parameter);
49 if (n != custom_mode_params.end())
52 custom_mode_params.erase(n);
57 bool chanrec::IsModeSet(char mode)
59 return modes[mode-65];
62 std::string chanrec::GetModeParameter(char mode)
69 return ConvToStr(this->limit);
71 CustomModeList::iterator n = custom_mode_params.find(mode);
72 if (n != custom_mode_params.end())
79 long chanrec::GetUserCounter()
81 return (this->internal_userlist.size());
84 void chanrec::AddUser(userrec* user)
86 internal_userlist[user] = user->nick;
89 unsigned long chanrec::DelUser(userrec* user)
91 CUListIter a = internal_userlist.find(user);
93 if (a != internal_userlist.end())
95 internal_userlist.erase(a);
96 /* And tidy any others... */
98 DelHalfoppedUser(user);
102 return internal_userlist.size();
105 bool chanrec::HasUser(userrec* user)
107 return (internal_userlist.find(user) != internal_userlist.end());
110 void chanrec::AddOppedUser(userrec* user)
112 internal_op_userlist[user] = user->nick;
115 void chanrec::DelOppedUser(userrec* user)
117 CUListIter a = internal_op_userlist.find(user);
118 if (a != internal_op_userlist.end())
120 internal_op_userlist.erase(a);
125 void chanrec::AddHalfoppedUser(userrec* user)
127 internal_halfop_userlist[user] = user->nick;
130 void chanrec::DelHalfoppedUser(userrec* user)
132 CUListIter a = internal_halfop_userlist.find(user);
134 if (a != internal_halfop_userlist.end())
136 internal_halfop_userlist.erase(a);
140 void chanrec::AddVoicedUser(userrec* user)
142 internal_voice_userlist[user] = user->nick;
145 void chanrec::DelVoicedUser(userrec* user)
147 CUListIter a = internal_voice_userlist.find(user);
149 if (a != internal_voice_userlist.end())
151 internal_voice_userlist.erase(a);
155 CUList* chanrec::GetUsers()
157 return &internal_userlist;
160 CUList* chanrec::GetOppedUsers()
162 return &internal_op_userlist;
165 CUList* chanrec::GetHalfoppedUsers()
167 return &internal_halfop_userlist;
170 CUList* chanrec::GetVoicedUsers()
172 return &internal_voice_userlist;
175 void chanrec::SetDefaultModes()
177 irc::spacesepstream list(ServerInstance->Config->DefaultModes);
178 std::string modeseq = list.GetToken();
179 std::string parameter;
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 parameter = list.GetToken().c_str();
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)
209 bool new_channel = false;
212 strlcpy(cname,cn,CHANMAX);
216 chanrec* Ptr = Instance->FindChan(cname);
220 if ((!IS_LOCAL(user)) && (!TS))
221 Instance->Log(DEBUG,"*** BUG *** chanrec::JoinUser called for REMOTE user '%s' on channel '%s' but no TS given!", user->nick, cn);
225 if (IS_LOCAL(user) && override == false)
228 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,NULL,cname,privs));
233 /* create a new one */
234 Ptr = new chanrec(Instance);
235 (*(Instance->chanlist))[cname] = Ptr;
237 strlcpy(Ptr->name, cname,CHANMAX);
239 /* As spotted by jilles, dont bother to set this on remote users */
241 Ptr->SetDefaultModes();
243 Ptr->created = TS ? TS : Instance->Time();
244 Ptr->age = Ptr->created;
252 /* Already on the channel */
253 if (Ptr->HasUser(user))
257 * remote users are allowed us to bypass channel modes
258 * and bans (used by servers)
260 if (IS_LOCAL(user) && override == false)
263 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname,privs));
268 else if (MOD_RESULT == 0)
273 FOREACH_RESULT_I(Instance,I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
276 if ((!key) || strcmp(key,Ptr->key))
278 user->WriteServ("475 %s %s :Cannot join channel (Incorrect channel key)",user->nick, Ptr->name);
283 if (Ptr->modes[CM_INVITEONLY])
286 FOREACH_RESULT_I(Instance,I_OnCheckInvite,OnCheckInvite(user, Ptr));
289 if (user->IsInvited(Ptr->name))
291 /* user was invited to channel */
292 /* there may be an optional channel NOTICE here */
296 user->WriteServ("473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
300 user->RemoveInvite(Ptr->name);
305 FOREACH_RESULT_I(Instance,I_OnCheckLimit,OnCheckLimit(user, Ptr));
308 if (Ptr->GetUserCounter() >= Ptr->limit)
310 user->WriteServ("471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
315 if (Ptr->bans.size())
317 if (Ptr->IsBanned(user))
319 user->WriteServ("474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
327 /* NOTE: If the user is an oper here, we can extend their user->chans by up to
328 * OperMaxchans. For remote users which are not bound by the channel limits,
329 * we can extend infinitely. Otherwise, nope, youre restricted to MaxChans.
331 if (!IS_LOCAL(user) || override == true)
333 return chanrec::ForceChan(Instance, Ptr, user, privs);
335 else if (IS_OPER(user))
337 /* Oper allows extension up to the OperMaxchans value */
338 if (user->chans.size() < Instance->Config->OperMaxChans)
340 return chanrec::ForceChan(Instance, Ptr, user, privs);
343 else if (user->chans.size() < Instance->Config->MaxChans)
345 return chanrec::ForceChan(Instance, Ptr, user, privs);
349 user->WriteServ("405 %s %s :You are on too many channels",user->nick, cname);
353 /* Things went seriously pear shaped, so take this away. bwahaha. */
354 chan_hash::iterator n = Instance->chanlist->find(cname);
355 if (n != Instance->chanlist->end())
359 Instance->chanlist->erase(n);
366 chanrec* chanrec::ForceChan(InspIRCd* Instance, chanrec* Ptr, userrec* user, const std::string &privs)
368 userrec* dummyuser = new userrec(Instance);
369 std::string nick = user->nick;
372 dummyuser->SetFd(FD_MAGIC_NUMBER);
375 /* Just in case they have no permissions */
376 user->chans[Ptr] = 0;
378 for (std::string::const_iterator x = privs.begin(); x != privs.end(); x++)
380 const char status = *x;
381 ModeHandler* mh = Instance->Modes->FindPrefix(status);
384 Ptr->SetPrefix(user, status, mh->GetPrefixRank(), true);
385 /* Make sure that the mode handler knows this mode was now set */
386 mh->OnModeChange(dummyuser, dummyuser, Ptr, nick, true);
388 switch (mh->GetPrefix())
390 /* These logic ops are SAFE IN THIS CASE
391 * because if the entry doesnt exist,
392 * addressing operator[] creates it.
393 * If they do exist, it points to it.
394 * At all other times where we dont want
395 * to create an item if it doesnt exist, we
396 * must stick to ::find().
399 user->chans[Ptr] |= UCMODE_OP;
402 user->chans[Ptr] |= UCMODE_HOP;
405 user->chans[Ptr] |= UCMODE_VOICE;
413 FOREACH_MOD_I(Instance,I_OnUserJoin,OnUserJoin(user, Ptr, silent));
416 Ptr->WriteChannel(user,"JOIN :%s",Ptr->name);
418 /* Theyre not the first ones in here, make sure everyone else sees the modes we gave the user */
419 std::string ms = Instance->Modes->ModeString(user, Ptr);
420 if ((Ptr->GetUserCounter() > 1) && (ms.length()))
421 Ptr->WriteAllExceptSender(user, true, 0, "MODE %s +%s", Ptr->name, ms.c_str());
423 /* Major improvement by Brain - we dont need to be calculating all this pointlessly for remote users */
428 user->WriteServ("332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
429 user->WriteServ("333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
433 FOREACH_MOD_I(Instance,I_OnPostJoin,OnPostJoin(user, Ptr));
437 bool chanrec::IsBanned(userrec* user)
441 FOREACH_RESULT(I_OnCheckBan,OnCheckBan(user, this));
444 snprintf(mask, MAXBUF, "%s!%s@%s", user->nick, user->ident, user->GetIPString());
445 for (BanList::iterator i = this->bans.begin(); i != this->bans.end(); i++)
447 /* This allows CIDR ban matching
449 * Full masked host Full unmasked host IP with/without CIDR
451 if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match(mask, i->data, true)))
461 * remove a channel from a users record, and return the number of users left.
462 * Therefore, if this function returns 0 the caller should delete the chanrec.
464 long chanrec::PartUser(userrec *user, const char* reason)
469 return this->GetUserCounter();
471 UCListIter i = user->chans.find(this);
472 if (i != user->chans.end())
474 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, reason ? reason : "", silent));
477 this->WriteChannel(user, "PART %s%s%s", this->name, reason ? " :" : "", reason ? reason : "");
479 user->chans.erase(i);
480 this->RemoveAllPrefixes(user);
483 if (!this->DelUser(user)) /* if there are no users left on the channel... */
485 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
486 /* kill the record */
487 if (iter != ServerInstance->chanlist->end())
489 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
490 ServerInstance->chanlist->erase(iter);
495 return this->GetUserCounter();
498 long chanrec::ServerKickUser(userrec* 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())
535 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
536 ServerInstance->chanlist->erase(iter);
541 return this->GetUserCounter();
544 long chanrec::KickUser(userrec *src, userrec *user, const char* reason)
548 if (!src || !user || !reason)
549 return this->GetUserCounter();
553 if (!this->HasUser(user))
555 src->WriteServ("441 %s %s %s :They are not on that channel",src->nick, user->nick, this->name);
556 return this->GetUserCounter();
558 if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
560 src->WriteServ("482 %s %s :Only a u-line may kick a u-line from a channel.",src->nick, this->name);
561 return this->GetUserCounter();
565 if (!ServerInstance->ULine(src->server))
568 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,this,reason));
570 return this->GetUserCounter();
572 /* Set to -1 by OnUserPreKick if explicit allow was set */
573 if (MOD_RESULT != -1)
575 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,this,AC_KICK));
576 if ((MOD_RESULT == ACR_DENY) && (!ServerInstance->ULine(src->server)))
577 return this->GetUserCounter();
579 if ((MOD_RESULT == ACR_DEFAULT) || (!ServerInstance->ULine(src->server)))
581 int them = this->GetStatus(src);
582 int us = this->GetStatus(user);
583 if ((them < STATUS_HOP) || (them < us))
585 src->WriteServ("482 %s %s :You must be a channel %soperator",src->nick, this->name, them == STATUS_HOP ? "" : "half-");
586 return this->GetUserCounter();
592 FOREACH_MOD(I_OnUserKick,OnUserKick(src, user, this, reason, silent));
594 UCListIter i = user->chans.find(this);
595 if (i != user->chans.end())
597 /* zap it from the channel list of the user */
599 this->WriteChannel(src, "KICK %s %s :%s", this->name, user->nick, reason);
601 user->chans.erase(i);
602 this->RemoveAllPrefixes(user);
605 if (!this->DelUser(user))
606 /* if there are no users left on the channel */
608 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
610 /* kill the record */
611 if (iter != ServerInstance->chanlist->end())
613 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
614 ServerInstance->chanlist->erase(iter);
619 return this->GetUserCounter();
622 void chanrec::WriteChannel(userrec* user, char* text, ...)
624 char textbuffer[MAXBUF];
630 va_start(argsPtr, text);
631 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
634 this->WriteChannel(user, std::string(textbuffer));
637 void chanrec::WriteChannel(userrec* user, const std::string &text)
639 CUList *ulist = this->GetUsers();
645 snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());
646 std::string out = tb;
648 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
650 if (IS_LOCAL(i->first))
651 i->first->Write(out);
655 void chanrec::WriteChannelWithServ(const char* ServName, const char* text, ...)
657 char textbuffer[MAXBUF];
663 va_start(argsPtr, text);
664 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
667 this->WriteChannelWithServ(ServName, std::string(textbuffer));
670 void chanrec::WriteChannelWithServ(const char* ServName, const std::string &text)
672 CUList *ulist = this->GetUsers();
675 snprintf(tb,MAXBUF,":%s %s",ServName ? ServName : ServerInstance->Config->ServerName, text.c_str());
676 std::string out = tb;
678 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
680 if (IS_LOCAL(i->first))
681 i->first->Write(out);
685 /* write formatted text from a source user to all users on a channel except
686 * for the sender (for privmsg etc) */
687 void chanrec::WriteAllExceptSender(userrec* user, bool serversource, char status, char* text, ...)
689 char textbuffer[MAXBUF];
695 va_start(argsPtr, text);
696 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
699 this->WriteAllExceptSender(user, serversource, status, std::string(textbuffer));
702 void chanrec::WriteAllExcept(userrec* user, bool serversource, char status, CUList &except_list, char* text, ...)
704 char textbuffer[MAXBUF];
710 va_start(argsPtr, text);
711 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
714 this->WriteAllExcept(user, serversource, status, except_list, std::string(textbuffer));
717 void chanrec::WriteAllExcept(userrec* user, bool serversource, char status, CUList &except_list, const std::string &text)
725 ulist = this->GetOppedUsers();
728 ulist = this->GetHalfoppedUsers();
731 ulist = this->GetVoicedUsers();
734 ulist = this->GetUsers();
738 snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());
739 std::string out = tb;
741 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
743 if ((IS_LOCAL(i->first)) && (except_list.find(i->first) == except_list.end()))
746 i->first->WriteServ(text);
748 i->first->Write(out);
753 void chanrec::WriteAllExceptSender(userrec* user, bool serversource, char status, const std::string& text)
756 except_list[user] = user->nick;
757 this->WriteAllExcept(user, serversource, status, except_list, std::string(text));
761 * return a count of the users on a specific channel accounting for
762 * invisible users who won't increase the count. e.g. for /LIST
764 int chanrec::CountInvisible()
767 CUList *ulist= this->GetUsers();
768 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
770 if (!(i->first->IsModeSet('i')))
777 char* chanrec::ChanModes(bool showkey)
779 static char scratch[MAXBUF];
780 static char sparam[MAXBUF];
781 char* offset = scratch;
782 std::string extparam;
787 /* This was still iterating up to 190, chanrec::modes is only 64 elements -- Om */
788 for(int n = 0; n < 64; n++)
797 extparam = (showkey ? this->key : "<key>");
800 extparam = ConvToStr(this->limit);
808 /* We know these have no parameters */
811 extparam = this->GetModeParameter(n + 65);
814 if (!extparam.empty())
816 charlcat(sparam,' ',MAXBUF);
817 strlcat(sparam,extparam.c_str(),MAXBUF);
822 /* Null terminate scratch */
824 strlcat(scratch,sparam,MAXBUF);
828 /* compile a userlist of a channel into a string, each nick seperated by
829 * spaces and op, voice etc status shown as @ and +, and send it to 'user'
831 void chanrec::UserList(userrec *user, CUList *ulist)
840 FOREACH_RESULT(I_OnUserList,OnUserList(user, this, ulist));
844 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, 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->modes[UM_INVISIBLE]))
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 = %s :", user->nick, 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 chanrec::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 chanrec::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* chanrec::GetPrefixChar(userrec *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* chanrec::GetAllPrefixChars(userrec* 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 chanrec::GetPrefixValue(userrec* 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 chanrec::GetStatusFlags(userrec *user)
989 UCListIter i = user->chans.find(this);
990 if (i != user->chans.end())
997 int chanrec::GetStatus(userrec *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 chanrec::SetPrefix(userrec* 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<userrec*,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 chanrec::RemoveAllPrefixes(userrec* user)
1061 prefixlist::iterator n = prefixes.find(user);
1062 if (n != prefixes.end())