]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/channels.cpp
Fixes to make server kicks work
[user/henk/code/inspircd.git] / src / channels.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include "inspircd_config.h"
20 #include "inspircd.h"
21 #include "inspircd_io.h"
22 #include <unistd.h>
23 #include <sys/errno.h>
24 #include <sys/ioctl.h>
25 #include <sys/utsname.h>
26 #include <time.h>
27 #include <string>
28 #ifdef GCC3
29 #include <ext/hash_map>
30 #else
31 #include <hash_map>
32 #endif
33 #include <map>
34 #include <sstream>
35 #include <vector>
36 #include <deque>
37 #include "users.h"
38 #include "ctables.h"
39 #include "globals.h"
40 #include "modules.h"
41 #include "dynamic.h"
42 #include "commands.h"
43 #include "wildcard.h"
44 #include "message.h"
45 #include "mode.h"
46 #include "xline.h"
47 #include "inspstring.h"
48 #include "helperfuncs.h"
49 #include "typedefs.h"
50
51 #ifdef GCC3
52 #define nspace __gnu_cxx
53 #else
54 #define nspace std
55 #endif
56
57 extern ServerConfig* Config;
58
59 extern int MODCOUNT;
60 extern std::vector<Module*> modules;
61 extern std::vector<ircd_module*> factory;
62 extern int WHOWAS_STALE;
63 extern int WHOWAS_MAX;
64 extern time_t TIME;
65 extern chan_hash chanlist;
66
67 using namespace std;
68
69 std::vector<ModeParameter> custom_mode_params;
70
71 chanrec* ForceChan(chanrec* Ptr,ucrec &a,userrec* user, int created);
72
73 chanrec::chanrec()
74 {
75         *name = *custom_modes = *topic = *setby = *key = 0;
76         created = topicset = limit = binarymodes = 0;
77         internal_userlist.clear();
78 }
79
80 void chanrec::SetCustomMode(char mode,bool mode_on)
81 {
82         if (mode_on) {
83                 static char m[3];
84                 m[0] = mode;
85                 m[1] = '\0';
86                 if (!strchr(this->custom_modes,mode))
87                 {
88                         strlcat(custom_modes,m,MAXMODES);
89                 }
90                 log(DEBUG,"Custom mode %c set",mode);
91         }
92         else {
93
94                 std::string a = this->custom_modes;
95                 int pos = a.find(mode);
96                 a.erase(pos,1);
97                 strlcpy(this->custom_modes,a.c_str(),MAXMODES);
98
99                 log(DEBUG,"Custom mode %c removed: modelist='%s'",mode,this->custom_modes);
100                 this->SetCustomModeParam(mode,"",false);
101         }
102 }
103
104
105 void chanrec::SetCustomModeParam(char mode,char* parameter,bool mode_on)
106 {
107
108         log(DEBUG,"SetCustomModeParam called");
109         ModeParameter M;
110         M.mode = mode;
111         strlcpy(M.channel,this->name,CHANMAX);
112         strlcpy(M.parameter,parameter,MAXBUF);
113         if (mode_on)
114         {
115                 log(DEBUG,"Custom mode parameter %c %s added",mode,parameter);
116                 custom_mode_params.push_back(M);
117         }
118         else
119         {
120                 if (custom_mode_params.size())
121                 {
122                         for (vector<ModeParameter>::iterator i = custom_mode_params.begin(); i < custom_mode_params.end(); i++)
123                         {
124                                 if ((i->mode == mode) && (!strcasecmp(this->name,i->channel)))
125                                 {
126                                         log(DEBUG,"Custom mode parameter %c %s removed",mode,parameter);
127                                         custom_mode_params.erase(i);
128                                         return;
129                                 }
130                         }
131                 }
132                 log(DEBUG,"*** BUG *** Attempt to remove non-existent mode parameter!");
133         }
134 }
135
136 bool chanrec::IsCustomModeSet(char mode)
137 {
138         return (strchr(this->custom_modes,mode));
139 }
140
141 std::string chanrec::GetModeParameter(char mode)
142 {
143         if (custom_mode_params.size())
144         {
145                 for (vector<ModeParameter>::iterator i = custom_mode_params.begin(); i < custom_mode_params.end(); i++)
146                 {
147                         if ((i->mode == mode) && (!strcasecmp(this->name,i->channel)))
148                         {
149                                 return i->parameter;
150                         }
151                 }
152         }
153         return "";
154 }
155
156 long chanrec::GetUserCounter()
157 {
158         return (this->internal_userlist.size());
159 }
160
161 void chanrec::AddUser(char* castuser)
162 {
163         internal_userlist[castuser] = castuser;
164         log(DEBUG,"Added casted user to channel's internal list");
165 }
166
167 void chanrec::DelUser(char* castuser)
168 {
169         std::map<char*,char*>::iterator a = internal_userlist.find(castuser);
170         if (a != internal_userlist.end())
171         {
172                 log(DEBUG,"Removed casted user from channel's internal list");
173                 internal_userlist.erase(a);
174                 return;
175         }
176         log(DEBUG,"BUG BUG BUG! Attempt to remove an uncasted user from the internal list of %s!",name);
177 }
178
179 std::map<char*,char*> *chanrec::GetUsers()
180 {
181         return &internal_userlist;
182 }
183
184 /* add a channel to a user, creating the record for it if needed and linking
185  * it to the user record */
186
187 chanrec* add_channel(userrec *user, const char* cn, const char* key, bool override)
188 {
189         if ((!user) || (!cn))
190         {
191                 log(DEFAULT,"*** BUG *** add_channel was given an invalid parameter");
192                 return 0;
193         }
194
195         int created = 0;
196         char cname[MAXBUF];
197         int MOD_RESULT = 0;
198         strlcpy(cname,cn,CHANMAX);
199         log(DEBUG,"cname='%s' cn='%s'",cname,cn);
200
201         log(DEBUG,"add_channel: %s %s",user->nick,cname);
202
203         chanrec* Ptr = FindChan(cname);
204
205         if (!Ptr)
206         {
207                 if (user->fd > -1)
208                 {
209                         MOD_RESULT = 0;
210                         FOREACH_RESULT(I_OnUserPreJoin,OnUserPreJoin(user,NULL,cname));
211                         if (MOD_RESULT == 1)
212                                 return NULL;
213                 }
214                 /* create a new one */
215                 chanlist[cname] = new chanrec();
216                 strlcpy(chanlist[cname]->name, cname,CHANMAX);
217                 chanlist[cname]->binarymodes = CM_TOPICLOCK | CM_NOEXTERNAL;
218                 chanlist[cname]->created = TIME;
219                 *chanlist[cname]->topic = 0;
220                 strlcpy(chanlist[cname]->setby, user->nick,NICKMAX);
221                 chanlist[cname]->topicset = 0;
222                 Ptr = chanlist[cname];
223                 log(DEBUG,"add_channel: created: %s",cname);
224                 /* set created to 2 to indicate user
225                  * is the first in the channel
226                  * and should be given ops */
227                 created = 2;
228         }
229         else
230         {
231                 /* Already on the channel */
232                 if (has_channel(user,Ptr))
233                         return NULL;
234
235                 // remote users are allowed us to bypass channel modes
236                 // and bans (used by servers)
237                 if (user->fd > -1)
238                 {
239                         MOD_RESULT = 0;
240                         FOREACH_RESULT(I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname));
241                         if (MOD_RESULT == 1)
242                         {
243                                 return NULL;
244                         }
245                         else if (MOD_RESULT == 0)
246                         {
247                                 if (*Ptr->key)
248                                 {
249                                         MOD_RESULT = 0;
250                                         FOREACH_RESULT(I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
251                                         if (!MOD_RESULT)
252                                         {
253                                                 if (!key)
254                                                 {
255                                                         log(DEBUG,"add_channel: no key given in JOIN");
256                                                         WriteServ(user->fd,"475 %s %s :Cannot join channel (Requires key)",user->nick, Ptr->name);
257                                                         return NULL;
258                                                 }
259                                                 else
260                                                 {
261                                                         if (strcasecmp(key,Ptr->key))
262                                                         {
263                                                                 log(DEBUG,"add_channel: bad key given in JOIN");
264                                                                 WriteServ(user->fd,"475 %s %s :Cannot join channel (Incorrect key)",user->nick, Ptr->name);
265                                                                 return NULL;
266                                                         }
267                                                 }
268                                         }
269                                 }
270                                 if (Ptr->binarymodes & CM_INVITEONLY)
271                                 {
272                                         MOD_RESULT = 0;
273                                         irc::string xname(Ptr->name);
274                                         FOREACH_RESULT(I_OnCheckInvite,OnCheckInvite(user, Ptr));
275                                         if (!MOD_RESULT)
276                                         {
277                                                 log(DEBUG,"add_channel: channel is +i");
278                                                 if (user->IsInvited(xname))
279                                                 {
280                                                         /* user was invited to channel */
281                                                         /* there may be an optional channel NOTICE here */
282                                                 }
283                                                 else
284                                                 {
285                                                         WriteServ(user->fd,"473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
286                                                         return NULL;
287                                                 }
288                                         }
289                                         user->RemoveInvite(xname);
290                                 }
291                                 if (Ptr->limit)
292                                 {
293                                         MOD_RESULT = 0;
294                                         FOREACH_RESULT(I_OnCheckLimit,OnCheckLimit(user, Ptr));
295                                         if (!MOD_RESULT)
296                                         {
297                                                 if (usercount(Ptr) >= Ptr->limit)
298                                                 {
299                                                         WriteServ(user->fd,"471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
300                                                         return NULL;
301                                                 }
302                                         }
303                                 }
304                                 if (Ptr->bans.size())
305                                 {
306                                         log(DEBUG,"add_channel: about to walk banlist");
307                                         MOD_RESULT = 0;
308                                         FOREACH_RESULT(I_OnCheckBan,OnCheckBan(user, Ptr));
309                                         if (!MOD_RESULT)
310                                         {
311                                                 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
312                                                 {
313                                                         if (match(user->GetFullHost(),i->data))
314                                                         {
315                                                                 WriteServ(user->fd,"474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
316                                                                 return NULL;
317                                                         }
318                                                 }
319                                         }
320                                 }
321                         }
322                 }
323                 else
324                 {
325                         log(DEBUG,"Overridden checks");
326                 }
327                 created = 1;
328         }
329
330         log(DEBUG,"Passed channel checks");
331
332         for (unsigned int index =0; index < user->chans.size(); index++)
333         {
334                 if (user->chans[index].channel == NULL)
335                 {
336                         return ForceChan(Ptr,user->chans[index],user,created);
337                 }
338         }
339         /* XXX: If the user is an oper here, we can just extend their user->chans vector by one
340          * and put the channel in here. Same for remote users which are not bound by
341          * the channel limits. Otherwise, nope, youre boned.
342          */
343         if (user->fd < 0)
344         {
345                 ucrec a;
346                 chanrec* c = ForceChan(Ptr,a,user,created);
347                 user->chans.push_back(a);
348                 return c;
349         }
350         else if (strchr(user->modes,'o'))
351         {
352                 /* Oper allows extension up to the OPERMAXCHANS value */
353                 if (user->chans.size() < OPERMAXCHANS)
354                 {
355                         ucrec a;
356                         chanrec* c = ForceChan(Ptr,a,user,created);
357                         user->chans.push_back(a);
358                         return c;
359                 }
360         }
361         log(DEBUG,"add_channel: user channel max exceeded: %s %s",user->nick,cname);
362         WriteServ(user->fd,"405 %s %s :You are on too many channels",user->nick, cname);
363         return NULL;
364 }
365
366 chanrec* ForceChan(chanrec* Ptr,ucrec &a,userrec* user, int created)
367 {
368         if (created == 2)
369         {
370                 /* first user in is given ops */
371                 a.uc_modes = UCMODE_OP;
372         }
373         else
374         {
375                 a.uc_modes = 0;
376         }
377         a.channel = Ptr;
378         Ptr->AddUser((char*)user);
379         WriteChannel(Ptr,user,"JOIN :%s",Ptr->name);
380         log(DEBUG,"Sent JOIN to client");
381         if (Ptr->topicset)
382         {
383                 WriteServ(user->fd,"332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
384                 WriteServ(user->fd,"333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
385         }
386         userlist(user,Ptr);
387         WriteServ(user->fd,"366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
388         FOREACH_MOD(I_OnUserJoin,OnUserJoin(user,Ptr));
389         return Ptr;
390 }
391
392 /* remove a channel from a users record, and remove the record from memory
393  * if the channel has become empty */
394
395 chanrec* del_channel(userrec *user, const char* cname, const char* reason, bool local)
396 {
397         if ((!user) || (!cname))
398         {
399                 log(DEFAULT,"*** BUG *** del_channel was given an invalid parameter");
400                 return NULL;
401         }
402
403         chanrec* Ptr = FindChan(cname);
404
405         if (!Ptr)
406                 return NULL;
407
408         log(DEBUG,"del_channel: removing: %s %s",user->nick,Ptr->name);
409
410         for (unsigned int i =0; i < user->chans.size(); i++)
411         {
412                 /* zap it from the channel list of the user */
413                 if (user->chans[i].channel == Ptr)
414                 {
415                         if (reason)
416                         {
417                                 FOREACH_MOD(I_OnUserPart,OnUserPart(user,Ptr,reason));
418                                 WriteChannel(Ptr,user,"PART %s :%s",Ptr->name, reason);
419                         }
420                         else
421                         {
422                                 FOREACH_MOD(I_OnUserPart,OnUserPart(user,Ptr,""));
423                                 WriteChannel(Ptr,user,"PART :%s",Ptr->name);
424                         }
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);
428                         break;
429                 }
430         }
431
432         Ptr->DelUser((char*)user);
433
434         /* if there are no users left on the channel */
435         if (!usercount(Ptr))
436         {
437                 chan_hash::iterator iter = chanlist.find(Ptr->name);
438
439                 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
440
441                 /* kill the record */
442                 if (iter != chanlist.end())
443                 {
444                         log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
445                         FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
446                         delete Ptr;
447                         chanlist.erase(iter);
448                 }
449         }
450
451         return NULL;
452 }
453
454 void server_kick_channel(userrec* user, chanrec* Ptr, char* reason, bool triggerevents)
455 {
456         if ((!user) || (!Ptr) || (!reason))
457         {
458                 return;
459         }
460
461         if (!has_channel(user,Ptr))
462         {
463                 /* Not on channel */
464                 return;
465         }
466
467         if (triggerevents)
468         {
469                 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL,user,Ptr,reason));
470         }
471
472         for (unsigned int i =0; i < user->chans.size(); i++)
473         {
474                 if (user->chans[i].channel)
475                 if (!strcasecmp(user->chans[i].channel->name,Ptr->name))
476                 {
477                         WriteChannelWithServ(Config->ServerName,Ptr,"KICK %s %s :%s",Ptr->name, user->nick, reason);
478                         user->chans[i].uc_modes = 0;
479                         user->chans[i].channel = NULL;
480                         break;
481                 }
482         }
483
484         Ptr->DelUser((char*)user);
485
486         if (!usercount(Ptr))
487         {
488                 chan_hash::iterator iter = chanlist.find(Ptr->name);
489                 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
490                 /* kill the record */
491                 if (iter != chanlist.end())
492                 {
493                         log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
494                         FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
495                         delete Ptr;
496                         chanlist.erase(iter);
497                 }
498         }
499 }
500
501 void kick_channel(userrec *src,userrec *user, chanrec *Ptr, char* reason)
502 {
503         if ((!src) || (!user) || (!Ptr) || (!reason))
504         {
505                 log(DEFAULT,"*** BUG *** kick_channel was given an invalid parameter");
506                 return;
507         }
508
509         log(DEBUG,"kick_channel: removing: %s %s %s",user->nick,Ptr->name,src->nick);
510
511         if (!has_channel(user,Ptr))
512         {
513                 WriteServ(src->fd,"441 %s %s %s :They are not on that channel",src->nick, user->nick, Ptr->name);
514                 return;
515         }
516
517         int MOD_RESULT = 0;
518         FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,Ptr,AC_KICK));
519         if ((MOD_RESULT == ACR_DENY) && (!is_uline(src->server)))
520                 return;
521
522         if ((MOD_RESULT == ACR_DEFAULT) || (!is_uline(src->server)))
523         {
524                 if ((cstatus(src,Ptr) < STATUS_HOP) || (cstatus(src,Ptr) < cstatus(user,Ptr)))
525                 {
526                         if (cstatus(src,Ptr) == STATUS_HOP)
527                         {
528                                 WriteServ(src->fd,"482 %s %s :You must be a channel operator",src->nick, Ptr->name);
529                         }
530                         else
531                         {
532                                 WriteServ(src->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",src->nick, Ptr->name);
533                         }
534
535                         return;
536                 }
537         }
538
539         if (!is_uline(src->server))
540         {
541                 MOD_RESULT = 0;
542                 FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,Ptr,reason));
543                 if (MOD_RESULT)
544                         return;
545         }
546
547         FOREACH_MOD(I_OnUserKick,OnUserKick(src,user,Ptr,reason));
548
549         for (unsigned int i =0; i < user->chans.size(); i++)
550         {
551                 /* zap it from the channel list of the user */
552                 if (user->chans[i].channel)
553                 if (!strcasecmp(user->chans[i].channel->name,Ptr->name))
554                 {
555                         WriteChannel(Ptr,src,"KICK %s %s :%s",Ptr->name, user->nick, reason);
556                         user->chans[i].uc_modes = 0;
557                         user->chans[i].channel = NULL;
558                         log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
559                         break;
560                 }
561         }
562
563         Ptr->DelUser((char*)user);
564
565         /* if there are no users left on the channel */
566         if (!usercount(Ptr))
567         {
568                 chan_hash::iterator iter = chanlist.find(Ptr->name);
569
570                 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
571
572                 /* kill the record */
573                 if (iter != chanlist.end())
574                 {
575                         log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
576                         FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
577                         delete Ptr;
578                         chanlist.erase(iter);
579                 }
580         }
581 }
582
583