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 "configreader.h"
22 #include <sys/errno.h>
23 #include <sys/ioctl.h>
24 #include <sys/utsname.h>
42 #include "inspstring.h"
43 #include "helperfuncs.h"
46 extern ServerConfig* Config;
49 extern std::vector<Module*> modules;
50 extern std::vector<ircd_module*> factory;
51 extern int WHOWAS_STALE;
52 extern int WHOWAS_MAX;
54 extern chan_hash chanlist;
57 chanrec* ForceChan(chanrec* Ptr,ucrec *a,userrec* user, int created);
61 *name = *topic = *setby = *key = 0;
62 created = topicset = limit = 0;
63 internal_userlist.clear();
67 void chanrec::SetCustomMode(char mode,bool mode_on)
69 modes[mode-65] = mode_on;
71 this->SetCustomModeParam(mode,"",false);
75 void chanrec::SetCustomModeParam(char mode,char* parameter,bool mode_on)
77 log(DEBUG,"SetCustomModeParam called");
79 std::map<char,char*>::iterator n = custom_mode_params.find(mode);
83 log(DEBUG,"Custom mode parameter %c %s added",mode,parameter);
84 if (n == custom_mode_params.end())
86 custom_mode_params[mode] = strdup(parameter);
91 if (n != custom_mode_params.end())
94 custom_mode_params.erase(n);
99 bool chanrec::IsModeSet(char mode)
101 return modes[mode-65];
104 std::string chanrec::GetModeParameter(char mode)
110 else if (mode == 'l')
112 return ConvToStr(this->limit);
116 std::map<char,char*>::iterator n = custom_mode_params.find(mode);
117 if (n != custom_mode_params.end())
125 long chanrec::GetUserCounter()
127 return (this->internal_userlist.size());
130 void chanrec::AddUser(userrec* user)
132 internal_userlist[user] = user;
135 unsigned long chanrec::DelUser(userrec* user)
137 CUList::iterator a = internal_userlist.find(user);
138 if (a != internal_userlist.end())
140 internal_userlist.erase(a);
141 /* And tidy any others... */
143 DelHalfoppedUser(user);
145 return internal_userlist.size();
147 return internal_userlist.size();
150 bool chanrec::HasUser(userrec* user)
152 return (internal_userlist.find(user) != internal_userlist.end());
155 void chanrec::AddOppedUser(userrec* user)
157 internal_op_userlist[user] = user;
160 void chanrec::DelOppedUser(userrec* user)
162 CUList::iterator a = internal_op_userlist.find(user);
163 if (a != internal_op_userlist.end())
165 internal_op_userlist.erase(a);
170 void chanrec::AddHalfoppedUser(userrec* user)
172 internal_halfop_userlist[user] = user;
175 void chanrec::DelHalfoppedUser(userrec* user)
177 CUList::iterator a = internal_halfop_userlist.find(user);
178 if (a != internal_halfop_userlist.end())
180 internal_halfop_userlist.erase(a);
185 void chanrec::AddVoicedUser(userrec* user)
187 internal_voice_userlist[user] = user;
190 void chanrec::DelVoicedUser(userrec* user)
192 CUList::iterator a = internal_voice_userlist.find(user);
193 if (a != internal_voice_userlist.end())
195 internal_voice_userlist.erase(a);
200 CUList* chanrec::GetUsers()
202 return &internal_userlist;
205 CUList* chanrec::GetOppedUsers()
207 return &internal_op_userlist;
210 CUList* chanrec::GetHalfoppedUsers()
212 return &internal_halfop_userlist;
215 CUList* chanrec::GetVoicedUsers()
217 return &internal_voice_userlist;
221 * add a channel to a user, creating the record for it if needed and linking
222 * it to the user record
225 chanrec* add_channel(userrec *user, const char* cn, const char* key, bool override)
227 if ((!user) || (!cn))
229 log(DEFAULT,"*** BUG *** add_channel was given an invalid parameter");
236 strlcpy(cname,cn,CHANMAX);
237 log(DEBUG,"cname='%s' cn='%s'",cname,cn);
239 log(DEBUG,"add_channel: %s %s",user->nick,cname);
241 chanrec* Ptr = FindChan(cname);
248 FOREACH_RESULT(I_OnUserPreJoin,OnUserPreJoin(user,NULL,cname));
253 /* create a new one */
254 chanlist[cname] = new chanrec();
255 strlcpy(chanlist[cname]->name, cname,CHANMAX);
256 chanlist[cname]->modes[CM_TOPICLOCK] = chanlist[cname]->modes[CM_NOEXTERNAL] = 1;
257 //chanlist[cname]->binarymodes = CM_TOPICLOCK | CM_NOEXTERNAL;
258 chanlist[cname]->created = TIME;
259 *chanlist[cname]->topic = 0;
260 strlcpy(chanlist[cname]->setby, user->nick,NICKMAX-1);
261 chanlist[cname]->topicset = 0;
262 Ptr = chanlist[cname];
263 log(DEBUG,"add_channel: created: %s",cname);
265 * set created to 2 to indicate user
266 * is the first in the channel
267 * and should be given ops
273 /* Already on the channel */
274 if (Ptr->HasUser(user))
278 * remote users are allowed us to bypass channel modes
279 * and bans (used by servers)
281 if (IS_LOCAL(user)) /* was a check on fd > -1 */
284 FOREACH_RESULT(I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname));
289 else if (MOD_RESULT == 0)
294 FOREACH_RESULT(I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
299 log(DEBUG,"add_channel: no key given in JOIN");
300 WriteServ(user->fd,"475 %s %s :Cannot join channel (Requires key)",user->nick, Ptr->name);
305 if (strcmp(key,Ptr->key))
307 log(DEBUG,"add_channel: bad key given in JOIN");
308 WriteServ(user->fd,"475 %s %s :Cannot join channel (Incorrect key)",user->nick, Ptr->name);
314 if (Ptr->modes[CM_INVITEONLY])
317 irc::string xname(Ptr->name);
318 FOREACH_RESULT(I_OnCheckInvite,OnCheckInvite(user, Ptr));
321 log(DEBUG,"add_channel: channel is +i");
322 if (user->IsInvited(xname))
324 /* user was invited to channel */
325 /* there may be an optional channel NOTICE here */
329 WriteServ(user->fd,"473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
333 user->RemoveInvite(xname);
338 FOREACH_RESULT(I_OnCheckLimit,OnCheckLimit(user, Ptr));
341 if (usercount(Ptr) >= Ptr->limit)
343 WriteServ(user->fd,"471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
348 if (Ptr->bans.size())
350 log(DEBUG,"add_channel: about to walk banlist");
352 FOREACH_RESULT(I_OnCheckBan,OnCheckBan(user, Ptr));
355 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
357 if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match((char*)inet_ntoa(user->ip4),i->data)))
359 WriteServ(user->fd,"474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
369 log(DEBUG,"Overridden checks");
374 log(DEBUG,"Passed channel checks");
376 for (std::vector<ucrec*>::const_iterator index = user->chans.begin(); index != user->chans.end(); index++)
378 if ((ucrec*)(*index)->channel == NULL)
380 return ForceChan(Ptr,(ucrec*)(*index),user,created);
385 * XXX: If the user is an oper here, we can just extend their user->chans vector by one
386 * and put the channel in here. Same for remote users which are not bound by
387 * the channel limits. Otherwise, nope, youre boned.
389 if (!IS_LOCAL(user)) /* was a check on fd < 0 */
391 ucrec* a = new ucrec();
392 chanrec* c = ForceChan(Ptr,a,user,created);
393 user->chans.push_back(a);
396 else if (*user->oper)
398 /* Oper allows extension up to the OPERMAXCHANS value */
399 if (user->chans.size() < OPERMAXCHANS)
401 ucrec* a = new ucrec();
402 chanrec* c = ForceChan(Ptr,a,user,created);
403 user->chans.push_back(a);
408 log(DEBUG,"add_channel: user channel max exceeded: %s %s",user->nick,cname);
409 WriteServ(user->fd,"405 %s %s :You are on too many channels",user->nick, cname);
413 log(DEBUG,"BLAMMO, Whacking channel.");
414 /* Things went seriously pear shaped, so take this away. bwahaha. */
415 chan_hash::iterator n = chanlist.find(cname);
416 if (n != chanlist.end())
421 for (unsigned int index =0; index < user->chans.size(); index++)
423 if (user->chans[index]->channel == Ptr)
425 user->chans[index]->channel = NULL;
426 user->chans[index]->uc_modes = 0;
434 chanrec* ForceChan(chanrec* Ptr,ucrec *a,userrec* user, int created)
438 /* first user in is given ops */
439 a->uc_modes = UCMODE_OP;
440 Ptr->AddOppedUser(user);
449 WriteChannel(Ptr,user,"JOIN :%s",Ptr->name);
451 /* Major improvement by Brain - we dont need to be calculating all this pointlessly for remote users */
454 log(DEBUG,"Sent JOIN to client");
457 WriteServ(user->fd,"332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
458 WriteServ(user->fd,"333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
461 WriteServ(user->fd,"366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
463 FOREACH_MOD(I_OnUserJoin,OnUserJoin(user,Ptr));
468 *remove a channel from a users record, and remove the record from memory
469 * if the channel has become empty
472 chanrec* del_channel(userrec *user, const char* cname, const char* reason, bool local)
474 if ((!user) || (!cname))
476 log(DEFAULT,"*** BUG *** del_channel was given an invalid parameter");
480 chanrec* Ptr = FindChan(cname);
485 log(DEBUG,"del_channel: removing: %s %s",user->nick,Ptr->name);
487 for (unsigned int i =0; i < user->chans.size(); i++)
489 /* zap it from the channel list of the user */
490 if (user->chans[i]->channel == Ptr)
494 FOREACH_MOD(I_OnUserPart,OnUserPart(user,Ptr,reason));
495 WriteChannel(Ptr,user,"PART %s :%s",Ptr->name, reason);
499 FOREACH_MOD(I_OnUserPart,OnUserPart(user,Ptr,""));
500 WriteChannel(Ptr,user,"PART :%s",Ptr->name);
502 user->chans[i]->uc_modes = 0;
503 user->chans[i]->channel = NULL;
504 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
511 /* if there are no users left on the channel */
514 chan_hash::iterator iter = chanlist.find(Ptr->name);
516 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
518 /* kill the record */
519 if (iter != chanlist.end())
521 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
522 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
524 chanlist.erase(iter);
531 void server_kick_channel(userrec* user, chanrec* Ptr, char* reason, bool triggerevents)
533 if ((!user) || (!Ptr) || (!reason))
540 if (!Ptr->HasUser(user))
549 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL,user,Ptr,reason));
552 for (unsigned int i =0; i < user->chans.size(); i++)
554 if ((user->chans[i]->channel) && (user->chans[i]->channel == Ptr))
556 WriteChannelWithServ(Config->ServerName,Ptr,"KICK %s %s :%s",Ptr->name, user->nick, reason);
557 user->chans[i]->uc_modes = 0;
558 user->chans[i]->channel = NULL;
567 chan_hash::iterator iter = chanlist.find(Ptr->name);
568 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
569 /* kill the record */
570 if (iter != chanlist.end())
572 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
573 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
575 chanlist.erase(iter);
580 void kick_channel(userrec *src,userrec *user, chanrec *Ptr, char* reason)
582 if ((!src) || (!user) || (!Ptr) || (!reason))
584 log(DEFAULT,"*** BUG *** kick_channel was given an invalid parameter");
588 log(DEBUG,"kick_channel: removing: %s %s %s",user->nick,Ptr->name,src->nick);
592 if (!Ptr->HasUser(user))
594 WriteServ(src->fd,"441 %s %s %s :They are not on that channel",src->nick, user->nick, Ptr->name);
599 if (!is_uline(src->server))
602 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,Ptr,reason));
606 /* Set to -1 by OnUserPreKick if explicit allow was set */
607 if (MOD_RESULT != -1)
609 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,Ptr,AC_KICK));
610 if ((MOD_RESULT == ACR_DENY) && (!is_uline(src->server)))
613 if ((MOD_RESULT == ACR_DEFAULT) || (!is_uline(src->server)))
615 if ((cstatus(src,Ptr) < STATUS_HOP) || (cstatus(src,Ptr) < cstatus(user,Ptr)))
617 if (cstatus(src,Ptr) == STATUS_HOP)
619 WriteServ(src->fd,"482 %s %s :You must be a channel operator",src->nick, Ptr->name);
623 WriteServ(src->fd,"482 %s %s :You must be at least a half-operator",src->nick, Ptr->name);
632 FOREACH_MOD(I_OnUserKick,OnUserKick(src,user,Ptr,reason));
634 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
636 /* zap it from the channel list of the user */
637 if ((((ucrec*)(*i))->channel) && (((ucrec*)(*i))->channel == Ptr))
639 WriteChannel(Ptr,src,"KICK %s %s :%s",Ptr->name, user->nick, reason);
640 ((ucrec*)(*i))->uc_modes = 0;
641 ((ucrec*)(*i))->channel = NULL;
642 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
647 if (!Ptr->DelUser(user))
648 /* if there are no users left on the channel */
650 chan_hash::iterator iter = chanlist.find(Ptr->name);
652 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
654 /* kill the record */
655 if (iter != chanlist.end())
657 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
658 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
660 chanlist.erase(iter);