summaryrefslogtreecommitdiff
path: root/src/coremods/core_channel/invite.cpp
blob: 7ac662edc9331c31560a6ce3bf00397987f7b71b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
 * InspIRCd -- Internet Relay Chat Daemon
 *
 *   Copyright (C) 2012, 2015 Attila Molnar <attilamolnar@hush.com>
 *
 * This file is part of InspIRCd.  InspIRCd is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, version 2.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "inspircd.h"

#include "invite.h"

class InviteExpireTimer : public Timer
{
	Invite::Invite* const inv;

	bool Tick(time_t currtime) CXX11_OVERRIDE;

 public:
	InviteExpireTimer(Invite::Invite* invite, time_t timeout);
};

static Invite::APIImpl* apiimpl;

void RemoveInvite(Invite::Invite* inv, bool remove_user, bool remove_chan)
{
	apiimpl->Destruct(inv, remove_user, remove_chan);
}

void UnserializeInvite(LocalUser* user, const std::string& str)
{
	apiimpl->Unserialize(user, str);
}

Invite::APIBase::APIBase(Module* parent)
	: DataProvider(parent, "core_channel_invite")
{
}

Invite::APIImpl::APIImpl(Module* parent)
	: APIBase(parent)
	, userext(parent, "invite_user")
	, chanext(parent, "invite_chan")
{
	apiimpl = this;
}

void Invite::APIImpl::Destruct(Invite* inv, bool remove_user, bool remove_chan)
{
	Store<LocalUser>* ustore = userext.get(inv->user);
	if (ustore)
	{
		ustore->invites.erase(inv);
		if ((remove_user) && (ustore->invites.empty()))
			userext.unset(inv->user);
	}

	Store<Channel>* cstore = chanext.get(inv->chan);
	if (cstore)
	{
		cstore->invites.erase(inv);
		if ((remove_chan) && (cstore->invites.empty()))
			chanext.unset(inv->chan);
	}

	delete inv;
}

bool Invite::APIImpl::Remove(LocalUser* user, Channel* chan)
{
	Invite* inv = Find(user, chan);
	if (inv)
	{
		Destruct(inv);
		return true;
	}
	return false;
}

void Invite::APIImpl::Create(LocalUser* user, Channel* chan, time_t timeout)
{
	if ((timeout != 0) && (ServerInstance->Time() >= timeout))
		// Expired, don't bother
		return;

	ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Invite::APIImpl::Create(): user=%s chan=%s timeout=%lu", user->uuid.c_str(), chan->name.c_str(), (unsigned long)timeout);

	Invite* inv = Find(user, chan);
	if (inv)
	{
		// We only ever extend invites, so nothing to do if the existing one is not timed
		if (!inv->IsTimed())
			return;

		ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Invite::APIImpl::Create(): changing expiration in %p", (void*) inv);
		if (timeout == 0)
		{
			// Convert timed invite to non-expiring
			delete inv->expiretimer;
			inv->expiretimer = NULL;
		}
		else if (inv->expiretimer->GetTrigger() >= ServerInstance->Time() + timeout)
		{
			// New expiration time is further than the current, extend the expiration
			inv->expiretimer->SetInterval(timeout - ServerInstance->Time());
		}
	}
	else
	{
		inv = new Invite(user, chan);
		if (timeout)
		{
			inv->expiretimer = new InviteExpireTimer(inv, timeout - ServerInstance->Time());
			ServerInstance->Timers.AddTimer(inv->expiretimer);
		}

		userext.get(user, true)->invites.push_front(inv);
		chanext.get(chan, true)->invites.push_front(inv);
		ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Invite::APIImpl::Create(): created new Invite %p", (void*) inv);
	}
}

Invite::Invite* Invite::APIImpl::Find(LocalUser* user, Channel* chan)
{
	const List* list = APIImpl::GetList(user);
	if (!list)
		return NULL;

	for (List::iterator i = list->begin(); i != list->end(); ++i)
	{
		Invite* inv = *i;
		if (inv->chan == chan)
			return inv;
	}

	return NULL;
}

const Invite::List* Invite::APIImpl::GetList(LocalUser* user)
{
	Store<LocalUser>* list = userext.get(user);
	if (list)
		return &list->invites;
	return NULL;
}

void Invite::APIImpl::Unserialize(LocalUser* user, const std::string& value)
{
	irc::spacesepstream ss(value);
	for (std::string channame, exptime; (ss.GetToken(channame) && ss.GetToken(exptime)); )
	{
		Channel* chan = ServerInstance->FindChan(channame);
		if (chan)
			Create(user, chan, ConvToInt(exptime));
	}
}

Invite::Invite::Invite(LocalUser* u, Channel* c)
	: user(u)
	, chan(c)
	, expiretimer(NULL)
{
}

Invite::Invite::~Invite()
{
	delete expiretimer;
	ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Invite::~ %p", (void*) this);
}

void Invite::Invite::Serialize(SerializeFormat format, bool show_chans, std::string& out)
{
	if (show_chans)
		out.append(this->chan->name);
	else
		out.append((format == FORMAT_USER) ? user->nick : user->uuid);
	out.push_back(' ');

	if (expiretimer)
		out.append(ConvToStr(expiretimer->GetTrigger()));
	else
		out.push_back('0');
	out.push_back(' ');
}

InviteExpireTimer::InviteExpireTimer(Invite::Invite* invite, time_t timeout)
	: Timer(timeout)
	, inv(invite)
{
}

bool InviteExpireTimer::Tick(time_t currtime)
{
	ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "InviteExpireTimer::Tick(): expired %p", (void*) inv);
	apiimpl->Destruct(inv);
	return false;
}