]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/channels.cpp
More cleanup
[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 = binarymodes = 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 void 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;
147         }
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]->binarymodes = CM_TOPICLOCK | CM_NOEXTERNAL;
257                 chanlist[cname]->created = TIME;
258                 *chanlist[cname]->topic = 0;
259                 strlcpy(chanlist[cname]->setby, user->nick,NICKMAX-1);
260                 chanlist[cname]->topicset = 0;
261                 Ptr = chanlist[cname];
262                 log(DEBUG,"add_channel: created: %s",cname);
263                 /*
264                  * set created to 2 to indicate user
265                  * is the first in the channel
266                  * and should be given ops
267                  */
268                 created = 2;
269         }
270         else
271         {
272                 /* Already on the channel */
273                 if (Ptr->HasUser(user))
274                         return NULL;
275
276                 /*
277                  * remote users are allowed us to bypass channel modes
278                  * and bans (used by servers)
279                  */
280                 if (IS_LOCAL(user)) /* was a check on fd > -1 */
281                 {
282                         MOD_RESULT = 0;
283                         FOREACH_RESULT(I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname));
284                         if (MOD_RESULT == 1)
285                         {
286                                 return NULL;
287                         }
288                         else if (MOD_RESULT == 0)
289                         {
290                                 if (*Ptr->key)
291                                 {
292                                         MOD_RESULT = 0;
293                                         FOREACH_RESULT(I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
294                                         if (!MOD_RESULT)
295                                         {
296                                                 if (!key)
297                                                 {
298                                                         log(DEBUG,"add_channel: no key given in JOIN");
299                                                         WriteServ(user->fd,"475 %s %s :Cannot join channel (Requires key)",user->nick, Ptr->name);
300                                                         return NULL;
301                                                 }
302                                                 else
303                                                 {
304                                                         if (strcmp(key,Ptr->key))
305                                                         {
306                                                                 log(DEBUG,"add_channel: bad key given in JOIN");
307                                                                 WriteServ(user->fd,"475 %s %s :Cannot join channel (Incorrect key)",user->nick, Ptr->name);
308                                                                 return NULL;
309                                                         }
310                                                 }
311                                         }
312                                 }
313                                 if (Ptr->binarymodes & CM_INVITEONLY)
314                                 {
315                                         MOD_RESULT = 0;
316                                         irc::string xname(Ptr->name);
317                                         FOREACH_RESULT(I_OnCheckInvite,OnCheckInvite(user, Ptr));
318                                         if (!MOD_RESULT)
319                                         {
320                                                 log(DEBUG,"add_channel: channel is +i");
321                                                 if (user->IsInvited(xname))
322                                                 {
323                                                         /* user was invited to channel */
324                                                         /* there may be an optional channel NOTICE here */
325                                                 }
326                                                 else
327                                                 {
328                                                         WriteServ(user->fd,"473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
329                                                         return NULL;
330                                                 }
331                                         }
332                                         user->RemoveInvite(xname);
333                                 }
334                                 if (Ptr->limit)
335                                 {
336                                         MOD_RESULT = 0;
337                                         FOREACH_RESULT(I_OnCheckLimit,OnCheckLimit(user, Ptr));
338                                         if (!MOD_RESULT)
339                                         {
340                                                 if (usercount(Ptr) >= Ptr->limit)
341                                                 {
342                                                         WriteServ(user->fd,"471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
343                                                         return NULL;
344                                                 }
345                                         }
346                                 }
347                                 if (Ptr->bans.size())
348                                 {
349                                         log(DEBUG,"add_channel: about to walk banlist");
350                                         MOD_RESULT = 0;
351                                         FOREACH_RESULT(I_OnCheckBan,OnCheckBan(user, Ptr));
352                                         if (!MOD_RESULT)
353                                         {
354                                                 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
355                                                 {
356                                                         if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match((char*)inet_ntoa(user->ip4),i->data)))
357                                                         {
358                                                                 WriteServ(user->fd,"474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
359                                                                 return NULL;
360                                                         }
361                                                 }
362                                         }
363                                 }
364                         }
365                 }
366                 else
367                 {
368                         log(DEBUG,"Overridden checks");
369                 }
370                 created = 1;
371         }
372
373         log(DEBUG,"Passed channel checks");
374
375         for (unsigned int index =0; index < user->chans.size(); index++)
376         {
377                 if (user->chans[index].channel == NULL)
378                 {
379                         return ForceChan(Ptr,user->chans[index],user,created);
380                 }
381         }
382
383         /*
384          * XXX: If the user is an oper here, we can just extend their user->chans vector by one
385          * and put the channel in here. Same for remote users which are not bound by
386          * the channel limits. Otherwise, nope, youre boned.
387          */
388         if (!IS_LOCAL(user)) /* was a check on fd < 0 */
389         {
390                 ucrec a;
391                 chanrec* c = ForceChan(Ptr,a,user,created);
392                 user->chans.push_back(a);
393                 return c;
394         }
395         else if (*user->oper)
396         {
397                 /* Oper allows extension up to the OPERMAXCHANS value */
398                 if (user->chans.size() < OPERMAXCHANS)
399                 {
400                         ucrec a;
401                         chanrec* c = ForceChan(Ptr,a,user,created);
402                         user->chans.push_back(a);
403                         return c;
404                 }
405         }
406
407         log(DEBUG,"add_channel: user channel max exceeded: %s %s",user->nick,cname);
408         WriteServ(user->fd,"405 %s %s :You are on too many channels",user->nick, cname);
409
410         if (created == 2)
411         {
412                 log(DEBUG,"BLAMMO, Whacking channel.");
413                 /* Things went seriously pear shaped, so take this away. bwahaha. */
414                 chan_hash::iterator n = chanlist.find(cname);
415                 if (n != chanlist.end())
416                 {
417                         Ptr->DelUser(user);
418                         delete Ptr;
419                         chanlist.erase(n);
420                         for (unsigned int index =0; index < user->chans.size(); index++)
421                         {
422                                 if (user->chans[index].channel == Ptr)
423                                 {
424                                         user->chans[index].channel = NULL;
425                                         user->chans[index].uc_modes = 0;        
426                                 }
427                         }
428                 }
429         }
430         return NULL;
431 }
432
433 chanrec* ForceChan(chanrec* Ptr,ucrec &a,userrec* user, int created)
434 {
435         if (created == 2)
436         {
437                 /* first user in is given ops */
438                 a.uc_modes = UCMODE_OP;
439                 Ptr->AddOppedUser(user);
440         }
441         else
442         {
443                 a.uc_modes = 0;
444         }
445
446         a.channel = Ptr;
447         Ptr->AddUser(user);
448         WriteChannel(Ptr,user,"JOIN :%s",Ptr->name);
449         log(DEBUG,"Sent JOIN to client");
450
451         if (Ptr->topicset)
452         {
453                 WriteServ(user->fd,"332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
454                 WriteServ(user->fd,"333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
455         }
456
457         userlist(user,Ptr);
458         WriteServ(user->fd,"366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
459         FOREACH_MOD(I_OnUserJoin,OnUserJoin(user,Ptr));
460         return Ptr;
461 }
462
463 /*
464  *remove a channel from a users record, and remove the record from memory
465  * if the channel has become empty
466  */
467
468 chanrec* del_channel(userrec *user, const char* cname, const char* reason, bool local)
469 {
470         if ((!user) || (!cname))
471         {
472                 log(DEFAULT,"*** BUG *** del_channel was given an invalid parameter");
473                 return NULL;
474         }
475
476         chanrec* Ptr = FindChan(cname);
477
478         if (!Ptr)
479                 return NULL;
480
481         log(DEBUG,"del_channel: removing: %s %s",user->nick,Ptr->name);
482
483         for (unsigned int i =0; i < user->chans.size(); i++)
484         {
485                 /* zap it from the channel list of the user */
486                 if (user->chans[i].channel == Ptr)
487                 {
488                         if (reason)
489                         {
490                                 FOREACH_MOD(I_OnUserPart,OnUserPart(user,Ptr,reason));
491                                 WriteChannel(Ptr,user,"PART %s :%s",Ptr->name, reason);
492                         }
493                         else
494                         {
495                                 FOREACH_MOD(I_OnUserPart,OnUserPart(user,Ptr,""));
496                                 WriteChannel(Ptr,user,"PART :%s",Ptr->name);
497                         }
498                         user->chans[i].uc_modes = 0;
499                         user->chans[i].channel = NULL;
500                         log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
501                         break;
502                 }
503         }
504
505         Ptr->DelUser(user);
506
507         /* if there are no users left on the channel */
508         if (!usercount(Ptr))
509         {
510                 chan_hash::iterator iter = chanlist.find(Ptr->name);
511
512                 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
513
514                 /* kill the record */
515                 if (iter != chanlist.end())
516                 {
517                         log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
518                         FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
519                         delete Ptr;
520                         chanlist.erase(iter);
521                 }
522         }
523
524         return NULL;
525 }
526
527 void server_kick_channel(userrec* user, chanrec* Ptr, char* reason, bool triggerevents)
528 {
529         if ((!user) || (!Ptr) || (!reason))
530         {
531                 return;
532         }
533
534         if (IS_LOCAL(user))
535         {
536                 if (!Ptr->HasUser(user))
537                 {
538                         /* Not on channel */
539                         return;
540                 }
541         }
542         
543         if (triggerevents)
544         {
545                 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL,user,Ptr,reason));
546         }
547
548         for (unsigned int i =0; i < user->chans.size(); i++)
549         {
550                 if (user->chans[i].channel)
551                 if (!strcasecmp(user->chans[i].channel->name,Ptr->name))
552                 {
553                         WriteChannelWithServ(Config->ServerName,Ptr,"KICK %s %s :%s",Ptr->name, user->nick, reason);
554                         user->chans[i].uc_modes = 0;
555                         user->chans[i].channel = NULL;
556                         break;
557                 }
558         }
559
560         Ptr->DelUser(user);
561
562         if (!usercount(Ptr))
563         {
564                 chan_hash::iterator iter = chanlist.find(Ptr->name);
565                 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
566                 /* kill the record */
567                 if (iter != chanlist.end())
568                 {
569                         log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
570                         FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
571                         delete Ptr;
572                         chanlist.erase(iter);
573                 }
574         }
575 }
576
577 void kick_channel(userrec *src,userrec *user, chanrec *Ptr, char* reason)
578 {
579         if ((!src) || (!user) || (!Ptr) || (!reason))
580         {
581                 log(DEFAULT,"*** BUG *** kick_channel was given an invalid parameter");
582                 return;
583         }
584
585         log(DEBUG,"kick_channel: removing: %s %s %s",user->nick,Ptr->name,src->nick);
586
587         if (IS_LOCAL(src))
588         {
589                 if (!Ptr->HasUser(user))
590                 {
591                         WriteServ(src->fd,"441 %s %s %s :They are not on that channel",src->nick, user->nick, Ptr->name);
592                         return;
593                 }
594                 int MOD_RESULT = 0;
595
596                 if (!is_uline(src->server))
597                 {
598                         MOD_RESULT = 0;
599                         FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,Ptr,reason));
600                         if (MOD_RESULT == 1)
601                                 return;
602                 }
603                 /* Set to -1 by OnUserPreKick if explicit allow was set */
604                 if (MOD_RESULT != -1)
605                 {
606                         FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,Ptr,AC_KICK));
607                         if ((MOD_RESULT == ACR_DENY) && (!is_uline(src->server)))
608                                 return;
609         
610                         if ((MOD_RESULT == ACR_DEFAULT) || (!is_uline(src->server)))
611                         {
612                                 if ((cstatus(src,Ptr) < STATUS_HOP) || (cstatus(src,Ptr) < cstatus(user,Ptr)))
613                                 {
614                                         if (cstatus(src,Ptr) == STATUS_HOP)
615                                         {
616                                                 WriteServ(src->fd,"482 %s %s :You must be a channel operator",src->nick, Ptr->name);
617                                         }
618                                         else
619                                         {
620                                                 WriteServ(src->fd,"482 %s %s :You must be at least a half-operator",src->nick, Ptr->name);
621                                         }
622                 
623                                         return;
624                                 }
625                         }
626                 }
627         }
628
629         FOREACH_MOD(I_OnUserKick,OnUserKick(src,user,Ptr,reason));
630
631         for (unsigned int i =0; i < user->chans.size(); i++)
632         {
633                 /* zap it from the channel list of the user */
634                 if (user->chans[i].channel)
635                 {
636                         if (!strcasecmp(user->chans[i].channel->name,Ptr->name))
637                         {
638                                 WriteChannel(Ptr,src,"KICK %s %s :%s",Ptr->name, user->nick, reason);
639                                 user->chans[i].uc_modes = 0;
640                                 user->chans[i].channel = NULL;
641                                 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
642                                 break;
643                         }
644                 }
645         }
646
647         Ptr->DelUser(user);
648
649         /* if there are no users left on the channel */
650         if (!usercount(Ptr))
651         {
652                 chan_hash::iterator iter = chanlist.find(Ptr->name);
653
654                 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
655
656                 /* kill the record */
657                 if (iter != chanlist.end())
658                 {
659                         log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
660                         FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(Ptr));
661                         delete Ptr;
662                         chanlist.erase(iter);
663                 }
664         }
665 }
666
667