]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/channels.cpp
Merge in initial numbered parameters patch from Phoenix, thanks :)
[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         char mask[MAXBUF];
493         int MOD_RESULT = 0;
494         FOREACH_RESULT(I_OnCheckBan,OnCheckBan(user, this));
495
496         if (MOD_RESULT == -1)
497                 return true;
498         else if (MOD_RESULT == 0)
499         {
500                 snprintf(mask, MAXBUF, "%s!%s@%s", user->nick.c_str(), user->ident.c_str(), user->GetIPString());
501                 for (BanList::iterator i = this->bans.begin(); i != this->bans.end(); i++)
502                 {
503                         if ((InspIRCd::Match(user->GetFullHost(),i->data, NULL)) || // host
504                                 (InspIRCd::Match(user->GetFullRealHost(),i->data, NULL)) || // uncloaked host
505                                 (InspIRCd::MatchCIDR(mask, i->data, NULL))) // ip
506                         {
507                                 return true;
508                         }
509                 }
510         }
511         return false;
512 }
513
514 bool Channel::IsExtBanned(const std::string &str, char type)
515 {
516         int MOD_RESULT = 0;
517         FOREACH_RESULT(I_OnCheckStringExtBan, OnCheckStringExtBan(str, this, type));
518
519         if (MOD_RESULT == -1)
520                 return true;
521         else if (MOD_RESULT == 0)
522         {
523                 for (BanList::iterator i = this->bans.begin(); i != this->bans.end(); i++)
524                 {
525                         if (i->data[0] != type || i->data[1] != ':')
526                                 continue;
527
528                         // Iterate past char and : to get to the mask without doing a data copy(!)
529                         std::string maskptr = i->data.substr(2);
530                         ServerInstance->Logs->Log("EXTBANS", DEBUG, "Checking %s against %s, type is %c", str.c_str(), maskptr.c_str(), type);
531
532                         if (InspIRCd::Match(str, maskptr, NULL))
533                                 return true;
534                 }
535         }
536
537         return false;
538 }
539
540 bool Channel::IsExtBanned(User *user, char type)
541 {
542         int MOD_RESULT = 0;
543         FOREACH_RESULT(I_OnCheckExtBan, OnCheckExtBan(user, this, type));
544
545         if (MOD_RESULT == -1)
546                 return true;
547         else if (MOD_RESULT == 0)
548         {
549                 char mask[MAXBUF];
550                 snprintf(mask, MAXBUF, "%s!%s@%s", user->nick.c_str(), user->ident.c_str(), user->GetIPString());
551
552                 // XXX: we should probably hook cloaked hosts in here somehow too..
553                 if (this->IsExtBanned(mask, type))
554                         return true;
555
556                 if (this->IsExtBanned(user->GetFullHost(), type))
557                         return true;
558
559                 if (this->IsExtBanned(user->GetFullRealHost(), type))
560                         return true;
561         }
562
563         return false;
564 }
565
566 /* Channel::PartUser
567  * remove a channel from a users record, and return the number of users left.
568  * Therefore, if this function returns 0 the caller should delete the Channel.
569  */
570 long Channel::PartUser(User *user, std::string &reason)
571 {
572         bool silent = false;
573
574         if (!user)
575                 return this->GetUserCounter();
576
577         UCListIter i = user->chans.find(this);
578         if (i != user->chans.end())
579         {
580                 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, reason, silent));
581
582                 if (!silent)
583                         this->WriteChannel(user, "PART %s%s%s", this->name.c_str(), reason.empty() ? "" : " :", reason.c_str());
584
585                 user->chans.erase(i);
586                 this->RemoveAllPrefixes(user);
587         }
588
589         if (!this->DelUser(user)) /* if there are no users left on the channel... */
590         {
591                 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
592                 /* kill the record */
593                 if (iter != ServerInstance->chanlist->end())
594                 {
595                         int MOD_RESULT = 0;
596                         FOREACH_RESULT_I(ServerInstance,I_OnChannelPreDelete, OnChannelPreDelete(this));
597                         if (MOD_RESULT == 1)
598                                 return 1; // delete halted by module
599                         FOREACH_MOD(I_OnChannelDelete, OnChannelDelete(this));
600                         ServerInstance->chanlist->erase(iter);
601                 }
602                 return 0;
603         }
604
605         return this->GetUserCounter();
606 }
607
608 long Channel::ServerKickUser(User* user, const char* reason, bool triggerevents, const char* servername)
609 {
610         bool silent = false;
611
612         if (!user || !reason)
613                 return this->GetUserCounter();
614
615         if (IS_LOCAL(user))
616         {
617                 if (!this->HasUser(user))
618                 {
619                         /* Not on channel */
620                         return this->GetUserCounter();
621                 }
622         }
623
624         if (servername == NULL || *ServerInstance->Config->HideWhoisServer)
625                 servername = ServerInstance->Config->ServerName;
626
627         if (triggerevents)
628         {
629                 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL, user, this, reason, silent));
630         }
631
632         UCListIter i = user->chans.find(this);
633         if (i != user->chans.end())
634         {
635                 if (!silent)
636                         this->WriteChannelWithServ(servername, "KICK %s %s :%s", this->name.c_str(), user->nick.c_str(), reason);
637
638                 user->chans.erase(i);
639                 this->RemoveAllPrefixes(user);
640         }
641
642         if (!this->DelUser(user))
643         {
644                 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
645                 /* kill the record */
646                 if (iter != ServerInstance->chanlist->end())
647                 {
648                         int MOD_RESULT = 0;
649                         FOREACH_RESULT_I(ServerInstance,I_OnChannelPreDelete, OnChannelPreDelete(this));
650                         if (MOD_RESULT == 1)
651                                 return 1; // delete halted by module
652                         FOREACH_MOD(I_OnChannelDelete, OnChannelDelete(this));
653                         ServerInstance->chanlist->erase(iter);
654                 }
655                 return 0;
656         }
657
658         return this->GetUserCounter();
659 }
660
661 long Channel::KickUser(User *src, User *user, const char* reason)
662 {
663         bool silent = false;
664
665         if (!src || !user || !reason)
666                 return this->GetUserCounter();
667
668         if (IS_LOCAL(src))
669         {
670                 if (!this->HasUser(user))
671                 {
672                         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());
673                         return this->GetUserCounter();
674                 }
675                 if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
676                 {
677                         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());
678                         return this->GetUserCounter();
679                 }
680                 int MOD_RESULT = 0;
681
682                 if (!ServerInstance->ULine(src->server))
683                 {
684                         MOD_RESULT = 0;
685                         FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,this,reason));
686                         if (MOD_RESULT == 1)
687                                 return this->GetUserCounter();
688                 }
689                 /* Set to -1 by OnUserPreKick if explicit allow was set */
690                 if (MOD_RESULT != -1)
691                 {
692                         FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,this,AC_KICK));
693                         if ((MOD_RESULT == ACR_DENY) && (!ServerInstance->ULine(src->server)))
694                                 return this->GetUserCounter();
695
696                         if ((MOD_RESULT == ACR_DEFAULT) || (!ServerInstance->ULine(src->server)))
697                         {
698                                 int them = this->GetStatus(src);
699                                 int us = this->GetStatus(user);
700                                 if ((them < STATUS_HOP) || (them < us))
701                                 {
702                                         src->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s %s :You must be a channel %soperator",src->nick.c_str(), this->name.c_str(), them == STATUS_HOP ? "" : "half-");
703                                         return this->GetUserCounter();
704                                 }
705                         }
706                 }
707         }
708
709         FOREACH_MOD(I_OnUserKick,OnUserKick(src, user, this, reason, silent));
710
711         UCListIter i = user->chans.find(this);
712         if (i != user->chans.end())
713         {
714                 /* zap it from the channel list of the user */
715                 if (!silent)
716                         this->WriteChannel(src, "KICK %s %s :%s", this->name.c_str(), user->nick.c_str(), reason);
717
718                 user->chans.erase(i);
719                 this->RemoveAllPrefixes(user);
720         }
721
722         if (!this->DelUser(user))
723         /* if there are no users left on the channel */
724         {
725                 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name.c_str());
726
727                 /* kill the record */
728                 if (iter != ServerInstance->chanlist->end())
729                 {
730                         int MOD_RESULT = 0;
731                         FOREACH_RESULT_I(ServerInstance,I_OnChannelPreDelete, OnChannelPreDelete(this));
732                         if (MOD_RESULT == 1)
733                                 return 1; // delete halted by module
734                         FOREACH_MOD(I_OnChannelDelete, OnChannelDelete(this));
735                         ServerInstance->chanlist->erase(iter);
736                 }
737                 return 0;
738         }
739
740         return this->GetUserCounter();
741 }
742
743 void Channel::WriteChannel(User* user, const char* text, ...)
744 {
745         char textbuffer[MAXBUF];
746         va_list argsPtr;
747
748         if (!user || !text)
749                 return;
750
751         va_start(argsPtr, text);
752         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
753         va_end(argsPtr);
754
755         this->WriteChannel(user, std::string(textbuffer));
756 }
757
758 void Channel::WriteChannel(User* user, const std::string &text)
759 {
760         CUList *ulist = this->GetUsers();
761         char tb[MAXBUF];
762
763         if (!user)
764                 return;
765
766         snprintf(tb,MAXBUF,":%s %s", user->GetFullHost().c_str(), text.c_str());
767         std::string out = tb;
768
769         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
770         {
771                 if (IS_LOCAL(i->first))
772                         i->first->Write(out);
773         }
774 }
775
776 void Channel::WriteChannelWithServ(const char* ServName, const char* text, ...)
777 {
778         char textbuffer[MAXBUF];
779         va_list argsPtr;
780
781         if (!text)
782                 return;
783
784         va_start(argsPtr, text);
785         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
786         va_end(argsPtr);
787
788         this->WriteChannelWithServ(ServName, std::string(textbuffer));
789 }
790
791 void Channel::WriteChannelWithServ(const char* ServName, const std::string &text)
792 {
793         CUList *ulist = this->GetUsers();
794         char tb[MAXBUF];
795
796         snprintf(tb,MAXBUF,":%s %s", ServName ? ServName : ServerInstance->Config->ServerName, text.c_str());
797         std::string out = tb;
798
799         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
800         {
801                 if (IS_LOCAL(i->first))
802                         i->first->Write(out);
803         }
804 }
805
806 /* write formatted text from a source user to all users on a channel except
807  * for the sender (for privmsg etc) */
808 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const char* text, ...)
809 {
810         char textbuffer[MAXBUF];
811         va_list argsPtr;
812
813         if (!text)
814                 return;
815
816         va_start(argsPtr, text);
817         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
818         va_end(argsPtr);
819
820         this->WriteAllExceptSender(user, serversource, status, std::string(textbuffer));
821 }
822
823 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const char* text, ...)
824 {
825         char textbuffer[MAXBUF];
826         va_list argsPtr;
827
828         if (!text)
829                 return;
830
831         va_start(argsPtr, text);
832         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
833         va_end(argsPtr);
834
835         this->WriteAllExcept(user, serversource, status, except_list, std::string(textbuffer));
836 }
837
838 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string &text)
839 {
840         CUList *ulist = this->GetUsers();
841         char tb[MAXBUF];
842
843         snprintf(tb,MAXBUF,":%s %s", user->GetFullHost().c_str(), text.c_str());
844         std::string out = tb;
845
846         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
847         {
848                 if ((IS_LOCAL(i->first)) && (except_list.find(i->first) == except_list.end()))
849                 {
850                         /* User doesnt have the status we're after */
851                         if (status && !strchr(this->GetAllPrefixChars(i->first), status))
852                                 continue;
853
854                         if (serversource)
855                                 i->first->WriteServ(text);
856                         else
857                                 i->first->Write(out);
858                 }
859         }
860 }
861
862 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const std::string& text)
863 {
864         CUList except_list;
865         except_list[user] = user->nick;
866         this->WriteAllExcept(user, serversource, status, except_list, std::string(text));
867 }
868
869 /*
870  * return a count of the users on a specific channel accounting for
871  * invisible users who won't increase the count. e.g. for /LIST
872  */
873 int Channel::CountInvisible()
874 {
875         int count = 0;
876         CUList *ulist= this->GetUsers();
877         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
878         {
879                 if (!(i->first->IsModeSet('i')))
880                         count++;
881         }
882
883         return count;
884 }
885
886 char* Channel::ChanModes(bool showkey)
887 {
888         static char scratch[MAXBUF];
889         static char sparam[MAXBUF];
890         char* offset = scratch;
891         std::string extparam;
892
893         *scratch = '\0';
894         *sparam = '\0';
895
896         /* This was still iterating up to 190, Channel::modes is only 64 elements -- Om */
897         for(int n = 0; n < 64; n++)
898         {
899                 if(this->modes[n])
900                 {
901                         *offset++ = n + 65;
902                         extparam.clear();
903                         switch (n)
904                         {
905                                 case CM_KEY:
906                                         // Unfortunately this must be special-cased, as we definitely don't want to always display key.
907                                         if (showkey)
908                                         {
909                                                 extparam = this->GetModeParameter('k');
910                                         }
911                                         else
912                                         {
913                                                 extparam = "<key>";
914                                         }
915                                         break;
916                                 case CM_NOEXTERNAL:
917                                 case CM_TOPICLOCK:
918                                 case CM_INVITEONLY:
919                                 case CM_MODERATED:
920                                 case CM_SECRET:
921                                 case CM_PRIVATE:
922                                         /* We know these have no parameters */
923                                 break;
924                                 default:
925                                         extparam = this->GetModeParameter(n + 65);
926                                 break;
927                         }
928                         if (!extparam.empty())
929                         {
930                                 charlcat(sparam,' ',MAXBUF);
931                                 strlcat(sparam,extparam.c_str(),MAXBUF);
932                         }
933                 }
934         }
935
936         /* Null terminate scratch */
937         *offset = '\0';
938         strlcat(scratch,sparam,MAXBUF);
939         return scratch;
940 }
941
942 /* compile a userlist of a channel into a string, each nick seperated by
943  * spaces and op, voice etc status shown as @ and +, and send it to 'user'
944  */
945 void Channel::UserList(User *user, CUList *ulist)
946 {
947         char list[MAXBUF];
948         size_t dlen, curlen;
949         int MOD_RESULT = 0;
950         bool call_modules = true;
951
952         if (!IS_LOCAL(user))
953                 return;
954
955         FOREACH_RESULT(I_OnUserList,OnUserList(user, this, ulist));
956         if (MOD_RESULT == 1)
957                 call_modules = false;
958
959         if (MOD_RESULT != -1)
960         {
961                 if ((this->IsModeSet('s')) && (!this->HasUser(user)))
962                 {
963                         user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nick/channel",user->nick.c_str(), this->name.c_str());
964                         return;
965                 }
966         }
967
968         dlen = curlen = snprintf(list,MAXBUF,"%s %c %s :", user->nick.c_str(), this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=',  this->name.c_str());
969
970         int numusers = 0;
971         char* ptr = list + dlen;
972
973         if (!ulist)
974                 ulist = this->GetUsers();
975
976         /* Improvement by Brain - this doesnt change in value, so why was it inside
977          * the loop?
978          */
979         bool has_user = this->HasUser(user);
980
981         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
982         {
983                 if ((!has_user) && (i->first->IsModeSet('i')))
984                 {
985                         /*
986                          * user is +i, and source not on the channel, does not show
987                          * nick in NAMES list
988                          */
989                         continue;
990                 }
991
992                 if (i->first->Visibility && !i->first->Visibility->VisibleTo(user))
993                         continue;
994
995                 std::string prefixlist = this->GetPrefixChar(i->first);
996                 std::string nick = i->first->nick;
997
998                 if (call_modules)
999                 {
1000                         FOREACH_MOD(I_OnNamesListItem, OnNamesListItem(user, i->first, this, prefixlist, nick));
1001
1002                         /* Nick was nuked, a module wants us to skip it */
1003                         if (nick.empty())
1004                                 continue;
1005                 }
1006
1007                 size_t ptrlen = 0;
1008
1009                 if (curlen + prefixlist.length() + nick.length() + 1 > 480)
1010                 {
1011                         /* list overflowed into multiple numerics */
1012                         user->WriteNumeric(RPL_NAMREPLY, std::string(list));
1013
1014                         /* reset our lengths */
1015                         dlen = curlen = snprintf(list,MAXBUF,"%s %c %s :", user->nick.c_str(), this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=', this->name.c_str());
1016                         ptr = list + dlen;
1017
1018                         ptrlen = 0;
1019                         numusers = 0;
1020                 }
1021
1022                 ptrlen = snprintf(ptr, MAXBUF, "%s%s ", prefixlist.c_str(), nick.c_str());
1023
1024                 curlen += ptrlen;
1025                 ptr += ptrlen;
1026
1027                 numusers++;
1028         }
1029
1030         /* if whats left in the list isnt empty, send it */
1031         if (numusers)
1032         {
1033                 user->WriteNumeric(RPL_NAMREPLY, std::string(list));
1034         }
1035
1036         user->WriteNumeric(RPL_ENDOFNAMES, "%s %s :End of /NAMES list.", user->nick.c_str(), this->name.c_str());
1037 }
1038
1039 long Channel::GetMaxBans()
1040 {
1041         /* Return the cached value if there is one */
1042         if (this->maxbans)
1043                 return this->maxbans;
1044
1045         /* If there isnt one, we have to do some O(n) hax to find it the first time. (ick) */
1046         for (std::map<std::string,int>::iterator n = ServerInstance->Config->maxbans.begin(); n != ServerInstance->Config->maxbans.end(); n++)
1047         {
1048                 if (InspIRCd::Match(this->name, n->first, NULL))
1049                 {
1050                         this->maxbans = n->second;
1051                         return n->second;
1052                 }
1053         }
1054
1055         /* Screw it, just return the default of 64 */
1056         this->maxbans = 64;
1057         return this->maxbans;
1058 }
1059
1060 void Channel::ResetMaxBans()
1061 {
1062         this->maxbans = 0;
1063 }
1064
1065 /* returns the status character for a given user on a channel, e.g. @ for op,
1066  * % for halfop etc. If the user has several modes set, the highest mode
1067  * the user has must be returned.
1068  */
1069 const char* Channel::GetPrefixChar(User *user)
1070 {
1071         static char pf[2] = {0, 0};
1072
1073         prefixlist::iterator n = prefixes.find(user);
1074         if (n != prefixes.end())
1075         {
1076                 if (n->second.size())
1077                 {
1078                         /* If the user has any prefixes, their highest prefix
1079                          * will always be at the head of the list, as the list is
1080                          * sorted in rank order highest first (see SetPrefix()
1081                          * for reasons why)
1082                          */
1083                         *pf = n->second.begin()->first;
1084                         return pf;
1085                 }
1086         }
1087
1088         *pf = 0;
1089         return pf;
1090 }
1091
1092
1093 const char* Channel::GetAllPrefixChars(User* user)
1094 {
1095         static char prefix[MAXBUF];
1096         int ctr = 0;
1097         *prefix = 0;
1098
1099         prefixlist::iterator n = prefixes.find(user);
1100         if (n != prefixes.end())
1101         {
1102                 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
1103                 {
1104                         prefix[ctr++] = x->first;
1105                 }
1106         }
1107
1108         prefix[ctr] = 0;
1109
1110         return prefix;
1111 }
1112
1113 unsigned int Channel::GetPrefixValue(User* user)
1114 {
1115         prefixlist::iterator n = prefixes.find(user);
1116         if (n != prefixes.end())
1117         {
1118                 if (n->second.size())
1119                         return n->second.begin()->second;
1120         }
1121         return 0;
1122 }
1123
1124 int Channel::GetStatusFlags(User *user)
1125 {
1126         UCListIter i = user->chans.find(this);
1127         if (i != user->chans.end())
1128         {
1129                 return i->second;
1130         }
1131         return 0;
1132 }
1133
1134 int Channel::GetStatus(User *user)
1135 {
1136         if (ServerInstance->ULine(user->server))
1137                 return STATUS_OP;
1138
1139         UCListIter i = user->chans.find(this);
1140         if (i != user->chans.end())
1141         {
1142                 if ((i->second & UCMODE_OP) > 0)
1143                 {
1144                         return STATUS_OP;
1145                 }
1146                 if ((i->second & UCMODE_HOP) > 0)
1147                 {
1148                         return STATUS_HOP;
1149                 }
1150                 if ((i->second & UCMODE_VOICE) > 0)
1151                 {
1152                         return STATUS_VOICE;
1153                 }
1154                 return STATUS_NORMAL;
1155         }
1156         return STATUS_NORMAL;
1157 }
1158
1159 void Channel::SetPrefix(User* user, char prefix, unsigned int prefix_value, bool adding)
1160 {
1161         prefixlist::iterator n = prefixes.find(user);
1162         prefixtype pfx = std::make_pair(prefix,prefix_value);
1163         if (adding)
1164         {
1165                 if (n != prefixes.end())
1166                 {
1167                         if (std::find(n->second.begin(), n->second.end(), pfx) == n->second.end())
1168                         {
1169                                 n->second.push_back(pfx);
1170                                 /* We must keep prefixes in rank order, largest first.
1171                                  * This is for two reasons, firstly because x-chat *ass-u-me's* this
1172                                  * state, and secondly it turns out to be a benefit to us later.
1173                                  * See above in GetPrefix().
1174                                  */
1175                                 std::sort(n->second.begin(), n->second.end(), ModeParser::PrefixComparison);
1176                         }
1177                 }
1178                 else
1179                 {
1180                         pfxcontainer one;
1181                         one.push_back(pfx);
1182                         prefixes.insert(std::make_pair<User*,pfxcontainer>(user, one));
1183                 }
1184         }
1185         else
1186         {
1187                 if (n != prefixes.end())
1188                 {
1189                         pfxcontainer::iterator x = std::find(n->second.begin(), n->second.end(), pfx);
1190                         if (x != n->second.end())
1191                                 n->second.erase(x);
1192                 }
1193         }
1194 }
1195
1196 void Channel::RemoveAllPrefixes(User* user)
1197 {
1198         prefixlist::iterator n = prefixes.find(user);
1199         if (n != prefixes.end())
1200         {
1201                 prefixes.erase(n);
1202         }
1203 }