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