1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2005 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)
86 if (!strchr(this->custom_modes,mode))
88 strlcat(custom_modes,m,MAXMODES);
90 log(DEBUG,"Custom mode %c set",mode);
94 std::string a = this->custom_modes;
95 int pos = a.find(mode);
97 strncpy(this->custom_modes,a.c_str(),MAXMODES);
99 log(DEBUG,"Custom mode %c removed: modelist='%s'",mode,this->custom_modes);
100 this->SetCustomModeParam(mode,"",false);
105 void chanrec::SetCustomModeParam(char mode,char* parameter,bool mode_on)
108 log(DEBUG,"SetCustomModeParam called");
111 strlcpy(M.channel,this->name,CHANMAX);
112 strlcpy(M.parameter,parameter,MAXBUF);
115 log(DEBUG,"Custom mode parameter %c %s added",mode,parameter);
116 custom_mode_params.push_back(M);
120 if (custom_mode_params.size())
122 for (vector<ModeParameter>::iterator i = custom_mode_params.begin(); i < custom_mode_params.end(); i++)
124 if ((i->mode == mode) && (!strcasecmp(this->name,i->channel)))
126 log(DEBUG,"Custom mode parameter %c %s removed",mode,parameter);
127 custom_mode_params.erase(i);
132 log(DEBUG,"*** BUG *** Attempt to remove non-existent mode parameter!");
136 bool chanrec::IsCustomModeSet(char mode)
138 return (strchr(this->custom_modes,mode));
141 std::string chanrec::GetModeParameter(char mode)
143 if (custom_mode_params.size())
145 for (vector<ModeParameter>::iterator i = custom_mode_params.begin(); i < custom_mode_params.end(); i++)
147 if ((i->mode == mode) && (!strcasecmp(this->name,i->channel)))
156 long chanrec::GetUserCounter()
158 return (this->internal_userlist.size());
161 void chanrec::AddUser(char* castuser)
163 internal_userlist.push_back(castuser);
164 log(DEBUG,"Added casted user to channel's internal list");
167 void chanrec::DelUser(char* castuser)
169 for (std::vector<char*>::iterator a = internal_userlist.begin(); a < internal_userlist.end(); a++)
173 log(DEBUG,"Removed casted user from channel's internal list");
174 internal_userlist.erase(a);
178 log(DEBUG,"BUG BUG BUG! Attempt to remove an uncasted user from the internal list of %s!",name);
181 std::vector<char*> *chanrec::GetUsers()
183 return &internal_userlist;
186 /* add a channel to a user, creating the record for it if needed and linking
187 * it to the user record */
189 chanrec* add_channel(userrec *user, const char* cn, const char* key, bool override)
191 if ((!user) || (!cn))
193 log(DEFAULT,"*** BUG *** add_channel was given an invalid parameter");
200 strncpy(cname,cn,CHANMAX);
202 log(DEBUG,"add_channel: %s %s",user->nick,cname);
204 chanrec* Ptr = FindChan(cname);
211 FOREACH_RESULT(I_OnUserPreJoin,OnUserPreJoin(user,NULL,cname));
215 /* create a new one */
216 chanlist[cname] = new chanrec();
217 strlcpy(chanlist[cname]->name, cname,CHANMAX);
218 chanlist[cname]->binarymodes = CM_TOPICLOCK | CM_NOEXTERNAL;
219 chanlist[cname]->created = TIME;
220 *chanlist[cname]->topic = 0;
221 strncpy(chanlist[cname]->setby, user->nick,NICKMAX);
222 chanlist[cname]->topicset = 0;
223 Ptr = chanlist[cname];
224 log(DEBUG,"add_channel: created: %s",cname);
225 /* set created to 2 to indicate user
226 * is the first in the channel
227 * and should be given ops */
232 /* Already on the channel */
233 if (has_channel(user,Ptr))
236 // remote users are allowed us to bypass channel modes
237 // and bans (used by servers)
241 FOREACH_RESULT(I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname));
246 else if (MOD_RESULT == 0)
251 FOREACH_RESULT(I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
256 log(DEBUG,"add_channel: no key given in JOIN");
257 WriteServ(user->fd,"475 %s %s :Cannot join channel (Requires key)",user->nick, Ptr->name);
262 if (strcasecmp(key,Ptr->key))
264 log(DEBUG,"add_channel: bad key given in JOIN");
265 WriteServ(user->fd,"475 %s %s :Cannot join channel (Incorrect key)",user->nick, Ptr->name);
271 if (Ptr->binarymodes & CM_INVITEONLY)
274 irc::string xname(Ptr->name);
275 FOREACH_RESULT(I_OnCheckInvite,OnCheckInvite(user, Ptr));
278 log(DEBUG,"add_channel: channel is +i");
279 if (user->IsInvited(xname))
281 /* user was invited to channel */
282 /* there may be an optional channel NOTICE here */
286 WriteServ(user->fd,"473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
290 user->RemoveInvite(xname);
295 FOREACH_RESULT(I_OnCheckLimit,OnCheckLimit(user, Ptr));
298 if (usercount(Ptr) >= Ptr->limit)
300 WriteServ(user->fd,"471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
305 if (Ptr->bans.size())
307 log(DEBUG,"add_channel: about to walk banlist");
309 FOREACH_RESULT(I_OnCheckBan,OnCheckBan(user, Ptr));
312 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
314 if (match(user->GetFullHost(),i->data))
316 WriteServ(user->fd,"474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
326 log(DEBUG,"Overridden checks");
331 log(DEBUG,"Passed channel checks");
333 for (unsigned int index =0; index < user->chans.size(); index++)
335 if (user->chans[index].channel == NULL)
337 return ForceChan(Ptr,user->chans[index],user,created);
340 /* XXX: If the user is an oper here, we can just extend their user->chans vector by one
341 * and put the channel in here. Same for remote users which are not bound by
342 * the channel limits. Otherwise, nope, youre boned.
347 chanrec* c = ForceChan(Ptr,a,user,created);
348 user->chans.push_back(a);
351 else if (strchr(user->modes,'o'))
353 /* Oper allows extension up to the OPERMAXCHANS value */
354 if (user->chans.size() < OPERMAXCHANS)
357 chanrec* c = ForceChan(Ptr,a,user,created);
358 user->chans.push_back(a);
362 log(DEBUG,"add_channel: user channel max exceeded: %s %s",user->nick,cname);
363 WriteServ(user->fd,"405 %s %s :You are on too many channels",user->nick, cname);
367 chanrec* ForceChan(chanrec* Ptr,ucrec &a,userrec* user, int created)
371 /* first user in is given ops */
372 a.uc_modes = UCMODE_OP;
379 Ptr->AddUser((char*)user);
380 WriteChannel(Ptr,user,"JOIN :%s",Ptr->name);
381 log(DEBUG,"Sent JOIN to client");
384 WriteServ(user->fd,"332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
385 WriteServ(user->fd,"333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
388 WriteServ(user->fd,"366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
389 FOREACH_MOD(I_OnUserJoin,OnUserJoin(user,Ptr));
393 /* remove a channel from a users record, and remove the record from memory
394 * if the channel has become empty */
396 chanrec* del_channel(userrec *user, const char* cname, const char* reason, bool local)
398 if ((!user) || (!cname))
400 log(DEFAULT,"*** BUG *** del_channel was given an invalid parameter");
404 chanrec* Ptr = FindChan(cname);
409 FOREACH_MOD(I_OnUserPart,OnUserPart(user,Ptr));
410 log(DEBUG,"del_channel: removing: %s %s",user->nick,Ptr->name);
412 for (unsigned int i =0; i < user->chans.size(); i++)
414 /* zap it from the channel list of the user */
415 if (user->chans[i].channel == Ptr)
419 WriteChannel(Ptr,user,"PART %s :%s",Ptr->name, reason);
423 WriteChannel(Ptr,user,"PART :%s",Ptr->name);
425 user->chans[i].uc_modes = 0;
426 user->chans[i].channel = NULL;
427 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
432 Ptr->DelUser((char*)user);
434 /* if there are no users left on the channel */
437 chan_hash::iterator iter = chanlist.find(Ptr->name);
439 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
441 /* kill the record */
442 if (iter != chanlist.end())
444 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
446 chanlist.erase(iter);
454 void kick_channel(userrec *src,userrec *user, chanrec *Ptr, char* reason)
456 if ((!src) || (!user) || (!Ptr) || (!reason))
458 log(DEFAULT,"*** BUG *** kick_channel was given an invalid parameter");
462 if ((!Ptr) || (!user) || (!src))
467 log(DEBUG,"kick_channel: removing: %s %s %s",user->nick,Ptr->name,src->nick);
469 if (!has_channel(user,Ptr))
471 WriteServ(src->fd,"441 %s %s %s :They are not on that channel",src->nick, user->nick, Ptr->name);
476 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,Ptr,AC_KICK));
477 if ((MOD_RESULT == ACR_DENY) && (!is_uline(src->server)))
480 if ((MOD_RESULT == ACR_DEFAULT) || (!is_uline(src->server)))
482 if ((cstatus(src,Ptr) < STATUS_HOP) || (cstatus(src,Ptr) < cstatus(user,Ptr)))
484 if (cstatus(src,Ptr) == STATUS_HOP)
486 WriteServ(src->fd,"482 %s %s :You must be a channel operator",src->nick, Ptr->name);
490 WriteServ(src->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",src->nick, Ptr->name);
497 if (!is_uline(src->server))
500 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,Ptr,reason));
505 FOREACH_MOD(I_OnUserKick,OnUserKick(src,user,Ptr,reason));
507 for (unsigned int i =0; i < user->chans.size(); i++)
509 /* zap it from the channel list of the user */
510 if (user->chans[i].channel)
511 if (!strcasecmp(user->chans[i].channel->name,Ptr->name))
513 WriteChannel(Ptr,src,"KICK %s %s :%s",Ptr->name, user->nick, reason);
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);
521 Ptr->DelUser((char*)user);
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);
535 chanlist.erase(iter);