From 2e6df89dc5105dfff20baf321c9f286c110373fa Mon Sep 17 00:00:00 2001 From: Tobias Fella Date: Fri, 18 Nov 2022 02:08:19 +0100 Subject: [PATCH] Add commands for banning, unbanning and kicking users --- src/actionsmodel.cpp | 114 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) diff --git a/src/actionsmodel.cpp b/src/actionsmodel.cpp index 22425145e..bb47c9000 100644 --- a/src/actionsmodel.cpp +++ b/src/actionsmodel.cpp @@ -7,6 +7,7 @@ #include "neochatroom.h" #include "neochatuser.h" #include +#include #include @@ -388,6 +389,119 @@ QVector actions{ kli18n(""), kli18n("React to the message with the given text"), }, + Action{ + QStringLiteral("ban"), + [](const QString &text, NeoChatRoom *room) { + auto parts = text.split(QLatin1String(" ")); + static const QRegularExpression mxidRegex( + QStringLiteral(R"((^|[][[:space:](){}`'";])([!#@][-a-z0-9_=#/.]{1,252}:\w(?:\w|\.|-)*\.\w+(?::\d{1,5})?))")); + auto regexMatch = mxidRegex.match(parts[0]); + if (!regexMatch.hasMatch()) { + Q_EMIT room->showMessage(NeoChatRoom::Error, i18nc("'' does not look like a matrix id.", "'%1' does not look like a matrix id.", text)); + return QString(); + } +#ifdef QUOTIENT_07 + auto state = room->currentState().get(parts[0]); + if (state && state->membership() == Membership::Ban) { + Q_EMIT room->showMessage(NeoChatRoom::Info, i18nc(" is already banned from this room.", "%1 is already banned from this room.", text)); + return QString(); + } +#endif + auto plEvent = room->getCurrentState(); + if (plEvent->ban() > plEvent->powerLevelForUser(room->localUser()->id())) { + Q_EMIT room->showMessage(NeoChatRoom::Error, i18n("You are not allowed to ban users from this room.")); + return QString(); + } + if (plEvent->powerLevelForUser(room->localUser()->id()) <= plEvent->powerLevelForUser(parts[0])) { + Q_EMIT room->showMessage( + NeoChatRoom::Error, + i18nc("You are not allowed to ban from this room.", "You are not allowed to ban %1 from this room.", parts[0])); + return QString(); + } + room->ban(parts[0], parts.size() > 1 ? parts.mid(1).join(" ") : QString()); + Q_EMIT room->showMessage(NeoChatRoom::Positive, i18nc(" was banned from this room.", "%1 was banned from this room.", parts[0])); + return QString(); + }, + false, + std::nullopt, + kli18n(" []"), + kli18n("Bans the given user"), + }, + Action{ + QStringLiteral("unban"), + [](const QString &text, NeoChatRoom *room) { + static const QRegularExpression mxidRegex( + QStringLiteral(R"((^|[][[:space:](){}`'";])([!#@][-a-z0-9_=#/.]{1,252}:\w(?:\w|\.|-)*\.\w+(?::\d{1,5})?))")); + auto regexMatch = mxidRegex.match(text); + if (!regexMatch.hasMatch()) { + Q_EMIT room->showMessage(NeoChatRoom::Error, i18nc("'' does not look like a matrix id.", "'%1' does not look like a matrix id.", text)); + return QString(); + } + auto plEvent = room->getCurrentState(); + if (plEvent->ban() > plEvent->powerLevelForUser(room->localUser()->id())) { + Q_EMIT room->showMessage(NeoChatRoom::Error, i18n("You are not allowed to unban users from this room.")); + return QString(); + } +#ifdef QUOTIENT_07 + auto state = room->currentState().get(text); + if (state && state->membership() != Membership::Ban) { + Q_EMIT room->showMessage(NeoChatRoom::Info, i18nc(" is not banned from this room.", "%1 is not banned from this room.", text)); + return QString(); + } +#endif + room->unban(text); + Q_EMIT room->showMessage(NeoChatRoom::Positive, i18nc(" was unbanned from this room.", "%1 was unbanned from this room.", text)); + + return QString(); + }, + false, + std::nullopt, + kli18n(""), + kli18n("Removes the ban of the given user"), + }, + Action{ + QStringLiteral("kick"), + [](const QString &text, NeoChatRoom *room) { + auto parts = text.split(QLatin1String(" ")); + static const QRegularExpression mxidRegex( + QStringLiteral(R"((^|[][[:space:](){}`'";])([!#@][-a-z0-9_=#/.]{1,252}:\w(?:\w|\.|-)*\.\w+(?::\d{1,5})?))")); + auto regexMatch = mxidRegex.match(parts[0]); + if (!regexMatch.hasMatch()) { + Q_EMIT room->showMessage(NeoChatRoom::Error, + i18nc("'' does not look like a matrix id.", "'%1' does not look like a matrix id.", parts[0])); + return QString(); + } + if (parts[0] == room->localUser()->id()) { + Q_EMIT room->showMessage(NeoChatRoom::Error, i18n("You cannot kick yourself from the room.")); + return QString(); + } +#ifdef QUOTIENT_07 + if (!room->isMember(parts[0])) { + Q_EMIT room->showMessage(NeoChatRoom::Error, i18nc(" is not in this room", "%1 is not in this room.", parts[0])); + return QString(); + } +#endif + auto plEvent = room->getCurrentState(); + auto kick = plEvent->kick(); + if (plEvent->powerLevelForUser(room->localUser()->id()) < kick) { + Q_EMIT room->showMessage(NeoChatRoom::Error, i18n("You are not allowed to kick users from this room.")); + return QString(); + } + if (plEvent->powerLevelForUser(room->localUser()->id()) <= plEvent->powerLevelForUser(parts[0])) { + Q_EMIT room->showMessage( + NeoChatRoom::Error, + i18nc("You are not allowed to kick from this room", "You are not allowed to kick %1 from this room.", parts[0])); + return QString(); + } + room->kickMember(parts[0], parts.size() > 1 ? parts.mid(1).join(" ") : QString()); + Q_EMIT room->showMessage(NeoChatRoom::Positive, i18nc(" was kicked from this room.", "%1 was kicked from this room.", parts[0])); + return QString(); + }, + false, + std::nullopt, + kli18n(" []"), + kli18n("Removes the user from the room"), + }, }; int ActionsModel::rowCount(const QModelIndex &parent) const