1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
20 #include "configreader.h"
27 chanrec::chanrec(InspIRCd* Instance) : ServerInstance(Instance)
29 *name = *topic = *setby = *key = 0;
30 created = topicset = limit = 0;
31 internal_userlist.clear();
35 void chanrec::SetMode(char mode,bool mode_on)
37 modes[mode-65] = mode_on;
39 this->SetModeParam(mode,"",false);
43 void chanrec::SetModeParam(char mode,const char* parameter,bool mode_on)
45 ServerInstance->Log(DEBUG,"SetModeParam called");
47 CustomModeList::iterator n = custom_mode_params.find(mode);
51 if (n == custom_mode_params.end())
53 custom_mode_params[mode] = strdup(parameter);
54 ServerInstance->Log(DEBUG,"Custom mode parameter %c %s added",mode,parameter);
58 ServerInstance->Log(DEBUG, "Tried to set custom mode parameter for %c '%s' when it was already '%s'", mode, parameter, n->second);
63 if (n != custom_mode_params.end())
66 custom_mode_params.erase(n);
71 bool chanrec::IsModeSet(char mode)
73 return modes[mode-65];
76 std::string chanrec::GetModeParameter(char mode)
84 return ConvToStr(this->limit);
88 CustomModeList::iterator n = custom_mode_params.find(mode);
89 if (n != custom_mode_params.end())
97 long chanrec::GetUserCounter()
99 return (this->internal_userlist.size());
102 void chanrec::AddUser(userrec* user)
104 internal_userlist[user] = user;
107 unsigned long chanrec::DelUser(userrec* user)
109 CUListIter a = internal_userlist.find(user);
111 if (a != internal_userlist.end())
113 internal_userlist.erase(a);
114 /* And tidy any others... */
116 DelHalfoppedUser(user);
120 return internal_userlist.size();
123 bool chanrec::HasUser(userrec* user)
125 return (internal_userlist.find(user) != internal_userlist.end());
128 void chanrec::AddOppedUser(userrec* user)
130 internal_op_userlist[user] = user;
133 void chanrec::DelOppedUser(userrec* user)
135 CUListIter a = internal_op_userlist.find(user);
136 if (a != internal_op_userlist.end())
138 internal_op_userlist.erase(a);
143 void chanrec::AddHalfoppedUser(userrec* user)
145 internal_halfop_userlist[user] = user;
148 void chanrec::DelHalfoppedUser(userrec* user)
150 CUListIter a = internal_halfop_userlist.find(user);
152 if (a != internal_halfop_userlist.end())
154 internal_halfop_userlist.erase(a);
158 void chanrec::AddVoicedUser(userrec* user)
160 internal_voice_userlist[user] = user;
163 void chanrec::DelVoicedUser(userrec* user)
165 CUListIter a = internal_voice_userlist.find(user);
167 if (a != internal_voice_userlist.end())
169 internal_voice_userlist.erase(a);
173 CUList* chanrec::GetUsers()
175 return &internal_userlist;
178 CUList* chanrec::GetOppedUsers()
180 return &internal_op_userlist;
183 CUList* chanrec::GetHalfoppedUsers()
185 return &internal_halfop_userlist;
188 CUList* chanrec::GetVoicedUsers()
190 return &internal_voice_userlist;
194 * add a channel to a user, creating the record for it if needed and linking
195 * it to the user record
197 chanrec* chanrec::JoinUser(InspIRCd* Instance, userrec *user, const char* cn, bool override, const char* key)
202 bool new_channel = false;
205 strlcpy(cname,cn,CHANMAX);
209 chanrec* Ptr = Instance->FindChan(cname);
217 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,NULL,cname,privs));
222 /* create a new one */
223 Ptr = new chanrec(Instance);
224 Instance->chanlist[cname] = Ptr;
226 strlcpy(Ptr->name, cname,CHANMAX);
227 Ptr->modes[CM_TOPICLOCK] = Ptr->modes[CM_NOEXTERNAL] = 1;
228 Ptr->created = Instance->Time();
232 Instance->Log(DEBUG,"chanrec::JoinUser(): created: %s",cname);
237 /* Already on the channel */
238 if (Ptr->HasUser(user))
242 * remote users are allowed us to bypass channel modes
243 * and bans (used by servers)
245 if (IS_LOCAL(user)) /* was a check on fd > -1 */
248 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname,privs));
253 else if (MOD_RESULT == 0)
258 FOREACH_RESULT_I(Instance,I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
261 if ((!key) || strcmp(key,Ptr->key))
263 user->WriteServ("475 %s %s :Cannot join channel (Incorrect channel key)",user->nick, Ptr->name);
268 if (Ptr->modes[CM_INVITEONLY])
271 irc::string xname(Ptr->name);
272 FOREACH_RESULT_I(Instance,I_OnCheckInvite,OnCheckInvite(user, Ptr));
275 if (user->IsInvited(xname))
277 /* user was invited to channel */
278 /* there may be an optional channel NOTICE here */
282 user->WriteServ("473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
286 user->RemoveInvite(xname);
291 FOREACH_RESULT_I(Instance,I_OnCheckLimit,OnCheckLimit(user, Ptr));
294 if (Ptr->GetUserCounter() >= Ptr->limit)
296 user->WriteServ("471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
301 if (Ptr->bans.size())
304 FOREACH_RESULT_I(Instance,I_OnCheckBan,OnCheckBan(user, Ptr));
306 sprintf(mask,"%s!%s@%s",user->nick, user->ident, user->GetIPString());
309 if (Ptr->IsBanned(user))
311 user->WriteServ("474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
320 Instance->Log(DEBUG,"chanrec::JoinUser(): Overridden checks");
324 for (UserChanList::const_iterator index = user->chans.begin(); index != user->chans.end(); index++)
326 if ((*index)->channel == NULL)
328 return chanrec::ForceChan(Instance, Ptr, *index, user, privs);
333 * XXX: If the user is an oper here, we can just extend their user->chans vector by one
334 * and put the channel in here. Same for remote users which are not bound by
335 * the channel limits. Otherwise, nope, youre boned.
337 if (!IS_LOCAL(user)) /* was a check on fd < 0 */
339 ucrec* a = new ucrec();
340 chanrec* c = chanrec::ForceChan(Instance, Ptr, a, user, privs);
341 user->chans.push_back(a);
344 else if (*user->oper)
346 /* Oper allows extension up to the OPERMAXCHANS value */
347 if (user->chans.size() < OPERMAXCHANS)
349 ucrec* a = new ucrec();
350 chanrec* c = chanrec::ForceChan(Instance, Ptr, a, user, privs);
351 user->chans.push_back(a);
356 user->WriteServ("405 %s %s :You are on too many channels",user->nick, cname);
360 Instance->Log(DEBUG,"BLAMMO, Whacking channel.");
361 /* Things went seriously pear shaped, so take this away. bwahaha. */
362 chan_hash::iterator n = Instance->chanlist.find(cname);
363 if (n != Instance->chanlist.end())
367 Instance->chanlist.erase(n);
368 for (unsigned int index =0; index < user->chans.size(); index++)
370 if (user->chans[index]->channel == Ptr)
372 user->chans[index]->channel = NULL;
373 user->chans[index]->uc_modes = 0;
380 for (unsigned int index =0; index < user->chans.size(); index++)
382 if (user->chans[index]->channel == Ptr)
384 user->chans[index]->channel = NULL;
385 user->chans[index]->uc_modes = 0;
392 chanrec* chanrec::ForceChan(InspIRCd* Instance, chanrec* Ptr,ucrec *a,userrec* user, const std::string &privs)
396 for (std::string::const_iterator x = privs.begin(); x != privs.end(); x++)
398 const char status = *x;
402 a->uc_modes = UCMODE_OP;
405 a->uc_modes = UCMODE_HOP;
408 a->uc_modes = UCMODE_VOICE;
411 ModeHandler* mh = Instance->Modes->FindPrefix(status);
414 Ptr->SetPrefix(user, status, mh->GetPrefixRank(), true);
420 user->ModChannelCount(1);
421 Ptr->WriteChannel(user,"JOIN :%s",Ptr->name);
423 /* Theyre not the first ones in here, make sure everyone else sees the modes we gave the user */
424 std::string ms = Instance->Modes->ModeString(user, Ptr);
425 if ((Ptr->GetUserCounter() > 1) && (ms.length()))
426 Ptr->WriteAllExceptSender(user, true, 0, "MODE %s +%s", Ptr->name, ms.c_str());
428 /* Major improvement by Brain - we dont need to be calculating all this pointlessly for remote users */
433 user->WriteServ("332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
434 user->WriteServ("333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
438 FOREACH_MOD_I(Instance,I_OnUserJoin,OnUserJoin(user,Ptr));
442 bool chanrec::IsBanned(userrec* user)
445 sprintf(mask,"%s!%s@%s",user->nick, user->ident, user->GetIPString());
446 for (BanList::iterator i = this->bans.begin(); i != this->bans.end(); i++)
448 /* This allows CIDR ban matching
450 * Full masked host Full unmasked host IP with/without CIDR
452 if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match(mask, i->data, true)))
462 * remove a channel from a users record, and remove the record from the hash
463 * if the channel has become empty
465 long chanrec::PartUser(userrec *user, const char* reason)
468 return this->GetUserCounter();
470 for (unsigned int i =0; i < user->chans.size(); i++)
472 /* zap it from the channel list of the user */
473 if (user->chans[i]->channel == this)
477 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, reason));
478 this->WriteChannel(user, "PART %s :%s", this->name, reason);
482 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, ""));
483 this->WriteChannel(user, "PART :%s", this->name);
485 user->chans[i]->uc_modes = 0;
486 user->chans[i]->channel = NULL;
487 user->ModChannelCount(-1);
488 this->RemoveAllPrefixes(user);
493 if (!this->DelUser(user)) /* if there are no users left on the channel... */
495 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
496 /* kill the record */
497 if (iter != ServerInstance->chanlist.end())
499 ServerInstance->Log(DEBUG,"del_channel: destroyed: %s", this->name);
500 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
501 ServerInstance->chanlist.erase(iter);
506 return this->GetUserCounter();
509 long chanrec::ServerKickUser(userrec* user, const char* reason, bool triggerevents)
511 if (!user || !reason)
512 return this->GetUserCounter();
516 if (!this->HasUser(user))
519 return this->GetUserCounter();
525 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL,user,this,reason));
528 for (unsigned int i =0; i < user->chans.size(); i++)
530 if (user->chans[i]->channel == this)
532 this->WriteChannelWithServ(ServerInstance->Config->ServerName, "KICK %s %s :%s", this->name, user->nick, reason);
533 user->chans[i]->uc_modes = 0;
534 user->chans[i]->channel = NULL;
535 this->RemoveAllPrefixes(user);
540 if (!this->DelUser(user))
542 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
543 /* kill the record */
544 if (iter != ServerInstance->chanlist.end())
546 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
547 ServerInstance->chanlist.erase(iter);
552 return this->GetUserCounter();
555 long chanrec::KickUser(userrec *src, userrec *user, const char* reason)
557 if (!src || !user || !reason)
558 return this->GetUserCounter();
562 if (!this->HasUser(user))
564 src->WriteServ("441 %s %s %s :They are not on that channel",src->nick, user->nick, this->name);
565 return this->GetUserCounter();
567 if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
569 src->WriteServ("482 %s %s :Only a u-line may kick a u-line from a channel.",src->nick, this->name);
570 return this->GetUserCounter();
574 if (!ServerInstance->ULine(src->server))
577 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,this,reason));
579 return this->GetUserCounter();
581 /* Set to -1 by OnUserPreKick if explicit allow was set */
582 if (MOD_RESULT != -1)
584 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,this,AC_KICK));
585 if ((MOD_RESULT == ACR_DENY) && (!ServerInstance->ULine(src->server)))
586 return this->GetUserCounter();
588 if ((MOD_RESULT == ACR_DEFAULT) || (!ServerInstance->ULine(src->server)))
590 int them = this->GetStatus(src);
591 int us = this->GetStatus(user);
592 if ((them < STATUS_HOP) || (them < us))
594 if (them == STATUS_HOP)
596 src->WriteServ("482 %s %s :You must be a channel operator",src->nick, this->name);
600 src->WriteServ("482 %s %s :You must be at least a half-operator",src->nick, this->name);
602 return this->GetUserCounter();
608 FOREACH_MOD(I_OnUserKick,OnUserKick(src,user,this,reason));
610 for (UserChanList::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
612 /* zap it from the channel list of the user */
613 if ((*i)->channel == this)
615 this->WriteChannel(src, "KICK %s %s :%s", this->name, user->nick, reason);
617 (*i)->channel = NULL;
618 this->RemoveAllPrefixes(user);
623 if (!this->DelUser(user))
624 /* if there are no users left on the channel */
626 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
628 /* kill the record */
629 if (iter != ServerInstance->chanlist.end())
631 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
632 ServerInstance->chanlist.erase(iter);
637 return this->GetUserCounter();
640 void chanrec::WriteChannel(userrec* user, char* text, ...)
642 char textbuffer[MAXBUF];
648 va_start(argsPtr, text);
649 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
652 this->WriteChannel(user, std::string(textbuffer));
655 void chanrec::WriteChannel(userrec* user, const std::string &text)
657 CUList *ulist = this->GetUsers();
662 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
664 if (IS_LOCAL(i->second))
665 user->WriteTo(i->second,text);
669 void chanrec::WriteChannelWithServ(const char* ServName, const char* text, ...)
671 char textbuffer[MAXBUF];
677 va_start(argsPtr, text);
678 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
681 this->WriteChannelWithServ(ServName, std::string(textbuffer));
684 void chanrec::WriteChannelWithServ(const char* ServName, const std::string &text)
686 CUList *ulist = this->GetUsers();
688 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
690 if (IS_LOCAL(i->second))
691 i->second->WriteServ(text);
695 /* write formatted text from a source user to all users on a channel except
696 * for the sender (for privmsg etc) */
697 void chanrec::WriteAllExceptSender(userrec* user, bool serversource, char status, char* text, ...)
699 char textbuffer[MAXBUF];
705 va_start(argsPtr, text);
706 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
709 this->WriteAllExceptSender(user, serversource, status, std::string(textbuffer));
712 void chanrec::WriteAllExceptSender(userrec* user, bool serversource, char status, const std::string& text)
719 ulist = this->GetOppedUsers();
722 ulist = this->GetHalfoppedUsers();
725 ulist = this->GetVoicedUsers();
728 ulist = this->GetUsers();
732 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
734 if ((IS_LOCAL(i->second)) && (user != i->second))
737 i->second->WriteServ(text);
739 i->second->WriteFrom(user,text);
745 * return a count of the users on a specific channel accounting for
746 * invisible users who won't increase the count. e.g. for /LIST
748 int chanrec::CountInvisible()
751 CUList *ulist= this->GetUsers();
752 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
754 if (!(i->second->modes[UM_INVISIBLE]))
761 char* chanrec::ChanModes(bool showkey)
763 static char scratch[MAXBUF];
764 static char sparam[MAXBUF];
765 char* offset = scratch;
766 std::string extparam = "";
771 /* This was still iterating up to 190, chanrec::custom_modes is only 64 elements -- Om */
772 for(int n = 0; n < 64; n++)
781 extparam = (showkey ? this->key : "<key>");
784 extparam = ConvToStr(this->limit);
792 /* We know these have no parameters */
795 extparam = this->GetModeParameter(n + 65);
800 charlcat(sparam,' ',MAXBUF);
801 strlcat(sparam,extparam.c_str(),MAXBUF);
806 /* Null terminate scratch */
808 strlcat(scratch,sparam,MAXBUF);
812 /* compile a userlist of a channel into a string, each nick seperated by
813 * spaces and op, voice etc status shown as @ and +, and send it to 'user'
815 void chanrec::UserList(userrec *user)
821 FOREACH_RESULT(I_OnUserList,OnUserList(user, this));
822 ServerInstance->Log(DEBUG,"MOD_RESULT for UserList = %d",MOD_RESULT);
826 ServerInstance->Log(DEBUG,"Using builtin NAMES list generation");
828 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
831 char* ptr = list + dlen;
833 CUList *ulist= this->GetUsers();
835 /* Improvement by Brain - this doesnt change in value, so why was it inside
838 bool has_user = this->HasUser(user);
840 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
842 if ((!has_user) && (i->second->modes[UM_INVISIBLE]))
845 * user is +i, and source not on the channel, does not show
851 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", this->GetPrefixChar(i->second), i->second->nick);
858 if (curlen > (480-NICKMAX))
860 /* list overflowed into multiple numerics */
861 user->WriteServ(std::string(list));
863 /* reset our lengths */
864 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
872 /* if whats left in the list isnt empty, send it */
875 user->WriteServ(std::string(list));
878 user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, this->name);
881 long chanrec::GetMaxBans()
884 for (std::map<std::string,int>::iterator n = ServerInstance->Config->maxbans.begin(); n != ServerInstance->Config->maxbans.end(); n++)
887 if (match(this->name,x.c_str()))
896 /* returns the status character for a given user on a channel, e.g. @ for op,
897 * % for halfop etc. If the user has several modes set, the highest mode
898 * the user has must be returned.
900 const char* chanrec::GetPrefixChar(userrec *user)
908 prefixlist::iterator n = prefixes.find(user);
909 if (n != prefixes.end())
911 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
924 const char* chanrec::GetAllPrefixChars(userrec* user)
926 static char prefix[MAXBUF];
930 prefixlist::iterator n = prefixes.find(user);
931 if (n != prefixes.end())
933 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
935 prefix[ctr++] = x->first;
944 unsigned int chanrec::GetPrefixValue(userrec* user)
948 prefixlist::iterator n = prefixes.find(user);
949 if (n != prefixes.end())
951 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
962 int chanrec::GetStatusFlags(userrec *user)
964 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
966 if ((*i)->channel == this)
968 return (*i)->uc_modes;
975 int chanrec::GetStatus(userrec *user)
977 if (ServerInstance->ULine(user->server))
980 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
982 if ((*i)->channel == this)
984 if (((*i)->uc_modes & UCMODE_OP) > 0)
988 if (((*i)->uc_modes & UCMODE_HOP) > 0)
992 if (((*i)->uc_modes & UCMODE_VOICE) > 0)
996 return STATUS_NORMAL;
999 return STATUS_NORMAL;
1002 /*bool ModeParser::PrefixComparison(const prefixtype one, const prefixtype two)
1004 return one.second > two.second;
1007 void chanrec::SetPrefix(userrec* user, char prefix, unsigned int prefix_value, bool adding)
1009 prefixlist::iterator n = prefixes.find(user);
1010 prefixtype pfx = std::make_pair(prefix,prefix_value);
1013 if (n != prefixes.end())
1015 if (std::find(n->second.begin(), n->second.end(), pfx) == n->second.end())
1017 n->second.push_back(pfx);
1018 std::sort(n->second.begin(), n->second.end(), ModeParser::PrefixComparison);
1025 prefixes.insert(std::make_pair<userrec*,pfxcontainer>(user, one));
1030 if (n != prefixes.end())
1032 pfxcontainer::iterator x = std::find(n->second.begin(), n->second.end(), pfx);
1033 if (x != n->second.end())
1039 void chanrec::RemoveAllPrefixes(userrec* user)
1041 prefixlist::iterator n = prefixes.find(user);
1042 if (n != prefixes.end())