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 static ModeReference ban(NULL, "ban");
35 Channel::Channel(const std::string &cname, time_t ts)
37 if (!ServerInstance->chanlist->insert(std::make_pair(cname, this)).second)
38 throw CoreException("Cannot create duplicate channel " + cname);
41 this->age = ts ? ts : ServerInstance->Time();
47 void Channel::SetMode(ModeHandler* mh, bool on)
49 modes[mh->GetModeChar() - 65] = on;
52 void Channel::SetModeParam(char mode, const std::string& parameter)
54 if (parameter.empty())
56 custom_mode_params.erase(mode);
57 modes[mode-65] = false;
61 custom_mode_params[mode] = parameter;
62 modes[mode-65] = true;
66 void Channel::SetModeParam(ModeHandler* mode, const std::string& parameter)
68 SetModeParam(mode->GetModeChar(), parameter);
71 std::string Channel::GetModeParameter(char mode)
73 CustomModeList::iterator n = custom_mode_params.find(mode);
74 if (n != custom_mode_params.end())
79 std::string Channel::GetModeParameter(ModeHandler* mode)
81 CustomModeList::iterator n = custom_mode_params.find(mode->GetModeChar());
82 if (n != custom_mode_params.end())
87 void Channel::SetTopic(User* u, const std::string& ntopic)
89 this->topic.assign(ntopic, 0, ServerInstance->Config->Limits.MaxTopic);
90 this->setby.assign(ServerInstance->Config->FullHostInTopic ? u->GetFullHost() : u->nick, 0, 128);
91 this->WriteChannel(u, "TOPIC %s :%s", this->name.c_str(), this->topic.c_str());
92 this->topicset = ServerInstance->Time();
94 FOREACH_MOD(I_OnPostTopicChange,OnPostTopicChange(u, this, this->topic));
97 Membership* Channel::AddUser(User* user)
99 Membership*& memb = userlist[user];
103 memb = new Membership(user, this);
107 void Channel::DelUser(User* user)
109 UserMembIter it = userlist.find(user);
110 if (it != userlist.end())
114 void Channel::CheckDestroy()
116 if (!userlist.empty())
120 FIRST_MOD_RESULT(OnChannelPreDelete, res, (this));
121 if (res == MOD_RES_DENY)
124 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
125 /* kill the record */
126 if (iter != ServerInstance->chanlist->end())
128 FOREACH_MOD(I_OnChannelDelete, OnChannelDelete(this));
129 ServerInstance->chanlist->erase(iter);
133 ServerInstance->GlobalCulls.AddItem(this);
136 void Channel::DelUser(const UserMembIter& membiter)
138 Membership* memb = membiter->second;
141 userlist.erase(membiter);
143 // If this channel became empty then it should be removed
147 Membership* Channel::GetUser(User* user)
149 UserMembIter i = userlist.find(user);
150 if (i == userlist.end())
155 void Channel::SetDefaultModes()
157 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "SetDefaultModes %s",
158 ServerInstance->Config->DefaultModes.c_str());
159 irc::spacesepstream list(ServerInstance->Config->DefaultModes);
161 std::string parameter;
163 list.GetToken(modeseq);
165 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
167 ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
170 if (mode->GetNumParams(true))
171 list.GetToken(parameter);
175 mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, this, parameter, true);
181 * add a channel to a user, creating the record for it if needed and linking
182 * it to the user record
184 Channel* Channel::JoinUser(LocalUser* user, std::string cname, bool override, const std::string& key)
186 if (user->registered != REG_ALL)
188 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "Attempted to join unregistered user " + user->uuid + " to channel " + cname);
193 * We don't restrict the number of channels that remote users or users that are override-joining may be in.
194 * We restrict local users to MaxChans channels.
195 * We restrict local operators to OperMaxChans channels.
196 * This is a lot more logical than how it was formerly. -- w00t
200 if (user->HasPrivPermission("channels/high-join-limit"))
202 if (user->chans.size() >= ServerInstance->Config->OperMaxChans)
204 user->WriteNumeric(ERR_TOOMANYCHANNELS, "%s %s :You are on too many channels",user->nick.c_str(), cname.c_str());
210 unsigned int maxchans = user->GetClass()->maxchans;
212 maxchans = ServerInstance->Config->MaxChans;
213 if (user->chans.size() >= maxchans)
215 user->WriteNumeric(ERR_TOOMANYCHANNELS, "%s %s :You are on too many channels",user->nick.c_str(), cname.c_str());
221 // Crop channel name if it's too long
222 if (cname.length() > ServerInstance->Config->Limits.ChanMax)
223 cname.resize(ServerInstance->Config->Limits.ChanMax);
225 Channel* chan = ServerInstance->FindChan(cname);
226 bool created_by_local = (chan == NULL); // Flag that will be passed to modules in the OnUserJoin() hook later
227 std::string privs; // Prefix mode(letter)s to give to the joining user
233 if (override == false)
235 // Ask the modules whether they're ok with the join, pass NULL as Channel* as the channel is yet to be created
236 ModResult MOD_RESULT;
237 FIRST_MOD_RESULT(OnUserPreJoin, MOD_RESULT, (user, NULL, cname, privs, key));
238 if (MOD_RESULT == MOD_RES_DENY)
239 return NULL; // A module wasn't happy with the join, abort
242 chan = new Channel(cname, ServerInstance->Time());
243 // Set the default modes on the channel (<options:defaultmodes>)
244 chan->SetDefaultModes();
248 /* Already on the channel */
249 if (chan->HasUser(user))
252 if (override == false)
254 ModResult MOD_RESULT;
255 FIRST_MOD_RESULT(OnUserPreJoin, MOD_RESULT, (user, chan, cname, privs, key));
257 // A module explicitly denied the join and (hopefully) generated a message
258 // describing the situation, so we may stop here without sending anything
259 if (MOD_RESULT == MOD_RES_DENY)
262 // If no module returned MOD_RES_DENY or MOD_RES_ALLOW (which is the case
263 // most of the time) then proceed to check channel modes +k, +i, +l and bans,
265 // If a module explicitly allowed the join (by returning MOD_RES_ALLOW),
266 // then this entire section is skipped
267 if (MOD_RESULT == MOD_RES_PASSTHRU)
269 std::string ckey = chan->GetModeParameter('k');
270 bool invited = user->IsInvited(chan);
271 bool can_bypass = ServerInstance->Config->InvBypassModes && invited;
275 FIRST_MOD_RESULT(OnCheckKey, MOD_RESULT, (user, chan, key));
276 if (!MOD_RESULT.check((ckey == key) || can_bypass))
278 // If no key provided, or key is not the right one, and can't bypass +k (not invited or option not enabled)
279 user->WriteNumeric(ERR_BADCHANNELKEY, "%s %s :Cannot join channel (Incorrect channel key)",user->nick.c_str(), chan->name.c_str());
284 if (chan->IsModeSet('i'))
286 FIRST_MOD_RESULT(OnCheckInvite, MOD_RESULT, (user, chan));
287 if (!MOD_RESULT.check(invited))
289 user->WriteNumeric(ERR_INVITEONLYCHAN, "%s %s :Cannot join channel (Invite only)",user->nick.c_str(), chan->name.c_str());
294 std::string limit = chan->GetModeParameter('l');
297 FIRST_MOD_RESULT(OnCheckLimit, MOD_RESULT, (user, chan));
298 if (!MOD_RESULT.check((chan->GetUserCounter() < atol(limit.c_str()) || can_bypass)))
300 user->WriteNumeric(ERR_CHANNELISFULL, "%s %s :Cannot join channel (Channel is full)",user->nick.c_str(), chan->name.c_str());
305 if (chan->IsBanned(user) && !can_bypass)
307 user->WriteNumeric(ERR_BANNEDFROMCHAN, "%s %s :Cannot join channel (You're banned)",user->nick.c_str(), chan->name.c_str());
312 * If the user has invites for this channel, remove them now
313 * after a successful join so they don't build up.
317 user->RemoveInvite(chan);
323 // We figured that this join is allowed and also created the
324 // channel if it didn't exist before, now do the actual join
325 chan->ForceJoin(user, &privs, false, created_by_local);
329 void Channel::ForceJoin(User* user, const std::string* privs, bool bursting, bool created_by_local)
333 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "Attempted to join server user " + user->uuid + " to channel " + this->name);
337 Membership* memb = this->AddUser(user);
339 return; // Already on the channel
341 user->chans.insert(this);
345 // If the user was granted prefix modes (in the OnUserPreJoin hook, or he's a
346 // remote user and his own server set the modes), then set them internally now
347 memb->modes = *privs;
348 for (std::string::const_iterator i = privs->begin(); i != privs->end(); ++i)
350 ModeHandler* mh = ServerInstance->Modes->FindMode(*i, MODETYPE_CHANNEL);
353 std::string nick = user->nick;
354 /* Set, and make sure that the mode handler knows this mode was now set */
355 this->SetPrefix(user, mh->GetModeChar(), true);
356 mh->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, this, nick, true);
361 // 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
363 FOREACH_MOD(I_OnUserJoin,OnUserJoin(memb, bursting, created_by_local, except_list));
365 this->WriteAllExcept(user, false, 0, except_list, "JOIN :%s", this->name.c_str());
367 /* Theyre not the first ones in here, make sure everyone else sees the modes we gave the user */
368 if ((GetUserCounter() > 1) && (!memb->modes.empty()))
370 std::string ms = memb->modes;
371 for(unsigned int i=0; i < memb->modes.length(); i++)
372 ms.append(" ").append(user->nick);
374 except_list.insert(user);
375 this->WriteAllExcept(user, !ServerInstance->Config->CycleHostsFromUser, 0, except_list, "MODE %s +%s", this->name.c_str(), ms.c_str());
382 user->WriteNumeric(RPL_TOPIC, "%s %s :%s", user->nick.c_str(), this->name.c_str(), this->topic.c_str());
383 user->WriteNumeric(RPL_TOPICTIME, "%s %s %s %lu", user->nick.c_str(), this->name.c_str(), this->setby.c_str(), (unsigned long)this->topicset);
385 this->UserList(user);
388 FOREACH_MOD(I_OnPostJoin,OnPostJoin(memb));
391 bool Channel::IsBanned(User* user)
394 FIRST_MOD_RESULT(OnCheckChannelBan, result, (user, this));
396 if (result != MOD_RES_PASSTHRU)
397 return (result == MOD_RES_DENY);
399 ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
400 const ListModeBase::ModeList* bans = banlm->GetList(this);
403 for (ListModeBase::ModeList::const_iterator it = bans->begin(); it != bans->end(); it++)
405 if (CheckBan(user, it->mask))
412 bool Channel::CheckBan(User* user, const std::string& mask)
415 FIRST_MOD_RESULT(OnCheckBan, result, (user, this, mask));
416 if (result != MOD_RES_PASSTHRU)
417 return (result == MOD_RES_DENY);
419 // extbans were handled above, if this is one it obviously didn't match
420 if ((mask.length() <= 2) || (mask[1] == ':'))
423 std::string::size_type at = mask.find('@');
424 if (at == std::string::npos)
427 const std::string nickIdent = user->nick + "!" + user->ident;
428 std::string prefix = mask.substr(0, at);
429 if (InspIRCd::Match(nickIdent, prefix, NULL))
431 std::string suffix = mask.substr(at + 1);
432 if (InspIRCd::Match(user->host, suffix, NULL) ||
433 InspIRCd::Match(user->dhost, suffix, NULL) ||
434 InspIRCd::MatchCIDR(user->GetIPString(), suffix, NULL))
440 ModResult Channel::GetExtBanStatus(User *user, char type)
443 FIRST_MOD_RESULT(OnExtBanCheck, rv, (user, this, type));
444 if (rv != MOD_RES_PASSTHRU)
447 ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
448 const ListModeBase::ModeList* bans = banlm->GetList(this);
452 for (ListModeBase::ModeList::const_iterator it = bans->begin(); it != bans->end(); ++it)
454 if (CheckBan(user, it->mask))
458 return MOD_RES_PASSTHRU;
462 * Remove a channel from a users record, remove the reference to the Membership object
463 * from the channel and destroy it.
465 void Channel::PartUser(User *user, std::string &reason)
467 UserMembIter membiter = userlist.find(user);
469 if (membiter != userlist.end())
471 Membership* memb = membiter->second;
473 FOREACH_MOD(I_OnUserPart,OnUserPart(memb, reason, except_list));
475 WriteAllExcept(user, false, 0, except_list, "PART %s%s%s", this->name.c_str(), reason.empty() ? "" : " :", reason.c_str());
477 // Remove this channel from the user's chanlist
478 user->chans.erase(this);
479 // Remove the Membership from this channel's userlist and destroy it
480 this->DelUser(membiter);
484 void Channel::KickUser(User* src, User* victim, const std::string& reason, Membership* srcmemb)
486 UserMembIter victimiter = userlist.find(victim);
487 Membership* memb = ((victimiter != userlist.end()) ? victimiter->second : NULL);
491 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());
495 // Do the following checks only if the KICK is done by a local user;
496 // each server enforces its own rules.
499 // Modules are allowed to explicitly allow or deny kicks done by local users
501 FIRST_MOD_RESULT(OnUserPreKick, res, (src,memb,reason));
502 if (res == MOD_RES_DENY)
505 if (res == MOD_RES_PASSTHRU)
508 srcmemb = GetUser(src);
509 unsigned int them = srcmemb ? srcmemb->getRank() : 0;
510 unsigned int req = HALFOP_VALUE;
511 for (std::string::size_type i = 0; i < memb->modes.length(); i++)
513 ModeHandler* mh = ServerInstance->Modes->FindMode(memb->modes[i], MODETYPE_CHANNEL);
514 if (mh && mh->GetLevelRequired() > req)
515 req = mh->GetLevelRequired();
520 src->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s %s :You must be a channel %soperator",
521 src->nick.c_str(), this->name.c_str(), req > HALFOP_VALUE ? "" : "half-");
528 FOREACH_MOD(I_OnUserKick,OnUserKick(src, memb, reason, except_list));
530 WriteAllExcept(src, false, 0, except_list, "KICK %s %s :%s", name.c_str(), victim->nick.c_str(), reason.c_str());
532 victim->chans.erase(this);
533 this->DelUser(victimiter);
536 void Channel::WriteChannel(User* user, const char* text, ...)
538 std::string textbuffer;
539 VAFORMAT(textbuffer, text, text);
540 this->WriteChannel(user, textbuffer);
543 void Channel::WriteChannel(User* user, const std::string &text)
545 const std::string message = ":" + user->GetFullHost() + " " + text;
547 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
549 if (IS_LOCAL(i->first))
550 i->first->Write(message);
554 void Channel::WriteChannelWithServ(const std::string& ServName, const char* text, ...)
556 std::string textbuffer;
557 VAFORMAT(textbuffer, text, text);
558 this->WriteChannelWithServ(ServName, textbuffer);
561 void Channel::WriteChannelWithServ(const std::string& ServName, const std::string &text)
563 const std::string message = ":" + (ServName.empty() ? ServerInstance->Config->ServerName : ServName) + " " + text;
565 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
567 if (IS_LOCAL(i->first))
568 i->first->Write(message);
572 /* write formatted text from a source user to all users on a channel except
573 * for the sender (for privmsg etc) */
574 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const char* text, ...)
576 std::string textbuffer;
577 VAFORMAT(textbuffer, text, text);
578 this->WriteAllExceptSender(user, serversource, status, textbuffer);
581 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const char* text, ...)
583 std::string textbuffer;
584 VAFORMAT(textbuffer, text, text);
585 textbuffer = ":" + (serversource ? ServerInstance->Config->ServerName : user->GetFullHost()) + " " + textbuffer;
586 this->RawWriteAllExcept(user, serversource, status, except_list, textbuffer);
589 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string &text)
591 const std::string message = ":" + (serversource ? ServerInstance->Config->ServerName : user->GetFullHost()) + " " + text;
592 this->RawWriteAllExcept(user, serversource, status, except_list, message);
595 void Channel::RawWriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string &out)
597 unsigned int minrank = 0;
600 ModeHandler* mh = ServerInstance->Modes->FindPrefix(status);
602 minrank = mh->GetPrefixRank();
604 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
606 if (IS_LOCAL(i->first) && (except_list.find(i->first) == except_list.end()))
608 /* User doesn't have the status we're after */
609 if (minrank && i->second->getRank() < minrank)
612 i->first->Write(out);
617 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const std::string& text)
620 except_list.insert(user);
621 this->WriteAllExcept(user, serversource, status, except_list, std::string(text));
624 const char* Channel::ChanModes(bool showkey)
626 static std::string scratch;
631 /* This was still iterating up to 190, Channel::modes is only 64 elements -- Om */
632 for(int n = 0; n < 64; n++)
636 scratch.push_back(n + 65);
637 if (n == 'k' - 65 && !showkey)
643 const std::string param = this->GetModeParameter(n + 65);
654 return scratch.c_str();
657 /* compile a userlist of a channel into a string, each nick seperated by
658 * spaces and op, voice etc status shown as @ and +, and send it to 'user'
660 void Channel::UserList(User *user)
662 if (this->IsModeSet('s') && !this->HasUser(user) && !user->HasPrivPermission("channels/auspex"))
664 user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nick/channel",user->nick.c_str(), this->name.c_str());
668 std::string list = user->nick;
670 list.push_back(this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=');
672 list.append(this->name).append(" :");
673 std::string::size_type pos = list.size();
675 bool has_one = false;
677 /* Improvement by Brain - this doesnt change in value, so why was it inside
680 bool has_user = this->HasUser(user);
682 std::string prefixlist;
684 for (UserMembIter i = userlist.begin(); i != userlist.end(); ++i)
686 if (i->first->quitting)
688 if ((!has_user) && (i->first->IsModeSet('i')))
691 * user is +i, and source not on the channel, does not show
697 prefixlist = this->GetPrefixChar(i->first);
698 nick = i->first->nick;
700 FOREACH_MOD(I_OnNamesListItem, OnNamesListItem(user, i->second, prefixlist, nick));
702 /* Nick was nuked, a module wants us to skip it */
706 if (list.size() + prefixlist.length() + nick.length() + 1 > 480)
708 /* list overflowed into multiple numerics */
709 user->WriteNumeric(RPL_NAMREPLY, list);
711 // Erase all nicks, keep the constant part
716 list.append(prefixlist).append(nick).push_back(' ');
721 /* if whats left in the list isnt empty, send it */
724 user->WriteNumeric(RPL_NAMREPLY, list);
727 user->WriteNumeric(RPL_ENDOFNAMES, "%s %s :End of /NAMES list.", user->nick.c_str(), this->name.c_str());
730 /* returns the status character for a given user on a channel, e.g. @ for op,
731 * % for halfop etc. If the user has several modes set, the highest mode
732 * the user has must be returned.
734 const char* Channel::GetPrefixChar(User *user)
736 static char pf[2] = {0, 0};
738 unsigned int bestrank = 0;
740 UserMembIter m = userlist.find(user);
741 if (m != userlist.end())
743 for(unsigned int i=0; i < m->second->modes.length(); i++)
745 char mchar = m->second->modes[i];
746 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
747 if (mh && mh->GetPrefixRank() > bestrank && mh->GetPrefix())
749 bestrank = mh->GetPrefixRank();
750 pf[0] = mh->GetPrefix();
757 unsigned int Membership::getRank()
759 char mchar = modes.c_str()[0];
763 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
765 rv = mh->GetPrefixRank();
770 const char* Channel::GetAllPrefixChars(User* user)
772 static char prefix[64];
775 UserMembIter m = userlist.find(user);
776 if (m != userlist.end())
778 for(unsigned int i=0; i < m->second->modes.length(); i++)
780 char mchar = m->second->modes[i];
781 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
782 if (mh && mh->GetPrefix())
783 prefix[ctr++] = mh->GetPrefix();
791 unsigned int Channel::GetPrefixValue(User* user)
793 UserMembIter m = userlist.find(user);
794 if (m == userlist.end())
796 return m->second->getRank();
799 bool Channel::SetPrefix(User* user, char prefix, bool adding)
801 ModeHandler* delta_mh = ServerInstance->Modes->FindMode(prefix, MODETYPE_CHANNEL);
804 UserMembIter m = userlist.find(user);
805 if (m == userlist.end())
807 for(unsigned int i=0; i < m->second->modes.length(); i++)
809 char mchar = m->second->modes[i];
810 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
811 if (mh && mh->GetPrefixRank() <= delta_mh->GetPrefixRank())
814 m->second->modes.substr(0,i) +
815 (adding ? std::string(1, prefix) : "") +
816 m->second->modes.substr(mchar == prefix ? i+1 : i);
817 return adding != (mchar == prefix);
821 m->second->modes += std::string(1, prefix);
825 void Invitation::Create(Channel* c, LocalUser* u, time_t timeout)
827 if ((timeout != 0) && (ServerInstance->Time() >= timeout))
828 // Expired, don't bother
831 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Create chan=%s user=%s", c->name.c_str(), u->uuid.c_str());
833 Invitation* inv = Invitation::Find(c, u, false);
836 if ((inv->expiry == 0) || (inv->expiry > timeout))
838 inv->expiry = timeout;
839 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Create changed expiry in existing invitation %p", (void*) inv);
843 inv = new Invitation(c, u, timeout);
844 c->invites.push_back(inv);
845 u->invites.push_back(inv);
846 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Create created new invitation %p", (void*) inv);
850 Invitation* Invitation::Find(Channel* c, LocalUser* u, bool check_expired)
852 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);
853 if (!u || u->invites.empty())
856 InviteList locallist;
857 locallist.swap(u->invites);
859 Invitation* result = NULL;
860 for (InviteList::iterator i = locallist.begin(); i != locallist.end(); )
862 Invitation* inv = *i;
863 if ((check_expired) && (inv->expiry != 0) && (inv->expiry <= ServerInstance->Time()))
865 /* Expired invite, remove it. */
866 std::string expiration = ServerInstance->TimeString(inv->expiry);
867 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Find ecountered expired entry: %p expired %s", (void*) inv, expiration.c_str());
868 i = locallist.erase(i);
874 /* Is it what we're searching for? */
884 locallist.swap(u->invites);
885 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Find result=%p", (void*) result);
889 Invitation::~Invitation()
891 // Remove this entry from both lists
892 InviteList::iterator it = std::find(chan->invites.begin(), chan->invites.end(), this);
893 if (it != chan->invites.end())
894 chan->invites.erase(it);
895 it = std::find(user->invites.begin(), user->invites.end(), this);
896 if (it != user->invites.end())
897 user->invites.erase(it);
900 void InviteBase::ClearInvites()
902 ServerInstance->Logs->Log("INVITEBASE", LOG_DEBUG, "InviteBase::ClearInvites %p", (void*) this);
903 InviteList locallist;
904 locallist.swap(invites);
905 for (InviteList::const_iterator i = locallist.begin(); i != locallist.end(); ++i)