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