]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/channels.cpp
Advanced to RC1
[user/henk/code/inspircd.git] / src / channels.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2005 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                 strncpy(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.push_back(castuser);
164         log(DEBUG,"Added casted user to channel's internal list");
165 }
166
167 void chanrec::DelUser(char* castuser)
168 {
169         for (std::vector<char*>::iterator a = internal_userlist.begin(); a < internal_userlist.end(); a++)
170         {
171                 if (*a == castuser)
172                 {
173                         log(DEBUG,"Removed casted user from channel's internal list");
174                         internal_userlist.erase(a);
175                         return;
176                 }
177         }
178         log(DEBUG,"BUG BUG BUG! Attempt to remove an uncasted user from the internal list of %s!",name);
179 }
180
181 std::vector<char*> *chanrec::GetUsers()
182 {
183         return &internal_userlist;
184 }
185
186 /* add a channel to a user, creating the record for it if needed and linking
187  * it to the user record */
188
189 chanrec* add_channel(userrec *user, const char* cn, const char* key, bool override)
190 {
191         if ((!user) || (!cn))
192         {
193                 log(DEFAULT,"*** BUG *** add_channel was given an invalid parameter");
194                 return 0;
195         }
196
197         int created = 0;
198         char cname[MAXBUF];
199         int MOD_RESULT = 0;
200         strncpy(cname,cn,CHANMAX);
201
202         log(DEBUG,"add_channel: %s %s",user->nick,cname);
203
204         chanrec* Ptr = FindChan(cname);
205
206         if (!Ptr)
207         {
208                 if (user->fd > -1)
209                 {
210                         MOD_RESULT = 0;
211                         FOREACH_RESULT(OnUserPreJoin(user,NULL,cname));
212                         if (MOD_RESULT == 1)
213                                 return NULL;
214                 }
215                 /* create a new one */
216                 chanlist[cname] = new chanrec();
217                 strlcpy(chanlist[cname]->name, cname,CHANMAX);
218                 chanlist[cname]->binarymodes = CM_TOPICLOCK | CM_NOEXTERNAL;
219                 chanlist[cname]->created = TIME;
220                 *chanlist[cname]->topic = 0;
221                 strncpy(chanlist[cname]->setby, user->nick,NICKMAX);
222                 chanlist[cname]->topicset = 0;
223                 Ptr = chanlist[cname];
224                 log(DEBUG,"add_channel: created: %s",cname);
225                 /* set created to 2 to indicate user
226                  * is the first in the channel
227                  * and should be given ops */
228                 created = 2;
229         }
230         else
231         {
232                 /* Already on the channel */
233                 if (has_channel(user,Ptr))
234                         return NULL;
235
236                 // remote users are allowed us to bypass channel modes
237                 // and bans (used by servers)
238                 if (user->fd > -1)
239                 {
240                         MOD_RESULT = 0;
241                         FOREACH_RESULT(OnUserPreJoin(user,Ptr,cname));
242                         if (MOD_RESULT == 1)
243                         {
244                                 return NULL;
245                         }
246                         else
247                         {
248                                 if (*Ptr->key)
249                                 {
250                                         MOD_RESULT = 0;
251                                         FOREACH_RESULT(OnCheckKey(user, Ptr, key ? key : ""));
252                                         if (!MOD_RESULT)
253                                         {
254                                                 if (!key)
255                                                 {
256                                                         log(DEBUG,"add_channel: no key given in JOIN");
257                                                         WriteServ(user->fd,"475 %s %s :Cannot join channel (Requires key)",user->nick, Ptr->name);
258                                                         return NULL;
259                                                 }
260                                                 else
261                                                 {
262                                                         if (strcasecmp(key,Ptr->key))
263                                                         {
264                                                                 log(DEBUG,"add_channel: bad key given in JOIN");
265                                                                 WriteServ(user->fd,"475 %s %s :Cannot join channel (Incorrect key)",user->nick, Ptr->name);
266                                                                 return NULL;
267                                                         }
268                                                 }
269                                         }
270                                 }
271                                 if (Ptr->binarymodes & CM_INVITEONLY)
272                                 {
273                                         MOD_RESULT = 0;
274                                         irc::string xname(Ptr->name);
275                                         FOREACH_RESULT(OnCheckInvite(user, Ptr));
276                                         if (!MOD_RESULT)
277                                         {
278                                                 log(DEBUG,"add_channel: channel is +i");
279                                                 if (user->IsInvited(xname))
280                                                 {
281                                                         /* user was invited to channel */
282                                                         /* there may be an optional channel NOTICE here */
283                                                 }
284                                                 else
285                                                 {
286                                                         WriteServ(user->fd,"473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
287                                                         return NULL;
288                                                 }
289                                         }
290                                         user->RemoveInvite(xname);
291                                 }
292                                 if (Ptr->limit)
293                                 {
294                                         MOD_RESULT = 0;
295                                         FOREACH_RESULT(OnCheckLimit(user, Ptr));
296                                         if (!MOD_RESULT)
297                                         {
298                                                 if (usercount(Ptr) >= Ptr->limit)
299                                                 {
300                                                         WriteServ(user->fd,"471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
301                                                         return NULL;
302                                                 }
303                                         }
304                                 }
305                                 if (Ptr->bans.size())
306                                 {
307                                         log(DEBUG,"add_channel: about to walk banlist");
308                                         MOD_RESULT = 0;
309                                         FOREACH_RESULT(OnCheckBan(user, Ptr));
310                                         if (!MOD_RESULT)
311                                         {
312                                                 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
313                                                 {
314                                                         if (match(user->GetFullHost(),i->data))
315                                                         {
316                                                                 WriteServ(user->fd,"474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
317                                                                 return NULL;
318                                                         }
319                                                 }
320                                         }
321                                 }
322                         }
323                 }
324                 else
325                 {
326                         log(DEBUG,"Overridden checks");
327                 }
328                 created = 1;
329         }
330
331         log(DEBUG,"Passed channel checks");
332
333         for (unsigned int index =0; index < user->chans.size(); index++)
334         {
335                 if (user->chans[index].channel == NULL)
336                 {
337                         return ForceChan(Ptr,user->chans[index],user,created);
338                 }
339         }
340         /* XXX: If the user is an oper here, we can just extend their user->chans vector by one
341          * and put the channel in here. Same for remote users which are not bound by
342          * the channel limits. Otherwise, nope, youre boned.
343          */
344         if (user->fd < 0)
345         {
346                 ucrec a;
347                 chanrec* c = ForceChan(Ptr,a,user,created);
348                 user->chans.push_back(a);
349                 return c;
350         }
351         else if (strchr(user->modes,'o'))
352         {
353                 /* Oper allows extension up to the OPERMAXCHANS value */
354                 if (user->chans.size() < OPERMAXCHANS)
355                 {
356                         ucrec a;
357                         chanrec* c = ForceChan(Ptr,a,user,created);
358                         user->chans.push_back(a);
359                         return c;
360                 }
361         }
362         log(DEBUG,"add_channel: user channel max exceeded: %s %s",user->nick,cname);
363         WriteServ(user->fd,"405 %s %s :You are on too many channels",user->nick, cname);
364         return NULL;
365 }
366
367 chanrec* ForceChan(chanrec* Ptr,ucrec &a,userrec* user, int created)
368 {
369         if (created == 2)
370         {
371                 /* first user in is given ops */
372                 a.uc_modes = UCMODE_OP;
373         }
374         else
375         {
376                 a.uc_modes = 0;
377         }
378         a.channel = Ptr;
379         Ptr->AddUser((char*)user);
380         WriteChannel(Ptr,user,"JOIN :%s",Ptr->name);
381         log(DEBUG,"Sent JOIN to client");
382         if (Ptr->topicset)
383         {
384                 WriteServ(user->fd,"332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
385                 WriteServ(user->fd,"333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
386         }
387         userlist(user,Ptr);
388         WriteServ(user->fd,"366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
389         FOREACH_MOD OnUserJoin(user,Ptr);
390         return Ptr;
391 }
392
393 /* remove a channel from a users record, and remove the record from memory
394  * if the channel has become empty */
395
396 chanrec* del_channel(userrec *user, const char* cname, const char* reason, bool local)
397 {
398         if ((!user) || (!cname))
399         {
400                 log(DEFAULT,"*** BUG *** del_channel was given an invalid parameter");
401                 return NULL;
402         }
403
404         chanrec* Ptr = FindChan(cname);
405
406         if (!Ptr)
407                 return NULL;
408
409         FOREACH_MOD OnUserPart(user,Ptr);
410         log(DEBUG,"del_channel: removing: %s %s",user->nick,Ptr->name);
411
412         for (unsigned int i =0; i < user->chans.size(); i++)
413         {
414                 /* zap it from the channel list of the user */
415                 if (user->chans[i].channel == Ptr)
416                 {
417                         if (reason)
418                         {
419                                 WriteChannel(Ptr,user,"PART %s :%s",Ptr->name, reason);
420                         }
421                         else
422                         {
423                                 WriteChannel(Ptr,user,"PART :%s",Ptr->name);
424                         }
425                         user->chans[i].uc_modes = 0;
426                         user->chans[i].channel = NULL;
427                         log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
428                         break;
429                 }
430         }
431
432         Ptr->DelUser((char*)user);
433
434         /* if there are no users left on the channel */
435         if (!usercount(Ptr))
436         {
437                 chan_hash::iterator iter = chanlist.find(Ptr->name);
438
439                 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
440
441                 /* kill the record */
442                 if (iter != chanlist.end())
443                 {
444                         log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
445                         delete Ptr;
446                         chanlist.erase(iter);
447                 }
448         }
449
450         return NULL;
451 }
452
453
454 void kick_channel(userrec *src,userrec *user, chanrec *Ptr, char* reason)
455 {
456         if ((!src) || (!user) || (!Ptr) || (!reason))
457         {
458                 log(DEFAULT,"*** BUG *** kick_channel was given an invalid parameter");
459                 return;
460         }
461
462         if ((!Ptr) || (!user) || (!src))
463         {
464                 return;
465         }
466
467         log(DEBUG,"kick_channel: removing: %s %s %s",user->nick,Ptr->name,src->nick);
468
469         if (!has_channel(user,Ptr))
470         {
471                 WriteServ(src->fd,"441 %s %s %s :They are not on that channel",src->nick, user->nick, Ptr->name);
472                 return;
473         }
474
475         int MOD_RESULT = 0;
476         FOREACH_RESULT(OnAccessCheck(src,user,Ptr,AC_KICK));
477         if ((MOD_RESULT == ACR_DENY) && (!is_uline(src->server)))
478                 return;
479
480         if ((MOD_RESULT == ACR_DEFAULT) || (!is_uline(src->server)))
481         {
482                 if ((cstatus(src,Ptr) < STATUS_HOP) || (cstatus(src,Ptr) < cstatus(user,Ptr)))
483                 {
484                         if (cstatus(src,Ptr) == STATUS_HOP)
485                         {
486                                 WriteServ(src->fd,"482 %s %s :You must be a channel operator",src->nick, Ptr->name);
487                         }
488                         else
489                         {
490                                 WriteServ(src->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",src->nick, Ptr->name);
491                         }
492
493                         return;
494                 }
495         }
496
497         if (!is_uline(src->server))
498         {
499                 MOD_RESULT = 0;
500                 FOREACH_RESULT(OnUserPreKick(src,user,Ptr,reason));
501                 if (MOD_RESULT)
502                         return;
503         }
504
505         FOREACH_MOD OnUserKick(src,user,Ptr,reason);
506
507         for (unsigned int i =0; i < user->chans.size(); i++)
508         {
509                 /* zap it from the channel list of the user */
510                 if (user->chans[i].channel)
511                 if (!strcasecmp(user->chans[i].channel->name,Ptr->name))
512                 {
513                         WriteChannel(Ptr,src,"KICK %s %s :%s",Ptr->name, user->nick, reason);
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((char*)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                         delete Ptr;
535                         chanlist.erase(iter);
536                 }
537         }
538 }
539
540