]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/channels.cpp
d99a577234dc4aeb5befafe604c136918055f0a6
[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                                 bool invited = user->IsInvited(chan);
239                                 bool can_bypass = ServerInstance->Config->InvBypassModes && invited;
240
241                                 if (!ckey.empty())
242                                 {
243                                         FIRST_MOD_RESULT(OnCheckKey, MOD_RESULT, (user, chan, key));
244                                         if (!MOD_RESULT.check(InspIRCd::TimingSafeCompare(ckey, key) || can_bypass))
245                                         {
246                                                 // If no key provided, or key is not the right one, and can't bypass +k (not invited or option not enabled)
247                                                 user->WriteNumeric(ERR_BADCHANNELKEY, "%s :Cannot join channel (Incorrect channel key)", chan->name.c_str());
248                                                 return NULL;
249                                         }
250                                 }
251
252                                 if (chan->IsModeSet(inviteonlymode))
253                                 {
254                                         FIRST_MOD_RESULT(OnCheckInvite, MOD_RESULT, (user, chan));
255                                         if (!MOD_RESULT.check(invited))
256                                         {
257                                                 user->WriteNumeric(ERR_INVITEONLYCHAN, "%s :Cannot join channel (Invite only)", chan->name.c_str());
258                                                 return NULL;
259                                         }
260                                 }
261
262                                 std::string limit = chan->GetModeParameter(limitmode);
263                                 if (!limit.empty())
264                                 {
265                                         FIRST_MOD_RESULT(OnCheckLimit, MOD_RESULT, (user, chan));
266                                         if (!MOD_RESULT.check((chan->GetUserCounter() < atol(limit.c_str()) || can_bypass)))
267                                         {
268                                                 user->WriteNumeric(ERR_CHANNELISFULL, "%s :Cannot join channel (Channel is full)", chan->name.c_str());
269                                                 return NULL;
270                                         }
271                                 }
272
273                                 if (chan->IsBanned(user) && !can_bypass)
274                                 {
275                                         user->WriteNumeric(ERR_BANNEDFROMCHAN, "%s :Cannot join channel (You're banned)", chan->name.c_str());
276                                         return NULL;
277                                 }
278
279                                 /*
280                                  * If the user has invites for this channel, remove them now
281                                  * after a successful join so they don't build up.
282                                  */
283                                 if (invited)
284                                 {
285                                         user->RemoveInvite(chan);
286                                 }
287                         }
288                 }
289         }
290
291         // We figured that this join is allowed and also created the
292         // channel if it didn't exist before, now do the actual join
293         chan->ForceJoin(user, &privs, false, created_by_local);
294         return chan;
295 }
296
297 Membership* Channel::ForceJoin(User* user, const std::string* privs, bool bursting, bool created_by_local)
298 {
299         if (IS_SERVER(user))
300         {
301                 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "Attempted to join server user " + user->uuid + " to channel " + this->name);
302                 return NULL;
303         }
304
305         Membership* memb = this->AddUser(user);
306         if (!memb)
307                 return NULL; // Already on the channel
308
309         user->chans.push_front(memb);
310
311         if (privs)
312         {
313                 // If the user was granted prefix modes (in the OnUserPreJoin hook, or he's a
314                 // remote user and his own server set the modes), then set them internally now
315                 for (std::string::const_iterator i = privs->begin(); i != privs->end(); ++i)
316                 {
317                         PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(*i);
318                         if (mh)
319                         {
320                                 std::string nick = user->nick;
321                                 // Set the mode on the user
322                                 mh->OnModeChange(ServerInstance->FakeClient, NULL, this, nick, true);
323                         }
324                 }
325         }
326
327         // 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
328         CUList except_list;
329         FOREACH_MOD(OnUserJoin, (memb, bursting, created_by_local, except_list));
330
331         this->WriteAllExcept(user, false, 0, except_list, "JOIN :%s", this->name.c_str());
332
333         /* Theyre not the first ones in here, make sure everyone else sees the modes we gave the user */
334         if ((GetUserCounter() > 1) && (!memb->modes.empty()))
335         {
336                 std::string ms = memb->modes;
337                 for(unsigned int i=0; i < memb->modes.length(); i++)
338                         ms.append(" ").append(user->nick);
339
340                 except_list.insert(user);
341                 this->WriteAllExcept(user, !ServerInstance->Config->CycleHostsFromUser, 0, except_list, "MODE %s +%s", this->name.c_str(), ms.c_str());
342         }
343
344         FOREACH_MOD(OnPostJoin, (memb));
345         return memb;
346 }
347
348 bool Channel::IsBanned(User* user)
349 {
350         ModResult result;
351         FIRST_MOD_RESULT(OnCheckChannelBan, result, (user, this));
352
353         if (result != MOD_RES_PASSTHRU)
354                 return (result == MOD_RES_DENY);
355
356         ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
357         const ListModeBase::ModeList* bans = banlm->GetList(this);
358         if (bans)
359         {
360                 for (ListModeBase::ModeList::const_iterator it = bans->begin(); it != bans->end(); it++)
361                 {
362                         if (CheckBan(user, it->mask))
363                                 return true;
364                 }
365         }
366         return false;
367 }
368
369 bool Channel::CheckBan(User* user, const std::string& mask)
370 {
371         ModResult result;
372         FIRST_MOD_RESULT(OnCheckBan, result, (user, this, mask));
373         if (result != MOD_RES_PASSTHRU)
374                 return (result == MOD_RES_DENY);
375
376         // extbans were handled above, if this is one it obviously didn't match
377         if ((mask.length() <= 2) || (mask[1] == ':'))
378                 return false;
379
380         std::string::size_type at = mask.find('@');
381         if (at == std::string::npos)
382                 return false;
383
384         const std::string nickIdent = user->nick + "!" + user->ident;
385         std::string prefix(mask, 0, at);
386         if (InspIRCd::Match(nickIdent, prefix, NULL))
387         {
388                 std::string suffix(mask, at + 1);
389                 if (InspIRCd::Match(user->host, suffix, NULL) ||
390                         InspIRCd::Match(user->dhost, suffix, NULL) ||
391                         InspIRCd::MatchCIDR(user->GetIPString(), suffix, NULL))
392                         return true;
393         }
394         return false;
395 }
396
397 ModResult Channel::GetExtBanStatus(User *user, char type)
398 {
399         ModResult rv;
400         FIRST_MOD_RESULT(OnExtBanCheck, rv, (user, this, type));
401         if (rv != MOD_RES_PASSTHRU)
402                 return rv;
403
404         ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
405         const ListModeBase::ModeList* bans = banlm->GetList(this);
406         if (bans)
407         {
408                 for (ListModeBase::ModeList::const_iterator it = bans->begin(); it != bans->end(); ++it)
409                 {
410                         if (CheckBan(user, it->mask))
411                                 return MOD_RES_DENY;
412                 }
413         }
414         return MOD_RES_PASSTHRU;
415 }
416
417 /* Channel::PartUser
418  * Remove a channel from a users record, remove the reference to the Membership object
419  * from the channel and destroy it.
420  */
421 void Channel::PartUser(User *user, std::string &reason)
422 {
423         MemberMap::iterator membiter = userlist.find(user);
424
425         if (membiter != userlist.end())
426         {
427                 Membership* memb = membiter->second;
428                 CUList except_list;
429                 FOREACH_MOD(OnUserPart, (memb, reason, except_list));
430
431                 WriteAllExcept(user, false, 0, except_list, "PART %s%s%s", this->name.c_str(), reason.empty() ? "" : " :", reason.c_str());
432
433                 // Remove this channel from the user's chanlist
434                 user->chans.erase(memb);
435                 // Remove the Membership from this channel's userlist and destroy it
436                 this->DelUser(membiter);
437         }
438 }
439
440 void Channel::KickUser(User* src, const MemberMap::iterator& victimiter, const std::string& reason)
441 {
442         Membership* memb = victimiter->second;
443         CUList except_list;
444         FOREACH_MOD(OnUserKick, (src, memb, reason, except_list));
445
446         User* victim = memb->user;
447         WriteAllExcept(src, false, 0, except_list, "KICK %s %s :%s", name.c_str(), victim->nick.c_str(), reason.c_str());
448
449         victim->chans.erase(memb);
450         this->DelUser(victimiter);
451 }
452
453 void Channel::WriteChannel(User* user, const char* text, ...)
454 {
455         std::string textbuffer;
456         VAFORMAT(textbuffer, text, text);
457         this->WriteChannel(user, textbuffer);
458 }
459
460 void Channel::WriteChannel(User* user, const std::string &text)
461 {
462         const std::string message = ":" + user->GetFullHost() + " " + text;
463
464         for (MemberMap::iterator i = userlist.begin(); i != userlist.end(); i++)
465         {
466                 if (IS_LOCAL(i->first))
467                         i->first->Write(message);
468         }
469 }
470
471 void Channel::WriteChannelWithServ(const std::string& ServName, const char* text, ...)
472 {
473         std::string textbuffer;
474         VAFORMAT(textbuffer, text, text);
475         this->WriteChannelWithServ(ServName, textbuffer);
476 }
477
478 void Channel::WriteChannelWithServ(const std::string& ServName, const std::string &text)
479 {
480         const std::string message = ":" + (ServName.empty() ? ServerInstance->Config->ServerName : ServName) + " " + text;
481
482         for (MemberMap::iterator i = userlist.begin(); i != userlist.end(); i++)
483         {
484                 if (IS_LOCAL(i->first))
485                         i->first->Write(message);
486         }
487 }
488
489 /* write formatted text from a source user to all users on a channel except
490  * for the sender (for privmsg etc) */
491 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const char* text, ...)
492 {
493         std::string textbuffer;
494         VAFORMAT(textbuffer, text, text);
495         this->WriteAllExceptSender(user, serversource, status, textbuffer);
496 }
497
498 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const char* text, ...)
499 {
500         std::string textbuffer;
501         VAFORMAT(textbuffer, text, text);
502         textbuffer = ":" + (serversource ? ServerInstance->Config->ServerName : user->GetFullHost()) + " " + textbuffer;
503         this->RawWriteAllExcept(user, serversource, status, except_list, textbuffer);
504 }
505
506 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string &text)
507 {
508         const std::string message = ":" + (serversource ? ServerInstance->Config->ServerName : user->GetFullHost()) + " " + text;
509         this->RawWriteAllExcept(user, serversource, status, except_list, message);
510 }
511
512 void Channel::RawWriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string &out)
513 {
514         unsigned int minrank = 0;
515         if (status)
516         {
517                 PrefixMode* mh = ServerInstance->Modes->FindPrefix(status);
518                 if (mh)
519                         minrank = mh->GetPrefixRank();
520         }
521         for (MemberMap::iterator i = userlist.begin(); i != userlist.end(); i++)
522         {
523                 if (IS_LOCAL(i->first) && (except_list.find(i->first) == except_list.end()))
524                 {
525                         /* User doesn't have the status we're after */
526                         if (minrank && i->second->getRank() < minrank)
527                                 continue;
528
529                         i->first->Write(out);
530                 }
531         }
532 }
533
534 void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const std::string& text)
535 {
536         CUList except_list;
537         except_list.insert(user);
538         this->WriteAllExcept(user, serversource, status, except_list, std::string(text));
539 }
540
541 const char* Channel::ChanModes(bool showkey)
542 {
543         static std::string scratch;
544         std::string sparam;
545
546         scratch.clear();
547
548         /* This was still iterating up to 190, Channel::modes is only 64 elements -- Om */
549         for(int n = 0; n < 64; n++)
550         {
551                 ModeHandler* mh = ServerInstance->Modes->FindMode(n + 65, MODETYPE_CHANNEL);
552                 if (mh && IsModeSet(mh))
553                 {
554                         scratch.push_back(n + 65);
555
556                         ParamModeBase* pm = mh->IsParameterMode();
557                         if (!pm)
558                                 continue;
559
560                         if (n == 'k' - 65 && !showkey)
561                         {
562                                 sparam += " <key>";
563                         }
564                         else
565                         {
566                                 sparam += ' ';
567                                 pm->GetParameter(this, sparam);
568                         }
569                 }
570         }
571
572         scratch += sparam;
573         return scratch.c_str();
574 }
575
576 /* returns the status character for a given user on a channel, e.g. @ for op,
577  * % for halfop etc. If the user has several modes set, the highest mode
578  * the user has must be returned.
579  */
580 char Membership::GetPrefixChar() const
581 {
582         char pf = 0;
583         unsigned int bestrank = 0;
584
585         for (std::string::const_iterator i = modes.begin(); i != modes.end(); ++i)
586         {
587                 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(*i);
588                 if (mh && mh->GetPrefixRank() > bestrank && mh->GetPrefix())
589                 {
590                         bestrank = mh->GetPrefixRank();
591                         pf = mh->GetPrefix();
592                 }
593         }
594         return pf;
595 }
596
597 unsigned int Membership::getRank()
598 {
599         char mchar = modes.c_str()[0];
600         unsigned int rv = 0;
601         if (mchar)
602         {
603                 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(mchar);
604                 if (mh)
605                         rv = mh->GetPrefixRank();
606         }
607         return rv;
608 }
609
610 const char* Membership::GetAllPrefixChars() const
611 {
612         static char prefix[64];
613         int ctr = 0;
614
615         for (std::string::const_iterator i = modes.begin(); i != modes.end(); ++i)
616         {
617                 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(*i);
618                 if (mh && mh->GetPrefix())
619                         prefix[ctr++] = mh->GetPrefix();
620         }
621         prefix[ctr] = 0;
622
623         return prefix;
624 }
625
626 unsigned int Channel::GetPrefixValue(User* user)
627 {
628         MemberMap::iterator m = userlist.find(user);
629         if (m == userlist.end())
630                 return 0;
631         return m->second->getRank();
632 }
633
634 bool Membership::SetPrefix(PrefixMode* delta_mh, bool adding)
635 {
636         char prefix = delta_mh->GetModeChar();
637         for (unsigned int i = 0; i < modes.length(); i++)
638         {
639                 char mchar = modes[i];
640                 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(mchar);
641                 if (mh && mh->GetPrefixRank() <= delta_mh->GetPrefixRank())
642                 {
643                         modes = modes.substr(0,i) +
644                                 (adding ? std::string(1, prefix) : "") +
645                                 modes.substr(mchar == prefix ? i+1 : i);
646                         return adding != (mchar == prefix);
647                 }
648         }
649         if (adding)
650                 modes.push_back(prefix);
651         return adding;
652 }
653
654 void Invitation::Create(Channel* c, LocalUser* u, time_t timeout)
655 {
656         if ((timeout != 0) && (ServerInstance->Time() >= timeout))
657                 // Expired, don't bother
658                 return;
659
660         ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Create chan=%s user=%s", c->name.c_str(), u->uuid.c_str());
661
662         Invitation* inv = Invitation::Find(c, u, false);
663         if (inv)
664         {
665                  if ((inv->expiry == 0) || (inv->expiry > timeout))
666                         return;
667                 inv->expiry = timeout;
668                 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Create changed expiry in existing invitation %p", (void*) inv);
669         }
670         else
671         {
672                 inv = new Invitation(c, u, timeout);
673                 c->invites.push_front(inv);
674                 u->invites.push_front(inv);
675                 ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Create created new invitation %p", (void*) inv);
676         }
677 }
678
679 Invitation* Invitation::Find(Channel* c, LocalUser* u, bool check_expired)
680 {
681         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);
682
683         Invitation* result = NULL;
684         for (InviteList::iterator i = u->invites.begin(); i != u->invites.end(); )
685         {
686                 Invitation* inv = *i;
687                 ++i;
688
689                 if ((check_expired) && (inv->expiry != 0) && (inv->expiry <= ServerInstance->Time()))
690                 {
691                         /* Expired invite, remove it. */
692                         std::string expiration = InspIRCd::TimeString(inv->expiry);
693                         ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Find ecountered expired entry: %p expired %s", (void*) inv, expiration.c_str());
694                         delete inv;
695                 }
696                 else
697                 {
698                         /* Is it what we're searching for? */
699                         if (inv->chan == c)
700                         {
701                                 result = inv;
702                                 break;
703                         }
704                 }
705         }
706
707         ServerInstance->Logs->Log("INVITATION", LOG_DEBUG, "Invitation::Find result=%p", (void*) result);
708         return result;
709 }
710
711 Invitation::~Invitation()
712 {
713         // Remove this entry from both lists
714         chan->invites.erase(this);
715         user->invites.erase(this);
716         ServerInstance->Logs->Log("INVITEBASE", LOG_DEBUG, "Invitation::~ %p", (void*) this);
717 }