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