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)
205 strlcpy(cname,cn,CHANMAX);
207 chanrec* Ptr = Instance->FindChan(cname);
214 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,NULL,cname));
219 /* create a new one */
220 Ptr = new chanrec(Instance);
221 Instance->chanlist[cname] = Ptr;
223 strlcpy(Ptr->name, cname,CHANMAX);
224 Ptr->modes[CM_TOPICLOCK] = Ptr->modes[CM_NOEXTERNAL] = 1;
225 Ptr->created = Instance->Time();
227 strlcpy(Ptr->setby, user->nick,NICKMAX-1);
229 Instance->Log(DEBUG,"chanrec::JoinUser(): created: %s",cname);
231 * set created to 2 to indicate user
232 * is the first in the channel
233 * and should be given ops
239 /* Already on the channel */
240 if (Ptr->HasUser(user))
244 * remote users are allowed us to bypass channel modes
245 * and bans (used by servers)
247 if (IS_LOCAL(user)) /* was a check on fd > -1 */
250 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname));
255 else if (MOD_RESULT == 0)
260 FOREACH_RESULT_I(Instance,I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
265 Instance->Log(DEBUG,"chanrec::JoinUser(): no key given in JOIN");
266 user->WriteServ("475 %s %s :Cannot join channel (Requires key)",user->nick, Ptr->name);
271 if (strcmp(key,Ptr->key))
273 Instance->Log(DEBUG,"chanrec::JoinUser(): bad key given in JOIN");
274 user->WriteServ("475 %s %s :Cannot join channel (Incorrect key)",user->nick, Ptr->name);
280 if (Ptr->modes[CM_INVITEONLY])
283 irc::string xname(Ptr->name);
284 FOREACH_RESULT_I(Instance,I_OnCheckInvite,OnCheckInvite(user, Ptr));
287 if (user->IsInvited(xname))
289 /* user was invited to channel */
290 /* there may be an optional channel NOTICE here */
294 user->WriteServ("473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
298 user->RemoveInvite(xname);
303 FOREACH_RESULT_I(Instance,I_OnCheckLimit,OnCheckLimit(user, Ptr));
306 if (Ptr->GetUserCounter() >= Ptr->limit)
308 user->WriteServ("471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
313 if (Ptr->bans.size())
316 FOREACH_RESULT_I(Instance,I_OnCheckBan,OnCheckBan(user, Ptr));
318 sprintf(mask,"%s!%s@%s",user->nick, user->ident, user->GetIPString());
321 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
323 /* This allows CIDR ban matching
325 * Full masked host Full unmasked host IP with/without CIDR
327 if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match(mask, i->data, true)))
329 user->WriteServ("474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
339 Instance->Log(DEBUG,"chanrec::JoinUser(): Overridden checks");
344 for (UserChanList::const_iterator index = user->chans.begin(); index != user->chans.end(); index++)
346 if ((*index)->channel == NULL)
348 return chanrec::ForceChan(Instance, Ptr, *index, user, created);
353 * XXX: If the user is an oper here, we can just extend their user->chans vector by one
354 * and put the channel in here. Same for remote users which are not bound by
355 * the channel limits. Otherwise, nope, youre boned.
357 if (!IS_LOCAL(user)) /* was a check on fd < 0 */
359 ucrec* a = new ucrec();
360 chanrec* c = chanrec::ForceChan(Instance, Ptr,a,user,created);
361 user->chans.push_back(a);
364 else if (*user->oper)
366 /* Oper allows extension up to the OPERMAXCHANS value */
367 if (user->chans.size() < OPERMAXCHANS)
369 ucrec* a = new ucrec();
370 chanrec* c = chanrec::ForceChan(Instance, Ptr,a,user,created);
371 user->chans.push_back(a);
376 user->WriteServ("405 %s %s :You are on too many channels",user->nick, cname);
380 Instance->Log(DEBUG,"BLAMMO, Whacking channel.");
381 /* Things went seriously pear shaped, so take this away. bwahaha. */
382 chan_hash::iterator n = Instance->chanlist.find(cname);
383 if (n != Instance->chanlist.end())
387 Instance->chanlist.erase(n);
388 for (unsigned int index =0; index < user->chans.size(); index++)
390 if (user->chans[index]->channel == Ptr)
392 user->chans[index]->channel = NULL;
393 user->chans[index]->uc_modes = 0;
400 for (unsigned int index =0; index < user->chans.size(); index++)
402 if (user->chans[index]->channel == Ptr)
404 user->chans[index]->channel = NULL;
405 user->chans[index]->uc_modes = 0;
412 chanrec* chanrec::ForceChan(InspIRCd* Instance, chanrec* Ptr,ucrec *a,userrec* user, int created)
416 /* first user in is given ops */
417 a->uc_modes = UCMODE_OP;
418 Ptr->AddOppedUser(user);
419 Ptr->SetPrefix(user, '@', OP_VALUE, true);
428 user->ModChannelCount(1);
429 Ptr->WriteChannel(user,"JOIN :%s",Ptr->name);
431 /* Major improvement by Brain - we dont need to be calculating all this pointlessly for remote users */
436 user->WriteServ("332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
437 user->WriteServ("333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
441 FOREACH_MOD_I(Instance,I_OnUserJoin,OnUserJoin(user,Ptr));
446 * remove a channel from a users record, and remove the record from the hash
447 * if the channel has become empty
449 long chanrec::PartUser(userrec *user, const char* reason)
452 return this->GetUserCounter();
454 for (unsigned int i =0; i < user->chans.size(); i++)
456 /* zap it from the channel list of the user */
457 if (user->chans[i]->channel == this)
461 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, reason));
462 this->WriteChannel(user, "PART %s :%s", this->name, reason);
466 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, ""));
467 this->WriteChannel(user, "PART :%s", this->name);
469 user->chans[i]->uc_modes = 0;
470 user->chans[i]->channel = NULL;
471 user->ModChannelCount(-1);
472 this->RemoveAllPrefixes(user);
477 if (!this->DelUser(user)) /* if there are no users left on the channel... */
479 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
480 /* kill the record */
481 if (iter != ServerInstance->chanlist.end())
483 ServerInstance->Log(DEBUG,"del_channel: destroyed: %s", this->name);
484 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
485 ServerInstance->chanlist.erase(iter);
490 return this->GetUserCounter();
493 long chanrec::ServerKickUser(userrec* user, const char* reason, bool triggerevents)
495 if (!user || !reason)
496 return this->GetUserCounter();
500 if (!this->HasUser(user))
503 return this->GetUserCounter();
509 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL,user,this,reason));
512 for (unsigned int i =0; i < user->chans.size(); i++)
514 if (user->chans[i]->channel == this)
516 this->WriteChannelWithServ(ServerInstance->Config->ServerName, "KICK %s %s :%s", this->name, user->nick, reason);
517 user->chans[i]->uc_modes = 0;
518 user->chans[i]->channel = NULL;
519 this->RemoveAllPrefixes(user);
524 if (!this->DelUser(user))
526 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
527 /* kill the record */
528 if (iter != ServerInstance->chanlist.end())
530 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
531 ServerInstance->chanlist.erase(iter);
536 return this->GetUserCounter();
539 long chanrec::KickUser(userrec *src, userrec *user, const char* reason)
541 if (!src || !user || !reason)
542 return this->GetUserCounter();
546 if (!this->HasUser(user))
548 src->WriteServ("441 %s %s %s :They are not on that channel",src->nick, user->nick, this->name);
549 return this->GetUserCounter();
551 if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
553 src->WriteServ("482 %s %s :Only a u-line may kick a u-line from a channel.",src->nick, this->name);
554 return this->GetUserCounter();
558 if (!ServerInstance->ULine(src->server))
561 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,this,reason));
563 return this->GetUserCounter();
565 /* Set to -1 by OnUserPreKick if explicit allow was set */
566 if (MOD_RESULT != -1)
568 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,this,AC_KICK));
569 if ((MOD_RESULT == ACR_DENY) && (!ServerInstance->ULine(src->server)))
570 return this->GetUserCounter();
572 if ((MOD_RESULT == ACR_DEFAULT) || (!ServerInstance->ULine(src->server)))
574 int them = this->GetStatus(src);
575 int us = this->GetStatus(user);
576 if ((them < STATUS_HOP) || (them < us))
578 if (them == STATUS_HOP)
580 src->WriteServ("482 %s %s :You must be a channel operator",src->nick, this->name);
584 src->WriteServ("482 %s %s :You must be at least a half-operator",src->nick, this->name);
586 return this->GetUserCounter();
592 FOREACH_MOD(I_OnUserKick,OnUserKick(src,user,this,reason));
594 for (UserChanList::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
596 /* zap it from the channel list of the user */
597 if ((*i)->channel == this)
599 this->WriteChannel(src, "KICK %s %s :%s", this->name, user->nick, reason);
601 (*i)->channel = NULL;
602 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();
646 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
648 if (IS_LOCAL(i->second))
649 user->WriteTo(i->second,text);
653 void chanrec::WriteChannelWithServ(const char* ServName, const char* text, ...)
655 char textbuffer[MAXBUF];
661 va_start(argsPtr, text);
662 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
665 this->WriteChannelWithServ(ServName, std::string(textbuffer));
668 void chanrec::WriteChannelWithServ(const char* ServName, const std::string &text)
670 CUList *ulist = this->GetUsers();
672 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
674 if (IS_LOCAL(i->second))
675 i->second->WriteServ(text);
679 /* write formatted text from a source user to all users on a channel except
680 * for the sender (for privmsg etc) */
681 void chanrec::WriteAllExceptSender(userrec* user, char status, char* text, ...)
683 char textbuffer[MAXBUF];
689 va_start(argsPtr, text);
690 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
693 this->WriteAllExceptSender(user, status, std::string(textbuffer));
696 void chanrec::WriteAllExceptSender(userrec* user, char status, const std::string& text)
706 ulist = this->GetOppedUsers();
709 ulist = this->GetHalfoppedUsers();
712 ulist = this->GetVoicedUsers();
715 ulist = this->GetUsers();
719 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
721 if ((IS_LOCAL(i->second)) && (user != i->second))
722 i->second->WriteFrom(user,text);
727 * return a count of the users on a specific channel accounting for
728 * invisible users who won't increase the count. e.g. for /LIST
730 int chanrec::CountInvisible()
733 CUList *ulist= this->GetUsers();
734 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
736 if (!(i->second->modes[UM_INVISIBLE]))
743 char* chanrec::ChanModes(bool showkey)
745 static char scratch[MAXBUF];
746 static char sparam[MAXBUF];
747 char* offset = scratch;
748 std::string extparam = "";
753 /* This was still iterating up to 190, chanrec::custom_modes is only 64 elements -- Om */
754 for(int n = 0; n < 64; n++)
763 extparam = (showkey ? this->key : "<key>");
766 extparam = ConvToStr(this->limit);
774 /* We know these have no parameters */
777 extparam = this->GetModeParameter(n + 65);
782 charlcat(sparam,' ',MAXBUF);
783 strlcat(sparam,extparam.c_str(),MAXBUF);
788 /* Null terminate scratch */
790 strlcat(scratch,sparam,MAXBUF);
794 /* compile a userlist of a channel into a string, each nick seperated by
795 * spaces and op, voice etc status shown as @ and +, and send it to 'user'
797 void chanrec::UserList(userrec *user)
803 FOREACH_RESULT(I_OnUserList,OnUserList(user, this));
804 ServerInstance->Log(DEBUG,"MOD_RESULT for UserList = %d",MOD_RESULT);
808 ServerInstance->Log(DEBUG,"Using builtin NAMES list generation");
810 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
813 char* ptr = list + dlen;
815 CUList *ulist= this->GetUsers();
817 /* Improvement by Brain - this doesnt change in value, so why was it inside
820 bool has_user = this->HasUser(user);
822 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
824 if ((!has_user) && (i->second->modes[UM_INVISIBLE]))
827 * user is +i, and source not on the channel, does not show
833 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", this->GetPrefixChar(i->second), i->second->nick);
840 if (curlen > (480-NICKMAX))
842 /* list overflowed into multiple numerics */
843 user->WriteServ(list);
845 /* reset our lengths */
846 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
854 /* if whats left in the list isnt empty, send it */
857 user->WriteServ(list);
860 user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, this->name);
863 long chanrec::GetMaxBans()
866 for (std::map<std::string,int>::iterator n = ServerInstance->Config->maxbans.begin(); n != ServerInstance->Config->maxbans.end(); n++)
869 if (match(this->name,x.c_str()))
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)
890 prefixlist::iterator n = prefixes.find(user);
891 if (n != prefixes.end())
893 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
906 const char* chanrec::GetAllPrefixChars(userrec* user)
908 static char prefix[MAXBUF];
912 prefixlist::iterator n = prefixes.find(user);
913 if (n != prefixes.end())
915 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
917 prefix[ctr++] = x->first;
926 unsigned int chanrec::GetPrefixValue(userrec* user)
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++)
944 int chanrec::GetStatusFlags(userrec *user)
946 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
948 if ((*i)->channel == this)
950 return (*i)->uc_modes;
957 int chanrec::GetStatus(userrec *user)
959 if (ServerInstance->ULine(user->server))
962 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
964 if ((*i)->channel == this)
966 if (((*i)->uc_modes & UCMODE_OP) > 0)
970 if (((*i)->uc_modes & UCMODE_HOP) > 0)
974 if (((*i)->uc_modes & UCMODE_VOICE) > 0)
978 return STATUS_NORMAL;
981 return STATUS_NORMAL;
984 /*bool ModeParser::PrefixComparison(const prefixtype one, const prefixtype two)
986 return one.second > two.second;
989 void chanrec::SetPrefix(userrec* user, char prefix, unsigned int prefix_value, bool adding)
991 prefixlist::iterator n = prefixes.find(user);
992 prefixtype pfx = std::make_pair(prefix,prefix_value);
995 if (n != prefixes.end())
997 if (std::find(n->second.begin(), n->second.end(), pfx) == n->second.end())
999 n->second.push_back(pfx);
1000 std::sort(n->second.begin(), n->second.end(), ModeParser::PrefixComparison);
1007 prefixes.insert(std::make_pair<userrec*,pfxcontainer>(user, one));
1012 if (n != prefixes.end())
1014 pfxcontainer::iterator x = std::find(n->second.begin(), n->second.end(), pfx);
1015 if (x != n->second.end())
1021 void chanrec::RemoveAllPrefixes(userrec* user)
1023 prefixlist::iterator n = prefixes.find(user);
1024 if (n != prefixes.end())