deltachat/
constants.rs

1//! # Constants.
2
3#![allow(missing_docs)]
4
5use std::sync::LazyLock;
6
7use deltachat_derive::{FromSql, ToSql};
8use percent_encoding::{AsciiSet, NON_ALPHANUMERIC};
9use serde::{Deserialize, Serialize};
10
11use crate::chat::ChatId;
12
13pub static DC_VERSION_STR: LazyLock<String> =
14    LazyLock::new(|| env!("CARGO_PKG_VERSION").to_string());
15
16/// Set of characters to percent-encode in email addresses and names.
17pub(crate) const NON_ALPHANUMERIC_WITHOUT_DOT: &AsciiSet = &NON_ALPHANUMERIC.remove(b'.');
18
19#[derive(
20    Debug,
21    Default,
22    Display,
23    Clone,
24    Copy,
25    PartialEq,
26    Eq,
27    FromPrimitive,
28    ToPrimitive,
29    FromSql,
30    ToSql,
31    Serialize,
32    Deserialize,
33)]
34#[repr(i8)]
35pub enum Blocked {
36    #[default]
37    Not = 0,
38    Yes = 1,
39    Request = 2,
40}
41
42#[derive(
43    Debug, Default, Display, Clone, Copy, PartialEq, Eq, FromPrimitive, ToPrimitive, FromSql, ToSql,
44)]
45#[repr(u8)]
46pub enum ShowEmails {
47    Off = 0,
48    AcceptedContacts = 1,
49    #[default] // also change Config.ShowEmails props(default) on changes
50    All = 2,
51}
52
53#[derive(
54    Debug, Default, Display, Clone, Copy, PartialEq, Eq, FromPrimitive, ToPrimitive, FromSql, ToSql,
55)]
56#[repr(u8)]
57pub enum MediaQuality {
58    #[default] // also change Config.MediaQuality props(default) on changes
59    Balanced = 0,
60    Worse = 1,
61}
62
63/// Video chat URL type.
64#[derive(
65    Debug, Default, Display, Clone, Copy, PartialEq, Eq, FromPrimitive, ToPrimitive, FromSql, ToSql,
66)]
67#[repr(i8)]
68pub enum VideochatType {
69    /// Unknown type.
70    #[default]
71    Unknown = 0,
72
73    /// [basicWebRTC](https://github.com/cracker0dks/basicwebrtc) instance.
74    BasicWebrtc = 1,
75
76    /// [Jitsi Meet](https://jitsi.org/jitsi-meet/) instance.
77    Jitsi = 2,
78}
79
80pub const DC_HANDSHAKE_CONTINUE_NORMAL_PROCESSING: i32 = 0x01;
81pub const DC_HANDSHAKE_STOP_NORMAL_PROCESSING: i32 = 0x02;
82pub const DC_HANDSHAKE_ADD_DELETE_JOB: i32 = 0x04;
83
84pub(crate) const DC_FROM_HANDSHAKE: i32 = 0x01;
85
86pub const DC_GCL_ARCHIVED_ONLY: usize = 0x01;
87pub const DC_GCL_NO_SPECIALS: usize = 0x02;
88pub const DC_GCL_ADD_ALLDONE_HINT: usize = 0x04;
89pub const DC_GCL_FOR_FORWARDING: usize = 0x08;
90
91pub const DC_GCL_VERIFIED_ONLY: u32 = 0x01;
92pub const DC_GCL_ADD_SELF: u32 = 0x02;
93
94// unchanged user avatars are resent to the recipients every some days
95pub(crate) const DC_RESEND_USER_AVATAR_DAYS: i64 = 14;
96
97// warn about an outdated app after a given number of days.
98// as we use the "provider-db generation date" as reference (that might not be updated very often)
99// and as not all system get speedy updates,
100// do not use too small value that will annoy users checking for nonexistent updates.
101pub(crate) const DC_OUTDATED_WARNING_DAYS: i64 = 365;
102
103/// messages that should be deleted get this chat_id; the messages are deleted from the working thread later then. This is also needed as rfc724_mid should be preset as long as the message is not deleted on the server (otherwise it is downloaded again)
104pub const DC_CHAT_ID_TRASH: ChatId = ChatId::new(3);
105/// only an indicator in a chatlist
106pub const DC_CHAT_ID_ARCHIVED_LINK: ChatId = ChatId::new(6);
107/// only an indicator in a chatlist
108pub const DC_CHAT_ID_ALLDONE_HINT: ChatId = ChatId::new(7);
109/// larger chat IDs are "real" chats, their messages are "real" messages.
110pub const DC_CHAT_ID_LAST_SPECIAL: ChatId = ChatId::new(9);
111
112/// Chat type.
113#[derive(
114    Debug,
115    Display,
116    Clone,
117    Copy,
118    PartialEq,
119    Eq,
120    FromPrimitive,
121    ToPrimitive,
122    FromSql,
123    ToSql,
124    IntoStaticStr,
125    Serialize,
126    Deserialize,
127)]
128#[repr(u32)]
129pub enum Chattype {
130    /// 1:1 chat.
131    Single = 100,
132
133    /// Group chat.
134    Group = 120,
135
136    /// Mailing list.
137    Mailinglist = 140,
138
139    /// Broadcast list.
140    Broadcast = 160,
141}
142
143pub const DC_MSG_ID_DAYMARKER: u32 = 9;
144pub const DC_MSG_ID_LAST_SPECIAL: u32 = 9;
145
146/// String that indicates that something is left out or truncated.
147pub(crate) const DC_ELLIPSIS: &str = "[...]";
148// how many lines desktop can display when fullscreen (fullscreen at zoomlevel 1x)
149// (taken from "subjective" testing what looks ok)
150pub const DC_DESIRED_TEXT_LINES: usize = 38;
151// how many chars desktop can display per line (from "subjective" testing)
152pub const DC_DESIRED_TEXT_LINE_LEN: usize = 100;
153
154/// Message length limit.
155///
156/// To keep bubbles and chat flow usable and to avoid problems with controls using very long texts,
157/// we limit the text length to `DC_DESIRED_TEXT_LEN`.  If the text is longer, the full text can be
158/// retrieved using has_html()/get_html().
159///
160/// Note that for simplicity maximum length is defined as the number of Unicode Scalar Values (Rust
161/// `char`s), not Unicode Grapheme Clusters.
162pub const DC_DESIRED_TEXT_LEN: usize = DC_DESIRED_TEXT_LINE_LEN * DC_DESIRED_TEXT_LINES;
163
164// Flags for configuring IMAP and SMTP servers.
165// These flags are optional
166// and may be set together with the username, password etc.
167// via dc_set_config() using the key "server_flags".
168
169/// Force OAuth2 authorization.
170///
171/// This flag does not skip automatic configuration.
172/// Before calling configure() with DC_LP_AUTH_OAUTH2 set,
173/// the user has to confirm access at the URL returned by dc_get_oauth2_url().
174pub const DC_LP_AUTH_OAUTH2: i32 = 0x2;
175
176/// Force NORMAL authorization, this is the default.
177/// If this flag is set, automatic configuration is skipped.
178pub const DC_LP_AUTH_NORMAL: i32 = 0x4;
179
180/// if none of these flags are set, the default is chosen
181pub const DC_LP_AUTH_FLAGS: i32 = DC_LP_AUTH_OAUTH2 | DC_LP_AUTH_NORMAL;
182
183// max. weight of images to send w/o recoding
184pub const BALANCED_IMAGE_BYTES: usize = 500_000;
185pub const WORSE_IMAGE_BYTES: usize = 130_000;
186
187// max. width/height and bytes of an avatar
188pub(crate) const BALANCED_AVATAR_SIZE: u32 = 512;
189pub(crate) const BALANCED_AVATAR_BYTES: usize = 60_000;
190pub(crate) const WORSE_AVATAR_SIZE: u32 = 128;
191pub(crate) const WORSE_AVATAR_BYTES: usize = 20_000; // this also fits to Outlook servers don't allowing headers larger than 32k.
192
193// max. width/height of images scaled down because of being too huge
194pub const BALANCED_IMAGE_SIZE: u32 = 1280;
195pub const WORSE_IMAGE_SIZE: u32 = 640;
196
197// Key for the folder configuration version (see below).
198pub(crate) const DC_FOLDERS_CONFIGURED_KEY: &str = "folders_configured";
199// this value can be increased if the folder configuration is changed and must be redone on next program start
200pub(crate) const DC_FOLDERS_CONFIGURED_VERSION: i32 = 5;
201
202// If more recipients are needed in SMTP's `RCPT TO:` header, the recipient list is split into
203// chunks. This does not affect MIME's `To:` header. Can be overwritten by setting
204// `max_smtp_rcpt_to` in the provider db.
205pub(crate) const DEFAULT_MAX_SMTP_RCPT_TO: usize = 50;
206
207/// How far the last quota check needs to be in the past to be checked by the background function (in seconds).
208pub(crate) const DC_BACKGROUND_FETCH_QUOTA_CHECK_RATELIMIT: u64 = 12 * 60 * 60; // 12 hours
209
210/// How far in the future the sender timestamp of a message is allowed to be, in seconds. Also used
211/// in the group membership consistency algo to reject outdated membership changes.
212pub(crate) const TIMESTAMP_SENT_TOLERANCE: i64 = 60;
213
214/// How long a 1:1 chat can't be used for sending while the SecureJoin is in progress. This should
215/// be 10-20 seconds so that we are reasonably sure that the app remains active and receiving also
216/// on mobile devices. See also [`crate::chat::CantSendReason::SecurejoinWait`].
217pub(crate) const SECUREJOIN_WAIT_TIMEOUT: u64 = 15;
218
219// To make text edits clearer for Non-Delta-MUA or old Delta Chats, edited text will be prefixed by EDITED_PREFIX.
220// Newer Delta Chats will remove the prefix as needed.
221pub(crate) const EDITED_PREFIX: &str = "✏️";
222
223// Strings needed to render the Autocrypt Setup Message.
224// Left untranslated as not being supported/recommended workflow and as translations would require deep knowledge.
225pub(crate) const ASM_SUBJECT: &str = "Autocrypt Setup Message";
226pub(crate) const ASM_BODY: &str = "This is the Autocrypt Setup Message \
227    used to transfer your end-to-end setup between clients.
228
229    To decrypt and use your setup, \
230    open the message in an Autocrypt-compliant client \
231    and enter the setup code presented on the generating device.
232
233    If you see this message in a chatmail client (Delta Chat, Arcane Chat, Delta Touch ...), \
234    use \"Settings / Add Second Device\" instead.";
235
236#[cfg(test)]
237mod tests {
238    use num_traits::FromPrimitive;
239
240    use super::*;
241
242    #[test]
243    fn test_chattype_values() {
244        // values may be written to disk and must not change
245        assert_eq!(Chattype::Single, Chattype::from_i32(100).unwrap());
246        assert_eq!(Chattype::Group, Chattype::from_i32(120).unwrap());
247        assert_eq!(Chattype::Mailinglist, Chattype::from_i32(140).unwrap());
248        assert_eq!(Chattype::Broadcast, Chattype::from_i32(160).unwrap());
249    }
250
251    #[test]
252    fn test_showemails_values() {
253        // values may be written to disk and must not change
254        assert_eq!(ShowEmails::All, ShowEmails::default());
255        assert_eq!(ShowEmails::Off, ShowEmails::from_i32(0).unwrap());
256        assert_eq!(
257            ShowEmails::AcceptedContacts,
258            ShowEmails::from_i32(1).unwrap()
259        );
260        assert_eq!(ShowEmails::All, ShowEmails::from_i32(2).unwrap());
261    }
262
263    #[test]
264    fn test_blocked_values() {
265        // values may be written to disk and must not change
266        assert_eq!(Blocked::Not, Blocked::default());
267        assert_eq!(Blocked::Not, Blocked::from_i32(0).unwrap());
268        assert_eq!(Blocked::Yes, Blocked::from_i32(1).unwrap());
269        assert_eq!(Blocked::Request, Blocked::from_i32(2).unwrap());
270    }
271
272    #[test]
273    fn test_mediaquality_values() {
274        // values may be written to disk and must not change
275        assert_eq!(MediaQuality::Balanced, MediaQuality::default());
276        assert_eq!(MediaQuality::Balanced, MediaQuality::from_i32(0).unwrap());
277        assert_eq!(MediaQuality::Worse, MediaQuality::from_i32(1).unwrap());
278    }
279
280    #[test]
281    fn test_videochattype_values() {
282        // values may be written to disk and must not change
283        assert_eq!(VideochatType::Unknown, VideochatType::default());
284        assert_eq!(VideochatType::Unknown, VideochatType::from_i32(0).unwrap());
285        assert_eq!(
286            VideochatType::BasicWebrtc,
287            VideochatType::from_i32(1).unwrap()
288        );
289        assert_eq!(VideochatType::Jitsi, VideochatType::from_i32(2).unwrap());
290    }
291}