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