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 * ---------------------------------------------------
25 #include "configreader.h"
37 #include "inspstring.h"
41 chanrec::chanrec(InspIRCd* Instance) : ServerInstance(Instance)
43 *name = *topic = *setby = *key = 0;
44 created = topicset = limit = 0;
45 internal_userlist.clear();
49 void chanrec::SetMode(char mode,bool mode_on)
51 modes[mode-65] = mode_on;
53 this->SetModeParam(mode,"",false);
57 void chanrec::SetModeParam(char mode,const char* parameter,bool mode_on)
59 ServerInstance->Log(DEBUG,"SetModeParam called");
61 CustomModeList::iterator n = custom_mode_params.find(mode);
65 if (n == custom_mode_params.end())
67 custom_mode_params[mode] = strdup(parameter);
68 ServerInstance->Log(DEBUG,"Custom mode parameter %c %s added",mode,parameter);
72 ServerInstance->Log(DEBUG, "Tried to set custom mode parameter for %c '%s' when it was already '%s'", mode, parameter, n->second);
77 if (n != custom_mode_params.end())
80 custom_mode_params.erase(n);
85 bool chanrec::IsModeSet(char mode)
87 return modes[mode-65];
90 std::string chanrec::GetModeParameter(char mode)
98 return ConvToStr(this->limit);
102 CustomModeList::iterator n = custom_mode_params.find(mode);
103 if (n != custom_mode_params.end())
111 long chanrec::GetUserCounter()
113 return (this->internal_userlist.size());
116 void chanrec::AddUser(userrec* user)
118 internal_userlist[user] = user;
121 unsigned long chanrec::DelUser(userrec* user)
123 CUListIter a = internal_userlist.find(user);
125 if (a != internal_userlist.end())
127 internal_userlist.erase(a);
128 /* And tidy any others... */
130 DelHalfoppedUser(user);
134 return internal_userlist.size();
137 bool chanrec::HasUser(userrec* user)
139 return (internal_userlist.find(user) != internal_userlist.end());
142 void chanrec::AddOppedUser(userrec* user)
144 internal_op_userlist[user] = user;
147 void chanrec::DelOppedUser(userrec* user)
149 CUListIter a = internal_op_userlist.find(user);
150 if (a != internal_op_userlist.end())
152 internal_op_userlist.erase(a);
157 void chanrec::AddHalfoppedUser(userrec* user)
159 internal_halfop_userlist[user] = user;
162 void chanrec::DelHalfoppedUser(userrec* user)
164 CUListIter a = internal_halfop_userlist.find(user);
166 if (a != internal_halfop_userlist.end())
168 internal_halfop_userlist.erase(a);
172 void chanrec::AddVoicedUser(userrec* user)
174 internal_voice_userlist[user] = user;
177 void chanrec::DelVoicedUser(userrec* user)
179 CUListIter a = internal_voice_userlist.find(user);
181 if (a != internal_voice_userlist.end())
183 internal_voice_userlist.erase(a);
187 CUList* chanrec::GetUsers()
189 return &internal_userlist;
192 CUList* chanrec::GetOppedUsers()
194 return &internal_op_userlist;
197 CUList* chanrec::GetHalfoppedUsers()
199 return &internal_halfop_userlist;
202 CUList* chanrec::GetVoicedUsers()
204 return &internal_voice_userlist;
208 * add a channel to a user, creating the record for it if needed and linking
209 * it to the user record
211 chanrec* chanrec::JoinUser(InspIRCd* Instance, userrec *user, const char* cn, bool override, const char* key)
219 strlcpy(cname,cn,CHANMAX);
221 chanrec* Ptr = Instance->FindChan(cname);
228 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,NULL,cname));
233 /* create a new one */
234 Ptr = new chanrec(Instance);
235 Instance->chanlist[cname] = Ptr;
237 strlcpy(Ptr->name, cname,CHANMAX);
238 Ptr->modes[CM_TOPICLOCK] = Ptr->modes[CM_NOEXTERNAL] = 1;
239 Ptr->created = Instance->Time();
241 strlcpy(Ptr->setby, user->nick,NICKMAX-1);
243 Instance->Log(DEBUG,"chanrec::JoinUser(): created: %s",cname);
245 * set created to 2 to indicate user
246 * is the first in the channel
247 * and should be given ops
253 /* Already on the channel */
254 if (Ptr->HasUser(user))
258 * remote users are allowed us to bypass channel modes
259 * and bans (used by servers)
261 if (IS_LOCAL(user)) /* was a check on fd > -1 */
264 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname));
269 else if (MOD_RESULT == 0)
274 FOREACH_RESULT_I(Instance,I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
279 Instance->Log(DEBUG,"chanrec::JoinUser(): no key given in JOIN");
280 user->WriteServ("475 %s %s :Cannot join channel (Requires key)",user->nick, Ptr->name);
285 if (strcmp(key,Ptr->key))
287 Instance->Log(DEBUG,"chanrec::JoinUser(): bad key given in JOIN");
288 user->WriteServ("475 %s %s :Cannot join channel (Incorrect key)",user->nick, Ptr->name);
294 if (Ptr->modes[CM_INVITEONLY])
297 irc::string xname(Ptr->name);
298 FOREACH_RESULT_I(Instance,I_OnCheckInvite,OnCheckInvite(user, Ptr));
301 if (user->IsInvited(xname))
303 /* user was invited to channel */
304 /* there may be an optional channel NOTICE here */
308 user->WriteServ("473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
312 user->RemoveInvite(xname);
317 FOREACH_RESULT_I(Instance,I_OnCheckLimit,OnCheckLimit(user, Ptr));
320 if (Ptr->GetUserCounter() >= Ptr->limit)
322 user->WriteServ("471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
327 if (Ptr->bans.size())
330 FOREACH_RESULT_I(Instance,I_OnCheckBan,OnCheckBan(user, Ptr));
332 sprintf(mask,"%s!%s@%s",user->nick, user->ident, user->GetIPString());
335 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
337 /* This allows CIDR ban matching
339 * Full masked host Full unmasked host IP with/without CIDR
341 if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match(mask, i->data, true)))
343 user->WriteServ("474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
353 Instance->Log(DEBUG,"chanrec::JoinUser(): Overridden checks");
358 for (UserChanList::const_iterator index = user->chans.begin(); index != user->chans.end(); index++)
360 if ((*index)->channel == NULL)
362 return chanrec::ForceChan(Instance, Ptr, *index, user, created);
367 * XXX: If the user is an oper here, we can just extend their user->chans vector by one
368 * and put the channel in here. Same for remote users which are not bound by
369 * the channel limits. Otherwise, nope, youre boned.
371 if (!IS_LOCAL(user)) /* was a check on fd < 0 */
373 ucrec* a = new ucrec();
374 chanrec* c = chanrec::ForceChan(Instance, Ptr,a,user,created);
375 user->chans.push_back(a);
378 else if (*user->oper)
380 /* Oper allows extension up to the OPERMAXCHANS value */
381 if (user->chans.size() < OPERMAXCHANS)
383 ucrec* a = new ucrec();
384 chanrec* c = chanrec::ForceChan(Instance, Ptr,a,user,created);
385 user->chans.push_back(a);
390 user->WriteServ("405 %s %s :You are on too many channels",user->nick, cname);
394 Instance->Log(DEBUG,"BLAMMO, Whacking channel.");
395 /* Things went seriously pear shaped, so take this away. bwahaha. */
396 chan_hash::iterator n = Instance->chanlist.find(cname);
397 if (n != Instance->chanlist.end())
401 Instance->chanlist.erase(n);
402 for (unsigned int index =0; index < user->chans.size(); index++)
404 if (user->chans[index]->channel == Ptr)
406 user->chans[index]->channel = NULL;
407 user->chans[index]->uc_modes = 0;
414 for (unsigned int index =0; index < user->chans.size(); index++)
416 if (user->chans[index]->channel == Ptr)
418 user->chans[index]->channel = NULL;
419 user->chans[index]->uc_modes = 0;
426 chanrec* chanrec::ForceChan(InspIRCd* Instance, chanrec* Ptr,ucrec *a,userrec* user, int created)
430 /* first user in is given ops */
431 a->uc_modes = UCMODE_OP;
432 Ptr->AddOppedUser(user);
441 Ptr->WriteChannel(user,"JOIN :%s",Ptr->name);
443 /* Major improvement by Brain - we dont need to be calculating all this pointlessly for remote users */
448 user->WriteServ("332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
449 user->WriteServ("333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
452 user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
454 FOREACH_MOD_I(Instance,I_OnUserJoin,OnUserJoin(user,Ptr));
459 * remove a channel from a users record, and remove the record from the hash
460 * if the channel has become empty
462 long chanrec::PartUser(userrec *user, const char* reason)
465 return this->GetUserCounter();
467 for (unsigned int i =0; i < user->chans.size(); i++)
469 /* zap it from the channel list of the user */
470 if (user->chans[i]->channel == this)
474 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, reason));
475 this->WriteChannel(user, "PART %s :%s", this->name, reason);
479 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, ""));
480 this->WriteChannel(user, "PART :%s", this->name);
482 user->chans[i]->uc_modes = 0;
483 user->chans[i]->channel = NULL;
488 if (!this->DelUser(user)) /* if there are no users left on the channel... */
490 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
491 /* kill the record */
492 if (iter != ServerInstance->chanlist.end())
494 ServerInstance->Log(DEBUG,"del_channel: destroyed: %s", this->name);
495 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
496 ServerInstance->chanlist.erase(iter);
501 return this->GetUserCounter();
504 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));
523 for (unsigned int i =0; i < user->chans.size(); i++)
525 if (user->chans[i]->channel == this)
527 this->WriteChannelWithServ(ServerInstance->Config->ServerName, "KICK %s %s :%s", this->name, user->nick, reason);
528 user->chans[i]->uc_modes = 0;
529 user->chans[i]->channel = NULL;
534 if (!this->DelUser(user))
536 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
537 /* kill the record */
538 if (iter != ServerInstance->chanlist.end())
540 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
541 ServerInstance->chanlist.erase(iter);
546 return this->GetUserCounter();
549 long chanrec::KickUser(userrec *src, userrec *user, const char* reason)
551 if (!src || !user || !reason)
552 return this->GetUserCounter();
556 if (!this->HasUser(user))
558 src->WriteServ("441 %s %s %s :They are not on that channel",src->nick, user->nick, this->name);
559 return this->GetUserCounter();
561 if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
563 src->WriteServ("482 %s %s :Only a u-line may kick a u-line from a channel.",src->nick, this->name);
564 return this->GetUserCounter();
568 if (!ServerInstance->ULine(src->server))
571 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,this,reason));
573 return this->GetUserCounter();
575 /* Set to -1 by OnUserPreKick if explicit allow was set */
576 if (MOD_RESULT != -1)
578 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,this,AC_KICK));
579 if ((MOD_RESULT == ACR_DENY) && (!ServerInstance->ULine(src->server)))
580 return this->GetUserCounter();
582 if ((MOD_RESULT == ACR_DEFAULT) || (!ServerInstance->ULine(src->server)))
584 int them = this->GetStatus(src);
585 int us = this->GetStatus(user);
586 if ((them < STATUS_HOP) || (them < us))
588 if (them == STATUS_HOP)
590 src->WriteServ("482 %s %s :You must be a channel operator",src->nick, this->name);
594 src->WriteServ("482 %s %s :You must be at least a half-operator",src->nick, this->name);
596 return this->GetUserCounter();
602 FOREACH_MOD(I_OnUserKick,OnUserKick(src,user,this,reason));
604 for (UserChanList::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
606 /* zap it from the channel list of the user */
607 if ((*i)->channel == this)
609 this->WriteChannel(src, "KICK %s %s :%s", this->name, user->nick, reason);
611 (*i)->channel = NULL;
616 if (!this->DelUser(user))
617 /* if there are no users left on the channel */
619 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
621 /* kill the record */
622 if (iter != ServerInstance->chanlist.end())
624 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
625 ServerInstance->chanlist.erase(iter);
630 return this->GetUserCounter();
633 void chanrec::WriteChannel(userrec* user, char* text, ...)
635 char textbuffer[MAXBUF];
641 va_start(argsPtr, text);
642 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
645 this->WriteChannel(user, std::string(textbuffer));
648 void chanrec::WriteChannel(userrec* user, const std::string &text)
650 CUList *ulist = this->GetUsers();
655 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
657 if (IS_LOCAL(i->second))
658 user->WriteTo(i->second,text);
662 void chanrec::WriteChannelWithServ(const char* ServName, const char* text, ...)
664 char textbuffer[MAXBUF];
670 va_start(argsPtr, text);
671 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
674 this->WriteChannelWithServ(ServName, std::string(textbuffer));
677 void chanrec::WriteChannelWithServ(const char* ServName, const std::string &text)
679 CUList *ulist = this->GetUsers();
681 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
683 if (IS_LOCAL(i->second))
684 i->second->WriteServ(text);
688 /* write formatted text from a source user to all users on a channel except
689 * for the sender (for privmsg etc) */
690 void chanrec::WriteAllExceptSender(userrec* user, char status, char* text, ...)
692 char textbuffer[MAXBUF];
698 va_start(argsPtr, text);
699 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
702 this->WriteAllExceptSender(user, status, std::string(textbuffer));
705 void chanrec::WriteAllExceptSender(userrec* user, char status, const std::string& text)
715 ulist = this->GetOppedUsers();
718 ulist = this->GetHalfoppedUsers();
721 ulist = this->GetVoicedUsers();
724 ulist = this->GetUsers();
728 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
730 if ((IS_LOCAL(i->second)) && (user != i->second))
731 i->second->WriteFrom(user,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::custom_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)
811 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
814 char* ptr = list + dlen;
816 CUList *ulist= this->GetUsers();
818 /* Improvement by Brain - this doesnt change in value, so why was it inside
821 bool has_user = this->HasUser(user);
823 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
825 if ((!has_user) && (i->second->modes[UM_INVISIBLE]))
828 * user is +i, and source not on the channel, does not show
834 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", this->GetStatusChar(i->second), i->second->nick);
841 if (curlen > (480-NICKMAX))
843 /* list overflowed into multiple numerics */
844 user->WriteServ(list);
846 /* reset our lengths */
847 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
855 /* if whats left in the list isnt empty, send it */
858 user->WriteServ(list);
862 long chanrec::GetMaxBans()
865 for (std::map<std::string,int>::iterator n = ServerInstance->Config->maxbans.begin(); n != ServerInstance->Config->maxbans.end(); n++)
868 if (match(this->name,x.c_str()))
877 /* returns the status character for a given user on a channel, e.g. @ for op,
878 * % for halfop etc. If the user has several modes set, the highest mode
879 * the user has must be returned. */
881 const char* chanrec::GetStatusChar(userrec *user)
883 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
885 if ((*i)->channel == this)
887 if (((*i)->uc_modes & UCMODE_OP) > 0)
891 if (((*i)->uc_modes & UCMODE_HOP) > 0)
895 if (((*i)->uc_modes & UCMODE_VOICE) > 0)
906 int chanrec::GetStatusFlags(userrec *user)
908 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
910 if ((*i)->channel == this)
912 return (*i)->uc_modes;
920 int chanrec::GetStatus(userrec *user)
922 if (ServerInstance->ULine(user->server))
925 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
927 if ((*i)->channel == this)
929 if (((*i)->uc_modes & UCMODE_OP) > 0)
933 if (((*i)->uc_modes & UCMODE_HOP) > 0)
937 if (((*i)->uc_modes & UCMODE_VOICE) > 0)
941 return STATUS_NORMAL;
944 return STATUS_NORMAL;