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 if (charremove(this->custom_modes,mode))
113 log(DEBUG,"Custom mode %c removed: modelist='%s'",mode,this->custom_modes);
114 /* Only call this if we found the mode */
115 this->SetCustomModeParam(mode,"",false);
121 void chanrec::SetCustomModeParam(char mode,char* parameter,bool mode_on)
124 log(DEBUG,"SetCustomModeParam called");
127 strlcpy(M.channel,this->name,CHANMAX);
128 strlcpy(M.parameter,parameter,MAXBUF);
131 log(DEBUG,"Custom mode parameter %c %s added",mode,parameter);
132 custom_mode_params.push_back(M);
136 if (custom_mode_params.size())
138 for (vector<ModeParameter>::iterator i = custom_mode_params.begin(); i < custom_mode_params.end(); i++)
140 if ((i->mode == mode) && (!strcasecmp(this->name,i->channel)))
142 log(DEBUG,"Custom mode parameter %c %s removed",mode,parameter);
143 custom_mode_params.erase(i);
151 bool chanrec::IsCustomModeSet(char mode)
153 return (strchr(this->custom_modes,mode));
156 std::string chanrec::GetModeParameter(char mode)
158 if (custom_mode_params.size())
160 for (vector<ModeParameter>::iterator i = custom_mode_params.begin(); i < custom_mode_params.end(); i++)
162 if ((i->mode == mode) && (!strcasecmp(this->name,i->channel)))
171 long chanrec::GetUserCounter()
173 return (this->internal_userlist.size());
176 void chanrec::AddUser(char* castuser)
178 internal_userlist[castuser] = castuser;
179 log(DEBUG,"Added casted user to channel's internal list");
182 void chanrec::DelUser(char* castuser)
184 std::map<char*,char*>::iterator a = internal_userlist.find(castuser);
185 if (a != internal_userlist.end())
187 log(DEBUG,"Removed casted user from channel's internal list");
188 internal_userlist.erase(a);
189 /* And tidy any others... */
190 DelOppedUser(castuser);
191 DelHalfoppedUser(castuser);
192 DelVoicedUser(castuser);
197 void chanrec::AddOppedUser(char* castuser)
199 internal_op_userlist[castuser] = castuser;
200 log(DEBUG,"Added casted user to channel's internal list");
203 void chanrec::DelOppedUser(char* castuser)
205 std::map<char*,char*>::iterator a = internal_op_userlist.find(castuser);
206 if (a != internal_op_userlist.end())
208 log(DEBUG,"Removed casted user from channel's internal list");
209 internal_op_userlist.erase(a);
214 void chanrec::AddHalfoppedUser(char* castuser)
216 internal_halfop_userlist[castuser] = castuser;
217 log(DEBUG,"Added casted user to channel's internal list");
220 void chanrec::DelHalfoppedUser(char* castuser)
222 std::map<char*,char*>::iterator a = internal_halfop_userlist.find(castuser);
223 if (a != internal_halfop_userlist.end())
225 log(DEBUG,"Removed casted user from channel's internal list");
226 internal_halfop_userlist.erase(a);
231 void chanrec::AddVoicedUser(char* castuser)
233 internal_voice_userlist[castuser] = castuser;
234 log(DEBUG,"Added casted user to channel's internal list");
237 void chanrec::DelVoicedUser(char* castuser)
239 std::map<char*,char*>::iterator a = internal_voice_userlist.find(castuser);
240 if (a != internal_voice_userlist.end())
242 log(DEBUG,"Removed casted user from channel's internal list");
243 internal_voice_userlist.erase(a);
248 std::map<char*,char*> *chanrec::GetUsers()
250 return &internal_userlist;
253 std::map<char*,char*> *chanrec::GetOppedUsers()
255 return &internal_op_userlist;
258 std::map<char*,char*> *chanrec::GetHalfoppedUsers()
260 return &internal_halfop_userlist;
263 std::map<char*,char*> *chanrec::GetVoicedUsers()
265 return &internal_voice_userlist;
269 * add a channel to a user, creating the record for it if needed and linking
270 * it to the user record
273 chanrec* add_channel(userrec *user, const char* cn, const char* key, bool override)
275 if ((!user) || (!cn))
277 log(DEFAULT,"*** BUG *** add_channel was given an invalid parameter");
284 strlcpy(cname,cn,CHANMAX);
285 log(DEBUG,"cname='%s' cn='%s'",cname,cn);
287 log(DEBUG,"add_channel: %s %s",user->nick,cname);
289 chanrec* Ptr = FindChan(cname);
296 FOREACH_RESULT(I_OnUserPreJoin,OnUserPreJoin(user,NULL,cname));
301 /* create a new one */
302 chanlist[cname] = new chanrec();
303 strlcpy(chanlist[cname]->name, cname,CHANMAX);
304 chanlist[cname]->binarymodes = CM_TOPICLOCK | CM_NOEXTERNAL;
305 chanlist[cname]->created = TIME;
306 *chanlist[cname]->topic = 0;
307 strlcpy(chanlist[cname]->setby, user->nick,NICKMAX-1);
308 chanlist[cname]->topicset = 0;
309 Ptr = chanlist[cname];
310 log(DEBUG,"add_channel: created: %s",cname);
312 * set created to 2 to indicate user
313 * is the first in the channel
314 * and should be given ops
320 /* Already on the channel */
321 if (has_channel(user,Ptr))
325 * remote users are allowed us to bypass channel modes
326 * and bans (used by servers)
328 if (IS_LOCAL(user)) /* was a check on fd > -1 */
331 FOREACH_RESULT(I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname));
336 else if (MOD_RESULT == 0)
341 FOREACH_RESULT(I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
346 log(DEBUG,"add_channel: no key given in JOIN");
347 WriteServ(user->fd,"475 %s %s :Cannot join channel (Requires key)",user->nick, Ptr->name);
352 if (strcmp(key,Ptr->key))
354 log(DEBUG,"add_channel: bad key given in JOIN");
355 WriteServ(user->fd,"475 %s %s :Cannot join channel (Incorrect key)",user->nick, Ptr->name);
361 if (Ptr->binarymodes & CM_INVITEONLY)
364 irc::string xname(Ptr->name);
365 FOREACH_RESULT(I_OnCheckInvite,OnCheckInvite(user, Ptr));
368 log(DEBUG,"add_channel: channel is +i");
369 if (user->IsInvited(xname))
371 /* user was invited to channel */
372 /* there may be an optional channel NOTICE here */
376 WriteServ(user->fd,"473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
380 user->RemoveInvite(xname);
385 FOREACH_RESULT(I_OnCheckLimit,OnCheckLimit(user, Ptr));
388 if (usercount(Ptr) >= Ptr->limit)
390 WriteServ(user->fd,"471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
395 if (Ptr->bans.size())
397 log(DEBUG,"add_channel: about to walk banlist");
399 FOREACH_RESULT(I_OnCheckBan,OnCheckBan(user, Ptr));
402 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
404 if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match((char*)inet_ntoa(user->ip4),i->data)))
406 WriteServ(user->fd,"474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
416 log(DEBUG,"Overridden checks");
421 log(DEBUG,"Passed channel checks");
423 for (unsigned int index =0; index < user->chans.size(); index++)
425 if (user->chans[index].channel == NULL)
427 return ForceChan(Ptr,user->chans[index],user,created);
432 * XXX: If the user is an oper here, we can just extend their user->chans vector by one
433 * and put the channel in here. Same for remote users which are not bound by
434 * the channel limits. Otherwise, nope, youre boned.
436 if (!IS_LOCAL(user)) /* was a check on fd < 0 */
439 chanrec* c = ForceChan(Ptr,a,user,created);
440 user->chans.push_back(a);
443 else if (*user->oper)
445 /* Oper allows extension up to the OPERMAXCHANS value */
446 if (user->chans.size() < OPERMAXCHANS)
449 chanrec* c = ForceChan(Ptr,a,user,created);
450 user->chans.push_back(a);
455 log(DEBUG,"add_channel: user channel max exceeded: %s %s",user->nick,cname);
456 WriteServ(user->fd,"405 %s %s :You are on too many channels",user->nick, cname);
460 log(DEBUG,"BLAMMO, Whacking channel.");
461 /* Things went seriously pear shaped, so take this away. bwahaha. */
462 chan_hash::iterator n = chanlist.find(cname);
463 if (n != chanlist.end())
465 Ptr->DelUser((char*)user);
468 for (unsigned int index =0; index < user->chans.size(); index++)
470 if (user->chans[index].channel == Ptr)
472 user->chans[index].channel = NULL;
473 user->chans[index].uc_modes = 0;
481 chanrec* ForceChan(chanrec* Ptr,ucrec &a,userrec* user, int created)
485 /* first user in is given ops */
486 a.uc_modes = UCMODE_OP;
487 Ptr->AddOppedUser((char*)user);
495 Ptr->AddUser((char*)user);
496 WriteChannel(Ptr,user,"JOIN :%s",Ptr->name);
497 log(DEBUG,"Sent JOIN to client");
501 WriteServ(user->fd,"332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
502 WriteServ(user->fd,"333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
506 WriteServ(user->fd,"366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
507 FOREACH_MOD(I_OnUserJoin,OnUserJoin(user,Ptr));
512 *remove a channel from a users record, and remove the record from memory
513 * if the channel has become empty
516 chanrec* del_channel(userrec *user, const char* cname, const char* reason, bool local)
518 if ((!user) || (!cname))
520 log(DEFAULT,"*** BUG *** del_channel was given an invalid parameter");
524 chanrec* Ptr = FindChan(cname);
529 log(DEBUG,"del_channel: removing: %s %s",user->nick,Ptr->name);
531 for (unsigned int i =0; i < user->chans.size(); i++)
533 /* zap it from the channel list of the user */
534 if (user->chans[i].channel == Ptr)
538 FOREACH_MOD(I_OnUserPart,OnUserPart(user,Ptr,reason));
539 WriteChannel(Ptr,user,"PART %s :%s",Ptr->name, reason);
543 FOREACH_MOD(I_OnUserPart,OnUserPart(user,Ptr,""));
544 WriteChannel(Ptr,user,"PART :%s",Ptr->name);
546 user->chans[i].uc_modes = 0;
547 user->chans[i].channel = NULL;
548 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
553 Ptr->DelUser((char*)user);
555 /* if there are no users left on the channel */
558 chan_hash::iterator iter = chanlist.find(Ptr->name);
560 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
562 /* kill the record */
563 if (iter != chanlist.end())
565 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
566 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
568 chanlist.erase(iter);
575 void server_kick_channel(userrec* user, chanrec* Ptr, char* reason, bool triggerevents)
577 if ((!user) || (!Ptr) || (!reason))
584 if (!has_channel(user,Ptr))
593 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL,user,Ptr,reason));
596 for (unsigned int i =0; i < user->chans.size(); i++)
598 if (user->chans[i].channel)
599 if (!strcasecmp(user->chans[i].channel->name,Ptr->name))
601 WriteChannelWithServ(Config->ServerName,Ptr,"KICK %s %s :%s",Ptr->name, user->nick, reason);
602 user->chans[i].uc_modes = 0;
603 user->chans[i].channel = NULL;
608 Ptr->DelUser((char*)user);
612 chan_hash::iterator iter = chanlist.find(Ptr->name);
613 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
614 /* kill the record */
615 if (iter != chanlist.end())
617 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
618 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
620 chanlist.erase(iter);
625 void kick_channel(userrec *src,userrec *user, chanrec *Ptr, char* reason)
627 if ((!src) || (!user) || (!Ptr) || (!reason))
629 log(DEFAULT,"*** BUG *** kick_channel was given an invalid parameter");
633 log(DEBUG,"kick_channel: removing: %s %s %s",user->nick,Ptr->name,src->nick);
637 if (!has_channel(user,Ptr))
639 WriteServ(src->fd,"441 %s %s %s :They are not on that channel",src->nick, user->nick, Ptr->name);
644 if (!is_uline(src->server))
647 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,Ptr,reason));
651 /* Set to -1 by OnUserPreKick if explicit allow was set */
652 if (MOD_RESULT != -1)
654 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,Ptr,AC_KICK));
655 if ((MOD_RESULT == ACR_DENY) && (!is_uline(src->server)))
658 if ((MOD_RESULT == ACR_DEFAULT) || (!is_uline(src->server)))
660 if ((cstatus(src,Ptr) < STATUS_HOP) || (cstatus(src,Ptr) < cstatus(user,Ptr)))
662 if (cstatus(src,Ptr) == STATUS_HOP)
664 WriteServ(src->fd,"482 %s %s :You must be a channel operator",src->nick, Ptr->name);
668 WriteServ(src->fd,"482 %s %s :You must be at least a half-operator",src->nick, Ptr->name);
677 FOREACH_MOD(I_OnUserKick,OnUserKick(src,user,Ptr,reason));
679 for (unsigned int i =0; i < user->chans.size(); i++)
681 /* zap it from the channel list of the user */
682 if (user->chans[i].channel)
684 if (!strcasecmp(user->chans[i].channel->name,Ptr->name))
686 WriteChannel(Ptr,src,"KICK %s %s :%s",Ptr->name, user->nick, reason);
687 user->chans[i].uc_modes = 0;
688 user->chans[i].channel = NULL;
689 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
695 Ptr->DelUser((char*)user);
697 /* if there are no users left on the channel */
700 chan_hash::iterator iter = chanlist.find(Ptr->name);
702 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
704 /* kill the record */
705 if (iter != chanlist.end())
707 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
708 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
710 chanlist.erase(iter);