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;
32 age = ServerInstance->Time(true);
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)
202 bool new_channel = false;
205 strlcpy(cname,cn,CHANMAX);
209 chanrec* Ptr = Instance->FindChan(cname);
215 if (IS_LOCAL(user) && override == false)
218 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,NULL,cname,privs));
223 /* create a new one */
224 Ptr = new chanrec(Instance);
225 Instance->chanlist[cname] = Ptr;
227 strlcpy(Ptr->name, cname,CHANMAX);
229 /* As spotted by jilles, dont bother to set this on remote users */
231 Ptr->modes[CM_TOPICLOCK] = Ptr->modes[CM_NOEXTERNAL] = 1;
233 Ptr->created = Instance->Time();
237 Instance->Log(DEBUG,"chanrec::JoinUser(): created: %s",cname);
242 /* Already on the channel */
243 if (Ptr->HasUser(user))
247 * remote users are allowed us to bypass channel modes
248 * and bans (used by servers)
250 if (IS_LOCAL(user) && override == false)
253 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname,privs));
258 else if (MOD_RESULT == 0)
263 FOREACH_RESULT_I(Instance,I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
266 if ((!key) || strcmp(key,Ptr->key))
268 user->WriteServ("475 %s %s :Cannot join channel (Incorrect channel key)",user->nick, Ptr->name);
273 if (Ptr->modes[CM_INVITEONLY])
276 FOREACH_RESULT_I(Instance,I_OnCheckInvite,OnCheckInvite(user, Ptr));
279 if (user->IsInvited(Ptr->name))
281 /* user was invited to channel */
282 /* there may be an optional channel NOTICE here */
286 user->WriteServ("473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
290 user->RemoveInvite(Ptr->name);
295 FOREACH_RESULT_I(Instance,I_OnCheckLimit,OnCheckLimit(user, Ptr));
298 if (Ptr->GetUserCounter() >= Ptr->limit)
300 user->WriteServ("471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
305 if (Ptr->bans.size())
307 if (Ptr->IsBanned(user))
309 user->WriteServ("474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
317 Instance->Log(DEBUG,"chanrec::JoinUser(): Overridden checks");
321 /* NOTE: If the user is an oper here, we can extend their user->chans by up to
322 * OPERMAXCHANS. For remote users which are not bound by the channel limits,
323 * we can extend infinitely. Otherwise, nope, youre restricted to MAXCHANS.
325 if (!IS_LOCAL(user) || override == true) /* was a check on fd < 0 */
327 return chanrec::ForceChan(Instance, Ptr, user, privs);
329 else if (*user->oper)
331 /* Oper allows extension up to the OPERMAXCHANS value */
332 if (user->chans.size() < OPERMAXCHANS)
334 return chanrec::ForceChan(Instance, Ptr, user, privs);
337 else if (user->chans.size() < MAXCHANS)
339 return chanrec::ForceChan(Instance, Ptr, user, privs);
343 user->WriteServ("405 %s %s :You are on too many channels",user->nick, cname);
347 Instance->Log(DEBUG,"BLAMMO, Whacking channel.");
348 /* Things went seriously pear shaped, so take this away. bwahaha. */
349 chan_hash::iterator n = Instance->chanlist.find(cname);
350 if (n != Instance->chanlist.end())
354 Instance->chanlist.erase(n);
361 chanrec* chanrec::ForceChan(InspIRCd* Instance, chanrec* Ptr, userrec* user, const std::string &privs)
363 userrec* dummyuser = new userrec(Instance);
364 std::string nick = user->nick;
366 dummyuser->SetFd(FD_MAGIC_NUMBER);
369 /* Just in case they have no permissions */
370 user->chans[Ptr] = 0;
372 for (std::string::const_iterator x = privs.begin(); x != privs.end(); x++)
374 const char status = *x;
375 ModeHandler* mh = Instance->Modes->FindPrefix(status);
378 Ptr->SetPrefix(user, status, mh->GetPrefixRank(), true);
379 /* Make sure that the mode handler knows this mode was now set */
380 mh->OnModeChange(dummyuser, dummyuser, Ptr, nick, true);
382 switch (mh->GetPrefix())
384 /* These logic ops are SAFE IN THIS CASE
385 * because if the entry doesnt exist,
386 * addressing operator[] creates it.
387 * If they do exist, it points to it.
388 * At all other times where we dont want
389 * to create an item if it doesnt exist, we
390 * must stick to ::find().
393 user->chans[Ptr] |= STATUS_OP;
396 user->chans[Ptr] |= STATUS_HOP;
399 user->chans[Ptr] |= STATUS_VOICE;
407 Ptr->WriteChannel(user,"JOIN :%s",Ptr->name);
409 /* Theyre not the first ones in here, make sure everyone else sees the modes we gave the user */
410 std::string ms = Instance->Modes->ModeString(user, Ptr);
411 if ((Ptr->GetUserCounter() > 1) && (ms.length()))
412 Ptr->WriteAllExceptSender(user, true, 0, "MODE %s +%s", Ptr->name, ms.c_str());
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));
425 FOREACH_MOD_I(Instance,I_OnPostJoin,OnPostJoin(user,Ptr));
429 bool chanrec::IsBanned(userrec* user)
433 FOREACH_RESULT(I_OnCheckBan,OnCheckBan(user, this));
436 snprintf(mask, MAXBUF, "%s!%s@%s", user->nick, user->ident, user->GetIPString());
437 for (BanList::iterator i = this->bans.begin(); i != this->bans.end(); i++)
439 /* This allows CIDR ban matching
441 * Full masked host Full unmasked host IP with/without CIDR
443 if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match(mask, i->data, true)))
453 * remove a channel from a users record, and return the number of users left.
454 * Therefore, if this function returns 0 the caller should delete the chanrec.
456 long chanrec::PartUser(userrec *user, const char* reason)
459 return this->GetUserCounter();
461 UCListIter i = user->chans.find(this);
462 if (i != user->chans.end())
464 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, reason ? reason : ""));
465 this->WriteChannel(user, "PART %s%s%s", this->name, reason ? " :" : "", reason ? reason : "");
466 user->chans.erase(i);
467 this->RemoveAllPrefixes(user);
470 if (!this->DelUser(user)) /* if there are no users left on the channel... */
472 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
473 /* kill the record */
474 if (iter != ServerInstance->chanlist.end())
476 ServerInstance->Log(DEBUG,"del_channel: destroyed: %s", this->name);
477 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
478 ServerInstance->chanlist.erase(iter);
483 return this->GetUserCounter();
486 long chanrec::ServerKickUser(userrec* user, const char* reason, bool triggerevents)
488 if (!user || !reason)
489 return this->GetUserCounter();
493 if (!this->HasUser(user))
496 return this->GetUserCounter();
502 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL,user,this,reason));
505 UCListIter i = user->chans.find(this);
506 if (i != user->chans.end())
508 this->WriteChannelWithServ(ServerInstance->Config->ServerName, "KICK %s %s :%s", this->name, user->nick, reason);
509 user->chans.erase(i);
510 this->RemoveAllPrefixes(user);
513 if (!this->DelUser(user))
515 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
516 /* kill the record */
517 if (iter != ServerInstance->chanlist.end())
519 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
520 ServerInstance->chanlist.erase(iter);
525 return this->GetUserCounter();
528 long chanrec::KickUser(userrec *src, userrec *user, const char* reason)
530 if (!src || !user || !reason)
531 return this->GetUserCounter();
535 if (!this->HasUser(user))
537 src->WriteServ("441 %s %s %s :They are not on that channel",src->nick, user->nick, this->name);
538 return this->GetUserCounter();
540 if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
542 src->WriteServ("482 %s %s :Only a u-line may kick a u-line from a channel.",src->nick, this->name);
543 return this->GetUserCounter();
547 if (!ServerInstance->ULine(src->server))
550 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,this,reason));
552 return this->GetUserCounter();
554 /* Set to -1 by OnUserPreKick if explicit allow was set */
555 if (MOD_RESULT != -1)
557 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,this,AC_KICK));
558 if ((MOD_RESULT == ACR_DENY) && (!ServerInstance->ULine(src->server)))
559 return this->GetUserCounter();
561 if ((MOD_RESULT == ACR_DEFAULT) || (!ServerInstance->ULine(src->server)))
563 int them = this->GetStatus(src);
564 int us = this->GetStatus(user);
565 if ((them < STATUS_HOP) || (them < us))
567 src->WriteServ("482 %s %s :You must be a channel %soperator",src->nick, this->name, them == STATUS_HOP ? "" : "half-");
568 return this->GetUserCounter();
574 FOREACH_MOD(I_OnUserKick,OnUserKick(src,user,this,reason));
576 UCListIter i = user->chans.find(this);
577 if (i != user->chans.end())
579 /* zap it from the channel list of the user */
580 this->WriteChannel(src, "KICK %s %s :%s", this->name, user->nick, reason);
581 user->chans.erase(i);
582 this->RemoveAllPrefixes(user);
585 if (!this->DelUser(user))
586 /* if there are no users left on the channel */
588 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
590 /* kill the record */
591 if (iter != ServerInstance->chanlist.end())
593 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
594 ServerInstance->chanlist.erase(iter);
599 return this->GetUserCounter();
602 void chanrec::WriteChannel(userrec* user, char* text, ...)
604 char textbuffer[MAXBUF];
610 va_start(argsPtr, text);
611 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
614 this->WriteChannel(user, std::string(textbuffer));
617 void chanrec::WriteChannel(userrec* user, const std::string &text)
619 CUList *ulist = this->GetUsers();
624 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
626 if (IS_LOCAL(i->second))
627 user->WriteTo(i->second,text);
631 void chanrec::WriteChannelWithServ(const char* ServName, const char* text, ...)
633 char textbuffer[MAXBUF];
639 va_start(argsPtr, text);
640 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
643 this->WriteChannelWithServ(ServName, std::string(textbuffer));
646 void chanrec::WriteChannelWithServ(const char* ServName, const std::string &text)
648 CUList *ulist = this->GetUsers();
650 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
652 if (IS_LOCAL(i->second))
653 i->second->WriteServ(text);
657 /* write formatted text from a source user to all users on a channel except
658 * for the sender (for privmsg etc) */
659 void chanrec::WriteAllExceptSender(userrec* user, bool serversource, char status, char* text, ...)
661 char textbuffer[MAXBUF];
667 va_start(argsPtr, text);
668 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
671 this->WriteAllExceptSender(user, serversource, status, std::string(textbuffer));
674 void chanrec::WriteAllExcept(userrec* user, bool serversource, char status, CUList &except_list, char* text, ...)
676 char textbuffer[MAXBUF];
682 va_start(argsPtr, text);
683 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
686 this->WriteAllExcept(user, serversource, status, except_list, std::string(textbuffer));
689 void chanrec::WriteAllExcept(userrec* user, bool serversource, char status, CUList &except_list, const std::string &text)
696 ulist = this->GetOppedUsers();
699 ulist = this->GetHalfoppedUsers();
702 ulist = this->GetVoicedUsers();
705 ulist = this->GetUsers();
709 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
711 if ((IS_LOCAL(i->second)) && (except_list.find(i->second) == except_list.end()))
714 i->second->WriteServ(text);
716 i->second->WriteFrom(user,text);
721 void chanrec::WriteAllExceptSender(userrec* user, bool serversource, char status, const std::string& text)
724 except_list[user] = user;
725 this->WriteAllExcept(user, serversource, status, except_list, std::string(text));
729 * return a count of the users on a specific channel accounting for
730 * invisible users who won't increase the count. e.g. for /LIST
732 int chanrec::CountInvisible()
735 CUList *ulist= this->GetUsers();
736 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
738 if (!(i->second->modes[UM_INVISIBLE]))
745 char* chanrec::ChanModes(bool showkey)
747 static char scratch[MAXBUF];
748 static char sparam[MAXBUF];
749 char* offset = scratch;
750 std::string extparam = "";
755 /* This was still iterating up to 190, chanrec::modes is only 64 elements -- Om */
756 for(int n = 0; n < 64; n++)
765 extparam = (showkey ? this->key : "<key>");
768 extparam = ConvToStr(this->limit);
776 /* We know these have no parameters */
779 extparam = this->GetModeParameter(n + 65);
784 charlcat(sparam,' ',MAXBUF);
785 strlcat(sparam,extparam.c_str(),MAXBUF);
790 /* Null terminate scratch */
792 strlcat(scratch,sparam,MAXBUF);
796 /* compile a userlist of a channel into a string, each nick seperated by
797 * spaces and op, voice etc status shown as @ and +, and send it to 'user'
799 void chanrec::UserList(userrec *user)
808 FOREACH_RESULT(I_OnUserList,OnUserList(user, this));
809 ServerInstance->Log(DEBUG,"MOD_RESULT for UserList = %d",MOD_RESULT);
813 ServerInstance->Log(DEBUG,"Using builtin NAMES list generation");
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(std::string(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(std::string(list));
865 user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, this->name);
868 long chanrec::GetMaxBans()
870 for (std::map<std::string,int>::iterator n = ServerInstance->Config->maxbans.begin(); n != ServerInstance->Config->maxbans.end(); n++)
872 if (match(this->name,n->first.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)
887 static char pf[2] = {0, 0};
889 prefixlist::iterator n = prefixes.find(user);
890 if (n != prefixes.end())
892 if (n->second.size())
894 /* If the user has any prefixes, their highest prefix
895 * will always be at the head of the list, as the list is
896 * sorted in rank order highest first (see SetPrefix()
899 *pf = n->second.begin()->first;
908 const char* chanrec::GetAllPrefixChars(userrec* user)
910 static char prefix[MAXBUF];
914 prefixlist::iterator n = prefixes.find(user);
915 if (n != prefixes.end())
917 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
919 prefix[ctr++] = x->first;
928 unsigned int chanrec::GetPrefixValue(userrec* user)
930 prefixlist::iterator n = prefixes.find(user);
931 if (n != prefixes.end())
933 if (n->second.size())
934 return n->second.begin()->second;
939 int chanrec::GetStatusFlags(userrec *user)
941 UCListIter i = user->chans.find(this);
942 if (i != user->chans.end())
949 int chanrec::GetStatus(userrec *user)
951 if (ServerInstance->ULine(user->server))
954 UCListIter i = user->chans.find(this);
955 if (i != user->chans.end())
957 if ((i->second & UCMODE_OP) > 0)
961 if ((i->second & UCMODE_HOP) > 0)
965 if ((i->second & UCMODE_VOICE) > 0)
969 return STATUS_NORMAL;
971 return STATUS_NORMAL;
974 void chanrec::SetPrefix(userrec* user, char prefix, unsigned int prefix_value, bool adding)
976 prefixlist::iterator n = prefixes.find(user);
977 prefixtype pfx = std::make_pair(prefix,prefix_value);
980 if (n != prefixes.end())
982 if (std::find(n->second.begin(), n->second.end(), pfx) == n->second.end())
984 n->second.push_back(pfx);
985 /* We must keep prefixes in rank order, largest first.
986 * This is for two reasons, firstly because x-chat *ass-u-me's* this
987 * state, and secondly it turns out to be a benefit to us later.
988 * See above in GetPrefix().
990 std::sort(n->second.begin(), n->second.end(), ModeParser::PrefixComparison);
997 prefixes.insert(std::make_pair<userrec*,pfxcontainer>(user, one));
1002 if (n != prefixes.end())
1004 pfxcontainer::iterator x = std::find(n->second.begin(), n->second.end(), pfx);
1005 if (x != n->second.end())
1009 ServerInstance->Log(DEBUG,"Added prefix %c to %s for %s, prefixlist size is now %d", prefix, this->name, user->nick, prefixes.size());
1012 void chanrec::RemoveAllPrefixes(userrec* user)
1014 prefixlist::iterator n = prefixes.find(user);
1015 if (n != prefixes.end())
1017 ServerInstance->Log(DEBUG,"Removed prefixes from %s for %s, prefixlist size is now %d", this->name, user->nick, prefixes.size());