1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
20 #include "configreader.h"
27 chanrec::chanrec(InspIRCd* Instance) : ServerInstance(Instance)
29 *name = *topic = *setby = *key = 0;
30 created = topicset = limit = 0;
31 internal_userlist.clear();
35 void chanrec::SetMode(char mode,bool mode_on)
37 modes[mode-65] = mode_on;
39 this->SetModeParam(mode,"",false);
43 void chanrec::SetModeParam(char mode,const char* parameter,bool mode_on)
45 ServerInstance->Log(DEBUG,"SetModeParam called");
47 CustomModeList::iterator n = custom_mode_params.find(mode);
51 if (n == custom_mode_params.end())
53 custom_mode_params[mode] = strdup(parameter);
54 ServerInstance->Log(DEBUG,"Custom mode parameter %c %s added",mode,parameter);
58 ServerInstance->Log(DEBUG, "Tried to set custom mode parameter for %c '%s' when it was already '%s'", mode, parameter, n->second);
63 if (n != custom_mode_params.end())
66 custom_mode_params.erase(n);
71 bool chanrec::IsModeSet(char mode)
73 return modes[mode-65];
76 std::string chanrec::GetModeParameter(char mode)
84 return ConvToStr(this->limit);
88 CustomModeList::iterator n = custom_mode_params.find(mode);
89 if (n != custom_mode_params.end())
97 long chanrec::GetUserCounter()
99 return (this->internal_userlist.size());
102 void chanrec::AddUser(userrec* user)
104 internal_userlist[user] = user;
107 unsigned long chanrec::DelUser(userrec* user)
109 CUListIter a = internal_userlist.find(user);
111 if (a != internal_userlist.end())
113 internal_userlist.erase(a);
114 /* And tidy any others... */
116 DelHalfoppedUser(user);
120 return internal_userlist.size();
123 bool chanrec::HasUser(userrec* user)
125 return (internal_userlist.find(user) != internal_userlist.end());
128 void chanrec::AddOppedUser(userrec* user)
130 internal_op_userlist[user] = user;
133 void chanrec::DelOppedUser(userrec* user)
135 CUListIter a = internal_op_userlist.find(user);
136 if (a != internal_op_userlist.end())
138 internal_op_userlist.erase(a);
143 void chanrec::AddHalfoppedUser(userrec* user)
145 internal_halfop_userlist[user] = user;
148 void chanrec::DelHalfoppedUser(userrec* user)
150 CUListIter a = internal_halfop_userlist.find(user);
152 if (a != internal_halfop_userlist.end())
154 internal_halfop_userlist.erase(a);
158 void chanrec::AddVoicedUser(userrec* user)
160 internal_voice_userlist[user] = user;
163 void chanrec::DelVoicedUser(userrec* user)
165 CUListIter a = internal_voice_userlist.find(user);
167 if (a != internal_voice_userlist.end())
169 internal_voice_userlist.erase(a);
173 CUList* chanrec::GetUsers()
175 return &internal_userlist;
178 CUList* chanrec::GetOppedUsers()
180 return &internal_op_userlist;
183 CUList* chanrec::GetHalfoppedUsers()
185 return &internal_halfop_userlist;
188 CUList* chanrec::GetVoicedUsers()
190 return &internal_voice_userlist;
194 * add a channel to a user, creating the record for it if needed and linking
195 * it to the user record
197 chanrec* chanrec::JoinUser(InspIRCd* Instance, userrec *user, const char* cn, bool override, const char* key)
202 bool new_channel = false;
205 strlcpy(cname,cn,CHANMAX);
209 chanrec* Ptr = Instance->FindChan(cname);
218 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,NULL,cname,privs));
223 /* create a new one */
224 Ptr = new chanrec(Instance);
225 Instance->chanlist[cname] = Ptr;
227 strlcpy(Ptr->name, cname,CHANMAX);
228 Ptr->modes[CM_TOPICLOCK] = Ptr->modes[CM_NOEXTERNAL] = 1;
229 Ptr->created = Instance->Time();
233 Instance->Log(DEBUG,"chanrec::JoinUser(): created: %s",cname);
238 /* Already on the channel */
239 if (Ptr->HasUser(user))
243 * remote users are allowed us to bypass channel modes
244 * and bans (used by servers)
246 if (IS_LOCAL(user)) /* was a check on fd > -1 */
249 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname,privs));
254 else if (MOD_RESULT == 0)
259 FOREACH_RESULT_I(Instance,I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
262 if ((!key) || strcmp(key,Ptr->key))
264 user->WriteServ("475 %s %s :Cannot join channel (Incorrect channel key)",user->nick, Ptr->name);
269 if (Ptr->modes[CM_INVITEONLY])
272 irc::string xname(Ptr->name);
273 FOREACH_RESULT_I(Instance,I_OnCheckInvite,OnCheckInvite(user, Ptr));
276 if (user->IsInvited(xname))
278 /* user was invited to channel */
279 /* there may be an optional channel NOTICE here */
283 user->WriteServ("473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
287 user->RemoveInvite(xname);
292 FOREACH_RESULT_I(Instance,I_OnCheckLimit,OnCheckLimit(user, Ptr));
295 if (Ptr->GetUserCounter() >= Ptr->limit)
297 user->WriteServ("471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
302 if (Ptr->bans.size())
305 FOREACH_RESULT_I(Instance,I_OnCheckBan,OnCheckBan(user, Ptr));
307 snprintf(mask, MAXBUF, "%s!%s@%s",user->nick, user->ident, user->GetIPString());
310 if (Ptr->IsBanned(user))
312 user->WriteServ("474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
321 Instance->Log(DEBUG,"chanrec::JoinUser(): Overridden checks");
325 for (UserChanList::const_iterator index = user->chans.begin(); index != user->chans.end(); index++)
327 if ((*index)->channel == NULL)
329 return chanrec::ForceChan(Instance, Ptr, *index, user, privs);
334 * XXX: If the user is an oper here, we can just extend their user->chans vector by one
335 * and put the channel in here. Same for remote users which are not bound by
336 * the channel limits. Otherwise, nope, youre boned.
338 if (!IS_LOCAL(user)) /* was a check on fd < 0 */
340 ucrec* a = new ucrec();
341 chanrec* c = chanrec::ForceChan(Instance, Ptr, a, user, privs);
342 user->chans.push_back(a);
345 else if (*user->oper)
347 /* Oper allows extension up to the OPERMAXCHANS value */
348 if (user->chans.size() < OPERMAXCHANS)
350 ucrec* a = new ucrec();
351 chanrec* c = chanrec::ForceChan(Instance, Ptr, a, user, privs);
352 user->chans.push_back(a);
357 user->WriteServ("405 %s %s :You are on too many channels",user->nick, cname);
361 Instance->Log(DEBUG,"BLAMMO, Whacking channel.");
362 /* Things went seriously pear shaped, so take this away. bwahaha. */
363 chan_hash::iterator n = Instance->chanlist.find(cname);
364 if (n != Instance->chanlist.end())
368 Instance->chanlist.erase(n);
369 for (unsigned int index =0; index < user->chans.size(); index++)
371 if (user->chans[index]->channel == Ptr)
373 user->chans[index]->channel = NULL;
374 user->chans[index]->uc_modes = 0;
381 for (unsigned int index =0; index < user->chans.size(); index++)
383 if (user->chans[index]->channel == Ptr)
385 user->chans[index]->channel = NULL;
386 user->chans[index]->uc_modes = 0;
393 chanrec* chanrec::ForceChan(InspIRCd* Instance, chanrec* Ptr,ucrec *a,userrec* user, const std::string &privs)
397 for (std::string::const_iterator x = privs.begin(); x != privs.end(); x++)
399 const char status = *x;
403 a->uc_modes |= UCMODE_OP;
406 a->uc_modes |= UCMODE_HOP;
409 a->uc_modes |= UCMODE_VOICE;
412 ModeHandler* mh = Instance->Modes->FindPrefix(status);
415 Ptr->SetPrefix(user, status, mh->GetPrefixRank(), true);
421 user->ModChannelCount(1);
422 Ptr->WriteChannel(user,"JOIN :%s",Ptr->name);
424 /* Theyre not the first ones in here, make sure everyone else sees the modes we gave the user */
425 std::string ms = Instance->Modes->ModeString(user, Ptr);
426 if ((Ptr->GetUserCounter() > 1) && (ms.length()))
427 Ptr->WriteAllExceptSender(user, true, 0, "MODE %s +%s", Ptr->name, ms.c_str());
429 /* Major improvement by Brain - we dont need to be calculating all this pointlessly for remote users */
434 user->WriteServ("332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
435 user->WriteServ("333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
439 FOREACH_MOD_I(Instance,I_OnUserJoin,OnUserJoin(user,Ptr));
443 bool chanrec::IsBanned(userrec* user)
446 snprintf(mask, MAXBUF, "%s!%s@%s", user->nick, user->ident, user->GetIPString());
447 for (BanList::iterator i = this->bans.begin(); i != this->bans.end(); i++)
449 /* This allows CIDR ban matching
451 * Full masked host Full unmasked host IP with/without CIDR
453 if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match(mask, i->data, true)))
463 * remove a channel from a users record, and remove the record from the hash
464 * if the channel has become empty
466 long chanrec::PartUser(userrec *user, const char* reason)
469 return this->GetUserCounter();
471 for (unsigned int i =0; i < user->chans.size(); i++)
473 /* zap it from the channel list of the user */
474 if (user->chans[i]->channel == this)
478 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, reason));
479 this->WriteChannel(user, "PART %s :%s", this->name, reason);
483 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, ""));
484 this->WriteChannel(user, "PART :%s", this->name);
486 user->chans[i]->uc_modes = 0;
487 user->chans[i]->channel = NULL;
488 user->ModChannelCount(-1);
489 this->RemoveAllPrefixes(user);
494 if (!this->DelUser(user)) /* if there are no users left on the channel... */
496 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
497 /* kill the record */
498 if (iter != ServerInstance->chanlist.end())
500 ServerInstance->Log(DEBUG,"del_channel: destroyed: %s", this->name);
501 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
502 ServerInstance->chanlist.erase(iter);
507 return this->GetUserCounter();
510 long chanrec::ServerKickUser(userrec* user, const char* reason, bool triggerevents)
512 if (!user || !reason)
513 return this->GetUserCounter();
517 if (!this->HasUser(user))
520 return this->GetUserCounter();
526 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL,user,this,reason));
529 for (unsigned int i =0; i < user->chans.size(); i++)
531 if (user->chans[i]->channel == this)
533 this->WriteChannelWithServ(ServerInstance->Config->ServerName, "KICK %s %s :%s", this->name, user->nick, reason);
534 user->chans[i]->uc_modes = 0;
535 user->chans[i]->channel = NULL;
536 this->RemoveAllPrefixes(user);
541 if (!this->DelUser(user))
543 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
544 /* kill the record */
545 if (iter != ServerInstance->chanlist.end())
547 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
548 ServerInstance->chanlist.erase(iter);
553 return this->GetUserCounter();
556 long chanrec::KickUser(userrec *src, userrec *user, const char* reason)
558 if (!src || !user || !reason)
559 return this->GetUserCounter();
563 if (!this->HasUser(user))
565 src->WriteServ("441 %s %s %s :They are not on that channel",src->nick, user->nick, this->name);
566 return this->GetUserCounter();
568 if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
570 src->WriteServ("482 %s %s :Only a u-line may kick a u-line from a channel.",src->nick, this->name);
571 return this->GetUserCounter();
575 if (!ServerInstance->ULine(src->server))
578 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,this,reason));
580 return this->GetUserCounter();
582 /* Set to -1 by OnUserPreKick if explicit allow was set */
583 if (MOD_RESULT != -1)
585 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,this,AC_KICK));
586 if ((MOD_RESULT == ACR_DENY) && (!ServerInstance->ULine(src->server)))
587 return this->GetUserCounter();
589 if ((MOD_RESULT == ACR_DEFAULT) || (!ServerInstance->ULine(src->server)))
591 int them = this->GetStatus(src);
592 int us = this->GetStatus(user);
593 if ((them < STATUS_HOP) || (them < us))
595 if (them == STATUS_HOP)
597 src->WriteServ("482 %s %s :You must be a channel operator",src->nick, this->name);
601 src->WriteServ("482 %s %s :You must be at least a half-operator",src->nick, this->name);
603 return this->GetUserCounter();
609 FOREACH_MOD(I_OnUserKick,OnUserKick(src,user,this,reason));
611 for (UserChanList::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
613 /* zap it from the channel list of the user */
614 if ((*i)->channel == this)
616 this->WriteChannel(src, "KICK %s %s :%s", this->name, user->nick, reason);
618 (*i)->channel = NULL;
619 this->RemoveAllPrefixes(user);
624 if (!this->DelUser(user))
625 /* if there are no users left on the channel */
627 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
629 /* kill the record */
630 if (iter != ServerInstance->chanlist.end())
632 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
633 ServerInstance->chanlist.erase(iter);
638 return this->GetUserCounter();
641 void chanrec::WriteChannel(userrec* user, char* text, ...)
643 char textbuffer[MAXBUF];
649 va_start(argsPtr, text);
650 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
653 this->WriteChannel(user, std::string(textbuffer));
656 void chanrec::WriteChannel(userrec* user, const std::string &text)
658 CUList *ulist = this->GetUsers();
663 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
665 if (IS_LOCAL(i->second))
666 user->WriteTo(i->second,text);
670 void chanrec::WriteChannelWithServ(const char* ServName, const char* text, ...)
672 char textbuffer[MAXBUF];
678 va_start(argsPtr, text);
679 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
682 this->WriteChannelWithServ(ServName, std::string(textbuffer));
685 void chanrec::WriteChannelWithServ(const char* ServName, const std::string &text)
687 CUList *ulist = this->GetUsers();
689 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
691 if (IS_LOCAL(i->second))
692 i->second->WriteServ(text);
696 /* write formatted text from a source user to all users on a channel except
697 * for the sender (for privmsg etc) */
698 void chanrec::WriteAllExceptSender(userrec* user, bool serversource, char status, char* text, ...)
700 char textbuffer[MAXBUF];
706 va_start(argsPtr, text);
707 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
710 this->WriteAllExceptSender(user, serversource, status, std::string(textbuffer));
713 void chanrec::WriteAllExceptSender(userrec* user, bool serversource, char status, const std::string& text)
720 ulist = this->GetOppedUsers();
723 ulist = this->GetHalfoppedUsers();
726 ulist = this->GetVoicedUsers();
729 ulist = this->GetUsers();
733 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
735 if ((IS_LOCAL(i->second)) && (user != i->second))
738 i->second->WriteServ(text);
740 i->second->WriteFrom(user,text);
746 * return a count of the users on a specific channel accounting for
747 * invisible users who won't increase the count. e.g. for /LIST
749 int chanrec::CountInvisible()
752 CUList *ulist= this->GetUsers();
753 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
755 if (!(i->second->modes[UM_INVISIBLE]))
762 char* chanrec::ChanModes(bool showkey)
764 static char scratch[MAXBUF];
765 static char sparam[MAXBUF];
766 char* offset = scratch;
767 std::string extparam = "";
772 /* This was still iterating up to 190, chanrec::custom_modes is only 64 elements -- Om */
773 for(int n = 0; n < 64; n++)
782 extparam = (showkey ? this->key : "<key>");
785 extparam = ConvToStr(this->limit);
793 /* We know these have no parameters */
796 extparam = this->GetModeParameter(n + 65);
801 charlcat(sparam,' ',MAXBUF);
802 strlcat(sparam,extparam.c_str(),MAXBUF);
807 /* Null terminate scratch */
809 strlcat(scratch,sparam,MAXBUF);
813 /* compile a userlist of a channel into a string, each nick seperated by
814 * spaces and op, voice etc status shown as @ and +, and send it to 'user'
816 void chanrec::UserList(userrec *user)
822 FOREACH_RESULT(I_OnUserList,OnUserList(user, this));
823 ServerInstance->Log(DEBUG,"MOD_RESULT for UserList = %d",MOD_RESULT);
827 ServerInstance->Log(DEBUG,"Using builtin NAMES list generation");
829 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
832 char* ptr = list + dlen;
834 CUList *ulist= this->GetUsers();
836 /* Improvement by Brain - this doesnt change in value, so why was it inside
839 bool has_user = this->HasUser(user);
841 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
843 if ((!has_user) && (i->second->modes[UM_INVISIBLE]))
846 * user is +i, and source not on the channel, does not show
852 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", this->GetPrefixChar(i->second), i->second->nick);
859 if (curlen > (480-NICKMAX))
861 /* list overflowed into multiple numerics */
862 user->WriteServ(std::string(list));
864 /* reset our lengths */
865 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
873 /* if whats left in the list isnt empty, send it */
876 user->WriteServ(std::string(list));
879 user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, this->name);
882 long chanrec::GetMaxBans()
885 for (std::map<std::string,int>::iterator n = ServerInstance->Config->maxbans.begin(); n != ServerInstance->Config->maxbans.end(); n++)
888 if (match(this->name,x.c_str()))
897 /* returns the status character for a given user on a channel, e.g. @ for op,
898 * % for halfop etc. If the user has several modes set, the highest mode
899 * the user has must be returned.
901 const char* chanrec::GetPrefixChar(userrec *user)
909 prefixlist::iterator n = prefixes.find(user);
910 if (n != prefixes.end())
912 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
925 const char* chanrec::GetAllPrefixChars(userrec* user)
927 static char prefix[MAXBUF];
931 prefixlist::iterator n = prefixes.find(user);
932 if (n != prefixes.end())
934 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
936 prefix[ctr++] = x->first;
945 unsigned int chanrec::GetPrefixValue(userrec* user)
949 prefixlist::iterator n = prefixes.find(user);
950 if (n != prefixes.end())
952 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
963 int chanrec::GetStatusFlags(userrec *user)
965 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
967 if ((*i)->channel == this)
969 return (*i)->uc_modes;
976 int chanrec::GetStatus(userrec *user)
978 if (ServerInstance->ULine(user->server))
981 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
983 if ((*i)->channel == this)
985 if (((*i)->uc_modes & UCMODE_OP) > 0)
989 if (((*i)->uc_modes & UCMODE_HOP) > 0)
993 if (((*i)->uc_modes & UCMODE_VOICE) > 0)
997 return STATUS_NORMAL;
1000 return STATUS_NORMAL;
1003 /*bool ModeParser::PrefixComparison(const prefixtype one, const prefixtype two)
1005 return one.second > two.second;
1008 void chanrec::SetPrefix(userrec* user, char prefix, unsigned int prefix_value, bool adding)
1010 prefixlist::iterator n = prefixes.find(user);
1011 prefixtype pfx = std::make_pair(prefix,prefix_value);
1014 if (n != prefixes.end())
1016 if (std::find(n->second.begin(), n->second.end(), pfx) == n->second.end())
1018 n->second.push_back(pfx);
1019 std::sort(n->second.begin(), n->second.end(), ModeParser::PrefixComparison);
1026 prefixes.insert(std::make_pair<userrec*,pfxcontainer>(user, one));
1031 if (n != prefixes.end())
1033 pfxcontainer::iterator x = std::find(n->second.begin(), n->second.end(), pfx);
1034 if (x != n->second.end())
1040 void chanrec::RemoveAllPrefixes(userrec* user)
1042 prefixlist::iterator n = prefixes.find(user);
1043 if (n != prefixes.end())