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 Ptr->WriteChannel(user,"JOIN :%s",Ptr->name);
430 /* Major improvement by Brain - we dont need to be calculating all this pointlessly for remote users */
435 user->WriteServ("332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
436 user->WriteServ("333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
440 FOREACH_MOD_I(Instance,I_OnUserJoin,OnUserJoin(user,Ptr));
445 * remove a channel from a users record, and remove the record from the hash
446 * if the channel has become empty
448 long chanrec::PartUser(userrec *user, const char* reason)
451 return this->GetUserCounter();
453 for (unsigned int i =0; i < user->chans.size(); i++)
455 /* zap it from the channel list of the user */
456 if (user->chans[i]->channel == this)
460 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, reason));
461 this->WriteChannel(user, "PART %s :%s", this->name, reason);
465 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, ""));
466 this->WriteChannel(user, "PART :%s", this->name);
468 user->chans[i]->uc_modes = 0;
469 user->chans[i]->channel = NULL;
470 this->RemoveAllPrefixes(user);
475 if (!this->DelUser(user)) /* if there are no users left on the channel... */
477 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
478 /* kill the record */
479 if (iter != ServerInstance->chanlist.end())
481 ServerInstance->Log(DEBUG,"del_channel: destroyed: %s", this->name);
482 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
483 ServerInstance->chanlist.erase(iter);
488 return this->GetUserCounter();
491 long chanrec::ServerKickUser(userrec* user, const char* reason, bool triggerevents)
493 if (!user || !reason)
494 return this->GetUserCounter();
498 if (!this->HasUser(user))
501 return this->GetUserCounter();
507 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL,user,this,reason));
510 for (unsigned int i =0; i < user->chans.size(); i++)
512 if (user->chans[i]->channel == this)
514 this->WriteChannelWithServ(ServerInstance->Config->ServerName, "KICK %s %s :%s", this->name, user->nick, reason);
515 user->chans[i]->uc_modes = 0;
516 user->chans[i]->channel = NULL;
517 this->RemoveAllPrefixes(user);
522 if (!this->DelUser(user))
524 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
525 /* kill the record */
526 if (iter != ServerInstance->chanlist.end())
528 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
529 ServerInstance->chanlist.erase(iter);
534 return this->GetUserCounter();
537 long chanrec::KickUser(userrec *src, userrec *user, const char* reason)
539 if (!src || !user || !reason)
540 return this->GetUserCounter();
544 if (!this->HasUser(user))
546 src->WriteServ("441 %s %s %s :They are not on that channel",src->nick, user->nick, this->name);
547 return this->GetUserCounter();
549 if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
551 src->WriteServ("482 %s %s :Only a u-line may kick a u-line from a channel.",src->nick, this->name);
552 return this->GetUserCounter();
556 if (!ServerInstance->ULine(src->server))
559 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,this,reason));
561 return this->GetUserCounter();
563 /* Set to -1 by OnUserPreKick if explicit allow was set */
564 if (MOD_RESULT != -1)
566 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,this,AC_KICK));
567 if ((MOD_RESULT == ACR_DENY) && (!ServerInstance->ULine(src->server)))
568 return this->GetUserCounter();
570 if ((MOD_RESULT == ACR_DEFAULT) || (!ServerInstance->ULine(src->server)))
572 int them = this->GetStatus(src);
573 int us = this->GetStatus(user);
574 if ((them < STATUS_HOP) || (them < us))
576 if (them == STATUS_HOP)
578 src->WriteServ("482 %s %s :You must be a channel operator",src->nick, this->name);
582 src->WriteServ("482 %s %s :You must be at least a half-operator",src->nick, this->name);
584 return this->GetUserCounter();
590 FOREACH_MOD(I_OnUserKick,OnUserKick(src,user,this,reason));
592 for (UserChanList::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
594 /* zap it from the channel list of the user */
595 if ((*i)->channel == this)
597 this->WriteChannel(src, "KICK %s %s :%s", this->name, user->nick, reason);
599 (*i)->channel = NULL;
600 this->RemoveAllPrefixes(user);
605 if (!this->DelUser(user))
606 /* if there are no users left on the channel */
608 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
610 /* kill the record */
611 if (iter != ServerInstance->chanlist.end())
613 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
614 ServerInstance->chanlist.erase(iter);
619 return this->GetUserCounter();
622 void chanrec::WriteChannel(userrec* user, char* text, ...)
624 char textbuffer[MAXBUF];
630 va_start(argsPtr, text);
631 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
634 this->WriteChannel(user, std::string(textbuffer));
637 void chanrec::WriteChannel(userrec* user, const std::string &text)
639 CUList *ulist = this->GetUsers();
644 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
646 if (IS_LOCAL(i->second))
647 user->WriteTo(i->second,text);
651 void chanrec::WriteChannelWithServ(const char* ServName, const char* text, ...)
653 char textbuffer[MAXBUF];
659 va_start(argsPtr, text);
660 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
663 this->WriteChannelWithServ(ServName, std::string(textbuffer));
666 void chanrec::WriteChannelWithServ(const char* ServName, const std::string &text)
668 CUList *ulist = this->GetUsers();
670 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
672 if (IS_LOCAL(i->second))
673 i->second->WriteServ(text);
677 /* write formatted text from a source user to all users on a channel except
678 * for the sender (for privmsg etc) */
679 void chanrec::WriteAllExceptSender(userrec* user, char status, char* text, ...)
681 char textbuffer[MAXBUF];
687 va_start(argsPtr, text);
688 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
691 this->WriteAllExceptSender(user, status, std::string(textbuffer));
694 void chanrec::WriteAllExceptSender(userrec* user, char status, const std::string& text)
704 ulist = this->GetOppedUsers();
707 ulist = this->GetHalfoppedUsers();
710 ulist = this->GetVoicedUsers();
713 ulist = this->GetUsers();
717 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
719 if ((IS_LOCAL(i->second)) && (user != i->second))
720 i->second->WriteFrom(user,text);
725 * return a count of the users on a specific channel accounting for
726 * invisible users who won't increase the count. e.g. for /LIST
728 int chanrec::CountInvisible()
731 CUList *ulist= this->GetUsers();
732 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
734 if (!(i->second->modes[UM_INVISIBLE]))
741 char* chanrec::ChanModes(bool showkey)
743 static char scratch[MAXBUF];
744 static char sparam[MAXBUF];
745 char* offset = scratch;
746 std::string extparam = "";
751 /* This was still iterating up to 190, chanrec::custom_modes is only 64 elements -- Om */
752 for(int n = 0; n < 64; n++)
761 extparam = (showkey ? this->key : "<key>");
764 extparam = ConvToStr(this->limit);
772 /* We know these have no parameters */
775 extparam = this->GetModeParameter(n + 65);
780 charlcat(sparam,' ',MAXBUF);
781 strlcat(sparam,extparam.c_str(),MAXBUF);
786 /* Null terminate scratch */
788 strlcat(scratch,sparam,MAXBUF);
792 /* compile a userlist of a channel into a string, each nick seperated by
793 * spaces and op, voice etc status shown as @ and +, and send it to 'user'
795 void chanrec::UserList(userrec *user)
801 FOREACH_RESULT(I_OnUserList,OnUserList(user, this));
802 ServerInstance->Log(DEBUG,"MOD_RESULT for UserList = %d",MOD_RESULT);
806 ServerInstance->Log(DEBUG,"Using builtin NAMES list generation");
808 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
811 char* ptr = list + dlen;
813 CUList *ulist= this->GetUsers();
815 /* Improvement by Brain - this doesnt change in value, so why was it inside
818 bool has_user = this->HasUser(user);
820 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
822 if ((!has_user) && (i->second->modes[UM_INVISIBLE]))
825 * user is +i, and source not on the channel, does not show
831 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", this->GetPrefixChar(i->second), i->second->nick);
838 if (curlen > (480-NICKMAX))
840 /* list overflowed into multiple numerics */
841 user->WriteServ(list);
843 /* reset our lengths */
844 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
852 /* if whats left in the list isnt empty, send it */
855 user->WriteServ(list);
858 user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, this->name);
861 long chanrec::GetMaxBans()
864 for (std::map<std::string,int>::iterator n = ServerInstance->Config->maxbans.begin(); n != ServerInstance->Config->maxbans.end(); n++)
867 if (match(this->name,x.c_str()))
876 /* returns the status character for a given user on a channel, e.g. @ for op,
877 * % for halfop etc. If the user has several modes set, the highest mode
878 * the user has must be returned.
880 const char* chanrec::GetPrefixChar(userrec *user)
888 prefixlist::iterator n = prefixes.find(user);
889 if (n != prefixes.end())
891 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
904 const char* chanrec::GetAllPrefixChars(userrec* user)
906 static char prefix[MAXBUF];
910 prefixlist::iterator n = prefixes.find(user);
911 if (n != prefixes.end())
913 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
915 prefix[ctr++] = x->first;
924 unsigned int chanrec::GetPrefixValue(userrec* user)
928 prefixlist::iterator n = prefixes.find(user);
929 if (n != prefixes.end())
931 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
942 int chanrec::GetStatusFlags(userrec *user)
944 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
946 if ((*i)->channel == this)
948 return (*i)->uc_modes;
955 int chanrec::GetStatus(userrec *user)
957 if (ServerInstance->ULine(user->server))
960 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
962 if ((*i)->channel == this)
964 if (((*i)->uc_modes & UCMODE_OP) > 0)
968 if (((*i)->uc_modes & UCMODE_HOP) > 0)
972 if (((*i)->uc_modes & UCMODE_VOICE) > 0)
976 return STATUS_NORMAL;
979 return STATUS_NORMAL;
982 /*bool ModeParser::PrefixComparison(const prefixtype one, const prefixtype two)
984 return one.second > two.second;
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);
998 std::sort(n->second.begin(), n->second.end(), ModeParser::PrefixComparison);
1005 prefixes.insert(std::make_pair<userrec*,pfxcontainer>(user, one));
1010 if (n != prefixes.end())
1012 pfxcontainer::iterator x = std::find(n->second.begin(), n->second.end(), pfx);
1013 if (x != n->second.end())
1019 void chanrec::RemoveAllPrefixes(userrec* user)
1021 prefixlist::iterator n = prefixes.find(user);
1022 if (n != prefixes.end())