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;
69 //std::vector<ModeParameter> custom_mode_params;
71 chanrec* ForceChan(chanrec* Ptr,ucrec &a,userrec* user, int created);
75 *name = *custom_modes = *topic = *setby = *key = 0;
76 created = topicset = limit = binarymodes = 0;
77 internal_userlist.clear();
80 void chanrec::SetCustomMode(char mode,bool mode_on)
84 char* mptr = this->custom_modes;
87 /* Attempt to find the end of the mode string */
90 /* While iterating the mode string, we found that they already have
91 * this mode in their list. Abort right now. */
94 /* Increment the string size, saves us doing strlen */
98 log(DEBUG,"ssize=%d",ssize);
100 /* Is there room left in the buffer? If there is append the mode */
101 if (ssize < MAXMODES-1)
107 log(DEBUG,"Custom mode %c set, modes='%s'",mode,this->custom_modes);
111 char* mptr = this->custom_modes;
112 bool shift_down = false;
113 /* Iterate through the string */
116 /* When we find the mode we intend to remove, set the
117 * flag to tell the loop to start moving chars down
122 /* If we're moving chars down one place, take the current
123 * char, and move it down one slot, so:
124 * ABC\0 becomes BBC\0 then next iteration, BBC\0 becomes
132 log(DEBUG,"Custom mode %c removed: modelist='%s'",mode,this->custom_modes);
134 /* Only call this if we found the mode */
136 this->SetCustomModeParam(mode,"",false);
141 void chanrec::SetCustomModeParam(char mode,char* parameter,bool mode_on)
144 log(DEBUG,"SetCustomModeParam called");
147 strlcpy(M.channel,this->name,CHANMAX);
148 strlcpy(M.parameter,parameter,MAXBUF);
151 log(DEBUG,"Custom mode parameter %c %s added",mode,parameter);
152 custom_mode_params.push_back(M);
156 if (custom_mode_params.size())
158 for (vector<ModeParameter>::iterator i = custom_mode_params.begin(); i < custom_mode_params.end(); i++)
160 if ((i->mode == mode) && (!strcasecmp(this->name,i->channel)))
162 log(DEBUG,"Custom mode parameter %c %s removed",mode,parameter);
163 custom_mode_params.erase(i);
171 bool chanrec::IsCustomModeSet(char mode)
173 return (strchr(this->custom_modes,mode));
176 std::string chanrec::GetModeParameter(char mode)
178 if (custom_mode_params.size())
180 for (vector<ModeParameter>::iterator i = custom_mode_params.begin(); i < custom_mode_params.end(); i++)
182 if ((i->mode == mode) && (!strcasecmp(this->name,i->channel)))
191 long chanrec::GetUserCounter()
193 return (this->internal_userlist.size());
196 void chanrec::AddUser(char* castuser)
198 internal_userlist[castuser] = castuser;
199 log(DEBUG,"Added casted user to channel's internal list");
202 void chanrec::DelUser(char* castuser)
204 std::map<char*,char*>::iterator a = internal_userlist.find(castuser);
205 if (a != internal_userlist.end())
207 log(DEBUG,"Removed casted user from channel's internal list");
208 internal_userlist.erase(a);
209 /* And tidy any others... */
210 DelOppedUser(castuser);
211 DelHalfoppedUser(castuser);
212 DelVoicedUser(castuser);
217 void chanrec::AddOppedUser(char* castuser)
219 internal_op_userlist[castuser] = castuser;
220 log(DEBUG,"Added casted user to channel's internal list");
223 void chanrec::DelOppedUser(char* castuser)
225 std::map<char*,char*>::iterator a = internal_op_userlist.find(castuser);
226 if (a != internal_op_userlist.end())
228 log(DEBUG,"Removed casted user from channel's internal list");
229 internal_op_userlist.erase(a);
234 void chanrec::AddHalfoppedUser(char* castuser)
236 internal_halfop_userlist[castuser] = castuser;
237 log(DEBUG,"Added casted user to channel's internal list");
240 void chanrec::DelHalfoppedUser(char* castuser)
242 std::map<char*,char*>::iterator a = internal_halfop_userlist.find(castuser);
243 if (a != internal_halfop_userlist.end())
245 log(DEBUG,"Removed casted user from channel's internal list");
246 internal_halfop_userlist.erase(a);
251 void chanrec::AddVoicedUser(char* castuser)
253 internal_voice_userlist[castuser] = castuser;
254 log(DEBUG,"Added casted user to channel's internal list");
257 void chanrec::DelVoicedUser(char* castuser)
259 std::map<char*,char*>::iterator a = internal_voice_userlist.find(castuser);
260 if (a != internal_voice_userlist.end())
262 log(DEBUG,"Removed casted user from channel's internal list");
263 internal_voice_userlist.erase(a);
268 std::map<char*,char*> *chanrec::GetUsers()
270 return &internal_userlist;
273 std::map<char*,char*> *chanrec::GetOppedUsers()
275 return &internal_op_userlist;
278 std::map<char*,char*> *chanrec::GetHalfoppedUsers()
280 return &internal_halfop_userlist;
283 std::map<char*,char*> *chanrec::GetVoicedUsers()
285 return &internal_voice_userlist;
289 * add a channel to a user, creating the record for it if needed and linking
290 * it to the user record
293 chanrec* add_channel(userrec *user, const char* cn, const char* key, bool override)
295 if ((!user) || (!cn))
297 log(DEFAULT,"*** BUG *** add_channel was given an invalid parameter");
304 strlcpy(cname,cn,CHANMAX);
305 log(DEBUG,"cname='%s' cn='%s'",cname,cn);
307 log(DEBUG,"add_channel: %s %s",user->nick,cname);
309 chanrec* Ptr = FindChan(cname);
316 FOREACH_RESULT(I_OnUserPreJoin,OnUserPreJoin(user,NULL,cname));
321 /* create a new one */
322 chanlist[cname] = new chanrec();
323 strlcpy(chanlist[cname]->name, cname,CHANMAX);
324 chanlist[cname]->binarymodes = CM_TOPICLOCK | CM_NOEXTERNAL;
325 chanlist[cname]->created = TIME;
326 *chanlist[cname]->topic = 0;
327 strlcpy(chanlist[cname]->setby, user->nick,NICKMAX-1);
328 chanlist[cname]->topicset = 0;
329 Ptr = chanlist[cname];
330 log(DEBUG,"add_channel: created: %s",cname);
332 * set created to 2 to indicate user
333 * is the first in the channel
334 * and should be given ops
340 /* Already on the channel */
341 if (has_channel(user,Ptr))
345 * remote users are allowed us to bypass channel modes
346 * and bans (used by servers)
348 if (IS_LOCAL(user)) /* was a check on fd > -1 */
351 FOREACH_RESULT(I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname));
356 else if (MOD_RESULT == 0)
361 FOREACH_RESULT(I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
366 log(DEBUG,"add_channel: no key given in JOIN");
367 WriteServ(user->fd,"475 %s %s :Cannot join channel (Requires key)",user->nick, Ptr->name);
372 if (strcmp(key,Ptr->key))
374 log(DEBUG,"add_channel: bad key given in JOIN");
375 WriteServ(user->fd,"475 %s %s :Cannot join channel (Incorrect key)",user->nick, Ptr->name);
381 if (Ptr->binarymodes & CM_INVITEONLY)
384 irc::string xname(Ptr->name);
385 FOREACH_RESULT(I_OnCheckInvite,OnCheckInvite(user, Ptr));
388 log(DEBUG,"add_channel: channel is +i");
389 if (user->IsInvited(xname))
391 /* user was invited to channel */
392 /* there may be an optional channel NOTICE here */
396 WriteServ(user->fd,"473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
400 user->RemoveInvite(xname);
405 FOREACH_RESULT(I_OnCheckLimit,OnCheckLimit(user, Ptr));
408 if (usercount(Ptr) >= Ptr->limit)
410 WriteServ(user->fd,"471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
415 if (Ptr->bans.size())
417 log(DEBUG,"add_channel: about to walk banlist");
419 FOREACH_RESULT(I_OnCheckBan,OnCheckBan(user, Ptr));
422 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
424 if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match((char*)inet_ntoa(user->ip4),i->data)))
426 WriteServ(user->fd,"474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
436 log(DEBUG,"Overridden checks");
441 log(DEBUG,"Passed channel checks");
443 for (unsigned int index =0; index < user->chans.size(); index++)
445 if (user->chans[index].channel == NULL)
447 return ForceChan(Ptr,user->chans[index],user,created);
452 * XXX: If the user is an oper here, we can just extend their user->chans vector by one
453 * and put the channel in here. Same for remote users which are not bound by
454 * the channel limits. Otherwise, nope, youre boned.
456 if (!IS_LOCAL(user)) /* was a check on fd < 0 */
459 chanrec* c = ForceChan(Ptr,a,user,created);
460 user->chans.push_back(a);
463 else if (*user->oper)
465 /* Oper allows extension up to the OPERMAXCHANS value */
466 if (user->chans.size() < OPERMAXCHANS)
469 chanrec* c = ForceChan(Ptr,a,user,created);
470 user->chans.push_back(a);
475 log(DEBUG,"add_channel: user channel max exceeded: %s %s",user->nick,cname);
476 WriteServ(user->fd,"405 %s %s :You are on too many channels",user->nick, cname);
480 log(DEBUG,"BLAMMO, Whacking channel.");
481 /* Things went seriously pear shaped, so take this away. bwahaha. */
482 chan_hash::iterator n = chanlist.find(cname);
483 if (n != chanlist.end())
485 Ptr->DelUser((char*)user);
488 for (unsigned int index =0; index < user->chans.size(); index++)
490 if (user->chans[index].channel == Ptr)
492 user->chans[index].channel = NULL;
493 user->chans[index].uc_modes = 0;
501 chanrec* ForceChan(chanrec* Ptr,ucrec &a,userrec* user, int created)
505 /* first user in is given ops */
506 a.uc_modes = UCMODE_OP;
507 Ptr->AddOppedUser((char*)user);
515 Ptr->AddUser((char*)user);
516 WriteChannel(Ptr,user,"JOIN :%s",Ptr->name);
517 log(DEBUG,"Sent JOIN to client");
521 WriteServ(user->fd,"332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
522 WriteServ(user->fd,"333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
526 WriteServ(user->fd,"366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
527 FOREACH_MOD(I_OnUserJoin,OnUserJoin(user,Ptr));
532 *remove a channel from a users record, and remove the record from memory
533 * if the channel has become empty
536 chanrec* del_channel(userrec *user, const char* cname, const char* reason, bool local)
538 if ((!user) || (!cname))
540 log(DEFAULT,"*** BUG *** del_channel was given an invalid parameter");
544 chanrec* Ptr = FindChan(cname);
549 log(DEBUG,"del_channel: removing: %s %s",user->nick,Ptr->name);
551 for (unsigned int i =0; i < user->chans.size(); i++)
553 /* zap it from the channel list of the user */
554 if (user->chans[i].channel == Ptr)
558 FOREACH_MOD(I_OnUserPart,OnUserPart(user,Ptr,reason));
559 WriteChannel(Ptr,user,"PART %s :%s",Ptr->name, reason);
563 FOREACH_MOD(I_OnUserPart,OnUserPart(user,Ptr,""));
564 WriteChannel(Ptr,user,"PART :%s",Ptr->name);
566 user->chans[i].uc_modes = 0;
567 user->chans[i].channel = NULL;
568 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
573 Ptr->DelUser((char*)user);
575 /* if there are no users left on the channel */
578 chan_hash::iterator iter = chanlist.find(Ptr->name);
580 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
582 /* kill the record */
583 if (iter != chanlist.end())
585 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
586 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
588 chanlist.erase(iter);
595 void server_kick_channel(userrec* user, chanrec* Ptr, char* reason, bool triggerevents)
597 if ((!user) || (!Ptr) || (!reason))
604 if (!has_channel(user,Ptr))
613 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL,user,Ptr,reason));
616 for (unsigned int i =0; i < user->chans.size(); i++)
618 if (user->chans[i].channel)
619 if (!strcasecmp(user->chans[i].channel->name,Ptr->name))
621 WriteChannelWithServ(Config->ServerName,Ptr,"KICK %s %s :%s",Ptr->name, user->nick, reason);
622 user->chans[i].uc_modes = 0;
623 user->chans[i].channel = NULL;
628 Ptr->DelUser((char*)user);
632 chan_hash::iterator iter = chanlist.find(Ptr->name);
633 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
634 /* kill the record */
635 if (iter != chanlist.end())
637 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
638 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
640 chanlist.erase(iter);
645 void kick_channel(userrec *src,userrec *user, chanrec *Ptr, char* reason)
647 if ((!src) || (!user) || (!Ptr) || (!reason))
649 log(DEFAULT,"*** BUG *** kick_channel was given an invalid parameter");
653 log(DEBUG,"kick_channel: removing: %s %s %s",user->nick,Ptr->name,src->nick);
657 if (!has_channel(user,Ptr))
659 WriteServ(src->fd,"441 %s %s %s :They are not on that channel",src->nick, user->nick, Ptr->name);
664 if (!is_uline(src->server))
667 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,Ptr,reason));
671 /* Set to -1 by OnUserPreKick if explicit allow was set */
672 if (MOD_RESULT != -1)
674 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,Ptr,AC_KICK));
675 if ((MOD_RESULT == ACR_DENY) && (!is_uline(src->server)))
678 if ((MOD_RESULT == ACR_DEFAULT) || (!is_uline(src->server)))
680 if ((cstatus(src,Ptr) < STATUS_HOP) || (cstatus(src,Ptr) < cstatus(user,Ptr)))
682 if (cstatus(src,Ptr) == STATUS_HOP)
684 WriteServ(src->fd,"482 %s %s :You must be a channel operator",src->nick, Ptr->name);
688 WriteServ(src->fd,"482 %s %s :You must be at least a half-operator",src->nick, Ptr->name);
697 FOREACH_MOD(I_OnUserKick,OnUserKick(src,user,Ptr,reason));
699 for (unsigned int i =0; i < user->chans.size(); i++)
701 /* zap it from the channel list of the user */
702 if (user->chans[i].channel)
704 if (!strcasecmp(user->chans[i].channel->name,Ptr->name))
706 WriteChannel(Ptr,src,"KICK %s %s :%s",Ptr->name, user->nick, reason);
707 user->chans[i].uc_modes = 0;
708 user->chans[i].channel = NULL;
709 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
715 Ptr->DelUser((char*)user);
717 /* if there are no users left on the channel */
720 chan_hash::iterator iter = chanlist.find(Ptr->name);
722 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
724 /* kill the record */
725 if (iter != chanlist.end())
727 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
728 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
730 chanlist.erase(iter);