X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcore%2Fcore.cpp;h=c894820c61d6bf525a4352aae8074fc70f179009;hp=18f022b13cfcb07cc0bf06dddf2ca569647b415e;hb=636812603e3308fece0ad677977c85b5cc9f18ee;hpb=e042ae69dbe4f42e9e4441f4b5832cfe5ca89067 diff --git a/src/core/core.cpp b/src/core/core.cpp index 18f022b1..c894820c 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()); @@ -236,7 +262,7 @@ QString Core::setupCoreForInternalUsage() { } setupData["AdminUser"] = "AdminUser"; setupData["AdminPasswd"] = QString::number(pass); - setupData["Backend"] = _storageBackends[_storageBackends.keys().first()]->displayName(); + setupData["Backend"] = QString("SQLite"); // mono client currently needs sqlite return setupCore(setupData); } @@ -246,8 +272,7 @@ QString Core::setupCore(QVariantMap setupData) { if(user.isEmpty() || password.isEmpty()) { return tr("Admin user or password not set."); } - _configured = initStorage(setupData, true); - if(!_configured) { + if(_configured || !(_configured = initStorage(setupData, true))) { return tr("Could not setup storage!"); } CoreSettings s; @@ -370,40 +395,39 @@ bool Core::startListening() { ); } else { switch(addr.protocol()) { - case QAbstractSocket::IPv4Protocol: - if(_server.listen(addr, port)) { + case QAbstractSocket::IPv6Protocol: + if(_v6server.listen(addr, port)) { quInfo() << qPrintable( - tr("Listening for GUI clients on IPv4 %1 port %2 using protocol version %3") + tr("Listening for GUI clients on IPv6 %1 port %2 using protocol version %3") .arg(addr.toString()) - .arg(_server.serverPort()) + .arg(_v6server.serverPort()) .arg(Quassel::buildInfo().protocolVersion) ); success = true; } else quWarning() << qPrintable( - tr("Could not open IPv4 interface %1:%2: %3") + tr("Could not open IPv6 interface %1:%2: %3") .arg(addr.toString()) .arg(port) - .arg(_server.errorString())); + .arg(_v6server.errorString())); break; - case QAbstractSocket::IPv6Protocol: - if(_v6server.listen(addr, port)) { + case QAbstractSocket::IPv4Protocol: + if(_server.listen(addr, port)) { quInfo() << qPrintable( - tr("Listening for GUI clients on IPv6 %1 port %2 using protocol version %3") + tr("Listening for GUI clients on IPv4 %1 port %2 using protocol version %3") .arg(addr.toString()) - .arg(_v6server.serverPort()) + .arg(_server.serverPort()) .arg(Quassel::buildInfo().protocolVersion) ); success = true; } else { - // if v4 succeeded on Any, the port will be already in use - don't display the error then - // FIXME: handle this more sanely, make sure we can listen to both v4 and v6 by default! - if(!success || _v6server.serverError() != QAbstractSocket::AddressInUseError) + // if v6 succeeded on Any, the port will be already in use - don't display the error then + if(!success || _server.serverError() != QAbstractSocket::AddressInUseError) quWarning() << qPrintable( - tr("Could not open IPv6 interface %1:%2: %3") + tr("Could not open IPv4 interface %1:%2: %3") .arg(addr.toString()) .arg(port) - .arg(_v6server.errorString())); + .arg(_server.errorString())); } break; default: @@ -493,10 +517,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 +536,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); @@ -648,20 +679,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(); // ensure that the write cache is flushed before we hand over the connection to another thread. + 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) { @@ -814,7 +873,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); @@ -852,8 +911,8 @@ void Core::changeUserPass(const QString &username) { return; } - if(_storage->updateUser(userId, password)) { - out << "Password changed successfuly!" << endl; + if(_configured && _storage->updateUser(userId, password)) { + out << "Password changed successfully!" << endl; } else { qWarning() << "Failed to change password!"; }