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