|
|
use std::collections::BTreeMap; |
|
|
use std::fmt; |
|
|
use std::path::PathBuf; |
|
|
use std::str; |
|
|
|
|
|
use anyhow::{bail, Error, Result}; |
|
|
use num_traits::FromPrimitive; |
|
|
use serde::{Deserialize, Serialize}; |
|
|
|
|
|
use crate::blob::BlobObject; |
|
|
use crate::context::Context; |
|
|
use crate::mimeparser::SystemMessage; |
|
|
|
|
|
|
|
|
#[derive( |
|
|
PartialEq, Eq, Debug, Clone, Copy, Hash, PartialOrd, Ord, FromPrimitive, Serialize, Deserialize, |
|
|
)] |
|
|
#[repr(u8)] |
|
|
pub enum Param { |
|
|
|
|
|
File = b'f', |
|
|
|
|
|
|
|
|
Filename = b'v', |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
OverrideSenderDisplayname = b'O', |
|
|
|
|
|
|
|
|
Width = b'w', |
|
|
|
|
|
|
|
|
Height = b'h', |
|
|
|
|
|
|
|
|
Duration = b'd', |
|
|
|
|
|
|
|
|
MimeType = b'm', |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SendHtml = b'T', |
|
|
|
|
|
|
|
|
GuaranteeE2ee = b'c', |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ProtectQuote = b'0', |
|
|
|
|
|
|
|
|
|
|
|
ErroneousE2ee = b'e', |
|
|
|
|
|
|
|
|
ForcePlaintext = b'u', |
|
|
|
|
|
|
|
|
SkipAutocrypt = b'o', |
|
|
|
|
|
|
|
|
WantsMdn = b'r', |
|
|
|
|
|
|
|
|
Reaction = b'x', |
|
|
|
|
|
|
|
|
LastReactionTimestamp = b'y', |
|
|
|
|
|
|
|
|
LastReactionMsgId = b'Y', |
|
|
|
|
|
|
|
|
LastReactionContactId = b'1', |
|
|
|
|
|
|
|
|
Bot = b'b', |
|
|
|
|
|
|
|
|
|
|
|
Forwarded = b'a', |
|
|
|
|
|
|
|
|
Quote = b'q', |
|
|
|
|
|
|
|
|
Cmd = b'S', |
|
|
|
|
|
|
|
|
Arg = b'E', |
|
|
|
|
|
|
|
|
Arg2 = b'F', |
|
|
|
|
|
|
|
|
Arg3 = b'G', |
|
|
|
|
|
|
|
|
Arg4 = b'H', |
|
|
|
|
|
|
|
|
AttachGroupImage = b'A', |
|
|
|
|
|
|
|
|
WebrtcRoom = b'V', |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PrepForwards = b'P', |
|
|
|
|
|
|
|
|
SetLatitude = b'l', |
|
|
|
|
|
|
|
|
SetLongitude = b'n', |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Unpromoted = b'U', |
|
|
|
|
|
|
|
|
ProfileImage = b'i', |
|
|
|
|
|
|
|
|
|
|
|
Selftalk = b'K', |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LastSubject = b't', |
|
|
|
|
|
|
|
|
Devicetalk = b'D', |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ListPost = b'p', |
|
|
|
|
|
|
|
|
|
|
|
ListId = b's', |
|
|
|
|
|
|
|
|
StatusTimestamp = b'j', |
|
|
|
|
|
|
|
|
AvatarTimestamp = b'J', |
|
|
|
|
|
|
|
|
EphemeralSettingsTimestamp = b'B', |
|
|
|
|
|
|
|
|
SubjectTimestamp = b'C', |
|
|
|
|
|
|
|
|
GroupNameTimestamp = b'g', |
|
|
|
|
|
|
|
|
MemberListTimestamp = b'k', |
|
|
|
|
|
|
|
|
WebxdcDocument = b'R', |
|
|
|
|
|
|
|
|
WebxdcDocumentTimestamp = b'W', |
|
|
|
|
|
|
|
|
WebxdcSummary = b'N', |
|
|
|
|
|
|
|
|
WebxdcSummaryTimestamp = b'Q', |
|
|
|
|
|
|
|
|
WebxdcIntegration = b'3', |
|
|
|
|
|
|
|
|
WebxdcIntegrateFor = b'2', |
|
|
|
|
|
|
|
|
ForceSticker = b'X', |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)] |
|
|
pub struct Params { |
|
|
inner: BTreeMap<Param, String>, |
|
|
} |
|
|
|
|
|
impl fmt::Display for Params { |
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
|
|
for (i, (key, value)) in self.inner.iter().enumerate() { |
|
|
if i > 0 { |
|
|
writeln!(f)?; |
|
|
} |
|
|
write!( |
|
|
f, |
|
|
"{}={}", |
|
|
*key as u8 as char, |
|
|
value.split('\n').collect::<Vec<&str>>().join("\n\n") |
|
|
)?; |
|
|
} |
|
|
Ok(()) |
|
|
} |
|
|
} |
|
|
|
|
|
impl str::FromStr for Params { |
|
|
type Err = Error; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> { |
|
|
let mut inner = BTreeMap::new(); |
|
|
let mut lines = s.lines().peekable(); |
|
|
|
|
|
while let Some(line) = lines.next() { |
|
|
if let [key, value] = line.splitn(2, '=').collect::<Vec<_>>()[..] { |
|
|
let key = key.to_string(); |
|
|
let mut value = value.to_string(); |
|
|
while let Some(s) = lines.peek() { |
|
|
if !s.is_empty() { |
|
|
break; |
|
|
} |
|
|
lines.next(); |
|
|
value.push('\n'); |
|
|
value += lines.next().unwrap_or_default(); |
|
|
} |
|
|
|
|
|
if let Some(key) = key.as_bytes().first().and_then(|key| Param::from_u8(*key)) { |
|
|
inner.insert(key, value); |
|
|
} |
|
|
} else { |
|
|
bail!("Not a key-value pair: {:?}", line); |
|
|
} |
|
|
} |
|
|
|
|
|
Ok(Params { inner }) |
|
|
} |
|
|
} |
|
|
|
|
|
impl Params { |
|
|
|
|
|
pub fn new() -> Self { |
|
|
Default::default() |
|
|
} |
|
|
|
|
|
|
|
|
pub fn get(&self, key: Param) -> Option<&str> { |
|
|
self.inner.get(&key).map(|s| s.as_str()) |
|
|
} |
|
|
|
|
|
|
|
|
pub fn exists(&self, key: Param) -> bool { |
|
|
self.inner.contains_key(&key) |
|
|
} |
|
|
|
|
|
|
|
|
pub fn set(&mut self, key: Param, value: impl ToString) -> &mut Self { |
|
|
self.inner.insert(key, value.to_string()); |
|
|
self |
|
|
} |
|
|
|
|
|
|
|
|
pub fn remove(&mut self, key: Param) -> &mut Self { |
|
|
self.inner.remove(&key); |
|
|
self |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn set_optional(&mut self, key: Param, value: Option<impl ToString>) -> &mut Self { |
|
|
if let Some(value) = value { |
|
|
self.set(key, value) |
|
|
} else { |
|
|
self.remove(key) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
pub fn is_empty(&self) -> bool { |
|
|
self.inner.is_empty() |
|
|
} |
|
|
|
|
|
|
|
|
pub fn len(&self) -> usize { |
|
|
self.inner.len() |
|
|
} |
|
|
|
|
|
|
|
|
pub fn get_int(&self, key: Param) -> Option<i32> { |
|
|
self.get(key).and_then(|s| s.parse().ok()) |
|
|
} |
|
|
|
|
|
|
|
|
pub fn get_i64(&self, key: Param) -> Option<i64> { |
|
|
self.get(key).and_then(|s| s.parse().ok()) |
|
|
} |
|
|
|
|
|
|
|
|
pub fn get_bool(&self, key: Param) -> Option<bool> { |
|
|
self.get_int(key).map(|v| v != 0) |
|
|
} |
|
|
|
|
|
|
|
|
pub fn get_cmd(&self) -> SystemMessage { |
|
|
self.get_int(Param::Cmd) |
|
|
.and_then(SystemMessage::from_i32) |
|
|
.unwrap_or_default() |
|
|
} |
|
|
|
|
|
|
|
|
pub fn set_cmd(&mut self, value: SystemMessage) { |
|
|
self.set_int(Param::Cmd, value as i32); |
|
|
} |
|
|
|
|
|
|
|
|
pub fn get_float(&self, key: Param) -> Option<f64> { |
|
|
self.get(key).and_then(|s| s.parse().ok()) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn get_file<'a>(&self, key: Param, context: &'a Context) -> Result<Option<ParamsFile<'a>>> { |
|
|
let val = match self.get(key) { |
|
|
Some(val) => val, |
|
|
None => return Ok(None), |
|
|
}; |
|
|
ParamsFile::from_param(context, val).map(Some) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[allow(clippy::needless_lifetimes)] |
|
|
pub async fn get_blob<'a>( |
|
|
&self, |
|
|
key: Param, |
|
|
context: &'a Context, |
|
|
create: bool, |
|
|
) -> Result<Option<BlobObject<'a>>> { |
|
|
let val = match self.get(key) { |
|
|
Some(val) => val, |
|
|
None => return Ok(None), |
|
|
}; |
|
|
let file = ParamsFile::from_param(context, val)?; |
|
|
let blob = match file { |
|
|
ParamsFile::FsPath(path) => match create { |
|
|
true => BlobObject::new_from_path(context, &path).await?, |
|
|
false => BlobObject::from_path(context, &path)?, |
|
|
}, |
|
|
ParamsFile::Blob(blob) => blob, |
|
|
}; |
|
|
Ok(Some(blob)) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn get_path(&self, key: Param, context: &Context) -> Result<Option<PathBuf>> { |
|
|
let val = match self.get(key) { |
|
|
Some(val) => val, |
|
|
None => return Ok(None), |
|
|
}; |
|
|
let file = ParamsFile::from_param(context, val)?; |
|
|
let path = match file { |
|
|
ParamsFile::FsPath(path) => path, |
|
|
ParamsFile::Blob(blob) => blob.to_abs_path(), |
|
|
}; |
|
|
Ok(Some(path)) |
|
|
} |
|
|
|
|
|
|
|
|
pub fn set_int(&mut self, key: Param, value: i32) -> &mut Self { |
|
|
self.set(key, format!("{value}")); |
|
|
self |
|
|
} |
|
|
|
|
|
|
|
|
pub fn set_i64(&mut self, key: Param, value: i64) -> &mut Self { |
|
|
self.set(key, value.to_string()); |
|
|
self |
|
|
} |
|
|
|
|
|
|
|
|
pub fn set_float(&mut self, key: Param, value: f64) -> &mut Self { |
|
|
self.set(key, format!("{value}")); |
|
|
self |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)] |
|
|
pub enum ParamsFile<'a> { |
|
|
FsPath(PathBuf), |
|
|
Blob(BlobObject<'a>), |
|
|
} |
|
|
|
|
|
impl<'a> ParamsFile<'a> { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn from_param(context: &'a Context, src: &str) -> Result<ParamsFile<'a>> { |
|
|
let param = match src.starts_with("$BLOBDIR/") { |
|
|
true => ParamsFile::Blob(BlobObject::from_name(context, src.to_string())?), |
|
|
false => ParamsFile::FsPath(PathBuf::from(src)), |
|
|
}; |
|
|
Ok(param) |
|
|
} |
|
|
} |
|
|
|
|
|
#[cfg(test)] |
|
|
mod tests { |
|
|
use std::path::Path; |
|
|
use std::str::FromStr; |
|
|
|
|
|
use tokio::fs; |
|
|
|
|
|
use super::*; |
|
|
use crate::test_utils::TestContext; |
|
|
|
|
|
#[test] |
|
|
fn test_dc_param() { |
|
|
let mut p1: Params = "a=1\nf=2\nc=3".parse().unwrap(); |
|
|
|
|
|
assert_eq!(p1.get_int(Param::Forwarded), Some(1)); |
|
|
assert_eq!(p1.get_int(Param::File), Some(2)); |
|
|
assert_eq!(p1.get_int(Param::Height), None); |
|
|
assert!(!p1.exists(Param::Height)); |
|
|
|
|
|
p1.set_int(Param::Duration, 4); |
|
|
|
|
|
assert_eq!(p1.get_int(Param::Duration), Some(4)); |
|
|
|
|
|
let mut p1 = Params::new(); |
|
|
|
|
|
p1.set(Param::Forwarded, "foo") |
|
|
.set_int(Param::File, 2) |
|
|
.remove(Param::GuaranteeE2ee) |
|
|
.set_int(Param::Duration, 4); |
|
|
|
|
|
assert_eq!(p1.to_string(), "a=foo\nd=4\nf=2"); |
|
|
|
|
|
p1.remove(Param::File); |
|
|
|
|
|
assert_eq!(p1.to_string(), "a=foo\nd=4",); |
|
|
assert_eq!(p1.len(), 2); |
|
|
|
|
|
p1.remove(Param::Forwarded); |
|
|
p1.remove(Param::Duration); |
|
|
|
|
|
assert_eq!(p1.to_string(), "",); |
|
|
|
|
|
assert!(p1.is_empty()); |
|
|
assert_eq!(p1.len(), 0) |
|
|
} |
|
|
|
|
|
#[test] |
|
|
fn test_roundtrip() { |
|
|
let mut params = Params::new(); |
|
|
params.set(Param::Height, "foo\nbar=baz\nquux"); |
|
|
params.set(Param::Width, "\n\n\na=\n="); |
|
|
assert_eq!(params.to_string().parse::<Params>().unwrap(), params); |
|
|
} |
|
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)] |
|
|
async fn test_params_file_fs_path() { |
|
|
let t = TestContext::new().await; |
|
|
if let ParamsFile::FsPath(p) = ParamsFile::from_param(&t, "/foo/bar/baz").unwrap() { |
|
|
assert_eq!(p, Path::new("/foo/bar/baz")); |
|
|
} else { |
|
|
panic!("Wrong enum variant"); |
|
|
} |
|
|
} |
|
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)] |
|
|
async fn test_params_file_blob() { |
|
|
let t = TestContext::new().await; |
|
|
if let ParamsFile::Blob(b) = ParamsFile::from_param(&t, "$BLOBDIR/foo").unwrap() { |
|
|
assert_eq!(b.as_name(), "$BLOBDIR/foo"); |
|
|
} else { |
|
|
panic!("Wrong enum variant"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)] |
|
|
async fn test_params_get_fileparam() { |
|
|
let t = TestContext::new().await; |
|
|
let fname = t.dir.path().join("foo"); |
|
|
let mut p = Params::new(); |
|
|
p.set(Param::File, fname.to_str().unwrap()); |
|
|
|
|
|
let file = p.get_file(Param::File, &t).unwrap().unwrap(); |
|
|
assert_eq!(file, ParamsFile::FsPath(fname.clone())); |
|
|
|
|
|
let path: PathBuf = p.get_path(Param::File, &t).unwrap().unwrap(); |
|
|
assert_eq!(path, fname); |
|
|
|
|
|
|
|
|
assert!(p.get_blob(Param::File, &t, false).await.is_err()); |
|
|
|
|
|
fs::write(fname, b"boo").await.unwrap(); |
|
|
let blob = p.get_blob(Param::File, &t, true).await.unwrap().unwrap(); |
|
|
assert!(blob.as_file_name().starts_with("foo")); |
|
|
|
|
|
|
|
|
let bar_path = t.get_blobdir().join("bar"); |
|
|
p.set(Param::File, bar_path.to_str().unwrap()); |
|
|
let blob = p.get_blob(Param::File, &t, false).await.unwrap().unwrap(); |
|
|
assert_eq!(blob, BlobObject::from_name(&t, "bar".to_string()).unwrap()); |
|
|
|
|
|
p.remove(Param::File); |
|
|
assert!(p.get_file(Param::File, &t).unwrap().is_none()); |
|
|
assert!(p.get_path(Param::File, &t).unwrap().is_none()); |
|
|
assert!(p.get_blob(Param::File, &t, false).await.unwrap().is_none()); |
|
|
} |
|
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)] |
|
|
async fn test_params_unknown_key() -> Result<()> { |
|
|
|
|
|
let p = Params::from_str("w=12\nZ=13\nh=14")?; |
|
|
assert_eq!(p.len(), 2); |
|
|
assert_eq!(p.get(Param::Width), Some("12")); |
|
|
assert_eq!(p.get(Param::Height), Some("14")); |
|
|
Ok(()) |
|
|
} |
|
|
} |
|
|
|