]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/channels.cpp
The start of extended bans infrastructure: syntax is e.g. +b n:w00tdiff@*, bans of...
[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 printf("realmask: %s\n", maskptr);
471
472                 /* This allows CIDR ban matching
473                  * 
474                  *        Full masked host                             Full unmasked host                     IP with/without CIDR
475                  */
476                 if ((match(user->GetFullHost(), maskptr)) || (match(user->GetFullRealHost(), maskptr)) || (match(mask, maskptr, true)))
477                 {
478                         return true;
479                 }
480         }
481
482         return false;
483 }
484
485 /* Channel::PartUser
486  * remove a channel from a users record, and return the number of users left.
487  * Therefore, if this function returns 0 the caller should delete the Channel.
488  */
489 long Channel::PartUser(User *user, const char* reason)
490 {
491         bool silent = false;
492
493         if (!user)
494                 return this->GetUserCounter();
495
496         UCListIter i = user->chans.find(this);
497         if (i != user->chans.end())
498         {
499                 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, reason ? reason : "", silent));
500
501                 if (!silent)
502                         this->WriteChannel(user, "PART %s%s%s", this->name, reason ? " :" : "", reason ? reason : "");
503
504                 user->chans.erase(i);
505                 this->RemoveAllPrefixes(user);
506         }
507
508         if (!this->DelUser(user)) /* if there are no users left on the channel... */
509         {
510                 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
511                 /* kill the record */
512                 if (iter != ServerInstance->chanlist->end())
513                 {
514                         int MOD_RESULT = 0;
515                         FOREACH_RESULT_I(ServerInstance,I_OnChannelPreDelete, OnChannelPreDelete(this));
516                         if (MOD_RESULT == 1)
517                                 return 1; // delete halted by module
518                         FOREACH_MOD(I_OnChannelDelete, OnChannelDelete(this));
519                         ServerInstance->chanlist->erase(iter);
520                 }
521                 return 0;
522         }
523
524         return this->GetUserCounter();
525 }
526
527 long Channel::ServerKickUser(User* user, const char* reason, bool triggerevents)
528 {
529         bool silent = false;
530
531         if (!user || !reason)
532                 return this->GetUserCounter();
533
534         if (IS_LOCAL(user))
535         {
536                 if (!this->HasUser(user))
537                 {
538                         /* Not on channel */
539                         return this->GetUserCounter();
540                 }
541         }
542
543         if (triggerevents)
544         {
545                 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL, user, this, reason, silent));
546         }
547
548         UCListIter i = user->chans.find(this);
549         if (i != user->chans.end())
550         {
551                 if (!silent)
552                         this->WriteChannelWithServ(ServerInstance->Config->ServerName, "KICK %s %s :%s", this->name, user->nick, reason);
553
554                 user->chans.erase(i);
555                 this->RemoveAllPrefixes(user);
556         }
557
558         if (!this->DelUser(user))
559         {
560                 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
561                 /* kill the record */
562                 if (iter != ServerInstance->chanlist->end())
563                 {
564                         int MOD_RESULT = 0;
565                         FOREACH_RESULT_I(ServerInstance,I_OnChannelPreDelete, OnChannelPreDelete(this));
566                         if (MOD_RESULT == 1)
567                                 return 1; // delete halted by module
568                         FOREACH_MOD(I_OnChannelDelete, OnChannelDelete(this));
569                         ServerInstance->chanlist->erase(iter);
570                 }
571                 return 0;
572         }
573
574         return this->GetUserCounter();
575 }
576
577 long Channel::KickUser(User *src, User *user, const char* reason)
578 {
579         bool silent = false;
580
581         if (!src || !user || !reason)
582                 return this->GetUserCounter();
583
584         if (IS_LOCAL(src))
585         {
586                 if (!this->HasUser(user))
587                 {
588                         src->WriteNumeric(441, "%s %s %s :They are not on that channel",src->nick, user->nick, this->name);
589                         return this->GetUserCounter();
590                 }
591                 if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
592                 {
593                         src->WriteNumeric(482, "%s %s :Only a u-line may kick a u-line from a channel.",src->nick, this->name);
594                         return this->GetUserCounter();
595                 }
596                 int MOD_RESULT = 0;
597
598                 if (!ServerInstance->ULine(src->server))
599                 {
600                         MOD_RESULT = 0;
601                         FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,this,reason));
602                         if (MOD_RESULT == 1)
603                                 return this->GetUserCounter();
604                 }
605                 /* Set to -1 by OnUserPreKick if explicit allow was set */
606                 if (MOD_RESULT != -1)
607                 {
608                         FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,this,AC_KICK));
609                         if ((MOD_RESULT == ACR_DENY) && (!ServerInstance->ULine(src->server)))
610                                 return this->GetUserCounter();
611         
612                         if ((MOD_RESULT == ACR_DEFAULT) || (!ServerInstance->ULine(src->server)))
613                         {
614                                 int them = this->GetStatus(src);
615                                 int us = this->GetStatus(user);
616                                 if ((them < STATUS_HOP) || (them < us))
617                                 {
618                                         src->WriteNumeric(482, "%s %s :You must be a channel %soperator",src->nick, this->name, them == STATUS_HOP ? "" : "half-");
619                                         return this->GetUserCounter();
620                                 }
621                         }
622                 }
623         }
624
625         FOREACH_MOD(I_OnUserKick,OnUserKick(src, user, this, reason, silent));
626
627         UCListIter i = user->chans.find(this);
628         if (i != user->chans.end())
629         {
630                 /* zap it from the channel list of the user */
631                 if (!silent)
632                         this->WriteChannel(src, "KICK %s %s :%s", this->name, user->nick, reason);
633
634                 user->chans.erase(i);
635                 this->RemoveAllPrefixes(user);
636         }
637
638         if (!this->DelUser(user))
639         /* if there are no users left on the channel */
640         {
641                 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
642
643                 /* kill the record */
644                 if (iter != ServerInstance->chanlist->end())
645                 {
646                         int MOD_RESULT = 0;
647                         FOREACH_RESULT_I(ServerInstance,I_OnChannelPreDelete, OnChannelPreDelete(this));
648                         if (MOD_RESULT == 1)
649                                 return 1; // delete halted by module
650                         FOREACH_MOD(I_OnChannelDelete, OnChannelDelete(this));
651                         ServerInstance->chanlist->erase(iter);
652                 }
653                 return 0;
654         }
655
656         return this->GetUserCounter();
657 }
658
659 void Channel::WriteChannel(User* user, const char* text, ...)
660 {
661         char textbuffer[MAXBUF];
662         va_list argsPtr;
663
664         if (!user || !text)
665                 return;
666
667         va_start(argsPtr, text);
668         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
669         va_end(argsPtr);
670
671         this->WriteChannel(user, std::string(textbuffer));
672 }
673
674 void Channel::WriteChannel(User* user, const std::string &text)
675 {
676         CUList *ulist = this->GetUsers();
677         char tb[MAXBUF];
678
679         if (!user)
680                 return;
681
682         snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());
683         std::string out = tb;
684
685         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
686         {
687                 if (IS_LOCAL(i->first))
688                         i->first->Write(out);
689         }
690 }
691
692 void Channel::WriteChannelWithServ(const char* ServName, const char* text, ...)
693 {
694         char textbuffer[MAXBUF];
695         va_list argsPtr;
696
697         if (!text)
698                 return;
699
700         va_start(argsPtr, text);
701         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
702         va_end(argsPtr);
703
704         this->WriteChannelWithServ(ServName, std::string(textbuffer));
705 }
706
707 void Channel::WriteChannelWithServ(const char* ServName, const std::string &text)
708 {
709         CUList *ulist = this->GetUsers();
710         char tb[MAXBUF];
711
712         snprintf(tb,MAXBUF,":%s %s",ServName ? ServName : ServerInstance->Config->ServerName, text.c_str());
713         std::string out = tb;
714
715         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
716         {
717                 if (IS_LOCAL(i->first))
718                         i->first->Write(out);
719         }
720 }
721
722 /* write formatted text from a source user to all users on a channel except
723  * for the sender (for privmsg etc) */
724 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const char* text, ...)
725 {
726         char textbuffer[MAXBUF];
727         va_list argsPtr;
728
729         if (!text)
730                 return;
731
732         va_start(argsPtr, text);
733         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
734         va_end(argsPtr);
735
736         this->WriteAllExceptSender(user, serversource, status, std::string(textbuffer));
737 }
738
739 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const char* text, ...)
740 {
741         char textbuffer[MAXBUF];
742         va_list argsPtr;
743
744         if (!text)
745                 return;
746
747         va_start(argsPtr, text);
748         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
749         va_end(argsPtr);
750
751         this->WriteAllExcept(user, serversource, status, except_list, std::string(textbuffer));
752 }
753
754 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string &text)
755 {
756         CUList *ulist = this->GetUsers();
757         char tb[MAXBUF];
758
759         snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());
760         std::string out = tb;
761
762         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
763         {
764                 if ((IS_LOCAL(i->first)) && (except_list.find(i->first) == except_list.end()))
765                 {
766                         /* User doesnt have the status we're after */
767                         if (status && !strchr(this->GetAllPrefixChars(i->first), status))
768                                 continue;
769
770                         if (serversource)
771                                 i->first->WriteServ(text);
772                         else
773                                 i->first->Write(out);
774                 }
775         }
776 }
777
778 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const std::string& text)
779 {
780         CUList except_list;
781         except_list[user] = user->nick;
782         this->WriteAllExcept(user, serversource, status, except_list, std::string(text));
783 }
784
785 /*
786  * return a count of the users on a specific channel accounting for
787  * invisible users who won't increase the count. e.g. for /LIST
788  */
789 int Channel::CountInvisible()
790 {
791         int count = 0;
792         CUList *ulist= this->GetUsers();
793         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
794         {
795                 if (!(i->first->IsModeSet('i')))
796                         count++;
797         }
798
799         return count;
800 }
801
802 char* Channel::ChanModes(bool showkey)
803 {
804         static char scratch[MAXBUF];
805         static char sparam[MAXBUF];
806         char* offset = scratch;
807         std::string extparam;
808
809         *scratch = '\0';
810         *sparam = '\0';
811
812         /* This was still iterating up to 190, Channel::modes is only 64 elements -- Om */
813         for(int n = 0; n < 64; n++)
814         {
815                 if(this->modes[n])
816                 {
817                         *offset++ = n + 65;
818                         extparam.clear();
819                         switch (n)
820                         {
821                                 case CM_KEY:
822                                         extparam = (showkey ? this->key : "<key>");
823                                 break;
824                                 case CM_LIMIT:
825                                         extparam = ConvToStr(this->limit);
826                                 break;
827                                 case CM_NOEXTERNAL:
828                                 case CM_TOPICLOCK:
829                                 case CM_INVITEONLY:
830                                 case CM_MODERATED:
831                                 case CM_SECRET:
832                                 case CM_PRIVATE:
833                                         /* We know these have no parameters */
834                                 break;
835                                 default:
836                                         extparam = this->GetModeParameter(n + 65);
837                                 break;
838                         }
839                         if (!extparam.empty())
840                         {
841                                 charlcat(sparam,' ',MAXBUF);
842                                 strlcat(sparam,extparam.c_str(),MAXBUF);
843                         }
844                 }
845         }
846
847         /* Null terminate scratch */
848         *offset = '\0';
849         strlcat(scratch,sparam,MAXBUF);
850         return scratch;
851 }
852
853 /* compile a userlist of a channel into a string, each nick seperated by
854  * spaces and op, voice etc status shown as @ and +, and send it to 'user'
855  */
856 void Channel::UserList(User *user, CUList *ulist)
857 {
858         char list[MAXBUF];
859         size_t dlen, curlen;
860         int MOD_RESULT = 0;
861         bool call_modules = true;
862
863         if (!IS_LOCAL(user))
864                 return;
865
866         FOREACH_RESULT(I_OnUserList,OnUserList(user, this, ulist));
867         if (MOD_RESULT == 1)
868                 call_modules = false;
869
870         if (MOD_RESULT != -1)
871         {
872                 if ((this->IsModeSet('s')) && (!this->HasUser(user)))
873                 {
874                         user->WriteNumeric(401, "%s %s :No such nick/channel",user->nick, this->name);
875                         return;
876                 }
877         }
878
879         dlen = curlen = snprintf(list,MAXBUF,"%s %c %s :", user->nick, this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=',  this->name);
880
881         int numusers = 0;
882         char* ptr = list + dlen;
883
884         if (!ulist)
885                 ulist = this->GetUsers();
886
887         /* Improvement by Brain - this doesnt change in value, so why was it inside
888          * the loop?
889          */
890         bool has_user = this->HasUser(user);
891
892         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
893         {
894                 if ((!has_user) && (i->first->IsModeSet('i')))
895                 {
896                         /*
897                          * user is +i, and source not on the channel, does not show
898                          * nick in NAMES list
899                          */
900                         continue;
901                 }
902
903                 if (i->first->Visibility && !i->first->Visibility->VisibleTo(user))
904                         continue;
905
906                 std::string prefixlist = this->GetPrefixChar(i->first);
907                 std::string nick = i->first->nick;
908
909                 if (call_modules)
910                 {
911                         FOREACH_MOD(I_OnNamesListItem, OnNamesListItem(user, i->first, this, prefixlist, nick));
912         
913                         /* Nick was nuked, a module wants us to skip it */
914                         if (nick.empty())
915                                 continue;
916                 }
917                 
918                 size_t ptrlen = 0;
919
920                 if (curlen > (480-NICKMAX))
921                 {
922                         /* list overflowed into multiple numerics */
923                         user->WriteServ(std::string(list));
924
925                         /* reset our lengths */
926                         dlen = curlen = snprintf(list,MAXBUF,"%s %c %s :", user->nick, this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=', this->name);
927                         ptr = list + dlen;
928
929                         ptrlen = 0;
930                         numusers = 0;
931                 }
932
933                 ptrlen = snprintf(ptr, MAXBUF, "%s%s ", prefixlist.c_str(), nick.c_str());
934
935                 curlen += ptrlen;
936                 ptr += ptrlen;
937
938                 numusers++;
939         }
940
941         /* if whats left in the list isnt empty, send it */
942         if (numusers)
943         {
944                 user->WriteNumeric(353,std::string(list));
945         }
946
947         user->WriteNumeric(366, "%s %s :End of /NAMES list.", user->nick, this->name);
948 }
949
950 long Channel::GetMaxBans()
951 {
952         /* Return the cached value if there is one */
953         if (this->maxbans)
954                 return this->maxbans;
955
956         /* If there isnt one, we have to do some O(n) hax to find it the first time. (ick) */
957         for (std::map<std::string,int>::iterator n = ServerInstance->Config->maxbans.begin(); n != ServerInstance->Config->maxbans.end(); n++)
958         {
959                 if (match(this->name,n->first.c_str()))
960                 {
961                         this->maxbans = n->second;
962                         return n->second;
963                 }
964         }
965
966         /* Screw it, just return the default of 64 */
967         this->maxbans = 64;
968         return this->maxbans;
969 }
970
971 void Channel::ResetMaxBans()
972 {
973         this->maxbans = 0;
974 }
975
976 /* returns the status character for a given user on a channel, e.g. @ for op,
977  * % for halfop etc. If the user has several modes set, the highest mode
978  * the user has must be returned.
979  */
980 const char* Channel::GetPrefixChar(User *user)
981 {
982         static char pf[2] = {0, 0};
983         
984         prefixlist::iterator n = prefixes.find(user);
985         if (n != prefixes.end())
986         {
987                 if (n->second.size())
988                 {
989                         /* If the user has any prefixes, their highest prefix
990                          * will always be at the head of the list, as the list is
991                          * sorted in rank order highest first (see SetPrefix()
992                          * for reasons why)
993                          */
994                         *pf = n->second.begin()->first;
995                         return pf;
996                 }
997         }
998
999         *pf = 0;
1000         return pf;
1001 }
1002
1003
1004 const char* Channel::GetAllPrefixChars(User* user)
1005 {
1006         static char prefix[MAXBUF];
1007         int ctr = 0;
1008         *prefix = 0;
1009
1010         prefixlist::iterator n = prefixes.find(user);
1011         if (n != prefixes.end())
1012         {
1013                 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
1014                 {
1015                         prefix[ctr++] = x->first;
1016                 }
1017         }
1018
1019         prefix[ctr] = 0;
1020
1021         return prefix;
1022 }
1023
1024 unsigned int Channel::GetPrefixValue(User* user)
1025 {
1026         prefixlist::iterator n = prefixes.find(user);
1027         if (n != prefixes.end())
1028         {
1029                 if (n->second.size())
1030                         return n->second.begin()->second;
1031         }
1032         return 0;
1033 }
1034
1035 int Channel::GetStatusFlags(User *user)
1036 {
1037         UCListIter i = user->chans.find(this);
1038         if (i != user->chans.end())
1039         {
1040                 return i->second;
1041         }
1042         return 0;
1043 }
1044
1045 int Channel::GetStatus(User *user)
1046 {
1047         if (ServerInstance->ULine(user->server))
1048                 return STATUS_OP;
1049
1050         UCListIter i = user->chans.find(this);
1051         if (i != user->chans.end())
1052         {
1053                 if ((i->second & UCMODE_OP) > 0)
1054                 {
1055                         return STATUS_OP;
1056                 }
1057                 if ((i->second & UCMODE_HOP) > 0)
1058                 {
1059                         return STATUS_HOP;
1060                 }
1061                 if ((i->second & UCMODE_VOICE) > 0)
1062                 {
1063                         return STATUS_VOICE;
1064                 }
1065                 return STATUS_NORMAL;
1066         }
1067         return STATUS_NORMAL;
1068 }
1069
1070 void Channel::SetPrefix(User* user, char prefix, unsigned int prefix_value, bool adding)
1071 {
1072         prefixlist::iterator n = prefixes.find(user);
1073         prefixtype pfx = std::make_pair(prefix,prefix_value);
1074         if (adding)
1075         {
1076                 if (n != prefixes.end())
1077                 {
1078                         if (std::find(n->second.begin(), n->second.end(), pfx) == n->second.end())
1079                         {
1080                                 n->second.push_back(pfx);
1081                                 /* We must keep prefixes in rank order, largest first.
1082                                  * This is for two reasons, firstly because x-chat *ass-u-me's* this
1083                                  * state, and secondly it turns out to be a benefit to us later.
1084                                  * See above in GetPrefix().
1085                                  */
1086                                 std::sort(n->second.begin(), n->second.end(), ModeParser::PrefixComparison);
1087                         }
1088                 }
1089                 else
1090                 {
1091                         pfxcontainer one;
1092                         one.push_back(pfx);
1093                         prefixes.insert(std::make_pair<User*,pfxcontainer>(user, one));
1094                 }
1095         }
1096         else
1097         {
1098                 if (n != prefixes.end())
1099                 {
1100                         pfxcontainer::iterator x = std::find(n->second.begin(), n->second.end(), pfx);
1101                         if (x != n->second.end())
1102                                 n->second.erase(x);
1103                 }
1104         }
1105 }
1106
1107 void Channel::RemoveAllPrefixes(User* user)
1108 {
1109         prefixlist::iterator n = prefixes.find(user);
1110         if (n != prefixes.end())
1111         {
1112                 prefixes.erase(n);
1113         }
1114 }
1115