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
63pub const DC_HANDSHAKE_CONTINUE_NORMAL_PROCESSING: i32 = 0x01;
64pub const DC_HANDSHAKE_STOP_NORMAL_PROCESSING: i32 = 0x02;
65pub const DC_HANDSHAKE_ADD_DELETE_JOB: i32 = 0x04;
66
67pub(crate) const DC_FROM_HANDSHAKE: i32 = 0x01;
68
69pub const DC_GCL_ARCHIVED_ONLY: usize = 0x01;
70pub const DC_GCL_NO_SPECIALS: usize = 0x02;
71pub const DC_GCL_ADD_ALLDONE_HINT: usize = 0x04;
72pub const DC_GCL_FOR_FORWARDING: usize = 0x08;
73
74pub const DC_GCL_ADD_SELF: u32 = 0x02;
75pub const DC_GCL_ADDRESS: u32 = 0x04;
76
77// unchanged user avatars are resent to the recipients every some days
78pub(crate) const DC_RESEND_USER_AVATAR_DAYS: i64 = 14;
79
80// warn about an outdated app after a given number of days.
81// reference is the release date.
82// as not all system get speedy updates,
83// do not use too small value that will annoy users checking for nonexistent updates.
84// "90 days" has proven to be too short at some point (user were informed but there was no update)
85pub(crate) const DC_OUTDATED_WARNING_DAYS: i64 = 183;
86
87/// 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)
88pub const DC_CHAT_ID_TRASH: ChatId = ChatId::new(3);
89/// only an indicator in a chatlist
90pub const DC_CHAT_ID_ARCHIVED_LINK: ChatId = ChatId::new(6);
91/// only an indicator in a chatlist
92pub const DC_CHAT_ID_ALLDONE_HINT: ChatId = ChatId::new(7);
93/// larger chat IDs are "real" chats, their messages are "real" messages.
94pub const DC_CHAT_ID_LAST_SPECIAL: ChatId = ChatId::new(9);
95
96/// Chat type.
97#[derive(
98    Debug,
99    Display,
100    Clone,
101    Copy,
102    PartialEq,
103    Eq,
104    FromPrimitive,
105    ToPrimitive,
106    FromSql,
107    ToSql,
108    IntoStaticStr,
109    Serialize,
110    Deserialize,
111)]
112#[repr(u32)]
113pub enum Chattype {
114    /// A 1:1 chat, i.e. a normal chat with a single contact.
115    ///
116    /// Created by [`ChatId::create_for_contact`].
117    Single = 100,
118
119    /// Group chat.
120    ///
121    /// Created by [`crate::chat::create_group_chat`].
122    Group = 120,
123
124    /// An (unencrypted) mailing list,
125    /// created by an incoming mailing list email.
126    Mailinglist = 140,
127
128    /// Outgoing broadcast channel, called "Channel" in the UI.
129    ///
130    /// The user can send into this chat,
131    /// and all recipients will receive messages
132    /// in an `InBroadcast`.
133    ///
134    /// Called `broadcast` here rather than `channel`,
135    /// because the word "channel" already appears a lot in the code,
136    /// which would make it hard to grep for it.
137    ///
138    /// Created by [`crate::chat::create_broadcast`].
139    OutBroadcast = 160,
140
141    /// Incoming broadcast channel, called "Channel" in the UI.
142    ///
143    /// This chat is read-only,
144    /// and we do not know who the other recipients are.
145    ///
146    /// This is similar to a `MailingList`,
147    /// with the main difference being that
148    /// `InBroadcast`s are encrypted.
149    ///
150    /// Called `broadcast` here rather than `channel`,
151    /// because the word "channel" already appears a lot in the code,
152    /// which would make it hard to grep for it.
153    InBroadcast = 165,
154}
155
156pub const DC_MSG_ID_DAYMARKER: u32 = 9;
157pub const DC_MSG_ID_LAST_SPECIAL: u32 = 9;
158
159/// String that indicates that something is left out or truncated.
160pub(crate) const DC_ELLIPSIS: &str = "[...]";
161// how many lines desktop can display when fullscreen (fullscreen at zoomlevel 1x)
162// (taken from "subjective" testing what looks ok)
163pub const DC_DESIRED_TEXT_LINES: usize = 38;
164// how many chars desktop can display per line (from "subjective" testing)
165pub const DC_DESIRED_TEXT_LINE_LEN: usize = 100;
166
167/// Message length limit.
168///
169/// To keep bubbles and chat flow usable and to avoid problems with controls using very long texts,
170/// we limit the text length to `DC_DESIRED_TEXT_LEN`.  If the text is longer, the full text can be
171/// retrieved using has_html()/get_html().
172///
173/// Note that for simplicity maximum length is defined as the number of Unicode Scalar Values (Rust
174/// `char`s), not Unicode Grapheme Clusters.
175pub const DC_DESIRED_TEXT_LEN: usize = DC_DESIRED_TEXT_LINE_LEN * DC_DESIRED_TEXT_LINES;
176
177// Flags for configuring IMAP and SMTP servers.
178// These flags are optional
179// and may be set together with the username, password etc.
180// via dc_set_config() using the key "server_flags".
181
182/// Force OAuth2 authorization.
183///
184/// This flag does not skip automatic configuration.
185/// Before calling configure() with DC_LP_AUTH_OAUTH2 set,
186/// the user has to confirm access at the URL returned by dc_get_oauth2_url().
187pub const DC_LP_AUTH_OAUTH2: i32 = 0x2;
188
189/// Force NORMAL authorization, this is the default.
190/// If this flag is set, automatic configuration is skipped.
191pub const DC_LP_AUTH_NORMAL: i32 = 0x4;
192
193/// if none of these flags are set, the default is chosen
194pub const DC_LP_AUTH_FLAGS: i32 = DC_LP_AUTH_OAUTH2 | DC_LP_AUTH_NORMAL;
195
196// max. weight of images to send w/o recoding
197pub const BALANCED_IMAGE_BYTES: usize = 500_000;
198pub const WORSE_IMAGE_BYTES: usize = 130_000;
199
200// max. width/height and bytes of an avatar
201pub(crate) const BALANCED_AVATAR_SIZE: u32 = 512;
202pub(crate) const BALANCED_AVATAR_BYTES: usize = 60_000;
203pub(crate) const WORSE_AVATAR_SIZE: u32 = 128;
204pub(crate) const WORSE_AVATAR_BYTES: usize = 20_000; // this also fits to Outlook servers don't allowing headers larger than 32k.
205
206// max. width/height of images scaled down because of being too huge
207pub const BALANCED_IMAGE_SIZE: u32 = 1280;
208pub const WORSE_IMAGE_SIZE: u32 = 640;
209
210/// Limit for received images size. Bigger images become `Viewtype::File` to avoid excessive memory
211/// usage by UIs.
212pub const MAX_RCVD_IMAGE_PIXELS: u32 = 50_000_000;
213
214// Key for the folder configuration version (see below).
215pub(crate) const DC_FOLDERS_CONFIGURED_KEY: &str = "folders_configured";
216// this value can be increased if the folder configuration is changed and must be redone on next program start
217pub(crate) const DC_FOLDERS_CONFIGURED_VERSION: i32 = 5;
218
219// If more recipients are needed in SMTP's `RCPT TO:` header, the recipient list is split into
220// chunks. This does not affect MIME's `To:` header. Can be overwritten by setting
221// `max_smtp_rcpt_to` in the provider db.
222pub(crate) const DEFAULT_MAX_SMTP_RCPT_TO: usize = 50;
223
224/// How far the last quota check needs to be in the past to be checked by the background function (in seconds).
225pub(crate) const DC_BACKGROUND_FETCH_QUOTA_CHECK_RATELIMIT: u64 = 12 * 60 * 60; // 12 hours
226
227/// How far in the future the sender timestamp of a message is allowed to be, in seconds. Also used
228/// in the group membership consistency algo to reject outdated membership changes.
229pub(crate) const TIMESTAMP_SENT_TOLERANCE: i64 = 60;
230
231// To make text edits clearer for Non-Delta-MUA or old Delta Chats, edited text will be prefixed by EDITED_PREFIX.
232// Newer Delta Chats will remove the prefix as needed.
233pub(crate) const EDITED_PREFIX: &str = "✏️";
234
235// Strings needed to render the Autocrypt Setup Message.
236// Left untranslated as not being supported/recommended workflow and as translations would require deep knowledge.
237pub(crate) const ASM_SUBJECT: &str = "Autocrypt Setup Message";
238pub(crate) const ASM_BODY: &str = "This is the Autocrypt Setup Message \
239    used to transfer your end-to-end setup between clients.
240
241    To decrypt and use your setup, \
242    open the message in an Autocrypt-compliant client \
243    and enter the setup code presented on the generating device.
244
245    If you see this message in a chatmail client (Delta Chat, Arcane Chat, Delta Touch ...), \
246    use \"Settings / Add Second Device\" instead.";
247
248/// Period between `sql::housekeeping()` runs.
249pub(crate) const HOUSEKEEPING_PERIOD: i64 = 24 * 60 * 60;
250
251#[cfg(test)]
252mod tests {
253    use num_traits::FromPrimitive;
254
255    use super::*;
256
257    #[test]
258    fn test_chattype_values() {
259        // values may be written to disk and must not change
260        assert_eq!(Chattype::Single, Chattype::from_i32(100).unwrap());
261        assert_eq!(Chattype::Group, Chattype::from_i32(120).unwrap());
262        assert_eq!(Chattype::Mailinglist, Chattype::from_i32(140).unwrap());
263        assert_eq!(Chattype::OutBroadcast, Chattype::from_i32(160).unwrap());
264    }
265
266    #[test]
267    fn test_showemails_values() {
268        // values may be written to disk and must not change
269        assert_eq!(ShowEmails::All, ShowEmails::default());
270        assert_eq!(ShowEmails::Off, ShowEmails::from_i32(0).unwrap());
271        assert_eq!(
272            ShowEmails::AcceptedContacts,
273            ShowEmails::from_i32(1).unwrap()
274        );
275        assert_eq!(ShowEmails::All, ShowEmails::from_i32(2).unwrap());
276    }
277
278    #[test]
279    fn test_blocked_values() {
280        // values may be written to disk and must not change
281        assert_eq!(Blocked::Not, Blocked::default());
282        assert_eq!(Blocked::Not, Blocked::from_i32(0).unwrap());
283        assert_eq!(Blocked::Yes, Blocked::from_i32(1).unwrap());
284        assert_eq!(Blocked::Request, Blocked::from_i32(2).unwrap());
285    }
286
287    #[test]
288    fn test_mediaquality_values() {
289        // values may be written to disk and must not change
290        assert_eq!(MediaQuality::Balanced, MediaQuality::default());
291        assert_eq!(MediaQuality::Balanced, MediaQuality::from_i32(0).unwrap());
292        assert_eq!(MediaQuality::Worse, MediaQuality::from_i32(1).unwrap());
293    }
294}