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 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 ServerInstance->Log(DEBUG,"SetModeParam called");
42 CustomModeList::iterator n = custom_mode_params.find(mode);
46 if (n == custom_mode_params.end())
48 custom_mode_params[mode] = strdup(parameter);
49 ServerInstance->Log(DEBUG,"Custom mode parameter %c %s added",mode,parameter);
53 ServerInstance->Log(DEBUG, "Tried to set custom mode parameter for %c '%s' when it was already '%s'", mode, parameter, n->second);
58 if (n != custom_mode_params.end())
61 custom_mode_params.erase(n);
66 bool chanrec::IsModeSet(char mode)
68 return modes[mode-65];
71 std::string chanrec::GetModeParameter(char mode)
79 return ConvToStr(this->limit);
83 CustomModeList::iterator n = custom_mode_params.find(mode);
84 if (n != custom_mode_params.end())
92 long chanrec::GetUserCounter()
94 return (this->internal_userlist.size());
97 void chanrec::AddUser(userrec* user)
99 internal_userlist[user] = user;
102 unsigned long chanrec::DelUser(userrec* user)
104 CUListIter a = internal_userlist.find(user);
106 if (a != internal_userlist.end())
108 internal_userlist.erase(a);
109 /* And tidy any others... */
111 DelHalfoppedUser(user);
115 return internal_userlist.size();
118 bool chanrec::HasUser(userrec* user)
120 return (internal_userlist.find(user) != internal_userlist.end());
123 void chanrec::AddOppedUser(userrec* user)
125 internal_op_userlist[user] = user;
128 void chanrec::DelOppedUser(userrec* user)
130 CUListIter a = internal_op_userlist.find(user);
131 if (a != internal_op_userlist.end())
133 internal_op_userlist.erase(a);
138 void chanrec::AddHalfoppedUser(userrec* user)
140 internal_halfop_userlist[user] = user;
143 void chanrec::DelHalfoppedUser(userrec* user)
145 CUListIter a = internal_halfop_userlist.find(user);
147 if (a != internal_halfop_userlist.end())
149 internal_halfop_userlist.erase(a);
153 void chanrec::AddVoicedUser(userrec* user)
155 internal_voice_userlist[user] = user;
158 void chanrec::DelVoicedUser(userrec* user)
160 CUListIter a = internal_voice_userlist.find(user);
162 if (a != internal_voice_userlist.end())
164 internal_voice_userlist.erase(a);
168 CUList* chanrec::GetUsers()
170 return &internal_userlist;
173 CUList* chanrec::GetOppedUsers()
175 return &internal_op_userlist;
178 CUList* chanrec::GetHalfoppedUsers()
180 return &internal_halfop_userlist;
183 CUList* chanrec::GetVoicedUsers()
185 return &internal_voice_userlist;
189 * add a channel to a user, creating the record for it if needed and linking
190 * it to the user record
192 chanrec* chanrec::JoinUser(InspIRCd* Instance, userrec *user, const char* cn, bool override, const char* key)
197 bool new_channel = false;
200 strlcpy(cname,cn,CHANMAX);
204 chanrec* Ptr = Instance->FindChan(cname);
210 if (IS_LOCAL(user) && override == false)
213 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,NULL,cname,privs));
218 /* create a new one */
219 Ptr = new chanrec(Instance);
220 (*(Instance->chanlist))[cname] = Ptr;
222 strlcpy(Ptr->name, cname,CHANMAX);
224 /* As spotted by jilles, dont bother to set this on remote users */
226 Ptr->modes[CM_TOPICLOCK] = Ptr->modes[CM_NOEXTERNAL] = 1;
228 Ptr->created = Instance->Time();
232 Instance->Log(DEBUG,"chanrec::JoinUser(): created: %s",cname);
237 /* Already on the channel */
238 if (Ptr->HasUser(user))
242 * remote users are allowed us to bypass channel modes
243 * and bans (used by servers)
245 if (IS_LOCAL(user) && override == false)
248 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname,privs));
253 else if (MOD_RESULT == 0)
258 FOREACH_RESULT_I(Instance,I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
261 if ((!key) || strcmp(key,Ptr->key))
263 user->WriteServ("475 %s %s :Cannot join channel (Incorrect channel key)",user->nick, Ptr->name);
268 if (Ptr->modes[CM_INVITEONLY])
271 FOREACH_RESULT_I(Instance,I_OnCheckInvite,OnCheckInvite(user, Ptr));
274 if (user->IsInvited(Ptr->name))
276 /* user was invited to channel */
277 /* there may be an optional channel NOTICE here */
281 user->WriteServ("473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
285 user->RemoveInvite(Ptr->name);
290 FOREACH_RESULT_I(Instance,I_OnCheckLimit,OnCheckLimit(user, Ptr));
293 if (Ptr->GetUserCounter() >= Ptr->limit)
295 user->WriteServ("471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
300 if (Ptr->bans.size())
302 if (Ptr->IsBanned(user))
304 user->WriteServ("474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
312 Instance->Log(DEBUG,"chanrec::JoinUser(): Overridden checks");
316 /* NOTE: If the user is an oper here, we can extend their user->chans by up to
317 * OPERMAXCHANS. For remote users which are not bound by the channel limits,
318 * we can extend infinitely. Otherwise, nope, youre restricted to MAXCHANS.
320 if (!IS_LOCAL(user) || override == true) /* was a check on fd < 0 */
322 return chanrec::ForceChan(Instance, Ptr, user, privs);
324 else if (*user->oper)
326 /* Oper allows extension up to the OPERMAXCHANS value */
327 if (user->chans.size() < OPERMAXCHANS)
329 return chanrec::ForceChan(Instance, Ptr, user, privs);
332 else if (user->chans.size() < MAXCHANS)
334 return chanrec::ForceChan(Instance, Ptr, user, privs);
338 user->WriteServ("405 %s %s :You are on too many channels",user->nick, cname);
342 Instance->Log(DEBUG,"BLAMMO, Whacking channel.");
343 /* Things went seriously pear shaped, so take this away. bwahaha. */
344 chan_hash::iterator n = Instance->chanlist->find(cname);
345 if (n != Instance->chanlist->end())
349 Instance->chanlist->erase(n);
356 chanrec* chanrec::ForceChan(InspIRCd* Instance, chanrec* Ptr, userrec* user, const std::string &privs)
358 userrec* dummyuser = new userrec(Instance);
359 std::string nick = user->nick;
361 dummyuser->SetFd(FD_MAGIC_NUMBER);
364 /* Just in case they have no permissions */
365 user->chans[Ptr] = 0;
367 for (std::string::const_iterator x = privs.begin(); x != privs.end(); x++)
369 const char status = *x;
370 ModeHandler* mh = Instance->Modes->FindPrefix(status);
373 Ptr->SetPrefix(user, status, mh->GetPrefixRank(), true);
374 /* Make sure that the mode handler knows this mode was now set */
375 mh->OnModeChange(dummyuser, dummyuser, Ptr, nick, true);
377 switch (mh->GetPrefix())
379 /* These logic ops are SAFE IN THIS CASE
380 * because if the entry doesnt exist,
381 * addressing operator[] creates it.
382 * If they do exist, it points to it.
383 * At all other times where we dont want
384 * to create an item if it doesnt exist, we
385 * must stick to ::find().
388 user->chans[Ptr] |= UCMODE_OP;
391 user->chans[Ptr] |= UCMODE_HOP;
394 user->chans[Ptr] |= UCMODE_VOICE;
402 Ptr->WriteChannel(user,"JOIN :%s",Ptr->name);
404 /* Theyre not the first ones in here, make sure everyone else sees the modes we gave the user */
405 std::string ms = Instance->Modes->ModeString(user, Ptr);
406 if ((Ptr->GetUserCounter() > 1) && (ms.length()))
407 Ptr->WriteAllExceptSender(user, true, 0, "MODE %s +%s", Ptr->name, ms.c_str());
409 /* Major improvement by Brain - we dont need to be calculating all this pointlessly for remote users */
414 user->WriteServ("332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
415 user->WriteServ("333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
419 FOREACH_MOD_I(Instance,I_OnUserJoin,OnUserJoin(user,Ptr));
420 FOREACH_MOD_I(Instance,I_OnPostJoin,OnPostJoin(user,Ptr));
424 bool chanrec::IsBanned(userrec* user)
428 FOREACH_RESULT(I_OnCheckBan,OnCheckBan(user, this));
431 snprintf(mask, MAXBUF, "%s!%s@%s", user->nick, user->ident, user->GetIPString());
432 for (BanList::iterator i = this->bans.begin(); i != this->bans.end(); i++)
434 /* This allows CIDR ban matching
436 * Full masked host Full unmasked host IP with/without CIDR
438 if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match(mask, i->data, true)))
448 * remove a channel from a users record, and return the number of users left.
449 * Therefore, if this function returns 0 the caller should delete the chanrec.
451 long chanrec::PartUser(userrec *user, const char* reason)
454 return this->GetUserCounter();
456 UCListIter i = user->chans.find(this);
457 if (i != user->chans.end())
459 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, reason ? reason : ""));
460 this->WriteChannel(user, "PART %s%s%s", this->name, reason ? " :" : "", reason ? reason : "");
461 user->chans.erase(i);
462 this->RemoveAllPrefixes(user);
465 if (!this->DelUser(user)) /* if there are no users left on the channel... */
467 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
468 /* kill the record */
469 if (iter != ServerInstance->chanlist->end())
471 ServerInstance->Log(DEBUG,"del_channel: destroyed: %s", this->name);
472 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
473 ServerInstance->chanlist->erase(iter);
478 return this->GetUserCounter();
481 long chanrec::ServerKickUser(userrec* user, const char* reason, bool triggerevents)
483 if (!user || !reason)
484 return this->GetUserCounter();
488 if (!this->HasUser(user))
491 return this->GetUserCounter();
497 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL,user,this,reason));
500 UCListIter i = user->chans.find(this);
501 if (i != user->chans.end())
503 this->WriteChannelWithServ(ServerInstance->Config->ServerName, "KICK %s %s :%s", this->name, user->nick, reason);
504 user->chans.erase(i);
505 this->RemoveAllPrefixes(user);
508 if (!this->DelUser(user))
510 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
511 /* kill the record */
512 if (iter != ServerInstance->chanlist->end())
514 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
515 ServerInstance->chanlist->erase(iter);
520 return this->GetUserCounter();
523 long chanrec::KickUser(userrec *src, userrec *user, const char* reason)
525 if (!src || !user || !reason)
526 return this->GetUserCounter();
530 if (!this->HasUser(user))
532 src->WriteServ("441 %s %s %s :They are not on that channel",src->nick, user->nick, this->name);
533 return this->GetUserCounter();
535 if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
537 src->WriteServ("482 %s %s :Only a u-line may kick a u-line from a channel.",src->nick, this->name);
538 return this->GetUserCounter();
542 if (!ServerInstance->ULine(src->server))
545 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,this,reason));
547 return this->GetUserCounter();
549 /* Set to -1 by OnUserPreKick if explicit allow was set */
550 if (MOD_RESULT != -1)
552 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,this,AC_KICK));
553 if ((MOD_RESULT == ACR_DENY) && (!ServerInstance->ULine(src->server)))
554 return this->GetUserCounter();
556 if ((MOD_RESULT == ACR_DEFAULT) || (!ServerInstance->ULine(src->server)))
558 int them = this->GetStatus(src);
559 int us = this->GetStatus(user);
560 if ((them < STATUS_HOP) || (them < us))
562 src->WriteServ("482 %s %s :You must be a channel %soperator",src->nick, this->name, them == STATUS_HOP ? "" : "half-");
563 return this->GetUserCounter();
569 FOREACH_MOD(I_OnUserKick,OnUserKick(src,user,this,reason));
571 UCListIter i = user->chans.find(this);
572 if (i != user->chans.end())
574 /* zap it from the channel list of the user */
575 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();
649 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
651 if (IS_LOCAL(i->second))
652 i->second->WriteServ(text);
656 /* write formatted text from a source user to all users on a channel except
657 * for the sender (for privmsg etc) */
658 void chanrec::WriteAllExceptSender(userrec* user, bool serversource, char status, char* text, ...)
660 char textbuffer[MAXBUF];
666 va_start(argsPtr, text);
667 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
670 this->WriteAllExceptSender(user, serversource, status, std::string(textbuffer));
673 void chanrec::WriteAllExcept(userrec* user, bool serversource, char status, CUList &except_list, char* text, ...)
675 char textbuffer[MAXBUF];
681 va_start(argsPtr, text);
682 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
685 this->WriteAllExcept(user, serversource, status, except_list, std::string(textbuffer));
688 void chanrec::WriteAllExcept(userrec* user, bool serversource, char status, CUList &except_list, const std::string &text)
696 ulist = this->GetOppedUsers();
699 ulist = this->GetHalfoppedUsers();
702 ulist = this->GetVoicedUsers();
705 ulist = this->GetUsers();
709 snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());
710 std::string out = tb;
712 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
714 if ((IS_LOCAL(i->second)) && (except_list.find(i->second) == except_list.end()))
717 i->second->WriteServ(text);
719 i->second->Write(out);
724 void chanrec::WriteAllExceptSender(userrec* user, bool serversource, char status, const std::string& text)
727 except_list[user] = user;
728 this->WriteAllExcept(user, serversource, status, except_list, std::string(text));
732 * return a count of the users on a specific channel accounting for
733 * invisible users who won't increase the count. e.g. for /LIST
735 int chanrec::CountInvisible()
738 CUList *ulist= this->GetUsers();
739 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
741 if (!(i->second->modes[UM_INVISIBLE]))
748 char* chanrec::ChanModes(bool showkey)
750 static char scratch[MAXBUF];
751 static char sparam[MAXBUF];
752 char* offset = scratch;
753 std::string extparam = "";
758 /* This was still iterating up to 190, chanrec::modes is only 64 elements -- Om */
759 for(int n = 0; n < 64; n++)
768 extparam = (showkey ? this->key : "<key>");
771 extparam = ConvToStr(this->limit);
779 /* We know these have no parameters */
782 extparam = this->GetModeParameter(n + 65);
787 charlcat(sparam,' ',MAXBUF);
788 strlcat(sparam,extparam.c_str(),MAXBUF);
793 /* Null terminate scratch */
795 strlcat(scratch,sparam,MAXBUF);
799 /* compile a userlist of a channel into a string, each nick seperated by
800 * spaces and op, voice etc status shown as @ and +, and send it to 'user'
802 void chanrec::UserList(userrec *user)
811 FOREACH_RESULT(I_OnUserList,OnUserList(user, this));
812 ServerInstance->Log(DEBUG,"MOD_RESULT for UserList = %d",MOD_RESULT);
816 ServerInstance->Log(DEBUG,"Using builtin NAMES list generation");
818 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
821 char* ptr = list + dlen;
823 CUList *ulist= this->GetUsers();
825 /* Improvement by Brain - this doesnt change in value, so why was it inside
828 bool has_user = this->HasUser(user);
830 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
832 if ((!has_user) && (i->second->modes[UM_INVISIBLE]))
835 * user is +i, and source not on the channel, does not show
841 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", this->GetPrefixChar(i->second), i->second->nick);
848 if (curlen > (480-NICKMAX))
850 /* list overflowed into multiple numerics */
851 user->WriteServ(std::string(list));
853 /* reset our lengths */
854 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
862 /* if whats left in the list isnt empty, send it */
865 user->WriteServ(std::string(list));
868 user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, this->name);
871 long chanrec::GetMaxBans()
873 for (std::map<std::string,int>::iterator n = ServerInstance->Config->maxbans.begin(); n != ServerInstance->Config->maxbans.end(); n++)
875 if (match(this->name,n->first.c_str()))
884 /* returns the status character for a given user on a channel, e.g. @ for op,
885 * % for halfop etc. If the user has several modes set, the highest mode
886 * the user has must be returned.
888 const char* chanrec::GetPrefixChar(userrec *user)
890 static char pf[2] = {0, 0};
892 prefixlist::iterator n = prefixes.find(user);
893 if (n != prefixes.end())
895 if (n->second.size())
897 /* If the user has any prefixes, their highest prefix
898 * will always be at the head of the list, as the list is
899 * sorted in rank order highest first (see SetPrefix()
902 *pf = n->second.begin()->first;
911 const char* chanrec::GetAllPrefixChars(userrec* user)
913 static char prefix[MAXBUF];
917 prefixlist::iterator n = prefixes.find(user);
918 if (n != prefixes.end())
920 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
922 prefix[ctr++] = x->first;
931 unsigned int chanrec::GetPrefixValue(userrec* user)
933 prefixlist::iterator n = prefixes.find(user);
934 if (n != prefixes.end())
936 if (n->second.size())
937 return n->second.begin()->second;
942 int chanrec::GetStatusFlags(userrec *user)
944 UCListIter i = user->chans.find(this);
945 if (i != user->chans.end())
952 int chanrec::GetStatus(userrec *user)
954 if (ServerInstance->ULine(user->server))
957 UCListIter i = user->chans.find(this);
958 if (i != user->chans.end())
960 if ((i->second & UCMODE_OP) > 0)
964 if ((i->second & UCMODE_HOP) > 0)
968 if ((i->second & UCMODE_VOICE) > 0)
972 return STATUS_NORMAL;
974 return STATUS_NORMAL;
977 void chanrec::SetPrefix(userrec* user, char prefix, unsigned int prefix_value, bool adding)
979 ServerInstance->Log(DEBUG,"Setting prefix: %c on user %s in %s to %d", prefix, user->nick, this->name, adding);
980 prefixlist::iterator n = prefixes.find(user);
981 prefixtype pfx = std::make_pair(prefix,prefix_value);
984 if (n != prefixes.end())
986 if (std::find(n->second.begin(), n->second.end(), pfx) == n->second.end())
988 n->second.push_back(pfx);
989 /* We must keep prefixes in rank order, largest first.
990 * This is for two reasons, firstly because x-chat *ass-u-me's* this
991 * state, and secondly it turns out to be a benefit to us later.
992 * See above in GetPrefix().
994 std::sort(n->second.begin(), n->second.end(), ModeParser::PrefixComparison);
1001 prefixes.insert(std::make_pair<userrec*,pfxcontainer>(user, one));
1006 if (n != prefixes.end())
1008 pfxcontainer::iterator x = std::find(n->second.begin(), n->second.end(), pfx);
1009 if (x != n->second.end())
1013 ServerInstance->Log(DEBUG,"Added prefix %c to %s for %s, prefixlist size is now %d", prefix, this->name, user->nick, prefixes.size());
1016 void chanrec::RemoveAllPrefixes(userrec* user)
1018 prefixlist::iterator n = prefixes.find(user);
1019 if (n != prefixes.end())
1021 ServerInstance->Log(DEBUG,"Removed prefixes from %s for %s, prefixlist size is now %d", this->name, user->nick, prefixes.size());