Skip to content

Commit ebeacbe

Browse files
committed
Imap::new() accepting login params
1 parent ce7cdf2 commit ebeacbe

File tree

4 files changed

+34
-46
lines changed

4 files changed

+34
-46
lines changed

src/configure.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -530,15 +530,11 @@ async fn configure(ctx: &Context, param: &EnteredLoginParam) -> Result<Option<&'
530530
let transport_id = 0;
531531
let (_s, r) = async_channel::bounded(1);
532532
let mut imap = Imap::new(
533+
ctx,
533534
transport_id,
534-
configured_param.imap.clone(),
535-
configured_param.imap_password.clone(),
536-
proxy_config,
537-
&configured_param.addr,
538-
strict_tls,
539-
configured_param.oauth2,
535+
configured_param.clone(),
540536
r,
541-
);
537+
).await?;
542538
let configuring = true;
543539
let mut imap_session = match imap.connect(ctx, configuring).await {
544540
Ok(session) => session,

src/imap.rs

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -248,20 +248,19 @@ impl<T: Iterator<Item = (i64, u32, String)>> Iterator for UidGrouper<T> {
248248

249249
impl Imap {
250250
/// Creates new disconnected IMAP client using the specific login parameters.
251-
///
252-
/// `addr` is used to renew token if OAuth2 authentication is used.
253-
#[expect(clippy::too_many_arguments)]
254-
pub fn new(
251+
pub async fn new(
252+
context: &Context,
255253
transport_id: u32,
256-
lp: Vec<ConfiguredServerLoginParam>,
257-
password: String,
258-
proxy_config: Option<ProxyConfig>,
259-
addr: &str,
260-
strict_tls: bool,
261-
oauth2: bool,
254+
param: ConfiguredLoginParam,
262255
idle_interrupt_receiver: Receiver<()>,
263-
) -> Self {
264-
Imap {
256+
) -> Result<Self> {
257+
let lp = param.imap.clone();
258+
let password = param.imap_password.clone();
259+
let proxy_config = ProxyConfig::load(context).await?;
260+
let addr = &param.addr;
261+
let strict_tls = param.strict_tls(proxy_config.is_some());
262+
let oauth2 = param.oauth2;
263+
Ok( Imap {
265264
transport_id,
266265
idle_interrupt_receiver,
267266
addr: addr.to_string(),
@@ -276,7 +275,7 @@ impl Imap {
276275
conn_backoff_ms: 0,
277276
// 1 connection per minute + a burst of 2.
278277
ratelimit: Ratelimit::new(Duration::new(120, 0), 2.0),
279-
}
278+
})
280279
}
281280

282281
/// Creates new disconnected IMAP client using configured parameters.
@@ -287,18 +286,7 @@ impl Imap {
287286
let (transport_id, param) = ConfiguredLoginParam::load(context)
288287
.await?
289288
.context("Not configured")?;
290-
let proxy_config = ProxyConfig::load(context).await?;
291-
let strict_tls = param.strict_tls(proxy_config.is_some());
292-
let imap = Self::new(
293-
transport_id,
294-
param.imap.clone(),
295-
param.imap_password.clone(),
296-
proxy_config,
297-
&param.addr,
298-
strict_tls,
299-
param.oauth2,
300-
idle_interrupt_receiver,
301-
);
289+
let imap = Self::new(context, transport_id, param, idle_interrupt_receiver).await?;
302290
Ok(imap)
303291
}
304292

src/scheduler.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use crate::smtp::{Smtp, send_smtp_messages};
2727
use crate::sql;
2828
use crate::stats::maybe_send_stats;
2929
use crate::tools::{self, duration_to_str, maybe_add_time_based_warnings, time, time_elapsed};
30+
use crate::transport::ConfiguredLoginParam;
3031
use crate::{constants, stats};
3132

3233
pub(crate) mod connectivity;
@@ -862,7 +863,11 @@ impl Scheduler {
862863
let mut oboxes = Vec::new();
863864
let mut start_recvs = Vec::new();
864865

865-
let (conn_state, inbox_handlers) = ImapConnectionState::new(ctx).await?;
866+
let (transport_id, configured_login_param) = ConfiguredLoginParam::load(ctx)
867+
.await?
868+
.context("Not configured")?;
869+
let (conn_state, inbox_handlers) =
870+
ImapConnectionState::new(ctx, transport_id, configured_login_param.clone()).await?;
866871
let (inbox_start_send, inbox_start_recv) = oneshot::channel();
867872
let handle = {
868873
let ctx = ctx.clone();
@@ -876,7 +881,7 @@ impl Scheduler {
876881
start_recvs.push(inbox_start_recv);
877882

878883
if ctx.should_watch_mvbox().await? {
879-
let (conn_state, handlers) = ImapConnectionState::new(ctx).await?;
884+
let (conn_state, handlers) = ImapConnectionState::new(ctx, transport_id, configured_login_param).await?;
880885
let (start_send, start_recv) = oneshot::channel();
881886
let ctx = ctx.clone();
882887
let meaning = FolderMeaning::Mvbox;
@@ -1097,12 +1102,17 @@ pub(crate) struct ImapConnectionState {
10971102

10981103
impl ImapConnectionState {
10991104
/// Construct a new connection.
1100-
async fn new(context: &Context) -> Result<(Self, ImapConnectionHandlers)> {
1105+
async fn new(
1106+
context: &Context,
1107+
transport_id: u32,
1108+
login_param: ConfiguredLoginParam,
1109+
) -> Result<(Self, ImapConnectionHandlers)> {
11011110
let stop_token = CancellationToken::new();
11021111
let (idle_interrupt_sender, idle_interrupt_receiver) = channel::bounded(1);
11031112

11041113
let handlers = ImapConnectionHandlers {
1105-
connection: Imap::new_configured(context, idle_interrupt_receiver).await?,
1114+
connection: Imap::new(context, transport_id, login_param, idle_interrupt_receiver)
1115+
.await?,
11061116
stop_token: stop_token.clone(),
11071117
};
11081118

src/transport.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -265,20 +265,14 @@ impl ConfiguredLoginParam {
265265
}
266266

267267
/// Loads configured login parameters for all transports.
268-
pub(crate) async fn load_all(context: &Context) -> Result<Vec<(u32, ConfiguredLoginParam)>> {
268+
pub(crate) async fn load_all(context: &Context) -> Result<Vec<(u32, Self)>> {
269269
context
270270
.sql
271-
.query_map("SELECT id, configured_param FROM transports", (), |row| {
271+
.query_map_vec("SELECT id, configured_param FROM transports", (), |row| {
272272
let id: u32 = row.get(0)?;
273273
let json: String = row.get(1)?;
274-
Ok((id, json))
275-
}, |rows| {
276-
let mut res = Vec::new();
277-
for row in rows {
278-
let (id, json) = row?;
279-
res.push((id, Self::from_json(&json)?));
280-
}
281-
Ok(res)
274+
let param = Self::from_json(&json)?;
275+
Ok((id, param))
282276
})
283277
.await
284278
}

0 commit comments

Comments
 (0)