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
|
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the Qt Messaging Framework.
**
** $QT_BEGIN_LICENSE:LGPL21$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "messageserver.h"
#include "servicehandler.h"
#include <qmailfolder.h>
#include <qmailmessage.h>
#include <qmailstore.h>
#include <QDataStream>
#include <QTextStream>
#include <QTimer>
#include <QCoreApplication>
#include <QDir>
#include <QFile>
#include <QSettings>
#include <qmaillog.h>
#include <qmailipc.h>
#include <qmailmessageserverplugin.h>
#define LC_MESSAGING "org.qt.messageserver"
#define LC_MAILSTORE "org.qt.mailstore"
#include "qmailservice_adaptor.h"
extern "C" {
#ifndef Q_OS_WIN
#include <sys/socket.h>
#endif
#include <signal.h>
}
#if defined(Q_OS_UNIX)
#include <unistd.h>
int MessageServer::sighupFd[2];
int MessageServer::sigtermFd[2];
int MessageServer::sigintFd[2];
#endif
MessageServer::MessageServer(QObject *parent)
: QObject(parent),
handler(nullptr)
{
readLogSettings();
}
MessageServer::~MessageServer()
{
// Unregister from D-Bus.
QDBusConnection dbus = QDBusConnection::sessionBus();
dbus.unregisterObject("/messageserver");
if (!dbus.unregisterService("org.qt.messageserver")) {
qCWarning(lcMessaging) << "Failed to unregister messageserver from D-Bus";
} else {
qCDebug(lcMessaging) << "Unregistered messageserver from D-Bus";
}
qDeleteAll(m_plugins);
}
bool MessageServer::init()
{
qCDebug(lcMessaging) << "MessageServer init begin";
#if defined(Q_OS_UNIX)
// Unix signal handlers. We use the trick described here: http://doc.qt.io/qt-5/unix-signals.html
// Looks shocking but the trick has certain reasons stated in Steven's book: http://cr.yp.to/docs/selfpipe.html
// Use a socket and notifier because signal handlers can't call Qt code
if (::socketpair(AF_UNIX, SOCK_STREAM, 0, sighupFd))
qFatal("Couldn't create HUP socketpair");
if (::socketpair(AF_UNIX, SOCK_STREAM, 0, sigtermFd))
qFatal("Couldn't create TERM socketpair");
if (::socketpair(AF_UNIX, SOCK_STREAM, 0, sigintFd))
qFatal("Couldn't create TERM socketpair");
snHup = new QSocketNotifier(sighupFd[1], QSocketNotifier::Read, this);
connect(snHup, SIGNAL(activated(int)), this, SLOT(handleSigHup()));
snTerm = new QSocketNotifier(sigtermFd[1], QSocketNotifier::Read, this);
connect(snTerm, SIGNAL(activated(int)), this, SLOT(handleSigTerm()));
snInt = new QSocketNotifier(sigintFd[1], QSocketNotifier::Read, this);
connect(snInt, SIGNAL(activated(int)), this, SLOT(handleSigInt()));
struct sigaction action;
action.sa_handler = MessageServer::hupSignalHandler;
sigemptyset(&action.sa_mask);
action.sa_flags = 0;
action.sa_flags |= SA_RESTART;
if (sigaction(SIGHUP, &action, 0) > 0)
qFatal("Couldn't register HUP handler");
action.sa_handler = MessageServer::termSignalHandler;
if (sigaction(SIGTERM, &action, 0) > 0)
qFatal("Couldn't register TERM handler");
action.sa_handler = MessageServer::intSignalHandler;
if (sigaction(SIGINT, &action, 0) > 0)
qFatal("Couldn't register INT handler");
#endif // defined(Q_OS_UNIX)
QMailStore *store = QMailStore::instance();
if (store->initializationState() != QMailStore::Initialized) {
qCWarning(lcMessaging) << "Messaging DB Invalid: Messaging cannot operate due to database incompatibilty!";
// Do not close, however, or QPE will start another instance.
return false;
}
// Register our object on the session bus and expose interface to others.
handler = new ServiceHandler(this);
new MessageserverAdaptor(handler);
QDBusConnection dbus = QDBusConnection::sessionBus();
if (!dbus.registerObject("/messageserver", handler)
|| !dbus.registerService("org.qt.messageserver")) {
qCWarning(lcMessaging) << "Failed to register to D-Bus, aborting start";
return false;
}
qCDebug(lcMessaging) << "Registered messageserver to D-Bus";
connect(handler, &ServiceHandler::transmissionReady,
this, &MessageServer::transmissionCompleted);
connect(handler, &ServiceHandler::retrievalReady,
this, &MessageServer::retrievalCompleted);
// clean up any temporary messages that were not cleaned up by clients
QTimer::singleShot(0, this, SLOT(cleanupTemporaryMessages()));
emit handler->actionsListed(QMailActionDataList());
qCDebug(lcMessaging) << "Initiating messageserver plugins.";
for (const QString &plugin : QMailMessageServerPluginFactory::keys()) {
QMailMessageServerService *service = QMailMessageServerPluginFactory::createService(plugin);
qCDebug(lcMessaging) << "service from" << plugin << "created.";
m_plugins.append(service);
}
return true;
}
void MessageServer::retrievalCompleted(quint64 action)
{
// Ensure the client receives any resulting events before a notification
QMailStore::instance()->flushIpcNotifications();
emit handler->retrievalCompleted(action);
}
void MessageServer::transmissionCompleted(quint64 action)
{
// Ensure the client receives any resulting events before the completion notification
QMailStore::instance()->flushIpcNotifications();
emit handler->transmissionCompleted(action);
}
void MessageServer::cleanupTemporaryMessages()
{
QMailStore::instance()->removeMessages(QMailMessageKey::status(QMailMessage::Temporary), QMailStore::NoRemovalRecord);
}
#if defined(Q_OS_UNIX)
void MessageServer::hupSignalHandler(int)
{
// Can't call Qt code. Write to the socket and the notifier will fire from the Qt event loop
char a = 1;
::write(sighupFd[0], &a, sizeof(a));
}
void MessageServer::handleSigHup()
{
snHup->setEnabled(false);
char tmp;
::read(sighupFd[1], &tmp, sizeof(tmp));
readLogSettings();
snHup->setEnabled(true);
}
void MessageServer::termSignalHandler(int)
{
char a = 1;
::write(sigtermFd[0], &a, sizeof(a));
}
void MessageServer::handleSigTerm()
{
snTerm->setEnabled(false);
char tmp;
::read(sigtermFd[1], &tmp, sizeof(tmp));
qCDebug(lcMessaging) << "Received SIGTERM, shutting down.";
QCoreApplication::exit();
snTerm->setEnabled(true);
}
void MessageServer::intSignalHandler(int)
{
char a = 1;
::write(sigintFd[0], &a, sizeof(a));
}
void MessageServer::handleSigInt()
{
snInt->setEnabled(false);
char tmp;
::read(sigintFd[1], &tmp, sizeof(tmp));
qCDebug(lcMessaging) << "Received SIGINT, shutting down.";
QCoreApplication::exit();
snInt->setEnabled(true);
}
static QtMessageHandler originalLogHandler = nullptr;
static QFile logFile;
static void logToFile(QtMsgType type,
const QMessageLogContext &context, const QString &msg)
{
if (logFile.isOpen()) {
static QTextStream out(&logFile);
out << QDateTime::currentDateTimeUtc().toString(Qt::ISODate);
switch (type) {
case QtDebugMsg:
out << " [D]";
break;
case QtInfoMsg:
out << " [I]";
break;
case QtWarningMsg:
out << " [W]";
break;
case QtCriticalMsg:
out << " [C]";
break;
case QtFatalMsg:
out << " [F]";
break;
default:
break;
}
if (context.category)
out << " " << context.category << ":";
out << " " << msg << '\n';
out.flush();
}
if (originalLogHandler)
originalLogHandler(type, context, msg);
}
void MessageServer::readLogSettings() const
{
QSettings settings(QLatin1String("QtProject"), QLatin1String("Messageserver"));
const QStringList groups = settings.childGroups();
if (!groups.contains(QLatin1String("LogCategories"))) {
settings.beginGroup(QLatin1String("LogCategories"));
settings.setValue(QLatin1String("Messaging"), 0);
settings.setValue(QLatin1String("MailStore"), 0);
settings.setValue(QLatin1String("IMAP"), 0);
settings.setValue(QLatin1String("SMTP"), 0);
settings.setValue(QLatin1String("POP"), 0);
settings.endGroup();
}
// Deprecated logger
if (settings.value(QLatin1String("Syslog/Enabled")).toBool()) {
qCWarning(lcMessaging) << "Syslog is a deprecated logger, remove it from settings.";
}
settings.beginGroup(QLatin1String("FileLog"));
if (settings.value(QLatin1String("Enabled")).toBool()) {
QString fileName = settings.value(QLatin1String("Path")).toString();
if (fileName.startsWith(QStringLiteral("~/"))) {
fileName.replace(0, 1, QDir::homePath());
}
if (fileName != logFile.fileName()) {
logFile.close();
logFile.setFileName(fileName);
}
if (!logFile.isOpen()) {
if (!logFile.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Unbuffered)) {
qWarning() << "Unable to open log file" << fileName;
}
}
} else {
logFile.close();
}
settings.endGroup();
if (!originalLogHandler) {
originalLogHandler = qInstallMessageHandler(logToFile);
}
// Build rules from the setting file
QStringList rules;
settings.beginGroup(QLatin1String("LogCategories"));
for (const QString& key : settings.allKeys()) {
if (key == QLatin1String("Messaging")) {
rules.append(QLatin1String(settings.value(key).toBool()
? LC_MESSAGING ".debug=true"
: LC_MESSAGING ".debug=false"));
} else if (key == QLatin1String("MailStore")) {
rules.append(QLatin1String(settings.value(key).toBool()
? LC_MAILSTORE ".debug=true"
: LC_MAILSTORE ".debug=false"));
} else if (key == QLatin1String("IMAP")) {
rules.append(QLatin1String(settings.value(key).toBool()
? LC_MESSAGING ".imap.debug=true"
: LC_MESSAGING ".imap.debug=false"));
} else if (key == QLatin1String("POP")) {
rules.append(QLatin1String(settings.value(key).toBool()
? LC_MESSAGING ".pop.debug=true"
: LC_MESSAGING ".pop.debug=false"));
} else if (key == QLatin1String("SMTP")) {
rules.append(QLatin1String(settings.value(key).toBool()
? LC_MESSAGING ".smtp.debug=true"
: LC_MESSAGING ".smtp.debug=false"));
}
};
settings.endGroup();
if (!rules.isEmpty()) {
QLoggingCategory::setFilterRules(rules.join('\n'));
}
}
#endif // defined(Q_OS_UNIX)
|