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