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();
33 age = ServerInstance->Time(true);
36 void chanrec::SetMode(char mode,bool mode_on)
38 modes[mode-65] = mode_on;
40 this->SetModeParam(mode,"",false);
44 void chanrec::SetModeParam(char mode,const char* parameter,bool mode_on)
46 ServerInstance->Log(DEBUG,"SetModeParam called");
48 CustomModeList::iterator n = custom_mode_params.find(mode);
52 if (n == custom_mode_params.end())
54 custom_mode_params[mode] = strdup(parameter);
55 ServerInstance->Log(DEBUG,"Custom mode parameter %c %s added",mode,parameter);
59 ServerInstance->Log(DEBUG, "Tried to set custom mode parameter for %c '%s' when it was already '%s'", mode, parameter, n->second);
64 if (n != custom_mode_params.end())
67 custom_mode_params.erase(n);
72 bool chanrec::IsModeSet(char mode)
74 return modes[mode-65];
77 std::string chanrec::GetModeParameter(char mode)
85 return ConvToStr(this->limit);
89 CustomModeList::iterator n = custom_mode_params.find(mode);
90 if (n != custom_mode_params.end())
98 long chanrec::GetUserCounter()
100 return (this->internal_userlist.size());
103 void chanrec::AddUser(userrec* user)
105 internal_userlist[user] = user;
108 unsigned long chanrec::DelUser(userrec* user)
110 CUListIter a = internal_userlist.find(user);
112 if (a != internal_userlist.end())
114 internal_userlist.erase(a);
115 /* And tidy any others... */
117 DelHalfoppedUser(user);
121 return internal_userlist.size();
124 bool chanrec::HasUser(userrec* user)
126 return (internal_userlist.find(user) != internal_userlist.end());
129 void chanrec::AddOppedUser(userrec* user)
131 internal_op_userlist[user] = user;
134 void chanrec::DelOppedUser(userrec* user)
136 CUListIter a = internal_op_userlist.find(user);
137 if (a != internal_op_userlist.end())
139 internal_op_userlist.erase(a);
144 void chanrec::AddHalfoppedUser(userrec* user)
146 internal_halfop_userlist[user] = user;
149 void chanrec::DelHalfoppedUser(userrec* user)
151 CUListIter a = internal_halfop_userlist.find(user);
153 if (a != internal_halfop_userlist.end())
155 internal_halfop_userlist.erase(a);
159 void chanrec::AddVoicedUser(userrec* user)
161 internal_voice_userlist[user] = user;
164 void chanrec::DelVoicedUser(userrec* user)
166 CUListIter a = internal_voice_userlist.find(user);
168 if (a != internal_voice_userlist.end())
170 internal_voice_userlist.erase(a);
174 CUList* chanrec::GetUsers()
176 return &internal_userlist;
179 CUList* chanrec::GetOppedUsers()
181 return &internal_op_userlist;
184 CUList* chanrec::GetHalfoppedUsers()
186 return &internal_halfop_userlist;
189 CUList* chanrec::GetVoicedUsers()
191 return &internal_voice_userlist;
195 * add a channel to a user, creating the record for it if needed and linking
196 * it to the user record
198 chanrec* chanrec::JoinUser(InspIRCd* Instance, userrec *user, const char* cn, bool override, const char* key)
203 bool new_channel = false;
206 strlcpy(cname,cn,CHANMAX);
210 chanrec* Ptr = Instance->FindChan(cname);
219 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,NULL,cname,privs));
224 /* create a new one */
225 Ptr = new chanrec(Instance);
226 Instance->chanlist[cname] = Ptr;
228 strlcpy(Ptr->name, cname,CHANMAX);
230 /* As spotted by jilles, dont bother to set this on remote users */
232 Ptr->modes[CM_TOPICLOCK] = Ptr->modes[CM_NOEXTERNAL] = 1;
234 Ptr->created = Instance->Time();
238 Instance->Log(DEBUG,"chanrec::JoinUser(): created: %s",cname);
243 /* Already on the channel */
244 if (Ptr->HasUser(user))
248 * remote users are allowed us to bypass channel modes
249 * and bans (used by servers)
251 if (IS_LOCAL(user)) /* was a check on fd > -1 */
254 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname,privs));
259 else if (MOD_RESULT == 0)
264 FOREACH_RESULT_I(Instance,I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
267 if ((!key) || strcmp(key,Ptr->key))
269 user->WriteServ("475 %s %s :Cannot join channel (Incorrect channel key)",user->nick, Ptr->name);
274 if (Ptr->modes[CM_INVITEONLY])
277 irc::string xname(Ptr->name);
278 FOREACH_RESULT_I(Instance,I_OnCheckInvite,OnCheckInvite(user, Ptr));
281 if (user->IsInvited(xname))
283 /* user was invited to channel */
284 /* there may be an optional channel NOTICE here */
288 user->WriteServ("473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
292 user->RemoveInvite(xname);
297 FOREACH_RESULT_I(Instance,I_OnCheckLimit,OnCheckLimit(user, Ptr));
300 if (Ptr->GetUserCounter() >= Ptr->limit)
302 user->WriteServ("471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
307 if (Ptr->bans.size())
309 if (Ptr->IsBanned(user))
311 user->WriteServ("474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
319 Instance->Log(DEBUG,"chanrec::JoinUser(): Overridden checks");
323 for (UserChanList::const_iterator index = user->chans.begin(); index != user->chans.end(); index++)
325 if ((*index)->channel == NULL)
327 return chanrec::ForceChan(Instance, Ptr, *index, user, privs);
332 * XXX: If the user is an oper here, we can just extend their user->chans vector by one
333 * and put the channel in here. Same for remote users which are not bound by
334 * the channel limits. Otherwise, nope, youre boned.
336 if (!IS_LOCAL(user)) /* was a check on fd < 0 */
338 ucrec* a = new ucrec();
339 chanrec* c = chanrec::ForceChan(Instance, Ptr, a, user, privs);
340 user->chans.push_back(a);
343 else if (*user->oper)
345 /* Oper allows extension up to the OPERMAXCHANS value */
346 if (user->chans.size() < OPERMAXCHANS)
348 ucrec* a = new ucrec();
349 chanrec* c = chanrec::ForceChan(Instance, Ptr, a, user, privs);
350 user->chans.push_back(a);
355 user->WriteServ("405 %s %s :You are on too many channels",user->nick, cname);
359 Instance->Log(DEBUG,"BLAMMO, Whacking channel.");
360 /* Things went seriously pear shaped, so take this away. bwahaha. */
361 chan_hash::iterator n = Instance->chanlist.find(cname);
362 if (n != Instance->chanlist.end())
366 Instance->chanlist.erase(n);
367 for (unsigned int index =0; index < user->chans.size(); index++)
369 if (user->chans[index]->channel == Ptr)
371 user->chans[index]->channel = NULL;
372 user->chans[index]->uc_modes = 0;
379 for (unsigned int index =0; index < user->chans.size(); index++)
381 if (user->chans[index]->channel == Ptr)
383 user->chans[index]->channel = NULL;
384 user->chans[index]->uc_modes = 0;
391 chanrec* chanrec::ForceChan(InspIRCd* Instance, chanrec* Ptr,ucrec *a,userrec* user, const std::string &privs)
393 userrec* dummyuser = new userrec(Instance);
394 std::string nick = user->nick;
397 dummyuser->SetFd(FD_MAGIC_NUMBER);
401 user->ModChannelCount(1);
403 for (std::string::const_iterator x = privs.begin(); x != privs.end(); x++)
405 const char status = *x;
406 ModeHandler* mh = Instance->Modes->FindPrefix(status);
409 Ptr->SetPrefix(user, status, mh->GetPrefixRank(), true);
410 /* Make sure that the mode handler knows this mode was now set */
411 mh->OnModeChange(dummyuser, dummyuser, Ptr, nick, true);
417 Ptr->WriteChannel(user,"JOIN :%s",Ptr->name);
419 /* Theyre not the first ones in here, make sure everyone else sees the modes we gave the user */
420 std::string ms = Instance->Modes->ModeString(user, Ptr);
421 if ((Ptr->GetUserCounter() > 1) && (ms.length()))
422 Ptr->WriteAllExceptSender(user, true, 0, "MODE %s +%s", Ptr->name, ms.c_str());
424 /* Major improvement by Brain - we dont need to be calculating all this pointlessly for remote users */
429 user->WriteServ("332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
430 user->WriteServ("333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
434 FOREACH_MOD_I(Instance,I_OnUserJoin,OnUserJoin(user,Ptr));
435 FOREACH_MOD_I(Instance,I_OnPostJoin,OnPostJoin(user,Ptr));
439 bool chanrec::IsBanned(userrec* user)
443 FOREACH_RESULT(I_OnCheckBan,OnCheckBan(user, this));
446 snprintf(mask, MAXBUF, "%s!%s@%s", user->nick, user->ident, user->GetIPString());
447 for (BanList::iterator i = this->bans.begin(); i != this->bans.end(); i++)
449 /* This allows CIDR ban matching
451 * Full masked host Full unmasked host IP with/without CIDR
453 if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match(mask, i->data, true)))
463 * remove a channel from a users record, and return the number of users left.
464 * Therefore, if this function returns 0 the caller should delete the chanrec.
466 long chanrec::PartUser(userrec *user, const char* reason)
469 return this->GetUserCounter();
471 for (unsigned int i =0; i < user->chans.size(); i++)
473 /* zap it from the channel list of the user */
474 if (user->chans[i]->channel == this)
476 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, reason ? reason : ""));
477 this->WriteChannel(user, "PART %s%s%s", this->name, reason ? " :" : "", reason ? reason : "");
478 user->chans[i]->uc_modes = 0;
479 user->chans[i]->channel = NULL;
480 user->ModChannelCount(-1);
481 this->RemoveAllPrefixes(user);
486 if (!this->DelUser(user)) /* if there are no users left on the channel... */
488 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
489 /* kill the record */
490 if (iter != ServerInstance->chanlist.end())
492 ServerInstance->Log(DEBUG,"del_channel: destroyed: %s", this->name);
493 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
494 ServerInstance->chanlist.erase(iter);
499 return this->GetUserCounter();
502 long chanrec::ServerKickUser(userrec* user, const char* reason, bool triggerevents)
504 if (!user || !reason)
505 return this->GetUserCounter();
509 if (!this->HasUser(user))
512 return this->GetUserCounter();
518 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL,user,this,reason));
521 for (unsigned int i =0; i < user->chans.size(); i++)
523 if (user->chans[i]->channel == this)
525 this->WriteChannelWithServ(ServerInstance->Config->ServerName, "KICK %s %s :%s", this->name, user->nick, reason);
526 user->chans[i]->uc_modes = 0;
527 user->chans[i]->channel = NULL;
528 this->RemoveAllPrefixes(user);
533 if (!this->DelUser(user))
535 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
536 /* kill the record */
537 if (iter != ServerInstance->chanlist.end())
539 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
540 ServerInstance->chanlist.erase(iter);
545 return this->GetUserCounter();
548 long chanrec::KickUser(userrec *src, userrec *user, const char* reason)
550 if (!src || !user || !reason)
551 return this->GetUserCounter();
555 if (!this->HasUser(user))
557 src->WriteServ("441 %s %s %s :They are not on that channel",src->nick, user->nick, this->name);
558 return this->GetUserCounter();
560 if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
562 src->WriteServ("482 %s %s :Only a u-line may kick a u-line from a channel.",src->nick, this->name);
563 return this->GetUserCounter();
567 if (!ServerInstance->ULine(src->server))
570 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,this,reason));
572 return this->GetUserCounter();
574 /* Set to -1 by OnUserPreKick if explicit allow was set */
575 if (MOD_RESULT != -1)
577 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,this,AC_KICK));
578 if ((MOD_RESULT == ACR_DENY) && (!ServerInstance->ULine(src->server)))
579 return this->GetUserCounter();
581 if ((MOD_RESULT == ACR_DEFAULT) || (!ServerInstance->ULine(src->server)))
583 int them = this->GetStatus(src);
584 int us = this->GetStatus(user);
585 if ((them < STATUS_HOP) || (them < us))
587 src->WriteServ("482 %s %s :You must be a channel %soperator",src->nick, this->name, them == STATUS_HOP ? "" : "half-");
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::WriteAllExcept(userrec* user, bool serversource, char status, CUList &except_list, char* text, ...)
700 char textbuffer[MAXBUF];
706 va_start(argsPtr, text);
707 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
710 this->WriteAllExcept(user, serversource, status, except_list, std::string(textbuffer));
713 void chanrec::WriteAllExcept(userrec* user, bool serversource, char status, CUList &except_list, const std::string &text)
720 ulist = this->GetOppedUsers();
723 ulist = this->GetHalfoppedUsers();
726 ulist = this->GetVoicedUsers();
729 ulist = this->GetUsers();
733 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
735 if ((IS_LOCAL(i->second)) && (except_list.find(i->second) == except_list.end()))
738 i->second->WriteServ(text);
740 i->second->WriteFrom(user,text);
745 void chanrec::WriteAllExceptSender(userrec* user, bool serversource, char status, const std::string& text)
748 except_list[user] = user;
749 this->WriteAllExcept(user, serversource, status, except_list, std::string(text));
753 * return a count of the users on a specific channel accounting for
754 * invisible users who won't increase the count. e.g. for /LIST
756 int chanrec::CountInvisible()
759 CUList *ulist= this->GetUsers();
760 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
762 if (!(i->second->modes[UM_INVISIBLE]))
769 char* chanrec::ChanModes(bool showkey)
771 static char scratch[MAXBUF];
772 static char sparam[MAXBUF];
773 char* offset = scratch;
774 std::string extparam = "";
779 /* This was still iterating up to 190, chanrec::modes is only 64 elements -- Om */
780 for(int n = 0; n < 64; n++)
789 extparam = (showkey ? this->key : "<key>");
792 extparam = ConvToStr(this->limit);
800 /* We know these have no parameters */
803 extparam = this->GetModeParameter(n + 65);
808 charlcat(sparam,' ',MAXBUF);
809 strlcat(sparam,extparam.c_str(),MAXBUF);
814 /* Null terminate scratch */
816 strlcat(scratch,sparam,MAXBUF);
820 /* compile a userlist of a channel into a string, each nick seperated by
821 * spaces and op, voice etc status shown as @ and +, and send it to 'user'
823 void chanrec::UserList(userrec *user)
832 FOREACH_RESULT(I_OnUserList,OnUserList(user, this));
833 ServerInstance->Log(DEBUG,"MOD_RESULT for UserList = %d",MOD_RESULT);
837 ServerInstance->Log(DEBUG,"Using builtin NAMES list generation");
839 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
842 char* ptr = list + dlen;
844 CUList *ulist= this->GetUsers();
846 /* Improvement by Brain - this doesnt change in value, so why was it inside
849 bool has_user = this->HasUser(user);
851 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
853 if ((!has_user) && (i->second->modes[UM_INVISIBLE]))
856 * user is +i, and source not on the channel, does not show
862 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", this->GetPrefixChar(i->second), i->second->nick);
869 if (curlen > (480-NICKMAX))
871 /* list overflowed into multiple numerics */
872 user->WriteServ(std::string(list));
874 /* reset our lengths */
875 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
883 /* if whats left in the list isnt empty, send it */
886 user->WriteServ(std::string(list));
889 user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, this->name);
892 long chanrec::GetMaxBans()
894 for (std::map<std::string,int>::iterator n = ServerInstance->Config->maxbans.begin(); n != ServerInstance->Config->maxbans.end(); n++)
896 if (match(this->name,n->first.c_str()))
905 /* returns the status character for a given user on a channel, e.g. @ for op,
906 * % for halfop etc. If the user has several modes set, the highest mode
907 * the user has must be returned.
909 const char* chanrec::GetPrefixChar(userrec *user)
911 static char pf[2] = {0, 0};
913 prefixlist::iterator n = prefixes.find(user);
914 if (n != prefixes.end())
916 if (n->second.size())
918 /* If the user has any prefixes, their highest prefix
919 * will always be at the head of the list, as the list is
920 * sorted in rank order highest first (see SetPrefix()
923 *pf = n->second.begin()->first;
932 const char* chanrec::GetAllPrefixChars(userrec* user)
934 static char prefix[MAXBUF];
938 prefixlist::iterator n = prefixes.find(user);
939 if (n != prefixes.end())
941 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
943 prefix[ctr++] = x->first;
952 unsigned int chanrec::GetPrefixValue(userrec* user)
954 prefixlist::iterator n = prefixes.find(user);
955 if (n != prefixes.end())
957 if (n->second.size())
958 return n->second.begin()->second;
964 int chanrec::GetStatusFlags(userrec *user)
966 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
968 if ((*i)->channel == this)
970 return (*i)->uc_modes;
977 int chanrec::GetStatus(userrec *user)
979 if (ServerInstance->ULine(user->server))
982 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
984 if ((*i)->channel == this)
986 if (((*i)->uc_modes & UCMODE_OP) > 0)
990 if (((*i)->uc_modes & UCMODE_HOP) > 0)
994 if (((*i)->uc_modes & UCMODE_VOICE) > 0)
998 return STATUS_NORMAL;
1001 return STATUS_NORMAL;
1004 void chanrec::SetPrefix(userrec* user, char prefix, unsigned int prefix_value, bool adding)
1006 prefixlist::iterator n = prefixes.find(user);
1007 prefixtype pfx = std::make_pair(prefix,prefix_value);
1010 if (n != prefixes.end())
1012 if (std::find(n->second.begin(), n->second.end(), pfx) == n->second.end())
1014 n->second.push_back(pfx);
1015 /* We must keep prefixes in rank order, largest first.
1016 * This is for two reasons, firstly because x-chat *ass-u-me's* this
1017 * state, and secondly it turns out to be a benefit to us later.
1018 * See above in GetPrefix().
1020 std::sort(n->second.begin(), n->second.end(), ModeParser::PrefixComparison);
1027 prefixes.insert(std::make_pair<userrec*,pfxcontainer>(user, one));
1032 if (n != prefixes.end())
1034 pfxcontainer::iterator x = std::find(n->second.begin(), n->second.end(), pfx);
1035 if (x != n->second.end())
1039 ServerInstance->Log(DEBUG,"Added prefix %c to %s for %s, prefixlist size is now %d", prefix, this->name, user->nick, prefixes.size());
1042 void chanrec::RemoveAllPrefixes(userrec* user)
1044 prefixlist::iterator n = prefixes.find(user);
1045 if (n != prefixes.end())
1047 ServerInstance->Log(DEBUG,"Removed prefixes from %s for %s, prefixlist size is now %d", this->name, user->nick, prefixes.size());