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)
87 if (!strchr(this->custom_modes,mode))
89 strlcat(custom_modes,m,MAXMODES);
91 log(DEBUG,"Custom mode %c set",mode);
95 std::string a = this->custom_modes;
96 int pos = a.find(mode);
98 strlcpy(this->custom_modes,a.c_str(),MAXMODES);
100 log(DEBUG,"Custom mode %c removed: modelist='%s'",mode,this->custom_modes);
101 this->SetCustomModeParam(mode,"",false);
106 void chanrec::SetCustomModeParam(char mode,char* parameter,bool mode_on)
109 log(DEBUG,"SetCustomModeParam called");
112 strlcpy(M.channel,this->name,CHANMAX);
113 strlcpy(M.parameter,parameter,MAXBUF);
116 log(DEBUG,"Custom mode parameter %c %s added",mode,parameter);
117 custom_mode_params.push_back(M);
121 if (custom_mode_params.size())
123 for (vector<ModeParameter>::iterator i = custom_mode_params.begin(); i < custom_mode_params.end(); i++)
125 if ((i->mode == mode) && (!strcasecmp(this->name,i->channel)))
127 log(DEBUG,"Custom mode parameter %c %s removed",mode,parameter);
128 custom_mode_params.erase(i);
133 log(DEBUG,"*** BUG *** Attempt to remove non-existent mode parameter!");
137 bool chanrec::IsCustomModeSet(char mode)
139 return (strchr(this->custom_modes,mode));
142 std::string chanrec::GetModeParameter(char mode)
144 if (custom_mode_params.size())
146 for (vector<ModeParameter>::iterator i = custom_mode_params.begin(); i < custom_mode_params.end(); i++)
148 if ((i->mode == mode) && (!strcasecmp(this->name,i->channel)))
157 long chanrec::GetUserCounter()
159 return (this->internal_userlist.size());
162 void chanrec::AddUser(char* castuser)
164 internal_userlist[castuser] = castuser;
165 log(DEBUG,"Added casted user to channel's internal list");
168 void chanrec::DelUser(char* castuser)
170 std::map<char*,char*>::iterator a = internal_userlist.find(castuser);
171 if (a != internal_userlist.end())
173 log(DEBUG,"Removed casted user from channel's internal list");
174 internal_userlist.erase(a);
175 /* And tidy any others... */
176 DelOppedUser(castuser);
177 DelHalfoppedUser(castuser);
178 DelVoicedUser(castuser);
183 void chanrec::AddOppedUser(char* castuser)
185 internal_op_userlist[castuser] = castuser;
186 log(DEBUG,"Added casted user to channel's internal list");
189 void chanrec::DelOppedUser(char* castuser)
191 std::map<char*,char*>::iterator a = internal_op_userlist.find(castuser);
192 if (a != internal_op_userlist.end())
194 log(DEBUG,"Removed casted user from channel's internal list");
195 internal_op_userlist.erase(a);
200 void chanrec::AddHalfoppedUser(char* castuser)
202 internal_halfop_userlist[castuser] = castuser;
203 log(DEBUG,"Added casted user to channel's internal list");
206 void chanrec::DelHalfoppedUser(char* castuser)
208 std::map<char*,char*>::iterator a = internal_halfop_userlist.find(castuser);
209 if (a != internal_halfop_userlist.end())
211 log(DEBUG,"Removed casted user from channel's internal list");
212 internal_halfop_userlist.erase(a);
217 void chanrec::AddVoicedUser(char* castuser)
219 internal_voice_userlist[castuser] = castuser;
220 log(DEBUG,"Added casted user to channel's internal list");
223 void chanrec::DelVoicedUser(char* castuser)
225 std::map<char*,char*>::iterator a = internal_voice_userlist.find(castuser);
226 if (a != internal_voice_userlist.end())
228 log(DEBUG,"Removed casted user from channel's internal list");
229 internal_voice_userlist.erase(a);
234 std::map<char*,char*> *chanrec::GetUsers()
236 return &internal_userlist;
239 std::map<char*,char*> *chanrec::GetOppedUsers()
241 return &internal_op_userlist;
244 std::map<char*,char*> *chanrec::GetHalfoppedUsers()
246 return &internal_halfop_userlist;
249 std::map<char*,char*> *chanrec::GetVoicedUsers()
251 return &internal_voice_userlist;
255 * add a channel to a user, creating the record for it if needed and linking
256 * it to the user record
259 chanrec* add_channel(userrec *user, const char* cn, const char* key, bool override)
261 if ((!user) || (!cn))
263 log(DEFAULT,"*** BUG *** add_channel was given an invalid parameter");
270 strlcpy(cname,cn,CHANMAX);
271 log(DEBUG,"cname='%s' cn='%s'",cname,cn);
273 log(DEBUG,"add_channel: %s %s",user->nick,cname);
275 chanrec* Ptr = FindChan(cname);
282 FOREACH_RESULT(I_OnUserPreJoin,OnUserPreJoin(user,NULL,cname));
287 /* create a new one */
288 chanlist[cname] = new chanrec();
289 strlcpy(chanlist[cname]->name, cname,CHANMAX);
290 chanlist[cname]->binarymodes = CM_TOPICLOCK | CM_NOEXTERNAL;
291 chanlist[cname]->created = TIME;
292 *chanlist[cname]->topic = 0;
293 strlcpy(chanlist[cname]->setby, user->nick,NICKMAX);
294 chanlist[cname]->topicset = 0;
295 Ptr = chanlist[cname];
296 log(DEBUG,"add_channel: created: %s",cname);
298 * set created to 2 to indicate user
299 * is the first in the channel
300 * and should be given ops
306 /* Already on the channel */
307 if (has_channel(user,Ptr))
311 * remote users are allowed us to bypass channel modes
312 * and bans (used by servers)
314 if (IS_LOCAL(user)) /* was a check on fd > -1 */
317 FOREACH_RESULT(I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname));
322 else if (MOD_RESULT == 0)
327 FOREACH_RESULT(I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
332 log(DEBUG,"add_channel: no key given in JOIN");
333 WriteServ(user->fd,"475 %s %s :Cannot join channel (Requires key)",user->nick, Ptr->name);
338 if (strcmp(key,Ptr->key))
340 log(DEBUG,"add_channel: bad key given in JOIN");
341 WriteServ(user->fd,"475 %s %s :Cannot join channel (Incorrect key)",user->nick, Ptr->name);
347 if (Ptr->binarymodes & CM_INVITEONLY)
350 irc::string xname(Ptr->name);
351 FOREACH_RESULT(I_OnCheckInvite,OnCheckInvite(user, Ptr));
354 log(DEBUG,"add_channel: channel is +i");
355 if (user->IsInvited(xname))
357 /* user was invited to channel */
358 /* there may be an optional channel NOTICE here */
362 WriteServ(user->fd,"473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
366 user->RemoveInvite(xname);
371 FOREACH_RESULT(I_OnCheckLimit,OnCheckLimit(user, Ptr));
374 if (usercount(Ptr) >= Ptr->limit)
376 WriteServ(user->fd,"471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
381 if (Ptr->bans.size())
383 log(DEBUG,"add_channel: about to walk banlist");
385 FOREACH_RESULT(I_OnCheckBan,OnCheckBan(user, Ptr));
388 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
390 if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match((char*)inet_ntoa(user->ip4),i->data)))
392 WriteServ(user->fd,"474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
402 log(DEBUG,"Overridden checks");
407 log(DEBUG,"Passed channel checks");
409 for (unsigned int index =0; index < user->chans.size(); index++)
411 if (user->chans[index].channel == NULL)
413 return ForceChan(Ptr,user->chans[index],user,created);
418 * XXX: If the user is an oper here, we can just extend their user->chans vector by one
419 * and put the channel in here. Same for remote users which are not bound by
420 * the channel limits. Otherwise, nope, youre boned.
422 if (!IS_LOCAL(user)) /* was a check on fd < 0 */
425 chanrec* c = ForceChan(Ptr,a,user,created);
426 user->chans.push_back(a);
429 else if (*user->oper)
431 /* Oper allows extension up to the OPERMAXCHANS value */
432 if (user->chans.size() < OPERMAXCHANS)
435 chanrec* c = ForceChan(Ptr,a,user,created);
436 user->chans.push_back(a);
441 log(DEBUG,"add_channel: user channel max exceeded: %s %s",user->nick,cname);
442 WriteServ(user->fd,"405 %s %s :You are on too many channels",user->nick, cname);
446 log(DEBUG,"BLAMMO, Whacking channel.");
447 /* Things went seriously pear shaped, so take this away. bwahaha. */
448 chan_hash::iterator n = chanlist.find(cname);
449 if (n != chanlist.end())
451 Ptr->DelUser((char*)user);
454 for (unsigned int index =0; index < user->chans.size(); index++)
456 if (user->chans[index].channel == Ptr)
458 user->chans[index].channel == NULL;
459 user->chans[index].uc_modes = 0;
467 chanrec* ForceChan(chanrec* Ptr,ucrec &a,userrec* user, int created)
471 /* first user in is given ops */
472 a.uc_modes = UCMODE_OP;
473 Ptr->AddOppedUser((char*)user);
481 Ptr->AddUser((char*)user);
482 WriteChannel(Ptr,user,"JOIN :%s",Ptr->name);
483 log(DEBUG,"Sent JOIN to client");
487 WriteServ(user->fd,"332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
488 WriteServ(user->fd,"333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
492 WriteServ(user->fd,"366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
493 FOREACH_MOD(I_OnUserJoin,OnUserJoin(user,Ptr));
498 *remove a channel from a users record, and remove the record from memory
499 * if the channel has become empty
502 chanrec* del_channel(userrec *user, const char* cname, const char* reason, bool local)
504 if ((!user) || (!cname))
506 log(DEFAULT,"*** BUG *** del_channel was given an invalid parameter");
510 chanrec* Ptr = FindChan(cname);
515 log(DEBUG,"del_channel: removing: %s %s",user->nick,Ptr->name);
517 for (unsigned int i =0; i < user->chans.size(); i++)
519 /* zap it from the channel list of the user */
520 if (user->chans[i].channel == Ptr)
524 FOREACH_MOD(I_OnUserPart,OnUserPart(user,Ptr,reason));
525 WriteChannel(Ptr,user,"PART %s :%s",Ptr->name, reason);
529 FOREACH_MOD(I_OnUserPart,OnUserPart(user,Ptr,""));
530 WriteChannel(Ptr,user,"PART :%s",Ptr->name);
532 user->chans[i].uc_modes = 0;
533 user->chans[i].channel = NULL;
534 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
539 Ptr->DelUser((char*)user);
541 /* if there are no users left on the channel */
544 chan_hash::iterator iter = chanlist.find(Ptr->name);
546 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
548 /* kill the record */
549 if (iter != chanlist.end())
551 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
552 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
554 chanlist.erase(iter);
561 void server_kick_channel(userrec* user, chanrec* Ptr, char* reason, bool triggerevents)
563 if ((!user) || (!Ptr) || (!reason))
570 if (!has_channel(user,Ptr))
579 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL,user,Ptr,reason));
582 for (unsigned int i =0; i < user->chans.size(); i++)
584 if (user->chans[i].channel)
585 if (!strcasecmp(user->chans[i].channel->name,Ptr->name))
587 WriteChannelWithServ(Config->ServerName,Ptr,"KICK %s %s :%s",Ptr->name, user->nick, reason);
588 user->chans[i].uc_modes = 0;
589 user->chans[i].channel = NULL;
594 Ptr->DelUser((char*)user);
598 chan_hash::iterator iter = chanlist.find(Ptr->name);
599 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
600 /* kill the record */
601 if (iter != chanlist.end())
603 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
604 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
606 chanlist.erase(iter);
611 void kick_channel(userrec *src,userrec *user, chanrec *Ptr, char* reason)
613 if ((!src) || (!user) || (!Ptr) || (!reason))
615 log(DEFAULT,"*** BUG *** kick_channel was given an invalid parameter");
619 log(DEBUG,"kick_channel: removing: %s %s %s",user->nick,Ptr->name,src->nick);
623 if (!has_channel(user,Ptr))
625 WriteServ(src->fd,"441 %s %s %s :They are not on that channel",src->nick, user->nick, Ptr->name);
630 if (!is_uline(src->server))
633 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,Ptr,reason));
637 /* Set to -1 by OnUserPreKick if explicit allow was set */
638 if (MOD_RESULT != -1)
640 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,Ptr,AC_KICK));
641 if ((MOD_RESULT == ACR_DENY) && (!is_uline(src->server)))
644 if ((MOD_RESULT == ACR_DEFAULT) || (!is_uline(src->server)))
646 if ((cstatus(src,Ptr) < STATUS_HOP) || (cstatus(src,Ptr) < cstatus(user,Ptr)))
648 if (cstatus(src,Ptr) == STATUS_HOP)
650 WriteServ(src->fd,"482 %s %s :You must be a channel operator",src->nick, Ptr->name);
654 WriteServ(src->fd,"482 %s %s :You must be at least a half-operator",src->nick, Ptr->name);
663 FOREACH_MOD(I_OnUserKick,OnUserKick(src,user,Ptr,reason));
665 for (unsigned int i =0; i < user->chans.size(); i++)
667 /* zap it from the channel list of the user */
668 if (user->chans[i].channel)
670 if (!strcasecmp(user->chans[i].channel->name,Ptr->name))
672 WriteChannel(Ptr,src,"KICK %s %s :%s",Ptr->name, user->nick, reason);
673 user->chans[i].uc_modes = 0;
674 user->chans[i].channel = NULL;
675 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
681 Ptr->DelUser((char*)user);
683 /* if there are no users left on the channel */
686 chan_hash::iterator iter = chanlist.find(Ptr->name);
688 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
690 /* kill the record */
691 if (iter != chanlist.end())
693 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
694 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
696 chanlist.erase(iter);