X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcore%2Fcore.cpp;h=29174b62b7ba973a18b8ffa1b4d4905fa49ebfcc;hp=003f6dafc1225f7b14097f6f4e05578fe74ea051;hb=ec7436e9be67c03be7f24b4e362db204c8d8315c;hpb=b040ef84cdc254a0b1f083db3151f2724e45d210 diff --git a/src/core/core.cpp b/src/core/core.cpp index 003f6daf..29174b62 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -37,9 +37,32 @@ #ifdef Q_OS_WIN32 # include #else +# include # include #endif /* Q_OS_WIN32 */ +// umask +#ifndef Q_OS_WIN32 +# include +# include +#endif /* Q_OS_WIN32 */ + +// ============================== +// Custom Events +// ============================== +const int Core::AddClientEventId = QEvent::registerEventType(); + +class AddClientEvent : public QEvent { +public: + AddClientEvent(QTcpSocket *socket, UserId uid) : QEvent(QEvent::Type(Core::AddClientEventId)), socket(socket), userId(uid) {} + QTcpSocket *socket; + UserId userId; +}; + + +// ============================== +// Core +// ============================== Core *Core::instanceptr = 0; Core *Core::instance() { @@ -57,6 +80,9 @@ void Core::destroy() { Core::Core() : _storage(0) { +#ifndef Q_OS_WIN32 + umask(S_IRWXG | S_IRWXO); +#endif /* Q_OS_WIN32 */ _startTime = QDateTime::currentDateTime().toUTC(); // for uptime :) Quassel::loadTranslation(QLocale::system()); @@ -493,10 +519,15 @@ void Core::processClientMessage(QTcpSocket *socket, const QVariantMap &msg) { socket->close(); return; } + reply["ProtocolVersion"] = Quassel::buildInfo().protocolVersion; reply["CoreVersion"] = Quassel::buildInfo().fancyVersionString; reply["CoreDate"] = Quassel::buildInfo().buildDate; - reply["ProtocolVersion"] = Quassel::buildInfo().protocolVersion; - // TODO: Make the core info configurable + reply["CoreStartTime"] = startTime(); // v10 clients don't necessarily parse this, see below + + // FIXME: newer clients no longer use the hardcoded CoreInfo (for now), since it gets the + // time zone wrong. With the next protocol bump (10 -> 11), we should remove this + // or make it properly configurable. + int uptime = startTime().secsTo(QDateTime::currentDateTime().toUTC()); int updays = uptime / 86400; uptime %= 86400; int uphours = uptime / 3600; uptime %= 3600; @@ -507,6 +538,8 @@ void Core::processClientMessage(QTcpSocket *socket, const QVariantMap &msg) { .arg(Quassel::buildInfo().buildDate) .arg(updays).arg(uphours,2,10,QChar('0')).arg(upmins,2,10,QChar('0')).arg(startTime().toString(Qt::TextDate)); + reply["CoreFeatures"] = (int)Quassel::features(); + #ifdef HAVE_SSL SslServer *sslServer = qobject_cast(&_server); QSslSocket *sslSocket = qobject_cast(socket); @@ -547,6 +580,7 @@ void Core::processClientMessage(QTcpSocket *socket, const QVariantMap &msg) { clientInfo[socket] = msg; // store for future reference reply["MsgType"] = "ClientInitAck"; SignalProxy::writeDataToDevice(socket, reply); + socket->flush(); // ensure that the write cache is flushed before we switch to ssl #ifdef HAVE_SSL // after we told the client that we are ssl capable we switch to ssl mode @@ -647,19 +681,48 @@ void Core::clientDisconnected() { } void Core::setupClientSession(QTcpSocket *socket, UserId uid) { - // Find or create session for validated user - SessionThread *sess; - if(sessions.contains(uid)) sess = sessions[uid]; - else sess = createSession(uid); - // Hand over socket, session then sends state itself + // From now on everything is handled by the client session disconnect(socket, 0, this, 0); + socket->flush(); blocksizes.remove(socket); clientInfo.remove(socket); - if(!sess) { - qWarning() << qPrintable(tr("Could not initialize session for client:")) << qPrintable(socket->peerAddress().toString()); + + // Find or create session for validated user + SessionThread *session; + if(sessions.contains(uid)) { + session = sessions[uid]; + } else { + session = createSession(uid); + if(!session) { + qWarning() << qPrintable(tr("Could not initialize session for client:")) << qPrintable(socket->peerAddress().toString()); + socket->close(); + return; + } + } + + // as we are currently handling an event triggered by incoming data on this socket + // it is unsafe to directly move the socket to the client thread. + QCoreApplication::postEvent(this, new AddClientEvent(socket, uid)); +} + +void Core::customEvent(QEvent *event) { + if(event->type() == AddClientEventId) { + AddClientEvent *addClientEvent = static_cast(event); + addClientHelper(addClientEvent->socket, addClientEvent->userId); + return; + } +} + +void Core::addClientHelper(QTcpSocket *socket, UserId uid) { + // Find or create session for validated user + if(!sessions.contains(uid)) { + qWarning() << qPrintable(tr("Could not find a session for client:")) << qPrintable(socket->peerAddress().toString()); socket->close(); + return; } - sess->addClient(socket); + + SessionThread *session = sessions[uid]; + session->addClient(socket); } void Core::setupInternalClientSession(SignalProxy *proxy) { @@ -668,7 +731,13 @@ void Core::setupInternalClientSession(SignalProxy *proxy) { setupCoreForInternalUsage(); } - UserId uid = _storage->internalUser(); + UserId uid; + if(_storage) { + uid = _storage->internalUser(); + } else { + qWarning() << "Core::setupInternalClientSession(): You're trying to run monolithic Quassel with an unusable Backend! Go fix it!"; + return; + } // Find or create session for validated user SessionThread *sess; @@ -806,7 +875,7 @@ void Core::createUser() { return; } - if(_storage->addUser(username, password).isValid()) { + if(_configured && _storage->addUser(username, password).isValid()) { out << "Added user " << username << " successfully!" << endl; } else { qWarning() << "Unable to add user:" << qPrintable(username); @@ -844,7 +913,7 @@ void Core::changeUserPass(const QString &username) { return; } - if(_storage->updateUser(userId, password)) { + if(_configured && _storage->updateUser(userId, password)) { out << "Password changed successfuly!" << endl; } else { qWarning() << "Failed to change password!";