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