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