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();
79 void chanrec::SetCustomMode(char mode,bool mode_on)
81 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::IsModeSet(char mode)
113 return modes[mode-65];
116 std::string chanrec::GetModeParameter(char mode)
122 else if (mode == 'l')
124 return ConvToStr(this->limit);
128 std::map<char,char*>::iterator n = custom_mode_params.find(mode);
129 if (n != custom_mode_params.end())
137 long chanrec::GetUserCounter()
139 return (this->internal_userlist.size());
142 void chanrec::AddUser(userrec* user)
144 internal_userlist[user] = user;
147 unsigned long chanrec::DelUser(userrec* user)
149 CUList::iterator a = internal_userlist.find(user);
150 if (a != internal_userlist.end())
152 internal_userlist.erase(a);
153 /* And tidy any others... */
155 DelHalfoppedUser(user);
157 return internal_userlist.size();
159 return internal_userlist.size();
162 bool chanrec::HasUser(userrec* user)
164 return (internal_userlist.find(user) != internal_userlist.end());
167 void chanrec::AddOppedUser(userrec* user)
169 internal_op_userlist[user] = user;
172 void chanrec::DelOppedUser(userrec* user)
174 CUList::iterator a = internal_op_userlist.find(user);
175 if (a != internal_op_userlist.end())
177 internal_op_userlist.erase(a);
182 void chanrec::AddHalfoppedUser(userrec* user)
184 internal_halfop_userlist[user] = user;
187 void chanrec::DelHalfoppedUser(userrec* user)
189 CUList::iterator a = internal_halfop_userlist.find(user);
190 if (a != internal_halfop_userlist.end())
192 internal_halfop_userlist.erase(a);
197 void chanrec::AddVoicedUser(userrec* user)
199 internal_voice_userlist[user] = user;
202 void chanrec::DelVoicedUser(userrec* user)
204 CUList::iterator a = internal_voice_userlist.find(user);
205 if (a != internal_voice_userlist.end())
207 internal_voice_userlist.erase(a);
212 CUList* chanrec::GetUsers()
214 return &internal_userlist;
217 CUList* chanrec::GetOppedUsers()
219 return &internal_op_userlist;
222 CUList* chanrec::GetHalfoppedUsers()
224 return &internal_halfop_userlist;
227 CUList* chanrec::GetVoicedUsers()
229 return &internal_voice_userlist;
233 * add a channel to a user, creating the record for it if needed and linking
234 * it to the user record
237 chanrec* add_channel(userrec *user, const char* cn, const char* key, bool override)
239 if ((!user) || (!cn))
241 log(DEFAULT,"*** BUG *** add_channel was given an invalid parameter");
248 strlcpy(cname,cn,CHANMAX);
249 log(DEBUG,"cname='%s' cn='%s'",cname,cn);
251 log(DEBUG,"add_channel: %s %s",user->nick,cname);
253 chanrec* Ptr = FindChan(cname);
260 FOREACH_RESULT(I_OnUserPreJoin,OnUserPreJoin(user,NULL,cname));
265 /* create a new one */
266 chanlist[cname] = new chanrec();
267 strlcpy(chanlist[cname]->name, cname,CHANMAX);
268 chanlist[cname]->modes[CM_TOPICLOCK] = chanlist[cname]->modes[CM_NOEXTERNAL] = 1;
269 //chanlist[cname]->binarymodes = CM_TOPICLOCK | CM_NOEXTERNAL;
270 chanlist[cname]->created = TIME;
271 *chanlist[cname]->topic = 0;
272 strlcpy(chanlist[cname]->setby, user->nick,NICKMAX-1);
273 chanlist[cname]->topicset = 0;
274 Ptr = chanlist[cname];
275 log(DEBUG,"add_channel: created: %s",cname);
277 * set created to 2 to indicate user
278 * is the first in the channel
279 * and should be given ops
285 /* Already on the channel */
286 if (Ptr->HasUser(user))
290 * remote users are allowed us to bypass channel modes
291 * and bans (used by servers)
293 if (IS_LOCAL(user)) /* was a check on fd > -1 */
296 FOREACH_RESULT(I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname));
301 else if (MOD_RESULT == 0)
306 FOREACH_RESULT(I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
311 log(DEBUG,"add_channel: no key given in JOIN");
312 WriteServ(user->fd,"475 %s %s :Cannot join channel (Requires key)",user->nick, Ptr->name);
317 if (strcmp(key,Ptr->key))
319 log(DEBUG,"add_channel: bad key given in JOIN");
320 WriteServ(user->fd,"475 %s %s :Cannot join channel (Incorrect key)",user->nick, Ptr->name);
326 if (Ptr->modes[CM_INVITEONLY])
329 irc::string xname(Ptr->name);
330 FOREACH_RESULT(I_OnCheckInvite,OnCheckInvite(user, Ptr));
333 log(DEBUG,"add_channel: channel is +i");
334 if (user->IsInvited(xname))
336 /* user was invited to channel */
337 /* there may be an optional channel NOTICE here */
341 WriteServ(user->fd,"473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
345 user->RemoveInvite(xname);
350 FOREACH_RESULT(I_OnCheckLimit,OnCheckLimit(user, Ptr));
353 if (usercount(Ptr) >= Ptr->limit)
355 WriteServ(user->fd,"471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
360 if (Ptr->bans.size())
362 log(DEBUG,"add_channel: about to walk banlist");
364 FOREACH_RESULT(I_OnCheckBan,OnCheckBan(user, Ptr));
367 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
369 if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match((char*)inet_ntoa(user->ip4),i->data)))
371 WriteServ(user->fd,"474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
381 log(DEBUG,"Overridden checks");
386 log(DEBUG,"Passed channel checks");
388 for (std::vector<ucrec*>::const_iterator index = user->chans.begin(); index != user->chans.end(); index++)
390 if ((ucrec*)(*index)->channel == NULL)
392 return ForceChan(Ptr,(ucrec*)(*index),user,created);
397 * XXX: If the user is an oper here, we can just extend their user->chans vector by one
398 * and put the channel in here. Same for remote users which are not bound by
399 * the channel limits. Otherwise, nope, youre boned.
401 if (!IS_LOCAL(user)) /* was a check on fd < 0 */
403 ucrec* a = new ucrec();
404 chanrec* c = ForceChan(Ptr,a,user,created);
405 user->chans.push_back(a);
408 else if (*user->oper)
410 /* Oper allows extension up to the OPERMAXCHANS value */
411 if (user->chans.size() < OPERMAXCHANS)
413 ucrec* a = new ucrec();
414 chanrec* c = ForceChan(Ptr,a,user,created);
415 user->chans.push_back(a);
420 log(DEBUG,"add_channel: user channel max exceeded: %s %s",user->nick,cname);
421 WriteServ(user->fd,"405 %s %s :You are on too many channels",user->nick, cname);
425 log(DEBUG,"BLAMMO, Whacking channel.");
426 /* Things went seriously pear shaped, so take this away. bwahaha. */
427 chan_hash::iterator n = chanlist.find(cname);
428 if (n != chanlist.end())
433 for (unsigned int index =0; index < user->chans.size(); index++)
435 if (user->chans[index]->channel == Ptr)
437 user->chans[index]->channel = NULL;
438 user->chans[index]->uc_modes = 0;
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(user);
461 WriteChannel(Ptr,user,"JOIN :%s",Ptr->name);
463 /* Major improvement by Brain - we dont need to be calculating all this pointlessly for remote users */
466 log(DEBUG,"Sent JOIN to client");
469 WriteServ(user->fd,"332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
470 WriteServ(user->fd,"333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
473 WriteServ(user->fd,"366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
475 FOREACH_MOD(I_OnUserJoin,OnUserJoin(user,Ptr));
480 *remove a channel from a users record, and remove the record from memory
481 * if the channel has become empty
484 chanrec* del_channel(userrec *user, const char* cname, const char* reason, bool local)
486 if ((!user) || (!cname))
488 log(DEFAULT,"*** BUG *** del_channel was given an invalid parameter");
492 chanrec* Ptr = FindChan(cname);
497 log(DEBUG,"del_channel: removing: %s %s",user->nick,Ptr->name);
499 for (unsigned int i =0; i < user->chans.size(); i++)
501 /* zap it from the channel list of the user */
502 if (user->chans[i]->channel == Ptr)
506 FOREACH_MOD(I_OnUserPart,OnUserPart(user,Ptr,reason));
507 WriteChannel(Ptr,user,"PART %s :%s",Ptr->name, reason);
511 FOREACH_MOD(I_OnUserPart,OnUserPart(user,Ptr,""));
512 WriteChannel(Ptr,user,"PART :%s",Ptr->name);
514 user->chans[i]->uc_modes = 0;
515 user->chans[i]->channel = NULL;
516 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
523 /* if there are no users left on the channel */
526 chan_hash::iterator iter = chanlist.find(Ptr->name);
528 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
530 /* kill the record */
531 if (iter != chanlist.end())
533 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
534 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
536 chanlist.erase(iter);
543 void server_kick_channel(userrec* user, chanrec* Ptr, char* reason, bool triggerevents)
545 if ((!user) || (!Ptr) || (!reason))
552 if (!Ptr->HasUser(user))
561 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL,user,Ptr,reason));
564 for (unsigned int i =0; i < user->chans.size(); i++)
566 if ((user->chans[i]->channel) && (user->chans[i]->channel == Ptr))
568 WriteChannelWithServ(Config->ServerName,Ptr,"KICK %s %s :%s",Ptr->name, user->nick, reason);
569 user->chans[i]->uc_modes = 0;
570 user->chans[i]->channel = NULL;
579 chan_hash::iterator iter = chanlist.find(Ptr->name);
580 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
581 /* kill the record */
582 if (iter != chanlist.end())
584 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
585 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
587 chanlist.erase(iter);
592 void kick_channel(userrec *src,userrec *user, chanrec *Ptr, char* reason)
594 if ((!src) || (!user) || (!Ptr) || (!reason))
596 log(DEFAULT,"*** BUG *** kick_channel was given an invalid parameter");
600 log(DEBUG,"kick_channel: removing: %s %s %s",user->nick,Ptr->name,src->nick);
604 if (!Ptr->HasUser(user))
606 WriteServ(src->fd,"441 %s %s %s :They are not on that channel",src->nick, user->nick, Ptr->name);
611 if (!is_uline(src->server))
614 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,Ptr,reason));
618 /* Set to -1 by OnUserPreKick if explicit allow was set */
619 if (MOD_RESULT != -1)
621 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,Ptr,AC_KICK));
622 if ((MOD_RESULT == ACR_DENY) && (!is_uline(src->server)))
625 if ((MOD_RESULT == ACR_DEFAULT) || (!is_uline(src->server)))
627 if ((cstatus(src,Ptr) < STATUS_HOP) || (cstatus(src,Ptr) < cstatus(user,Ptr)))
629 if (cstatus(src,Ptr) == STATUS_HOP)
631 WriteServ(src->fd,"482 %s %s :You must be a channel operator",src->nick, Ptr->name);
635 WriteServ(src->fd,"482 %s %s :You must be at least a half-operator",src->nick, Ptr->name);
644 FOREACH_MOD(I_OnUserKick,OnUserKick(src,user,Ptr,reason));
646 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
648 /* zap it from the channel list of the user */
649 if ((((ucrec*)(*i))->channel) && (((ucrec*)(*i))->channel == Ptr))
651 WriteChannel(Ptr,src,"KICK %s %s :%s",Ptr->name, user->nick, reason);
652 ((ucrec*)(*i))->uc_modes = 0;
653 ((ucrec*)(*i))->channel = NULL;
654 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
659 if (!Ptr->DelUser(user))
660 /* if there are no users left on the channel */
662 chan_hash::iterator iter = chanlist.find(Ptr->name);
664 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
666 /* kill the record */
667 if (iter != chanlist.end())
669 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
670 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
672 chanlist.erase(iter);