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 = 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(userrec* user)
133 internal_userlist[user] = user;
136 unsigned long chanrec::DelUser(userrec* user)
138 CUList::iterator a = internal_userlist.find(user);
139 if (a != internal_userlist.end())
141 internal_userlist.erase(a);
142 /* And tidy any others... */
144 DelHalfoppedUser(user);
146 return internal_userlist.size();
148 return internal_userlist.size();
151 bool chanrec::HasUser(userrec* user)
153 return (internal_userlist.find(user) != internal_userlist.end());
156 void chanrec::AddOppedUser(userrec* user)
158 internal_op_userlist[user] = user;
161 void chanrec::DelOppedUser(userrec* user)
163 CUList::iterator a = internal_op_userlist.find(user);
164 if (a != internal_op_userlist.end())
166 internal_op_userlist.erase(a);
171 void chanrec::AddHalfoppedUser(userrec* user)
173 internal_halfop_userlist[user] = user;
176 void chanrec::DelHalfoppedUser(userrec* user)
178 CUList::iterator a = internal_halfop_userlist.find(user);
179 if (a != internal_halfop_userlist.end())
181 internal_halfop_userlist.erase(a);
186 void chanrec::AddVoicedUser(userrec* user)
188 internal_voice_userlist[user] = user;
191 void chanrec::DelVoicedUser(userrec* user)
193 CUList::iterator a = internal_voice_userlist.find(user);
194 if (a != internal_voice_userlist.end())
196 internal_voice_userlist.erase(a);
201 CUList* chanrec::GetUsers()
203 return &internal_userlist;
206 CUList* chanrec::GetOppedUsers()
208 return &internal_op_userlist;
211 CUList* chanrec::GetHalfoppedUsers()
213 return &internal_halfop_userlist;
216 CUList* chanrec::GetVoicedUsers()
218 return &internal_voice_userlist;
222 * add a channel to a user, creating the record for it if needed and linking
223 * it to the user record
226 chanrec* add_channel(userrec *user, const char* cn, const char* key, bool override)
228 if ((!user) || (!cn))
230 log(DEFAULT,"*** BUG *** add_channel was given an invalid parameter");
237 strlcpy(cname,cn,CHANMAX);
238 log(DEBUG,"cname='%s' cn='%s'",cname,cn);
240 log(DEBUG,"add_channel: %s %s",user->nick,cname);
242 chanrec* Ptr = FindChan(cname);
249 FOREACH_RESULT(I_OnUserPreJoin,OnUserPreJoin(user,NULL,cname));
254 /* create a new one */
255 chanlist[cname] = new chanrec();
256 strlcpy(chanlist[cname]->name, cname,CHANMAX);
257 chanlist[cname]->custom_modes[CM_TOPICLOCK] = chanlist[cname]->custom_modes[CM_NOEXTERNAL] = 1;
258 //chanlist[cname]->binarymodes = CM_TOPICLOCK | CM_NOEXTERNAL;
259 chanlist[cname]->created = TIME;
260 *chanlist[cname]->topic = 0;
261 strlcpy(chanlist[cname]->setby, user->nick,NICKMAX-1);
262 chanlist[cname]->topicset = 0;
263 Ptr = chanlist[cname];
264 log(DEBUG,"add_channel: created: %s",cname);
266 * set created to 2 to indicate user
267 * is the first in the channel
268 * and should be given ops
274 /* Already on the channel */
275 if (Ptr->HasUser(user))
279 * remote users are allowed us to bypass channel modes
280 * and bans (used by servers)
282 if (IS_LOCAL(user)) /* was a check on fd > -1 */
285 FOREACH_RESULT(I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname));
290 else if (MOD_RESULT == 0)
295 FOREACH_RESULT(I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
300 log(DEBUG,"add_channel: no key given in JOIN");
301 WriteServ(user->fd,"475 %s %s :Cannot join channel (Requires key)",user->nick, Ptr->name);
306 if (strcmp(key,Ptr->key))
308 log(DEBUG,"add_channel: bad key given in JOIN");
309 WriteServ(user->fd,"475 %s %s :Cannot join channel (Incorrect key)",user->nick, Ptr->name);
315 if (Ptr->custom_modes[CM_INVITEONLY])
318 irc::string xname(Ptr->name);
319 FOREACH_RESULT(I_OnCheckInvite,OnCheckInvite(user, Ptr));
322 log(DEBUG,"add_channel: channel is +i");
323 if (user->IsInvited(xname))
325 /* user was invited to channel */
326 /* there may be an optional channel NOTICE here */
330 WriteServ(user->fd,"473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
334 user->RemoveInvite(xname);
339 FOREACH_RESULT(I_OnCheckLimit,OnCheckLimit(user, Ptr));
342 if (usercount(Ptr) >= Ptr->limit)
344 WriteServ(user->fd,"471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
349 if (Ptr->bans.size())
351 log(DEBUG,"add_channel: about to walk banlist");
353 FOREACH_RESULT(I_OnCheckBan,OnCheckBan(user, Ptr));
356 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
358 if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match((char*)inet_ntoa(user->ip4),i->data)))
360 WriteServ(user->fd,"474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
370 log(DEBUG,"Overridden checks");
375 log(DEBUG,"Passed channel checks");
377 for (std::vector<ucrec*>::const_iterator index = user->chans.begin(); index != user->chans.end(); index++)
379 if ((ucrec*)(*index)->channel == NULL)
381 return ForceChan(Ptr,(ucrec*)(*index),user,created);
386 * XXX: If the user is an oper here, we can just extend their user->chans vector by one
387 * and put the channel in here. Same for remote users which are not bound by
388 * the channel limits. Otherwise, nope, youre boned.
390 if (!IS_LOCAL(user)) /* was a check on fd < 0 */
392 ucrec* a = new ucrec();
393 chanrec* c = ForceChan(Ptr,a,user,created);
394 user->chans.push_back(a);
397 else if (*user->oper)
399 /* Oper allows extension up to the OPERMAXCHANS value */
400 if (user->chans.size() < OPERMAXCHANS)
402 ucrec* a = new ucrec();
403 chanrec* c = ForceChan(Ptr,a,user,created);
404 user->chans.push_back(a);
409 log(DEBUG,"add_channel: user channel max exceeded: %s %s",user->nick,cname);
410 WriteServ(user->fd,"405 %s %s :You are on too many channels",user->nick, cname);
414 log(DEBUG,"BLAMMO, Whacking channel.");
415 /* Things went seriously pear shaped, so take this away. bwahaha. */
416 chan_hash::iterator n = chanlist.find(cname);
417 if (n != chanlist.end())
422 for (unsigned int index =0; index < user->chans.size(); index++)
424 if (user->chans[index]->channel == Ptr)
426 user->chans[index]->channel = NULL;
427 user->chans[index]->uc_modes = 0;
435 chanrec* ForceChan(chanrec* Ptr,ucrec *a,userrec* user, int created)
439 /* first user in is given ops */
440 a->uc_modes = UCMODE_OP;
441 Ptr->AddOppedUser(user);
450 WriteChannel(Ptr,user,"JOIN :%s",Ptr->name);
452 /* Major improvement by Brain - we dont need to be calculating all this pointlessly for remote users */
455 log(DEBUG,"Sent JOIN to client");
458 WriteServ(user->fd,"332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
459 WriteServ(user->fd,"333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
462 WriteServ(user->fd,"366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
464 FOREACH_MOD(I_OnUserJoin,OnUserJoin(user,Ptr));
469 *remove a channel from a users record, and remove the record from memory
470 * if the channel has become empty
473 chanrec* del_channel(userrec *user, const char* cname, const char* reason, bool local)
475 if ((!user) || (!cname))
477 log(DEFAULT,"*** BUG *** del_channel was given an invalid parameter");
481 chanrec* Ptr = FindChan(cname);
486 log(DEBUG,"del_channel: removing: %s %s",user->nick,Ptr->name);
488 for (unsigned int i =0; i < user->chans.size(); i++)
490 /* zap it from the channel list of the user */
491 if (user->chans[i]->channel == Ptr)
495 FOREACH_MOD(I_OnUserPart,OnUserPart(user,Ptr,reason));
496 WriteChannel(Ptr,user,"PART %s :%s",Ptr->name, reason);
500 FOREACH_MOD(I_OnUserPart,OnUserPart(user,Ptr,""));
501 WriteChannel(Ptr,user,"PART :%s",Ptr->name);
503 user->chans[i]->uc_modes = 0;
504 user->chans[i]->channel = NULL;
505 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
512 /* if there are no users left on the channel */
515 chan_hash::iterator iter = chanlist.find(Ptr->name);
517 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
519 /* kill the record */
520 if (iter != chanlist.end())
522 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
523 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
525 chanlist.erase(iter);
532 void server_kick_channel(userrec* user, chanrec* Ptr, char* reason, bool triggerevents)
534 if ((!user) || (!Ptr) || (!reason))
541 if (!Ptr->HasUser(user))
550 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL,user,Ptr,reason));
553 for (unsigned int i =0; i < user->chans.size(); i++)
555 if ((user->chans[i]->channel) && (user->chans[i]->channel == Ptr))
557 WriteChannelWithServ(Config->ServerName,Ptr,"KICK %s %s :%s",Ptr->name, user->nick, reason);
558 user->chans[i]->uc_modes = 0;
559 user->chans[i]->channel = NULL;
568 chan_hash::iterator iter = chanlist.find(Ptr->name);
569 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
570 /* kill the record */
571 if (iter != chanlist.end())
573 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
574 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
576 chanlist.erase(iter);
581 void kick_channel(userrec *src,userrec *user, chanrec *Ptr, char* reason)
583 if ((!src) || (!user) || (!Ptr) || (!reason))
585 log(DEFAULT,"*** BUG *** kick_channel was given an invalid parameter");
589 log(DEBUG,"kick_channel: removing: %s %s %s",user->nick,Ptr->name,src->nick);
593 if (!Ptr->HasUser(user))
595 WriteServ(src->fd,"441 %s %s %s :They are not on that channel",src->nick, user->nick, Ptr->name);
600 if (!is_uline(src->server))
603 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,Ptr,reason));
607 /* Set to -1 by OnUserPreKick if explicit allow was set */
608 if (MOD_RESULT != -1)
610 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,Ptr,AC_KICK));
611 if ((MOD_RESULT == ACR_DENY) && (!is_uline(src->server)))
614 if ((MOD_RESULT == ACR_DEFAULT) || (!is_uline(src->server)))
616 if ((cstatus(src,Ptr) < STATUS_HOP) || (cstatus(src,Ptr) < cstatus(user,Ptr)))
618 if (cstatus(src,Ptr) == STATUS_HOP)
620 WriteServ(src->fd,"482 %s %s :You must be a channel operator",src->nick, Ptr->name);
624 WriteServ(src->fd,"482 %s %s :You must be at least a half-operator",src->nick, Ptr->name);
633 FOREACH_MOD(I_OnUserKick,OnUserKick(src,user,Ptr,reason));
635 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
637 /* zap it from the channel list of the user */
638 if ((((ucrec*)(*i))->channel) && (((ucrec*)(*i))->channel == Ptr))
640 WriteChannel(Ptr,src,"KICK %s %s :%s",Ptr->name, user->nick, reason);
641 ((ucrec*)(*i))->uc_modes = 0;
642 ((ucrec*)(*i))->channel = NULL;
643 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
648 if (!Ptr->DelUser(user))
649 /* if there are no users left on the channel */
651 chan_hash::iterator iter = chanlist.find(Ptr->name);
653 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
655 /* kill the record */
656 if (iter != chanlist.end())
658 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
659 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
661 chanlist.erase(iter);