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