]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/channels.cpp
Decide that it wasn't quite appropriate :(
[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-1);
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
444         if (created == 2)
445         {
446                 log(DEBUG,"BLAMMO, Whacking channel.");
447                 /* Things went seriously pear shaped, so take this away. bwahaha. */
448                 chan_hash::iterator n = chanlist.find(cname);
449                 if (n != chanlist.end())
450                 {
451                         Ptr->DelUser((char*)user);
452                         delete Ptr;
453                         chanlist.erase(n);
454                         for (unsigned int index =0; index < user->chans.size(); index++)
455                         {
456                                 if (user->chans[index].channel == Ptr)
457                                 {
458                                         user->chans[index].channel == NULL;
459                                         user->chans[index].uc_modes = 0;        
460                                 }
461                         }
462                 }
463         }
464         return NULL;
465 }
466
467 chanrec* ForceChan(chanrec* Ptr,ucrec &a,userrec* user, int created)
468 {
469         if (created == 2)
470         {
471                 /* first user in is given ops */
472                 a.uc_modes = UCMODE_OP;
473                 Ptr->AddOppedUser((char*)user);
474         }
475         else
476         {
477                 a.uc_modes = 0;
478         }
479
480         a.channel = Ptr;
481         Ptr->AddUser((char*)user);
482         WriteChannel(Ptr,user,"JOIN :%s",Ptr->name);
483         log(DEBUG,"Sent JOIN to client");
484
485         if (Ptr->topicset)
486         {
487                 WriteServ(user->fd,"332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
488                 WriteServ(user->fd,"333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
489         }
490
491         userlist(user,Ptr);
492         WriteServ(user->fd,"366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
493         FOREACH_MOD(I_OnUserJoin,OnUserJoin(user,Ptr));
494         return Ptr;
495 }
496
497 /*
498  *remove a channel from a users record, and remove the record from memory
499  * if the channel has become empty
500  */
501
502 chanrec* del_channel(userrec *user, const char* cname, const char* reason, bool local)
503 {
504         if ((!user) || (!cname))
505         {
506                 log(DEFAULT,"*** BUG *** del_channel was given an invalid parameter");
507                 return NULL;
508         }
509
510         chanrec* Ptr = FindChan(cname);
511
512         if (!Ptr)
513                 return NULL;
514
515         log(DEBUG,"del_channel: removing: %s %s",user->nick,Ptr->name);
516
517         for (unsigned int i =0; i < user->chans.size(); i++)
518         {
519                 /* zap it from the channel list of the user */
520                 if (user->chans[i].channel == Ptr)
521                 {
522                         if (reason)
523                         {
524                                 FOREACH_MOD(I_OnUserPart,OnUserPart(user,Ptr,reason));
525                                 WriteChannel(Ptr,user,"PART %s :%s",Ptr->name, reason);
526                         }
527                         else
528                         {
529                                 FOREACH_MOD(I_OnUserPart,OnUserPart(user,Ptr,""));
530                                 WriteChannel(Ptr,user,"PART :%s",Ptr->name);
531                         }
532                         user->chans[i].uc_modes = 0;
533                         user->chans[i].channel = NULL;
534                         log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
535                         break;
536                 }
537         }
538
539         Ptr->DelUser((char*)user);
540
541         /* if there are no users left on the channel */
542         if (!usercount(Ptr))
543         {
544                 chan_hash::iterator iter = chanlist.find(Ptr->name);
545
546                 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
547
548                 /* kill the record */
549                 if (iter != chanlist.end())
550                 {
551                         log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
552                         FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
553                         delete Ptr;
554                         chanlist.erase(iter);
555                 }
556         }
557
558         return NULL;
559 }
560
561 void server_kick_channel(userrec* user, chanrec* Ptr, char* reason, bool triggerevents)
562 {
563         if ((!user) || (!Ptr) || (!reason))
564         {
565                 return;
566         }
567
568         if (IS_LOCAL(user))
569         {
570                 if (!has_channel(user,Ptr))
571                 {
572                         /* Not on channel */
573                         return;
574                 }
575         }
576         
577         if (triggerevents)
578         {
579                 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL,user,Ptr,reason));
580         }
581
582         for (unsigned int i =0; i < user->chans.size(); i++)
583         {
584                 if (user->chans[i].channel)
585                 if (!strcasecmp(user->chans[i].channel->name,Ptr->name))
586                 {
587                         WriteChannelWithServ(Config->ServerName,Ptr,"KICK %s %s :%s",Ptr->name, user->nick, reason);
588                         user->chans[i].uc_modes = 0;
589                         user->chans[i].channel = NULL;
590                         break;
591                 }
592         }
593
594         Ptr->DelUser((char*)user);
595
596         if (!usercount(Ptr))
597         {
598                 chan_hash::iterator iter = chanlist.find(Ptr->name);
599                 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
600                 /* kill the record */
601                 if (iter != chanlist.end())
602                 {
603                         log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
604                         FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
605                         delete Ptr;
606                         chanlist.erase(iter);
607                 }
608         }
609 }
610
611 void kick_channel(userrec *src,userrec *user, chanrec *Ptr, char* reason)
612 {
613         if ((!src) || (!user) || (!Ptr) || (!reason))
614         {
615                 log(DEFAULT,"*** BUG *** kick_channel was given an invalid parameter");
616                 return;
617         }
618
619         log(DEBUG,"kick_channel: removing: %s %s %s",user->nick,Ptr->name,src->nick);
620
621         if (IS_LOCAL(src))
622         {
623                 if (!has_channel(user,Ptr))
624                 {
625                         WriteServ(src->fd,"441 %s %s %s :They are not on that channel",src->nick, user->nick, Ptr->name);
626                         return;
627                 }
628                 int MOD_RESULT = 0;
629
630                 if (!is_uline(src->server))
631                 {
632                         MOD_RESULT = 0;
633                         FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,Ptr,reason));
634                         if (MOD_RESULT == 1)
635                                 return;
636                 }
637                 /* Set to -1 by OnUserPreKick if explicit allow was set */
638                 if (MOD_RESULT != -1)
639                 {
640                         FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,Ptr,AC_KICK));
641                         if ((MOD_RESULT == ACR_DENY) && (!is_uline(src->server)))
642                                 return;
643         
644                         if ((MOD_RESULT == ACR_DEFAULT) || (!is_uline(src->server)))
645                         {
646                                 if ((cstatus(src,Ptr) < STATUS_HOP) || (cstatus(src,Ptr) < cstatus(user,Ptr)))
647                                 {
648                                         if (cstatus(src,Ptr) == STATUS_HOP)
649                                         {
650                                                 WriteServ(src->fd,"482 %s %s :You must be a channel operator",src->nick, Ptr->name);
651                                         }
652                                         else
653                                         {
654                                                 WriteServ(src->fd,"482 %s %s :You must be at least a half-operator",src->nick, Ptr->name);
655                                         }
656                 
657                                         return;
658                                 }
659                         }
660                 }
661         }
662
663         FOREACH_MOD(I_OnUserKick,OnUserKick(src,user,Ptr,reason));
664
665         for (unsigned int i =0; i < user->chans.size(); i++)
666         {
667                 /* zap it from the channel list of the user */
668                 if (user->chans[i].channel)
669                 {
670                         if (!strcasecmp(user->chans[i].channel->name,Ptr->name))
671                         {
672                                 WriteChannel(Ptr,src,"KICK %s %s :%s",Ptr->name, user->nick, reason);
673                                 user->chans[i].uc_modes = 0;
674                                 user->chans[i].channel = NULL;
675                                 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
676                                 break;
677                         }
678                 }
679         }
680
681         Ptr->DelUser((char*)user);
682
683         /* if there are no users left on the channel */
684         if (!usercount(Ptr))
685         {
686                 chan_hash::iterator iter = chanlist.find(Ptr->name);
687
688                 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
689
690                 /* kill the record */
691                 if (iter != chanlist.end())
692                 {
693                         log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
694                         FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
695                         delete Ptr;
696                         chanlist.erase(iter);
697                 }
698         }
699 }
700
701