]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/channels.cpp
m_services_account Remove now needless workaround added to 2.0 to display the correct...
[user/henk/code/inspircd.git] / src / channels.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2006-2008 Robin Burchell <robin+git@viroteck.net>
6  *   Copyright (C) 2006, 2008 Oliver Lupton <oliverlupton@gmail.com>
7  *   Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
8  *   Copyright (C) 2003-2008 Craig Edwards <craigedwards@brainbox.cc>
9  *   Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
10  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
11  *
12  * This file is part of InspIRCd.  InspIRCd is free software: you can
13  * redistribute it and/or modify it under the terms of the GNU General Public
14  * License as published by the Free Software Foundation, version 2.
15  *
16  * This program is distributed in the hope that it will be useful, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
19  * details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24
25
26 #include "inspircd.h"
27 #include "listmode.h"
28 #include <cstdarg>
29 #include "mode.h"
30
31 namespace
32 {
33         ChanModeReference ban(NULL, "ban");
34         ChanModeReference inviteonlymode(NULL, "inviteonly");
35         ChanModeReference keymode(NULL, "key");
36         ChanModeReference limitmode(NULL, "limit");
37         ChanModeReference secretmode(NULL, "secret");
38         ChanModeReference privatemode(NULL, "private");
39         UserModeReference invisiblemode(NULL, "invisible");
40 }
41
42 Channel::Channel(const std::string &cname, time_t ts)
43         : name(cname), age(ts), topicset(0)
44 {
45         if (!ServerInstance->chanlist.insert(std::make_pair(cname, this)).second)
46                 throw CoreException("Cannot create duplicate channel " + cname);
47 }
48
49 void Channel::SetMode(ModeHandler* mh, bool on)
50 {
51         modes[mh->GetId()] = on;
52 }
53
54 void Channel::SetTopic(User* u, const std::string& ntopic)
55 {
56         this->topic.assign(ntopic, 0, ServerInstance->Config->Limits.MaxTopic);
57         this->setby.assign(ServerInstance->Config->FullHostInTopic ? u->GetFullHost() : u->nick, 0, 128);
58         this->WriteChannel(u, "TOPIC %s :%s", this->name.c_str(), this->topic.c_str());
59         this->topicset = ServerInstance->Time();
60
61         FOREACH_MOD(OnPostTopicChange, (u, this, this->topic));
62 }
63
64 Membership* Channel::AddUser(User* user)
65 {
66         Membership*& memb = userlist[user];
67         if (memb)
68                 return NULL;
69
70         memb = new Membership(user, this);
71         return memb;
72 }
73
74 void Channel::DelUser(User* user)
75 {
76         UserMembIter it = userlist.find(user);
77         if (it != userlist.end())
78                 DelUser(it);
79 }
80
81 void Channel::CheckDestroy()
82 {
83         if (!userlist.empty())
84                 return;
85
86         ModResult res;
87         FIRST_MOD_RESULT(OnChannelPreDelete, res, (this));
88         if (res == MOD_RES_DENY)
89                 return;
90
91         chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
92         /* kill the record */
93         if (iter != ServerInstance->chanlist.end())
94         {
95                 FOREACH_MOD(OnChannelDelete, (this));
96                 ServerInstance->chanlist.erase(iter);
97         }
98
99         ClearInvites();
100         ServerInstance->GlobalCulls.AddItem(this);
101 }
102
103 void Channel::DelUser(const UserMembIter& membiter)
104 {
105         Membership* memb = membiter->second;
106         memb->cull();
107         delete memb;
108         userlist.erase(membiter);
109
110         // If this channel became empty then it should be removed
111         CheckDestroy();
112 }
113
114 Membership* Channel::GetUser(User* user)
115 {
116         UserMembIter i = userlist.find(user);
117         if (i == userlist.end())
118                 return NULL;
119         return i->second;
120 }
121
122 void Channel::SetDefaultModes()
123 {
124         ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "SetDefaultModes %s",
125                 ServerInstance->Config->DefaultModes.c_str());
126         irc::spacesepstream list(ServerInstance->Config->DefaultModes);
127         std::string modeseq;
128         std::string parameter;
129
130         list.GetToken(modeseq);
131
132         for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
133         {
134                 ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
135                 if (mode)
136                 {
137                         if (mode->IsPrefixMode())
138                                 continue;
139
140                         if (mode->GetNumParams(true))
141                                 list.GetToken(parameter);
142                         else
143                                 parameter.clear();
144
145                         mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, this, parameter, true);
146                 }
147         }
148 }
149
150 /*
151  * add a channel to a user, creating the record for it if needed and linking
152  * it to the user record
153  */
154 Channel* Channel::JoinUser(LocalUser* user, std::string cname, bool override, const std::string& key)
155 {
156         if (user->registered != REG_ALL)
157         {
158                 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "Attempted to join unregistered user " + user->uuid + " to channel " + cname);
159                 return NULL;
160         }
161
162         /*
163          * We don't restrict the number of channels that remote users or users that are override-joining may be in.
164          * We restrict local users to MaxChans channels.
165          * We restrict local operators to OperMaxChans channels.
166          * This is a lot more logical than how it was formerly. -- w00t
167          */
168         if (!override)
169         {
170                 if (user->HasPrivPermission("channels/high-join-limit"))
171                 {
172                         if (user->chans.size() >= ServerInstance->Config->OperMaxChans)
173                         {
174                                 user->WriteNumeric(ERR_TOOMANYCHANNELS, "%s :You are on too many channels", cname.c_str());
175                                 return NULL;
176                         }
177                 }
178                 else
179                 {
180                         unsigned int maxchans = user->GetClass()->maxchans;
181                         if (!maxchans)
182                                 maxchans = ServerInstance->Config->MaxChans;
183                         if (user->chans.size() >= maxchans)
184                         {
185                                 user->WriteNumeric(ERR_TOOMANYCHANNELS, "%s :You are on too many channels", cname.c_str());
186                                 return NULL;
187                         }
188                 }
189         }
190
191         // Crop channel name if it's too long
192         if (cname.length() > ServerInstance->Config->Limits.ChanMax)
193                 cname.resize(ServerInstance->Config->Limits.ChanMax);
194
195         Channel* chan = ServerInstance->FindChan(cname);
196         bool created_by_local = (chan == NULL); // Flag that will be passed to modules in the OnUserJoin() hook later
197         std::string privs; // Prefix mode(letter)s to give to the joining user
198
199         if (!chan)
200         {
201                 privs = ServerInstance->Config->DefaultModes.substr(0, ServerInstance->Config->DefaultModes.find(' '));
202
203                 if (override == false)
204                 {
205                         // Ask the modules whether they're ok with the join, pass NULL as Channel* as the channel is yet to be created
206                         ModResult MOD_RESULT;
207                         FIRST_MOD_RESULT(OnUserPreJoin, MOD_RESULT, (user, NULL, cname, privs, key));
208                         if (MOD_RESULT == MOD_RES_DENY)
209                                 return NULL; // A module wasn't happy with the join, abort
210                 }
211
212                 chan = new Channel(cname, ServerInstance->Time());
213                 // Set the default modes on the channel (<options:defaultmodes>)
214                 chan->SetDefaultModes();
215         }
216         else
217         {
218                 /* Already on the channel */
219                 if (chan->HasUser(user))
220                         return NULL;
221
222                 if (override == false)
223                 {
224                         ModResult MOD_RESULT;
225                         FIRST_MOD_RESULT(OnUserPreJoin, MOD_RESULT, (user, chan, cname, privs, key));
226
227                         // A module explicitly denied the join and (hopefully) generated a message
228                         // describing the situation, so we may stop here without sending anything
229                         if (MOD_RESULT == MOD_RES_DENY)
230                                 return NULL;
231
232                         // If no module returned MOD_RES_DENY or MOD_RES_ALLOW (which is the case
233                         // most of the time) then proceed to check channel modes +k, +i, +l and bans,
234                         // in this order.
235                         // If a module explicitly allowed the join (by returning MOD_RES_ALLOW),
236                         // then this entire section is skipped
237                         if (MOD_RESULT == MOD_RES_PASSTHRU)
238                         {
239                                 std::string ckey = chan->GetModeParameter(keymode);
240                                 bool invited = user->IsInvited(chan);
241                                 bool can_bypass = ServerInstance->Config->InvBypassModes && invited;
242
243                                 if (!ckey.empty())
244                                 {
245                                         FIRST_MOD_RESULT(OnCheckKey, MOD_RESULT, (user, chan, key));
246                                         if (!MOD_RESULT.check((ckey == key) || can_bypass))
247                                         {
248                                                 // If no key provided, or key is not the right one, and can't bypass +k (not invited or option not enabled)
249                                                 user->WriteNumeric(ERR_BADCHANNELKEY, "%s :Cannot join channel (Incorrect channel key)", chan->name.c_str());
250                                                 return NULL;
251                                         }
252                                 }
253
254                                 if (chan->IsModeSet(inviteonlymode))
255                                 {
256                                         FIRST_MOD_RESULT(OnCheckInvite, MOD_RESULT, (user, chan));
257                                         if (!MOD_RESULT.check(invited))
258                                         {
259                                                 user->WriteNumeric(ERR_INVITEONLYCHAN, "%s :Cannot join channel (Invite only)", chan->name.c_str());
260                                                 return NULL;
261                                         }
262                                 }
263
264                                 std::string limit = chan->GetModeParameter(limitmode);
265                                 if (!limit.empty())
266                                 {
267                                         FIRST_MOD_RESULT(OnCheckLimit, MOD_RESULT, (user, chan));
268                                         if (!MOD_RESULT.check((chan->GetUserCounter() < atol(limit.c_str()) || can_bypass)))
269                                         {
270                                                 user->WriteNumeric(ERR_CHANNELISFULL, "%s :Cannot join channel (Channel is full)", chan->name.c_str());
271                                                 return NULL;
272                                         }
273                                 }
274
275                                 if (chan->IsBanned(user) && !can_bypass)
276                                 {
277                                         user->WriteNumeric(ERR_BANNEDFROMCHAN, "%s :Cannot join channel (You're banned)", chan->name.c_str());
278                                         return NULL;
279                                 }
280
281                                 /*
282                                  * If the user has invites for this channel, remove them now
283                                  * after a successful join so they don't build up.
284                                  */
285                                 if (invited)
286                                 {
287                                         user->RemoveInvite(chan);
288                                 }
289                         }
290                 }
291         }
292
293         // We figured that this join is allowed and also created the
294         // channel if it didn't exist before, now do the actual join
295         chan->ForceJoin(user, &privs, false, created_by_local);
296         return chan;
297 }
298
299 void Channel::ForceJoin(User* user, const std::string* privs, bool bursting, bool created_by_local)
300 {
301         if (IS_SERVER(user))
302         {
303                 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "Attempted to join server user " + user->uuid + " to channel " + this->name);
304                 return;
305         }
306
307         Membership* memb = this->AddUser(user);
308         if (!memb)
309                 return; // Already on the channel
310
311         user->chans.push_front(memb);
312
313         if (privs)
314         {
315                 // If the user was granted prefix modes (in the OnUserPreJoin hook, or he's a
316                 // remote user and his own server set the modes), then set them internally now
317                 for (std::string::const_iterator i = privs->begin(); i != privs->end(); ++i)
318                 {
319                         PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(*i);
320                         if (mh)
321                         {
322                                 std::string nick = user->nick;
323                                 // Set the mode on the user
324                                 mh->OnModeChange(ServerInstance->FakeClient, NULL, this, nick, true);
325                         }
326                 }
327         }
328
329         // Tell modules about this join, they have the chance now to populate except_list with users we won't send the JOIN (and possibly MODE) to
330         CUList except_list;
331         FOREACH_MOD(OnUserJoin, (memb, bursting, created_by_local, except_list));
332
333         this->WriteAllExcept(user, false, 0, except_list, "JOIN :%s", this->name.c_str());
334
335         /* Theyre not the first ones in here, make sure everyone else sees the modes we gave the user */
336         if ((GetUserCounter() > 1) && (!memb->modes.empty()))
337         {
338                 std::string ms = memb->modes;
339                 for(unsigned int i=0; i < memb->modes.length(); i++)
340                         ms.append(" ").append(user->nick);
341
342                 except_list.insert(user);
343                 this->WriteAllExcept(user, !ServerInstance->Config->CycleHostsFromUser, 0, except_list, "MODE %s +%s", this->name.c_str(), ms.c_str());
344         }
345
346         if (IS_LOCAL(user))
347         {
348                 if (this->topicset)
349                 {
350                         user->WriteNumeric(RPL_TOPIC, "%s :%s", this->name.c_str(), this->topic.c_str());
351                         user->WriteNumeric(RPL_TOPICTIME, "%s %s %lu", this->name.c_str(), this->setby.c_str(), (unsigned long)this->topicset);
352                 }
353                 this->UserList(user);
354         }
355
356         FOREACH_MOD(OnPostJoin, (memb));
357 }
358
359 bool Channel::IsBanned(User* user)
360 {
361         ModResult result;
362         FIRST_MOD_RESULT(OnCheckChannelBan, result, (user, this));
363
364         if (result != MOD_RES_PASSTHRU)
365                 return (result == MOD_RES_DENY);
366
367         ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
368         const ListModeBase::ModeList* bans = banlm->GetList(this);
369         if (bans)
370         {
371                 for (ListModeBase::ModeList::const_iterator it = bans->begin(); it != bans->end(); it++)
372                 {
373                         if (CheckBan(user, it->mask))
374                                 return true;
375                 }
376         }
377         return false;
378 }
379
380 bool Channel::CheckBan(User* user, const std::string& mask)
381 {
382         ModResult result;
383         FIRST_MOD_RESULT(OnCheckBan, result, (user, this, mask));
384         if (result != MOD_RES_PASSTHRU)
385                 return (result == MOD_RES_DENY);
386
387         // extbans were handled above, if this is one it obviously didn't match
388         if ((mask.length() <= 2) || (mask[1] == ':'))
389                 return false;
390
391         std::string::size_type at = mask.find('@');
392         if (at == std::string::npos)
393                 return false;
394
395         const std::string nickIdent = user->nick + "!" + user->ident;
396         std::string prefix = mask.substr(0, at);
397         if (InspIRCd::Match(nickIdent, prefix, NULL))
398         {
399                 std::string suffix = mask.substr(at + 1);
400                 if (InspIRCd::Match(user->host, suffix, NULL) ||
401                         InspIRCd::Match(user->dhost, suffix, NULL) ||
402                         InspIRCd::MatchCIDR(user->GetIPString(), suffix, NULL))
403                         return true;
404         }
405         return false;
406 }
407
408 ModResult Channel::GetExtBanStatus(User *user, char type)
409 {
410         ModResult rv;
411         FIRST_MOD_RESULT(OnExtBanCheck, rv, (user, this, type));
412         if (rv != MOD_RES_PASSTHRU)
413                 return rv;
414
415         ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
416         const ListModeBase::ModeList* bans = banlm->GetList(this);
417         if (bans)
418         {
419                 for (ListModeBase::ModeList::const_iterator it = bans->begin(); it != bans->end(); ++it)
420                 {
421                         if (CheckBan(user, it->mask))
422                                 return MOD_RES_DENY;
423                 }
424         }
425         return MOD_RES_PASSTHRU;
426 }
427
428 /* Channel::PartUser
429  * Remove a channel from a users record, remove the reference to the Membership object
430  * from the channel and destroy it.
431  */
432 void Channel::PartUser(User *user, std::string &reason)
433 {
434         UserMembIter membiter = userlist.find(user);
435
436         if (membiter != userlist.end())
437         {
438                 Membership* memb = membiter->second;
439                 CUList except_list;
440                 FOREACH_MOD(OnUserPart, (memb, reason, except_list));
441
442                 WriteAllExcept(user, false, 0, except_list, "PART %s%s%s", this->name.c_str(), reason.empty() ? "" : " :", reason.c_str());
443
444                 // Remove this channel from the user's chanlist
445                 user->chans.erase(memb);
446                 // Remove the Membership from this channel's userlist and destroy it
447                 this->DelUser(membiter);
448         }
449 }
450
451 void Channel::KickUser(User* src, User* victim, const std::string& reason, Membership* srcmemb)
452 {
453         UserMembIter victimiter = userlist.find(victim);
454         Membership* memb = ((victimiter != userlist.end()) ? victimiter->second : NULL);
455
456         if (!memb)
457         {
458                 src->WriteNumeric(ERR_USERNOTINCHANNEL, "%s %s :They are not on that channel", victim->nick.c_str(), this->name.c_str());
459                 return;
460         }
461
462         // Do the following checks only if the KICK is done by a local user;
463         // each server enforces its own rules.
464         if (IS_LOCAL(src))
465         {
466                 // Modules are allowed to explicitly allow or deny kicks done by local users
467                 ModResult res;
468                 FIRST_MOD_RESULT(OnUserPreKick, res, (src,memb,reason));
469                 if (res == MOD_RES_DENY)
470                         return;
471
472                 if (res == MOD_RES_PASSTHRU)
473                 {
474                         if (!srcmemb)
475                                 srcmemb = GetUser(src);
476                         unsigned int them = srcmemb ? srcmemb->getRank() : 0;
477                         unsigned int req = HALFOP_VALUE;
478                         for (std::string::size_type i = 0; i < memb->modes.length(); i++)
479                         {
480                                 ModeHandler* mh = ServerInstance->Modes->FindMode(memb->modes[i], MODETYPE_CHANNEL);
481                                 if (mh && mh->GetLevelRequired() > req)
482                                         req = mh->GetLevelRequired();
483                         }
484
485                         if (them < req)
486                         {
487                                 src->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s :You must be a channel %soperator",
488                                         this->name.c_str(), req > HALFOP_VALUE ? "" : "half-");
489                                 return;
490                         }
491                 }
492         }
493
494         CUList except_list;
495         FOREACH_MOD(OnUserKick, (src, memb, reason, except_list));
496
497         WriteAllExcept(src, false, 0, except_list, "KICK %s %s :%s", name.c_str(), victim->nick.c_str(), reason.c_str());
498
499         victim->chans.erase(memb);
500         this->DelUser(victimiter);
501 }
502
503 void Channel::WriteChannel(User* user, const char* text, ...)
504 {
505         std::string textbuffer;
506         VAFORMAT(textbuffer, text, text);
507         this->WriteChannel(user, textbuffer);
508 }
509
510 void Channel::WriteChannel(User* user, const std::string &text)
511 {
512         const std::string message = ":" + user->GetFullHost() + " " + text;
513
514         for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
515         {
516                 if (IS_LOCAL(i->first))
517                         i->first->Write(message);
518         }
519 }
520
521 void Channel::WriteChannelWithServ(const std::string& ServName, const char* text, ...)
522 {
523         std::string textbuffer;
524         VAFORMAT(textbuffer, text, text);
525         this->WriteChannelWithServ(ServName, textbuffer);
526 }
527
528 void Channel::WriteChannelWithServ(const std::string& ServName, const std::string &text)
529 {
530         const std::string message = ":" + (ServName.empty() ? ServerInstance->Config->ServerName : ServName) + " " + text;
531
532         for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
533         {
534                 if (IS_LOCAL(i->first))
535                         i->first->Write(message);
536         }
537 }
538
539 /* write formatted text from a source user to all users on a channel except
540  * for the sender (for privmsg etc) */
541 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const char* text, ...)
542 {
543         std::string textbuffer;
544         VAFORMAT(textbuffer, text, text);
545         this->WriteAllExceptSender(user, serversource, status, textbuffer);
546 }
547
548 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const char* text, ...)
549 {
550         std::string textbuffer;
551         VAFORMAT(textbuffer, text, text);
552         textbuffer = ":" + (serversource ? ServerInstance->Config->ServerName : user->GetFullHost()) + " " + textbuffer;
553         this->RawWriteAllExcept(user, serversource, status, except_list, textbuffer);
554 }
555
556 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string &text)
557 {
558         const std::string message = ":" + (serversource ? ServerInstance->Config->ServerName : user->GetFullHost()) + " " + text;
559         this->RawWriteAllExcept(user, serversource, status, except_list, message);
560 }
561
562 void Channel::RawWriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string &out)
563 {
564         unsigned int minrank = 0;
565         if (status)
566         {
567                 PrefixMode* mh = ServerInstance->Modes->FindPrefix(status);
568                 if (mh)
569                         minrank = mh->GetPrefixRank();
570         }
571         for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
572         {
573                 if (IS_LOCAL(i->first) && (except_list.find(i->first) == except_list.end()))
574                 {
575                         /* User doesn't have the status we're after */
576                         if (minrank && i->second->getRank() < minrank)
577                                 continue;
578
579                         i->first->Write(out);
580                 }
581         }
582 }
583
584 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const std::string& text)
585 {
586         CUList except_list;
587         except_list.insert(user);
588         this->WriteAllExcept(user, serversource, status, except_list, std::string(text));
589 }
590
591 const char* Channel::ChanModes(bool showkey)
592 {
593         static std::string scratch;
594         std::string sparam;
595
596         scratch.clear();
597
598         /* This was still iterating up to 190, Channel::modes is only 64 elements -- Om */
599         for(int n = 0; n < 64; n++)
600         {
601                 ModeHandler* mh = ServerInstance->Modes->FindMode(n + 65, MODETYPE_CHANNEL);
602                 if (mh && IsModeSet(mh))
603                 {
604                         scratch.push_back(n + 65);
605
606                         ParamModeBase* pm = mh->IsParameterMode();
607                         if (!pm)
608                                 continue;
609
610                         if (n == 'k' - 65 && !showkey)
611                         {
612                                 sparam += " <key>";
613                         }
614                         else
615                         {
616                                 sparam += ' ';
617                                 pm->GetParameter(this, sparam);
618                         }
619                 }
620         }
621
622         scratch += sparam;
623         return scratch.c_str();
624 }
625
626 /* compile a userlist of a channel into a string, each nick seperated by
627  * spaces and op, voice etc status shown as @ and +, and send it to 'user'
628  */
629 void Channel::UserList(User* user, bool has_user)
630 {
631         bool has_privs = user->HasPrivPermission("channels/auspex");
632         std::string list;
633         list.push_back(this->IsModeSet(secretmode) ? '@' : this->IsModeSet(privatemode) ? '*' : '=');
634         list.push_back(' ');
635         list.append(this->name).append(" :");
636         std::string::size_type pos = list.size();
637
638         const size_t maxlen = ServerInstance->Config->Limits.MaxLine - 10 - ServerInstance->Config->ServerName.size();
639         std::string prefixlist;
640         std::string nick;
641         for (UserMembIter i = userlist.begin(); i != userlist.end(); ++i)
642         {
643                 if ((!has_user) && (i->first->IsModeSet(invisiblemode)) && (!has_privs))
644                 {
645                         /*
646                          * user is +i, and source not on the channel, does not show
647                          * nick in NAMES list
648                          */
649                         continue;
650                 }
651
652                 Membership* memb = i->second;
653
654                 prefixlist.clear();
655                 char prefix = memb->GetPrefixChar();
656                 if (prefix)
657                         prefixlist.push_back(prefix);
658                 nick = i->first->nick;
659
660                 FOREACH_MOD(OnNamesListItem, (user, memb, prefixlist, nick));
661
662                 /* Nick was nuked, a module wants us to skip it */
663                 if (nick.empty())
664                         continue;
665
666                 if (list.size() + prefixlist.length() + nick.length() + 1 > maxlen)
667                 {
668                         /* list overflowed into multiple numerics */
669                         user->WriteNumeric(RPL_NAMREPLY, list);
670
671                         // Erase all nicks, keep the constant part
672                         list.erase(pos);
673                 }
674
675                 list.append(prefixlist).append(nick).push_back(' ');
676         }
677
678         // Only send the user list numeric if there is at least one user in it
679         if (list.size() != pos)
680                 user->WriteNumeric(RPL_NAMREPLY, list);
681
682         user->WriteNumeric(RPL_ENDOFNAMES, "%s :End of /NAMES list.", this->name.c_str());
683 }
684
685 /* returns the status character for a given user on a channel, e.g. @ for op,
686  * % for halfop etc. If the user has several modes set, the highest mode
687  * the user has must be returned.
688  */
689 char Membership::GetPrefixChar() const
690 {
691         char pf = 0;
692         unsigned int bestrank = 0;
693
694         for (std::string::const_iterator i = modes.begin(); i != modes.end(); ++i)
695         {
696                 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(*i);
697                 if (mh && mh->GetPrefixRank() > bestrank && mh->GetPrefix())
698                 {
699                         bestrank = mh->GetPrefixRank();
700                         pf = mh->GetPrefix();
701                 }
702         }
703         return pf;
704 }
705
706 unsigned int Membership::getRank()
707 {
708         char mchar = modes.c_str()[0];
709         unsigned int rv = 0;
710         if (mchar)
711         {
712                 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(mchar);
713                 if (mh)
714                         rv = mh->GetPrefixRank();
715         }
716         return rv;
717 }
718
719 const char* Membership::GetAllPrefixChars() const
720 {
721         static char prefix[64];
722         int ctr = 0;
723
724         for (std::string::const_iterator i = modes.begin(); i != modes.end(); ++i)
725         {
726                 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(*i);
727                 if (mh && mh->GetPrefix())
728                         prefix[ctr++] = mh->GetPrefix();
729         }
730         prefix[ctr] = 0;
731
732         return prefix;
733 }
734
735 unsigned int Channel::GetPrefixValue(User* user)
736 {
737         UserMembIter m = userlist.find(user);
738         if (m == userlist.end())
739                 return 0;
740         return m->second->getRank();
741 }
742
743 bool Membership::SetPrefix(PrefixMode* delta_mh, bool adding)
744 {
745         char prefix = delta_mh->GetModeChar();
746         for (unsigned int i = 0; i < modes.length(); i++)
747         {
748                 char mchar = modes[i];
749                 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(mchar);
750                 if (mh && mh->GetPrefixRank() <= delta_mh->GetPrefixRank())
751                 {
752                         modes = modes.substr(0,i) +
753                                 (adding ? std::string(1, prefix) : "") +
754                                 modes.substr(mchar == prefix ? i+1 : i);
755                         return adding != (mchar == prefix);
756                 }
757         }
758         if (adding)
759                 modes.push_back(prefix);
760         return adding;
761 }
762
763 void Invitation::Create(Channel* c, LocalUser* u, time_t timeout)
764 {
765         if ((timeout != 0) && (ServerInstance->Time() >= timeout))
766                 // Expired, don't bother
767                 return;
768
769         ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Create chan=%s user=%s", c->name.c_str(), u->uuid.c_str());
770
771         Invitation* inv = Invitation::Find(c, u, false);
772         if (inv)
773         {
774                  if ((inv->expiry == 0) || (inv->expiry > timeout))
775                         return;
776                 inv->expiry = timeout;
777                 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Create changed expiry in existing invitation %p", (void*) inv);
778         }
779         else
780         {
781                 inv = new Invitation(c, u, timeout);
782                 c->invites.push_front(inv);
783                 u->invites.push_front(inv);
784                 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Create created new invitation %p", (void*) inv);
785         }
786 }
787
788 Invitation* Invitation::Find(Channel* c, LocalUser* u, bool check_expired)
789 {
790         ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Find chan=%s user=%s check_expired=%d", c ? c->name.c_str() : "NULL", u ? u->uuid.c_str() : "NULL", check_expired);
791         if (!u || u->invites.empty())
792                 return NULL;
793
794         Invitation* result = NULL;
795         for (InviteList::iterator i = u->invites.begin(); i != u->invites.end(); )
796         {
797                 Invitation* inv = *i;
798                 ++i;
799
800                 if ((check_expired) && (inv->expiry != 0) && (inv->expiry <= ServerInstance->Time()))
801                 {
802                         /* Expired invite, remove it. */
803                         std::string expiration = InspIRCd::TimeString(inv->expiry);
804                         ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Find ecountered expired entry: %p expired %s", (void*) inv, expiration.c_str());
805                         delete inv;
806                 }
807                 else
808                 {
809                         /* Is it what we're searching for? */
810                         if (inv->chan == c)
811                         {
812                                 result = inv;
813                                 break;
814                         }
815                 }
816         }
817
818         ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Find result=%p", (void*) result);
819         return result;
820 }
821
822 Invitation::~Invitation()
823 {
824         // Remove this entry from both lists
825         chan->invites.erase(this);
826         user->invites.erase(this);
827         ServerInstance->Logs->Log("INVITEBASE", LOG_DEBUG, "Invitation::~ %p", (void*) this);
828 }