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