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 /* XXX - would it not be better to store a vector for each channel, rather than one huge list? -- w00t */
143 std::string chanrec::GetModeParameter(char mode)
145 if (custom_mode_params.size())
147 for (vector<ModeParameter>::iterator i = custom_mode_params.begin(); i < custom_mode_params.end(); i++)
149 if ((i->mode == mode) && (!strcasecmp(this->name,i->channel)))
158 long chanrec::GetUserCounter()
160 return (this->internal_userlist.size());
163 void chanrec::AddUser(char* castuser)
165 internal_userlist[castuser] = castuser;
166 log(DEBUG,"Added casted user to channel's internal list");
169 void chanrec::DelUser(char* castuser)
171 std::map<char*,char*>::iterator a = internal_userlist.find(castuser);
172 if (a != internal_userlist.end())
174 log(DEBUG,"Removed casted user from channel's internal list");
175 internal_userlist.erase(a);
176 /* And tidy any others... */
177 DelOppedUser(castuser);
178 DelHalfoppedUser(castuser);
179 DelVoicedUser(castuser);
184 void chanrec::AddOppedUser(char* castuser)
186 internal_op_userlist[castuser] = castuser;
187 log(DEBUG,"Added casted user to channel's internal list");
190 void chanrec::DelOppedUser(char* castuser)
192 std::map<char*,char*>::iterator a = internal_op_userlist.find(castuser);
193 if (a != internal_op_userlist.end())
195 log(DEBUG,"Removed casted user from channel's internal list");
196 internal_op_userlist.erase(a);
201 void chanrec::AddHalfoppedUser(char* castuser)
203 internal_halfop_userlist[castuser] = castuser;
204 log(DEBUG,"Added casted user to channel's internal list");
207 void chanrec::DelHalfoppedUser(char* castuser)
209 std::map<char*,char*>::iterator a = internal_halfop_userlist.find(castuser);
210 if (a != internal_halfop_userlist.end())
212 log(DEBUG,"Removed casted user from channel's internal list");
213 internal_halfop_userlist.erase(a);
218 void chanrec::AddVoicedUser(char* castuser)
220 internal_voice_userlist[castuser] = castuser;
221 log(DEBUG,"Added casted user to channel's internal list");
224 void chanrec::DelVoicedUser(char* castuser)
226 std::map<char*,char*>::iterator a = internal_voice_userlist.find(castuser);
227 if (a != internal_voice_userlist.end())
229 log(DEBUG,"Removed casted user from channel's internal list");
230 internal_voice_userlist.erase(a);
235 std::map<char*,char*> *chanrec::GetUsers()
237 return &internal_userlist;
240 std::map<char*,char*> *chanrec::GetOppedUsers()
242 return &internal_op_userlist;
245 std::map<char*,char*> *chanrec::GetHalfoppedUsers()
247 return &internal_halfop_userlist;
250 std::map<char*,char*> *chanrec::GetVoicedUsers()
252 return &internal_voice_userlist;
256 * add a channel to a user, creating the record for it if needed and linking
257 * it to the user record
260 chanrec* add_channel(userrec *user, const char* cn, const char* key, bool override)
262 if ((!user) || (!cn))
264 log(DEFAULT,"*** BUG *** add_channel was given an invalid parameter");
271 strlcpy(cname,cn,CHANMAX);
272 log(DEBUG,"cname='%s' cn='%s'",cname,cn);
274 log(DEBUG,"add_channel: %s %s",user->nick,cname);
276 chanrec* Ptr = FindChan(cname);
283 FOREACH_RESULT(I_OnUserPreJoin,OnUserPreJoin(user,NULL,cname));
288 /* create a new one */
289 chanlist[cname] = new chanrec();
290 strlcpy(chanlist[cname]->name, cname,CHANMAX);
291 chanlist[cname]->binarymodes = CM_TOPICLOCK | CM_NOEXTERNAL;
292 chanlist[cname]->created = TIME;
293 *chanlist[cname]->topic = 0;
294 strlcpy(chanlist[cname]->setby, user->nick,NICKMAX);
295 chanlist[cname]->topicset = 0;
296 Ptr = chanlist[cname];
297 log(DEBUG,"add_channel: created: %s",cname);
299 * set created to 2 to indicate user
300 * is the first in the channel
301 * and should be given ops
307 /* Already on the channel */
308 if (has_channel(user,Ptr))
312 * remote users are allowed us to bypass channel modes
313 * and bans (used by servers)
315 if (IS_LOCAL(user)) /* was a check on fd > -1 */
318 FOREACH_RESULT(I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname));
323 else if (MOD_RESULT == 0)
328 FOREACH_RESULT(I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
333 log(DEBUG,"add_channel: no key given in JOIN");
334 WriteServ(user->fd,"475 %s %s :Cannot join channel (Requires key)",user->nick, Ptr->name);
339 if (strcmp(key,Ptr->key))
341 log(DEBUG,"add_channel: bad key given in JOIN");
342 WriteServ(user->fd,"475 %s %s :Cannot join channel (Incorrect key)",user->nick, Ptr->name);
348 if (Ptr->binarymodes & CM_INVITEONLY)
351 irc::string xname(Ptr->name);
352 FOREACH_RESULT(I_OnCheckInvite,OnCheckInvite(user, Ptr));
355 log(DEBUG,"add_channel: channel is +i");
356 if (user->IsInvited(xname))
358 /* user was invited to channel */
359 /* there may be an optional channel NOTICE here */
363 WriteServ(user->fd,"473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
367 user->RemoveInvite(xname);
372 FOREACH_RESULT(I_OnCheckLimit,OnCheckLimit(user, Ptr));
375 if (usercount(Ptr) >= Ptr->limit)
377 WriteServ(user->fd,"471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
382 if (Ptr->bans.size())
384 log(DEBUG,"add_channel: about to walk banlist");
386 FOREACH_RESULT(I_OnCheckBan,OnCheckBan(user, Ptr));
389 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
391 if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match((char*)inet_ntoa(user->ip4),i->data)))
393 WriteServ(user->fd,"474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
403 log(DEBUG,"Overridden checks");
408 log(DEBUG,"Passed channel checks");
410 for (unsigned int index =0; index < user->chans.size(); index++)
412 if (user->chans[index].channel == NULL)
414 return ForceChan(Ptr,user->chans[index],user,created);
419 * XXX: If the user is an oper here, we can just extend their user->chans vector by one
420 * and put the channel in here. Same for remote users which are not bound by
421 * the channel limits. Otherwise, nope, youre boned.
423 if (!IS_LOCAL(user)) /* was a check on fd < 0 */
426 chanrec* c = ForceChan(Ptr,a,user,created);
427 user->chans.push_back(a);
430 else if (strchr(user->modes,'o'))
432 /* Oper allows extension up to the OPERMAXCHANS value */
433 if (user->chans.size() < OPERMAXCHANS)
436 chanrec* c = ForceChan(Ptr,a,user,created);
437 user->chans.push_back(a);
442 log(DEBUG,"add_channel: user channel max exceeded: %s %s",user->nick,cname);
443 WriteServ(user->fd,"405 %s %s :You are on too many channels",user->nick, cname);
447 chanrec* ForceChan(chanrec* Ptr,ucrec &a,userrec* user, int created)
451 /* first user in is given ops */
452 a.uc_modes = UCMODE_OP;
453 Ptr->AddOppedUser((char*)user);
461 Ptr->AddUser((char*)user);
462 WriteChannel(Ptr,user,"JOIN :%s",Ptr->name);
463 log(DEBUG,"Sent JOIN to client");
467 WriteServ(user->fd,"332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
468 WriteServ(user->fd,"333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
472 WriteServ(user->fd,"366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
473 FOREACH_MOD(I_OnUserJoin,OnUserJoin(user,Ptr));
478 *remove a channel from a users record, and remove the record from memory
479 * if the channel has become empty
482 chanrec* del_channel(userrec *user, const char* cname, const char* reason, bool local)
484 if ((!user) || (!cname))
486 log(DEFAULT,"*** BUG *** del_channel was given an invalid parameter");
490 chanrec* Ptr = FindChan(cname);
495 log(DEBUG,"del_channel: removing: %s %s",user->nick,Ptr->name);
497 for (unsigned int i =0; i < user->chans.size(); i++)
499 /* zap it from the channel list of the user */
500 if (user->chans[i].channel == Ptr)
504 FOREACH_MOD(I_OnUserPart,OnUserPart(user,Ptr,reason));
505 WriteChannel(Ptr,user,"PART %s :%s",Ptr->name, reason);
509 FOREACH_MOD(I_OnUserPart,OnUserPart(user,Ptr,""));
510 WriteChannel(Ptr,user,"PART :%s",Ptr->name);
512 user->chans[i].uc_modes = 0;
513 user->chans[i].channel = NULL;
514 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
519 Ptr->DelUser((char*)user);
521 /* if there are no users left on the channel */
524 chan_hash::iterator iter = chanlist.find(Ptr->name);
526 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
528 /* kill the record */
529 if (iter != chanlist.end())
531 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
532 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
534 chanlist.erase(iter);
541 void server_kick_channel(userrec* user, chanrec* Ptr, char* reason, bool triggerevents)
543 if ((!user) || (!Ptr) || (!reason))
550 if (!has_channel(user,Ptr))
559 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL,user,Ptr,reason));
562 for (unsigned int i =0; i < user->chans.size(); i++)
564 if (user->chans[i].channel)
565 if (!strcasecmp(user->chans[i].channel->name,Ptr->name))
567 WriteChannelWithServ(Config->ServerName,Ptr,"KICK %s %s :%s",Ptr->name, user->nick, reason);
568 user->chans[i].uc_modes = 0;
569 user->chans[i].channel = NULL;
574 Ptr->DelUser((char*)user);
578 chan_hash::iterator iter = chanlist.find(Ptr->name);
579 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
580 /* kill the record */
581 if (iter != chanlist.end())
583 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
584 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
586 chanlist.erase(iter);
591 void kick_channel(userrec *src,userrec *user, chanrec *Ptr, char* reason)
593 if ((!src) || (!user) || (!Ptr) || (!reason))
595 log(DEFAULT,"*** BUG *** kick_channel was given an invalid parameter");
599 log(DEBUG,"kick_channel: removing: %s %s %s",user->nick,Ptr->name,src->nick);
603 if (!has_channel(user,Ptr))
605 WriteServ(src->fd,"441 %s %s %s :They are not on that channel",src->nick, user->nick, Ptr->name);
610 if (!is_uline(src->server))
613 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,Ptr,reason));
617 /* Set to -1 by OnUserPreKick if explicit allow was set */
618 if (MOD_RESULT != -1)
620 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,Ptr,AC_KICK));
621 if ((MOD_RESULT == ACR_DENY) && (!is_uline(src->server)))
624 if ((MOD_RESULT == ACR_DEFAULT) || (!is_uline(src->server)))
626 if ((cstatus(src,Ptr) < STATUS_HOP) || (cstatus(src,Ptr) < cstatus(user,Ptr)))
628 if (cstatus(src,Ptr) == STATUS_HOP)
630 WriteServ(src->fd,"482 %s %s :You must be a channel operator",src->nick, Ptr->name);
634 WriteServ(src->fd,"482 %s %s :You must be at least a half-operator",src->nick, Ptr->name);
643 FOREACH_MOD(I_OnUserKick,OnUserKick(src,user,Ptr,reason));
645 for (unsigned int i =0; i < user->chans.size(); i++)
647 /* zap it from the channel list of the user */
648 if (user->chans[i].channel)
650 if (!strcasecmp(user->chans[i].channel->name,Ptr->name))
652 WriteChannel(Ptr,src,"KICK %s %s :%s",Ptr->name, user->nick, reason);
653 user->chans[i].uc_modes = 0;
654 user->chans[i].channel = NULL;
655 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
661 Ptr->DelUser((char*)user);
663 /* if there are no users left on the channel */
666 chan_hash::iterator iter = chanlist.find(Ptr->name);
668 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
670 /* kill the record */
671 if (iter != chanlist.end())
673 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
674 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
676 chanlist.erase(iter);