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 * ---------------------------------------------------
24 #include "configreader.h"
37 #include "inspstring.h"
38 #include "helperfuncs.h"
41 extern ServerConfig* Config;
44 extern std::vector<Module*> modules;
45 extern std::vector<ircd_module*> factory;
47 extern chan_hash chanlist;
50 chanrec* ForceChan(chanrec* Ptr,ucrec *a,userrec* user, int created);
54 *name = *topic = *setby = *key = 0;
55 created = topicset = limit = 0;
56 internal_userlist.clear();
60 void chanrec::SetCustomMode(char mode,bool mode_on)
62 modes[mode-65] = mode_on;
64 this->SetCustomModeParam(mode,"",false);
68 void chanrec::SetCustomModeParam(char mode,char* parameter,bool mode_on)
70 log(DEBUG,"SetCustomModeParam called");
72 CustomModeList::iterator n = custom_mode_params.find(mode);
76 if (n == custom_mode_params.end())
78 custom_mode_params[mode] = strdup(parameter);
79 log(DEBUG,"Custom mode parameter %c %s added",mode,parameter);
83 log(DEBUG, "Tried to set custom mode parameter for %c '%s' when it was already '%s'", mode, parameter, n->second);
88 if (n != custom_mode_params.end())
91 custom_mode_params.erase(n);
96 bool chanrec::IsModeSet(char mode)
98 return modes[mode-65];
101 std::string chanrec::GetModeParameter(char mode)
107 else if (mode == 'l')
109 return ConvToStr(this->limit);
113 CustomModeList::iterator n = custom_mode_params.find(mode);
114 if (n != custom_mode_params.end())
122 long chanrec::GetUserCounter()
124 return (this->internal_userlist.size());
127 void chanrec::AddUser(userrec* user)
129 internal_userlist[user] = user;
132 unsigned long chanrec::DelUser(userrec* user)
134 CUList::iterator a = internal_userlist.find(user);
135 if (a != internal_userlist.end())
137 internal_userlist.erase(a);
138 /* And tidy any others... */
140 DelHalfoppedUser(user);
142 return internal_userlist.size();
144 return internal_userlist.size();
147 bool chanrec::HasUser(userrec* user)
149 return (internal_userlist.find(user) != internal_userlist.end());
152 void chanrec::AddOppedUser(userrec* user)
154 internal_op_userlist[user] = user;
157 void chanrec::DelOppedUser(userrec* user)
159 CUList::iterator a = internal_op_userlist.find(user);
160 if (a != internal_op_userlist.end())
162 internal_op_userlist.erase(a);
167 void chanrec::AddHalfoppedUser(userrec* user)
169 internal_halfop_userlist[user] = user;
172 void chanrec::DelHalfoppedUser(userrec* user)
174 CUList::iterator a = internal_halfop_userlist.find(user);
175 if (a != internal_halfop_userlist.end())
177 internal_halfop_userlist.erase(a);
182 void chanrec::AddVoicedUser(userrec* user)
184 internal_voice_userlist[user] = user;
187 void chanrec::DelVoicedUser(userrec* user)
189 CUList::iterator a = internal_voice_userlist.find(user);
190 if (a != internal_voice_userlist.end())
192 internal_voice_userlist.erase(a);
197 CUList* chanrec::GetUsers()
199 return &internal_userlist;
202 CUList* chanrec::GetOppedUsers()
204 return &internal_op_userlist;
207 CUList* chanrec::GetHalfoppedUsers()
209 return &internal_halfop_userlist;
212 CUList* chanrec::GetVoicedUsers()
214 return &internal_voice_userlist;
218 * add a channel to a user, creating the record for it if needed and linking
219 * it to the user record
222 chanrec* add_channel(userrec *user, const char* cn, const char* key, bool override)
224 if ((!user) || (!cn))
226 log(DEFAULT,"*** BUG *** add_channel was given an invalid parameter");
233 strlcpy(cname,cn,CHANMAX);
234 log(DEBUG,"cname='%s' cn='%s'",cname,cn);
236 log(DEBUG,"add_channel: %s %s",user->nick,cname);
238 chanrec* Ptr = FindChan(cname);
245 FOREACH_RESULT(I_OnUserPreJoin,OnUserPreJoin(user,NULL,cname));
250 /* create a new one */
251 chanlist[cname] = new chanrec();
252 strlcpy(chanlist[cname]->name, cname,CHANMAX);
253 chanlist[cname]->modes[CM_TOPICLOCK] = chanlist[cname]->modes[CM_NOEXTERNAL] = 1;
254 //chanlist[cname]->binarymodes = CM_TOPICLOCK | CM_NOEXTERNAL;
255 chanlist[cname]->created = TIME;
256 *chanlist[cname]->topic = 0;
257 strlcpy(chanlist[cname]->setby, user->nick,NICKMAX-1);
258 chanlist[cname]->topicset = 0;
259 Ptr = chanlist[cname];
260 log(DEBUG,"add_channel: created: %s",cname);
262 * set created to 2 to indicate user
263 * is the first in the channel
264 * and should be given ops
270 /* Already on the channel */
271 if (Ptr->HasUser(user))
275 * remote users are allowed us to bypass channel modes
276 * and bans (used by servers)
278 if (IS_LOCAL(user)) /* was a check on fd > -1 */
281 FOREACH_RESULT(I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname));
286 else if (MOD_RESULT == 0)
291 FOREACH_RESULT(I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
296 log(DEBUG,"add_channel: no key given in JOIN");
297 WriteServ(user->fd,"475 %s %s :Cannot join channel (Requires key)",user->nick, Ptr->name);
302 if (strcmp(key,Ptr->key))
304 log(DEBUG,"add_channel: bad key given in JOIN");
305 WriteServ(user->fd,"475 %s %s :Cannot join channel (Incorrect key)",user->nick, Ptr->name);
311 if (Ptr->modes[CM_INVITEONLY])
314 irc::string xname(Ptr->name);
315 FOREACH_RESULT(I_OnCheckInvite,OnCheckInvite(user, Ptr));
318 log(DEBUG,"add_channel: channel is +i");
319 if (user->IsInvited(xname))
321 /* user was invited to channel */
322 /* there may be an optional channel NOTICE here */
326 WriteServ(user->fd,"473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
330 user->RemoveInvite(xname);
335 FOREACH_RESULT(I_OnCheckLimit,OnCheckLimit(user, Ptr));
338 if (usercount(Ptr) >= Ptr->limit)
340 WriteServ(user->fd,"471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
345 if (Ptr->bans.size())
347 log(DEBUG,"add_channel: about to walk banlist");
349 FOREACH_RESULT(I_OnCheckBan,OnCheckBan(user, Ptr));
352 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
354 if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match((char*)inet_ntoa(user->ip4),i->data)))
356 WriteServ(user->fd,"474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
366 log(DEBUG,"Overridden checks");
371 log(DEBUG,"Passed channel checks");
373 for (std::vector<ucrec*>::const_iterator index = user->chans.begin(); index != user->chans.end(); index++)
375 if ((*index)->channel == NULL)
377 return ForceChan(Ptr, *index, user, created);
382 * XXX: If the user is an oper here, we can just extend their user->chans vector by one
383 * and put the channel in here. Same for remote users which are not bound by
384 * the channel limits. Otherwise, nope, youre boned.
386 if (!IS_LOCAL(user)) /* was a check on fd < 0 */
388 ucrec* a = new ucrec();
389 chanrec* c = ForceChan(Ptr,a,user,created);
390 user->chans.push_back(a);
393 else if (*user->oper)
395 /* Oper allows extension up to the OPERMAXCHANS value */
396 if (user->chans.size() < OPERMAXCHANS)
398 ucrec* a = new ucrec();
399 chanrec* c = ForceChan(Ptr,a,user,created);
400 user->chans.push_back(a);
405 log(DEBUG,"add_channel: user channel max exceeded: %s %s",user->nick,cname);
406 WriteServ(user->fd,"405 %s %s :You are on too many channels",user->nick, cname);
410 log(DEBUG,"BLAMMO, Whacking channel.");
411 /* Things went seriously pear shaped, so take this away. bwahaha. */
412 chan_hash::iterator n = chanlist.find(cname);
413 if (n != chanlist.end())
418 for (unsigned int index =0; index < user->chans.size(); index++)
420 if (user->chans[index]->channel == Ptr)
422 user->chans[index]->channel = NULL;
423 user->chans[index]->uc_modes = 0;
431 chanrec* ForceChan(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 WriteChannel(Ptr,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 WriteServ(user->fd,"332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
455 WriteServ(user->fd,"333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
458 WriteServ(user->fd,"366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
460 FOREACH_MOD(I_OnUserJoin,OnUserJoin(user,Ptr));
465 *remove a channel from a users record, and remove the record from memory
466 * if the channel has become empty
469 chanrec* del_channel(userrec *user, const char* cname, const char* reason, bool local)
471 if ((!user) || (!cname))
473 log(DEFAULT,"*** BUG *** del_channel was given an invalid parameter");
477 chanrec* Ptr = FindChan(cname);
482 log(DEBUG,"del_channel: removing: %s %s",user->nick,Ptr->name);
484 for (unsigned int i =0; i < user->chans.size(); i++)
486 /* zap it from the channel list of the user */
487 if (user->chans[i]->channel == Ptr)
491 FOREACH_MOD(I_OnUserPart,OnUserPart(user,Ptr,reason));
492 WriteChannel(Ptr,user,"PART %s :%s",Ptr->name, reason);
496 FOREACH_MOD(I_OnUserPart,OnUserPart(user,Ptr,""));
497 WriteChannel(Ptr,user,"PART :%s",Ptr->name);
499 user->chans[i]->uc_modes = 0;
500 user->chans[i]->channel = NULL;
501 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
508 /* if there are no users left on the channel */
511 chan_hash::iterator iter = chanlist.find(Ptr->name);
513 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
515 /* kill the record */
516 if (iter != chanlist.end())
518 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
519 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
521 chanlist.erase(iter);
528 void server_kick_channel(userrec* user, chanrec* Ptr, char* reason, bool triggerevents)
530 if ((!user) || (!Ptr) || (!reason))
537 if (!Ptr->HasUser(user))
546 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL,user,Ptr,reason));
549 for (unsigned int i =0; i < user->chans.size(); i++)
551 if ((user->chans[i]->channel) && (user->chans[i]->channel == Ptr))
553 WriteChannelWithServ(Config->ServerName,Ptr,"KICK %s %s :%s",Ptr->name, user->nick, reason);
554 user->chans[i]->uc_modes = 0;
555 user->chans[i]->channel = NULL;
564 chan_hash::iterator iter = chanlist.find(Ptr->name);
565 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
566 /* kill the record */
567 if (iter != chanlist.end())
569 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
570 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
572 chanlist.erase(iter);
577 void kick_channel(userrec *src,userrec *user, chanrec *Ptr, char* reason)
579 if ((!src) || (!user) || (!Ptr) || (!reason))
581 log(DEFAULT,"*** BUG *** kick_channel was given an invalid parameter");
585 log(DEBUG,"kick_channel: removing: %s %s %s",user->nick,Ptr->name,src->nick);
589 if (!Ptr->HasUser(user))
591 WriteServ(src->fd,"441 %s %s %s :They are not on that channel",src->nick, user->nick, Ptr->name);
596 if (!is_uline(src->server))
599 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,Ptr,reason));
603 /* Set to -1 by OnUserPreKick if explicit allow was set */
604 if (MOD_RESULT != -1)
606 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,Ptr,AC_KICK));
607 if ((MOD_RESULT == ACR_DENY) && (!is_uline(src->server)))
610 if ((MOD_RESULT == ACR_DEFAULT) || (!is_uline(src->server)))
612 if ((cstatus(src,Ptr) < STATUS_HOP) || (cstatus(src,Ptr) < cstatus(user,Ptr)))
614 if (cstatus(src,Ptr) == STATUS_HOP)
616 WriteServ(src->fd,"482 %s %s :You must be a channel operator",src->nick, Ptr->name);
620 WriteServ(src->fd,"482 %s %s :You must be at least a half-operator",src->nick, Ptr->name);
629 FOREACH_MOD(I_OnUserKick,OnUserKick(src,user,Ptr,reason));
631 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
633 /* zap it from the channel list of the user */
634 if ((*i)->channel && ((*i)->channel == Ptr))
636 WriteChannel(Ptr,src,"KICK %s %s :%s",Ptr->name, user->nick, reason);
638 (*i)->channel = NULL;
639 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
644 if (!Ptr->DelUser(user))
645 /* if there are no users left on the channel */
647 chan_hash::iterator iter = chanlist.find(Ptr->name);
649 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
651 /* kill the record */
652 if (iter != chanlist.end())
654 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
655 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
657 chanlist.erase(iter);