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