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 * ---------------------------------------------------
25 #include "configreader.h"
37 #include "inspstring.h"
41 chanrec::chanrec(InspIRCd* Instance) : ServerInstance(Instance)
43 *name = *topic = *setby = *key = 0;
44 created = topicset = limit = 0;
45 internal_userlist.clear();
49 void chanrec::SetMode(char mode,bool mode_on)
51 modes[mode-65] = mode_on;
53 this->SetModeParam(mode,"",false);
57 void chanrec::SetModeParam(char mode,const char* parameter,bool mode_on)
59 ServerInstance->Log(DEBUG,"SetModeParam called");
61 CustomModeList::iterator n = custom_mode_params.find(mode);
65 if (n == custom_mode_params.end())
67 custom_mode_params[mode] = strdup(parameter);
68 ServerInstance->Log(DEBUG,"Custom mode parameter %c %s added",mode,parameter);
72 ServerInstance->Log(DEBUG, "Tried to set custom mode parameter for %c '%s' when it was already '%s'", mode, parameter, n->second);
77 if (n != custom_mode_params.end())
80 custom_mode_params.erase(n);
85 bool chanrec::IsModeSet(char mode)
87 return modes[mode-65];
90 std::string chanrec::GetModeParameter(char mode)
98 return ConvToStr(this->limit);
102 CustomModeList::iterator n = custom_mode_params.find(mode);
103 if (n != custom_mode_params.end())
111 long chanrec::GetUserCounter()
113 return (this->internal_userlist.size());
116 void chanrec::AddUser(userrec* user)
118 internal_userlist[user] = user;
121 unsigned long chanrec::DelUser(userrec* user)
123 CUListIter a = internal_userlist.find(user);
125 if (a != internal_userlist.end())
127 internal_userlist.erase(a);
128 /* And tidy any others... */
130 DelHalfoppedUser(user);
134 return internal_userlist.size();
137 bool chanrec::HasUser(userrec* user)
139 return (internal_userlist.find(user) != internal_userlist.end());
142 void chanrec::AddOppedUser(userrec* user)
144 internal_op_userlist[user] = user;
147 void chanrec::DelOppedUser(userrec* user)
149 CUListIter a = internal_op_userlist.find(user);
150 if (a != internal_op_userlist.end())
152 internal_op_userlist.erase(a);
157 void chanrec::AddHalfoppedUser(userrec* user)
159 internal_halfop_userlist[user] = user;
162 void chanrec::DelHalfoppedUser(userrec* user)
164 CUListIter a = internal_halfop_userlist.find(user);
166 if (a != internal_halfop_userlist.end())
168 internal_halfop_userlist.erase(a);
172 void chanrec::AddVoicedUser(userrec* user)
174 internal_voice_userlist[user] = user;
177 void chanrec::DelVoicedUser(userrec* user)
179 CUListIter a = internal_voice_userlist.find(user);
181 if (a != internal_voice_userlist.end())
183 internal_voice_userlist.erase(a);
187 CUList* chanrec::GetUsers()
189 return &internal_userlist;
192 CUList* chanrec::GetOppedUsers()
194 return &internal_op_userlist;
197 CUList* chanrec::GetHalfoppedUsers()
199 return &internal_halfop_userlist;
202 CUList* chanrec::GetVoicedUsers()
204 return &internal_voice_userlist;
208 * add a channel to a user, creating the record for it if needed and linking
209 * it to the user record
211 chanrec* chanrec::JoinUser(InspIRCd* Instance, userrec *user, const char* cn, bool override, const char* key)
219 strlcpy(cname,cn,CHANMAX);
221 chanrec* Ptr = Instance->FindChan(cname);
228 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,NULL,cname));
233 /* create a new one */
234 Ptr = new chanrec(Instance);
235 Instance->chanlist[cname] = Ptr;
237 strlcpy(Ptr->name, cname,CHANMAX);
238 Ptr->modes[CM_TOPICLOCK] = Ptr->modes[CM_NOEXTERNAL] = 1;
239 Ptr->created = Instance->Time();
241 strlcpy(Ptr->setby, user->nick,NICKMAX-1);
243 Instance->Log(DEBUG,"chanrec::JoinUser(): created: %s",cname);
245 * set created to 2 to indicate user
246 * is the first in the channel
247 * and should be given ops
253 /* Already on the channel */
254 if (Ptr->HasUser(user))
258 * remote users are allowed us to bypass channel modes
259 * and bans (used by servers)
261 if (IS_LOCAL(user)) /* was a check on fd > -1 */
264 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname));
269 else if (MOD_RESULT == 0)
274 FOREACH_RESULT_I(Instance,I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
279 Instance->Log(DEBUG,"chanrec::JoinUser(): no key given in JOIN");
280 user->WriteServ("475 %s %s :Cannot join channel (Requires key)",user->nick, Ptr->name);
285 if (strcmp(key,Ptr->key))
287 Instance->Log(DEBUG,"chanrec::JoinUser(): bad key given in JOIN");
288 user->WriteServ("475 %s %s :Cannot join channel (Incorrect key)",user->nick, Ptr->name);
294 if (Ptr->modes[CM_INVITEONLY])
297 irc::string xname(Ptr->name);
298 FOREACH_RESULT_I(Instance,I_OnCheckInvite,OnCheckInvite(user, Ptr));
301 if (user->IsInvited(xname))
303 /* user was invited to channel */
304 /* there may be an optional channel NOTICE here */
308 user->WriteServ("473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
312 user->RemoveInvite(xname);
317 FOREACH_RESULT_I(Instance,I_OnCheckLimit,OnCheckLimit(user, Ptr));
320 if (Ptr->GetUserCounter() >= Ptr->limit)
322 user->WriteServ("471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
327 if (Ptr->bans.size())
330 FOREACH_RESULT_I(Instance,I_OnCheckBan,OnCheckBan(user, Ptr));
332 sprintf(mask,"%s!%s@%s",user->nick, user->ident, user->GetIPString());
335 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
337 /* This allows CIDR ban matching
339 * Full masked host Full unmasked host IP with/without CIDR
341 if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match(mask, i->data, true)))
343 user->WriteServ("474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
353 Instance->Log(DEBUG,"chanrec::JoinUser(): Overridden checks");
358 for (UserChanList::const_iterator index = user->chans.begin(); index != user->chans.end(); index++)
360 if ((*index)->channel == NULL)
362 return chanrec::ForceChan(Instance, Ptr, *index, user, created);
367 * XXX: If the user is an oper here, we can just extend their user->chans vector by one
368 * and put the channel in here. Same for remote users which are not bound by
369 * the channel limits. Otherwise, nope, youre boned.
371 if (!IS_LOCAL(user)) /* was a check on fd < 0 */
373 ucrec* a = new ucrec();
374 chanrec* c = chanrec::ForceChan(Instance, Ptr,a,user,created);
375 user->chans.push_back(a);
378 else if (*user->oper)
380 /* Oper allows extension up to the OPERMAXCHANS value */
381 if (user->chans.size() < OPERMAXCHANS)
383 ucrec* a = new ucrec();
384 chanrec* c = chanrec::ForceChan(Instance, Ptr,a,user,created);
385 user->chans.push_back(a);
390 user->WriteServ("405 %s %s :You are on too many channels",user->nick, cname);
394 Instance->Log(DEBUG,"BLAMMO, Whacking channel.");
395 /* Things went seriously pear shaped, so take this away. bwahaha. */
396 chan_hash::iterator n = Instance->chanlist.find(cname);
397 if (n != Instance->chanlist.end())
401 Instance->chanlist.erase(n);
402 for (unsigned int index =0; index < user->chans.size(); index++)
404 if (user->chans[index]->channel == Ptr)
406 user->chans[index]->channel = NULL;
407 user->chans[index]->uc_modes = 0;
414 for (unsigned int index =0; index < user->chans.size(); index++)
416 if (user->chans[index]->channel == Ptr)
418 user->chans[index]->channel = NULL;
419 user->chans[index]->uc_modes = 0;
426 chanrec* chanrec::ForceChan(InspIRCd* Instance, chanrec* Ptr,ucrec *a,userrec* user, int created)
430 /* first user in is given ops */
431 a->uc_modes = UCMODE_OP;
432 Ptr->AddOppedUser(user);
433 Ptr->SetPrefix(user, '@', OP_VALUE, true);
442 Ptr->WriteChannel(user,"JOIN :%s",Ptr->name);
444 /* Major improvement by Brain - we dont need to be calculating all this pointlessly for remote users */
449 user->WriteServ("332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
450 user->WriteServ("333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
453 user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
455 FOREACH_MOD_I(Instance,I_OnUserJoin,OnUserJoin(user,Ptr));
460 * remove a channel from a users record, and remove the record from the hash
461 * if the channel has become empty
463 long chanrec::PartUser(userrec *user, const char* reason)
466 return this->GetUserCounter();
468 for (unsigned int i =0; i < user->chans.size(); i++)
470 /* zap it from the channel list of the user */
471 if (user->chans[i]->channel == this)
475 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, reason));
476 this->WriteChannel(user, "PART %s :%s", this->name, reason);
480 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, ""));
481 this->WriteChannel(user, "PART :%s", this->name);
483 user->chans[i]->uc_modes = 0;
484 user->chans[i]->channel = NULL;
489 if (!this->DelUser(user)) /* if there are no users left on the channel... */
491 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
492 /* kill the record */
493 if (iter != ServerInstance->chanlist.end())
495 ServerInstance->Log(DEBUG,"del_channel: destroyed: %s", this->name);
496 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
497 ServerInstance->chanlist.erase(iter);
502 return this->GetUserCounter();
505 long chanrec::ServerKickUser(userrec* user, const char* reason, bool triggerevents)
507 if (!user || !reason)
508 return this->GetUserCounter();
512 if (!this->HasUser(user))
515 return this->GetUserCounter();
521 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL,user,this,reason));
524 for (unsigned int i =0; i < user->chans.size(); i++)
526 if (user->chans[i]->channel == this)
528 this->WriteChannelWithServ(ServerInstance->Config->ServerName, "KICK %s %s :%s", this->name, user->nick, reason);
529 user->chans[i]->uc_modes = 0;
530 user->chans[i]->channel = NULL;
535 if (!this->DelUser(user))
537 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
538 /* kill the record */
539 if (iter != ServerInstance->chanlist.end())
541 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
542 ServerInstance->chanlist.erase(iter);
547 return this->GetUserCounter();
550 long chanrec::KickUser(userrec *src, userrec *user, const char* reason)
552 if (!src || !user || !reason)
553 return this->GetUserCounter();
557 if (!this->HasUser(user))
559 src->WriteServ("441 %s %s %s :They are not on that channel",src->nick, user->nick, this->name);
560 return this->GetUserCounter();
562 if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
564 src->WriteServ("482 %s %s :Only a u-line may kick a u-line from a channel.",src->nick, this->name);
565 return this->GetUserCounter();
569 if (!ServerInstance->ULine(src->server))
572 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,this,reason));
574 return this->GetUserCounter();
576 /* Set to -1 by OnUserPreKick if explicit allow was set */
577 if (MOD_RESULT != -1)
579 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,this,AC_KICK));
580 if ((MOD_RESULT == ACR_DENY) && (!ServerInstance->ULine(src->server)))
581 return this->GetUserCounter();
583 if ((MOD_RESULT == ACR_DEFAULT) || (!ServerInstance->ULine(src->server)))
585 int them = this->GetStatus(src);
586 int us = this->GetStatus(user);
587 if ((them < STATUS_HOP) || (them < us))
589 if (them == STATUS_HOP)
591 src->WriteServ("482 %s %s :You must be a channel operator",src->nick, this->name);
595 src->WriteServ("482 %s %s :You must be at least a half-operator",src->nick, this->name);
597 return this->GetUserCounter();
603 FOREACH_MOD(I_OnUserKick,OnUserKick(src,user,this,reason));
605 for (UserChanList::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
607 /* zap it from the channel list of the user */
608 if ((*i)->channel == this)
610 this->WriteChannel(src, "KICK %s %s :%s", this->name, user->nick, reason);
612 (*i)->channel = NULL;
617 if (!this->DelUser(user))
618 /* if there are no users left on the channel */
620 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
622 /* kill the record */
623 if (iter != ServerInstance->chanlist.end())
625 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
626 ServerInstance->chanlist.erase(iter);
631 return this->GetUserCounter();
634 void chanrec::WriteChannel(userrec* user, char* text, ...)
636 char textbuffer[MAXBUF];
642 va_start(argsPtr, text);
643 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
646 this->WriteChannel(user, std::string(textbuffer));
649 void chanrec::WriteChannel(userrec* user, const std::string &text)
651 CUList *ulist = this->GetUsers();
656 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
658 if (IS_LOCAL(i->second))
659 user->WriteTo(i->second,text);
663 void chanrec::WriteChannelWithServ(const char* ServName, const char* text, ...)
665 char textbuffer[MAXBUF];
671 va_start(argsPtr, text);
672 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
675 this->WriteChannelWithServ(ServName, std::string(textbuffer));
678 void chanrec::WriteChannelWithServ(const char* ServName, const std::string &text)
680 CUList *ulist = this->GetUsers();
682 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
684 if (IS_LOCAL(i->second))
685 i->second->WriteServ(text);
689 /* write formatted text from a source user to all users on a channel except
690 * for the sender (for privmsg etc) */
691 void chanrec::WriteAllExceptSender(userrec* user, char status, char* text, ...)
693 char textbuffer[MAXBUF];
699 va_start(argsPtr, text);
700 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
703 this->WriteAllExceptSender(user, status, std::string(textbuffer));
706 void chanrec::WriteAllExceptSender(userrec* user, char status, const std::string& text)
716 ulist = this->GetOppedUsers();
719 ulist = this->GetHalfoppedUsers();
722 ulist = this->GetVoicedUsers();
725 ulist = this->GetUsers();
729 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
731 if ((IS_LOCAL(i->second)) && (user != i->second))
732 i->second->WriteFrom(user,text);
737 * return a count of the users on a specific channel accounting for
738 * invisible users who won't increase the count. e.g. for /LIST
740 int chanrec::CountInvisible()
743 CUList *ulist= this->GetUsers();
744 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
746 if (!(i->second->modes[UM_INVISIBLE]))
753 char* chanrec::ChanModes(bool showkey)
755 static char scratch[MAXBUF];
756 static char sparam[MAXBUF];
757 char* offset = scratch;
758 std::string extparam = "";
763 /* This was still iterating up to 190, chanrec::custom_modes is only 64 elements -- Om */
764 for(int n = 0; n < 64; n++)
773 extparam = (showkey ? this->key : "<key>");
776 extparam = ConvToStr(this->limit);
784 /* We know these have no parameters */
787 extparam = this->GetModeParameter(n + 65);
792 charlcat(sparam,' ',MAXBUF);
793 strlcat(sparam,extparam.c_str(),MAXBUF);
798 /* Null terminate scratch */
800 strlcat(scratch,sparam,MAXBUF);
804 /* compile a userlist of a channel into a string, each nick seperated by
805 * spaces and op, voice etc status shown as @ and +, and send it to 'user'
807 void chanrec::UserList(userrec *user)
812 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
815 char* ptr = list + dlen;
817 CUList *ulist= this->GetUsers();
819 /* Improvement by Brain - this doesnt change in value, so why was it inside
822 bool has_user = this->HasUser(user);
824 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
826 if ((!has_user) && (i->second->modes[UM_INVISIBLE]))
829 * user is +i, and source not on the channel, does not show
835 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", this->GetStatusChar(i->second), i->second->nick);
842 if (curlen > (480-NICKMAX))
844 /* list overflowed into multiple numerics */
845 user->WriteServ(list);
847 /* reset our lengths */
848 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
856 /* if whats left in the list isnt empty, send it */
859 user->WriteServ(list);
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::GetStatusChar(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++)
907 int chanrec::GetStatusFlags(userrec *user)
909 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
911 if ((*i)->channel == this)
913 return (*i)->uc_modes;
921 int chanrec::GetStatus(userrec *user)
923 if (ServerInstance->ULine(user->server))
926 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
928 if ((*i)->channel == this)
930 if (((*i)->uc_modes & UCMODE_OP) > 0)
934 if (((*i)->uc_modes & UCMODE_HOP) > 0)
938 if (((*i)->uc_modes & UCMODE_VOICE) > 0)
942 return STATUS_NORMAL;
945 return STATUS_NORMAL;
948 void chanrec::SetPrefix(userrec* user, char prefix, unsigned int prefix_value, bool adding)
950 prefixlist::iterator n = prefixes.find(user);
951 prefixtype pfx = std::make_pair(prefix,prefix_value);
954 if (n != prefixes.end())
956 if (std::find(n->second.begin(), n->second.end(), pfx) == n->second.end())
958 n->second.push_back(pfx);
965 prefixes.insert(std::make_pair<userrec*,pfxcontainer>(user, one));
970 if (n != prefixes.end())
972 pfxcontainer::iterator x = std::find(n->second.begin(), n->second.end(), pfx);
973 if (x != n->second.end())