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