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