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