]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/channels.cpp
115afea52bea47a3098ae6c8682f06fb8a45f7fa
[user/henk/code/inspircd.git] / src / channels.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 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 &name, 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 " + name);
26
27         (*(ServerInstance->chanlist))[name.c_str()] = this;
28         strlcpy(this->name, name.c_str(), CHANMAX);
29         this->created = ts ? ts : ServerInstance->Time(true);
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                         Ptr->SetPrefix(user, status, mh->GetPrefixRank(), true);
383                         /* Make sure that the mode handler knows this mode was now set */
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
389                                  * because if the entry doesnt exist,
390                                  * addressing operator[] creates it.
391                                  * If they do exist, it points to it.
392                                  * At all other times where we dont want
393                                  * to create an item if it doesnt exist, we
394                                  * must stick to ::find().
395                                  */
396                                 case '@':
397                                         user->chans[Ptr] |= UCMODE_OP;
398                                 break;
399                                 case '%':
400                                         user->chans[Ptr] |= UCMODE_HOP;
401                                 break;
402                                 case '+':
403                                         user->chans[Ptr] |= UCMODE_VOICE;
404                                 break;
405                         }
406                 }
407         }
408
409         FOREACH_MOD_I(Instance,I_OnUserJoin,OnUserJoin(user, Ptr, bursting, silent));
410
411         if (!silent)
412                 Ptr->WriteChannel(user,"JOIN :%s",Ptr->name);
413
414         /* Theyre not the first ones in here, make sure everyone else sees the modes we gave the user */
415         std::string ms = Instance->Modes->ModeString(user, Ptr);
416         if ((Ptr->GetUserCounter() > 1) && (ms.length()))
417                 Ptr->WriteAllExceptSender(user, true, 0, "MODE %s +%s", Ptr->name, ms.c_str());
418
419         /* Major improvement by Brain - we dont need to be calculating all this pointlessly for remote users */
420         if (IS_LOCAL(user))
421         {
422                 if (Ptr->topicset)
423                 {
424                         user->WriteServ("332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
425                         user->WriteServ("333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
426                 }
427                 Ptr->UserList(user);
428         }
429         FOREACH_MOD_I(Instance,I_OnPostJoin,OnPostJoin(user, Ptr));
430         return Ptr;
431 }
432
433 bool Channel::IsBanned(User* user)
434 {
435         char mask[MAXBUF];
436         int MOD_RESULT = 0;
437         FOREACH_RESULT(I_OnCheckBan,OnCheckBan(user, this));
438         if (!MOD_RESULT)
439         {
440                 snprintf(mask, MAXBUF, "%s!%s@%s", user->nick, user->ident, user->GetIPString());
441                 for (BanList::iterator i = this->bans.begin(); i != this->bans.end(); i++)
442                 {
443                         /* This allows CIDR ban matching
444                          * 
445                          *        Full masked host                      Full unmasked host                   IP with/without CIDR
446                          */
447                         if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match(mask, i->data, true)))
448                         {
449                                 return true;
450                         }
451                 }
452         }
453         return false;
454 }
455
456 /* Channel::PartUser
457  * remove a channel from a users record, and return the number of users left.
458  * Therefore, if this function returns 0 the caller should delete the Channel.
459  */
460 long Channel::PartUser(User *user, const char* reason)
461 {
462         bool silent = false;
463
464         if (!user)
465                 return this->GetUserCounter();
466
467         UCListIter i = user->chans.find(this);
468         if (i != user->chans.end())
469         {
470                 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, reason ? reason : "", silent));
471
472                 if (!silent)
473                         this->WriteChannel(user, "PART %s%s%s", this->name, reason ? " :" : "", reason ? reason : "");
474
475                 user->chans.erase(i);
476                 this->RemoveAllPrefixes(user);
477         }
478
479         if (!this->DelUser(user)) /* if there are no users left on the channel... */
480         {
481                 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
482                 /* kill the record */
483                 if (iter != ServerInstance->chanlist->end())
484                 {
485                         FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
486                         ServerInstance->chanlist->erase(iter);
487                 }
488                 return 0;
489         }
490
491         return this->GetUserCounter();
492 }
493
494 long Channel::ServerKickUser(User* user, const char* reason, bool triggerevents)
495 {
496         bool silent = false;
497
498         if (!user || !reason)
499                 return this->GetUserCounter();
500
501         if (IS_LOCAL(user))
502         {
503                 if (!this->HasUser(user))
504                 {
505                         /* Not on channel */
506                         return this->GetUserCounter();
507                 }
508         }
509
510         if (triggerevents)
511         {
512                 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL, user, this, reason, silent));
513         }
514
515         UCListIter i = user->chans.find(this);
516         if (i != user->chans.end())
517         {
518                 if (!silent)
519                         this->WriteChannelWithServ(ServerInstance->Config->ServerName, "KICK %s %s :%s", this->name, user->nick, reason);
520
521                 user->chans.erase(i);
522                 this->RemoveAllPrefixes(user);
523         }
524
525         if (!this->DelUser(user))
526         {
527                 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
528                 /* kill the record */
529                 if (iter != ServerInstance->chanlist->end())
530                 {
531                         FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
532                         ServerInstance->chanlist->erase(iter);
533                 }
534                 return 0;
535         }
536
537         return this->GetUserCounter();
538 }
539
540 long Channel::KickUser(User *src, User *user, const char* reason)
541 {
542         bool silent = false;
543
544         if (!src || !user || !reason)
545                 return this->GetUserCounter();
546
547         if (IS_LOCAL(src))
548         {
549                 if (!this->HasUser(user))
550                 {
551                         src->WriteServ("441 %s %s %s :They are not on that channel",src->nick, user->nick, this->name);
552                         return this->GetUserCounter();
553                 }
554                 if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
555                 {
556                         src->WriteServ("482 %s %s :Only a u-line may kick a u-line from a channel.",src->nick, this->name);
557                         return this->GetUserCounter();
558                 }
559                 int MOD_RESULT = 0;
560
561                 if (!ServerInstance->ULine(src->server))
562                 {
563                         MOD_RESULT = 0;
564                         FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,this,reason));
565                         if (MOD_RESULT == 1)
566                                 return this->GetUserCounter();
567                 }
568                 /* Set to -1 by OnUserPreKick if explicit allow was set */
569                 if (MOD_RESULT != -1)
570                 {
571                         FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,this,AC_KICK));
572                         if ((MOD_RESULT == ACR_DENY) && (!ServerInstance->ULine(src->server)))
573                                 return this->GetUserCounter();
574         
575                         if ((MOD_RESULT == ACR_DEFAULT) || (!ServerInstance->ULine(src->server)))
576                         {
577                                 int them = this->GetStatus(src);
578                                 int us = this->GetStatus(user);
579                                 if ((them < STATUS_HOP) || (them < us))
580                                 {
581                                         src->WriteServ("482 %s %s :You must be a channel %soperator",src->nick, this->name, them == STATUS_HOP ? "" : "half-");
582                                         return this->GetUserCounter();
583                                 }
584                         }
585                 }
586         }
587
588         FOREACH_MOD(I_OnUserKick,OnUserKick(src, user, this, reason, silent));
589
590         UCListIter i = user->chans.find(this);
591         if (i != user->chans.end())
592         {
593                 /* zap it from the channel list of the user */
594                 if (!silent)
595                         this->WriteChannel(src, "KICK %s %s :%s", this->name, user->nick, reason);
596
597                 user->chans.erase(i);
598                 this->RemoveAllPrefixes(user);
599         }
600
601         if (!this->DelUser(user))
602         /* if there are no users left on the channel */
603         {
604                 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
605
606                 /* kill the record */
607                 if (iter != ServerInstance->chanlist->end())
608                 {
609                         FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
610                         ServerInstance->chanlist->erase(iter);
611                 }
612                 return 0;
613         }
614
615         return this->GetUserCounter();
616 }
617
618 void Channel::WriteChannel(User* user, char* text, ...)
619 {
620         char textbuffer[MAXBUF];
621         va_list argsPtr;
622
623         if (!user || !text)
624                 return;
625
626         va_start(argsPtr, text);
627         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
628         va_end(argsPtr);
629
630         this->WriteChannel(user, std::string(textbuffer));
631 }
632
633 void Channel::WriteChannel(User* user, const std::string &text)
634 {
635         CUList *ulist = this->GetUsers();
636         char tb[MAXBUF];
637
638         if (!user)
639                 return;
640
641         snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());
642         std::string out = tb;
643
644         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
645         {
646                 if (IS_LOCAL(i->first))
647                         i->first->Write(out);
648         }
649 }
650
651 void Channel::WriteChannelWithServ(const char* ServName, const char* text, ...)
652 {
653         char textbuffer[MAXBUF];
654         va_list argsPtr;
655
656         if (!text)
657                 return;
658
659         va_start(argsPtr, text);
660         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
661         va_end(argsPtr);
662
663         this->WriteChannelWithServ(ServName, std::string(textbuffer));
664 }
665
666 void Channel::WriteChannelWithServ(const char* ServName, const std::string &text)
667 {
668         CUList *ulist = this->GetUsers();
669         char tb[MAXBUF];
670
671         snprintf(tb,MAXBUF,":%s %s",ServName ? ServName : ServerInstance->Config->ServerName, text.c_str());
672         std::string out = tb;
673
674         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
675         {
676                 if (IS_LOCAL(i->first))
677                         i->first->Write(out);
678         }
679 }
680
681 /* write formatted text from a source user to all users on a channel except
682  * for the sender (for privmsg etc) */
683 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, char* text, ...)
684 {
685         char textbuffer[MAXBUF];
686         va_list argsPtr;
687
688         if (!text)
689                 return;
690
691         va_start(argsPtr, text);
692         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
693         va_end(argsPtr);
694
695         this->WriteAllExceptSender(user, serversource, status, std::string(textbuffer));
696 }
697
698 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, char* text, ...)
699 {
700         char textbuffer[MAXBUF];
701         va_list argsPtr;
702
703         if (!text)
704                 return;
705
706         va_start(argsPtr, text);
707         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
708         va_end(argsPtr);
709
710         this->WriteAllExcept(user, serversource, status, except_list, std::string(textbuffer));
711 }
712
713 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string &text)
714 {
715         CUList *ulist;
716         char tb[MAXBUF];
717
718         switch (status)
719         {
720                 case '@':
721                         ulist = this->GetOppedUsers();
722                         break;
723                 case '%':
724                         ulist = this->GetHalfoppedUsers();
725                         break;
726                 case '+':
727                         ulist = this->GetVoicedUsers();
728                         break;
729                 default:
730                         ulist = this->GetUsers();
731                         break;
732         }
733
734         snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());
735         std::string out = tb;
736
737         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
738         {
739                 if ((IS_LOCAL(i->first)) && (except_list.find(i->first) == except_list.end()))
740                 {
741                         if (serversource)
742                                 i->first->WriteServ(text);
743                         else
744                                 i->first->Write(out);
745                 }
746         }
747 }
748
749 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const std::string& text)
750 {
751         CUList except_list;
752         except_list[user] = user->nick;
753         this->WriteAllExcept(user, serversource, status, except_list, std::string(text));
754 }
755
756 /*
757  * return a count of the users on a specific channel accounting for
758  * invisible users who won't increase the count. e.g. for /LIST
759  */
760 int Channel::CountInvisible()
761 {
762         int count = 0;
763         CUList *ulist= this->GetUsers();
764         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
765         {
766                 if (!(i->first->IsModeSet('i')))
767                         count++;
768         }
769
770         return count;
771 }
772
773 char* Channel::ChanModes(bool showkey)
774 {
775         static char scratch[MAXBUF];
776         static char sparam[MAXBUF];
777         char* offset = scratch;
778         std::string extparam;
779
780         *scratch = '\0';
781         *sparam = '\0';
782
783         /* This was still iterating up to 190, Channel::modes is only 64 elements -- Om */
784         for(int n = 0; n < 64; n++)
785         {
786                 if(this->modes[n])
787                 {
788                         *offset++ = n + 65;
789                         extparam.clear();
790                         switch (n)
791                         {
792                                 case CM_KEY:
793                                         extparam = (showkey ? this->key : "<key>");
794                                 break;
795                                 case CM_LIMIT:
796                                         extparam = ConvToStr(this->limit);
797                                 break;
798                                 case CM_NOEXTERNAL:
799                                 case CM_TOPICLOCK:
800                                 case CM_INVITEONLY:
801                                 case CM_MODERATED:
802                                 case CM_SECRET:
803                                 case CM_PRIVATE:
804                                         /* We know these have no parameters */
805                                 break;
806                                 default:
807                                         extparam = this->GetModeParameter(n + 65);
808                                 break;
809                         }
810                         if (!extparam.empty())
811                         {
812                                 charlcat(sparam,' ',MAXBUF);
813                                 strlcat(sparam,extparam.c_str(),MAXBUF);
814                         }
815                 }
816         }
817
818         /* Null terminate scratch */
819         *offset = '\0';
820         strlcat(scratch,sparam,MAXBUF);
821         return scratch;
822 }
823
824 /* compile a userlist of a channel into a string, each nick seperated by
825  * spaces and op, voice etc status shown as @ and +, and send it to 'user'
826  */
827 void Channel::UserList(User *user, CUList *ulist)
828 {
829         char list[MAXBUF];
830         size_t dlen, curlen;
831         int MOD_RESULT = 0;
832
833         if (!IS_LOCAL(user))
834                 return;
835
836         FOREACH_RESULT(I_OnUserList,OnUserList(user, this, ulist));
837         if (MOD_RESULT == 1)
838                 return;
839         if (MOD_RESULT != -1)
840         {
841                 if ((this->IsModeSet('s')) && (!this->HasUser(user)))
842                 {
843                         user->WriteServ("401 %s %s :No such nick/channel",user->nick, this->name);
844                         return;
845                 }
846         }
847
848         dlen = curlen = snprintf(list,MAXBUF,"353 %s %c %s :", user->nick, this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=',  this->name);
849
850         int numusers = 0;
851         char* ptr = list + dlen;
852
853         if (!ulist)
854                 ulist = this->GetUsers();
855
856         /* Improvement by Brain - this doesnt change in value, so why was it inside
857          * the loop?
858          */
859         bool has_user = this->HasUser(user);
860
861         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
862         {
863                 if ((!has_user) && (i->first->IsModeSet('i')))
864                 {
865                         /*
866                          * user is +i, and source not on the channel, does not show
867                          * nick in NAMES list
868                          */
869                         continue;
870                 }
871
872                 if (i->first->Visibility && !i->first->Visibility->VisibleTo(user))
873                         continue;
874
875                 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", this->GetPrefixChar(i->first), i->second.c_str());
876                 /* OnUserList can change this - reset it back to normal */
877                 i->second = i->first->nick;
878
879                 curlen += ptrlen;
880                 ptr += ptrlen;
881
882                 numusers++;
883
884                 if (curlen > (480-NICKMAX))
885                 {
886                         /* list overflowed into multiple numerics */
887                         user->WriteServ(std::string(list));
888
889                         /* reset our lengths */
890                         dlen = curlen = snprintf(list,MAXBUF,"353 %s %c %s :", user->nick, this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=', this->name);
891                         ptr = list + dlen;
892
893                         ptrlen = 0;
894                         numusers = 0;
895                 }
896         }
897
898         /* if whats left in the list isnt empty, send it */
899         if (numusers)
900         {
901                 user->WriteServ(std::string(list));
902         }
903
904         user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, this->name);
905 }
906
907 long Channel::GetMaxBans()
908 {
909         /* Return the cached value if there is one */
910         if (this->maxbans)
911                 return this->maxbans;
912
913         /* If there isnt one, we have to do some O(n) hax to find it the first time. (ick) */
914         for (std::map<std::string,int>::iterator n = ServerInstance->Config->maxbans.begin(); n != ServerInstance->Config->maxbans.end(); n++)
915         {
916                 if (match(this->name,n->first.c_str()))
917                 {
918                         this->maxbans = n->second;
919                         return n->second;
920                 }
921         }
922
923         /* Screw it, just return the default of 64 */
924         this->maxbans = 64;
925         return this->maxbans;
926 }
927
928 void Channel::ResetMaxBans()
929 {
930         this->maxbans = 0;
931 }
932
933 /* returns the status character for a given user on a channel, e.g. @ for op,
934  * % for halfop etc. If the user has several modes set, the highest mode
935  * the user has must be returned.
936  */
937 const char* Channel::GetPrefixChar(User *user)
938 {
939         static char pf[2] = {0, 0};
940         
941         prefixlist::iterator n = prefixes.find(user);
942         if (n != prefixes.end())
943         {
944                 if (n->second.size())
945                 {
946                         /* If the user has any prefixes, their highest prefix
947                          * will always be at the head of the list, as the list is
948                          * sorted in rank order highest first (see SetPrefix()
949                          * for reasons why)
950                          */
951                         *pf = n->second.begin()->first;
952                         return pf;
953                 }
954         }
955
956         *pf = 0;
957         return pf;
958 }
959
960 const char* Channel::GetAllPrefixChars(User* user)
961 {
962         static char prefix[MAXBUF];
963         int ctr = 0;
964         *prefix = 0;
965
966         prefixlist::iterator n = prefixes.find(user);
967         if (n != prefixes.end())
968         {
969                 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
970                 {
971                         prefix[ctr++] = x->first;
972                 }
973         }
974
975         prefix[ctr] = 0;
976
977         return prefix;
978 }
979
980 unsigned int Channel::GetPrefixValue(User* user)
981 {
982         prefixlist::iterator n = prefixes.find(user);
983         if (n != prefixes.end())
984         {
985                 if (n->second.size())
986                         return n->second.begin()->second;
987         }
988         return 0;
989 }
990
991 int Channel::GetStatusFlags(User *user)
992 {
993         UCListIter i = user->chans.find(this);
994         if (i != user->chans.end())
995         {
996                 return i->second;
997         }
998         return 0;
999 }
1000
1001 int Channel::GetStatus(User *user)
1002 {
1003         if (ServerInstance->ULine(user->server))
1004                 return STATUS_OP;
1005
1006         UCListIter i = user->chans.find(this);
1007         if (i != user->chans.end())
1008         {
1009                 if ((i->second & UCMODE_OP) > 0)
1010                 {
1011                         return STATUS_OP;
1012                 }
1013                 if ((i->second & UCMODE_HOP) > 0)
1014                 {
1015                         return STATUS_HOP;
1016                 }
1017                 if ((i->second & UCMODE_VOICE) > 0)
1018                 {
1019                         return STATUS_VOICE;
1020                 }
1021                 return STATUS_NORMAL;
1022         }
1023         return STATUS_NORMAL;
1024 }
1025
1026 void Channel::SetPrefix(User* user, char prefix, unsigned int prefix_value, bool adding)
1027 {
1028         prefixlist::iterator n = prefixes.find(user);
1029         prefixtype pfx = std::make_pair(prefix,prefix_value);
1030         if (adding)
1031         {
1032                 if (n != prefixes.end())
1033                 {
1034                         if (std::find(n->second.begin(), n->second.end(), pfx) == n->second.end())
1035                         {
1036                                 n->second.push_back(pfx);
1037                                 /* We must keep prefixes in rank order, largest first.
1038                                  * This is for two reasons, firstly because x-chat *ass-u-me's* this
1039                                  * state, and secondly it turns out to be a benefit to us later.
1040                                  * See above in GetPrefix().
1041                                  */
1042                                 std::sort(n->second.begin(), n->second.end(), ModeParser::PrefixComparison);
1043                         }
1044                 }
1045                 else
1046                 {
1047                         pfxcontainer one;
1048                         one.push_back(pfx);
1049                         prefixes.insert(std::make_pair<User*,pfxcontainer>(user, one));
1050                 }
1051         }
1052         else
1053         {
1054                 if (n != prefixes.end())
1055                 {
1056                         pfxcontainer::iterator x = std::find(n->second.begin(), n->second.end(), pfx);
1057                         if (x != n->second.end())
1058                                 n->second.erase(x);
1059                 }
1060         }
1061 }
1062
1063 void Channel::RemoveAllPrefixes(User* user)
1064 {
1065         prefixlist::iterator n = prefixes.find(user);
1066         if (n != prefixes.end())
1067         {
1068                 prefixes.erase(n);
1069         }
1070 }
1071