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