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