2025-11-19 02:14:37 +08:00
|
|
|
//
|
2026-03-10 01:32:57 +08:00
|
|
|
// Copyright (c) 2025-2026 rustmailer.com (https://rustmailer.com)
|
2025-11-19 02:14:37 +08:00
|
|
|
//
|
|
|
|
|
// This file is part of the Bichon Email Archiving Project
|
|
|
|
|
//
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
use crate::modules::account::migration::{AccountModel, AccountType};
|
|
|
|
|
use crate::modules::cache::imap::mailbox::{Attribute, AttributeEnum, MailBox};
|
|
|
|
|
use crate::modules::error::code::ErrorCode;
|
2026-01-28 20:41:17 +08:00
|
|
|
use crate::modules::error::BichonResult;
|
|
|
|
|
use crate::modules::imap::executor::ImapExecutor;
|
|
|
|
|
use crate::modules::imap::session::SessionStream;
|
2025-11-19 02:14:37 +08:00
|
|
|
use crate::modules::utils::create_hash;
|
|
|
|
|
use crate::raise_error;
|
|
|
|
|
use async_imap::types::Name;
|
2026-01-28 20:41:17 +08:00
|
|
|
use async_imap::Session;
|
2025-11-19 02:14:37 +08:00
|
|
|
|
|
|
|
|
pub async fn get_account_mailboxes(account_id: u64, remote: bool) -> BichonResult<Vec<MailBox>> {
|
2025-11-24 22:34:40 +08:00
|
|
|
let account = AccountModel::check_account_exists(account_id).await?;
|
2025-11-19 02:14:37 +08:00
|
|
|
if remote {
|
|
|
|
|
if matches!(account.account_type, AccountType::IMAP) {
|
|
|
|
|
request_imap_all_mailbox_list(account_id).await
|
|
|
|
|
} else {
|
|
|
|
|
return Err(raise_error!(
|
|
|
|
|
"The 'remote' option can only be used with IMAP accounts.".into(),
|
|
|
|
|
ErrorCode::InvalidParameter
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
MailBox::list_all(account_id).await
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn request_imap_all_mailbox_list(account_id: u64) -> BichonResult<Vec<MailBox>> {
|
2026-01-28 20:41:17 +08:00
|
|
|
let mut session = ImapExecutor::create_connection(account_id).await?;
|
|
|
|
|
let names = ImapExecutor::list_all_mailboxes(&mut session).await?;
|
|
|
|
|
let result = convert_names_to_mailboxes(account_id, &mut session, names.iter()).await?;
|
|
|
|
|
session.logout().await.ok();
|
|
|
|
|
Ok(result)
|
2025-11-19 02:14:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn contains_no_select(attributes: &[Attribute]) -> bool {
|
|
|
|
|
attributes
|
|
|
|
|
.iter()
|
|
|
|
|
.any(|attr| attr.attr == AttributeEnum::NoSelect)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn convert_names_to_mailboxes(
|
|
|
|
|
account_id: u64,
|
2026-01-28 20:41:17 +08:00
|
|
|
session: &mut Session<Box<dyn SessionStream>>,
|
2025-11-19 02:14:37 +08:00
|
|
|
names: impl IntoIterator<Item = &Name>,
|
|
|
|
|
) -> BichonResult<Vec<MailBox>> {
|
2026-01-28 20:41:17 +08:00
|
|
|
let mut mailboxes = Vec::new();
|
2025-11-19 02:14:37 +08:00
|
|
|
|
2026-01-28 20:41:17 +08:00
|
|
|
for name in names {
|
2025-11-19 02:14:37 +08:00
|
|
|
let mailbox_name = name.name().to_string();
|
|
|
|
|
let mut mailbox: MailBox = name.into();
|
|
|
|
|
|
|
|
|
|
if contains_no_select(&mailbox.attributes) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2026-01-28 20:41:17 +08:00
|
|
|
|
2025-11-19 02:14:37 +08:00
|
|
|
mailbox.account_id = account_id;
|
|
|
|
|
mailbox.id = create_hash(account_id, &mailbox.name);
|
2026-01-28 20:41:17 +08:00
|
|
|
let mx = session
|
|
|
|
|
.examine(mailbox_name.as_str())
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|e| raise_error!(format!("{:#?}", e), ErrorCode::ImapCommandFailed))?;
|
|
|
|
|
mailbox.exists = mx.exists;
|
|
|
|
|
mailbox.unseen = mx.unseen;
|
|
|
|
|
mailbox.uid_next = mx.uid_next;
|
|
|
|
|
mailbox.uid_validity = mx.uid_validity;
|
2025-11-19 02:14:37 +08:00
|
|
|
|
2026-01-28 20:41:17 +08:00
|
|
|
mailboxes.push(mailbox);
|
2025-11-19 02:14:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(mailboxes)
|
|
|
|
|
}
|