]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/channels.cpp
Just to mess with om's head, remove helperfuncs.h from everywhere
[user/henk/code/inspircd.git] / src / channels.cpp
1 /*   +------------------------------------+
2  *   | Inspire Internet Relay Chat Daemon |
3  *   +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *   E-mail:
7  *        <brain@chatspike.net>
8  *        <Craig@chatspike.net>
9  * 
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *      the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include <string>
20 #include <map>
21 #include <sstream>
22 #include <vector>
23 #include <deque>
24 #include <stdarg.h>
25 #include "configreader.h"
26 #include "inspircd.h"
27 #include "hash_map.h"
28 #include "users.h"
29 #include "ctables.h"
30 #include "globals.h"
31 #include "modules.h"
32 #include "dynamic.h"
33 #include "commands.h"
34 #include "wildcard.h"
35 #include "mode.h"
36 #include "xline.h"
37 #include "inspstring.h"
38
39 #include "typedefs.h"
40
41 chanrec::chanrec(InspIRCd* Instance) : ServerInstance(Instance)
42 {
43         *name = *topic = *setby = *key = 0;
44         created = topicset = limit = 0;
45         internal_userlist.clear();
46         memset(&modes,0,64);
47 }
48
49 void chanrec::SetMode(char mode,bool mode_on)
50 {
51         modes[mode-65] = mode_on;
52         if (!mode_on)
53                 this->SetModeParam(mode,"",false);
54 }
55
56
57 void chanrec::SetModeParam(char mode,const char* parameter,bool mode_on)
58 {
59         ServerInstance->Log(DEBUG,"SetModeParam called");
60         
61         CustomModeList::iterator n = custom_mode_params.find(mode);     
62
63         if (mode_on)
64         {
65                 if (n == custom_mode_params.end())
66                 {
67                         custom_mode_params[mode] = strdup(parameter);
68                         ServerInstance->Log(DEBUG,"Custom mode parameter %c %s added",mode,parameter);
69                 }
70                 else
71                 {
72                         ServerInstance->Log(DEBUG, "Tried to set custom mode parameter for %c '%s' when it was already '%s'", mode, parameter, n->second);
73                 }
74         }
75         else
76         {
77                 if (n != custom_mode_params.end())
78                 {
79                         free(n->second);
80                         custom_mode_params.erase(n);
81                 }
82         }
83 }
84
85 bool chanrec::IsModeSet(char mode)
86 {
87         return modes[mode-65];
88 }
89
90 std::string chanrec::GetModeParameter(char mode)
91 {
92         if (mode == 'k')
93         {
94                 return this->key;
95         }
96         else if (mode == 'l')
97         {
98                 return ConvToStr(this->limit);
99         }
100         else
101         {
102                 CustomModeList::iterator n = custom_mode_params.find(mode);
103                 if (n != custom_mode_params.end())
104                 {
105                         return n->second;
106                 }
107                 return "";
108         }
109 }
110
111 long chanrec::GetUserCounter()
112 {
113         return (this->internal_userlist.size());
114 }
115
116 void chanrec::AddUser(userrec* user)
117 {
118         internal_userlist[user] = user;
119 }
120
121 unsigned long chanrec::DelUser(userrec* user)
122 {
123         CUListIter a = internal_userlist.find(user);
124         
125         if (a != internal_userlist.end())
126         {
127                 internal_userlist.erase(a);
128                 /* And tidy any others... */
129                 DelOppedUser(user);
130                 DelHalfoppedUser(user);
131                 DelVoicedUser(user);
132         }
133         
134         return internal_userlist.size();
135 }
136
137 bool chanrec::HasUser(userrec* user)
138 {
139         return (internal_userlist.find(user) != internal_userlist.end());
140 }
141
142 void chanrec::AddOppedUser(userrec* user)
143 {
144         internal_op_userlist[user] = user;
145 }
146
147 void chanrec::DelOppedUser(userrec* user)
148 {
149         CUListIter a = internal_op_userlist.find(user);
150         if (a != internal_op_userlist.end())
151         {
152                 internal_op_userlist.erase(a);
153                 return;
154         }
155 }
156
157 void chanrec::AddHalfoppedUser(userrec* user)
158 {
159         internal_halfop_userlist[user] = user;
160 }
161
162 void chanrec::DelHalfoppedUser(userrec* user)
163 {
164         CUListIter a = internal_halfop_userlist.find(user);
165
166         if (a != internal_halfop_userlist.end())
167         {   
168                 internal_halfop_userlist.erase(a);
169         }
170 }
171
172 void chanrec::AddVoicedUser(userrec* user)
173 {
174         internal_voice_userlist[user] = user;
175 }
176
177 void chanrec::DelVoicedUser(userrec* user)
178 {
179         CUListIter a = internal_voice_userlist.find(user);
180         
181         if (a != internal_voice_userlist.end())
182         {
183                 internal_voice_userlist.erase(a);
184         }
185 }
186
187 CUList* chanrec::GetUsers()
188 {
189         return &internal_userlist;
190 }
191
192 CUList* chanrec::GetOppedUsers()
193 {
194         return &internal_op_userlist;
195 }
196
197 CUList* chanrec::GetHalfoppedUsers()
198 {
199         return &internal_halfop_userlist;
200 }
201
202 CUList* chanrec::GetVoicedUsers()
203 {
204         return &internal_voice_userlist;
205 }
206
207 /* 
208  * add a channel to a user, creating the record for it if needed and linking
209  * it to the user record 
210  */
211 chanrec* chanrec::JoinUser(InspIRCd* Instance, userrec *user, const char* cn, bool override, const char* key)
212 {
213         if (!user || !cn)
214                 return NULL;
215
216         int created = 0;
217         char cname[MAXBUF];
218         int MOD_RESULT = 0;
219         strlcpy(cname,cn,CHANMAX);
220
221         chanrec* Ptr = Instance->FindChan(cname);
222
223         if (!Ptr)
224         {
225                 if (user->fd > -1)
226                 {
227                         MOD_RESULT = 0;
228                         FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,NULL,cname));
229                         if (MOD_RESULT == 1)
230                                 return NULL;
231                 }
232
233                 /* create a new one */
234                 Ptr = new chanrec(Instance);
235                 Instance->chanlist[cname] = Ptr;
236
237                 strlcpy(Ptr->name, cname,CHANMAX);
238                 Ptr->modes[CM_TOPICLOCK] = Ptr->modes[CM_NOEXTERNAL] = 1;
239                 Ptr->created = Instance->Time();
240                 *Ptr->topic = 0;
241                 strlcpy(Ptr->setby, user->nick,NICKMAX-1);
242                 Ptr->topicset = 0;
243                 Instance->Log(DEBUG,"chanrec::JoinUser(): created: %s",cname);
244                 /*
245                  * set created to 2 to indicate user
246                  * is the first in the channel
247                  * and should be given ops
248                  */
249                 created = 2;
250         }
251         else
252         {
253                 /* Already on the channel */
254                 if (Ptr->HasUser(user))
255                         return NULL;
256
257                 /*
258                  * remote users are allowed us to bypass channel modes
259                  * and bans (used by servers)
260                  */
261                 if (IS_LOCAL(user)) /* was a check on fd > -1 */
262                 {
263                         MOD_RESULT = 0;
264                         FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname));
265                         if (MOD_RESULT == 1)
266                         {
267                                 return NULL;
268                         }
269                         else if (MOD_RESULT == 0)
270                         {
271                                 if (*Ptr->key)
272                                 {
273                                         MOD_RESULT = 0;
274                                         FOREACH_RESULT_I(Instance,I_OnCheckKey,OnCheckKey(user, Ptr, key ? key : ""));
275                                         if (!MOD_RESULT)
276                                         {
277                                                 if (!key)
278                                                 {
279                                                         Instance->Log(DEBUG,"chanrec::JoinUser(): no key given in JOIN");
280                                                         user->WriteServ("475 %s %s :Cannot join channel (Requires key)",user->nick, Ptr->name);
281                                                         return NULL;
282                                                 }
283                                                 else
284                                                 {
285                                                         if (strcmp(key,Ptr->key))
286                                                         {
287                                                                 Instance->Log(DEBUG,"chanrec::JoinUser(): bad key given in JOIN");
288                                                                 user->WriteServ("475 %s %s :Cannot join channel (Incorrect key)",user->nick, Ptr->name);
289                                                                 return NULL;
290                                                         }
291                                                 }
292                                         }
293                                 }
294                                 if (Ptr->modes[CM_INVITEONLY])
295                                 {
296                                         MOD_RESULT = 0;
297                                         irc::string xname(Ptr->name);
298                                         FOREACH_RESULT_I(Instance,I_OnCheckInvite,OnCheckInvite(user, Ptr));
299                                         if (!MOD_RESULT)
300                                         {
301                                                 if (user->IsInvited(xname))
302                                                 {
303                                                         /* user was invited to channel */
304                                                         /* there may be an optional channel NOTICE here */
305                                                 }
306                                                 else
307                                                 {
308                                                         user->WriteServ("473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
309                                                         return NULL;
310                                                 }
311                                         }
312                                         user->RemoveInvite(xname);
313                                 }
314                                 if (Ptr->limit)
315                                 {
316                                         MOD_RESULT = 0;
317                                         FOREACH_RESULT_I(Instance,I_OnCheckLimit,OnCheckLimit(user, Ptr));
318                                         if (!MOD_RESULT)
319                                         {
320                                                 if (Ptr->GetUserCounter() >= Ptr->limit)
321                                                 {
322                                                         user->WriteServ("471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
323                                                         return NULL;
324                                                 }
325                                         }
326                                 }
327                                 if (Ptr->bans.size())
328                                 {
329                                         MOD_RESULT = 0;
330                                         FOREACH_RESULT_I(Instance,I_OnCheckBan,OnCheckBan(user, Ptr));
331                                         char mask[MAXBUF];
332                                         sprintf(mask,"%s!%s@%s",user->nick, user->ident, user->GetIPString());
333                                         if (!MOD_RESULT)
334                                         {
335                                                 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
336                                                 {
337                                                         /* This allows CIDR ban matching
338                                                          * 
339                                                          *        Full masked host                      Full unmasked host                   IP with/without CIDR
340                                                          */
341                                                         if ((match(user->GetFullHost(),i->data)) || (match(user->GetFullRealHost(),i->data)) || (match(mask, i->data, true)))
342                                                         {
343                                                                 user->WriteServ("474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
344                                                                 return NULL;
345                                                         }
346                                                 }
347                                         }
348                                 }
349                         }
350                 }
351                 else
352                 {
353                         Instance->Log(DEBUG,"chanrec::JoinUser(): Overridden checks");
354                 }
355                 created = 1;
356         }
357
358         for (UserChanList::const_iterator index = user->chans.begin(); index != user->chans.end(); index++)
359         {
360                 if ((*index)->channel == NULL)
361                 {
362                         return chanrec::ForceChan(Instance, Ptr, *index, user, created);
363                 }
364         }
365
366         /*
367          * XXX: If the user is an oper here, we can just extend their user->chans vector by one
368          * and put the channel in here. Same for remote users which are not bound by
369          * the channel limits. Otherwise, nope, youre boned.
370          */
371         if (!IS_LOCAL(user)) /* was a check on fd < 0 */
372         {
373                 ucrec* a = new ucrec();
374                 chanrec* c = chanrec::ForceChan(Instance, Ptr,a,user,created);
375                 user->chans.push_back(a);
376                 return c;
377         }
378         else if (*user->oper)
379         {
380                 /* Oper allows extension up to the OPERMAXCHANS value */
381                 if (user->chans.size() < OPERMAXCHANS)
382                 {
383                         ucrec* a = new ucrec();
384                         chanrec* c = chanrec::ForceChan(Instance, Ptr,a,user,created);
385                         user->chans.push_back(a);
386                         return c;
387                 }
388         }
389
390         user->WriteServ("405 %s %s :You are on too many channels",user->nick, cname);
391
392         if (created == 2)
393         {
394                 Instance->Log(DEBUG,"BLAMMO, Whacking channel.");
395                 /* Things went seriously pear shaped, so take this away. bwahaha. */
396                 chan_hash::iterator n = Instance->chanlist.find(cname);
397                 if (n != Instance->chanlist.end())
398                 {
399                         Ptr->DelUser(user);
400                         DELETE(Ptr);
401                         Instance->chanlist.erase(n);
402                         for (unsigned int index =0; index < user->chans.size(); index++)
403                         {
404                                 if (user->chans[index]->channel == Ptr)
405                                 {
406                                         user->chans[index]->channel = NULL;
407                                         user->chans[index]->uc_modes = 0;       
408                                 }
409                         }
410                 }
411         }
412         else
413         {
414                 for (unsigned int index =0; index < user->chans.size(); index++)
415                 {
416                         if (user->chans[index]->channel == Ptr)
417                         {
418                                 user->chans[index]->channel = NULL;
419                                 user->chans[index]->uc_modes = 0;
420                         }
421                 }
422         }
423         return NULL;
424 }
425
426 chanrec* chanrec::ForceChan(InspIRCd* Instance, chanrec* Ptr,ucrec *a,userrec* user, int created)
427 {
428         if (created == 2)
429         {
430                 /* first user in is given ops */
431                 a->uc_modes = UCMODE_OP;
432                 Ptr->AddOppedUser(user);
433         }
434         else
435         {
436                 a->uc_modes = 0;
437         }
438
439         a->channel = Ptr;
440         Ptr->AddUser(user);
441         Ptr->WriteChannel(user,"JOIN :%s",Ptr->name);
442
443         /* Major improvement by Brain - we dont need to be calculating all this pointlessly for remote users */
444         if (IS_LOCAL(user))
445         {
446                 if (Ptr->topicset)
447                 {
448                         user->WriteServ("332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
449                         user->WriteServ("333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
450                 }
451                 Ptr->UserList(user);
452                 user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
453         }
454         FOREACH_MOD_I(Instance,I_OnUserJoin,OnUserJoin(user,Ptr));
455         return Ptr;
456 }
457
458 /* chanrec::PartUser
459  * remove a channel from a users record, and remove the record from the hash
460  * if the channel has become empty
461  */
462 long chanrec::PartUser(userrec *user, const char* reason)
463 {
464         if (!user)
465                 return this->GetUserCounter();
466
467         for (unsigned int i =0; i < user->chans.size(); i++)
468         {
469                 /* zap it from the channel list of the user */
470                 if (user->chans[i]->channel == this)
471                 {
472                         if (reason)
473                         {
474                                 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, reason));
475                                 this->WriteChannel(user, "PART %s :%s", this->name, reason);
476                         }
477                         else
478                         {
479                                 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, ""));
480                                 this->WriteChannel(user, "PART :%s", this->name);
481                         }
482                         user->chans[i]->uc_modes = 0;
483                         user->chans[i]->channel = NULL;
484                         break;
485                 }
486         }
487
488         if (!this->DelUser(user)) /* if there are no users left on the channel... */
489         {
490                 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
491                 /* kill the record */
492                 if (iter != ServerInstance->chanlist.end())
493                 {
494                         ServerInstance->Log(DEBUG,"del_channel: destroyed: %s", this->name);
495                         FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
496                         ServerInstance->chanlist.erase(iter);
497                 }
498                 return 0;
499         }
500
501         return this->GetUserCounter();
502 }
503
504 long chanrec::ServerKickUser(userrec* user, const char* reason, bool triggerevents)
505 {
506         if (!user || !reason)
507                 return this->GetUserCounter();
508
509         if (IS_LOCAL(user))
510         {
511                 if (!this->HasUser(user))
512                 {
513                         /* Not on channel */
514                         return this->GetUserCounter();
515                 }
516         }
517
518         if (triggerevents)
519         {
520                 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL,user,this,reason));
521         }
522
523         for (unsigned int i =0; i < user->chans.size(); i++)
524         {
525                 if (user->chans[i]->channel == this)
526                 {
527                         this->WriteChannelWithServ(ServerInstance->Config->ServerName, "KICK %s %s :%s", this->name, user->nick, reason);
528                         user->chans[i]->uc_modes = 0;
529                         user->chans[i]->channel = NULL;
530                         break;
531                 }
532         }
533
534         if (!this->DelUser(user))
535         {
536                 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
537                 /* kill the record */
538                 if (iter != ServerInstance->chanlist.end())
539                 {
540                         FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
541                         ServerInstance->chanlist.erase(iter);
542                 }
543                 return 0;
544         }
545
546         return this->GetUserCounter();
547 }
548
549 long chanrec::KickUser(userrec *src, userrec *user, const char* reason)
550 {
551         if (!src || !user || !reason)
552                 return this->GetUserCounter();
553
554         if (IS_LOCAL(src))
555         {
556                 if (!this->HasUser(user))
557                 {
558                         src->WriteServ("441 %s %s %s :They are not on that channel",src->nick, user->nick, this->name);
559                         return this->GetUserCounter();
560                 }
561                 if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
562                 {
563                         src->WriteServ("482 %s %s :Only a u-line may kick a u-line from a channel.",src->nick, this->name);
564                         return this->GetUserCounter();
565                 }
566                 int MOD_RESULT = 0;
567
568                 if (!ServerInstance->ULine(src->server))
569                 {
570                         MOD_RESULT = 0;
571                         FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,this,reason));
572                         if (MOD_RESULT == 1)
573                                 return this->GetUserCounter();
574                 }
575                 /* Set to -1 by OnUserPreKick if explicit allow was set */
576                 if (MOD_RESULT != -1)
577                 {
578                         FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,this,AC_KICK));
579                         if ((MOD_RESULT == ACR_DENY) && (!ServerInstance->ULine(src->server)))
580                                 return this->GetUserCounter();
581         
582                         if ((MOD_RESULT == ACR_DEFAULT) || (!ServerInstance->ULine(src->server)))
583                         {
584                                 int them = this->GetStatus(src);
585                                 int us = this->GetStatus(user);
586                                 if ((them < STATUS_HOP) || (them < us))
587                                 {
588                                         if (them == STATUS_HOP)
589                                         {
590                                                 src->WriteServ("482 %s %s :You must be a channel operator",src->nick, this->name);
591                                         }
592                                         else
593                                         {
594                                                 src->WriteServ("482 %s %s :You must be at least a half-operator",src->nick, this->name);
595                                         }
596                                         return this->GetUserCounter();
597                                 }
598                         }
599                 }
600         }
601
602         FOREACH_MOD(I_OnUserKick,OnUserKick(src,user,this,reason));
603                         
604         for (UserChanList::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
605         {
606                 /* zap it from the channel list of the user */
607                 if ((*i)->channel == this)
608                 {
609                         this->WriteChannel(src, "KICK %s %s :%s", this->name, user->nick, reason);
610                         (*i)->uc_modes = 0;
611                         (*i)->channel = NULL;
612                         break;
613                 }
614         }
615
616         if (!this->DelUser(user))
617         /* if there are no users left on the channel */
618         {
619                 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
620
621                 /* kill the record */
622                 if (iter != ServerInstance->chanlist.end())
623                 {
624                         FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
625                         ServerInstance->chanlist.erase(iter);
626                 }
627                 return 0;
628         }
629
630         return this->GetUserCounter();
631 }
632
633 void chanrec::WriteChannel(userrec* user, char* text, ...)
634 {
635         char textbuffer[MAXBUF];
636         va_list argsPtr;
637
638         if (!user || !text)
639                 return;
640
641         va_start(argsPtr, text);
642         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
643         va_end(argsPtr);
644
645         this->WriteChannel(user, std::string(textbuffer));
646 }
647
648 void chanrec::WriteChannel(userrec* user, const std::string &text)
649 {
650         CUList *ulist = this->GetUsers();
651
652         if (!user)
653                 return;
654
655         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
656         {
657                 if (i->second->fd != FD_MAGIC_NUMBER)
658                         user->WriteTo(i->second,text);
659         }
660 }
661
662 void chanrec::WriteChannelWithServ(const char* ServName, const char* text, ...)
663 {
664         char textbuffer[MAXBUF];
665         va_list argsPtr;
666
667         if (!text)
668                 return;
669
670         va_start(argsPtr, text);
671         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
672         va_end(argsPtr);
673
674         this->WriteChannelWithServ(ServName, std::string(textbuffer));
675 }
676
677 void chanrec::WriteChannelWithServ(const char* ServName, const std::string &text)
678 {
679         CUList *ulist = this->GetUsers();
680
681         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
682         {
683                 if (IS_LOCAL(i->second))
684                         i->second->WriteServ(text);
685         }
686 }
687
688 /* write formatted text from a source user to all users on a channel except
689  * for the sender (for privmsg etc) */
690 void chanrec::WriteAllExceptSender(userrec* user, char status, char* text, ...)
691 {
692         char textbuffer[MAXBUF];
693         va_list argsPtr;
694
695         if (!user || !text)
696                 return;
697
698         va_start(argsPtr, text);
699         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
700         va_end(argsPtr);
701
702         this->WriteAllExceptSender(user, status, std::string(textbuffer));
703 }
704
705 void chanrec::WriteAllExceptSender(userrec* user, char status, const std::string& text)
706 {
707         CUList *ulist;
708
709         if (!user)
710                 return;
711
712         switch (status)
713         {
714                 case '@':
715                         ulist = this->GetOppedUsers();
716                         break;
717                 case '%':
718                         ulist = this->GetHalfoppedUsers();
719                         break;
720                 case '+':
721                         ulist = this->GetVoicedUsers();
722                         break;
723                 default:
724                         ulist = this->GetUsers();
725                         break;
726         }
727
728         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
729         {
730                 if ((IS_LOCAL(i->second)) && (user != i->second))
731                         i->second->WriteFrom(user,text);
732         }
733 }
734
735 /*
736  * return a count of the users on a specific channel accounting for
737  * invisible users who won't increase the count. e.g. for /LIST
738  */
739 int chanrec::CountInvisible()
740 {
741         int count = 0;
742         CUList *ulist= this->GetUsers();
743         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
744         {
745                 if (!(i->second->modes[UM_INVISIBLE]))
746                         count++;
747         }
748
749         return count;
750 }
751
752 char* chanrec::ChanModes(bool showkey)
753 {
754         static char scratch[MAXBUF];
755         static char sparam[MAXBUF];
756         char* offset = scratch;
757         std::string extparam = "";
758
759         *scratch = '\0';
760         *sparam = '\0';
761
762         /* This was still iterating up to 190, chanrec::custom_modes is only 64 elements -- Om */
763         for(int n = 0; n < 64; n++)
764         {
765                 if(this->modes[n])
766                 {
767                         *offset++ = n + 65;
768                         extparam = "";
769                         switch (n)
770                         {
771                                 case CM_KEY:
772                                         extparam = (showkey ? this->key : "<key>");
773                                 break;
774                                 case CM_LIMIT:
775                                         extparam = ConvToStr(this->limit);
776                                 break;
777                                 case CM_NOEXTERNAL:
778                                 case CM_TOPICLOCK:
779                                 case CM_INVITEONLY:
780                                 case CM_MODERATED:
781                                 case CM_SECRET:
782                                 case CM_PRIVATE:
783                                         /* We know these have no parameters */
784                                 break;
785                                 default:
786                                         extparam = this->GetModeParameter(n + 65);
787                                 break;
788                         }
789                         if (extparam != "")
790                         {
791                                 charlcat(sparam,' ',MAXBUF);
792                                 strlcat(sparam,extparam.c_str(),MAXBUF);
793                         }
794                 }
795         }
796
797         /* Null terminate scratch */
798         *offset = '\0';
799         strlcat(scratch,sparam,MAXBUF);
800         return scratch;
801 }
802
803 /* compile a userlist of a channel into a string, each nick seperated by
804  * spaces and op, voice etc status shown as @ and +, and send it to 'user'
805  */
806 void chanrec::UserList(userrec *user)
807 {
808         char list[MAXBUF];
809         size_t dlen, curlen;
810
811         dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
812
813         int numusers = 0;
814         char* ptr = list + dlen;
815
816         CUList *ulist= this->GetUsers();
817
818         /* Improvement by Brain - this doesnt change in value, so why was it inside
819          * the loop?
820          */
821         bool has_user = this->HasUser(user);
822
823         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
824         {
825                 if ((!has_user) && (i->second->modes[UM_INVISIBLE]))
826                 {
827                         /*
828                          * user is +i, and source not on the channel, does not show
829                          * nick in NAMES list
830                          */
831                         continue;
832                 }
833
834                 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", this->GetStatusChar(i->second), i->second->nick);
835
836                 curlen += ptrlen;
837                 ptr += ptrlen;
838
839                 numusers++;
840
841                 if (curlen > (480-NICKMAX))
842                 {
843                         /* list overflowed into multiple numerics */
844                         user->WriteServ(list);
845
846                         /* reset our lengths */
847                         dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
848                         ptr = list + dlen;
849
850                         ptrlen = 0;
851                         numusers = 0;
852                 }
853         }
854
855         /* if whats left in the list isnt empty, send it */
856         if (numusers)
857         {
858                 user->WriteServ(list);
859         }
860 }
861
862 long chanrec::GetMaxBans()
863 {
864         std::string x;
865         for (std::map<std::string,int>::iterator n = ServerInstance->Config->maxbans.begin(); n != ServerInstance->Config->maxbans.end(); n++)
866         {
867                 x = n->first;
868                 if (match(this->name,x.c_str()))
869                 {
870                         return n->second;
871                 }
872         }
873         return 64;
874 }
875
876
877 /* returns the status character for a given user on a channel, e.g. @ for op,
878  * % for halfop etc. If the user has several modes set, the highest mode
879  * the user has must be returned. */
880
881 const char* chanrec::GetStatusChar(userrec *user)
882 {
883         for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
884         {
885                 if ((*i)->channel == this)
886                 {
887                         if (((*i)->uc_modes & UCMODE_OP) > 0)
888                         {
889                                 return "@";
890                         }
891                         if (((*i)->uc_modes & UCMODE_HOP) > 0)
892                         {
893                                 return "%";
894                         }
895                         if (((*i)->uc_modes & UCMODE_VOICE) > 0)
896                         {
897                                 return "+";
898                         }
899                         return "";
900                 }
901         }
902         return "";
903 }
904
905
906 int chanrec::GetStatusFlags(userrec *user)
907 {
908         for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
909         {
910                 if ((*i)->channel == this)
911                 {
912                         return (*i)->uc_modes;
913                 }
914         }
915         return 0;
916 }
917
918
919
920 int chanrec::GetStatus(userrec *user)
921 {
922         if (ServerInstance->ULine(user->server))
923                 return STATUS_OP;
924
925         for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
926         {
927                 if ((*i)->channel == this)
928                 {
929                         if (((*i)->uc_modes & UCMODE_OP) > 0)
930                         {
931                                 return STATUS_OP;
932                         }
933                         if (((*i)->uc_modes & UCMODE_HOP) > 0)
934                         {
935                                 return STATUS_HOP;
936                         }
937                         if (((*i)->uc_modes & UCMODE_VOICE) > 0)
938                         {
939                                 return STATUS_VOICE;
940                         }
941                         return STATUS_NORMAL;
942                 }
943         }
944         return STATUS_NORMAL;
945 }
946
947