]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/channels.cpp
Cleaning up irrelevent stuff in channels.cpp
[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         memset(&modes,0,64);
32         age = ServerInstance->Time(true);
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) && override == false)
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
229                 /* As spotted by jilles, dont bother to set this on remote users */
230                 if (IS_LOCAL(user))
231                         Ptr->modes[CM_TOPICLOCK] = Ptr->modes[CM_NOEXTERNAL] = 1;
232
233                 Ptr->created = Instance->Time();
234                 *Ptr->topic = 0;
235                 *Ptr->setby = 0;
236                 Ptr->topicset = 0;
237                 Instance->Log(DEBUG,"chanrec::JoinUser(): created: %s",cname);
238                 new_channel = true;
239         }
240         else
241         {
242                 /* Already on the channel */
243                 if (Ptr->HasUser(user))
244                         return NULL;
245
246                 /*
247                  * remote users are allowed us to bypass channel modes
248                  * and bans (used by servers)
249                  */
250                 if (IS_LOCAL(user) && override == false)
251                 {
252                         MOD_RESULT = 0;
253                         FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname,privs));
254                         if (MOD_RESULT == 1)
255                         {
256                                 return NULL;
257                         }
258                         else if (MOD_RESULT == 0)
259                         {
260                                 if (*Ptr->key)
261                                 {
262                                         MOD_RESULT = 0;
263                                         FOREACH_RESULT_I(Instance,I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
264                                         if (!MOD_RESULT)
265                                         {
266                                                 if ((!key) || strcmp(key,Ptr->key))
267                                                 {
268                                                         user->WriteServ("475 %s %s :Cannot join channel (Incorrect channel key)",user->nick, Ptr->name);
269                                                         return NULL;
270                                                 }
271                                         }
272                                 }
273                                 if (Ptr->modes[CM_INVITEONLY])
274                                 {
275                                         MOD_RESULT = 0;
276                                         FOREACH_RESULT_I(Instance,I_OnCheckInvite,OnCheckInvite(user, Ptr));
277                                         if (!MOD_RESULT)
278                                         {
279                                                 if (user->IsInvited(Ptr->name))
280                                                 {
281                                                         /* user was invited to channel */
282                                                         /* there may be an optional channel NOTICE here */
283                                                 }
284                                                 else
285                                                 {
286                                                         user->WriteServ("473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
287                                                         return NULL;
288                                                 }
289                                         }
290                                         user->RemoveInvite(Ptr->name);
291                                 }
292                                 if (Ptr->limit)
293                                 {
294                                         MOD_RESULT = 0;
295                                         FOREACH_RESULT_I(Instance,I_OnCheckLimit,OnCheckLimit(user, Ptr));
296                                         if (!MOD_RESULT)
297                                         {
298                                                 if (Ptr->GetUserCounter() >= Ptr->limit)
299                                                 {
300                                                         user->WriteServ("471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
301                                                         return NULL;
302                                                 }
303                                         }
304                                 }
305                                 if (Ptr->bans.size())
306                                 {
307                                         if (Ptr->IsBanned(user))
308                                         {
309                                                 user->WriteServ("474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
310                                                 return NULL;
311                                         }
312                                 }
313                         }
314                 }
315                 else
316                 {
317                         Instance->Log(DEBUG,"chanrec::JoinUser(): Overridden checks");
318                 }
319         }
320
321         /* NOTE: If the user is an oper here, we can extend their user->chans by up to
322          * OPERMAXCHANS. For remote users which are not bound by the channel limits,
323          * we can extend infinitely. Otherwise, nope, youre restricted to MAXCHANS.
324          */
325         if (!IS_LOCAL(user) || override == true) /* was a check on fd < 0 */
326         {
327                 return chanrec::ForceChan(Instance, Ptr, user, privs);
328         }
329         else if (*user->oper)
330         {
331                 /* Oper allows extension up to the OPERMAXCHANS value */
332                 if (user->chans.size() < OPERMAXCHANS)
333                 {
334                         return chanrec::ForceChan(Instance, Ptr, user, privs);
335                 }
336         }
337         else if (user->chans.size() < MAXCHANS)
338         {
339                 return chanrec::ForceChan(Instance, Ptr, user, privs);
340         }
341
342
343         user->WriteServ("405 %s %s :You are on too many channels",user->nick, cname);
344
345         if (new_channel)
346         {
347                 Instance->Log(DEBUG,"BLAMMO, Whacking channel.");
348                 /* Things went seriously pear shaped, so take this away. bwahaha. */
349                 chan_hash::iterator n = Instance->chanlist.find(cname);
350                 if (n != Instance->chanlist.end())
351                 {
352                         Ptr->DelUser(user);
353                         DELETE(Ptr);
354                         Instance->chanlist.erase(n);
355                 }
356         }
357
358         return NULL;
359 }
360
361 chanrec* chanrec::ForceChan(InspIRCd* Instance, chanrec* Ptr, userrec* user, const std::string &privs)
362 {
363         userrec* dummyuser = new userrec(Instance);
364         std::string nick = user->nick;
365
366         dummyuser->SetFd(FD_MAGIC_NUMBER);
367         Ptr->AddUser(user);
368
369         /* Just in case they have no permissions */
370         user->chans[Ptr] = 0;
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                 this->RemoveAllPrefixes(user);
468         }
469
470         if (!this->DelUser(user)) /* if there are no users left on the channel... */
471         {
472                 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
473                 /* kill the record */
474                 if (iter != ServerInstance->chanlist.end())
475                 {
476                         ServerInstance->Log(DEBUG,"del_channel: destroyed: %s", this->name);
477                         FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
478                         ServerInstance->chanlist.erase(iter);
479                 }
480                 return 0;
481         }
482
483         return this->GetUserCounter();
484 }
485
486 long chanrec::ServerKickUser(userrec* user, const char* reason, bool triggerevents)
487 {
488         if (!user || !reason)
489                 return this->GetUserCounter();
490
491         if (IS_LOCAL(user))
492         {
493                 if (!this->HasUser(user))
494                 {
495                         /* Not on channel */
496                         return this->GetUserCounter();
497                 }
498         }
499
500         if (triggerevents)
501         {
502                 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL,user,this,reason));
503         }
504
505         UCListIter i = user->chans.find(this);
506         if (i != user->chans.end())
507         {
508                 this->WriteChannelWithServ(ServerInstance->Config->ServerName, "KICK %s %s :%s", this->name, user->nick, reason);
509                 user->chans.erase(i);
510                 this->RemoveAllPrefixes(user);
511         }
512
513         if (!this->DelUser(user))
514         {
515                 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
516                 /* kill the record */
517                 if (iter != ServerInstance->chanlist.end())
518                 {
519                         FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
520                         ServerInstance->chanlist.erase(iter);
521                 }
522                 return 0;
523         }
524
525         return this->GetUserCounter();
526 }
527
528 long chanrec::KickUser(userrec *src, userrec *user, const char* reason)
529 {
530         if (!src || !user || !reason)
531                 return this->GetUserCounter();
532
533         if (IS_LOCAL(src))
534         {
535                 if (!this->HasUser(user))
536                 {
537                         src->WriteServ("441 %s %s %s :They are not on that channel",src->nick, user->nick, this->name);
538                         return this->GetUserCounter();
539                 }
540                 if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
541                 {
542                         src->WriteServ("482 %s %s :Only a u-line may kick a u-line from a channel.",src->nick, this->name);
543                         return this->GetUserCounter();
544                 }
545                 int MOD_RESULT = 0;
546
547                 if (!ServerInstance->ULine(src->server))
548                 {
549                         MOD_RESULT = 0;
550                         FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,this,reason));
551                         if (MOD_RESULT == 1)
552                                 return this->GetUserCounter();
553                 }
554                 /* Set to -1 by OnUserPreKick if explicit allow was set */
555                 if (MOD_RESULT != -1)
556                 {
557                         FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,this,AC_KICK));
558                         if ((MOD_RESULT == ACR_DENY) && (!ServerInstance->ULine(src->server)))
559                                 return this->GetUserCounter();
560         
561                         if ((MOD_RESULT == ACR_DEFAULT) || (!ServerInstance->ULine(src->server)))
562                         {
563                                 int them = this->GetStatus(src);
564                                 int us = this->GetStatus(user);
565                                 if ((them < STATUS_HOP) || (them < us))
566                                 {
567                                         src->WriteServ("482 %s %s :You must be a channel %soperator",src->nick, this->name, them == STATUS_HOP ? "" : "half-");
568                                         return this->GetUserCounter();
569                                 }
570                         }
571                 }
572         }
573
574         FOREACH_MOD(I_OnUserKick,OnUserKick(src,user,this,reason));
575
576         UCListIter i = user->chans.find(this);
577         if (i != user->chans.end())
578         {
579                 /* zap it from the channel list of the user */
580                 this->WriteChannel(src, "KICK %s %s :%s", this->name, user->nick, reason);
581                 user->chans.erase(i);
582                 this->RemoveAllPrefixes(user);
583         }
584
585         if (!this->DelUser(user))
586         /* if there are no users left on the channel */
587         {
588                 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
589
590                 /* kill the record */
591                 if (iter != ServerInstance->chanlist.end())
592                 {
593                         FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
594                         ServerInstance->chanlist.erase(iter);
595                 }
596                 return 0;
597         }
598
599         return this->GetUserCounter();
600 }
601
602 void chanrec::WriteChannel(userrec* user, char* text, ...)
603 {
604         char textbuffer[MAXBUF];
605         va_list argsPtr;
606
607         if (!user || !text)
608                 return;
609
610         va_start(argsPtr, text);
611         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
612         va_end(argsPtr);
613
614         this->WriteChannel(user, std::string(textbuffer));
615 }
616
617 void chanrec::WriteChannel(userrec* user, const std::string &text)
618 {
619         CUList *ulist = this->GetUsers();
620
621         if (!user)
622                 return;
623
624         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
625         {
626                 if (IS_LOCAL(i->second))
627                         user->WriteTo(i->second,text);
628         }
629 }
630
631 void chanrec::WriteChannelWithServ(const char* ServName, const char* text, ...)
632 {
633         char textbuffer[MAXBUF];
634         va_list argsPtr;
635
636         if (!text)
637                 return;
638
639         va_start(argsPtr, text);
640         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
641         va_end(argsPtr);
642
643         this->WriteChannelWithServ(ServName, std::string(textbuffer));
644 }
645
646 void chanrec::WriteChannelWithServ(const char* ServName, const std::string &text)
647 {
648         CUList *ulist = this->GetUsers();
649
650         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
651         {
652                 if (IS_LOCAL(i->second))
653                         i->second->WriteServ(text);
654         }
655 }
656
657 /* write formatted text from a source user to all users on a channel except
658  * for the sender (for privmsg etc) */
659 void chanrec::WriteAllExceptSender(userrec* user, bool serversource, char status, char* text, ...)
660 {
661         char textbuffer[MAXBUF];
662         va_list argsPtr;
663
664         if (!text)
665                 return;
666
667         va_start(argsPtr, text);
668         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
669         va_end(argsPtr);
670
671         this->WriteAllExceptSender(user, serversource, status, std::string(textbuffer));
672 }
673
674 void chanrec::WriteAllExcept(userrec* user, bool serversource, char status, CUList &except_list, char* text, ...)
675 {
676         char textbuffer[MAXBUF];
677         va_list argsPtr;
678
679         if (!text)
680                 return;
681
682         va_start(argsPtr, text);
683         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
684         va_end(argsPtr);
685
686         this->WriteAllExcept(user, serversource, status, except_list, std::string(textbuffer));
687 }
688
689 void chanrec::WriteAllExcept(userrec* user, bool serversource, char status, CUList &except_list, const std::string &text)
690 {
691         CUList *ulist;
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         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
710         {
711                 if ((IS_LOCAL(i->second)) && (except_list.find(i->second) == except_list.end()))
712                 {
713                         if (serversource)
714                                 i->second->WriteServ(text);
715                         else
716                                 i->second->WriteFrom(user,text);
717                 }
718         }
719 }
720
721 void chanrec::WriteAllExceptSender(userrec* user, bool serversource, char status, const std::string& text)
722 {
723         CUList except_list;
724         except_list[user] = user;
725         this->WriteAllExcept(user, serversource, status, except_list, std::string(text));
726 }
727
728 /*
729  * return a count of the users on a specific channel accounting for
730  * invisible users who won't increase the count. e.g. for /LIST
731  */
732 int chanrec::CountInvisible()
733 {
734         int count = 0;
735         CUList *ulist= this->GetUsers();
736         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
737         {
738                 if (!(i->second->modes[UM_INVISIBLE]))
739                         count++;
740         }
741
742         return count;
743 }
744
745 char* chanrec::ChanModes(bool showkey)
746 {
747         static char scratch[MAXBUF];
748         static char sparam[MAXBUF];
749         char* offset = scratch;
750         std::string extparam = "";
751
752         *scratch = '\0';
753         *sparam = '\0';
754
755         /* This was still iterating up to 190, chanrec::modes is only 64 elements -- Om */
756         for(int n = 0; n < 64; n++)
757         {
758                 if(this->modes[n])
759                 {
760                         *offset++ = n + 65;
761                         extparam = "";
762                         switch (n)
763                         {
764                                 case CM_KEY:
765                                         extparam = (showkey ? this->key : "<key>");
766                                 break;
767                                 case CM_LIMIT:
768                                         extparam = ConvToStr(this->limit);
769                                 break;
770                                 case CM_NOEXTERNAL:
771                                 case CM_TOPICLOCK:
772                                 case CM_INVITEONLY:
773                                 case CM_MODERATED:
774                                 case CM_SECRET:
775                                 case CM_PRIVATE:
776                                         /* We know these have no parameters */
777                                 break;
778                                 default:
779                                         extparam = this->GetModeParameter(n + 65);
780                                 break;
781                         }
782                         if (extparam != "")
783                         {
784                                 charlcat(sparam,' ',MAXBUF);
785                                 strlcat(sparam,extparam.c_str(),MAXBUF);
786                         }
787                 }
788         }
789
790         /* Null terminate scratch */
791         *offset = '\0';
792         strlcat(scratch,sparam,MAXBUF);
793         return scratch;
794 }
795
796 /* compile a userlist of a channel into a string, each nick seperated by
797  * spaces and op, voice etc status shown as @ and +, and send it to 'user'
798  */
799 void chanrec::UserList(userrec *user)
800 {
801         char list[MAXBUF];
802         size_t dlen, curlen;
803         int MOD_RESULT = 0;
804
805         if (!IS_LOCAL(user))
806                 return;
807
808         FOREACH_RESULT(I_OnUserList,OnUserList(user, this));
809         ServerInstance->Log(DEBUG,"MOD_RESULT for UserList = %d",MOD_RESULT);
810         if (MOD_RESULT == 1)
811                 return;
812
813         ServerInstance->Log(DEBUG,"Using builtin NAMES list generation");
814
815         dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
816
817         int numusers = 0;
818         char* ptr = list + dlen;
819
820         CUList *ulist= this->GetUsers();
821
822         /* Improvement by Brain - this doesnt change in value, so why was it inside
823          * the loop?
824          */
825         bool has_user = this->HasUser(user);
826
827         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
828         {
829                 if ((!has_user) && (i->second->modes[UM_INVISIBLE]))
830                 {
831                         /*
832                          * user is +i, and source not on the channel, does not show
833                          * nick in NAMES list
834                          */
835                         continue;
836                 }
837
838                 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", this->GetPrefixChar(i->second), i->second->nick);
839
840                 curlen += ptrlen;
841                 ptr += ptrlen;
842
843                 numusers++;
844
845                 if (curlen > (480-NICKMAX))
846                 {
847                         /* list overflowed into multiple numerics */
848                         user->WriteServ(std::string(list));
849
850                         /* reset our lengths */
851                         dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
852                         ptr = list + dlen;
853
854                         ptrlen = 0;
855                         numusers = 0;
856                 }
857         }
858
859         /* if whats left in the list isnt empty, send it */
860         if (numusers)
861         {
862                 user->WriteServ(std::string(list));
863         }
864
865         user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, this->name);
866 }
867
868 long chanrec::GetMaxBans()
869 {
870         for (std::map<std::string,int>::iterator n = ServerInstance->Config->maxbans.begin(); n != ServerInstance->Config->maxbans.end(); n++)
871         {
872                 if (match(this->name,n->first.c_str()))
873                 {
874                         return n->second;
875                 }
876         }
877         return 64;
878 }
879
880
881 /* returns the status character for a given user on a channel, e.g. @ for op,
882  * % for halfop etc. If the user has several modes set, the highest mode
883  * the user has must be returned.
884  */
885 const char* chanrec::GetPrefixChar(userrec *user)
886 {
887         static char pf[2] = {0, 0};
888         
889         prefixlist::iterator n = prefixes.find(user);
890         if (n != prefixes.end())
891         {
892                 if (n->second.size())
893                 {
894                         /* If the user has any prefixes, their highest prefix
895                          * will always be at the head of the list, as the list is
896                          * sorted in rank order highest first (see SetPrefix()
897                          * for reasons why)
898                          */
899                         *pf = n->second.begin()->first;
900                         return pf;
901                 }
902         }
903
904         *pf = 0;
905         return pf;
906 }
907
908 const char* chanrec::GetAllPrefixChars(userrec* user)
909 {
910         static char prefix[MAXBUF];
911         int ctr = 0;
912         *prefix = 0;
913
914         prefixlist::iterator n = prefixes.find(user);
915         if (n != prefixes.end())
916         {
917                 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
918                 {
919                         prefix[ctr++] = x->first;
920                 }
921         }
922
923         prefix[ctr] = 0;
924
925         return prefix;
926 }
927
928 unsigned int chanrec::GetPrefixValue(userrec* user)
929 {
930         prefixlist::iterator n = prefixes.find(user);
931         if (n != prefixes.end())
932         {
933                 if (n->second.size())
934                         return n->second.begin()->second;
935         }
936         return 0;
937 }
938
939 int chanrec::GetStatusFlags(userrec *user)
940 {
941         UCListIter i = user->chans.find(this);
942         if (i != user->chans.end())
943         {
944                 return i->second;
945         }
946         return 0;
947 }
948
949 int chanrec::GetStatus(userrec *user)
950 {
951         if (ServerInstance->ULine(user->server))
952                 return STATUS_OP;
953
954         UCListIter i = user->chans.find(this);
955         if (i != user->chans.end())
956         {
957                 if ((i->second & UCMODE_OP) > 0)
958                 {
959                         return STATUS_OP;
960                 }
961                 if ((i->second & UCMODE_HOP) > 0)
962                 {
963                         return STATUS_HOP;
964                 }
965                 if ((i->second & UCMODE_VOICE) > 0)
966                 {
967                         return STATUS_VOICE;
968                 }
969                 return STATUS_NORMAL;
970         }
971         return STATUS_NORMAL;
972 }
973
974 void chanrec::SetPrefix(userrec* user, char prefix, unsigned int prefix_value, bool adding)
975 {
976         prefixlist::iterator n = prefixes.find(user);
977         prefixtype pfx = std::make_pair(prefix,prefix_value);
978         if (adding)
979         {
980                 if (n != prefixes.end())
981                 {
982                         if (std::find(n->second.begin(), n->second.end(), pfx) == n->second.end())
983                         {
984                                 n->second.push_back(pfx);
985                                 /* We must keep prefixes in rank order, largest first.
986                                  * This is for two reasons, firstly because x-chat *ass-u-me's* this
987                                  * state, and secondly it turns out to be a benefit to us later.
988                                  * See above in GetPrefix().
989                                  */
990                                 std::sort(n->second.begin(), n->second.end(), ModeParser::PrefixComparison);
991                         }
992                 }
993                 else
994                 {
995                         pfxcontainer one;
996                         one.push_back(pfx);
997                         prefixes.insert(std::make_pair<userrec*,pfxcontainer>(user, one));
998                 }
999         }
1000         else
1001         {
1002                 if (n != prefixes.end())
1003                 {
1004                         pfxcontainer::iterator x = std::find(n->second.begin(), n->second.end(), pfx);
1005                         if (x != n->second.end())
1006                                 n->second.erase(x);
1007                 }
1008         }
1009         ServerInstance->Log(DEBUG,"Added prefix %c to %s for %s, prefixlist size is now %d", prefix, this->name, user->nick, prefixes.size());
1010 }
1011
1012 void chanrec::RemoveAllPrefixes(userrec* user)
1013 {
1014         prefixlist::iterator n = prefixes.find(user);
1015         if (n != prefixes.end())
1016         {
1017                 ServerInstance->Log(DEBUG,"Removed prefixes from %s for %s, prefixlist size is now %d", this->name, user->nick, prefixes.size());
1018                 prefixes.erase(n);
1019         }
1020 }
1021