]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/channels.cpp
4d68af99cf1d45a6a00dbaf8f389924db4bf3722
[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 (IS_LOCAL(user))
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                 Ptr->SetPrefix(user, '@', OP_VALUE, true);
434         }
435         else
436         {
437                 a->uc_modes = 0;
438         }
439
440         a->channel = Ptr;
441         Ptr->AddUser(user);
442         Ptr->WriteChannel(user,"JOIN :%s",Ptr->name);
443
444         /* Major improvement by Brain - we dont need to be calculating all this pointlessly for remote users */
445         if (IS_LOCAL(user))
446         {
447                 if (Ptr->topicset)
448                 {
449                         user->WriteServ("332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
450                         user->WriteServ("333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
451                 }
452                 Ptr->UserList(user);
453                 user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
454         }
455         FOREACH_MOD_I(Instance,I_OnUserJoin,OnUserJoin(user,Ptr));
456         return Ptr;
457 }
458
459 /* chanrec::PartUser
460  * remove a channel from a users record, and remove the record from the hash
461  * if the channel has become empty
462  */
463 long chanrec::PartUser(userrec *user, const char* reason)
464 {
465         if (!user)
466                 return this->GetUserCounter();
467
468         for (unsigned int i =0; i < user->chans.size(); i++)
469         {
470                 /* zap it from the channel list of the user */
471                 if (user->chans[i]->channel == this)
472                 {
473                         if (reason)
474                         {
475                                 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, reason));
476                                 this->WriteChannel(user, "PART %s :%s", this->name, reason);
477                         }
478                         else
479                         {
480                                 FOREACH_MOD(I_OnUserPart,OnUserPart(user, this, ""));
481                                 this->WriteChannel(user, "PART :%s", this->name);
482                         }
483                         user->chans[i]->uc_modes = 0;
484                         user->chans[i]->channel = NULL;
485                         break;
486                 }
487         }
488
489         if (!this->DelUser(user)) /* if there are no users left on the channel... */
490         {
491                 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
492                 /* kill the record */
493                 if (iter != ServerInstance->chanlist.end())
494                 {
495                         ServerInstance->Log(DEBUG,"del_channel: destroyed: %s", this->name);
496                         FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
497                         ServerInstance->chanlist.erase(iter);
498                 }
499                 return 0;
500         }
501
502         return this->GetUserCounter();
503 }
504
505 long chanrec::ServerKickUser(userrec* user, const char* reason, bool triggerevents)
506 {
507         if (!user || !reason)
508                 return this->GetUserCounter();
509
510         if (IS_LOCAL(user))
511         {
512                 if (!this->HasUser(user))
513                 {
514                         /* Not on channel */
515                         return this->GetUserCounter();
516                 }
517         }
518
519         if (triggerevents)
520         {
521                 FOREACH_MOD(I_OnUserKick,OnUserKick(NULL,user,this,reason));
522         }
523
524         for (unsigned int i =0; i < user->chans.size(); i++)
525         {
526                 if (user->chans[i]->channel == this)
527                 {
528                         this->WriteChannelWithServ(ServerInstance->Config->ServerName, "KICK %s %s :%s", this->name, user->nick, reason);
529                         user->chans[i]->uc_modes = 0;
530                         user->chans[i]->channel = NULL;
531                         break;
532                 }
533         }
534
535         if (!this->DelUser(user))
536         {
537                 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
538                 /* kill the record */
539                 if (iter != ServerInstance->chanlist.end())
540                 {
541                         FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
542                         ServerInstance->chanlist.erase(iter);
543                 }
544                 return 0;
545         }
546
547         return this->GetUserCounter();
548 }
549
550 long chanrec::KickUser(userrec *src, userrec *user, const char* reason)
551 {
552         if (!src || !user || !reason)
553                 return this->GetUserCounter();
554
555         if (IS_LOCAL(src))
556         {
557                 if (!this->HasUser(user))
558                 {
559                         src->WriteServ("441 %s %s %s :They are not on that channel",src->nick, user->nick, this->name);
560                         return this->GetUserCounter();
561                 }
562                 if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
563                 {
564                         src->WriteServ("482 %s %s :Only a u-line may kick a u-line from a channel.",src->nick, this->name);
565                         return this->GetUserCounter();
566                 }
567                 int MOD_RESULT = 0;
568
569                 if (!ServerInstance->ULine(src->server))
570                 {
571                         MOD_RESULT = 0;
572                         FOREACH_RESULT(I_OnUserPreKick,OnUserPreKick(src,user,this,reason));
573                         if (MOD_RESULT == 1)
574                                 return this->GetUserCounter();
575                 }
576                 /* Set to -1 by OnUserPreKick if explicit allow was set */
577                 if (MOD_RESULT != -1)
578                 {
579                         FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(src,user,this,AC_KICK));
580                         if ((MOD_RESULT == ACR_DENY) && (!ServerInstance->ULine(src->server)))
581                                 return this->GetUserCounter();
582         
583                         if ((MOD_RESULT == ACR_DEFAULT) || (!ServerInstance->ULine(src->server)))
584                         {
585                                 int them = this->GetStatus(src);
586                                 int us = this->GetStatus(user);
587                                 if ((them < STATUS_HOP) || (them < us))
588                                 {
589                                         if (them == STATUS_HOP)
590                                         {
591                                                 src->WriteServ("482 %s %s :You must be a channel operator",src->nick, this->name);
592                                         }
593                                         else
594                                         {
595                                                 src->WriteServ("482 %s %s :You must be at least a half-operator",src->nick, this->name);
596                                         }
597                                         return this->GetUserCounter();
598                                 }
599                         }
600                 }
601         }
602
603         FOREACH_MOD(I_OnUserKick,OnUserKick(src,user,this,reason));
604                         
605         for (UserChanList::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
606         {
607                 /* zap it from the channel list of the user */
608                 if ((*i)->channel == this)
609                 {
610                         this->WriteChannel(src, "KICK %s %s :%s", this->name, user->nick, reason);
611                         (*i)->uc_modes = 0;
612                         (*i)->channel = NULL;
613                         break;
614                 }
615         }
616
617         if (!this->DelUser(user))
618         /* if there are no users left on the channel */
619         {
620                 chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
621
622                 /* kill the record */
623                 if (iter != ServerInstance->chanlist.end())
624                 {
625                         FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
626                         ServerInstance->chanlist.erase(iter);
627                 }
628                 return 0;
629         }
630
631         return this->GetUserCounter();
632 }
633
634 void chanrec::WriteChannel(userrec* user, char* text, ...)
635 {
636         char textbuffer[MAXBUF];
637         va_list argsPtr;
638
639         if (!user || !text)
640                 return;
641
642         va_start(argsPtr, text);
643         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
644         va_end(argsPtr);
645
646         this->WriteChannel(user, std::string(textbuffer));
647 }
648
649 void chanrec::WriteChannel(userrec* user, const std::string &text)
650 {
651         CUList *ulist = this->GetUsers();
652
653         if (!user)
654                 return;
655
656         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
657         {
658                 if (IS_LOCAL(i->second))
659                         user->WriteTo(i->second,text);
660         }
661 }
662
663 void chanrec::WriteChannelWithServ(const char* ServName, const char* text, ...)
664 {
665         char textbuffer[MAXBUF];
666         va_list argsPtr;
667
668         if (!text)
669                 return;
670
671         va_start(argsPtr, text);
672         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
673         va_end(argsPtr);
674
675         this->WriteChannelWithServ(ServName, std::string(textbuffer));
676 }
677
678 void chanrec::WriteChannelWithServ(const char* ServName, const std::string &text)
679 {
680         CUList *ulist = this->GetUsers();
681
682         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
683         {
684                 if (IS_LOCAL(i->second))
685                         i->second->WriteServ(text);
686         }
687 }
688
689 /* write formatted text from a source user to all users on a channel except
690  * for the sender (for privmsg etc) */
691 void chanrec::WriteAllExceptSender(userrec* user, char status, char* text, ...)
692 {
693         char textbuffer[MAXBUF];
694         va_list argsPtr;
695
696         if (!user || !text)
697                 return;
698
699         va_start(argsPtr, text);
700         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
701         va_end(argsPtr);
702
703         this->WriteAllExceptSender(user, status, std::string(textbuffer));
704 }
705
706 void chanrec::WriteAllExceptSender(userrec* user, char status, const std::string& text)
707 {
708         CUList *ulist;
709
710         if (!user)
711                 return;
712
713         switch (status)
714         {
715                 case '@':
716                         ulist = this->GetOppedUsers();
717                         break;
718                 case '%':
719                         ulist = this->GetHalfoppedUsers();
720                         break;
721                 case '+':
722                         ulist = this->GetVoicedUsers();
723                         break;
724                 default:
725                         ulist = this->GetUsers();
726                         break;
727         }
728
729         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
730         {
731                 if ((IS_LOCAL(i->second)) && (user != i->second))
732                         i->second->WriteFrom(user,text);
733         }
734 }
735
736 /*
737  * return a count of the users on a specific channel accounting for
738  * invisible users who won't increase the count. e.g. for /LIST
739  */
740 int chanrec::CountInvisible()
741 {
742         int count = 0;
743         CUList *ulist= this->GetUsers();
744         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
745         {
746                 if (!(i->second->modes[UM_INVISIBLE]))
747                         count++;
748         }
749
750         return count;
751 }
752
753 char* chanrec::ChanModes(bool showkey)
754 {
755         static char scratch[MAXBUF];
756         static char sparam[MAXBUF];
757         char* offset = scratch;
758         std::string extparam = "";
759
760         *scratch = '\0';
761         *sparam = '\0';
762
763         /* This was still iterating up to 190, chanrec::custom_modes is only 64 elements -- Om */
764         for(int n = 0; n < 64; n++)
765         {
766                 if(this->modes[n])
767                 {
768                         *offset++ = n + 65;
769                         extparam = "";
770                         switch (n)
771                         {
772                                 case CM_KEY:
773                                         extparam = (showkey ? this->key : "<key>");
774                                 break;
775                                 case CM_LIMIT:
776                                         extparam = ConvToStr(this->limit);
777                                 break;
778                                 case CM_NOEXTERNAL:
779                                 case CM_TOPICLOCK:
780                                 case CM_INVITEONLY:
781                                 case CM_MODERATED:
782                                 case CM_SECRET:
783                                 case CM_PRIVATE:
784                                         /* We know these have no parameters */
785                                 break;
786                                 default:
787                                         extparam = this->GetModeParameter(n + 65);
788                                 break;
789                         }
790                         if (extparam != "")
791                         {
792                                 charlcat(sparam,' ',MAXBUF);
793                                 strlcat(sparam,extparam.c_str(),MAXBUF);
794                         }
795                 }
796         }
797
798         /* Null terminate scratch */
799         *offset = '\0';
800         strlcat(scratch,sparam,MAXBUF);
801         return scratch;
802 }
803
804 /* compile a userlist of a channel into a string, each nick seperated by
805  * spaces and op, voice etc status shown as @ and +, and send it to 'user'
806  */
807 void chanrec::UserList(userrec *user)
808 {
809         char list[MAXBUF];
810         size_t dlen, curlen;
811
812         dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
813
814         int numusers = 0;
815         char* ptr = list + dlen;
816
817         CUList *ulist= this->GetUsers();
818
819         /* Improvement by Brain - this doesnt change in value, so why was it inside
820          * the loop?
821          */
822         bool has_user = this->HasUser(user);
823
824         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
825         {
826                 if ((!has_user) && (i->second->modes[UM_INVISIBLE]))
827                 {
828                         /*
829                          * user is +i, and source not on the channel, does not show
830                          * nick in NAMES list
831                          */
832                         continue;
833                 }
834
835                 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", this->GetStatusChar(i->second), i->second->nick);
836
837                 curlen += ptrlen;
838                 ptr += ptrlen;
839
840                 numusers++;
841
842                 if (curlen > (480-NICKMAX))
843                 {
844                         /* list overflowed into multiple numerics */
845                         user->WriteServ(list);
846
847                         /* reset our lengths */
848                         dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, this->name);
849                         ptr = list + dlen;
850
851                         ptrlen = 0;
852                         numusers = 0;
853                 }
854         }
855
856         /* if whats left in the list isnt empty, send it */
857         if (numusers)
858         {
859                 user->WriteServ(list);
860         }
861 }
862
863 long chanrec::GetMaxBans()
864 {
865         std::string x;
866         for (std::map<std::string,int>::iterator n = ServerInstance->Config->maxbans.begin(); n != ServerInstance->Config->maxbans.end(); n++)
867         {
868                 x = n->first;
869                 if (match(this->name,x.c_str()))
870                 {
871                         return n->second;
872                 }
873         }
874         return 64;
875 }
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::GetStatusChar(userrec *user)
883 {
884         static char px[2];
885         unsigned int mx = 0;
886
887         *px = 0;
888         *(px+1) = 0;
889
890         prefixlist::iterator n = prefixes.find(user);
891         if (n != prefixes.end())
892         {
893                 for (std::vector<prefixtype>::iterator x = n->second.begin(); x != n->second.end(); x++)
894                 {
895                         if (x->second > mx)
896                         {
897                                 *px = x->first;
898                                 mx  = x->second;
899                         }
900                 }
901         }
902
903         return px;
904 }
905
906
907 int chanrec::GetStatusFlags(userrec *user)
908 {
909         for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
910         {
911                 if ((*i)->channel == this)
912                 {
913                         return (*i)->uc_modes;
914                 }
915         }
916         return 0;
917 }
918
919
920
921 int chanrec::GetStatus(userrec *user)
922 {
923         if (ServerInstance->ULine(user->server))
924                 return STATUS_OP;
925
926         for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
927         {
928                 if ((*i)->channel == this)
929                 {
930                         if (((*i)->uc_modes & UCMODE_OP) > 0)
931                         {
932                                 return STATUS_OP;
933                         }
934                         if (((*i)->uc_modes & UCMODE_HOP) > 0)
935                         {
936                                 return STATUS_HOP;
937                         }
938                         if (((*i)->uc_modes & UCMODE_VOICE) > 0)
939                         {
940                                 return STATUS_VOICE;
941                         }
942                         return STATUS_NORMAL;
943                 }
944         }
945         return STATUS_NORMAL;
946 }
947
948 void chanrec::SetPrefix(userrec* user, char prefix, unsigned int prefix_value, bool adding)
949 {
950         prefixlist::iterator n = prefixes.find(user);
951         prefixtype pfx = std::make_pair(prefix,prefix_value);
952         if (adding)
953         {
954                 if (n != prefixes.end())
955                 {
956                         if (std::find(n->second.begin(), n->second.end(), pfx) == n->second.end())
957                         {
958                                 n->second.push_back(pfx);
959                         }
960                 }
961                 else
962                 {
963                         pfxcontainer one;
964                         one.push_back(pfx);
965                         prefixes.insert(std::make_pair<userrec*,pfxcontainer>(user, one));
966                 }
967         }
968         else
969         {
970                 if (n != prefixes.end())
971                 {
972                         pfxcontainer::iterator x = std::find(n->second.begin(), n->second.end(), pfx);
973                         if (x != n->second.end())
974                                 n->second.erase(x);
975                 }
976         }
977 }
978