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"
38 #include "inspstring.h"
39 #include "helperfuncs.h"
42 extern ServerConfig* Config;
45 extern std::vector<Module*> modules;
46 extern std::vector<ircd_module*> factory;
48 extern chan_hash chanlist;
52 *name = *topic = *setby = *key = 0;
53 created = topicset = limit = 0;
54 internal_userlist.clear();
58 void chanrec::SetMode(char mode,bool mode_on)
60 modes[mode-65] = mode_on;
62 this->SetModeParam(mode,"",false);
66 void chanrec::SetModeParam(char mode,const char* parameter,bool mode_on)
68 log(DEBUG,"SetModeParam called");
70 CustomModeList::iterator n = custom_mode_params.find(mode);
74 if (n == custom_mode_params.end())
76 custom_mode_params[mode] = strdup(parameter);
77 log(DEBUG,"Custom mode parameter %c %s added",mode,parameter);
81 log(DEBUG, "Tried to set custom mode parameter for %c '%s' when it was already '%s'", mode, parameter, n->second);
86 if (n != custom_mode_params.end())
89 custom_mode_params.erase(n);
94 bool chanrec::IsModeSet(char mode)
96 return modes[mode-65];
99 std::string chanrec::GetModeParameter(char mode)
105 else if (mode == 'l')
107 return ConvToStr(this->limit);
111 CustomModeList::iterator n = custom_mode_params.find(mode);
112 if (n != custom_mode_params.end())
120 long chanrec::GetUserCounter()
122 return (this->internal_userlist.size());
125 void chanrec::AddUser(userrec* user)
127 internal_userlist[user] = user;
130 unsigned long chanrec::DelUser(userrec* user)
132 CUListIter a = internal_userlist.find(user);
134 if (a != internal_userlist.end())
136 internal_userlist.erase(a);
137 /* And tidy any others... */
139 DelHalfoppedUser(user);
143 return internal_userlist.size();
146 bool chanrec::HasUser(userrec* user)
148 return (internal_userlist.find(user) != internal_userlist.end());
151 void chanrec::AddOppedUser(userrec* user)
153 internal_op_userlist[user] = user;
156 void chanrec::DelOppedUser(userrec* user)
158 CUListIter a = internal_op_userlist.find(user);
159 if (a != internal_op_userlist.end())
161 internal_op_userlist.erase(a);
166 void chanrec::AddHalfoppedUser(userrec* user)
168 internal_halfop_userlist[user] = user;
171 void chanrec::DelHalfoppedUser(userrec* user)
173 CUListIter a = internal_halfop_userlist.find(user);
175 if (a != internal_halfop_userlist.end())
177 internal_halfop_userlist.erase(a);
181 void chanrec::AddVoicedUser(userrec* user)
183 internal_voice_userlist[user] = user;
186 void chanrec::DelVoicedUser(userrec* user)
188 CUListIter a = internal_voice_userlist.find(user);
190 if (a != internal_voice_userlist.end())
192 internal_voice_userlist.erase(a);
196 CUList* chanrec::GetUsers()
198 return &internal_userlist;
201 CUList* chanrec::GetOppedUsers()
203 return &internal_op_userlist;
206 CUList* chanrec::GetHalfoppedUsers()
208 return &internal_halfop_userlist;
211 CUList* chanrec::GetVoicedUsers()
213 return &internal_voice_userlist;
217 * add a channel to a user, creating the record for it if needed and linking
218 * it to the user record
220 chanrec* chanrec::JoinUser(userrec *user, const char* cn, bool override, const char* key)
228 strlcpy(cname,cn,CHANMAX);
230 chanrec* Ptr = FindChan(cname);
237 FOREACH_RESULT(I_OnUserPreJoin,OnUserPreJoin(user,NULL,cname));
242 /* create a new one */
243 chanlist[cname] = new chanrec();
244 strlcpy(chanlist[cname]->name, cname,CHANMAX);
245 chanlist[cname]->modes[CM_TOPICLOCK] = chanlist[cname]->modes[CM_NOEXTERNAL] = 1;
246 //chanlist[cname]->binarymodes = CM_TOPICLOCK | CM_NOEXTERNAL;
247 chanlist[cname]->created = TIME;
248 *chanlist[cname]->topic = 0;
249 strlcpy(chanlist[cname]->setby, user->nick,NICKMAX-1);
250 chanlist[cname]->topicset = 0;
251 Ptr = chanlist[cname];
252 log(DEBUG,"chanrec::JoinUser(): created: %s",cname);
254 * set created to 2 to indicate user
255 * is the first in the channel
256 * and should be given ops
262 /* Already on the channel */
263 if (Ptr->HasUser(user))
267 * remote users are allowed us to bypass channel modes
268 * and bans (used by servers)
270 if (IS_LOCAL(user)) /* was a check on fd > -1 */
273 FOREACH_RESULT(I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname));
278 else if (MOD_RESULT == 0)
283 FOREACH_RESULT(I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
288 log(DEBUG,"chanrec::JoinUser(): no key given in JOIN");
289 WriteServ(user->fd,"475 %s %s :Cannot join channel (Requires key)",user->nick, Ptr->name);
294 if (strcmp(key,Ptr->key))
296 log(DEBUG,"chanrec::JoinUser(): bad key given in JOIN");
297 WriteServ(user->fd,"475 %s %s :Cannot join channel (Incorrect key)",user->nick, Ptr->name);
303 if (Ptr->modes[CM_INVITEONLY])
306 irc::string xname(Ptr->name);
307 FOREACH_RESULT(I_OnCheckInvite,OnCheckInvite(user, Ptr));
310 if (user->IsInvited(xname))
312 /* user was invited to channel */
313 /* there may be an optional channel NOTICE here */
317 WriteServ(user->fd,"473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
321 user->RemoveInvite(xname);
326 FOREACH_RESULT(I_OnCheckLimit,OnCheckLimit(user, Ptr));
329 if (usercount(Ptr) >= Ptr->limit)
331 WriteServ(user->fd,"471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
336 if (Ptr->bans.size())
339 FOREACH_RESULT(I_OnCheckBan,OnCheckBan(user, Ptr));
341 sprintf(mask,"%s!%s@%s",user->nick, user->ident, user->GetIPString());
344 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
346 /* This allows CIDR ban matching
348 * Full masked host Full unmasked host IP with/without CIDR
350 if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match(mask, i->data, true)))
352 WriteServ(user->fd,"474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
362 log(DEBUG,"chanrec::JoinUser(): Overridden checks");
367 for (UserChanList::const_iterator index = user->chans.begin(); index != user->chans.end(); index++)
369 if ((*index)->channel == NULL)
371 return chanrec::ForceChan(Ptr, *index, user, created);
376 * XXX: If the user is an oper here, we can just extend their user->chans vector by one
377 * and put the channel in here. Same for remote users which are not bound by
378 * the channel limits. Otherwise, nope, youre boned.
380 if (!IS_LOCAL(user)) /* was a check on fd < 0 */
382 ucrec* a = new ucrec();
383 chanrec* c = chanrec::ForceChan(Ptr,a,user,created);
384 user->chans.push_back(a);
387 else if (*user->oper)
389 /* Oper allows extension up to the OPERMAXCHANS value */
390 if (user->chans.size() < OPERMAXCHANS)
392 ucrec* a = new ucrec();
393 chanrec* c = chanrec::ForceChan(Ptr,a,user,created);
394 user->chans.push_back(a);
399 WriteServ(user->fd,"405 %s %s :You are on too many channels",user->nick, cname);
403 log(DEBUG,"BLAMMO, Whacking channel.");
404 /* Things went seriously pear shaped, so take this away. bwahaha. */
405 chan_hash::iterator n = chanlist.find(cname);
406 if (n != chanlist.end())
411 for (unsigned int index =0; index < user->chans.size(); index++)
413 if (user->chans[index]->channel == Ptr)
415 user->chans[index]->channel = NULL;
416 user->chans[index]->uc_modes = 0;
423 for (unsigned int index =0; index < user->chans.size(); index++)
425 if (user->chans[index]->channel == Ptr)
427 user->chans[index]->channel = NULL;
428 user->chans[index]->uc_modes = 0;
435 chanrec* chanrec::ForceChan(chanrec* Ptr,ucrec *a,userrec* user, int created)
439 /* first user in is given ops */
440 a->uc_modes = UCMODE_OP;
441 Ptr->AddOppedUser(user);
450 Ptr->WriteChannel(user,"JOIN :%s",Ptr->name);
452 /* Major improvement by Brain - we dont need to be calculating all this pointlessly for remote users */
455 log(DEBUG,"Sent JOIN to client");
458 WriteServ(user->fd,"332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
459 WriteServ(user->fd,"333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
462 WriteServ(user->fd,"366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
464 FOREACH_MOD(I_OnUserJoin,OnUserJoin(user,Ptr));
469 * remove a channel from a users record, and remove the record from the hash
470 * if the channel has become empty
472 long chanrec::PartUser(userrec *user, const char* reason)
475 return this->GetUserCounter();
477 for (unsigned int i =0; i < user->chans.size(); i++)
479 /* zap it from the channel list of the user */
480 if (user->chans[i]->channel == this)
484 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, reason));
485 this->WriteChannel(user, "PART %s :%s", this->name, reason);
489 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, ""));
490 this->WriteChannel(user, "PART :%s", this->name);
492 user->chans[i]->uc_modes = 0;
493 user->chans[i]->channel = NULL;
498 if (!this->DelUser(user)) /* if there are no users left on the channel... */
500 chan_hash::iterator iter = chanlist.find(this->name);
501 /* kill the record */
502 if (iter != chanlist.end())
504 log(DEBUG,"del_channel: destroyed: %s", this->name);
505 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
506 chanlist.erase(iter);
511 return this->GetUserCounter();
514 long chanrec::ServerKickUser(userrec* user, const char* reason, bool triggerevents)
516 if (!user || !reason)
517 return this->GetUserCounter();
521 if (!this->HasUser(user))
524 return this->GetUserCounter();
530 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL,user,this,reason));
533 for (unsigned int i =0; i < user->chans.size(); i++)
535 if (user->chans[i]->channel == this)
537 this->WriteChannelWithServ(Config->ServerName, "KICK %s %s :%s", this->name, user->nick, reason);
538 user->chans[i]->uc_modes = 0;
539 user->chans[i]->channel = NULL;
544 if (!this->DelUser(user))
546 chan_hash::iterator iter = chanlist.find(this->name);
547 /* kill the record */
548 if (iter != chanlist.end())
550 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
551 chanlist.erase(iter);
556 return this->GetUserCounter();
559 long chanrec::KickUser(userrec *src, userrec *user, const char* reason)
561 if (!src || !user || !reason)
562 return this->GetUserCounter();
566 if (!this->HasUser(user))
568 WriteServ(src->fd,"441 %s %s %s :They are not on that channel",src->nick, user->nick, this->name);
569 return this->GetUserCounter();
571 if ((is_uline(user->server)) && (!is_uline(src->server)))
573 WriteServ(src->fd,"482 %s %s :Only a u-line may kick a u-line from a channel.",src->nick, this->name);
574 return this->GetUserCounter();
578 if (!is_uline(src->server))
581 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,this,reason));
583 return this->GetUserCounter();
585 /* Set to -1 by OnUserPreKick if explicit allow was set */
586 if (MOD_RESULT != -1)
588 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,this,AC_KICK));
589 if ((MOD_RESULT == ACR_DENY) && (!is_uline(src->server)))
590 return this->GetUserCounter();
592 if ((MOD_RESULT == ACR_DEFAULT) || (!is_uline(src->server)))
594 int them = cstatus(src, this);
595 int us = cstatus(user, this);
596 if ((them < STATUS_HOP) || (them < us))
598 if (them == STATUS_HOP)
600 WriteServ(src->fd,"482 %s %s :You must be a channel operator",src->nick, this->name);
604 WriteServ(src->fd,"482 %s %s :You must be at least a half-operator",src->nick, this->name);
606 return this->GetUserCounter();
612 FOREACH_MOD(I_OnUserKick,OnUserKick(src,user,this,reason));
614 for (UserChanList::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
616 /* zap it from the channel list of the user */
617 if ((*i)->channel == this)
619 this->WriteChannel(src, "KICK %s %s :%s", this->name, user->nick, reason);
621 (*i)->channel = NULL;
626 if (!this->DelUser(user))
627 /* if there are no users left on the channel */
629 chan_hash::iterator iter = chanlist.find(this->name);
631 /* kill the record */
632 if (iter != chanlist.end())
634 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
635 chanlist.erase(iter);
640 return this->GetUserCounter();
643 void chanrec::WriteChannel(userrec* user, char* text, ...)
645 char textbuffer[MAXBUF];
651 va_start(argsPtr, text);
652 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
655 this->WriteChannel(user, std::string(textbuffer));
658 void chanrec::WriteChannel(userrec* user, const std::string &text)
660 CUList *ulist = this->GetUsers();
665 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
667 if (i->second->fd != FD_MAGIC_NUMBER)
668 WriteTo_NoFormat(user,i->second,text.c_str());
672 void chanrec::WriteChannelWithServ(const char* ServName, const char* text, ...)
674 char textbuffer[MAXBUF];
680 va_start(argsPtr, text);
681 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
684 this->WriteChannelWithServ(ServName, std::string(textbuffer));
687 void chanrec::WriteChannelWithServ(const char* ServName, const std::string &text)
689 CUList *ulist = this->GetUsers();
691 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
693 if (IS_LOCAL(i->second))
694 WriteServ_NoFormat(i->second->fd,text.c_str());
698 /* write formatted text from a source user to all users on a channel except
699 * for the sender (for privmsg etc) */
700 void chanrec::WriteAllExceptSender(userrec* user, char status, char* text, ...)
702 char textbuffer[MAXBUF];
708 va_start(argsPtr, text);
709 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
712 this->WriteAllExceptSender(user, status, std::string(textbuffer));
715 void chanrec::WriteAllExceptSender(userrec* user, char status, const std::string& text)
725 ulist = this->GetOppedUsers();
728 ulist = this->GetHalfoppedUsers();
731 ulist = this->GetVoicedUsers();
734 ulist = this->GetUsers();
738 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
740 if ((IS_LOCAL(i->second)) && (user != i->second))
741 WriteFrom_NoFormat(i->second->fd,user,text.c_str());