deltachat/
constants.rs

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