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