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);
454 FOREACH_MOD_I(Instance,I_OnUserJoin,OnUserJoin(user,Ptr));
459 * remove a channel from a users record, and remove the record from the hash
460 * if the channel has become empty
462 long chanrec::PartUser(userrec *user, const char* reason)
465 return this->GetUserCounter();
467 for (unsigned int i =0; i < user->chans.size(); i++)
469 /* zap it from the channel list of the user */
470 if (user->chans[i]->channel == this)
474 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, reason));
475 this->WriteChannel(user, "PART %s :%s", this->name, reason);
479 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, ""));
480 this->WriteChannel(user, "PART :%s", this->name);
482 user->chans[i]->uc_modes = 0;
483 user->chans[i]->channel = NULL;
484 this->RemoveAllPrefixes(user);
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;
531 this->RemoveAllPrefixes(user);
536 if (!this->DelUser(user))
538 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
539 /* kill the record */
540 if (iter != ServerInstance->chanlist.end())
542 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
543 ServerInstance->chanlist.erase(iter);
548 return this->GetUserCounter();
551 long chanrec::KickUser(userrec *src, userrec *user, const char* reason)
553 if (!src || !user || !reason)
554 return this->GetUserCounter();
558 if (!this->HasUser(user))
560 src->WriteServ("441 %s %s %s :They are not on that channel",src->nick, user->nick, this->name);
561 return this->GetUserCounter();
563 if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
565 src->WriteServ("482 %s %s :Only a u-line may kick a u-line from a channel.",src->nick, this->name);
566 return this->GetUserCounter();
570 if (!ServerInstance->ULine(src->server))
573 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,this,reason));
575 return this->GetUserCounter();
577 /* Set to -1 by OnUserPreKick if explicit allow was set */
578 if (MOD_RESULT != -1)
580 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,this,AC_KICK));
581 if ((MOD_RESULT == ACR_DENY) && (!ServerInstance->ULine(src->server)))
582 return this->GetUserCounter();
584 if ((MOD_RESULT == ACR_DEFAULT) || (!ServerInstance->ULine(src->server)))
586 int them = this->GetStatus(src);
587 int us = this->GetStatus(user);
588 if ((them < STATUS_HOP) || (them < us))
590 if (them == STATUS_HOP)
592 src->WriteServ("482 %s %s :You must be a channel operator",src->nick, this->name);
596 src->WriteServ("482 %s %s :You must be at least a half-operator",src->nick, this->name);
598 return this->GetUserCounter();
604 FOREACH_MOD(I_OnUserKick,OnUserKick(src,user,this,reason));
606 for (UserChanList::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
608 /* zap it from the channel list of the user */
609 if ((*i)->channel == this)
611 this->WriteChannel(src, "KICK %s %s :%s", this->name, user->nick, reason);
613 (*i)->channel = NULL;
614 this->RemoveAllPrefixes(user);
619 if (!this->DelUser(user))
620 /* if there are no users left on the channel */
622 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
624 /* kill the record */
625 if (iter != ServerInstance->chanlist.end())
627 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
628 ServerInstance->chanlist.erase(iter);
633 return this->GetUserCounter();
636 void chanrec::WriteChannel(userrec* user, char* text, ...)
638 char textbuffer[MAXBUF];
644 va_start(argsPtr, text);
645 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
648 this->WriteChannel(user, std::string(textbuffer));
651 void chanrec::WriteChannel(userrec* user, const std::string &text)
653 CUList *ulist = this->GetUsers();
658 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
660 if (IS_LOCAL(i->second))
661 user->WriteTo(i->second,text);
665 void chanrec::WriteChannelWithServ(const char* ServName, const char* text, ...)
667 char textbuffer[MAXBUF];
673 va_start(argsPtr, text);
674 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
677 this->WriteChannelWithServ(ServName, std::string(textbuffer));
680 void chanrec::WriteChannelWithServ(const char* ServName, const std::string &text)
682 CUList *ulist = this->GetUsers();
684 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
686 if (IS_LOCAL(i->second))
687 i->second->WriteServ(text);
691 /* write formatted text from a source user to all users on a channel except
692 * for the sender (for privmsg etc) */
693 void chanrec::WriteAllExceptSender(userrec* user, char status, char* text, ...)
695 char textbuffer[MAXBUF];
701 va_start(argsPtr, text);
702 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
705 this->WriteAllExceptSender(user, status, std::string(textbuffer));
708 void chanrec::WriteAllExceptSender(userrec* user, char status, const std::string& text)
718 ulist = this->GetOppedUsers();
721 ulist = this->GetHalfoppedUsers();
724 ulist = this->GetVoicedUsers();
727 ulist = this->GetUsers();
731 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
733 if ((IS_LOCAL(i->second)) && (user != i->second))
734 i->second->WriteFrom(user,text);
739 * return a count of the users on a specific channel accounting for
740 * invisible users who won't increase the count. e.g. for /LIST
742 int chanrec::CountInvisible()
745 CUList *ulist= this->GetUsers();
746 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
748 if (!(i->second->modes[UM_INVISIBLE]))
755 char* chanrec::ChanModes(bool showkey)
757 static char scratch[MAXBUF];
758 static char sparam[MAXBUF];
759 char* offset = scratch;
760 std::string extparam = "";
765 /* This was still iterating up to 190, chanrec::custom_modes is only 64 elements -- Om */
766 for(int n = 0; n < 64; n++)
775 extparam = (showkey ? this->key : "<key>");
778 extparam = ConvToStr(this->limit);
786 /* We know these have no parameters */
789 extparam = this->GetModeParameter(n + 65);
794 charlcat(sparam,' ',MAXBUF);
795 strlcat(sparam,extparam.c_str(),MAXBUF);
800 /* Null terminate scratch */
802 strlcat(scratch,sparam,MAXBUF);
806 /* compile a userlist of a channel into a string, each nick seperated by
807 * spaces and op, voice etc status shown as @ and +, and send it to 'user'
809 void chanrec::UserList(userrec *user)
815 FOREACH_RESULT(I_OnUserList,OnUserList(user, this));
816 ServerInstance->Log(DEBUG,"MOD_RESULT for UserList = %d",MOD_RESULT);
820 ServerInstance->Log(DEBUG,"Using builtin NAMES list generation");
822 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
825 char* ptr = list + dlen;
827 CUList *ulist= this->GetUsers();
829 /* Improvement by Brain - this doesnt change in value, so why was it inside
832 bool has_user = this->HasUser(user);
834 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
836 if ((!has_user) && (i->second->modes[UM_INVISIBLE]))
839 * user is +i, and source not on the channel, does not show
845 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", this->GetPrefixChar(i->second), i->second->nick);
852 if (curlen > (480-NICKMAX))
854 /* list overflowed into multiple numerics */
855 user->WriteServ(list);
857 /* reset our lengths */
858 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
866 /* if whats left in the list isnt empty, send it */
869 user->WriteServ(list);
872 user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, this->name);
875 long chanrec::GetMaxBans()
878 for (std::map<std::string,int>::iterator n = ServerInstance->Config->maxbans.begin(); n != ServerInstance->Config->maxbans.end(); n++)
881 if (match(this->name,x.c_str()))
890 /* returns the status character for a given user on a channel, e.g. @ for op,
891 * % for halfop etc. If the user has several modes set, the highest mode
892 * the user has must be returned.
894 const char* chanrec::GetPrefixChar(userrec *user)
902 prefixlist::iterator n = prefixes.find(user);
903 if (n != prefixes.end())
905 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
918 const char* chanrec::GetAllPrefixChars(userrec* user)
920 static char prefix[MAXBUF];
924 prefixlist::iterator n = prefixes.find(user);
925 if (n != prefixes.end())
927 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
929 prefix[ctr++] = x->first;
938 unsigned int chanrec::GetPrefixValue(userrec* user)
942 prefixlist::iterator n = prefixes.find(user);
943 if (n != prefixes.end())
945 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
956 int chanrec::GetStatusFlags(userrec *user)
958 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
960 if ((*i)->channel == this)
962 return (*i)->uc_modes;
969 int chanrec::GetStatus(userrec *user)
971 if (ServerInstance->ULine(user->server))
974 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
976 if ((*i)->channel == this)
978 if (((*i)->uc_modes & UCMODE_OP) > 0)
982 if (((*i)->uc_modes & UCMODE_HOP) > 0)
986 if (((*i)->uc_modes & UCMODE_VOICE) > 0)
990 return STATUS_NORMAL;
993 return STATUS_NORMAL;
996 void chanrec::SetPrefix(userrec* user, char prefix, unsigned int prefix_value, bool adding)
998 prefixlist::iterator n = prefixes.find(user);
999 prefixtype pfx = std::make_pair(prefix,prefix_value);
1002 if (n != prefixes.end())
1004 if (std::find(n->second.begin(), n->second.end(), pfx) == n->second.end())
1006 n->second.push_back(pfx);
1013 prefixes.insert(std::make_pair<userrec*,pfxcontainer>(user, one));
1018 if (n != prefixes.end())
1020 pfxcontainer::iterator x = std::find(n->second.begin(), n->second.end(), pfx);
1021 if (x != n->second.end())
1027 void chanrec::RemoveAllPrefixes(userrec* user)
1029 prefixlist::iterator n = prefixes.find(user);
1030 if (n != prefixes.end())