]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/channels.cpp
Add <connect:maxchans> as per feature bug #338 - combined with the last feature,...
[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 "inspircd.h"
15 #include <stdarg.h>
16 #include "configreader.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         CustomModeList::iterator n = custom_mode_params.find(mode);     
41
42         if (mode_on)
43         {
44                 if (n == custom_mode_params.end())
45                         custom_mode_params[mode] = strdup(parameter);
46         }
47         else
48         {
49                 if (n != custom_mode_params.end())
50                 {
51                         free(n->second);
52                         custom_mode_params.erase(n);
53                 }
54         }
55 }
56
57 bool chanrec::IsModeSet(char mode)
58 {
59         return modes[mode-65];
60 }
61
62 std::string chanrec::GetModeParameter(char mode)
63 {
64         switch (mode)
65         {
66                 case 'k':
67                         return this->key;
68                 case 'l':
69                         return ConvToStr(this->limit);
70                 default:
71                         CustomModeList::iterator n = custom_mode_params.find(mode);
72                         if (n != custom_mode_params.end())
73                                 return n->second;
74                         return "";
75                 break;
76         }
77 }
78
79 long chanrec::GetUserCounter()
80 {
81         return (this->internal_userlist.size());
82 }
83
84 void chanrec::AddUser(userrec* user)
85 {
86         internal_userlist[user] = user->nick;
87 }
88
89 unsigned long chanrec::DelUser(userrec* user)
90 {
91         CUListIter a = internal_userlist.find(user);
92         
93         if (a != internal_userlist.end())
94         {
95                 internal_userlist.erase(a);
96                 /* And tidy any others... */
97                 DelOppedUser(user);
98                 DelHalfoppedUser(user);
99                 DelVoicedUser(user);
100         }
101         
102         return internal_userlist.size();
103 }
104
105 bool chanrec::HasUser(userrec* user)
106 {
107         return (internal_userlist.find(user) != internal_userlist.end());
108 }
109
110 void chanrec::AddOppedUser(userrec* user)
111 {
112         internal_op_userlist[user] = user->nick;
113 }
114
115 void chanrec::DelOppedUser(userrec* user)
116 {
117         CUListIter a = internal_op_userlist.find(user);
118         if (a != internal_op_userlist.end())
119         {
120                 internal_op_userlist.erase(a);
121                 return;
122         }
123 }
124
125 void chanrec::AddHalfoppedUser(userrec* user)
126 {
127         internal_halfop_userlist[user] = user->nick;
128 }
129
130 void chanrec::DelHalfoppedUser(userrec* user)
131 {
132         CUListIter a = internal_halfop_userlist.find(user);
133
134         if (a != internal_halfop_userlist.end())
135         {   
136                 internal_halfop_userlist.erase(a);
137         }
138 }
139
140 void chanrec::AddVoicedUser(userrec* user)
141 {
142         internal_voice_userlist[user] = user->nick;
143 }
144
145 void chanrec::DelVoicedUser(userrec* user)
146 {
147         CUListIter a = internal_voice_userlist.find(user);
148         
149         if (a != internal_voice_userlist.end())
150         {
151                 internal_voice_userlist.erase(a);
152         }
153 }
154
155 CUList* chanrec::GetUsers()
156 {
157         return &internal_userlist;
158 }
159
160 CUList* chanrec::GetOppedUsers()
161 {
162         return &internal_op_userlist;
163 }
164
165 CUList* chanrec::GetHalfoppedUsers()
166 {
167         return &internal_halfop_userlist;
168 }
169
170 CUList* chanrec::GetVoicedUsers()
171 {
172         return &internal_voice_userlist;
173 }
174
175 void chanrec::SetDefaultModes()
176 {
177         irc::spacesepstream list(ServerInstance->Config->DefaultModes);
178         std::string modeseq = list.GetToken();
179         std::string parameter;
180         userrec* dummyuser = new userrec(ServerInstance);
181         dummyuser->SetFd(FD_MAGIC_NUMBER);
182
183         for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
184         {
185                 ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
186                 if (mode)
187                 {
188                         if (mode->GetNumParams(true))
189                                 parameter = list.GetToken().c_str();
190                         else
191                                 parameter.clear();
192
193                         mode->OnModeChange(dummyuser, dummyuser, this, parameter, true);
194                 }
195         }
196
197         delete dummyuser;
198 }
199
200 /* 
201  * add a channel to a user, creating the record for it if needed and linking
202  * it to the user record 
203  */
204 chanrec* chanrec::JoinUser(InspIRCd* Instance, userrec *user, const char* cn, bool override, const char* key, time_t TS)
205 {
206         if (!user || !cn)
207                 return NULL;
208
209         char cname[MAXBUF];
210         int MOD_RESULT = 0;
211         std::string privs;
212         chanrec *Ptr;
213
214         /*
215          * We don't restrict the number of channels that remote users or users that are override-joining may be in.
216          * We restrict local users to MaxChans channels.
217          * We restrict local operators to OperMaxChans channels.
218          * This is a lot more logical than how it was formerly. -- w00t
219          */
220         if (IS_LOCAL(user) && !override)
221         {
222                 if (user->GetMaxChans())
223                 {
224                         if (user->chans.size() >= user->GetMaxChans())
225                         {
226                                 user->WriteServ("405 %s %s :You are on too many channels",user->nick, cn);
227                                 return NULL;
228                         }
229                 }
230                 else
231                 {
232                         if (IS_OPER(user))
233                         {
234                                 if (user->chans.size() >= Instance->Config->OperMaxChans)
235                                 {
236                                         user->WriteServ("405 %s %s :You are on too many channels",user->nick, cn);
237                                         return NULL;
238                                 }
239                         }
240                         else
241                         {
242                                 if (user->chans.size() >= Instance->Config->MaxChans)
243                                 {
244                                         user->WriteServ("405 %s %s :You are on too many channels",user->nick, cn);
245                                         return NULL;
246                                 }
247                         }
248                 }
249         }
250
251         strlcpy(cname, cn, CHANMAX);
252         Ptr = Instance->FindChan(cname);
253
254         if (!Ptr)
255         {
256                 /*
257                  * Fix: desync bug was here, don't set @ on remote users - spanningtree handles their permissions. bug #358. -- w00t
258                  */
259                 if (!IS_LOCAL(user))
260                 {
261                         if (!TS)
262                                 Instance->Log(DEBUG,"*** BUG *** chanrec::JoinUser called for REMOTE user '%s' on channel '%s' but no TS given!", user->nick, cn);
263                 }
264                 else
265                 {
266                         privs = "@";
267                 }
268
269                 if (IS_LOCAL(user) && override == false)
270                 {
271                         MOD_RESULT = 0;
272                         FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,NULL,cname,privs));
273                         if (MOD_RESULT == 1)
274                                 return NULL;
275                 }
276
277                 /* create a new one */
278                 Ptr = new chanrec(Instance);
279                 (*(Instance->chanlist))[cname] = Ptr;
280
281                 strlcpy(Ptr->name, cname,CHANMAX);
282
283                 /* As spotted by jilles, dont bother to set this on remote users */
284                 if (IS_LOCAL(user))
285                         Ptr->SetDefaultModes();
286
287                 Ptr->created = TS ? TS : Instance->Time();
288                 Ptr->age = Ptr->created;
289                 *Ptr->topic = 0;
290                 *Ptr->setby = 0;
291                 Ptr->topicset = 0;
292         }
293         else
294         {
295                 /* Already on the channel */
296                 if (Ptr->HasUser(user))
297                         return NULL;
298
299                 /*
300                  * remote users are allowed us to bypass channel modes
301                  * and bans (used by servers)
302                  */
303                 if (IS_LOCAL(user) && override == false)
304                 {
305                         MOD_RESULT = 0;
306                         FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname,privs));
307                         if (MOD_RESULT == 1)
308                         {
309                                 return NULL;
310                         }
311                         else if (MOD_RESULT == 0)
312                         {
313                                 if (*Ptr->key)
314                                 {
315                                         MOD_RESULT = 0;
316                                         FOREACH_RESULT_I(Instance,I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
317                                         if (!MOD_RESULT)
318                                         {
319                                                 if ((!key) || strcmp(key,Ptr->key))
320                                                 {
321                                                         user->WriteServ("475 %s %s :Cannot join channel (Incorrect channel key)",user->nick, Ptr->name);
322                                                         return NULL;
323                                                 }
324                                         }
325                                 }
326                                 if (Ptr->IsModeSet('i'))
327                                 {
328                                         MOD_RESULT = 0;
329                                         FOREACH_RESULT_I(Instance,I_OnCheckInvite,OnCheckInvite(user, Ptr));
330                                         if (!MOD_RESULT)
331                                         {
332                                                 if (!user->IsInvited(Ptr->name))
333                                                 {
334                                                         user->WriteServ("473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
335                                                         return NULL;
336                                                 }
337                                         }
338                                         user->RemoveInvite(Ptr->name);
339                                 }
340                                 if (Ptr->limit)
341                                 {
342                                         MOD_RESULT = 0;
343                                         FOREACH_RESULT_I(Instance,I_OnCheckLimit,OnCheckLimit(user, Ptr));
344                                         if (!MOD_RESULT)
345                                         {
346                                                 if (Ptr->GetUserCounter() >= Ptr->limit)
347                                                 {
348                                                         user->WriteServ("471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
349                                                         return NULL;
350                                                 }
351                                         }
352                                 }
353                                 if (Ptr->bans.size())
354                                 {
355                                         if (Ptr->IsBanned(user))
356                                         {
357                                                 user->WriteServ("474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
358                                                 return NULL;
359                                         }
360                                 }
361                         }
362                 }
363         }
364
365         return chanrec::ForceChan(Instance, Ptr, user, privs);
366 }
367
368 chanrec* chanrec::ForceChan(InspIRCd* Instance, chanrec* Ptr, userrec* user, const std::string &privs)
369 {
370         userrec* dummyuser = new userrec(Instance);
371         std::string nick = user->nick;
372         bool silent = false;
373
374         dummyuser->SetFd(FD_MAGIC_NUMBER);
375         Ptr->AddUser(user);
376
377         /* Just in case they have no permissions */
378         user->chans[Ptr] = 0;
379
380         for (std::string::const_iterator x = privs.begin(); x != privs.end(); x++)
381         {
382                 const char status = *x;
383                 ModeHandler* mh = Instance->Modes->FindPrefix(status);
384                 if (mh)
385                 {
386                         Ptr->SetPrefix(user, status, mh->GetPrefixRank(), true);
387                         /* Make sure that the mode handler knows this mode was now set */
388                         mh->OnModeChange(dummyuser, dummyuser, Ptr, nick, true);
389
390                         switch (mh->GetPrefix())
391                         {
392                                 /* These logic ops are SAFE IN THIS CASE
393                                  * because if the entry doesnt exist,
394                                  * addressing operator[] creates it.
395                                  * If they do exist, it points to it.
396                                  * At all other times where we dont want
397                                  * to create an item if it doesnt exist, we
398                                  * must stick to ::find().
399                                  */
400                                 case '@':
401                                         user->chans[Ptr] |= UCMODE_OP;
402                                 break;
403                                 case '%':
404                                         user->chans[Ptr] |= UCMODE_HOP;
405                                 break;
406                                 case '+':
407                                         user->chans[Ptr] |= UCMODE_VOICE;
408                                 break;
409                         }
410                 }
411         }
412
413         delete dummyuser;
414
415         FOREACH_MOD_I(Instance,I_OnUserJoin,OnUserJoin(user, Ptr, silent));
416
417         if (!silent)
418                 Ptr->WriteChannel(user,"JOIN :%s",Ptr->name);
419
420         /* Theyre not the first ones in here, make sure everyone else sees the modes we gave the user */
421         std::string ms = Instance->Modes->ModeString(user, Ptr);
422         if ((Ptr->GetUserCounter() > 1) && (ms.length()))
423                 Ptr->WriteAllExceptSender(user, true, 0, "MODE %s +%s", Ptr->name, ms.c_str());
424
425         /* Major improvement by Brain - we dont need to be calculating all this pointlessly for remote users */
426         if (IS_LOCAL(user))
427         {
428                 if (Ptr->topicset)
429                 {
430                         user->WriteServ("332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
431                         user->WriteServ("333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
432                 }
433                 Ptr->UserList(user);
434         }
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         bool silent = false;
469
470         if (!user)
471                 return this->GetUserCounter();
472
473         UCListIter i = user->chans.find(this);
474         if (i != user->chans.end())
475         {
476                 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, reason ? reason : "", silent));
477
478                 if (!silent)
479                         this->WriteChannel(user, "PART %s%s%s", this->name, reason ? " :" : "", reason ? reason : "");
480
481                 user->chans.erase(i);
482                 this->RemoveAllPrefixes(user);
483         }
484
485         if (!this->DelUser(user)) /* if there are no users left on the channel... */
486         {
487                 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
488                 /* kill the record */
489                 if (iter != ServerInstance->chanlist->end())
490                 {
491                         FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
492                         ServerInstance->chanlist->erase(iter);
493                 }
494                 return 0;
495         }
496
497         return this->GetUserCounter();
498 }
499
500 long chanrec::ServerKickUser(userrec* user, const char* reason, bool triggerevents)
501 {
502         bool silent = false;
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, silent));
519         }
520
521         UCListIter i = user->chans.find(this);
522         if (i != user->chans.end())
523         {
524                 if (!silent)
525                         this->WriteChannelWithServ(ServerInstance->Config->ServerName, "KICK %s %s :%s", this->name, user->nick, reason);
526
527                 user->chans.erase(i);
528                 this->RemoveAllPrefixes(user);
529         }
530
531         if (!this->DelUser(user))
532         {
533                 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
534                 /* kill the record */
535                 if (iter != ServerInstance->chanlist->end())
536                 {
537                         FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
538                         ServerInstance->chanlist->erase(iter);
539                 }
540                 return 0;
541         }
542
543         return this->GetUserCounter();
544 }
545
546 long chanrec::KickUser(userrec *src, userrec *user, const char* reason)
547 {
548         bool silent = false;
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, silent));
595
596         UCListIter i = user->chans.find(this);
597         if (i != user->chans.end())
598         {
599                 /* zap it from the channel list of the user */
600                 if (!silent)
601                         this->WriteChannel(src, "KICK %s %s :%s", this->name, user->nick, reason);
602
603                 user->chans.erase(i);
604                 this->RemoveAllPrefixes(user);
605         }
606
607         if (!this->DelUser(user))
608         /* if there are no users left on the channel */
609         {
610                 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
611
612                 /* kill the record */
613                 if (iter != ServerInstance->chanlist->end())
614                 {
615                         FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
616                         ServerInstance->chanlist->erase(iter);
617                 }
618                 return 0;
619         }
620
621         return this->GetUserCounter();
622 }
623
624 void chanrec::WriteChannel(userrec* user, char* text, ...)
625 {
626         char textbuffer[MAXBUF];
627         va_list argsPtr;
628
629         if (!user || !text)
630                 return;
631
632         va_start(argsPtr, text);
633         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
634         va_end(argsPtr);
635
636         this->WriteChannel(user, std::string(textbuffer));
637 }
638
639 void chanrec::WriteChannel(userrec* user, const std::string &text)
640 {
641         CUList *ulist = this->GetUsers();
642         char tb[MAXBUF];
643
644         if (!user)
645                 return;
646
647         snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());
648         std::string out = tb;
649
650         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
651         {
652                 if (IS_LOCAL(i->first))
653                         i->first->Write(out);
654         }
655 }
656
657 void chanrec::WriteChannelWithServ(const char* ServName, const char* text, ...)
658 {
659         char textbuffer[MAXBUF];
660         va_list argsPtr;
661
662         if (!text)
663                 return;
664
665         va_start(argsPtr, text);
666         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
667         va_end(argsPtr);
668
669         this->WriteChannelWithServ(ServName, std::string(textbuffer));
670 }
671
672 void chanrec::WriteChannelWithServ(const char* ServName, const std::string &text)
673 {
674         CUList *ulist = this->GetUsers();
675         char tb[MAXBUF];
676
677         snprintf(tb,MAXBUF,":%s %s",ServName ? ServName : ServerInstance->Config->ServerName, text.c_str());
678         std::string out = tb;
679
680         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
681         {
682                 if (IS_LOCAL(i->first))
683                         i->first->Write(out);
684         }
685 }
686
687 /* write formatted text from a source user to all users on a channel except
688  * for the sender (for privmsg etc) */
689 void chanrec::WriteAllExceptSender(userrec* user, bool serversource, char status, char* text, ...)
690 {
691         char textbuffer[MAXBUF];
692         va_list argsPtr;
693
694         if (!text)
695                 return;
696
697         va_start(argsPtr, text);
698         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
699         va_end(argsPtr);
700
701         this->WriteAllExceptSender(user, serversource, status, std::string(textbuffer));
702 }
703
704 void chanrec::WriteAllExcept(userrec* user, bool serversource, char status, CUList &except_list, char* text, ...)
705 {
706         char textbuffer[MAXBUF];
707         va_list argsPtr;
708
709         if (!text)
710                 return;
711
712         va_start(argsPtr, text);
713         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
714         va_end(argsPtr);
715
716         this->WriteAllExcept(user, serversource, status, except_list, std::string(textbuffer));
717 }
718
719 void chanrec::WriteAllExcept(userrec* user, bool serversource, char status, CUList &except_list, const std::string &text)
720 {
721         CUList *ulist;
722         char tb[MAXBUF];
723
724         switch (status)
725         {
726                 case '@':
727                         ulist = this->GetOppedUsers();
728                         break;
729                 case '%':
730                         ulist = this->GetHalfoppedUsers();
731                         break;
732                 case '+':
733                         ulist = this->GetVoicedUsers();
734                         break;
735                 default:
736                         ulist = this->GetUsers();
737                         break;
738         }
739
740         snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());
741         std::string out = tb;
742
743         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
744         {
745                 if ((IS_LOCAL(i->first)) && (except_list.find(i->first) == except_list.end()))
746                 {
747                         if (serversource)
748                                 i->first->WriteServ(text);
749                         else
750                                 i->first->Write(out);
751                 }
752         }
753 }
754
755 void chanrec::WriteAllExceptSender(userrec* user, bool serversource, char status, const std::string& text)
756 {
757         CUList except_list;
758         except_list[user] = user->nick;
759         this->WriteAllExcept(user, serversource, status, except_list, std::string(text));
760 }
761
762 /*
763  * return a count of the users on a specific channel accounting for
764  * invisible users who won't increase the count. e.g. for /LIST
765  */
766 int chanrec::CountInvisible()
767 {
768         int count = 0;
769         CUList *ulist= this->GetUsers();
770         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
771         {
772                 if (!(i->first->IsModeSet('i')))
773                         count++;
774         }
775
776         return count;
777 }
778
779 char* chanrec::ChanModes(bool showkey)
780 {
781         static char scratch[MAXBUF];
782         static char sparam[MAXBUF];
783         char* offset = scratch;
784         std::string extparam;
785
786         *scratch = '\0';
787         *sparam = '\0';
788
789         /* This was still iterating up to 190, chanrec::modes is only 64 elements -- Om */
790         for(int n = 0; n < 64; n++)
791         {
792                 if(this->modes[n])
793                 {
794                         *offset++ = n + 65;
795                         extparam.clear();
796                         switch (n)
797                         {
798                                 case CM_KEY:
799                                         extparam = (showkey ? this->key : "<key>");
800                                 break;
801                                 case CM_LIMIT:
802                                         extparam = ConvToStr(this->limit);
803                                 break;
804                                 case CM_NOEXTERNAL:
805                                 case CM_TOPICLOCK:
806                                 case CM_INVITEONLY:
807                                 case CM_MODERATED:
808                                 case CM_SECRET:
809                                 case CM_PRIVATE:
810                                         /* We know these have no parameters */
811                                 break;
812                                 default:
813                                         extparam = this->GetModeParameter(n + 65);
814                                 break;
815                         }
816                         if (!extparam.empty())
817                         {
818                                 charlcat(sparam,' ',MAXBUF);
819                                 strlcat(sparam,extparam.c_str(),MAXBUF);
820                         }
821                 }
822         }
823
824         /* Null terminate scratch */
825         *offset = '\0';
826         strlcat(scratch,sparam,MAXBUF);
827         return scratch;
828 }
829
830 /* compile a userlist of a channel into a string, each nick seperated by
831  * spaces and op, voice etc status shown as @ and +, and send it to 'user'
832  */
833 void chanrec::UserList(userrec *user, CUList *ulist)
834 {
835         char list[MAXBUF];
836         size_t dlen, curlen;
837         int MOD_RESULT = 0;
838
839         if (!IS_LOCAL(user))
840                 return;
841
842         FOREACH_RESULT(I_OnUserList,OnUserList(user, this, ulist));
843         if (MOD_RESULT == 1)
844                 return;
845
846         dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
847
848         int numusers = 0;
849         char* ptr = list + dlen;
850
851         if (!ulist)
852                 ulist = this->GetUsers();
853
854         /* Improvement by Brain - this doesnt change in value, so why was it inside
855          * the loop?
856          */
857         bool has_user = this->HasUser(user);
858
859         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
860         {
861                 if ((!has_user) && (i->first->IsModeSet('i')))
862                 {
863                         /*
864                          * user is +i, and source not on the channel, does not show
865                          * nick in NAMES list
866                          */
867                         continue;
868                 }
869
870                 if (i->first->Visibility && !i->first->Visibility->VisibleTo(user))
871                         continue;
872
873                 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", this->GetPrefixChar(i->first), i->second.c_str());
874                 /* OnUserList can change this - reset it back to normal */
875                 i->second = i->first->nick;
876
877                 curlen += ptrlen;
878                 ptr += ptrlen;
879
880                 numusers++;
881
882                 if (curlen > (480-NICKMAX))
883                 {
884                         /* list overflowed into multiple numerics */
885                         user->WriteServ(std::string(list));
886
887                         /* reset our lengths */
888                         dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
889                         ptr = list + dlen;
890
891                         ptrlen = 0;
892                         numusers = 0;
893                 }
894         }
895
896         /* if whats left in the list isnt empty, send it */
897         if (numusers)
898         {
899                 user->WriteServ(std::string(list));
900         }
901
902         user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, this->name);
903 }
904
905 long chanrec::GetMaxBans()
906 {
907         /* Return the cached value if there is one */
908         if (this->maxbans)
909                 return this->maxbans;
910
911         /* If there isnt one, we have to do some O(n) hax to find it the first time. (ick) */
912         for (std::map<std::string,int>::iterator n = ServerInstance->Config->maxbans.begin(); n != ServerInstance->Config->maxbans.end(); n++)
913         {
914                 if (match(this->name,n->first.c_str()))
915                 {
916                         this->maxbans = n->second;
917                         return n->second;
918                 }
919         }
920
921         /* Screw it, just return the default of 64 */
922         this->maxbans = 64;
923         return this->maxbans;
924 }
925
926 void chanrec::ResetMaxBans()
927 {
928         this->maxbans = 0;
929 }
930
931 /* returns the status character for a given user on a channel, e.g. @ for op,
932  * % for halfop etc. If the user has several modes set, the highest mode
933  * the user has must be returned.
934  */
935 const char* chanrec::GetPrefixChar(userrec *user)
936 {
937         static char pf[2] = {0, 0};
938         
939         prefixlist::iterator n = prefixes.find(user);
940         if (n != prefixes.end())
941         {
942                 if (n->second.size())
943                 {
944                         /* If the user has any prefixes, their highest prefix
945                          * will always be at the head of the list, as the list is
946                          * sorted in rank order highest first (see SetPrefix()
947                          * for reasons why)
948                          */
949                         *pf = n->second.begin()->first;
950                         return pf;
951                 }
952         }
953
954         *pf = 0;
955         return pf;
956 }
957
958 const char* chanrec::GetAllPrefixChars(userrec* user)
959 {
960         static char prefix[MAXBUF];
961         int ctr = 0;
962         *prefix = 0;
963
964         prefixlist::iterator n = prefixes.find(user);
965         if (n != prefixes.end())
966         {
967                 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
968                 {
969                         prefix[ctr++] = x->first;
970                 }
971         }
972
973         prefix[ctr] = 0;
974
975         return prefix;
976 }
977
978 unsigned int chanrec::GetPrefixValue(userrec* user)
979 {
980         prefixlist::iterator n = prefixes.find(user);
981         if (n != prefixes.end())
982         {
983                 if (n->second.size())
984                         return n->second.begin()->second;
985         }
986         return 0;
987 }
988
989 int chanrec::GetStatusFlags(userrec *user)
990 {
991         UCListIter i = user->chans.find(this);
992         if (i != user->chans.end())
993         {
994                 return i->second;
995         }
996         return 0;
997 }
998
999 int chanrec::GetStatus(userrec *user)
1000 {
1001         if (ServerInstance->ULine(user->server))
1002                 return STATUS_OP;
1003
1004         UCListIter i = user->chans.find(this);
1005         if (i != user->chans.end())
1006         {
1007                 if ((i->second & UCMODE_OP) > 0)
1008                 {
1009                         return STATUS_OP;
1010                 }
1011                 if ((i->second & UCMODE_HOP) > 0)
1012                 {
1013                         return STATUS_HOP;
1014                 }
1015                 if ((i->second & UCMODE_VOICE) > 0)
1016                 {
1017                         return STATUS_VOICE;
1018                 }
1019                 return STATUS_NORMAL;
1020         }
1021         return STATUS_NORMAL;
1022 }
1023
1024 void chanrec::SetPrefix(userrec* user, char prefix, unsigned int prefix_value, bool adding)
1025 {
1026         prefixlist::iterator n = prefixes.find(user);
1027         prefixtype pfx = std::make_pair(prefix,prefix_value);
1028         if (adding)
1029         {
1030                 if (n != prefixes.end())
1031                 {
1032                         if (std::find(n->second.begin(), n->second.end(), pfx) == n->second.end())
1033                         {
1034                                 n->second.push_back(pfx);
1035                                 /* We must keep prefixes in rank order, largest first.
1036                                  * This is for two reasons, firstly because x-chat *ass-u-me's* this
1037                                  * state, and secondly it turns out to be a benefit to us later.
1038                                  * See above in GetPrefix().
1039                                  */
1040                                 std::sort(n->second.begin(), n->second.end(), ModeParser::PrefixComparison);
1041                         }
1042                 }
1043                 else
1044                 {
1045                         pfxcontainer one;
1046                         one.push_back(pfx);
1047                         prefixes.insert(std::make_pair<userrec*,pfxcontainer>(user, one));
1048                 }
1049         }
1050         else
1051         {
1052                 if (n != prefixes.end())
1053                 {
1054                         pfxcontainer::iterator x = std::find(n->second.begin(), n->second.end(), pfx);
1055                         if (x != n->second.end())
1056                                 n->second.erase(x);
1057                 }
1058         }
1059 }
1060
1061 void chanrec::RemoveAllPrefixes(userrec* user)
1062 {
1063         prefixlist::iterator n = prefixes.find(user);
1064         if (n != prefixes.end())
1065         {
1066                 prefixes.erase(n);
1067         }
1068 }
1069