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