Welcome to mirror list, hosted at ThFree Co, Russian Federation.

RPC.cpp « murmur « src - github.com/mumble-voip/mumble.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3ef6b6b193abd00fd83864b27473f5e6b401de9f (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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
// Copyright 2008-2022 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.

#include <QtCore/QtGlobal>

#ifdef Q_OS_WIN
#	include "win.h"
#endif

#include "Channel.h"
#include "ChannelListenerManager.h"
#include "Group.h"
#include "Meta.h"
#include "QtUtils.h"
#include "Server.h"
#include "ServerDB.h"
#include "ServerUser.h"
#include "Version.h"

#ifdef Q_OS_WIN
#	include <winsock2.h>
#endif

void Server::setUserState(User *pUser, Channel *cChannel, bool mute, bool deaf, bool suppressed, bool prioritySpeaker,
						  const QString &name, const QString &comment) {
	bool changed = false;

	if (deaf)
		mute = true;
	if (!mute)
		deaf = false;

	MumbleProto::UserState mpus;
	mpus.set_session(pUser->uiSession);
	if (mute != pUser->bMute) {
		changed = true;
		mpus.set_mute(mute);
	}
	if (deaf != pUser->bDeaf) {
		changed = true;
		mpus.set_deaf(deaf);
	}
	if (suppressed != pUser->bSuppress) {
		changed = true;
		mpus.set_suppress(suppressed);
	}
	if (prioritySpeaker != pUser->bPrioritySpeaker) {
		changed = true;
		mpus.set_priority_speaker(prioritySpeaker);
	}
	if (comment != pUser->qsComment) {
		changed = true;
		mpus.set_comment(u8(comment));
		if (pUser->iId >= 0) {
			QMap< int, QString > info;
			info.insert(ServerDB::User_Comment, comment);
			setInfo(pUser->iId, info);
		}
	}
	if (name != pUser->qsName) {
		changed = true;
		mpus.set_name(u8(name));
	}

	{
		QWriteLocker wl(&qrwlVoiceThread);
		pUser->bDeaf     = deaf;
		pUser->bMute     = mute;
		pUser->bSuppress = suppressed;
	}

	pUser->bPrioritySpeaker = prioritySpeaker;
	pUser->qsName           = name;
	hashAssign(pUser->qsComment, pUser->qbaCommentHash, comment);

	if (cChannel != pUser->cChannel) {
		changed = true;
		mpus.set_channel_id(cChannel->iId);
		userEnterChannel(pUser, cChannel, mpus);
	}

	if (changed) {
		sendAll(mpus, Version::fromComponents(1, 2, 2), Version::CompareMode::LessThan);
		if (mpus.has_comment() && !pUser->qbaCommentHash.isEmpty()) {
			mpus.clear_comment();
			mpus.set_comment_hash(blob(pUser->qbaCommentHash));
		}
		sendAll(mpus, Version::fromComponents(1, 2, 2), Version::CompareMode::AtLeast);

		emit userStateChanged(pUser);
	}
}

bool Server::setChannelState(Channel *cChannel, Channel *cParent, const QString &qsName, const QSet< Channel * > &links,
							 const QString &desc, const int position) {
	bool changed = false;
	bool updated = false;

	MumbleProto::ChannelState mpcs;
	mpcs.set_channel_id(cChannel->iId);

	if (cChannel->qsName != qsName) {
		cChannel->qsName = qsName;
		mpcs.set_name(u8(qsName));
		updated = true;
		changed = true;
	}

	if ((cParent != cChannel) && (cParent != cChannel->cParent)) {
		Channel *p = cParent;
		while (p) {
			if (p == cChannel)
				return false;
			p = p->cParent;
		}

		if (!canNest(cParent, cChannel)) {
			return false;
		}

		{
			QWriteLocker wl(&qrwlVoiceThread);
			cChannel->cParent->removeChannel(cChannel);
			cParent->addChannel(cChannel);
		}

		mpcs.set_parent(cParent->iId);

		updated = true;
		changed = true;
	}

	const QSet< Channel * > &oldset = cChannel->qsPermLinks;

	if (links != oldset) {
		// Remove
		foreach (Channel *l, oldset) {
			if (!links.contains(l)) {
				removeLink(cChannel, l);
				mpcs.add_links_remove(l->iId);
			}
		}

		// Add
		foreach (Channel *l, links) {
			if (!oldset.contains(l)) {
				addLink(cChannel, l);
				mpcs.add_links_add(l->iId);
			}
		}

		changed = true;
	}

	if (position != cChannel->iPosition) {
		changed             = true;
		updated             = true;
		cChannel->iPosition = position;
		mpcs.set_position(position);
	}

	if (!desc.isNull() && desc != cChannel->qsDesc) {
		updated = true;
		changed = true;
		hashAssign(cChannel->qsDesc, cChannel->qbaDescHash, desc);
		mpcs.set_description(u8(desc));
	}

	if (updated)
		updateChannel(cChannel);
	if (changed) {
		sendAll(mpcs, Version::fromComponents(1, 2, 2), Version::CompareMode::LessThan);
		if (mpcs.has_description() && !cChannel->qbaDescHash.isEmpty()) {
			mpcs.clear_description();
			mpcs.set_description_hash(blob(cChannel->qbaDescHash));
		}
		sendAll(mpcs, Version::fromComponents(1, 2, 2), Version::CompareMode::AtLeast);
		emit channelStateChanged(cChannel);
	}

	return true;
}

void Server::sendTextMessage(Channel *cChannel, ServerUser *pUser, bool tree, const QString &text) {
	MumbleProto::TextMessage mptm;
	mptm.set_message(u8(text));

	if (pUser) {
		mptm.add_session(pUser->uiSession);
		sendMessage(pUser, mptm);
	} else {
		if (tree)
			mptm.add_tree_id(cChannel->iId);
		else
			mptm.add_channel_id(cChannel->iId);

		QSet< Channel * > chans;
		QQueue< Channel * > q;
		q << cChannel;
		chans.insert(cChannel);
		Channel *c;

		if (tree) {
			while (!q.isEmpty()) {
				c = q.dequeue();
				chans.insert(c);
				foreach (c, c->qlChannels)
					q.enqueue(c);
			}
		}
		foreach (c, chans) {
			foreach (User *p, c->qlUsers)
				sendMessage(static_cast< ServerUser * >(p), mptm);
		}
	}
}

/**
 * Sets the list of temporary groups the given userid is a member of. If no channel is given root will
 * be targeted.
 *
 * If userid is negative the absolute value is a session id. If it is positive it is a registration id.
 */
void Server::setTempGroups(int userid, int sessionId, Channel *cChannel, const QStringList &groups) {
	if (!cChannel)
		cChannel = qhChannels.value(0);

	{
		QWriteLocker wl(&qrwlVoiceThread);

		Group *g;
		foreach (g, cChannel->qhGroups) {
			g->qsTemporary.remove(userid);
			if (sessionId != 0)
				g->qsTemporary.remove(-sessionId);
		}

		QString gname;
		foreach (gname, groups) {
			g = cChannel->qhGroups.value(gname);
			if (!g) {
				g = new Group(cChannel, gname);
			}
			g->qsTemporary.insert(userid);
			if (sessionId != 0)
				g->qsTemporary.insert(-sessionId);
		}
	}

	User *p = qhUsers.value(userid);
	if (p)
		clearACLCache(p);
}

/**
 * Clears temporary group memberships for the given User. If no channel is given root will be targeted.
 * If recursion is activated all temporary memberships in related channels will also be cleared.
 */
void Server::clearTempGroups(User *user, Channel *cChannel, bool recurse) {
	QList< Channel * > qlChans;
	if (!cChannel)
		cChannel = qhChannels.value(0);

	qlChans.append(cChannel);

	{
		QWriteLocker wl(&qrwlVoiceThread);

		while (!qlChans.isEmpty()) {
			Channel *chan = qlChans.takeLast();
			Group *g;
			foreach (g, chan->qhGroups) {
				g->qsTemporary.remove(user->iId);
				g->qsTemporary.remove(-static_cast< int >(user->uiSession));
			}

			if (recurse)
				qlChans << chan->qlChannels;
		}
	}

	clearACLCache(user);
}

void Server::connectAuthenticator(QObject *obj) {
	connect(this, SIGNAL(registerUserSig(int &, const QMap< int, QString > &)), obj,
			SLOT(registerUserSlot(int &, const QMap< int, QString > &)));
	connect(this, SIGNAL(unregisterUserSig(int &, int)), obj, SLOT(unregisterUserSlot(int &, int)));
	connect(this, SIGNAL(getRegisteredUsersSig(const QString &, QMap< int, QString > &)), obj,
			SLOT(getRegisteredUsersSlot(const QString &, QMap< int, QString > &)));
	connect(this, SIGNAL(getRegistrationSig(int &, int, QMap< int, QString > &)), obj,
			SLOT(getRegistrationSlot(int &, int, QMap< int, QString > &)));
	connect(this,
			SIGNAL(authenticateSig(int &, QString &, int, const QList< QSslCertificate > &, const QString &, bool,
								   const QString &)),
			obj,
			SLOT(authenticateSlot(int &, QString &, int, const QList< QSslCertificate > &, const QString &, bool,
								  const QString &)));
	connect(this, SIGNAL(setInfoSig(int &, int, const QMap< int, QString > &)), obj,
			SLOT(setInfoSlot(int &, int, const QMap< int, QString > &)));
	connect(this, SIGNAL(setTextureSig(int &, int, const QByteArray &)), obj,
			SLOT(setTextureSlot(int &, int, const QByteArray &)));
	connect(this, SIGNAL(idToNameSig(QString &, int)), obj, SLOT(idToNameSlot(QString &, int)));
	connect(this, SIGNAL(nameToIdSig(int &, const QString &)), obj, SLOT(nameToIdSlot(int &, const QString &)));
	connect(this, SIGNAL(idToTextureSig(QByteArray &, int)), obj, SLOT(idToTextureSlot(QByteArray &, int)));
}

void Server::disconnectAuthenticator(QObject *obj) {
	disconnect(this, SIGNAL(registerUserSig(int &, const QMap< int, QString > &)), obj,
			   SLOT(registerUserSlot(int &, const QMap< int, QString > &)));
	disconnect(this, SIGNAL(unregisterUserSig(int &, int)), obj, SLOT(unregisterUserSlot(int &, int)));
	disconnect(this, SIGNAL(getRegisteredUsersSig(const QString &, QMap< int, QString > &)), obj,
			   SLOT(getRegisteredUsersSlot(const QString &, QMap< int, QString > &)));
	disconnect(this, SIGNAL(getRegistrationSig(int &, int, QMap< int, QString > &)), obj,
			   SLOT(getRegistrationSlot(int &, int, QMap< int, QString > &)));
	disconnect(this,
			   SIGNAL(authenticateSig(int &, QString &, int, const QList< QSslCertificate > &, const QString &, bool,
									  const QString &)),
			   obj,
			   SLOT(authenticateSlot(int &, QString &, int, const QList< QSslCertificate > &, const QString &, bool,
									 const QString &)));
	disconnect(this, SIGNAL(setInfoSig(int &, int, const QMap< int, QString > &)), obj,
			   SLOT(setInfoSlot(int &, int, const QMap< int, QString > &)));
	disconnect(this, SIGNAL(setTextureSig(int &, int, const QByteArray &)), obj,
			   SLOT(setTextureSlot(int &, int, const QByteArray &)));
	disconnect(this, SIGNAL(idToNameSig(QString &, int)), obj, SLOT(idToNameSlot(QString &, int)));
	disconnect(this, SIGNAL(nameToIdSig(int &, const QString &)), obj, SLOT(nameToIdSlot(int &, const QString &)));
	disconnect(this, SIGNAL(idToTextureSig(QByteArray &, int)), obj, SLOT(idToTextureSlot(QByteArray &, int)));
}

void Server::connectListener(QObject *obj) {
	connect(this, SIGNAL(userStateChanged(const User *)), obj, SLOT(userStateChanged(const User *)));
	connect(this, SIGNAL(userTextMessage(const User *, const TextMessage &)), obj,
			SLOT(userTextMessage(const User *, const TextMessage &)));
	connect(this, SIGNAL(userConnected(const User *)), obj, SLOT(userConnected(const User *)));
	connect(this, SIGNAL(userDisconnected(const User *)), obj, SLOT(userDisconnected(const User *)));
	connect(this, SIGNAL(channelStateChanged(const Channel *)), obj, SLOT(channelStateChanged(const Channel *)));
	connect(this, SIGNAL(channelCreated(const Channel *)), obj, SLOT(channelCreated(const Channel *)));
	connect(this, SIGNAL(channelRemoved(const Channel *)), obj, SLOT(channelRemoved(const Channel *)));
}

void Server::disconnectListener(QObject *obj) {
	disconnect(this, SIGNAL(userStateChanged(const User *)), obj, SLOT(userStateChanged(const User *)));
	disconnect(this, SIGNAL(userTextMessage(const User *, const TextMessage &)), obj,
			   SLOT(userTextMessage(const User *, const TextMessage &)));
	disconnect(this, SIGNAL(userConnected(const User *)), obj, SLOT(userConnected(const User *)));
	disconnect(this, SIGNAL(userDisconnected(const User *)), obj, SLOT(userDisconnected(const User *)));
	disconnect(this, SIGNAL(channelStateChanged(const Channel *)), obj, SLOT(channelStateChanged(const Channel *)));
	disconnect(this, SIGNAL(channelCreated(const Channel *)), obj, SLOT(channelCreated(const Channel *)));
	disconnect(this, SIGNAL(channelRemoved(const Channel *)), obj, SLOT(channelRemoved(const Channel *)));
}

void Server::startListeningToChannel(ServerUser *user, Channel *cChannel) {
	if (m_channelListenerManager.isListening(user->uiSession, cChannel->iId)) {
		// The user is already listening to this channel
		return;
	}

	addChannelListener(*user, *cChannel);

	MumbleProto::UserState mpus;
	mpus.set_session(user->uiSession);

	mpus.add_listening_channel_add(cChannel->iId);

	if (!broadcastListenerVolumeAdjustments) {
		sendExcept(user, mpus);
	}

	// Adding a listener might resurrect an old volume adjustment, so we need to
	// inform the (all) client(s) about this volume adjustment.
	float volumeAdjustment =
		m_channelListenerManager.getListenerVolumeAdjustment(user->uiSession, cChannel->iId).factor;
	MumbleProto::UserState::VolumeAdjustment *volume_adjustment = mpus.add_listening_volume_adjustment();
	volume_adjustment->set_listening_channel(cChannel->iId);
	volume_adjustment->set_volume_adjustment(volumeAdjustment);

	if (broadcastListenerVolumeAdjustments) {
		sendAll(mpus);
	} else {
		sendMessage(user, mpus);
	}
}

void Server::stopListeningToChannel(ServerUser *user, Channel *cChannel) {
	if (!m_channelListenerManager.isListening(user->uiSession, cChannel->iId)) {
		// The user is not listening to this channel
		return;
	}

	disableChannelListener(*user, *cChannel);

	MumbleProto::UserState mpus;
	mpus.set_session(user->uiSession);

	mpus.add_listening_channel_remove(cChannel->iId);

	sendAll(mpus);
}

void Server::setListenerVolumeAdjustment(ServerUser *user, const Channel *cChannel,
										 const VolumeAdjustment &volumeAdjustment) {
	setChannelListenerVolume(*user, *cChannel, volumeAdjustment.factor);

	// Inform clients about this change
	MumbleProto::UserState mpus;
	mpus.set_session(user->uiSession);

	MumbleProto::UserState::VolumeAdjustment *volume_adjustment = mpus.add_listening_volume_adjustment();
	volume_adjustment->set_listening_channel(cChannel->iId);
	volume_adjustment->set_volume_adjustment(volumeAdjustment.factor);

	if (broadcastListenerVolumeAdjustments) {
		sendAll(mpus);
	} else {
		sendMessage(user, mpus);
	}
}

void Server::sendWelcomeMessageTo(ServerUser *user) {
	MumbleProto::ServerConfig mpsc;
	mpsc.set_welcome_text(qsWelcomeText.toUtf8().data());

	sendMessage(user, mpsc);
}

void Meta::connectListener(QObject *obj) {
	connect(this, SIGNAL(started(Server *)), obj, SLOT(started(Server *)));
	connect(this, SIGNAL(stopped(Server *)), obj, SLOT(stopped(Server *)));
}

void Meta::getVersion(Version::component_t &major, Version::component_t &minor, Version::component_t &patch,
					  QString &string) {
	string = Version::getRelease();
	major = minor = patch = 0;
	Version::getComponents(major, minor, patch);
}