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();
219 Ptr->age = Ptr->created;
227 /* Already on the channel */
228 if (Ptr->HasUser(user))
232 * remote users are allowed us to bypass channel modes
233 * and bans (used by servers)
235 if (IS_LOCAL(user) && override == false)
238 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname,privs));
243 else if (MOD_RESULT == 0)
248 FOREACH_RESULT_I(Instance,I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
251 if ((!key) || strcmp(key,Ptr->key))
253 user->WriteServ("475 %s %s :Cannot join channel (Incorrect channel key)",user->nick, Ptr->name);
258 if (Ptr->modes[CM_INVITEONLY])
261 FOREACH_RESULT_I(Instance,I_OnCheckInvite,OnCheckInvite(user, Ptr));
264 if (user->IsInvited(Ptr->name))
266 /* user was invited to channel */
267 /* there may be an optional channel NOTICE here */
271 user->WriteServ("473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
275 user->RemoveInvite(Ptr->name);
280 FOREACH_RESULT_I(Instance,I_OnCheckLimit,OnCheckLimit(user, Ptr));
283 if (Ptr->GetUserCounter() >= Ptr->limit)
285 user->WriteServ("471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
290 if (Ptr->bans.size())
292 if (Ptr->IsBanned(user))
294 user->WriteServ("474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
302 /* NOTE: If the user is an oper here, we can extend their user->chans by up to
303 * OperMaxchans. For remote users which are not bound by the channel limits,
304 * we can extend infinitely. Otherwise, nope, youre restricted to MaxChans.
306 if (!IS_LOCAL(user) || override == true)
308 return chanrec::ForceChan(Instance, Ptr, user, privs);
310 else if (*user->oper)
312 /* Oper allows extension up to the OperMaxchans value */
313 if (user->chans.size() < Instance->Config->OperMaxChans)
315 return chanrec::ForceChan(Instance, Ptr, user, privs);
318 else if (user->chans.size() < Instance->Config->MaxChans)
320 return chanrec::ForceChan(Instance, Ptr, user, privs);
324 user->WriteServ("405 %s %s :You are on too many channels",user->nick, cname);
328 /* Things went seriously pear shaped, so take this away. bwahaha. */
329 chan_hash::iterator n = Instance->chanlist->find(cname);
330 if (n != Instance->chanlist->end())
334 Instance->chanlist->erase(n);
341 chanrec* chanrec::ForceChan(InspIRCd* Instance, chanrec* Ptr, userrec* user, const std::string &privs)
343 userrec* dummyuser = new userrec(Instance);
344 std::string nick = user->nick;
347 dummyuser->SetFd(FD_MAGIC_NUMBER);
350 /* Just in case they have no permissions */
351 user->chans[Ptr] = 0;
353 for (std::string::const_iterator x = privs.begin(); x != privs.end(); x++)
355 const char status = *x;
356 ModeHandler* mh = Instance->Modes->FindPrefix(status);
359 Ptr->SetPrefix(user, status, mh->GetPrefixRank(), true);
360 /* Make sure that the mode handler knows this mode was now set */
361 mh->OnModeChange(dummyuser, dummyuser, Ptr, nick, true);
363 switch (mh->GetPrefix())
365 /* These logic ops are SAFE IN THIS CASE
366 * because if the entry doesnt exist,
367 * addressing operator[] creates it.
368 * If they do exist, it points to it.
369 * At all other times where we dont want
370 * to create an item if it doesnt exist, we
371 * must stick to ::find().
374 user->chans[Ptr] |= UCMODE_OP;
377 user->chans[Ptr] |= UCMODE_HOP;
380 user->chans[Ptr] |= UCMODE_VOICE;
388 FOREACH_MOD_I(Instance,I_OnUserJoin,OnUserJoin(user, Ptr, silent));
391 Ptr->WriteChannel(user,"JOIN :%s",Ptr->name);
393 /* Theyre not the first ones in here, make sure everyone else sees the modes we gave the user */
394 std::string ms = Instance->Modes->ModeString(user, Ptr);
395 if ((Ptr->GetUserCounter() > 1) && (ms.length()))
396 Ptr->WriteAllExceptSender(user, true, 0, "MODE %s +%s", Ptr->name, ms.c_str());
398 /* Major improvement by Brain - we dont need to be calculating all this pointlessly for remote users */
403 user->WriteServ("332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
404 user->WriteServ("333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
408 FOREACH_MOD_I(Instance,I_OnPostJoin,OnPostJoin(user, Ptr));
412 bool chanrec::IsBanned(userrec* user)
416 FOREACH_RESULT(I_OnCheckBan,OnCheckBan(user, this));
419 snprintf(mask, MAXBUF, "%s!%s@%s", user->nick, user->ident, user->GetIPString());
420 for (BanList::iterator i = this->bans.begin(); i != this->bans.end(); i++)
422 /* This allows CIDR ban matching
424 * Full masked host Full unmasked host IP with/without CIDR
426 if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match(mask, i->data, true)))
436 * remove a channel from a users record, and return the number of users left.
437 * Therefore, if this function returns 0 the caller should delete the chanrec.
439 long chanrec::PartUser(userrec *user, const char* reason)
444 return this->GetUserCounter();
446 UCListIter i = user->chans.find(this);
447 if (i != user->chans.end())
449 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, reason ? reason : "", silent));
452 this->WriteChannel(user, "PART %s%s%s", this->name, reason ? " :" : "", reason ? reason : "");
454 user->chans.erase(i);
455 this->RemoveAllPrefixes(user);
458 if (!this->DelUser(user)) /* if there are no users left on the channel... */
460 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
461 /* kill the record */
462 if (iter != ServerInstance->chanlist->end())
464 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
465 ServerInstance->chanlist->erase(iter);
470 return this->GetUserCounter();
473 long chanrec::ServerKickUser(userrec* user, const char* reason, bool triggerevents)
477 if (!user || !reason)
478 return this->GetUserCounter();
482 if (!this->HasUser(user))
485 return this->GetUserCounter();
491 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL, user, this, reason, silent));
494 UCListIter i = user->chans.find(this);
495 if (i != user->chans.end())
498 this->WriteChannelWithServ(ServerInstance->Config->ServerName, "KICK %s %s :%s", this->name, user->nick, reason);
500 user->chans.erase(i);
501 this->RemoveAllPrefixes(user);
504 if (!this->DelUser(user))
506 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
507 /* kill the record */
508 if (iter != ServerInstance->chanlist->end())
510 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
511 ServerInstance->chanlist->erase(iter);
516 return this->GetUserCounter();
519 long chanrec::KickUser(userrec *src, userrec *user, const char* reason)
523 if (!src || !user || !reason)
524 return this->GetUserCounter();
528 if (!this->HasUser(user))
530 src->WriteServ("441 %s %s %s :They are not on that channel",src->nick, user->nick, this->name);
531 return this->GetUserCounter();
533 if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
535 src->WriteServ("482 %s %s :Only a u-line may kick a u-line from a channel.",src->nick, this->name);
536 return this->GetUserCounter();
540 if (!ServerInstance->ULine(src->server))
543 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,this,reason));
545 return this->GetUserCounter();
547 /* Set to -1 by OnUserPreKick if explicit allow was set */
548 if (MOD_RESULT != -1)
550 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,this,AC_KICK));
551 if ((MOD_RESULT == ACR_DENY) && (!ServerInstance->ULine(src->server)))
552 return this->GetUserCounter();
554 if ((MOD_RESULT == ACR_DEFAULT) || (!ServerInstance->ULine(src->server)))
556 int them = this->GetStatus(src);
557 int us = this->GetStatus(user);
558 if ((them < STATUS_HOP) || (them < us))
560 src->WriteServ("482 %s %s :You must be a channel %soperator",src->nick, this->name, them == STATUS_HOP ? "" : "half-");
561 return this->GetUserCounter();
567 FOREACH_MOD(I_OnUserKick,OnUserKick(src, user, this, reason, silent));
569 UCListIter i = user->chans.find(this);
570 if (i != user->chans.end())
572 /* zap it from the channel list of the user */
574 this->WriteChannel(src, "KICK %s %s :%s", this->name, user->nick, reason);
576 user->chans.erase(i);
577 this->RemoveAllPrefixes(user);
580 if (!this->DelUser(user))
581 /* if there are no users left on the channel */
583 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
585 /* kill the record */
586 if (iter != ServerInstance->chanlist->end())
588 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
589 ServerInstance->chanlist->erase(iter);
594 return this->GetUserCounter();
597 void chanrec::WriteChannel(userrec* user, char* text, ...)
599 char textbuffer[MAXBUF];
605 va_start(argsPtr, text);
606 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
609 this->WriteChannel(user, std::string(textbuffer));
612 void chanrec::WriteChannel(userrec* user, const std::string &text)
614 CUList *ulist = this->GetUsers();
620 snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());
621 std::string out = tb;
623 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
625 if (IS_LOCAL(i->second))
626 i->second->Write(out);
630 void chanrec::WriteChannelWithServ(const char* ServName, const char* text, ...)
632 char textbuffer[MAXBUF];
638 va_start(argsPtr, text);
639 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
642 this->WriteChannelWithServ(ServName, std::string(textbuffer));
645 void chanrec::WriteChannelWithServ(const char* ServName, const std::string &text)
647 CUList *ulist = this->GetUsers();
650 snprintf(tb,MAXBUF,":%s %s",ServName ? ServName : ServerInstance->Config->ServerName, text.c_str());
651 std::string out = tb;
653 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
655 if (IS_LOCAL(i->second))
656 i->second->Write(out);
660 /* write formatted text from a source user to all users on a channel except
661 * for the sender (for privmsg etc) */
662 void chanrec::WriteAllExceptSender(userrec* user, bool serversource, char status, char* text, ...)
664 char textbuffer[MAXBUF];
670 va_start(argsPtr, text);
671 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
674 this->WriteAllExceptSender(user, serversource, status, std::string(textbuffer));
677 void chanrec::WriteAllExcept(userrec* user, bool serversource, char status, CUList &except_list, char* text, ...)
679 char textbuffer[MAXBUF];
685 va_start(argsPtr, text);
686 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
689 this->WriteAllExcept(user, serversource, status, except_list, std::string(textbuffer));
692 void chanrec::WriteAllExcept(userrec* user, bool serversource, char status, CUList &except_list, const std::string &text)
700 ulist = this->GetOppedUsers();
703 ulist = this->GetHalfoppedUsers();
706 ulist = this->GetVoicedUsers();
709 ulist = this->GetUsers();
713 snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());
714 std::string out = tb;
716 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
718 if ((IS_LOCAL(i->second)) && (except_list.find(i->second) == except_list.end()))
721 i->second->WriteServ(text);
723 i->second->Write(out);
728 void chanrec::WriteAllExceptSender(userrec* user, bool serversource, char status, const std::string& text)
731 except_list[user] = user;
732 this->WriteAllExcept(user, serversource, status, except_list, std::string(text));
736 * return a count of the users on a specific channel accounting for
737 * invisible users who won't increase the count. e.g. for /LIST
739 int chanrec::CountInvisible()
742 CUList *ulist= this->GetUsers();
743 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
745 if (!(i->second->modes[UM_INVISIBLE]))
752 char* chanrec::ChanModes(bool showkey)
754 static char scratch[MAXBUF];
755 static char sparam[MAXBUF];
756 char* offset = scratch;
757 std::string extparam;
762 /* This was still iterating up to 190, chanrec::modes is only 64 elements -- Om */
763 for(int n = 0; n < 64; n++)
772 extparam = (showkey ? this->key : "<key>");
775 extparam = ConvToStr(this->limit);
783 /* We know these have no parameters */
786 extparam = this->GetModeParameter(n + 65);
791 charlcat(sparam,' ',MAXBUF);
792 strlcat(sparam,extparam.c_str(),MAXBUF);
797 /* Null terminate scratch */
799 strlcat(scratch,sparam,MAXBUF);
803 /* compile a userlist of a channel into a string, each nick seperated by
804 * spaces and op, voice etc status shown as @ and +, and send it to 'user'
806 void chanrec::UserList(userrec *user, CUList *ulist)
815 FOREACH_RESULT(I_OnUserList,OnUserList(user, this, ulist));
819 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
822 char* ptr = list + dlen;
825 ulist = this->GetUsers();
827 /* Improvement by Brain - this doesnt change in value, so why was it inside
830 bool has_user = this->HasUser(user);
832 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
834 if ((!has_user) && (i->second->modes[UM_INVISIBLE]))
837 * user is +i, and source not on the channel, does not show
843 if (i->second->Visibility && !i->second->Visibility->VisibleTo(user))
846 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", this->GetPrefixChar(i->second), i->second->nick);
853 if (curlen > (480-NICKMAX))
855 /* list overflowed into multiple numerics */
856 user->WriteServ(std::string(list));
858 /* reset our lengths */
859 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
867 /* if whats left in the list isnt empty, send it */
870 user->WriteServ(std::string(list));
873 user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, this->name);
876 long chanrec::GetMaxBans()
878 /* Return the cached value if there is one */
880 return this->maxbans;
882 /* If there isnt one, we have to do some O(n) hax to find it the first time. (ick) */
883 for (std::map<std::string,int>::iterator n = ServerInstance->Config->maxbans.begin(); n != ServerInstance->Config->maxbans.end(); n++)
885 if (match(this->name,n->first.c_str()))
887 this->maxbans = n->second;
892 /* Screw it, just return the default of 64 */
894 return this->maxbans;
897 void chanrec::ResetMaxBans()
902 /* returns the status character for a given user on a channel, e.g. @ for op,
903 * % for halfop etc. If the user has several modes set, the highest mode
904 * the user has must be returned.
906 const char* chanrec::GetPrefixChar(userrec *user)
908 static char pf[2] = {0, 0};
910 prefixlist::iterator n = prefixes.find(user);
911 if (n != prefixes.end())
913 if (n->second.size())
915 /* If the user has any prefixes, their highest prefix
916 * will always be at the head of the list, as the list is
917 * sorted in rank order highest first (see SetPrefix()
920 *pf = n->second.begin()->first;
929 const char* chanrec::GetAllPrefixChars(userrec* user)
931 static char prefix[MAXBUF];
935 prefixlist::iterator n = prefixes.find(user);
936 if (n != prefixes.end())
938 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
940 prefix[ctr++] = x->first;
949 unsigned int chanrec::GetPrefixValue(userrec* user)
951 prefixlist::iterator n = prefixes.find(user);
952 if (n != prefixes.end())
954 if (n->second.size())
955 return n->second.begin()->second;
960 int chanrec::GetStatusFlags(userrec *user)
962 UCListIter i = user->chans.find(this);
963 if (i != user->chans.end())
970 int chanrec::GetStatus(userrec *user)
972 if (ServerInstance->ULine(user->server))
975 UCListIter i = user->chans.find(this);
976 if (i != user->chans.end())
978 if ((i->second & UCMODE_OP) > 0)
982 if ((i->second & UCMODE_HOP) > 0)
986 if ((i->second & UCMODE_VOICE) > 0)
990 return STATUS_NORMAL;
992 return STATUS_NORMAL;
995 void chanrec::SetPrefix(userrec* user, char prefix, unsigned int prefix_value, bool adding)
997 prefixlist::iterator n = prefixes.find(user);
998 prefixtype pfx = std::make_pair(prefix,prefix_value);
1001 if (n != prefixes.end())
1003 if (std::find(n->second.begin(), n->second.end(), pfx) == n->second.end())
1005 n->second.push_back(pfx);
1006 /* We must keep prefixes in rank order, largest first.
1007 * This is for two reasons, firstly because x-chat *ass-u-me's* this
1008 * state, and secondly it turns out to be a benefit to us later.
1009 * See above in GetPrefix().
1011 std::sort(n->second.begin(), n->second.end(), ModeParser::PrefixComparison);
1018 prefixes.insert(std::make_pair<userrec*,pfxcontainer>(user, one));
1023 if (n != prefixes.end())
1025 pfxcontainer::iterator x = std::find(n->second.begin(), n->second.end(), pfx);
1026 if (x != n->second.end())
1032 void chanrec::RemoveAllPrefixes(userrec* user)
1034 prefixlist::iterator n = prefixes.find(user);
1035 if (n != prefixes.end())