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;
66 extern chan_hash chanlist;
70 std::vector<ModeParameter> custom_mode_params;
72 chanrec* ForceChan(chanrec* Ptr,ucrec &a,userrec* user, int created);
77 strcpy(custom_modes,"");
81 created = topicset = limit = 0;
83 internal_userlist.clear();
86 void chanrec::SetCustomMode(char mode,bool mode_on)
92 if (!strchr(this->custom_modes,mode))
94 strlcat(custom_modes,m,MAXMODES);
96 log(DEBUG,"Custom mode %c set",mode);
100 std::string a = this->custom_modes;
101 int pos = a.find(mode);
103 strncpy(this->custom_modes,a.c_str(),MAXMODES);
105 log(DEBUG,"Custom mode %c removed: modelist='%s'",mode,this->custom_modes);
106 this->SetCustomModeParam(mode,"",false);
111 void chanrec::SetCustomModeParam(char mode,char* parameter,bool mode_on)
114 log(DEBUG,"SetCustomModeParam called");
117 strlcpy(M.channel,this->name,CHANMAX);
118 strlcpy(M.parameter,parameter,MAXBUF);
121 log(DEBUG,"Custom mode parameter %c %s added",mode,parameter);
122 custom_mode_params.push_back(M);
126 if (custom_mode_params.size())
128 for (vector<ModeParameter>::iterator i = custom_mode_params.begin(); i < custom_mode_params.end(); i++)
130 if ((i->mode == mode) && (!strcasecmp(this->name,i->channel)))
132 log(DEBUG,"Custom mode parameter %c %s removed",mode,parameter);
133 custom_mode_params.erase(i);
138 log(DEBUG,"*** BUG *** Attempt to remove non-existent mode parameter!");
142 bool chanrec::IsCustomModeSet(char mode)
144 return (strchr(this->custom_modes,mode));
147 std::string chanrec::GetModeParameter(char mode)
149 if (custom_mode_params.size())
151 for (vector<ModeParameter>::iterator i = custom_mode_params.begin(); i < custom_mode_params.end(); i++)
153 if ((i->mode == mode) && (!strcasecmp(this->name,i->channel)))
162 long chanrec::GetUserCounter()
164 return (this->internal_userlist.size());
167 void chanrec::AddUser(char* castuser)
169 internal_userlist.push_back(castuser);
170 log(DEBUG,"Added casted user to channel's internal list");
173 void chanrec::DelUser(char* castuser)
175 for (std::vector<char*>::iterator a = internal_userlist.begin(); a < internal_userlist.end(); a++)
179 log(DEBUG,"Removed casted user from channel's internal list");
180 internal_userlist.erase(a);
184 log(DEBUG,"BUG BUG BUG! Attempt to remove an uncasted user from the internal list of %s!",name);
187 std::vector<char*> *chanrec::GetUsers()
189 return &internal_userlist;
192 /* add a channel to a user, creating the record for it if needed and linking
193 * it to the user record */
195 chanrec* add_channel(userrec *user, const char* cn, const char* key, bool override)
197 if ((!user) || (!cn))
199 log(DEFAULT,"*** BUG *** add_channel was given an invalid parameter");
206 strncpy(cname,cn,CHANMAX);
208 log(DEBUG,"add_channel: %s %s",user->nick,cname);
210 chanrec* Ptr = FindChan(cname);
217 FOREACH_RESULT(OnUserPreJoin(user,NULL,cname));
221 /* create a new one */
222 chanlist[cname] = new chanrec();
223 strlcpy(chanlist[cname]->name, cname,CHANMAX);
224 chanlist[cname]->binarymodes = CM_TOPICLOCK | CM_NOEXTERNAL;
225 chanlist[cname]->created = TIME;
226 strcpy(chanlist[cname]->topic, "");
227 strncpy(chanlist[cname]->setby, user->nick,NICKMAX);
228 chanlist[cname]->topicset = 0;
229 Ptr = chanlist[cname];
230 log(DEBUG,"add_channel: created: %s",cname);
231 /* set created to 2 to indicate user
232 * is the first in the channel
233 * and should be given ops */
238 /* Already on the channel */
239 if (has_channel(user,Ptr))
242 // remote users are allowed us to bypass channel modes
243 // and bans (used by servers)
247 FOREACH_RESULT(OnUserPreJoin(user,Ptr,cname));
257 FOREACH_RESULT(OnCheckKey(user, Ptr, key ? key : ""));
262 log(DEBUG,"add_channel: no key given in JOIN");
263 WriteServ(user->fd,"475 %s %s :Cannot join channel (Requires key)",user->nick, Ptr->name);
268 if (strcasecmp(key,Ptr->key))
270 log(DEBUG,"add_channel: bad key given in JOIN");
271 WriteServ(user->fd,"475 %s %s :Cannot join channel (Incorrect key)",user->nick, Ptr->name);
277 if (Ptr->binarymodes & CM_INVITEONLY)
280 FOREACH_RESULT(OnCheckInvite(user, Ptr));
283 log(DEBUG,"add_channel: channel is +i");
284 if (user->IsInvited(Ptr->name))
286 /* user was invited to channel */
287 /* there may be an optional channel NOTICE here */
291 WriteServ(user->fd,"473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
295 user->RemoveInvite(Ptr->name);
300 FOREACH_RESULT(OnCheckLimit(user, Ptr));
303 if (usercount(Ptr) >= Ptr->limit)
305 WriteServ(user->fd,"471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
310 if (Ptr->bans.size())
312 log(DEBUG,"add_channel: about to walk banlist");
314 FOREACH_RESULT(OnCheckBan(user, Ptr));
317 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
319 if (match(user->GetFullHost(),i->data))
321 WriteServ(user->fd,"474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
331 log(DEBUG,"Overridden checks");
336 log(DEBUG,"Passed channel checks");
338 for (unsigned int index =0; index < user->chans.size(); index++)
340 if (user->chans[index].channel == NULL)
342 return ForceChan(Ptr,user->chans[index],user,created);
345 /* XXX: If the user is an oper here, we can just extend their user->chans vector by one
346 * and put the channel in here. Same for remote users which are not bound by
347 * the channel limits. Otherwise, nope, youre boned.
352 chanrec* c = ForceChan(Ptr,a,user,created);
353 user->chans.push_back(a);
356 else if (strchr(user->modes,'o'))
358 /* Oper allows extension up to the OPERMAXCHANS value */
359 if (user->chans.size() < OPERMAXCHANS)
362 chanrec* c = ForceChan(Ptr,a,user,created);
363 user->chans.push_back(a);
367 log(DEBUG,"add_channel: user channel max exceeded: %s %s",user->nick,cname);
368 WriteServ(user->fd,"405 %s %s :You are on too many channels",user->nick, cname);
372 chanrec* ForceChan(chanrec* Ptr,ucrec &a,userrec* user, int created)
376 /* first user in is given ops */
377 a.uc_modes = UCMODE_OP;
384 Ptr->AddUser((char*)user);
385 WriteChannel(Ptr,user,"JOIN :%s",Ptr->name);
386 log(DEBUG,"Sent JOIN to client");
389 WriteServ(user->fd,"332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
390 WriteServ(user->fd,"333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
393 WriteServ(user->fd,"366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
394 FOREACH_MOD OnUserJoin(user,Ptr);
398 /* remove a channel from a users record, and remove the record from memory
399 * if the channel has become empty */
401 chanrec* del_channel(userrec *user, const char* cname, const char* reason, bool local)
403 if ((!user) || (!cname))
405 log(DEFAULT,"*** BUG *** del_channel was given an invalid parameter");
409 chanrec* Ptr = FindChan(cname);
414 FOREACH_MOD OnUserPart(user,Ptr);
415 log(DEBUG,"del_channel: removing: %s %s",user->nick,Ptr->name);
417 for (unsigned int i =0; i < user->chans.size(); i++)
419 /* zap it from the channel list of the user */
420 if (user->chans[i].channel == Ptr)
424 WriteChannel(Ptr,user,"PART %s :%s",Ptr->name, reason);
428 WriteChannel(Ptr,user,"PART :%s",Ptr->name);
430 user->chans[i].uc_modes = 0;
431 user->chans[i].channel = NULL;
432 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
437 Ptr->DelUser((char*)user);
439 /* if there are no users left on the channel */
442 chan_hash::iterator iter = chanlist.find(Ptr->name);
444 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
446 /* kill the record */
447 if (iter != chanlist.end())
449 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
451 chanlist.erase(iter);
459 void kick_channel(userrec *src,userrec *user, chanrec *Ptr, char* reason)
461 if ((!src) || (!user) || (!Ptr) || (!reason))
463 log(DEFAULT,"*** BUG *** kick_channel was given an invalid parameter");
467 if ((!Ptr) || (!user) || (!src))
472 log(DEBUG,"kick_channel: removing: %s %s %s",user->nick,Ptr->name,src->nick);
474 if (!has_channel(user,Ptr))
476 WriteServ(src->fd,"441 %s %s %s :They are not on that channel",src->nick, user->nick, Ptr->name);
481 FOREACH_RESULT(OnAccessCheck(src,user,Ptr,AC_KICK));
482 if ((MOD_RESULT == ACR_DENY) && (!is_uline(src->server)))
485 if ((MOD_RESULT == ACR_DEFAULT) || (!is_uline(src->server)))
487 if ((cstatus(src,Ptr) < STATUS_HOP) || (cstatus(src,Ptr) < cstatus(user,Ptr)))
489 if (cstatus(src,Ptr) == STATUS_HOP)
491 WriteServ(src->fd,"482 %s %s :You must be a channel operator",src->nick, Ptr->name);
495 WriteServ(src->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",src->nick, Ptr->name);
502 if (!is_uline(src->server))
505 FOREACH_RESULT(OnUserPreKick(src,user,Ptr,reason));
510 FOREACH_MOD OnUserKick(src,user,Ptr,reason);
512 for (unsigned int i =0; i < user->chans.size(); i++)
514 /* zap it from the channel list of the user */
515 if (user->chans[i].channel)
516 if (!strcasecmp(user->chans[i].channel->name,Ptr->name))
518 WriteChannel(Ptr,src,"KICK %s %s :%s",Ptr->name, user->nick, reason);
519 user->chans[i].uc_modes = 0;
520 user->chans[i].channel = NULL;
521 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
526 Ptr->DelUser((char*)user);
528 /* if there are no users left on the channel */
531 chan_hash::iterator iter = chanlist.find(Ptr->name);
533 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
535 /* kill the record */
536 if (iter != chanlist.end())
538 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
540 chanlist.erase(iter);