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 * ---------------------------------------------------
19 chanrec::chanrec(InspIRCd* Instance) : ServerInstance(Instance)
21 *name = *topic = *setby = *key = 0;
22 maxbans = created = topicset = limit = 0;
24 age = ServerInstance->Time(true);
27 void chanrec::SetMode(char mode,bool mode_on)
29 modes[mode-65] = mode_on;
31 this->SetModeParam(mode,"",false);
35 void chanrec::SetModeParam(char mode,const char* parameter,bool mode_on)
37 CustomModeList::iterator n = custom_mode_params.find(mode);
41 if (n == custom_mode_params.end())
42 custom_mode_params[mode] = strdup(parameter);
46 if (n != custom_mode_params.end())
49 custom_mode_params.erase(n);
54 bool chanrec::IsModeSet(char mode)
56 return modes[mode-65];
59 std::string chanrec::GetModeParameter(char mode)
66 return ConvToStr(this->limit);
68 CustomModeList::iterator n = custom_mode_params.find(mode);
69 if (n != custom_mode_params.end())
76 long chanrec::GetUserCounter()
78 return (this->internal_userlist.size());
81 void chanrec::AddUser(userrec* user)
83 internal_userlist[user] = user->nick;
86 unsigned long chanrec::DelUser(userrec* user)
88 CUListIter a = internal_userlist.find(user);
90 if (a != internal_userlist.end())
92 internal_userlist.erase(a);
93 /* And tidy any others... */
95 DelHalfoppedUser(user);
99 return internal_userlist.size();
102 bool chanrec::HasUser(userrec* user)
104 return (internal_userlist.find(user) != internal_userlist.end());
107 void chanrec::AddOppedUser(userrec* user)
109 internal_op_userlist[user] = user->nick;
112 void chanrec::DelOppedUser(userrec* user)
114 CUListIter a = internal_op_userlist.find(user);
115 if (a != internal_op_userlist.end())
117 internal_op_userlist.erase(a);
122 void chanrec::AddHalfoppedUser(userrec* user)
124 internal_halfop_userlist[user] = user->nick;
127 void chanrec::DelHalfoppedUser(userrec* user)
129 CUListIter a = internal_halfop_userlist.find(user);
131 if (a != internal_halfop_userlist.end())
133 internal_halfop_userlist.erase(a);
137 void chanrec::AddVoicedUser(userrec* user)
139 internal_voice_userlist[user] = user->nick;
142 void chanrec::DelVoicedUser(userrec* user)
144 CUListIter a = internal_voice_userlist.find(user);
146 if (a != internal_voice_userlist.end())
148 internal_voice_userlist.erase(a);
152 CUList* chanrec::GetUsers()
154 return &internal_userlist;
157 CUList* chanrec::GetOppedUsers()
159 return &internal_op_userlist;
162 CUList* chanrec::GetHalfoppedUsers()
164 return &internal_halfop_userlist;
167 CUList* chanrec::GetVoicedUsers()
169 return &internal_voice_userlist;
172 void chanrec::SetDefaultModes()
174 irc::spacesepstream list(ServerInstance->Config->DefaultModes);
176 std::string parameter;
178 list.GetToken(modeseq);
180 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
182 ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
185 if (mode->GetNumParams(true))
186 list.GetToken(parameter);
190 mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, this, parameter, true);
196 * add a channel to a user, creating the record for it if needed and linking
197 * it to the user record
199 chanrec* chanrec::JoinUser(InspIRCd* Instance, userrec *user, const char* cn, bool override, const char* key, time_t TS)
210 * We don't restrict the number of channels that remote users or users that are override-joining may be in.
211 * We restrict local users to MaxChans channels.
212 * We restrict local operators to OperMaxChans channels.
213 * This is a lot more logical than how it was formerly. -- w00t
215 if (IS_LOCAL(user) && !override)
217 if (user->GetMaxChans())
219 if (user->chans.size() >= user->GetMaxChans())
221 user->WriteServ("405 %s %s :You are on too many channels",user->nick, cn);
229 if (user->chans.size() >= Instance->Config->OperMaxChans)
231 user->WriteServ("405 %s %s :You are on too many channels",user->nick, cn);
237 if (user->chans.size() >= Instance->Config->MaxChans)
239 user->WriteServ("405 %s %s :You are on too many channels",user->nick, cn);
246 strlcpy(cname, cn, CHANMAX);
247 Ptr = Instance->FindChan(cname);
252 * Fix: desync bug was here, don't set @ on remote users - spanningtree handles their permissions. bug #358. -- w00t
257 Instance->Log(DEBUG,"*** BUG *** chanrec::JoinUser called for REMOTE user '%s' on channel '%s' but no TS given!", user->nick, cn);
264 if (IS_LOCAL(user) && override == false)
267 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,NULL,cname,privs));
272 /* create a new one */
273 Ptr = new chanrec(Instance);
274 (*(Instance->chanlist))[cname] = Ptr;
276 strlcpy(Ptr->name, cname,CHANMAX);
278 /* As spotted by jilles, dont bother to set this on remote users */
280 Ptr->SetDefaultModes();
282 Ptr->created = TS ? TS : Instance->Time();
283 Ptr->age = Ptr->created;
290 /* Already on the channel */
291 if (Ptr->HasUser(user))
295 * remote users are allowed us to bypass channel modes
296 * and bans (used by servers)
298 if (IS_LOCAL(user) && override == false)
301 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname,privs));
306 else if (MOD_RESULT == 0)
311 FOREACH_RESULT_I(Instance,I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
314 if ((!key) || strcmp(key,Ptr->key))
316 user->WriteServ("475 %s %s :Cannot join channel (Incorrect channel key)",user->nick, Ptr->name);
321 if (Ptr->IsModeSet('i'))
324 FOREACH_RESULT_I(Instance,I_OnCheckInvite,OnCheckInvite(user, Ptr));
327 if (!user->IsInvited(Ptr->name))
329 user->WriteServ("473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
333 user->RemoveInvite(Ptr->name);
338 FOREACH_RESULT_I(Instance,I_OnCheckLimit,OnCheckLimit(user, Ptr));
341 if (Ptr->GetUserCounter() >= Ptr->limit)
343 user->WriteServ("471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
348 if (Ptr->bans.size())
350 if (Ptr->IsBanned(user))
352 user->WriteServ("474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
360 return chanrec::ForceChan(Instance, Ptr, user, privs);
363 chanrec* chanrec::ForceChan(InspIRCd* Instance, chanrec* Ptr, userrec* user, const std::string &privs)
365 std::string nick = user->nick;
370 /* Just in case they have no permissions */
371 user->chans[Ptr] = 0;
373 for (std::string::const_iterator x = privs.begin(); x != privs.end(); x++)
375 const char status = *x;
376 ModeHandler* mh = Instance->Modes->FindPrefix(status);
379 Ptr->SetPrefix(user, status, mh->GetPrefixRank(), true);
380 /* Make sure that the mode handler knows this mode was now set */
381 mh->OnModeChange(Instance->FakeClient, Instance->FakeClient, Ptr, nick, true);
383 switch (mh->GetPrefix())
385 /* These logic ops are SAFE IN THIS CASE
386 * because if the entry doesnt exist,
387 * addressing operator[] creates it.
388 * If they do exist, it points to it.
389 * At all other times where we dont want
390 * to create an item if it doesnt exist, we
391 * must stick to ::find().
394 user->chans[Ptr] |= UCMODE_OP;
397 user->chans[Ptr] |= UCMODE_HOP;
400 user->chans[Ptr] |= UCMODE_VOICE;
406 FOREACH_MOD_I(Instance,I_OnUserJoin,OnUserJoin(user, Ptr, silent));
409 Ptr->WriteChannel(user,"JOIN :%s",Ptr->name);
411 /* Theyre not the first ones in here, make sure everyone else sees the modes we gave the user */
412 std::string ms = Instance->Modes->ModeString(user, Ptr);
413 if ((Ptr->GetUserCounter() > 1) && (ms.length()))
414 Ptr->WriteAllExceptSender(user, true, 0, "MODE %s +%s", Ptr->name, ms.c_str());
416 /* Major improvement by Brain - we dont need to be calculating all this pointlessly for remote users */
421 user->WriteServ("332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
422 user->WriteServ("333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
426 FOREACH_MOD_I(Instance,I_OnPostJoin,OnPostJoin(user, Ptr));
430 bool chanrec::IsBanned(userrec* user)
434 FOREACH_RESULT(I_OnCheckBan,OnCheckBan(user, this));
437 snprintf(mask, MAXBUF, "%s!%s@%s", user->nick, user->ident, user->GetIPString());
438 for (BanList::iterator i = this->bans.begin(); i != this->bans.end(); i++)
440 /* This allows CIDR ban matching
442 * Full masked host Full unmasked host IP with/without CIDR
444 if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match(mask, i->data, true)))
454 * remove a channel from a users record, and return the number of users left.
455 * Therefore, if this function returns 0 the caller should delete the chanrec.
457 long chanrec::PartUser(userrec *user, const char* reason)
462 return this->GetUserCounter();
464 UCListIter i = user->chans.find(this);
465 if (i != user->chans.end())
467 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, reason ? reason : "", silent));
470 this->WriteChannel(user, "PART %s%s%s", this->name, reason ? " :" : "", reason ? reason : "");
472 user->chans.erase(i);
473 this->RemoveAllPrefixes(user);
476 if (!this->DelUser(user)) /* if there are no users left on the channel... */
478 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
479 /* kill the record */
480 if (iter != ServerInstance->chanlist->end())
482 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
483 ServerInstance->chanlist->erase(iter);
488 return this->GetUserCounter();
491 long chanrec::ServerKickUser(userrec* user, const char* reason, bool triggerevents)
495 if (!user || !reason)
496 return this->GetUserCounter();
500 if (!this->HasUser(user))
503 return this->GetUserCounter();
509 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL, user, this, reason, silent));
512 UCListIter i = user->chans.find(this);
513 if (i != user->chans.end())
516 this->WriteChannelWithServ(ServerInstance->Config->ServerName, "KICK %s %s :%s", this->name, user->nick, reason);
518 user->chans.erase(i);
519 this->RemoveAllPrefixes(user);
522 if (!this->DelUser(user))
524 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
525 /* kill the record */
526 if (iter != ServerInstance->chanlist->end())
528 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
529 ServerInstance->chanlist->erase(iter);
534 return this->GetUserCounter();
537 long chanrec::KickUser(userrec *src, userrec *user, const char* reason)
541 if (!src || !user || !reason)
542 return this->GetUserCounter();
546 if (!this->HasUser(user))
548 src->WriteServ("441 %s %s %s :They are not on that channel",src->nick, user->nick, this->name);
549 return this->GetUserCounter();
551 if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
553 src->WriteServ("482 %s %s :Only a u-line may kick a u-line from a channel.",src->nick, this->name);
554 return this->GetUserCounter();
558 if (!ServerInstance->ULine(src->server))
561 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,this,reason));
563 return this->GetUserCounter();
565 /* Set to -1 by OnUserPreKick if explicit allow was set */
566 if (MOD_RESULT != -1)
568 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,this,AC_KICK));
569 if ((MOD_RESULT == ACR_DENY) && (!ServerInstance->ULine(src->server)))
570 return this->GetUserCounter();
572 if ((MOD_RESULT == ACR_DEFAULT) || (!ServerInstance->ULine(src->server)))
574 int them = this->GetStatus(src);
575 int us = this->GetStatus(user);
576 if ((them < STATUS_HOP) || (them < us))
578 src->WriteServ("482 %s %s :You must be a channel %soperator",src->nick, this->name, them == STATUS_HOP ? "" : "half-");
579 return this->GetUserCounter();
585 FOREACH_MOD(I_OnUserKick,OnUserKick(src, user, this, reason, silent));
587 UCListIter i = user->chans.find(this);
588 if (i != user->chans.end())
590 /* zap it from the channel list of the user */
592 this->WriteChannel(src, "KICK %s %s :%s", this->name, user->nick, reason);
594 user->chans.erase(i);
595 this->RemoveAllPrefixes(user);
598 if (!this->DelUser(user))
599 /* if there are no users left on the channel */
601 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
603 /* kill the record */
604 if (iter != ServerInstance->chanlist->end())
606 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
607 ServerInstance->chanlist->erase(iter);
612 return this->GetUserCounter();
615 void chanrec::WriteChannel(userrec* user, char* text, ...)
617 char textbuffer[MAXBUF];
623 va_start(argsPtr, text);
624 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
627 this->WriteChannel(user, std::string(textbuffer));
630 void chanrec::WriteChannel(userrec* user, const std::string &text)
632 CUList *ulist = this->GetUsers();
638 snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());
639 std::string out = tb;
641 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
643 if (IS_LOCAL(i->first))
644 i->first->Write(out);
648 void chanrec::WriteChannelWithServ(const char* ServName, const char* text, ...)
650 char textbuffer[MAXBUF];
656 va_start(argsPtr, text);
657 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
660 this->WriteChannelWithServ(ServName, std::string(textbuffer));
663 void chanrec::WriteChannelWithServ(const char* ServName, const std::string &text)
665 CUList *ulist = this->GetUsers();
668 snprintf(tb,MAXBUF,":%s %s",ServName ? ServName : ServerInstance->Config->ServerName, text.c_str());
669 std::string out = tb;
671 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
673 if (IS_LOCAL(i->first))
674 i->first->Write(out);
678 /* write formatted text from a source user to all users on a channel except
679 * for the sender (for privmsg etc) */
680 void chanrec::WriteAllExceptSender(userrec* user, bool serversource, char status, char* text, ...)
682 char textbuffer[MAXBUF];
688 va_start(argsPtr, text);
689 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
692 this->WriteAllExceptSender(user, serversource, status, std::string(textbuffer));
695 void chanrec::WriteAllExcept(userrec* user, bool serversource, char status, CUList &except_list, char* text, ...)
697 char textbuffer[MAXBUF];
703 va_start(argsPtr, text);
704 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
707 this->WriteAllExcept(user, serversource, status, except_list, std::string(textbuffer));
710 void chanrec::WriteAllExcept(userrec* user, bool serversource, char status, CUList &except_list, const std::string &text)
718 ulist = this->GetOppedUsers();
721 ulist = this->GetHalfoppedUsers();
724 ulist = this->GetVoicedUsers();
727 ulist = this->GetUsers();
731 snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());
732 std::string out = tb;
734 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
736 if ((IS_LOCAL(i->first)) && (except_list.find(i->first) == except_list.end()))
739 i->first->WriteServ(text);
741 i->first->Write(out);
746 void chanrec::WriteAllExceptSender(userrec* user, bool serversource, char status, const std::string& text)
749 except_list[user] = user->nick;
750 this->WriteAllExcept(user, serversource, status, except_list, std::string(text));
754 * return a count of the users on a specific channel accounting for
755 * invisible users who won't increase the count. e.g. for /LIST
757 int chanrec::CountInvisible()
760 CUList *ulist= this->GetUsers();
761 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
763 if (!(i->first->IsModeSet('i')))
770 char* chanrec::ChanModes(bool showkey)
772 static char scratch[MAXBUF];
773 static char sparam[MAXBUF];
774 char* offset = scratch;
775 std::string extparam;
780 /* This was still iterating up to 190, chanrec::modes is only 64 elements -- Om */
781 for(int n = 0; n < 64; n++)
790 extparam = (showkey ? this->key : "<key>");
793 extparam = ConvToStr(this->limit);
801 /* We know these have no parameters */
804 extparam = this->GetModeParameter(n + 65);
807 if (!extparam.empty())
809 charlcat(sparam,' ',MAXBUF);
810 strlcat(sparam,extparam.c_str(),MAXBUF);
815 /* Null terminate scratch */
817 strlcat(scratch,sparam,MAXBUF);
821 /* compile a userlist of a channel into a string, each nick seperated by
822 * spaces and op, voice etc status shown as @ and +, and send it to 'user'
824 void chanrec::UserList(userrec *user, CUList *ulist)
833 FOREACH_RESULT(I_OnUserList,OnUserList(user, this, ulist));
837 dlen = curlen = snprintf(list,MAXBUF,"353 %s %c %s :", user->nick, this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=', this->name);
840 char* ptr = list + dlen;
843 ulist = this->GetUsers();
845 /* Improvement by Brain - this doesnt change in value, so why was it inside
848 bool has_user = this->HasUser(user);
850 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
852 if ((!has_user) && (i->first->IsModeSet('i')))
855 * user is +i, and source not on the channel, does not show
861 if (i->first->Visibility && !i->first->Visibility->VisibleTo(user))
864 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", this->GetPrefixChar(i->first), i->second.c_str());
865 /* OnUserList can change this - reset it back to normal */
866 i->second = i->first->nick;
873 if (curlen > (480-NICKMAX))
875 /* list overflowed into multiple numerics */
876 user->WriteServ(std::string(list));
878 /* reset our lengths */
879 dlen = curlen = snprintf(list,MAXBUF,"353 %s %c %s :", user->nick, this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=', this->name);
887 /* if whats left in the list isnt empty, send it */
890 user->WriteServ(std::string(list));
893 user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, this->name);
896 long chanrec::GetMaxBans()
898 /* Return the cached value if there is one */
900 return this->maxbans;
902 /* If there isnt one, we have to do some O(n) hax to find it the first time. (ick) */
903 for (std::map<std::string,int>::iterator n = ServerInstance->Config->maxbans.begin(); n != ServerInstance->Config->maxbans.end(); n++)
905 if (match(this->name,n->first.c_str()))
907 this->maxbans = n->second;
912 /* Screw it, just return the default of 64 */
914 return this->maxbans;
917 void chanrec::ResetMaxBans()
922 /* returns the status character for a given user on a channel, e.g. @ for op,
923 * % for halfop etc. If the user has several modes set, the highest mode
924 * the user has must be returned.
926 const char* chanrec::GetPrefixChar(userrec *user)
928 static char pf[2] = {0, 0};
930 prefixlist::iterator n = prefixes.find(user);
931 if (n != prefixes.end())
933 if (n->second.size())
935 /* If the user has any prefixes, their highest prefix
936 * will always be at the head of the list, as the list is
937 * sorted in rank order highest first (see SetPrefix()
940 *pf = n->second.begin()->first;
949 const char* chanrec::GetAllPrefixChars(userrec* user)
951 static char prefix[MAXBUF];
955 prefixlist::iterator n = prefixes.find(user);
956 if (n != prefixes.end())
958 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
960 prefix[ctr++] = x->first;
969 unsigned int chanrec::GetPrefixValue(userrec* user)
971 prefixlist::iterator n = prefixes.find(user);
972 if (n != prefixes.end())
974 if (n->second.size())
975 return n->second.begin()->second;
980 int chanrec::GetStatusFlags(userrec *user)
982 UCListIter i = user->chans.find(this);
983 if (i != user->chans.end())
990 int chanrec::GetStatus(userrec *user)
992 if (ServerInstance->ULine(user->server))
995 UCListIter i = user->chans.find(this);
996 if (i != user->chans.end())
998 if ((i->second & UCMODE_OP) > 0)
1002 if ((i->second & UCMODE_HOP) > 0)
1006 if ((i->second & UCMODE_VOICE) > 0)
1008 return STATUS_VOICE;
1010 return STATUS_NORMAL;
1012 return STATUS_NORMAL;
1015 void chanrec::SetPrefix(userrec* user, char prefix, unsigned int prefix_value, bool adding)
1017 prefixlist::iterator n = prefixes.find(user);
1018 prefixtype pfx = std::make_pair(prefix,prefix_value);
1021 if (n != prefixes.end())
1023 if (std::find(n->second.begin(), n->second.end(), pfx) == n->second.end())
1025 n->second.push_back(pfx);
1026 /* We must keep prefixes in rank order, largest first.
1027 * This is for two reasons, firstly because x-chat *ass-u-me's* this
1028 * state, and secondly it turns out to be a benefit to us later.
1029 * See above in GetPrefix().
1031 std::sort(n->second.begin(), n->second.end(), ModeParser::PrefixComparison);
1038 prefixes.insert(std::make_pair<userrec*,pfxcontainer>(user, one));
1043 if (n != prefixes.end())
1045 pfxcontainer::iterator x = std::find(n->second.begin(), n->second.end(), pfx);
1046 if (x != n->second.end())
1052 void chanrec::RemoveAllPrefixes(userrec* user)
1054 prefixlist::iterator n = prefixes.find(user);
1055 if (n != prefixes.end())