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[castuser] = castuser;
164 log(DEBUG,"Added casted user to channel's internal list");
167 void chanrec::DelUser(char* castuser)
169 std::map<char*,char*>::iterator a = internal_userlist.find(castuser);
170 if (a != internal_userlist.end())
172 log(DEBUG,"Removed casted user from channel's internal list");
173 internal_userlist.erase(a);
176 log(DEBUG,"BUG BUG BUG! Attempt to remove an uncasted user from the internal list of %s!",name);
179 std::map<char*,char*> *chanrec::GetUsers()
181 return &internal_userlist;
184 /* add a channel to a user, creating the record for it if needed and linking
185 * it to the user record */
187 chanrec* add_channel(userrec *user, const char* cn, const char* key, bool override)
189 if ((!user) || (!cn))
191 log(DEFAULT,"*** BUG *** add_channel was given an invalid parameter");
198 strncpy(cname,cn,CHANMAX);
200 log(DEBUG,"add_channel: %s %s",user->nick,cname);
202 chanrec* Ptr = FindChan(cname);
209 FOREACH_RESULT(I_OnUserPreJoin,OnUserPreJoin(user,NULL,cname));
213 /* create a new one */
214 chanlist[cname] = new chanrec();
215 strlcpy(chanlist[cname]->name, cname,CHANMAX);
216 chanlist[cname]->binarymodes = CM_TOPICLOCK | CM_NOEXTERNAL;
217 chanlist[cname]->created = TIME;
218 *chanlist[cname]->topic = 0;
219 strncpy(chanlist[cname]->setby, user->nick,NICKMAX);
220 chanlist[cname]->topicset = 0;
221 Ptr = chanlist[cname];
222 log(DEBUG,"add_channel: created: %s",cname);
223 /* set created to 2 to indicate user
224 * is the first in the channel
225 * and should be given ops */
230 /* Already on the channel */
231 if (has_channel(user,Ptr))
234 // remote users are allowed us to bypass channel modes
235 // and bans (used by servers)
239 FOREACH_RESULT(I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname));
244 else if (MOD_RESULT == 0)
249 FOREACH_RESULT(I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
254 log(DEBUG,"add_channel: no key given in JOIN");
255 WriteServ(user->fd,"475 %s %s :Cannot join channel (Requires key)",user->nick, Ptr->name);
260 if (strcasecmp(key,Ptr->key))
262 log(DEBUG,"add_channel: bad key given in JOIN");
263 WriteServ(user->fd,"475 %s %s :Cannot join channel (Incorrect key)",user->nick, Ptr->name);
269 if (Ptr->binarymodes & CM_INVITEONLY)
272 irc::string xname(Ptr->name);
273 FOREACH_RESULT(I_OnCheckInvite,OnCheckInvite(user, Ptr));
276 log(DEBUG,"add_channel: channel is +i");
277 if (user->IsInvited(xname))
279 /* user was invited to channel */
280 /* there may be an optional channel NOTICE here */
284 WriteServ(user->fd,"473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
288 user->RemoveInvite(xname);
293 FOREACH_RESULT(I_OnCheckLimit,OnCheckLimit(user, Ptr));
296 if (usercount(Ptr) >= Ptr->limit)
298 WriteServ(user->fd,"471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
303 if (Ptr->bans.size())
305 log(DEBUG,"add_channel: about to walk banlist");
307 FOREACH_RESULT(I_OnCheckBan,OnCheckBan(user, Ptr));
310 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
312 if (match(user->GetFullHost(),i->data))
314 WriteServ(user->fd,"474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
324 log(DEBUG,"Overridden checks");
329 log(DEBUG,"Passed channel checks");
331 for (unsigned int index =0; index < user->chans.size(); index++)
333 if (user->chans[index].channel == NULL)
335 return ForceChan(Ptr,user->chans[index],user,created);
338 /* XXX: If the user is an oper here, we can just extend their user->chans vector by one
339 * and put the channel in here. Same for remote users which are not bound by
340 * the channel limits. Otherwise, nope, youre boned.
345 chanrec* c = ForceChan(Ptr,a,user,created);
346 user->chans.push_back(a);
349 else if (strchr(user->modes,'o'))
351 /* Oper allows extension up to the OPERMAXCHANS value */
352 if (user->chans.size() < OPERMAXCHANS)
355 chanrec* c = ForceChan(Ptr,a,user,created);
356 user->chans.push_back(a);
360 log(DEBUG,"add_channel: user channel max exceeded: %s %s",user->nick,cname);
361 WriteServ(user->fd,"405 %s %s :You are on too many channels",user->nick, cname);
365 chanrec* ForceChan(chanrec* Ptr,ucrec &a,userrec* user, int created)
369 /* first user in is given ops */
370 a.uc_modes = UCMODE_OP;
377 Ptr->AddUser((char*)user);
378 WriteChannel(Ptr,user,"JOIN :%s",Ptr->name);
379 log(DEBUG,"Sent JOIN to client");
382 WriteServ(user->fd,"332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
383 WriteServ(user->fd,"333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
386 WriteServ(user->fd,"366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
387 FOREACH_MOD(I_OnUserJoin,OnUserJoin(user,Ptr));
391 /* remove a channel from a users record, and remove the record from memory
392 * if the channel has become empty */
394 chanrec* del_channel(userrec *user, const char* cname, const char* reason, bool local)
396 if ((!user) || (!cname))
398 log(DEFAULT,"*** BUG *** del_channel was given an invalid parameter");
402 chanrec* Ptr = FindChan(cname);
407 FOREACH_MOD(I_OnUserPart,OnUserPart(user,Ptr));
408 log(DEBUG,"del_channel: removing: %s %s",user->nick,Ptr->name);
410 for (unsigned int i =0; i < user->chans.size(); i++)
412 /* zap it from the channel list of the user */
413 if (user->chans[i].channel == Ptr)
417 WriteChannel(Ptr,user,"PART %s :%s",Ptr->name, reason);
421 WriteChannel(Ptr,user,"PART :%s",Ptr->name);
423 user->chans[i].uc_modes = 0;
424 user->chans[i].channel = NULL;
425 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
430 Ptr->DelUser((char*)user);
432 /* if there are no users left on the channel */
435 chan_hash::iterator iter = chanlist.find(Ptr->name);
437 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
439 /* kill the record */
440 if (iter != chanlist.end())
442 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
444 chanlist.erase(iter);
452 void kick_channel(userrec *src,userrec *user, chanrec *Ptr, char* reason)
454 if ((!src) || (!user) || (!Ptr) || (!reason))
456 log(DEFAULT,"*** BUG *** kick_channel was given an invalid parameter");
460 if ((!Ptr) || (!user) || (!src))
465 log(DEBUG,"kick_channel: removing: %s %s %s",user->nick,Ptr->name,src->nick);
467 if (!has_channel(user,Ptr))
469 WriteServ(src->fd,"441 %s %s %s :They are not on that channel",src->nick, user->nick, Ptr->name);
474 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,Ptr,AC_KICK));
475 if ((MOD_RESULT == ACR_DENY) && (!is_uline(src->server)))
478 if ((MOD_RESULT == ACR_DEFAULT) || (!is_uline(src->server)))
480 if ((cstatus(src,Ptr) < STATUS_HOP) || (cstatus(src,Ptr) < cstatus(user,Ptr)))
482 if (cstatus(src,Ptr) == STATUS_HOP)
484 WriteServ(src->fd,"482 %s %s :You must be a channel operator",src->nick, Ptr->name);
488 WriteServ(src->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",src->nick, Ptr->name);
495 if (!is_uline(src->server))
498 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,Ptr,reason));
503 FOREACH_MOD(I_OnUserKick,OnUserKick(src,user,Ptr,reason));
505 for (unsigned int i =0; i < user->chans.size(); i++)
507 /* zap it from the channel list of the user */
508 if (user->chans[i].channel)
509 if (!strcasecmp(user->chans[i].channel->name,Ptr->name))
511 WriteChannel(Ptr,src,"KICK %s %s :%s",Ptr->name, user->nick, reason);
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);
533 chanlist.erase(iter);