feat: complete code formatting and linting compliance
- Fix all Rust clippy warnings with targeted #[allow] attributes for justified cases - Implement server-side IMAP SEARCH keyword filtering in Go implementation - Add graceful fallback from server-side to client-side filtering when IMAP server lacks SEARCH support - Ensure both implementations use identical filtering logic for consistent results - Complete comprehensive testing of filtering and attachment handling functionality - Verify production readiness with proper linting standards for both Go and Rust 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
436276f0ef
commit
6c387abfbb
13 changed files with 851 additions and 432 deletions
|
|
@ -1,5 +1,5 @@
|
|||
//! Command line interface for mail2couch
|
||||
//!
|
||||
//!
|
||||
//! This module handles command line argument parsing and bash completion generation,
|
||||
//! matching the behavior of the Go implementation.
|
||||
|
||||
|
|
@ -47,7 +47,7 @@ pub fn parse_command_line() -> CommandLineArgs {
|
|||
}
|
||||
|
||||
CommandLineArgs {
|
||||
config_path: matches.get_one::<String>("config").map(|s| s.clone()),
|
||||
config_path: matches.get_one::<String>("config").cloned(),
|
||||
max_messages: matches.get_one::<u32>("max-messages").copied(),
|
||||
dry_run: matches.get_flag("dry-run"),
|
||||
generate_bash_completion: matches.get_flag("generate-bash-completion"),
|
||||
|
|
@ -57,20 +57,23 @@ pub fn parse_command_line() -> CommandLineArgs {
|
|||
|
||||
/// Generate bash completion script for mail2couch
|
||||
pub fn generate_bash_completion() {
|
||||
let app_name = env::args().next()
|
||||
let app_name = env::args()
|
||||
.next()
|
||||
.map(|path| {
|
||||
Path::new(&path).file_name()
|
||||
Path::new(&path)
|
||||
.file_name()
|
||||
.and_then(|name| name.to_str())
|
||||
.unwrap_or("mail2couch")
|
||||
.to_string()
|
||||
})
|
||||
.unwrap_or_else(|| "mail2couch".to_string());
|
||||
|
||||
let script = format!(r#"#!/bin/bash
|
||||
# Bash completion script for {}
|
||||
# Generated automatically by {} --generate-bash-completion
|
||||
let script = format!(
|
||||
r#"#!/bin/bash
|
||||
# Bash completion script for {app_name}
|
||||
# Generated automatically by {app_name} --generate-bash-completion
|
||||
|
||||
_{}_completions() {{
|
||||
_{app_name}_completions() {{
|
||||
local cur prev words cword
|
||||
_init_completion || return
|
||||
|
||||
|
|
@ -98,15 +101,16 @@ _{}_completions() {{
|
|||
}}
|
||||
|
||||
# Register the completion function
|
||||
complete -F _{}_completions {}
|
||||
complete -F _{app_name}_completions {app_name}
|
||||
|
||||
# Enable completion for common variations of the command name
|
||||
if [[ "$({} --help 2>/dev/null)" =~ "mail2couch" ]]; then
|
||||
complete -F _{}_completions mail2couch
|
||||
if [[ "$({app_name} --help 2>/dev/null)" =~ "mail2couch" ]]; then
|
||||
complete -F _{app_name}_completions mail2couch
|
||||
fi
|
||||
"#, app_name, app_name, app_name, app_name, app_name, app_name, app_name);
|
||||
"#
|
||||
);
|
||||
|
||||
print!("{}", script);
|
||||
print!("{script}");
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
@ -122,4 +126,4 @@ mod tests {
|
|||
});
|
||||
// Just verify it doesn't panic, we can't easily test the output without capturing stdout
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
//! Configuration loading and management for mail2couch
|
||||
//!
|
||||
//!
|
||||
//! This module handles loading configuration from JSON files with automatic
|
||||
//! file discovery, matching the behavior of the Go implementation.
|
||||
|
||||
|
|
@ -117,7 +117,7 @@ impl Config {
|
|||
pub fn load_from_path(path: &str) -> Result<Self, ConfigError> {
|
||||
let content = fs::read_to_string(path)?;
|
||||
let mut config: Config = serde_json::from_str(&content)?;
|
||||
|
||||
|
||||
// Validate and set defaults for mail sources
|
||||
for source in &mut config.mail_sources {
|
||||
if source.mode.is_empty() {
|
||||
|
|
@ -130,7 +130,7 @@ impl Config {
|
|||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
|
|
@ -161,7 +161,12 @@ impl Config {
|
|||
|
||||
// Add user directory paths
|
||||
if let Some(home_dir) = dirs::home_dir() {
|
||||
candidates.push(home_dir.join(".config").join("mail2couch").join("config.json"));
|
||||
candidates.push(
|
||||
home_dir
|
||||
.join(".config")
|
||||
.join("mail2couch")
|
||||
.join("config.json"),
|
||||
);
|
||||
candidates.push(home_dir.join(".mail2couch.json"));
|
||||
}
|
||||
|
||||
|
|
@ -172,9 +177,7 @@ impl Config {
|
|||
}
|
||||
}
|
||||
|
||||
Err(ConfigError::NotFound {
|
||||
paths: candidates,
|
||||
})
|
||||
Err(ConfigError::NotFound { paths: candidates })
|
||||
}
|
||||
|
||||
/// Load configuration with automatic file discovery
|
||||
|
|
@ -224,10 +227,10 @@ mod tests {
|
|||
"#;
|
||||
|
||||
let config: Config = serde_json::from_str(config_json).unwrap();
|
||||
|
||||
|
||||
assert_eq!(config.couch_db.url, "http://localhost:5984");
|
||||
assert_eq!(config.mail_sources.len(), 1);
|
||||
|
||||
|
||||
let source = &config.mail_sources[0];
|
||||
assert_eq!(source.name, "Test Account");
|
||||
assert_eq!(source.mode, "archive");
|
||||
|
|
@ -267,20 +270,20 @@ mod tests {
|
|||
fn test_config_file_discovery() {
|
||||
let temp_dir = tempdir().unwrap();
|
||||
let config_path = temp_dir.path().join("config.json");
|
||||
|
||||
|
||||
let config_content = r#"
|
||||
{
|
||||
"couchDb": {"url": "http://localhost:5984", "user": "admin", "password": "password"},
|
||||
"mailSources": []
|
||||
}
|
||||
"#;
|
||||
|
||||
|
||||
fs::write(&config_path, config_content).unwrap();
|
||||
|
||||
|
||||
// Change to temp directory for relative path test
|
||||
let original_dir = std::env::current_dir().unwrap();
|
||||
std::env::set_current_dir(&temp_dir).unwrap();
|
||||
|
||||
|
||||
let args = CommandLineArgs {
|
||||
config_path: None,
|
||||
max_messages: None,
|
||||
|
|
@ -288,11 +291,11 @@ mod tests {
|
|||
generate_bash_completion: false,
|
||||
help: false,
|
||||
};
|
||||
|
||||
|
||||
let found_path = Config::find_config_file(&args).unwrap();
|
||||
assert_eq!(found_path, PathBuf::from("config.json"));
|
||||
|
||||
|
||||
// Restore original directory
|
||||
std::env::set_current_dir(original_dir).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
//! CouchDB client integration for mail2couch
|
||||
//!
|
||||
//!
|
||||
//! This module provides a CouchDB client that handles database operations
|
||||
//! for storing email messages and sync metadata.
|
||||
|
||||
|
|
@ -58,14 +58,14 @@ impl CouchClient {
|
|||
match operation().await {
|
||||
Ok(result) => {
|
||||
if attempt > 1 {
|
||||
log::debug!("✅ CouchDB {} successful on attempt {}", operation_name, attempt);
|
||||
log::debug!("✅ CouchDB {operation_name} successful on attempt {attempt}");
|
||||
}
|
||||
return Ok(result);
|
||||
}
|
||||
Err(e) => {
|
||||
// Check if this is a retryable error
|
||||
let is_retryable = match &e.downcast_ref::<CouchError>() {
|
||||
Some(CouchError::Http(_)) => true, // Network errors are retryable
|
||||
Some(CouchError::Http(_)) => true, // Network errors are retryable
|
||||
Some(CouchError::CouchDb { status, .. }) => {
|
||||
// Retry on server errors (5xx) but not client errors (4xx)
|
||||
*status >= 500
|
||||
|
|
@ -74,7 +74,7 @@ impl CouchClient {
|
|||
};
|
||||
|
||||
last_error = Some(e);
|
||||
|
||||
|
||||
if is_retryable && attempt < MAX_RETRIES {
|
||||
log::warn!(
|
||||
"🔄 CouchDB {} attempt {} failed, retrying in {}ms: {}",
|
||||
|
|
@ -120,13 +120,13 @@ impl CouchClient {
|
|||
pub async fn test_connection(&self) -> Result<()> {
|
||||
let url = format!("{}/", self.base_url);
|
||||
let mut request = self.client.get(&url);
|
||||
|
||||
|
||||
if let Some((username, password)) = &self.auth {
|
||||
request = request.basic_auth(username, Some(password));
|
||||
}
|
||||
|
||||
let response = request.send().await?;
|
||||
|
||||
|
||||
if response.status().is_success() {
|
||||
Ok(())
|
||||
} else {
|
||||
|
|
@ -143,18 +143,23 @@ impl CouchClient {
|
|||
|
||||
let url = format!("{}/{}", self.base_url, db_name);
|
||||
let mut request = self.client.put(&url);
|
||||
|
||||
|
||||
if let Some((username, password)) = &self.auth {
|
||||
request = request.basic_auth(username, Some(password));
|
||||
}
|
||||
|
||||
let response = request.send().await?;
|
||||
|
||||
|
||||
match response.status() {
|
||||
StatusCode::CREATED | StatusCode::ACCEPTED => Ok(()),
|
||||
status => {
|
||||
let error_text = response.text().await?;
|
||||
Err(anyhow!("Failed to create database {}: {} - {}", db_name, status, error_text))
|
||||
Err(anyhow!(
|
||||
"Failed to create database {}: {} - {}",
|
||||
db_name,
|
||||
status,
|
||||
error_text
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -163,7 +168,7 @@ impl CouchClient {
|
|||
pub async fn database_exists(&self, db_name: &str) -> Result<bool> {
|
||||
let url = format!("{}/{}", self.base_url, db_name);
|
||||
let mut request = self.client.head(&url);
|
||||
|
||||
|
||||
if let Some((username, password)) = &self.auth {
|
||||
request = request.basic_auth(username, Some(password));
|
||||
}
|
||||
|
|
@ -173,14 +178,18 @@ impl CouchClient {
|
|||
}
|
||||
|
||||
/// Store a mail document in CouchDB with optional attachments and retry logic
|
||||
pub async fn store_mail_document(&self, db_name: &str, mut document: MailDocument) -> Result<String> {
|
||||
pub async fn store_mail_document(
|
||||
&self,
|
||||
db_name: &str,
|
||||
mut document: MailDocument,
|
||||
) -> Result<String> {
|
||||
// Set the document ID if not already set
|
||||
if document.id.is_none() {
|
||||
document.set_id();
|
||||
}
|
||||
|
||||
let doc_id = document.id.as_ref().unwrap().clone();
|
||||
|
||||
|
||||
// Check if document already exists to avoid duplicates
|
||||
if self.document_exists(db_name, &doc_id).await? {
|
||||
return Ok(doc_id);
|
||||
|
|
@ -190,30 +199,33 @@ impl CouchClient {
|
|||
let encoded_doc_id = urlencoding::encode(&doc_id);
|
||||
let url = format!("{}/{}/{}", self.base_url, db_name, encoded_doc_id);
|
||||
let mut request = self.client.put(&url).json(&document);
|
||||
|
||||
|
||||
if let Some((username, password)) = &self.auth {
|
||||
request = request.basic_auth(username, Some(password));
|
||||
}
|
||||
|
||||
let response = request.send().await
|
||||
.map_err(|e| CouchError::Http(e))?;
|
||||
|
||||
let response = request.send().await.map_err(CouchError::Http)?;
|
||||
|
||||
match response.status() {
|
||||
StatusCode::CREATED | StatusCode::ACCEPTED => {
|
||||
let couch_response: CouchResponse = response.json().await
|
||||
.map_err(|e| CouchError::Http(e))?;
|
||||
let couch_response: CouchResponse =
|
||||
response.json().await.map_err(CouchError::Http)?;
|
||||
Ok(couch_response.id.unwrap_or_else(|| doc_id.clone()))
|
||||
}
|
||||
status => {
|
||||
let error_text = response.text().await
|
||||
let error_text = response
|
||||
.text()
|
||||
.await
|
||||
.unwrap_or_else(|_| "Failed to read error response".to_string());
|
||||
Err(CouchError::CouchDb {
|
||||
status: status.as_u16(),
|
||||
message: error_text,
|
||||
}.into())
|
||||
}
|
||||
.into())
|
||||
}
|
||||
}
|
||||
}).await
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
/// Store an attachment for a document in CouchDB
|
||||
|
|
@ -232,26 +244,35 @@ impl CouchClient {
|
|||
// Upload the attachment
|
||||
let encoded_doc_id = urlencoding::encode(doc_id);
|
||||
let encoded_attachment_name = urlencoding::encode(attachment_name);
|
||||
let url = format!("{}/{}/{}/{}?rev={}", self.base_url, db_name, encoded_doc_id, encoded_attachment_name, rev);
|
||||
let mut request = self.client
|
||||
let url = format!(
|
||||
"{}/{}/{}/{}?rev={}",
|
||||
self.base_url, db_name, encoded_doc_id, encoded_attachment_name, rev
|
||||
);
|
||||
let mut request = self
|
||||
.client
|
||||
.put(&url)
|
||||
.header("Content-Type", content_type)
|
||||
.body(data.to_vec());
|
||||
|
||||
|
||||
if let Some((username, password)) = &self.auth {
|
||||
request = request.basic_auth(username, Some(password));
|
||||
}
|
||||
|
||||
let response = request.send().await?;
|
||||
|
||||
|
||||
match response.status() {
|
||||
StatusCode::CREATED | StatusCode::ACCEPTED => {
|
||||
let couch_response: CouchResponse = response.json().await?;
|
||||
Ok(couch_response.rev.unwrap_or_else(|| rev))
|
||||
Ok(couch_response.rev.unwrap_or(rev))
|
||||
}
|
||||
status => {
|
||||
let error_text = response.text().await?;
|
||||
Err(anyhow!("Failed to store attachment {}: {} - {}", attachment_name, status, error_text))
|
||||
Err(anyhow!(
|
||||
"Failed to store attachment {}: {} - {}",
|
||||
attachment_name,
|
||||
status,
|
||||
error_text
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -261,13 +282,13 @@ impl CouchClient {
|
|||
let encoded_doc_id = urlencoding::encode(doc_id);
|
||||
let url = format!("{}/{}/{}", self.base_url, db_name, encoded_doc_id);
|
||||
let mut request = self.client.get(&url);
|
||||
|
||||
|
||||
if let Some((username, password)) = &self.auth {
|
||||
request = request.basic_auth(username, Some(password));
|
||||
}
|
||||
|
||||
let response = request.send().await?;
|
||||
|
||||
|
||||
match response.status() {
|
||||
StatusCode::OK => {
|
||||
let doc: Value = response.json().await?;
|
||||
|
|
@ -276,15 +297,24 @@ impl CouchClient {
|
|||
StatusCode::NOT_FOUND => Ok(None),
|
||||
status => {
|
||||
let error_text = response.text().await?;
|
||||
Err(anyhow!("Failed to get document {}: {} - {}", doc_id, status, error_text))
|
||||
Err(anyhow!(
|
||||
"Failed to get document {}: {} - {}",
|
||||
doc_id,
|
||||
status,
|
||||
error_text
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Store sync metadata in CouchDB
|
||||
pub async fn store_sync_metadata(&self, db_name: &str, metadata: &SyncMetadata) -> Result<String> {
|
||||
pub async fn store_sync_metadata(
|
||||
&self,
|
||||
db_name: &str,
|
||||
metadata: &SyncMetadata,
|
||||
) -> Result<String> {
|
||||
let doc_id = metadata.id.as_ref().unwrap();
|
||||
|
||||
|
||||
// Try to get existing document first to get the revision
|
||||
let mut metadata_to_store = metadata.clone();
|
||||
if let Ok(existing) = self.get_sync_metadata(db_name, &metadata.mailbox).await {
|
||||
|
|
@ -294,13 +324,13 @@ impl CouchClient {
|
|||
let encoded_doc_id = urlencoding::encode(doc_id);
|
||||
let url = format!("{}/{}/{}", self.base_url, db_name, encoded_doc_id);
|
||||
let mut request = self.client.put(&url).json(&metadata_to_store);
|
||||
|
||||
|
||||
if let Some((username, password)) = &self.auth {
|
||||
request = request.basic_auth(username, Some(password));
|
||||
}
|
||||
|
||||
let response = request.send().await?;
|
||||
|
||||
|
||||
match response.status() {
|
||||
StatusCode::CREATED | StatusCode::ACCEPTED => {
|
||||
let couch_response: CouchResponse = response.json().await?;
|
||||
|
|
@ -308,35 +338,43 @@ impl CouchClient {
|
|||
}
|
||||
status => {
|
||||
let error_text = response.text().await?;
|
||||
Err(anyhow!("Failed to store sync metadata {}: {} - {}", doc_id, status, error_text))
|
||||
Err(anyhow!(
|
||||
"Failed to store sync metadata {}: {} - {}",
|
||||
doc_id,
|
||||
status,
|
||||
error_text
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Get sync metadata for a mailbox
|
||||
pub async fn get_sync_metadata(&self, db_name: &str, mailbox: &str) -> Result<SyncMetadata> {
|
||||
let doc_id = format!("sync_metadata_{}", mailbox);
|
||||
let doc_id = format!("sync_metadata_{mailbox}");
|
||||
let encoded_doc_id = urlencoding::encode(&doc_id);
|
||||
let url = format!("{}/{}/{}", self.base_url, db_name, encoded_doc_id);
|
||||
let mut request = self.client.get(&url);
|
||||
|
||||
|
||||
if let Some((username, password)) = &self.auth {
|
||||
request = request.basic_auth(username, Some(password));
|
||||
}
|
||||
|
||||
let response = request.send().await?;
|
||||
|
||||
|
||||
match response.status() {
|
||||
StatusCode::OK => {
|
||||
let metadata: SyncMetadata = response.json().await?;
|
||||
Ok(metadata)
|
||||
}
|
||||
StatusCode::NOT_FOUND => {
|
||||
Err(CouchError::NotFound { id: doc_id }.into())
|
||||
}
|
||||
StatusCode::NOT_FOUND => Err(CouchError::NotFound { id: doc_id }.into()),
|
||||
status => {
|
||||
let error_text = response.text().await?;
|
||||
Err(anyhow!("Failed to get sync metadata {}: {} - {}", doc_id, status, error_text))
|
||||
Err(anyhow!(
|
||||
"Failed to get sync metadata {}: {} - {}",
|
||||
doc_id,
|
||||
status,
|
||||
error_text
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -346,7 +384,7 @@ impl CouchClient {
|
|||
let encoded_doc_id = urlencoding::encode(doc_id);
|
||||
let url = format!("{}/{}/{}", self.base_url, db_name, encoded_doc_id);
|
||||
let mut request = self.client.head(&url);
|
||||
|
||||
|
||||
if let Some((username, password)) = &self.auth {
|
||||
request = request.basic_auth(username, Some(password));
|
||||
}
|
||||
|
|
@ -359,13 +397,13 @@ impl CouchClient {
|
|||
pub async fn get_database_info(&self, db_name: &str) -> Result<Value> {
|
||||
let url = format!("{}/{}", self.base_url, db_name);
|
||||
let mut request = self.client.get(&url);
|
||||
|
||||
|
||||
if let Some((username, password)) = &self.auth {
|
||||
request = request.basic_auth(username, Some(password));
|
||||
}
|
||||
|
||||
let response = request.send().await?;
|
||||
|
||||
|
||||
match response.status() {
|
||||
StatusCode::OK => {
|
||||
let info: Value = response.json().await?;
|
||||
|
|
@ -373,7 +411,12 @@ impl CouchClient {
|
|||
}
|
||||
status => {
|
||||
let error_text = response.text().await?;
|
||||
Err(anyhow!("Failed to get database info for {}: {} - {}", db_name, status, error_text))
|
||||
Err(anyhow!(
|
||||
"Failed to get database info for {}: {} - {}",
|
||||
db_name,
|
||||
status,
|
||||
error_text
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -382,21 +425,24 @@ impl CouchClient {
|
|||
pub async fn get_mailbox_uids(&self, db_name: &str, mailbox: &str) -> Result<Vec<u32>> {
|
||||
let url = format!("{}/{}/_all_docs", self.base_url, db_name);
|
||||
let query_params = [
|
||||
("startkey", format!("\"{}\"", mailbox)),
|
||||
("endkey", format!("\"{}\\ufff0\"", mailbox)), // High Unicode character for range end
|
||||
("startkey", format!("\"{mailbox}\"")),
|
||||
("endkey", format!("\"{mailbox}\\ufff0\"")), // High Unicode character for range end
|
||||
("include_docs", "false".to_string()),
|
||||
];
|
||||
|
||||
let mut request = self.client.get(&url).query(&query_params);
|
||||
|
||||
|
||||
if let Some((username, password)) = &self.auth {
|
||||
request = request.basic_auth(username, Some(password));
|
||||
}
|
||||
|
||||
let response = request.send().await?;
|
||||
|
||||
|
||||
if !response.status().is_success() {
|
||||
return Err(anyhow!("Failed to query stored messages: {}", response.status()));
|
||||
return Err(anyhow!(
|
||||
"Failed to query stored messages: {}",
|
||||
response.status()
|
||||
));
|
||||
}
|
||||
|
||||
let result: serde_json::Value = response.json().await?;
|
||||
|
|
@ -406,7 +452,7 @@ impl CouchClient {
|
|||
for row in rows {
|
||||
if let Some(id) = row["id"].as_str() {
|
||||
// Parse UID from document ID format: {mailbox}_{uid}
|
||||
if let Some(uid_str) = id.strip_prefix(&format!("{}_", mailbox)) {
|
||||
if let Some(uid_str) = id.strip_prefix(&format!("{mailbox}_")) {
|
||||
if let Ok(uid) = uid_str.parse::<u32>() {
|
||||
uids.push(uid);
|
||||
}
|
||||
|
|
@ -424,36 +470,45 @@ impl CouchClient {
|
|||
let encoded_doc_id = urlencoding::encode(doc_id);
|
||||
let url = format!("{}/{}/{}", self.base_url, db_name, encoded_doc_id);
|
||||
let mut request = self.client.get(&url);
|
||||
|
||||
|
||||
if let Some((username, password)) = &self.auth {
|
||||
request = request.basic_auth(username, Some(password));
|
||||
}
|
||||
|
||||
let response = request.send().await?;
|
||||
|
||||
|
||||
if response.status() == StatusCode::NOT_FOUND {
|
||||
return Ok(()); // Document already doesn't exist
|
||||
}
|
||||
|
||||
let doc: Value = response.json().await?;
|
||||
let rev = doc["_rev"].as_str()
|
||||
let rev = doc["_rev"]
|
||||
.as_str()
|
||||
.ok_or_else(|| anyhow!("Document {} has no _rev field", doc_id))?;
|
||||
|
||||
// Now delete the document
|
||||
let delete_url = format!("{}/{}/{}?rev={}", self.base_url, db_name, encoded_doc_id, rev);
|
||||
// Now delete the document
|
||||
let delete_url = format!(
|
||||
"{}/{}/{}?rev={}",
|
||||
self.base_url, db_name, encoded_doc_id, rev
|
||||
);
|
||||
let mut delete_request = self.client.delete(&delete_url);
|
||||
|
||||
|
||||
if let Some((username, password)) = &self.auth {
|
||||
delete_request = delete_request.basic_auth(username, Some(password));
|
||||
}
|
||||
|
||||
let delete_response = delete_request.send().await?;
|
||||
|
||||
|
||||
match delete_response.status() {
|
||||
StatusCode::OK | StatusCode::ACCEPTED => Ok(()),
|
||||
status => {
|
||||
let error_text = delete_response.text().await?;
|
||||
Err(anyhow!("Failed to delete document {}: {} - {}", doc_id, status, error_text))
|
||||
Err(anyhow!(
|
||||
"Failed to delete document {}: {} - {}",
|
||||
doc_id,
|
||||
status,
|
||||
error_text
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -481,4 +536,4 @@ mod tests {
|
|||
|
||||
// Note: Additional integration tests would require a running CouchDB instance
|
||||
// These would be similar to the Go implementation tests
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
//! Folder and message filtering functionality for mail2couch
|
||||
//!
|
||||
//!
|
||||
//! This module provides filtering logic for determining which folders and messages
|
||||
//! should be processed, matching the behavior of the Go implementation.
|
||||
|
||||
|
|
@ -14,12 +14,18 @@ pub fn should_process_folder(folder_name: &str, filter: &FolderFilter) -> bool {
|
|||
let included = if filter.include.is_empty() {
|
||||
true
|
||||
} else {
|
||||
filter.include.iter().any(|pattern| matches_pattern(folder_name, pattern))
|
||||
filter
|
||||
.include
|
||||
.iter()
|
||||
.any(|pattern| matches_pattern(folder_name, pattern))
|
||||
};
|
||||
|
||||
// If included, check if it's excluded
|
||||
if included {
|
||||
!filter.exclude.iter().any(|pattern| matches_pattern(folder_name, pattern))
|
||||
!filter
|
||||
.exclude
|
||||
.iter()
|
||||
.any(|pattern| matches_pattern(folder_name, pattern))
|
||||
} else {
|
||||
false
|
||||
}
|
||||
|
|
@ -45,7 +51,8 @@ fn matches_pattern(folder_name: &str, pattern: &str) -> bool {
|
|||
|
||||
/// Apply folder filters to a list of folders and return the filtered list
|
||||
pub fn filter_folders(folders: &[String], filter: &FolderFilter) -> Vec<String> {
|
||||
folders.iter()
|
||||
folders
|
||||
.iter()
|
||||
.filter(|folder| should_process_folder(folder, filter))
|
||||
.cloned()
|
||||
.collect()
|
||||
|
|
@ -53,19 +60,23 @@ pub fn filter_folders(folders: &[String], filter: &FolderFilter) -> Vec<String>
|
|||
|
||||
/// Expand wildcard patterns to match actual folder names
|
||||
/// This is useful for validating patterns against available folders
|
||||
pub fn expand_patterns(patterns: &[String], available_folders: &[String]) -> Result<HashSet<String>> {
|
||||
pub fn expand_patterns(
|
||||
patterns: &[String],
|
||||
available_folders: &[String],
|
||||
) -> Result<HashSet<String>> {
|
||||
let mut expanded = HashSet::new();
|
||||
|
||||
for pattern in patterns {
|
||||
let matches: Vec<_> = available_folders.iter()
|
||||
let matches: Vec<_> = available_folders
|
||||
.iter()
|
||||
.filter(|folder| matches_pattern(folder, pattern))
|
||||
.cloned()
|
||||
.collect();
|
||||
|
||||
if matches.is_empty() {
|
||||
log::warn!("Pattern '{}' matches no folders", pattern);
|
||||
log::warn!("Pattern '{pattern}' matches no folders");
|
||||
} else {
|
||||
log::debug!("Pattern '{}' matches: {:?}", pattern, matches);
|
||||
log::debug!("Pattern '{pattern}' matches: {matches:?}");
|
||||
expanded.extend(matches);
|
||||
}
|
||||
}
|
||||
|
|
@ -75,26 +86,31 @@ pub fn expand_patterns(patterns: &[String], available_folders: &[String]) -> Res
|
|||
|
||||
/// Validate folder filter patterns against available folders
|
||||
/// Returns warnings for patterns that don't match any folders
|
||||
pub fn validate_folder_patterns(filter: &FolderFilter, available_folders: &[String]) -> Vec<String> {
|
||||
pub fn validate_folder_patterns(
|
||||
filter: &FolderFilter,
|
||||
available_folders: &[String],
|
||||
) -> Vec<String> {
|
||||
let mut warnings = Vec::new();
|
||||
|
||||
// Check include patterns
|
||||
for pattern in &filter.include {
|
||||
let matches = available_folders.iter()
|
||||
let matches = available_folders
|
||||
.iter()
|
||||
.any(|folder| matches_pattern(folder, pattern));
|
||||
|
||||
|
||||
if !matches {
|
||||
warnings.push(format!("Include pattern '{}' matches no folders", pattern));
|
||||
warnings.push(format!("Include pattern '{pattern}' matches no folders"));
|
||||
}
|
||||
}
|
||||
|
||||
// Check exclude patterns
|
||||
for pattern in &filter.exclude {
|
||||
let matches = available_folders.iter()
|
||||
let matches = available_folders
|
||||
.iter()
|
||||
.any(|folder| matches_pattern(folder, pattern));
|
||||
|
||||
|
||||
if !matches {
|
||||
warnings.push(format!("Exclude pattern '{}' matches no folders", pattern));
|
||||
warnings.push(format!("Exclude pattern '{pattern}' matches no folders"));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -103,17 +119,16 @@ pub fn validate_folder_patterns(filter: &FolderFilter, available_folders: &[Stri
|
|||
|
||||
/// Get a summary of folder filtering results
|
||||
pub fn get_filter_summary(
|
||||
all_folders: &[String],
|
||||
filtered_folders: &[String],
|
||||
filter: &FolderFilter
|
||||
all_folders: &[String],
|
||||
filtered_folders: &[String],
|
||||
filter: &FolderFilter,
|
||||
) -> String {
|
||||
let total_count = all_folders.len();
|
||||
let filtered_count = filtered_folders.len();
|
||||
let excluded_count = total_count - filtered_count;
|
||||
|
||||
let mut summary = format!(
|
||||
"Folder filtering: {} total, {} selected, {} excluded",
|
||||
total_count, filtered_count, excluded_count
|
||||
"Folder filtering: {total_count} total, {filtered_count} selected, {excluded_count} excluded"
|
||||
);
|
||||
|
||||
if !filter.include.is_empty() {
|
||||
|
|
@ -155,7 +170,7 @@ mod tests {
|
|||
assert!(matches_pattern("Work/Projects", "*/Projects"));
|
||||
assert!(matches_pattern("Work/Archive", "Work/A*"));
|
||||
assert!(matches_pattern("Sent", "?ent"));
|
||||
|
||||
|
||||
assert!(!matches_pattern("INBOX", "Sent"));
|
||||
assert!(!matches_pattern("Work/Projects", "Personal/*"));
|
||||
assert!(!matches_pattern("INBOX", "??"));
|
||||
|
|
@ -170,7 +185,7 @@ mod tests {
|
|||
};
|
||||
|
||||
let filtered = filter_folders(&folders, &filter);
|
||||
|
||||
|
||||
assert!(filtered.contains(&"INBOX".to_string()));
|
||||
assert!(filtered.contains(&"Work/Projects".to_string()));
|
||||
assert!(!filtered.contains(&"Trash".to_string()));
|
||||
|
|
@ -181,12 +196,16 @@ mod tests {
|
|||
fn test_folder_filtering_specific() {
|
||||
let folders = create_test_folders();
|
||||
let filter = FolderFilter {
|
||||
include: vec!["INBOX".to_string(), "Sent".to_string(), "Work/*".to_string()],
|
||||
include: vec![
|
||||
"INBOX".to_string(),
|
||||
"Sent".to_string(),
|
||||
"Work/*".to_string(),
|
||||
],
|
||||
exclude: vec!["*Temp*".to_string()],
|
||||
};
|
||||
|
||||
let filtered = filter_folders(&folders, &filter);
|
||||
|
||||
|
||||
assert!(filtered.contains(&"INBOX".to_string()));
|
||||
assert!(filtered.contains(&"Sent".to_string()));
|
||||
assert!(filtered.contains(&"Work/Projects".to_string()));
|
||||
|
|
@ -205,7 +224,7 @@ mod tests {
|
|||
};
|
||||
|
||||
let filtered = filter_folders(&folders, &filter);
|
||||
|
||||
|
||||
// Should include everything except excluded
|
||||
assert!(filtered.contains(&"INBOX".to_string()));
|
||||
assert!(filtered.contains(&"Work/Projects".to_string()));
|
||||
|
|
@ -218,9 +237,9 @@ mod tests {
|
|||
fn test_pattern_expansion() {
|
||||
let folders = create_test_folders();
|
||||
let patterns = vec!["Work/*".to_string(), "Personal/*".to_string()];
|
||||
|
||||
|
||||
let expanded = expand_patterns(&patterns, &folders).unwrap();
|
||||
|
||||
|
||||
assert!(expanded.contains("Work/Projects"));
|
||||
assert!(expanded.contains("Work/Archive"));
|
||||
assert!(expanded.contains("Work/Temp"));
|
||||
|
|
@ -239,7 +258,7 @@ mod tests {
|
|||
};
|
||||
|
||||
let warnings = validate_folder_patterns(&filter, &folders);
|
||||
|
||||
|
||||
assert_eq!(warnings.len(), 2);
|
||||
assert!(warnings.iter().any(|w| w.contains("NonExistent/*")));
|
||||
assert!(warnings.iter().any(|w| w.contains("AnotherNonExistent")));
|
||||
|
|
@ -255,9 +274,9 @@ mod tests {
|
|||
let filtered = filter_folders(&folders, &filter);
|
||||
|
||||
let summary = get_filter_summary(&folders, &filtered, &filter);
|
||||
|
||||
|
||||
assert!(summary.contains(&format!("{} total", folders.len())));
|
||||
assert!(summary.contains(&format!("{} selected", filtered.len())));
|
||||
assert!(summary.contains("exclude:"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
337
rust/src/imap.rs
337
rust/src/imap.rs
|
|
@ -1,10 +1,10 @@
|
|||
//! IMAP client functionality for mail2couch
|
||||
//!
|
||||
//!
|
||||
//! This module provides IMAP client operations for connecting to mail servers,
|
||||
//! listing mailboxes, and retrieving messages.
|
||||
|
||||
use crate::config::{MailSource, MessageFilter};
|
||||
use crate::schemas::{MailDocument, AttachmentStub};
|
||||
use crate::schemas::{AttachmentStub, MailDocument};
|
||||
use anyhow::{anyhow, Result};
|
||||
use async_imap::types::Fetch;
|
||||
use async_imap::{Client, Session};
|
||||
|
|
@ -112,7 +112,7 @@ impl ImapClient {
|
|||
session: None,
|
||||
source,
|
||||
};
|
||||
|
||||
|
||||
client.establish_connection_with_retry().await?;
|
||||
Ok(client)
|
||||
}
|
||||
|
|
@ -128,7 +128,7 @@ impl ImapClient {
|
|||
match self.establish_connection().await {
|
||||
Ok(()) => {
|
||||
if attempt > 1 {
|
||||
log::info!("✅ IMAP connection successful on attempt {}", attempt);
|
||||
log::info!("✅ IMAP connection successful on attempt {attempt}");
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
|
|
@ -154,20 +154,22 @@ impl ImapClient {
|
|||
))
|
||||
}
|
||||
|
||||
|
||||
/// Establish connection to IMAP server
|
||||
async fn establish_connection(&mut self) -> Result<()> {
|
||||
// Connect to the IMAP server
|
||||
let addr = format!("{}:{}", self.source.host, self.source.port);
|
||||
let tcp_stream = TcpStream::connect(&addr).await
|
||||
.map_err(|e| ImapError::Connection(format!("Failed to connect to {}: {}", addr, e)))?;
|
||||
let tcp_stream = TcpStream::connect(&addr)
|
||||
.await
|
||||
.map_err(|e| ImapError::Connection(format!("Failed to connect to {addr}: {e}")))?;
|
||||
|
||||
// Determine if we should use TLS based on port
|
||||
let imap_stream = if self.should_use_tls() {
|
||||
// Use TLS for secure connection (typically port 993)
|
||||
let tls_connector = TlsConnector::new();
|
||||
let tls_stream = tls_connector.connect(&self.source.host, tcp_stream).await
|
||||
.map_err(|e| ImapError::Connection(format!("TLS connection failed: {}", e)))?;
|
||||
let tls_stream = tls_connector
|
||||
.connect(&self.source.host, tcp_stream)
|
||||
.await
|
||||
.map_err(|e| ImapError::Connection(format!("TLS connection failed: {e}")))?;
|
||||
ImapStream::Tls(tls_stream)
|
||||
} else {
|
||||
// Use plain connection (typically port 143 or test environments)
|
||||
|
|
@ -175,7 +177,11 @@ impl ImapClient {
|
|||
};
|
||||
|
||||
// Log connection type for debugging
|
||||
let connection_type = if self.should_use_tls() { "TLS" } else { "Plain" };
|
||||
let connection_type = if self.should_use_tls() {
|
||||
"TLS"
|
||||
} else {
|
||||
"Plain"
|
||||
};
|
||||
log::debug!(
|
||||
"Connecting to {}:{} using {} connection",
|
||||
self.source.host,
|
||||
|
|
@ -190,7 +196,7 @@ impl ImapClient {
|
|||
let session = client
|
||||
.login(&self.source.user, &self.source.password)
|
||||
.await
|
||||
.map_err(|e| ImapError::Authentication(format!("Login failed: {:?}", e)))?;
|
||||
.map_err(|e| ImapError::Authentication(format!("Login failed: {e:?}")))?;
|
||||
|
||||
self.session = Some(session);
|
||||
Ok(())
|
||||
|
|
@ -219,17 +225,25 @@ impl ImapClient {
|
|||
|
||||
/// List all mailboxes
|
||||
pub async fn list_mailboxes(&mut self) -> Result<Vec<String>> {
|
||||
let session = self.session.as_mut()
|
||||
let session = self
|
||||
.session
|
||||
.as_mut()
|
||||
.ok_or_else(|| anyhow!("Not connected to IMAP server"))?;
|
||||
|
||||
let mut mailboxes = session.list(Some(""), Some("*")).await
|
||||
.map_err(|e| ImapError::Operation(format!("Failed to list mailboxes: {:?}", e)))?;
|
||||
let mut mailboxes = session
|
||||
.list(Some(""), Some("*"))
|
||||
.await
|
||||
.map_err(|e| ImapError::Operation(format!("Failed to list mailboxes: {e:?}")))?;
|
||||
|
||||
let mut mailbox_names = Vec::new();
|
||||
while let Some(mailbox_result) = mailboxes.next().await {
|
||||
match mailbox_result {
|
||||
Ok(mailbox) => mailbox_names.push(mailbox.name().to_string()),
|
||||
Err(e) => return Err(ImapError::Operation(format!("Error processing mailbox: {:?}", e)).into()),
|
||||
Err(e) => {
|
||||
return Err(
|
||||
ImapError::Operation(format!("Error processing mailbox: {e:?}")).into(),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -237,8 +251,13 @@ impl ImapClient {
|
|||
}
|
||||
|
||||
/// List mailboxes using IMAP LIST with server-side pattern filtering
|
||||
pub async fn list_filtered_mailboxes(&mut self, filter: &crate::config::FolderFilter) -> Result<Vec<String>> {
|
||||
let session = self.session.as_mut()
|
||||
pub async fn list_filtered_mailboxes(
|
||||
&mut self,
|
||||
filter: &crate::config::FolderFilter,
|
||||
) -> Result<Vec<String>> {
|
||||
let session = self
|
||||
.session
|
||||
.as_mut()
|
||||
.ok_or_else(|| anyhow!("Not connected to IMAP server"))?;
|
||||
|
||||
let mut all_mailboxes = Vec::new();
|
||||
|
|
@ -251,13 +270,14 @@ impl ImapClient {
|
|||
|
||||
// Use IMAP LIST with each include pattern for server-side filtering
|
||||
for pattern in &filter.include {
|
||||
log::debug!("Listing mailboxes with pattern: {}", pattern);
|
||||
|
||||
let mut mailboxes = session.list(Some(""), Some(pattern)).await
|
||||
.map_err(|e| {
|
||||
log::warn!("Failed to list mailboxes with pattern '{}': {:?}", pattern, e);
|
||||
ImapError::Operation(format!("Failed to list mailboxes with pattern '{}': {:?}", pattern, e))
|
||||
})?;
|
||||
log::debug!("Listing mailboxes with pattern: {pattern}");
|
||||
|
||||
let mut mailboxes = session.list(Some(""), Some(pattern)).await.map_err(|e| {
|
||||
log::warn!("Failed to list mailboxes with pattern '{pattern}': {e:?}");
|
||||
ImapError::Operation(format!(
|
||||
"Failed to list mailboxes with pattern '{pattern}': {e:?}"
|
||||
))
|
||||
})?;
|
||||
|
||||
while let Some(mailbox_result) = mailboxes.next().await {
|
||||
match mailbox_result {
|
||||
|
|
@ -268,7 +288,7 @@ impl ImapClient {
|
|||
}
|
||||
}
|
||||
Err(e) => {
|
||||
log::warn!("Error processing mailbox with pattern '{}': {:?}", pattern, e);
|
||||
log::warn!("Error processing mailbox with pattern '{pattern}': {e:?}");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
|
@ -283,9 +303,10 @@ impl ImapClient {
|
|||
let filtered_mailboxes: Vec<String> = all_mailboxes
|
||||
.into_iter()
|
||||
.filter(|mailbox| {
|
||||
!filter.exclude.iter().any(|exclude_pattern| {
|
||||
self.matches_imap_pattern(exclude_pattern, mailbox)
|
||||
})
|
||||
!filter
|
||||
.exclude
|
||||
.iter()
|
||||
.any(|exclude_pattern| self.matches_imap_pattern(exclude_pattern, mailbox))
|
||||
})
|
||||
.collect();
|
||||
|
||||
|
|
@ -300,8 +321,8 @@ impl ImapClient {
|
|||
}
|
||||
|
||||
// Handle simple prefix wildcard: "Work*" should match "Work/Projects"
|
||||
if pattern.ends_with('*') && !pattern[..pattern.len()-1].contains('*') {
|
||||
let prefix = &pattern[..pattern.len()-1];
|
||||
if pattern.ends_with('*') && !pattern[..pattern.len() - 1].contains('*') {
|
||||
let prefix = &pattern[..pattern.len() - 1];
|
||||
return name.starts_with(prefix);
|
||||
}
|
||||
|
||||
|
|
@ -313,7 +334,7 @@ impl ImapClient {
|
|||
|
||||
// Handle contains wildcard: "*Temp*" should match "Work/Temp/Archive"
|
||||
if pattern.starts_with('*') && pattern.ends_with('*') {
|
||||
let middle = &pattern[1..pattern.len()-1];
|
||||
let middle = &pattern[1..pattern.len() - 1];
|
||||
return name.contains(middle);
|
||||
}
|
||||
|
||||
|
|
@ -323,11 +344,14 @@ impl ImapClient {
|
|||
|
||||
/// Select a mailbox
|
||||
pub async fn select_mailbox(&mut self, mailbox: &str) -> Result<MailboxInfo> {
|
||||
let session = self.session.as_mut()
|
||||
let session = self
|
||||
.session
|
||||
.as_mut()
|
||||
.ok_or_else(|| anyhow!("Not connected to IMAP server"))?;
|
||||
|
||||
let mailbox_data = session.select(mailbox).await
|
||||
.map_err(|e| ImapError::Operation(format!("Failed to select mailbox {}: {:?}", mailbox, e)))?;
|
||||
let mailbox_data = session.select(mailbox).await.map_err(|e| {
|
||||
ImapError::Operation(format!("Failed to select mailbox {mailbox}: {e:?}"))
|
||||
})?;
|
||||
|
||||
Ok(MailboxInfo {
|
||||
name: mailbox.to_string(),
|
||||
|
|
@ -340,19 +364,22 @@ impl ImapClient {
|
|||
|
||||
/// Search for messages using IMAP SEARCH command with retry logic
|
||||
/// Returns UIDs of matching messages
|
||||
pub async fn search_messages(&mut self, since_date: Option<&DateTime<Utc>>) -> Result<Vec<u32>> {
|
||||
pub async fn search_messages(
|
||||
&mut self,
|
||||
since_date: Option<&DateTime<Utc>>,
|
||||
) -> Result<Vec<u32>> {
|
||||
const MAX_RETRIES: u32 = 3;
|
||||
const RETRY_DELAY_MS: u64 = 500;
|
||||
|
||||
|
||||
let mut last_error = None;
|
||||
|
||||
for attempt in 1..=MAX_RETRIES {
|
||||
let result = self.search_messages_internal(since_date).await;
|
||||
|
||||
|
||||
match result {
|
||||
Ok(uids) => {
|
||||
if attempt > 1 {
|
||||
log::debug!("✅ IMAP search successful on attempt {}", attempt);
|
||||
log::debug!("✅ IMAP search successful on attempt {attempt}");
|
||||
}
|
||||
return Ok(uids);
|
||||
}
|
||||
|
|
@ -379,29 +406,35 @@ impl ImapClient {
|
|||
}
|
||||
|
||||
/// Internal search implementation without retry logic
|
||||
async fn search_messages_internal(&mut self, since_date: Option<&DateTime<Utc>>) -> Result<Vec<u32>> {
|
||||
let session = self.session.as_mut()
|
||||
async fn search_messages_internal(
|
||||
&mut self,
|
||||
since_date: Option<&DateTime<Utc>>,
|
||||
) -> Result<Vec<u32>> {
|
||||
let session = self
|
||||
.session
|
||||
.as_mut()
|
||||
.ok_or_else(|| anyhow!("Not connected to IMAP server"))?;
|
||||
|
||||
let search_query = if let Some(since) = since_date {
|
||||
// Format date as required by IMAP (DD-MMM-YYYY)
|
||||
// IMAP months are 3-letter abbreviations in English
|
||||
let formatted_date = since.format("%d-%b-%Y").to_string();
|
||||
log::debug!("Searching for messages since: {}", formatted_date);
|
||||
format!("SINCE {}", formatted_date)
|
||||
log::debug!("Searching for messages since: {formatted_date}");
|
||||
format!("SINCE {formatted_date}")
|
||||
} else {
|
||||
log::debug!("Searching for all messages");
|
||||
"ALL".to_string()
|
||||
};
|
||||
|
||||
log::debug!("IMAP search query: {}", search_query);
|
||||
|
||||
let uids = session.uid_search(&search_query).await
|
||||
.map_err(|e| ImapError::Operation(format!("Search failed with query '{}': {:?}", search_query, e)))?;
|
||||
log::debug!("IMAP search query: {search_query}");
|
||||
|
||||
let uids = session.uid_search(&search_query).await.map_err(|e| {
|
||||
ImapError::Operation(format!("Search failed with query '{search_query}': {e:?}"))
|
||||
})?;
|
||||
|
||||
let uid_vec: Vec<u32> = uids.into_iter().collect();
|
||||
log::debug!("Found {} messages matching search criteria", uid_vec.len());
|
||||
|
||||
|
||||
Ok(uid_vec)
|
||||
}
|
||||
|
||||
|
|
@ -414,7 +447,9 @@ impl ImapClient {
|
|||
subject_keywords: Option<&[String]>,
|
||||
from_keywords: Option<&[String]>,
|
||||
) -> Result<Vec<u32>> {
|
||||
let session = self.session.as_mut()
|
||||
let session = self
|
||||
.session
|
||||
.as_mut()
|
||||
.ok_or_else(|| anyhow!("Not connected to IMAP server"))?;
|
||||
|
||||
let mut search_parts = Vec::new();
|
||||
|
|
@ -422,12 +457,12 @@ impl ImapClient {
|
|||
// Add date filters
|
||||
if let Some(since) = since_date {
|
||||
let formatted_date = since.format("%d-%b-%Y").to_string();
|
||||
search_parts.push(format!("SINCE {}", formatted_date));
|
||||
search_parts.push(format!("SINCE {formatted_date}"));
|
||||
}
|
||||
|
||||
if let Some(before) = before_date {
|
||||
let formatted_date = before.format("%d-%b-%Y").to_string();
|
||||
search_parts.push(format!("BEFORE {}", formatted_date));
|
||||
search_parts.push(format!("BEFORE {formatted_date}"));
|
||||
}
|
||||
|
||||
// Add subject keyword filters
|
||||
|
|
@ -451,25 +486,39 @@ impl ImapClient {
|
|||
search_parts.join(" ")
|
||||
};
|
||||
|
||||
log::debug!("Advanced IMAP search query: {}", search_query);
|
||||
|
||||
let uids = session.uid_search(&search_query).await
|
||||
.map_err(|e| ImapError::Operation(format!("Advanced search failed with query '{}': {:?}", search_query, e)))?;
|
||||
log::debug!("Advanced IMAP search query: {search_query}");
|
||||
|
||||
let uids = session.uid_search(&search_query).await.map_err(|e| {
|
||||
ImapError::Operation(format!(
|
||||
"Advanced search failed with query '{search_query}': {e:?}"
|
||||
))
|
||||
})?;
|
||||
|
||||
let uid_vec: Vec<u32> = uids.into_iter().collect();
|
||||
log::debug!("Found {} messages matching advanced search criteria", uid_vec.len());
|
||||
|
||||
log::debug!(
|
||||
"Found {} messages matching advanced search criteria",
|
||||
uid_vec.len()
|
||||
);
|
||||
|
||||
Ok(uid_vec)
|
||||
}
|
||||
|
||||
/// Fetch message by UID with attachment data
|
||||
pub async fn fetch_message(&mut self, uid: u32, mailbox: &str) -> Result<(MailDocument, Vec<(String, String, Vec<u8>)>)> {
|
||||
let session = self.session.as_mut()
|
||||
pub async fn fetch_message(
|
||||
&mut self,
|
||||
uid: u32,
|
||||
mailbox: &str,
|
||||
) -> Result<(MailDocument, Vec<(String, String, Vec<u8>)>)> {
|
||||
let session = self
|
||||
.session
|
||||
.as_mut()
|
||||
.ok_or_else(|| anyhow!("Not connected to IMAP server"))?;
|
||||
|
||||
// Fetch message headers and body
|
||||
let mut messages = session.uid_fetch(format!("{}", uid), "RFC822").await
|
||||
.map_err(|e| ImapError::Operation(format!("Failed to fetch message {}: {:?}", uid, e)))?;
|
||||
let mut messages = session
|
||||
.uid_fetch(format!("{uid}"), "RFC822")
|
||||
.await
|
||||
.map_err(|e| ImapError::Operation(format!("Failed to fetch message {uid}: {e:?}")))?;
|
||||
|
||||
// Collect the first message
|
||||
if let Some(message_result) = messages.next().await {
|
||||
|
|
@ -479,7 +528,10 @@ impl ImapClient {
|
|||
drop(messages);
|
||||
self.parse_message(&message, uid, mailbox).await
|
||||
}
|
||||
Err(e) => Err(ImapError::Operation(format!("Failed to process message {}: {:?}", uid, e)).into()),
|
||||
Err(e) => Err(ImapError::Operation(format!(
|
||||
"Failed to process message {uid}: {e:?}"
|
||||
))
|
||||
.into()),
|
||||
}
|
||||
} else {
|
||||
Err(anyhow!("Message {} not found", uid))
|
||||
|
|
@ -487,12 +539,19 @@ impl ImapClient {
|
|||
}
|
||||
|
||||
/// Fetch multiple messages by UIDs with attachment data
|
||||
pub async fn fetch_messages(&mut self, uids: &[u32], max_count: Option<u32>, mailbox: &str) -> Result<Vec<(MailDocument, Vec<(String, String, Vec<u8>)>)>> {
|
||||
pub async fn fetch_messages(
|
||||
&mut self,
|
||||
uids: &[u32],
|
||||
max_count: Option<u32>,
|
||||
mailbox: &str,
|
||||
) -> Result<Vec<(MailDocument, Vec<(String, String, Vec<u8>)>)>> {
|
||||
if uids.is_empty() {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
|
||||
let session = self.session.as_mut()
|
||||
let session = self
|
||||
.session
|
||||
.as_mut()
|
||||
.ok_or_else(|| anyhow!("Not connected to IMAP server"))?;
|
||||
|
||||
// Limit the number of messages if specified
|
||||
|
|
@ -507,24 +566,27 @@ impl ImapClient {
|
|||
};
|
||||
|
||||
// Create UID sequence
|
||||
let uid_sequence = uids_to_fetch.iter()
|
||||
let uid_sequence = uids_to_fetch
|
||||
.iter()
|
||||
.map(|uid| uid.to_string())
|
||||
.collect::<Vec<_>>()
|
||||
.join(",");
|
||||
|
||||
// Fetch messages
|
||||
let mut messages = session.uid_fetch(&uid_sequence, "RFC822").await
|
||||
.map_err(|e| ImapError::Operation(format!("Failed to fetch messages: {:?}", e)))?;
|
||||
let mut messages = session
|
||||
.uid_fetch(&uid_sequence, "RFC822")
|
||||
.await
|
||||
.map_err(|e| ImapError::Operation(format!("Failed to fetch messages: {e:?}")))?;
|
||||
|
||||
// Collect all messages first to avoid borrowing issues
|
||||
let mut fetched_messages = Vec::new();
|
||||
while let Some(message_result) = messages.next().await {
|
||||
match message_result {
|
||||
Ok(message) => fetched_messages.push(message),
|
||||
Err(e) => log::warn!("Failed to fetch message: {:?}", e),
|
||||
Err(e) => log::warn!("Failed to fetch message: {e:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Drop the messages stream to release the session borrow
|
||||
drop(messages);
|
||||
|
||||
|
|
@ -534,7 +596,7 @@ impl ImapClient {
|
|||
match self.parse_message(message, uid, mailbox).await {
|
||||
Ok((doc, attachments)) => mail_documents.push((doc, attachments)),
|
||||
Err(e) => {
|
||||
log::warn!("Failed to parse message {}: {}", uid, e);
|
||||
log::warn!("Failed to parse message {uid}: {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -544,8 +606,14 @@ impl ImapClient {
|
|||
}
|
||||
|
||||
/// Parse a raw IMAP message into a MailDocument with attachment data
|
||||
async fn parse_message(&self, message: &Fetch, uid: u32, mailbox: &str) -> Result<(MailDocument, Vec<(String, String, Vec<u8>)>)> {
|
||||
let body = message.body()
|
||||
async fn parse_message(
|
||||
&self,
|
||||
message: &Fetch,
|
||||
uid: u32,
|
||||
mailbox: &str,
|
||||
) -> Result<(MailDocument, Vec<(String, String, Vec<u8>)>)> {
|
||||
let body = message
|
||||
.body()
|
||||
.ok_or_else(|| ImapError::Parsing("No message body found".to_string()))?;
|
||||
|
||||
// Parse the email using mail-parser library
|
||||
|
|
@ -554,7 +622,7 @@ impl ImapClient {
|
|||
|
||||
// Extract sender addresses
|
||||
let from = self.extract_addresses(&parsed_message, "From");
|
||||
|
||||
|
||||
// Extract recipient addresses
|
||||
let to = self.extract_addresses(&parsed_message, "To");
|
||||
|
||||
|
|
@ -566,7 +634,7 @@ impl ImapClient {
|
|||
|
||||
// Extract date
|
||||
let date = if let Some(date_time) = parsed_message.get_date() {
|
||||
DateTime::from_timestamp(date_time.to_timestamp(), 0).unwrap_or_else(|| Utc::now())
|
||||
DateTime::from_timestamp(date_time.to_timestamp(), 0).unwrap_or_else(Utc::now)
|
||||
} else {
|
||||
Utc::now()
|
||||
};
|
||||
|
|
@ -578,7 +646,8 @@ impl ImapClient {
|
|||
let headers = self.extract_headers(&parsed_message);
|
||||
|
||||
// Extract attachments and their data
|
||||
let (has_attachments, _attachment_stubs, attachment_data) = self.extract_attachments_with_data(&parsed_message);
|
||||
let (has_attachments, _attachment_stubs, attachment_data) =
|
||||
self.extract_attachments_with_data(&parsed_message);
|
||||
|
||||
let mail_doc = MailDocument::new(
|
||||
uid.to_string(),
|
||||
|
|
@ -598,7 +667,11 @@ impl ImapClient {
|
|||
|
||||
// Log attachment information
|
||||
if !attachment_data.is_empty() {
|
||||
log::info!("Found {} attachments for message {}", attachment_data.len(), uid);
|
||||
log::info!(
|
||||
"Found {} attachments for message {}",
|
||||
attachment_data.len(),
|
||||
uid
|
||||
);
|
||||
}
|
||||
|
||||
Ok((mail_doc, attachment_data))
|
||||
|
|
@ -610,7 +683,7 @@ impl ImapClient {
|
|||
// For address headers, use as_text() and parse manually
|
||||
// mail-parser doesn't provide a direct address parsing method
|
||||
let header_text = header.as_text_ref().unwrap_or("");
|
||||
|
||||
|
||||
// Simple address extraction - split by comma and clean up
|
||||
header_text
|
||||
.split(',')
|
||||
|
|
@ -673,45 +746,55 @@ impl ImapClient {
|
|||
/// Extract all headers from a parsed message
|
||||
fn extract_headers(&self, message: &Message) -> HashMap<String, Vec<String>> {
|
||||
let mut headers = HashMap::new();
|
||||
|
||||
|
||||
for header in message.get_headers() {
|
||||
let name = header.name().to_lowercase();
|
||||
let value = match header.value().as_text_ref() {
|
||||
Some(text) => text.to_string(),
|
||||
None => format!("{:?}", header.value()), // Fallback for non-text values
|
||||
};
|
||||
|
||||
headers.entry(name)
|
||||
.or_insert_with(Vec::new)
|
||||
.push(value);
|
||||
|
||||
headers.entry(name).or_insert_with(Vec::new).push(value);
|
||||
}
|
||||
|
||||
|
||||
headers
|
||||
}
|
||||
|
||||
/// Extract attachments from a parsed message with binary data
|
||||
/// Returns (has_attachments, attachment_stubs, attachment_data)
|
||||
fn extract_attachments_with_data(&self, message: &Message) -> (bool, HashMap<String, AttachmentStub>, Vec<(String, String, Vec<u8>)>) {
|
||||
#[allow(clippy::type_complexity)]
|
||||
fn extract_attachments_with_data(
|
||||
&self,
|
||||
message: &Message,
|
||||
) -> (
|
||||
bool,
|
||||
HashMap<String, AttachmentStub>,
|
||||
Vec<(String, String, Vec<u8>)>,
|
||||
) {
|
||||
let mut attachment_stubs = HashMap::new();
|
||||
let mut attachment_data = Vec::new();
|
||||
|
||||
|
||||
// Iterate through all message parts looking for attachments
|
||||
for (index, part) in message.parts.iter().enumerate() {
|
||||
// Check if this part is an attachment
|
||||
if let Some(content_type) = part.get_content_type() {
|
||||
let is_attachment = self.is_attachment_part(part, &content_type);
|
||||
|
||||
let is_attachment = self.is_attachment_part(part, content_type);
|
||||
|
||||
if is_attachment {
|
||||
// Generate a filename for the attachment
|
||||
let filename = self.get_attachment_filename(part, index);
|
||||
|
||||
|
||||
// Get the binary content data using the proper mail-parser API
|
||||
// This works for both text and binary attachments (images, PDFs, etc.)
|
||||
let body_data = part.get_contents().to_vec();
|
||||
log::debug!("Found attachment content: {} bytes (content-type: {})", body_data.len(), content_type.c_type);
|
||||
|
||||
log::debug!(
|
||||
"Found attachment content: {} bytes (content-type: {})",
|
||||
body_data.len(),
|
||||
content_type.c_type
|
||||
);
|
||||
|
||||
let content_type_str = content_type.c_type.to_string();
|
||||
|
||||
|
||||
// Create attachment stub - get_contents() always returns the full data
|
||||
if !body_data.is_empty() {
|
||||
let attachment_stub = AttachmentStub {
|
||||
|
|
@ -719,36 +802,39 @@ impl ImapClient {
|
|||
length: Some(body_data.len() as u64),
|
||||
stub: None, // Will be stored as actual attachment data
|
||||
};
|
||||
|
||||
|
||||
attachment_stubs.insert(filename.clone(), attachment_stub);
|
||||
attachment_data.push((filename, content_type_str, body_data));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
let has_attachments = !attachment_stubs.is_empty();
|
||||
(has_attachments, attachment_stubs, attachment_data)
|
||||
}
|
||||
|
||||
|
||||
/// Determine if a message part is an attachment
|
||||
fn is_attachment_part(&self, part: &mail_parser::MessagePart, content_type: &mail_parser::ContentType) -> bool {
|
||||
fn is_attachment_part(
|
||||
&self,
|
||||
part: &mail_parser::MessagePart,
|
||||
content_type: &mail_parser::ContentType,
|
||||
) -> bool {
|
||||
// Check Content-Disposition header first
|
||||
if let Some(disposition) = part.get_content_disposition() {
|
||||
return disposition.c_type.to_lowercase() == "attachment";
|
||||
}
|
||||
|
||||
|
||||
// If no explicit disposition, check content type
|
||||
// Consider non-text types as potential attachments
|
||||
let main_type = content_type.c_type.split('/').next().unwrap_or("");
|
||||
match main_type {
|
||||
"text" => false, // Text parts are usually body content
|
||||
"text" => false, // Text parts are usually body content
|
||||
"multipart" => false, // Multipart containers are not attachments
|
||||
_ => true, // Images, applications, etc. are likely attachments
|
||||
_ => true, // Images, applications, etc. are likely attachments
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Generate a filename for an attachment
|
||||
fn get_attachment_filename(&self, part: &mail_parser::MessagePart, index: usize) -> String {
|
||||
// Try to get filename from Content-Disposition
|
||||
|
|
@ -762,7 +848,7 @@ impl ImapClient {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Try to get filename from Content-Type
|
||||
if let Some(content_type) = part.get_content_type() {
|
||||
// Find name in attributes vector
|
||||
|
|
@ -774,16 +860,16 @@ impl ImapClient {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Generate a default filename based on content type and index
|
||||
if let Some(content_type) = part.get_content_type() {
|
||||
let extension = self.get_extension_from_content_type(&content_type.c_type);
|
||||
format!("attachment_{}{}", index, extension)
|
||||
format!("attachment_{index}{extension}")
|
||||
} else {
|
||||
format!("attachment_{}.bin", index)
|
||||
format!("attachment_{index}.bin")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Get file extension from MIME content type
|
||||
fn get_extension_from_content_type(&self, content_type: &str) -> &'static str {
|
||||
match content_type {
|
||||
|
|
@ -805,22 +891,23 @@ impl ImapClient {
|
|||
/// Close the IMAP connection
|
||||
pub async fn close(self) -> Result<()> {
|
||||
if let Some(mut session) = self.session {
|
||||
session.logout().await
|
||||
.map_err(|e| ImapError::Operation(format!("Logout failed: {:?}", e)))?;
|
||||
session
|
||||
.logout()
|
||||
.await
|
||||
.map_err(|e| ImapError::Operation(format!("Logout failed: {e:?}")))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Apply message filters to determine if a message should be processed
|
||||
pub fn should_process_message(
|
||||
mail_doc: &MailDocument,
|
||||
filter: &MessageFilter,
|
||||
) -> bool {
|
||||
pub fn should_process_message(mail_doc: &MailDocument, filter: &MessageFilter) -> bool {
|
||||
// Check subject keywords
|
||||
if !filter.subject_keywords.is_empty() {
|
||||
let subject_lower = mail_doc.subject.to_lowercase();
|
||||
let has_subject_keyword = filter.subject_keywords.iter()
|
||||
let has_subject_keyword = filter
|
||||
.subject_keywords
|
||||
.iter()
|
||||
.any(|keyword| subject_lower.contains(&keyword.to_lowercase()));
|
||||
if !has_subject_keyword {
|
||||
return false;
|
||||
|
|
@ -829,12 +916,13 @@ pub fn should_process_message(
|
|||
|
||||
// Check sender keywords
|
||||
if !filter.sender_keywords.is_empty() {
|
||||
let has_sender_keyword = mail_doc.from.iter()
|
||||
.any(|from_addr| {
|
||||
let from_lower = from_addr.to_lowercase();
|
||||
filter.sender_keywords.iter()
|
||||
.any(|keyword| from_lower.contains(&keyword.to_lowercase()))
|
||||
});
|
||||
let has_sender_keyword = mail_doc.from.iter().any(|from_addr| {
|
||||
let from_lower = from_addr.to_lowercase();
|
||||
filter
|
||||
.sender_keywords
|
||||
.iter()
|
||||
.any(|keyword| from_lower.contains(&keyword.to_lowercase()))
|
||||
});
|
||||
if !has_sender_keyword {
|
||||
return false;
|
||||
}
|
||||
|
|
@ -842,12 +930,13 @@ pub fn should_process_message(
|
|||
|
||||
// Check recipient keywords
|
||||
if !filter.recipient_keywords.is_empty() {
|
||||
let has_recipient_keyword = mail_doc.to.iter()
|
||||
.any(|to_addr| {
|
||||
let to_lower = to_addr.to_lowercase();
|
||||
filter.recipient_keywords.iter()
|
||||
.any(|keyword| to_lower.contains(&keyword.to_lowercase()))
|
||||
});
|
||||
let has_recipient_keyword = mail_doc.to.iter().any(|to_addr| {
|
||||
let to_lower = to_addr.to_lowercase();
|
||||
filter
|
||||
.recipient_keywords
|
||||
.iter()
|
||||
.any(|keyword| to_lower.contains(&keyword.to_lowercase()))
|
||||
});
|
||||
if !has_recipient_keyword {
|
||||
return false;
|
||||
}
|
||||
|
|
@ -898,7 +987,7 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_rfc822_parsing() {
|
||||
let client = ImapClient {
|
||||
let _client = ImapClient {
|
||||
session: None,
|
||||
source: MailSource {
|
||||
name: "test".to_string(),
|
||||
|
|
@ -918,4 +1007,4 @@ mod tests {
|
|||
// This test needs to be updated to use actual message parsing
|
||||
// For now, we'll skip the detailed test since it requires a full email message
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,16 +14,16 @@
|
|||
//! The library uses well-defined CouchDB document schemas that are compatible
|
||||
//! with the Go implementation. See the `schemas` module for details.
|
||||
|
||||
pub mod schemas;
|
||||
pub mod config;
|
||||
pub mod cli;
|
||||
pub mod config;
|
||||
pub mod couch;
|
||||
pub mod imap;
|
||||
pub mod filters;
|
||||
pub mod imap;
|
||||
pub mod schemas;
|
||||
pub mod sync;
|
||||
|
||||
// Re-export main types for convenience
|
||||
pub use schemas::{MailDocument, SyncMetadata, AttachmentStub, generate_database_name};
|
||||
pub use config::{Config, MailSource, CommandLineArgs};
|
||||
pub use config::{CommandLineArgs, Config, MailSource};
|
||||
pub use couch::CouchClient;
|
||||
pub use imap::ImapClient;
|
||||
pub use imap::ImapClient;
|
||||
pub use schemas::{generate_database_name, AttachmentStub, MailDocument, SyncMetadata};
|
||||
|
|
|
|||
|
|
@ -1,11 +1,7 @@
|
|||
use anyhow::Result;
|
||||
use env_logger::Env;
|
||||
use log::{error, info};
|
||||
use mail2couch::{
|
||||
cli::parse_command_line,
|
||||
config::Config,
|
||||
sync::SyncCoordinator,
|
||||
};
|
||||
use mail2couch::{cli::parse_command_line, config::Config, sync::SyncCoordinator};
|
||||
use std::process;
|
||||
|
||||
#[tokio::main]
|
||||
|
|
@ -18,7 +14,7 @@ async fn main() {
|
|||
|
||||
// Run the main application
|
||||
if let Err(e) = run(args).await {
|
||||
error!("❌ Application failed: {}", e);
|
||||
error!("❌ Application failed: {e}");
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
|
|
@ -31,7 +27,7 @@ async fn run(args: mail2couch::config::CommandLineArgs) -> Result<()> {
|
|||
info!("Using configuration file: {}", config_path.display());
|
||||
|
||||
if let Some(max) = args.max_messages {
|
||||
info!("Maximum messages per mailbox: {}", max);
|
||||
info!("Maximum messages per mailbox: {max}");
|
||||
} else {
|
||||
info!("Maximum messages per mailbox: unlimited");
|
||||
}
|
||||
|
|
@ -67,7 +63,11 @@ fn print_config_summary(config: &mail2couch::config::Config) {
|
|||
info!(" Mail sources: {}", config.mail_sources.len());
|
||||
|
||||
for (i, source) in config.mail_sources.iter().enumerate() {
|
||||
let status = if source.enabled { "enabled" } else { "disabled" };
|
||||
let status = if source.enabled {
|
||||
"enabled"
|
||||
} else {
|
||||
"disabled"
|
||||
};
|
||||
info!(
|
||||
" {}: {} ({}) - {} ({})",
|
||||
i + 1,
|
||||
|
|
@ -88,8 +88,11 @@ fn print_config_summary(config: &mail2couch::config::Config) {
|
|||
info!(" Since: {:?}", source.message_filter.since);
|
||||
}
|
||||
if !source.message_filter.subject_keywords.is_empty() {
|
||||
info!(" Subject keywords: {:?}", source.message_filter.subject_keywords);
|
||||
info!(
|
||||
" Subject keywords: {:?}",
|
||||
source.message_filter.subject_keywords
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -121,6 +121,7 @@ pub struct SyncMetadata {
|
|||
|
||||
impl MailDocument {
|
||||
/// Create a new MailDocument with required fields
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn new(
|
||||
source_uid: String,
|
||||
mailbox: String,
|
||||
|
|
@ -134,7 +135,7 @@ impl MailDocument {
|
|||
) -> Self {
|
||||
let now = Utc::now();
|
||||
Self {
|
||||
id: None, // Will be set when storing to CouchDB
|
||||
id: None, // Will be set when storing to CouchDB
|
||||
rev: None, // Managed by CouchDB
|
||||
attachments: None,
|
||||
source_uid,
|
||||
|
|
@ -172,7 +173,7 @@ impl SyncMetadata {
|
|||
) -> Self {
|
||||
let now = Utc::now();
|
||||
Self {
|
||||
id: Some(format!("sync_metadata_{}", mailbox)),
|
||||
id: Some(format!("sync_metadata_{mailbox}")),
|
||||
rev: None, // Managed by CouchDB
|
||||
doc_type: "sync_metadata".to_string(),
|
||||
mailbox,
|
||||
|
|
@ -199,7 +200,15 @@ pub fn generate_database_name(account_name: &str, user_email: &str) -> String {
|
|||
.to_lowercase()
|
||||
.chars()
|
||||
.map(|c| {
|
||||
if c.is_ascii_alphanumeric() || c == '_' || c == '$' || c == '(' || c == ')' || c == '+' || c == '-' || c == '/' {
|
||||
if c.is_ascii_alphanumeric()
|
||||
|| c == '_'
|
||||
|| c == '$'
|
||||
|| c == '('
|
||||
|| c == ')'
|
||||
|| c == '+'
|
||||
|| c == '-'
|
||||
|| c == '/'
|
||||
{
|
||||
c
|
||||
} else {
|
||||
'_'
|
||||
|
|
@ -209,9 +218,9 @@ pub fn generate_database_name(account_name: &str, user_email: &str) -> String {
|
|||
|
||||
// Ensure starts with a letter
|
||||
if valid_name.is_empty() || !valid_name.chars().next().unwrap().is_ascii_lowercase() {
|
||||
valid_name = format!("m2c_mail_{}", valid_name);
|
||||
valid_name = format!("m2c_mail_{valid_name}");
|
||||
} else {
|
||||
valid_name = format!("m2c_{}", valid_name);
|
||||
valid_name = format!("m2c_{valid_name}");
|
||||
}
|
||||
|
||||
valid_name
|
||||
|
|
@ -223,8 +232,14 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_generate_database_name() {
|
||||
assert_eq!(generate_database_name("Personal Gmail", ""), "m2c_personal_gmail");
|
||||
assert_eq!(generate_database_name("", "user@example.com"), "m2c_user_example_com");
|
||||
assert_eq!(
|
||||
generate_database_name("Personal Gmail", ""),
|
||||
"m2c_personal_gmail"
|
||||
);
|
||||
assert_eq!(
|
||||
generate_database_name("", "user@example.com"),
|
||||
"m2c_user_example_com"
|
||||
);
|
||||
assert_eq!(generate_database_name("123work", ""), "m2c_mail_123work");
|
||||
}
|
||||
|
||||
|
|
@ -243,19 +258,14 @@ mod tests {
|
|||
);
|
||||
|
||||
assert_eq!(doc.generate_id(), "INBOX_123");
|
||||
|
||||
|
||||
doc.set_id();
|
||||
assert_eq!(doc.id, Some("INBOX_123".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sync_metadata_creation() {
|
||||
let metadata = SyncMetadata::new(
|
||||
"INBOX".to_string(),
|
||||
Utc::now(),
|
||||
456,
|
||||
100,
|
||||
);
|
||||
let metadata = SyncMetadata::new("INBOX".to_string(), Utc::now(), 456, 100);
|
||||
|
||||
assert_eq!(metadata.id, Some("sync_metadata_INBOX".to_string()));
|
||||
assert_eq!(metadata.doc_type, "sync_metadata");
|
||||
|
|
@ -263,4 +273,4 @@ mod tests {
|
|||
assert_eq!(metadata.last_message_uid, 456);
|
||||
assert_eq!(metadata.message_count, 100);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
270
rust/src/sync.rs
270
rust/src/sync.rs
|
|
@ -1,16 +1,16 @@
|
|||
//! Synchronization logic for mail2couch
|
||||
//!
|
||||
//!
|
||||
//! This module coordinates the synchronization process between IMAP servers and CouchDB,
|
||||
//! implementing incremental sync with metadata tracking.
|
||||
|
||||
use crate::config::{Config, MailSource, CommandLineArgs};
|
||||
use crate::config::{CommandLineArgs, Config, MailSource};
|
||||
use crate::couch::CouchClient;
|
||||
use crate::filters::{get_filter_summary, validate_folder_patterns};
|
||||
use crate::imap::{ImapClient, should_process_message};
|
||||
use crate::schemas::{SyncMetadata, generate_database_name};
|
||||
use crate::imap::{should_process_message, ImapClient};
|
||||
use crate::schemas::{generate_database_name, SyncMetadata};
|
||||
use anyhow::{anyhow, Result};
|
||||
use chrono::{DateTime, Utc};
|
||||
use log::{info, warn, error, debug};
|
||||
use log::{debug, error, info, warn};
|
||||
|
||||
/// Main synchronization coordinator
|
||||
pub struct SyncCoordinator {
|
||||
|
|
@ -46,7 +46,7 @@ impl SyncCoordinator {
|
|||
/// Create a new sync coordinator
|
||||
pub fn new(config: Config, args: CommandLineArgs) -> Result<Self> {
|
||||
let couch_client = CouchClient::new(&config.couch_db)?;
|
||||
|
||||
|
||||
Ok(SyncCoordinator {
|
||||
config,
|
||||
couch_client,
|
||||
|
|
@ -57,7 +57,9 @@ impl SyncCoordinator {
|
|||
/// Test connections to all services
|
||||
pub async fn test_connections(&self) -> Result<()> {
|
||||
info!("Testing CouchDB connection...");
|
||||
self.couch_client.test_connection().await
|
||||
self.couch_client
|
||||
.test_connection()
|
||||
.await
|
||||
.map_err(|e| anyhow!("CouchDB connection failed: {}", e))?;
|
||||
info!("✅ CouchDB connection successful");
|
||||
|
||||
|
|
@ -68,9 +70,10 @@ impl SyncCoordinator {
|
|||
}
|
||||
|
||||
info!("Testing IMAP connection to {}...", source.name);
|
||||
let imap_client = ImapClient::connect(source.clone()).await
|
||||
let imap_client = ImapClient::connect(source.clone())
|
||||
.await
|
||||
.map_err(|e| anyhow!("IMAP connection to {} failed: {}", source.name, e))?;
|
||||
|
||||
|
||||
imap_client.close().await?;
|
||||
info!("✅ IMAP connection to {} successful", source.name);
|
||||
}
|
||||
|
|
@ -95,9 +98,7 @@ impl SyncCoordinator {
|
|||
Ok(result) => {
|
||||
info!(
|
||||
"✅ Completed sync for {}: {} messages across {} mailboxes",
|
||||
result.source_name,
|
||||
result.total_messages,
|
||||
result.mailboxes_processed
|
||||
result.source_name, result.total_messages, result.mailboxes_processed
|
||||
);
|
||||
results.push(result);
|
||||
}
|
||||
|
|
@ -114,41 +115,52 @@ impl SyncCoordinator {
|
|||
/// Synchronize a single mail source
|
||||
async fn sync_source(&mut self, source: &MailSource) -> Result<SourceSyncResult> {
|
||||
let start_time = Utc::now();
|
||||
|
||||
|
||||
// Generate database name
|
||||
let db_name = generate_database_name(&source.name, &source.user);
|
||||
info!("Using database: {}", db_name);
|
||||
info!("Using database: {db_name}");
|
||||
|
||||
// Create database if it doesn't exist (skip in dry-run mode)
|
||||
if !self.args.dry_run {
|
||||
self.couch_client.create_database(&db_name).await?;
|
||||
} else {
|
||||
info!("🔍 DRY-RUN: Would create database {}", db_name);
|
||||
info!("🔍 DRY-RUN: Would create database {db_name}");
|
||||
}
|
||||
|
||||
// Connect to IMAP server
|
||||
let mut imap_client = ImapClient::connect(source.clone()).await?;
|
||||
|
||||
// Use IMAP LIST with patterns for server-side filtering
|
||||
let filtered_mailboxes = imap_client.list_filtered_mailboxes(&source.folder_filter).await?;
|
||||
info!("Found {} matching mailboxes after server-side filtering", filtered_mailboxes.len());
|
||||
let filtered_mailboxes = imap_client
|
||||
.list_filtered_mailboxes(&source.folder_filter)
|
||||
.await?;
|
||||
info!(
|
||||
"Found {} matching mailboxes after server-side filtering",
|
||||
filtered_mailboxes.len()
|
||||
);
|
||||
|
||||
// For validation and summary, we still need the full list
|
||||
let all_mailboxes = if !source.folder_filter.include.is_empty() || !source.folder_filter.exclude.is_empty() {
|
||||
let all_mailboxes = if !source.folder_filter.include.is_empty()
|
||||
|| !source.folder_filter.exclude.is_empty()
|
||||
{
|
||||
// Only fetch all mailboxes if we have filters (for logging/validation)
|
||||
imap_client.list_mailboxes().await.unwrap_or_else(|_| Vec::new())
|
||||
imap_client
|
||||
.list_mailboxes()
|
||||
.await
|
||||
.unwrap_or_else(|_| Vec::new())
|
||||
} else {
|
||||
filtered_mailboxes.clone()
|
||||
};
|
||||
|
||||
if !all_mailboxes.is_empty() {
|
||||
let filter_summary = get_filter_summary(&all_mailboxes, &filtered_mailboxes, &source.folder_filter);
|
||||
info!("{}", filter_summary);
|
||||
let filter_summary =
|
||||
get_filter_summary(&all_mailboxes, &filtered_mailboxes, &source.folder_filter);
|
||||
info!("{filter_summary}");
|
||||
|
||||
// Validate folder patterns and show warnings
|
||||
let warnings = validate_folder_patterns(&source.folder_filter, &all_mailboxes);
|
||||
for warning in warnings {
|
||||
warn!("{}", warning);
|
||||
warn!("{warning}");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -157,9 +169,12 @@ impl SyncCoordinator {
|
|||
let mut total_messages = 0;
|
||||
|
||||
for mailbox in &filtered_mailboxes {
|
||||
info!("Syncing mailbox: {}", mailbox);
|
||||
|
||||
match self.sync_mailbox(&mut imap_client, &db_name, mailbox, source).await {
|
||||
info!("Syncing mailbox: {mailbox}");
|
||||
|
||||
match self
|
||||
.sync_mailbox(&mut imap_client, &db_name, mailbox, source)
|
||||
.await
|
||||
{
|
||||
Ok(result) => {
|
||||
if result.messages_deleted > 0 {
|
||||
info!(
|
||||
|
|
@ -183,7 +198,7 @@ impl SyncCoordinator {
|
|||
mailbox_results.push(result);
|
||||
}
|
||||
Err(e) => {
|
||||
error!(" ❌ Failed to sync mailbox {}: {}", mailbox, e);
|
||||
error!(" ❌ Failed to sync mailbox {mailbox}: {e}");
|
||||
// Continue with other mailboxes
|
||||
}
|
||||
}
|
||||
|
|
@ -214,35 +229,45 @@ impl SyncCoordinator {
|
|||
|
||||
// Select the mailbox
|
||||
let mailbox_info = imap_client.select_mailbox(mailbox).await?;
|
||||
debug!("Selected mailbox {}: {} messages", mailbox, mailbox_info.exists);
|
||||
debug!(
|
||||
"Selected mailbox {}: {} messages",
|
||||
mailbox, mailbox_info.exists
|
||||
);
|
||||
|
||||
// Get last sync metadata (skip in dry-run mode)
|
||||
let since_date = if !self.args.dry_run {
|
||||
match self.couch_client.get_sync_metadata(db_name, mailbox).await {
|
||||
Ok(metadata) => {
|
||||
info!(" Found sync metadata, last sync: {}", metadata.last_sync_time);
|
||||
info!(
|
||||
" Found sync metadata, last sync: {}",
|
||||
metadata.last_sync_time
|
||||
);
|
||||
Some(metadata.last_sync_time)
|
||||
}
|
||||
Err(_) => {
|
||||
info!(" No sync metadata found, performing full sync");
|
||||
// Parse since date from message filter if provided
|
||||
source.message_filter.since.as_ref()
|
||||
.and_then(|since_str| {
|
||||
DateTime::parse_from_str(&format!("{} 00:00:00 +0000", since_str), "%Y-%m-%d %H:%M:%S %z")
|
||||
.map(|dt| dt.with_timezone(&Utc))
|
||||
.ok()
|
||||
})
|
||||
source.message_filter.since.as_ref().and_then(|since_str| {
|
||||
DateTime::parse_from_str(
|
||||
&format!("{since_str} 00:00:00 +0000"),
|
||||
"%Y-%m-%d %H:%M:%S %z",
|
||||
)
|
||||
.map(|dt| dt.with_timezone(&Utc))
|
||||
.ok()
|
||||
})
|
||||
}
|
||||
}
|
||||
} else {
|
||||
info!(" 🔍 DRY-RUN: Would check for sync metadata");
|
||||
// In dry-run mode, use config since date if available
|
||||
source.message_filter.since.as_ref()
|
||||
.and_then(|since_str| {
|
||||
DateTime::parse_from_str(&format!("{} 00:00:00 +0000", since_str), "%Y-%m-%d %H:%M:%S %z")
|
||||
.map(|dt| dt.with_timezone(&Utc))
|
||||
.ok()
|
||||
})
|
||||
source.message_filter.since.as_ref().and_then(|since_str| {
|
||||
DateTime::parse_from_str(
|
||||
&format!("{since_str} 00:00:00 +0000"),
|
||||
"%Y-%m-%d %H:%M:%S %z",
|
||||
)
|
||||
.map(|dt| dt.with_timezone(&Utc))
|
||||
.ok()
|
||||
})
|
||||
};
|
||||
|
||||
// Search for messages using server-side IMAP SEARCH with keyword filtering when possible
|
||||
|
|
@ -258,32 +283,41 @@ impl SyncCoordinator {
|
|||
} else {
|
||||
Some(source.message_filter.sender_keywords.as_slice())
|
||||
};
|
||||
|
||||
|
||||
info!(" Using IMAP SEARCH with keyword filters");
|
||||
imap_client.search_messages_advanced(
|
||||
since_date.as_ref(),
|
||||
None, // before_date
|
||||
subject_keywords,
|
||||
from_keywords,
|
||||
).await?
|
||||
imap_client
|
||||
.search_messages_advanced(
|
||||
since_date.as_ref(),
|
||||
None, // before_date
|
||||
subject_keywords,
|
||||
from_keywords,
|
||||
)
|
||||
.await?
|
||||
} else {
|
||||
// Use simple date-based search
|
||||
imap_client.search_messages(since_date.as_ref()).await?
|
||||
};
|
||||
info!(" Found {} messages matching search criteria", message_uids.len());
|
||||
info!(
|
||||
" Found {} messages matching search criteria",
|
||||
message_uids.len()
|
||||
);
|
||||
|
||||
// Handle sync mode - check for deleted messages (skip in dry-run mode)
|
||||
let mut messages_deleted = 0;
|
||||
if source.mode == "sync" {
|
||||
if !self.args.dry_run {
|
||||
messages_deleted = self.handle_deleted_messages(db_name, mailbox, &message_uids).await
|
||||
messages_deleted = self
|
||||
.handle_deleted_messages(db_name, mailbox, &message_uids)
|
||||
.await
|
||||
.unwrap_or_else(|e| {
|
||||
warn!(" Failed to handle deleted messages: {}", e);
|
||||
warn!(" Failed to handle deleted messages: {e}");
|
||||
0
|
||||
});
|
||||
|
||||
|
||||
if messages_deleted > 0 {
|
||||
info!(" 🗑️ Deleted {} messages that no longer exist on server", messages_deleted);
|
||||
info!(
|
||||
" 🗑️ Deleted {messages_deleted} messages that no longer exist on server"
|
||||
);
|
||||
}
|
||||
} else {
|
||||
info!(" 🔍 DRY-RUN: Would check for deleted messages in sync mode");
|
||||
|
|
@ -305,7 +339,7 @@ impl SyncCoordinator {
|
|||
// Apply max message limit if specified
|
||||
let uids_to_process = if let Some(max) = self.args.max_messages {
|
||||
if message_uids.len() > max as usize {
|
||||
info!(" Limiting to {} messages due to --max-messages flag", max);
|
||||
info!(" Limiting to {max} messages due to --max-messages flag");
|
||||
&message_uids[..max as usize]
|
||||
} else {
|
||||
&message_uids
|
||||
|
|
@ -315,8 +349,10 @@ impl SyncCoordinator {
|
|||
};
|
||||
|
||||
// Fetch and process messages
|
||||
let messages = imap_client.fetch_messages(uids_to_process, self.args.max_messages, mailbox).await?;
|
||||
|
||||
let messages = imap_client
|
||||
.fetch_messages(uids_to_process, self.args.max_messages, mailbox)
|
||||
.await?;
|
||||
|
||||
let mut messages_stored = 0;
|
||||
let mut messages_skipped = 0;
|
||||
let mut last_uid = None;
|
||||
|
|
@ -333,50 +369,63 @@ impl SyncCoordinator {
|
|||
|
||||
// Store the message document first (skip in dry-run mode)
|
||||
if !self.args.dry_run {
|
||||
match self.couch_client.store_mail_document(db_name, mail_doc).await {
|
||||
match self
|
||||
.couch_client
|
||||
.store_mail_document(db_name, mail_doc)
|
||||
.await
|
||||
{
|
||||
Ok(doc_id) => {
|
||||
messages_stored += 1;
|
||||
|
||||
|
||||
// Store attachments if any exist
|
||||
if !attachments.is_empty() {
|
||||
for (filename, content_type, data) in attachments {
|
||||
match self.couch_client.store_attachment(
|
||||
db_name,
|
||||
&doc_id,
|
||||
&filename,
|
||||
&content_type,
|
||||
&data,
|
||||
).await {
|
||||
match self
|
||||
.couch_client
|
||||
.store_attachment(
|
||||
db_name,
|
||||
&doc_id,
|
||||
&filename,
|
||||
&content_type,
|
||||
&data,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(_) => {
|
||||
debug!(" Stored attachment: {}", filename);
|
||||
debug!(" Stored attachment: {filename}");
|
||||
}
|
||||
Err(e) => {
|
||||
warn!(" Failed to store attachment {}: {}", filename, e);
|
||||
warn!(" Failed to store attachment {filename}: {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Parse UID from source_uid
|
||||
if let Ok(uid) = uid_str.parse::<u32>() {
|
||||
last_uid = Some(last_uid.map_or(uid, |prev: u32| prev.max(uid)));
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
warn!(" Failed to store message {}: {}", uid_str, e);
|
||||
warn!(" Failed to store message {uid_str}: {e}");
|
||||
messages_skipped += 1;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// In dry-run mode, simulate successful storage
|
||||
messages_stored += 1;
|
||||
debug!(" 🔍 DRY-RUN: Would store message {} (Subject: {})",
|
||||
uid_str, mail_doc.subject);
|
||||
|
||||
debug!(
|
||||
" 🔍 DRY-RUN: Would store message {} (Subject: {})",
|
||||
uid_str, mail_doc.subject
|
||||
);
|
||||
|
||||
if !attachments.is_empty() {
|
||||
debug!(" 🔍 DRY-RUN: Would store {} attachments", attachments.len());
|
||||
debug!(
|
||||
" 🔍 DRY-RUN: Would store {} attachments",
|
||||
attachments.len()
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
// Parse UID from source_uid
|
||||
if let Ok(uid) = uid_str.parse::<u32>() {
|
||||
last_uid = Some(last_uid.map_or(uid, |prev: u32| prev.max(uid)));
|
||||
|
|
@ -387,18 +436,20 @@ impl SyncCoordinator {
|
|||
// Update sync metadata (skip in dry-run mode)
|
||||
if let Some(uid) = last_uid {
|
||||
if !self.args.dry_run {
|
||||
let sync_metadata = SyncMetadata::new(
|
||||
mailbox.to_string(),
|
||||
start_time,
|
||||
uid,
|
||||
messages_stored,
|
||||
);
|
||||
let sync_metadata =
|
||||
SyncMetadata::new(mailbox.to_string(), start_time, uid, messages_stored);
|
||||
|
||||
if let Err(e) = self.couch_client.store_sync_metadata(db_name, &sync_metadata).await {
|
||||
warn!(" Failed to store sync metadata: {}", e);
|
||||
if let Err(e) = self
|
||||
.couch_client
|
||||
.store_sync_metadata(db_name, &sync_metadata)
|
||||
.await
|
||||
{
|
||||
warn!(" Failed to store sync metadata: {e}");
|
||||
}
|
||||
} else {
|
||||
info!(" 🔍 DRY-RUN: Would update sync metadata (last UID: {}, {} messages)", uid, messages_stored);
|
||||
info!(
|
||||
" 🔍 DRY-RUN: Would update sync metadata (last UID: {uid}, {messages_stored} messages)"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -424,27 +475,28 @@ impl SyncCoordinator {
|
|||
) -> Result<u32> {
|
||||
// Get all stored message UIDs for this mailbox from CouchDB
|
||||
let stored_uids = self.get_stored_message_uids(db_name, mailbox).await?;
|
||||
|
||||
|
||||
if stored_uids.is_empty() {
|
||||
return Ok(0); // No stored messages to delete
|
||||
}
|
||||
|
||||
// Find UIDs that exist in CouchDB but not on the server
|
||||
let server_uid_set: std::collections::HashSet<u32> = current_server_uids.iter().cloned().collect();
|
||||
let server_uid_set: std::collections::HashSet<u32> =
|
||||
current_server_uids.iter().cloned().collect();
|
||||
let mut deleted_count = 0;
|
||||
|
||||
for stored_uid in stored_uids {
|
||||
if !server_uid_set.contains(&stored_uid) {
|
||||
// This message was deleted from the server, remove it from CouchDB
|
||||
let doc_id = format!("{}_{}", mailbox, stored_uid);
|
||||
|
||||
let doc_id = format!("{mailbox}_{stored_uid}");
|
||||
|
||||
match self.couch_client.delete_document(db_name, &doc_id).await {
|
||||
Ok(_) => {
|
||||
debug!(" Deleted document: {}", doc_id);
|
||||
debug!(" Deleted document: {doc_id}");
|
||||
deleted_count += 1;
|
||||
}
|
||||
Err(e) => {
|
||||
warn!(" Failed to delete document {}: {}", doc_id, e);
|
||||
warn!(" Failed to delete document {doc_id}: {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -499,18 +551,16 @@ impl SyncCoordinator {
|
|||
info!("{}", "=".repeat(50));
|
||||
if self.args.dry_run {
|
||||
info!(
|
||||
"📊 DRY-RUN Total: {} sources, {} mailboxes, {} messages found",
|
||||
total_sources, total_mailboxes, total_messages
|
||||
"📊 DRY-RUN Total: {total_sources} sources, {total_mailboxes} mailboxes, {total_messages} messages found"
|
||||
);
|
||||
} else {
|
||||
info!(
|
||||
"📊 Total: {} sources, {} mailboxes, {} messages",
|
||||
total_sources, total_mailboxes, total_messages
|
||||
"📊 Total: {total_sources} sources, {total_mailboxes} mailboxes, {total_messages} messages"
|
||||
);
|
||||
}
|
||||
|
||||
if let Some(max) = self.args.max_messages {
|
||||
info!("⚠️ Message limit was applied: {} per mailbox", max);
|
||||
info!("⚠️ Message limit was applied: {max} per mailbox");
|
||||
}
|
||||
|
||||
if self.args.dry_run {
|
||||
|
|
@ -531,23 +581,21 @@ mod tests {
|
|||
user: "admin".to_string(),
|
||||
password: "password".to_string(),
|
||||
},
|
||||
mail_sources: vec![
|
||||
MailSource {
|
||||
name: "Test Account".to_string(),
|
||||
enabled: true,
|
||||
protocol: "imap".to_string(),
|
||||
host: "localhost".to_string(),
|
||||
port: 3143,
|
||||
user: "testuser".to_string(),
|
||||
password: "testpass".to_string(),
|
||||
mode: "archive".to_string(),
|
||||
folder_filter: FolderFilter {
|
||||
include: vec!["*".to_string()],
|
||||
exclude: vec!["Trash".to_string()],
|
||||
},
|
||||
message_filter: MessageFilter::default(),
|
||||
}
|
||||
],
|
||||
mail_sources: vec![MailSource {
|
||||
name: "Test Account".to_string(),
|
||||
enabled: true,
|
||||
protocol: "imap".to_string(),
|
||||
host: "localhost".to_string(),
|
||||
port: 3143,
|
||||
user: "testuser".to_string(),
|
||||
password: "testpass".to_string(),
|
||||
mode: "archive".to_string(),
|
||||
folder_filter: FolderFilter {
|
||||
include: vec!["*".to_string()],
|
||||
exclude: vec!["Trash".to_string()],
|
||||
},
|
||||
message_filter: MessageFilter::default(),
|
||||
}],
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -568,4 +616,4 @@ mod tests {
|
|||
}
|
||||
|
||||
// Additional integration tests would require running services
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue