]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/channels.cpp
Allow CIDR channel bans to be matched
[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 <string>
20 #include <map>
21 #include <sstream>
22 #include <vector>
23 #include <deque>
24 #include "configreader.h"
25 #include "inspircd.h"
26 #include "hash_map.h"
27 #include "users.h"
28 #include "ctables.h"
29 #include "globals.h"
30 #include "modules.h"
31 #include "dynamic.h"
32 #include "commands.h"
33 #include "wildcard.h"
34 #include "message.h"
35 #include "mode.h"
36 #include "xline.h"
37 #include "inspstring.h"
38 #include "helperfuncs.h"
39 #include "typedefs.h"
40
41 extern ServerConfig* Config;
42
43 extern int MODCOUNT;
44 extern std::vector<Module*> modules;
45 extern std::vector<ircd_module*> factory;
46 extern time_t TIME;
47 extern chan_hash chanlist;
48
49
50 chanrec* ForceChan(chanrec* Ptr,ucrec *a,userrec* user, int created);
51
52 chanrec::chanrec()
53 {
54         *name = *topic = *setby = *key = 0;
55         created = topicset = limit = 0;
56         internal_userlist.clear();
57         memset(&modes,0,64);
58 }
59
60 void chanrec::SetMode(char mode,bool mode_on)
61 {
62         modes[mode-65] = mode_on;
63         if (!mode_on)
64                 this->SetModeParam(mode,"",false);
65 }
66
67
68 void chanrec::SetModeParam(char mode,const char* parameter,bool mode_on)
69 {
70         log(DEBUG,"SetModeParam called");
71         
72         CustomModeList::iterator n = custom_mode_params.find(mode);     
73
74         if (mode_on)
75         {
76                 if (n == custom_mode_params.end())
77                 {
78                         custom_mode_params[mode] = strdup(parameter);
79                         log(DEBUG,"Custom mode parameter %c %s added",mode,parameter);
80                 }
81                 else
82                 {
83                         log(DEBUG, "Tried to set custom mode parameter for %c '%s' when it was already '%s'", mode, parameter, n->second);
84                 }
85         }
86         else
87         {
88                 if (n != custom_mode_params.end())
89                 {
90                         free(n->second);
91                         custom_mode_params.erase(n);
92                 }
93         }
94 }
95
96 bool chanrec::IsModeSet(char mode)
97 {
98         return modes[mode-65];
99 }
100
101 std::string chanrec::GetModeParameter(char mode)
102 {
103         if (mode == 'k')
104         {
105                 return this->key;
106         }
107         else if (mode == 'l')
108         {
109                 return ConvToStr(this->limit);
110         }
111         else
112         {
113                 CustomModeList::iterator n = custom_mode_params.find(mode);
114                 if (n != custom_mode_params.end())
115                 {
116                         return n->second;
117                 }
118                 return "";
119         }
120 }
121
122 long chanrec::GetUserCounter()
123 {
124         return (this->internal_userlist.size());
125 }
126
127 void chanrec::AddUser(userrec* user)
128 {
129         internal_userlist[user] = user;
130 }
131
132 unsigned long chanrec::DelUser(userrec* user)
133 {
134         CUListIter a = internal_userlist.find(user);
135         
136         if (a != internal_userlist.end())
137         {
138                 internal_userlist.erase(a);
139                 /* And tidy any others... */
140                 DelOppedUser(user);
141                 DelHalfoppedUser(user);
142                 DelVoicedUser(user);
143         }
144         
145         return internal_userlist.size();
146 }
147
148 bool chanrec::HasUser(userrec* user)
149 {
150         return (internal_userlist.find(user) != internal_userlist.end());
151 }
152
153 void chanrec::AddOppedUser(userrec* user)
154 {
155         internal_op_userlist[user] = user;
156 }
157
158 void chanrec::DelOppedUser(userrec* user)
159 {
160         CUListIter a = internal_op_userlist.find(user);
161         if (a != internal_op_userlist.end())
162         {
163                 internal_op_userlist.erase(a);
164                 return;
165         }
166 }
167
168 void chanrec::AddHalfoppedUser(userrec* user)
169 {
170         internal_halfop_userlist[user] = user;
171 }
172
173 void chanrec::DelHalfoppedUser(userrec* user)
174 {
175         CUListIter a = internal_halfop_userlist.find(user);
176
177         if (a != internal_halfop_userlist.end())
178         {   
179                 internal_halfop_userlist.erase(a);
180         }
181 }
182
183 void chanrec::AddVoicedUser(userrec* user)
184 {
185         internal_voice_userlist[user] = user;
186 }
187
188 void chanrec::DelVoicedUser(userrec* user)
189 {
190         CUListIter a = internal_voice_userlist.find(user);
191         
192         if (a != internal_voice_userlist.end())
193         {
194                 internal_voice_userlist.erase(a);
195         }
196 }
197
198 CUList* chanrec::GetUsers()
199 {
200         return &internal_userlist;
201 }
202
203 CUList* chanrec::GetOppedUsers()
204 {
205         return &internal_op_userlist;
206 }
207
208 CUList* chanrec::GetHalfoppedUsers()
209 {
210         return &internal_halfop_userlist;
211 }
212
213 CUList* chanrec::GetVoicedUsers()
214 {
215         return &internal_voice_userlist;
216 }
217
218 /* 
219  * add a channel to a user, creating the record for it if needed and linking
220  * it to the user record 
221  */
222
223 chanrec* add_channel(userrec *user, const char* cn, const char* key, bool override)
224 {
225         if ((!user) || (!cn))
226         {
227                 log(DEFAULT,"*** BUG *** add_channel was given an invalid parameter");
228                 return 0;
229         }
230
231         int created = 0;
232         char cname[MAXBUF];
233         int MOD_RESULT = 0;
234         strlcpy(cname,cn,CHANMAX);
235         log(DEBUG,"cname='%s' cn='%s'",cname,cn);
236
237         log(DEBUG,"add_channel: %s %s",user->nick,cname);
238
239         chanrec* Ptr = FindChan(cname);
240
241         if (!Ptr)
242         {
243                 if (user->fd > -1)
244                 {
245                         MOD_RESULT = 0;
246                         FOREACH_RESULT(I_OnUserPreJoin,OnUserPreJoin(user,NULL,cname));
247                         if (MOD_RESULT == 1)
248                                 return NULL;
249                 }
250
251                 /* create a new one */
252                 chanlist[cname] = new chanrec();
253                 strlcpy(chanlist[cname]->name, cname,CHANMAX);
254                 chanlist[cname]->modes[CM_TOPICLOCK] = chanlist[cname]->modes[CM_NOEXTERNAL] = 1;
255                 //chanlist[cname]->binarymodes = CM_TOPICLOCK | CM_NOEXTERNAL;
256                 chanlist[cname]->created = TIME;
257                 *chanlist[cname]->topic = 0;
258                 strlcpy(chanlist[cname]->setby, user->nick,NICKMAX-1);
259                 chanlist[cname]->topicset = 0;
260                 Ptr = chanlist[cname];
261                 log(DEBUG,"add_channel: created: %s",cname);
262                 /*
263                  * set created to 2 to indicate user
264                  * is the first in the channel
265                  * and should be given ops
266                  */
267                 created = 2;
268         }
269         else
270         {
271                 /* Already on the channel */
272                 if (Ptr->HasUser(user))
273                         return NULL;
274
275                 /*
276                  * remote users are allowed us to bypass channel modes
277                  * and bans (used by servers)
278                  */
279                 if (IS_LOCAL(user)) /* was a check on fd > -1 */
280                 {
281                         MOD_RESULT = 0;
282                         FOREACH_RESULT(I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname));
283                         if (MOD_RESULT == 1)
284                         {
285                                 return NULL;
286                         }
287                         else if (MOD_RESULT == 0)
288                         {
289                                 if (*Ptr->key)
290                                 {
291                                         MOD_RESULT = 0;
292                                         FOREACH_RESULT(I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
293                                         if (!MOD_RESULT)
294                                         {
295                                                 if (!key)
296                                                 {
297                                                         log(DEBUG,"add_channel: no key given in JOIN");
298                                                         WriteServ(user->fd,"475 %s %s :Cannot join channel (Requires key)",user->nick, Ptr->name);
299                                                         return NULL;
300                                                 }
301                                                 else
302                                                 {
303                                                         if (strcmp(key,Ptr->key))
304                                                         {
305                                                                 log(DEBUG,"add_channel: bad key given in JOIN");
306                                                                 WriteServ(user->fd,"475 %s %s :Cannot join channel (Incorrect key)",user->nick, Ptr->name);
307                                                                 return NULL;
308                                                         }
309                                                 }
310                                         }
311                                 }
312                                 if (Ptr->modes[CM_INVITEONLY])
313                                 {
314                                         MOD_RESULT = 0;
315                                         irc::string xname(Ptr->name);
316                                         FOREACH_RESULT(I_OnCheckInvite,OnCheckInvite(user, Ptr));
317                                         if (!MOD_RESULT)
318                                         {
319                                                 log(DEBUG,"add_channel: channel is +i");
320                                                 if (user->IsInvited(xname))
321                                                 {
322                                                         /* user was invited to channel */
323                                                         /* there may be an optional channel NOTICE here */
324                                                 }
325                                                 else
326                                                 {
327                                                         WriteServ(user->fd,"473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
328                                                         return NULL;
329                                                 }
330                                         }
331                                         user->RemoveInvite(xname);
332                                 }
333                                 if (Ptr->limit)
334                                 {
335                                         MOD_RESULT = 0;
336                                         FOREACH_RESULT(I_OnCheckLimit,OnCheckLimit(user, Ptr));
337                                         if (!MOD_RESULT)
338                                         {
339                                                 if (usercount(Ptr) >= Ptr->limit)
340                                                 {
341                                                         WriteServ(user->fd,"471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
342                                                         return NULL;
343                                                 }
344                                         }
345                                 }
346                                 if (Ptr->bans.size())
347                                 {
348                                         log(DEBUG,"add_channel: about to walk banlist");
349                                         MOD_RESULT = 0;
350                                         FOREACH_RESULT(I_OnCheckBan,OnCheckBan(user, Ptr));
351                                         char mask[MAXBUF];
352                                         sprintf(mask,"%s@%s",user->ident, user->GetIPString());
353                                         if (!MOD_RESULT)
354                                         {
355                                                 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
356                                                 {
357                                                         /* This allows CIDR ban matching */
358                                                         if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match(mask, i->data, true)))
359                                                         {
360                                                                 WriteServ(user->fd,"474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
361                                                                 return NULL;
362                                                         }
363                                                 }
364                                         }
365                                 }
366                         }
367                 }
368                 else
369                 {
370                         log(DEBUG,"Overridden checks");
371                 }
372                 created = 1;
373         }
374
375         log(DEBUG,"Passed channel checks");
376
377         for (UserChanList::const_iterator index = user->chans.begin(); index != user->chans.end(); index++)
378         {
379                 if ((*index)->channel == NULL)
380                 {
381                         return ForceChan(Ptr, *index, user, created);
382                 }
383         }
384
385         /*
386          * XXX: If the user is an oper here, we can just extend their user->chans vector by one
387          * and put the channel in here. Same for remote users which are not bound by
388          * the channel limits. Otherwise, nope, youre boned.
389          */
390         if (!IS_LOCAL(user)) /* was a check on fd < 0 */
391         {
392                 ucrec* a = new ucrec();
393                 chanrec* c = ForceChan(Ptr,a,user,created);
394                 user->chans.push_back(a);
395                 return c;
396         }
397         else if (*user->oper)
398         {
399                 /* Oper allows extension up to the OPERMAXCHANS value */
400                 if (user->chans.size() < OPERMAXCHANS)
401                 {
402                         ucrec* a = new ucrec();
403                         chanrec* c = ForceChan(Ptr,a,user,created);
404                         user->chans.push_back(a);
405                         return c;
406                 }
407         }
408
409         log(DEBUG,"add_channel: user channel max exceeded: %s %s",user->nick,cname);
410         WriteServ(user->fd,"405 %s %s :You are on too many channels",user->nick, cname);
411
412         if (created == 2)
413         {
414                 log(DEBUG,"BLAMMO, Whacking channel.");
415                 /* Things went seriously pear shaped, so take this away. bwahaha. */
416                 chan_hash::iterator n = chanlist.find(cname);
417                 if (n != chanlist.end())
418                 {
419                         Ptr->DelUser(user);
420                         DELETE(Ptr);
421                         chanlist.erase(n);
422                         for (unsigned int index =0; index < user->chans.size(); index++)
423                         {
424                                 if (user->chans[index]->channel == Ptr)
425                                 {
426                                         user->chans[index]->channel = NULL;
427                                         user->chans[index]->uc_modes = 0;       
428                                 }
429                         }
430                 }
431         }
432         else
433         {
434                 for (unsigned int index =0; index < user->chans.size(); index++)
435                 {
436                         if (user->chans[index]->channel == Ptr)
437                         {
438                                 user->chans[index]->channel = NULL;
439                                 user->chans[index]->uc_modes = 0;
440                         }
441                 }
442         }
443         return NULL;
444 }
445
446 chanrec* ForceChan(chanrec* Ptr,ucrec *a,userrec* user, int created)
447 {
448         if (created == 2)
449         {
450                 /* first user in is given ops */
451                 a->uc_modes = UCMODE_OP;
452                 Ptr->AddOppedUser(user);
453         }
454         else
455         {
456                 a->uc_modes = 0;
457         }
458
459         a->channel = Ptr;
460         Ptr->AddUser(user);
461         WriteChannel(Ptr,user,"JOIN :%s",Ptr->name);
462
463         /* Major improvement by Brain - we dont need to be calculating all this pointlessly for remote users */
464         if (IS_LOCAL(user))
465         {
466                 log(DEBUG,"Sent JOIN to client");
467                 if (Ptr->topicset)
468                 {
469                         WriteServ(user->fd,"332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
470                         WriteServ(user->fd,"333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
471                 }
472                 userlist(user,Ptr);
473                 WriteServ(user->fd,"366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
474         }
475         FOREACH_MOD(I_OnUserJoin,OnUserJoin(user,Ptr));
476         return Ptr;
477 }
478
479 /*
480  *remove a channel from a users record, and remove the record from memory
481  * if the channel has become empty
482  */
483
484 chanrec* del_channel(userrec *user, const char* cname, const char* reason, bool local)
485 {
486         if ((!user) || (!cname))
487         {
488                 log(DEFAULT,"*** BUG *** del_channel was given an invalid parameter");
489                 return NULL;
490         }
491
492         chanrec* Ptr = FindChan(cname);
493
494         if (!Ptr)
495                 return NULL;
496
497         log(DEBUG,"del_channel: removing: %s %s",user->nick,Ptr->name);
498
499         for (unsigned int i =0; i < user->chans.size(); i++)
500         {
501                 /* zap it from the channel list of the user */
502                 if (user->chans[i]->channel == Ptr)
503                 {
504                         if (reason)
505                         {
506                                 FOREACH_MOD(I_OnUserPart,OnUserPart(user,Ptr,reason));
507                                 WriteChannel(Ptr,user,"PART %s :%s",Ptr->name, reason);
508                         }
509                         else
510                         {
511                                 FOREACH_MOD(I_OnUserPart,OnUserPart(user,Ptr,""));
512                                 WriteChannel(Ptr,user,"PART :%s",Ptr->name);
513                         }
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);
517                         break;
518                 }
519         }
520
521         Ptr->DelUser(user);
522
523         /* if there are no users left on the channel */
524         if (!usercount(Ptr))
525         {
526                 chan_hash::iterator iter = chanlist.find(Ptr->name);
527
528                 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
529
530                 /* kill the record */
531                 if (iter != chanlist.end())
532                 {
533                         log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
534                         FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
535                         DELETE(Ptr);
536                         chanlist.erase(iter);
537                 }
538         }
539
540         return NULL;
541 }
542
543 void server_kick_channel(userrec* user, chanrec* Ptr, char* reason, bool triggerevents)
544 {
545         if ((!user) || (!Ptr) || (!reason))
546         {
547                 return;
548         }
549
550         if (IS_LOCAL(user))
551         {
552                 if (!Ptr->HasUser(user))
553                 {
554                         /* Not on channel */
555                         return;
556                 }
557         }
558         
559         if (triggerevents)
560         {
561                 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL,user,Ptr,reason));
562         }
563
564         for (unsigned int i =0; i < user->chans.size(); i++)
565         {
566                 if ((user->chans[i]->channel) && (user->chans[i]->channel == Ptr))
567                 {
568                         WriteChannelWithServ(Config->ServerName,Ptr,"KICK %s %s :%s",Ptr->name, user->nick, reason);
569                         user->chans[i]->uc_modes = 0;
570                         user->chans[i]->channel = NULL;
571                         break;
572                 }
573         }
574
575         Ptr->DelUser(user);
576
577         if (!usercount(Ptr))
578         {
579                 chan_hash::iterator iter = chanlist.find(Ptr->name);
580                 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
581                 /* kill the record */
582                 if (iter != chanlist.end())
583                 {
584                         log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
585                         FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
586                         DELETE(Ptr);
587                         chanlist.erase(iter);
588                 }
589         }
590 }
591
592 void kick_channel(userrec *src,userrec *user, chanrec *Ptr, char* reason)
593 {
594         if ((!src) || (!user) || (!Ptr) || (!reason))
595         {
596                 log(DEFAULT,"*** BUG *** kick_channel was given an invalid parameter");
597                 return;
598         }
599
600         log(DEBUG,"kick_channel: removing: %s %s %s",user->nick,Ptr->name,src->nick);
601
602         if (IS_LOCAL(src))
603         {
604                 if (!Ptr->HasUser(user))
605                 {
606                         WriteServ(src->fd,"441 %s %s %s :They are not on that channel",src->nick, user->nick, Ptr->name);
607                         return;
608                 }
609                 if ((is_uline(user->server)) && (!is_uline(src->server)))
610                 {
611                         WriteServ(src->fd,"482 %s %s :Only a u-line may kick a u-line from a channel.",src->nick, Ptr->name);
612                         return;
613                 }
614                 int MOD_RESULT = 0;
615
616                 if (!is_uline(src->server))
617                 {
618                         MOD_RESULT = 0;
619                         FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,Ptr,reason));
620                         if (MOD_RESULT == 1)
621                                 return;
622                 }
623                 /* Set to -1 by OnUserPreKick if explicit allow was set */
624                 if (MOD_RESULT != -1)
625                 {
626                         FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,Ptr,AC_KICK));
627                         if ((MOD_RESULT == ACR_DENY) && (!is_uline(src->server)))
628                                 return;
629         
630                         if ((MOD_RESULT == ACR_DEFAULT) || (!is_uline(src->server)))
631                         {
632                                 if ((cstatus(src,Ptr) < STATUS_HOP) || (cstatus(src,Ptr) < cstatus(user,Ptr)))
633                                 {
634                                         if (cstatus(src,Ptr) == STATUS_HOP)
635                                         {
636                                                 WriteServ(src->fd,"482 %s %s :You must be a channel operator",src->nick, Ptr->name);
637                                         }
638                                         else
639                                         {
640                                                 WriteServ(src->fd,"482 %s %s :You must be at least a half-operator",src->nick, Ptr->name);
641                                         }
642                 
643                                         return;
644                                 }
645                         }
646                 }
647         }
648
649         FOREACH_MOD(I_OnUserKick,OnUserKick(src,user,Ptr,reason));
650                         
651         for (UserChanList::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
652         {
653                 /* zap it from the channel list of the user */
654                 if ((*i)->channel && ((*i)->channel == Ptr))
655                 {
656                         WriteChannel(Ptr,src,"KICK %s %s :%s",Ptr->name, user->nick, reason);
657                         (*i)->uc_modes = 0;
658                         (*i)->channel = NULL;
659                         log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
660                         break;
661                 }
662         }
663
664         if (!Ptr->DelUser(user))
665         /* if there are no users left on the channel */
666         {
667                 chan_hash::iterator iter = chanlist.find(Ptr->name);
668
669                 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
670
671                 /* kill the record */
672                 if (iter != chanlist.end())
673                 {
674                         log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
675                         FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
676                         DELETE(Ptr);
677                         chanlist.erase(iter);
678                 }
679         }
680 }