Files
yourchat/src/core/chat_room.h
Torsten Schulz (local) 016de9e5cf Implement access control enhancements in ChatRoom and related classes
- Introduced a new overloaded method `accessAllowed` in `ChatRoom` to include user age verification for adult rooms.
- Added `falukant_user_id` method in `ChatUser` to retrieve user ID for age checks.
- Implemented `getUserAge` method in `ChatUser` to fetch and decrypt user birthdate from the database for age validation.
- Updated `roomAllowed` methods in `Server` and `SSLServer` to utilize the new access control logic, ensuring proper user context is considered.
- Enhanced debugging output for better traceability during access checks.
2025-12-18 14:10:50 +01:00

156 lines
6.1 KiB
C++
Executable File

// renamed from room.h
#ifndef YC_LIB_CHAT_ROOM_H
#define YC_LIB_CHAT_ROOM_H
#include <vector>
#include <string>
#include <queue>
#include <thread>
#include <future>
#include <utility>
#include <vector>
#include <map>
#include <chrono>
#include "lib/base.h"
#include "chat_user.h"
#include "object/room.h"
#include "lib/database.h"
namespace Yc
{
namespace Lib
{
class Server;
class ChatRoom : public Base, public std::enable_shared_from_this<ChatRoom>
{
public:
enum RoomType
{
none = 0,
dice = 1,
poker = 2,
rounds = 4,
password = 8
};
ChatRoom(std::shared_ptr<Server> parent, Json::Value roomParams);
~ChatRoom();
std::shared_ptr<Server> getServer() const { return _parent; }
void run();
std::string name();
bool addUser(std::string userName, std::string color, std::string password, int socket);
bool addUser(std::string userName, std::string color, std::string password, void* wsi);
bool addUser(std::string userName, std::string color, std::string password, void* wsi, std::string token);
bool addUser(std::shared_ptr<ChatUser> user, std::string password);
bool userNameExists(std::string userName);
void removeUser(std::string _token, bool silent = false);
void removeUser(std::shared_ptr<ChatUser> user, bool silent = false);
void removeUserDisconnected(std::shared_ptr<ChatUser> userToRemove);
void setStop();
void addMessage(ChatUser::MsgType type, const char *messageText, std::string userName = "", std::string color = "");
void addMessage(ChatUser::MsgType type, std::string messageText, std::string userName = "", std::string color = "");
void addMessage(ChatUser::MsgType type, Json::Value messageText, std::string userName = "", std::string color = "");
void setDatabase(std::shared_ptr<Yc::Lib::Database> database);
RoomType type();
bool isType(RoomType type);
bool canDice();
void sendToAllUsers(ChatUser::MsgType type, Json::Value message);
unsigned int addDice(std::shared_ptr<ChatUser> user, int diceValue);
bool accessAllowed(std::string userName, std::string password);
bool accessAllowed(std::string userName, std::string password, std::shared_ptr<ChatUser> user);
bool userIsInRoom(std::string userName);
std::shared_ptr<ChatUser> findUserByName(std::string userName);
std::shared_ptr<ChatUser> findUserByToken(std::string token);
void addUserWhenQueueEmpty(std::shared_ptr<ChatUser> user);
bool userToNewRoom(std::shared_ptr<ChatUser> user, std::string newRoom, std::string password);
unsigned int flags();
Json::Value userList();
// Message structure for internal use
struct Message
{
ChatUser::MsgType type;
std::string messageTr;
std::string userName;
std::string color;
};
// Refactored helper methods
bool hasMessagesToProcess() const;
void processMessageQueue();
void broadcastMessageToUsers(const Message& message);
bool shouldSuppressMessageForUser(const Message& message, std::shared_ptr<ChatUser> user) const;
bool isDiceRoom() const;
// Neue Würfel-Funktionen
bool startDiceGame(int rounds, std::shared_ptr<ChatUser> admin);
bool rollDice(std::shared_ptr<ChatUser> user, int diceValue);
void endDiceGame();
bool isDiceGameRunning() const { return _diceGameRunning; }
int getCurrentRound() const { return _currentRound; }
int getTotalRounds() const { return _totalRounds; }
// Raumliste neu laden
void reloadRoomList();
private:
struct DiceResult
{
std::string userName;
int diceValue;
std::chrono::steady_clock::time_point rollTime;
bool valid;
};
std::shared_ptr<Server> _parent;
std::shared_ptr<Yc::Lib::Database> _database;
std::string _name;
std::string _password;
std::vector<std::string> _allowedUsers;
RoomType _type;
std::vector<std::shared_ptr<ChatUser>> _users;
bool _blocked;
bool _stop;
std::queue<Message> _msgQueue;
std::unique_ptr<std::thread> thread;
bool _roundRunning;
time_t _roundStart;
time_t _lastRoundEnd;
int _roundLength;
std::vector<std::pair<std::shared_ptr<ChatUser>, int>> _diceValues;
Yc::Object::Room _room;
// Neue Würfel-Spiel-Variablen
bool _diceGameRunning;
int _currentRound;
int _totalRounds;
std::chrono::steady_clock::time_point _roundStartTime;
std::chrono::steady_clock::time_point _gameStartTime;
std::map<std::string, std::vector<DiceResult>> _gameResults; // userName -> Runden-Ergebnisse
std::map<std::string, bool> _hasRolledThisRound; // userName -> hat in dieser Runde gewürfelt
std::thread _roundTimerThread;
void _handleDice();
void _startDiceRound();
void _endDiceRound();
void _initRound();
void _showDiceRoundResults();
// Neue Würfel-Funktionen
void _startRoundTimer();
void _endRoundTimer();
void _processRoundResults();
void _showGameResults();
void _resetRound();
bool _isUserAdmin(std::shared_ptr<ChatUser> user) const;
};
} // namespace Lib
} // namespace Yc
#endif // YC_LIB_CHAT_ROOM_H