1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
15 #include "configreader.h"
22 chanrec::chanrec(InspIRCd* Instance) : ServerInstance(Instance)
24 *name = *topic = *setby = *key = 0;
25 maxbans = created = topicset = limit = 0;
27 age = ServerInstance->Time(true);
30 void chanrec::SetMode(char mode,bool mode_on)
32 modes[mode-65] = mode_on;
34 this->SetModeParam(mode,"",false);
38 void chanrec::SetModeParam(char mode,const char* parameter,bool mode_on)
40 CustomModeList::iterator n = custom_mode_params.find(mode);
44 if (n == custom_mode_params.end())
45 custom_mode_params[mode] = strdup(parameter);
49 if (n != custom_mode_params.end())
52 custom_mode_params.erase(n);
57 bool chanrec::IsModeSet(char mode)
59 return modes[mode-65];
62 std::string chanrec::GetModeParameter(char mode)
69 return ConvToStr(this->limit);
71 CustomModeList::iterator n = custom_mode_params.find(mode);
72 if (n != custom_mode_params.end())
79 long chanrec::GetUserCounter()
81 return (this->internal_userlist.size());
84 void chanrec::AddUser(userrec* user)
86 internal_userlist[user] = user;
89 unsigned long chanrec::DelUser(userrec* user)
91 CUListIter a = internal_userlist.find(user);
93 if (a != internal_userlist.end())
95 internal_userlist.erase(a);
96 /* And tidy any others... */
98 DelHalfoppedUser(user);
102 return internal_userlist.size();
105 bool chanrec::HasUser(userrec* user)
107 return (internal_userlist.find(user) != internal_userlist.end());
110 void chanrec::AddOppedUser(userrec* user)
112 internal_op_userlist[user] = user;
115 void chanrec::DelOppedUser(userrec* user)
117 CUListIter a = internal_op_userlist.find(user);
118 if (a != internal_op_userlist.end())
120 internal_op_userlist.erase(a);
125 void chanrec::AddHalfoppedUser(userrec* user)
127 internal_halfop_userlist[user] = user;
130 void chanrec::DelHalfoppedUser(userrec* user)
132 CUListIter a = internal_halfop_userlist.find(user);
134 if (a != internal_halfop_userlist.end())
136 internal_halfop_userlist.erase(a);
140 void chanrec::AddVoicedUser(userrec* user)
142 internal_voice_userlist[user] = user;
145 void chanrec::DelVoicedUser(userrec* user)
147 CUListIter a = internal_voice_userlist.find(user);
149 if (a != internal_voice_userlist.end())
151 internal_voice_userlist.erase(a);
155 CUList* chanrec::GetUsers()
157 return &internal_userlist;
160 CUList* chanrec::GetOppedUsers()
162 return &internal_op_userlist;
165 CUList* chanrec::GetHalfoppedUsers()
167 return &internal_halfop_userlist;
170 CUList* chanrec::GetVoicedUsers()
172 return &internal_voice_userlist;
176 * add a channel to a user, creating the record for it if needed and linking
177 * it to the user record
179 chanrec* chanrec::JoinUser(InspIRCd* Instance, userrec *user, const char* cn, bool override, const char* key, time_t TS)
184 bool new_channel = false;
187 strlcpy(cname,cn,CHANMAX);
191 chanrec* Ptr = Instance->FindChan(cname);
195 if ((!IS_LOCAL(user)) && (!TS))
196 Instance->Log(DEBUG,"*** BUG *** chanrec::JoinUser called for REMOTE user '%s' on channel '%s' but no TS given!", user->nick, cn);
200 if (IS_LOCAL(user) && override == false)
203 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,NULL,cname,privs));
208 /* create a new one */
209 Ptr = new chanrec(Instance);
210 (*(Instance->chanlist))[cname] = Ptr;
212 strlcpy(Ptr->name, cname,CHANMAX);
214 /* As spotted by jilles, dont bother to set this on remote users */
216 Ptr->modes[CM_TOPICLOCK] = Ptr->modes[CM_NOEXTERNAL] = 1;
218 Ptr->created = TS ? TS : Instance->Time();
226 /* Already on the channel */
227 if (Ptr->HasUser(user))
231 * remote users are allowed us to bypass channel modes
232 * and bans (used by servers)
234 if (IS_LOCAL(user) && override == false)
237 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname,privs));
242 else if (MOD_RESULT == 0)
247 FOREACH_RESULT_I(Instance,I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
250 if ((!key) || strcmp(key,Ptr->key))
252 user->WriteServ("475 %s %s :Cannot join channel (Incorrect channel key)",user->nick, Ptr->name);
257 if (Ptr->modes[CM_INVITEONLY])
260 FOREACH_RESULT_I(Instance,I_OnCheckInvite,OnCheckInvite(user, Ptr));
263 if (user->IsInvited(Ptr->name))
265 /* user was invited to channel */
266 /* there may be an optional channel NOTICE here */
270 user->WriteServ("473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
274 user->RemoveInvite(Ptr->name);
279 FOREACH_RESULT_I(Instance,I_OnCheckLimit,OnCheckLimit(user, Ptr));
282 if (Ptr->GetUserCounter() >= Ptr->limit)
284 user->WriteServ("471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
289 if (Ptr->bans.size())
291 if (Ptr->IsBanned(user))
293 user->WriteServ("474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
301 /* NOTE: If the user is an oper here, we can extend their user->chans by up to
302 * OperMaxchans. For remote users which are not bound by the channel limits,
303 * we can extend infinitely. Otherwise, nope, youre restricted to MaxChans.
305 if (!IS_LOCAL(user) || override == true)
307 return chanrec::ForceChan(Instance, Ptr, user, privs);
309 else if (*user->oper)
311 /* Oper allows extension up to the OperMaxchans value */
312 if (user->chans.size() < Instance->Config->OperMaxChans)
314 return chanrec::ForceChan(Instance, Ptr, user, privs);
317 else if (user->chans.size() < Instance->Config->MaxChans)
319 return chanrec::ForceChan(Instance, Ptr, user, privs);
323 user->WriteServ("405 %s %s :You are on too many channels",user->nick, cname);
327 /* Things went seriously pear shaped, so take this away. bwahaha. */
328 chan_hash::iterator n = Instance->chanlist->find(cname);
329 if (n != Instance->chanlist->end())
333 Instance->chanlist->erase(n);
340 chanrec* chanrec::ForceChan(InspIRCd* Instance, chanrec* Ptr, userrec* user, const std::string &privs)
342 userrec* dummyuser = new userrec(Instance);
343 std::string nick = user->nick;
346 dummyuser->SetFd(FD_MAGIC_NUMBER);
349 /* Just in case they have no permissions */
350 user->chans[Ptr] = 0;
352 for (std::string::const_iterator x = privs.begin(); x != privs.end(); x++)
354 const char status = *x;
355 ModeHandler* mh = Instance->Modes->FindPrefix(status);
358 Ptr->SetPrefix(user, status, mh->GetPrefixRank(), true);
359 /* Make sure that the mode handler knows this mode was now set */
360 mh->OnModeChange(dummyuser, dummyuser, Ptr, nick, true);
362 switch (mh->GetPrefix())
364 /* These logic ops are SAFE IN THIS CASE
365 * because if the entry doesnt exist,
366 * addressing operator[] creates it.
367 * If they do exist, it points to it.
368 * At all other times where we dont want
369 * to create an item if it doesnt exist, we
370 * must stick to ::find().
373 user->chans[Ptr] |= UCMODE_OP;
376 user->chans[Ptr] |= UCMODE_HOP;
379 user->chans[Ptr] |= UCMODE_VOICE;
387 FOREACH_MOD_I(Instance,I_OnUserJoin,OnUserJoin(user, Ptr, silent));
390 Ptr->WriteChannel(user,"JOIN :%s",Ptr->name);
392 /* Theyre not the first ones in here, make sure everyone else sees the modes we gave the user */
393 std::string ms = Instance->Modes->ModeString(user, Ptr);
394 if ((Ptr->GetUserCounter() > 1) && (ms.length()))
395 Ptr->WriteAllExceptSender(user, true, 0, "MODE %s +%s", Ptr->name, ms.c_str());
397 /* Major improvement by Brain - we dont need to be calculating all this pointlessly for remote users */
402 user->WriteServ("332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
403 user->WriteServ("333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
407 FOREACH_MOD_I(Instance,I_OnPostJoin,OnPostJoin(user, Ptr));
411 bool chanrec::IsBanned(userrec* user)
415 FOREACH_RESULT(I_OnCheckBan,OnCheckBan(user, this));
418 snprintf(mask, MAXBUF, "%s!%s@%s", user->nick, user->ident, user->GetIPString());
419 for (BanList::iterator i = this->bans.begin(); i != this->bans.end(); i++)
421 /* This allows CIDR ban matching
423 * Full masked host Full unmasked host IP with/without CIDR
425 if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match(mask, i->data, true)))
435 * remove a channel from a users record, and return the number of users left.
436 * Therefore, if this function returns 0 the caller should delete the chanrec.
438 long chanrec::PartUser(userrec *user, const char* reason)
443 return this->GetUserCounter();
445 UCListIter i = user->chans.find(this);
446 if (i != user->chans.end())
448 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, reason ? reason : "", silent));
451 this->WriteChannel(user, "PART %s%s%s", this->name, reason ? " :" : "", reason ? reason : "");
453 user->chans.erase(i);
454 this->RemoveAllPrefixes(user);
457 if (!this->DelUser(user)) /* if there are no users left on the channel... */
459 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
460 /* kill the record */
461 if (iter != ServerInstance->chanlist->end())
463 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
464 ServerInstance->chanlist->erase(iter);
469 return this->GetUserCounter();
472 long chanrec::ServerKickUser(userrec* user, const char* reason, bool triggerevents)
476 if (!user || !reason)
477 return this->GetUserCounter();
481 if (!this->HasUser(user))
484 return this->GetUserCounter();
490 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL, user, this, reason, silent));
493 UCListIter i = user->chans.find(this);
494 if (i != user->chans.end())
497 this->WriteChannelWithServ(ServerInstance->Config->ServerName, "KICK %s %s :%s", this->name, user->nick, reason);
499 user->chans.erase(i);
500 this->RemoveAllPrefixes(user);
503 if (!this->DelUser(user))
505 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
506 /* kill the record */
507 if (iter != ServerInstance->chanlist->end())
509 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
510 ServerInstance->chanlist->erase(iter);
515 return this->GetUserCounter();
518 long chanrec::KickUser(userrec *src, userrec *user, const char* reason)
522 if (!src || !user || !reason)
523 return this->GetUserCounter();
527 if (!this->HasUser(user))
529 src->WriteServ("441 %s %s %s :They are not on that channel",src->nick, user->nick, this->name);
530 return this->GetUserCounter();
532 if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
534 src->WriteServ("482 %s %s :Only a u-line may kick a u-line from a channel.",src->nick, this->name);
535 return this->GetUserCounter();
539 if (!ServerInstance->ULine(src->server))
542 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,this,reason));
544 return this->GetUserCounter();
546 /* Set to -1 by OnUserPreKick if explicit allow was set */
547 if (MOD_RESULT != -1)
549 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,this,AC_KICK));
550 if ((MOD_RESULT == ACR_DENY) && (!ServerInstance->ULine(src->server)))
551 return this->GetUserCounter();
553 if ((MOD_RESULT == ACR_DEFAULT) || (!ServerInstance->ULine(src->server)))
555 int them = this->GetStatus(src);
556 int us = this->GetStatus(user);
557 if ((them < STATUS_HOP) || (them < us))
559 src->WriteServ("482 %s %s :You must be a channel %soperator",src->nick, this->name, them == STATUS_HOP ? "" : "half-");
560 return this->GetUserCounter();
566 FOREACH_MOD(I_OnUserKick,OnUserKick(src, user, this, reason, silent));
568 UCListIter i = user->chans.find(this);
569 if (i != user->chans.end())
571 /* zap it from the channel list of the user */
573 this->WriteChannel(src, "KICK %s %s :%s", this->name, user->nick, reason);
575 user->chans.erase(i);
576 this->RemoveAllPrefixes(user);
579 if (!this->DelUser(user))
580 /* if there are no users left on the channel */
582 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
584 /* kill the record */
585 if (iter != ServerInstance->chanlist->end())
587 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
588 ServerInstance->chanlist->erase(iter);
593 return this->GetUserCounter();
596 void chanrec::WriteChannel(userrec* user, char* text, ...)
598 char textbuffer[MAXBUF];
604 va_start(argsPtr, text);
605 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
608 this->WriteChannel(user, std::string(textbuffer));
611 void chanrec::WriteChannel(userrec* user, const std::string &text)
613 CUList *ulist = this->GetUsers();
619 snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());
620 std::string out = tb;
622 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
624 if (IS_LOCAL(i->second))
625 i->second->Write(out);
629 void chanrec::WriteChannelWithServ(const char* ServName, const char* text, ...)
631 char textbuffer[MAXBUF];
637 va_start(argsPtr, text);
638 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
641 this->WriteChannelWithServ(ServName, std::string(textbuffer));
644 void chanrec::WriteChannelWithServ(const char* ServName, const std::string &text)
646 CUList *ulist = this->GetUsers();
649 snprintf(tb,MAXBUF,":%s %s",ServName ? ServName : ServerInstance->Config->ServerName, text.c_str());
650 std::string out = tb;
652 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
654 if (IS_LOCAL(i->second))
655 i->second->Write(out);
659 /* write formatted text from a source user to all users on a channel except
660 * for the sender (for privmsg etc) */
661 void chanrec::WriteAllExceptSender(userrec* user, bool serversource, char status, char* text, ...)
663 char textbuffer[MAXBUF];
669 va_start(argsPtr, text);
670 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
673 this->WriteAllExceptSender(user, serversource, status, std::string(textbuffer));
676 void chanrec::WriteAllExcept(userrec* user, bool serversource, char status, CUList &except_list, char* text, ...)
678 char textbuffer[MAXBUF];
684 va_start(argsPtr, text);
685 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
688 this->WriteAllExcept(user, serversource, status, except_list, std::string(textbuffer));
691 void chanrec::WriteAllExcept(userrec* user, bool serversource, char status, CUList &except_list, const std::string &text)
699 ulist = this->GetOppedUsers();
702 ulist = this->GetHalfoppedUsers();
705 ulist = this->GetVoicedUsers();
708 ulist = this->GetUsers();
712 snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());
713 std::string out = tb;
715 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
717 if ((IS_LOCAL(i->second)) && (except_list.find(i->second) == except_list.end()))
720 i->second->WriteServ(text);
722 i->second->Write(out);
727 void chanrec::WriteAllExceptSender(userrec* user, bool serversource, char status, const std::string& text)
730 except_list[user] = user;
731 this->WriteAllExcept(user, serversource, status, except_list, std::string(text));
735 * return a count of the users on a specific channel accounting for
736 * invisible users who won't increase the count. e.g. for /LIST
738 int chanrec::CountInvisible()
741 CUList *ulist= this->GetUsers();
742 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
744 if (!(i->second->modes[UM_INVISIBLE]))
751 char* chanrec::ChanModes(bool showkey)
753 static char scratch[MAXBUF];
754 static char sparam[MAXBUF];
755 char* offset = scratch;
756 std::string extparam;
761 /* This was still iterating up to 190, chanrec::modes is only 64 elements -- Om */
762 for(int n = 0; n < 64; n++)
771 extparam = (showkey ? this->key : "<key>");
774 extparam = ConvToStr(this->limit);
782 /* We know these have no parameters */
785 extparam = this->GetModeParameter(n + 65);
790 charlcat(sparam,' ',MAXBUF);
791 strlcat(sparam,extparam.c_str(),MAXBUF);
796 /* Null terminate scratch */
798 strlcat(scratch,sparam,MAXBUF);
802 /* compile a userlist of a channel into a string, each nick seperated by
803 * spaces and op, voice etc status shown as @ and +, and send it to 'user'
805 void chanrec::UserList(userrec *user, CUList *ulist)
814 FOREACH_RESULT(I_OnUserList,OnUserList(user, this, ulist));
818 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
821 char* ptr = list + dlen;
824 ulist = this->GetUsers();
826 /* Improvement by Brain - this doesnt change in value, so why was it inside
829 bool has_user = this->HasUser(user);
831 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
833 if ((!has_user) && (i->second->modes[UM_INVISIBLE]))
836 * user is +i, and source not on the channel, does not show
842 if (i->second->Visibility && !i->second->Visibility->VisibleTo(user))
845 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", this->GetPrefixChar(i->second), i->second->nick);
852 if (curlen > (480-NICKMAX))
854 /* list overflowed into multiple numerics */
855 user->WriteServ(std::string(list));
857 /* reset our lengths */
858 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
866 /* if whats left in the list isnt empty, send it */
869 user->WriteServ(std::string(list));
872 user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, this->name);
875 long chanrec::GetMaxBans()
877 /* Return the cached value if there is one */
879 return this->maxbans;
881 /* If there isnt one, we have to do some O(n) hax to find it the first time. (ick) */
882 for (std::map<std::string,int>::iterator n = ServerInstance->Config->maxbans.begin(); n != ServerInstance->Config->maxbans.end(); n++)
884 if (match(this->name,n->first.c_str()))
886 this->maxbans = n->second;
891 /* Screw it, just return the default of 64 */
893 return this->maxbans;
896 void chanrec::ResetMaxBans()
901 /* returns the status character for a given user on a channel, e.g. @ for op,
902 * % for halfop etc. If the user has several modes set, the highest mode
903 * the user has must be returned.
905 const char* chanrec::GetPrefixChar(userrec *user)
907 static char pf[2] = {0, 0};
909 prefixlist::iterator n = prefixes.find(user);
910 if (n != prefixes.end())
912 if (n->second.size())
914 /* If the user has any prefixes, their highest prefix
915 * will always be at the head of the list, as the list is
916 * sorted in rank order highest first (see SetPrefix()
919 *pf = n->second.begin()->first;
928 const char* chanrec::GetAllPrefixChars(userrec* user)
930 static char prefix[MAXBUF];
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++)
939 prefix[ctr++] = x->first;
948 unsigned int chanrec::GetPrefixValue(userrec* user)
950 prefixlist::iterator n = prefixes.find(user);
951 if (n != prefixes.end())
953 if (n->second.size())
954 return n->second.begin()->second;
959 int chanrec::GetStatusFlags(userrec *user)
961 UCListIter i = user->chans.find(this);
962 if (i != user->chans.end())
969 int chanrec::GetStatus(userrec *user)
971 if (ServerInstance->ULine(user->server))
974 UCListIter i = user->chans.find(this);
975 if (i != user->chans.end())
977 if ((i->second & UCMODE_OP) > 0)
981 if ((i->second & UCMODE_HOP) > 0)
985 if ((i->second & UCMODE_VOICE) > 0)
989 return STATUS_NORMAL;
991 return STATUS_NORMAL;
994 void chanrec::SetPrefix(userrec* user, char prefix, unsigned int prefix_value, bool adding)
996 prefixlist::iterator n = prefixes.find(user);
997 prefixtype pfx = std::make_pair(prefix,prefix_value);
1000 if (n != prefixes.end())
1002 if (std::find(n->second.begin(), n->second.end(), pfx) == n->second.end())
1004 n->second.push_back(pfx);
1005 /* We must keep prefixes in rank order, largest first.
1006 * This is for two reasons, firstly because x-chat *ass-u-me's* this
1007 * state, and secondly it turns out to be a benefit to us later.
1008 * See above in GetPrefix().
1010 std::sort(n->second.begin(), n->second.end(), ModeParser::PrefixComparison);
1017 prefixes.insert(std::make_pair<userrec*,pfxcontainer>(user, one));
1022 if (n != prefixes.end())
1024 pfxcontainer::iterator x = std::find(n->second.begin(), n->second.end(), pfx);
1025 if (x != n->second.end())
1031 void chanrec::RemoveAllPrefixes(userrec* user)
1033 prefixlist::iterator n = prefixes.find(user);
1034 if (n != prefixes.end())