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