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
48pub(crate) fn truncate(buf: &str, approx_chars: usize) -> Cow<'_, str> {
51 let count = buf.chars().count();
52 if count <= approx_chars + DC_ELLIPSIS.len() {
53 return Cow::Borrowed(buf);
54 }
55 let end_pos = buf
56 .char_indices()
57 .nth(approx_chars)
58 .map(|(n, _)| n)
59 .unwrap_or_default();
60
61 if let Some(index) = buf.get(..end_pos).and_then(|s| s.rfind([' ', '\n'])) {
62 Cow::Owned(format!(
63 "{}{}",
64 &buf.get(..=index).unwrap_or_default(),
65 DC_ELLIPSIS
66 ))
67 } else {
68 Cow::Owned(format!(
69 "{}{}",
70 &buf.get(..end_pos).unwrap_or_default(),
71 DC_ELLIPSIS
72 ))
73 }
74}
75
76pub(crate) fn truncate_by_lines(
81 buf: String,
82 max_lines: usize,
83 max_line_len: usize,
84) -> (String, bool) {
85 let mut lines = 0;
86 let mut line_chars = 0;
87 let mut break_point: Option<usize> = None;
88
89 for (index, char) in buf.char_indices() {
90 if char == '\n' {
91 line_chars = 0;
92 lines += 1;
93 } else {
94 line_chars += 1;
95 if line_chars > max_line_len {
96 line_chars = 1;
97 lines += 1;
98 }
99 }
100 if lines == max_lines {
101 break_point = Some(index);
102 break;
103 }
104 }
105
106 if let Some(end_pos) = break_point {
107 let text = {
109 if let Some(buffer) = buf.get(..end_pos) {
110 if let Some(index) = buffer.rfind([' ', '\n']) {
111 buf.get(..=index)
112 } else {
113 buf.get(..end_pos)
114 }
115 } else {
116 None
117 }
118 };
119
120 if let Some(truncated_text) = text {
121 (format!("{truncated_text}{DC_ELLIPSIS}"), true)
122 } else {
123 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.]";
127 (error_text.to_string(), true)
128 }
129 } else {
130 (buf, false)
132 }
133}
134
135pub(crate) async fn truncate_msg_text(context: &Context, text: String) -> Result<(String, bool)> {
140 if context.get_config_bool(Config::Bot).await? {
141 return Ok((text, false));
142 }
143 Ok(truncate_by_lines(
145 text,
146 constants::DC_DESIRED_TEXT_LINES,
147 constants::DC_DESIRED_TEXT_LINE_LEN,
148 ))
149}
150
151pub fn timestamp_to_str(wanted: i64) -> String {
157 if let Some(ts) = Local.timestamp_opt(wanted, 0).single() {
158 ts.format("%Y.%m.%d %H:%M:%S").to_string()
159 } else {
160 "??.??.?? ??:??:??".to_string()
162 }
163}
164
165pub fn duration_to_str(duration: Duration) -> String {
167 let secs = duration.as_secs();
168 let h = secs / 3600;
169 let m = (secs % 3600) / 60;
170 let s = (secs % 3600) % 60;
171 format!("{h}h {m}m {s}s")
172}
173
174pub(crate) fn gm2local_offset() -> i64 {
175 let lt = Local::now();
178 i64::from(lt.offset().local_minus_utc())
179}
180
181pub(crate) fn smeared_time(context: &Context) -> i64 {
185 let now = time();
186 let ts = context.smeared_timestamp.current();
187 std::cmp::max(ts, now)
188}
189
190pub(crate) fn create_smeared_timestamp(context: &Context) -> i64 {
192 let now = time();
193 context.smeared_timestamp.create(now)
194}
195
196pub(crate) fn create_smeared_timestamps(context: &Context, count: usize) -> i64 {
200 let now = time();
201 context.smeared_timestamp.create_n(now, count as i64)
202}
203
204pub fn get_release_timestamp() -> i64 {
207 NaiveDateTime::new(
208 *crate::release::DATE,
209 NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
210 )
211 .and_utc()
212 .timestamp_millis()
213 / 1_000
214}
215
216pub(crate) async fn maybe_add_time_based_warnings(context: &Context) {
220 if !maybe_warn_on_bad_time(context, time(), get_release_timestamp()).await {
221 maybe_warn_on_outdated(context, time(), get_release_timestamp()).await;
222 }
223}
224
225async fn maybe_warn_on_bad_time(context: &Context, now: i64, known_past_timestamp: i64) -> bool {
226 if now < known_past_timestamp {
227 let mut msg = Message::new(Viewtype::Text);
228 msg.text = stock_str::bad_time_msg_body(
229 context,
230 &Local.timestamp_opt(now, 0).single().map_or_else(
231 || "YY-MM-DD hh:mm:ss".to_string(),
232 |ts| ts.format("%Y-%m-%d %H:%M:%S").to_string(),
233 ),
234 )
235 .await;
236 if let Some(timestamp) = chrono::DateTime::<chrono::Utc>::from_timestamp(now, 0) {
237 add_device_msg_with_importance(
238 context,
239 Some(
240 format!(
241 "bad-time-warning-{}",
242 timestamp.format("%Y-%m-%d") )
244 .as_str(),
245 ),
246 Some(&mut msg),
247 true,
248 )
249 .await
250 .ok();
251 } else {
252 warn!(context, "Can't convert current timestamp");
253 }
254 return true;
255 }
256 false
257}
258
259async fn maybe_warn_on_outdated(context: &Context, now: i64, approx_compile_time: i64) {
260 if now > approx_compile_time + DC_OUTDATED_WARNING_DAYS * 24 * 60 * 60 {
261 let mut msg = Message::new_text(stock_str::update_reminder_msg_body(context).await);
262 if let Some(timestamp) = chrono::DateTime::<chrono::Utc>::from_timestamp(now, 0) {
263 add_device_msg(
264 context,
265 Some(
266 format!(
267 "outdated-warning-{}",
268 timestamp.format("%Y-%m") )
270 .as_str(),
271 ),
272 Some(&mut msg),
273 )
274 .await
275 .ok();
276 }
277 }
278}
279
280pub(crate) fn create_id() -> String {
292 let mut arr = [0u8; 18];
294 rand::fill(&mut arr[..]);
295
296 base64::engine::general_purpose::URL_SAFE.encode(arr)
297}
298
299pub(crate) fn create_broadcast_secret() -> String {
306 let mut arr = [0u8; 33];
309 rand::fill(&mut arr[..]);
310
311 let mut res = base64::engine::general_purpose::URL_SAFE.encode(arr);
312 res.truncate(43);
313 res
314}
315
316pub(crate) fn validate_id(s: &str) -> bool {
320 let alphabet = base64::alphabet::URL_SAFE.as_str();
321 s.chars().all(|c| alphabet.contains(c)) && s.len() > 10 && s.len() <= 32
322}
323
324pub(crate) fn validate_broadcast_secret(s: &str) -> bool {
325 let alphabet = base64::alphabet::URL_SAFE.as_str();
326 s.chars().all(|c| alphabet.contains(c)) && s.len() >= 43 && s.len() <= 100
327}
328
329pub(crate) fn create_outgoing_rfc724_mid() -> String {
334 let uuid = Uuid::new_v4();
342 format!("{uuid}@localhost")
343}
344
345pub fn get_filesuffix_lc(path_filename: &str) -> Option<String> {
347 Path::new(path_filename)
348 .extension()
349 .map(|p| p.to_string_lossy().to_lowercase())
350}
351
352pub fn get_filemeta(buf: &[u8]) -> Result<(u32, u32)> {
354 let image = image::ImageReader::new(Cursor::new(buf)).with_guessed_format()?;
355 let dimensions = image.into_dimensions()?;
356 Ok(dimensions)
357}
358
359pub(crate) fn get_abs_path(context: &Context, path: &Path) -> PathBuf {
364 if let Ok(p) = path.strip_prefix("$BLOBDIR") {
365 context.get_blobdir().join(p)
366 } else {
367 path.into()
368 }
369}
370
371pub(crate) async fn get_filebytes(context: &Context, path: &Path) -> Result<u64> {
372 let path_abs = get_abs_path(context, path);
373 let meta = fs::metadata(&path_abs).await?;
374 Ok(meta.len())
375}
376
377pub(crate) async fn delete_file(context: &Context, path: &Path) -> Result<()> {
378 let path_abs = get_abs_path(context, path);
379 if !path_abs.exists() {
380 bail!("path {} does not exist", path_abs.display());
381 }
382 if !path_abs.is_file() {
383 warn!(context, "refusing to delete non-file {}.", path.display());
384 bail!("not a file: \"{}\"", path.display());
385 }
386
387 let dpath = format!("{}", path.to_string_lossy());
388 fs::remove_file(path_abs)
389 .await
390 .with_context(|| format!("cannot delete {dpath:?}"))?;
391 context.emit_event(EventType::DeletedBlobFile(dpath));
392 Ok(())
393}
394
395pub(crate) fn sanitize_filename(mut name: &str) -> String {
403 for part in name.rsplit('/') {
404 if !part.is_empty() {
405 name = part;
406 break;
407 }
408 }
409 for part in name.rsplit('\\') {
410 if !part.is_empty() {
411 name = part;
412 break;
413 }
414 }
415
416 let opts = sanitize_filename::Options {
417 truncate: true,
418 windows: true,
419 replacement: "",
420 };
421 let name = sanitize_filename::sanitize_with_options(name, opts);
422
423 if name.starts_with('.') || name.is_empty() {
424 format!("file{name}")
425 } else {
426 name
427 }
428}
429
430#[derive(Debug)]
434pub(crate) struct TempPathGuard {
435 path: PathBuf,
436}
437
438impl TempPathGuard {
439 pub(crate) fn new(path: PathBuf) -> Self {
440 Self { path }
441 }
442}
443
444impl Drop for TempPathGuard {
445 fn drop(&mut self) {
446 let path = self.path.clone();
447 std::fs::remove_file(path).ok();
448 }
449}
450
451impl Deref for TempPathGuard {
452 type Target = Path;
453
454 fn deref(&self) -> &Self::Target {
455 &self.path
456 }
457}
458
459impl AsRef<Path> for TempPathGuard {
460 fn as_ref(&self) -> &Path {
461 self
462 }
463}
464
465pub(crate) async fn create_folder(context: &Context, path: &Path) -> Result<(), io::Error> {
466 let path_abs = get_abs_path(context, path);
467 if !path_abs.exists() {
468 match fs::create_dir_all(path_abs).await {
469 Ok(_) => Ok(()),
470 Err(err) => {
471 warn!(
472 context,
473 "Cannot create directory \"{}\": {}",
474 path.display(),
475 err
476 );
477 Err(err)
478 }
479 }
480 } else {
481 Ok(())
482 }
483}
484
485pub(crate) async fn write_file(
487 context: &Context,
488 path: &Path,
489 buf: &[u8],
490) -> Result<(), io::Error> {
491 let path_abs = get_abs_path(context, path);
492 fs::write(&path_abs, buf).await.map_err(|err| {
493 warn!(
494 context,
495 "Cannot write {} bytes to \"{}\": {}",
496 buf.len(),
497 path.display(),
498 err
499 );
500 err
501 })
502}
503
504pub async fn read_file(context: &Context, path: &Path) -> Result<Vec<u8>> {
506 let path_abs = get_abs_path(context, path);
507
508 match fs::read(&path_abs).await {
509 Ok(bytes) => Ok(bytes),
510 Err(err) => {
511 warn!(
512 context,
513 "Cannot read \"{}\" or file is empty: {}",
514 path.display(),
515 err
516 );
517 Err(err.into())
518 }
519 }
520}
521
522pub async fn open_file(context: &Context, path: &Path) -> Result<fs::File> {
523 let path_abs = get_abs_path(context, path);
524
525 match fs::File::open(&path_abs).await {
526 Ok(bytes) => Ok(bytes),
527 Err(err) => {
528 warn!(
529 context,
530 "Cannot read \"{}\" or file is empty: {}",
531 path.display(),
532 err
533 );
534 Err(err.into())
535 }
536 }
537}
538
539pub fn open_file_std(context: &Context, path: impl AsRef<Path>) -> Result<std::fs::File> {
540 let path_abs = get_abs_path(context, path.as_ref());
541
542 match std::fs::File::open(path_abs) {
543 Ok(bytes) => Ok(bytes),
544 Err(err) => {
545 warn!(
546 context,
547 "Cannot read \"{}\" or file is empty: {}",
548 path.as_ref().display(),
549 err
550 );
551 Err(err.into())
552 }
553 }
554}
555
556pub async fn read_dir(path: &Path) -> Result<Vec<fs::DirEntry>> {
558 let res = tokio_stream::wrappers::ReadDirStream::new(fs::read_dir(path).await?)
559 .try_collect()
560 .await?;
561 Ok(res)
562}
563
564pub(crate) fn time() -> i64 {
565 SystemTime::now()
566 .duration_since(SystemTime::UNIX_EPOCH)
567 .unwrap_or_default()
568 .as_secs() as i64
569}
570
571pub(crate) fn time_elapsed(time: &Time) -> Duration {
572 time.elapsed().unwrap_or_default()
573}
574
575#[derive(Debug, Default, Eq, PartialEq)]
577pub struct MailTo {
578 pub to: Vec<EmailAddress>,
579 pub subject: Option<String>,
580 pub body: Option<String>,
581}
582
583pub fn parse_mailto(mailto_url: &str) -> Option<MailTo> {
585 if let Ok(url) = Url::parse(mailto_url) {
586 if url.scheme() == "mailto" {
587 let mut mailto: MailTo = Default::default();
588 url.path().split(',').for_each(|email| {
590 if let Ok(email) = EmailAddress::new(email) {
591 mailto.to.push(email);
592 }
593 });
594
595 for (key, value) in url.query_pairs() {
597 if key == "subject" {
598 mailto.subject = Some(value.to_string());
599 } else if key == "body" {
600 mailto.body = Some(value.to_string());
601 }
602 }
603 Some(mailto)
604 } else {
605 None
606 }
607 } else {
608 None
609 }
610}
611
612pub(crate) trait IsNoneOrEmpty<T> {
613 fn is_none_or_empty(&self) -> bool;
616}
617impl<T> IsNoneOrEmpty<T> for Option<T>
618where
619 T: AsRef<str>,
620{
621 fn is_none_or_empty(&self) -> bool {
622 !matches!(self, Some(s) if !s.as_ref().is_empty())
623 }
624}
625
626pub(crate) trait ToOption<T> {
627 fn to_option(self) -> Option<T>;
628}
629impl<'a> ToOption<&'a str> for &'a String {
630 fn to_option(self) -> Option<&'a str> {
631 if self.is_empty() { None } else { Some(self) }
632 }
633}
634impl ToOption<String> for u16 {
635 fn to_option(self) -> Option<String> {
636 if self == 0 {
637 None
638 } else {
639 Some(self.to_string())
640 }
641 }
642}
643impl ToOption<String> for Option<i32> {
644 fn to_option(self) -> Option<String> {
645 match self {
646 None | Some(0) => None,
647 Some(v) => Some(v.to_string()),
648 }
649 }
650}
651
652pub fn remove_subject_prefix(last_subject: &str) -> String {
653 let subject_start = if last_subject.starts_with("Chat:") {
654 0
655 } else {
656 match last_subject.chars().take(5).position(|c| c == ':') {
660 Some(prefix_end) => prefix_end + 1,
661 None => 0,
662 }
663 };
664 last_subject
665 .chars()
666 .skip(subject_start)
667 .collect::<String>()
668 .trim()
669 .to_string()
670}
671
672fn extract_address_from_receive_header<'a>(header: &'a str, start: &str) -> Option<&'a str> {
675 let header_len = header.len();
676 header.find(start).and_then(|mut begin| {
677 begin += start.len();
678 let end = header
679 .get(begin..)?
680 .find(|c: char| c.is_whitespace())
681 .unwrap_or(header_len);
682 header.get(begin..begin + end)
683 })
684}
685
686pub(crate) fn parse_receive_header(header: &str) -> String {
687 let header = header.replace(&['\r', '\n'][..], "");
688 let mut hop_info = String::from("Hop: ");
689
690 if let Some(from) = extract_address_from_receive_header(&header, "from ") {
691 hop_info += &format!("From: {}; ", from.trim());
692 }
693
694 if let Some(by) = extract_address_from_receive_header(&header, "by ") {
695 hop_info += &format!("By: {}; ", by.trim());
696 }
697
698 if let Ok(date) = dateparse(&header) {
699 #[cfg(test)]
701 let date_obj = chrono::Utc.timestamp_opt(date, 0).single();
702 #[cfg(not(test))]
703 let date_obj = Local.timestamp_opt(date, 0).single();
704
705 hop_info += &format!(
706 "Date: {}",
707 date_obj.map_or_else(|| "?".to_string(), |x| x.to_rfc2822())
708 );
709 };
710
711 hop_info
712}
713
714pub(crate) fn parse_receive_headers(headers: &Headers) -> String {
716 headers
717 .get_all_headers("Received")
718 .iter()
719 .rev()
720 .filter_map(|header_map_item| from_utf8(header_map_item.get_value_raw()).ok())
721 .map(parse_receive_header)
722 .collect::<Vec<_>>()
723 .join("\n")
724}
725
726pub(crate) fn single_value<T>(collection: impl IntoIterator<Item = T>) -> Option<T> {
729 let mut iter = collection.into_iter();
730 if let Some(value) = iter.next() {
731 if iter.next().is_none() {
732 return Some(value);
733 }
734 }
735 None
736}
737
738const BROTLI_BUFSZ: usize = 4096;
740
741pub(crate) fn buf_compress(buf: &[u8]) -> Result<Vec<u8>> {
748 if buf.is_empty() {
749 return Ok(Vec::new());
750 }
751 let q: u32 = if buf.len() > 1_000_000 { 4 } else { 6 };
756 let lgwin: u32 = 22; let mut compressor = brotli::CompressorWriter::new(Vec::new(), BROTLI_BUFSZ, q, lgwin);
758 compressor.write_all(buf)?;
759 Ok(compressor.into_inner())
760}
761
762pub(crate) fn buf_decompress(buf: &[u8]) -> Result<Vec<u8>> {
765 if buf.is_empty() {
766 return Ok(Vec::new());
767 }
768 let mut decompressor = brotli::DecompressorWriter::new(Vec::new(), BROTLI_BUFSZ);
769 decompressor.write_all(buf)?;
770 decompressor.flush()?;
771 Ok(mem::take(decompressor.get_mut()))
772}
773
774pub(crate) fn to_lowercase(s: &str) -> Cow<'_, str> {
776 match s.chars().all(char::is_lowercase) {
777 true => Cow::Borrowed(s),
778 false => Cow::Owned(s.to_lowercase()),
779 }
780}
781
782pub(crate) fn inc_and_check<T: PrimInt + AddAssign + std::fmt::Debug>(
784 t: &mut T,
785 expected: T,
786) -> Result<()> {
787 *t += T::one();
788 ensure!(*t == expected, "Incremented value != {expected:?}");
789 Ok(())
790}
791
792#[macro_export]
795macro_rules! ensure_and_debug_assert {
796 ($cond:expr, $($arg:tt)*) => {
797 let cond_val = $cond;
798 debug_assert!(cond_val, $($arg)*);
799 anyhow::ensure!(cond_val, $($arg)*);
800 };
801}
802
803#[macro_export]
806macro_rules! ensure_and_debug_assert_eq {
807 ($left:expr, $right:expr, $($arg:tt)*) => {
808 match (&$left, &$right) {
809 (left_val, right_val) => {
810 debug_assert_eq!(left_val, right_val, $($arg)*);
811 anyhow::ensure!(left_val == right_val, $($arg)*);
812 }
813 }
814 };
815}
816
817#[macro_export]
820macro_rules! ensure_and_debug_assert_ne {
821 ($left:expr, $right:expr, $($arg:tt)*) => {
822 match (&$left, &$right) {
823 (left_val, right_val) => {
824 debug_assert_ne!(left_val, right_val, $($arg)*);
825 anyhow::ensure!(left_val != right_val, $($arg)*);
826 }
827 }
828 };
829}
830
831#[macro_export]
834macro_rules! logged_debug_assert {
835 ($ctx:expr, $cond:expr, $($arg:tt)*) => {
836 let cond_val = $cond;
837 if !cond_val {
838 warn!($ctx, $($arg)*);
839 }
840 debug_assert!(cond_val, $($arg)*);
841 };
842}
843
844#[cfg(test)]
845mod tools_tests;