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"
38 #include "helperfuncs.h"
42 extern std::vector<Module*> modules;
43 extern std::vector<ircd_module*> factory;
46 chanrec::chanrec(InspIRCd* Instance) : ServerInstance(Instance)
48 *name = *topic = *setby = *key = 0;
49 created = topicset = limit = 0;
50 internal_userlist.clear();
54 void chanrec::SetMode(char mode,bool mode_on)
56 modes[mode-65] = mode_on;
58 this->SetModeParam(mode,"",false);
62 void chanrec::SetModeParam(char mode,const char* parameter,bool mode_on)
64 log(DEBUG,"SetModeParam called");
66 CustomModeList::iterator n = custom_mode_params.find(mode);
70 if (n == custom_mode_params.end())
72 custom_mode_params[mode] = strdup(parameter);
73 log(DEBUG,"Custom mode parameter %c %s added",mode,parameter);
77 log(DEBUG, "Tried to set custom mode parameter for %c '%s' when it was already '%s'", mode, parameter, n->second);
82 if (n != custom_mode_params.end())
85 custom_mode_params.erase(n);
90 bool chanrec::IsModeSet(char mode)
92 return modes[mode-65];
95 std::string chanrec::GetModeParameter(char mode)
101 else if (mode == 'l')
103 return ConvToStr(this->limit);
107 CustomModeList::iterator n = custom_mode_params.find(mode);
108 if (n != custom_mode_params.end())
116 long chanrec::GetUserCounter()
118 return (this->internal_userlist.size());
121 void chanrec::AddUser(userrec* user)
123 internal_userlist[user] = user;
126 unsigned long chanrec::DelUser(userrec* user)
128 CUListIter a = internal_userlist.find(user);
130 if (a != internal_userlist.end())
132 internal_userlist.erase(a);
133 /* And tidy any others... */
135 DelHalfoppedUser(user);
139 return internal_userlist.size();
142 bool chanrec::HasUser(userrec* user)
144 return (internal_userlist.find(user) != internal_userlist.end());
147 void chanrec::AddOppedUser(userrec* user)
149 internal_op_userlist[user] = user;
152 void chanrec::DelOppedUser(userrec* user)
154 CUListIter a = internal_op_userlist.find(user);
155 if (a != internal_op_userlist.end())
157 internal_op_userlist.erase(a);
162 void chanrec::AddHalfoppedUser(userrec* user)
164 internal_halfop_userlist[user] = user;
167 void chanrec::DelHalfoppedUser(userrec* user)
169 CUListIter a = internal_halfop_userlist.find(user);
171 if (a != internal_halfop_userlist.end())
173 internal_halfop_userlist.erase(a);
177 void chanrec::AddVoicedUser(userrec* user)
179 internal_voice_userlist[user] = user;
182 void chanrec::DelVoicedUser(userrec* user)
184 CUListIter a = internal_voice_userlist.find(user);
186 if (a != internal_voice_userlist.end())
188 internal_voice_userlist.erase(a);
192 CUList* chanrec::GetUsers()
194 return &internal_userlist;
197 CUList* chanrec::GetOppedUsers()
199 return &internal_op_userlist;
202 CUList* chanrec::GetHalfoppedUsers()
204 return &internal_halfop_userlist;
207 CUList* chanrec::GetVoicedUsers()
209 return &internal_voice_userlist;
213 * add a channel to a user, creating the record for it if needed and linking
214 * it to the user record
216 chanrec* chanrec::JoinUser(InspIRCd* Instance, userrec *user, const char* cn, bool override, const char* key)
224 strlcpy(cname,cn,CHANMAX);
226 chanrec* Ptr = Instance->FindChan(cname);
233 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,NULL,cname));
238 /* create a new one */
239 Ptr = new chanrec(Instance);
240 Instance->chanlist[cname] = Ptr;
242 strlcpy(Ptr->name, cname,CHANMAX);
243 Ptr->modes[CM_TOPICLOCK] = Ptr->modes[CM_NOEXTERNAL] = 1;
246 strlcpy(Ptr->setby, user->nick,NICKMAX-1);
248 log(DEBUG,"chanrec::JoinUser(): created: %s",cname);
250 * set created to 2 to indicate user
251 * is the first in the channel
252 * and should be given ops
258 /* Already on the channel */
259 if (Ptr->HasUser(user))
263 * remote users are allowed us to bypass channel modes
264 * and bans (used by servers)
266 if (IS_LOCAL(user)) /* was a check on fd > -1 */
269 FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname));
274 else if (MOD_RESULT == 0)
279 FOREACH_RESULT_I(Instance,I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
284 log(DEBUG,"chanrec::JoinUser(): no key given in JOIN");
285 user->WriteServ("475 %s %s :Cannot join channel (Requires key)",user->nick, Ptr->name);
290 if (strcmp(key,Ptr->key))
292 log(DEBUG,"chanrec::JoinUser(): bad key given in JOIN");
293 user->WriteServ("475 %s %s :Cannot join channel (Incorrect key)",user->nick, Ptr->name);
299 if (Ptr->modes[CM_INVITEONLY])
302 irc::string xname(Ptr->name);
303 FOREACH_RESULT_I(Instance,I_OnCheckInvite,OnCheckInvite(user, Ptr));
306 if (user->IsInvited(xname))
308 /* user was invited to channel */
309 /* there may be an optional channel NOTICE here */
313 user->WriteServ("473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
317 user->RemoveInvite(xname);
322 FOREACH_RESULT_I(Instance,I_OnCheckLimit,OnCheckLimit(user, Ptr));
325 if (Ptr->GetUserCounter() >= Ptr->limit)
327 user->WriteServ("471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
332 if (Ptr->bans.size())
335 FOREACH_RESULT_I(Instance,I_OnCheckBan,OnCheckBan(user, Ptr));
337 sprintf(mask,"%s!%s@%s",user->nick, user->ident, user->GetIPString());
340 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
342 /* This allows CIDR ban matching
344 * Full masked host Full unmasked host IP with/without CIDR
346 if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match(mask, i->data, true)))
348 user->WriteServ("474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
358 log(DEBUG,"chanrec::JoinUser(): Overridden checks");
363 for (UserChanList::const_iterator index = user->chans.begin(); index != user->chans.end(); index++)
365 if ((*index)->channel == NULL)
367 return chanrec::ForceChan(Instance, Ptr, *index, user, created);
372 * XXX: If the user is an oper here, we can just extend their user->chans vector by one
373 * and put the channel in here. Same for remote users which are not bound by
374 * the channel limits. Otherwise, nope, youre boned.
376 if (!IS_LOCAL(user)) /* was a check on fd < 0 */
378 ucrec* a = new ucrec();
379 chanrec* c = chanrec::ForceChan(Instance, Ptr,a,user,created);
380 user->chans.push_back(a);
383 else if (*user->oper)
385 /* Oper allows extension up to the OPERMAXCHANS value */
386 if (user->chans.size() < OPERMAXCHANS)
388 ucrec* a = new ucrec();
389 chanrec* c = chanrec::ForceChan(Instance, Ptr,a,user,created);
390 user->chans.push_back(a);
395 user->WriteServ("405 %s %s :You are on too many channels",user->nick, cname);
399 log(DEBUG,"BLAMMO, Whacking channel.");
400 /* Things went seriously pear shaped, so take this away. bwahaha. */
401 chan_hash::iterator n = Instance->chanlist.find(cname);
402 if (n != Instance->chanlist.end())
406 Instance->chanlist.erase(n);
407 for (unsigned int index =0; index < user->chans.size(); index++)
409 if (user->chans[index]->channel == Ptr)
411 user->chans[index]->channel = NULL;
412 user->chans[index]->uc_modes = 0;
419 for (unsigned int index =0; index < user->chans.size(); index++)
421 if (user->chans[index]->channel == Ptr)
423 user->chans[index]->channel = NULL;
424 user->chans[index]->uc_modes = 0;
431 chanrec* chanrec::ForceChan(InspIRCd* Instance, chanrec* Ptr,ucrec *a,userrec* user, int created)
435 /* first user in is given ops */
436 a->uc_modes = UCMODE_OP;
437 Ptr->AddOppedUser(user);
446 Ptr->WriteChannel(user,"JOIN :%s",Ptr->name);
448 /* Major improvement by Brain - we dont need to be calculating all this pointlessly for remote users */
451 log(DEBUG,"Sent JOIN to client");
454 user->WriteServ("332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
455 user->WriteServ("333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
458 user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
460 FOREACH_MOD_I(Instance,I_OnUserJoin,OnUserJoin(user,Ptr));
465 * remove a channel from a users record, and remove the record from the hash
466 * if the channel has become empty
468 long chanrec::PartUser(userrec *user, const char* reason)
471 return this->GetUserCounter();
473 for (unsigned int i =0; i < user->chans.size(); i++)
475 /* zap it from the channel list of the user */
476 if (user->chans[i]->channel == this)
480 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, reason));
481 this->WriteChannel(user, "PART %s :%s", this->name, reason);
485 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, ""));
486 this->WriteChannel(user, "PART :%s", this->name);
488 user->chans[i]->uc_modes = 0;
489 user->chans[i]->channel = NULL;
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 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;
540 if (!this->DelUser(user))
542 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
543 /* kill the record */
544 if (iter != ServerInstance->chanlist.end())
546 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
547 ServerInstance->chanlist.erase(iter);
552 return this->GetUserCounter();
555 long chanrec::KickUser(userrec *src, userrec *user, const char* reason)
557 if (!src || !user || !reason)
558 return this->GetUserCounter();
562 if (!this->HasUser(user))
564 src->WriteServ("441 %s %s %s :They are not on that channel",src->nick, user->nick, this->name);
565 return this->GetUserCounter();
567 if ((is_uline(user->server)) && (!is_uline(src->server)))
569 src->WriteServ("482 %s %s :Only a u-line may kick a u-line from a channel.",src->nick, this->name);
570 return this->GetUserCounter();
574 if (!is_uline(src->server))
577 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,this,reason));
579 return this->GetUserCounter();
581 /* Set to -1 by OnUserPreKick if explicit allow was set */
582 if (MOD_RESULT != -1)
584 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,this,AC_KICK));
585 if ((MOD_RESULT == ACR_DENY) && (!is_uline(src->server)))
586 return this->GetUserCounter();
588 if ((MOD_RESULT == ACR_DEFAULT) || (!is_uline(src->server)))
590 int them = this->GetStatus(src);
591 int us = this->GetStatus(user);
592 if ((them < STATUS_HOP) || (them < us))
594 if (them == STATUS_HOP)
596 src->WriteServ("482 %s %s :You must be a channel operator",src->nick, this->name);
600 src->WriteServ("482 %s %s :You must be at least a half-operator",src->nick, this->name);
602 return this->GetUserCounter();
608 FOREACH_MOD(I_OnUserKick,OnUserKick(src,user,this,reason));
610 for (UserChanList::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
612 /* zap it from the channel list of the user */
613 if ((*i)->channel == this)
615 this->WriteChannel(src, "KICK %s %s :%s", this->name, user->nick, reason);
617 (*i)->channel = NULL;
622 if (!this->DelUser(user))
623 /* if there are no users left on the channel */
625 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
627 /* kill the record */
628 if (iter != ServerInstance->chanlist.end())
630 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
631 ServerInstance->chanlist.erase(iter);
636 return this->GetUserCounter();
639 void chanrec::WriteChannel(userrec* user, char* text, ...)
641 char textbuffer[MAXBUF];
647 va_start(argsPtr, text);
648 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
651 this->WriteChannel(user, std::string(textbuffer));
654 void chanrec::WriteChannel(userrec* user, const std::string &text)
656 CUList *ulist = this->GetUsers();
661 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
663 if (i->second->fd != FD_MAGIC_NUMBER)
664 user->WriteTo(i->second,text);
668 void chanrec::WriteChannelWithServ(const char* ServName, const char* text, ...)
670 char textbuffer[MAXBUF];
676 va_start(argsPtr, text);
677 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
680 this->WriteChannelWithServ(ServName, std::string(textbuffer));
683 void chanrec::WriteChannelWithServ(const char* ServName, const std::string &text)
685 CUList *ulist = this->GetUsers();
687 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
689 if (IS_LOCAL(i->second))
690 i->second->WriteServ(text);
694 /* write formatted text from a source user to all users on a channel except
695 * for the sender (for privmsg etc) */
696 void chanrec::WriteAllExceptSender(userrec* user, char status, char* text, ...)
698 char textbuffer[MAXBUF];
704 va_start(argsPtr, text);
705 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
708 this->WriteAllExceptSender(user, status, std::string(textbuffer));
711 void chanrec::WriteAllExceptSender(userrec* user, char status, const std::string& text)
721 ulist = this->GetOppedUsers();
724 ulist = this->GetHalfoppedUsers();
727 ulist = this->GetVoicedUsers();
730 ulist = this->GetUsers();
734 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
736 if ((IS_LOCAL(i->second)) && (user != i->second))
737 i->second->WriteFrom(user,text);
742 * return a count of the users on a specific channel accounting for
743 * invisible users who won't increase the count. e.g. for /LIST
745 int chanrec::CountInvisible()
748 CUList *ulist= this->GetUsers();
749 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
751 if (!(i->second->modes[UM_INVISIBLE]))
758 char* chanrec::ChanModes(bool showkey)
760 static char scratch[MAXBUF];
761 static char sparam[MAXBUF];
762 char* offset = scratch;
763 std::string extparam = "";
768 /* This was still iterating up to 190, chanrec::custom_modes is only 64 elements -- Om */
769 for(int n = 0; n < 64; n++)
778 extparam = (showkey ? this->key : "<key>");
781 extparam = ConvToStr(this->limit);
789 /* We know these have no parameters */
792 extparam = this->GetModeParameter(n + 65);
797 charlcat(sparam,' ',MAXBUF);
798 strlcat(sparam,extparam.c_str(),MAXBUF);
803 /* Null terminate scratch */
805 strlcat(scratch,sparam,MAXBUF);
809 /* compile a userlist of a channel into a string, each nick seperated by
810 * spaces and op, voice etc status shown as @ and +, and send it to 'user'
812 void chanrec::UserList(userrec *user)
817 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
820 char* ptr = list + dlen;
822 CUList *ulist= this->GetUsers();
824 /* Improvement by Brain - this doesnt change in value, so why was it inside
827 bool has_user = this->HasUser(user);
829 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
831 if ((!has_user) && (i->second->modes[UM_INVISIBLE]))
834 * user is +i, and source not on the channel, does not show
840 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", this->GetStatusChar(i->second), i->second->nick);
847 if (curlen > (480-NICKMAX))
849 /* list overflowed into multiple numerics */
850 user->WriteServ(list);
852 /* reset our lengths */
853 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
861 /* if whats left in the list isnt empty, send it */
864 user->WriteServ(list);
868 long chanrec::GetMaxBans()
871 for (std::map<std::string,int>::iterator n = ServerInstance->Config->maxbans.begin(); n != ServerInstance->Config->maxbans.end(); n++)
874 if (match(this->name,x.c_str()))
883 /* returns the status character for a given user on a channel, e.g. @ for op,
884 * % for halfop etc. If the user has several modes set, the highest mode
885 * the user has must be returned. */
887 const char* chanrec::GetStatusChar(userrec *user)
889 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
891 if ((*i)->channel == this)
893 if (((*i)->uc_modes & UCMODE_OP) > 0)
897 if (((*i)->uc_modes & UCMODE_HOP) > 0)
901 if (((*i)->uc_modes & UCMODE_VOICE) > 0)
912 int chanrec::GetStatusFlags(userrec *user)
914 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
916 if ((*i)->channel == this)
918 return (*i)->uc_modes;
926 int chanrec::GetStatus(userrec *user)
928 if (is_uline(user->server))
931 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
933 if ((*i)->channel == this)
935 if (((*i)->uc_modes & UCMODE_OP) > 0)
939 if (((*i)->uc_modes & UCMODE_HOP) > 0)
943 if (((*i)->uc_modes & UCMODE_VOICE) > 0)
947 return STATUS_NORMAL;
950 return STATUS_NORMAL;