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 chanrec* ForceChan(chanrec* Ptr,ucrec &a,userrec* user, int created)
450 /* first user in is given ops */
451 a.uc_modes = UCMODE_OP;
452 Ptr->AddOppedUser((char*)user);
460 Ptr->AddUser((char*)user);
461 WriteChannel(Ptr,user,"JOIN :%s",Ptr->name);
462 log(DEBUG,"Sent JOIN to client");
466 WriteServ(user->fd,"332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
467 WriteServ(user->fd,"333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
471 WriteServ(user->fd,"366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
472 FOREACH_MOD(I_OnUserJoin,OnUserJoin(user,Ptr));
477 *remove a channel from a users record, and remove the record from memory
478 * if the channel has become empty
481 chanrec* del_channel(userrec *user, const char* cname, const char* reason, bool local)
483 if ((!user) || (!cname))
485 log(DEFAULT,"*** BUG *** del_channel was given an invalid parameter");
489 chanrec* Ptr = FindChan(cname);
494 log(DEBUG,"del_channel: removing: %s %s",user->nick,Ptr->name);
496 for (unsigned int i =0; i < user->chans.size(); i++)
498 /* zap it from the channel list of the user */
499 if (user->chans[i].channel == Ptr)
503 FOREACH_MOD(I_OnUserPart,OnUserPart(user,Ptr,reason));
504 WriteChannel(Ptr,user,"PART %s :%s",Ptr->name, reason);
508 FOREACH_MOD(I_OnUserPart,OnUserPart(user,Ptr,""));
509 WriteChannel(Ptr,user,"PART :%s",Ptr->name);
511 user->chans[i].uc_modes = 0;
512 user->chans[i].channel = NULL;
513 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
518 Ptr->DelUser((char*)user);
520 /* if there are no users left on the channel */
523 chan_hash::iterator iter = chanlist.find(Ptr->name);
525 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
527 /* kill the record */
528 if (iter != chanlist.end())
530 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
531 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
533 chanlist.erase(iter);
540 void server_kick_channel(userrec* user, chanrec* Ptr, char* reason, bool triggerevents)
542 if ((!user) || (!Ptr) || (!reason))
549 if (!has_channel(user,Ptr))
558 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL,user,Ptr,reason));
561 for (unsigned int i =0; i < user->chans.size(); i++)
563 if (user->chans[i].channel)
564 if (!strcasecmp(user->chans[i].channel->name,Ptr->name))
566 WriteChannelWithServ(Config->ServerName,Ptr,"KICK %s %s :%s",Ptr->name, user->nick, reason);
567 user->chans[i].uc_modes = 0;
568 user->chans[i].channel = NULL;
573 Ptr->DelUser((char*)user);
577 chan_hash::iterator iter = chanlist.find(Ptr->name);
578 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
579 /* kill the record */
580 if (iter != chanlist.end())
582 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
583 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
585 chanlist.erase(iter);
590 void kick_channel(userrec *src,userrec *user, chanrec *Ptr, char* reason)
592 if ((!src) || (!user) || (!Ptr) || (!reason))
594 log(DEFAULT,"*** BUG *** kick_channel was given an invalid parameter");
598 log(DEBUG,"kick_channel: removing: %s %s %s",user->nick,Ptr->name,src->nick);
602 if (!has_channel(user,Ptr))
604 WriteServ(src->fd,"441 %s %s %s :They are not on that channel",src->nick, user->nick, Ptr->name);
609 if (!is_uline(src->server))
612 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,Ptr,reason));
616 /* Set to -1 by OnUserPreKick if explicit allow was set */
617 if (MOD_RESULT != -1)
619 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,Ptr,AC_KICK));
620 if ((MOD_RESULT == ACR_DENY) && (!is_uline(src->server)))
623 if ((MOD_RESULT == ACR_DEFAULT) || (!is_uline(src->server)))
625 if ((cstatus(src,Ptr) < STATUS_HOP) || (cstatus(src,Ptr) < cstatus(user,Ptr)))
627 if (cstatus(src,Ptr) == STATUS_HOP)
629 WriteServ(src->fd,"482 %s %s :You must be a channel operator",src->nick, Ptr->name);
633 WriteServ(src->fd,"482 %s %s :You must be at least a half-operator",src->nick, Ptr->name);
642 FOREACH_MOD(I_OnUserKick,OnUserKick(src,user,Ptr,reason));
644 for (unsigned int i =0; i < user->chans.size(); i++)
646 /* zap it from the channel list of the user */
647 if (user->chans[i].channel)
649 if (!strcasecmp(user->chans[i].channel->name,Ptr->name))
651 WriteChannel(Ptr,src,"KICK %s %s :%s",Ptr->name, user->nick, reason);
652 user->chans[i].uc_modes = 0;
653 user->chans[i].channel = NULL;
654 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
660 Ptr->DelUser((char*)user);
662 /* if there are no users left on the channel */
665 chan_hash::iterator iter = chanlist.find(Ptr->name);
667 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
669 /* kill the record */
670 if (iter != chanlist.end())
672 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
673 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
675 chanlist.erase(iter);