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