]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/channels.cpp
Craq removal at your service
[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 "inspircd_util.h"
23 #include <unistd.h>
24 #include <sys/errno.h>
25 #include <sys/ioctl.h>
26 #include <sys/utsname.h>
27 #include <time.h>
28 #include <string>
29 #ifdef GCC3
30 #include <ext/hash_map>
31 #else
32 #include <hash_map>
33 #endif
34 #include <map>
35 #include <sstream>
36 #include <vector>
37 #include <deque>
38 #include "users.h"
39 #include "ctables.h"
40 #include "globals.h"
41 #include "modules.h"
42 #include "dynamic.h"
43 #include "commands.h"
44 #include "wildcard.h"
45 #include "message.h"
46 #include "mode.h"
47 #include "xline.h"
48 #include "inspstring.h"
49 #include "helperfuncs.h"
50 #include "typedefs.h"
51
52 #ifdef GCC3
53 #define nspace __gnu_cxx
54 #else
55 #define nspace std
56 #endif
57
58 extern ServerConfig* Config;
59
60 extern int MODCOUNT;
61 extern std::vector<Module*> modules;
62 extern std::vector<ircd_module*> factory;
63 extern int WHOWAS_STALE;
64 extern int WHOWAS_MAX;
65 extern time_t startup_time;
66 extern std::vector<std::string> module_names;
67 extern int boundPortCount;
68 extern std::stringstream config_f;
69 extern time_t TIME;
70 extern chan_hash chanlist;
71
72 using namespace std;
73
74 std::vector<ModeParameter> custom_mode_params;
75
76 chanrec* ForceChan(chanrec* Ptr,ucrec &a,userrec* user, int created);
77
78 chanrec::chanrec()
79 {
80         strcpy(name,"");
81         strcpy(custom_modes,"");
82         strcpy(topic,"");
83         strcpy(setby,"");
84         strcpy(key,"");
85         created = topicset = limit = 0;
86         binarymodes = 0;
87         internal_userlist.clear();
88 }
89
90 void chanrec::SetCustomMode(char mode,bool mode_on)
91 {
92         if (mode_on) {
93                 static char m[3];
94                 m[0] = mode;
95                 m[1] = '\0';
96                 if (!strchr(this->custom_modes,mode))
97                 {
98                         strlcat(custom_modes,m,MAXMODES);
99                 }
100                 log(DEBUG,"Custom mode %c set",mode);
101         }
102         else {
103
104                 std::string a = this->custom_modes;
105                 int pos = a.find(mode);
106                 a.erase(pos,1);
107                 strncpy(this->custom_modes,a.c_str(),MAXMODES);
108
109                 log(DEBUG,"Custom mode %c removed: modelist='%s'",mode,this->custom_modes);
110                 this->SetCustomModeParam(mode,"",false);
111         }
112 }
113
114
115 void chanrec::SetCustomModeParam(char mode,char* parameter,bool mode_on)
116 {
117
118         log(DEBUG,"SetCustomModeParam called");
119         ModeParameter M;
120         M.mode = mode;
121         strlcpy(M.channel,this->name,CHANMAX);
122         strlcpy(M.parameter,parameter,MAXBUF);
123         if (mode_on)
124         {
125                 log(DEBUG,"Custom mode parameter %c %s added",mode,parameter);
126                 custom_mode_params.push_back(M);
127         }
128         else
129         {
130                 if (custom_mode_params.size())
131                 {
132                         for (vector<ModeParameter>::iterator i = custom_mode_params.begin(); i < custom_mode_params.end(); i++)
133                         {
134                                 if ((i->mode == mode) && (!strcasecmp(this->name,i->channel)))
135                                 {
136                                         log(DEBUG,"Custom mode parameter %c %s removed",mode,parameter);
137                                         custom_mode_params.erase(i);
138                                         return;
139                                 }
140                         }
141                 }
142                 log(DEBUG,"*** BUG *** Attempt to remove non-existent mode parameter!");
143         }
144 }
145
146 bool chanrec::IsCustomModeSet(char mode)
147 {
148         return (strchr(this->custom_modes,mode));
149 }
150
151 std::string chanrec::GetModeParameter(char mode)
152 {
153         if (custom_mode_params.size())
154         {
155                 for (vector<ModeParameter>::iterator i = custom_mode_params.begin(); i < custom_mode_params.end(); i++)
156                 {
157                         if ((i->mode == mode) && (!strcasecmp(this->name,i->channel)))
158                         {
159                                 return i->parameter;
160                         }
161                 }
162         }
163         return "";
164 }
165
166 long chanrec::GetUserCounter()
167 {
168         return (this->internal_userlist.size());
169 }
170
171 void chanrec::AddUser(char* castuser)
172 {
173         internal_userlist.push_back(castuser);
174         log(DEBUG,"Added casted user to channel's internal list");
175 }
176
177 void chanrec::DelUser(char* castuser)
178 {
179         for (std::vector<char*>::iterator a = internal_userlist.begin(); a < internal_userlist.end(); a++)
180         {
181                 if (*a == castuser)
182                 {
183                         log(DEBUG,"Removed casted user from channel's internal list");
184                         internal_userlist.erase(a);
185                         return;
186                 }
187         }
188         log(DEBUG,"BUG BUG BUG! Attempt to remove an uncasted user from the internal list of %s!",name);
189 }
190
191 std::vector<char*> *chanrec::GetUsers()
192 {
193         return &internal_userlist;
194 }
195
196 /* add a channel to a user, creating the record for it if needed and linking
197  * it to the user record */
198
199 chanrec* add_channel(userrec *user, const char* cn, const char* key, bool override)
200 {
201         if ((!user) || (!cn))
202         {
203                 log(DEFAULT,"*** BUG *** add_channel was given an invalid parameter");
204                 return 0;
205         }
206
207         int created = 0;
208         char cname[MAXBUF];
209         int MOD_RESULT = 0;
210         strncpy(cname,cn,CHANMAX);
211
212         log(DEBUG,"add_channel: %s %s",user->nick,cname);
213
214         chanrec* Ptr = FindChan(cname);
215
216         if (!Ptr)
217         {
218                 if (user->fd > -1)
219                 {
220                         MOD_RESULT = 0;
221                         FOREACH_RESULT(OnUserPreJoin(user,NULL,cname));
222                         if (MOD_RESULT == 1)
223                                 return NULL;
224                 }
225                 /* create a new one */
226                 chanlist[cname] = new chanrec();
227                 strlcpy(chanlist[cname]->name, cname,CHANMAX);
228                 chanlist[cname]->binarymodes = CM_TOPICLOCK | CM_NOEXTERNAL;
229                 chanlist[cname]->created = TIME;
230                 strcpy(chanlist[cname]->topic, "");
231                 strncpy(chanlist[cname]->setby, user->nick,NICKMAX);
232                 chanlist[cname]->topicset = 0;
233                 Ptr = chanlist[cname];
234                 log(DEBUG,"add_channel: created: %s",cname);
235                 /* set created to 2 to indicate user
236                  * is the first in the channel
237                  * and should be given ops */
238                 created = 2;
239         }
240         else
241         {
242                 /* Already on the channel */
243                 if (has_channel(user,Ptr))
244                         return NULL;
245
246                 // remote users are allowed us to bypass channel modes
247                 // and bans (used by servers)
248                 if (user->fd > -1)
249                 {
250                         MOD_RESULT = 0;
251                         FOREACH_RESULT(OnUserPreJoin(user,Ptr,cname));
252                         if (MOD_RESULT == 1)
253                         {
254                                 return NULL;
255                         }
256                         else
257                         {
258                                 if (*Ptr->key)
259                                 {
260                                         MOD_RESULT = 0;
261                                         FOREACH_RESULT(OnCheckKey(user, Ptr, key ? key : ""));
262                                         if (!MOD_RESULT)
263                                         {
264                                                 if (!key)
265                                                 {
266                                                         log(DEBUG,"add_channel: no key given in JOIN");
267                                                         WriteServ(user->fd,"475 %s %s :Cannot join channel (Requires key)",user->nick, Ptr->name);
268                                                         return NULL;
269                                                 }
270                                                 else
271                                                 {
272                                                         if (strcasecmp(key,Ptr->key))
273                                                         {
274                                                                 log(DEBUG,"add_channel: bad key given in JOIN");
275                                                                 WriteServ(user->fd,"475 %s %s :Cannot join channel (Incorrect key)",user->nick, Ptr->name);
276                                                                 return NULL;
277                                                         }
278                                                 }
279                                         }
280                                 }
281                                 if (Ptr->binarymodes & CM_INVITEONLY)
282                                 {
283                                         MOD_RESULT = 0;
284                                         FOREACH_RESULT(OnCheckInvite(user, Ptr));
285                                         if (!MOD_RESULT)
286                                         {
287                                                 log(DEBUG,"add_channel: channel is +i");
288                                                 if (user->IsInvited(Ptr->name))
289                                                 {
290                                                         /* user was invited to channel */
291                                                         /* there may be an optional channel NOTICE here */
292                                                 }
293                                                 else
294                                                 {
295                                                         WriteServ(user->fd,"473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
296                                                         return NULL;
297                                                 }
298                                         }
299                                         user->RemoveInvite(Ptr->name);
300                                 }
301                                 if (Ptr->limit)
302                                 {
303                                         MOD_RESULT = 0;
304                                         FOREACH_RESULT(OnCheckLimit(user, Ptr));
305                                         if (!MOD_RESULT)
306                                         {
307                                                 if (usercount(Ptr) >= Ptr->limit)
308                                                 {
309                                                         WriteServ(user->fd,"471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
310                                                         return NULL;
311                                                 }
312                                         }
313                                 }
314                                 if (Ptr->bans.size())
315                                 {
316                                         log(DEBUG,"add_channel: about to walk banlist");
317                                         MOD_RESULT = 0;
318                                         FOREACH_RESULT(OnCheckBan(user, Ptr));
319                                         if (!MOD_RESULT)
320                                         {
321                                                 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
322                                                 {
323                                                         if (match(user->GetFullHost(),i->data))
324                                                         {
325                                                                 WriteServ(user->fd,"474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
326                                                                 return NULL;
327                                                         }
328                                                 }
329                                         }
330                                 }
331                         }
332                 }
333                 else
334                 {
335                         log(DEBUG,"Overridden checks");
336                 }
337                 created = 1;
338         }
339
340         log(DEBUG,"Passed channel checks");
341
342         for (unsigned int index =0; index < user->chans.size(); index++)
343         {
344                 if (user->chans[index].channel == NULL)
345                 {
346                         return ForceChan(Ptr,user->chans[index],user,created);
347                 }
348         }
349         /* XXX: If the user is an oper here, we can just extend their user->chans vector by one
350          * and put the channel in here. Same for remote users which are not bound by
351          * the channel limits. Otherwise, nope, youre boned.
352          */
353         if (user->fd < 0)
354         {
355                 ucrec a;
356                 chanrec* c = ForceChan(Ptr,a,user,created);
357                 user->chans.push_back(a);
358                 return c;
359         }
360         else if (strchr(user->modes,'o'))
361         {
362                 /* Oper allows extension up to the OPERMAXCHANS value */
363                 if (user->chans.size() < OPERMAXCHANS)
364                 {
365                         ucrec a;
366                         chanrec* c = ForceChan(Ptr,a,user,created);
367                         user->chans.push_back(a);
368                         return c;
369                 }
370         }
371         log(DEBUG,"add_channel: user channel max exceeded: %s %s",user->nick,cname);
372         WriteServ(user->fd,"405 %s %s :You are on too many channels",user->nick, cname);
373         return NULL;
374 }
375
376 chanrec* ForceChan(chanrec* Ptr,ucrec &a,userrec* user, int created)
377 {
378         if (created == 2)
379         {
380                 /* first user in is given ops */
381                 a.uc_modes = UCMODE_OP;
382         }
383         else
384         {
385                 a.uc_modes = 0;
386         }
387         a.channel = Ptr;
388         Ptr->AddUser((char*)user);
389         WriteChannel(Ptr,user,"JOIN :%s",Ptr->name);
390         log(DEBUG,"Sent JOIN to client");
391         if (Ptr->topicset)
392         {
393                 WriteServ(user->fd,"332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
394                 WriteServ(user->fd,"333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
395         }
396         userlist(user,Ptr);
397         WriteServ(user->fd,"366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
398         FOREACH_MOD OnUserJoin(user,Ptr);
399         return Ptr;
400 }
401
402 /* remove a channel from a users record, and remove the record from memory
403  * if the channel has become empty */
404
405 chanrec* del_channel(userrec *user, const char* cname, const char* reason, bool local)
406 {
407         if ((!user) || (!cname))
408         {
409                 log(DEFAULT,"*** BUG *** del_channel was given an invalid parameter");
410                 return NULL;
411         }
412
413         chanrec* Ptr = FindChan(cname);
414
415         if (!Ptr)
416                 return NULL;
417
418         FOREACH_MOD OnUserPart(user,Ptr);
419         log(DEBUG,"del_channel: removing: %s %s",user->nick,Ptr->name);
420
421         for (unsigned int i =0; i < user->chans.size(); i++)
422         {
423                 /* zap it from the channel list of the user */
424                 if (user->chans[i].channel == Ptr)
425                 {
426                         if (reason)
427                         {
428                                 WriteChannel(Ptr,user,"PART %s :%s",Ptr->name, reason);
429                         }
430                         else
431                         {
432                                 WriteChannel(Ptr,user,"PART :%s",Ptr->name);
433                         }
434                         user->chans[i].uc_modes = 0;
435                         user->chans[i].channel = NULL;
436                         log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
437                         break;
438                 }
439         }
440
441         Ptr->DelUser((char*)user);
442
443         /* if there are no users left on the channel */
444         if (!usercount(Ptr))
445         {
446                 chan_hash::iterator iter = chanlist.find(Ptr->name);
447
448                 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
449
450                 /* kill the record */
451                 if (iter != chanlist.end())
452                 {
453                         log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
454                         delete Ptr;
455                         chanlist.erase(iter);
456                 }
457         }
458
459         return NULL;
460 }
461
462
463 void kick_channel(userrec *src,userrec *user, chanrec *Ptr, char* reason)
464 {
465         if ((!src) || (!user) || (!Ptr) || (!reason))
466         {
467                 log(DEFAULT,"*** BUG *** kick_channel was given an invalid parameter");
468                 return;
469         }
470
471         if ((!Ptr) || (!user) || (!src))
472         {
473                 return;
474         }
475
476         log(DEBUG,"kick_channel: removing: %s %s %s",user->nick,Ptr->name,src->nick);
477
478         if (!has_channel(user,Ptr))
479         {
480                 WriteServ(src->fd,"441 %s %s %s :They are not on that channel",src->nick, user->nick, Ptr->name);
481                 return;
482         }
483
484         int MOD_RESULT = 0;
485         FOREACH_RESULT(OnAccessCheck(src,user,Ptr,AC_KICK));
486         if ((MOD_RESULT == ACR_DENY) && (!is_uline(src->server)))
487                 return;
488
489         if ((MOD_RESULT == ACR_DEFAULT) || (!is_uline(src->server)))
490         {
491                 if ((cstatus(src,Ptr) < STATUS_HOP) || (cstatus(src,Ptr) < cstatus(user,Ptr)))
492                 {
493                         if (cstatus(src,Ptr) == STATUS_HOP)
494                         {
495                                 WriteServ(src->fd,"482 %s %s :You must be a channel operator",src->nick, Ptr->name);
496                         }
497                         else
498                         {
499                                 WriteServ(src->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",src->nick, Ptr->name);
500                         }
501
502                         return;
503                 }
504         }
505
506         if (!is_uline(src->server))
507         {
508                 MOD_RESULT = 0;
509                 FOREACH_RESULT(OnUserPreKick(src,user,Ptr,reason));
510                 if (MOD_RESULT)
511                         return;
512         }
513
514         FOREACH_MOD OnUserKick(src,user,Ptr,reason);
515
516         for (unsigned int i =0; i < user->chans.size(); i++)
517         {
518                 /* zap it from the channel list of the user */
519                 if (user->chans[i].channel)
520                 if (!strcasecmp(user->chans[i].channel->name,Ptr->name))
521                 {
522                         WriteChannel(Ptr,src,"KICK %s %s :%s",Ptr->name, user->nick, reason);
523                         user->chans[i].uc_modes = 0;
524                         user->chans[i].channel = NULL;
525                         log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
526                         break;
527                 }
528         }
529
530         Ptr->DelUser((char*)user);
531
532         /* if there are no users left on the channel */
533         if (!usercount(Ptr))
534         {
535                 chan_hash::iterator iter = chanlist.find(Ptr->name);
536
537                 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
538
539                 /* kill the record */
540                 if (iter != chanlist.end())
541                 {
542                         log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
543                         delete Ptr;
544                         chanlist.erase(iter);
545                 }
546         }
547 }
548
549