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