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 * ---------------------------------------------------
16 #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);
47 /* TODO: We really need to do away with this in 1.2 */
49 this->limit = atoi(parameter);
51 strlcpy(this->key, parameter, sizeof(this->key));
55 if (n != custom_mode_params.end())
58 custom_mode_params.erase(n);
69 bool chanrec::IsModeSet(char mode)
71 return modes[mode-65];
74 std::string chanrec::GetModeParameter(char mode)
81 return ConvToStr(this->limit);
83 CustomModeList::iterator n = custom_mode_params.find(mode);
84 if (n != custom_mode_params.end())
91 long chanrec::GetUserCounter()
93 return (this->internal_userlist.size());
96 void chanrec::AddUser(userrec* user)
98 internal_userlist[user] = user->nick;
101 unsigned long chanrec::DelUser(userrec* user)
103 CUListIter a = internal_userlist.find(user);
105 if (a != internal_userlist.end())
107 internal_userlist.erase(a);
108 /* And tidy any others... */
110 DelHalfoppedUser(user);
114 return internal_userlist.size();
117 bool chanrec::HasUser(userrec* user)
119 return (internal_userlist.find(user) != internal_userlist.end());
122 void chanrec::AddOppedUser(userrec* user)
124 internal_op_userlist[user] = user->nick;
127 void chanrec::DelOppedUser(userrec* user)
129 CUListIter a = internal_op_userlist.find(user);
130 if (a != internal_op_userlist.end())
132 internal_op_userlist.erase(a);
137 void chanrec::AddHalfoppedUser(userrec* user)
139 internal_halfop_userlist[user] = user->nick;
142 void chanrec::DelHalfoppedUser(userrec* user)
144 CUListIter a = internal_halfop_userlist.find(user);
146 if (a != internal_halfop_userlist.end())
148 internal_halfop_userlist.erase(a);
152 void chanrec::AddVoicedUser(userrec* user)
154 internal_voice_userlist[user] = user->nick;
157 void chanrec::DelVoicedUser(userrec* user)
159 CUListIter a = internal_voice_userlist.find(user);
161 if (a != internal_voice_userlist.end())
163 internal_voice_userlist.erase(a);
167 CUList* chanrec::GetUsers()
169 return &internal_userlist;
172 CUList* chanrec::GetOppedUsers()
174 return &internal_op_userlist;
177 CUList* chanrec::GetHalfoppedUsers()
179 return &internal_halfop_userlist;
182 CUList* chanrec::GetVoicedUsers()
184 return &internal_voice_userlist;
187 void chanrec::SetDefaultModes()
189 irc::spacesepstream list(ServerInstance->Config->DefaultModes);
190 std::string modeseq = list.GetToken();
192 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
194 ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
197 this->SetMode(*n, true);
198 if (mode->GetNumParams(true))
199 this->SetModeParam(*n, list.GetToken().c_str(), true);
205 * add a channel to a user, creating the record for it if needed and linking
206 * it to the user record
208 chanrec* chanrec::JoinUser(InspIRCd* Instance, userrec *user, const char* cn, bool override, const char* key, time_t TS)
213 bool new_channel = false;
216 strlcpy(cname,cn,CHANMAX);
220 chanrec* Ptr = Instance->FindChan(cname);
224 if ((!IS_LOCAL(user)) && (!TS))
225 Instance->Log(DEBUG,"*** BUG *** chanrec::JoinUser called for REMOTE user '%s' on channel '%s' but no TS given!", user->nick, cn);
229 if (IS_LOCAL(user) && override == false)
232 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,NULL,cname,privs));
237 /* create a new one */
238 Ptr = new chanrec(Instance);
239 (*(Instance->chanlist))[cname] = Ptr;
241 strlcpy(Ptr->name, cname,CHANMAX);
243 /* As spotted by jilles, dont bother to set this on remote users */
245 Ptr->SetDefaultModes();
247 Ptr->created = TS ? TS : Instance->Time();
248 Ptr->age = Ptr->created;
256 /* Already on the channel */
257 if (Ptr->HasUser(user))
261 * remote users are allowed us to bypass channel modes
262 * and bans (used by servers)
264 if (IS_LOCAL(user) && override == false)
267 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname,privs));
272 else if (MOD_RESULT == 0)
277 FOREACH_RESULT_I(Instance,I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
280 if ((!key) || strcmp(key,Ptr->key))
282 user->WriteServ("475 %s %s :Cannot join channel (Incorrect channel key)",user->nick, Ptr->name);
287 if (Ptr->modes[CM_INVITEONLY])
290 FOREACH_RESULT_I(Instance,I_OnCheckInvite,OnCheckInvite(user, Ptr));
293 if (user->IsInvited(Ptr->name))
295 /* user was invited to channel */
296 /* there may be an optional channel NOTICE here */
300 user->WriteServ("473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
304 user->RemoveInvite(Ptr->name);
309 FOREACH_RESULT_I(Instance,I_OnCheckLimit,OnCheckLimit(user, Ptr));
312 if (Ptr->GetUserCounter() >= Ptr->limit)
314 user->WriteServ("471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
319 if (Ptr->bans.size())
321 if (Ptr->IsBanned(user))
323 user->WriteServ("474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
331 /* NOTE: If the user is an oper here, we can extend their user->chans by up to
332 * OperMaxchans. For remote users which are not bound by the channel limits,
333 * we can extend infinitely. Otherwise, nope, youre restricted to MaxChans.
335 if (!IS_LOCAL(user) || override == true)
337 return chanrec::ForceChan(Instance, Ptr, user, privs);
339 else if (IS_OPER(user))
341 /* Oper allows extension up to the OperMaxchans value */
342 if (user->chans.size() < Instance->Config->OperMaxChans)
344 return chanrec::ForceChan(Instance, Ptr, user, privs);
347 else if (user->chans.size() < Instance->Config->MaxChans)
349 return chanrec::ForceChan(Instance, Ptr, user, privs);
353 user->WriteServ("405 %s %s :You are on too many channels",user->nick, cname);
357 /* Things went seriously pear shaped, so take this away. bwahaha. */
358 chan_hash::iterator n = Instance->chanlist->find(cname);
359 if (n != Instance->chanlist->end())
363 Instance->chanlist->erase(n);
370 chanrec* chanrec::ForceChan(InspIRCd* Instance, chanrec* Ptr, userrec* user, const std::string &privs)
372 userrec* dummyuser = new userrec(Instance);
373 std::string nick = user->nick;
376 dummyuser->SetFd(FD_MAGIC_NUMBER);
379 /* Just in case they have no permissions */
380 user->chans[Ptr] = 0;
382 for (std::string::const_iterator x = privs.begin(); x != privs.end(); x++)
384 const char status = *x;
385 ModeHandler* mh = Instance->Modes->FindPrefix(status);
388 Ptr->SetPrefix(user, status, mh->GetPrefixRank(), true);
389 /* Make sure that the mode handler knows this mode was now set */
390 mh->OnModeChange(dummyuser, dummyuser, Ptr, nick, true);
392 switch (mh->GetPrefix())
394 /* These logic ops are SAFE IN THIS CASE
395 * because if the entry doesnt exist,
396 * addressing operator[] creates it.
397 * If they do exist, it points to it.
398 * At all other times where we dont want
399 * to create an item if it doesnt exist, we
400 * must stick to ::find().
403 user->chans[Ptr] |= UCMODE_OP;
406 user->chans[Ptr] |= UCMODE_HOP;
409 user->chans[Ptr] |= UCMODE_VOICE;
417 FOREACH_MOD_I(Instance,I_OnUserJoin,OnUserJoin(user, Ptr, silent));
420 Ptr->WriteChannel(user,"JOIN :%s",Ptr->name);
422 /* Theyre not the first ones in here, make sure everyone else sees the modes we gave the user */
423 std::string ms = Instance->Modes->ModeString(user, Ptr);
424 if ((Ptr->GetUserCounter() > 1) && (ms.length()))
425 Ptr->WriteAllExceptSender(user, true, 0, "MODE %s +%s", Ptr->name, ms.c_str());
427 /* Major improvement by Brain - we dont need to be calculating all this pointlessly for remote users */
432 user->WriteServ("332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
433 user->WriteServ("333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
437 FOREACH_MOD_I(Instance,I_OnPostJoin,OnPostJoin(user, Ptr));
441 bool chanrec::IsBanned(userrec* user)
445 FOREACH_RESULT(I_OnCheckBan,OnCheckBan(user, this));
448 snprintf(mask, MAXBUF, "%s!%s@%s", user->nick, user->ident, user->GetIPString());
449 for (BanList::iterator i = this->bans.begin(); i != this->bans.end(); i++)
451 /* This allows CIDR ban matching
453 * Full masked host Full unmasked host IP with/without CIDR
455 if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match(mask, i->data, true)))
465 * remove a channel from a users record, and return the number of users left.
466 * Therefore, if this function returns 0 the caller should delete the chanrec.
468 long chanrec::PartUser(userrec *user, const char* reason)
473 return this->GetUserCounter();
475 UCListIter i = user->chans.find(this);
476 if (i != user->chans.end())
478 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, reason ? reason : "", silent));
481 this->WriteChannel(user, "PART %s%s%s", this->name, reason ? " :" : "", reason ? reason : "");
483 user->chans.erase(i);
484 this->RemoveAllPrefixes(user);
487 if (!this->DelUser(user)) /* if there are no users left on the channel... */
489 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
490 /* kill the record */
491 if (iter != ServerInstance->chanlist->end())
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)
506 if (!user || !reason)
507 return this->GetUserCounter();
511 if (!this->HasUser(user))
514 return this->GetUserCounter();
520 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL, user, this, reason, silent));
523 UCListIter i = user->chans.find(this);
524 if (i != user->chans.end())
527 this->WriteChannelWithServ(ServerInstance->Config->ServerName, "KICK %s %s :%s", this->name, user->nick, reason);
529 user->chans.erase(i);
530 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)
552 if (!src || !user || !reason)
553 return this->GetUserCounter();
557 if (!this->HasUser(user))
559 src->WriteServ("441 %s %s %s :They are not on that channel",src->nick, user->nick, this->name);
560 return this->GetUserCounter();
562 if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
564 src->WriteServ("482 %s %s :Only a u-line may kick a u-line from a channel.",src->nick, this->name);
565 return this->GetUserCounter();
569 if (!ServerInstance->ULine(src->server))
572 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,this,reason));
574 return this->GetUserCounter();
576 /* Set to -1 by OnUserPreKick if explicit allow was set */
577 if (MOD_RESULT != -1)
579 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,this,AC_KICK));
580 if ((MOD_RESULT == ACR_DENY) && (!ServerInstance->ULine(src->server)))
581 return this->GetUserCounter();
583 if ((MOD_RESULT == ACR_DEFAULT) || (!ServerInstance->ULine(src->server)))
585 int them = this->GetStatus(src);
586 int us = this->GetStatus(user);
587 if ((them < STATUS_HOP) || (them < us))
589 src->WriteServ("482 %s %s :You must be a channel %soperator",src->nick, this->name, them == STATUS_HOP ? "" : "half-");
590 return this->GetUserCounter();
596 FOREACH_MOD(I_OnUserKick,OnUserKick(src, user, this, reason, silent));
598 UCListIter i = user->chans.find(this);
599 if (i != user->chans.end())
601 /* zap it from the channel list of the user */
603 this->WriteChannel(src, "KICK %s %s :%s", this->name, user->nick, reason);
605 user->chans.erase(i);
606 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();
649 snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());
650 std::string out = tb;
652 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
654 if (IS_LOCAL(i->first))
655 i->first->Write(out);
659 void chanrec::WriteChannelWithServ(const char* ServName, const char* text, ...)
661 char textbuffer[MAXBUF];
667 va_start(argsPtr, text);
668 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
671 this->WriteChannelWithServ(ServName, std::string(textbuffer));
674 void chanrec::WriteChannelWithServ(const char* ServName, const std::string &text)
676 CUList *ulist = this->GetUsers();
679 snprintf(tb,MAXBUF,":%s %s",ServName ? ServName : ServerInstance->Config->ServerName, text.c_str());
680 std::string out = tb;
682 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
684 if (IS_LOCAL(i->first))
685 i->first->Write(out);
689 /* write formatted text from a source user to all users on a channel except
690 * for the sender (for privmsg etc) */
691 void chanrec::WriteAllExceptSender(userrec* user, bool serversource, char status, char* text, ...)
693 char textbuffer[MAXBUF];
699 va_start(argsPtr, text);
700 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
703 this->WriteAllExceptSender(user, serversource, status, std::string(textbuffer));
706 void chanrec::WriteAllExcept(userrec* user, bool serversource, char status, CUList &except_list, char* text, ...)
708 char textbuffer[MAXBUF];
714 va_start(argsPtr, text);
715 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
718 this->WriteAllExcept(user, serversource, status, except_list, std::string(textbuffer));
721 void chanrec::WriteAllExcept(userrec* user, bool serversource, char status, CUList &except_list, const std::string &text)
729 ulist = this->GetOppedUsers();
732 ulist = this->GetHalfoppedUsers();
735 ulist = this->GetVoicedUsers();
738 ulist = this->GetUsers();
742 snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());
743 std::string out = tb;
745 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
747 if ((IS_LOCAL(i->first)) && (except_list.find(i->first) == except_list.end()))
750 i->first->WriteServ(text);
752 i->first->Write(out);
757 void chanrec::WriteAllExceptSender(userrec* user, bool serversource, char status, const std::string& text)
760 except_list[user] = user->nick;
761 this->WriteAllExcept(user, serversource, status, except_list, std::string(text));
765 * return a count of the users on a specific channel accounting for
766 * invisible users who won't increase the count. e.g. for /LIST
768 int chanrec::CountInvisible()
771 CUList *ulist= this->GetUsers();
772 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
774 if (!(i->first->IsModeSet('i')))
781 char* chanrec::ChanModes(bool showkey)
783 static char scratch[MAXBUF];
784 static char sparam[MAXBUF];
785 char* offset = scratch;
786 std::string extparam;
791 /* This was still iterating up to 190, chanrec::modes is only 64 elements -- Om */
792 for(int n = 0; n < 64; n++)
801 extparam = (showkey ? this->key : "<key>");
804 extparam = ConvToStr(this->limit);
812 /* We know these have no parameters */
815 extparam = this->GetModeParameter(n + 65);
820 charlcat(sparam,' ',MAXBUF);
821 strlcat(sparam,extparam.c_str(),MAXBUF);
826 /* Null terminate scratch */
828 strlcat(scratch,sparam,MAXBUF);
832 /* compile a userlist of a channel into a string, each nick seperated by
833 * spaces and op, voice etc status shown as @ and +, and send it to 'user'
835 void chanrec::UserList(userrec *user, CUList *ulist)
844 FOREACH_RESULT(I_OnUserList,OnUserList(user, this, ulist));
848 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
851 char* ptr = list + dlen;
854 ulist = this->GetUsers();
856 /* Improvement by Brain - this doesnt change in value, so why was it inside
859 bool has_user = this->HasUser(user);
861 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
863 if ((!has_user) && (i->first->modes[UM_INVISIBLE]))
866 * user is +i, and source not on the channel, does not show
872 if (i->first->Visibility && !i->first->Visibility->VisibleTo(user))
875 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", this->GetPrefixChar(i->first), i->second.c_str());
876 /* OnUserList can change this - reset it back to normal */
877 i->second = i->first->nick;
884 if (curlen > (480-NICKMAX))
886 /* list overflowed into multiple numerics */
887 user->WriteServ(std::string(list));
889 /* reset our lengths */
890 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
898 /* if whats left in the list isnt empty, send it */
901 user->WriteServ(std::string(list));
904 user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, this->name);
907 long chanrec::GetMaxBans()
909 /* Return the cached value if there is one */
911 return this->maxbans;
913 /* If there isnt one, we have to do some O(n) hax to find it the first time. (ick) */
914 for (std::map<std::string,int>::iterator n = ServerInstance->Config->maxbans.begin(); n != ServerInstance->Config->maxbans.end(); n++)
916 if (match(this->name,n->first.c_str()))
918 this->maxbans = n->second;
923 /* Screw it, just return the default of 64 */
925 return this->maxbans;
928 void chanrec::ResetMaxBans()
933 /* returns the status character for a given user on a channel, e.g. @ for op,
934 * % for halfop etc. If the user has several modes set, the highest mode
935 * the user has must be returned.
937 const char* chanrec::GetPrefixChar(userrec *user)
939 static char pf[2] = {0, 0};
941 prefixlist::iterator n = prefixes.find(user);
942 if (n != prefixes.end())
944 if (n->second.size())
946 /* If the user has any prefixes, their highest prefix
947 * will always be at the head of the list, as the list is
948 * sorted in rank order highest first (see SetPrefix()
951 *pf = n->second.begin()->first;
960 const char* chanrec::GetAllPrefixChars(userrec* user)
962 static char prefix[MAXBUF];
966 prefixlist::iterator n = prefixes.find(user);
967 if (n != prefixes.end())
969 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
971 prefix[ctr++] = x->first;
980 unsigned int chanrec::GetPrefixValue(userrec* user)
982 prefixlist::iterator n = prefixes.find(user);
983 if (n != prefixes.end())
985 if (n->second.size())
986 return n->second.begin()->second;
991 int chanrec::GetStatusFlags(userrec *user)
993 UCListIter i = user->chans.find(this);
994 if (i != user->chans.end())
1001 int chanrec::GetStatus(userrec *user)
1003 if (ServerInstance->ULine(user->server))
1006 UCListIter i = user->chans.find(this);
1007 if (i != user->chans.end())
1009 if ((i->second & UCMODE_OP) > 0)
1013 if ((i->second & UCMODE_HOP) > 0)
1017 if ((i->second & UCMODE_VOICE) > 0)
1019 return STATUS_VOICE;
1021 return STATUS_NORMAL;
1023 return STATUS_NORMAL;
1026 void chanrec::SetPrefix(userrec* user, char prefix, unsigned int prefix_value, bool adding)
1028 prefixlist::iterator n = prefixes.find(user);
1029 prefixtype pfx = std::make_pair(prefix,prefix_value);
1032 if (n != prefixes.end())
1034 if (std::find(n->second.begin(), n->second.end(), pfx) == n->second.end())
1036 n->second.push_back(pfx);
1037 /* We must keep prefixes in rank order, largest first.
1038 * This is for two reasons, firstly because x-chat *ass-u-me's* this
1039 * state, and secondly it turns out to be a benefit to us later.
1040 * See above in GetPrefix().
1042 std::sort(n->second.begin(), n->second.end(), ModeParser::PrefixComparison);
1049 prefixes.insert(std::make_pair<userrec*,pfxcontainer>(user, one));
1054 if (n != prefixes.end())
1056 pfxcontainer::iterator x = std::find(n->second.begin(), n->second.end(), pfx);
1057 if (x != n->second.end())
1063 void chanrec::RemoveAllPrefixes(userrec* user)
1065 prefixlist::iterator n = prefixes.find(user);
1066 if (n != prefixes.end())