]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/channels.cpp
Move tons more stuff into class InspIRCd
[user/henk/code/inspircd.git] / src / channels.cpp
1 /*   +------------------------------------+
2  *   | Inspire Internet Relay Chat Daemon |
3  *   +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *   E-mail:
7  *        <brain@chatspike.net>
8  *        <Craig@chatspike.net>
9  * 
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *      the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include <string>
20 #include <map>
21 #include <sstream>
22 #include <vector>
23 #include <deque>
24 #include <stdarg.h>
25 #include "configreader.h"
26 #include "inspircd.h"
27 #include "hash_map.h"
28 #include "users.h"
29 #include "ctables.h"
30 #include "globals.h"
31 #include "modules.h"
32 #include "dynamic.h"
33 #include "commands.h"
34 #include "wildcard.h"
35 #include "message.h"
36 #include "mode.h"
37 #include "xline.h"
38 #include "inspstring.h"
39 #include "helperfuncs.h"
40 #include "typedefs.h"
41
42 extern InspIRCd* ServerInstance;
43
44 extern int MODCOUNT;
45 extern std::vector<Module*> modules;
46 extern std::vector<ircd_module*> factory;
47 extern time_t TIME;
48
49 chanrec::chanrec()
50 {
51         *name = *topic = *setby = *key = 0;
52         created = topicset = limit = 0;
53         internal_userlist.clear();
54         memset(&modes,0,64);
55 }
56
57 void chanrec::SetMode(char mode,bool mode_on)
58 {
59         modes[mode-65] = mode_on;
60         if (!mode_on)
61                 this->SetModeParam(mode,"",false);
62 }
63
64
65 void chanrec::SetModeParam(char mode,const char* parameter,bool mode_on)
66 {
67         log(DEBUG,"SetModeParam called");
68         
69         CustomModeList::iterator n = custom_mode_params.find(mode);     
70
71         if (mode_on)
72         {
73                 if (n == custom_mode_params.end())
74                 {
75                         custom_mode_params[mode] = strdup(parameter);
76                         log(DEBUG,"Custom mode parameter %c %s added",mode,parameter);
77                 }
78                 else
79                 {
80                         log(DEBUG, "Tried to set custom mode parameter for %c '%s' when it was already '%s'", mode, parameter, n->second);
81                 }
82         }
83         else
84         {
85                 if (n != custom_mode_params.end())
86                 {
87                         free(n->second);
88                         custom_mode_params.erase(n);
89                 }
90         }
91 }
92
93 bool chanrec::IsModeSet(char mode)
94 {
95         return modes[mode-65];
96 }
97
98 std::string chanrec::GetModeParameter(char mode)
99 {
100         if (mode == 'k')
101         {
102                 return this->key;
103         }
104         else if (mode == 'l')
105         {
106                 return ConvToStr(this->limit);
107         }
108         else
109         {
110                 CustomModeList::iterator n = custom_mode_params.find(mode);
111                 if (n != custom_mode_params.end())
112                 {
113                         return n->second;
114                 }
115                 return "";
116         }
117 }
118
119 long chanrec::GetUserCounter()
120 {
121         return (this->internal_userlist.size());
122 }
123
124 void chanrec::AddUser(userrec* user)
125 {
126         internal_userlist[user] = user;
127 }
128
129 unsigned long chanrec::DelUser(userrec* user)
130 {
131         CUListIter a = internal_userlist.find(user);
132         
133         if (a != internal_userlist.end())
134         {
135                 internal_userlist.erase(a);
136                 /* And tidy any others... */
137                 DelOppedUser(user);
138                 DelHalfoppedUser(user);
139                 DelVoicedUser(user);
140         }
141         
142         return internal_userlist.size();
143 }
144
145 bool chanrec::HasUser(userrec* user)
146 {
147         return (internal_userlist.find(user) != internal_userlist.end());
148 }
149
150 void chanrec::AddOppedUser(userrec* user)
151 {
152         internal_op_userlist[user] = user;
153 }
154
155 void chanrec::DelOppedUser(userrec* user)
156 {
157         CUListIter a = internal_op_userlist.find(user);
158         if (a != internal_op_userlist.end())
159         {
160                 internal_op_userlist.erase(a);
161                 return;
162         }
163 }
164
165 void chanrec::AddHalfoppedUser(userrec* user)
166 {
167         internal_halfop_userlist[user] = user;
168 }
169
170 void chanrec::DelHalfoppedUser(userrec* user)
171 {
172         CUListIter a = internal_halfop_userlist.find(user);
173
174         if (a != internal_halfop_userlist.end())
175         {   
176                 internal_halfop_userlist.erase(a);
177         }
178 }
179
180 void chanrec::AddVoicedUser(userrec* user)
181 {
182         internal_voice_userlist[user] = user;
183 }
184
185 void chanrec::DelVoicedUser(userrec* user)
186 {
187         CUListIter a = internal_voice_userlist.find(user);
188         
189         if (a != internal_voice_userlist.end())
190         {
191                 internal_voice_userlist.erase(a);
192         }
193 }
194
195 CUList* chanrec::GetUsers()
196 {
197         return &internal_userlist;
198 }
199
200 CUList* chanrec::GetOppedUsers()
201 {
202         return &internal_op_userlist;
203 }
204
205 CUList* chanrec::GetHalfoppedUsers()
206 {
207         return &internal_halfop_userlist;
208 }
209
210 CUList* chanrec::GetVoicedUsers()
211 {
212         return &internal_voice_userlist;
213 }
214
215 /* 
216  * add a channel to a user, creating the record for it if needed and linking
217  * it to the user record 
218  */
219 chanrec* chanrec::JoinUser(userrec *user, const char* cn, bool override, const char* key)
220 {
221         if (!user || !cn)
222                 return NULL;
223
224         int created = 0;
225         char cname[MAXBUF];
226         int MOD_RESULT = 0;
227         strlcpy(cname,cn,CHANMAX);
228
229         chanrec* Ptr = FindChan(cname);
230
231         if (!Ptr)
232         {
233                 if (user->fd > -1)
234                 {
235                         MOD_RESULT = 0;
236                         FOREACH_RESULT(I_OnUserPreJoin,OnUserPreJoin(user,NULL,cname));
237                         if (MOD_RESULT == 1)
238                                 return NULL;
239                 }
240
241                 /* create a new one */
242                 Ptr = new chanrec();
243                 ServerInstance->chanlist[cname] = Ptr;
244
245                 strlcpy(Ptr->name, cname,CHANMAX);
246                 Ptr->modes[CM_TOPICLOCK] = Ptr->modes[CM_NOEXTERNAL] = 1;
247                 Ptr->created = TIME;
248                 *Ptr->topic = 0;
249                 strlcpy(Ptr->setby, user->nick,NICKMAX-1);
250                 Ptr->topicset = 0;
251                 log(DEBUG,"chanrec::JoinUser(): created: %s",cname);
252                 /*
253                  * set created to 2 to indicate user
254                  * is the first in the channel
255                  * and should be given ops
256                  */
257                 created = 2;
258         }
259         else
260         {
261                 /* Already on the channel */
262                 if (Ptr->HasUser(user))
263                         return NULL;
264
265                 /*
266                  * remote users are allowed us to bypass channel modes
267                  * and bans (used by servers)
268                  */
269                 if (IS_LOCAL(user)) /* was a check on fd > -1 */
270                 {
271                         MOD_RESULT = 0;
272                         FOREACH_RESULT(I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname));
273                         if (MOD_RESULT == 1)
274                         {
275                                 return NULL;
276                         }
277                         else if (MOD_RESULT == 0)
278                         {
279                                 if (*Ptr->key)
280                                 {
281                                         MOD_RESULT = 0;
282                                         FOREACH_RESULT(I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
283                                         if (!MOD_RESULT)
284                                         {
285                                                 if (!key)
286                                                 {
287                                                         log(DEBUG,"chanrec::JoinUser(): no key given in JOIN");
288                                                         user->WriteServ("475 %s %s :Cannot join channel (Requires key)",user->nick, Ptr->name);
289                                                         return NULL;
290                                                 }
291                                                 else
292                                                 {
293                                                         if (strcmp(key,Ptr->key))
294                                                         {
295                                                                 log(DEBUG,"chanrec::JoinUser(): bad key given in JOIN");
296                                                                 user->WriteServ("475 %s %s :Cannot join channel (Incorrect key)",user->nick, Ptr->name);
297                                                                 return NULL;
298                                                         }
299                                                 }
300                                         }
301                                 }
302                                 if (Ptr->modes[CM_INVITEONLY])
303                                 {
304                                         MOD_RESULT = 0;
305                                         irc::string xname(Ptr->name);
306                                         FOREACH_RESULT(I_OnCheckInvite,OnCheckInvite(user, Ptr));
307                                         if (!MOD_RESULT)
308                                         {
309                                                 if (user->IsInvited(xname))
310                                                 {
311                                                         /* user was invited to channel */
312                                                         /* there may be an optional channel NOTICE here */
313                                                 }
314                                                 else
315                                                 {
316                                                         user->WriteServ("473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
317                                                         return NULL;
318                                                 }
319                                         }
320                                         user->RemoveInvite(xname);
321                                 }
322                                 if (Ptr->limit)
323                                 {
324                                         MOD_RESULT = 0;
325                                         FOREACH_RESULT(I_OnCheckLimit,OnCheckLimit(user, Ptr));
326                                         if (!MOD_RESULT)
327                                         {
328                                                 if (Ptr->GetUserCounter() >= Ptr->limit)
329                                                 {
330                                                         user->WriteServ("471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
331                                                         return NULL;
332                                                 }
333                                         }
334                                 }
335                                 if (Ptr->bans.size())
336                                 {
337                                         MOD_RESULT = 0;
338                                         FOREACH_RESULT(I_OnCheckBan,OnCheckBan(user, Ptr));
339                                         char mask[MAXBUF];
340                                         sprintf(mask,"%s!%s@%s",user->nick, user->ident, user->GetIPString());
341                                         if (!MOD_RESULT)
342                                         {
343                                                 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
344                                                 {
345                                                         /* This allows CIDR ban matching
346                                                          * 
347                                                          *        Full masked host                      Full unmasked host                   IP with/without CIDR
348                                                          */
349                                                         if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match(mask, i->data, true)))
350                                                         {
351                                                                 user->WriteServ("474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
352                                                                 return NULL;
353                                                         }
354                                                 }
355                                         }
356                                 }
357                         }
358                 }
359                 else
360                 {
361                         log(DEBUG,"chanrec::JoinUser(): Overridden checks");
362                 }
363                 created = 1;
364         }
365
366         for (UserChanList::const_iterator index = user->chans.begin(); index != user->chans.end(); index++)
367         {
368                 if ((*index)->channel == NULL)
369                 {
370                         return chanrec::ForceChan(Ptr, *index, user, created);
371                 }
372         }
373
374         /*
375          * XXX: If the user is an oper here, we can just extend their user->chans vector by one
376          * and put the channel in here. Same for remote users which are not bound by
377          * the channel limits. Otherwise, nope, youre boned.
378          */
379         if (!IS_LOCAL(user)) /* was a check on fd < 0 */
380         {
381                 ucrec* a = new ucrec();
382                 chanrec* c = chanrec::ForceChan(Ptr,a,user,created);
383                 user->chans.push_back(a);
384                 return c;
385         }
386         else if (*user->oper)
387         {
388                 /* Oper allows extension up to the OPERMAXCHANS value */
389                 if (user->chans.size() < OPERMAXCHANS)
390                 {
391                         ucrec* a = new ucrec();
392                         chanrec* c = chanrec::ForceChan(Ptr,a,user,created);
393                         user->chans.push_back(a);
394                         return c;
395                 }
396         }
397
398         user->WriteServ("405 %s %s :You are on too many channels",user->nick, cname);
399
400         if (created == 2)
401         {
402                 log(DEBUG,"BLAMMO, Whacking channel.");
403                 /* Things went seriously pear shaped, so take this away. bwahaha. */
404                 chan_hash::iterator n = ServerInstance->chanlist.find(cname);
405                 if (n != ServerInstance->chanlist.end())
406                 {
407                         Ptr->DelUser(user);
408                         DELETE(Ptr);
409                         ServerInstance->chanlist.erase(n);
410                         for (unsigned int index =0; index < user->chans.size(); index++)
411                         {
412                                 if (user->chans[index]->channel == Ptr)
413                                 {
414                                         user->chans[index]->channel = NULL;
415                                         user->chans[index]->uc_modes = 0;       
416                                 }
417                         }
418                 }
419         }
420         else
421         {
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         return NULL;
432 }
433
434 chanrec* chanrec::ForceChan(chanrec* Ptr,ucrec *a,userrec* user, int created)
435 {
436         if (created == 2)
437         {
438                 /* first user in is given ops */
439                 a->uc_modes = UCMODE_OP;
440                 Ptr->AddOppedUser(user);
441         }
442         else
443         {
444                 a->uc_modes = 0;
445         }
446
447         a->channel = Ptr;
448         Ptr->AddUser(user);
449         Ptr->WriteChannel(user,"JOIN :%s",Ptr->name);
450
451         /* Major improvement by Brain - we dont need to be calculating all this pointlessly for remote users */
452         if (IS_LOCAL(user))
453         {
454                 log(DEBUG,"Sent JOIN to client");
455                 if (Ptr->topicset)
456                 {
457                         user->WriteServ("332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
458                         user->WriteServ("333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
459                 }
460                 userlist(user,Ptr);
461                 user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
462         }
463         FOREACH_MOD(I_OnUserJoin,OnUserJoin(user,Ptr));
464         return Ptr;
465 }
466
467 /* chanrec::PartUser
468  * remove a channel from a users record, and remove the record from the hash
469  * if the channel has become empty
470  */
471 long chanrec::PartUser(userrec *user, const char* reason)
472 {
473         if (!user)
474                 return this->GetUserCounter();
475
476         for (unsigned int i =0; i < user->chans.size(); i++)
477         {
478                 /* zap it from the channel list of the user */
479                 if (user->chans[i]->channel == this)
480                 {
481                         if (reason)
482                         {
483                                 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, reason));
484                                 this->WriteChannel(user, "PART %s :%s", this->name, reason);
485                         }
486                         else
487                         {
488                                 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, ""));
489                                 this->WriteChannel(user, "PART :%s", this->name);
490                         }
491                         user->chans[i]->uc_modes = 0;
492                         user->chans[i]->channel = NULL;
493                         break;
494                 }
495         }
496
497         if (!this->DelUser(user)) /* if there are no users left on the channel... */
498         {
499                 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
500                 /* kill the record */
501                 if (iter != ServerInstance->chanlist.end())
502                 {
503                         log(DEBUG,"del_channel: destroyed: %s", this->name);
504                         FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
505                         ServerInstance->chanlist.erase(iter);
506                 }
507                 return 0;
508         }
509
510         return this->GetUserCounter();
511 }
512
513 long chanrec::ServerKickUser(userrec* user, const char* reason, bool triggerevents)
514 {
515         if (!user || !reason)
516                 return this->GetUserCounter();
517
518         if (IS_LOCAL(user))
519         {
520                 if (!this->HasUser(user))
521                 {
522                         /* Not on channel */
523                         return this->GetUserCounter();
524                 }
525         }
526
527         if (triggerevents)
528         {
529                 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL,user,this,reason));
530         }
531
532         for (unsigned int i =0; i < user->chans.size(); i++)
533         {
534                 if (user->chans[i]->channel == this)
535                 {
536                         this->WriteChannelWithServ(ServerInstance->Config->ServerName, "KICK %s %s :%s", this->name, user->nick, reason);
537                         user->chans[i]->uc_modes = 0;
538                         user->chans[i]->channel = NULL;
539                         break;
540                 }
541         }
542
543         if (!this->DelUser(user))
544         {
545                 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
546                 /* kill the record */
547                 if (iter != ServerInstance->chanlist.end())
548                 {
549                         FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
550                         ServerInstance->chanlist.erase(iter);
551                 }
552                 return 0;
553         }
554
555         return this->GetUserCounter();
556 }
557
558 long chanrec::KickUser(userrec *src, userrec *user, const char* reason)
559 {
560         if (!src || !user || !reason)
561                 return this->GetUserCounter();
562
563         if (IS_LOCAL(src))
564         {
565                 if (!this->HasUser(user))
566                 {
567                         src->WriteServ("441 %s %s %s :They are not on that channel",src->nick, user->nick, this->name);
568                         return this->GetUserCounter();
569                 }
570                 if ((is_uline(user->server)) && (!is_uline(src->server)))
571                 {
572                         src->WriteServ("482 %s %s :Only a u-line may kick a u-line from a channel.",src->nick, this->name);
573                         return this->GetUserCounter();
574                 }
575                 int MOD_RESULT = 0;
576
577                 if (!is_uline(src->server))
578                 {
579                         MOD_RESULT = 0;
580                         FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,this,reason));
581                         if (MOD_RESULT == 1)
582                                 return this->GetUserCounter();
583                 }
584                 /* Set to -1 by OnUserPreKick if explicit allow was set */
585                 if (MOD_RESULT != -1)
586                 {
587                         FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,this,AC_KICK));
588                         if ((MOD_RESULT == ACR_DENY) && (!is_uline(src->server)))
589                                 return this->GetUserCounter();
590         
591                         if ((MOD_RESULT == ACR_DEFAULT) || (!is_uline(src->server)))
592                         {
593                                 int them = cstatus(src, this);
594                                 int us = cstatus(user, this);
595                                 if ((them < STATUS_HOP) || (them < us))
596                                 {
597                                         if (them == STATUS_HOP)
598                                         {
599                                                 src->WriteServ("482 %s %s :You must be a channel operator",src->nick, this->name);
600                                         }
601                                         else
602                                         {
603                                                 src->WriteServ("482 %s %s :You must be at least a half-operator",src->nick, this->name);
604                                         }
605                                         return this->GetUserCounter();
606                                 }
607                         }
608                 }
609         }
610
611         FOREACH_MOD(I_OnUserKick,OnUserKick(src,user,this,reason));
612                         
613         for (UserChanList::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
614         {
615                 /* zap it from the channel list of the user */
616                 if ((*i)->channel == this)
617                 {
618                         this->WriteChannel(src, "KICK %s %s :%s", this->name, user->nick, reason);
619                         (*i)->uc_modes = 0;
620                         (*i)->channel = NULL;
621                         break;
622                 }
623         }
624
625         if (!this->DelUser(user))
626         /* if there are no users left on the channel */
627         {
628                 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
629
630                 /* kill the record */
631                 if (iter != ServerInstance->chanlist.end())
632                 {
633                         FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
634                         ServerInstance->chanlist.erase(iter);
635                 }
636                 return 0;
637         }
638
639         return this->GetUserCounter();
640 }
641
642 void chanrec::WriteChannel(userrec* user, char* text, ...)
643 {
644         char textbuffer[MAXBUF];
645         va_list argsPtr;
646
647         if (!user || !text)
648                 return;
649
650         va_start(argsPtr, text);
651         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
652         va_end(argsPtr);
653
654         this->WriteChannel(user, std::string(textbuffer));
655 }
656
657 void chanrec::WriteChannel(userrec* user, const std::string &text)
658 {
659         CUList *ulist = this->GetUsers();
660
661         if (!user)
662                 return;
663
664         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
665         {
666                 if (i->second->fd != FD_MAGIC_NUMBER)
667                         user->WriteTo(i->second,text);
668         }
669 }
670
671 void chanrec::WriteChannelWithServ(const char* ServName, const char* text, ...)
672 {
673         char textbuffer[MAXBUF];
674         va_list argsPtr;
675
676         if (!text)
677                 return;
678
679         va_start(argsPtr, text);
680         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
681         va_end(argsPtr);
682
683         this->WriteChannelWithServ(ServName, std::string(textbuffer));
684 }
685
686 void chanrec::WriteChannelWithServ(const char* ServName, const std::string &text)
687 {
688         CUList *ulist = this->GetUsers();
689
690         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
691         {
692                 if (IS_LOCAL(i->second))
693                         i->second->WriteServ(text);
694         }
695 }
696
697 /* write formatted text from a source user to all users on a channel except
698  * for the sender (for privmsg etc) */
699 void chanrec::WriteAllExceptSender(userrec* user, char status, char* text, ...)
700 {
701         char textbuffer[MAXBUF];
702         va_list argsPtr;
703
704         if (!user || !text)
705                 return;
706
707         va_start(argsPtr, text);
708         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
709         va_end(argsPtr);
710
711         this->WriteAllExceptSender(user, status, std::string(textbuffer));
712 }
713
714 void chanrec::WriteAllExceptSender(userrec* user, char status, const std::string& text)
715 {
716         CUList *ulist;
717
718         if (!user)
719                 return;
720
721         switch (status)
722         {
723                 case '@':
724                         ulist = this->GetOppedUsers();
725                         break;
726                 case '%':
727                         ulist = this->GetHalfoppedUsers();
728                         break;
729                 case '+':
730                         ulist = this->GetVoicedUsers();
731                         break;
732                 default:
733                         ulist = this->GetUsers();
734                         break;
735         }
736
737         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
738         {
739                 if ((IS_LOCAL(i->second)) && (user != i->second))
740                         i->second->WriteFrom(user,text);
741         }
742 }
743