zaun_community_protocol/command/object/
bot.rs1mod check;
4pub mod events;
5
6pub use check::{config_key_ok, host_pattern_ok, local_address_ok};
7
8use std::collections::BTreeMap;
9
10use serde::{Deserialize, Serialize};
11
12use crate::bits::Permissions;
13use crate::ids::{BotId, MemberId, PublisherId};
14
15#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
17pub struct Capabilities {
18 #[serde(default)]
20 pub message_content: bool,
21 #[serde(default)]
23 pub members: bool,
24 #[serde(default)]
26 pub presence: bool,
27 #[serde(default)]
29 pub timers: bool,
30 #[serde(default)]
32 pub attachments: bool,
33 #[serde(default)]
35 pub voice: bool,
36 #[serde(default)]
38 pub dms: bool,
39 #[serde(default)]
41 pub storage_bytes: u64,
42 #[serde(default)]
44 pub network: Vec<String>,
45 #[serde(default, skip_serializing_if = "Vec::is_empty")]
49 pub local_network: Vec<String>,
50}
51
52impl Capabilities {
53 #[must_use]
55 pub fn within(&self, other: &Self) -> bool {
56 let flag = |a: bool, b: bool| !a || b;
57 flag(self.message_content, other.message_content)
58 && flag(self.members, other.members)
59 && flag(self.presence, other.presence)
60 && flag(self.timers, other.timers)
61 && flag(self.attachments, other.attachments)
62 && flag(self.voice, other.voice)
63 && flag(self.dms, other.dms)
64 && self.storage_bytes <= other.storage_bytes
65 && self.network.iter().all(|h| other.network.contains(h))
66 && self
67 .local_network
68 .iter()
69 .all(|a| other.local_network.contains(a))
70 }
71
72 #[must_use]
74 pub fn intersect(&self, other: &Self) -> Self {
75 Self {
76 message_content: self.message_content && other.message_content,
77 members: self.members && other.members,
78 presence: self.presence && other.presence,
79 timers: self.timers && other.timers,
80 attachments: self.attachments && other.attachments,
81 voice: self.voice && other.voice,
82 dms: self.dms && other.dms,
83 storage_bytes: self.storage_bytes.min(other.storage_bytes),
84 network: self
85 .network
86 .iter()
87 .filter(|h| other.network.contains(h))
88 .cloned()
89 .collect(),
90 local_network: self
91 .local_network
92 .iter()
93 .filter(|a| other.local_network.contains(a))
94 .cloned()
95 .collect(),
96 }
97 }
98}
99
100#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
102pub struct Grants {
103 pub permissions: Permissions,
105 pub capabilities: Capabilities,
107}
108
109impl Grants {
110 #[must_use]
112 pub fn within(&self, other: &Self) -> bool {
113 other.permissions.contains(self.permissions)
114 && self.capabilities.within(&other.capabilities)
115 }
116
117 #[must_use]
119 pub fn intersect(&self, other: &Self) -> Self {
120 Self {
121 permissions: self.permissions & other.permissions,
122 capabilities: self.capabilities.intersect(&other.capabilities),
123 }
124 }
125}
126
127#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
129pub struct ManifestLimits {
130 #[serde(default, skip_serializing_if = "Option::is_none")]
132 pub memory_bytes: Option<u64>,
133 #[serde(default, skip_serializing_if = "Option::is_none")]
135 pub cpu_ms: Option<u32>,
136}
137
138#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
140pub struct ConfigKey {
141 pub description: String,
143 #[serde(default, skip_serializing_if = "Option::is_none")]
145 pub default: Option<String>,
146 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
148 pub secret: bool,
149}
150
151#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
153pub struct Manifest {
154 pub app_id: String,
156 pub version: String,
158 pub name: String,
160 #[serde(default, skip_serializing_if = "Option::is_none")]
162 pub description: Option<String>,
163 #[serde(default)]
165 pub permissions: Permissions,
166 #[serde(default)]
168 pub capabilities: Capabilities,
169 #[serde(default, skip_serializing_if = "Vec::is_empty")]
171 pub events: Vec<String>,
172 #[serde(default, skip_serializing_if = "Option::is_none")]
174 pub limits: Option<ManifestLimits>,
175 #[serde(default, skip_serializing_if = "Option::is_none")]
177 pub update_url: Option<String>,
178 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
181 pub config: BTreeMap<String, ConfigKey>,
182}
183
184impl Manifest {
185 #[must_use]
187 pub fn request(&self) -> Grants {
188 Grants {
189 permissions: self.permissions,
190 capabilities: self.capabilities.clone(),
191 }
192 }
193}
194
195#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
197#[serde(rename_all = "snake_case")]
198pub enum BotState {
199 Enabled,
201 Disabled,
203 PendingApproval,
205 Crashed,
207}
208
209#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
211#[serde(rename_all = "snake_case")]
212pub enum BotKind {
213 Hosted,
215 External,
217}
218
219#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
221pub struct BotCard {
222 pub bot_id: BotId,
224 pub member_id: MemberId,
226 pub app_id: String,
228 pub name: String,
230 #[serde(default, skip_serializing_if = "Option::is_none")]
232 pub description: Option<String>,
233 pub version: String,
235 pub publisher: Option<PublisherId>,
237 pub signed: bool,
239 pub kind: BotKind,
241 pub state: BotState,
243 pub permissions: Permissions,
245 pub capabilities: Capabilities,
247 #[serde(default, skip_serializing_if = "Option::is_none")]
249 pub requested: Option<Grants>,
250 #[serde(default)]
252 pub events: Vec<String>,
253 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
256 pub config: BTreeMap<String, ConfigKey>,
257 pub installed_at: u64,
259 pub installed_by: Option<MemberId>,
261 pub updated_at: u64,
263 pub granted_at: u64,
265}