show ban duration in client

This commit is contained in:
Max-Wilhelm Bruker 2012-03-31 12:07:25 +02:00
parent 78d188c462
commit 13b992cf12
15 changed files with 159 additions and 91 deletions

2
.gitignore vendored
View file

@ -1,2 +1,4 @@
tags
build
*.qm
.directory

View file

@ -38,7 +38,6 @@ class AbstractClient : public QObject {
Q_OBJECT
signals:
void statusChanged(ClientStatus _status);
void serverError(Response::ResponseCode resp, QString reasonStr);
// Room events
void roomEventReceived(const RoomEvent &event);

View file

@ -24,6 +24,7 @@ RemoteClient::RemoteClient(QObject *parent)
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(slotSocketError(QAbstractSocket::SocketError)));
connect(this, SIGNAL(serverIdentificationEventReceived(const Event_ServerIdentification &)), this, SLOT(processServerIdentificationEvent(const Event_ServerIdentification &)));
connect(this, SIGNAL(connectionClosedEventReceived(Event_ConnectionClosed)), this, SLOT(processConnectionClosedEvent(Event_ConnectionClosed)));
connect(this, SIGNAL(sigConnectToServer(QString, unsigned int, QString, QString)), this, SLOT(doConnectToServer(QString, unsigned int, QString, QString)));
connect(this, SIGNAL(sigDisconnectFromServer()), this, SLOT(doDisconnectFromServer()));
}
@ -66,6 +67,11 @@ void RemoteClient::processServerIdentificationEvent(const Event_ServerIdentifica
sendCommand(pend);
}
void RemoteClient::processConnectionClosedEvent(const Event_ConnectionClosed & /*event*/)
{
doDisconnectFromServer();
}
void RemoteClient::loginResponse(const Response &response)
{
const Response_Login &resp = response.GetExtension(Response_Login::ext);
@ -83,7 +89,7 @@ void RemoteClient::loginResponse(const Response &response)
ignoreList.append(resp.ignore_list(i));
emit ignoreListReceived(ignoreList);
} else {
emit serverError(response.response_code(), QString::fromStdString(resp.denied_reason_str()));
emit loginError(response.response_code(), QString::fromStdString(resp.denied_reason_str()), resp.denied_end_time());
setStatus(StatusDisconnecting);
}
}

View file

@ -11,6 +11,7 @@ class RemoteClient : public AbstractClient {
signals:
void maxPingTime(int seconds, int maxSeconds);
void serverTimeout();
void loginError(Response::ResponseCode resp, QString reasonStr, quint32 endTime);
void socketError(const QString &errorString);
void protocolVersionMismatch(int clientVersion, int serverVersion);
void protocolError();
@ -22,6 +23,7 @@ private slots:
void slotSocketError(QAbstractSocket::SocketError error);
void ping();
void processServerIdentificationEvent(const Event_ServerIdentification &event);
void processConnectionClosedEvent(const Event_ConnectionClosed &event);
void loginResponse(const Response &response);
void doConnectToServer(const QString &hostname, unsigned int port, const QString &_userName, const QString &_password);
void doDisconnectFromServer();

View file

@ -27,6 +27,7 @@
#include <QFile>
#include <QFileDialog>
#include <QThread>
#include <QDateTime>
#include "main.h"
#include "window_main.h"
@ -67,6 +68,10 @@ void MainWindow::processConnectionClosedEvent(const Event_ConnectionClosed &even
case Event_ConnectionClosed::TOO_MANY_CONNECTIONS: reasonStr = tr("There are too many concurrent connections from your address."); break;
case Event_ConnectionClosed::BANNED: {
reasonStr = tr("Banned by moderator");
if (event.has_end_time())
reasonStr.append("\n" + tr("Expected end time: %1").arg(QDateTime::fromTime_t(event.end_time()).toString()));
else
reasonStr.append("\n" + tr("This ban lasts indefinitely."));
if (event.has_reason_str())
reasonStr.append("\n\n" + QString::fromStdString(event.reason_str()));
break;
@ -234,13 +239,29 @@ void MainWindow::serverTimeout()
QMessageBox::critical(this, tr("Error"), tr("Server timeout"));
}
void MainWindow::serverError(Response::ResponseCode r, QString reasonStr)
void MainWindow::loginError(Response::ResponseCode r, QString reasonStr, quint32 endTime)
{
switch (r) {
case Response::RespWrongPassword: QMessageBox::critical(this, tr("Error"), tr("Invalid login data.")); break;
case Response::RespWouldOverwriteOldSession: QMessageBox::critical(this, tr("Error"), tr("There is already an active session using this user name.\nPlease close that session first and re-login.")); break;
case Response::RespUserIsBanned: QMessageBox::critical(this, tr("Error"), tr("You are banned.\n%1").arg(reasonStr)); break;
default: QMessageBox::critical(this, tr("Error"), tr("Unknown server error: %1").arg(static_cast<int>(r)));
case Response::RespWrongPassword:
QMessageBox::critical(this, tr("Error"), tr("Invalid login data."));
break;
case Response::RespWouldOverwriteOldSession:
QMessageBox::critical(this, tr("Error"), tr("There is already an active session using this user name.\nPlease close that session first and re-login."));
break;
case Response::RespUserIsBanned: {
QString bannedStr;
if (endTime)
bannedStr = tr("You are banned until %1.").arg(QDateTime::fromTime_t(endTime).toString());
else
bannedStr = tr("You are banned indefinitely.");
if (!reasonStr.isEmpty())
bannedStr.append("\n\n" + reasonStr);
QMessageBox::critical(this, tr("Error"), bannedStr);
break;
}
default:
QMessageBox::critical(this, tr("Error"), tr("Unknown login error: %1").arg(static_cast<int>(r)));
}
}
@ -343,7 +364,7 @@ MainWindow::MainWindow(QWidget *parent)
client = new RemoteClient;
connect(client, SIGNAL(connectionClosedEventReceived(const Event_ConnectionClosed &)), this, SLOT(processConnectionClosedEvent(const Event_ConnectionClosed &)));
connect(client, SIGNAL(serverShutdownEventReceived(const Event_ServerShutdown &)), this, SLOT(processServerShutdownEvent(const Event_ServerShutdown &)));
connect(client, SIGNAL(serverError(Response::ResponseCode, QString)), this, SLOT(serverError(Response::ResponseCode, QString)));
connect(client, SIGNAL(loginError(Response::ResponseCode, QString, quint32)), this, SLOT(loginError(Response::ResponseCode, QString, quint32)));
connect(client, SIGNAL(socketError(const QString &)), this, SLOT(socketError(const QString &)));
connect(client, SIGNAL(serverTimeout()), this, SLOT(serverTimeout()));
connect(client, SIGNAL(statusChanged(ClientStatus)), this, SLOT(statusChanged(ClientStatus)));

View file

@ -39,7 +39,7 @@ private slots:
void processConnectionClosedEvent(const Event_ConnectionClosed &event);
void processServerShutdownEvent(const Event_ServerShutdown &event);
void serverTimeout();
void serverError(Response::ResponseCode r, QString reasonStr);
void loginError(Response::ResponseCode r, QString reasonStr, quint32 endTime);
void socketError(const QString &errorStr);
void protocolVersionMismatch(int localVersion, int remoteVersion);
void userInfoReceived(const ServerInfo_User &userInfo);

View file

@ -2293,7 +2293,7 @@ Dies wird nur für Moderatoren gespeichert und kann von der gebannten Person nic
<context>
<name>MainWindow</name>
<message>
<location filename="../src/window_main.cpp" line="67"/>
<location filename="../src/window_main.cpp" line="68"/>
<source>There are too many concurrent connections from your address.</source>
<translation>Es gibt zu viele gleichzeitige Verbindungen von Ihrer Adresse.</translation>
</message>
@ -2302,7 +2302,7 @@ Dies wird nur für Moderatoren gespeichert und kann von der gebannten Person nic
<translation type="obsolete">Gebannt von einem Moderator.</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="74"/>
<location filename="../src/window_main.cpp" line="79"/>
<source>Scheduled server shutdown.</source>
<translation>Planmäßige Serverabschaltung.</translation>
</message>
@ -2311,29 +2311,39 @@ Dies wird nur für Moderatoren gespeichert und kann von der gebannten Person nic
<translation type="obsolete">Unbekannter Grund.</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="69"/>
<location filename="../src/window_main.cpp" line="70"/>
<source>Banned by moderator</source>
<translation>Gebannt von einem Moderator</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="77"/>
<location filename="../src/window_main.cpp" line="72"/>
<source>Expected end time: %1</source>
<translation>Voraussichtliches Ende: %1</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="74"/>
<source>This ban lasts indefinitely.</source>
<translation>Dieser Bann ist unbefristet.</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="82"/>
<source>Connection closed</source>
<translation>Verbindung geschlossen</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="77"/>
<location filename="../src/window_main.cpp" line="82"/>
<source>The server has terminated your connection.
Reason: %1</source>
<translation>Der Server hat Ihre Verbindung beendet.
Grund: %1</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="82"/>
<location filename="../src/window_main.cpp" line="87"/>
<source>Scheduled server shutdown</source>
<translation>Planmäßige Serverabschaltung</translation>
</message>
<message numerus="yes">
<location filename="../src/window_main.cpp" line="82"/>
<location filename="../src/window_main.cpp" line="87"/>
<source>The server is going to be restarted in %n minute(s).
All running games will be lost.
Reason for shutdown: %1</source>
@ -2347,150 +2357,163 @@ Grund für die Abschaltung: %1</numerusform>
</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="131"/>
<location filename="../src/window_main.cpp" line="136"/>
<source>Number of players</source>
<translation>Spieleranzahl</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="131"/>
<location filename="../src/window_main.cpp" line="136"/>
<source>Please enter the number of players.</source>
<translation>Bitte die Spieleranzahl eingeben:</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="140"/>
<location filename="../src/window_main.cpp" line="146"/>
<location filename="../src/window_main.cpp" line="145"/>
<location filename="../src/window_main.cpp" line="151"/>
<source>Player %1</source>
<translation>Spieler %1</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="158"/>
<location filename="../src/window_main.cpp" line="163"/>
<source>Load replay</source>
<translation>Aufgezeichnetes Spiel laden</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="214"/>
<location filename="../src/window_main.cpp" line="219"/>
<source>About Cockatrice</source>
<translation>Über Cockatrice</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="216"/>
<location filename="../src/window_main.cpp" line="221"/>
<source>Version %1</source>
<translation>Version %1</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="217"/>
<location filename="../src/window_main.cpp" line="222"/>
<source>Authors:</source>
<translation>Autoren:</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="218"/>
<location filename="../src/window_main.cpp" line="223"/>
<source>Translators:</source>
<translation>Übersetzer:</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="219"/>
<location filename="../src/window_main.cpp" line="224"/>
<source>Spanish:</source>
<translation>Spanisch:</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="220"/>
<location filename="../src/window_main.cpp" line="225"/>
<source>Portugese (Portugal):</source>
<translation>Portugiesisch (Portugal):</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="221"/>
<location filename="../src/window_main.cpp" line="226"/>
<source>Portugese (Brazil):</source>
<translation>Portugiesisch (Brasilien):</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="222"/>
<location filename="../src/window_main.cpp" line="227"/>
<source>French:</source>
<translation>Französisch:</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="223"/>
<location filename="../src/window_main.cpp" line="228"/>
<source>Japanese:</source>
<translation>Japanisch:</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="224"/>
<location filename="../src/window_main.cpp" line="229"/>
<source>Russian:</source>
<translation>Russisch:</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="225"/>
<location filename="../src/window_main.cpp" line="230"/>
<source>Czech:</source>
<translation>Tschechisch:</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="227"/>
<location filename="../src/window_main.cpp" line="232"/>
<source>Italian:</source>
<translation>Italienisch:</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="228"/>
<location filename="../src/window_main.cpp" line="233"/>
<source>Swedish:</source>
<translation>Schwedisch:</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="254"/>
<source>You are banned until %1.</source>
<translation>Sie sind gebannt bis: %1.</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="256"/>
<source>You are banned indefinitely.</source>
<translation>Sie sind auf unbestimmte Zeit gebannt.</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="264"/>
<source>Unknown login error: %1</source>
<translation>Unbekannter Login-Fehler: %1</translation>
</message>
<message>
<source>Slovak:</source>
<translation type="obsolete">Slowakisch:</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="234"/>
<location filename="../src/window_main.cpp" line="240"/>
<location filename="../src/window_main.cpp" line="241"/>
<location filename="../src/window_main.cpp" line="242"/>
<location filename="../src/window_main.cpp" line="243"/>
<location filename="../src/window_main.cpp" line="239"/>
<location filename="../src/window_main.cpp" line="246"/>
<location filename="../src/window_main.cpp" line="249"/>
<location filename="../src/window_main.cpp" line="255"/>
<location filename="../src/window_main.cpp" line="257"/>
<location filename="../src/window_main.cpp" line="260"/>
<location filename="../src/window_main.cpp" line="264"/>
<location filename="../src/window_main.cpp" line="270"/>
<location filename="../src/window_main.cpp" line="276"/>
<location filename="../src/window_main.cpp" line="278"/>
<source>Error</source>
<translation>Fehler</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="234"/>
<location filename="../src/window_main.cpp" line="239"/>
<source>Server timeout</source>
<translation>Server Zeitüberschreitung</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="240"/>
<location filename="../src/window_main.cpp" line="246"/>
<source>Invalid login data.</source>
<translation>Ungültige Anmeldedaten.</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="241"/>
<location filename="../src/window_main.cpp" line="249"/>
<source>There is already an active session using this user name.
Please close that session first and re-login.</source>
<translation>Es gibt bereits eine aktive Verbindung mit diesem Benutzernamen.
Bitte schließen Sie diese Verbindung zuerst und versuchen Sie es dann erneut.</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="242"/>
<source>You are banned.
%1</source>
<translation>Sie sind gebannt.
<translation type="obsolete">Sie sind gebannt.
%1</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="243"/>
<source>Unknown server error: %1</source>
<translation>Unbekannter Serverfehler: %1</translation>
<translation type="obsolete">Unbekannter Serverfehler: %1</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="249"/>
<location filename="../src/window_main.cpp" line="270"/>
<source>Socket error: %1</source>
<translation>Netzwerkfehler: %1</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="255"/>
<location filename="../src/window_main.cpp" line="276"/>
<source>You are trying to connect to an obsolete server. Please downgrade your Cockatrice version or connect to a suitable server.
Local version is %1, remote version is %2.</source>
<translation>Sie versuchen sich an einem veralteten Server anzumelden. Bitte verwenden Sie eine ältere Cockatrice-Version oder melden Sie sich an einem aktuellen Server an.
Lokale Version ist %1, Serverversion ist %2.</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="257"/>
<location filename="../src/window_main.cpp" line="278"/>
<source>Your Cockatrice client is obsolete. Please update your Cockatrice version.
Local version is %1, remote version is %2.</source>
<translation>Ihr Cockatrice-Client ist veraltet. Bitte laden Sie sich die neueste Version herunter.
@ -2501,62 +2524,62 @@ Lokale Version ist %1, Serverversion ist %2.</translation>
<translation type="obsolete">Protokollversionen stimmen nicht überein. Lokale Version: %1, Serverversion: %2.</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="263"/>
<location filename="../src/window_main.cpp" line="284"/>
<source>Connecting to %1...</source>
<translation>Verbinde zu %1...</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="264"/>
<location filename="../src/window_main.cpp" line="285"/>
<source>Disconnected</source>
<translation>nicht verbunden</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="265"/>
<location filename="../src/window_main.cpp" line="286"/>
<source>Connected, logging in at %1</source>
<translation>Verbunden, Anmeldung bei %1</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="266"/>
<location filename="../src/window_main.cpp" line="287"/>
<source>Logged in at %1</source>
<translation>Angemeldet bei %1</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="275"/>
<location filename="../src/window_main.cpp" line="296"/>
<source>&amp;Connect...</source>
<translation>&amp;Verbinden...</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="276"/>
<location filename="../src/window_main.cpp" line="297"/>
<source>&amp;Disconnect</source>
<translation>Verbindung &amp;trennen</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="277"/>
<location filename="../src/window_main.cpp" line="298"/>
<source>Start &amp;local game...</source>
<translation>&amp;Lokales Spiel starten...</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="278"/>
<location filename="../src/window_main.cpp" line="299"/>
<source>&amp;Watch replay...</source>
<translation>&amp;Aufgezeichnetes Spiel abspielen...</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="287"/>
<location filename="../src/window_main.cpp" line="308"/>
<source>&amp;About Cockatrice</source>
<translation>&amp;Über Cockatrice</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="288"/>
<location filename="../src/window_main.cpp" line="309"/>
<source>&amp;Help</source>
<translation>&amp;Hilfe</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="380"/>
<location filename="../src/window_main.cpp" line="401"/>
<source>Are you sure?</source>
<translation>Sind Sie sicher?</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="380"/>
<location filename="../src/window_main.cpp" line="401"/>
<source>There are still open games. Are you sure you want to quit?</source>
<translation>Es gibt noch offene Spiele. Wollen Sie das Programm wirklich beenden?</translation>
</message>
@ -2573,27 +2596,27 @@ Lokale Version ist %1, Serverversion ist %2.</translation>
<translation type="obsolete">Spiel ver&amp;lassen</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="279"/>
<location filename="../src/window_main.cpp" line="300"/>
<source>&amp;Deck editor</source>
<translation>&amp;Deck-Editor</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="280"/>
<location filename="../src/window_main.cpp" line="301"/>
<source>&amp;Full screen</source>
<translation>&amp;Vollbild</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="281"/>
<location filename="../src/window_main.cpp" line="302"/>
<source>Ctrl+F</source>
<translation>Ctrl+F</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="282"/>
<location filename="../src/window_main.cpp" line="303"/>
<source>&amp;Settings...</source>
<translation>&amp;Einstellungen...</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="283"/>
<location filename="../src/window_main.cpp" line="304"/>
<source>&amp;Exit</source>
<translation>&amp;Beenden</translation>
</message>
@ -2606,7 +2629,7 @@ Lokale Version ist %1, Serverversion ist %2.</translation>
<translation type="obsolete">Esc</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="285"/>
<location filename="../src/window_main.cpp" line="306"/>
<source>&amp;Cockatrice</source>
<translation>&amp;Cockatrice</translation>
</message>
@ -5171,7 +5194,7 @@ Lokale Version ist %1, Serverversion ist %2.</translation>
<translation type="obsolete">Alle Dateien (*.*)</translation>
</message>
<message>
<location filename="../src/window_main.cpp" line="160"/>
<location filename="../src/window_main.cpp" line="165"/>
<source>Cockatrice replays (*.cor)</source>
<translation>Aufgezeichnete Cockatrice-Spiele (*.cor)</translation>
</message>
@ -5197,32 +5220,32 @@ Lokale Version ist %1, Serverversion ist %2.</translation>
<context>
<name>RemoteReplayList_TreeModel</name>
<message>
<location filename="../src/remotereplaylist_treewidget.cpp" line="125"/>
<location filename="../src/remotereplaylist_treewidget.cpp" line="137"/>
<source>ID</source>
<translation>ID</translation>
</message>
<message>
<location filename="../src/remotereplaylist_treewidget.cpp" line="126"/>
<location filename="../src/remotereplaylist_treewidget.cpp" line="138"/>
<source>Name</source>
<translation>Name</translation>
</message>
<message>
<location filename="../src/remotereplaylist_treewidget.cpp" line="127"/>
<location filename="../src/remotereplaylist_treewidget.cpp" line="139"/>
<source>Players</source>
<translation>Spieler</translation>
</message>
<message>
<location filename="../src/remotereplaylist_treewidget.cpp" line="128"/>
<location filename="../src/remotereplaylist_treewidget.cpp" line="140"/>
<source>Keep</source>
<translation>Behalten</translation>
</message>
<message>
<location filename="../src/remotereplaylist_treewidget.cpp" line="129"/>
<location filename="../src/remotereplaylist_treewidget.cpp" line="141"/>
<source>Time started</source>
<translation>Startzeit</translation>
</message>
<message>
<location filename="../src/remotereplaylist_treewidget.cpp" line="130"/>
<location filename="../src/remotereplaylist_treewidget.cpp" line="142"/>
<source>Duration (sec)</source>
<translation>Länge (Sekunden)</translation>
</message>

View file

@ -12,4 +12,5 @@ message Event_ConnectionClosed {
}
optional CloseReason reason = 1;
optional string reason_str = 2;
optional uint32 end_time = 3;
}

View file

@ -9,4 +9,5 @@ message Response_Login {
repeated ServerInfo_User buddy_list = 2;
repeated ServerInfo_User ignore_list = 3;
optional string denied_reason_str = 4;
optional uint64 denied_end_time = 5;
}

View file

@ -66,14 +66,14 @@ void Server::prepareDestroy()
roomsLock.unlock();
}
AuthenticationResult Server::loginUser(Server_ProtocolHandler *session, QString &name, const QString &password, QString &reasonStr)
AuthenticationResult Server::loginUser(Server_ProtocolHandler *session, QString &name, const QString &password, QString &reasonStr, int &secondsLeft)
{
if (name.size() > 35)
name = name.left(35);
QWriteLocker locker(&clientsLock);
AuthenticationResult authState = checkUserPassword(session, name, password, reasonStr);
AuthenticationResult authState = checkUserPassword(session, name, password, reasonStr, secondsLeft);
if ((authState == NotLoggedIn) || (authState == UserIsBanned))
return authState;

View file

@ -41,7 +41,7 @@ public:
mutable QReadWriteLock clientsLock, roomsLock; // locking order: roomsLock before clientsLock
Server(QObject *parent = 0);
~Server();
AuthenticationResult loginUser(Server_ProtocolHandler *session, QString &name, const QString &password, QString &reason);
AuthenticationResult loginUser(Server_ProtocolHandler *session, QString &name, const QString &password, QString &reason, int &secondsLeft);
const QMap<int, Server_Room *> &getRooms() { return rooms; }
virtual int getNextGameId() { return nextGameId++; }
virtual int getNextReplayId() { return nextReplayId++; }
@ -111,7 +111,7 @@ protected:
virtual qint64 startSession(const QString &userName, const QString &address) { return 0; }
virtual void endSession(qint64 sessionId) { }
virtual bool userExists(const QString &user) { return false; }
virtual AuthenticationResult checkUserPassword(Server_ProtocolHandler *handler, const QString &user, const QString &password, QString &reason) { return UnknownUser; }
virtual AuthenticationResult checkUserPassword(Server_ProtocolHandler *handler, const QString &user, const QString &password, QString &reason, int &secondsLeft) { return UnknownUser; }
virtual ServerInfo_User getUserData(const QString &name, bool withId = false) = 0;
int getUsersCount() const;
int getGamesCount() const;

View file

@ -337,11 +337,14 @@ Response::ResponseCode Server_ProtocolHandler::cmdLogin(const Command_Login &cmd
if (userName.isEmpty() || (userInfo != 0))
return Response::RespContextError;
QString reasonStr;
AuthenticationResult res = server->loginUser(this, userName, QString::fromStdString(cmd.password()), reasonStr);
int banSecondsLeft = 0;
AuthenticationResult res = server->loginUser(this, userName, QString::fromStdString(cmd.password()), reasonStr, banSecondsLeft);
switch (res) {
case UserIsBanned: {
Response_Login *re = new Response_Login;
re->set_denied_reason_str(reasonStr.toStdString());
if (banSecondsLeft != 0)
re->set_denied_end_time(QDateTime::currentDateTime().addSecs(banSecondsLeft).toTime_t());
rc.setResponseExtension(re);
return Response::RespUserIsBanned;
}

View file

@ -297,7 +297,7 @@ QList<ServerProperties> Servatrice::getServerList() const
return result;
}
AuthenticationResult Servatrice::checkUserPassword(Server_ProtocolHandler *handler, const QString &user, const QString &password, QString &reasonStr)
AuthenticationResult Servatrice::checkUserPassword(Server_ProtocolHandler *handler, const QString &user, const QString &password, QString &reasonStr, int &banSecondsLeft)
{
QMutexLocker locker(&dbMutex);
const QString method = settings->value("authentication/method").toString();
@ -308,7 +308,7 @@ AuthenticationResult Servatrice::checkUserPassword(Server_ProtocolHandler *handl
return UnknownUser;
QSqlQuery ipBanQuery;
ipBanQuery.prepare("select time_to_sec(timediff(now(), date_add(b.time_from, interval b.minutes minute))) < 0, b.minutes <=> 0, b.visible_reason from " + dbPrefix + "_bans b where b.time_from = (select max(c.time_from) from " + dbPrefix + "_bans c where c.ip_address = :address) and b.ip_address = :address2");
ipBanQuery.prepare("select time_to_sec(timediff(now(), date_add(b.time_from, interval b.minutes minute))), b.minutes <=> 0, b.visible_reason from " + dbPrefix + "_bans b where b.time_from = (select max(c.time_from) from " + dbPrefix + "_bans c where c.ip_address = :address) and b.ip_address = :address2");
ipBanQuery.bindValue(":address", static_cast<ServerSocketInterface *>(handler)->getPeerAddress().toString());
ipBanQuery.bindValue(":address2", static_cast<ServerSocketInterface *>(handler)->getPeerAddress().toString());
if (!execSqlQuery(ipBanQuery)) {
@ -316,15 +316,19 @@ AuthenticationResult Servatrice::checkUserPassword(Server_ProtocolHandler *handl
return NotLoggedIn;
}
if (ipBanQuery.next())
if (ipBanQuery.value(0).toInt() || ipBanQuery.value(1).toInt()) {
if (ipBanQuery.next()) {
const int secondsLeft = -ipBanQuery.value(0).toInt();
const bool permanentBan = ipBanQuery.value(1).toInt();
if ((secondsLeft > 0) || permanentBan) {
reasonStr = ipBanQuery.value(2).toString();
banSecondsLeft = permanentBan ? 0 : secondsLeft;
qDebug("Login denied: banned by address");
return UserIsBanned;
}
}
QSqlQuery nameBanQuery;
nameBanQuery.prepare("select time_to_sec(timediff(now(), date_add(b.time_from, interval b.minutes minute))) < 0, b.minutes <=> 0, b.visible_reason from " + dbPrefix + "_bans b where b.time_from = (select max(c.time_from) from " + dbPrefix + "_bans c where c.user_name = :name2) and b.user_name = :name1");
nameBanQuery.prepare("select time_to_sec(timediff(now(), date_add(b.time_from, interval b.minutes minute))), b.minutes <=> 0, b.visible_reason from " + dbPrefix + "_bans b where b.time_from = (select max(c.time_from) from " + dbPrefix + "_bans c where c.user_name = :name2) and b.user_name = :name1");
nameBanQuery.bindValue(":name1", user);
nameBanQuery.bindValue(":name2", user);
if (!execSqlQuery(nameBanQuery)) {
@ -332,12 +336,16 @@ AuthenticationResult Servatrice::checkUserPassword(Server_ProtocolHandler *handl
return NotLoggedIn;
}
if (nameBanQuery.next())
if (nameBanQuery.value(0).toInt() || nameBanQuery.value(1).toInt()) {
if (nameBanQuery.next()) {
const int secondsLeft = -nameBanQuery.value(0).toInt();
const bool permanentBan = nameBanQuery.value(1).toInt();
if ((secondsLeft > 0) || permanentBan) {
reasonStr = nameBanQuery.value(2).toString();
banSecondsLeft = permanentBan ? 0 : secondsLeft;
qDebug("Login denied: banned by name");
return UserIsBanned;
}
}
QSqlQuery passwordQuery;
passwordQuery.prepare("select password_sha512 from " + dbPrefix + "_users where name = :name and active = 1");

View file

@ -130,7 +130,7 @@ protected:
qint64 startSession(const QString &userName, const QString &address);
void endSession(qint64 sessionId);
bool userExists(const QString &user);
AuthenticationResult checkUserPassword(Server_ProtocolHandler *handler, const QString &user, const QString &password, QString &reasonStr);
AuthenticationResult checkUserPassword(Server_ProtocolHandler *handler, const QString &user, const QString &password, QString &reasonStr, int &secondsLeft);
void clearSessionTables();
void lockSessionTables();

View file

@ -587,8 +587,8 @@ Response::ResponseCode ServerSocketInterface::cmdBanFromServer(const Command_Ban
query.bindValue(":ip_address", address);
query.bindValue(":id_admin", userInfo->id());
query.bindValue(":minutes", minutes);
query.bindValue(":reason", QString::fromStdString(cmd.reason()) + "\n");
query.bindValue(":visible_reason", QString::fromStdString(cmd.visible_reason()) + "\n");
query.bindValue(":reason", QString::fromStdString(cmd.reason()));
query.bindValue(":visible_reason", QString::fromStdString(cmd.visible_reason()));
servatrice->execSqlQuery(query);
servatrice->dbMutex.unlock();
@ -601,6 +601,8 @@ Response::ResponseCode ServerSocketInterface::cmdBanFromServer(const Command_Ban
event.set_reason(Event_ConnectionClosed::BANNED);
if (cmd.has_visible_reason())
event.set_reason_str(cmd.visible_reason());
if (minutes)
event.set_end_time(QDateTime::currentDateTime().addSecs(60 * minutes).toTime_t());
for (int i = 0; i < userList.size(); ++i) {
SessionEvent *se = userList[i]->prepareSessionEvent(event);
userList[i]->sendProtocolItem(*se);