]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/channels.cpp
Extban rework: allow exceptions to override bans on join
[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         bool created_by_local = false;
300
301         if (!Ptr)
302         {
303                 /*
304                  * Fix: desync bug was here, don't set @ on remote users - spanningtree handles their permissions. bug #358. -- w00t
305                  */
306                 if (!IS_LOCAL(user))
307                 {
308                         if (!TS)
309                                 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);
310                 }
311                 else
312                 {
313                         privs = "@";
314                         created_by_local = true;
315                 }
316
317                 if (IS_LOCAL(user) && override == false)
318                 {
319                         MOD_RESULT = 0;
320                         FOREACH_RESULT_I(Instance,I_OnUserPreJoin, OnUserPreJoin(user, NULL, cname, privs, key ? key : ""));
321                         if (MOD_RESULT == 1)
322                                 return NULL;
323                 }
324
325                 Ptr = new Channel(Instance, cname, TS);
326         }
327         else
328         {
329                 /* Already on the channel */
330                 if (Ptr->HasUser(user))
331                         return NULL;
332
333                 /*
334                  * remote users are allowed us to bypass channel modes
335                  * and bans (used by servers)
336                  */
337                 if (IS_LOCAL(user) && override == false)
338                 {
339                         MOD_RESULT = 0;
340                         FOREACH_RESULT_I(Instance,I_OnUserPreJoin, OnUserPreJoin(user, Ptr, cname, privs, key ? key : ""));
341                         if (MOD_RESULT == 1)
342                         {
343                                 return NULL;
344                         }
345                         else if (MOD_RESULT == 0)
346                         {
347                                 std::string ckey = Ptr->GetModeParameter('k');
348                                 bool invited = user->IsInvited(Ptr->name.c_str());
349                                 bool can_bypass = Instance->Config->InvBypassModes && invited;
350
351                                 if (!ckey.empty())
352                                 {
353                                         MOD_RESULT = 0;
354                                         FOREACH_RESULT_I(Instance, I_OnCheckKey, OnCheckKey(user, Ptr, key ? key : ""));
355                                         if (!MOD_RESULT)
356                                         {
357                                                 // If no key provided, or key is not the right one, and can't bypass +k (not invited or option not enabled)
358                                                 if ((!key || ckey != key) && !can_bypass)
359                                                 {
360                                                         user->WriteNumeric(ERR_BADCHANNELKEY, "%s %s :Cannot join channel (Incorrect channel key)",user->nick.c_str(), Ptr->name.c_str());
361                                                         return NULL;
362                                                 }
363                                         }
364                                 }
365
366                                 if (Ptr->IsModeSet('i'))
367                                 {
368                                         MOD_RESULT = 0;
369                                         FOREACH_RESULT_I(Instance,I_OnCheckInvite,OnCheckInvite(user, Ptr));
370                                         if (!MOD_RESULT)
371                                         {
372                                                 if (!invited)
373                                                 {
374                                                         user->WriteNumeric(ERR_INVITEONLYCHAN, "%s %s :Cannot join channel (Invite only)",user->nick.c_str(), Ptr->name.c_str());
375                                                         return NULL;
376                                                 }
377                                         }
378                                 }
379
380                                 std::string limit = Ptr->GetModeParameter('l');
381                                 if (!limit.empty())
382                                 {
383                                         MOD_RESULT = 0;
384                                         FOREACH_RESULT_I(Instance, I_OnCheckLimit, OnCheckLimit(user, Ptr));
385                                         if (!MOD_RESULT)
386                                         {
387                                                 long llimit = atol(limit.c_str());
388                                                 if (Ptr->GetUserCounter() >= llimit && !can_bypass)
389                                                 {
390                                                         user->WriteNumeric(ERR_CHANNELISFULL, "%s %s :Cannot join channel (Channel is full)",user->nick.c_str(), Ptr->name.c_str());
391                                                         return NULL;
392                                                 }
393                                         }
394                                 }
395
396                                 if (Ptr->bans.size())
397                                 {
398                                         if (Ptr->IsBanned(user) && !can_bypass)
399                                         {
400                                                 user->WriteNumeric(ERR_BANNEDFROMCHAN, "%s %s :Cannot join channel (You're banned)",user->nick.c_str(), Ptr->name.c_str());
401                                                 return NULL;
402                                         }
403                                 }
404
405                                 /*
406                                  * If the user has invites for this channel, remove them now
407                                  * after a successful join so they don't build up.
408                                  */
409                                 if (invited)
410                                 {
411                                         user->RemoveInvite(Ptr->name.c_str());
412                                 }
413                         }
414                 }
415         }
416
417         if (created_by_local)
418         {
419                 /* As spotted by jilles, dont bother to set this on remote users */
420                 Ptr->SetDefaultModes();
421         }
422
423         return Channel::ForceChan(Instance, Ptr, user, privs, bursting);
424 }
425
426 Channel* Channel::ForceChan(InspIRCd* Instance, Channel* Ptr, User* user, const std::string &privs, bool bursting)
427 {
428         std::string nick = user->nick;
429         bool silent = false;
430
431         Ptr->AddUser(user);
432
433         /* Just in case they have no permissions */
434         user->chans[Ptr] = 0;
435
436         for (std::string::const_iterator x = privs.begin(); x != privs.end(); x++)
437         {
438                 const char status = *x;
439                 ModeHandler* mh = Instance->Modes->FindPrefix(status);
440                 if (mh)
441                 {
442                         /* Set, and make sure that the mode handler knows this mode was now set */
443                         Ptr->SetPrefix(user, status, mh->GetPrefixRank(), true);
444                         mh->OnModeChange(Instance->FakeClient, Instance->FakeClient, Ptr, nick, true);
445
446                         switch (mh->GetPrefix())
447                         {
448                                 /* These logic ops are SAFE IN THIS CASE because if the entry doesnt exist,
449                                  * addressing operator[] creates it. If they do exist, it points to it.
450                                  * At all other times where we dont want to create an item if it doesnt exist, we
451                                  * must stick to ::find().
452                                  */
453                                 case '@':
454                                         user->chans[Ptr] |= UCMODE_OP;
455                                 break;
456                                 case '%':
457                                         user->chans[Ptr] |= UCMODE_HOP;
458                                 break;
459                                 case '+':
460                                         user->chans[Ptr] |= UCMODE_VOICE;
461                                 break;
462                         }
463                 }
464         }
465
466         FOREACH_MOD_I(Instance,I_OnUserJoin,OnUserJoin(user, Ptr, bursting, silent));
467
468         if (!silent)
469                 Ptr->WriteChannel(user,"JOIN :%s",Ptr->name.c_str());
470
471         /* Theyre not the first ones in here, make sure everyone else sees the modes we gave the user */
472         std::string ms = Instance->Modes->ModeString(user, Ptr);
473         if ((Ptr->GetUserCounter() > 1) && (ms.length()))
474                 Ptr->WriteAllExceptSender(user, true, 0, "MODE %s +%s", Ptr->name.c_str(), ms.c_str());
475
476         /* Major improvement by Brain - we dont need to be calculating all this pointlessly for remote users */
477         if (IS_LOCAL(user))
478         {
479                 if (Ptr->topicset)
480                 {
481                         user->WriteNumeric(RPL_TOPIC, "%s %s :%s", user->nick.c_str(), Ptr->name.c_str(), Ptr->topic.c_str());
482                         user->WriteNumeric(RPL_TOPICTIME, "%s %s %s %lu", user->nick.c_str(), Ptr->name.c_str(), Ptr->setby.c_str(), (unsigned long)Ptr->topicset);
483                 }
484                 Ptr->UserList(user);
485         }
486         FOREACH_MOD_I(Instance,I_OnPostJoin,OnPostJoin(user, Ptr));
487         return Ptr;
488 }
489
490 bool Channel::IsBanned(User* user)
491 {
492         int result = 0;
493         FOREACH_RESULT_MAP(I_OnCheckBan, OnCheckBan(user, this),
494                 result = banmatch_reduce(result, MOD_RESULT);
495         );
496
497         if (result)
498                 return (result < 0);
499
500         char mask[MAXBUF];
501         snprintf(mask, MAXBUF, "%s!%s@%s", user->nick.c_str(), user->ident.c_str(), user->GetIPString());
502         for (BanList::iterator i = this->bans.begin(); i != this->bans.end(); i++)
503         {
504                 if ((InspIRCd::Match(user->GetFullHost(),i->data, NULL)) || // host
505                         (InspIRCd::Match(user->GetFullRealHost(),i->data, NULL)) || // uncloaked host
506                         (InspIRCd::MatchCIDR(mask, i->data, NULL))) // ip
507                 {
508                         return true;
509                 }
510         }
511         return false;
512 }
513
514 int Channel::GetExtBanStatus(const std::string &str, char type)
515 {
516         int result = 0;
517         FOREACH_RESULT_MAP(I_OnCheckStringExtBan, OnCheckStringExtBan(str, this, type),
518                 result = banmatch_reduce(result, MOD_RESULT);
519         );
520
521         if (result)
522                 return result;
523
524         // nobody decided for us, check the ban list
525         for (BanList::iterator i = this->bans.begin(); i != this->bans.end(); i++)
526         {
527                 if (i->data[0] != type || i->data[1] != ':')
528                         continue;
529
530                 std::string maskptr = i->data.substr(2);
531                 ServerInstance->Logs->Log("EXTBANS", DEBUG, "Checking %s against %s, type is %c", str.c_str(), maskptr.c_str(), type);
532
533                 if (InspIRCd::Match(str, maskptr, NULL))
534                         return -1;
535         }
536
537         return 0;
538 }
539
540 int Channel::GetExtBanStatus(User *user, char type)
541 {
542         int result = 0;
543         FOREACH_RESULT_MAP(I_OnCheckExtBan, OnCheckExtBan(user, this, type),
544                 result = banmatch_reduce(result, MOD_RESULT);
545         );
546
547         if (result)
548                 return result;
549
550         char mask[MAXBUF];
551         int rv = 0;
552         snprintf(mask, MAXBUF, "%s!%s@%s", user->nick.c_str(), user->ident.c_str(), user->GetIPString());
553
554         // XXX: we should probably hook cloaked hosts in here somehow too..
555         rv = banmatch_reduce(rv, this->GetExtBanStatus(mask, type));
556         rv = banmatch_reduce(rv, this->GetExtBanStatus(user->GetFullHost(), type));
557         rv = banmatch_reduce(rv, this->GetExtBanStatus(user->GetFullRealHost(), type));
558         return rv;
559 }
560
561 /* Channel::PartUser
562  * remove a channel from a users record, and return the number of users left.
563  * Therefore, if this function returns 0 the caller should delete the Channel.
564  */
565 long Channel::PartUser(User *user, std::string &reason)
566 {
567         bool silent = false;
568
569         if (!user)
570                 return this->GetUserCounter();
571
572         UCListIter i = user->chans.find(this);
573         if (i != user->chans.end())
574         {
575                 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, reason, silent));
576
577                 if (!silent)
578                         this->WriteChannel(user, "PART %s%s%s", this->name.c_str(), reason.empty() ? "" : " :", reason.c_str());
579
580                 user->chans.erase(i);
581                 this->RemoveAllPrefixes(user);
582         }
583
584         if (!this->DelUser(user)) /* if there are no users left on the channel... */
585         {
586                 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
587                 /* kill the record */
588                 if (iter != ServerInstance->chanlist->end())
589                 {
590                         int MOD_RESULT = 0;
591                         FOREACH_RESULT_I(ServerInstance,I_OnChannelPreDelete, OnChannelPreDelete(this));
592                         if (MOD_RESULT == 1)
593                                 return 1; // delete halted by module
594                         FOREACH_MOD(I_OnChannelDelete, OnChannelDelete(this));
595                         ServerInstance->chanlist->erase(iter);
596                 }
597                 return 0;
598         }
599
600         return this->GetUserCounter();
601 }
602
603 long Channel::ServerKickUser(User* user, const char* reason, bool triggerevents, const char* servername)
604 {
605         bool silent = false;
606
607         if (!user || !reason)
608                 return this->GetUserCounter();
609
610         if (IS_LOCAL(user))
611         {
612                 if (!this->HasUser(user))
613                 {
614                         /* Not on channel */
615                         return this->GetUserCounter();
616                 }
617         }
618
619         if (servername == NULL || *ServerInstance->Config->HideWhoisServer)
620                 servername = ServerInstance->Config->ServerName;
621
622         if (triggerevents)
623         {
624                 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL, user, this, reason, silent));
625         }
626
627         UCListIter i = user->chans.find(this);
628         if (i != user->chans.end())
629         {
630                 if (!silent)
631                         this->WriteChannelWithServ(servername, "KICK %s %s :%s", this->name.c_str(), user->nick.c_str(), reason);
632
633                 user->chans.erase(i);
634                 this->RemoveAllPrefixes(user);
635         }
636
637         if (!this->DelUser(user))
638         {
639                 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
640                 /* kill the record */
641                 if (iter != ServerInstance->chanlist->end())
642                 {
643                         int MOD_RESULT = 0;
644                         FOREACH_RESULT_I(ServerInstance,I_OnChannelPreDelete, OnChannelPreDelete(this));
645                         if (MOD_RESULT == 1)
646                                 return 1; // delete halted by module
647                         FOREACH_MOD(I_OnChannelDelete, OnChannelDelete(this));
648                         ServerInstance->chanlist->erase(iter);
649                 }
650                 return 0;
651         }
652
653         return this->GetUserCounter();
654 }
655
656 long Channel::KickUser(User *src, User *user, const char* reason)
657 {
658         bool silent = false;
659
660         if (!src || !user || !reason)
661                 return this->GetUserCounter();
662
663         if (IS_LOCAL(src))
664         {
665                 if (!this->HasUser(user))
666                 {
667                         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());
668                         return this->GetUserCounter();
669                 }
670                 if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
671                 {
672                         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());
673                         return this->GetUserCounter();
674                 }
675                 int MOD_RESULT = 0;
676
677                 if (!ServerInstance->ULine(src->server))
678                 {
679                         MOD_RESULT = 0;
680                         FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,this,reason));
681                         if (MOD_RESULT == 1)
682                                 return this->GetUserCounter();
683                 }
684                 /* Set to -1 by OnUserPreKick if explicit allow was set */
685                 if (MOD_RESULT != -1)
686                 {
687                         FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,this,AC_KICK));
688                         if ((MOD_RESULT == ACR_DENY) && (!ServerInstance->ULine(src->server)))
689                                 return this->GetUserCounter();
690
691                         if ((MOD_RESULT == ACR_DEFAULT) || (!ServerInstance->ULine(src->server)))
692                         {
693                                 int them = this->GetStatus(src);
694                                 int us = this->GetStatus(user);
695                                 if ((them < STATUS_HOP) || (them < us))
696                                 {
697                                         src->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s %s :You must be a channel %soperator",src->nick.c_str(), this->name.c_str(), them == STATUS_HOP ? "" : "half-");
698                                         return this->GetUserCounter();
699                                 }
700                         }
701                 }
702         }
703
704         FOREACH_MOD(I_OnUserKick,OnUserKick(src, user, this, reason, silent));
705
706         UCListIter i = user->chans.find(this);
707         if (i != user->chans.end())
708         {
709                 /* zap it from the channel list of the user */
710                 if (!silent)
711                         this->WriteChannel(src, "KICK %s %s :%s", this->name.c_str(), user->nick.c_str(), reason);
712
713                 user->chans.erase(i);
714                 this->RemoveAllPrefixes(user);
715         }
716
717         if (!this->DelUser(user))
718         /* if there are no users left on the channel */
719         {
720                 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name.c_str());
721
722                 /* kill the record */
723                 if (iter != ServerInstance->chanlist->end())
724                 {
725                         int MOD_RESULT = 0;
726                         FOREACH_RESULT_I(ServerInstance,I_OnChannelPreDelete, OnChannelPreDelete(this));
727                         if (MOD_RESULT == 1)
728                                 return 1; // delete halted by module
729                         FOREACH_MOD(I_OnChannelDelete, OnChannelDelete(this));
730                         ServerInstance->chanlist->erase(iter);
731                 }
732                 return 0;
733         }
734
735         return this->GetUserCounter();
736 }
737
738 void Channel::WriteChannel(User* user, const char* text, ...)
739 {
740         char textbuffer[MAXBUF];
741         va_list argsPtr;
742
743         if (!user || !text)
744                 return;
745
746         va_start(argsPtr, text);
747         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
748         va_end(argsPtr);
749
750         this->WriteChannel(user, std::string(textbuffer));
751 }
752
753 void Channel::WriteChannel(User* user, const std::string &text)
754 {
755         CUList *ulist = this->GetUsers();
756         char tb[MAXBUF];
757
758         if (!user)
759                 return;
760
761         snprintf(tb,MAXBUF,":%s %s", user->GetFullHost().c_str(), text.c_str());
762         std::string out = tb;
763
764         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
765         {
766                 if (IS_LOCAL(i->first))
767                         i->first->Write(out);
768         }
769 }
770
771 void Channel::WriteChannelWithServ(const char* ServName, const char* text, ...)
772 {
773         char textbuffer[MAXBUF];
774         va_list argsPtr;
775
776         if (!text)
777                 return;
778
779         va_start(argsPtr, text);
780         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
781         va_end(argsPtr);
782
783         this->WriteChannelWithServ(ServName, std::string(textbuffer));
784 }
785
786 void Channel::WriteChannelWithServ(const char* ServName, const std::string &text)
787 {
788         CUList *ulist = this->GetUsers();
789         char tb[MAXBUF];
790
791         snprintf(tb,MAXBUF,":%s %s", ServName ? ServName : ServerInstance->Config->ServerName, text.c_str());
792         std::string out = tb;
793
794         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
795         {
796                 if (IS_LOCAL(i->first))
797                         i->first->Write(out);
798         }
799 }
800
801 /* write formatted text from a source user to all users on a channel except
802  * for the sender (for privmsg etc) */
803 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const char* text, ...)
804 {
805         char textbuffer[MAXBUF];
806         va_list argsPtr;
807
808         if (!text)
809                 return;
810
811         va_start(argsPtr, text);
812         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
813         va_end(argsPtr);
814
815         this->WriteAllExceptSender(user, serversource, status, std::string(textbuffer));
816 }
817
818 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const char* text, ...)
819 {
820         char textbuffer[MAXBUF];
821         va_list argsPtr;
822
823         if (!text)
824                 return;
825
826         va_start(argsPtr, text);
827         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
828         va_end(argsPtr);
829
830         this->WriteAllExcept(user, serversource, status, except_list, std::string(textbuffer));
831 }
832
833 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string &text)
834 {
835         CUList *ulist = this->GetUsers();
836         char tb[MAXBUF];
837
838         snprintf(tb,MAXBUF,":%s %s", user->GetFullHost().c_str(), text.c_str());
839         std::string out = tb;
840
841         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
842         {
843                 if ((IS_LOCAL(i->first)) && (except_list.find(i->first) == except_list.end()))
844                 {
845                         /* User doesnt have the status we're after */
846                         if (status && !strchr(this->GetAllPrefixChars(i->first), status))
847                                 continue;
848
849                         if (serversource)
850                                 i->first->WriteServ(text);
851                         else
852                                 i->first->Write(out);
853                 }
854         }
855 }
856
857 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const std::string& text)
858 {
859         CUList except_list;
860         except_list[user] = user->nick;
861         this->WriteAllExcept(user, serversource, status, except_list, std::string(text));
862 }
863
864 /*
865  * return a count of the users on a specific channel accounting for
866  * invisible users who won't increase the count. e.g. for /LIST
867  */
868 int Channel::CountInvisible()
869 {
870         int count = 0;
871         CUList *ulist= this->GetUsers();
872         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
873         {
874                 if (!(i->first->IsModeSet('i')))
875                         count++;
876         }
877
878         return count;
879 }
880
881 char* Channel::ChanModes(bool showkey)
882 {
883         static char scratch[MAXBUF];
884         static char sparam[MAXBUF];
885         char* offset = scratch;
886         std::string extparam;
887
888         *scratch = '\0';
889         *sparam = '\0';
890
891         /* This was still iterating up to 190, Channel::modes is only 64 elements -- Om */
892         for(int n = 0; n < 64; n++)
893         {
894                 if(this->modes[n])
895                 {
896                         *offset++ = n + 65;
897                         extparam.clear();
898                         switch (n)
899                         {
900                                 case CM_KEY:
901                                         // Unfortunately this must be special-cased, as we definitely don't want to always display key.
902                                         if (showkey)
903                                         {
904                                                 extparam = this->GetModeParameter('k');
905                                         }
906                                         else
907                                         {
908                                                 extparam = "<key>";
909                                         }
910                                         break;
911                                 case CM_NOEXTERNAL:
912                                 case CM_TOPICLOCK:
913                                 case CM_INVITEONLY:
914                                 case CM_MODERATED:
915                                 case CM_SECRET:
916                                 case CM_PRIVATE:
917                                         /* We know these have no parameters */
918                                 break;
919                                 default:
920                                         extparam = this->GetModeParameter(n + 65);
921                                 break;
922                         }
923                         if (!extparam.empty())
924                         {
925                                 charlcat(sparam,' ',MAXBUF);
926                                 strlcat(sparam,extparam.c_str(),MAXBUF);
927                         }
928                 }
929         }
930
931         /* Null terminate scratch */
932         *offset = '\0';
933         strlcat(scratch,sparam,MAXBUF);
934         return scratch;
935 }
936
937 /* compile a userlist of a channel into a string, each nick seperated by
938  * spaces and op, voice etc status shown as @ and +, and send it to 'user'
939  */
940 void Channel::UserList(User *user, CUList *ulist)
941 {
942         char list[MAXBUF];
943         size_t dlen, curlen;
944         int MOD_RESULT = 0;
945         bool call_modules = true;
946
947         if (!IS_LOCAL(user))
948                 return;
949
950         FOREACH_RESULT(I_OnUserList,OnUserList(user, this, ulist));
951         if (MOD_RESULT == 1)
952                 call_modules = false;
953
954         if (MOD_RESULT != -1)
955         {
956                 if ((this->IsModeSet('s')) && (!this->HasUser(user)))
957                 {
958                         user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nick/channel",user->nick.c_str(), this->name.c_str());
959                         return;
960                 }
961         }
962
963         dlen = curlen = snprintf(list,MAXBUF,"%s %c %s :", user->nick.c_str(), this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=',  this->name.c_str());
964
965         int numusers = 0;
966         char* ptr = list + dlen;
967
968         if (!ulist)
969                 ulist = this->GetUsers();
970
971         /* Improvement by Brain - this doesnt change in value, so why was it inside
972          * the loop?
973          */
974         bool has_user = this->HasUser(user);
975
976         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
977         {
978                 if ((!has_user) && (i->first->IsModeSet('i')))
979                 {
980                         /*
981                          * user is +i, and source not on the channel, does not show
982                          * nick in NAMES list
983                          */
984                         continue;
985                 }
986
987                 if (i->first->Visibility && !i->first->Visibility->VisibleTo(user))
988                         continue;
989
990                 std::string prefixlist = this->GetPrefixChar(i->first);
991                 std::string nick = i->first->nick;
992
993                 if (call_modules)
994                 {
995                         FOREACH_MOD(I_OnNamesListItem, OnNamesListItem(user, i->first, this, prefixlist, nick));
996
997                         /* Nick was nuked, a module wants us to skip it */
998                         if (nick.empty())
999                                 continue;
1000                 }
1001
1002                 size_t ptrlen = 0;
1003
1004                 if (curlen + prefixlist.length() + nick.length() + 1 > 480)
1005                 {
1006                         /* list overflowed into multiple numerics */
1007                         user->WriteNumeric(RPL_NAMREPLY, std::string(list));
1008
1009                         /* reset our lengths */
1010                         dlen = curlen = snprintf(list,MAXBUF,"%s %c %s :", user->nick.c_str(), this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=', this->name.c_str());
1011                         ptr = list + dlen;
1012
1013                         ptrlen = 0;
1014                         numusers = 0;
1015                 }
1016
1017                 ptrlen = snprintf(ptr, MAXBUF, "%s%s ", prefixlist.c_str(), nick.c_str());
1018
1019                 curlen += ptrlen;
1020                 ptr += ptrlen;
1021
1022                 numusers++;
1023         }
1024
1025         /* if whats left in the list isnt empty, send it */
1026         if (numusers)
1027         {
1028                 user->WriteNumeric(RPL_NAMREPLY, std::string(list));
1029         }
1030
1031         user->WriteNumeric(RPL_ENDOFNAMES, "%s %s :End of /NAMES list.", user->nick.c_str(), this->name.c_str());
1032 }
1033
1034 long Channel::GetMaxBans()
1035 {
1036         /* Return the cached value if there is one */
1037         if (this->maxbans)
1038                 return this->maxbans;
1039
1040         /* If there isnt one, we have to do some O(n) hax to find it the first time. (ick) */
1041         for (std::map<std::string,int>::iterator n = ServerInstance->Config->maxbans.begin(); n != ServerInstance->Config->maxbans.end(); n++)
1042         {
1043                 if (InspIRCd::Match(this->name, n->first, NULL))
1044                 {
1045                         this->maxbans = n->second;
1046                         return n->second;
1047                 }
1048         }
1049
1050         /* Screw it, just return the default of 64 */
1051         this->maxbans = 64;
1052         return this->maxbans;
1053 }
1054
1055 void Channel::ResetMaxBans()
1056 {
1057         this->maxbans = 0;
1058 }
1059
1060 /* returns the status character for a given user on a channel, e.g. @ for op,
1061  * % for halfop etc. If the user has several modes set, the highest mode
1062  * the user has must be returned.
1063  */
1064 const char* Channel::GetPrefixChar(User *user)
1065 {
1066         static char pf[2] = {0, 0};
1067
1068         prefixlist::iterator n = prefixes.find(user);
1069         if (n != prefixes.end())
1070         {
1071                 if (n->second.size())
1072                 {
1073                         /* If the user has any prefixes, their highest prefix
1074                          * will always be at the head of the list, as the list is
1075                          * sorted in rank order highest first (see SetPrefix()
1076                          * for reasons why)
1077                          */
1078                         *pf = n->second.begin()->first;
1079                         return pf;
1080                 }
1081         }
1082
1083         *pf = 0;
1084         return pf;
1085 }
1086
1087
1088 const char* Channel::GetAllPrefixChars(User* user)
1089 {
1090         static char prefix[MAXBUF];
1091         int ctr = 0;
1092         *prefix = 0;
1093
1094         prefixlist::iterator n = prefixes.find(user);
1095         if (n != prefixes.end())
1096         {
1097                 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
1098                 {
1099                         prefix[ctr++] = x->first;
1100                 }
1101         }
1102
1103         prefix[ctr] = 0;
1104
1105         return prefix;
1106 }
1107
1108 unsigned int Channel::GetPrefixValue(User* user)
1109 {
1110         prefixlist::iterator n = prefixes.find(user);
1111         if (n != prefixes.end())
1112         {
1113                 if (n->second.size())
1114                         return n->second.begin()->second;
1115         }
1116         return 0;
1117 }
1118
1119 int Channel::GetStatusFlags(User *user)
1120 {
1121         UCListIter i = user->chans.find(this);
1122         if (i != user->chans.end())
1123         {
1124                 return i->second;
1125         }
1126         return 0;
1127 }
1128
1129 int Channel::GetStatus(User *user)
1130 {
1131         if (ServerInstance->ULine(user->server))
1132                 return STATUS_OP;
1133
1134         UCListIter i = user->chans.find(this);
1135         if (i != user->chans.end())
1136         {
1137                 if ((i->second & UCMODE_OP) > 0)
1138                 {
1139                         return STATUS_OP;
1140                 }
1141                 if ((i->second & UCMODE_HOP) > 0)
1142                 {
1143                         return STATUS_HOP;
1144                 }
1145                 if ((i->second & UCMODE_VOICE) > 0)
1146                 {
1147                         return STATUS_VOICE;
1148                 }
1149                 return STATUS_NORMAL;
1150         }
1151         return STATUS_NORMAL;
1152 }
1153
1154 void Channel::SetPrefix(User* user, char prefix, unsigned int prefix_value, bool adding)
1155 {
1156         prefixlist::iterator n = prefixes.find(user);
1157         prefixtype pfx = std::make_pair(prefix,prefix_value);
1158         if (adding)
1159         {
1160                 if (n != prefixes.end())
1161                 {
1162                         if (std::find(n->second.begin(), n->second.end(), pfx) == n->second.end())
1163                         {
1164                                 n->second.push_back(pfx);
1165                                 /* We must keep prefixes in rank order, largest first.
1166                                  * This is for two reasons, firstly because x-chat *ass-u-me's* this
1167                                  * state, and secondly it turns out to be a benefit to us later.
1168                                  * See above in GetPrefix().
1169                                  */
1170                                 std::sort(n->second.begin(), n->second.end(), ModeParser::PrefixComparison);
1171                         }
1172                 }
1173                 else
1174                 {
1175                         pfxcontainer one;
1176                         one.push_back(pfx);
1177                         prefixes.insert(std::make_pair<User*,pfxcontainer>(user, one));
1178                 }
1179         }
1180         else
1181         {
1182                 if (n != prefixes.end())
1183                 {
1184                         pfxcontainer::iterator x = std::find(n->second.begin(), n->second.end(), pfx);
1185                         if (x != n->second.end())
1186                                 n->second.erase(x);
1187                 }
1188         }
1189 }
1190
1191 void Channel::RemoveAllPrefixes(User* user)
1192 {
1193         prefixlist::iterator n = prefixes.find(user);
1194         if (n != prefixes.end())
1195         {
1196                 prefixes.erase(n);
1197         }
1198 }