]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/channels.cpp
Remove channel::created as its unused and only serves to confuse (see last commit...
[user/henk/code/inspircd.git] / src / channels.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /* $Core */
15
16 #include "inspircd.h"
17 #include <cstdarg>
18 #include "mode.h"
19
20 Channel::Channel(InspIRCd* Instance, const std::string &cname, time_t ts) : ServerInstance(Instance)
21 {
22         chan_hash::iterator findchan = ServerInstance->chanlist->find(cname);
23         if (findchan != Instance->chanlist->end())
24                 throw CoreException("Cannot create duplicate channel " + cname);
25
26         (*(ServerInstance->chanlist))[cname.c_str()] = this;
27         this->name.assign(cname, 0, ServerInstance->Config->Limits.ChanMax);
28         this->age = ts ? ts : ServerInstance->Time();
29
30         maxbans = topicset = 0;
31         modes.reset();
32 }
33
34 void Channel::SetMode(char mode,bool mode_on)
35 {
36         modes[mode-65] = mode_on;
37         if (!mode_on)
38                 this->SetModeParam(mode,"",false);
39 }
40
41
42 void Channel::SetModeParam(char mode,const char* parameter,bool mode_on)
43 {
44         CustomModeList::iterator n = custom_mode_params.find(mode);
45
46         if (mode_on)
47         {
48                 if (n == custom_mode_params.end())
49                         custom_mode_params[mode] = strdup(parameter);
50         }
51         else
52         {
53                 if (n != custom_mode_params.end())
54                 {
55                         free(n->second);
56                         custom_mode_params.erase(n);
57                 }
58         }
59 }
60
61 bool Channel::IsModeSet(char mode)
62 {
63         return modes[mode-65];
64 }
65
66 std::string Channel::GetModeParameter(char mode)
67 {
68         CustomModeList::iterator n = custom_mode_params.find(mode);
69         if (n != custom_mode_params.end())
70                 return n->second;
71         return "";
72 }
73
74 int Channel::SetTopic(User *u, std::string &ntopic, bool forceset)
75 {
76         if (u && IS_LOCAL(u))
77         {
78                 if(!forceset)
79                 {
80                         int MOD_RESULT = 0;
81                         /* 0: check status, 1: don't, -1: disallow change silently */
82
83                         FOREACH_RESULT(I_OnLocalTopicChange,OnLocalTopicChange(u,this,ntopic));
84                 
85                         if (MOD_RESULT == 1)
86                                 return CMD_FAILURE;
87                         else if (MOD_RESULT == 0)
88                         {
89                                 if (!this->HasUser(u))
90                                 {
91                                         u->WriteNumeric(442, "%s %s :You're not on that channel!",u->nick.c_str(), this->name.c_str());
92                                         return CMD_FAILURE;
93                                 }
94                                 if ((this->IsModeSet('t')) && (this->GetStatus(u) < STATUS_HOP))
95                                 {
96                                         u->WriteNumeric(482, "%s %s :You must be at least a half-operator to change the topic on this channel", u->nick.c_str(), this->name.c_str());
97                                         return CMD_FAILURE;
98                                 }
99                         }
100                 }
101         }
102
103         this->topic.assign(ntopic, 0, ServerInstance->Config->Limits.MaxTopic);
104         if (u)
105         {
106                 this->setby.assign(ServerInstance->Config->FullHostInTopic ? u->GetFullHost() : u->nick, 0, 128);
107                 this->WriteChannel(u, "TOPIC %s :%s", this->name.c_str(), this->topic.c_str());
108         }
109         else
110         {
111                 this->setby.assign(ServerInstance->Config->ServerName);
112                 this->WriteChannelWithServ(ServerInstance->Config->ServerName, "TOPIC %s :%s", this->name.c_str(), this->topic.c_str());
113         }
114
115         this->topicset = ServerInstance->Time();
116
117         if (u && IS_LOCAL(u))
118         {
119                 FOREACH_MOD(I_OnPostLocalTopicChange,OnPostLocalTopicChange(u, this, this->topic));
120         }
121
122         return CMD_SUCCESS;
123 }
124
125 long Channel::GetUserCounter()
126 {
127         return (this->internal_userlist.size());
128 }
129
130 void Channel::AddUser(User* user)
131 {
132         internal_userlist[user] = user->nick;
133 }
134
135 unsigned long Channel::DelUser(User* user)
136 {
137         CUListIter a = internal_userlist.find(user);
138
139         if (a != internal_userlist.end())
140         {
141                 internal_userlist.erase(a);
142                 /* And tidy any others... */
143                 DelOppedUser(user);
144                 DelHalfoppedUser(user);
145                 DelVoicedUser(user);
146         }
147
148         return internal_userlist.size();
149 }
150
151 bool Channel::HasUser(User* user)
152 {
153         return (internal_userlist.find(user) != internal_userlist.end());
154 }
155
156 void Channel::AddOppedUser(User* user)
157 {
158         internal_op_userlist[user] = user->nick;
159 }
160
161 void Channel::DelOppedUser(User* user)
162 {
163         CUListIter a = internal_op_userlist.find(user);
164         if (a != internal_op_userlist.end())
165         {
166                 internal_op_userlist.erase(a);
167                 return;
168         }
169 }
170
171 void Channel::AddHalfoppedUser(User* user)
172 {
173         internal_halfop_userlist[user] = user->nick;
174 }
175
176 void Channel::DelHalfoppedUser(User* user)
177 {
178         CUListIter a = internal_halfop_userlist.find(user);
179
180         if (a != internal_halfop_userlist.end())
181         {
182                 internal_halfop_userlist.erase(a);
183         }
184 }
185
186 void Channel::AddVoicedUser(User* user)
187 {
188         internal_voice_userlist[user] = user->nick;
189 }
190
191 void Channel::DelVoicedUser(User* user)
192 {
193         CUListIter a = internal_voice_userlist.find(user);
194
195         if (a != internal_voice_userlist.end())
196         {
197                 internal_voice_userlist.erase(a);
198         }
199 }
200
201 CUList* Channel::GetUsers()
202 {
203         return &internal_userlist;
204 }
205
206 CUList* Channel::GetOppedUsers()
207 {
208         return &internal_op_userlist;
209 }
210
211 CUList* Channel::GetHalfoppedUsers()
212 {
213         return &internal_halfop_userlist;
214 }
215
216 CUList* Channel::GetVoicedUsers()
217 {
218         return &internal_voice_userlist;
219 }
220
221 void Channel::SetDefaultModes()
222 {
223         ServerInstance->Logs->Log("CHANNELS", DEBUG, "SetDefaultModes %s", ServerInstance->Config->DefaultModes);
224         irc::spacesepstream list(ServerInstance->Config->DefaultModes);
225         std::string modeseq;
226         std::string parameter;
227
228         list.GetToken(modeseq);
229
230         for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
231         {
232                 ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
233                 if (mode)
234                 {
235                         if (mode->GetNumParams(true))
236                                 list.GetToken(parameter);
237                         else
238                                 parameter.clear();
239
240                         mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, this, parameter, true);
241                 }
242         }
243 }
244
245 /*
246  * add a channel to a user, creating the record for it if needed and linking
247  * it to the user record
248  */
249 Channel* Channel::JoinUser(InspIRCd* Instance, User *user, const char* cn, bool override, const char* key, bool bursting, time_t TS)
250 {
251         if (!user || !cn)
252                 return NULL;
253
254         char cname[MAXBUF];
255         int MOD_RESULT = 0;
256         std::string privs;
257         Channel *Ptr;
258
259         /*
260          * We don't restrict the number of channels that remote users or users that are override-joining may be in.
261          * We restrict local users to MaxChans channels.
262          * We restrict local operators to OperMaxChans channels.
263          * This is a lot more logical than how it was formerly. -- w00t
264          */
265         if (IS_LOCAL(user) && !override)
266         {
267                 // Checking MyClass exists because we *may* get here with NULL, not 100% sure.
268                 if (user->MyClass && user->MyClass->GetMaxChans())
269                 {
270                         if (user->chans.size() >= user->MyClass->GetMaxChans())
271                         {
272                                 user->WriteNumeric(ERR_TOOMANYCHANNELS, "%s %s :You are on too many channels",user->nick.c_str(), cn);
273                                 return NULL;
274                         }
275                 }
276                 else
277                 {
278                         if (user->HasPrivPermission("channels/high-join-limit"))
279                         {
280                                 if (user->chans.size() >= Instance->Config->OperMaxChans)
281                                 {
282                                         user->WriteNumeric(ERR_TOOMANYCHANNELS, "%s %s :You are on too many channels",user->nick.c_str(), cn);
283                                         return NULL;
284                                 }
285                         }
286                         else
287                         {
288                                 if (user->chans.size() >= Instance->Config->MaxChans)
289                                 {
290                                         user->WriteNumeric(ERR_TOOMANYCHANNELS, "%s %s :You are on too many channels",user->nick.c_str(), cn);
291                                         return NULL;
292                                 }
293                         }
294                 }
295         }
296
297         strlcpy(cname, cn, Instance->Config->Limits.ChanMax);
298         Ptr = Instance->FindChan(cname);
299
300         if (!Ptr)
301         {
302                 /*
303                  * Fix: desync bug was here, don't set @ on remote users - spanningtree handles their permissions. bug #358. -- w00t
304                  */
305                 if (!IS_LOCAL(user))
306                 {
307                         if (!TS)
308                                 Instance->Logs->Log("CHANNEL",DEBUG,"*** BUG *** Channel::JoinUser called for REMOTE user '%s' on channel '%s' but no TS given!", user->nick.c_str(), cn);
309                 }
310                 else
311                 {
312                         privs = "@";
313                 }
314
315                 if (IS_LOCAL(user) && override == false)
316                 {
317                         MOD_RESULT = 0;
318                         FOREACH_RESULT_I(Instance,I_OnUserPreJoin, OnUserPreJoin(user, NULL, cname, privs, key ? key : ""));
319                         if (MOD_RESULT == 1)
320                                 return NULL;
321                 }
322
323                 Ptr = new Channel(Instance, cname, TS);
324         }
325         else
326         {
327                 /* Already on the channel */
328                 if (Ptr->HasUser(user))
329                         return NULL;
330
331                 /*
332                  * remote users are allowed us to bypass channel modes
333                  * and bans (used by servers)
334                  */
335                 if (IS_LOCAL(user) && override == false)
336                 {
337                         MOD_RESULT = 0;
338                         FOREACH_RESULT_I(Instance,I_OnUserPreJoin, OnUserPreJoin(user, Ptr, cname, privs, key ? key : ""));
339                         if (MOD_RESULT == 1)
340                         {
341                                 return NULL;
342                         }
343                         else if (MOD_RESULT == 0)
344                         {
345                                 std::string ckey = Ptr->GetModeParameter('k');
346                                 bool invited = user->IsInvited(Ptr->name.c_str());
347                                 bool can_bypass = Instance->Config->InvBypassModes && invited;
348
349                                 if (!ckey.empty())
350                                 {
351                                         MOD_RESULT = 0;
352                                         FOREACH_RESULT_I(Instance, I_OnCheckKey, OnCheckKey(user, Ptr, key ? key : ""));
353                                         if (!MOD_RESULT)
354                                         {
355                                                 // If no key provided, or key is not the right one, and can't bypass +k (not invited or option not enabled)
356                                                 if ((!key || ckey != key) && !can_bypass)
357                                                 {
358                                                         user->WriteNumeric(ERR_BADCHANNELKEY, "%s %s :Cannot join channel (Incorrect channel key)",user->nick.c_str(), Ptr->name.c_str());
359                                                         return NULL;
360                                                 }
361                                         }
362                                 }
363
364                                 if (Ptr->IsModeSet('i'))
365                                 {
366                                         MOD_RESULT = 0;
367                                         FOREACH_RESULT_I(Instance,I_OnCheckInvite,OnCheckInvite(user, Ptr));
368                                         if (!MOD_RESULT)
369                                         {
370                                                 if (!invited)
371                                                 {
372                                                         user->WriteNumeric(ERR_INVITEONLYCHAN, "%s %s :Cannot join channel (Invite only)",user->nick.c_str(), Ptr->name.c_str());
373                                                         return NULL;
374                                                 }
375                                         }
376                                 }
377
378                                 std::string limit = Ptr->GetModeParameter('l');
379                                 if (!limit.empty())
380                                 {
381                                         MOD_RESULT = 0;
382                                         FOREACH_RESULT_I(Instance, I_OnCheckLimit, OnCheckLimit(user, Ptr));
383                                         if (!MOD_RESULT)
384                                         {
385                                                 long llimit = atol(limit.c_str());
386                                                 if (Ptr->GetUserCounter() >= llimit && !can_bypass)
387                                                 {
388                                                         user->WriteNumeric(ERR_CHANNELISFULL, "%s %s :Cannot join channel (Channel is full)",user->nick.c_str(), Ptr->name.c_str());
389                                                         return NULL;
390                                                 }
391                                         }
392                                 }
393
394                                 if (Ptr->bans.size())
395                                 {
396                                         if (Ptr->IsBanned(user) && !can_bypass)
397                                         {
398                                                 user->WriteNumeric(ERR_BANNEDFROMCHAN, "%s %s :Cannot join channel (You're banned)",user->nick.c_str(), Ptr->name.c_str());
399                                                 return NULL;
400                                         }
401                                 }
402
403                                 /*
404                                  * If the user has invites for this channel, remove them now
405                                  * after a successful join so they don't build up.
406                                  */
407                                 if (invited)
408                                 {
409                                         user->RemoveInvite(Ptr->name.c_str());
410                                 }
411                         }
412                 }
413         }
414
415         /* As spotted by jilles, dont bother to set this on remote users */
416         if (IS_LOCAL(user) && Ptr->GetUserCounter() == 0)
417                 Ptr->SetDefaultModes();
418
419         return Channel::ForceChan(Instance, Ptr, user, privs, bursting);
420 }
421
422 Channel* Channel::ForceChan(InspIRCd* Instance, Channel* Ptr, User* user, const std::string &privs, bool bursting)
423 {
424         std::string nick = user->nick;
425         bool silent = false;
426
427         Ptr->AddUser(user);
428
429         /* Just in case they have no permissions */
430         user->chans[Ptr] = 0;
431
432         for (std::string::const_iterator x = privs.begin(); x != privs.end(); x++)
433         {
434                 const char status = *x;
435                 ModeHandler* mh = Instance->Modes->FindPrefix(status);
436                 if (mh)
437                 {
438                         /* Set, and make sure that the mode handler knows this mode was now set */
439                         Ptr->SetPrefix(user, status, mh->GetPrefixRank(), true);
440                         mh->OnModeChange(Instance->FakeClient, Instance->FakeClient, Ptr, nick, true);
441
442                         switch (mh->GetPrefix())
443                         {
444                                 /* These logic ops are SAFE IN THIS CASE because if the entry doesnt exist,
445                                  * addressing operator[] creates it. If they do exist, it points to it.
446                                  * At all other times where we dont want to create an item if it doesnt exist, we
447                                  * must stick to ::find().
448                                  */
449                                 case '@':
450                                         user->chans[Ptr] |= UCMODE_OP;
451                                 break;
452                                 case '%':
453                                         user->chans[Ptr] |= UCMODE_HOP;
454                                 break;
455                                 case '+':
456                                         user->chans[Ptr] |= UCMODE_VOICE;
457                                 break;
458                         }
459                 }
460         }
461
462         FOREACH_MOD_I(Instance,I_OnUserJoin,OnUserJoin(user, Ptr, bursting, silent));
463
464         if (!silent)
465                 Ptr->WriteChannel(user,"JOIN :%s",Ptr->name.c_str());
466
467         /* Theyre not the first ones in here, make sure everyone else sees the modes we gave the user */
468         std::string ms = Instance->Modes->ModeString(user, Ptr);
469         if ((Ptr->GetUserCounter() > 1) && (ms.length()))
470                 Ptr->WriteAllExceptSender(user, true, 0, "MODE %s +%s", Ptr->name.c_str(), ms.c_str());
471
472         /* Major improvement by Brain - we dont need to be calculating all this pointlessly for remote users */
473         if (IS_LOCAL(user))
474         {
475                 if (Ptr->topicset)
476                 {
477                         user->WriteNumeric(RPL_TOPIC, "%s %s :%s", user->nick.c_str(), Ptr->name.c_str(), Ptr->topic.c_str());
478                         user->WriteNumeric(RPL_TOPICTIME, "%s %s %s %lu", user->nick.c_str(), Ptr->name.c_str(), Ptr->setby.c_str(), (unsigned long)Ptr->topicset);
479                 }
480                 Ptr->UserList(user);
481         }
482         FOREACH_MOD_I(Instance,I_OnPostJoin,OnPostJoin(user, Ptr));
483         return Ptr;
484 }
485
486 bool Channel::IsBanned(User* user)
487 {
488         char mask[MAXBUF];
489         int MOD_RESULT = 0;
490         FOREACH_RESULT(I_OnCheckBan,OnCheckBan(user, this));
491
492         if (MOD_RESULT == -1)
493                 return true;
494         else if (MOD_RESULT == 0)
495         {
496                 snprintf(mask, MAXBUF, "%s!%s@%s", user->nick.c_str(), user->ident.c_str(), user->GetIPString());
497                 for (BanList::iterator i = this->bans.begin(); i != this->bans.end(); i++)
498                 {
499                         if ((InspIRCd::Match(user->GetFullHost(),i->data, NULL)) || // host
500                                 (InspIRCd::Match(user->GetFullRealHost(),i->data, NULL)) || // uncloaked host
501                                 (InspIRCd::MatchCIDR(mask, i->data, NULL))) // ip
502                         {
503                                 return true;
504                         }
505                 }
506         }
507         return false;
508 }
509
510 bool Channel::IsExtBanned(const std::string &str, char type)
511 {
512         int MOD_RESULT = 0;
513         FOREACH_RESULT(I_OnCheckStringExtBan, OnCheckStringExtBan(str, this, type));
514
515         if (MOD_RESULT == -1)
516                 return true;
517         else if (MOD_RESULT == 0)
518         {
519                 for (BanList::iterator i = this->bans.begin(); i != this->bans.end(); i++)
520                 {
521                         if (i->data[0] != type || i->data[1] != ':')
522                                 continue;
523
524                         // Iterate past char and : to get to the mask without doing a data copy(!)
525                         std::string maskptr = i->data.substr(2);
526                         ServerInstance->Logs->Log("EXTBANS", DEBUG, "Checking %s against %s, type is %c", str.c_str(), maskptr.c_str(), type);
527
528                         if (InspIRCd::Match(str, maskptr, NULL))
529                                 return true;
530                 }
531         }
532
533         return false;
534 }
535
536 bool Channel::IsExtBanned(User *user, char type)
537 {
538         int MOD_RESULT = 0;
539         FOREACH_RESULT(I_OnCheckExtBan, OnCheckExtBan(user, this, type));
540
541         if (MOD_RESULT == -1)
542                 return true;
543         else if (MOD_RESULT == 0)
544         {
545                 char mask[MAXBUF];
546                 snprintf(mask, MAXBUF, "%s!%s@%s", user->nick.c_str(), user->ident.c_str(), user->GetIPString());
547
548                 // XXX: we should probably hook cloaked hosts in here somehow too..
549                 if (this->IsExtBanned(mask, type))
550                         return true;
551
552                 if (this->IsExtBanned(user->GetFullHost(), type))
553                         return true;
554
555                 if (this->IsExtBanned(user->GetFullRealHost(), type))
556                         return true;
557         }
558
559         return false;
560 }
561
562 /* Channel::PartUser
563  * remove a channel from a users record, and return the number of users left.
564  * Therefore, if this function returns 0 the caller should delete the Channel.
565  */
566 long Channel::PartUser(User *user, std::string &reason)
567 {
568         bool silent = false;
569
570         if (!user)
571                 return this->GetUserCounter();
572
573         UCListIter i = user->chans.find(this);
574         if (i != user->chans.end())
575         {
576                 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, reason, silent));
577
578                 if (!silent)
579                         this->WriteChannel(user, "PART %s%s%s", this->name.c_str(), reason.empty() ? "" : " :", reason.c_str());
580
581                 user->chans.erase(i);
582                 this->RemoveAllPrefixes(user);
583         }
584
585         if (!this->DelUser(user)) /* if there are no users left on the channel... */
586         {
587                 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
588                 /* kill the record */
589                 if (iter != ServerInstance->chanlist->end())
590                 {
591                         int MOD_RESULT = 0;
592                         FOREACH_RESULT_I(ServerInstance,I_OnChannelPreDelete, OnChannelPreDelete(this));
593                         if (MOD_RESULT == 1)
594                                 return 1; // delete halted by module
595                         FOREACH_MOD(I_OnChannelDelete, OnChannelDelete(this));
596                         ServerInstance->chanlist->erase(iter);
597                 }
598                 return 0;
599         }
600
601         return this->GetUserCounter();
602 }
603
604 long Channel::ServerKickUser(User* user, const char* reason, bool triggerevents, const char* servername)
605 {
606         bool silent = false;
607
608         if (!user || !reason)
609                 return this->GetUserCounter();
610
611         if (IS_LOCAL(user))
612         {
613                 if (!this->HasUser(user))
614                 {
615                         /* Not on channel */
616                         return this->GetUserCounter();
617                 }
618         }
619
620         if (servername == NULL || *ServerInstance->Config->HideWhoisServer)
621                 servername = ServerInstance->Config->ServerName;
622
623         if (triggerevents)
624         {
625                 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL, user, this, reason, silent));
626         }
627
628         UCListIter i = user->chans.find(this);
629         if (i != user->chans.end())
630         {
631                 if (!silent)
632                         this->WriteChannelWithServ(servername, "KICK %s %s :%s", this->name.c_str(), user->nick.c_str(), reason);
633
634                 user->chans.erase(i);
635                 this->RemoveAllPrefixes(user);
636         }
637
638         if (!this->DelUser(user))
639         {
640                 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
641                 /* kill the record */
642                 if (iter != ServerInstance->chanlist->end())
643                 {
644                         int MOD_RESULT = 0;
645                         FOREACH_RESULT_I(ServerInstance,I_OnChannelPreDelete, OnChannelPreDelete(this));
646                         if (MOD_RESULT == 1)
647                                 return 1; // delete halted by module
648                         FOREACH_MOD(I_OnChannelDelete, OnChannelDelete(this));
649                         ServerInstance->chanlist->erase(iter);
650                 }
651                 return 0;
652         }
653
654         return this->GetUserCounter();
655 }
656
657 long Channel::KickUser(User *src, User *user, const char* reason)
658 {
659         bool silent = false;
660
661         if (!src || !user || !reason)
662                 return this->GetUserCounter();
663
664         if (IS_LOCAL(src))
665         {
666                 if (!this->HasUser(user))
667                 {
668                         src->WriteNumeric(ERR_USERNOTINCHANNEL, "%s %s %s :They are not on that channel",src->nick.c_str(), user->nick.c_str(), this->name.c_str());
669                         return this->GetUserCounter();
670                 }
671                 if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
672                 {
673                         src->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s %s :Only a u-line may kick a u-line from a channel.",src->nick.c_str(), this->name.c_str());
674                         return this->GetUserCounter();
675                 }
676                 int MOD_RESULT = 0;
677
678                 if (!ServerInstance->ULine(src->server))
679                 {
680                         MOD_RESULT = 0;
681                         FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,this,reason));
682                         if (MOD_RESULT == 1)
683                                 return this->GetUserCounter();
684                 }
685                 /* Set to -1 by OnUserPreKick if explicit allow was set */
686                 if (MOD_RESULT != -1)
687                 {
688                         FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,this,AC_KICK));
689                         if ((MOD_RESULT == ACR_DENY) && (!ServerInstance->ULine(src->server)))
690                                 return this->GetUserCounter();
691
692                         if ((MOD_RESULT == ACR_DEFAULT) || (!ServerInstance->ULine(src->server)))
693                         {
694                                 int them = this->GetStatus(src);
695                                 int us = this->GetStatus(user);
696                                 if ((them < STATUS_HOP) || (them < us))
697                                 {
698                                         src->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s %s :You must be a channel %soperator",src->nick.c_str(), this->name.c_str(), them == STATUS_HOP ? "" : "half-");
699                                         return this->GetUserCounter();
700                                 }
701                         }
702                 }
703         }
704
705         FOREACH_MOD(I_OnUserKick,OnUserKick(src, user, this, reason, silent));
706
707         UCListIter i = user->chans.find(this);
708         if (i != user->chans.end())
709         {
710                 /* zap it from the channel list of the user */
711                 if (!silent)
712                         this->WriteChannel(src, "KICK %s %s :%s", this->name.c_str(), user->nick.c_str(), reason);
713
714                 user->chans.erase(i);
715                 this->RemoveAllPrefixes(user);
716         }
717
718         if (!this->DelUser(user))
719         /* if there are no users left on the channel */
720         {
721                 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name.c_str());
722
723                 /* kill the record */
724                 if (iter != ServerInstance->chanlist->end())
725                 {
726                         int MOD_RESULT = 0;
727                         FOREACH_RESULT_I(ServerInstance,I_OnChannelPreDelete, OnChannelPreDelete(this));
728                         if (MOD_RESULT == 1)
729                                 return 1; // delete halted by module
730                         FOREACH_MOD(I_OnChannelDelete, OnChannelDelete(this));
731                         ServerInstance->chanlist->erase(iter);
732                 }
733                 return 0;
734         }
735
736         return this->GetUserCounter();
737 }
738
739 void Channel::WriteChannel(User* user, const char* text, ...)
740 {
741         char textbuffer[MAXBUF];
742         va_list argsPtr;
743
744         if (!user || !text)
745                 return;
746
747         va_start(argsPtr, text);
748         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
749         va_end(argsPtr);
750
751         this->WriteChannel(user, std::string(textbuffer));
752 }
753
754 void Channel::WriteChannel(User* user, const std::string &text)
755 {
756         CUList *ulist = this->GetUsers();
757         char tb[MAXBUF];
758
759         if (!user)
760                 return;
761
762         snprintf(tb,MAXBUF,":%s %s", user->GetFullHost().c_str(), text.c_str());
763         std::string out = tb;
764
765         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
766         {
767                 if (IS_LOCAL(i->first))
768                         i->first->Write(out);
769         }
770 }
771
772 void Channel::WriteChannelWithServ(const char* ServName, const char* text, ...)
773 {
774         char textbuffer[MAXBUF];
775         va_list argsPtr;
776
777         if (!text)
778                 return;
779
780         va_start(argsPtr, text);
781         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
782         va_end(argsPtr);
783
784         this->WriteChannelWithServ(ServName, std::string(textbuffer));
785 }
786
787 void Channel::WriteChannelWithServ(const char* ServName, const std::string &text)
788 {
789         CUList *ulist = this->GetUsers();
790         char tb[MAXBUF];
791
792         snprintf(tb,MAXBUF,":%s %s", ServName ? ServName : ServerInstance->Config->ServerName, text.c_str());
793         std::string out = tb;
794
795         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
796         {
797                 if (IS_LOCAL(i->first))
798                         i->first->Write(out);
799         }
800 }
801
802 /* write formatted text from a source user to all users on a channel except
803  * for the sender (for privmsg etc) */
804 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const char* text, ...)
805 {
806         char textbuffer[MAXBUF];
807         va_list argsPtr;
808
809         if (!text)
810                 return;
811
812         va_start(argsPtr, text);
813         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
814         va_end(argsPtr);
815
816         this->WriteAllExceptSender(user, serversource, status, std::string(textbuffer));
817 }
818
819 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const char* text, ...)
820 {
821         char textbuffer[MAXBUF];
822         va_list argsPtr;
823
824         if (!text)
825                 return;
826
827         va_start(argsPtr, text);
828         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
829         va_end(argsPtr);
830
831         this->WriteAllExcept(user, serversource, status, except_list, std::string(textbuffer));
832 }
833
834 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string &text)
835 {
836         CUList *ulist = this->GetUsers();
837         char tb[MAXBUF];
838
839         snprintf(tb,MAXBUF,":%s %s", user->GetFullHost().c_str(), text.c_str());
840         std::string out = tb;
841
842         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
843         {
844                 if ((IS_LOCAL(i->first)) && (except_list.find(i->first) == except_list.end()))
845                 {
846                         /* User doesnt have the status we're after */
847                         if (status && !strchr(this->GetAllPrefixChars(i->first), status))
848                                 continue;
849
850                         if (serversource)
851                                 i->first->WriteServ(text);
852                         else
853                                 i->first->Write(out);
854                 }
855         }
856 }
857
858 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const std::string& text)
859 {
860         CUList except_list;
861         except_list[user] = user->nick;
862         this->WriteAllExcept(user, serversource, status, except_list, std::string(text));
863 }
864
865 /*
866  * return a count of the users on a specific channel accounting for
867  * invisible users who won't increase the count. e.g. for /LIST
868  */
869 int Channel::CountInvisible()
870 {
871         int count = 0;
872         CUList *ulist= this->GetUsers();
873         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
874         {
875                 if (!(i->first->IsModeSet('i')))
876                         count++;
877         }
878
879         return count;
880 }
881
882 char* Channel::ChanModes(bool showkey)
883 {
884         static char scratch[MAXBUF];
885         static char sparam[MAXBUF];
886         char* offset = scratch;
887         std::string extparam;
888
889         *scratch = '\0';
890         *sparam = '\0';
891
892         /* This was still iterating up to 190, Channel::modes is only 64 elements -- Om */
893         for(int n = 0; n < 64; n++)
894         {
895                 if(this->modes[n])
896                 {
897                         *offset++ = n + 65;
898                         extparam.clear();
899                         switch (n)
900                         {
901                                 case CM_KEY:
902                                         // Unfortunately this must be special-cased, as we definitely don't want to always display key.
903                                         if (showkey)
904                                         {
905                                                 extparam = this->GetModeParameter('k');
906                                         }
907                                         else
908                                         {
909                                                 extparam = "<key>";
910                                         }
911                                         break;
912                                 case CM_NOEXTERNAL:
913                                 case CM_TOPICLOCK:
914                                 case CM_INVITEONLY:
915                                 case CM_MODERATED:
916                                 case CM_SECRET:
917                                 case CM_PRIVATE:
918                                         /* We know these have no parameters */
919                                 break;
920                                 default:
921                                         extparam = this->GetModeParameter(n + 65);
922                                 break;
923                         }
924                         if (!extparam.empty())
925                         {
926                                 charlcat(sparam,' ',MAXBUF);
927                                 strlcat(sparam,extparam.c_str(),MAXBUF);
928                         }
929                 }
930         }
931
932         /* Null terminate scratch */
933         *offset = '\0';
934         strlcat(scratch,sparam,MAXBUF);
935         return scratch;
936 }
937
938 /* compile a userlist of a channel into a string, each nick seperated by
939  * spaces and op, voice etc status shown as @ and +, and send it to 'user'
940  */
941 void Channel::UserList(User *user, CUList *ulist)
942 {
943         char list[MAXBUF];
944         size_t dlen, curlen;
945         int MOD_RESULT = 0;
946         bool call_modules = true;
947
948         if (!IS_LOCAL(user))
949                 return;
950
951         FOREACH_RESULT(I_OnUserList,OnUserList(user, this, ulist));
952         if (MOD_RESULT == 1)
953                 call_modules = false;
954
955         if (MOD_RESULT != -1)
956         {
957                 if ((this->IsModeSet('s')) && (!this->HasUser(user)))
958                 {
959                         user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nick/channel",user->nick.c_str(), this->name.c_str());
960                         return;
961                 }
962         }
963
964         dlen = curlen = snprintf(list,MAXBUF,"%s %c %s :", user->nick.c_str(), this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=',  this->name.c_str());
965
966         int numusers = 0;
967         char* ptr = list + dlen;
968
969         if (!ulist)
970                 ulist = this->GetUsers();
971
972         /* Improvement by Brain - this doesnt change in value, so why was it inside
973          * the loop?
974          */
975         bool has_user = this->HasUser(user);
976
977         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
978         {
979                 if ((!has_user) && (i->first->IsModeSet('i')))
980                 {
981                         /*
982                          * user is +i, and source not on the channel, does not show
983                          * nick in NAMES list
984                          */
985                         continue;
986                 }
987
988                 if (i->first->Visibility && !i->first->Visibility->VisibleTo(user))
989                         continue;
990
991                 std::string prefixlist = this->GetPrefixChar(i->first);
992                 std::string nick = i->first->nick;
993
994                 if (call_modules)
995                 {
996                         FOREACH_MOD(I_OnNamesListItem, OnNamesListItem(user, i->first, this, prefixlist, nick));
997
998                         /* Nick was nuked, a module wants us to skip it */
999                         if (nick.empty())
1000                                 continue;
1001                 }
1002
1003                 size_t ptrlen = 0;
1004
1005                 if (curlen + prefixlist.length() + nick.length() + 1 > 480)
1006                 {
1007                         /* list overflowed into multiple numerics */
1008                         user->WriteNumeric(RPL_NAMREPLY, std::string(list));
1009
1010                         /* reset our lengths */
1011                         dlen = curlen = snprintf(list,MAXBUF,"%s %c %s :", user->nick.c_str(), this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=', this->name.c_str());
1012                         ptr = list + dlen;
1013
1014                         ptrlen = 0;
1015                         numusers = 0;
1016                 }
1017
1018                 ptrlen = snprintf(ptr, MAXBUF, "%s%s ", prefixlist.c_str(), nick.c_str());
1019
1020                 curlen += ptrlen;
1021                 ptr += ptrlen;
1022
1023                 numusers++;
1024         }
1025
1026         /* if whats left in the list isnt empty, send it */
1027         if (numusers)
1028         {
1029                 user->WriteNumeric(RPL_NAMREPLY, std::string(list));
1030         }
1031
1032         user->WriteNumeric(RPL_ENDOFNAMES, "%s %s :End of /NAMES list.", user->nick.c_str(), this->name.c_str());
1033 }
1034
1035 long Channel::GetMaxBans()
1036 {
1037         /* Return the cached value if there is one */
1038         if (this->maxbans)
1039                 return this->maxbans;
1040
1041         /* If there isnt one, we have to do some O(n) hax to find it the first time. (ick) */
1042         for (std::map<std::string,int>::iterator n = ServerInstance->Config->maxbans.begin(); n != ServerInstance->Config->maxbans.end(); n++)
1043         {
1044                 if (InspIRCd::Match(this->name, n->first, NULL))
1045                 {
1046                         this->maxbans = n->second;
1047                         return n->second;
1048                 }
1049         }
1050
1051         /* Screw it, just return the default of 64 */
1052         this->maxbans = 64;
1053         return this->maxbans;
1054 }
1055
1056 void Channel::ResetMaxBans()
1057 {
1058         this->maxbans = 0;
1059 }
1060
1061 /* returns the status character for a given user on a channel, e.g. @ for op,
1062  * % for halfop etc. If the user has several modes set, the highest mode
1063  * the user has must be returned.
1064  */
1065 const char* Channel::GetPrefixChar(User *user)
1066 {
1067         static char pf[2] = {0, 0};
1068
1069         prefixlist::iterator n = prefixes.find(user);
1070         if (n != prefixes.end())
1071         {
1072                 if (n->second.size())
1073                 {
1074                         /* If the user has any prefixes, their highest prefix
1075                          * will always be at the head of the list, as the list is
1076                          * sorted in rank order highest first (see SetPrefix()
1077                          * for reasons why)
1078                          */
1079                         *pf = n->second.begin()->first;
1080                         return pf;
1081                 }
1082         }
1083
1084         *pf = 0;
1085         return pf;
1086 }
1087
1088
1089 const char* Channel::GetAllPrefixChars(User* user)
1090 {
1091         static char prefix[MAXBUF];
1092         int ctr = 0;
1093         *prefix = 0;
1094
1095         prefixlist::iterator n = prefixes.find(user);
1096         if (n != prefixes.end())
1097         {
1098                 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
1099                 {
1100                         prefix[ctr++] = x->first;
1101                 }
1102         }
1103
1104         prefix[ctr] = 0;
1105
1106         return prefix;
1107 }
1108
1109 unsigned int Channel::GetPrefixValue(User* user)
1110 {
1111         prefixlist::iterator n = prefixes.find(user);
1112         if (n != prefixes.end())
1113         {
1114                 if (n->second.size())
1115                         return n->second.begin()->second;
1116         }
1117         return 0;
1118 }
1119
1120 int Channel::GetStatusFlags(User *user)
1121 {
1122         UCListIter i = user->chans.find(this);
1123         if (i != user->chans.end())
1124         {
1125                 return i->second;
1126         }
1127         return 0;
1128 }
1129
1130 int Channel::GetStatus(User *user)
1131 {
1132         if (ServerInstance->ULine(user->server))
1133                 return STATUS_OP;
1134
1135         UCListIter i = user->chans.find(this);
1136         if (i != user->chans.end())
1137         {
1138                 if ((i->second & UCMODE_OP) > 0)
1139                 {
1140                         return STATUS_OP;
1141                 }
1142                 if ((i->second & UCMODE_HOP) > 0)
1143                 {
1144                         return STATUS_HOP;
1145                 }
1146                 if ((i->second & UCMODE_VOICE) > 0)
1147                 {
1148                         return STATUS_VOICE;
1149                 }
1150                 return STATUS_NORMAL;
1151         }
1152         return STATUS_NORMAL;
1153 }
1154
1155 void Channel::SetPrefix(User* user, char prefix, unsigned int prefix_value, bool adding)
1156 {
1157         prefixlist::iterator n = prefixes.find(user);
1158         prefixtype pfx = std::make_pair(prefix,prefix_value);
1159         if (adding)
1160         {
1161                 if (n != prefixes.end())
1162                 {
1163                         if (std::find(n->second.begin(), n->second.end(), pfx) == n->second.end())
1164                         {
1165                                 n->second.push_back(pfx);
1166                                 /* We must keep prefixes in rank order, largest first.
1167                                  * This is for two reasons, firstly because x-chat *ass-u-me's* this
1168                                  * state, and secondly it turns out to be a benefit to us later.
1169                                  * See above in GetPrefix().
1170                                  */
1171                                 std::sort(n->second.begin(), n->second.end(), ModeParser::PrefixComparison);
1172                         }
1173                 }
1174                 else
1175                 {
1176                         pfxcontainer one;
1177                         one.push_back(pfx);
1178                         prefixes.insert(std::make_pair<User*,pfxcontainer>(user, one));
1179                 }
1180         }
1181         else
1182         {
1183                 if (n != prefixes.end())
1184                 {
1185                         pfxcontainer::iterator x = std::find(n->second.begin(), n->second.end(), pfx);
1186                         if (x != n->second.end())
1187                                 n->second.erase(x);
1188                 }
1189         }
1190 }
1191
1192 void Channel::RemoveAllPrefixes(User* user)
1193 {
1194         prefixlist::iterator n = prefixes.find(user);
1195         if (n != prefixes.end())
1196         {
1197                 prefixes.erase(n);
1198         }
1199 }