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