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::SetMode(char mode,bool mode_on)
62 modes[mode-65] = mode_on;
64 this->SetModeParam(mode,"",false);
68 void chanrec::SetModeParam(char mode,const char* parameter,bool mode_on)
70 log(DEBUG,"SetModeParam 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 CUListIter a = internal_userlist.find(user);
136 if (a != internal_userlist.end())
138 internal_userlist.erase(a);
139 /* And tidy any others... */
141 DelHalfoppedUser(user);
145 return internal_userlist.size();
148 bool chanrec::HasUser(userrec* user)
150 return (internal_userlist.find(user) != internal_userlist.end());
153 void chanrec::AddOppedUser(userrec* user)
155 internal_op_userlist[user] = user;
158 void chanrec::DelOppedUser(userrec* user)
160 CUListIter a = internal_op_userlist.find(user);
161 if (a != internal_op_userlist.end())
163 internal_op_userlist.erase(a);
168 void chanrec::AddHalfoppedUser(userrec* user)
170 internal_halfop_userlist[user] = user;
173 void chanrec::DelHalfoppedUser(userrec* user)
175 CUListIter a = internal_halfop_userlist.find(user);
177 if (a != internal_halfop_userlist.end())
179 internal_halfop_userlist.erase(a);
183 void chanrec::AddVoicedUser(userrec* user)
185 internal_voice_userlist[user] = user;
188 void chanrec::DelVoicedUser(userrec* user)
190 CUListIter a = internal_voice_userlist.find(user);
192 if (a != internal_voice_userlist.end())
194 internal_voice_userlist.erase(a);
198 CUList* chanrec::GetUsers()
200 return &internal_userlist;
203 CUList* chanrec::GetOppedUsers()
205 return &internal_op_userlist;
208 CUList* chanrec::GetHalfoppedUsers()
210 return &internal_halfop_userlist;
213 CUList* chanrec::GetVoicedUsers()
215 return &internal_voice_userlist;
219 * add a channel to a user, creating the record for it if needed and linking
220 * it to the user record
223 chanrec* add_channel(userrec *user, const char* cn, const char* key, bool override)
225 if ((!user) || (!cn))
227 log(DEFAULT,"*** BUG *** add_channel was given an invalid parameter");
234 strlcpy(cname,cn,CHANMAX);
235 log(DEBUG,"cname='%s' cn='%s'",cname,cn);
237 log(DEBUG,"add_channel: %s %s",user->nick,cname);
239 chanrec* Ptr = FindChan(cname);
246 FOREACH_RESULT(I_OnUserPreJoin,OnUserPreJoin(user,NULL,cname));
251 /* create a new one */
252 chanlist[cname] = new chanrec();
253 strlcpy(chanlist[cname]->name, cname,CHANMAX);
254 chanlist[cname]->modes[CM_TOPICLOCK] = chanlist[cname]->modes[CM_NOEXTERNAL] = 1;
255 //chanlist[cname]->binarymodes = CM_TOPICLOCK | CM_NOEXTERNAL;
256 chanlist[cname]->created = TIME;
257 *chanlist[cname]->topic = 0;
258 strlcpy(chanlist[cname]->setby, user->nick,NICKMAX-1);
259 chanlist[cname]->topicset = 0;
260 Ptr = chanlist[cname];
261 log(DEBUG,"add_channel: created: %s",cname);
263 * set created to 2 to indicate user
264 * is the first in the channel
265 * and should be given ops
271 /* Already on the channel */
272 if (Ptr->HasUser(user))
276 * remote users are allowed us to bypass channel modes
277 * and bans (used by servers)
279 if (IS_LOCAL(user)) /* was a check on fd > -1 */
282 FOREACH_RESULT(I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname));
287 else if (MOD_RESULT == 0)
292 FOREACH_RESULT(I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
297 log(DEBUG,"add_channel: no key given in JOIN");
298 WriteServ(user->fd,"475 %s %s :Cannot join channel (Requires key)",user->nick, Ptr->name);
303 if (strcmp(key,Ptr->key))
305 log(DEBUG,"add_channel: bad key given in JOIN");
306 WriteServ(user->fd,"475 %s %s :Cannot join channel (Incorrect key)",user->nick, Ptr->name);
312 if (Ptr->modes[CM_INVITEONLY])
315 irc::string xname(Ptr->name);
316 FOREACH_RESULT(I_OnCheckInvite,OnCheckInvite(user, Ptr));
319 log(DEBUG,"add_channel: channel is +i");
320 if (user->IsInvited(xname))
322 /* user was invited to channel */
323 /* there may be an optional channel NOTICE here */
327 WriteServ(user->fd,"473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
331 user->RemoveInvite(xname);
336 FOREACH_RESULT(I_OnCheckLimit,OnCheckLimit(user, Ptr));
339 if (usercount(Ptr) >= Ptr->limit)
341 WriteServ(user->fd,"471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
346 if (Ptr->bans.size())
348 log(DEBUG,"add_channel: about to walk banlist");
350 FOREACH_RESULT(I_OnCheckBan,OnCheckBan(user, Ptr));
352 sprintf(mask,"%s@%s",user->ident, user->GetIPString());
355 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
357 /* This allows CIDR ban matching
359 * Full masked host Full unmasked host IP with CIDR IP without CIDR
361 if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match(mask, i->data, true)) || (match(mask, i->data, false)))
363 WriteServ(user->fd,"474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
373 log(DEBUG,"Overridden checks");
378 log(DEBUG,"Passed channel checks");
380 for (UserChanList::const_iterator index = user->chans.begin(); index != user->chans.end(); index++)
382 if ((*index)->channel == NULL)
384 return ForceChan(Ptr, *index, user, created);
389 * XXX: If the user is an oper here, we can just extend their user->chans vector by one
390 * and put the channel in here. Same for remote users which are not bound by
391 * the channel limits. Otherwise, nope, youre boned.
393 if (!IS_LOCAL(user)) /* was a check on fd < 0 */
395 ucrec* a = new ucrec();
396 chanrec* c = ForceChan(Ptr,a,user,created);
397 user->chans.push_back(a);
400 else if (*user->oper)
402 /* Oper allows extension up to the OPERMAXCHANS value */
403 if (user->chans.size() < OPERMAXCHANS)
405 ucrec* a = new ucrec();
406 chanrec* c = ForceChan(Ptr,a,user,created);
407 user->chans.push_back(a);
412 log(DEBUG,"add_channel: user channel max exceeded: %s %s",user->nick,cname);
413 WriteServ(user->fd,"405 %s %s :You are on too many channels",user->nick, cname);
417 log(DEBUG,"BLAMMO, Whacking channel.");
418 /* Things went seriously pear shaped, so take this away. bwahaha. */
419 chan_hash::iterator n = chanlist.find(cname);
420 if (n != chanlist.end())
425 for (unsigned int index =0; index < user->chans.size(); index++)
427 if (user->chans[index]->channel == Ptr)
429 user->chans[index]->channel = NULL;
430 user->chans[index]->uc_modes = 0;
437 for (unsigned int index =0; index < user->chans.size(); index++)
439 if (user->chans[index]->channel == Ptr)
441 user->chans[index]->channel = NULL;
442 user->chans[index]->uc_modes = 0;
449 chanrec* ForceChan(chanrec* Ptr,ucrec *a,userrec* user, int created)
453 /* first user in is given ops */
454 a->uc_modes = UCMODE_OP;
455 Ptr->AddOppedUser(user);
464 WriteChannel(Ptr,user,"JOIN :%s",Ptr->name);
466 /* Major improvement by Brain - we dont need to be calculating all this pointlessly for remote users */
469 log(DEBUG,"Sent JOIN to client");
472 WriteServ(user->fd,"332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
473 WriteServ(user->fd,"333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
476 WriteServ(user->fd,"366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
478 FOREACH_MOD(I_OnUserJoin,OnUserJoin(user,Ptr));
483 *remove a channel from a users record, and remove the record from memory
484 * if the channel has become empty
487 chanrec* del_channel(userrec *user, const char* cname, const char* reason, bool local)
489 if ((!user) || (!cname))
491 log(DEFAULT,"*** BUG *** del_channel was given an invalid parameter");
495 chanrec* Ptr = FindChan(cname);
500 log(DEBUG,"del_channel: removing: %s %s",user->nick,Ptr->name);
502 for (unsigned int i =0; i < user->chans.size(); i++)
504 /* zap it from the channel list of the user */
505 if (user->chans[i]->channel == Ptr)
509 FOREACH_MOD(I_OnUserPart,OnUserPart(user,Ptr,reason));
510 WriteChannel(Ptr,user,"PART %s :%s",Ptr->name, reason);
514 FOREACH_MOD(I_OnUserPart,OnUserPart(user,Ptr,""));
515 WriteChannel(Ptr,user,"PART :%s",Ptr->name);
517 user->chans[i]->uc_modes = 0;
518 user->chans[i]->channel = NULL;
519 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
526 /* if there are no users left on the channel */
529 chan_hash::iterator iter = chanlist.find(Ptr->name);
531 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
533 /* kill the record */
534 if (iter != chanlist.end())
536 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
537 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
539 chanlist.erase(iter);
546 void server_kick_channel(userrec* user, chanrec* Ptr, char* reason, bool triggerevents)
548 if ((!user) || (!Ptr) || (!reason))
555 if (!Ptr->HasUser(user))
564 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL,user,Ptr,reason));
567 for (unsigned int i =0; i < user->chans.size(); i++)
569 if ((user->chans[i]->channel) && (user->chans[i]->channel == Ptr))
571 WriteChannelWithServ(Config->ServerName,Ptr,"KICK %s %s :%s",Ptr->name, user->nick, reason);
572 user->chans[i]->uc_modes = 0;
573 user->chans[i]->channel = NULL;
582 chan_hash::iterator iter = chanlist.find(Ptr->name);
583 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
584 /* kill the record */
585 if (iter != chanlist.end())
587 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
588 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
590 chanlist.erase(iter);
595 void kick_channel(userrec *src,userrec *user, chanrec *Ptr, char* reason)
597 if ((!src) || (!user) || (!Ptr) || (!reason))
599 log(DEFAULT,"*** BUG *** kick_channel was given an invalid parameter");
603 log(DEBUG,"kick_channel: removing: %s %s %s",user->nick,Ptr->name,src->nick);
607 if (!Ptr->HasUser(user))
609 WriteServ(src->fd,"441 %s %s %s :They are not on that channel",src->nick, user->nick, Ptr->name);
612 if ((is_uline(user->server)) && (!is_uline(src->server)))
614 WriteServ(src->fd,"482 %s %s :Only a u-line may kick a u-line from a channel.",src->nick, Ptr->name);
619 if (!is_uline(src->server))
622 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,Ptr,reason));
626 /* Set to -1 by OnUserPreKick if explicit allow was set */
627 if (MOD_RESULT != -1)
629 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,Ptr,AC_KICK));
630 if ((MOD_RESULT == ACR_DENY) && (!is_uline(src->server)))
633 if ((MOD_RESULT == ACR_DEFAULT) || (!is_uline(src->server)))
635 if ((cstatus(src,Ptr) < STATUS_HOP) || (cstatus(src,Ptr) < cstatus(user,Ptr)))
637 if (cstatus(src,Ptr) == STATUS_HOP)
639 WriteServ(src->fd,"482 %s %s :You must be a channel operator",src->nick, Ptr->name);
643 WriteServ(src->fd,"482 %s %s :You must be at least a half-operator",src->nick, Ptr->name);
652 FOREACH_MOD(I_OnUserKick,OnUserKick(src,user,Ptr,reason));
654 for (UserChanList::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
656 /* zap it from the channel list of the user */
657 if ((*i)->channel && ((*i)->channel == Ptr))
659 WriteChannel(Ptr,src,"KICK %s %s :%s",Ptr->name, user->nick, reason);
661 (*i)->channel = NULL;
662 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
667 if (!Ptr->DelUser(user))
668 /* if there are no users left on the channel */
670 chan_hash::iterator iter = chanlist.find(Ptr->name);
672 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
674 /* kill the record */
675 if (iter != chanlist.end())
677 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
678 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
680 chanlist.erase(iter);