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