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