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