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"
38 #include "inspstring.h"
39 #include "helperfuncs.h"
43 extern std::vector<Module*> modules;
44 extern std::vector<ircd_module*> factory;
47 chanrec::chanrec(InspIRCd* Instance) : ServerInstance(Instance)
49 *name = *topic = *setby = *key = 0;
50 created = topicset = limit = 0;
51 internal_userlist.clear();
55 void chanrec::SetMode(char mode,bool mode_on)
57 modes[mode-65] = mode_on;
59 this->SetModeParam(mode,"",false);
63 void chanrec::SetModeParam(char mode,const char* parameter,bool mode_on)
65 log(DEBUG,"SetModeParam called");
67 CustomModeList::iterator n = custom_mode_params.find(mode);
71 if (n == custom_mode_params.end())
73 custom_mode_params[mode] = strdup(parameter);
74 log(DEBUG,"Custom mode parameter %c %s added",mode,parameter);
78 log(DEBUG, "Tried to set custom mode parameter for %c '%s' when it was already '%s'", mode, parameter, n->second);
83 if (n != custom_mode_params.end())
86 custom_mode_params.erase(n);
91 bool chanrec::IsModeSet(char mode)
93 return modes[mode-65];
96 std::string chanrec::GetModeParameter(char mode)
102 else if (mode == 'l')
104 return ConvToStr(this->limit);
108 CustomModeList::iterator n = custom_mode_params.find(mode);
109 if (n != custom_mode_params.end())
117 long chanrec::GetUserCounter()
119 return (this->internal_userlist.size());
122 void chanrec::AddUser(userrec* user)
124 internal_userlist[user] = user;
127 unsigned long chanrec::DelUser(userrec* user)
129 CUListIter a = internal_userlist.find(user);
131 if (a != internal_userlist.end())
133 internal_userlist.erase(a);
134 /* And tidy any others... */
136 DelHalfoppedUser(user);
140 return internal_userlist.size();
143 bool chanrec::HasUser(userrec* user)
145 return (internal_userlist.find(user) != internal_userlist.end());
148 void chanrec::AddOppedUser(userrec* user)
150 internal_op_userlist[user] = user;
153 void chanrec::DelOppedUser(userrec* user)
155 CUListIter a = internal_op_userlist.find(user);
156 if (a != internal_op_userlist.end())
158 internal_op_userlist.erase(a);
163 void chanrec::AddHalfoppedUser(userrec* user)
165 internal_halfop_userlist[user] = user;
168 void chanrec::DelHalfoppedUser(userrec* user)
170 CUListIter a = internal_halfop_userlist.find(user);
172 if (a != internal_halfop_userlist.end())
174 internal_halfop_userlist.erase(a);
178 void chanrec::AddVoicedUser(userrec* user)
180 internal_voice_userlist[user] = user;
183 void chanrec::DelVoicedUser(userrec* user)
185 CUListIter a = internal_voice_userlist.find(user);
187 if (a != internal_voice_userlist.end())
189 internal_voice_userlist.erase(a);
193 CUList* chanrec::GetUsers()
195 return &internal_userlist;
198 CUList* chanrec::GetOppedUsers()
200 return &internal_op_userlist;
203 CUList* chanrec::GetHalfoppedUsers()
205 return &internal_halfop_userlist;
208 CUList* chanrec::GetVoicedUsers()
210 return &internal_voice_userlist;
214 * add a channel to a user, creating the record for it if needed and linking
215 * it to the user record
217 chanrec* chanrec::JoinUser(InspIRCd* Instance, userrec *user, const char* cn, bool override, const char* key)
225 strlcpy(cname,cn,CHANMAX);
227 chanrec* Ptr = Instance->FindChan(cname);
234 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,NULL,cname));
239 /* create a new one */
240 Ptr = new chanrec(Instance);
241 Instance->chanlist[cname] = Ptr;
243 strlcpy(Ptr->name, cname,CHANMAX);
244 Ptr->modes[CM_TOPICLOCK] = Ptr->modes[CM_NOEXTERNAL] = 1;
247 strlcpy(Ptr->setby, user->nick,NICKMAX-1);
249 log(DEBUG,"chanrec::JoinUser(): created: %s",cname);
251 * set created to 2 to indicate user
252 * is the first in the channel
253 * and should be given ops
259 /* Already on the channel */
260 if (Ptr->HasUser(user))
264 * remote users are allowed us to bypass channel modes
265 * and bans (used by servers)
267 if (IS_LOCAL(user)) /* was a check on fd > -1 */
270 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname));
275 else if (MOD_RESULT == 0)
280 FOREACH_RESULT_I(Instance,I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
285 log(DEBUG,"chanrec::JoinUser(): no key given in JOIN");
286 user->WriteServ("475 %s %s :Cannot join channel (Requires key)",user->nick, Ptr->name);
291 if (strcmp(key,Ptr->key))
293 log(DEBUG,"chanrec::JoinUser(): bad key given in JOIN");
294 user->WriteServ("475 %s %s :Cannot join channel (Incorrect key)",user->nick, Ptr->name);
300 if (Ptr->modes[CM_INVITEONLY])
303 irc::string xname(Ptr->name);
304 FOREACH_RESULT_I(Instance,I_OnCheckInvite,OnCheckInvite(user, Ptr));
307 if (user->IsInvited(xname))
309 /* user was invited to channel */
310 /* there may be an optional channel NOTICE here */
314 user->WriteServ("473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
318 user->RemoveInvite(xname);
323 FOREACH_RESULT_I(Instance,I_OnCheckLimit,OnCheckLimit(user, Ptr));
326 if (Ptr->GetUserCounter() >= Ptr->limit)
328 user->WriteServ("471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
333 if (Ptr->bans.size())
336 FOREACH_RESULT_I(Instance,I_OnCheckBan,OnCheckBan(user, Ptr));
338 sprintf(mask,"%s!%s@%s",user->nick, user->ident, user->GetIPString());
341 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
343 /* This allows CIDR ban matching
345 * Full masked host Full unmasked host IP with/without CIDR
347 if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match(mask, i->data, true)))
349 user->WriteServ("474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
359 log(DEBUG,"chanrec::JoinUser(): Overridden checks");
364 for (UserChanList::const_iterator index = user->chans.begin(); index != user->chans.end(); index++)
366 if ((*index)->channel == NULL)
368 return chanrec::ForceChan(Instance, Ptr, *index, user, created);
373 * XXX: If the user is an oper here, we can just extend their user->chans vector by one
374 * and put the channel in here. Same for remote users which are not bound by
375 * the channel limits. Otherwise, nope, youre boned.
377 if (!IS_LOCAL(user)) /* was a check on fd < 0 */
379 ucrec* a = new ucrec();
380 chanrec* c = chanrec::ForceChan(Instance, Ptr,a,user,created);
381 user->chans.push_back(a);
384 else if (*user->oper)
386 /* Oper allows extension up to the OPERMAXCHANS value */
387 if (user->chans.size() < OPERMAXCHANS)
389 ucrec* a = new ucrec();
390 chanrec* c = chanrec::ForceChan(Instance, Ptr,a,user,created);
391 user->chans.push_back(a);
396 user->WriteServ("405 %s %s :You are on too many channels",user->nick, cname);
400 log(DEBUG,"BLAMMO, Whacking channel.");
401 /* Things went seriously pear shaped, so take this away. bwahaha. */
402 chan_hash::iterator n = Instance->chanlist.find(cname);
403 if (n != Instance->chanlist.end())
407 Instance->chanlist.erase(n);
408 for (unsigned int index =0; index < user->chans.size(); index++)
410 if (user->chans[index]->channel == Ptr)
412 user->chans[index]->channel = NULL;
413 user->chans[index]->uc_modes = 0;
420 for (unsigned int index =0; index < user->chans.size(); index++)
422 if (user->chans[index]->channel == Ptr)
424 user->chans[index]->channel = NULL;
425 user->chans[index]->uc_modes = 0;
432 chanrec* chanrec::ForceChan(InspIRCd* Instance, chanrec* Ptr,ucrec *a,userrec* user, int created)
436 /* first user in is given ops */
437 a->uc_modes = UCMODE_OP;
438 Ptr->AddOppedUser(user);
447 Ptr->WriteChannel(user,"JOIN :%s",Ptr->name);
449 /* Major improvement by Brain - we dont need to be calculating all this pointlessly for remote users */
452 log(DEBUG,"Sent JOIN to client");
455 user->WriteServ("332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
456 user->WriteServ("333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
459 user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
461 FOREACH_MOD_I(Instance,I_OnUserJoin,OnUserJoin(user,Ptr));
466 * remove a channel from a users record, and remove the record from the hash
467 * if the channel has become empty
469 long chanrec::PartUser(userrec *user, const char* reason)
472 return this->GetUserCounter();
474 for (unsigned int i =0; i < user->chans.size(); i++)
476 /* zap it from the channel list of the user */
477 if (user->chans[i]->channel == this)
481 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, reason));
482 this->WriteChannel(user, "PART %s :%s", this->name, reason);
486 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, ""));
487 this->WriteChannel(user, "PART :%s", this->name);
489 user->chans[i]->uc_modes = 0;
490 user->chans[i]->channel = NULL;
495 if (!this->DelUser(user)) /* if there are no users left on the channel... */
497 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
498 /* kill the record */
499 if (iter != ServerInstance->chanlist.end())
501 log(DEBUG,"del_channel: destroyed: %s", this->name);
502 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
503 ServerInstance->chanlist.erase(iter);
508 return this->GetUserCounter();
511 long chanrec::ServerKickUser(userrec* user, const char* reason, bool triggerevents)
513 if (!user || !reason)
514 return this->GetUserCounter();
518 if (!this->HasUser(user))
521 return this->GetUserCounter();
527 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL,user,this,reason));
530 for (unsigned int i =0; i < user->chans.size(); i++)
532 if (user->chans[i]->channel == this)
534 this->WriteChannelWithServ(ServerInstance->Config->ServerName, "KICK %s %s :%s", this->name, user->nick, reason);
535 user->chans[i]->uc_modes = 0;
536 user->chans[i]->channel = NULL;
541 if (!this->DelUser(user))
543 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
544 /* kill the record */
545 if (iter != ServerInstance->chanlist.end())
547 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
548 ServerInstance->chanlist.erase(iter);
553 return this->GetUserCounter();
556 long chanrec::KickUser(userrec *src, userrec *user, const char* reason)
558 if (!src || !user || !reason)
559 return this->GetUserCounter();
563 if (!this->HasUser(user))
565 src->WriteServ("441 %s %s %s :They are not on that channel",src->nick, user->nick, this->name);
566 return this->GetUserCounter();
568 if ((is_uline(user->server)) && (!is_uline(src->server)))
570 src->WriteServ("482 %s %s :Only a u-line may kick a u-line from a channel.",src->nick, this->name);
571 return this->GetUserCounter();
575 if (!is_uline(src->server))
578 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,this,reason));
580 return this->GetUserCounter();
582 /* Set to -1 by OnUserPreKick if explicit allow was set */
583 if (MOD_RESULT != -1)
585 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,this,AC_KICK));
586 if ((MOD_RESULT == ACR_DENY) && (!is_uline(src->server)))
587 return this->GetUserCounter();
589 if ((MOD_RESULT == ACR_DEFAULT) || (!is_uline(src->server)))
591 int them = this->GetStatus(src);
592 int us = this->GetStatus(user);
593 if ((them < STATUS_HOP) || (them < us))
595 if (them == STATUS_HOP)
597 src->WriteServ("482 %s %s :You must be a channel operator",src->nick, this->name);
601 src->WriteServ("482 %s %s :You must be at least a half-operator",src->nick, this->name);
603 return this->GetUserCounter();
609 FOREACH_MOD(I_OnUserKick,OnUserKick(src,user,this,reason));
611 for (UserChanList::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
613 /* zap it from the channel list of the user */
614 if ((*i)->channel == this)
616 this->WriteChannel(src, "KICK %s %s :%s", this->name, user->nick, reason);
618 (*i)->channel = NULL;
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 (i->second->fd != FD_MAGIC_NUMBER)
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, char status, char* text, ...)
699 char textbuffer[MAXBUF];
705 va_start(argsPtr, text);
706 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
709 this->WriteAllExceptSender(user, status, std::string(textbuffer));
712 void chanrec::WriteAllExceptSender(userrec* user, char status, const std::string& text)
722 ulist = this->GetOppedUsers();
725 ulist = this->GetHalfoppedUsers();
728 ulist = this->GetVoicedUsers();
731 ulist = this->GetUsers();
735 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
737 if ((IS_LOCAL(i->second)) && (user != i->second))
738 i->second->WriteFrom(user,text);
743 * return a count of the users on a specific channel accounting for
744 * invisible users who won't increase the count. e.g. for /LIST
746 int chanrec::CountInvisible()
749 CUList *ulist= this->GetUsers();
750 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
752 if (!(i->second->modes[UM_INVISIBLE]))
759 char* chanrec::ChanModes(bool showkey)
761 static char scratch[MAXBUF];
762 static char sparam[MAXBUF];
763 char* offset = scratch;
764 std::string extparam = "";
769 /* This was still iterating up to 190, chanrec::custom_modes is only 64 elements -- Om */
770 for(int n = 0; n < 64; n++)
779 extparam = (showkey ? this->key : "<key>");
782 extparam = ConvToStr(this->limit);
790 /* We know these have no parameters */
793 extparam = this->GetModeParameter(n + 65);
798 charlcat(sparam,' ',MAXBUF);
799 strlcat(sparam,extparam.c_str(),MAXBUF);
804 /* Null terminate scratch */
806 strlcat(scratch,sparam,MAXBUF);
810 /* compile a userlist of a channel into a string, each nick seperated by
811 * spaces and op, voice etc status shown as @ and +, and send it to 'user'
813 void chanrec::UserList(userrec *user)
818 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
821 char* ptr = list + dlen;
823 CUList *ulist= this->GetUsers();
825 /* Improvement by Brain - this doesnt change in value, so why was it inside
828 bool has_user = this->HasUser(user);
830 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
832 if ((!has_user) && (i->second->modes[UM_INVISIBLE]))
835 * user is +i, and source not on the channel, does not show
841 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", this->GetStatusChar(i->second), i->second->nick);
848 if (curlen > (480-NICKMAX))
850 /* list overflowed into multiple numerics */
851 user->WriteServ(list);
853 /* reset our lengths */
854 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
862 /* if whats left in the list isnt empty, send it */
865 user->WriteServ(list);
869 long chanrec::GetMaxBans()
872 for (std::map<std::string,int>::iterator n = ServerInstance->Config->maxbans.begin(); n != ServerInstance->Config->maxbans.end(); n++)
875 if (match(this->name,x.c_str()))
884 /* returns the status character for a given user on a channel, e.g. @ for op,
885 * % for halfop etc. If the user has several modes set, the highest mode
886 * the user has must be returned. */
888 const char* chanrec::GetStatusChar(userrec *user)
890 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
892 if ((*i)->channel == this)
894 if (((*i)->uc_modes & UCMODE_OP) > 0)
898 if (((*i)->uc_modes & UCMODE_HOP) > 0)
902 if (((*i)->uc_modes & UCMODE_VOICE) > 0)
913 int chanrec::GetStatusFlags(userrec *user)
915 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
917 if ((*i)->channel == this)
919 return (*i)->uc_modes;
927 int chanrec::GetStatus(userrec *user)
929 if (is_uline(user->server))
932 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
934 if ((*i)->channel == this)
936 if (((*i)->uc_modes & UCMODE_OP) > 0)
940 if (((*i)->uc_modes & UCMODE_HOP) > 0)
944 if (((*i)->uc_modes & UCMODE_VOICE) > 0)
948 return STATUS_NORMAL;
951 return STATUS_NORMAL;