Skip to main content

zaun_community_protocol/permissions/lockdown/
restrictions.rs

1//! The named restrictions of a lockdown and their permission bits.
2
3use bitflags::bitflags;
4
5use crate::bits::Permissions;
6
7bitflags! {
8    /// The restrictions a lockdown names (specification §8.10).
9    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
10    pub struct LockdownRestrictions: u16 {
11        /// `pause_joins`: checked by admission commands, not a permission bit.
12        const PAUSE_JOINS = 1 << 0;
13        /// `disable_invite_redeem`: checked by `invite.redeem`.
14        const DISABLE_INVITE_REDEEM = 1 << 1;
15        /// `disable_invite_create`: removes `CREATE_INVITE`.
16        const DISABLE_INVITE_CREATE = 1 << 2;
17        /// `require_approval`: checked by admission commands.
18        const REQUIRE_APPROVAL = 1 << 3;
19        /// `restrict_newcomers`: the newcomer mask for members admitted within the window.
20        const RESTRICT_NEWCOMERS = 1 << 4;
21        /// `disable_attachments`: removes `ATTACH_FILES`.
22        const DISABLE_ATTACHMENTS = 1 << 5;
23        /// `disable_links`: removes `EMBED_LINKS` (and the engine rejects content with links).
24        const DISABLE_LINKS = 1 << 6;
25        /// `disable_mass_mentions`: removes `MENTION_EVERYONE`.
26        const DISABLE_MASS_MENTIONS = 1 << 7;
27        /// `restrict_voice`: removes `CONNECT` and its dependants.
28        const RESTRICT_VOICE = 1 << 8;
29        /// `restrict_expensive`: checked by the expensive commands themselves
30        /// (`message.history` depth, the channel streams of `sync.events` and `sync.resume`,
31        /// `member.list` paging; see [`super::EXPENSIVE_HISTORY_DEPTH`] and
32        /// [`super::EXPENSIVE_MEMBER_PAGE`]).
33        const RESTRICT_EXPENSIVE = 1 << 9;
34    }
35}
36
37/// The wire names of [`LockdownRestrictions`], in bit order.
38const WIRE: [(&str, LockdownRestrictions); 10] = [
39    ("pause_joins", LockdownRestrictions::PAUSE_JOINS),
40    (
41        "disable_invite_redeem",
42        LockdownRestrictions::DISABLE_INVITE_REDEEM,
43    ),
44    (
45        "disable_invite_create",
46        LockdownRestrictions::DISABLE_INVITE_CREATE,
47    ),
48    ("require_approval", LockdownRestrictions::REQUIRE_APPROVAL),
49    (
50        "restrict_newcomers",
51        LockdownRestrictions::RESTRICT_NEWCOMERS,
52    ),
53    (
54        "disable_attachments",
55        LockdownRestrictions::DISABLE_ATTACHMENTS,
56    ),
57    ("disable_links", LockdownRestrictions::DISABLE_LINKS),
58    (
59        "disable_mass_mentions",
60        LockdownRestrictions::DISABLE_MASS_MENTIONS,
61    ),
62    ("restrict_voice", LockdownRestrictions::RESTRICT_VOICE),
63    (
64        "restrict_expensive",
65        LockdownRestrictions::RESTRICT_EXPENSIVE,
66    ),
67];
68
69impl LockdownRestrictions {
70    /// Parses one wire name; `None` for an unknown restriction.
71    #[must_use]
72    pub fn from_wire(name: &str) -> Option<Self> {
73        WIRE.iter().find(|(n, _)| *n == name).map(|(_, r)| *r)
74    }
75
76    /// The bits every member loses while these restrictions are active, before the
77    /// per-member `restrict_newcomers` part (permissions §3.3).
78    #[must_use]
79    pub fn permission_mask(self) -> Permissions {
80        let mut mask = Permissions::empty();
81        let pairs = [
82            (Self::DISABLE_INVITE_CREATE, Permissions::CREATE_INVITE),
83            (Self::DISABLE_ATTACHMENTS, Permissions::ATTACH_FILES),
84            // permissions.md §3.3; SPEC: specification §8.10 does not name the bit, but §8.5
85            // rejects a URL without EMBED_LINKS with reason `lockdown`.
86            (Self::DISABLE_LINKS, Permissions::EMBED_LINKS),
87            (Self::DISABLE_MASS_MENTIONS, Permissions::MENTION_EVERYONE),
88            (Self::RESTRICT_VOICE, Permissions::CONNECT),
89        ];
90        for (flag, bits) in pairs {
91            if self.contains(flag) {
92                mask |= bits;
93            }
94        }
95        mask
96    }
97}