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