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 * ---------------------------------------------------
19 #include "inspircd_config.h"
21 #include "inspircd_io.h"
23 #include <sys/errno.h>
24 #include <sys/ioctl.h>
25 #include <sys/utsname.h>
29 #include <ext/hash_map>
47 #include "inspstring.h"
48 #include "helperfuncs.h"
52 #define nspace __gnu_cxx
57 extern ServerConfig* Config;
60 extern std::vector<Module*> modules;
61 extern std::vector<ircd_module*> factory;
62 extern int WHOWAS_STALE;
63 extern int WHOWAS_MAX;
65 extern chan_hash chanlist;
68 chanrec* ForceChan(chanrec* Ptr,ucrec *a,userrec* user, int created);
72 *name = *topic = *setby = *key = 0;
73 created = topicset = limit = 0;
74 internal_userlist.clear();
78 void chanrec::SetCustomMode(char mode,bool mode_on)
80 modes[mode-65] = mode_on;
82 this->SetCustomModeParam(mode,"",false);
86 void chanrec::SetCustomModeParam(char mode,char* parameter,bool mode_on)
88 log(DEBUG,"SetCustomModeParam called");
90 std::map<char,char*>::iterator n = custom_mode_params.find(mode);
94 log(DEBUG,"Custom mode parameter %c %s added",mode,parameter);
95 if (n == custom_mode_params.end())
97 custom_mode_params[mode] = strdup(parameter);
102 if (n != custom_mode_params.end())
105 custom_mode_params.erase(n);
110 bool chanrec::IsModeSet(char mode)
112 return modes[mode-65];
115 std::string chanrec::GetModeParameter(char mode)
121 else if (mode == 'l')
123 return ConvToStr(this->limit);
127 std::map<char,char*>::iterator n = custom_mode_params.find(mode);
128 if (n != custom_mode_params.end())
136 long chanrec::GetUserCounter()
138 return (this->internal_userlist.size());
141 void chanrec::AddUser(userrec* user)
143 internal_userlist[user] = user;
146 unsigned long chanrec::DelUser(userrec* user)
148 CUList::iterator a = internal_userlist.find(user);
149 if (a != internal_userlist.end())
151 internal_userlist.erase(a);
152 /* And tidy any others... */
154 DelHalfoppedUser(user);
156 return internal_userlist.size();
158 return internal_userlist.size();
161 bool chanrec::HasUser(userrec* user)
163 return (internal_userlist.find(user) != internal_userlist.end());
166 void chanrec::AddOppedUser(userrec* user)
168 internal_op_userlist[user] = user;
171 void chanrec::DelOppedUser(userrec* user)
173 CUList::iterator a = internal_op_userlist.find(user);
174 if (a != internal_op_userlist.end())
176 internal_op_userlist.erase(a);
181 void chanrec::AddHalfoppedUser(userrec* user)
183 internal_halfop_userlist[user] = user;
186 void chanrec::DelHalfoppedUser(userrec* user)
188 CUList::iterator a = internal_halfop_userlist.find(user);
189 if (a != internal_halfop_userlist.end())
191 internal_halfop_userlist.erase(a);
196 void chanrec::AddVoicedUser(userrec* user)
198 internal_voice_userlist[user] = user;
201 void chanrec::DelVoicedUser(userrec* user)
203 CUList::iterator a = internal_voice_userlist.find(user);
204 if (a != internal_voice_userlist.end())
206 internal_voice_userlist.erase(a);
211 CUList* chanrec::GetUsers()
213 return &internal_userlist;
216 CUList* chanrec::GetOppedUsers()
218 return &internal_op_userlist;
221 CUList* chanrec::GetHalfoppedUsers()
223 return &internal_halfop_userlist;
226 CUList* chanrec::GetVoicedUsers()
228 return &internal_voice_userlist;
232 * add a channel to a user, creating the record for it if needed and linking
233 * it to the user record
236 chanrec* add_channel(userrec *user, const char* cn, const char* key, bool override)
238 if ((!user) || (!cn))
240 log(DEFAULT,"*** BUG *** add_channel was given an invalid parameter");
247 strlcpy(cname,cn,CHANMAX);
248 log(DEBUG,"cname='%s' cn='%s'",cname,cn);
250 log(DEBUG,"add_channel: %s %s",user->nick,cname);
252 chanrec* Ptr = FindChan(cname);
259 FOREACH_RESULT(I_OnUserPreJoin,OnUserPreJoin(user,NULL,cname));
264 /* create a new one */
265 chanlist[cname] = new chanrec();
266 strlcpy(chanlist[cname]->name, cname,CHANMAX);
267 chanlist[cname]->modes[CM_TOPICLOCK] = chanlist[cname]->modes[CM_NOEXTERNAL] = 1;
268 //chanlist[cname]->binarymodes = CM_TOPICLOCK | CM_NOEXTERNAL;
269 chanlist[cname]->created = TIME;
270 *chanlist[cname]->topic = 0;
271 strlcpy(chanlist[cname]->setby, user->nick,NICKMAX-1);
272 chanlist[cname]->topicset = 0;
273 Ptr = chanlist[cname];
274 log(DEBUG,"add_channel: created: %s",cname);
276 * set created to 2 to indicate user
277 * is the first in the channel
278 * and should be given ops
284 /* Already on the channel */
285 if (Ptr->HasUser(user))
289 * remote users are allowed us to bypass channel modes
290 * and bans (used by servers)
292 if (IS_LOCAL(user)) /* was a check on fd > -1 */
295 FOREACH_RESULT(I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname));
300 else if (MOD_RESULT == 0)
305 FOREACH_RESULT(I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
310 log(DEBUG,"add_channel: no key given in JOIN");
311 WriteServ(user->fd,"475 %s %s :Cannot join channel (Requires key)",user->nick, Ptr->name);
316 if (strcmp(key,Ptr->key))
318 log(DEBUG,"add_channel: bad key given in JOIN");
319 WriteServ(user->fd,"475 %s %s :Cannot join channel (Incorrect key)",user->nick, Ptr->name);
325 if (Ptr->modes[CM_INVITEONLY])
328 irc::string xname(Ptr->name);
329 FOREACH_RESULT(I_OnCheckInvite,OnCheckInvite(user, Ptr));
332 log(DEBUG,"add_channel: channel is +i");
333 if (user->IsInvited(xname))
335 /* user was invited to channel */
336 /* there may be an optional channel NOTICE here */
340 WriteServ(user->fd,"473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
344 user->RemoveInvite(xname);
349 FOREACH_RESULT(I_OnCheckLimit,OnCheckLimit(user, Ptr));
352 if (usercount(Ptr) >= Ptr->limit)
354 WriteServ(user->fd,"471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
359 if (Ptr->bans.size())
361 log(DEBUG,"add_channel: about to walk banlist");
363 FOREACH_RESULT(I_OnCheckBan,OnCheckBan(user, Ptr));
366 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
368 if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match((char*)inet_ntoa(user->ip4),i->data)))
370 WriteServ(user->fd,"474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
380 log(DEBUG,"Overridden checks");
385 log(DEBUG,"Passed channel checks");
387 for (std::vector<ucrec*>::const_iterator index = user->chans.begin(); index != user->chans.end(); index++)
389 if ((ucrec*)(*index)->channel == NULL)
391 return ForceChan(Ptr,(ucrec*)(*index),user,created);
396 * XXX: If the user is an oper here, we can just extend their user->chans vector by one
397 * and put the channel in here. Same for remote users which are not bound by
398 * the channel limits. Otherwise, nope, youre boned.
400 if (!IS_LOCAL(user)) /* was a check on fd < 0 */
402 ucrec* a = new ucrec();
403 chanrec* c = ForceChan(Ptr,a,user,created);
404 user->chans.push_back(a);
407 else if (*user->oper)
409 /* Oper allows extension up to the OPERMAXCHANS value */
410 if (user->chans.size() < OPERMAXCHANS)
412 ucrec* a = new ucrec();
413 chanrec* c = ForceChan(Ptr,a,user,created);
414 user->chans.push_back(a);
419 log(DEBUG,"add_channel: user channel max exceeded: %s %s",user->nick,cname);
420 WriteServ(user->fd,"405 %s %s :You are on too many channels",user->nick, cname);
424 log(DEBUG,"BLAMMO, Whacking channel.");
425 /* Things went seriously pear shaped, so take this away. bwahaha. */
426 chan_hash::iterator n = chanlist.find(cname);
427 if (n != chanlist.end())
432 for (unsigned int index =0; index < user->chans.size(); index++)
434 if (user->chans[index]->channel == Ptr)
436 user->chans[index]->channel = NULL;
437 user->chans[index]->uc_modes = 0;
445 chanrec* ForceChan(chanrec* Ptr,ucrec *a,userrec* user, int created)
449 /* first user in is given ops */
450 a->uc_modes = UCMODE_OP;
451 Ptr->AddOppedUser(user);
460 WriteChannel(Ptr,user,"JOIN :%s",Ptr->name);
462 /* Major improvement by Brain - we dont need to be calculating all this pointlessly for remote users */
465 log(DEBUG,"Sent JOIN to client");
468 WriteServ(user->fd,"332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
469 WriteServ(user->fd,"333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
472 WriteServ(user->fd,"366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
474 FOREACH_MOD(I_OnUserJoin,OnUserJoin(user,Ptr));
479 *remove a channel from a users record, and remove the record from memory
480 * if the channel has become empty
483 chanrec* del_channel(userrec *user, const char* cname, const char* reason, bool local)
485 if ((!user) || (!cname))
487 log(DEFAULT,"*** BUG *** del_channel was given an invalid parameter");
491 chanrec* Ptr = FindChan(cname);
496 log(DEBUG,"del_channel: removing: %s %s",user->nick,Ptr->name);
498 for (unsigned int i =0; i < user->chans.size(); i++)
500 /* zap it from the channel list of the user */
501 if (user->chans[i]->channel == Ptr)
505 FOREACH_MOD(I_OnUserPart,OnUserPart(user,Ptr,reason));
506 WriteChannel(Ptr,user,"PART %s :%s",Ptr->name, reason);
510 FOREACH_MOD(I_OnUserPart,OnUserPart(user,Ptr,""));
511 WriteChannel(Ptr,user,"PART :%s",Ptr->name);
513 user->chans[i]->uc_modes = 0;
514 user->chans[i]->channel = NULL;
515 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
522 /* if there are no users left on the channel */
525 chan_hash::iterator iter = chanlist.find(Ptr->name);
527 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
529 /* kill the record */
530 if (iter != chanlist.end())
532 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
533 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
535 chanlist.erase(iter);
542 void server_kick_channel(userrec* user, chanrec* Ptr, char* reason, bool triggerevents)
544 if ((!user) || (!Ptr) || (!reason))
551 if (!Ptr->HasUser(user))
560 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL,user,Ptr,reason));
563 for (unsigned int i =0; i < user->chans.size(); i++)
565 if ((user->chans[i]->channel) && (user->chans[i]->channel == Ptr))
567 WriteChannelWithServ(Config->ServerName,Ptr,"KICK %s %s :%s",Ptr->name, user->nick, reason);
568 user->chans[i]->uc_modes = 0;
569 user->chans[i]->channel = NULL;
578 chan_hash::iterator iter = chanlist.find(Ptr->name);
579 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
580 /* kill the record */
581 if (iter != chanlist.end())
583 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
584 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
586 chanlist.erase(iter);
591 void kick_channel(userrec *src,userrec *user, chanrec *Ptr, char* reason)
593 if ((!src) || (!user) || (!Ptr) || (!reason))
595 log(DEFAULT,"*** BUG *** kick_channel was given an invalid parameter");
599 log(DEBUG,"kick_channel: removing: %s %s %s",user->nick,Ptr->name,src->nick);
603 if (!Ptr->HasUser(user))
605 WriteServ(src->fd,"441 %s %s %s :They are not on that channel",src->nick, user->nick, Ptr->name);
610 if (!is_uline(src->server))
613 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,Ptr,reason));
617 /* Set to -1 by OnUserPreKick if explicit allow was set */
618 if (MOD_RESULT != -1)
620 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,Ptr,AC_KICK));
621 if ((MOD_RESULT == ACR_DENY) && (!is_uline(src->server)))
624 if ((MOD_RESULT == ACR_DEFAULT) || (!is_uline(src->server)))
626 if ((cstatus(src,Ptr) < STATUS_HOP) || (cstatus(src,Ptr) < cstatus(user,Ptr)))
628 if (cstatus(src,Ptr) == STATUS_HOP)
630 WriteServ(src->fd,"482 %s %s :You must be a channel operator",src->nick, Ptr->name);
634 WriteServ(src->fd,"482 %s %s :You must be at least a half-operator",src->nick, Ptr->name);
643 FOREACH_MOD(I_OnUserKick,OnUserKick(src,user,Ptr,reason));
645 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
647 /* zap it from the channel list of the user */
648 if ((((ucrec*)(*i))->channel) && (((ucrec*)(*i))->channel == Ptr))
650 WriteChannel(Ptr,src,"KICK %s %s :%s",Ptr->name, user->nick, reason);
651 ((ucrec*)(*i))->uc_modes = 0;
652 ((ucrec*)(*i))->channel = NULL;
653 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
658 if (!Ptr->DelUser(user))
659 /* if there are no users left on the channel */
661 chan_hash::iterator iter = chanlist.find(Ptr->name);
663 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
665 /* kill the record */
666 if (iter != chanlist.end())
668 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
669 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
671 chanlist.erase(iter);