1#![allow(missing_docs)]
5
6use std::borrow::Cow;
7use std::io::{Cursor, Write};
8use std::mem;
9use std::ops::{AddAssign, Deref};
10use std::path::{Path, PathBuf};
11use std::str::from_utf8;
12use std::time::Duration;
20pub use std::time::SystemTime as Time;
21#[cfg(not(test))]
22pub use std::time::SystemTime;
23
24use anyhow::{Context as _, Result, bail, ensure};
25use base64::Engine as _;
26use chrono::{Local, NaiveDateTime, NaiveTime, TimeZone};
27use deltachat_contact_tools::EmailAddress;
28#[cfg(test)]
29pub use deltachat_time::SystemTimeTools as SystemTime;
30use futures::TryStreamExt;
31use mailparse::MailHeaderMap;
32use mailparse::dateparse;
33use mailparse::headers::Headers;
34use num_traits::PrimInt;
35use tokio::{fs, io};
36use url::Url;
37use uuid::Uuid;
38
39use crate::chat::{add_device_msg, add_device_msg_with_importance};
40use crate::config::Config;
41use crate::constants::{self, DC_ELLIPSIS, DC_OUTDATED_WARNING_DAYS};
42use crate::context::Context;
43use crate::events::EventType;
44use crate::log::warn;
45use crate::message::{Message, Viewtype};
46use crate::stock_str;
47
48#[expect(clippy::arithmetic_side_effects)]
51pub(crate) fn truncate(buf: &str, approx_chars: usize) -> Cow<'_, str> {
52 let count = buf.chars().count();
53 if count <= approx_chars + DC_ELLIPSIS.len() {
54 return Cow::Borrowed(buf);
55 }
56 let end_pos = buf
57 .char_indices()
58 .nth(approx_chars)
59 .map(|(n, _)| n)
60 .unwrap_or_default();
61
62 if let Some(index) = buf.get(..end_pos).and_then(|s| s.rfind([' ', '\n'])) {
63 Cow::Owned(format!(
64 "{}{}",
65 buf.get(..=index).unwrap_or_default(),
66 DC_ELLIPSIS
67 ))
68 } else {
69 Cow::Owned(format!(
70 "{}{}",
71 buf.get(..end_pos).unwrap_or_default(),
72 DC_ELLIPSIS
73 ))
74 }
75}
76
77#[expect(clippy::arithmetic_side_effects)]
82pub(crate) fn truncate_by_lines(
83 buf: String,
84 max_lines: usize,
85 max_line_len: usize,
86) -> (String, bool) {
87 let mut lines = 0;
88 let mut line_chars = 0;
89 let mut break_point: Option<usize> = None;
90
91 for (index, char) in buf.char_indices() {
92 if char == '\n' {
93 line_chars = 0;
94 lines += 1;
95 } else {
96 line_chars += 1;
97 if line_chars > max_line_len {
98 line_chars = 1;
99 lines += 1;
100 }
101 }
102 if lines == max_lines {
103 break_point = Some(index);
104 break;
105 }
106 }
107
108 if let Some(end_pos) = break_point {
109 let text = {
111 if let Some(buffer) = buf.get(..end_pos) {
112 if let Some(index) = buffer.rfind([' ', '\n']) {
113 buf.get(..=index)
114 } else {
115 buf.get(..end_pos)
116 }
117 } else {
118 None
119 }
120 };
121
122 if let Some(truncated_text) = text {
123 (format!("{truncated_text}{DC_ELLIPSIS}"), true)
124 } else {
125 let error_text = "[Truncation of the message failed, this is a bug in the Delta Chat core. Please report it.\nYou can still open the full text to view the original message.]";
129 (error_text.to_string(), true)
130 }
131 } else {
132 (buf, false)
134 }
135}
136
137pub(crate) async fn truncate_msg_text(context: &Context, text: String) -> Result<(String, bool)> {
142 if context.get_config_bool(Config::Bot).await? {
143 return Ok((text, false));
144 }
145 Ok(truncate_by_lines(
147 text,
148 constants::DC_DESIRED_TEXT_LINES,
149 constants::DC_DESIRED_TEXT_LINE_LEN,
150 ))
151}
152
153pub fn timestamp_to_str(wanted: i64) -> String {
159 if let Some(ts) = Local.timestamp_opt(wanted, 0).single() {
160 ts.format("%Y.%m.%d %H:%M:%S").to_string()
161 } else {
162 "??.??.?? ??:??:??".to_string()
164 }
165}
166
167pub fn duration_to_str(duration: Duration) -> String {
169 let secs = duration.as_secs();
170 let h = secs / 3600;
171 let m = (secs % 3600) / 60;
172 let s = (secs % 3600) % 60;
173 format!("{h}h {m}m {s}s")
174}
175
176pub(crate) fn gm2local_offset() -> i64 {
177 let lt = Local::now();
180 i64::from(lt.offset().local_minus_utc())
181}
182
183pub fn get_release_timestamp() -> i64 {
186 NaiveDateTime::new(
187 *crate::release::DATE,
188 NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
189 )
190 .and_utc()
191 .timestamp_millis()
192 / 1_000
193}
194
195pub(crate) async fn maybe_add_time_based_warnings(context: &Context) {
199 if !maybe_warn_on_bad_time(context, time(), get_release_timestamp()).await {
200 maybe_warn_on_outdated(context, time(), get_release_timestamp()).await;
201 }
202}
203
204async fn maybe_warn_on_bad_time(context: &Context, now: i64, known_past_timestamp: i64) -> bool {
205 if now < known_past_timestamp {
206 let mut msg = Message::new(Viewtype::Text);
207 msg.text = stock_str::bad_time_msg_body(
208 context,
209 &Local.timestamp_opt(now, 0).single().map_or_else(
210 || "YY-MM-DD hh:mm:ss".to_string(),
211 |ts| ts.format("%Y-%m-%d %H:%M:%S").to_string(),
212 ),
213 );
214 if let Some(timestamp) = chrono::DateTime::<chrono::Utc>::from_timestamp(now, 0) {
215 add_device_msg_with_importance(
216 context,
217 Some(
218 format!(
219 "bad-time-warning-{}",
220 timestamp.format("%Y-%m-%d") )
222 .as_str(),
223 ),
224 Some(&mut msg),
225 true,
226 )
227 .await
228 .ok();
229 } else {
230 warn!(context, "Can't convert current timestamp");
231 }
232 return true;
233 }
234 false
235}
236
237#[expect(clippy::arithmetic_side_effects)]
238async fn maybe_warn_on_outdated(context: &Context, now: i64, approx_compile_time: i64) {
239 if now > approx_compile_time + DC_OUTDATED_WARNING_DAYS * 24 * 60 * 60 {
240 let mut msg = Message::new_text(stock_str::update_reminder_msg_body(context));
241 if let Some(timestamp) = chrono::DateTime::<chrono::Utc>::from_timestamp(now, 0) {
242 add_device_msg(
243 context,
244 Some(
245 format!(
246 "outdated-warning-{}",
247 timestamp.format("%Y-%m") )
249 .as_str(),
250 ),
251 Some(&mut msg),
252 )
253 .await
254 .ok();
255 }
256 }
257}
258
259pub(crate) fn create_id() -> String {
271 let mut arr = [0u8; 18];
273 rand::fill(&mut arr[..]);
274
275 base64::engine::general_purpose::URL_SAFE.encode(arr)
276}
277
278pub(crate) fn create_broadcast_secret() -> String {
285 let mut arr = [0u8; 33];
288 rand::fill(&mut arr[..]);
289
290 let mut res = base64::engine::general_purpose::URL_SAFE.encode(arr);
291 res.truncate(43);
292 res
293}
294
295pub(crate) fn validate_id(s: &str) -> bool {
299 let alphabet = base64::alphabet::URL_SAFE.as_str();
300 s.chars().all(|c| alphabet.contains(c)) && s.len() > 10 && s.len() <= 32
301}
302
303pub(crate) fn validate_broadcast_secret(s: &str) -> bool {
304 let alphabet = base64::alphabet::URL_SAFE.as_str();
305 s.chars().all(|c| alphabet.contains(c)) && s.len() >= 43 && s.len() <= 100
306}
307
308pub(crate) fn create_outgoing_rfc724_mid() -> String {
313 let uuid = Uuid::new_v4();
321 format!("{uuid}@localhost")
322}
323
324pub fn get_filesuffix_lc(path_filename: &str) -> Option<String> {
326 Path::new(path_filename)
327 .extension()
328 .map(|p| p.to_string_lossy().to_lowercase())
329}
330
331pub fn get_filemeta(buf: &[u8]) -> Result<(u32, u32)> {
333 let image = image::ImageReader::new(Cursor::new(buf)).with_guessed_format()?;
334 let dimensions = image.into_dimensions()?;
335 Ok(dimensions)
336}
337
338pub(crate) fn get_abs_path(context: &Context, path: &Path) -> PathBuf {
343 if let Ok(p) = path.strip_prefix("$BLOBDIR") {
344 context.get_blobdir().join(p)
345 } else {
346 path.into()
347 }
348}
349
350pub(crate) async fn get_filebytes(context: &Context, path: &Path) -> Result<u64> {
351 let path_abs = get_abs_path(context, path);
352 let meta = fs::metadata(&path_abs).await?;
353 Ok(meta.len())
354}
355
356pub(crate) async fn delete_file(context: &Context, path: &Path) -> Result<()> {
357 let path_abs = get_abs_path(context, path);
358 if !path_abs.exists() {
359 bail!("path {} does not exist", path_abs.display());
360 }
361 if !path_abs.is_file() {
362 warn!(context, "refusing to delete non-file {}.", path.display());
363 bail!("not a file: \"{}\"", path.display());
364 }
365
366 let dpath = format!("{}", path.to_string_lossy());
367 fs::remove_file(path_abs)
368 .await
369 .with_context(|| format!("cannot delete {dpath:?}"))?;
370 context.emit_event(EventType::DeletedBlobFile(dpath));
371 Ok(())
372}
373
374pub(crate) fn sanitize_filename(mut name: &str) -> String {
382 for part in name.rsplit('/') {
383 if !part.is_empty() {
384 name = part;
385 break;
386 }
387 }
388 for part in name.rsplit('\\') {
389 if !part.is_empty() {
390 name = part;
391 break;
392 }
393 }
394
395 let opts = sanitize_filename::Options {
396 truncate: true,
397 windows: true,
398 replacement: "",
399 };
400 let name = sanitize_filename::sanitize_with_options(name, opts);
401
402 if name.starts_with('.') || name.is_empty() {
403 format!("file{name}")
404 } else {
405 name
406 }
407}
408
409#[derive(Debug)]
413pub(crate) struct TempPathGuard {
414 path: PathBuf,
415}
416
417impl TempPathGuard {
418 pub(crate) fn new(path: PathBuf) -> Self {
419 Self { path }
420 }
421}
422
423impl Drop for TempPathGuard {
424 fn drop(&mut self) {
425 let path = self.path.clone();
426 std::fs::remove_file(path).ok();
427 }
428}
429
430impl Deref for TempPathGuard {
431 type Target = Path;
432
433 fn deref(&self) -> &Self::Target {
434 &self.path
435 }
436}
437
438impl AsRef<Path> for TempPathGuard {
439 fn as_ref(&self) -> &Path {
440 self
441 }
442}
443
444pub(crate) async fn create_folder(context: &Context, path: &Path) -> Result<(), io::Error> {
445 let path_abs = get_abs_path(context, path);
446 if !path_abs.exists() {
447 match fs::create_dir_all(path_abs).await {
448 Ok(_) => Ok(()),
449 Err(err) => {
450 warn!(
451 context,
452 "Cannot create directory \"{}\": {}",
453 path.display(),
454 err
455 );
456 Err(err)
457 }
458 }
459 } else {
460 Ok(())
461 }
462}
463
464pub(crate) async fn write_file(
466 context: &Context,
467 path: &Path,
468 buf: &[u8],
469) -> Result<(), io::Error> {
470 let path_abs = get_abs_path(context, path);
471 fs::write(&path_abs, buf).await.map_err(|err| {
472 warn!(
473 context,
474 "Cannot write {} bytes to \"{}\": {}",
475 buf.len(),
476 path.display(),
477 err
478 );
479 err
480 })
481}
482
483pub async fn read_file(context: &Context, path: &Path) -> Result<Vec<u8>> {
485 let path_abs = get_abs_path(context, path);
486
487 match fs::read(&path_abs).await {
488 Ok(bytes) => Ok(bytes),
489 Err(err) => {
490 warn!(
491 context,
492 "Cannot read \"{}\" or file is empty: {}",
493 path.display(),
494 err
495 );
496 Err(err.into())
497 }
498 }
499}
500
501pub async fn open_file(context: &Context, path: &Path) -> Result<fs::File> {
502 let path_abs = get_abs_path(context, path);
503
504 match fs::File::open(&path_abs).await {
505 Ok(bytes) => Ok(bytes),
506 Err(err) => {
507 warn!(
508 context,
509 "Cannot read \"{}\" or file is empty: {}",
510 path.display(),
511 err
512 );
513 Err(err.into())
514 }
515 }
516}
517
518pub fn open_file_std(context: &Context, path: impl AsRef<Path>) -> Result<std::fs::File> {
519 let path_abs = get_abs_path(context, path.as_ref());
520
521 match std::fs::File::open(path_abs) {
522 Ok(bytes) => Ok(bytes),
523 Err(err) => {
524 warn!(
525 context,
526 "Cannot read \"{}\" or file is empty: {}",
527 path.as_ref().display(),
528 err
529 );
530 Err(err.into())
531 }
532 }
533}
534
535pub async fn read_dir(path: &Path) -> Result<Vec<fs::DirEntry>> {
537 let res = tokio_stream::wrappers::ReadDirStream::new(fs::read_dir(path).await?)
538 .try_collect()
539 .await?;
540 Ok(res)
541}
542
543pub(crate) fn time() -> i64 {
544 SystemTime::now()
545 .duration_since(SystemTime::UNIX_EPOCH)
546 .unwrap_or_default()
547 .as_secs() as i64
548}
549
550pub(crate) fn time_elapsed(time: &Time) -> Duration {
551 time.elapsed().unwrap_or_default()
552}
553
554#[derive(Debug, Default, Eq, PartialEq)]
556pub struct MailTo {
557 pub to: Vec<EmailAddress>,
558 pub subject: Option<String>,
559 pub body: Option<String>,
560}
561
562pub fn parse_mailto(mailto_url: &str) -> Option<MailTo> {
564 if let Ok(url) = Url::parse(mailto_url) {
565 if url.scheme() == "mailto" {
566 let mut mailto: MailTo = Default::default();
567 url.path().split(',').for_each(|email| {
569 if let Ok(email) = EmailAddress::new(email) {
570 mailto.to.push(email);
571 }
572 });
573
574 for (key, value) in url.query_pairs() {
576 if key == "subject" {
577 mailto.subject = Some(value.to_string());
578 } else if key == "body" {
579 mailto.body = Some(value.to_string());
580 }
581 }
582 Some(mailto)
583 } else {
584 None
585 }
586 } else {
587 None
588 }
589}
590
591pub(crate) trait IsNoneOrEmpty<T> {
592 fn is_none_or_empty(&self) -> bool;
595}
596impl<T> IsNoneOrEmpty<T> for Option<T>
597where
598 T: AsRef<str>,
599{
600 fn is_none_or_empty(&self) -> bool {
601 !matches!(self, Some(s) if !s.as_ref().is_empty())
602 }
603}
604
605pub(crate) trait ToOption<T> {
606 fn to_option(self) -> Option<T>;
607}
608impl<'a> ToOption<&'a str> for &'a String {
609 fn to_option(self) -> Option<&'a str> {
610 if self.is_empty() { None } else { Some(self) }
611 }
612}
613impl ToOption<String> for u16 {
614 fn to_option(self) -> Option<String> {
615 if self == 0 {
616 None
617 } else {
618 Some(self.to_string())
619 }
620 }
621}
622impl ToOption<String> for Option<i32> {
623 fn to_option(self) -> Option<String> {
624 match self {
625 None | Some(0) => None,
626 Some(v) => Some(v.to_string()),
627 }
628 }
629}
630
631#[expect(clippy::arithmetic_side_effects)]
632pub fn remove_subject_prefix(last_subject: &str) -> String {
633 let subject_start = if last_subject.starts_with("Chat:") {
634 0
635 } else {
636 match last_subject.chars().take(5).position(|c| c == ':') {
640 Some(prefix_end) => prefix_end + 1,
641 None => 0,
642 }
643 };
644 last_subject
645 .chars()
646 .skip(subject_start)
647 .collect::<String>()
648 .trim()
649 .to_string()
650}
651
652#[expect(clippy::arithmetic_side_effects)]
655fn extract_address_from_receive_header<'a>(header: &'a str, start: &str) -> Option<&'a str> {
656 let header_len = header.len();
657 header.find(start).and_then(|mut begin| {
658 begin += start.len();
659 let end = header
660 .get(begin..)?
661 .find(|c: char| c.is_whitespace())
662 .unwrap_or(header_len);
663 header.get(begin..begin + end)
664 })
665}
666
667pub(crate) fn parse_receive_header(header: &str) -> String {
668 let header = header.replace(&['\r', '\n'][..], "");
669 let mut hop_info = String::from("Hop: ");
670
671 if let Some(from) = extract_address_from_receive_header(&header, "from ") {
672 hop_info += &format!("From: {}; ", from.trim());
673 }
674
675 if let Some(by) = extract_address_from_receive_header(&header, "by ") {
676 hop_info += &format!("By: {}; ", by.trim());
677 }
678
679 if let Ok(date) = dateparse(&header) {
680 #[cfg(test)]
682 let date_obj = chrono::Utc.timestamp_opt(date, 0).single();
683 #[cfg(not(test))]
684 let date_obj = Local.timestamp_opt(date, 0).single();
685
686 hop_info += &format!(
687 "Date: {}",
688 date_obj.map_or_else(|| "?".to_string(), |x| x.to_rfc2822())
689 );
690 };
691
692 hop_info
693}
694
695pub(crate) fn parse_receive_headers(headers: &Headers) -> String {
697 headers
698 .get_all_headers("Received")
699 .iter()
700 .rev()
701 .filter_map(|header_map_item| from_utf8(header_map_item.get_value_raw()).ok())
702 .map(parse_receive_header)
703 .collect::<Vec<_>>()
704 .join("\n")
705}
706
707pub(crate) fn single_value<T>(collection: impl IntoIterator<Item = T>) -> Option<T> {
710 let mut iter = collection.into_iter();
711 if let Some(value) = iter.next()
712 && iter.next().is_none()
713 {
714 return Some(value);
715 }
716 None
717}
718
719const BROTLI_BUFSZ: usize = 4096;
721
722pub(crate) fn buf_compress(buf: &[u8]) -> Result<Vec<u8>> {
729 if buf.is_empty() {
730 return Ok(Vec::new());
731 }
732 let q: u32 = if buf.len() > 1_000_000 { 4 } else { 6 };
737 let lgwin: u32 = 22; let mut compressor = brotli::CompressorWriter::new(Vec::new(), BROTLI_BUFSZ, q, lgwin);
739 compressor.write_all(buf)?;
740 Ok(compressor.into_inner())
741}
742
743pub(crate) fn buf_decompress(buf: &[u8]) -> Result<Vec<u8>> {
746 if buf.is_empty() {
747 return Ok(Vec::new());
748 }
749 let mut decompressor = brotli::DecompressorWriter::new(Vec::new(), BROTLI_BUFSZ);
750 decompressor.write_all(buf)?;
751 decompressor.flush()?;
752 Ok(mem::take(decompressor.get_mut()))
753}
754
755pub(crate) fn to_lowercase(s: &str) -> Cow<'_, str> {
757 match s.chars().all(char::is_lowercase) {
758 true => Cow::Borrowed(s),
759 false => Cow::Owned(s.to_lowercase()),
760 }
761}
762
763pub(crate) fn normalize_text(text: &str) -> Option<String> {
766 if text.is_ascii() {
767 return None;
768 };
769 Some(text.to_lowercase()).filter(|t| t != text)
770}
771
772#[expect(clippy::arithmetic_side_effects)]
774pub(crate) fn inc_and_check<T: PrimInt + AddAssign + std::fmt::Debug>(
775 t: &mut T,
776 expected: T,
777) -> Result<()> {
778 *t += T::one();
779 ensure!(*t == expected, "Incremented value != {expected:?}");
780 Ok(())
781}
782
783pub(crate) fn usize_to_u64(v: usize) -> u64 {
800 u64::try_from(v).unwrap_or(u64::MAX)
801}
802
803#[macro_export]
806macro_rules! ensure_and_debug_assert {
807 ($cond:expr, $($arg:tt)*) => {
808 let cond_val = $cond;
809 debug_assert!(cond_val, $($arg)*);
810 anyhow::ensure!(cond_val, $($arg)*);
811 };
812}
813
814#[macro_export]
817macro_rules! ensure_and_debug_assert_eq {
818 ($left:expr, $right:expr, $($arg:tt)*) => {
819 match (&$left, &$right) {
820 (left_val, right_val) => {
821 debug_assert_eq!(left_val, right_val, $($arg)*);
822 anyhow::ensure!(left_val == right_val, $($arg)*);
823 }
824 }
825 };
826}
827
828#[macro_export]
831macro_rules! ensure_and_debug_assert_ne {
832 ($left:expr, $right:expr, $($arg:tt)*) => {
833 match (&$left, &$right) {
834 (left_val, right_val) => {
835 debug_assert_ne!(left_val, right_val, $($arg)*);
836 anyhow::ensure!(left_val != right_val, $($arg)*);
837 }
838 }
839 };
840}
841
842#[macro_export]
845macro_rules! logged_debug_assert {
846 ($ctx:expr, $cond:expr, $($arg:tt)*) => {
847 let cond_val = $cond;
848 if !cond_val {
849 warn!($ctx, $($arg)*);
850 }
851 debug_assert!(cond_val, $($arg)*);
852 };
853}
854
855#[cfg(test)]
856mod tools_tests;