zaun_community_protocol/limits.rs
1//! Protocol limits (specification §5.2, §10.5, §12). Hosts may be stricter and report the
2//! effective values in `session.welcome.limits`.
3//!
4//! Human-text limits count Unicode scalar values; each also has a byte ceiling of 4× that.
5
6/// Protocol version defined by this crate (specification §1.1).
7pub const PROTOCOL_VERSION: u16 = 1;
8
9/// Maximum reassembled envelope in bytes.
10pub const MAX_FRAME: usize = 131_072;
11/// Maximum command envelope, except `community.create` and `community.descriptor.publish`.
12pub const MAX_COMMAND: usize = 65_536;
13/// Fragments per frame.
14pub const MAX_FRAGMENTS: usize = 32;
15/// Milliseconds from the first to the last fragment before a reassembly is discarded.
16pub const FRAGMENT_TIMEOUT_MS: u64 = 60_000;
17/// Open reassemblies per connection; a further one discards the oldest.
18pub const MAX_OPEN_REASSEMBLIES: usize = 4;
19/// Unlisted variable-length fields in canonical encodings (specification §3.1).
20pub const MAX_CANONICAL_FIELD: usize = 65_536;
21
22/// Default in-flight window in SMP messages per member.
23pub const IN_FLIGHT_DEFAULT: u32 = 48;
24/// Lowest configurable in-flight window (the largest frame must fit).
25pub const IN_FLIGHT_MIN: u32 = 32;
26/// Highest configurable in-flight window (half the 128-message SMP queue stays free).
27pub const IN_FLIGHT_MAX: u32 = 64;
28
29/// Community name length (scalars).
30pub const COMMUNITY_NAME: (usize, usize) = (2, 100);
31/// Community description, maximum scalars.
32pub const COMMUNITY_DESCRIPTION_MAX: usize = 1_000;
33/// Channel name length.
34pub const CHANNEL_NAME: (usize, usize) = (1, 100);
35/// Channel topic, maximum scalars.
36pub const CHANNEL_TOPIC_MAX: usize = 1_024;
37/// Role name length.
38pub const ROLE_NAME: (usize, usize) = (1, 100);
39/// Member display name length.
40pub const DISPLAY_NAME: (usize, usize) = (1, 32);
41/// Message content length. The content may be empty when the message has an attachment or
42/// an embed (§8.5).
43pub const MESSAGE_CONTENT: (usize, usize) = (1, 4_000);
44/// Moderation reason, maximum scalars.
45pub const REASON_MAX: usize = 512;
46/// Report resolution `note`, maximum scalars.
47pub const REPORT_NOTE_MAX: usize = 512;
48/// Presence `activity`, maximum scalars.
49pub const ACTIVITY_MAX: usize = 128;
50/// Largest role colour, `0xFFFFFF` (§8.4).
51pub const COLOR_MAX: u32 = 0x00FF_FFFF;
52/// Most message ids in one `messages.bulk_deleted` event (§9.2).
53pub const BULK_DELETE_MAX: usize = 1_000;
54/// Emoji, maximum bytes.
55pub const EMOJI_MAX_BYTES: usize = 64;
56/// Distinct reaction emoji per message.
57pub const REACTIONS_PER_MESSAGE: usize = 20;
58/// Roles per community, including `@everyone`.
59pub const ROLES_PER_COMMUNITY: usize = 250;
60/// Roles per member.
61pub const ROLES_PER_MEMBER: usize = 250;
62/// Channels per community, including categories.
63pub const CHANNELS_PER_COMMUNITY: usize = 500;
64/// Overrides per channel.
65pub const OVERRIDES_PER_CHANNEL: usize = 100;
66/// Member mentions per message.
67pub const MENTION_MEMBERS: usize = 50;
68/// Role mentions per message.
69pub const MENTION_ROLES: usize = 20;
70/// Page size: `message.history`, `audit.list`, `invite.list`, `invite.uses`, `member.bans`,
71/// `membership.pending`, `report.list`.
72pub const PAGE_SMALL: usize = 100;
73/// Page size: `member.list`, `sync.events`.
74pub const PAGE_LARGE: usize = 200;
75/// Page size: `community.rotations` (§8.1).
76pub const ROTATIONS_PAGE: usize = 4;
77/// `sync.resume`: channels per request.
78pub const RESUME_CHANNELS: usize = 100;
79/// `sync.resume`: events per stream.
80pub const RESUME_EVENTS_PER_STREAM: usize = 50;
81/// `sync.snapshot` page.
82pub const SNAPSHOT_PAGE: usize = 100;
83/// Invite `max_uses` upper bound (0 means unlimited).
84pub const INVITE_MAX_USES: u32 = 100_000;
85/// Invite expiry upper bound in milliseconds (30 days).
86pub const INVITE_EXPIRY_MAX_MS: u64 = 30 * DAY_MS;
87/// Slowmode upper bound in seconds.
88pub const SLOWMODE_MAX_S: u32 = 21_600;
89/// Timeout upper bound in milliseconds (28 days).
90pub const TIMEOUT_MAX_MS: u64 = 28 * DAY_MS;
91/// Ban message-deletion window upper bound in milliseconds (7 days).
92pub const BAN_DELETE_WINDOW_MAX_MS: u64 = 7 * DAY_MS;
93/// Idempotency record retention lower bound in milliseconds.
94pub const IDEMPOTENCY_RETENTION_MS: u64 = DAY_MS;
95/// Unbound connection deadline (default) in milliseconds.
96pub const UNBOUND_DEADLINE_MS: u64 = 10 * 60_000;
97/// Pending members per community.
98pub const PENDING_PER_COMMUNITY: usize = 1_000;
99/// Pending expiry upper bound in milliseconds (30 days); default 7 days.
100pub const PENDING_EXPIRY_MAX_MS: u64 = 30 * DAY_MS;
101/// Membership request note, maximum scalars.
102pub const REQUEST_NOTE_MAX: usize = 500;
103/// Suspension upper bound in milliseconds (90 days), or until restored.
104pub const SUSPENSION_MAX_MS: u64 = 90 * DAY_MS;
105/// Lockdown duration bounds in milliseconds per `enable` (60 s – 7 days).
106pub const LOCKDOWN_MS: (u64, u64) = (60_000, 7 * DAY_MS);
107/// Largest lockdown `newcomer_window_s`: 30 days, in seconds (§8.10).
108pub const NEWCOMER_WINDOW_MAX_S: u64 = 30 * DAY_MS / 1000;
109/// Report description, maximum scalars.
110pub const REPORT_DESCRIPTION_MAX: usize = 2_000;
111/// Report evidence, maximum messages.
112pub const REPORT_EVIDENCE_MAX: usize = 20;
113/// `sync.ack` channels.
114pub const ACK_CHANNELS: usize = 100;
115/// Minimum interval between signals per member per channel, in milliseconds.
116pub const SIGNAL_INTERVAL_MS: u64 = 3_000;
117/// `last_n` history bounds.
118pub const LAST_N: (u32, u32) = (1, 10_000);
119/// Descriptor icon, maximum bytes (PNG or WebP).
120pub const ICON_MAX: usize = 49_152;
121/// `session.hello` capabilities: count and bytes each.
122pub const CAPABILITIES: (usize, usize) = (32, 64);
123
124/// Attachment file name, scalars.
125pub const ATTACHMENT_NAME: (usize, usize) = (1, 255);
126/// Attachment media type, bytes.
127pub const ATTACHMENT_MIME_MAX: usize = 127;
128/// Largest attachment the protocol allows, in bytes (XFTP's 1 GiB); hosts set lower limits.
129pub const ATTACHMENT_SIZE_MAX: u64 = 1 << 30;
130/// Attachments per message.
131pub const ATTACHMENTS_PER_MESSAGE: usize = 10;
132
133/// Embeds per message (§8.5).
134pub const EMBEDS_PER_MESSAGE: usize = 10;
135/// Embed `title`, scalars.
136pub const EMBED_TITLE: (usize, usize) = (1, 256);
137/// Embed `description`, scalars.
138pub const EMBED_DESCRIPTION: (usize, usize) = (1, 4_096);
139/// Embed `url` and `author.url`, maximum bytes.
140pub const EMBED_URL_MAX: usize = 2_048;
141/// Embed `author.name`, scalars.
142pub const EMBED_AUTHOR_NAME: (usize, usize) = (1, 256);
143/// Embed `footer.text`, scalars.
144pub const EMBED_FOOTER_TEXT: (usize, usize) = (1, 2_048);
145/// Fields per embed.
146pub const EMBED_FIELDS: usize = 25;
147/// Embed field `name`, scalars.
148pub const EMBED_FIELD_NAME: (usize, usize) = (1, 256);
149/// Embed field `value`, scalars.
150pub const EMBED_FIELD_VALUE: (usize, usize) = (1, 1_024);
151/// The text of all of a message's embeds together (titles, descriptions, field names and
152/// values, author names, footer texts), scalars.
153pub const EMBED_TEXT_TOTAL: usize = 6_000;
154
155/// A message's `content` and `embeds` together, as JSON bytes the host writes them
156/// ([`message_json_len`](crate::command::object::message_json_len)), so that every message,
157/// message event and report fits a page. One `message.send` never exceeds it (its envelope is
158/// at most [`MAX_COMMAND`]); an edit that adds long content beside large stored embeds can.
159pub const MESSAGE_JSON_MAX: usize = 65_536;
160/// The items of one paged response (`message.history` messages, `sync.events` events,
161/// `report.list` reports; for `sync.resume`, the whole body), as JSON bytes: below
162/// [`MAX_FRAME`] by enough for the envelope, so a page always fits one frame. A host stops
163/// before it would exceed this and signals the rest (`has_more`, `next`); it always keeps the
164/// first item, except in `sync.resume`, whose streams continue with `sync.events`.
165pub const PAGE_BYTES: usize = MAX_FRAME - 4_096;
166/// The evidence snapshots (content and embeds as JSON) one rendered report carries; the
167/// first snapshot always goes, and `evidence_next` names where the rest continue.
168pub const REPORT_SNAPSHOT_BYTES: usize = MESSAGE_JSON_MAX;
169
170/// One day in milliseconds.
171pub const DAY_MS: u64 = 86_400_000;
172
173/// Largest `key_package` of `voice.join`, decoded bytes (opaque to the host; the media
174/// layer's packages are 4 680 bytes).
175pub const VOICE_KEY_PACKAGE_MAX: usize = 6_144;
176/// Largest `data` of `voice.signal` (WebRTC SDP or ICE), decoded bytes.
177pub const VOICE_SIGNAL_MAX: usize = 8_192;
178/// Largest `sender_keys` of `voice.keys`, decoded bytes: the sender's signed message for one
179/// epoch, the same for every recipient (the media layer's are at most 5 468 bytes).
180pub const VOICE_SENDER_KEYS_MAX: usize = 6_144;
181/// Largest `wrapped` key of one `voice.keys` entry, decoded bytes (the media layer's are
182/// 1 168 bytes).
183pub const VOICE_WRAPPED_KEY_MAX: usize = 1_536;
184/// The most participants a voice channel can have on any host (`mesh_max` ≤ this). Every
185/// participant's key package is in the `voice.join` response, which must fit in
186/// [`MAX_FRAME`] (9 base64 packages of [`VOICE_KEY_PACKAGE_MAX`] bytes are about 74 KiB),
187/// and one `voice.keys` for the whole mesh must fit in [`MAX_COMMAND`].
188pub const VOICE_MESH_MAX: u32 = 10;
189/// `user_limit` of a voice channel.
190pub const VOICE_USER_LIMIT: (u32, u32) = (1, 99);
191/// Most ICE servers a host lists in `voice.join`.
192pub const ICE_SERVERS_MAX: usize = 8;
193/// Longest ICE server URL, username or credential, in bytes.
194pub const ICE_FIELD_MAX: usize = 256;
195
196/// The largest `count` of an anonymous `signal.typing` (§10.5): 3 means "three or more", so
197/// a larger channel never learns exactly how many are typing.
198pub const TYPING_COUNT_CAP: u32 = 3;
199
200/// Bot package header, manifest and component sizes, bytes (§4.9).
201pub const BOT_HEADER_MAX: usize = 4_096;
202/// The manifest JSON inside a bot package, bytes.
203pub const BOT_MANIFEST_MAX: usize = 65_536;
204/// The WebAssembly component inside a bot package, bytes.
205pub const BOT_COMPONENT_MAX: usize = 64 << 20;
206/// A bot's `description`, scalars.
207pub const BOT_DESCRIPTION_MAX: usize = 400;
208/// Network hosts a bot may ask for.
209pub const BOT_NETWORK_HOSTS: usize = 32;
210/// Settings a bot may declare (`config`).
211pub const BOT_CONFIG_KEYS: usize = 32;
212/// A setting's value or default, bytes.
213pub const BOT_CONFIG_VALUE_MAX: usize = 4_096;
214/// `local_network` addresses a bot may ask for.
215pub const BOT_LOCAL_NETWORK: usize = 8;
216/// A bot's key-value storage quota, bytes.
217pub const BOT_STORAGE_MAX: u64 = 1 << 30;
218/// Bots per community.
219pub const BOTS_PER_COMMUNITY: usize = 50;
220
221/// Application commands per bot: `CHAT_INPUT`, `USER`, `MESSAGE` (§8.14).
222pub const APP_COMMANDS: (usize, usize, usize) = (100, 15, 15);
223/// Command and option `name`, scalars.
224pub const APP_COMMAND_NAME: (usize, usize) = (1, 32);
225/// Command and option `description` (`CHAT_INPUT`), scalars.
226pub const APP_COMMAND_DESCRIPTION: (usize, usize) = (1, 100);
227/// Options per command or subcommand, and choices per option.
228pub const APP_COMMAND_OPTIONS: usize = 25;
229/// Choice `name` and string `value`, scalars.
230pub const APP_CHOICE_TEXT: (usize, usize) = (1, 100);
231/// All names, descriptions, choice names and values of one command, scalars.
232pub const APP_COMMAND_TEXT_TOTAL: usize = 8_000;
233/// String option `min_length` / `max_length` bound.
234pub const APP_OPTION_LENGTH_MAX: u32 = 6_000;
235/// Localized names per field.
236pub const APP_LOCALIZATIONS: usize = 50;
237
238/// Interaction answer deadline, ms (§8.14).
239pub const INTERACTION_ANSWER_MS: u64 = 3_000;
240/// Follow-ups, edits and modal submits are accepted this long after the interaction, ms.
241pub const INTERACTION_LIFETIME_MS: u64 = 15 * 60_000;
242/// Follow-ups per interaction.
243pub const INTERACTION_FOLLOWUPS: usize = 10;
244/// Autocomplete choices.
245pub const AUTOCOMPLETE_CHOICES: usize = 25;
246
247/// Component `custom_id`, bytes.
248pub const COMPONENT_CUSTOM_ID: (usize, usize) = (1, 100);
249/// Top-level action rows of a message without `IS_COMPONENTS_V2`.
250pub const COMPONENT_ROWS: usize = 5;
251/// Buttons per action row.
252pub const COMPONENT_ROW_BUTTONS: usize = 5;
253/// Components in one message, counting nested ones.
254pub const COMPONENTS_TOTAL: usize = 40;
255/// Button `label`, scalars.
256pub const BUTTON_LABEL_MAX: usize = 80;
257/// Select options.
258pub const SELECT_OPTIONS: usize = 25;
259/// Select option `label`, `value`, `description`, scalars.
260pub const SELECT_OPTION_TEXT: usize = 100;
261/// Select `placeholder`, scalars.
262pub const SELECT_PLACEHOLDER_MAX: usize = 150;
263/// Text input value and length bounds, scalars.
264pub const TEXT_INPUT_MAX: u32 = 4_000;
265/// Text input `placeholder`, scalars.
266pub const TEXT_INPUT_PLACEHOLDER_MAX: usize = 100;
267/// All text displays of one message or modal together, scalars.
268pub const TEXT_DISPLAY_TOTAL: usize = 4_000;
269/// Section text displays.
270pub const SECTION_TEXTS: (usize, usize) = (1, 3);
271/// Media description, scalars.
272pub const MEDIA_DESCRIPTION_MAX: usize = 1_024;
273/// Media gallery items.
274pub const MEDIA_GALLERY_ITEMS: (usize, usize) = (1, 10);
275/// Modal `title`, scalars.
276pub const MODAL_TITLE: (usize, usize) = (1, 45);
277/// Top-level modal items.
278pub const MODAL_ITEMS: (usize, usize) = (1, 5);
279/// Label `label` and `description`, scalars.
280pub const LABEL_TEXT: (usize, usize) = (45, 100);
281/// Files in one file upload.
282pub const FILE_UPLOAD_MAX: u32 = 10;
283/// Radio group options.
284pub const RADIO_OPTIONS: (usize, usize) = (2, 10);
285/// Checkbox group options.
286pub const CHECKBOX_OPTIONS: (usize, usize) = (1, 10);
287
288/// New DMs one bot may open per minute (§8.15).
289pub const DM_OPENS_PER_MINUTE: usize = 10;
290/// Messages a bot may write into a DM per hour while the member has never written in it.
291pub const DM_UNANSWERED_PER_HOUR: usize = 5;
292
293/// Whether `text` has between `min` and `max` Unicode scalar values and at most 4× `max` bytes.
294#[must_use]
295pub fn text_within(text: &str, (min, max): (usize, usize)) -> bool {
296 if text.len() > max.saturating_mul(4) {
297 return false;
298 }
299 let scalars = text.chars().count();
300 (min..=max).contains(&scalars)
301}
302
303#[cfg(test)]
304mod tests {
305 use super::*;
306
307 #[test]
308 fn text_counts_scalars() {
309 assert!(text_within("ab", COMMUNITY_NAME));
310 assert!(!text_within("a", COMMUNITY_NAME));
311 assert!(text_within(&"é".repeat(32), DISPLAY_NAME));
312 assert!(!text_within(&"é".repeat(33), DISPLAY_NAME));
313 assert!(!text_within("", MESSAGE_CONTENT));
314 }
315
316 // The window must hold the largest frame and leave half the 128-message queue free.
317 const _: () = assert!(IN_FLIGHT_MIN as usize >= MAX_FRAGMENTS);
318 const _: () = assert!(IN_FLIGHT_MAX <= 64);
319
320 // The largest `voice.join` response fits in one frame: the other participants' base64
321 // key packages (plus about 400 bytes of fields each) and the ICE servers.
322 const B64_PACKAGE: usize = VOICE_KEY_PACKAGE_MAX.div_ceil(3) * 4;
323 const ICE_SERVER: usize = 3 * ICE_FIELD_MAX.div_ceil(3) * 4 + 64;
324 const _: () = assert!(
325 (VOICE_MESH_MAX as usize - 1) * (B64_PACKAGE + 400) + ICE_SERVERS_MAX * ICE_SERVER + 1_024
326 < MAX_FRAME
327 );
328 // A whole-mesh `voice.keys` (one sender-keys message, a wrapped key per other
329 // participant) fits in one command envelope, so it never needs splitting.
330 const B64_SENDER: usize = VOICE_SENDER_KEYS_MAX.div_ceil(3) * 4;
331 const B64_WRAPPED: usize = VOICE_WRAPPED_KEY_MAX.div_ceil(3) * 4;
332 const _: () = assert!(
333 B64_SENDER + (VOICE_MESH_MAX as usize - 1) * (B64_WRAPPED + 64) + 1_024 < MAX_COMMAND
334 );
335 const _: () = assert!(IN_FLIGHT_MIN <= IN_FLIGHT_DEFAULT && IN_FLIGHT_DEFAULT <= IN_FLIGHT_MAX);
336}