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