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