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