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