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)
43 : name(cname), age(ts), topicset(0)
45 if (!ServerInstance->chanlist->insert(std::make_pair(cname, this)).second)
46 throw CoreException("Cannot create duplicate channel " + cname);
49 void Channel::SetMode(ModeHandler* mh, bool on)
51 modes[mh->GetModeChar() - 65] = on;
54 void Channel::SetModeParam(ModeHandler* mh, const std::string& parameter)
56 char mode = mh->GetModeChar();
57 if (parameter.empty())
59 custom_mode_params.erase(mode);
60 modes[mode-65] = false;
64 custom_mode_params[mode] = parameter;
65 modes[mode-65] = true;
69 std::string Channel::GetModeParameter(ModeHandler* mode)
71 CustomModeList::iterator n = custom_mode_params.find(mode->GetModeChar());
72 if (n != custom_mode_params.end())
77 void Channel::SetTopic(User* u, const std::string& ntopic)
79 this->topic.assign(ntopic, 0, ServerInstance->Config->Limits.MaxTopic);
80 this->setby.assign(ServerInstance->Config->FullHostInTopic ? u->GetFullHost() : u->nick, 0, 128);
81 this->WriteChannel(u, "TOPIC %s :%s", this->name.c_str(), this->topic.c_str());
82 this->topicset = ServerInstance->Time();
84 FOREACH_MOD(OnPostTopicChange, (u, this, this->topic));
87 Membership* Channel::AddUser(User* user)
89 Membership*& memb = userlist[user];
93 memb = new Membership(user, this);
97 void Channel::DelUser(User* user)
99 UserMembIter it = userlist.find(user);
100 if (it != userlist.end())
104 void Channel::CheckDestroy()
106 if (!userlist.empty())
110 FIRST_MOD_RESULT(OnChannelPreDelete, res, (this));
111 if (res == MOD_RES_DENY)
114 chan_hash::iterator iter = ServerInstance->chanlist->find(this->name);
115 /* kill the record */
116 if (iter != ServerInstance->chanlist->end())
118 FOREACH_MOD(OnChannelDelete, (this));
119 ServerInstance->chanlist->erase(iter);
123 ServerInstance->GlobalCulls.AddItem(this);
126 void Channel::DelUser(const UserMembIter& membiter)
128 Membership* memb = membiter->second;
131 userlist.erase(membiter);
133 // If this channel became empty then it should be removed
137 Membership* Channel::GetUser(User* user)
139 UserMembIter i = userlist.find(user);
140 if (i == userlist.end())
145 void Channel::SetDefaultModes()
147 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "SetDefaultModes %s",
148 ServerInstance->Config->DefaultModes.c_str());
149 irc::spacesepstream list(ServerInstance->Config->DefaultModes);
151 std::string parameter;
153 list.GetToken(modeseq);
155 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
157 ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
160 if (mode->GetPrefixRank())
163 if (mode->GetNumParams(true))
164 list.GetToken(parameter);
168 mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, this, parameter, true);
174 * add a channel to a user, creating the record for it if needed and linking
175 * it to the user record
177 Channel* Channel::JoinUser(LocalUser* user, std::string cname, bool override, const std::string& key)
179 if (user->registered != REG_ALL)
181 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "Attempted to join unregistered user " + user->uuid + " to channel " + cname);
186 * We don't restrict the number of channels that remote users or users that are override-joining may be in.
187 * We restrict local users to MaxChans channels.
188 * We restrict local operators to OperMaxChans channels.
189 * This is a lot more logical than how it was formerly. -- w00t
193 if (user->HasPrivPermission("channels/high-join-limit"))
195 if (user->chans.size() >= ServerInstance->Config->OperMaxChans)
197 user->WriteNumeric(ERR_TOOMANYCHANNELS, "%s %s :You are on too many channels",user->nick.c_str(), cname.c_str());
203 unsigned int maxchans = user->GetClass()->maxchans;
205 maxchans = ServerInstance->Config->MaxChans;
206 if (user->chans.size() >= maxchans)
208 user->WriteNumeric(ERR_TOOMANYCHANNELS, "%s %s :You are on too many channels",user->nick.c_str(), cname.c_str());
214 // Crop channel name if it's too long
215 if (cname.length() > ServerInstance->Config->Limits.ChanMax)
216 cname.resize(ServerInstance->Config->Limits.ChanMax);
218 Channel* chan = ServerInstance->FindChan(cname);
219 bool created_by_local = (chan == NULL); // Flag that will be passed to modules in the OnUserJoin() hook later
220 std::string privs; // Prefix mode(letter)s to give to the joining user
224 privs = ServerInstance->Config->DefaultModes.substr(0, ServerInstance->Config->DefaultModes.find(' '));
226 if (override == false)
228 // Ask the modules whether they're ok with the join, pass NULL as Channel* as the channel is yet to be created
229 ModResult MOD_RESULT;
230 FIRST_MOD_RESULT(OnUserPreJoin, MOD_RESULT, (user, NULL, cname, privs, key));
231 if (MOD_RESULT == MOD_RES_DENY)
232 return NULL; // A module wasn't happy with the join, abort
235 chan = new Channel(cname, ServerInstance->Time());
236 // Set the default modes on the channel (<options:defaultmodes>)
237 chan->SetDefaultModes();
241 /* Already on the channel */
242 if (chan->HasUser(user))
245 if (override == false)
247 ModResult MOD_RESULT;
248 FIRST_MOD_RESULT(OnUserPreJoin, MOD_RESULT, (user, chan, cname, privs, key));
250 // A module explicitly denied the join and (hopefully) generated a message
251 // describing the situation, so we may stop here without sending anything
252 if (MOD_RESULT == MOD_RES_DENY)
255 // If no module returned MOD_RES_DENY or MOD_RES_ALLOW (which is the case
256 // most of the time) then proceed to check channel modes +k, +i, +l and bans,
258 // If a module explicitly allowed the join (by returning MOD_RES_ALLOW),
259 // then this entire section is skipped
260 if (MOD_RESULT == MOD_RES_PASSTHRU)
262 std::string ckey = chan->GetModeParameter(keymode);
263 bool invited = user->IsInvited(chan);
264 bool can_bypass = ServerInstance->Config->InvBypassModes && invited;
268 FIRST_MOD_RESULT(OnCheckKey, MOD_RESULT, (user, chan, key));
269 if (!MOD_RESULT.check((ckey == key) || can_bypass))
271 // If no key provided, or key is not the right one, and can't bypass +k (not invited or option not enabled)
272 user->WriteNumeric(ERR_BADCHANNELKEY, "%s %s :Cannot join channel (Incorrect channel key)",user->nick.c_str(), chan->name.c_str());
277 if (chan->IsModeSet(inviteonlymode))
279 FIRST_MOD_RESULT(OnCheckInvite, MOD_RESULT, (user, chan));
280 if (!MOD_RESULT.check(invited))
282 user->WriteNumeric(ERR_INVITEONLYCHAN, "%s %s :Cannot join channel (Invite only)",user->nick.c_str(), chan->name.c_str());
287 std::string limit = chan->GetModeParameter(limitmode);
290 FIRST_MOD_RESULT(OnCheckLimit, MOD_RESULT, (user, chan));
291 if (!MOD_RESULT.check((chan->GetUserCounter() < atol(limit.c_str()) || can_bypass)))
293 user->WriteNumeric(ERR_CHANNELISFULL, "%s %s :Cannot join channel (Channel is full)",user->nick.c_str(), chan->name.c_str());
298 if (chan->IsBanned(user) && !can_bypass)
300 user->WriteNumeric(ERR_BANNEDFROMCHAN, "%s %s :Cannot join channel (You're banned)",user->nick.c_str(), chan->name.c_str());
305 * If the user has invites for this channel, remove them now
306 * after a successful join so they don't build up.
310 user->RemoveInvite(chan);
316 // We figured that this join is allowed and also created the
317 // channel if it didn't exist before, now do the actual join
318 chan->ForceJoin(user, &privs, false, created_by_local);
322 void Channel::ForceJoin(User* user, const std::string* privs, bool bursting, bool created_by_local)
326 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "Attempted to join server user " + user->uuid + " to channel " + this->name);
330 Membership* memb = this->AddUser(user);
332 return; // Already on the channel
334 user->chans.insert(this);
338 // If the user was granted prefix modes (in the OnUserPreJoin hook, or he's a
339 // remote user and his own server set the modes), then set them internally now
340 memb->modes = *privs;
341 for (std::string::const_iterator i = privs->begin(); i != privs->end(); ++i)
343 ModeHandler* mh = ServerInstance->Modes->FindMode(*i, MODETYPE_CHANNEL);
344 if (mh && mh->GetPrefixRank())
346 std::string nick = user->nick;
347 /* Set, and make sure that the mode handler knows this mode was now set */
348 this->SetPrefix(user, mh->GetModeChar(), true);
349 mh->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, this, nick, true);
354 // 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
356 FOREACH_MOD(OnUserJoin, (memb, bursting, created_by_local, except_list));
358 this->WriteAllExcept(user, false, 0, except_list, "JOIN :%s", this->name.c_str());
360 /* Theyre not the first ones in here, make sure everyone else sees the modes we gave the user */
361 if ((GetUserCounter() > 1) && (!memb->modes.empty()))
363 std::string ms = memb->modes;
364 for(unsigned int i=0; i < memb->modes.length(); i++)
365 ms.append(" ").append(user->nick);
367 except_list.insert(user);
368 this->WriteAllExcept(user, !ServerInstance->Config->CycleHostsFromUser, 0, except_list, "MODE %s +%s", this->name.c_str(), ms.c_str());
375 user->WriteNumeric(RPL_TOPIC, "%s %s :%s", user->nick.c_str(), this->name.c_str(), this->topic.c_str());
376 user->WriteNumeric(RPL_TOPICTIME, "%s %s %s %lu", user->nick.c_str(), this->name.c_str(), this->setby.c_str(), (unsigned long)this->topicset);
378 this->UserList(user);
381 FOREACH_MOD(OnPostJoin, (memb));
384 bool Channel::IsBanned(User* user)
387 FIRST_MOD_RESULT(OnCheckChannelBan, result, (user, this));
389 if (result != MOD_RES_PASSTHRU)
390 return (result == MOD_RES_DENY);
392 ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
393 const ListModeBase::ModeList* bans = banlm->GetList(this);
396 for (ListModeBase::ModeList::const_iterator it = bans->begin(); it != bans->end(); it++)
398 if (CheckBan(user, it->mask))
405 bool Channel::CheckBan(User* user, const std::string& mask)
408 FIRST_MOD_RESULT(OnCheckBan, result, (user, this, mask));
409 if (result != MOD_RES_PASSTHRU)
410 return (result == MOD_RES_DENY);
412 // extbans were handled above, if this is one it obviously didn't match
413 if ((mask.length() <= 2) || (mask[1] == ':'))
416 std::string::size_type at = mask.find('@');
417 if (at == std::string::npos)
420 const std::string nickIdent = user->nick + "!" + user->ident;
421 std::string prefix = mask.substr(0, at);
422 if (InspIRCd::Match(nickIdent, prefix, NULL))
424 std::string suffix = mask.substr(at + 1);
425 if (InspIRCd::Match(user->host, suffix, NULL) ||
426 InspIRCd::Match(user->dhost, suffix, NULL) ||
427 InspIRCd::MatchCIDR(user->GetIPString(), suffix, NULL))
433 ModResult Channel::GetExtBanStatus(User *user, char type)
436 FIRST_MOD_RESULT(OnExtBanCheck, rv, (user, this, type));
437 if (rv != MOD_RES_PASSTHRU)
440 ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
441 const ListModeBase::ModeList* bans = banlm->GetList(this);
445 for (ListModeBase::ModeList::const_iterator it = bans->begin(); it != bans->end(); ++it)
447 if (CheckBan(user, it->mask))
451 return MOD_RES_PASSTHRU;
455 * Remove a channel from a users record, remove the reference to the Membership object
456 * from the channel and destroy it.
458 void Channel::PartUser(User *user, std::string &reason)
460 UserMembIter membiter = userlist.find(user);
462 if (membiter != userlist.end())
464 Membership* memb = membiter->second;
466 FOREACH_MOD(OnUserPart, (memb, reason, except_list));
468 WriteAllExcept(user, false, 0, except_list, "PART %s%s%s", this->name.c_str(), reason.empty() ? "" : " :", reason.c_str());
470 // Remove this channel from the user's chanlist
471 user->chans.erase(this);
472 // Remove the Membership from this channel's userlist and destroy it
473 this->DelUser(membiter);
477 void Channel::KickUser(User* src, User* victim, const std::string& reason, Membership* srcmemb)
479 UserMembIter victimiter = userlist.find(victim);
480 Membership* memb = ((victimiter != userlist.end()) ? victimiter->second : NULL);
484 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());
488 // Do the following checks only if the KICK is done by a local user;
489 // each server enforces its own rules.
492 // Modules are allowed to explicitly allow or deny kicks done by local users
494 FIRST_MOD_RESULT(OnUserPreKick, res, (src,memb,reason));
495 if (res == MOD_RES_DENY)
498 if (res == MOD_RES_PASSTHRU)
501 srcmemb = GetUser(src);
502 unsigned int them = srcmemb ? srcmemb->getRank() : 0;
503 unsigned int req = HALFOP_VALUE;
504 for (std::string::size_type i = 0; i < memb->modes.length(); i++)
506 ModeHandler* mh = ServerInstance->Modes->FindMode(memb->modes[i], MODETYPE_CHANNEL);
507 if (mh && mh->GetLevelRequired() > req)
508 req = mh->GetLevelRequired();
513 src->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s %s :You must be a channel %soperator",
514 src->nick.c_str(), this->name.c_str(), req > HALFOP_VALUE ? "" : "half-");
521 FOREACH_MOD(OnUserKick, (src, memb, reason, except_list));
523 WriteAllExcept(src, false, 0, except_list, "KICK %s %s :%s", name.c_str(), victim->nick.c_str(), reason.c_str());
525 victim->chans.erase(this);
526 this->DelUser(victimiter);
529 void Channel::WriteChannel(User* user, const char* text, ...)
531 std::string textbuffer;
532 VAFORMAT(textbuffer, text, text);
533 this->WriteChannel(user, textbuffer);
536 void Channel::WriteChannel(User* user, const std::string &text)
538 const std::string message = ":" + user->GetFullHost() + " " + text;
540 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
542 if (IS_LOCAL(i->first))
543 i->first->Write(message);
547 void Channel::WriteChannelWithServ(const std::string& ServName, const char* text, ...)
549 std::string textbuffer;
550 VAFORMAT(textbuffer, text, text);
551 this->WriteChannelWithServ(ServName, textbuffer);
554 void Channel::WriteChannelWithServ(const std::string& ServName, const std::string &text)
556 const std::string message = ":" + (ServName.empty() ? ServerInstance->Config->ServerName : ServName) + " " + text;
558 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
560 if (IS_LOCAL(i->first))
561 i->first->Write(message);
565 /* write formatted text from a source user to all users on a channel except
566 * for the sender (for privmsg etc) */
567 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const char* text, ...)
569 std::string textbuffer;
570 VAFORMAT(textbuffer, text, text);
571 this->WriteAllExceptSender(user, serversource, status, textbuffer);
574 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const char* text, ...)
576 std::string textbuffer;
577 VAFORMAT(textbuffer, text, text);
578 textbuffer = ":" + (serversource ? ServerInstance->Config->ServerName : user->GetFullHost()) + " " + textbuffer;
579 this->RawWriteAllExcept(user, serversource, status, except_list, textbuffer);
582 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string &text)
584 const std::string message = ":" + (serversource ? ServerInstance->Config->ServerName : user->GetFullHost()) + " " + text;
585 this->RawWriteAllExcept(user, serversource, status, except_list, message);
588 void Channel::RawWriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string &out)
590 unsigned int minrank = 0;
593 ModeHandler* mh = ServerInstance->Modes->FindPrefix(status);
595 minrank = mh->GetPrefixRank();
597 for (UserMembIter i = userlist.begin(); i != userlist.end(); i++)
599 if (IS_LOCAL(i->first) && (except_list.find(i->first) == except_list.end()))
601 /* User doesn't have the status we're after */
602 if (minrank && i->second->getRank() < minrank)
605 i->first->Write(out);
610 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const std::string& text)
613 except_list.insert(user);
614 this->WriteAllExcept(user, serversource, status, except_list, std::string(text));
617 const char* Channel::ChanModes(bool showkey)
619 static std::string scratch;
624 /* This was still iterating up to 190, Channel::modes is only 64 elements -- Om */
625 for(int n = 0; n < 64; n++)
629 scratch.push_back(n + 65);
630 ModeHandler* mh = ServerInstance->Modes->FindMode(n+'A', MODETYPE_CHANNEL);
634 if (n == 'k' - 65 && !showkey)
640 const std::string param = this->GetModeParameter(mh);
651 return scratch.c_str();
654 /* compile a userlist of a channel into a string, each nick seperated by
655 * spaces and op, voice etc status shown as @ and +, and send it to 'user'
657 void Channel::UserList(User *user)
659 if (this->IsModeSet(secretmode) && !this->HasUser(user) && !user->HasPrivPermission("channels/auspex"))
661 user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nick/channel",user->nick.c_str(), this->name.c_str());
665 std::string list = user->nick;
667 list.push_back(this->IsModeSet(secretmode) ? '@' : this->IsModeSet(privatemode) ? '*' : '=');
669 list.append(this->name).append(" :");
670 std::string::size_type pos = list.size();
672 bool has_one = false;
674 /* Improvement by Brain - this doesnt change in value, so why was it inside
677 bool has_user = this->HasUser(user);
679 std::string prefixlist;
681 for (UserMembIter i = userlist.begin(); i != userlist.end(); ++i)
683 if (i->first->quitting)
685 if ((!has_user) && (i->first->IsModeSet(invisiblemode)))
688 * user is +i, and source not on the channel, does not show
694 prefixlist = this->GetPrefixChar(i->first);
695 nick = i->first->nick;
697 FOREACH_MOD(OnNamesListItem, (user, i->second, prefixlist, nick));
699 /* Nick was nuked, a module wants us to skip it */
703 if (list.size() + prefixlist.length() + nick.length() + 1 > 480)
705 /* list overflowed into multiple numerics */
706 user->WriteNumeric(RPL_NAMREPLY, list);
708 // Erase all nicks, keep the constant part
713 list.append(prefixlist).append(nick).push_back(' ');
718 /* if whats left in the list isnt empty, send it */
721 user->WriteNumeric(RPL_NAMREPLY, list);
724 user->WriteNumeric(RPL_ENDOFNAMES, "%s %s :End of /NAMES list.", user->nick.c_str(), this->name.c_str());
727 /* returns the status character for a given user on a channel, e.g. @ for op,
728 * % for halfop etc. If the user has several modes set, the highest mode
729 * the user has must be returned.
731 const char* Channel::GetPrefixChar(User *user)
733 static char pf[2] = {0, 0};
735 unsigned int bestrank = 0;
737 UserMembIter m = userlist.find(user);
738 if (m != userlist.end())
740 for(unsigned int i=0; i < m->second->modes.length(); i++)
742 char mchar = m->second->modes[i];
743 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
744 if (mh && mh->GetPrefixRank() > bestrank && mh->GetPrefix())
746 bestrank = mh->GetPrefixRank();
747 pf[0] = mh->GetPrefix();
754 unsigned int Membership::getRank()
756 char mchar = modes.c_str()[0];
760 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
762 rv = mh->GetPrefixRank();
767 const char* Channel::GetAllPrefixChars(User* user)
769 static char prefix[64];
772 UserMembIter m = userlist.find(user);
773 if (m != userlist.end())
775 for(unsigned int i=0; i < m->second->modes.length(); i++)
777 char mchar = m->second->modes[i];
778 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
779 if (mh && mh->GetPrefix())
780 prefix[ctr++] = mh->GetPrefix();
788 unsigned int Channel::GetPrefixValue(User* user)
790 UserMembIter m = userlist.find(user);
791 if (m == userlist.end())
793 return m->second->getRank();
796 bool Channel::SetPrefix(User* user, char prefix, bool adding)
798 ModeHandler* delta_mh = ServerInstance->Modes->FindMode(prefix, MODETYPE_CHANNEL);
801 UserMembIter m = userlist.find(user);
802 if (m == userlist.end())
804 for(unsigned int i=0; i < m->second->modes.length(); i++)
806 char mchar = m->second->modes[i];
807 ModeHandler* mh = ServerInstance->Modes->FindMode(mchar, MODETYPE_CHANNEL);
808 if (mh && mh->GetPrefixRank() <= delta_mh->GetPrefixRank())
811 m->second->modes.substr(0,i) +
812 (adding ? std::string(1, prefix) : "") +
813 m->second->modes.substr(mchar == prefix ? i+1 : i);
814 return adding != (mchar == prefix);
818 m->second->modes += std::string(1, prefix);
822 void Invitation::Create(Channel* c, LocalUser* u, time_t timeout)
824 if ((timeout != 0) && (ServerInstance->Time() >= timeout))
825 // Expired, don't bother
828 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Create chan=%s user=%s", c->name.c_str(), u->uuid.c_str());
830 Invitation* inv = Invitation::Find(c, u, false);
833 if ((inv->expiry == 0) || (inv->expiry > timeout))
835 inv->expiry = timeout;
836 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Create changed expiry in existing invitation %p", (void*) inv);
840 inv = new Invitation(c, u, timeout);
841 c->invites.push_back(inv);
842 u->invites.push_back(inv);
843 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Create created new invitation %p", (void*) inv);
847 Invitation* Invitation::Find(Channel* c, LocalUser* u, bool check_expired)
849 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);
850 if (!u || u->invites.empty())
853 InviteList locallist;
854 locallist.swap(u->invites);
856 Invitation* result = NULL;
857 for (InviteList::iterator i = locallist.begin(); i != locallist.end(); )
859 Invitation* inv = *i;
860 if ((check_expired) && (inv->expiry != 0) && (inv->expiry <= ServerInstance->Time()))
862 /* Expired invite, remove it. */
863 std::string expiration = ServerInstance->TimeString(inv->expiry);
864 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Find ecountered expired entry: %p expired %s", (void*) inv, expiration.c_str());
865 i = locallist.erase(i);
871 /* Is it what we're searching for? */
881 locallist.swap(u->invites);
882 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Find result=%p", (void*) result);
886 Invitation::~Invitation()
888 // Remove this entry from both lists
889 InviteList::iterator it = std::find(chan->invites.begin(), chan->invites.end(), this);
890 if (it != chan->invites.end())
891 chan->invites.erase(it);
892 it = std::find(user->invites.begin(), user->invites.end(), this);
893 if (it != user->invites.end())
894 user->invites.erase(it);
897 void InviteBase::ClearInvites()
899 ServerInstance->Logs->Log("INVITEBASE", LOG_DEBUG, "InviteBase::ClearInvites %p", (void*) this);
900 InviteList locallist;
901 locallist.swap(invites);
902 for (InviteList::const_iterator i = locallist.begin(); i != locallist.end(); ++i)