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