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;
345 dummyuser->SetFd(FD_MAGIC_NUMBER);
348 /* Just in case they have no permissions */
349 user->chans[Ptr] = 0;
351 for (std::string::const_iterator x = privs.begin(); x != privs.end(); x++)
353 const char status = *x;
354 ModeHandler* mh = Instance->Modes->FindPrefix(status);
357 Ptr->SetPrefix(user, status, mh->GetPrefixRank(), true);
358 /* Make sure that the mode handler knows this mode was now set */
359 mh->OnModeChange(dummyuser, dummyuser, Ptr, nick, true);
361 switch (mh->GetPrefix())
363 /* These logic ops are SAFE IN THIS CASE
364 * because if the entry doesnt exist,
365 * addressing operator[] creates it.
366 * If they do exist, it points to it.
367 * At all other times where we dont want
368 * to create an item if it doesnt exist, we
369 * must stick to ::find().
372 user->chans[Ptr] |= UCMODE_OP;
375 user->chans[Ptr] |= UCMODE_HOP;
378 user->chans[Ptr] |= UCMODE_VOICE;
386 Ptr->WriteChannel(user,"JOIN :%s",Ptr->name);
388 /* Theyre not the first ones in here, make sure everyone else sees the modes we gave the user */
389 std::string ms = Instance->Modes->ModeString(user, Ptr);
390 if ((Ptr->GetUserCounter() > 1) && (ms.length()))
391 Ptr->WriteAllExceptSender(user, true, 0, "MODE %s +%s", Ptr->name, ms.c_str());
393 /* Major improvement by Brain - we dont need to be calculating all this pointlessly for remote users */
398 user->WriteServ("332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
399 user->WriteServ("333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
403 FOREACH_MOD_I(Instance,I_OnUserJoin,OnUserJoin(user,Ptr));
404 FOREACH_MOD_I(Instance,I_OnPostJoin,OnPostJoin(user,Ptr));
408 bool chanrec::IsBanned(userrec* user)
412 FOREACH_RESULT(I_OnCheckBan,OnCheckBan(user, this));
415 snprintf(mask, MAXBUF, "%s!%s@%s", user->nick, user->ident, user->GetIPString());
416 for (BanList::iterator i = this->bans.begin(); i != this->bans.end(); i++)
418 /* This allows CIDR ban matching
420 * Full masked host Full unmasked host IP with/without CIDR
422 if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match(mask, i->data, true)))
432 * remove a channel from a users record, and return the number of users left.
433 * Therefore, if this function returns 0 the caller should delete the chanrec.
435 long chanrec::PartUser(userrec *user, const char* reason)
438 return this->GetUserCounter();
440 UCListIter i = user->chans.find(this);
441 if (i != user->chans.end())
443 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, reason ? reason : ""));
444 this->WriteChannel(user, "PART %s%s%s", this->name, reason ? " :" : "", reason ? reason : "");
445 user->chans.erase(i);
446 this->RemoveAllPrefixes(user);
449 if (!this->DelUser(user)) /* if there are no users left on the channel... */
451 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
452 /* kill the record */
453 if (iter != ServerInstance->chanlist->end())
455 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
456 ServerInstance->chanlist->erase(iter);
461 return this->GetUserCounter();
464 long chanrec::ServerKickUser(userrec* user, const char* reason, bool triggerevents)
466 if (!user || !reason)
467 return this->GetUserCounter();
471 if (!this->HasUser(user))
474 return this->GetUserCounter();
480 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL,user,this,reason));
483 UCListIter i = user->chans.find(this);
484 if (i != user->chans.end())
486 this->WriteChannelWithServ(ServerInstance->Config->ServerName, "KICK %s %s :%s", this->name, user->nick, reason);
487 user->chans.erase(i);
488 this->RemoveAllPrefixes(user);
491 if (!this->DelUser(user))
493 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
494 /* kill the record */
495 if (iter != ServerInstance->chanlist->end())
497 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
498 ServerInstance->chanlist->erase(iter);
503 return this->GetUserCounter();
506 long chanrec::KickUser(userrec *src, userrec *user, const char* reason)
508 if (!src || !user || !reason)
509 return this->GetUserCounter();
513 if (!this->HasUser(user))
515 src->WriteServ("441 %s %s %s :They are not on that channel",src->nick, user->nick, this->name);
516 return this->GetUserCounter();
518 if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
520 src->WriteServ("482 %s %s :Only a u-line may kick a u-line from a channel.",src->nick, this->name);
521 return this->GetUserCounter();
525 if (!ServerInstance->ULine(src->server))
528 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,this,reason));
530 return this->GetUserCounter();
532 /* Set to -1 by OnUserPreKick if explicit allow was set */
533 if (MOD_RESULT != -1)
535 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,this,AC_KICK));
536 if ((MOD_RESULT == ACR_DENY) && (!ServerInstance->ULine(src->server)))
537 return this->GetUserCounter();
539 if ((MOD_RESULT == ACR_DEFAULT) || (!ServerInstance->ULine(src->server)))
541 int them = this->GetStatus(src);
542 int us = this->GetStatus(user);
543 if ((them < STATUS_HOP) || (them < us))
545 src->WriteServ("482 %s %s :You must be a channel %soperator",src->nick, this->name, them == STATUS_HOP ? "" : "half-");
546 return this->GetUserCounter();
552 FOREACH_MOD(I_OnUserKick,OnUserKick(src,user,this,reason));
554 UCListIter i = user->chans.find(this);
555 if (i != user->chans.end())
557 /* zap it from the channel list of the user */
558 this->WriteChannel(src, "KICK %s %s :%s", this->name, user->nick, reason);
559 user->chans.erase(i);
560 this->RemoveAllPrefixes(user);
563 if (!this->DelUser(user))
564 /* if there are no users left on the channel */
566 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
568 /* kill the record */
569 if (iter != ServerInstance->chanlist->end())
571 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
572 ServerInstance->chanlist->erase(iter);
577 return this->GetUserCounter();
580 void chanrec::WriteChannel(userrec* user, char* text, ...)
582 char textbuffer[MAXBUF];
588 va_start(argsPtr, text);
589 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
592 this->WriteChannel(user, std::string(textbuffer));
595 void chanrec::WriteChannel(userrec* user, const std::string &text)
597 CUList *ulist = this->GetUsers();
603 snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());
604 std::string out = tb;
606 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
608 if (IS_LOCAL(i->second))
609 i->second->Write(out);
613 void chanrec::WriteChannelWithServ(const char* ServName, const char* text, ...)
615 char textbuffer[MAXBUF];
621 va_start(argsPtr, text);
622 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
625 this->WriteChannelWithServ(ServName, std::string(textbuffer));
628 void chanrec::WriteChannelWithServ(const char* ServName, const std::string &text)
630 CUList *ulist = this->GetUsers();
633 snprintf(tb,MAXBUF,":%s %s",ServName ? ServName : ServerInstance->Config->ServerName, text.c_str());
634 std::string out = tb;
636 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
638 if (IS_LOCAL(i->second))
639 i->second->Write(out);
643 /* write formatted text from a source user to all users on a channel except
644 * for the sender (for privmsg etc) */
645 void chanrec::WriteAllExceptSender(userrec* user, bool serversource, char status, char* text, ...)
647 char textbuffer[MAXBUF];
653 va_start(argsPtr, text);
654 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
657 this->WriteAllExceptSender(user, serversource, status, std::string(textbuffer));
660 void chanrec::WriteAllExcept(userrec* user, bool serversource, char status, CUList &except_list, char* text, ...)
662 char textbuffer[MAXBUF];
668 va_start(argsPtr, text);
669 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
672 this->WriteAllExcept(user, serversource, status, except_list, std::string(textbuffer));
675 void chanrec::WriteAllExcept(userrec* user, bool serversource, char status, CUList &except_list, const std::string &text)
683 ulist = this->GetOppedUsers();
686 ulist = this->GetHalfoppedUsers();
689 ulist = this->GetVoicedUsers();
692 ulist = this->GetUsers();
696 snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());
697 std::string out = tb;
699 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
701 if ((IS_LOCAL(i->second)) && (except_list.find(i->second) == except_list.end()))
704 i->second->WriteServ(text);
706 i->second->Write(out);
711 void chanrec::WriteAllExceptSender(userrec* user, bool serversource, char status, const std::string& text)
714 except_list[user] = user;
715 this->WriteAllExcept(user, serversource, status, except_list, std::string(text));
719 * return a count of the users on a specific channel accounting for
720 * invisible users who won't increase the count. e.g. for /LIST
722 int chanrec::CountInvisible()
725 CUList *ulist= this->GetUsers();
726 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
728 if (!(i->second->modes[UM_INVISIBLE]))
735 char* chanrec::ChanModes(bool showkey)
737 static char scratch[MAXBUF];
738 static char sparam[MAXBUF];
739 char* offset = scratch;
740 std::string extparam;
745 /* This was still iterating up to 190, chanrec::modes is only 64 elements -- Om */
746 for(int n = 0; n < 64; n++)
755 extparam = (showkey ? this->key : "<key>");
758 extparam = ConvToStr(this->limit);
766 /* We know these have no parameters */
769 extparam = this->GetModeParameter(n + 65);
774 charlcat(sparam,' ',MAXBUF);
775 strlcat(sparam,extparam.c_str(),MAXBUF);
780 /* Null terminate scratch */
782 strlcat(scratch,sparam,MAXBUF);
786 /* compile a userlist of a channel into a string, each nick seperated by
787 * spaces and op, voice etc status shown as @ and +, and send it to 'user'
789 void chanrec::UserList(userrec *user)
798 FOREACH_RESULT(I_OnUserList,OnUserList(user, this));
802 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
805 char* ptr = list + dlen;
807 CUList *ulist= this->GetUsers();
809 /* Improvement by Brain - this doesnt change in value, so why was it inside
812 bool has_user = this->HasUser(user);
814 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
816 if ((!has_user) && (i->second->modes[UM_INVISIBLE]))
819 * user is +i, and source not on the channel, does not show
825 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", this->GetPrefixChar(i->second), i->second->nick);
832 if (curlen > (480-NICKMAX))
834 /* list overflowed into multiple numerics */
835 user->WriteServ(std::string(list));
837 /* reset our lengths */
838 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
846 /* if whats left in the list isnt empty, send it */
849 user->WriteServ(std::string(list));
852 user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, this->name);
855 long chanrec::GetMaxBans()
857 /* Return the cached value if there is one */
859 return this->maxbans;
861 /* If there isnt one, we have to do some O(n) hax to find it the first time. (ick) */
862 for (std::map<std::string,int>::iterator n = ServerInstance->Config->maxbans.begin(); n != ServerInstance->Config->maxbans.end(); n++)
864 if (match(this->name,n->first.c_str()))
866 this->maxbans = n->second;
871 /* Screw it, just return the default of 64 */
873 return this->maxbans;
876 void chanrec::ResetMaxBans()
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())
1011 void chanrec::RemoveAllPrefixes(userrec* user)
1013 prefixlist::iterator n = prefixes.find(user);
1014 if (n != prefixes.end())