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