display reason for ban to banned user
This commit is contained in:
parent
6344b987de
commit
ff3eb9b5f4
19 changed files with 98 additions and 59 deletions
|
@ -36,7 +36,7 @@ class AbstractClient : public QObject {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
signals:
|
signals:
|
||||||
void statusChanged(ClientStatus _status);
|
void statusChanged(ClientStatus _status);
|
||||||
void serverError(Response::ResponseCode resp);
|
void serverError(Response::ResponseCode resp, QString reasonStr);
|
||||||
|
|
||||||
// Room events
|
// Room events
|
||||||
void roomEventReceived(const RoomEvent &event);
|
void roomEventReceived(const RoomEvent &event);
|
||||||
|
|
|
@ -11,7 +11,7 @@ class LocalServer : public Server
|
||||||
public:
|
public:
|
||||||
LocalServer(QObject *parent = 0);
|
LocalServer(QObject *parent = 0);
|
||||||
~LocalServer();
|
~LocalServer();
|
||||||
AuthenticationResult checkUserPassword(Server_ProtocolHandler * /*handler*/, const QString & /*user*/, const QString & /*password*/) { return UnknownUser; }
|
AuthenticationResult checkUserPassword(Server_ProtocolHandler * /*handler*/, const QString & /*user*/, const QString & /*password*/, QString & /*reasonStr*/) { return UnknownUser; }
|
||||||
QString getLoginMessage() const { return QString(); }
|
QString getLoginMessage() const { return QString(); }
|
||||||
bool getGameShouldPing() const { return false; }
|
bool getGameShouldPing() const { return false; }
|
||||||
int getMaxGameInactivityTime() const { return 9999999; }
|
int getMaxGameInactivityTime() const { return 9999999; }
|
||||||
|
|
|
@ -56,8 +56,8 @@ void RemoteClient::processServerIdentificationEvent(const Event_ServerIdentifica
|
||||||
|
|
||||||
void RemoteClient::loginResponse(const Response &response)
|
void RemoteClient::loginResponse(const Response &response)
|
||||||
{
|
{
|
||||||
|
const Response_Login &resp = response.GetExtension(Response_Login::ext);
|
||||||
if (response.response_code() == Response::RespOk) {
|
if (response.response_code() == Response::RespOk) {
|
||||||
const Response_Login &resp = response.GetExtension(Response_Login::ext);
|
|
||||||
setStatus(StatusLoggedIn);
|
setStatus(StatusLoggedIn);
|
||||||
emit userInfoChanged(resp.user_info());
|
emit userInfoChanged(resp.user_info());
|
||||||
|
|
||||||
|
@ -71,7 +71,7 @@ void RemoteClient::loginResponse(const Response &response)
|
||||||
ignoreList.append(resp.ignore_list(i));
|
ignoreList.append(resp.ignore_list(i));
|
||||||
emit ignoreListReceived(ignoreList);
|
emit ignoreListReceived(ignoreList);
|
||||||
} else {
|
} else {
|
||||||
emit serverError(response.response_code());
|
emit serverError(response.response_code(), QString::fromStdString(resp.denied_reason_str()));
|
||||||
setStatus(StatusDisconnecting);
|
setStatus(StatusDisconnecting);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -81,6 +81,9 @@ BanDialog::BanDialog(const ServerInfo_User &info, QWidget *parent)
|
||||||
QLabel *reasonLabel = new QLabel(tr("Please enter the reason for the ban.\nThis is only saved for moderators and cannot be seen by the banned person."));
|
QLabel *reasonLabel = new QLabel(tr("Please enter the reason for the ban.\nThis is only saved for moderators and cannot be seen by the banned person."));
|
||||||
reasonEdit = new QPlainTextEdit;
|
reasonEdit = new QPlainTextEdit;
|
||||||
|
|
||||||
|
QLabel *visibleReasonLabel = new QLabel(tr("Please enter the reason for the ban that will be visible to the banned person."));
|
||||||
|
visibleReasonEdit = new QPlainTextEdit;
|
||||||
|
|
||||||
QPushButton *okButton = new QPushButton(tr("&OK"));
|
QPushButton *okButton = new QPushButton(tr("&OK"));
|
||||||
okButton->setAutoDefault(true);
|
okButton->setAutoDefault(true);
|
||||||
connect(okButton, SIGNAL(clicked()), this, SLOT(okClicked()));
|
connect(okButton, SIGNAL(clicked()), this, SLOT(okClicked()));
|
||||||
|
@ -97,6 +100,8 @@ BanDialog::BanDialog(const ServerInfo_User &info, QWidget *parent)
|
||||||
vbox->addWidget(durationGroupBox);
|
vbox->addWidget(durationGroupBox);
|
||||||
vbox->addWidget(reasonLabel);
|
vbox->addWidget(reasonLabel);
|
||||||
vbox->addWidget(reasonEdit);
|
vbox->addWidget(reasonEdit);
|
||||||
|
vbox->addWidget(visibleReasonLabel);
|
||||||
|
vbox->addWidget(visibleReasonEdit);
|
||||||
vbox->addLayout(buttonLayout);
|
vbox->addLayout(buttonLayout);
|
||||||
|
|
||||||
setLayout(vbox);
|
setLayout(vbox);
|
||||||
|
@ -142,6 +147,11 @@ QString BanDialog::getReason() const
|
||||||
return reasonEdit->toPlainText();
|
return reasonEdit->toPlainText();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString BanDialog::getVisibleReason() const
|
||||||
|
{
|
||||||
|
return visibleReasonEdit->toPlainText();
|
||||||
|
}
|
||||||
|
|
||||||
UserListItemDelegate::UserListItemDelegate(QObject *const parent)
|
UserListItemDelegate::UserListItemDelegate(QObject *const parent)
|
||||||
: QStyledItemDelegate(parent)
|
: QStyledItemDelegate(parent)
|
||||||
{
|
{
|
||||||
|
@ -344,6 +354,7 @@ void UserList::banUser_dialogFinished()
|
||||||
cmd.set_address(dlg->getBanIP().toStdString());
|
cmd.set_address(dlg->getBanIP().toStdString());
|
||||||
cmd.set_minutes(dlg->getMinutes());
|
cmd.set_minutes(dlg->getMinutes());
|
||||||
cmd.set_reason(dlg->getReason().toStdString());
|
cmd.set_reason(dlg->getReason().toStdString());
|
||||||
|
cmd.set_visible_reason(dlg->getVisibleReason().toStdString());
|
||||||
|
|
||||||
client->sendCommand(client->prepareModeratorCommand(cmd));
|
client->sendCommand(client->prepareModeratorCommand(cmd));
|
||||||
}
|
}
|
||||||
|
@ -418,7 +429,7 @@ void UserList::showContextMenu(const QPoint &pos, const QModelIndex &index)
|
||||||
cmd.set_user_name(userName.toStdString());
|
cmd.set_user_name(userName.toStdString());
|
||||||
|
|
||||||
PendingCommand *pend = client->prepareSessionCommand(cmd);
|
PendingCommand *pend = client->prepareSessionCommand(cmd);
|
||||||
connect(pend, SIGNAL(finished(ProtocolResponse *)), this, SLOT(gamesOfUserReceived(ProtocolResponse *)));
|
connect(pend, SIGNAL(finished(const Response &)), this, SLOT(gamesOfUserReceived(const Response &)));
|
||||||
|
|
||||||
client->sendCommand(pend);
|
client->sendCommand(pend);
|
||||||
} else if (actionClicked == aAddToIgnoreList) {
|
} else if (actionClicked == aAddToIgnoreList) {
|
||||||
|
@ -438,7 +449,7 @@ void UserList::showContextMenu(const QPoint &pos, const QModelIndex &index)
|
||||||
cmd.set_user_name(userName.toStdString());
|
cmd.set_user_name(userName.toStdString());
|
||||||
|
|
||||||
PendingCommand *pend = client->prepareSessionCommand(cmd);
|
PendingCommand *pend = client->prepareSessionCommand(cmd);
|
||||||
connect(pend, SIGNAL(finished(ProtocolResponse *)), this, SLOT(banUser_processUserInfoResponse(ProtocolResponse *)));
|
connect(pend, SIGNAL(finished(const Response &)), this, SLOT(banUser_processUserInfoResponse(const Response &)));
|
||||||
|
|
||||||
client->sendCommand(pend);
|
client->sendCommand(pend);
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,7 +25,7 @@ private:
|
||||||
QLineEdit *nameBanEdit, *ipBanEdit;
|
QLineEdit *nameBanEdit, *ipBanEdit;
|
||||||
QSpinBox *daysEdit, *hoursEdit, *minutesEdit;
|
QSpinBox *daysEdit, *hoursEdit, *minutesEdit;
|
||||||
QRadioButton *permanentRadio, *temporaryRadio;
|
QRadioButton *permanentRadio, *temporaryRadio;
|
||||||
QPlainTextEdit *reasonEdit;
|
QPlainTextEdit *reasonEdit, *visibleReasonEdit;
|
||||||
private slots:
|
private slots:
|
||||||
void okClicked();
|
void okClicked();
|
||||||
void enableTemporaryEdits(bool enabled);
|
void enableTemporaryEdits(bool enabled);
|
||||||
|
@ -35,6 +35,7 @@ public:
|
||||||
QString getBanIP() const;
|
QString getBanIP() const;
|
||||||
int getMinutes() const;
|
int getMinutes() const;
|
||||||
QString getReason() const;
|
QString getReason() const;
|
||||||
|
QString getVisibleReason() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
class UserListItemDelegate : public QStyledItemDelegate {
|
class UserListItemDelegate : public QStyledItemDelegate {
|
||||||
|
|
|
@ -53,17 +53,19 @@ void MainWindow::updateTabMenu(QMenu *menu)
|
||||||
|
|
||||||
void MainWindow::processConnectionClosedEvent(const Event_ConnectionClosed &event)
|
void MainWindow::processConnectionClosedEvent(const Event_ConnectionClosed &event)
|
||||||
{
|
{
|
||||||
QString reason = QString::fromStdString(event.reason());
|
|
||||||
client->disconnectFromServer();
|
client->disconnectFromServer();
|
||||||
QString reasonStr;
|
QString reasonStr;
|
||||||
if (reason == "too_many_connections")
|
switch (event.reason()) {
|
||||||
reasonStr = tr("There are too many concurrent connections from your address.");
|
case Event_ConnectionClosed::TOO_MANY_CONNECTIONS: reasonStr = tr("There are too many concurrent connections from your address."); break;
|
||||||
else if (reason == "banned")
|
case Event_ConnectionClosed::BANNED: {
|
||||||
reasonStr = tr("Banned by moderator.");
|
reasonStr = tr("Banned by moderator");
|
||||||
else if (reason == "server_shutdown")
|
if (event.has_reason_str())
|
||||||
reasonStr = tr("Scheduled server shutdown.");
|
reasonStr.append("\n\n" + QString::fromStdString(event.reason_str()));
|
||||||
else
|
break;
|
||||||
reasonStr = tr("Unknown reason.");
|
}
|
||||||
|
case Event_ConnectionClosed::SERVER_SHUTDOWN: reasonStr = tr("Scheduled server shutdown."); break;
|
||||||
|
default: reasonStr = QString::fromStdString(event.reason_str());
|
||||||
|
}
|
||||||
QMessageBox::critical(this, tr("Connection closed"), tr("The server has terminated your connection.\nReason: %1").arg(reasonStr));
|
QMessageBox::critical(this, tr("Connection closed"), tr("The server has terminated your connection.\nReason: %1").arg(reasonStr));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -201,11 +203,12 @@ void MainWindow::serverTimeout()
|
||||||
QMessageBox::critical(this, tr("Error"), tr("Server timeout"));
|
QMessageBox::critical(this, tr("Error"), tr("Server timeout"));
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::serverError(Response::ResponseCode r)
|
void MainWindow::serverError(Response::ResponseCode r, QString reasonStr)
|
||||||
{
|
{
|
||||||
switch (r) {
|
switch (r) {
|
||||||
case Response::RespWrongPassword: QMessageBox::critical(this, tr("Error"), tr("Invalid login data.")); break;
|
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::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: ;
|
default: ;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -304,7 +307,7 @@ MainWindow::MainWindow(QWidget *parent)
|
||||||
client = new RemoteClient(this);
|
client = new RemoteClient(this);
|
||||||
connect(client, SIGNAL(connectionClosedEventReceived(const Event_ConnectionClosed &)), this, SLOT(processConnectionClosedEvent(const Event_ConnectionClosed &)));
|
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(serverShutdownEventReceived(const Event_ServerShutdown &)), this, SLOT(processServerShutdownEvent(const Event_ServerShutdown &)));
|
||||||
connect(client, SIGNAL(serverError(Response::ResponseCode)), this, SLOT(serverError(Response::ResponseCode)));
|
connect(client, SIGNAL(serverError(Response::ResponseCode, QString)), this, SLOT(serverError(Response::ResponseCode, QString)));
|
||||||
connect(client, SIGNAL(socketError(const QString &)), this, SLOT(socketError(const QString &)));
|
connect(client, SIGNAL(socketError(const QString &)), this, SLOT(socketError(const QString &)));
|
||||||
connect(client, SIGNAL(serverTimeout()), this, SLOT(serverTimeout()));
|
connect(client, SIGNAL(serverTimeout()), this, SLOT(serverTimeout()));
|
||||||
connect(client, SIGNAL(statusChanged(ClientStatus)), this, SLOT(statusChanged(ClientStatus)));
|
connect(client, SIGNAL(statusChanged(ClientStatus)), this, SLOT(statusChanged(ClientStatus)));
|
||||||
|
|
|
@ -38,7 +38,7 @@ private slots:
|
||||||
void processConnectionClosedEvent(const Event_ConnectionClosed &event);
|
void processConnectionClosedEvent(const Event_ConnectionClosed &event);
|
||||||
void processServerShutdownEvent(const Event_ServerShutdown &event);
|
void processServerShutdownEvent(const Event_ServerShutdown &event);
|
||||||
void serverTimeout();
|
void serverTimeout();
|
||||||
void serverError(Response::ResponseCode r);
|
void serverError(Response::ResponseCode r, QString reasonStr);
|
||||||
void socketError(const QString &errorStr);
|
void socketError(const QString &errorStr);
|
||||||
void protocolVersionMismatch(int localVersion, int remoteVersion);
|
void protocolVersionMismatch(int localVersion, int remoteVersion);
|
||||||
void userInfoReceived(const ServerInfo_User &userInfo);
|
void userInfoReceived(const ServerInfo_User &userInfo);
|
||||||
|
|
|
@ -4,5 +4,12 @@ message Event_ConnectionClosed {
|
||||||
extend SessionEvent {
|
extend SessionEvent {
|
||||||
optional Event_ConnectionClosed ext = 1002;
|
optional Event_ConnectionClosed ext = 1002;
|
||||||
}
|
}
|
||||||
optional string reason = 1;
|
enum CloseReason {
|
||||||
|
OTHER = 1;
|
||||||
|
SERVER_SHUTDOWN = 2;
|
||||||
|
TOO_MANY_CONNECTIONS = 3;
|
||||||
|
BANNED = 4;
|
||||||
|
}
|
||||||
|
optional CloseReason reason = 1;
|
||||||
|
optional string reason_str = 2;
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,4 +13,5 @@ message Command_BanFromServer {
|
||||||
optional string address = 2;
|
optional string address = 2;
|
||||||
optional uint32 minutes = 3;
|
optional uint32 minutes = 3;
|
||||||
optional string reason = 4;
|
optional string reason = 4;
|
||||||
|
optional string visible_reason = 5;
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,7 @@ message Response {
|
||||||
RespInIgnoreList = 16;
|
RespInIgnoreList = 16;
|
||||||
RespWouldOverwriteOldSession = 17;
|
RespWouldOverwriteOldSession = 17;
|
||||||
RespChatFlood = 18;
|
RespChatFlood = 18;
|
||||||
|
RespUserIsBanned = 19;
|
||||||
}
|
}
|
||||||
enum ResponseType {
|
enum ResponseType {
|
||||||
JOIN_ROOM = 1000;
|
JOIN_ROOM = 1000;
|
||||||
|
|
|
@ -8,4 +8,5 @@ message Response_Login {
|
||||||
optional ServerInfo_User user_info = 1;
|
optional ServerInfo_User user_info = 1;
|
||||||
repeated ServerInfo_User buddy_list = 2;
|
repeated ServerInfo_User buddy_list = 2;
|
||||||
repeated ServerInfo_User ignore_list = 3;
|
repeated ServerInfo_User ignore_list = 3;
|
||||||
|
optional string denied_reason_str = 4;
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,13 +49,13 @@ void Server::prepareDestroy()
|
||||||
delete roomIterator.next().value();
|
delete roomIterator.next().value();
|
||||||
}
|
}
|
||||||
|
|
||||||
AuthenticationResult Server::loginUser(Server_ProtocolHandler *session, QString &name, const QString &password)
|
AuthenticationResult Server::loginUser(Server_ProtocolHandler *session, QString &name, const QString &password, QString &reasonStr)
|
||||||
{
|
{
|
||||||
QMutexLocker locker(&serverMutex);
|
QMutexLocker locker(&serverMutex);
|
||||||
if (name.size() > 35)
|
if (name.size() > 35)
|
||||||
name = name.left(35);
|
name = name.left(35);
|
||||||
AuthenticationResult authState = checkUserPassword(session, name, password);
|
AuthenticationResult authState = checkUserPassword(session, name, password, reasonStr);
|
||||||
if (authState == PasswordWrong)
|
if ((authState == NotLoggedIn) || (authState == UserIsBanned))
|
||||||
return authState;
|
return authState;
|
||||||
|
|
||||||
ServerInfo_User data = getUserData(name);
|
ServerInfo_User data = getUserData(name);
|
||||||
|
|
|
@ -11,7 +11,7 @@ class Server_Game;
|
||||||
class Server_Room;
|
class Server_Room;
|
||||||
class Server_ProtocolHandler;
|
class Server_ProtocolHandler;
|
||||||
|
|
||||||
enum AuthenticationResult { PasswordWrong = 0, PasswordRight = 1, UnknownUser = 2, WouldOverwriteOldSession = 3 };
|
enum AuthenticationResult { NotLoggedIn = 0, PasswordRight = 1, UnknownUser = 2, WouldOverwriteOldSession = 3, UserIsBanned = 4 };
|
||||||
|
|
||||||
class Server : public QObject
|
class Server : public QObject
|
||||||
{
|
{
|
||||||
|
@ -24,7 +24,7 @@ public:
|
||||||
mutable QMutex serverMutex;
|
mutable QMutex serverMutex;
|
||||||
Server(QObject *parent = 0);
|
Server(QObject *parent = 0);
|
||||||
~Server();
|
~Server();
|
||||||
AuthenticationResult loginUser(Server_ProtocolHandler *session, QString &name, const QString &password);
|
AuthenticationResult loginUser(Server_ProtocolHandler *session, QString &name, const QString &password, QString &reason);
|
||||||
const QMap<int, Server_Room *> &getRooms() { return rooms; }
|
const QMap<int, Server_Room *> &getRooms() { return rooms; }
|
||||||
int getNextGameId() { return nextGameId++; }
|
int getNextGameId() { return nextGameId++; }
|
||||||
|
|
||||||
|
@ -55,7 +55,7 @@ protected:
|
||||||
virtual int startSession(const QString &userName, const QString &address) = 0;
|
virtual int startSession(const QString &userName, const QString &address) = 0;
|
||||||
virtual void endSession(int sessionId) = 0;
|
virtual void endSession(int sessionId) = 0;
|
||||||
virtual bool userExists(const QString &user) = 0;
|
virtual bool userExists(const QString &user) = 0;
|
||||||
virtual AuthenticationResult checkUserPassword(Server_ProtocolHandler *handler, const QString &user, const QString &password) = 0;
|
virtual AuthenticationResult checkUserPassword(Server_ProtocolHandler *handler, const QString &user, const QString &password, QString &reason) = 0;
|
||||||
virtual ServerInfo_User getUserData(const QString &name) = 0;
|
virtual ServerInfo_User getUserData(const QString &name) = 0;
|
||||||
int getUsersCount() const;
|
int getUsersCount() const;
|
||||||
int getGamesCount() const;
|
int getGamesCount() const;
|
||||||
|
|
|
@ -85,7 +85,7 @@
|
||||||
#include <google/protobuf/descriptor.h>
|
#include <google/protobuf/descriptor.h>
|
||||||
|
|
||||||
Server_ProtocolHandler::Server_ProtocolHandler(Server *_server, QObject *parent)
|
Server_ProtocolHandler::Server_ProtocolHandler(Server *_server, QObject *parent)
|
||||||
: QObject(parent), server(_server), authState(PasswordWrong), acceptsUserListChanges(false), acceptsRoomListChanges(false), userInfo(0), sessionId(-1), timeRunning(0), lastDataReceived(0), gameListMutex(QMutex::Recursive)
|
: QObject(parent), server(_server), authState(NotLoggedIn), acceptsUserListChanges(false), acceptsRoomListChanges(false), userInfo(0), sessionId(-1), timeRunning(0), lastDataReceived(0), gameListMutex(QMutex::Recursive)
|
||||||
{
|
{
|
||||||
connect(server, SIGNAL(pingClockTimeout()), this, SLOT(pingClockTimeout()));
|
connect(server, SIGNAL(pingClockTimeout()), this, SLOT(pingClockTimeout()));
|
||||||
}
|
}
|
||||||
|
@ -247,7 +247,7 @@ Response::ResponseCode Server_ProtocolHandler::processSessionCommandContainer(co
|
||||||
|
|
||||||
Response::ResponseCode Server_ProtocolHandler::processRoomCommandContainer(const CommandContainer &cont, ResponseContainer &rc)
|
Response::ResponseCode Server_ProtocolHandler::processRoomCommandContainer(const CommandContainer &cont, ResponseContainer &rc)
|
||||||
{
|
{
|
||||||
if (authState == PasswordWrong)
|
if (authState == NotLoggedIn)
|
||||||
return Response::RespLoginNeeded;
|
return Response::RespLoginNeeded;
|
||||||
|
|
||||||
Server_Room *room = rooms.value(cont.room_id(), 0);
|
Server_Room *room = rooms.value(cont.room_id(), 0);
|
||||||
|
@ -282,7 +282,7 @@ Response::ResponseCode Server_ProtocolHandler::processRoomCommandContainer(const
|
||||||
|
|
||||||
Response::ResponseCode Server_ProtocolHandler::processGameCommandContainer(const CommandContainer &cont, ResponseContainer &rc)
|
Response::ResponseCode Server_ProtocolHandler::processGameCommandContainer(const CommandContainer &cont, ResponseContainer &rc)
|
||||||
{
|
{
|
||||||
if (authState == PasswordWrong)
|
if (authState == NotLoggedIn)
|
||||||
return Response::RespLoginNeeded;
|
return Response::RespLoginNeeded;
|
||||||
|
|
||||||
gameListMutex.lock();
|
gameListMutex.lock();
|
||||||
|
@ -493,11 +493,19 @@ Response::ResponseCode Server_ProtocolHandler::cmdLogin(const Command_Login &cmd
|
||||||
QString userName = QString::fromStdString(cmd.user_name()).simplified();
|
QString userName = QString::fromStdString(cmd.user_name()).simplified();
|
||||||
if (userName.isEmpty() || (userInfo != 0))
|
if (userName.isEmpty() || (userInfo != 0))
|
||||||
return Response::RespContextError;
|
return Response::RespContextError;
|
||||||
authState = server->loginUser(this, userName, QString::fromStdString(cmd.password()));
|
QString reasonStr;
|
||||||
if (authState == PasswordWrong)
|
AuthenticationResult res = server->loginUser(this, userName, QString::fromStdString(cmd.password()), reasonStr);
|
||||||
return Response::RespWrongPassword;
|
switch (res) {
|
||||||
if (authState == WouldOverwriteOldSession)
|
case UserIsBanned: {
|
||||||
return Response::RespWouldOverwriteOldSession;
|
Response_Login *re = new Response_Login;
|
||||||
|
re->set_denied_reason_str(reasonStr.toStdString());
|
||||||
|
rc.setResponseExtension(re);
|
||||||
|
return Response::RespUserIsBanned;
|
||||||
|
}
|
||||||
|
case NotLoggedIn: return Response::RespWrongPassword;
|
||||||
|
case WouldOverwriteOldSession: return Response::RespWouldOverwriteOldSession;
|
||||||
|
default: authState = res;
|
||||||
|
}
|
||||||
|
|
||||||
userName = QString::fromStdString(userInfo->name());
|
userName = QString::fromStdString(userInfo->name());
|
||||||
Event_ServerMessage event;
|
Event_ServerMessage event;
|
||||||
|
@ -505,8 +513,7 @@ Response::ResponseCode Server_ProtocolHandler::cmdLogin(const Command_Login &cmd
|
||||||
rc.enqueuePostResponseItem(ServerMessage::SESSION_EVENT, prepareSessionEvent(event));
|
rc.enqueuePostResponseItem(ServerMessage::SESSION_EVENT, prepareSessionEvent(event));
|
||||||
|
|
||||||
Response_Login *re = new Response_Login;
|
Response_Login *re = new Response_Login;
|
||||||
// XXX stimmt so nicht, beim alten Kopierkonstruktor wurde hier "true" übergeben
|
re->mutable_user_info()->CopyFrom(copyUserInfo(true));
|
||||||
re->mutable_user_info()->CopyFrom(*userInfo);
|
|
||||||
|
|
||||||
if (authState == PasswordRight) {
|
if (authState == PasswordRight) {
|
||||||
QMapIterator<QString, ServerInfo_User> buddyIterator(server->getBuddyList(userName));
|
QMapIterator<QString, ServerInfo_User> buddyIterator(server->getBuddyList(userName));
|
||||||
|
@ -569,7 +576,7 @@ Response::ResponseCode Server_ProtocolHandler::cmdLogin(const Command_Login &cmd
|
||||||
|
|
||||||
Response::ResponseCode Server_ProtocolHandler::cmdMessage(const Command_Message &cmd, ResponseContainer &rc)
|
Response::ResponseCode Server_ProtocolHandler::cmdMessage(const Command_Message &cmd, ResponseContainer &rc)
|
||||||
{
|
{
|
||||||
if (authState == PasswordWrong)
|
if (authState == NotLoggedIn)
|
||||||
return Response::RespLoginNeeded;
|
return Response::RespLoginNeeded;
|
||||||
|
|
||||||
QString receiver = QString::fromStdString(cmd.user_name());
|
QString receiver = QString::fromStdString(cmd.user_name());
|
||||||
|
@ -592,7 +599,7 @@ Response::ResponseCode Server_ProtocolHandler::cmdMessage(const Command_Message
|
||||||
|
|
||||||
Response::ResponseCode Server_ProtocolHandler::cmdGetGamesOfUser(const Command_GetGamesOfUser &cmd, ResponseContainer &rc)
|
Response::ResponseCode Server_ProtocolHandler::cmdGetGamesOfUser(const Command_GetGamesOfUser &cmd, ResponseContainer &rc)
|
||||||
{
|
{
|
||||||
if (authState == PasswordWrong)
|
if (authState == NotLoggedIn)
|
||||||
return Response::RespLoginNeeded;
|
return Response::RespLoginNeeded;
|
||||||
|
|
||||||
server->serverMutex.lock();
|
server->serverMutex.lock();
|
||||||
|
@ -618,7 +625,7 @@ Response::ResponseCode Server_ProtocolHandler::cmdGetGamesOfUser(const Command_G
|
||||||
|
|
||||||
Response::ResponseCode Server_ProtocolHandler::cmdGetUserInfo(const Command_GetUserInfo &cmd, ResponseContainer &rc)
|
Response::ResponseCode Server_ProtocolHandler::cmdGetUserInfo(const Command_GetUserInfo &cmd, ResponseContainer &rc)
|
||||||
{
|
{
|
||||||
if (authState == PasswordWrong)
|
if (authState == NotLoggedIn)
|
||||||
return Response::RespLoginNeeded;
|
return Response::RespLoginNeeded;
|
||||||
|
|
||||||
QString userName = QString::fromStdString(cmd.user_name());
|
QString userName = QString::fromStdString(cmd.user_name());
|
||||||
|
@ -638,7 +645,7 @@ Response::ResponseCode Server_ProtocolHandler::cmdGetUserInfo(const Command_GetU
|
||||||
|
|
||||||
Response::ResponseCode Server_ProtocolHandler::cmdListRooms(const Command_ListRooms & /*cmd*/, ResponseContainer &rc)
|
Response::ResponseCode Server_ProtocolHandler::cmdListRooms(const Command_ListRooms & /*cmd*/, ResponseContainer &rc)
|
||||||
{
|
{
|
||||||
if (authState == PasswordWrong)
|
if (authState == NotLoggedIn)
|
||||||
return Response::RespLoginNeeded;
|
return Response::RespLoginNeeded;
|
||||||
|
|
||||||
Event_ListRooms event;
|
Event_ListRooms event;
|
||||||
|
@ -653,7 +660,7 @@ Response::ResponseCode Server_ProtocolHandler::cmdListRooms(const Command_ListRo
|
||||||
|
|
||||||
Response::ResponseCode Server_ProtocolHandler::cmdJoinRoom(const Command_JoinRoom &cmd, ResponseContainer &rc)
|
Response::ResponseCode Server_ProtocolHandler::cmdJoinRoom(const Command_JoinRoom &cmd, ResponseContainer &rc)
|
||||||
{
|
{
|
||||||
if (authState == PasswordWrong)
|
if (authState == NotLoggedIn)
|
||||||
return Response::RespLoginNeeded;
|
return Response::RespLoginNeeded;
|
||||||
|
|
||||||
if (rooms.contains(cmd.room_id()))
|
if (rooms.contains(cmd.room_id()))
|
||||||
|
@ -681,7 +688,7 @@ Response::ResponseCode Server_ProtocolHandler::cmdJoinRoom(const Command_JoinRoo
|
||||||
|
|
||||||
Response::ResponseCode Server_ProtocolHandler::cmdListUsers(const Command_ListUsers & /*cmd*/, ResponseContainer &rc)
|
Response::ResponseCode Server_ProtocolHandler::cmdListUsers(const Command_ListUsers & /*cmd*/, ResponseContainer &rc)
|
||||||
{
|
{
|
||||||
if (authState == PasswordWrong)
|
if (authState == NotLoggedIn)
|
||||||
return Response::RespLoginNeeded;
|
return Response::RespLoginNeeded;
|
||||||
|
|
||||||
Response_ListUsers *re = new Response_ListUsers;
|
Response_ListUsers *re = new Response_ListUsers;
|
||||||
|
@ -731,7 +738,7 @@ Response::ResponseCode Server_ProtocolHandler::cmdRoomSay(const Command_RoomSay
|
||||||
|
|
||||||
Response::ResponseCode Server_ProtocolHandler::cmdCreateGame(const Command_CreateGame &cmd, Server_Room *room, ResponseContainer &rc)
|
Response::ResponseCode Server_ProtocolHandler::cmdCreateGame(const Command_CreateGame &cmd, Server_Room *room, ResponseContainer &rc)
|
||||||
{
|
{
|
||||||
if (authState == PasswordWrong)
|
if (authState == NotLoggedIn)
|
||||||
return Response::RespLoginNeeded;
|
return Response::RespLoginNeeded;
|
||||||
|
|
||||||
if (server->getMaxGamesPerUser() > 0)
|
if (server->getMaxGamesPerUser() > 0)
|
||||||
|
@ -779,7 +786,7 @@ Response::ResponseCode Server_ProtocolHandler::cmdCreateGame(const Command_Creat
|
||||||
|
|
||||||
Response::ResponseCode Server_ProtocolHandler::cmdJoinGame(const Command_JoinGame &cmd, Server_Room *room, ResponseContainer &rc)
|
Response::ResponseCode Server_ProtocolHandler::cmdJoinGame(const Command_JoinGame &cmd, Server_Room *room, ResponseContainer &rc)
|
||||||
{
|
{
|
||||||
if (authState == PasswordWrong)
|
if (authState == NotLoggedIn)
|
||||||
return Response::RespLoginNeeded;
|
return Response::RespLoginNeeded;
|
||||||
|
|
||||||
QMutexLocker gameListLocker(&gameListMutex);
|
QMutexLocker gameListLocker(&gameListMutex);
|
||||||
|
|
|
@ -9,8 +9,9 @@ INCLUDEPATH += . src ../common
|
||||||
MOC_DIR = build
|
MOC_DIR = build
|
||||||
OBJECTS_DIR = build
|
OBJECTS_DIR = build
|
||||||
LIBS += -lgcrypt -lprotobuf
|
LIBS += -lgcrypt -lprotobuf
|
||||||
|
QMAKE_CXXFLAGS_RELEASE += -O2
|
||||||
|
|
||||||
CONFIG += qt debug
|
CONFIG += qt
|
||||||
QT += network sql
|
QT += network sql
|
||||||
QT -= gui
|
QT -= gui
|
||||||
|
|
||||||
|
|
|
@ -157,6 +157,7 @@ CREATE TABLE `cockatrice_bans` (
|
||||||
`time_from` datetime NOT NULL,
|
`time_from` datetime NOT NULL,
|
||||||
`minutes` int(6) NOT NULL,
|
`minutes` int(6) NOT NULL,
|
||||||
`reason` text NOT NULL,
|
`reason` text NOT NULL,
|
||||||
|
`visible_reason` text NOT NULL,
|
||||||
PRIMARY KEY (`user_name`,`time_from`),
|
PRIMARY KEY (`user_name`,`time_from`),
|
||||||
KEY `time_from` (`time_from`,`ip_address`)
|
KEY `time_from` (`time_from`,`ip_address`)
|
||||||
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
|
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
|
||||||
|
|
|
@ -167,7 +167,7 @@ bool Servatrice::execSqlQuery(QSqlQuery &query)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
AuthenticationResult Servatrice::checkUserPassword(Server_ProtocolHandler *handler, const QString &user, const QString &password)
|
AuthenticationResult Servatrice::checkUserPassword(Server_ProtocolHandler *handler, const QString &user, const QString &password, QString &reasonStr)
|
||||||
{
|
{
|
||||||
QMutexLocker locker(&dbMutex);
|
QMutexLocker locker(&dbMutex);
|
||||||
const QString method = settings->value("authentication/method").toString();
|
const QString method = settings->value("authentication/method").toString();
|
||||||
|
@ -177,33 +177,35 @@ AuthenticationResult Servatrice::checkUserPassword(Server_ProtocolHandler *handl
|
||||||
checkSql();
|
checkSql();
|
||||||
|
|
||||||
QSqlQuery ipBanQuery;
|
QSqlQuery ipBanQuery;
|
||||||
ipBanQuery.prepare("select time_to_sec(timediff(now(), date_add(b.time_from, interval b.minutes minute))) < 0, b.minutes <=> 0 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))) < 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.bindValue(":address", static_cast<ServerSocketInterface *>(handler)->getPeerAddress().toString());
|
ipBanQuery.bindValue(":address", static_cast<ServerSocketInterface *>(handler)->getPeerAddress().toString());
|
||||||
ipBanQuery.bindValue(":address2", static_cast<ServerSocketInterface *>(handler)->getPeerAddress().toString());
|
ipBanQuery.bindValue(":address2", static_cast<ServerSocketInterface *>(handler)->getPeerAddress().toString());
|
||||||
if (!execSqlQuery(ipBanQuery)) {
|
if (!execSqlQuery(ipBanQuery)) {
|
||||||
qDebug("Login denied: SQL error");
|
qDebug("Login denied: SQL error");
|
||||||
return PasswordWrong;
|
return NotLoggedIn;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ipBanQuery.next())
|
if (ipBanQuery.next())
|
||||||
if (ipBanQuery.value(0).toInt() || ipBanQuery.value(1).toInt()) {
|
if (ipBanQuery.value(0).toInt() || ipBanQuery.value(1).toInt()) {
|
||||||
|
reasonStr = ipBanQuery.value(2).toString();
|
||||||
qDebug("Login denied: banned by address");
|
qDebug("Login denied: banned by address");
|
||||||
return PasswordWrong;
|
return UserIsBanned;
|
||||||
}
|
}
|
||||||
|
|
||||||
QSqlQuery nameBanQuery;
|
QSqlQuery nameBanQuery;
|
||||||
nameBanQuery.prepare("select time_to_sec(timediff(now(), date_add(b.time_from, interval b.minutes minute))) < 0, b.minutes <=> 0 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))) < 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.bindValue(":name1", user);
|
nameBanQuery.bindValue(":name1", user);
|
||||||
nameBanQuery.bindValue(":name2", user);
|
nameBanQuery.bindValue(":name2", user);
|
||||||
if (!execSqlQuery(nameBanQuery)) {
|
if (!execSqlQuery(nameBanQuery)) {
|
||||||
qDebug("Login denied: SQL error");
|
qDebug("Login denied: SQL error");
|
||||||
return PasswordWrong;
|
return NotLoggedIn;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nameBanQuery.next())
|
if (nameBanQuery.next())
|
||||||
if (nameBanQuery.value(0).toInt() || nameBanQuery.value(1).toInt()) {
|
if (nameBanQuery.value(0).toInt() || nameBanQuery.value(1).toInt()) {
|
||||||
|
reasonStr = nameBanQuery.value(2).toString();
|
||||||
qDebug("Login denied: banned by name");
|
qDebug("Login denied: banned by name");
|
||||||
return PasswordWrong;
|
return UserIsBanned;
|
||||||
}
|
}
|
||||||
|
|
||||||
QSqlQuery passwordQuery;
|
QSqlQuery passwordQuery;
|
||||||
|
@ -211,7 +213,7 @@ AuthenticationResult Servatrice::checkUserPassword(Server_ProtocolHandler *handl
|
||||||
passwordQuery.bindValue(":name", user);
|
passwordQuery.bindValue(":name", user);
|
||||||
if (!execSqlQuery(passwordQuery)) {
|
if (!execSqlQuery(passwordQuery)) {
|
||||||
qDebug("Login denied: SQL error");
|
qDebug("Login denied: SQL error");
|
||||||
return PasswordWrong;
|
return NotLoggedIn;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (passwordQuery.next()) {
|
if (passwordQuery.next()) {
|
||||||
|
@ -221,7 +223,7 @@ AuthenticationResult Servatrice::checkUserPassword(Server_ProtocolHandler *handl
|
||||||
return PasswordRight;
|
return PasswordRight;
|
||||||
} else {
|
} else {
|
||||||
qDebug("Login denied: password wrong");
|
qDebug("Login denied: password wrong");
|
||||||
return PasswordWrong;
|
return NotLoggedIn;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
qDebug("Login accepted: unknown user");
|
qDebug("Login accepted: unknown user");
|
||||||
|
@ -527,7 +529,7 @@ void Servatrice::shutdownTimeout()
|
||||||
se = Server_ProtocolHandler::prepareSessionEvent(event);
|
se = Server_ProtocolHandler::prepareSessionEvent(event);
|
||||||
} else {
|
} else {
|
||||||
Event_ConnectionClosed event;
|
Event_ConnectionClosed event;
|
||||||
event.set_reason("server_shutdown");
|
event.set_reason(Event_ConnectionClosed::SERVER_SHUTDOWN);
|
||||||
se = Server_ProtocolHandler::prepareSessionEvent(event);
|
se = Server_ProtocolHandler::prepareSessionEvent(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -85,7 +85,7 @@ protected:
|
||||||
int startSession(const QString &userName, const QString &address);
|
int startSession(const QString &userName, const QString &address);
|
||||||
void endSession(int sessionId);
|
void endSession(int sessionId);
|
||||||
bool userExists(const QString &user);
|
bool userExists(const QString &user);
|
||||||
AuthenticationResult checkUserPassword(Server_ProtocolHandler *handler, const QString &user, const QString &password);
|
AuthenticationResult checkUserPassword(Server_ProtocolHandler *handler, const QString &user, const QString &password, QString &reasonStr);
|
||||||
private:
|
private:
|
||||||
QTimer *pingClock, *statusUpdateClock;
|
QTimer *pingClock, *statusUpdateClock;
|
||||||
QTcpServer *tcpServer;
|
QTcpServer *tcpServer;
|
||||||
|
|
|
@ -65,7 +65,7 @@ ServerSocketInterface::ServerSocketInterface(Servatrice *_server, QTcpSocket *_s
|
||||||
int maxUsers = _server->getMaxUsersPerAddress();
|
int maxUsers = _server->getMaxUsersPerAddress();
|
||||||
if ((maxUsers > 0) && (_server->getUsersWithAddress(socket->peerAddress()) >= maxUsers)) {
|
if ((maxUsers > 0) && (_server->getUsersWithAddress(socket->peerAddress()) >= maxUsers)) {
|
||||||
Event_ConnectionClosed event;
|
Event_ConnectionClosed event;
|
||||||
event.set_reason("too_many_connections");
|
event.set_reason(Event_ConnectionClosed::TOO_MANY_CONNECTIONS);
|
||||||
SessionEvent *se = prepareSessionEvent(event);
|
SessionEvent *se = prepareSessionEvent(event);
|
||||||
sendProtocolItem(*se);
|
sendProtocolItem(*se);
|
||||||
delete se;
|
delete se;
|
||||||
|
@ -482,19 +482,22 @@ Response::ResponseCode ServerSocketInterface::cmdBanFromServer(const Command_Ban
|
||||||
|
|
||||||
servatrice->dbMutex.lock();
|
servatrice->dbMutex.lock();
|
||||||
QSqlQuery query;
|
QSqlQuery query;
|
||||||
query.prepare("insert into " + servatrice->getDbPrefix() + "_bans (user_name, ip_address, id_admin, time_from, minutes, reason) values(:user_name, :ip_address, :id_admin, NOW(), :minutes, :reason)");
|
query.prepare("insert into " + servatrice->getDbPrefix() + "_bans (user_name, ip_address, id_admin, time_from, minutes, reason, visible_reason) values(:user_name, :ip_address, :id_admin, NOW(), :minutes, :reason, :visible_reason)");
|
||||||
query.bindValue(":user_name", userName);
|
query.bindValue(":user_name", userName);
|
||||||
query.bindValue(":ip_address", address);
|
query.bindValue(":ip_address", address);
|
||||||
query.bindValue(":id_admin", servatrice->getUserIdInDB(QString::fromStdString(userInfo->name())));
|
query.bindValue(":id_admin", servatrice->getUserIdInDB(QString::fromStdString(userInfo->name())));
|
||||||
query.bindValue(":minutes", minutes);
|
query.bindValue(":minutes", minutes);
|
||||||
query.bindValue(":reason", QString::fromStdString(cmd.reason()) + "\n");
|
query.bindValue(":reason", QString::fromStdString(cmd.reason()) + "\n");
|
||||||
|
query.bindValue(":visible_reason", QString::fromStdString(cmd.visible_reason()) + "\n");
|
||||||
servatrice->execSqlQuery(query);
|
servatrice->execSqlQuery(query);
|
||||||
servatrice->dbMutex.unlock();
|
servatrice->dbMutex.unlock();
|
||||||
|
|
||||||
ServerSocketInterface *user = static_cast<ServerSocketInterface *>(server->getUsers().value(userName));
|
ServerSocketInterface *user = static_cast<ServerSocketInterface *>(server->getUsers().value(userName));
|
||||||
if (user) {
|
if (user) {
|
||||||
Event_ConnectionClosed event;
|
Event_ConnectionClosed event;
|
||||||
event.set_reason("banned");
|
event.set_reason(Event_ConnectionClosed::BANNED);
|
||||||
|
if (cmd.has_visible_reason())
|
||||||
|
event.set_reason_str(cmd.visible_reason());
|
||||||
SessionEvent *se = user->prepareSessionEvent(event);
|
SessionEvent *se = user->prepareSessionEvent(event);
|
||||||
user->sendProtocolItem(*se);
|
user->sendProtocolItem(*se);
|
||||||
delete se;
|
delete se;
|
||||||
|
|
Loading…
Reference in a new issue