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;
19pub use std::time::SystemTime as Time;
20#[cfg(not(test))]
21pub use std::time::SystemTime;
22
23use anyhow::{Context as _, Result, bail, ensure};
24use base64::Engine as _;
25use chrono::{Local, NaiveDateTime, NaiveTime, TimeZone};
26use deltachat_contact_tools::EmailAddress;
27#[cfg(test)]
28pub use deltachat_time::SystemTimeTools as SystemTime;
29use futures::TryStreamExt;
30use mailparse::MailHeaderMap;
31use mailparse::dateparse;
32use mailparse::headers::Headers;
33use num_traits::PrimInt;
34use rand::{Rng, thread_rng};
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 rng = thread_rng();
294
295 let mut arr = [0u8; 18];
297 rng.fill(&mut arr[..]);
298
299 base64::engine::general_purpose::URL_SAFE.encode(arr)
300}
301
302pub(crate) fn validate_id(s: &str) -> bool {
306 let alphabet = base64::alphabet::URL_SAFE.as_str();
307 s.chars().all(|c| alphabet.contains(c)) && s.len() > 10 && s.len() <= 32
308}
309
310pub(crate) fn create_outgoing_rfc724_mid() -> String {
315 let uuid = Uuid::new_v4();
323 format!("{uuid}@localhost")
324}
325
326pub fn get_filesuffix_lc(path_filename: &str) -> Option<String> {
328 Path::new(path_filename)
329 .extension()
330 .map(|p| p.to_string_lossy().to_lowercase())
331}
332
333pub fn get_filemeta(buf: &[u8]) -> Result<(u32, u32)> {
335 let image = image::ImageReader::new(Cursor::new(buf)).with_guessed_format()?;
336 let dimensions = image.into_dimensions()?;
337 Ok(dimensions)
338}
339
340pub(crate) fn get_abs_path(context: &Context, path: &Path) -> PathBuf {
345 if let Ok(p) = path.strip_prefix("$BLOBDIR") {
346 context.get_blobdir().join(p)
347 } else {
348 path.into()
349 }
350}
351
352pub(crate) async fn get_filebytes(context: &Context, path: &Path) -> Result<u64> {
353 let path_abs = get_abs_path(context, path);
354 let meta = fs::metadata(&path_abs).await?;
355 Ok(meta.len())
356}
357
358pub(crate) async fn delete_file(context: &Context, path: &Path) -> Result<()> {
359 let path_abs = get_abs_path(context, path);
360 if !path_abs.exists() {
361 bail!("path {} does not exist", path_abs.display());
362 }
363 if !path_abs.is_file() {
364 warn!(context, "refusing to delete non-file {}.", path.display());
365 bail!("not a file: \"{}\"", path.display());
366 }
367
368 let dpath = format!("{}", path.to_string_lossy());
369 fs::remove_file(path_abs)
370 .await
371 .with_context(|| format!("cannot delete {dpath:?}"))?;
372 context.emit_event(EventType::DeletedBlobFile(dpath));
373 Ok(())
374}
375
376pub(crate) fn sanitize_filename(mut name: &str) -> String {
384 for part in name.rsplit('/') {
385 if !part.is_empty() {
386 name = part;
387 break;
388 }
389 }
390 for part in name.rsplit('\\') {
391 if !part.is_empty() {
392 name = part;
393 break;
394 }
395 }
396
397 let opts = sanitize_filename::Options {
398 truncate: true,
399 windows: true,
400 replacement: "",
401 };
402 let name = sanitize_filename::sanitize_with_options(name, opts);
403
404 if name.starts_with('.') || name.is_empty() {
405 format!("file{name}")
406 } else {
407 name
408 }
409}
410
411#[derive(Debug)]
415pub(crate) struct TempPathGuard {
416 path: PathBuf,
417}
418
419impl TempPathGuard {
420 pub(crate) fn new(path: PathBuf) -> Self {
421 Self { path }
422 }
423}
424
425impl Drop for TempPathGuard {
426 fn drop(&mut self) {
427 let path = self.path.clone();
428 std::fs::remove_file(path).ok();
429 }
430}
431
432impl Deref for TempPathGuard {
433 type Target = Path;
434
435 fn deref(&self) -> &Self::Target {
436 &self.path
437 }
438}
439
440impl AsRef<Path> for TempPathGuard {
441 fn as_ref(&self) -> &Path {
442 self
443 }
444}
445
446pub(crate) async fn create_folder(context: &Context, path: &Path) -> Result<(), io::Error> {
447 let path_abs = get_abs_path(context, path);
448 if !path_abs.exists() {
449 match fs::create_dir_all(path_abs).await {
450 Ok(_) => Ok(()),
451 Err(err) => {
452 warn!(
453 context,
454 "Cannot create directory \"{}\": {}",
455 path.display(),
456 err
457 );
458 Err(err)
459 }
460 }
461 } else {
462 Ok(())
463 }
464}
465
466pub(crate) async fn write_file(
468 context: &Context,
469 path: &Path,
470 buf: &[u8],
471) -> Result<(), io::Error> {
472 let path_abs = get_abs_path(context, path);
473 fs::write(&path_abs, buf).await.map_err(|err| {
474 warn!(
475 context,
476 "Cannot write {} bytes to \"{}\": {}",
477 buf.len(),
478 path.display(),
479 err
480 );
481 err
482 })
483}
484
485pub async fn read_file(context: &Context, path: &Path) -> Result<Vec<u8>> {
487 let path_abs = get_abs_path(context, path);
488
489 match fs::read(&path_abs).await {
490 Ok(bytes) => Ok(bytes),
491 Err(err) => {
492 warn!(
493 context,
494 "Cannot read \"{}\" or file is empty: {}",
495 path.display(),
496 err
497 );
498 Err(err.into())
499 }
500 }
501}
502
503pub async fn open_file(context: &Context, path: &Path) -> Result<fs::File> {
504 let path_abs = get_abs_path(context, path);
505
506 match fs::File::open(&path_abs).await {
507 Ok(bytes) => Ok(bytes),
508 Err(err) => {
509 warn!(
510 context,
511 "Cannot read \"{}\" or file is empty: {}",
512 path.display(),
513 err
514 );
515 Err(err.into())
516 }
517 }
518}
519
520pub fn open_file_std(context: &Context, path: impl AsRef<Path>) -> Result<std::fs::File> {
521 let path_abs = get_abs_path(context, path.as_ref());
522
523 match std::fs::File::open(path_abs) {
524 Ok(bytes) => Ok(bytes),
525 Err(err) => {
526 warn!(
527 context,
528 "Cannot read \"{}\" or file is empty: {}",
529 path.as_ref().display(),
530 err
531 );
532 Err(err.into())
533 }
534 }
535}
536
537pub async fn read_dir(path: &Path) -> Result<Vec<fs::DirEntry>> {
539 let res = tokio_stream::wrappers::ReadDirStream::new(fs::read_dir(path).await?)
540 .try_collect()
541 .await?;
542 Ok(res)
543}
544
545pub(crate) fn time() -> i64 {
546 SystemTime::now()
547 .duration_since(SystemTime::UNIX_EPOCH)
548 .unwrap_or_default()
549 .as_secs() as i64
550}
551
552pub(crate) fn time_elapsed(time: &Time) -> Duration {
553 time.elapsed().unwrap_or_default()
554}
555
556#[derive(Debug, Default, Eq, PartialEq)]
558pub struct MailTo {
559 pub to: Vec<EmailAddress>,
560 pub subject: Option<String>,
561 pub body: Option<String>,
562}
563
564pub fn parse_mailto(mailto_url: &str) -> Option<MailTo> {
566 if let Ok(url) = Url::parse(mailto_url) {
567 if url.scheme() == "mailto" {
568 let mut mailto: MailTo = Default::default();
569 url.path().split(',').for_each(|email| {
571 if let Ok(email) = EmailAddress::new(email) {
572 mailto.to.push(email);
573 }
574 });
575
576 for (key, value) in url.query_pairs() {
578 if key == "subject" {
579 mailto.subject = Some(value.to_string());
580 } else if key == "body" {
581 mailto.body = Some(value.to_string());
582 }
583 }
584 Some(mailto)
585 } else {
586 None
587 }
588 } else {
589 None
590 }
591}
592
593pub(crate) trait IsNoneOrEmpty<T> {
594 fn is_none_or_empty(&self) -> bool;
597}
598impl<T> IsNoneOrEmpty<T> for Option<T>
599where
600 T: AsRef<str>,
601{
602 fn is_none_or_empty(&self) -> bool {
603 !matches!(self, Some(s) if !s.as_ref().is_empty())
604 }
605}
606
607pub(crate) trait ToOption<T> {
608 fn to_option(self) -> Option<T>;
609}
610impl<'a> ToOption<&'a str> for &'a String {
611 fn to_option(self) -> Option<&'a str> {
612 if self.is_empty() { None } else { Some(self) }
613 }
614}
615impl ToOption<String> for u16 {
616 fn to_option(self) -> Option<String> {
617 if self == 0 {
618 None
619 } else {
620 Some(self.to_string())
621 }
622 }
623}
624impl ToOption<String> for Option<i32> {
625 fn to_option(self) -> Option<String> {
626 match self {
627 None | Some(0) => None,
628 Some(v) => Some(v.to_string()),
629 }
630 }
631}
632
633pub fn remove_subject_prefix(last_subject: &str) -> String {
634 let subject_start = if last_subject.starts_with("Chat:") {
635 0
636 } else {
637 match last_subject.chars().take(5).position(|c| c == ':') {
641 Some(prefix_end) => prefix_end + 1,
642 None => 0,
643 }
644 };
645 last_subject
646 .chars()
647 .skip(subject_start)
648 .collect::<String>()
649 .trim()
650 .to_string()
651}
652
653fn 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 if iter.next().is_none() {
713 return Some(value);
714 }
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 inc_and_check<T: PrimInt + AddAssign + std::fmt::Debug>(
757 t: &mut T,
758 expected: T,
759) -> Result<()> {
760 *t += T::one();
761 ensure!(*t == expected, "Incremented value != {expected:?}");
762 Ok(())
763}
764
765#[cfg(test)]
766mod tools_tests;