| 1 | #include "mtx/events/nheko_extensions/invite_permissions.hpp" |
| 2 | |
| 3 | #include <tuple> |
| 4 | |
| 5 | #include <nlohmann/json.hpp> |
| 6 | |
| 7 | #include <mtx/log.hpp> |
| 8 | |
| 9 | static std::tuple<std::map<std::string, std::string, std::less<>>, |
| 10 | std::map<std::string, std::string, std::less<>>> |
| 11 | parse_subset(const std::string &key, const nlohmann::json &obj) |
| 12 | { |
| 13 | std::map<std::string, std::string, std::less<>> allow, deny; |
| 14 | |
| 15 | if (auto o = obj.find(key); o != obj.end()) { |
| 16 | auto a = o->value("allow", std::map<std::string, nlohmann::json>()); |
| 17 | auto d = o->value("deny", std::map<std::string, nlohmann::json>()); |
| 18 | |
| 19 | for (const auto &[k, v] : a) { |
| 20 | allow.emplace(k, v.dump()); |
| 21 | } |
| 22 | for (const auto &[k, v] : d) { |
| 23 | deny.emplace(k, v.dump()); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | return {allow, deny}; |
| 28 | } |
| 29 | |
| 30 | static void |
| 31 | emit_subset(nlohmann::json &obj, |
| 32 | std::string_view key, |
| 33 | const std::map<std::string, std::string, std::less<>> &allow, |
| 34 | const std::map<std::string, std::string, std::less<>> &deny) |
| 35 | { |
| 36 | for (const auto &[k, v] : allow) { |
| 37 | obj[key]["allow"][k] = nlohmann::json::parse(v); |
| 38 | } |
| 39 | for (const auto &[k, v] : deny) { |
| 40 | obj[key]["deny"][k] = nlohmann::json::parse(v); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | namespace mtx { |
| 45 | namespace events { |
| 46 | namespace account_data { |
| 47 | namespace nheko_extensions { |
| 48 | |
| 49 | void |
| 50 | from_json(const nlohmann::json &obj, InvitePermissions &content) |
| 51 | { |
| 52 | if (obj.contains("default")) { |
| 53 | content.default_ = obj.at("default").get<std::string>(); |
| 54 | } else { |
| 55 | content.default_ = "allow"; |
| 56 | } |
| 57 | |
| 58 | std::tie(content.server_allow, content.server_deny) = parse_subset("server", obj); |
| 59 | std::tie(content.room_allow, content.room_deny) = parse_subset("room", obj); |
| 60 | std::tie(content.user_allow, content.user_deny) = parse_subset("user", obj); |
| 61 | } |
| 62 | |
| 63 | void |
| 64 | to_json(nlohmann::json &obj, const InvitePermissions &content) |
| 65 | { |
| 66 | obj["default"] = content.default_; |
| 67 | |
| 68 | emit_subset(obj, "server", content.server_allow, content.server_deny); |
| 69 | emit_subset(obj, "room", content.room_allow, content.room_deny); |
| 70 | emit_subset(obj, "user", content.user_allow, content.user_deny); |
| 71 | } |
| 72 | |
| 73 | bool |
| 74 | InvitePermissions::invite_allowed(std::string_view room_id, std::string_view inviter) const |
| 75 | { |
| 76 | if (this->user_deny.contains(inviter)) |
| 77 | return false; |
| 78 | if (this->user_allow.contains(inviter)) |
| 79 | return true; |
| 80 | if (this->room_deny.contains(room_id)) |
| 81 | return false; |
| 82 | if (this->room_allow.contains(room_id)) |
| 83 | return true; |
| 84 | |
| 85 | if (auto pos = inviter.find_first_of(':'); pos != std::string_view::npos) { |
| 86 | auto server = inviter.substr(pos + 1); |
| 87 | if (this->server_deny.contains(server)) |
| 88 | return false; |
| 89 | if (this->server_allow.contains(server)) |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | return this->default_ != "deny"; |
| 94 | } |
| 95 | } |
| 96 | } // namespace account_data |
| 97 | } // namespace events |
| 98 | } // namespace mtx |