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