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 chanrec* ForceChan(chanrec* Ptr,ucrec &a,userrec* user, int created);
73 *name = *topic = *setby = *key = 0;
74 created = topicset = limit = binarymodes = 0;
75 internal_userlist.clear();
76 memset(&custom_modes,0,64);
79 void chanrec::SetCustomMode(char mode,bool mode_on)
81 custom_modes[mode-65] = mode_on;
83 this->SetCustomModeParam(mode,"",false);
87 void chanrec::SetCustomModeParam(char mode,char* parameter,bool mode_on)
89 log(DEBUG,"SetCustomModeParam called");
91 std::map<char,char*>::iterator n = custom_mode_params.find(mode);
95 log(DEBUG,"Custom mode parameter %c %s added",mode,parameter);
96 if (n == custom_mode_params.end())
98 custom_mode_params[mode] = strdup(parameter);
103 if (n != custom_mode_params.end())
106 custom_mode_params.erase(n);
111 bool chanrec::IsCustomModeSet(char mode)
113 return custom_modes[mode-65];
116 std::string chanrec::GetModeParameter(char mode)
118 std::map<char,char*>::iterator n = custom_mode_params.find(mode);
119 if (n != custom_mode_params.end())
126 long chanrec::GetUserCounter()
128 return (this->internal_userlist.size());
131 void chanrec::AddUser(char* castuser)
133 internal_userlist[castuser] = castuser;
134 log(DEBUG,"Added casted user to channel's internal list");
137 void chanrec::DelUser(char* castuser)
139 std::map<char*,char*>::iterator a = internal_userlist.find(castuser);
140 if (a != internal_userlist.end())
142 log(DEBUG,"Removed casted user from channel's internal list");
143 internal_userlist.erase(a);
144 /* And tidy any others... */
145 DelOppedUser(castuser);
146 DelHalfoppedUser(castuser);
147 DelVoicedUser(castuser);
152 void chanrec::AddOppedUser(char* castuser)
154 internal_op_userlist[castuser] = castuser;
155 log(DEBUG,"Added casted user to channel's internal list");
158 void chanrec::DelOppedUser(char* castuser)
160 std::map<char*,char*>::iterator a = internal_op_userlist.find(castuser);
161 if (a != internal_op_userlist.end())
163 log(DEBUG,"Removed casted user from channel's internal list");
164 internal_op_userlist.erase(a);
169 void chanrec::AddHalfoppedUser(char* castuser)
171 internal_halfop_userlist[castuser] = castuser;
172 log(DEBUG,"Added casted user to channel's internal list");
175 void chanrec::DelHalfoppedUser(char* castuser)
177 std::map<char*,char*>::iterator a = internal_halfop_userlist.find(castuser);
178 if (a != internal_halfop_userlist.end())
180 log(DEBUG,"Removed casted user from channel's internal list");
181 internal_halfop_userlist.erase(a);
186 void chanrec::AddVoicedUser(char* castuser)
188 internal_voice_userlist[castuser] = castuser;
189 log(DEBUG,"Added casted user to channel's internal list");
192 void chanrec::DelVoicedUser(char* castuser)
194 std::map<char*,char*>::iterator a = internal_voice_userlist.find(castuser);
195 if (a != internal_voice_userlist.end())
197 log(DEBUG,"Removed casted user from channel's internal list");
198 internal_voice_userlist.erase(a);
203 std::map<char*,char*> *chanrec::GetUsers()
205 return &internal_userlist;
208 std::map<char*,char*> *chanrec::GetOppedUsers()
210 return &internal_op_userlist;
213 std::map<char*,char*> *chanrec::GetHalfoppedUsers()
215 return &internal_halfop_userlist;
218 std::map<char*,char*> *chanrec::GetVoicedUsers()
220 return &internal_voice_userlist;
224 * add a channel to a user, creating the record for it if needed and linking
225 * it to the user record
228 chanrec* add_channel(userrec *user, const char* cn, const char* key, bool override)
230 if ((!user) || (!cn))
232 log(DEFAULT,"*** BUG *** add_channel was given an invalid parameter");
239 strlcpy(cname,cn,CHANMAX);
240 log(DEBUG,"cname='%s' cn='%s'",cname,cn);
242 log(DEBUG,"add_channel: %s %s",user->nick,cname);
244 chanrec* Ptr = FindChan(cname);
251 FOREACH_RESULT(I_OnUserPreJoin,OnUserPreJoin(user,NULL,cname));
256 /* create a new one */
257 chanlist[cname] = new chanrec();
258 strlcpy(chanlist[cname]->name, cname,CHANMAX);
259 chanlist[cname]->binarymodes = CM_TOPICLOCK | CM_NOEXTERNAL;
260 chanlist[cname]->created = TIME;
261 *chanlist[cname]->topic = 0;
262 strlcpy(chanlist[cname]->setby, user->nick,NICKMAX-1);
263 chanlist[cname]->topicset = 0;
264 Ptr = chanlist[cname];
265 log(DEBUG,"add_channel: created: %s",cname);
267 * set created to 2 to indicate user
268 * is the first in the channel
269 * and should be given ops
275 /* Already on the channel */
276 if (has_channel(user,Ptr))
280 * remote users are allowed us to bypass channel modes
281 * and bans (used by servers)
283 if (IS_LOCAL(user)) /* was a check on fd > -1 */
286 FOREACH_RESULT(I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname));
291 else if (MOD_RESULT == 0)
296 FOREACH_RESULT(I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
301 log(DEBUG,"add_channel: no key given in JOIN");
302 WriteServ(user->fd,"475 %s %s :Cannot join channel (Requires key)",user->nick, Ptr->name);
307 if (strcmp(key,Ptr->key))
309 log(DEBUG,"add_channel: bad key given in JOIN");
310 WriteServ(user->fd,"475 %s %s :Cannot join channel (Incorrect key)",user->nick, Ptr->name);
316 if (Ptr->binarymodes & CM_INVITEONLY)
319 irc::string xname(Ptr->name);
320 FOREACH_RESULT(I_OnCheckInvite,OnCheckInvite(user, Ptr));
323 log(DEBUG,"add_channel: channel is +i");
324 if (user->IsInvited(xname))
326 /* user was invited to channel */
327 /* there may be an optional channel NOTICE here */
331 WriteServ(user->fd,"473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
335 user->RemoveInvite(xname);
340 FOREACH_RESULT(I_OnCheckLimit,OnCheckLimit(user, Ptr));
343 if (usercount(Ptr) >= Ptr->limit)
345 WriteServ(user->fd,"471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
350 if (Ptr->bans.size())
352 log(DEBUG,"add_channel: about to walk banlist");
354 FOREACH_RESULT(I_OnCheckBan,OnCheckBan(user, Ptr));
357 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
359 if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match((char*)inet_ntoa(user->ip4),i->data)))
361 WriteServ(user->fd,"474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
371 log(DEBUG,"Overridden checks");
376 log(DEBUG,"Passed channel checks");
378 for (unsigned int index =0; index < user->chans.size(); index++)
380 if (user->chans[index].channel == NULL)
382 return ForceChan(Ptr,user->chans[index],user,created);
387 * XXX: If the user is an oper here, we can just extend their user->chans vector by one
388 * and put the channel in here. Same for remote users which are not bound by
389 * the channel limits. Otherwise, nope, youre boned.
391 if (!IS_LOCAL(user)) /* was a check on fd < 0 */
394 chanrec* c = ForceChan(Ptr,a,user,created);
395 user->chans.push_back(a);
398 else if (*user->oper)
400 /* Oper allows extension up to the OPERMAXCHANS value */
401 if (user->chans.size() < OPERMAXCHANS)
404 chanrec* c = ForceChan(Ptr,a,user,created);
405 user->chans.push_back(a);
410 log(DEBUG,"add_channel: user channel max exceeded: %s %s",user->nick,cname);
411 WriteServ(user->fd,"405 %s %s :You are on too many channels",user->nick, cname);
415 log(DEBUG,"BLAMMO, Whacking channel.");
416 /* Things went seriously pear shaped, so take this away. bwahaha. */
417 chan_hash::iterator n = chanlist.find(cname);
418 if (n != chanlist.end())
420 Ptr->DelUser((char*)user);
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;
436 chanrec* ForceChan(chanrec* Ptr,ucrec &a,userrec* user, int created)
440 /* first user in is given ops */
441 a.uc_modes = UCMODE_OP;
442 Ptr->AddOppedUser((char*)user);
450 Ptr->AddUser((char*)user);
451 WriteChannel(Ptr,user,"JOIN :%s",Ptr->name);
452 log(DEBUG,"Sent JOIN to client");
456 WriteServ(user->fd,"332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
457 WriteServ(user->fd,"333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
461 WriteServ(user->fd,"366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
462 FOREACH_MOD(I_OnUserJoin,OnUserJoin(user,Ptr));
467 *remove a channel from a users record, and remove the record from memory
468 * if the channel has become empty
471 chanrec* del_channel(userrec *user, const char* cname, const char* reason, bool local)
473 if ((!user) || (!cname))
475 log(DEFAULT,"*** BUG *** del_channel was given an invalid parameter");
479 chanrec* Ptr = FindChan(cname);
484 log(DEBUG,"del_channel: removing: %s %s",user->nick,Ptr->name);
486 for (unsigned int i =0; i < user->chans.size(); i++)
488 /* zap it from the channel list of the user */
489 if (user->chans[i].channel == Ptr)
493 FOREACH_MOD(I_OnUserPart,OnUserPart(user,Ptr,reason));
494 WriteChannel(Ptr,user,"PART %s :%s",Ptr->name, reason);
498 FOREACH_MOD(I_OnUserPart,OnUserPart(user,Ptr,""));
499 WriteChannel(Ptr,user,"PART :%s",Ptr->name);
501 user->chans[i].uc_modes = 0;
502 user->chans[i].channel = NULL;
503 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
508 Ptr->DelUser((char*)user);
510 /* if there are no users left on the channel */
513 chan_hash::iterator iter = chanlist.find(Ptr->name);
515 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
517 /* kill the record */
518 if (iter != chanlist.end())
520 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
521 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
523 chanlist.erase(iter);
530 void server_kick_channel(userrec* user, chanrec* Ptr, char* reason, bool triggerevents)
532 if ((!user) || (!Ptr) || (!reason))
539 if (!has_channel(user,Ptr))
548 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL,user,Ptr,reason));
551 for (unsigned int i =0; i < user->chans.size(); i++)
553 if (user->chans[i].channel)
554 if (!strcasecmp(user->chans[i].channel->name,Ptr->name))
556 WriteChannelWithServ(Config->ServerName,Ptr,"KICK %s %s :%s",Ptr->name, user->nick, reason);
557 user->chans[i].uc_modes = 0;
558 user->chans[i].channel = NULL;
563 Ptr->DelUser((char*)user);
567 chan_hash::iterator iter = chanlist.find(Ptr->name);
568 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
569 /* kill the record */
570 if (iter != chanlist.end())
572 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
573 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
575 chanlist.erase(iter);
580 void kick_channel(userrec *src,userrec *user, chanrec *Ptr, char* reason)
582 if ((!src) || (!user) || (!Ptr) || (!reason))
584 log(DEFAULT,"*** BUG *** kick_channel was given an invalid parameter");
588 log(DEBUG,"kick_channel: removing: %s %s %s",user->nick,Ptr->name,src->nick);
592 if (!has_channel(user,Ptr))
594 WriteServ(src->fd,"441 %s %s %s :They are not on that channel",src->nick, user->nick, Ptr->name);
599 if (!is_uline(src->server))
602 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,Ptr,reason));
606 /* Set to -1 by OnUserPreKick if explicit allow was set */
607 if (MOD_RESULT != -1)
609 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,Ptr,AC_KICK));
610 if ((MOD_RESULT == ACR_DENY) && (!is_uline(src->server)))
613 if ((MOD_RESULT == ACR_DEFAULT) || (!is_uline(src->server)))
615 if ((cstatus(src,Ptr) < STATUS_HOP) || (cstatus(src,Ptr) < cstatus(user,Ptr)))
617 if (cstatus(src,Ptr) == STATUS_HOP)
619 WriteServ(src->fd,"482 %s %s :You must be a channel operator",src->nick, Ptr->name);
623 WriteServ(src->fd,"482 %s %s :You must be at least a half-operator",src->nick, Ptr->name);
632 FOREACH_MOD(I_OnUserKick,OnUserKick(src,user,Ptr,reason));
634 for (unsigned int i =0; i < user->chans.size(); i++)
636 /* zap it from the channel list of the user */
637 if (user->chans[i].channel)
639 if (!strcasecmp(user->chans[i].channel->name,Ptr->name))
641 WriteChannel(Ptr,src,"KICK %s %s :%s",Ptr->name, user->nick, reason);
642 user->chans[i].uc_modes = 0;
643 user->chans[i].channel = NULL;
644 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
650 Ptr->DelUser((char*)user);
652 /* if there are no users left on the channel */
655 chan_hash::iterator iter = chanlist.find(Ptr->name);
657 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
659 /* kill the record */
660 if (iter != chanlist.end())
662 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
663 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
665 chanlist.erase(iter);