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