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 * ---------------------------------------------------
15 #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;
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;
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;
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;
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;
176 * add a channel to a user, creating the record for it if needed and linking
177 * it to the user record
179 chanrec* chanrec::JoinUser(InspIRCd* Instance, userrec *user, const char* cn, bool override, const char* key)
184 bool new_channel = false;
187 strlcpy(cname,cn,CHANMAX);
191 chanrec* Ptr = Instance->FindChan(cname);
197 if (IS_LOCAL(user) && override == false)
200 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,NULL,cname,privs));
205 /* create a new one */
206 Ptr = new chanrec(Instance);
207 (*(Instance->chanlist))[cname] = Ptr;
209 strlcpy(Ptr->name, cname,CHANMAX);
211 /* As spotted by jilles, dont bother to set this on remote users */
213 Ptr->modes[CM_TOPICLOCK] = Ptr->modes[CM_NOEXTERNAL] = 1;
215 Ptr->created = Instance->Time();
223 /* Already on the channel */
224 if (Ptr->HasUser(user))
228 * remote users are allowed us to bypass channel modes
229 * and bans (used by servers)
231 if (IS_LOCAL(user) && override == false)
234 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname,privs));
239 else if (MOD_RESULT == 0)
244 FOREACH_RESULT_I(Instance,I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
247 if ((!key) || strcmp(key,Ptr->key))
249 user->WriteServ("475 %s %s :Cannot join channel (Incorrect channel key)",user->nick, Ptr->name);
254 if (Ptr->modes[CM_INVITEONLY])
257 FOREACH_RESULT_I(Instance,I_OnCheckInvite,OnCheckInvite(user, Ptr));
260 if (user->IsInvited(Ptr->name))
262 /* user was invited to channel */
263 /* there may be an optional channel NOTICE here */
267 user->WriteServ("473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
271 user->RemoveInvite(Ptr->name);
276 FOREACH_RESULT_I(Instance,I_OnCheckLimit,OnCheckLimit(user, Ptr));
279 if (Ptr->GetUserCounter() >= Ptr->limit)
281 user->WriteServ("471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
286 if (Ptr->bans.size())
288 if (Ptr->IsBanned(user))
290 user->WriteServ("474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
298 /* NOTE: If the user is an oper here, we can extend their user->chans by up to
299 * OPERMAXCHANS. For remote users which are not bound by the channel limits,
300 * we can extend infinitely. Otherwise, nope, youre restricted to MAXCHANS.
302 if (!IS_LOCAL(user) || override == true) /* was a check on fd < 0 */
304 return chanrec::ForceChan(Instance, Ptr, user, privs);
306 else if (*user->oper)
308 /* Oper allows extension up to the OPERMAXCHANS value */
309 if (user->chans.size() < OPERMAXCHANS)
311 return chanrec::ForceChan(Instance, Ptr, user, privs);
314 else if (user->chans.size() < MAXCHANS)
316 return chanrec::ForceChan(Instance, Ptr, user, privs);
320 user->WriteServ("405 %s %s :You are on too many channels",user->nick, cname);
324 /* Things went seriously pear shaped, so take this away. bwahaha. */
325 chan_hash::iterator n = Instance->chanlist->find(cname);
326 if (n != Instance->chanlist->end())
330 Instance->chanlist->erase(n);
337 chanrec* chanrec::ForceChan(InspIRCd* Instance, chanrec* Ptr, userrec* user, const std::string &privs)
339 userrec* dummyuser = new userrec(Instance);
340 std::string nick = user->nick;
342 dummyuser->SetFd(FD_MAGIC_NUMBER);
345 /* Just in case they have no permissions */
346 user->chans[Ptr] = 0;
348 for (std::string::const_iterator x = privs.begin(); x != privs.end(); x++)
350 const char status = *x;
351 ModeHandler* mh = Instance->Modes->FindPrefix(status);
354 Ptr->SetPrefix(user, status, mh->GetPrefixRank(), true);
355 /* Make sure that the mode handler knows this mode was now set */
356 mh->OnModeChange(dummyuser, dummyuser, Ptr, nick, true);
358 switch (mh->GetPrefix())
360 /* These logic ops are SAFE IN THIS CASE
361 * because if the entry doesnt exist,
362 * addressing operator[] creates it.
363 * If they do exist, it points to it.
364 * At all other times where we dont want
365 * to create an item if it doesnt exist, we
366 * must stick to ::find().
369 user->chans[Ptr] |= UCMODE_OP;
372 user->chans[Ptr] |= UCMODE_HOP;
375 user->chans[Ptr] |= UCMODE_VOICE;
383 Ptr->WriteChannel(user,"JOIN :%s",Ptr->name);
385 /* Theyre not the first ones in here, make sure everyone else sees the modes we gave the user */
386 std::string ms = Instance->Modes->ModeString(user, Ptr);
387 if ((Ptr->GetUserCounter() > 1) && (ms.length()))
388 Ptr->WriteAllExceptSender(user, true, 0, "MODE %s +%s", Ptr->name, ms.c_str());
390 /* Major improvement by Brain - we dont need to be calculating all this pointlessly for remote users */
395 user->WriteServ("332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
396 user->WriteServ("333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
400 FOREACH_MOD_I(Instance,I_OnUserJoin,OnUserJoin(user,Ptr));
401 FOREACH_MOD_I(Instance,I_OnPostJoin,OnPostJoin(user,Ptr));
405 bool chanrec::IsBanned(userrec* user)
409 FOREACH_RESULT(I_OnCheckBan,OnCheckBan(user, this));
412 snprintf(mask, MAXBUF, "%s!%s@%s", user->nick, user->ident, user->GetIPString());
413 for (BanList::iterator i = this->bans.begin(); i != this->bans.end(); i++)
415 /* This allows CIDR ban matching
417 * Full masked host Full unmasked host IP with/without CIDR
419 if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match(mask, i->data, true)))
429 * remove a channel from a users record, and return the number of users left.
430 * Therefore, if this function returns 0 the caller should delete the chanrec.
432 long chanrec::PartUser(userrec *user, const char* reason)
435 return this->GetUserCounter();
437 UCListIter i = user->chans.find(this);
438 if (i != user->chans.end())
440 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, reason ? reason : ""));
441 this->WriteChannel(user, "PART %s%s%s", this->name, reason ? " :" : "", reason ? reason : "");
442 user->chans.erase(i);
443 this->RemoveAllPrefixes(user);
446 if (!this->DelUser(user)) /* if there are no users left on the channel... */
448 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
449 /* kill the record */
450 if (iter != ServerInstance->chanlist->end())
452 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
453 ServerInstance->chanlist->erase(iter);
458 return this->GetUserCounter();
461 long chanrec::ServerKickUser(userrec* user, const char* reason, bool triggerevents)
463 if (!user || !reason)
464 return this->GetUserCounter();
468 if (!this->HasUser(user))
471 return this->GetUserCounter();
477 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL,user,this,reason));
480 UCListIter i = user->chans.find(this);
481 if (i != user->chans.end())
483 this->WriteChannelWithServ(ServerInstance->Config->ServerName, "KICK %s %s :%s", this->name, user->nick, reason);
484 user->chans.erase(i);
485 this->RemoveAllPrefixes(user);
488 if (!this->DelUser(user))
490 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
491 /* kill the record */
492 if (iter != ServerInstance->chanlist->end())
494 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
495 ServerInstance->chanlist->erase(iter);
500 return this->GetUserCounter();
503 long chanrec::KickUser(userrec *src, userrec *user, const char* reason)
505 if (!src || !user || !reason)
506 return this->GetUserCounter();
510 if (!this->HasUser(user))
512 src->WriteServ("441 %s %s %s :They are not on that channel",src->nick, user->nick, this->name);
513 return this->GetUserCounter();
515 if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
517 src->WriteServ("482 %s %s :Only a u-line may kick a u-line from a channel.",src->nick, this->name);
518 return this->GetUserCounter();
522 if (!ServerInstance->ULine(src->server))
525 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,this,reason));
527 return this->GetUserCounter();
529 /* Set to -1 by OnUserPreKick if explicit allow was set */
530 if (MOD_RESULT != -1)
532 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,this,AC_KICK));
533 if ((MOD_RESULT == ACR_DENY) && (!ServerInstance->ULine(src->server)))
534 return this->GetUserCounter();
536 if ((MOD_RESULT == ACR_DEFAULT) || (!ServerInstance->ULine(src->server)))
538 int them = this->GetStatus(src);
539 int us = this->GetStatus(user);
540 if ((them < STATUS_HOP) || (them < us))
542 src->WriteServ("482 %s %s :You must be a channel %soperator",src->nick, this->name, them == STATUS_HOP ? "" : "half-");
543 return this->GetUserCounter();
549 FOREACH_MOD(I_OnUserKick,OnUserKick(src,user,this,reason));
551 UCListIter i = user->chans.find(this);
552 if (i != user->chans.end())
554 /* zap it from the channel list of the user */
555 this->WriteChannel(src, "KICK %s %s :%s", this->name, user->nick, reason);
556 user->chans.erase(i);
557 this->RemoveAllPrefixes(user);
560 if (!this->DelUser(user))
561 /* if there are no users left on the channel */
563 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
565 /* kill the record */
566 if (iter != ServerInstance->chanlist->end())
568 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
569 ServerInstance->chanlist->erase(iter);
574 return this->GetUserCounter();
577 void chanrec::WriteChannel(userrec* user, char* text, ...)
579 char textbuffer[MAXBUF];
585 va_start(argsPtr, text);
586 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
589 this->WriteChannel(user, std::string(textbuffer));
592 void chanrec::WriteChannel(userrec* user, const std::string &text)
594 CUList *ulist = this->GetUsers();
600 snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());
601 std::string out = tb;
603 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
605 if (IS_LOCAL(i->second))
606 i->second->Write(out);
610 void chanrec::WriteChannelWithServ(const char* ServName, const char* text, ...)
612 char textbuffer[MAXBUF];
618 va_start(argsPtr, text);
619 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
622 this->WriteChannelWithServ(ServName, std::string(textbuffer));
625 void chanrec::WriteChannelWithServ(const char* ServName, const std::string &text)
627 CUList *ulist = this->GetUsers();
630 snprintf(tb,MAXBUF,":%s %s",ServName ? ServName : ServerInstance->Config->ServerName, text.c_str());
631 std::string out = tb;
633 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
635 if (IS_LOCAL(i->second))
636 i->second->Write(out);
640 /* write formatted text from a source user to all users on a channel except
641 * for the sender (for privmsg etc) */
642 void chanrec::WriteAllExceptSender(userrec* user, bool serversource, char status, char* text, ...)
644 char textbuffer[MAXBUF];
650 va_start(argsPtr, text);
651 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
654 this->WriteAllExceptSender(user, serversource, status, std::string(textbuffer));
657 void chanrec::WriteAllExcept(userrec* user, bool serversource, char status, CUList &except_list, char* text, ...)
659 char textbuffer[MAXBUF];
665 va_start(argsPtr, text);
666 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
669 this->WriteAllExcept(user, serversource, status, except_list, std::string(textbuffer));
672 void chanrec::WriteAllExcept(userrec* user, bool serversource, char status, CUList &except_list, const std::string &text)
680 ulist = this->GetOppedUsers();
683 ulist = this->GetHalfoppedUsers();
686 ulist = this->GetVoicedUsers();
689 ulist = this->GetUsers();
693 snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());
694 std::string out = tb;
696 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
698 if ((IS_LOCAL(i->second)) && (except_list.find(i->second) == except_list.end()))
701 i->second->WriteServ(text);
703 i->second->Write(out);
708 void chanrec::WriteAllExceptSender(userrec* user, bool serversource, char status, const std::string& text)
711 except_list[user] = user;
712 this->WriteAllExcept(user, serversource, status, except_list, std::string(text));
716 * return a count of the users on a specific channel accounting for
717 * invisible users who won't increase the count. e.g. for /LIST
719 int chanrec::CountInvisible()
722 CUList *ulist= this->GetUsers();
723 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
725 if (!(i->second->modes[UM_INVISIBLE]))
732 char* chanrec::ChanModes(bool showkey)
734 static char scratch[MAXBUF];
735 static char sparam[MAXBUF];
736 char* offset = scratch;
737 std::string extparam;
742 /* This was still iterating up to 190, chanrec::modes is only 64 elements -- Om */
743 for(int n = 0; n < 64; n++)
752 extparam = (showkey ? this->key : "<key>");
755 extparam = ConvToStr(this->limit);
763 /* We know these have no parameters */
766 extparam = this->GetModeParameter(n + 65);
771 charlcat(sparam,' ',MAXBUF);
772 strlcat(sparam,extparam.c_str(),MAXBUF);
777 /* Null terminate scratch */
779 strlcat(scratch,sparam,MAXBUF);
783 /* compile a userlist of a channel into a string, each nick seperated by
784 * spaces and op, voice etc status shown as @ and +, and send it to 'user'
786 void chanrec::UserList(userrec *user)
795 FOREACH_RESULT(I_OnUserList,OnUserList(user, this));
799 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
802 char* ptr = list + dlen;
804 CUList *ulist= this->GetUsers();
806 /* Improvement by Brain - this doesnt change in value, so why was it inside
809 bool has_user = this->HasUser(user);
811 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
813 if ((!has_user) && (i->second->modes[UM_INVISIBLE]))
816 * user is +i, and source not on the channel, does not show
822 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", this->GetPrefixChar(i->second), i->second->nick);
829 if (curlen > (480-NICKMAX))
831 /* list overflowed into multiple numerics */
832 user->WriteServ(std::string(list));
834 /* reset our lengths */
835 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
843 /* if whats left in the list isnt empty, send it */
846 user->WriteServ(std::string(list));
849 user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, this->name);
852 long chanrec::GetMaxBans()
854 /* Return the cached value if there is one */
856 return this->maxbans;
858 /* If there isnt one, we have to do some O(n) hax to find it the first time. (ick) */
859 for (std::map<std::string,int>::iterator n = ServerInstance->Config->maxbans.begin(); n != ServerInstance->Config->maxbans.end(); n++)
861 if (match(this->name,n->first.c_str()))
863 this->maxbans = n->second;
868 /* Screw it, just return the default of 64 */
870 return this->maxbans;
873 void chanrec::ResetMaxBans()
878 /* returns the status character for a given user on a channel, e.g. @ for op,
879 * % for halfop etc. If the user has several modes set, the highest mode
880 * the user has must be returned.
882 const char* chanrec::GetPrefixChar(userrec *user)
884 static char pf[2] = {0, 0};
886 prefixlist::iterator n = prefixes.find(user);
887 if (n != prefixes.end())
889 if (n->second.size())
891 /* If the user has any prefixes, their highest prefix
892 * will always be at the head of the list, as the list is
893 * sorted in rank order highest first (see SetPrefix()
896 *pf = n->second.begin()->first;
905 const char* chanrec::GetAllPrefixChars(userrec* user)
907 static char prefix[MAXBUF];
911 prefixlist::iterator n = prefixes.find(user);
912 if (n != prefixes.end())
914 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
916 prefix[ctr++] = x->first;
925 unsigned int chanrec::GetPrefixValue(userrec* user)
927 prefixlist::iterator n = prefixes.find(user);
928 if (n != prefixes.end())
930 if (n->second.size())
931 return n->second.begin()->second;
936 int chanrec::GetStatusFlags(userrec *user)
938 UCListIter i = user->chans.find(this);
939 if (i != user->chans.end())
946 int chanrec::GetStatus(userrec *user)
948 if (ServerInstance->ULine(user->server))
951 UCListIter i = user->chans.find(this);
952 if (i != user->chans.end())
954 if ((i->second & UCMODE_OP) > 0)
958 if ((i->second & UCMODE_HOP) > 0)
962 if ((i->second & UCMODE_VOICE) > 0)
966 return STATUS_NORMAL;
968 return STATUS_NORMAL;
971 void chanrec::SetPrefix(userrec* user, char prefix, unsigned int prefix_value, bool adding)
973 prefixlist::iterator n = prefixes.find(user);
974 prefixtype pfx = std::make_pair(prefix,prefix_value);
977 if (n != prefixes.end())
979 if (std::find(n->second.begin(), n->second.end(), pfx) == n->second.end())
981 n->second.push_back(pfx);
982 /* We must keep prefixes in rank order, largest first.
983 * This is for two reasons, firstly because x-chat *ass-u-me's* this
984 * state, and secondly it turns out to be a benefit to us later.
985 * See above in GetPrefix().
987 std::sort(n->second.begin(), n->second.end(), ModeParser::PrefixComparison);
994 prefixes.insert(std::make_pair<userrec*,pfxcontainer>(user, one));
999 if (n != prefixes.end())
1001 pfxcontainer::iterator x = std::find(n->second.begin(), n->second.end(), pfx);
1002 if (x != n->second.end())
1008 void chanrec::RemoveAllPrefixes(userrec* user)
1010 prefixlist::iterator n = prefixes.find(user);
1011 if (n != prefixes.end())