user banning

This commit is contained in:
Max-Wilhelm Bruker 2011-03-03 01:16:13 +01:00
parent 6145d6d524
commit 57f9e2c3b4
27 changed files with 220 additions and 37 deletions

View file

@ -32,6 +32,7 @@ void AbstractClient::processProtocolItem(ProtocolItem *item)
GenericEvent *genericEvent = qobject_cast<GenericEvent *>(item); GenericEvent *genericEvent = qobject_cast<GenericEvent *>(item);
if (genericEvent) { if (genericEvent) {
switch (genericEvent->getItemId()) { switch (genericEvent->getItemId()) {
case ItemId_Event_ConnectionClosed: emit connectionClosedEventReceived(qobject_cast<Event_ConnectionClosed *>(item)); break;
case ItemId_Event_AddToList: emit addToListEventReceived(qobject_cast<Event_AddToList *>(item)); break; case ItemId_Event_AddToList: emit addToListEventReceived(qobject_cast<Event_AddToList *>(item)); break;
case ItemId_Event_RemoveFromList: emit removeFromListEventReceived(qobject_cast<Event_RemoveFromList *>(item)); break; case ItemId_Event_RemoveFromList: emit removeFromListEventReceived(qobject_cast<Event_RemoveFromList *>(item)); break;
case ItemId_Event_UserJoined: emit userJoinedEventReceived(qobject_cast<Event_UserJoined *>(item)); break; case ItemId_Event_UserJoined: emit userJoinedEventReceived(qobject_cast<Event_UserJoined *>(item)); break;
@ -63,7 +64,6 @@ void AbstractClient::processProtocolItem(ProtocolItem *item)
} }
} }
void AbstractClient::setStatus(const ClientStatus _status) void AbstractClient::setStatus(const ClientStatus _status)
{ {
if (_status != status) { if (_status != status) {

View file

@ -20,6 +20,7 @@ class Event_ServerMessage;
class Event_ListRooms; class Event_ListRooms;
class Event_GameJoined; class Event_GameJoined;
class Event_Message; class Event_Message;
class Event_ConnectionClosed;
enum ClientStatus { enum ClientStatus {
StatusDisconnected, StatusDisconnected,
@ -41,6 +42,7 @@ signals:
// Game events // Game events
void gameEventContainerReceived(GameEventContainer *event); void gameEventContainerReceived(GameEventContainer *event);
// Generic events // Generic events
void connectionClosedEventReceived(Event_ConnectionClosed *event);
void addToListEventReceived(Event_AddToList *event); void addToListEventReceived(Event_AddToList *event);
void removeFromListEventReceived(Event_RemoveFromList *event); void removeFromListEventReceived(Event_RemoveFromList *event);
void userJoinedEventReceived(Event_UserJoined *event); void userJoinedEventReceived(Event_UserJoined *event);

View file

@ -19,6 +19,7 @@ private:
ResponseCode cmdDeckUpload(Command_DeckUpload * /*cmd*/, CommandContainer * /*cont*/) { return RespFunctionNotAllowed; } ResponseCode cmdDeckUpload(Command_DeckUpload * /*cmd*/, CommandContainer * /*cont*/) { return RespFunctionNotAllowed; }
ResponseCode cmdDeckDownload(Command_DeckDownload * /*cmd*/, CommandContainer * /*cont*/) { return RespFunctionNotAllowed; } ResponseCode cmdDeckDownload(Command_DeckDownload * /*cmd*/, CommandContainer * /*cont*/) { return RespFunctionNotAllowed; }
ResponseCode cmdUpdateServerMessage(Command_UpdateServerMessage * /*cmd*/, CommandContainer * /*cont*/) { return RespFunctionNotAllowed; } ResponseCode cmdUpdateServerMessage(Command_UpdateServerMessage * /*cmd*/, CommandContainer * /*cont*/) { return RespFunctionNotAllowed; }
ResponseCode cmdBanFromServer(Command_BanFromServer * /*cmd*/, CommandContainer * /*cont*/) { return RespFunctionNotAllowed; }
public: public:
LocalServerInterface(LocalServer *_server); LocalServerInterface(LocalServer *_server);
~LocalServerInterface(); ~LocalServerInterface();

View file

@ -8,7 +8,7 @@ extern CardDatabase *db;
extern QTranslator *translator; extern QTranslator *translator;
const QString translationPrefix = "cockatrice"; const QString translationPrefix = "cockatrice";
const QString versionString = "0.20110123"; const QString versionString = "0.20110303";
void installNewTranslator(); void installNewTranslator();

View file

@ -31,8 +31,9 @@ RemoteClient::~RemoteClient()
void RemoteClient::slotSocketError(QAbstractSocket::SocketError /*error*/) void RemoteClient::slotSocketError(QAbstractSocket::SocketError /*error*/)
{ {
emit socketError(socket->errorString()); QString errorString = socket->errorString();
disconnectFromServer(); disconnectFromServer();
emit socketError(errorString);
} }
void RemoteClient::slotConnected() void RemoteClient::slotConnected()

View file

@ -7,7 +7,7 @@
#include "protocol_items.h" #include "protocol_items.h"
TabAdmin::TabAdmin(TabSupervisor *_tabSupervisor, AbstractClient *_client, QWidget *parent) TabAdmin::TabAdmin(TabSupervisor *_tabSupervisor, AbstractClient *_client, QWidget *parent)
: Tab(_tabSupervisor, parent), client(_client) : Tab(_tabSupervisor, parent), locked(true), client(_client)
{ {
updateServerMessageButton = new QPushButton; updateServerMessageButton = new QPushButton;
connect(updateServerMessageButton, SIGNAL(clicked()), this, SLOT(actUpdateServerMessage())); connect(updateServerMessageButton, SIGNAL(clicked()), this, SLOT(actUpdateServerMessage()));
@ -55,6 +55,7 @@ void TabAdmin::actUnlock()
adminGroupBox->setEnabled(true); adminGroupBox->setEnabled(true);
lockButton->setEnabled(true); lockButton->setEnabled(true);
unlockButton->setEnabled(false); unlockButton->setEnabled(false);
locked = false;
} }
} }
@ -63,4 +64,5 @@ void TabAdmin::actLock()
adminGroupBox->setEnabled(false); adminGroupBox->setEnabled(false);
lockButton->setEnabled(false); lockButton->setEnabled(false);
unlockButton->setEnabled(true); unlockButton->setEnabled(true);
locked = true;
} }

View file

@ -11,6 +11,7 @@ class QPushButton;
class TabAdmin : public Tab { class TabAdmin : public Tab {
Q_OBJECT Q_OBJECT
private: private:
bool locked;
AbstractClient *client; AbstractClient *client;
QPushButton *updateServerMessageButton; QPushButton *updateServerMessageButton;
QGroupBox *adminGroupBox; QGroupBox *adminGroupBox;
@ -24,6 +25,7 @@ public:
TabAdmin(TabSupervisor *_tabSupervisor, AbstractClient *_client, QWidget *parent = 0); TabAdmin(TabSupervisor *_tabSupervisor, AbstractClient *_client, QWidget *parent = 0);
void retranslateUi(); void retranslateUi();
QString getTabText() const { return tr("Administration"); } QString getTabText() const { return tr("Administration"); }
bool getLocked() const { return locked; }
}; };
#endif #endif

View file

@ -132,7 +132,7 @@ TabRoom::TabRoom(TabSupervisor *_tabSupervisor, AbstractClient *_client, const Q
gameTypes.insert(gameTypeList[i]->getGameTypeId(), gameTypeList[i]->getDescription()); gameTypes.insert(gameTypeList[i]->getGameTypeId(), gameTypeList[i]->getDescription());
gameSelector = new GameSelector(client, this); gameSelector = new GameSelector(client, this);
userList = new UserList(tabSupervisor->getUserListsTab(), client, UserList::RoomList); userList = new UserList(tabSupervisor, client, UserList::RoomList);
connect(userList, SIGNAL(openMessageDialog(const QString &, bool)), this, SIGNAL(openMessageDialog(const QString &, bool))); connect(userList, SIGNAL(openMessageDialog(const QString &, bool)), this, SIGNAL(openMessageDialog(const QString &, bool)));
chatView = new ChatView(ownName); chatView = new ChatView(ownName);
@ -205,10 +205,18 @@ void TabRoom::sendMessage()
if (sayEdit->text().isEmpty()) if (sayEdit->text().isEmpty())
return; return;
client->sendCommand(new Command_RoomSay(roomId, sayEdit->text())); Command_RoomSay *cmd = new Command_RoomSay(roomId, sayEdit->text());
connect(cmd, SIGNAL(finished(ProtocolResponse *)), this, SLOT(sayFinished(ProtocolResponse *)));
client->sendCommand(cmd);
sayEdit->clear(); sayEdit->clear();
} }
void TabRoom::sayFinished(ProtocolResponse *response)
{
if (response->getResponseCode() == RespChatFlood)
chatView->appendMessage(QString(), tr("You are flooding the chat. Please wait a couple of seconds."));
}
void TabRoom::actLeaveRoom() void TabRoom::actLeaveRoom()
{ {
client->sendCommand(new Command_LeaveRoom(roomId)); client->sendCommand(new Command_LeaveRoom(roomId));

View file

@ -23,6 +23,7 @@ class Event_ListGames;
class Event_JoinRoom; class Event_JoinRoom;
class Event_LeaveRoom; class Event_LeaveRoom;
class Event_RoomSay; class Event_RoomSay;
class ProtocolResponse;
class TabRoom; class TabRoom;
class GameSelector : public QGroupBox { class GameSelector : public QGroupBox {
@ -73,6 +74,7 @@ signals:
private slots: private slots:
void sendMessage(); void sendMessage();
void actLeaveRoom(); void actLeaveRoom();
void sayFinished(ProtocolResponse *response);
void processListGamesEvent(Event_ListGames *event); void processListGamesEvent(Event_ListGames *event);
void processJoinRoomEvent(Event_JoinRoom *event); void processJoinRoomEvent(Event_JoinRoom *event);

View file

@ -291,3 +291,10 @@ void TabSupervisor::updateCurrent(int index)
} else } else
emit setMenu(0); emit setMenu(0);
} }
bool TabSupervisor::getAdminLocked() const
{
if (!tabAdmin)
return true;
return tabAdmin->getLocked();
}

View file

@ -45,6 +45,7 @@ public:
void stop(); void stop();
int getGameCount() const { return gameTabs.size(); } int getGameCount() const { return gameTabs.size(); }
TabUserLists *getUserListsTab() const { return tabUserLists; } TabUserLists *getUserListsTab() const { return tabUserLists; }
bool getAdminLocked() const;
signals: signals:
void setMenu(QMenu *menu); void setMenu(QMenu *menu);
void localGameEnded(); void localGameEnded();

View file

@ -10,9 +10,9 @@
TabUserLists::TabUserLists(TabSupervisor *_tabSupervisor, AbstractClient *_client, ServerInfo_User *userInfo, QWidget *parent) TabUserLists::TabUserLists(TabSupervisor *_tabSupervisor, AbstractClient *_client, ServerInfo_User *userInfo, QWidget *parent)
: Tab(_tabSupervisor, parent), client(_client) : Tab(_tabSupervisor, parent), client(_client)
{ {
allUsersList = new UserList(this, client, UserList::AllUsersList); allUsersList = new UserList(_tabSupervisor, client, UserList::AllUsersList);
buddyList = new UserList(this, client, UserList::BuddyList); buddyList = new UserList(_tabSupervisor, client, UserList::BuddyList);
ignoreList = new UserList(this, client, UserList::IgnoreList); ignoreList = new UserList(_tabSupervisor, client, UserList::IgnoreList);
userInfoBox = new UserInfoBox(client, false); userInfoBox = new UserInfoBox(client, false);
userInfoBox->updateInfo(userInfo); userInfoBox->updateInfo(userInfo);

View file

@ -1,5 +1,6 @@
#include "userlist.h" #include "userlist.h"
#include "tab_userlists.h" #include "tab_userlists.h"
#include "tab_supervisor.h"
#include "abstractclient.h" #include "abstractclient.h"
#include "pixmapgenerator.h" #include "pixmapgenerator.h"
#include "userinfobox.h" #include "userinfobox.h"
@ -8,6 +9,7 @@
#include <QVBoxLayout> #include <QVBoxLayout>
#include <QMouseEvent> #include <QMouseEvent>
#include <QMenu> #include <QMenu>
#include <QInputDialog>
UserListItemDelegate::UserListItemDelegate(QObject *const parent) UserListItemDelegate::UserListItemDelegate(QObject *const parent)
: QStyledItemDelegate(parent) : QStyledItemDelegate(parent)
@ -45,8 +47,8 @@ bool UserListTWI::operator<(const QTreeWidgetItem &other) const
return data(2, Qt::UserRole).toString().toLower() < other.data(2, Qt::UserRole).toString().toLower(); return data(2, Qt::UserRole).toString().toLower() < other.data(2, Qt::UserRole).toString().toLower();
} }
UserList::UserList(TabUserLists *_tabUserLists, AbstractClient *_client, UserListType _type, QWidget *parent) UserList::UserList(TabSupervisor *_tabSupervisor, AbstractClient *_client, UserListType _type, QWidget *parent)
: QGroupBox(parent), tabUserLists(_tabUserLists), client(_client), type(_type), onlineCount(0) : QGroupBox(parent), tabSupervisor(_tabSupervisor), client(_client), type(_type), onlineCount(0)
{ {
itemDelegate = new UserListItemDelegate(this); itemDelegate = new UserListItemDelegate(this);
@ -173,6 +175,7 @@ void UserList::showContextMenu(const QPoint &pos, const QModelIndex &index)
QAction *aRemoveFromBuddyList = new QAction(tr("Remove from &buddy list"), this); QAction *aRemoveFromBuddyList = new QAction(tr("Remove from &buddy list"), this);
QAction *aAddToIgnoreList = new QAction(tr("Add to &ignore list"), this); QAction *aAddToIgnoreList = new QAction(tr("Add to &ignore list"), this);
QAction *aRemoveFromIgnoreList = new QAction(tr("Remove from &ignore list"), this); QAction *aRemoveFromIgnoreList = new QAction(tr("Remove from &ignore list"), this);
QAction *aBan = new QAction(tr("Ban from &server"), this);
QMenu *menu = new QMenu(this); QMenu *menu = new QMenu(this);
menu->addAction(aUserName); menu->addAction(aUserName);
@ -180,14 +183,18 @@ void UserList::showContextMenu(const QPoint &pos, const QModelIndex &index)
menu->addAction(aDetails); menu->addAction(aDetails);
menu->addAction(aChat); menu->addAction(aChat);
menu->addSeparator(); menu->addSeparator();
if (tabUserLists->getBuddyList()->userInList(userName)) if (tabSupervisor->getUserListsTab()->getBuddyList()->userInList(userName))
menu->addAction(aRemoveFromBuddyList); menu->addAction(aRemoveFromBuddyList);
else else
menu->addAction(aAddToBuddyList); menu->addAction(aAddToBuddyList);
if (tabUserLists->getIgnoreList()->userInList(userName)) if (tabSupervisor->getUserListsTab()->getIgnoreList()->userInList(userName))
menu->addAction(aRemoveFromIgnoreList); menu->addAction(aRemoveFromIgnoreList);
else else
menu->addAction(aAddToIgnoreList); menu->addAction(aAddToIgnoreList);
if (!tabSupervisor->getAdminLocked()) {
menu->addSeparator();
menu->addAction(aBan);
}
QAction *actionClicked = menu->exec(pos); QAction *actionClicked = menu->exec(pos);
if (actionClicked == aDetails) { if (actionClicked == aDetails) {
@ -204,6 +211,11 @@ void UserList::showContextMenu(const QPoint &pos, const QModelIndex &index)
client->sendCommand(new Command_AddToList("ignore", userName)); client->sendCommand(new Command_AddToList("ignore", userName));
else if (actionClicked == aRemoveFromIgnoreList) else if (actionClicked == aRemoveFromIgnoreList)
client->sendCommand(new Command_RemoveFromList("ignore", userName)); client->sendCommand(new Command_RemoveFromList("ignore", userName));
else if (actionClicked == aBan) {
bool ok;
int minutes = QInputDialog::getInt(this, tr("Duration"), tr("Please enter the duration of the ban (in minutes).\nEnter 0 for an indefinite ban."), 0, 0, 2147483647, 10, &ok);
client->sendCommand(new Command_BanFromServer(userName, minutes));
}
delete menu; delete menu;
delete aUserName; delete aUserName;
@ -213,6 +225,7 @@ void UserList::showContextMenu(const QPoint &pos, const QModelIndex &index)
delete aRemoveFromBuddyList; delete aRemoveFromBuddyList;
delete aAddToIgnoreList; delete aAddToIgnoreList;
delete aRemoveFromIgnoreList; delete aRemoveFromIgnoreList;
delete aBan;
} }
bool UserList::userInList(const QString &userName) const bool UserList::userInList(const QString &userName) const

View file

@ -8,7 +8,7 @@
class QTreeWidget; class QTreeWidget;
class ServerInfo_User; class ServerInfo_User;
class AbstractClient; class AbstractClient;
class TabUserLists; class TabSupervisor;
class UserListItemDelegate : public QStyledItemDelegate { class UserListItemDelegate : public QStyledItemDelegate {
public: public:
@ -27,7 +27,7 @@ class UserList : public QGroupBox {
public: public:
enum UserListType { AllUsersList, RoomList, BuddyList, IgnoreList }; enum UserListType { AllUsersList, RoomList, BuddyList, IgnoreList };
private: private:
TabUserLists *tabUserLists; TabSupervisor *tabSupervisor;
AbstractClient *client; AbstractClient *client;
UserListType type; UserListType type;
QTreeWidget *userTree; QTreeWidget *userTree;
@ -45,7 +45,7 @@ signals:
void addIgnore(const QString &userName); void addIgnore(const QString &userName);
void removeIgnore(const QString &userName); void removeIgnore(const QString &userName);
public: public:
UserList(TabUserLists *_tabUserLists, AbstractClient *_client, UserListType _type, QWidget *parent = 0); UserList(TabSupervisor *_tabSupervisor, AbstractClient *_client, UserListType _type, QWidget *parent = 0);
void retranslateUi(); void retranslateUi();
void processUserInfo(ServerInfo_User *user, bool online); void processUserInfo(ServerInfo_User *user, bool online);
bool deleteUser(const QString &userName); bool deleteUser(const QString &userName);

View file

@ -47,6 +47,20 @@ void MainWindow::updateTabMenu(QMenu *menu)
menuBar()->insertMenu(helpMenu->menuAction(), menu); menuBar()->insertMenu(helpMenu->menuAction(), menu);
} }
void MainWindow::processConnectionClosedEvent(Event_ConnectionClosed *event)
{
QString reason = event->getReason();
client->disconnectFromServer();
QString reasonStr;
if (reason == "too_many_connections")
reasonStr = tr("There are too many concurrent connections from your address.");
else if (reason == "banned")
reasonStr = tr("Banned by moderator.");
else
reasonStr = tr("Unknown reason.");
QMessageBox::critical(this, tr("Connection closed"), tr("The server has terminated your connection.\nReason: %1").arg(reasonStr));
}
void MainWindow::statusChanged(ClientStatus _status) void MainWindow::statusChanged(ClientStatus _status)
{ {
setClientStatusTitle(); setClientStatusTitle();
@ -273,6 +287,7 @@ MainWindow::MainWindow(QWidget *parent)
QPixmapCache::setCacheLimit(200000); QPixmapCache::setCacheLimit(200000);
client = new RemoteClient(this); client = new RemoteClient(this);
connect(client, SIGNAL(connectionClosedEventReceived(Event_ConnectionClosed *)), this, SLOT(processConnectionClosedEvent(Event_ConnectionClosed *)));
connect(client, SIGNAL(serverError(ResponseCode)), this, SLOT(serverError(ResponseCode))); connect(client, SIGNAL(serverError(ResponseCode)), this, SLOT(serverError(ResponseCode)));
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()));

View file

@ -35,6 +35,7 @@ class MainWindow : public QMainWindow {
private slots: private slots:
void updateTabMenu(QMenu *menu); void updateTabMenu(QMenu *menu);
void statusChanged(ClientStatus _status); void statusChanged(ClientStatus _status);
void processConnectionClosedEvent(Event_ConnectionClosed *event);
void serverTimeout(); void serverTimeout();
void serverError(ResponseCode r); void serverError(ResponseCode r);
void socketError(const QString &errorStr); void socketError(const QString &errorStr);

View file

@ -63,16 +63,18 @@ ItemId_Event_DumpZone = 1061,
ItemId_Event_StopDumpZone = 1062, ItemId_Event_StopDumpZone = 1062,
ItemId_Event_RemoveFromList = 1063, ItemId_Event_RemoveFromList = 1063,
ItemId_Event_ServerMessage = 1064, ItemId_Event_ServerMessage = 1064,
ItemId_Event_Message = 1065, ItemId_Event_ConnectionClosed = 1065,
ItemId_Event_GameJoined = 1066, ItemId_Event_Message = 1066,
ItemId_Event_UserLeft = 1067, ItemId_Event_GameJoined = 1067,
ItemId_Event_LeaveRoom = 1068, ItemId_Event_UserLeft = 1068,
ItemId_Event_RoomSay = 1069, ItemId_Event_LeaveRoom = 1069,
ItemId_Context_ReadyStart = 1070, ItemId_Event_RoomSay = 1070,
ItemId_Context_Concede = 1071, ItemId_Context_ReadyStart = 1071,
ItemId_Context_DeckSelect = 1072, ItemId_Context_Concede = 1072,
ItemId_Context_UndoDraw = 1073, ItemId_Context_DeckSelect = 1073,
ItemId_Context_MoveCard = 1074, ItemId_Context_UndoDraw = 1074,
ItemId_Command_UpdateServerMessage = 1075, ItemId_Context_MoveCard = 1075,
ItemId_Other = 1076 ItemId_Command_UpdateServerMessage = 1076,
ItemId_Command_BanFromServer = 1077,
ItemId_Other = 1078
}; };

View file

@ -389,6 +389,11 @@ Event_ServerMessage::Event_ServerMessage(const QString &_message)
{ {
insertItem(new SerializableItem_String("message", _message)); insertItem(new SerializableItem_String("message", _message));
} }
Event_ConnectionClosed::Event_ConnectionClosed(const QString &_reason)
: GenericEvent("connection_closed")
{
insertItem(new SerializableItem_String("reason", _reason));
}
Event_Message::Event_Message(const QString &_senderName, const QString &_receiverName, const QString &_text) Event_Message::Event_Message(const QString &_senderName, const QString &_receiverName, const QString &_text)
: GenericEvent("message") : GenericEvent("message")
{ {
@ -448,6 +453,12 @@ Command_UpdateServerMessage::Command_UpdateServerMessage()
: AdminCommand("update_server_message") : AdminCommand("update_server_message")
{ {
} }
Command_BanFromServer::Command_BanFromServer(const QString &_userName, int _minutes)
: AdminCommand("ban_from_server")
{
insertItem(new SerializableItem_String("user_name", _userName));
insertItem(new SerializableItem_Int("minutes", _minutes));
}
void ProtocolItem::initializeHashAuto() void ProtocolItem::initializeHashAuto()
{ {
itemNameHash.insert("cmdping", Command_Ping::newItem); itemNameHash.insert("cmdping", Command_Ping::newItem);
@ -514,6 +525,7 @@ void ProtocolItem::initializeHashAuto()
itemNameHash.insert("game_eventstop_dump_zone", Event_StopDumpZone::newItem); itemNameHash.insert("game_eventstop_dump_zone", Event_StopDumpZone::newItem);
itemNameHash.insert("generic_eventremove_from_list", Event_RemoveFromList::newItem); itemNameHash.insert("generic_eventremove_from_list", Event_RemoveFromList::newItem);
itemNameHash.insert("generic_eventserver_message", Event_ServerMessage::newItem); itemNameHash.insert("generic_eventserver_message", Event_ServerMessage::newItem);
itemNameHash.insert("generic_eventconnection_closed", Event_ConnectionClosed::newItem);
itemNameHash.insert("generic_eventmessage", Event_Message::newItem); itemNameHash.insert("generic_eventmessage", Event_Message::newItem);
itemNameHash.insert("generic_eventgame_joined", Event_GameJoined::newItem); itemNameHash.insert("generic_eventgame_joined", Event_GameJoined::newItem);
itemNameHash.insert("generic_eventuser_left", Event_UserLeft::newItem); itemNameHash.insert("generic_eventuser_left", Event_UserLeft::newItem);
@ -525,4 +537,5 @@ void ProtocolItem::initializeHashAuto()
itemNameHash.insert("game_event_contextundo_draw", Context_UndoDraw::newItem); itemNameHash.insert("game_event_contextundo_draw", Context_UndoDraw::newItem);
itemNameHash.insert("game_event_contextmove_card", Context_MoveCard::newItem); itemNameHash.insert("game_event_contextmove_card", Context_MoveCard::newItem);
itemNameHash.insert("cmdupdate_server_message", Command_UpdateServerMessage::newItem); itemNameHash.insert("cmdupdate_server_message", Command_UpdateServerMessage::newItem);
itemNameHash.insert("cmdban_from_server", Command_BanFromServer::newItem);
} }

View file

@ -62,6 +62,7 @@
3:stop_dump_zone:i,zone_owner_id:s,zone 3:stop_dump_zone:i,zone_owner_id:s,zone
4:remove_from_list:s,list:s,user_name 4:remove_from_list:s,list:s,user_name
4:server_message:s,message 4:server_message:s,message
4:connection_closed:s,reason
4:message:s,sender_name:s,receiver_name:s,text 4:message:s,sender_name:s,receiver_name:s,text
4:game_joined:i,game_id:s,game_description:i,player_id:b,spectator:b,spectators_can_talk:b,spectators_see_everything:b,resuming 4:game_joined:i,game_id:s,game_description:i,player_id:b,spectator:b,spectators_can_talk:b,spectators_see_everything:b,resuming
4:user_left:s,user_name 4:user_left:s,user_name
@ -73,3 +74,4 @@
6:undo_draw 6:undo_draw
6:move_card 6:move_card
7:update_server_message 7:update_server_message
7:ban_from_server:s,user_name:i,minutes

View file

@ -583,6 +583,14 @@ public:
static SerializableItem *newItem() { return new Event_ServerMessage; } static SerializableItem *newItem() { return new Event_ServerMessage; }
int getItemId() const { return ItemId_Event_ServerMessage; } int getItemId() const { return ItemId_Event_ServerMessage; }
}; };
class Event_ConnectionClosed : public GenericEvent {
Q_OBJECT
public:
Event_ConnectionClosed(const QString &_reason = QString());
QString getReason() const { return static_cast<SerializableItem_String *>(itemMap.value("reason"))->getData(); };
static SerializableItem *newItem() { return new Event_ConnectionClosed; }
int getItemId() const { return ItemId_Event_ConnectionClosed; }
};
class Event_Message : public GenericEvent { class Event_Message : public GenericEvent {
Q_OBJECT Q_OBJECT
public: public:
@ -675,5 +683,14 @@ public:
static SerializableItem *newItem() { return new Command_UpdateServerMessage; } static SerializableItem *newItem() { return new Command_UpdateServerMessage; }
int getItemId() const { return ItemId_Command_UpdateServerMessage; } int getItemId() const { return ItemId_Command_UpdateServerMessage; }
}; };
class Command_BanFromServer : public AdminCommand {
Q_OBJECT
public:
Command_BanFromServer(const QString &_userName = QString(), int _minutes = -1);
QString getUserName() const { return static_cast<SerializableItem_String *>(itemMap.value("user_name"))->getData(); };
int getMinutes() const { return static_cast<SerializableItem_Int *>(itemMap.value("minutes"))->getData(); };
static SerializableItem *newItem() { return new Command_BanFromServer; }
int getItemId() const { return ItemId_Command_BanFromServer; }
};
#endif #endif

View file

@ -44,6 +44,7 @@ public:
virtual QMap<QString, ServerInfo_User *> getBuddyList(const QString &name) = 0; virtual QMap<QString, ServerInfo_User *> getBuddyList(const QString &name) = 0;
virtual QMap<QString, ServerInfo_User *> getIgnoreList(const QString &name) = 0; virtual QMap<QString, ServerInfo_User *> getIgnoreList(const QString &name) = 0;
virtual bool getUserBanned(Server_ProtocolHandler * /*client*/, const QString & /*userName*/) const { return false; }
protected: protected:
QMap<int, Server_Game *> games; QMap<int, Server_Game *> games;
QList<Server_ProtocolHandler *> clients; QList<Server_ProtocolHandler *> clients;

View file

@ -133,6 +133,7 @@ ResponseCode Server_ProtocolHandler::processCommandHelper(Command *command, Comm
switch (command->getItemId()) { switch (command->getItemId()) {
case ItemId_Command_UpdateServerMessage: return cmdUpdateServerMessage(static_cast<Command_UpdateServerMessage *>(command), cont); case ItemId_Command_UpdateServerMessage: return cmdUpdateServerMessage(static_cast<Command_UpdateServerMessage *>(command), cont);
case ItemId_Command_BanFromServer: return cmdBanFromServer(static_cast<Command_BanFromServer *>(command), cont);
default: return RespInvalidCommand; default: return RespInvalidCommand;
} }
} }
@ -244,6 +245,8 @@ ResponseCode Server_ProtocolHandler::cmdLogin(Command_Login *cmd, CommandContain
QString userName = cmd->getUsername().simplified(); QString userName = cmd->getUsername().simplified();
if (userName.isEmpty() || (userInfo != 0)) if (userName.isEmpty() || (userInfo != 0))
return RespContextError; return RespContextError;
if (server->getUserBanned(this, userName))
return RespWrongPassword;
authState = server->loginUser(this, userName, cmd->getPassword()); authState = server->loginUser(this, userName, cmd->getPassword());
if (authState == PasswordWrong) if (authState == PasswordWrong)
return RespWrongPassword; return RespWrongPassword;

View file

@ -85,6 +85,7 @@ private:
ResponseCode cmdStopDumpZone(Command_StopDumpZone *cmd, CommandContainer *cont, Server_Game *game, Server_Player *player); ResponseCode cmdStopDumpZone(Command_StopDumpZone *cmd, CommandContainer *cont, Server_Game *game, Server_Player *player);
ResponseCode cmdRevealCards(Command_RevealCards *cmd, CommandContainer *cont, Server_Game *game, Server_Player *player); ResponseCode cmdRevealCards(Command_RevealCards *cmd, CommandContainer *cont, Server_Game *game, Server_Player *player);
virtual ResponseCode cmdUpdateServerMessage(Command_UpdateServerMessage *cmd, CommandContainer *cont) = 0; virtual ResponseCode cmdUpdateServerMessage(Command_UpdateServerMessage *cmd, CommandContainer *cont) = 0;
virtual ResponseCode cmdBanFromServer(Command_BanFromServer *cmd, CommandContainer *cont) = 0;
ResponseCode processCommandHelper(Command *command, CommandContainer *cont); ResponseCode processCommandHelper(Command *command, CommandContainer *cont);
private slots: private slots:

View file

@ -33,6 +33,10 @@ Servatrice::Servatrice(QObject *parent)
connect(pingClock, SIGNAL(timeout()), this, SIGNAL(pingClockTimeout())); connect(pingClock, SIGNAL(timeout()), this, SIGNAL(pingClockTimeout()));
pingClock->start(1000); pingClock->start(1000);
banTimeoutClock = new QTimer(this);
connect(banTimeoutClock, SIGNAL(timeout()), this, SLOT(updateBanTimer()));
banTimeoutClock->start(60000);
ProtocolItem::initializeHash(); ProtocolItem::initializeHash();
settings = new QSettings("servatrice.ini", QSettings::IniFormat, this); settings = new QSettings("servatrice.ini", QSettings::IniFormat, this);
@ -85,6 +89,7 @@ Servatrice::Servatrice(QObject *parent)
maxGameInactivityTime = settings->value("game/max_game_inactivity_time").toInt(); maxGameInactivityTime = settings->value("game/max_game_inactivity_time").toInt();
maxPlayerInactivityTime = settings->value("game/max_player_inactivity_time").toInt(); maxPlayerInactivityTime = settings->value("game/max_player_inactivity_time").toInt();
maxUsersPerAddress = settings->value("security/max_users_per_address").toInt();
messageCountingInterval = settings->value("security/message_counting_interval").toInt(); messageCountingInterval = settings->value("security/message_counting_interval").toInt();
maxMessageCountPerInterval = settings->value("security/max_message_count_per_interval").toInt(); maxMessageCountPerInterval = settings->value("security/max_message_count_per_interval").toInt();
maxMessageSizePerInterval = settings->value("security/max_message_size_per_interval").toInt(); maxMessageSizePerInterval = settings->value("security/max_message_size_per_interval").toInt();
@ -232,6 +237,15 @@ ServerInfo_User *Servatrice::getUserData(const QString &name)
return new ServerInfo_User(name, ServerInfo_User::IsUser); return new ServerInfo_User(name, ServerInfo_User::IsUser);
} }
int Servatrice::getUsersWithAddress(const QHostAddress &address) const
{
int result = 0;
for (int i = 0; i < clients.size(); ++i)
if (static_cast<ServerSocketInterface *>(clients[i])->getPeerAddress() == address)
++result;
return result;
}
QMap<QString, ServerInfo_User *> Servatrice::getBuddyList(const QString &name) QMap<QString, ServerInfo_User *> Servatrice::getBuddyList(const QString &name)
{ {
QMap<QString, ServerInfo_User *> result; QMap<QString, ServerInfo_User *> result;
@ -276,6 +290,32 @@ QMap<QString, ServerInfo_User *> Servatrice::getIgnoreList(const QString &name)
return result; return result;
} }
bool Servatrice::getUserBanned(Server_ProtocolHandler *client, const QString &userName) const
{
QHostAddress address = static_cast<ServerSocketInterface *>(client)->getPeerAddress();
for (int i = 0; i < addressBanList.size(); ++i)
if (address == addressBanList[i].first)
return true;
for (int i = 0; i < nameBanList.size(); ++i)
if (userName == nameBanList[i].first)
return true;
return false;
}
void Servatrice::updateBanTimer()
{
for (int i = 0; i < addressBanList.size(); )
if (--(addressBanList[i].second) <= 0)
addressBanList.removeAt(i);
else
++i;
for (int i = 0; i < nameBanList.size(); )
if (--(nameBanList[i].second) <= 0)
nameBanList.removeAt(i);
else
++i;
}
void Servatrice::updateLoginMessage() void Servatrice::updateLoginMessage()
{ {
checkSql(); checkSql();
@ -308,4 +348,4 @@ void Servatrice::statusUpdate()
execSqlQuery(query); execSqlQuery(query);
} }
const QString Servatrice::versionString = "Servatrice 0.20110215"; const QString Servatrice::versionString = "Servatrice 0.20110303";

View file

@ -34,6 +34,7 @@ class Servatrice : public Server
private slots: private slots:
void newConnection(); void newConnection();
void statusUpdate(); void statusUpdate();
void updateBanTimer();
public: public:
static const QString versionString; static const QString versionString;
Servatrice(QObject *parent = 0); Servatrice(QObject *parent = 0);
@ -45,27 +46,33 @@ public:
bool getGameShouldPing() const { return true; } bool getGameShouldPing() const { return true; }
int getMaxGameInactivityTime() const { return maxGameInactivityTime; } int getMaxGameInactivityTime() const { return maxGameInactivityTime; }
int getMaxPlayerInactivityTime() const { return maxPlayerInactivityTime; } int getMaxPlayerInactivityTime() const { return maxPlayerInactivityTime; }
int getMaxUsersPerAddress() const { return maxUsersPerAddress; }
int getMessageCountingInterval() const { return messageCountingInterval; } int getMessageCountingInterval() const { return messageCountingInterval; }
int getMaxMessageCountPerInterval() const { return maxMessageCountPerInterval; } int getMaxMessageCountPerInterval() const { return maxMessageCountPerInterval; }
int getMaxMessageSizePerInterval() const { return maxMessageSizePerInterval; } int getMaxMessageSizePerInterval() const { return maxMessageSizePerInterval; }
QString getDbPrefix() const { return dbPrefix; } QString getDbPrefix() const { return dbPrefix; }
void updateLoginMessage(); void updateLoginMessage();
ServerInfo_User *getUserData(const QString &name); ServerInfo_User *getUserData(const QString &name);
int getUsersWithAddress(const QHostAddress &address) const;
QMap<QString, ServerInfo_User *> getBuddyList(const QString &name);
QMap<QString, ServerInfo_User *> getIgnoreList(const QString &name);
bool getUserBanned(Server_ProtocolHandler *client, const QString &userName) const;
void addAddressBan(const QHostAddress &address, int minutes) { addressBanList.append(QPair<QHostAddress, int>(address, minutes)); }
void addNameBan(const QString &name, int minutes) { nameBanList.append(QPair<QString, int>(name, minutes)); }
protected: protected:
bool userExists(const QString &user); bool userExists(const QString &user);
AuthenticationResult checkUserPassword(const QString &user, const QString &password); AuthenticationResult checkUserPassword(const QString &user, const QString &password);
QMap<QString, ServerInfo_User *> getBuddyList(const QString &name);
QMap<QString, ServerInfo_User *> getIgnoreList(const QString &name);
private: private:
QTimer *pingClock, *statusUpdateClock; QTimer *pingClock, *statusUpdateClock, *banTimeoutClock;
QTcpServer *tcpServer; QTcpServer *tcpServer;
QString loginMessage; QString loginMessage;
QString dbPrefix; QString dbPrefix;
QSettings *settings; QSettings *settings;
int uptime; int uptime;
int maxGameInactivityTime; QList<QPair<QHostAddress, int> > addressBanList;
int maxPlayerInactivityTime; QList<QPair<QString, int> > nameBanList;
int messageCountingInterval, maxMessageCountPerInterval, maxMessageSizePerInterval; int maxGameInactivityTime, maxPlayerInactivityTime;
int maxUsersPerAddress, messageCountingInterval, maxMessageCountPerInterval, maxMessageSizePerInterval;
ServerInfo_User *evalUserQueryResult(const QSqlQuery &query, bool complete); ServerInfo_User *evalUserQueryResult(const QSqlQuery &query, bool complete);
}; };

View file

@ -21,6 +21,7 @@
#include <QXmlStreamReader> #include <QXmlStreamReader>
#include <QXmlStreamWriter> #include <QXmlStreamWriter>
#include <QtSql> #include <QtSql>
#include <QHostAddress>
#include <QDebug> #include <QDebug>
#include "serversocketinterface.h" #include "serversocketinterface.h"
#include "servatrice.h" #include "servatrice.h"
@ -45,13 +46,19 @@ ServerSocketInterface::ServerSocketInterface(Servatrice *_server, QTcpSocket *_s
xmlWriter->writeStartElement("cockatrice_server_stream"); xmlWriter->writeStartElement("cockatrice_server_stream");
xmlWriter->writeAttribute("version", QString::number(ProtocolItem::protocolVersion)); xmlWriter->writeAttribute("version", QString::number(ProtocolItem::protocolVersion));
sendProtocolItem(new Event_ServerMessage(Servatrice::versionString)); int maxUsers = _server->getMaxUsersPerAddress();
if ((maxUsers > 0) && (_server->getUsersWithAddress(socket->peerAddress()) >= maxUsers)) {
sendProtocolItem(new Event_ConnectionClosed("too_many_connections"));
deleteLater();
} else
sendProtocolItem(new Event_ServerMessage(Servatrice::versionString));
} }
ServerSocketInterface::~ServerSocketInterface() ServerSocketInterface::~ServerSocketInterface()
{ {
qDebug("ServerSocketInterface destructor"); qDebug("ServerSocketInterface destructor");
socket->flush();
delete xmlWriter; delete xmlWriter;
delete xmlReader; delete xmlReader;
delete socket; delete socket;
@ -424,3 +431,35 @@ ResponseCode ServerSocketInterface::cmdUpdateServerMessage(Command_UpdateServerM
servatrice->updateLoginMessage(); servatrice->updateLoginMessage();
return RespOk; return RespOk;
} }
ResponseCode ServerSocketInterface::cmdBanFromServer(Command_BanFromServer *cmd, CommandContainer *cont)
{
QString userName = cmd->getUserName();
if (!server->getUsers().contains(userName))
return RespNameNotFound;
int minutes = cmd->getMinutes();
ServerSocketInterface *user = static_cast<ServerSocketInterface *>(server->getUsers().value(userName));
if (user->getUserInfo()->getUserLevel() & ServerInfo_User::IsRegistered) {
// Registered users can be banned by name.
if (minutes == 0) {
QSqlQuery query;
query.prepare("update " + servatrice->getDbPrefix() + "_users set banned=1 where name = :name");
query.bindValue(":name", userName);
servatrice->execSqlQuery(query);
} else
servatrice->addNameBan(userName, minutes);
} else {
// Unregistered users must be banned by IP address.
// Indefinite address bans are not reasonable -> default to 30 minutes.
if (minutes == 0)
minutes = 30;
servatrice->addAddressBan(user->getPeerAddress(), minutes);
}
user->sendProtocolItem(new Event_ConnectionClosed("banned"));
user->deleteLater();
return RespOk;
}

View file

@ -21,6 +21,7 @@
#define SERVERSOCKETINTERFACE_H #define SERVERSOCKETINTERFACE_H
#include <QTcpSocket> #include <QTcpSocket>
#include <QHostAddress>
#include "server_protocolhandler.h" #include "server_protocolhandler.h"
class QTcpSocket; class QTcpSocket;
@ -59,9 +60,11 @@ private:
DeckList *getDeckFromDatabase(int deckId); DeckList *getDeckFromDatabase(int deckId);
ResponseCode cmdDeckDownload(Command_DeckDownload *cmd, CommandContainer *cont); ResponseCode cmdDeckDownload(Command_DeckDownload *cmd, CommandContainer *cont);
ResponseCode cmdUpdateServerMessage(Command_UpdateServerMessage *cmd, CommandContainer *cont); ResponseCode cmdUpdateServerMessage(Command_UpdateServerMessage *cmd, CommandContainer *cont);
ResponseCode cmdBanFromServer(Command_BanFromServer *cmd, CommandContainer *cont);
public: public:
ServerSocketInterface(Servatrice *_server, QTcpSocket *_socket, QObject *parent = 0); ServerSocketInterface(Servatrice *_server, QTcpSocket *_socket, QObject *parent = 0);
~ServerSocketInterface(); ~ServerSocketInterface();
QHostAddress getPeerAddress() const { return socket->peerAddress(); }
void sendProtocolItem(ProtocolItem *item, bool deleteItem = true); void sendProtocolItem(ProtocolItem *item, bool deleteItem = true);
}; };