2 * InspIRCd -- Internet Relay Chat Daemon
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>
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.
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
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/>.
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");
42 Channel::Channel(const std::string &cname, time_t ts)
44 if (!ServerInstance->chanlist->insert(std::make_pair(cname, this)).second)
45 throw CoreException("Cannot create duplicate channel " + cname);
48 this->age = ts ? ts : ServerInstance->Time();
54 void Channel::SetMode(ModeHandler* mh, bool on)
56 modes[mh->GetModeChar() - 65] = on;
59 void Channel::SetModeParam(ModeHandler* mh, const std::string& parameter)
61 char mode = mh->GetModeChar();
62 if (parameter.empty())
64 custom_mode_params.erase(mode);
65 modes[mode-65] = false;
69 custom_mode_params[mode] = parameter;
70 modes[mode-65] = true;
74 std::string Channel::GetModeParameter(ModeHandler* mode)
76 CustomModeList::iterator n = custom_mode_params.find(mode->GetModeChar());
77 if (n != custom_mode_params.end())
82 void Channel::SetTopic(User* u, const std::string& ntopic)
84 this->topic.assign(ntopic, 0, ServerInstance->Config->Limits.MaxTopic);
85 this->setby.assign(ServerInstance->Config->FullHostInTopic ? u->GetFullHost() : u->nick, 0, 128);
86 this->WriteChannel(u, "TOPIC %s :%s", this->name.c_str(), this->topic.c_str());
87 this->topicset = ServerInstance->Time();
89 FOREACH_MOD(OnPostTopicChange, (u, this, this->topic));
92 Membership* Channel::AddUser(User* user)
94 Membership*& memb = userlist[user];
98 memb = new Membership(user, this);
102 void Channel::DelUser(User* user)
104 UserMembIter it = userlist.find(user);
105 if (it != userlist.end())
109 void Channel::CheckDestroy()
111 if (!userlist.empty())
115 FIRST_MOD_RESULT(OnChannelPreDelete, res, (this));
116 if (res == MOD_RES_DENY)
119 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
120 /* kill the record */
121 if (iter != ServerInstance->chanlist->end())
123 FOREACH_MOD(OnChannelDelete, (this));
124 ServerInstance->chanlist->erase(iter);
128 ServerInstance->GlobalCulls.AddItem(this);
131 void Channel::DelUser(const UserMembIter& membiter)
133 Membership* memb = membiter->second;
136 userlist.erase(membiter);
138 // If this channel became empty then it should be removed
142 Membership* Channel::GetUser(User* user)
144 UserMembIter i = userlist.find(user);
145 if (i == userlist.end())
150 void Channel::SetDefaultModes()
152 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "SetDefaultModes %s",
153 ServerInstance->Config->DefaultModes.c_str());
154 irc::spacesepstream list(ServerInstance->Config->DefaultModes);
156 std::string parameter;
158 list.GetToken(modeseq);
160 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
162 ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
165 if (mode->GetPrefixRank())
168 if (mode->GetNumParams(true))
169 list.GetToken(parameter);
173 mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, this, parameter, true);
179 * add a channel to a user, creating the record for it if needed and linking
180 * it to the user record
182 Channel* Channel::JoinUser(LocalUser* user, std::string cname, bool override, const std::string& key)
184 if (user->registered != REG_ALL)
186 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "Attempted to join unregistered user " + user->uuid + " to channel " + cname);
191 * We don't restrict the number of channels that remote users or users that are override-joining may be in.
192 * We restrict local users to MaxChans channels.
193 * We restrict local operators to OperMaxChans channels.
194 * This is a lot more logical than how it was formerly. -- w00t
198 if (user->HasPrivPermission("channels/high-join-limit"))
200 if (user->chans.size() >= ServerInstance->Config->OperMaxChans)
202 user->WriteNumeric(ERR_TOOMANYCHANNELS, "%s %s :You are on too many channels",user->nick.c_str(), cname.c_str());
208 unsigned int maxchans = user->GetClass()->maxchans;
210 maxchans = ServerInstance->Config->MaxChans;
211 if (user->chans.size() >= maxchans)
213 user->WriteNumeric(ERR_TOOMANYCHANNELS, "%s %s :You are on too many channels",user->nick.c_str(), cname.c_str());
219 // Crop channel name if it's too long
220 if (cname.length() > ServerInstance->Config->Limits.ChanMax)
221 cname.resize(ServerInstance->Config->Limits.ChanMax);
223 Channel* chan = ServerInstance->FindChan(cname);
224 bool created_by_local = (chan == NULL); // Flag that will be passed to modules in the OnUserJoin() hook later
225 std::string privs; // Prefix mode(letter)s to give to the joining user
229 privs = ServerInstance->Config->DefaultModes.substr(0, ServerInstance->Config->DefaultModes.find(' '));
231 if (override == false)
233 // Ask the modules whether they're ok with the join, pass NULL as Channel* as the channel is yet to be created
234 ModResult MOD_RESULT;
235 FIRST_MOD_RESULT(OnUserPreJoin, MOD_RESULT, (user, NULL, cname, privs, key));
236 if (MOD_RESULT == MOD_RES_DENY)
237 return NULL; // A module wasn't happy with the join, abort
240 chan = new Channel(cname, ServerInstance->Time());
241 // Set the default modes on the channel (<options:defaultmodes>)
242 chan->SetDefaultModes();
246 /* Already on the channel */
247 if (chan->HasUser(user))
250 if (override == false)
252 ModResult MOD_RESULT;
253 FIRST_MOD_RESULT(OnUserPreJoin, MOD_RESULT, (user, chan, cname, privs, key));
255 // A module explicitly denied the join and (hopefully) generated a message
256 // describing the situation, so we may stop here without sending anything
257 if (MOD_RESULT == MOD_RES_DENY)
260 // If no module returned MOD_RES_DENY or MOD_RES_ALLOW (which is the case
261 // most of the time) then proceed to check channel modes +k, +i, +l and bans,
263 // If a module explicitly allowed the join (by returning MOD_RES_ALLOW),
264 // then this entire section is skipped
265 if (MOD_RESULT == MOD_RES_PASSTHRU)
267 std::string ckey = chan->GetModeParameter(keymode);
268 bool invited = user->IsInvited(chan);
269 bool can_bypass = ServerInstance->Config->InvBypassModes && invited;
273 FIRST_MOD_RESULT(OnCheckKey, MOD_RESULT, (user, chan, key));
274 if (!MOD_RESULT.check((ckey == key) || can_bypass))
276 // If no key provided, or key is not the right one, and can't bypass +k (not invited or option not enabled)
277 user->WriteNumeric(ERR_BADCHANNELKEY, "%s %s :Cannot join channel (Incorrect channel key)",user->nick.c_str(), chan->name.c_str());
282 if (chan->IsModeSet(inviteonlymode))
284 FIRST_MOD_RESULT(OnCheckInvite, MOD_RESULT, (user, chan));
285 if (!MOD_RESULT.check(invited))
287 user->WriteNumeric(ERR_INVITEONLYCHAN, "%s %s :Cannot join channel (Invite only)",user->nick.c_str(), chan->name.c_str());
292 std::string limit = chan->GetModeParameter(limitmode);
295 FIRST_MOD_RESULT(OnCheckLimit, MOD_RESULT, (user, chan));
296 if (!MOD_RESULT.check((chan->GetUserCounter() < atol(limit.c_str()) || can_bypass)))
298 user->WriteNumeric(ERR_CHANNELISFULL, "%s %s :Cannot join channel (Channel is full)",user->nick.c_str(), chan->name.c_str());
303 if (chan->IsBanned(user) && !can_bypass)
305 user->WriteNumeric(ERR_BANNEDFROMCHAN, "%s %s :Cannot join channel (You're banned)",user->nick.c_str(), chan->name.c_str());
310 * If the user has invites for this channel, remove them now
311 * after a successful join so they don't build up.
315 user->RemoveInvite(chan);
321 // We figured that this join is allowed and also created the
322 // channel if it didn't exist before, now do the actual join
323 chan->ForceJoin(user, &privs, false, created_by_local);
327 void Channel::ForceJoin(User* user, const std::string* privs, bool bursting, bool created_by_local)
331 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "Attempted to join server user " + user->uuid + " to channel " + this->name);
335 Membership* memb = this->AddUser(user);
337 return; // Already on the channel
339 user->chans.insert(this);
343 // If the user was granted prefix modes (in the OnUserPreJoin hook, or he's a
344 // remote user and his own server set the modes), then set them internally now
345 memb->modes = *privs;
346 for (std::string::const_iterator i = privs->begin(); i != privs->end(); ++i)
348 ModeHandler* mh = ServerInstance->Modes->FindMode(*i, MODETYPE_CHANNEL);
349 if (mh && mh->GetPrefixRank())
351 std::string nick = user->nick;
352 /* Set, and make sure that the mode handler knows this mode was now set */
353 this->SetPrefix(user, mh->GetModeChar(), true);
354 mh->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, this, nick, true);
359 // 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
361 FOREACH_MOD(OnUserJoin, (memb, bursting, created_by_local, except_list));
363 this->WriteAllExcept(user, false, 0, except_list, "JOIN :%s", this->name.c_str());
365 /* Theyre not the first ones in here, make sure everyone else sees the modes we gave the user */
366 if ((GetUserCounter() > 1) && (!memb->modes.empty()))
368 std::string ms = memb->modes;
369 for(unsigned int i=0; i < memb->modes.length(); i++)
370 ms.append(" ").append(user->nick);
372 except_list.insert(user);
373 this->WriteAllExcept(user, !ServerInstance->Config->CycleHostsFromUser, 0, except_list, "MODE %s +%s", this->name.c_str(), ms.c_str());
380 user->WriteNumeric(RPL_TOPIC, "%s %s :%s", user->nick.c_str(), this->name.c_str(), this->topic.c_str());
381 user->WriteNumeric(RPL_TOPICTIME, "%s %s %s %lu", user->nick.c_str(), this->name.c_str(), this->setby.c_str(), (unsigned long)this->topicset);
383 this->UserList(user);
386 FOREACH_MOD(OnPostJoin, (memb));
389 bool Channel::IsBanned(User* user)
392 FIRST_MOD_RESULT(OnCheckChannelBan, result, (user, this));
394 if (result != MOD_RES_PASSTHRU)
395 return (result == MOD_RES_DENY);
397 ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
398 const ListModeBase::ModeList* bans = banlm->GetList(this);
401 for (ListModeBase::ModeList::const_iterator it = bans->begin(); it != bans->end(); it++)
403 if (CheckBan(user, it->mask))
410 bool Channel::CheckBan(User* user, const std::string& mask)
413 FIRST_MOD_RESULT(OnCheckBan, result, (user, this, mask));
414 if (result != MOD_RES_PASSTHRU)
415 return (result == MOD_RES_DENY);
417 // extbans were handled above, if this is one it obviously didn't match
418 if ((mask.length() <= 2) || (mask[1] == ':'))
421 std::string::size_type at = mask.find('@');
422 if (at == std::string::npos)
425 const std::string nickIdent = user->nick + "!" + user->ident;
426 std::string prefix = mask.substr(0, at);
427 if (InspIRCd::Match(nickIdent, prefix, NULL))
429 std::string suffix = mask.substr(at + 1);
430 if (InspIRCd::Match(user->host, suffix, NULL) ||
431 InspIRCd::Match(user->dhost, suffix, NULL) ||
432 InspIRCd::MatchCIDR(user->GetIPString(), suffix, NULL))
438 ModResult Channel::GetExtBanStatus(User *user, char type)
441 FIRST_MOD_RESULT(OnExtBanCheck, rv, (user, this, type));
442 if (rv != MOD_RES_PASSTHRU)
445 ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
446 const ListModeBase::ModeList* bans = banlm->GetList(this);
450 for (ListModeBase::ModeList::const_iterator it = bans->begin(); it != bans->end(); ++it)
452 if (CheckBan(user, it->mask))
456 return MOD_RES_PASSTHRU;
460 * Remove a channel from a users record, remove the reference to the Membership object
461 * from the channel and destroy it.
463 void Channel::PartUser(User *user, std::string &reason)
465 UserMembIter membiter = userlist.find(user);
467 if (membiter != userlist.end())
469 Membership* memb = membiter->second;
471 FOREACH_MOD(OnUserPart, (memb, reason, except_list));
473 WriteAllExcept(user, false, 0, except_list, "PART %s%s%s", this->name.c_str(), reason.empty() ? "" : " :", reason.c_str());
475 // Remove this channel from the user's chanlist
476 user->chans.erase(this);
477 // Remove the Membership from this channel's userlist and destroy it
478 this->DelUser(membiter);
482 void Channel::KickUser(User* src, User* victim, const std::string& reason, Membership* srcmemb)
484 UserMembIter victimiter = userlist.find(victim);
485 Membership* memb = ((victimiter != userlist.end()) ? victimiter->second : NULL);
489 src->WriteNumeric(ERR_USERNOTINCHANNEL, "%s %s %s :They are not on that channel",src->nick.c_str(), victim->nick.c_str(), this->name.c_str());
493 // Do the following checks only if the KICK is done by a local user;
494 // each server enforces its own rules.
497 // Modules are allowed to explicitly allow or deny kicks done by local users
499 FIRST_MOD_RESULT(OnUserPreKick, res, (src,memb,reason));
500 if (res == MOD_RES_DENY)
503 if (res == MOD_RES_PASSTHRU)
506 srcmemb = GetUser(src);
507 unsigned int them = srcmemb ? srcmemb->getRank() : 0;
508 unsigned int req = HALFOP_VALUE;
509 for (std::string::size_type i = 0; i < memb->modes.length(); i++)
511 ModeHandler* mh = ServerInstance->Modes->FindMode(memb->modes[i], MODETYPE_CHANNEL);
512 if (mh && mh->GetLevelRequired() > req)
513 req = mh->GetLevelRequired();
518 src->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s %s :You must be a channel %soperator",
519 src->nick.c_str(), this->name.c_str(), req > HALFOP_VALUE ? "" : "half-");
526 FOREACH_MOD(OnUserKick, (src, memb, reason, except_list));
528 WriteAllExcept(src, false, 0, except_list, "KICK %s %s :%s", name.c_str(), victim->nick.c_str(), reason.c_str());
530 victim->chans.erase(this);
531 this->DelUser(victimiter);
534 void Channel::WriteChannel(User* user, const char* text, ...)
536 std::string textbuffer;
537 VAFORMAT(textbuffer, text, text);
538 this->WriteChannel(user, textbuffer);
541 void Channel::WriteChannel(User* user, const std::string &text)
543 const std::string message = ":" + user->GetFullHost() + " " + text;
545 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
547 if (IS_LOCAL(i->first))
548 i->first->Write(message);
552 void Channel::WriteChannelWithServ(const std::string& ServName, const char* text, ...)
554 std::string textbuffer;
555 VAFORMAT(textbuffer, text, text);
556 this->WriteChannelWithServ(ServName, textbuffer);
559 void Channel::WriteChannelWithServ(const std::string& ServName, const std::string &text)
561 const std::string message = ":" + (ServName.empty() ? ServerInstance->Config->ServerName : ServName) + " " + text;
563 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
565 if (IS_LOCAL(i->first))
566 i->first->Write(message);
570 /* write formatted text from a source user to all users on a channel except
571 * for the sender (for privmsg etc) */
572 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const char* text, ...)
574 std::string textbuffer;
575 VAFORMAT(textbuffer, text, text);
576 this->WriteAllExceptSender(user, serversource, status, textbuffer);
579 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const char* text, ...)
581 std::string textbuffer;
582 VAFORMAT(textbuffer, text, text);
583 textbuffer = ":" + (serversource ? ServerInstance->Config->ServerName : user->GetFullHost()) + " " + textbuffer;
584 this->RawWriteAllExcept(user, serversource, status, except_list, textbuffer);
587 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string &text)
589 const std::string message = ":" + (serversource ? ServerInstance->Config->ServerName : user->GetFullHost()) + " " + text;
590 this->RawWriteAllExcept(user, serversource, status, except_list, message);
593 void Channel::RawWriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string &out)
595 unsigned int minrank = 0;
598 ModeHandler* mh = ServerInstance->Modes->FindPrefix(status);
600 minrank = mh->GetPrefixRank();
602 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
604 if (IS_LOCAL(i->first) && (except_list.find(i->first) == except_list.end()))
606 /* User doesn't have the status we're after */
607 if (minrank && i->second->getRank() < minrank)
610 i->first->Write(out);
615 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const std::string& text)
618 except_list.insert(user);
619 this->WriteAllExcept(user, serversource, status, except_list, std::string(text));
622 const char* Channel::ChanModes(bool showkey)
624 static std::string scratch;
629 /* This was still iterating up to 190, Channel::modes is only 64 elements -- Om */
630 for(int n = 0; n < 64; n++)
634 scratch.push_back(n + 65);
635 ModeHandler* mh = ServerInstance->Modes->FindMode(n+'A', MODETYPE_CHANNEL);
639 if (n == 'k' - 65 && !showkey)
645 const std::string param = this->GetModeParameter(mh);
656 return scratch.c_str();
659 /* compile a userlist of a channel into a string, each nick seperated by
660 * spaces and op, voice etc status shown as @ and +, and send it to 'user'
662 void Channel::UserList(User *user)
664 if (this->IsModeSet(secretmode) && !this->HasUser(user) && !user->HasPrivPermission("channels/auspex"))
666 user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nick/channel",user->nick.c_str(), this->name.c_str());
670 std::string list = user->nick;
672 list.push_back(this->IsModeSet(secretmode) ? '@' : this->IsModeSet(privatemode) ? '*' : '=');
674 list.append(this->name).append(" :");
675 std::string::size_type pos = list.size();
677 bool has_one = false;
679 /* Improvement by Brain - this doesnt change in value, so why was it inside
682 bool has_user = this->HasUser(user);
684 std::string prefixlist;
686 for (UserMembIter i = userlist.begin(); i != userlist.end(); ++i)
688 if (i->first->quitting)
690 if ((!has_user) && (i->first->IsModeSet(invisiblemode)))
693 * user is +i, and source not on the channel, does not show
699 prefixlist = this->GetPrefixChar(i->first);
700 nick = i->first->nick;
702 FOREACH_MOD(OnNamesListItem, (user, i->second, prefixlist, nick));
704 /* Nick was nuked, a module wants us to skip it */
708 if (list.size() + prefixlist.length() + nick.length() + 1 > 480)
710 /* list overflowed into multiple numerics */
711 user->WriteNumeric(RPL_NAMREPLY, list);
713 // Erase all nicks, keep the constant part
718 list.append(prefixlist).append(nick).push_back(' ');
723 /* if whats left in the list isnt empty, send it */
726 user->WriteNumeric(RPL_NAMREPLY, list);
729 user->WriteNumeric(RPL_ENDOFNAMES, "%s %s :End of /NAMES list.", user->nick.c_str(), this->name.c_str());
732 /* returns the status character for a given user on a channel, e.g. @ for op,
733 * % for halfop etc. If the user has several modes set, the highest mode
734 * the user has must be returned.
736 const char* Channel::GetPrefixChar(User *user)
738 static char pf[2] = {0, 0};
740 unsigned int bestrank = 0;
742 UserMembIter m = userlist.find(user);
743 if (m != userlist.end())
745 for(unsigned int i=0; i < m->second->modes.length(); i++)
747 char mchar = m->second->modes[i];
748 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
749 if (mh && mh->GetPrefixRank() > bestrank && mh->GetPrefix())
751 bestrank = mh->GetPrefixRank();
752 pf[0] = mh->GetPrefix();
759 unsigned int Membership::getRank()
761 char mchar = modes.c_str()[0];
765 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
767 rv = mh->GetPrefixRank();
772 const char* Channel::GetAllPrefixChars(User* user)
774 static char prefix[64];
777 UserMembIter m = userlist.find(user);
778 if (m != userlist.end())
780 for(unsigned int i=0; i < m->second->modes.length(); i++)
782 char mchar = m->second->modes[i];
783 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
784 if (mh && mh->GetPrefix())
785 prefix[ctr++] = mh->GetPrefix();
793 unsigned int Channel::GetPrefixValue(User* user)
795 UserMembIter m = userlist.find(user);
796 if (m == userlist.end())
798 return m->second->getRank();
801 bool Channel::SetPrefix(User* user, char prefix, bool adding)
803 ModeHandler* delta_mh = ServerInstance->Modes->FindMode(prefix, MODETYPE_CHANNEL);
806 UserMembIter m = userlist.find(user);
807 if (m == userlist.end())
809 for(unsigned int i=0; i < m->second->modes.length(); i++)
811 char mchar = m->second->modes[i];
812 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
813 if (mh && mh->GetPrefixRank() <= delta_mh->GetPrefixRank())
816 m->second->modes.substr(0,i) +
817 (adding ? std::string(1, prefix) : "") +
818 m->second->modes.substr(mchar == prefix ? i+1 : i);
819 return adding != (mchar == prefix);
823 m->second->modes += std::string(1, prefix);
827 void Invitation::Create(Channel* c, LocalUser* u, time_t timeout)
829 if ((timeout != 0) && (ServerInstance->Time() >= timeout))
830 // Expired, don't bother
833 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Create chan=%s user=%s", c->name.c_str(), u->uuid.c_str());
835 Invitation* inv = Invitation::Find(c, u, false);
838 if ((inv->expiry == 0) || (inv->expiry > timeout))
840 inv->expiry = timeout;
841 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Create changed expiry in existing invitation %p", (void*) inv);
845 inv = new Invitation(c, u, timeout);
846 c->invites.push_back(inv);
847 u->invites.push_back(inv);
848 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Create created new invitation %p", (void*) inv);
852 Invitation* Invitation::Find(Channel* c, LocalUser* u, bool check_expired)
854 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);
855 if (!u || u->invites.empty())
858 InviteList locallist;
859 locallist.swap(u->invites);
861 Invitation* result = NULL;
862 for (InviteList::iterator i = locallist.begin(); i != locallist.end(); )
864 Invitation* inv = *i;
865 if ((check_expired) && (inv->expiry != 0) && (inv->expiry <= ServerInstance->Time()))
867 /* Expired invite, remove it. */
868 std::string expiration = ServerInstance->TimeString(inv->expiry);
869 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Find ecountered expired entry: %p expired %s", (void*) inv, expiration.c_str());
870 i = locallist.erase(i);
876 /* Is it what we're searching for? */
886 locallist.swap(u->invites);
887 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Find result=%p", (void*) result);
891 Invitation::~Invitation()
893 // Remove this entry from both lists
894 InviteList::iterator it = std::find(chan->invites.begin(), chan->invites.end(), this);
895 if (it != chan->invites.end())
896 chan->invites.erase(it);
897 it = std::find(user->invites.begin(), user->invites.end(), this);
898 if (it != user->invites.end())
899 user->invites.erase(it);
902 void InviteBase::ClearInvites()
904 ServerInstance->Logs->Log("INVITEBASE", LOG_DEBUG, "InviteBase::ClearInvites %p", (void*) this);
905 InviteList locallist;
906 locallist.swap(invites);
907 for (InviteList::const_iterator i = locallist.begin(); i != locallist.end(); ++i)