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;
485 this->RemoveAllPrefixes(user);
490 if (!this->DelUser(user)) /* if there are no users left on the channel... */
492 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
493 /* kill the record */
494 if (iter != ServerInstance->chanlist.end())
496 ServerInstance->Log(DEBUG,"del_channel: destroyed: %s", this->name);
497 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
498 ServerInstance->chanlist.erase(iter);
503 return this->GetUserCounter();
506 long chanrec::ServerKickUser(userrec* user, const char* reason, bool triggerevents)
508 if (!user || !reason)
509 return this->GetUserCounter();
513 if (!this->HasUser(user))
516 return this->GetUserCounter();
522 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL,user,this,reason));
525 for (unsigned int i =0; i < user->chans.size(); i++)
527 if (user->chans[i]->channel == this)
529 this->WriteChannelWithServ(ServerInstance->Config->ServerName, "KICK %s %s :%s", this->name, user->nick, reason);
530 user->chans[i]->uc_modes = 0;
531 user->chans[i]->channel = NULL;
532 this->RemoveAllPrefixes(user);
537 if (!this->DelUser(user))
539 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
540 /* kill the record */
541 if (iter != ServerInstance->chanlist.end())
543 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
544 ServerInstance->chanlist.erase(iter);
549 return this->GetUserCounter();
552 long chanrec::KickUser(userrec *src, userrec *user, const char* reason)
554 if (!src || !user || !reason)
555 return this->GetUserCounter();
559 if (!this->HasUser(user))
561 src->WriteServ("441 %s %s %s :They are not on that channel",src->nick, user->nick, this->name);
562 return this->GetUserCounter();
564 if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
566 src->WriteServ("482 %s %s :Only a u-line may kick a u-line from a channel.",src->nick, this->name);
567 return this->GetUserCounter();
571 if (!ServerInstance->ULine(src->server))
574 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,this,reason));
576 return this->GetUserCounter();
578 /* Set to -1 by OnUserPreKick if explicit allow was set */
579 if (MOD_RESULT != -1)
581 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,this,AC_KICK));
582 if ((MOD_RESULT == ACR_DENY) && (!ServerInstance->ULine(src->server)))
583 return this->GetUserCounter();
585 if ((MOD_RESULT == ACR_DEFAULT) || (!ServerInstance->ULine(src->server)))
587 int them = this->GetStatus(src);
588 int us = this->GetStatus(user);
589 if ((them < STATUS_HOP) || (them < us))
591 if (them == STATUS_HOP)
593 src->WriteServ("482 %s %s :You must be a channel operator",src->nick, this->name);
597 src->WriteServ("482 %s %s :You must be at least a half-operator",src->nick, this->name);
599 return this->GetUserCounter();
605 FOREACH_MOD(I_OnUserKick,OnUserKick(src,user,this,reason));
607 for (UserChanList::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
609 /* zap it from the channel list of the user */
610 if ((*i)->channel == this)
612 this->WriteChannel(src, "KICK %s %s :%s", this->name, user->nick, reason);
614 (*i)->channel = NULL;
615 this->RemoveAllPrefixes(user);
620 if (!this->DelUser(user))
621 /* if there are no users left on the channel */
623 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
625 /* kill the record */
626 if (iter != ServerInstance->chanlist.end())
628 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
629 ServerInstance->chanlist.erase(iter);
634 return this->GetUserCounter();
637 void chanrec::WriteChannel(userrec* user, char* text, ...)
639 char textbuffer[MAXBUF];
645 va_start(argsPtr, text);
646 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
649 this->WriteChannel(user, std::string(textbuffer));
652 void chanrec::WriteChannel(userrec* user, const std::string &text)
654 CUList *ulist = this->GetUsers();
659 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
661 if (IS_LOCAL(i->second))
662 user->WriteTo(i->second,text);
666 void chanrec::WriteChannelWithServ(const char* ServName, const char* text, ...)
668 char textbuffer[MAXBUF];
674 va_start(argsPtr, text);
675 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
678 this->WriteChannelWithServ(ServName, std::string(textbuffer));
681 void chanrec::WriteChannelWithServ(const char* ServName, const std::string &text)
683 CUList *ulist = this->GetUsers();
685 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
687 if (IS_LOCAL(i->second))
688 i->second->WriteServ(text);
692 /* write formatted text from a source user to all users on a channel except
693 * for the sender (for privmsg etc) */
694 void chanrec::WriteAllExceptSender(userrec* user, char status, char* text, ...)
696 char textbuffer[MAXBUF];
702 va_start(argsPtr, text);
703 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
706 this->WriteAllExceptSender(user, status, std::string(textbuffer));
709 void chanrec::WriteAllExceptSender(userrec* user, 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))
735 i->second->WriteFrom(user,text);
740 * return a count of the users on a specific channel accounting for
741 * invisible users who won't increase the count. e.g. for /LIST
743 int chanrec::CountInvisible()
746 CUList *ulist= this->GetUsers();
747 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
749 if (!(i->second->modes[UM_INVISIBLE]))
756 char* chanrec::ChanModes(bool showkey)
758 static char scratch[MAXBUF];
759 static char sparam[MAXBUF];
760 char* offset = scratch;
761 std::string extparam = "";
766 /* This was still iterating up to 190, chanrec::custom_modes is only 64 elements -- Om */
767 for(int n = 0; n < 64; n++)
776 extparam = (showkey ? this->key : "<key>");
779 extparam = ConvToStr(this->limit);
787 /* We know these have no parameters */
790 extparam = this->GetModeParameter(n + 65);
795 charlcat(sparam,' ',MAXBUF);
796 strlcat(sparam,extparam.c_str(),MAXBUF);
801 /* Null terminate scratch */
803 strlcat(scratch,sparam,MAXBUF);
807 /* compile a userlist of a channel into a string, each nick seperated by
808 * spaces and op, voice etc status shown as @ and +, and send it to 'user'
810 void chanrec::UserList(userrec *user)
815 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
818 char* ptr = list + dlen;
820 CUList *ulist= this->GetUsers();
822 /* Improvement by Brain - this doesnt change in value, so why was it inside
825 bool has_user = this->HasUser(user);
827 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
829 if ((!has_user) && (i->second->modes[UM_INVISIBLE]))
832 * user is +i, and source not on the channel, does not show
838 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", this->GetPrefixChar(i->second), i->second->nick);
845 if (curlen > (480-NICKMAX))
847 /* list overflowed into multiple numerics */
848 user->WriteServ(list);
850 /* reset our lengths */
851 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
859 /* if whats left in the list isnt empty, send it */
862 user->WriteServ(list);
866 long chanrec::GetMaxBans()
869 for (std::map<std::string,int>::iterator n = ServerInstance->Config->maxbans.begin(); n != ServerInstance->Config->maxbans.end(); n++)
872 if (match(this->name,x.c_str()))
881 /* returns the status character for a given user on a channel, e.g. @ for op,
882 * % for halfop etc. If the user has several modes set, the highest mode
883 * the user has must be returned.
885 const char* chanrec::GetPrefixChar(userrec *user)
893 prefixlist::iterator n = prefixes.find(user);
894 if (n != prefixes.end())
896 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
909 const char* chanrec::GetAllPrefixChars(userrec* user)
911 static char prefix[MAXBUF];
915 prefixlist::iterator n = prefixes.find(user);
916 if (n != prefixes.end())
918 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
920 prefix[ctr++] = x->first;
929 unsigned int chanrec::GetPrefixValue(userrec* user)
933 prefixlist::iterator n = prefixes.find(user);
934 if (n != prefixes.end())
936 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
947 int chanrec::GetStatusFlags(userrec *user)
949 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
951 if ((*i)->channel == this)
953 return (*i)->uc_modes;
960 int chanrec::GetStatus(userrec *user)
962 if (ServerInstance->ULine(user->server))
965 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
967 if ((*i)->channel == this)
969 if (((*i)->uc_modes & UCMODE_OP) > 0)
973 if (((*i)->uc_modes & UCMODE_HOP) > 0)
977 if (((*i)->uc_modes & UCMODE_VOICE) > 0)
981 return STATUS_NORMAL;
984 return STATUS_NORMAL;
987 void chanrec::SetPrefix(userrec* user, char prefix, unsigned int prefix_value, bool adding)
989 prefixlist::iterator n = prefixes.find(user);
990 prefixtype pfx = std::make_pair(prefix,prefix_value);
993 if (n != prefixes.end())
995 if (std::find(n->second.begin(), n->second.end(), pfx) == n->second.end())
997 n->second.push_back(pfx);
1004 prefixes.insert(std::make_pair<userrec*,pfxcontainer>(user, one));
1009 if (n != prefixes.end())
1011 pfxcontainer::iterator x = std::find(n->second.begin(), n->second.end(), pfx);
1012 if (x != n->second.end())
1018 void chanrec::RemoveAllPrefixes(userrec* user)
1020 prefixlist::iterator n = prefixes.find(user);
1021 if (n != prefixes.end())