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