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(char mode,bool mode_on)
49 modes[mode-65] = mode_on;
52 void Channel::SetMode(ModeHandler* mh, bool on)
54 modes[mh->GetModeChar() - 65] = on;
57 void Channel::SetModeParam(char mode, const std::string& parameter)
59 if (parameter.empty())
61 custom_mode_params.erase(mode);
62 modes[mode-65] = false;
66 custom_mode_params[mode] = parameter;
67 modes[mode-65] = true;
71 void Channel::SetModeParam(ModeHandler* mode, const std::string& parameter)
73 SetModeParam(mode->GetModeChar(), parameter);
76 std::string Channel::GetModeParameter(char mode)
78 CustomModeList::iterator n = custom_mode_params.find(mode);
79 if (n != custom_mode_params.end())
84 std::string Channel::GetModeParameter(ModeHandler* mode)
86 CustomModeList::iterator n = custom_mode_params.find(mode->GetModeChar());
87 if (n != custom_mode_params.end())
92 void Channel::SetTopic(User* u, const std::string& ntopic)
94 this->topic.assign(ntopic, 0, ServerInstance->Config->Limits.MaxTopic);
95 this->setby.assign(ServerInstance->Config->FullHostInTopic ? u->GetFullHost() : u->nick, 0, 128);
96 this->WriteChannel(u, "TOPIC %s :%s", this->name.c_str(), this->topic.c_str());
97 this->topicset = ServerInstance->Time();
99 FOREACH_MOD(I_OnPostTopicChange,OnPostTopicChange(u, this, this->topic));
102 Membership* Channel::AddUser(User* user)
104 Membership*& memb = userlist[user];
108 memb = new Membership(user, this);
112 void Channel::DelUser(User* user)
114 UserMembIter it = userlist.find(user);
115 if (it != userlist.end())
119 void Channel::CheckDestroy()
121 if (!userlist.empty())
125 FIRST_MOD_RESULT(OnChannelPreDelete, res, (this));
126 if (res == MOD_RES_DENY)
129 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
130 /* kill the record */
131 if (iter != ServerInstance->chanlist->end())
133 FOREACH_MOD(I_OnChannelDelete, OnChannelDelete(this));
134 ServerInstance->chanlist->erase(iter);
138 ServerInstance->GlobalCulls.AddItem(this);
141 void Channel::DelUser(const UserMembIter& membiter)
143 Membership* memb = membiter->second;
146 userlist.erase(membiter);
148 // If this channel became empty then it should be removed
152 Membership* Channel::GetUser(User* user)
154 UserMembIter i = userlist.find(user);
155 if (i == userlist.end())
160 void Channel::SetDefaultModes()
162 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "SetDefaultModes %s",
163 ServerInstance->Config->DefaultModes.c_str());
164 irc::spacesepstream list(ServerInstance->Config->DefaultModes);
166 std::string parameter;
168 list.GetToken(modeseq);
170 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
172 ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
175 if (mode->GetNumParams(true))
176 list.GetToken(parameter);
180 mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, this, parameter, true);
186 * add a channel to a user, creating the record for it if needed and linking
187 * it to the user record
189 Channel* Channel::JoinUser(LocalUser* user, std::string cname, bool override, const std::string& key)
191 if (user->registered != REG_ALL)
193 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "Attempted to join unregistered user " + user->uuid + " to channel " + cname);
198 * We don't restrict the number of channels that remote users or users that are override-joining may be in.
199 * We restrict local users to MaxChans channels.
200 * We restrict local operators to OperMaxChans channels.
201 * This is a lot more logical than how it was formerly. -- w00t
205 if (user->HasPrivPermission("channels/high-join-limit"))
207 if (user->chans.size() >= ServerInstance->Config->OperMaxChans)
209 user->WriteNumeric(ERR_TOOMANYCHANNELS, "%s %s :You are on too many channels",user->nick.c_str(), cname.c_str());
215 unsigned int maxchans = user->GetClass()->maxchans;
217 maxchans = ServerInstance->Config->MaxChans;
218 if (user->chans.size() >= maxchans)
220 user->WriteNumeric(ERR_TOOMANYCHANNELS, "%s %s :You are on too many channels",user->nick.c_str(), cname.c_str());
226 // Crop channel name if it's too long
227 if (cname.length() > ServerInstance->Config->Limits.ChanMax)
228 cname.resize(ServerInstance->Config->Limits.ChanMax);
230 Channel* chan = ServerInstance->FindChan(cname);
231 bool created_by_local = (chan == NULL); // Flag that will be passed to modules in the OnUserJoin() hook later
232 std::string privs; // Prefix mode(letter)s to give to the joining user
238 if (override == false)
240 // Ask the modules whether they're ok with the join, pass NULL as Channel* as the channel is yet to be created
241 ModResult MOD_RESULT;
242 FIRST_MOD_RESULT(OnUserPreJoin, MOD_RESULT, (user, NULL, cname, privs, key));
243 if (MOD_RESULT == MOD_RES_DENY)
244 return NULL; // A module wasn't happy with the join, abort
247 chan = new Channel(cname, ServerInstance->Time());
248 // Set the default modes on the channel (<options:defaultmodes>)
249 chan->SetDefaultModes();
253 /* Already on the channel */
254 if (chan->HasUser(user))
257 if (override == false)
259 ModResult MOD_RESULT;
260 FIRST_MOD_RESULT(OnUserPreJoin, MOD_RESULT, (user, chan, cname, privs, key));
262 // A module explicitly denied the join and (hopefully) generated a message
263 // describing the situation, so we may stop here without sending anything
264 if (MOD_RESULT == MOD_RES_DENY)
267 // If no module returned MOD_RES_DENY or MOD_RES_ALLOW (which is the case
268 // most of the time) then proceed to check channel modes +k, +i, +l and bans,
270 // If a module explicitly allowed the join (by returning MOD_RES_ALLOW),
271 // then this entire section is skipped
272 if (MOD_RESULT == MOD_RES_PASSTHRU)
274 std::string ckey = chan->GetModeParameter('k');
275 bool invited = user->IsInvited(chan);
276 bool can_bypass = ServerInstance->Config->InvBypassModes && invited;
280 FIRST_MOD_RESULT(OnCheckKey, MOD_RESULT, (user, chan, key));
281 if (!MOD_RESULT.check((ckey == key) || can_bypass))
283 // If no key provided, or key is not the right one, and can't bypass +k (not invited or option not enabled)
284 user->WriteNumeric(ERR_BADCHANNELKEY, "%s %s :Cannot join channel (Incorrect channel key)",user->nick.c_str(), chan->name.c_str());
289 if (chan->IsModeSet('i'))
291 FIRST_MOD_RESULT(OnCheckInvite, MOD_RESULT, (user, chan));
292 if (!MOD_RESULT.check(invited))
294 user->WriteNumeric(ERR_INVITEONLYCHAN, "%s %s :Cannot join channel (Invite only)",user->nick.c_str(), chan->name.c_str());
299 std::string limit = chan->GetModeParameter('l');
302 FIRST_MOD_RESULT(OnCheckLimit, MOD_RESULT, (user, chan));
303 if (!MOD_RESULT.check((chan->GetUserCounter() < atol(limit.c_str()) || can_bypass)))
305 user->WriteNumeric(ERR_CHANNELISFULL, "%s %s :Cannot join channel (Channel is full)",user->nick.c_str(), chan->name.c_str());
310 if (chan->IsBanned(user) && !can_bypass)
312 user->WriteNumeric(ERR_BANNEDFROMCHAN, "%s %s :Cannot join channel (You're banned)",user->nick.c_str(), chan->name.c_str());
317 * If the user has invites for this channel, remove them now
318 * after a successful join so they don't build up.
322 user->RemoveInvite(chan);
328 // We figured that this join is allowed and also created the
329 // channel if it didn't exist before, now do the actual join
330 chan->ForceJoin(user, &privs, false, created_by_local);
334 void Channel::ForceJoin(User* user, const std::string* privs, bool bursting, bool created_by_local)
336 Membership* memb = this->AddUser(user);
338 return; // Already on the channel
342 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "Attempted to join server user " + user->uuid + " to channel " + this->name);
346 user->chans.insert(this);
350 // If the user was granted prefix modes (in the OnUserPreJoin hook, or he's a
351 // remote user and his own server set the modes), then set them internally now
352 memb->modes = *privs;
353 for (std::string::const_iterator i = privs->begin(); i != privs->end(); ++i)
355 ModeHandler* mh = ServerInstance->Modes->FindMode(*i, MODETYPE_CHANNEL);
358 std::string nick = user->nick;
359 /* Set, and make sure that the mode handler knows this mode was now set */
360 this->SetPrefix(user, mh->GetModeChar(), true);
361 mh->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, this, nick, true);
366 // 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
368 FOREACH_MOD(I_OnUserJoin,OnUserJoin(memb, bursting, created_by_local, except_list));
370 this->WriteAllExcept(user, false, 0, except_list, "JOIN :%s", this->name.c_str());
372 /* Theyre not the first ones in here, make sure everyone else sees the modes we gave the user */
373 if ((GetUserCounter() > 1) && (!memb->modes.empty()))
375 std::string ms = memb->modes;
376 for(unsigned int i=0; i < memb->modes.length(); i++)
377 ms.append(" ").append(user->nick);
379 except_list.insert(user);
380 this->WriteAllExcept(user, !ServerInstance->Config->CycleHostsFromUser, 0, except_list, "MODE %s +%s", this->name.c_str(), ms.c_str());
387 user->WriteNumeric(RPL_TOPIC, "%s %s :%s", user->nick.c_str(), this->name.c_str(), this->topic.c_str());
388 user->WriteNumeric(RPL_TOPICTIME, "%s %s %s %lu", user->nick.c_str(), this->name.c_str(), this->setby.c_str(), (unsigned long)this->topicset);
390 this->UserList(user);
393 FOREACH_MOD(I_OnPostJoin,OnPostJoin(memb));
396 bool Channel::IsBanned(User* user)
399 FIRST_MOD_RESULT(OnCheckChannelBan, result, (user, this));
401 if (result != MOD_RES_PASSTHRU)
402 return (result == MOD_RES_DENY);
404 ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
405 const ListModeBase::ModeList* bans = banlm->GetList(this);
408 for (ListModeBase::ModeList::const_iterator it = bans->begin(); it != bans->end(); it++)
410 if (CheckBan(user, it->mask))
417 bool Channel::CheckBan(User* user, const std::string& mask)
420 FIRST_MOD_RESULT(OnCheckBan, result, (user, this, mask));
421 if (result != MOD_RES_PASSTHRU)
422 return (result == MOD_RES_DENY);
424 // extbans were handled above, if this is one it obviously didn't match
425 if ((mask.length() <= 2) || (mask[1] == ':'))
428 std::string::size_type at = mask.find('@');
429 if (at == std::string::npos)
432 const std::string nickIdent = user->nick + "!" + user->ident;
433 std::string prefix = mask.substr(0, at);
434 if (InspIRCd::Match(nickIdent, prefix, NULL))
436 std::string suffix = mask.substr(at + 1);
437 if (InspIRCd::Match(user->host, suffix, NULL) ||
438 InspIRCd::Match(user->dhost, suffix, NULL) ||
439 InspIRCd::MatchCIDR(user->GetIPString(), suffix, NULL))
445 ModResult Channel::GetExtBanStatus(User *user, char type)
448 FIRST_MOD_RESULT(OnExtBanCheck, rv, (user, this, type));
449 if (rv != MOD_RES_PASSTHRU)
452 ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
453 const ListModeBase::ModeList* bans = banlm->GetList(this);
457 for (ListModeBase::ModeList::const_iterator it = bans->begin(); it != bans->end(); ++it)
459 if (CheckBan(user, it->mask))
463 return MOD_RES_PASSTHRU;
467 * Remove a channel from a users record, remove the reference to the Membership object
468 * from the channel and destroy it.
470 void Channel::PartUser(User *user, std::string &reason)
472 UserMembIter membiter = userlist.find(user);
474 if (membiter != userlist.end())
476 Membership* memb = membiter->second;
478 FOREACH_MOD(I_OnUserPart,OnUserPart(memb, reason, except_list));
480 WriteAllExcept(user, false, 0, except_list, "PART %s%s%s", this->name.c_str(), reason.empty() ? "" : " :", reason.c_str());
482 // Remove this channel from the user's chanlist
483 user->chans.erase(this);
484 // Remove the Membership from this channel's userlist and destroy it
485 this->DelUser(membiter);
489 void Channel::KickUser(User* src, User* victim, const std::string& reason, Membership* srcmemb)
491 UserMembIter victimiter = userlist.find(victim);
492 Membership* memb = ((victimiter != userlist.end()) ? victimiter->second : NULL);
496 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());
500 // Do the following checks only if the KICK is done by a local user;
501 // each server enforces its own rules.
504 // Modules are allowed to explicitly allow or deny kicks done by local users
506 FIRST_MOD_RESULT(OnUserPreKick, res, (src,memb,reason));
507 if (res == MOD_RES_DENY)
510 if (res == MOD_RES_PASSTHRU)
513 srcmemb = GetUser(src);
514 unsigned int them = srcmemb ? srcmemb->getRank() : 0;
515 unsigned int req = HALFOP_VALUE;
516 for (std::string::size_type i = 0; i < memb->modes.length(); i++)
518 ModeHandler* mh = ServerInstance->Modes->FindMode(memb->modes[i], MODETYPE_CHANNEL);
519 if (mh && mh->GetLevelRequired() > req)
520 req = mh->GetLevelRequired();
525 src->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s %s :You must be a channel %soperator",
526 src->nick.c_str(), this->name.c_str(), req > HALFOP_VALUE ? "" : "half-");
533 FOREACH_MOD(I_OnUserKick,OnUserKick(src, memb, reason, except_list));
535 WriteAllExcept(src, false, 0, except_list, "KICK %s %s :%s", name.c_str(), victim->nick.c_str(), reason.c_str());
537 victim->chans.erase(this);
538 this->DelUser(victimiter);
541 void Channel::WriteChannel(User* user, const char* text, ...)
543 std::string textbuffer;
544 VAFORMAT(textbuffer, text, text);
545 this->WriteChannel(user, textbuffer);
548 void Channel::WriteChannel(User* user, const std::string &text)
550 const std::string message = ":" + user->GetFullHost() + " " + text;
552 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
554 if (IS_LOCAL(i->first))
555 i->first->Write(message);
559 void Channel::WriteChannelWithServ(const std::string& ServName, const char* text, ...)
561 std::string textbuffer;
562 VAFORMAT(textbuffer, text, text);
563 this->WriteChannelWithServ(ServName, textbuffer);
566 void Channel::WriteChannelWithServ(const std::string& ServName, const std::string &text)
568 const std::string message = ":" + (ServName.empty() ? ServerInstance->Config->ServerName : ServName) + " " + text;
570 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
572 if (IS_LOCAL(i->first))
573 i->first->Write(message);
577 /* write formatted text from a source user to all users on a channel except
578 * for the sender (for privmsg etc) */
579 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const char* text, ...)
581 std::string textbuffer;
582 VAFORMAT(textbuffer, text, text);
583 this->WriteAllExceptSender(user, serversource, status, textbuffer);
586 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const char* text, ...)
588 std::string textbuffer;
589 VAFORMAT(textbuffer, text, text);
590 textbuffer = ":" + (serversource ? ServerInstance->Config->ServerName : user->GetFullHost()) + " " + textbuffer;
591 this->RawWriteAllExcept(user, serversource, status, except_list, textbuffer);
594 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string &text)
596 const std::string message = ":" + (serversource ? ServerInstance->Config->ServerName : user->GetFullHost()) + " " + text;
597 this->RawWriteAllExcept(user, serversource, status, except_list, message);
600 void Channel::RawWriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string &out)
602 unsigned int minrank = 0;
605 ModeHandler* mh = ServerInstance->Modes->FindPrefix(status);
607 minrank = mh->GetPrefixRank();
609 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
611 if (IS_LOCAL(i->first) && (except_list.find(i->first) == except_list.end()))
613 /* User doesn't have the status we're after */
614 if (minrank && i->second->getRank() < minrank)
617 i->first->Write(out);
622 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const std::string& text)
625 except_list.insert(user);
626 this->WriteAllExcept(user, serversource, status, except_list, std::string(text));
629 const char* Channel::ChanModes(bool showkey)
631 static std::string scratch;
636 /* This was still iterating up to 190, Channel::modes is only 64 elements -- Om */
637 for(int n = 0; n < 64; n++)
641 scratch.push_back(n + 65);
642 if (n == 'k' - 65 && !showkey)
648 const std::string param = this->GetModeParameter(n + 65);
659 return scratch.c_str();
662 /* compile a userlist of a channel into a string, each nick seperated by
663 * spaces and op, voice etc status shown as @ and +, and send it to 'user'
665 void Channel::UserList(User *user)
667 if (this->IsModeSet('s') && !this->HasUser(user) && !user->HasPrivPermission("channels/auspex"))
669 user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nick/channel",user->nick.c_str(), this->name.c_str());
673 std::string list = user->nick;
675 list.push_back(this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=');
677 list.append(this->name).append(" :");
678 std::string::size_type pos = list.size();
680 bool has_one = false;
682 /* Improvement by Brain - this doesnt change in value, so why was it inside
685 bool has_user = this->HasUser(user);
687 std::string prefixlist;
689 for (UserMembIter i = userlist.begin(); i != userlist.end(); ++i)
691 if (i->first->quitting)
693 if ((!has_user) && (i->first->IsModeSet('i')))
696 * user is +i, and source not on the channel, does not show
702 prefixlist = this->GetPrefixChar(i->first);
703 nick = i->first->nick;
705 FOREACH_MOD(I_OnNamesListItem, OnNamesListItem(user, i->second, prefixlist, nick));
707 /* Nick was nuked, a module wants us to skip it */
711 if (list.size() + prefixlist.length() + nick.length() + 1 > 480)
713 /* list overflowed into multiple numerics */
714 user->WriteNumeric(RPL_NAMREPLY, list);
716 // Erase all nicks, keep the constant part
721 list.append(prefixlist).append(nick).push_back(' ');
726 /* if whats left in the list isnt empty, send it */
729 user->WriteNumeric(RPL_NAMREPLY, list);
732 user->WriteNumeric(RPL_ENDOFNAMES, "%s %s :End of /NAMES list.", user->nick.c_str(), this->name.c_str());
735 /* returns the status character for a given user on a channel, e.g. @ for op,
736 * % for halfop etc. If the user has several modes set, the highest mode
737 * the user has must be returned.
739 const char* Channel::GetPrefixChar(User *user)
741 static char pf[2] = {0, 0};
743 unsigned int bestrank = 0;
745 UserMembIter m = userlist.find(user);
746 if (m != userlist.end())
748 for(unsigned int i=0; i < m->second->modes.length(); i++)
750 char mchar = m->second->modes[i];
751 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
752 if (mh && mh->GetPrefixRank() > bestrank && mh->GetPrefix())
754 bestrank = mh->GetPrefixRank();
755 pf[0] = mh->GetPrefix();
762 unsigned int Membership::getRank()
764 char mchar = modes.c_str()[0];
768 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
770 rv = mh->GetPrefixRank();
775 const char* Channel::GetAllPrefixChars(User* user)
777 static char prefix[64];
780 UserMembIter m = userlist.find(user);
781 if (m != userlist.end())
783 for(unsigned int i=0; i < m->second->modes.length(); i++)
785 char mchar = m->second->modes[i];
786 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
787 if (mh && mh->GetPrefix())
788 prefix[ctr++] = mh->GetPrefix();
796 unsigned int Channel::GetPrefixValue(User* user)
798 UserMembIter m = userlist.find(user);
799 if (m == userlist.end())
801 return m->second->getRank();
804 bool Channel::SetPrefix(User* user, char prefix, bool adding)
806 ModeHandler* delta_mh = ServerInstance->Modes->FindMode(prefix, MODETYPE_CHANNEL);
809 UserMembIter m = userlist.find(user);
810 if (m == userlist.end())
812 for(unsigned int i=0; i < m->second->modes.length(); i++)
814 char mchar = m->second->modes[i];
815 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
816 if (mh && mh->GetPrefixRank() <= delta_mh->GetPrefixRank())
819 m->second->modes.substr(0,i) +
820 (adding ? std::string(1, prefix) : "") +
821 m->second->modes.substr(mchar == prefix ? i+1 : i);
822 return adding != (mchar == prefix);
826 m->second->modes += std::string(1, prefix);
830 void Invitation::Create(Channel* c, LocalUser* u, time_t timeout)
832 if ((timeout != 0) && (ServerInstance->Time() >= timeout))
833 // Expired, don't bother
836 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Create chan=%s user=%s", c->name.c_str(), u->uuid.c_str());
838 Invitation* inv = Invitation::Find(c, u, false);
841 if ((inv->expiry == 0) || (inv->expiry > timeout))
843 inv->expiry = timeout;
844 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Create changed expiry in existing invitation %p", (void*) inv);
848 inv = new Invitation(c, u, timeout);
849 c->invites.push_back(inv);
850 u->invites.push_back(inv);
851 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Create created new invitation %p", (void*) inv);
855 Invitation* Invitation::Find(Channel* c, LocalUser* u, bool check_expired)
857 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);
858 if (!u || u->invites.empty())
861 InviteList locallist;
862 locallist.swap(u->invites);
864 Invitation* result = NULL;
865 for (InviteList::iterator i = locallist.begin(); i != locallist.end(); )
867 Invitation* inv = *i;
868 if ((check_expired) && (inv->expiry != 0) && (inv->expiry <= ServerInstance->Time()))
870 /* Expired invite, remove it. */
871 std::string expiration = ServerInstance->TimeString(inv->expiry);
872 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Find ecountered expired entry: %p expired %s", (void*) inv, expiration.c_str());
873 i = locallist.erase(i);
879 /* Is it what we're searching for? */
889 locallist.swap(u->invites);
890 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Find result=%p", (void*) result);
894 Invitation::~Invitation()
896 // Remove this entry from both lists
897 InviteList::iterator it = std::find(chan->invites.begin(), chan->invites.end(), this);
898 if (it != chan->invites.end())
899 chan->invites.erase(it);
900 it = std::find(user->invites.begin(), user->invites.end(), this);
901 if (it != user->invites.end())
902 user->invites.erase(it);
905 void InviteBase::ClearInvites()
907 ServerInstance->Logs->Log("INVITEBASE", LOG_DEBUG, "InviteBase::ClearInvites %p", (void*) this);
908 InviteList locallist;
909 locallist.swap(invites);
910 for (InviteList::const_iterator i = locallist.begin(); i != locallist.end(); ++i)