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();
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 : ""));
263 if ((!key) || strcmp(key,Ptr->key))
265 user->WriteServ("475 %s %s :Cannot join channel (Incorrect channel key)",user->nick, Ptr->name);
270 if (Ptr->modes[CM_INVITEONLY])
273 irc::string xname(Ptr->name);
274 FOREACH_RESULT_I(Instance,I_OnCheckInvite,OnCheckInvite(user, Ptr));
277 if (user->IsInvited(xname))
279 /* user was invited to channel */
280 /* there may be an optional channel NOTICE here */
284 user->WriteServ("473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
288 user->RemoveInvite(xname);
293 FOREACH_RESULT_I(Instance,I_OnCheckLimit,OnCheckLimit(user, Ptr));
296 if (Ptr->GetUserCounter() >= Ptr->limit)
298 user->WriteServ("471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
303 if (Ptr->bans.size())
306 FOREACH_RESULT_I(Instance,I_OnCheckBan,OnCheckBan(user, Ptr));
308 sprintf(mask,"%s!%s@%s",user->nick, user->ident, user->GetIPString());
311 if (Ptr->IsBanned(user))
313 user->WriteServ("474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
322 Instance->Log(DEBUG,"chanrec::JoinUser(): Overridden checks");
327 for (UserChanList::const_iterator index = user->chans.begin(); index != user->chans.end(); index++)
329 if ((*index)->channel == NULL)
331 return chanrec::ForceChan(Instance, Ptr, *index, user, created);
336 * XXX: If the user is an oper here, we can just extend their user->chans vector by one
337 * and put the channel in here. Same for remote users which are not bound by
338 * the channel limits. Otherwise, nope, youre boned.
340 if (!IS_LOCAL(user)) /* was a check on fd < 0 */
342 ucrec* a = new ucrec();
343 chanrec* c = chanrec::ForceChan(Instance, Ptr,a,user,created);
344 user->chans.push_back(a);
347 else if (*user->oper)
349 /* Oper allows extension up to the OPERMAXCHANS value */
350 if (user->chans.size() < OPERMAXCHANS)
352 ucrec* a = new ucrec();
353 chanrec* c = chanrec::ForceChan(Instance, Ptr,a,user,created);
354 user->chans.push_back(a);
359 user->WriteServ("405 %s %s :You are on too many channels",user->nick, cname);
363 Instance->Log(DEBUG,"BLAMMO, Whacking channel.");
364 /* Things went seriously pear shaped, so take this away. bwahaha. */
365 chan_hash::iterator n = Instance->chanlist.find(cname);
366 if (n != Instance->chanlist.end())
370 Instance->chanlist.erase(n);
371 for (unsigned int index =0; index < user->chans.size(); index++)
373 if (user->chans[index]->channel == Ptr)
375 user->chans[index]->channel = NULL;
376 user->chans[index]->uc_modes = 0;
383 for (unsigned int index =0; index < user->chans.size(); index++)
385 if (user->chans[index]->channel == Ptr)
387 user->chans[index]->channel = NULL;
388 user->chans[index]->uc_modes = 0;
395 chanrec* chanrec::ForceChan(InspIRCd* Instance, chanrec* Ptr,ucrec *a,userrec* user, int created)
399 /* first user in is given ops */
400 a->uc_modes = UCMODE_OP;
401 Ptr->AddOppedUser(user);
402 Ptr->SetPrefix(user, '@', OP_VALUE, true);
411 user->ModChannelCount(1);
412 Ptr->WriteChannel(user,"JOIN :%s",Ptr->name);
414 /* Major improvement by Brain - we dont need to be calculating all this pointlessly for remote users */
419 user->WriteServ("332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
420 user->WriteServ("333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
424 FOREACH_MOD_I(Instance,I_OnUserJoin,OnUserJoin(user,Ptr));
428 bool chanrec::IsBanned(userrec* user)
431 sprintf(mask,"%s!%s@%s",user->nick, user->ident, user->GetIPString());
432 for (BanList::iterator i = this->bans.begin(); i != this->bans.end(); i++)
434 /* This allows CIDR ban matching
436 * Full masked host Full unmasked host IP with/without CIDR
438 if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match(mask, i->data, true)))
448 * remove a channel from a users record, and remove the record from the hash
449 * if the channel has become empty
451 long chanrec::PartUser(userrec *user, const char* reason)
454 return this->GetUserCounter();
456 for (unsigned int i =0; i < user->chans.size(); i++)
458 /* zap it from the channel list of the user */
459 if (user->chans[i]->channel == this)
463 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, reason));
464 this->WriteChannel(user, "PART %s :%s", this->name, reason);
468 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, ""));
469 this->WriteChannel(user, "PART :%s", this->name);
471 user->chans[i]->uc_modes = 0;
472 user->chans[i]->channel = NULL;
473 user->ModChannelCount(-1);
474 this->RemoveAllPrefixes(user);
479 if (!this->DelUser(user)) /* if there are no users left on the channel... */
481 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
482 /* kill the record */
483 if (iter != ServerInstance->chanlist.end())
485 ServerInstance->Log(DEBUG,"del_channel: destroyed: %s", this->name);
486 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
487 ServerInstance->chanlist.erase(iter);
492 return this->GetUserCounter();
495 long chanrec::ServerKickUser(userrec* user, const char* reason, bool triggerevents)
497 if (!user || !reason)
498 return this->GetUserCounter();
502 if (!this->HasUser(user))
505 return this->GetUserCounter();
511 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL,user,this,reason));
514 for (unsigned int i =0; i < user->chans.size(); i++)
516 if (user->chans[i]->channel == this)
518 this->WriteChannelWithServ(ServerInstance->Config->ServerName, "KICK %s %s :%s", this->name, user->nick, reason);
519 user->chans[i]->uc_modes = 0;
520 user->chans[i]->channel = NULL;
521 this->RemoveAllPrefixes(user);
526 if (!this->DelUser(user))
528 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
529 /* kill the record */
530 if (iter != ServerInstance->chanlist.end())
532 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
533 ServerInstance->chanlist.erase(iter);
538 return this->GetUserCounter();
541 long chanrec::KickUser(userrec *src, userrec *user, const char* reason)
543 if (!src || !user || !reason)
544 return this->GetUserCounter();
548 if (!this->HasUser(user))
550 src->WriteServ("441 %s %s %s :They are not on that channel",src->nick, user->nick, this->name);
551 return this->GetUserCounter();
553 if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
555 src->WriteServ("482 %s %s :Only a u-line may kick a u-line from a channel.",src->nick, this->name);
556 return this->GetUserCounter();
560 if (!ServerInstance->ULine(src->server))
563 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,this,reason));
565 return this->GetUserCounter();
567 /* Set to -1 by OnUserPreKick if explicit allow was set */
568 if (MOD_RESULT != -1)
570 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,this,AC_KICK));
571 if ((MOD_RESULT == ACR_DENY) && (!ServerInstance->ULine(src->server)))
572 return this->GetUserCounter();
574 if ((MOD_RESULT == ACR_DEFAULT) || (!ServerInstance->ULine(src->server)))
576 int them = this->GetStatus(src);
577 int us = this->GetStatus(user);
578 if ((them < STATUS_HOP) || (them < us))
580 if (them == STATUS_HOP)
582 src->WriteServ("482 %s %s :You must be a channel operator",src->nick, this->name);
586 src->WriteServ("482 %s %s :You must be at least a half-operator",src->nick, this->name);
588 return this->GetUserCounter();
594 FOREACH_MOD(I_OnUserKick,OnUserKick(src,user,this,reason));
596 for (UserChanList::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
598 /* zap it from the channel list of the user */
599 if ((*i)->channel == this)
601 this->WriteChannel(src, "KICK %s %s :%s", this->name, user->nick, reason);
603 (*i)->channel = NULL;
604 this->RemoveAllPrefixes(user);
609 if (!this->DelUser(user))
610 /* if there are no users left on the channel */
612 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
614 /* kill the record */
615 if (iter != ServerInstance->chanlist.end())
617 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
618 ServerInstance->chanlist.erase(iter);
623 return this->GetUserCounter();
626 void chanrec::WriteChannel(userrec* user, char* text, ...)
628 char textbuffer[MAXBUF];
634 va_start(argsPtr, text);
635 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
638 this->WriteChannel(user, std::string(textbuffer));
641 void chanrec::WriteChannel(userrec* user, const std::string &text)
643 CUList *ulist = this->GetUsers();
648 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
650 if (IS_LOCAL(i->second))
651 user->WriteTo(i->second,text);
655 void chanrec::WriteChannelWithServ(const char* ServName, const char* text, ...)
657 char textbuffer[MAXBUF];
663 va_start(argsPtr, text);
664 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
667 this->WriteChannelWithServ(ServName, std::string(textbuffer));
670 void chanrec::WriteChannelWithServ(const char* ServName, const std::string &text)
672 CUList *ulist = this->GetUsers();
674 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
676 if (IS_LOCAL(i->second))
677 i->second->WriteServ(text);
681 /* write formatted text from a source user to all users on a channel except
682 * for the sender (for privmsg etc) */
683 void chanrec::WriteAllExceptSender(userrec* user, bool serversource, char status, char* text, ...)
685 char textbuffer[MAXBUF];
691 va_start(argsPtr, text);
692 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
695 this->WriteAllExceptSender(user, serversource, status, std::string(textbuffer));
698 void chanrec::WriteAllExceptSender(userrec* user, bool serversource, char status, const std::string& text)
705 ulist = this->GetOppedUsers();
708 ulist = this->GetHalfoppedUsers();
711 ulist = this->GetVoicedUsers();
714 ulist = this->GetUsers();
718 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
720 if ((IS_LOCAL(i->second)) && (user != i->second))
723 i->second->WriteServ(text);
725 i->second->WriteFrom(user,text);
731 * return a count of the users on a specific channel accounting for
732 * invisible users who won't increase the count. e.g. for /LIST
734 int chanrec::CountInvisible()
737 CUList *ulist= this->GetUsers();
738 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
740 if (!(i->second->modes[UM_INVISIBLE]))
747 char* chanrec::ChanModes(bool showkey)
749 static char scratch[MAXBUF];
750 static char sparam[MAXBUF];
751 char* offset = scratch;
752 std::string extparam = "";
757 /* This was still iterating up to 190, chanrec::custom_modes is only 64 elements -- Om */
758 for(int n = 0; n < 64; n++)
767 extparam = (showkey ? this->key : "<key>");
770 extparam = ConvToStr(this->limit);
778 /* We know these have no parameters */
781 extparam = this->GetModeParameter(n + 65);
786 charlcat(sparam,' ',MAXBUF);
787 strlcat(sparam,extparam.c_str(),MAXBUF);
792 /* Null terminate scratch */
794 strlcat(scratch,sparam,MAXBUF);
798 /* compile a userlist of a channel into a string, each nick seperated by
799 * spaces and op, voice etc status shown as @ and +, and send it to 'user'
801 void chanrec::UserList(userrec *user)
807 FOREACH_RESULT(I_OnUserList,OnUserList(user, this));
808 ServerInstance->Log(DEBUG,"MOD_RESULT for UserList = %d",MOD_RESULT);
812 ServerInstance->Log(DEBUG,"Using builtin NAMES list generation");
814 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
817 char* ptr = list + dlen;
819 CUList *ulist= this->GetUsers();
821 /* Improvement by Brain - this doesnt change in value, so why was it inside
824 bool has_user = this->HasUser(user);
826 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
828 if ((!has_user) && (i->second->modes[UM_INVISIBLE]))
831 * user is +i, and source not on the channel, does not show
837 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", this->GetPrefixChar(i->second), i->second->nick);
844 if (curlen > (480-NICKMAX))
846 /* list overflowed into multiple numerics */
847 user->WriteServ(std::string(list));
849 /* reset our lengths */
850 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
858 /* if whats left in the list isnt empty, send it */
861 user->WriteServ(std::string(list));
864 user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, this->name);
867 long chanrec::GetMaxBans()
870 for (std::map<std::string,int>::iterator n = ServerInstance->Config->maxbans.begin(); n != ServerInstance->Config->maxbans.end(); n++)
873 if (match(this->name,x.c_str()))
882 /* returns the status character for a given user on a channel, e.g. @ for op,
883 * % for halfop etc. If the user has several modes set, the highest mode
884 * the user has must be returned.
886 const char* chanrec::GetPrefixChar(userrec *user)
894 prefixlist::iterator n = prefixes.find(user);
895 if (n != prefixes.end())
897 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
910 const char* chanrec::GetAllPrefixChars(userrec* user)
912 static char prefix[MAXBUF];
916 prefixlist::iterator n = prefixes.find(user);
917 if (n != prefixes.end())
919 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
921 prefix[ctr++] = x->first;
930 unsigned int chanrec::GetPrefixValue(userrec* user)
934 prefixlist::iterator n = prefixes.find(user);
935 if (n != prefixes.end())
937 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
948 int chanrec::GetStatusFlags(userrec *user)
950 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
952 if ((*i)->channel == this)
954 return (*i)->uc_modes;
961 int chanrec::GetStatus(userrec *user)
963 if (ServerInstance->ULine(user->server))
966 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
968 if ((*i)->channel == this)
970 if (((*i)->uc_modes & UCMODE_OP) > 0)
974 if (((*i)->uc_modes & UCMODE_HOP) > 0)
978 if (((*i)->uc_modes & UCMODE_VOICE) > 0)
982 return STATUS_NORMAL;
985 return STATUS_NORMAL;
988 /*bool ModeParser::PrefixComparison(const prefixtype one, const prefixtype two)
990 return one.second > two.second;
993 void chanrec::SetPrefix(userrec* user, char prefix, unsigned int prefix_value, bool adding)
995 prefixlist::iterator n = prefixes.find(user);
996 prefixtype pfx = std::make_pair(prefix,prefix_value);
999 if (n != prefixes.end())
1001 if (std::find(n->second.begin(), n->second.end(), pfx) == n->second.end())
1003 n->second.push_back(pfx);
1004 std::sort(n->second.begin(), n->second.end(), ModeParser::PrefixComparison);
1011 prefixes.insert(std::make_pair<userrec*,pfxcontainer>(user, one));
1016 if (n != prefixes.end())
1018 pfxcontainer::iterator x = std::find(n->second.begin(), n->second.end(), pfx);
1019 if (x != n->second.end())
1025 void chanrec::RemoveAllPrefixes(userrec* user)
1027 prefixlist::iterator n = prefixes.find(user);
1028 if (n != prefixes.end())