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