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