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