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