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
|
// Copyright 2005-2019 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 "SocketRPC.h"
#include "Channel.h"
#include "ClientUser.h"
#include "MainWindow.h"
#include "ServerHandler.h"
// We define a global macro called 'g'. This can lead to issues when included code uses 'g' as a type or parameter name (like protobuf 3.7 does). As such, for now, we have to make this our last include.
#include "Global.h"
SocketRPCClient::SocketRPCClient(QLocalSocket *s, QObject *p) : QObject(p), qlsSocket(s), qbBuffer(NULL) {
qlsSocket->setParent(this);
connect(qlsSocket, SIGNAL(disconnected()), this, SLOT(disconnected()));
connect(qlsSocket, SIGNAL(error(QLocalSocket::LocalSocketError)), this, SLOT(error(QLocalSocket::LocalSocketError)));
connect(qlsSocket, SIGNAL(readyRead()), this, SLOT(readyRead()));
qxsrReader.setDevice(qlsSocket);
qxswWriter.setAutoFormatting(true);
qbBuffer = new QBuffer(&qbaOutput, this);
qbBuffer->open(QIODevice::WriteOnly);
qxswWriter.setDevice(qbBuffer);
}
void SocketRPCClient::disconnected() {
deleteLater();
}
void SocketRPCClient::error(QLocalSocket::LocalSocketError) {
}
void SocketRPCClient::readyRead() {
forever {
switch (qxsrReader.readNext()) {
case QXmlStreamReader::Invalid: {
if (qxsrReader.error() != QXmlStreamReader::PrematureEndOfDocumentError) {
qWarning() << "Malformed" << qxsrReader.error();
qlsSocket->abort();
}
return;
}
break;
case QXmlStreamReader::EndDocument: {
qxswWriter.writeCurrentToken(qxsrReader);
processXml();
qxsrReader.clear();
qxsrReader.setDevice(qlsSocket);
qxswWriter.setDevice(NULL);
delete qbBuffer;
qbaOutput = QByteArray();
qbBuffer = new QBuffer(&qbaOutput, this);
qbBuffer->open(QIODevice::WriteOnly);
qxswWriter.setDevice(qbBuffer);
}
break;
default:
qxswWriter.writeCurrentToken(qxsrReader);
break;
}
}
}
void SocketRPCClient::processXml() {
QDomDocument qdd;
qdd.setContent(qbaOutput, false);
QDomElement request = qdd.firstChildElement();
if (! request.isNull()) {
bool ack = false;
QMap<QString, QVariant> qmRequest;
QMap<QString, QVariant> qmReply;
QMap<QString, QVariant>::const_iterator iter;
QDomNamedNodeMap attributes = request.attributes();
for (int i=0;i<attributes.count();++i) {
QDomAttr attr = attributes.item(i).toAttr();
qmRequest.insert(attr.name(), attr.value());
}
QDomNodeList childNodes = request.childNodes();
for (int i=0;i<childNodes.count();++i) {
QDomElement child = childNodes.item(i).toElement();
if (! child.isNull())
qmRequest.insert(child.nodeName(), child.text());
}
iter = qmRequest.find(QLatin1String("reqid"));
if (iter != qmRequest.constEnd())
qmReply.insert(iter.key(), iter.value());
if (request.nodeName() == QLatin1String("focus")) {
g.mw->show();
g.mw->raise();
g.mw->activateWindow();
ack = true;
} else if (request.nodeName() == QLatin1String("self")) {
iter = qmRequest.find(QLatin1String("mute"));
if (iter != qmRequest.constEnd()) {
bool set = iter.value().toBool();
if (set != g.s.bMute) {
g.mw->qaAudioMute->setChecked(! set);
g.mw->qaAudioMute->trigger();
}
}
iter = qmRequest.find(QLatin1String("unmute"));
if (iter != qmRequest.constEnd()) {
bool set = iter.value().toBool();
if (set == g.s.bMute) {
g.mw->qaAudioMute->setChecked(set);
g.mw->qaAudioMute->trigger();
}
}
iter = qmRequest.find(QLatin1String("togglemute"));
if (iter != qmRequest.constEnd()) {
bool set = iter.value().toBool();
if (set == g.s.bMute) {
g.mw->qaAudioMute->setChecked(set);
g.mw->qaAudioMute->trigger();
} else {
g.mw->qaAudioMute->setChecked(! set);
g.mw->qaAudioMute->trigger();
}
}
iter = qmRequest.find(QLatin1String("deaf"));
if (iter != qmRequest.constEnd()) {
bool set = iter.value().toBool();
if (set != g.s.bDeaf) {
g.mw->qaAudioDeaf->setChecked(! set);
g.mw->qaAudioDeaf->trigger();
}
}
iter = qmRequest.find(QLatin1String("undeaf"));
if (iter != qmRequest.constEnd()) {
bool set = iter.value().toBool();
if (set == g.s.bDeaf) {
g.mw->qaAudioDeaf->setChecked(set);
g.mw->qaAudioDeaf->trigger();
}
}
iter = qmRequest.find(QLatin1String("toggledeaf"));
if (iter != qmRequest.constEnd()) {
bool set = iter.value().toBool();
if (set == g.s.bDeaf) {
g.mw->qaAudioDeaf->setChecked(set);
g.mw->qaAudioDeaf->trigger();
} else {
g.mw->qaAudioDeaf->setChecked(! set);
g.mw->qaAudioDeaf->trigger();
}
}
ack = true;
} else if (request.nodeName() == QLatin1String("url")) {
if (g.sh && g.sh->isRunning() && g.uiSession) {
QString host, user, pw;
unsigned short port;
QUrl u;
g.sh->getConnectionInfo(host, port, user, pw);
u.setScheme(QLatin1String("mumble"));
u.setHost(host);
u.setPort(port);
u.setUserName(user);
#if QT_VERSION >= 0x050000
QUrlQuery query;
query.addQueryItem(QLatin1String("version"), QLatin1String("1.2.0"));
u.setQuery(query);
#else
u.addQueryItem(QLatin1String("version"), QLatin1String("1.2.0"));
#endif
QStringList path;
Channel *c = ClientUser::get(g.uiSession)->cChannel;
while (c->cParent) {
path.prepend(c->qsName);
c = c->cParent;
}
u.setPath(path.join(QLatin1String("/")));
qmReply.insert(QLatin1String("href"), u);
}
iter = qmRequest.find(QLatin1String("href"));
if (iter != qmRequest.constEnd()) {
QUrl u = iter.value().toUrl();
if (u.isValid() && u.scheme() == QLatin1String("mumble")) {
OpenURLEvent *oue = new OpenURLEvent(u);
qApp->postEvent(g.mw, oue);
ack = true;
}
} else {
ack = true;
}
}
QDomDocument replydoc;
QDomElement reply = replydoc.createElement(QLatin1String("reply"));
qmReply.insert(QLatin1String("succeeded"), ack);
for (iter = qmReply.constBegin(); iter != qmReply.constEnd(); ++iter) {
QDomElement elem = replydoc.createElement(iter.key());
QDomText text = replydoc.createTextNode(iter.value().toString());
elem.appendChild(text);
reply.appendChild(elem);
}
replydoc.appendChild(reply);
qlsSocket->write(replydoc.toByteArray());
}
}
SocketRPC::SocketRPC(const QString &basename, QObject *p) : QObject(p) {
qlsServer = new QLocalServer(this);
QString pipepath;
#ifdef Q_OS_WIN
pipepath = basename;
#else
{
QString xdgRuntimePath = QProcessEnvironment::systemEnvironment().value(QLatin1String("XDG_RUNTIME_DIR"));
QDir xdgRuntimeDir = QDir(xdgRuntimePath);
if (! xdgRuntimePath.isNull() && xdgRuntimeDir.exists()) {
pipepath = xdgRuntimeDir.absoluteFilePath(basename + QLatin1String("Socket"));
} else {
pipepath = QDir::home().absoluteFilePath(QLatin1String(".") + basename + QLatin1String("Socket"));
}
}
{
QFile f(pipepath);
if (f.exists()) {
qWarning() << "SocketRPC: Removing old socket on" << pipepath;
f.remove();
}
}
#endif
if (! qlsServer->listen(pipepath)) {
qWarning() << "SocketRPC: Listen failed";
delete qlsServer;
qlsServer = NULL;
} else {
connect(qlsServer, SIGNAL(newConnection()), this, SLOT(newConnection()));
}
}
void SocketRPC::newConnection() {
while (true) {
QLocalSocket *qls = qlsServer->nextPendingConnection();
if (! qls)
break;
new SocketRPCClient(qls, this);
}
}
bool SocketRPC::send(const QString &basename, const QString &request, const QMap<QString, QVariant> ¶m) {
QString pipepath;
#ifdef Q_OS_WIN
pipepath = basename;
#else
{
QString xdgRuntimePath = QProcessEnvironment::systemEnvironment().value(QLatin1String("XDG_RUNTIME_DIR"));
QDir xdgRuntimeDir = QDir(xdgRuntimePath);
if (! xdgRuntimePath.isNull() && xdgRuntimeDir.exists()) {
pipepath = xdgRuntimeDir.absoluteFilePath(basename + QLatin1String("Socket"));
} else {
pipepath = QDir::home().absoluteFilePath(QLatin1String(".") + basename + QLatin1String("Socket"));
}
}
#endif
QLocalSocket qls;
qls.connectToServer(pipepath);
if (! qls.waitForConnected(1000)) {
return false;
}
QDomDocument requestdoc;
QDomElement req = requestdoc.createElement(request);
for (QMap<QString, QVariant>::const_iterator iter = param.constBegin(); iter != param.constEnd(); ++iter) {
QDomElement elem = requestdoc.createElement(iter.key());
QDomText text = requestdoc.createTextNode(iter.value().toString());
elem.appendChild(text);
req.appendChild(elem);
}
requestdoc.appendChild(req);
qls.write(requestdoc.toByteArray());
qls.flush();
if (! qls.waitForReadyRead(2000)) {
return false;
}
QByteArray qba = qls.readAll();
QDomDocument replydoc;
replydoc.setContent(qba);
QDomElement succ = replydoc.firstChildElement(QLatin1String("reply"));
succ = succ.firstChildElement(QLatin1String("succeeded"));
if (succ.isNull())
return false;
return QVariant(succ.text()).toBool();
}
|