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"
22 #include "inspircd_util.h"
24 #include <sys/errno.h>
25 #include <sys/ioctl.h>
26 #include <sys/utsname.h>
30 #include <ext/hash_map>
48 #include "inspstring.h"
49 #include "helperfuncs.h"
53 #define nspace __gnu_cxx
58 extern ServerConfig* Config;
61 extern std::vector<Module*> modules;
62 extern std::vector<ircd_module*> factory;
63 extern int WHOWAS_STALE;
64 extern int WHOWAS_MAX;
65 extern time_t startup_time;
66 extern std::vector<std::string> module_names;
67 extern int boundPortCount;
68 extern std::stringstream config_f;
70 extern chan_hash chanlist;
74 std::vector<ModeParameter> custom_mode_params;
76 chanrec* ForceChan(chanrec* Ptr,ucrec &a,userrec* user, int created);
81 strcpy(custom_modes,"");
85 created = topicset = limit = 0;
87 internal_userlist.clear();
90 void chanrec::SetCustomMode(char mode,bool mode_on)
96 if (!strchr(this->custom_modes,mode))
98 strlcat(custom_modes,m,MAXMODES);
100 log(DEBUG,"Custom mode %c set",mode);
104 std::string a = this->custom_modes;
105 int pos = a.find(mode);
107 strncpy(this->custom_modes,a.c_str(),MAXMODES);
109 log(DEBUG,"Custom mode %c removed: modelist='%s'",mode,this->custom_modes);
110 this->SetCustomModeParam(mode,"",false);
115 void chanrec::SetCustomModeParam(char mode,char* parameter,bool mode_on)
118 log(DEBUG,"SetCustomModeParam called");
121 strlcpy(M.channel,this->name,CHANMAX);
122 strlcpy(M.parameter,parameter,MAXBUF);
125 log(DEBUG,"Custom mode parameter %c %s added",mode,parameter);
126 custom_mode_params.push_back(M);
130 if (custom_mode_params.size())
132 for (vector<ModeParameter>::iterator i = custom_mode_params.begin(); i < custom_mode_params.end(); i++)
134 if ((i->mode == mode) && (!strcasecmp(this->name,i->channel)))
136 log(DEBUG,"Custom mode parameter %c %s removed",mode,parameter);
137 custom_mode_params.erase(i);
142 log(DEBUG,"*** BUG *** Attempt to remove non-existent mode parameter!");
146 bool chanrec::IsCustomModeSet(char mode)
148 return (strchr(this->custom_modes,mode));
151 std::string chanrec::GetModeParameter(char mode)
153 if (custom_mode_params.size())
155 for (vector<ModeParameter>::iterator i = custom_mode_params.begin(); i < custom_mode_params.end(); i++)
157 if ((i->mode == mode) && (!strcasecmp(this->name,i->channel)))
166 long chanrec::GetUserCounter()
168 return (this->internal_userlist.size());
171 void chanrec::AddUser(char* castuser)
173 internal_userlist.push_back(castuser);
174 log(DEBUG,"Added casted user to channel's internal list");
177 void chanrec::DelUser(char* castuser)
179 for (std::vector<char*>::iterator a = internal_userlist.begin(); a < internal_userlist.end(); a++)
183 log(DEBUG,"Removed casted user from channel's internal list");
184 internal_userlist.erase(a);
188 log(DEBUG,"BUG BUG BUG! Attempt to remove an uncasted user from the internal list of %s!",name);
191 std::vector<char*> *chanrec::GetUsers()
193 return &internal_userlist;
196 /* add a channel to a user, creating the record for it if needed and linking
197 * it to the user record */
199 chanrec* add_channel(userrec *user, const char* cn, const char* key, bool override)
201 if ((!user) || (!cn))
203 log(DEFAULT,"*** BUG *** add_channel was given an invalid parameter");
210 strncpy(cname,cn,CHANMAX);
212 log(DEBUG,"add_channel: %s %s",user->nick,cname);
214 chanrec* Ptr = FindChan(cname);
221 FOREACH_RESULT(OnUserPreJoin(user,NULL,cname));
225 /* create a new one */
226 chanlist[cname] = new chanrec();
227 strlcpy(chanlist[cname]->name, cname,CHANMAX);
228 chanlist[cname]->binarymodes = CM_TOPICLOCK | CM_NOEXTERNAL;
229 chanlist[cname]->created = TIME;
230 strcpy(chanlist[cname]->topic, "");
231 strncpy(chanlist[cname]->setby, user->nick,NICKMAX);
232 chanlist[cname]->topicset = 0;
233 Ptr = chanlist[cname];
234 log(DEBUG,"add_channel: created: %s",cname);
235 /* set created to 2 to indicate user
236 * is the first in the channel
237 * and should be given ops */
242 /* Already on the channel */
243 if (has_channel(user,Ptr))
246 // remote users are allowed us to bypass channel modes
247 // and bans (used by servers)
251 FOREACH_RESULT(OnUserPreJoin(user,Ptr,cname));
261 FOREACH_RESULT(OnCheckKey(user, Ptr, key ? key : ""));
266 log(DEBUG,"add_channel: no key given in JOIN");
267 WriteServ(user->fd,"475 %s %s :Cannot join channel (Requires key)",user->nick, Ptr->name);
272 if (strcasecmp(key,Ptr->key))
274 log(DEBUG,"add_channel: bad key given in JOIN");
275 WriteServ(user->fd,"475 %s %s :Cannot join channel (Incorrect key)",user->nick, Ptr->name);
281 if (Ptr->binarymodes & CM_INVITEONLY)
284 FOREACH_RESULT(OnCheckInvite(user, Ptr));
287 log(DEBUG,"add_channel: channel is +i");
288 if (user->IsInvited(Ptr->name))
290 /* user was invited to channel */
291 /* there may be an optional channel NOTICE here */
295 WriteServ(user->fd,"473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
299 user->RemoveInvite(Ptr->name);
304 FOREACH_RESULT(OnCheckLimit(user, Ptr));
307 if (usercount(Ptr) >= Ptr->limit)
309 WriteServ(user->fd,"471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
314 if (Ptr->bans.size())
316 log(DEBUG,"add_channel: about to walk banlist");
318 FOREACH_RESULT(OnCheckBan(user, Ptr));
321 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
323 if (match(user->GetFullHost(),i->data))
325 WriteServ(user->fd,"474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
335 log(DEBUG,"Overridden checks");
340 log(DEBUG,"Passed channel checks");
342 for (unsigned int index =0; index < user->chans.size(); index++)
344 if (user->chans[index].channel == NULL)
346 return ForceChan(Ptr,user->chans[index],user,created);
349 /* XXX: If the user is an oper here, we can just extend their user->chans vector by one
350 * and put the channel in here. Same for remote users which are not bound by
351 * the channel limits. Otherwise, nope, youre boned.
356 chanrec* c = ForceChan(Ptr,a,user,created);
357 user->chans.push_back(a);
360 else if (strchr(user->modes,'o'))
362 /* Oper allows extension up to the OPERMAXCHANS value */
363 if (user->chans.size() < OPERMAXCHANS)
366 chanrec* c = ForceChan(Ptr,a,user,created);
367 user->chans.push_back(a);
371 log(DEBUG,"add_channel: user channel max exceeded: %s %s",user->nick,cname);
372 WriteServ(user->fd,"405 %s %s :You are on too many channels",user->nick, cname);
376 chanrec* ForceChan(chanrec* Ptr,ucrec &a,userrec* user, int created)
380 /* first user in is given ops */
381 a.uc_modes = UCMODE_OP;
388 Ptr->AddUser((char*)user);
389 WriteChannel(Ptr,user,"JOIN :%s",Ptr->name);
390 log(DEBUG,"Sent JOIN to client");
393 WriteServ(user->fd,"332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
394 WriteServ(user->fd,"333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
397 WriteServ(user->fd,"366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
398 FOREACH_MOD OnUserJoin(user,Ptr);
402 /* remove a channel from a users record, and remove the record from memory
403 * if the channel has become empty */
405 chanrec* del_channel(userrec *user, const char* cname, const char* reason, bool local)
407 if ((!user) || (!cname))
409 log(DEFAULT,"*** BUG *** del_channel was given an invalid parameter");
413 chanrec* Ptr = FindChan(cname);
418 FOREACH_MOD OnUserPart(user,Ptr);
419 log(DEBUG,"del_channel: removing: %s %s",user->nick,Ptr->name);
421 for (unsigned int i =0; i < user->chans.size(); i++)
423 /* zap it from the channel list of the user */
424 if (user->chans[i].channel == Ptr)
428 WriteChannel(Ptr,user,"PART %s :%s",Ptr->name, reason);
432 WriteChannel(Ptr,user,"PART :%s",Ptr->name);
434 user->chans[i].uc_modes = 0;
435 user->chans[i].channel = NULL;
436 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
441 Ptr->DelUser((char*)user);
443 /* if there are no users left on the channel */
446 chan_hash::iterator iter = chanlist.find(Ptr->name);
448 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
450 /* kill the record */
451 if (iter != chanlist.end())
453 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
455 chanlist.erase(iter);
463 void kick_channel(userrec *src,userrec *user, chanrec *Ptr, char* reason)
465 if ((!src) || (!user) || (!Ptr) || (!reason))
467 log(DEFAULT,"*** BUG *** kick_channel was given an invalid parameter");
471 if ((!Ptr) || (!user) || (!src))
476 log(DEBUG,"kick_channel: removing: %s %s %s",user->nick,Ptr->name,src->nick);
478 if (!has_channel(user,Ptr))
480 WriteServ(src->fd,"441 %s %s %s :They are not on that channel",src->nick, user->nick, Ptr->name);
485 FOREACH_RESULT(OnAccessCheck(src,user,Ptr,AC_KICK));
486 if ((MOD_RESULT == ACR_DENY) && (!is_uline(src->server)))
489 if ((MOD_RESULT == ACR_DEFAULT) || (!is_uline(src->server)))
491 if ((cstatus(src,Ptr) < STATUS_HOP) || (cstatus(src,Ptr) < cstatus(user,Ptr)))
493 if (cstatus(src,Ptr) == STATUS_HOP)
495 WriteServ(src->fd,"482 %s %s :You must be a channel operator",src->nick, Ptr->name);
499 WriteServ(src->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",src->nick, Ptr->name);
506 if (!is_uline(src->server))
509 FOREACH_RESULT(OnUserPreKick(src,user,Ptr,reason));
514 FOREACH_MOD OnUserKick(src,user,Ptr,reason);
516 for (unsigned int i =0; i < user->chans.size(); i++)
518 /* zap it from the channel list of the user */
519 if (user->chans[i].channel)
520 if (!strcasecmp(user->chans[i].channel->name,Ptr->name))
522 WriteChannel(Ptr,src,"KICK %s %s :%s",Ptr->name, user->nick, reason);
523 user->chans[i].uc_modes = 0;
524 user->chans[i].channel = NULL;
525 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
530 Ptr->DelUser((char*)user);
532 /* if there are no users left on the channel */
535 chan_hash::iterator iter = chanlist.find(Ptr->name);
537 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
539 /* kill the record */
540 if (iter != chanlist.end())
542 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
544 chanlist.erase(iter);