Files
inbuxa-server/vendor/sieve-rs/src/compiler/grammar/tests/test_date.rs
T
jcoffey-dev cc6f1eb298
ci / fork-checks (pull_request) Successful in 16s
ci / build (pull_request) Successful in 7m53s
Rename the identifiers that carried the upstream name
Everything clients, users and operators meet now carries the fork's name,
with no aliases (SPEC.md §2.4, changed here from "protocol identifiers
stay"):

- JMAP: upstream's registry capability is urn:inbuxa:jmap:registry, beside
  the fork's own urn:inbuxa:jmap.
- WebDAV lock and sync tokens are urn:inbuxa:dav*; clients resync once.
- Sieve: vnd.inbuxa.while and vnd.inbuxa.expressions. sieve-rs spells these
  into its compiler, so it's vendored (vendor/sieve-rs, 0.7.3) and patched in;
  a unit test fails if Cargo.lock ever moves past the vendored copy. The
  trusted runtime now names itself too, rather than answering sieve-rs's
  default.
- The web interface's OAuth client is inbuxa-webui. On every start the old
  stalwart-webui client is removed and any application naming it is moved
  over.
- The spam filter's blobs are INBUXA_SPAM_*; every start moves any left
  under the old keys, so a trained model survives.
- SQL stores and log files default to inbuxa, in the code and in the
  schema served to the admin (checksum regenerated).
- Settings are INBUXA_* only. A STALWART_* variable that's set where its
  INBUXA_* one isn't stops the server at startup, naming it.
- The version-upgrade messages link docs.inbuxa.org's migration page, and
  the OpenAPI description, smtp crate metadata and web-push test fixtures
  lose the name.

Kept on purpose, allowlisted with reasons: the OAuth key-derivation
contexts (renaming them would end every session and invalidate every
sealed client id) and the hashed application prefix.

Also fixes a latent start-up failure: ensure_client updated an existing
first-party client with a revision of 0, which the registry's assertion
never matches, so adding a redirect URI or changing the webmail secret
failed start-up. And the principal session test now expects
legacyProtocols (C-1, added 2026-09-21), which it had missed.

Tested: the server builds without warnings; common's 106 unit tests,
including the vendoring check; a new integration test for the two
start-up migrations; and the webdav, jmap, imap and SMTP Sieve suites.
2026-09-22 19:33:02 -07:00

369 lines
13 KiB
Rust

/*
* SPDX-FileCopyrightText: 2020 Stalwart Labs Ltd <[email protected]>
*
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-SEL
*/
use mail_parser::HeaderName;
use crate::compiler::{
CompileError, ErrorType, Number, Value,
grammar::{Capability, Comparator, instruction::CompilerState},
lexer::{StringConstant, Token, word::Word},
};
use crate::compiler::grammar::{MatchType, test::Test};
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(
any(test, feature = "serde"),
derive(serde::Serialize, serde::Deserialize)
)]
#[cfg_attr(
feature = "rkyv",
derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive)
)]
pub(crate) struct TestDate {
pub header_name: Value,
pub key_list: Vec<Value>,
pub match_type: MatchType,
pub comparator: Comparator,
pub index: Option<i32>,
pub zone: Zone,
pub date_part: DatePart,
pub mime_anychild: bool,
pub is_not: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(
any(test, feature = "serde"),
derive(serde::Serialize, serde::Deserialize)
)]
#[cfg_attr(
feature = "rkyv",
derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive)
)]
pub(crate) struct TestCurrentDate {
pub zone: Option<i64>,
pub match_type: MatchType,
pub comparator: Comparator,
pub date_part: DatePart,
pub key_list: Vec<Value>,
pub is_not: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(
any(test, feature = "serde"),
derive(serde::Serialize, serde::Deserialize)
)]
#[cfg_attr(
feature = "rkyv",
derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive)
)]
pub(crate) enum Zone {
Time(i64),
Original,
Local,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(
any(test, feature = "serde"),
derive(serde::Serialize, serde::Deserialize)
)]
#[cfg_attr(
feature = "rkyv",
derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive)
)]
pub(crate) enum DatePart {
Year,
Month,
Day,
Date,
Julian,
Hour,
Minute,
Second,
Time,
Iso8601,
Std11,
Zone,
Weekday,
}
impl CompilerState<'_> {
pub(crate) fn parse_test_date(&mut self) -> Result<Test, CompileError> {
let mut match_type = MatchType::Is;
let mut comparator = Comparator::AsciiCaseMap;
let mut header_name = None;
let mut key_list;
let mut index = None;
let mut index_last = false;
let mut zone = Zone::Local;
let mut date_part = None;
let mut mime = false;
let mut mime_anychild = false;
loop {
let token_info = self.tokens.unwrap_next()?;
match token_info.token {
Token::Tag(
word @ (Word::Is
| Word::Contains
| Word::Matches
| Word::Value
| Word::Count
| Word::Regex
| Word::List),
) => {
self.validate_argument(
1,
match word {
Word::Value | Word::Count => Capability::Relational.into(),
Word::Regex => Capability::Regex.into(),
Word::List => Capability::ExtLists.into(),
_ => None,
},
token_info.line_num,
token_info.line_pos,
)?;
match_type = self.parse_match_type(word)?;
}
Token::Tag(Word::Comparator) => {
self.validate_argument(2, None, token_info.line_num, token_info.line_pos)?;
comparator = self.parse_comparator()?;
}
Token::Tag(Word::Index) => {
self.validate_argument(
3,
Capability::Index.into(),
token_info.line_num,
token_info.line_pos,
)?;
index = (self.tokens.expect_number(u16::MAX as usize)? as i32).into();
}
Token::Tag(Word::Last) => {
self.validate_argument(
4,
Capability::Index.into(),
token_info.line_num,
token_info.line_pos,
)?;
index_last = true;
}
Token::Tag(Word::Mime) => {
self.validate_argument(
5,
Capability::Mime.into(),
token_info.line_num,
token_info.line_pos,
)?;
mime = true;
}
Token::Tag(Word::AnyChild) => {
self.validate_argument(
6,
Capability::Mime.into(),
token_info.line_num,
token_info.line_pos,
)?;
mime_anychild = true;
}
Token::Tag(Word::OriginalZone) => {
self.validate_argument(7, None, token_info.line_num, token_info.line_pos)?;
zone = Zone::Original;
}
Token::Tag(Word::Zone) => {
self.validate_argument(7, None, token_info.line_num, token_info.line_pos)?;
zone = Zone::Time(self.parse_timezone()?);
}
_ => {
if header_name.is_none() {
let header = self.parse_string_token(token_info)?;
if let Value::Text(header_name) = &header
&& HeaderName::parse(header_name.as_ref()).is_none()
{
return Err(self
.tokens
.unwrap_next()?
.custom(ErrorType::InvalidHeaderName));
}
header_name = header.into();
} else if date_part.is_none() {
if let Token::StringConstant(string) = &token_info.token
&& let Some(date_part_) =
lookup_date_part(&string.to_string().to_ascii_lowercase())
{
date_part = date_part_.into();
continue;
}
return Err(token_info.expected("valid date part"));
} else {
key_list = self.parse_strings_token(token_info)?;
break;
}
}
}
}
if !mime && mime_anychild {
return Err(self.tokens.unwrap_next()?.missing_tag(":mime"));
}
self.validate_match(&match_type, &mut key_list)?;
Ok(Test::Date(TestDate {
header_name: header_name.unwrap(),
key_list,
date_part: date_part.unwrap(),
match_type,
comparator,
index: if index_last { index.map(|i| -i) } else { index },
zone,
mime_anychild,
is_not: false,
}))
}
pub(crate) fn parse_test_currentdate(&mut self) -> Result<Test, CompileError> {
let mut match_type = MatchType::Is;
let mut comparator = Comparator::AsciiCaseMap;
let mut key_list;
let mut zone = None;
let mut date_part = None;
loop {
let token_info = self.tokens.unwrap_next()?;
match token_info.token {
Token::Tag(
word @ (Word::Is
| Word::Contains
| Word::Matches
| Word::Value
| Word::Count
| Word::Regex
| Word::List),
) => {
self.validate_argument(
1,
match word {
Word::Value | Word::Count => Capability::Relational.into(),
Word::Regex => Capability::Regex.into(),
Word::List => Capability::ExtLists.into(),
_ => None,
},
token_info.line_num,
token_info.line_pos,
)?;
match_type = self.parse_match_type(word)?;
}
Token::Tag(Word::Comparator) => {
self.validate_argument(2, None, token_info.line_num, token_info.line_pos)?;
comparator = self.parse_comparator()?;
}
Token::Tag(Word::Zone) => {
self.validate_argument(3, None, token_info.line_num, token_info.line_pos)?;
zone = self.parse_timezone()?.into();
}
_ => {
if date_part.is_none() {
if let Token::StringConstant(string) = &token_info.token
&& let Some(date_part_) =
lookup_date_part(&string.to_string().to_ascii_lowercase())
{
date_part = date_part_.into();
continue;
}
return Err(token_info.expected("valid date part"));
} else {
key_list = self.parse_strings_token(token_info)?;
break;
}
}
}
}
self.validate_match(&match_type, &mut key_list)?;
Ok(Test::CurrentDate(TestCurrentDate {
key_list,
date_part: date_part.unwrap(),
match_type,
comparator,
zone,
is_not: false,
}))
}
pub(crate) fn parse_timezone(&mut self) -> Result<i64, CompileError> {
let token_info = self.tokens.unwrap_next()?;
if let Token::StringConstant(value) = &token_info.token {
let timezone = match value {
StringConstant::String(value) => value.parse::<i64>().unwrap_or(i64::MAX),
StringConstant::Number(Number::Integer(n)) => *n,
StringConstant::Number(Number::Float(n)) => *n as i64,
};
return match timezone {
0..=1400 => Ok((timezone / 100 * 3600) + (timezone % 100 * 60)),
-1200..=-1 => Ok((timezone / 100 * 3600) - (-timezone % 100 * 60)),
_ => Err(token_info.expected("invalid timezone")),
};
}
Err(token_info.expected("string containing time zone"))
}
}
/*
"year" => the year, "0000" .. "9999".
"month" => the month, "01" .. "12".
"day" => the day, "01" .. "31".
"date" => the date in "yyyy-mm-dd" format.
"julian" => the Modified Julian Day, that is, the date
expressed as an integer number of days since
00:00 UTC on November 17, 1858 (using the Gregorian
calendar). This corresponds to the regular
Julian Day minus 2400000.5. Sample routines to
convert to and from modified Julian dates are
given in Appendix A.
"hour" => the hour, "00" .. "23".
"minute" => the minute, "00" .. "59".
"second" => the second, "00" .. "60".
"time" => the time in "hh:mm:ss" format.
"iso8601" => the date and time in restricted ISO 8601 format.
"std11" => the date and time in a format appropriate
for use in a Date: header field [RFC2822].
"zone" => the time zone in use. If the user specified a
time zone with ":zone", "zone" will
contain that value. If :originalzone is specified
this value will be the original zone specified
in the date-time value. If neither argument is
specified the value will be the server's default
time zone in offset format "+hhmm" or "-hhmm". An
offset of 0 (Zulu) always has a positive sign.
"weekday" => the day of the week expressed as an integer between
"0" and "6". "0" is Sunday, "1" is Monday, etc.
*/
fn lookup_date_part(input: &str) -> Option<DatePart> {
hashify::tiny_map!(
input.as_bytes(),
"year" => DatePart::Year,
"month" => DatePart::Month,
"day" => DatePart::Day,
"date" => DatePart::Date,
"julian" => DatePart::Julian,
"hour" => DatePart::Hour,
"minute" => DatePart::Minute,
"second" => DatePart::Second,
"time" => DatePart::Time,
"iso8601" => DatePart::Iso8601,
"std11" => DatePart::Std11,
"zone" => DatePart::Zone,
"weekday" => DatePart::Weekday,
)
}