]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/channels.cpp
Explicitly call the std::string version of WriteServ in chanrec::UserList to prevent...
[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                                                 if (Ptr->IsBanned(user))
322                                                 {
323                                                         user->WriteServ("474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
324                                                         return NULL;
325                                                 }
326                                         }
327                                 }
328                         }
329                 }
330                 else
331                 {
332                         Instance->Log(DEBUG,"chanrec::JoinUser(): Overridden checks");
333                 }
334                 created = 1;
335         }
336
337         for (UserChanList::const_iterator index = user->chans.begin(); index != user->chans.end(); index++)
338         {
339                 if ((*index)->channel == NULL)
340                 {
341                         return chanrec::ForceChan(Instance, Ptr, *index, user, created);
342                 }
343         }
344
345         /*
346          * XXX: If the user is an oper here, we can just extend their user->chans vector by one
347          * and put the channel in here. Same for remote users which are not bound by
348          * the channel limits. Otherwise, nope, youre boned.
349          */
350         if (!IS_LOCAL(user)) /* was a check on fd < 0 */
351         {
352                 ucrec* a = new ucrec();
353                 chanrec* c = chanrec::ForceChan(Instance, Ptr,a,user,created);
354                 user->chans.push_back(a);
355                 return c;
356         }
357         else if (*user->oper)
358         {
359                 /* Oper allows extension up to the OPERMAXCHANS value */
360                 if (user->chans.size() < OPERMAXCHANS)
361                 {
362                         ucrec* a = new ucrec();
363                         chanrec* c = chanrec::ForceChan(Instance, Ptr,a,user,created);
364                         user->chans.push_back(a);
365                         return c;
366                 }
367         }
368
369         user->WriteServ("405 %s %s :You are on too many channels",user->nick, cname);
370
371         if (created == 2)
372         {
373                 Instance->Log(DEBUG,"BLAMMO, Whacking channel.");
374                 /* Things went seriously pear shaped, so take this away. bwahaha. */
375                 chan_hash::iterator n = Instance->chanlist.find(cname);
376                 if (n != Instance->chanlist.end())
377                 {
378                         Ptr->DelUser(user);
379                         DELETE(Ptr);
380                         Instance->chanlist.erase(n);
381                         for (unsigned int index =0; index < user->chans.size(); index++)
382                         {
383                                 if (user->chans[index]->channel == Ptr)
384                                 {
385                                         user->chans[index]->channel = NULL;
386                                         user->chans[index]->uc_modes = 0;       
387                                 }
388                         }
389                 }
390         }
391         else
392         {
393                 for (unsigned int index =0; index < user->chans.size(); index++)
394                 {
395                         if (user->chans[index]->channel == Ptr)
396                         {
397                                 user->chans[index]->channel = NULL;
398                                 user->chans[index]->uc_modes = 0;
399                         }
400                 }
401         }
402         return NULL;
403 }
404
405 chanrec* chanrec::ForceChan(InspIRCd* Instance, chanrec* Ptr,ucrec *a,userrec* user, int created)
406 {
407         if (created == 2)
408         {
409                 /* first user in is given ops */
410                 a->uc_modes = UCMODE_OP;
411                 Ptr->AddOppedUser(user);
412                 Ptr->SetPrefix(user, '@', OP_VALUE, true);
413         }
414         else
415         {
416                 a->uc_modes = 0;
417         }
418
419         a->channel = Ptr;
420         Ptr->AddUser(user);
421         user->ModChannelCount(1);
422         Ptr->WriteChannel(user,"JOIN :%s",Ptr->name);
423
424         /* Major improvement by Brain - we dont need to be calculating all this pointlessly for remote users */
425         if (IS_LOCAL(user))
426         {
427                 if (Ptr->topicset)
428                 {
429                         user->WriteServ("332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
430                         user->WriteServ("333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
431                 }
432                 Ptr->UserList(user);
433         }
434         FOREACH_MOD_I(Instance,I_OnUserJoin,OnUserJoin(user,Ptr));
435         return Ptr;
436 }
437
438 bool chanrec::IsBanned(userrec* user)
439 {
440         char mask[MAXBUF];
441         sprintf(mask,"%s!%s@%s",user->nick, user->ident, user->GetIPString());
442         for (BanList::iterator i = this->bans.begin(); i != this->bans.end(); i++)
443         {
444                 /* This allows CIDR ban matching
445                  * 
446                  *        Full masked host                      Full unmasked host                   IP with/without CIDR
447                  */
448                 if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match(mask, i->data, true)))
449                 {
450                         return true;
451                 }
452
453         }
454         return false;
455 }
456
457 /* chanrec::PartUser
458  * remove a channel from a users record, and remove the record from the hash
459  * if the channel has become empty
460  */
461 long chanrec::PartUser(userrec *user, const char* reason)
462 {
463         if (!user)
464                 return this->GetUserCounter();
465
466         for (unsigned int i =0; i < user->chans.size(); i++)
467         {
468                 /* zap it from the channel list of the user */
469                 if (user->chans[i]->channel == this)
470                 {
471                         if (reason)
472                         {
473                                 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, reason));
474                                 this->WriteChannel(user, "PART %s :%s", this->name, reason);
475                         }
476                         else
477                         {
478                                 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, ""));
479                                 this->WriteChannel(user, "PART :%s", this->name);
480                         }
481                         user->chans[i]->uc_modes = 0;
482                         user->chans[i]->channel = NULL;
483                         user->ModChannelCount(-1);
484                         this->RemoveAllPrefixes(user);
485                         break;
486                 }
487         }
488
489         if (!this->DelUser(user)) /* if there are no users left on the channel... */
490         {
491                 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
492                 /* kill the record */
493                 if (iter != ServerInstance->chanlist.end())
494                 {
495                         ServerInstance->Log(DEBUG,"del_channel: destroyed: %s", this->name);
496                         FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
497                         ServerInstance->chanlist.erase(iter);
498                 }
499                 return 0;
500         }
501
502         return this->GetUserCounter();
503 }
504
505 long chanrec::ServerKickUser(userrec* user, const char* reason, bool triggerevents)
506 {
507         if (!user || !reason)
508                 return this->GetUserCounter();
509
510         if (IS_LOCAL(user))
511         {
512                 if (!this->HasUser(user))
513                 {
514                         /* Not on channel */
515                         return this->GetUserCounter();
516                 }
517         }
518
519         if (triggerevents)
520         {
521                 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL,user,this,reason));
522         }
523
524         for (unsigned int i =0; i < user->chans.size(); i++)
525         {
526                 if (user->chans[i]->channel == this)
527                 {
528                         this->WriteChannelWithServ(ServerInstance->Config->ServerName, "KICK %s %s :%s", this->name, user->nick, reason);
529                         user->chans[i]->uc_modes = 0;
530                         user->chans[i]->channel = NULL;
531                         this->RemoveAllPrefixes(user);
532                         break;
533                 }
534         }
535
536         if (!this->DelUser(user))
537         {
538                 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
539                 /* kill the record */
540                 if (iter != ServerInstance->chanlist.end())
541                 {
542                         FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
543                         ServerInstance->chanlist.erase(iter);
544                 }
545                 return 0;
546         }
547
548         return this->GetUserCounter();
549 }
550
551 long chanrec::KickUser(userrec *src, userrec *user, const char* reason)
552 {
553         if (!src || !user || !reason)
554                 return this->GetUserCounter();
555
556         if (IS_LOCAL(src))
557         {
558                 if (!this->HasUser(user))
559                 {
560                         src->WriteServ("441 %s %s %s :They are not on that channel",src->nick, user->nick, this->name);
561                         return this->GetUserCounter();
562                 }
563                 if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
564                 {
565                         src->WriteServ("482 %s %s :Only a u-line may kick a u-line from a channel.",src->nick, this->name);
566                         return this->GetUserCounter();
567                 }
568                 int MOD_RESULT = 0;
569
570                 if (!ServerInstance->ULine(src->server))
571                 {
572                         MOD_RESULT = 0;
573                         FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,this,reason));
574                         if (MOD_RESULT == 1)
575                                 return this->GetUserCounter();
576                 }
577                 /* Set to -1 by OnUserPreKick if explicit allow was set */
578                 if (MOD_RESULT != -1)
579                 {
580                         FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,this,AC_KICK));
581                         if ((MOD_RESULT == ACR_DENY) && (!ServerInstance->ULine(src->server)))
582                                 return this->GetUserCounter();
583         
584                         if ((MOD_RESULT == ACR_DEFAULT) || (!ServerInstance->ULine(src->server)))
585                         {
586                                 int them = this->GetStatus(src);
587                                 int us = this->GetStatus(user);
588                                 if ((them < STATUS_HOP) || (them < us))
589                                 {
590                                         if (them == STATUS_HOP)
591                                         {
592                                                 src->WriteServ("482 %s %s :You must be a channel operator",src->nick, this->name);
593                                         }
594                                         else
595                                         {
596                                                 src->WriteServ("482 %s %s :You must be at least a half-operator",src->nick, this->name);
597                                         }
598                                         return this->GetUserCounter();
599                                 }
600                         }
601                 }
602         }
603
604         FOREACH_MOD(I_OnUserKick,OnUserKick(src,user,this,reason));
605                         
606         for (UserChanList::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
607         {
608                 /* zap it from the channel list of the user */
609                 if ((*i)->channel == this)
610                 {
611                         this->WriteChannel(src, "KICK %s %s :%s", this->name, user->nick, reason);
612                         (*i)->uc_modes = 0;
613                         (*i)->channel = NULL;
614                         this->RemoveAllPrefixes(user);
615                         break;
616                 }
617         }
618
619         if (!this->DelUser(user))
620         /* if there are no users left on the channel */
621         {
622                 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
623
624                 /* kill the record */
625                 if (iter != ServerInstance->chanlist.end())
626                 {
627                         FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
628                         ServerInstance->chanlist.erase(iter);
629                 }
630                 return 0;
631         }
632
633         return this->GetUserCounter();
634 }
635
636 void chanrec::WriteChannel(userrec* user, char* text, ...)
637 {
638         char textbuffer[MAXBUF];
639         va_list argsPtr;
640
641         if (!user || !text)
642                 return;
643
644         va_start(argsPtr, text);
645         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
646         va_end(argsPtr);
647
648         this->WriteChannel(user, std::string(textbuffer));
649 }
650
651 void chanrec::WriteChannel(userrec* user, const std::string &text)
652 {
653         CUList *ulist = this->GetUsers();
654
655         if (!user)
656                 return;
657
658         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
659         {
660                 if (IS_LOCAL(i->second))
661                         user->WriteTo(i->second,text);
662         }
663 }
664
665 void chanrec::WriteChannelWithServ(const char* ServName, const char* text, ...)
666 {
667         char textbuffer[MAXBUF];
668         va_list argsPtr;
669
670         if (!text)
671                 return;
672
673         va_start(argsPtr, text);
674         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
675         va_end(argsPtr);
676
677         this->WriteChannelWithServ(ServName, std::string(textbuffer));
678 }
679
680 void chanrec::WriteChannelWithServ(const char* ServName, const std::string &text)
681 {
682         CUList *ulist = this->GetUsers();
683
684         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
685         {
686                 if (IS_LOCAL(i->second))
687                         i->second->WriteServ(text);
688         }
689 }
690
691 /* write formatted text from a source user to all users on a channel except
692  * for the sender (for privmsg etc) */
693 void chanrec::WriteAllExceptSender(userrec* user, char status, char* text, ...)
694 {
695         char textbuffer[MAXBUF];
696         va_list argsPtr;
697
698         if (!user || !text)
699                 return;
700
701         va_start(argsPtr, text);
702         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
703         va_end(argsPtr);
704
705         this->WriteAllExceptSender(user, status, std::string(textbuffer));
706 }
707
708 void chanrec::WriteAllExceptSender(userrec* user, char status, const std::string& text)
709 {
710         CUList *ulist;
711
712         if (!user)
713                 return;
714
715         switch (status)
716         {
717                 case '@':
718                         ulist = this->GetOppedUsers();
719                         break;
720                 case '%':
721                         ulist = this->GetHalfoppedUsers();
722                         break;
723                 case '+':
724                         ulist = this->GetVoicedUsers();
725                         break;
726                 default:
727                         ulist = this->GetUsers();
728                         break;
729         }
730
731         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
732         {
733                 if ((IS_LOCAL(i->second)) && (user != i->second))
734                         i->second->WriteFrom(user,text);
735         }
736 }
737
738 /*
739  * return a count of the users on a specific channel accounting for
740  * invisible users who won't increase the count. e.g. for /LIST
741  */
742 int chanrec::CountInvisible()
743 {
744         int count = 0;
745         CUList *ulist= this->GetUsers();
746         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
747         {
748                 if (!(i->second->modes[UM_INVISIBLE]))
749                         count++;
750         }
751
752         return count;
753 }
754
755 char* chanrec::ChanModes(bool showkey)
756 {
757         static char scratch[MAXBUF];
758         static char sparam[MAXBUF];
759         char* offset = scratch;
760         std::string extparam = "";
761
762         *scratch = '\0';
763         *sparam = '\0';
764
765         /* This was still iterating up to 190, chanrec::custom_modes is only 64 elements -- Om */
766         for(int n = 0; n < 64; n++)
767         {
768                 if(this->modes[n])
769                 {
770                         *offset++ = n + 65;
771                         extparam = "";
772                         switch (n)
773                         {
774                                 case CM_KEY:
775                                         extparam = (showkey ? this->key : "<key>");
776                                 break;
777                                 case CM_LIMIT:
778                                         extparam = ConvToStr(this->limit);
779                                 break;
780                                 case CM_NOEXTERNAL:
781                                 case CM_TOPICLOCK:
782                                 case CM_INVITEONLY:
783                                 case CM_MODERATED:
784                                 case CM_SECRET:
785                                 case CM_PRIVATE:
786                                         /* We know these have no parameters */
787                                 break;
788                                 default:
789                                         extparam = this->GetModeParameter(n + 65);
790                                 break;
791                         }
792                         if (extparam != "")
793                         {
794                                 charlcat(sparam,' ',MAXBUF);
795                                 strlcat(sparam,extparam.c_str(),MAXBUF);
796                         }
797                 }
798         }
799
800         /* Null terminate scratch */
801         *offset = '\0';
802         strlcat(scratch,sparam,MAXBUF);
803         return scratch;
804 }
805
806 /* compile a userlist of a channel into a string, each nick seperated by
807  * spaces and op, voice etc status shown as @ and +, and send it to 'user'
808  */
809 void chanrec::UserList(userrec *user)
810 {
811         char list[MAXBUF];
812         size_t dlen, curlen;
813         int MOD_RESULT = 0;
814
815         FOREACH_RESULT(I_OnUserList,OnUserList(user, this));
816         ServerInstance->Log(DEBUG,"MOD_RESULT for UserList = %d",MOD_RESULT);
817         if (MOD_RESULT == 1)
818                 return;
819
820         ServerInstance->Log(DEBUG,"Using builtin NAMES list generation");
821
822         dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
823
824         int numusers = 0;
825         char* ptr = list + dlen;
826
827         CUList *ulist= this->GetUsers();
828
829         /* Improvement by Brain - this doesnt change in value, so why was it inside
830          * the loop?
831          */
832         bool has_user = this->HasUser(user);
833
834         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
835         {
836                 if ((!has_user) && (i->second->modes[UM_INVISIBLE]))
837                 {
838                         /*
839                          * user is +i, and source not on the channel, does not show
840                          * nick in NAMES list
841                          */
842                         continue;
843                 }
844
845                 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", this->GetPrefixChar(i->second), i->second->nick);
846
847                 curlen += ptrlen;
848                 ptr += ptrlen;
849
850                 numusers++;
851
852                 if (curlen > (480-NICKMAX))
853                 {
854                         /* list overflowed into multiple numerics */
855                         user->WriteServ(std::string(list));
856
857                         /* reset our lengths */
858                         dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
859                         ptr = list + dlen;
860
861                         ptrlen = 0;
862                         numusers = 0;
863                 }
864         }
865
866         /* if whats left in the list isnt empty, send it */
867         if (numusers)
868         {
869                 user->WriteServ(std::string(list));
870         }
871
872         user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, this->name);
873 }
874
875 long chanrec::GetMaxBans()
876 {
877         std::string x;
878         for (std::map<std::string,int>::iterator n = ServerInstance->Config->maxbans.begin(); n != ServerInstance->Config->maxbans.end(); n++)
879         {
880                 x = n->first;
881                 if (match(this->name,x.c_str()))
882                 {
883                         return n->second;
884                 }
885         }
886         return 64;
887 }
888
889
890 /* returns the status character for a given user on a channel, e.g. @ for op,
891  * % for halfop etc. If the user has several modes set, the highest mode
892  * the user has must be returned.
893  */
894 const char* chanrec::GetPrefixChar(userrec *user)
895 {
896         static char px[2];
897         unsigned int mx = 0;
898
899         *px = 0;
900         *(px+1) = 0;
901
902         prefixlist::iterator n = prefixes.find(user);
903         if (n != prefixes.end())
904         {
905                 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
906                 {
907                         if (x->second > mx)
908                         {
909                                 *px = x->first;
910                                 mx  = x->second;
911                         }
912                 }
913         }
914
915         return px;
916 }
917
918 const char* chanrec::GetAllPrefixChars(userrec* user)
919 {
920         static char prefix[MAXBUF];
921         int ctr = 0;
922         *prefix = 0;
923
924         prefixlist::iterator n = prefixes.find(user);
925         if (n != prefixes.end())
926         {
927                 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
928                 {
929                         prefix[ctr++] = x->first;
930                 }
931         }
932
933         prefix[ctr] = 0;
934
935         return prefix;
936 }
937
938 unsigned int chanrec::GetPrefixValue(userrec* user)
939 {
940         unsigned int mx = 0;
941
942         prefixlist::iterator n = prefixes.find(user);
943         if (n != prefixes.end())
944         {
945                 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
946                 {
947                         if (x->second > mx)
948                                 mx  = x->second;
949                 }
950         }
951
952         return mx;
953 }
954
955
956 int chanrec::GetStatusFlags(userrec *user)
957 {
958         for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
959         {
960                 if ((*i)->channel == this)
961                 {
962                         return (*i)->uc_modes;
963                 }
964         }
965         return 0;
966 }
967
968
969 int chanrec::GetStatus(userrec *user)
970 {
971         if (ServerInstance->ULine(user->server))
972                 return STATUS_OP;
973
974         for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
975         {
976                 if ((*i)->channel == this)
977                 {
978                         if (((*i)->uc_modes & UCMODE_OP) > 0)
979                         {
980                                 return STATUS_OP;
981                         }
982                         if (((*i)->uc_modes & UCMODE_HOP) > 0)
983                         {
984                                 return STATUS_HOP;
985                         }
986                         if (((*i)->uc_modes & UCMODE_VOICE) > 0)
987                         {
988                                 return STATUS_VOICE;
989                         }
990                         return STATUS_NORMAL;
991                 }
992         }
993         return STATUS_NORMAL;
994 }
995
996 /*bool ModeParser::PrefixComparison(const prefixtype one, const prefixtype two)
997 {       
998         return one.second > two.second;
999 }*/
1000
1001 void chanrec::SetPrefix(userrec* user, char prefix, unsigned int prefix_value, bool adding)
1002 {
1003         prefixlist::iterator n = prefixes.find(user);
1004         prefixtype pfx = std::make_pair(prefix,prefix_value);
1005         if (adding)
1006         {
1007                 if (n != prefixes.end())
1008                 {
1009                         if (std::find(n->second.begin(), n->second.end(), pfx) == n->second.end())
1010                         {
1011                                 n->second.push_back(pfx);
1012                                 std::sort(n->second.begin(), n->second.end(), ModeParser::PrefixComparison);
1013                         }
1014                 }
1015                 else
1016                 {
1017                         pfxcontainer one;
1018                         one.push_back(pfx);
1019                         prefixes.insert(std::make_pair<userrec*,pfxcontainer>(user, one));
1020                 }
1021         }
1022         else
1023         {
1024                 if (n != prefixes.end())
1025                 {
1026                         pfxcontainer::iterator x = std::find(n->second.begin(), n->second.end(), pfx);
1027                         if (x != n->second.end())
1028                                 n->second.erase(x);
1029                 }
1030         }
1031 }
1032
1033 void chanrec::RemoveAllPrefixes(userrec* user)
1034 {
1035         prefixlist::iterator n = prefixes.find(user);
1036         if (n != prefixes.end())
1037                 prefixes.erase(n);
1038 }
1039