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