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