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