Add export-managesieve: an archive's Sieve scripts into any ManageSieve server #31
No files matched your search
@@ -5,6 +5,15 @@ inbuxa suite: a series (`27.1`), its patches (`27.1.3`) and release
|
||||
candidates before a series ships (`27.1-rc.1`). The tag is the version with a
|
||||
`v`. Releases before 27.1 were dates (`v2026.9.30`).
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
### Added
|
||||
- `export-managesieve` writes an archive's Sieve scripts into any
|
||||
ManageSieve server and makes the archive's active script the active one.
|
||||
Like `export-imap`, it writes only what's missing or differs, so running
|
||||
it again writes nothing. A script the server refuses is counted and the
|
||||
rest go on; `--dry-run` checks each one with `CHECKSCRIPT`.
|
||||
|
||||
## [27.1-rc.2]
|
||||
|
||||
The second release candidate of the 27.1 series: three ways out of an
|
||||
|
||||
@@ -19,6 +19,7 @@ contacts, identities, Sieve scripts and files.
|
||||
inbuxa-migrate export … alice.sqlite the archive into inbuxa
|
||||
inbuxa-migrate export-files alice.sqlite out the archive as plain files
|
||||
inbuxa-migrate export-imap … alice.sqlite its mail into any IMAP server
|
||||
inbuxa-migrate export-managesieve … alice.sqlite its filters, likewise
|
||||
inbuxa-migrate export-directory … out a server's accounts, no secrets
|
||||
|
||||
Between the two sides sits an archive: one SQLite file holding one account.
|
||||
@@ -37,7 +38,8 @@ and refuses a different one unless told otherwise.
|
||||
|
||||
Export speaks JMAP only. It writes into inbuxa, or into any JMAP server that
|
||||
advertises the types being written. `export-imap` writes the mail into any
|
||||
IMAP server, and `export-files` is the way out to everything else: it writes
|
||||
IMAP server, `export-managesieve` the Sieve filters into any ManageSieve
|
||||
server, and `export-files` is the way out to everything else: it writes
|
||||
the archive as a Maildir++ tree, vCard, iCalendar and Sieve files that other
|
||||
servers and mail, calendar and contacts apps open, with no network and no
|
||||
store format in the way. For a whole server, `export-directory` writes the
|
||||
|
||||
+35
-3
@@ -13,7 +13,7 @@ and how to install it.
|
||||
## Commands and global flags
|
||||
|
||||
```
|
||||
inbuxa-migrate <import|export|export-imap|export-files|export-directory|inspect> [args...]
|
||||
inbuxa-migrate <import|export|export-imap|export-managesieve|export-files|export-directory|inspect> [args...]
|
||||
```
|
||||
|
||||
Every command takes a few global flags -- verbosity, worker pool size, retry
|
||||
@@ -294,8 +294,9 @@ inbuxa-migrate export-imap \
|
||||
|
||||
Writes an archive's mail into an account on any IMAP server, for a target
|
||||
that doesn't speak JMAP. It signs in the way `import imap` does, with the
|
||||
same flags. Contacts, calendars and filters don't go over IMAP; write those
|
||||
with `export-files`.
|
||||
same flags. Contacts, calendars and filters don't go over IMAP; write the
|
||||
filters with `export-managesieve`, and contacts and calendars with
|
||||
`export-files`.
|
||||
|
||||
Like `export`, it keeps no state. Each run reads the target folder first,
|
||||
matching messages by Message-ID, or without one by sender, subject, date and
|
||||
@@ -322,6 +323,35 @@ connection dropped is found, not doubled.
|
||||
With `--dry-run` it signs in, reads the target with `EXAMINE`, and counts
|
||||
what it would create, append and update, without writing anything.
|
||||
|
||||
## Export to ManageSieve
|
||||
|
||||
```
|
||||
inbuxa-migrate export-managesieve \
|
||||
--url <sieve://host[:port]|sieves://host[:port]> \
|
||||
(--auth-basic <USER> [--auth-password <PASS>] | --auth-bearer [TOKEN] --auth-user <USER>) \
|
||||
[--allow-cleartext] [--dry-run] \
|
||||
<ARCHIVE>
|
||||
```
|
||||
|
||||
Writes an archive's Sieve scripts into an account on any ManageSieve server
|
||||
(RFC 5804), with the flags `import managesieve` takes. It keeps no state:
|
||||
each run lists the target's scripts, writes each one that is missing or
|
||||
differs with `PUTSCRIPT`, and leaves alone the ones that match, so a second
|
||||
run writes nothing. A script with no name is written as `script-<id>`.
|
||||
|
||||
- The archive's active script is made active on the target. If no script
|
||||
in the archive is active, the target's active script stays as it is.
|
||||
- A script the server refuses, for instance one using an extension it
|
||||
lacks, is counted as failed with the server's reason, and the run goes
|
||||
on. If the active script is refused, it is not made active.
|
||||
- Scripts are never deleted from the target, and names that match are
|
||||
overwritten only when their content differs.
|
||||
|
||||
With `--dry-run` it signs in, lists and compares, and checks each script it
|
||||
would write with `CHECKSCRIPT`, so a script the server would refuse is
|
||||
counted as failed before anything is written. A server without
|
||||
`CHECKSCRIPT` (one that advertises no `VERSION`) gets the comparison only.
|
||||
|
||||
## Export to files
|
||||
|
||||
```
|
||||
@@ -471,6 +501,8 @@ cargo test --test integration_baikal -- --ignored --test-threads=1
|
||||
cargo test --test integration_webdav -- --ignored --test-threads=1
|
||||
cargo test --test integration_dovecot -- --ignored --test-threads=1
|
||||
cargo test --test integration_cyrus -- --ignored --test-threads=1
|
||||
cargo test --test integration_export_imap -- --ignored --test-threads=1
|
||||
cargo test --test integration_export_managesieve -- --ignored --test-threads=1
|
||||
|
||||
# Slow tests
|
||||
cargo test --test mock_jmap -- --ignored
|
||||
|
||||
+75
@@ -22,6 +22,7 @@ use crate::jmap::http::Auth;
|
||||
use crate::logging::Logger;
|
||||
use crate::secret;
|
||||
use crate::sync::export_imap::ExportImapConfig;
|
||||
use crate::sync::export_managesieve::ExportManageSieveConfig;
|
||||
use crate::sync::import_dav::{DavAuth, DavImportConfig, DavKindArg};
|
||||
use crate::sync::import_exchange_ews::{EwsAuth, EwsImportConfig};
|
||||
use crate::sync::import_exchange_graph::{GraphAuth, GraphImportConfig};
|
||||
@@ -62,6 +63,11 @@ enum Command {
|
||||
name = "export-imap"
|
||||
)]
|
||||
ExportImap(Box<ExportImapArgs>),
|
||||
#[command(
|
||||
about = "Write a local archive's Sieve scripts into a ManageSieve server",
|
||||
name = "export-managesieve"
|
||||
)]
|
||||
ExportManageSieve(Box<ExportManageSieveArgs>),
|
||||
#[command(
|
||||
about = "Write a server's domains, accounts, groups, lists and aliases as JSON and CSV, without secrets",
|
||||
name = "export-directory"
|
||||
@@ -426,6 +432,55 @@ struct ExportImapArgs {
|
||||
archive: PathBuf,
|
||||
}
|
||||
|
||||
#[derive(Args)]
|
||||
#[command(group(clap::ArgGroup::new("auth").required(true).args(["auth_basic", "auth_bearer"])))]
|
||||
struct ExportManageSieveArgs {
|
||||
#[arg(
|
||||
long,
|
||||
value_name = "URL",
|
||||
help = "sieve://host[:port] or sieves://host[:port]"
|
||||
)]
|
||||
url: String,
|
||||
|
||||
#[arg(
|
||||
long,
|
||||
value_name = "USER",
|
||||
help = "SASL PLAIN (preferred) with LOGIN fallback"
|
||||
)]
|
||||
auth_basic: Option<String>,
|
||||
|
||||
#[arg(
|
||||
long,
|
||||
value_name = "PASS",
|
||||
help = "Basic password (prefer $INBUXA_MIGRATE_PASSWORD or prompt)"
|
||||
)]
|
||||
auth_password: Option<String>,
|
||||
|
||||
#[arg(
|
||||
long,
|
||||
value_name = "TOKEN",
|
||||
num_args = 0..=1,
|
||||
help = "Bearer token for OAUTHBEARER (value optional: $INBUXA_MIGRATE_API_KEY or prompt)"
|
||||
)]
|
||||
auth_bearer: Option<Option<String>>,
|
||||
|
||||
#[arg(
|
||||
long,
|
||||
value_name = "USER",
|
||||
help = "OAUTHBEARER identity (required with --auth-bearer)"
|
||||
)]
|
||||
auth_user: Option<String>,
|
||||
|
||||
#[arg(long, help = "Permit credentials on sieve:// without STARTTLS")]
|
||||
allow_cleartext: bool,
|
||||
|
||||
#[command(flatten)]
|
||||
global: GlobalArgs,
|
||||
|
||||
#[arg(value_name = "ARCHIVE", help = "Local SQLite archive")]
|
||||
archive: PathBuf,
|
||||
}
|
||||
|
||||
#[derive(Args)]
|
||||
struct ExportFilesArgs {
|
||||
#[arg(
|
||||
@@ -549,6 +604,7 @@ pub enum Action {
|
||||
ExportFiles(ExportFilesConfig),
|
||||
ExportDirectory(ExportDirectoryConfig),
|
||||
ExportImap(CommonConfig, ExportImapConfig),
|
||||
ExportManageSieve(CommonConfig, ExportManageSieveConfig),
|
||||
Inspect(InspectConfig),
|
||||
}
|
||||
|
||||
@@ -677,6 +733,7 @@ impl Cli {
|
||||
Command::ExportFiles(args) => resolve_export_files(*args),
|
||||
Command::ExportDirectory(args) => resolve_export_directory(*args),
|
||||
Command::ExportImap(args) => resolve_export_imap(*args),
|
||||
Command::ExportManageSieve(args) => resolve_export_managesieve(*args),
|
||||
Command::Inspect(args) => resolve_inspect(*args),
|
||||
}
|
||||
}
|
||||
@@ -1073,6 +1130,24 @@ fn resolve_export_imap(args: ExportImapArgs) -> Result<Action, Error> {
|
||||
))
|
||||
}
|
||||
|
||||
fn resolve_export_managesieve(args: ExportManageSieveArgs) -> Result<Action, Error> {
|
||||
let common = common_config(&args.global, args.archive);
|
||||
let auth = resolve_managesieve_auth(
|
||||
args.auth_basic.as_deref(),
|
||||
args.auth_password.as_deref(),
|
||||
args.auth_bearer.as_ref(),
|
||||
args.auth_user.as_deref(),
|
||||
)?;
|
||||
Ok(Action::ExportManageSieve(
|
||||
common,
|
||||
ExportManageSieveConfig {
|
||||
url: args.url,
|
||||
auth,
|
||||
allow_cleartext: args.allow_cleartext,
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
fn resolve_export_files(args: ExportFilesArgs) -> Result<Action, Error> {
|
||||
let objects = args.objects.as_deref().map(parse_object_list).transpose()?;
|
||||
Ok(Action::ExportFiles(ExportFilesConfig {
|
||||
|
||||
@@ -87,6 +87,13 @@ fn run() -> i32 {
|
||||
let logger = config.logger;
|
||||
(RunOutcome::from_result(export_files::run(config)), logger)
|
||||
}
|
||||
Action::ExportManageSieve(common, config) => {
|
||||
let logger = common.logger;
|
||||
(
|
||||
RunOutcome::from_result(sync::export_managesieve::run(common, config)),
|
||||
logger,
|
||||
)
|
||||
}
|
||||
Action::ExportDirectory(config) => {
|
||||
let logger = config.logger;
|
||||
(
|
||||
|
||||
@@ -180,6 +180,35 @@ impl SieveClient {
|
||||
self.run(&command::getscript(name))
|
||||
}
|
||||
|
||||
pub fn putscript(&mut self, name: &str, script: &[u8]) -> Result<(), SieveError> {
|
||||
self.run_logged(
|
||||
&command::putscript(name, script),
|
||||
&format!("PUTSCRIPT {name:?}"),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn checkscript(&mut self, script: &[u8]) -> Result<(), SieveError> {
|
||||
self.run_logged(&command::checkscript(script), "CHECKSCRIPT")
|
||||
}
|
||||
|
||||
pub fn setactive(&mut self, name: &str) -> Result<(), SieveError> {
|
||||
self.run(&command::setactive(name)).map(|_| ())
|
||||
}
|
||||
|
||||
/// As `run_raw`, logging `label` in place of a script's bytes.
|
||||
fn run_logged(&mut self, bytes: &[u8], label: &str) -> Result<(), SieveError> {
|
||||
let started = Instant::now();
|
||||
self.write_all(bytes)?;
|
||||
let block = read_response(&mut self.reader)?;
|
||||
self.logger.trace_cmd(
|
||||
"SIEVE",
|
||||
label,
|
||||
&sieve_status(&block.status),
|
||||
started.elapsed(),
|
||||
);
|
||||
finish_block(block, &mut self.closed).map(|_| ())
|
||||
}
|
||||
|
||||
pub fn noop(&mut self) -> Result<(), SieveError> {
|
||||
let _ = self.run(&command::noop())?;
|
||||
Ok(())
|
||||
|
||||
@@ -34,6 +34,39 @@ pub fn getscript(name: &str) -> String {
|
||||
out
|
||||
}
|
||||
|
||||
/// `PUTSCRIPT` with the script as a non-synchronizing literal (RFC 5804,
|
||||
/// section 4): the bytes follow at once, and a CRLF ends the command.
|
||||
pub fn putscript(name: &str, script: &[u8]) -> Vec<u8> {
|
||||
literal_command("PUTSCRIPT", Some(name), script)
|
||||
}
|
||||
|
||||
/// `CHECKSCRIPT`: the server compiles the script and stores nothing.
|
||||
pub fn checkscript(script: &[u8]) -> Vec<u8> {
|
||||
literal_command("CHECKSCRIPT", None, script)
|
||||
}
|
||||
|
||||
pub fn setactive(name: &str) -> String {
|
||||
let mut out = String::with_capacity(name.len() + 16);
|
||||
out.push_str("SETACTIVE ");
|
||||
encode_argument(name, &mut out);
|
||||
out.push_str("\r\n");
|
||||
out
|
||||
}
|
||||
|
||||
fn literal_command(verb: &str, name: Option<&str>, script: &[u8]) -> Vec<u8> {
|
||||
let mut head = String::with_capacity(verb.len() + 32);
|
||||
head.push_str(verb);
|
||||
if let Some(name) = name {
|
||||
head.push(' ');
|
||||
encode_argument(name, &mut head);
|
||||
}
|
||||
head.push_str(&format!(" {{{}+}}\r\n", script.len()));
|
||||
let mut out = head.into_bytes();
|
||||
out.extend_from_slice(script);
|
||||
out.extend_from_slice(b"\r\n");
|
||||
out
|
||||
}
|
||||
|
||||
pub fn authenticate(mechanism: &str) -> String {
|
||||
let mut out = String::with_capacity(mechanism.len() + 20);
|
||||
out.push_str("AUTHENTICATE ");
|
||||
|
||||
@@ -0,0 +1,206 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2026 Coffey Labs LLC <[email protected]>
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
//! `export-managesieve`: an archive's Sieve scripts written into any
|
||||
//! ManageSieve server (RFC 5804). Like `export-imap`, it keeps no state:
|
||||
//! each run lists the target's scripts, writes the ones that are missing or
|
||||
//! differ, and makes the archive's active script the active one, so a
|
||||
//! second run writes nothing. It never deletes a script.
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use rusqlite::Connection;
|
||||
|
||||
use crate::db::{blobs, init};
|
||||
use crate::error::Error;
|
||||
use crate::imap::transport::Connector;
|
||||
use crate::logging::{LEVEL_PROGRESS, Logger};
|
||||
use crate::managesieve::error::SieveError;
|
||||
use crate::managesieve::response::parse_listscripts;
|
||||
use crate::managesieve::{ConnectMode, SieveClient, parse_getscript};
|
||||
use crate::sync::import_managesieve::ManageSieveAuth;
|
||||
use crate::sync::import_managesieve::coordinator::{authenticate, parse_endpoint};
|
||||
use crate::sync::{CommonConfig, Summary, TypeCounts};
|
||||
|
||||
pub struct ExportManageSieveConfig {
|
||||
pub url: String,
|
||||
pub auth: ManageSieveAuth,
|
||||
pub allow_cleartext: bool,
|
||||
}
|
||||
|
||||
struct Script {
|
||||
name: String,
|
||||
active: bool,
|
||||
bytes: Vec<u8>,
|
||||
}
|
||||
|
||||
/// The archive's scripts by name. A script with no name is given one.
|
||||
fn read_scripts(conn: &Connection) -> Result<Vec<Script>, Error> {
|
||||
let mut stmt =
|
||||
conn.prepare("SELECT id, name, is_active, blob_id FROM sieve_scripts ORDER BY name, id")?;
|
||||
let rows: Vec<(i64, Option<String>, bool, i64)> = stmt
|
||||
.query_map([], |r| Ok((r.get(0)?, r.get(1)?, r.get(2)?, r.get(3)?)))?
|
||||
.collect::<Result<_, _>>()?;
|
||||
let mut out = Vec::with_capacity(rows.len());
|
||||
for (id, name, active, blob_id) in rows {
|
||||
let Some(bytes) = blobs::blob_bytes(conn, blob_id)? else {
|
||||
return Err(Error::Partial(format!(
|
||||
"the archive is missing the blob for Sieve script {id}"
|
||||
)));
|
||||
};
|
||||
out.push(Script {
|
||||
name: name
|
||||
.filter(|n| !n.is_empty())
|
||||
.unwrap_or_else(|| format!("script-{id}")),
|
||||
active,
|
||||
bytes,
|
||||
});
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
fn wire(context: &str) -> impl Fn(SieveError) -> Error + '_ {
|
||||
move |e| Error::Connection(format!("{context}: {e}"))
|
||||
}
|
||||
|
||||
pub fn run(common: CommonConfig, config: ExportManageSieveConfig) -> Result<Summary, Error> {
|
||||
if !common.archive.is_file() {
|
||||
return Err(Error::Usage(format!(
|
||||
"no archive at {}",
|
||||
common.archive.display()
|
||||
)));
|
||||
}
|
||||
let conn = init::open(&common.archive)?;
|
||||
let scripts = read_scripts(&conn)?;
|
||||
let logger = common.logger;
|
||||
let dry_run = common.dry_run;
|
||||
|
||||
let endpoint = parse_endpoint(&config.url)?;
|
||||
let connector =
|
||||
Connector::new(common.allow_invalid_certs).map_err(|e| Error::Connection(e.to_string()))?;
|
||||
let mode = if endpoint.implicit_tls {
|
||||
ConnectMode::ImplicitTls
|
||||
} else {
|
||||
ConnectMode::StartTls
|
||||
};
|
||||
let mut client = SieveClient::connect(
|
||||
&connector,
|
||||
&endpoint.host,
|
||||
endpoint.port,
|
||||
mode,
|
||||
config.allow_cleartext,
|
||||
logger,
|
||||
)
|
||||
.map_err(|e| Error::Connection(e.to_string()))?;
|
||||
authenticate(&mut client, &config.auth)?;
|
||||
if !client.had_fresh_post_auth_caps()
|
||||
&& let Err(e) = client.refresh_capabilities()
|
||||
{
|
||||
logger.warn(&format!(
|
||||
"post-auth CAPABILITY refresh failed: {e}; continuing"
|
||||
));
|
||||
}
|
||||
|
||||
let listing = client.listscripts().map_err(wire("LISTSCRIPTS"))?;
|
||||
let present: HashMap<String, bool> = parse_listscripts(&listing.data)
|
||||
.map_err(wire("LISTSCRIPTS"))?
|
||||
.into_iter()
|
||||
.map(|s| (s.name, s.active))
|
||||
.collect();
|
||||
// CHECKSCRIPT is RFC 5804's; a server that predates it has no VERSION.
|
||||
let can_check = client.capabilities().version.is_some();
|
||||
|
||||
let mut counts = TypeCounts::default();
|
||||
let mut written: Vec<&str> = Vec::new();
|
||||
for script in &scripts {
|
||||
let name = script.name.as_str();
|
||||
let exists = present.contains_key(name);
|
||||
if exists {
|
||||
let block = client
|
||||
.getscript(name)
|
||||
.map_err(wire(&format!("GETSCRIPT {name:?}")))?;
|
||||
let theirs = parse_getscript(&block.data).map_err(wire("GETSCRIPT"))?;
|
||||
if theirs == script.bytes {
|
||||
counts.skipped += 1;
|
||||
written.push(name);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
let outcome = if dry_run {
|
||||
if can_check {
|
||||
client.checkscript(&script.bytes)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
} else {
|
||||
client.putscript(name, &script.bytes)
|
||||
};
|
||||
match outcome {
|
||||
Ok(()) => {
|
||||
if exists {
|
||||
counts.updated += 1;
|
||||
} else {
|
||||
counts.created += 1;
|
||||
}
|
||||
written.push(name);
|
||||
if logger.enabled(LEVEL_PROGRESS) {
|
||||
let verb = if exists { "updated" } else { "created" };
|
||||
eprintln!("sievescript {name:?}: {verb}");
|
||||
}
|
||||
}
|
||||
// The server refused this script: say why and go on.
|
||||
Err(SieveError::No(no)) => {
|
||||
logger.warn(&format!("script {name:?} was refused: {no}"));
|
||||
counts.failed += 1;
|
||||
}
|
||||
Err(e) => return Err(wire(&format!("PUTSCRIPT {name:?}"))(e)),
|
||||
}
|
||||
}
|
||||
|
||||
// The archive's active script becomes the target's. With none active in
|
||||
// the archive, the target's choice is left as it is.
|
||||
if let Some(active) = scripts.iter().find(|s| s.active) {
|
||||
let name = active.name.as_str();
|
||||
let already = present.get(name).copied().unwrap_or(false);
|
||||
if !already && written.contains(&name) {
|
||||
if dry_run {
|
||||
log_activation(logger, name, true);
|
||||
} else {
|
||||
match client.setactive(name) {
|
||||
Ok(()) => log_activation(logger, name, false),
|
||||
Err(SieveError::No(no)) => {
|
||||
logger.warn(&format!("SETACTIVE {name:?} was refused: {no}"));
|
||||
counts.failed += 1;
|
||||
}
|
||||
Err(e) => return Err(wire(&format!("SETACTIVE {name:?}"))(e)),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let _ = client.logout();
|
||||
|
||||
if dry_run {
|
||||
logger.warn(&format!(
|
||||
"dry run: nothing was written{}",
|
||||
if can_check {
|
||||
"; each script to write was checked with CHECKSCRIPT"
|
||||
} else {
|
||||
"; the server has no CHECKSCRIPT, so scripts were not checked"
|
||||
}
|
||||
));
|
||||
}
|
||||
Ok(Summary {
|
||||
per_type: vec![("sievescript", counts)],
|
||||
..Summary::default()
|
||||
})
|
||||
}
|
||||
|
||||
fn log_activation(logger: Logger, name: &str, dry_run: bool) {
|
||||
if logger.enabled(LEVEL_PROGRESS) {
|
||||
let verb = if dry_run { "would be made" } else { "made" };
|
||||
eprintln!("sievescript {name:?}: {verb} active");
|
||||
}
|
||||
}
|
||||
@@ -41,10 +41,10 @@ pub enum ManageSieveAuth {
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct Endpoint {
|
||||
host: String,
|
||||
port: u16,
|
||||
implicit_tls: bool,
|
||||
pub(crate) struct Endpoint {
|
||||
pub(crate) host: String,
|
||||
pub(crate) port: u16,
|
||||
pub(crate) implicit_tls: bool,
|
||||
}
|
||||
|
||||
struct ControlCtx {
|
||||
@@ -378,7 +378,7 @@ fn build_dry_run_summary(
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_endpoint(url: &str) -> Result<Endpoint, Error> {
|
||||
pub(crate) fn parse_endpoint(url: &str) -> Result<Endpoint, Error> {
|
||||
let parsed =
|
||||
Url::parse(url).map_err(|e| Error::Usage(format!("invalid --url {url:?}: {e}")))?;
|
||||
let scheme = parsed.scheme();
|
||||
@@ -403,7 +403,10 @@ fn parse_endpoint(url: &str) -> Result<Endpoint, Error> {
|
||||
})
|
||||
}
|
||||
|
||||
fn authenticate(client: &mut SieveClient, auth: &ManageSieveAuth) -> Result<String, Error> {
|
||||
pub(crate) fn authenticate(
|
||||
client: &mut SieveClient,
|
||||
auth: &ManageSieveAuth,
|
||||
) -> Result<String, Error> {
|
||||
do_authenticate(client, auth).map_err(|e| match e {
|
||||
SieveAuthError::TerminallyRefused(text) => {
|
||||
Error::Connection(format!("auth failed: {text}"))
|
||||
|
||||
@@ -9,6 +9,7 @@ pub mod batch;
|
||||
pub mod emailmeta;
|
||||
pub mod export;
|
||||
pub mod export_imap;
|
||||
pub mod export_managesieve;
|
||||
pub mod import_dav;
|
||||
pub mod import_exchange_ews;
|
||||
pub mod import_exchange_graph;
|
||||
|
||||
@@ -0,0 +1,141 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2026 Coffey Labs LLC <[email protected]>
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
//! `export-managesieve` into a real Dovecot (Pigeonhole), read back with
|
||||
//! `import managesieve`: a dry run checks every script and writes nothing,
|
||||
//! scripts and the active one arrive exactly, a second run writes nothing, a changed script is all a third run sends, and a
|
||||
//! script the server refuses is counted and doesn't stop the rest.
|
||||
|
||||
mod integration;
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use integration::dovecot::Dovecot;
|
||||
use integration::validate::{cleanup, common, open_archive, tmp_archive};
|
||||
|
||||
use inbuxa_migrate::db::{blobs, init};
|
||||
use inbuxa_migrate::sync::export_managesieve::{self, ExportManageSieveConfig};
|
||||
use inbuxa_migrate::sync::import_managesieve::{self, ManageSieveAuth, ManageSieveImportConfig};
|
||||
use inbuxa_migrate::sync::{Summary, TypeCounts};
|
||||
use rusqlite::{Connection, params};
|
||||
|
||||
const VACATION: &str = "require \"vacation\";\r\nvacation :days 7 \"Away until Monday.\";\r\n";
|
||||
const FILTER: &str = "require \"fileinto\";\r\nif header :contains \"subject\" \"[list]\" {\r\n fileinto \"INBOX\";\r\n}\r\n";
|
||||
const BROKEN: &str = "this is not sieve;\r\n";
|
||||
|
||||
fn seed(conn: &Connection, scripts: &[(&str, &str, bool)]) {
|
||||
for (name, body, active) in scripts {
|
||||
let blob = blobs::intern_blob(conn, body.as_bytes()).unwrap();
|
||||
conn.execute(
|
||||
"INSERT INTO sieve_scripts (name, is_active, blob_id) VALUES (?1, ?2, ?3)",
|
||||
params![name, active, blob],
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
fn counts(summary: &Summary) -> &TypeCounts {
|
||||
&summary
|
||||
.per_type
|
||||
.iter()
|
||||
.find(|(t, _)| *t == "sievescript")
|
||||
.unwrap()
|
||||
.1
|
||||
}
|
||||
|
||||
/// Each script as name → (active, body).
|
||||
fn scripts(conn: &Connection) -> BTreeMap<String, (bool, String)> {
|
||||
conn.prepare(
|
||||
"SELECT s.name, s.is_active, b.data FROM sieve_scripts s JOIN blobs b ON b.id = s.blob_id",
|
||||
)
|
||||
.unwrap()
|
||||
.query_map([], |r| {
|
||||
Ok((
|
||||
r.get::<_, String>(0)?,
|
||||
(
|
||||
r.get::<_, bool>(1)?,
|
||||
String::from_utf8(r.get::<_, Vec<u8>>(2)?).unwrap(),
|
||||
),
|
||||
))
|
||||
})
|
||||
.unwrap()
|
||||
.map(Result::unwrap)
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "requires Docker"]
|
||||
fn dovecot_export_managesieve_round_trip_converges() {
|
||||
let d = Dovecot::start().expect("dovecot start");
|
||||
let account = d.accounts.last().expect("account").clone();
|
||||
let auth = ManageSieveAuth::Basic {
|
||||
user: account.username.clone(),
|
||||
password: account.password.clone(),
|
||||
};
|
||||
let url = format!("sieve://{}:{}", d.sieve.host, d.sieve.port);
|
||||
let config = || ExportManageSieveConfig {
|
||||
url: url.clone(),
|
||||
auth: auth.clone(),
|
||||
allow_cleartext: true,
|
||||
};
|
||||
|
||||
let archive = tmp_archive("export-sieve-src");
|
||||
seed(
|
||||
&init::open(&archive).unwrap(),
|
||||
&[
|
||||
("vacation", VACATION, false),
|
||||
("filter", FILTER, true),
|
||||
("broken", BROKEN, false),
|
||||
],
|
||||
);
|
||||
|
||||
let mut dry = common(&archive);
|
||||
dry.dry_run = true;
|
||||
let planned = export_managesieve::run(dry, config()).expect("dry run");
|
||||
let c = counts(&planned);
|
||||
// CHECKSCRIPT finds the broken one before anything is written.
|
||||
assert_eq!((c.created, c.failed), (2, 1), "{planned:?}");
|
||||
|
||||
// Created, not skipped: the dry run wrote nothing.
|
||||
let first = export_managesieve::run(common(&archive), config()).expect("first export");
|
||||
let c = counts(&first);
|
||||
assert_eq!((c.created, c.failed), (2, 1), "{first:?}");
|
||||
|
||||
let again = export_managesieve::run(common(&archive), config()).expect("second export");
|
||||
let c = counts(&again);
|
||||
assert_eq!((c.created, c.updated, c.skipped, c.failed), (0, 0, 2, 1));
|
||||
|
||||
let back = tmp_archive("export-sieve-back");
|
||||
let imported = import_managesieve::run(
|
||||
common(&back),
|
||||
ManageSieveImportConfig {
|
||||
url: url.clone(),
|
||||
auth: auth.clone(),
|
||||
allow_cleartext: true,
|
||||
allow_source_change: false,
|
||||
},
|
||||
)
|
||||
.expect("import back");
|
||||
assert!(!imported.any_failed(), "{imported:?}");
|
||||
let mut expected = scripts(&open_archive(&archive));
|
||||
expected.remove("broken");
|
||||
assert_eq!(scripts(&open_archive(&back)), expected);
|
||||
|
||||
let changed = blobs::intern_blob(&open_archive(&archive), b"keep;\r\n").unwrap();
|
||||
open_archive(&archive)
|
||||
.execute(
|
||||
"UPDATE sieve_scripts SET blob_id = ?1 WHERE name = 'vacation'",
|
||||
params![changed],
|
||||
)
|
||||
.unwrap();
|
||||
let third = export_managesieve::run(common(&archive), config()).expect("third export");
|
||||
let c = counts(&third);
|
||||
assert_eq!((c.created, c.updated, c.skipped), (0, 1, 1), "{third:?}");
|
||||
|
||||
cleanup(&archive);
|
||||
cleanup(&back);
|
||||
d.stop().expect("dovecot stop");
|
||||
}
|
||||
@@ -1057,3 +1057,86 @@ fn transient_no_exhausting_max_retries_skips_script_and_followup_picks_it_up() {
|
||||
assert_eq!(count(&conn, "sieve_scripts"), 1);
|
||||
let _ = std::fs::remove_file(&archive);
|
||||
}
|
||||
|
||||
/// The script a `PUTSCRIPT "<name>" {N+}` line announces, read off the wire.
|
||||
fn read_put(conn: &mut MockConn, name: &str) -> std::io::Result<Vec<u8>> {
|
||||
use std::io::Read;
|
||||
let cmd = conn.read_line()?;
|
||||
let prefix = format!("PUTSCRIPT \"{name}\" {{");
|
||||
let size: usize = cmd
|
||||
.strip_prefix(&prefix)
|
||||
.and_then(|rest| rest.strip_suffix("+}"))
|
||||
.unwrap_or_else(|| panic!("got {cmd}"))
|
||||
.parse()
|
||||
.unwrap();
|
||||
let mut body = vec![0; size + 2];
|
||||
conn.reader.read_exact(&mut body)?;
|
||||
assert_eq!(&body[size..], b"\r\n");
|
||||
body.truncate(size);
|
||||
Ok(body)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn export_writes_what_differs_and_moves_the_active_script() {
|
||||
use inbuxa_migrate::db::{blobs, init};
|
||||
use inbuxa_migrate::sync::export_managesieve::{self, ExportManageSieveConfig};
|
||||
|
||||
let archive = tempfile("export");
|
||||
{
|
||||
let conn = init::open(&archive).unwrap();
|
||||
for (name, body, active) in [
|
||||
("bad", "nonsense", false),
|
||||
("new", "keep;", true),
|
||||
("same", "stop;", false),
|
||||
] {
|
||||
let blob = blobs::intern_blob(&conn, body.as_bytes()).unwrap();
|
||||
conn.execute(
|
||||
"INSERT INTO sieve_scripts (name, is_active, blob_id) VALUES (?1, ?2, ?3)",
|
||||
rusqlite::params![name, active, blob],
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
let server = MockSieveServer::start(|conn| {
|
||||
auth_then(conn, |c| {
|
||||
assert_eq!(c.read_line()?, "LISTSCRIPTS");
|
||||
c.write_line("\"same\" ACTIVE")?;
|
||||
c.write_line("OK")?;
|
||||
|
||||
assert_eq!(read_put(c, "bad")?, b"nonsense");
|
||||
c.write_line("NO \"line 1: unknown command\"")?;
|
||||
|
||||
assert_eq!(read_put(c, "new")?, b"keep;");
|
||||
c.write_line("OK")?;
|
||||
|
||||
assert_eq!(c.read_line()?, "GETSCRIPT \"same\"");
|
||||
c.write_line("{5}")?;
|
||||
c.write_raw(b"stop;\r\n")?;
|
||||
c.write_line("OK")?;
|
||||
|
||||
assert_eq!(c.read_line()?, "SETACTIVE \"new\"");
|
||||
c.write_line("OK")?;
|
||||
Ok(())
|
||||
})
|
||||
});
|
||||
let common = CommonConfig {
|
||||
archive: archive.clone(),
|
||||
threads: 1,
|
||||
dry_run: false,
|
||||
max_retries: 1,
|
||||
allow_invalid_certs: false,
|
||||
logger: Logger::from_flags(true, 0),
|
||||
};
|
||||
let config = ExportManageSieveConfig {
|
||||
url: server.url(),
|
||||
auth: ManageSieveAuth::Basic {
|
||||
user: "alice".to_owned(),
|
||||
password: "p@ss".to_owned(),
|
||||
},
|
||||
allow_cleartext: true,
|
||||
};
|
||||
let summary = export_managesieve::run(common, config).expect("export");
|
||||
let c = &summary.per_type[0].1;
|
||||
assert_eq!((c.created, c.updated, c.skipped, c.failed), (1, 0, 1, 1));
|
||||
let _ = std::fs::remove_file(&archive);
|
||||
}
|
||||
Reference in new issue
Block a user