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