Skip to main content

zaun_community_protocol/permissions/
requirement.rs

1//! The shape of one row of the command table (permissions §6) and its check.
2
3use crate::bits::Permissions;
4
5use super::denial::{Denial, DenialReason};
6use super::resolved::Resolved;
7
8/// Who may send the command at all, before any bit is checked.
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
10pub enum Access {
11    /// Any connection, bound (in any state the gate lets through) or not: `session.hello`,
12    /// `community.rotations`.
13    Any,
14    /// An unbound connection only.
15    Unbound,
16    /// Any bound state the gate lets through, `Pending` and `Suspended` included.
17    Bound,
18    /// A `Probation` or `Active` member.
19    Member,
20    /// The owner only; no bit substitutes.
21    Owner,
22}
23
24/// Where the required bits are evaluated.
25#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
26pub enum Scope {
27    /// `cp(A)`.
28    Community,
29    /// `chp(A, ch)` for the command's channel (for `channel.reorder`: each moved channel).
30    Channel,
31    /// `chp` when the command names a channel, else `cp`.
32    ChannelIfGiven,
33}
34
35/// What a command requires (permissions §6, specification §8).
36#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
37pub struct Requirement {
38    /// Who may send it.
39    pub access: Access,
40    /// Where the bits are evaluated.
41    pub scope: Scope,
42    /// Bits that must all be effective.
43    pub all: Permissions,
44    /// When non-empty, at least one of these must be effective as well.
45    pub any: Permissions,
46    /// When acting on another member's object (message, reaction, nickname, invitation,
47    /// report), these bits replace `all`.
48    pub others: Option<Permissions>,
49    /// Bits needed only for some bodies: the engine passes the ones that apply (attachments,
50    /// links, a new emoji, a pre-approved invitation, a channel stream, a full record).
51    pub conditional: Permissions,
52    /// Refused with `TIMED_OUT` while timed out, even when the bits survive the timeout.
53    pub not_timed_out: bool,
54    /// Refused with `FORBIDDEN{reason:"probation"}` during the waiting period.
55    pub not_waiting: bool,
56    /// Reserved (P1/P2): a v1 host answers `UNSUPPORTED_FEATURE` before any check.
57    pub reserved: bool,
58}
59
60impl Requirement {
61    /// A row with no bits.
62    #[must_use]
63    pub const fn access(access: Access) -> Self {
64        Self {
65            access,
66            scope: Scope::Community,
67            all: Permissions::empty(),
68            any: Permissions::empty(),
69            others: None,
70            conditional: Permissions::empty(),
71            not_timed_out: false,
72            not_waiting: false,
73            reserved: false,
74        }
75    }
76
77    /// A member command needing `all` in `scope`.
78    #[must_use]
79    pub const fn bits(scope: Scope, all: Permissions) -> Self {
80        Self {
81            scope,
82            all,
83            ..Self::access(Access::Member)
84        }
85    }
86
87    /// Checks the resolved permissions in the requirement's scope. `other` is true when the
88    /// actor acts on another member's object; `conditional` holds the conditional bits the
89    /// body needs (masked by [`Requirement::conditional`]).
90    pub fn check(
91        &self,
92        resolved: &Resolved,
93        other: bool,
94        conditional: Permissions,
95    ) -> Result<(), Denial> {
96        let base = match (other, self.others) {
97            (true, Some(o)) => o,
98            _ => self.all,
99        };
100        // Specification §8 preamble: content commands fail with `TIMED_OUT` while timed out.
101        if self.not_timed_out && resolved.timed_out {
102            return Err(Denial {
103                missing: Permissions::empty(),
104                reason: Some(DenialReason::Timeout),
105            });
106        }
107        resolved.require(base | (conditional & self.conditional))?;
108        resolved.require_any(self.any)?;
109        if self.not_waiting && resolved.waiting {
110            return Err(Denial {
111                missing: Permissions::empty(),
112                reason: Some(DenialReason::Probation),
113            });
114        }
115        Ok(())
116    }
117}