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