Skip to content

Commit f774034

Browse files
author
Alexander Krotov
committed
Replace DC_LP_..._SOCKET_FLAGS with DC_SOCKET_...
This change moves socket security settings flom server_flags to separate configuration variables for IMAP and SMTP, similar to what Delta Chat Desktop already uses. This allows to completely separate IMAP and SMTP server configuration.
1 parent a398d71 commit f774034

File tree

12 files changed

+166
-170
lines changed

12 files changed

+166
-170
lines changed

deltachat-ffi/deltachat.h

+27-38
Original file line numberDiff line numberDiff line change
@@ -4169,86 +4169,75 @@ int64_t dc_lot_get_timestamp (const dc_lot_t* lot);
41694169

41704170

41714171
/**
4172-
* @}
4173-
*/
4174-
4175-
4176-
/**
4177-
* @defgroup DC_LP DC_LP
4172+
* @defgroup DC_SOCKET DC_SOCKET
41784173
*
4179-
* Flags for configuring IMAP and SMTP servers.
4180-
* These flags are optional
4181-
* and may be set together with the username, password etc.
4182-
* via dc_set_config() using the key "server_flags".
4174+
* These constants configure socket security.
41834175
*
4184-
* @addtogroup DC_LP
4176+
* @addtogroup DC_SOCKET
41854177
* @{
41864178
*/
41874179

4188-
41894180
/**
4190-
* Force OAuth2 authorization. This flag does not skip automatic configuration.
4191-
* Before calling dc_configure() with DC_LP_AUTH_OAUTH2 set,
4192-
* the user has to confirm access at the URL returned by dc_get_oauth2_url().
4181+
* @}
41934182
*/
4194-
#define DC_LP_AUTH_OAUTH2 0x2
4195-
41964183

41974184
/**
4198-
* Force NORMAL authorization, this is the default.
4199-
* If this flag is set, automatic configuration is skipped.
4185+
* Connect via SSL/TLS.
42004186
*/
4201-
#define DC_LP_AUTH_NORMAL 0x4
4187+
#define DC_SOCKET_SSL 1
42024188

42034189

42044190
/**
4205-
* Connect to IMAP via STARTTLS.
4191+
* Connect via STARTTLS.
42064192
* If this flag is set, automatic configuration is skipped.
42074193
*/
4208-
#define DC_LP_IMAP_SOCKET_STARTTLS 0x100
4194+
#define DC_SOCKET_STARTTLS 2
42094195

42104196

42114197
/**
4212-
* Connect to IMAP via SSL.
4213-
* If this flag is set, automatic configuration is skipped.
4198+
* Connect unencrypted, this should not be used.
42144199
*/
4215-
#define DC_LP_IMAP_SOCKET_SSL 0x200
4200+
#define DC_SOCKET_PLAIN 3
42164201

42174202

42184203
/**
4219-
* Connect to IMAP unencrypted, this should not be used.
4220-
* If this flag is set, automatic configuration is skipped.
4204+
* @}
42214205
*/
4222-
#define DC_LP_IMAP_SOCKET_PLAIN 0x400
42234206

42244207

42254208
/**
4226-
* Connect to SMTP via STARTTLS.
4227-
* If this flag is set, automatic configuration is skipped.
4209+
* @defgroup DC_LP DC_LP
4210+
*
4211+
* Flags for configuring IMAP and SMTP servers.
4212+
* These flags are optional
4213+
* and may be set together with the username, password etc.
4214+
* via dc_set_config() using the key "server_flags".
4215+
*
4216+
* @addtogroup DC_LP
4217+
* @{
42284218
*/
4229-
#define DC_LP_SMTP_SOCKET_STARTTLS 0x10000
42304219

42314220

42324221
/**
4233-
* Connect to SMTP via SSL.
4234-
* If this flag is set, automatic configuration is skipped.
4222+
* Force OAuth2 authorization. This flag does not skip automatic configuration.
4223+
* Before calling dc_configure() with DC_LP_AUTH_OAUTH2 set,
4224+
* the user has to confirm access at the URL returned by dc_get_oauth2_url().
42354225
*/
4236-
#define DC_LP_SMTP_SOCKET_SSL 0x20000
4226+
#define DC_LP_AUTH_OAUTH2 0x2
42374227

42384228

42394229
/**
4240-
* Connect to SMTP unencrypted, this should not be used.
4230+
* Force NORMAL authorization, this is the default.
42414231
* If this flag is set, automatic configuration is skipped.
42424232
*/
4243-
#define DC_LP_SMTP_SOCKET_PLAIN 0x40000 ///<
4233+
#define DC_LP_AUTH_NORMAL 0x4
4234+
42444235

42454236
/**
42464237
* @}
42474238
*/
42484239

42494240
#define DC_LP_AUTH_FLAGS (DC_LP_AUTH_OAUTH2|DC_LP_AUTH_NORMAL) // if none of these flags are set, the default is chosen
4250-
#define DC_LP_IMAP_SOCKET_FLAGS (DC_LP_IMAP_SOCKET_STARTTLS|DC_LP_IMAP_SOCKET_SSL|DC_LP_IMAP_SOCKET_PLAIN) // if none of these flags are set, the default is chosen
4251-
#define DC_LP_SMTP_SOCKET_FLAGS (DC_LP_SMTP_SOCKET_STARTTLS|DC_LP_SMTP_SOCKET_SSL|DC_LP_SMTP_SOCKET_PLAIN) // if none of these flags are set, the default is chosen
42524241

42534242
/**
42544243
* @defgroup DC_CERTCK DC_CERTCK

src/config.rs

+2
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ pub enum Config {
2424
MailUser,
2525
MailPw,
2626
MailPort,
27+
MailSecurity,
2728
ImapCertificateChecks,
2829
SendServer,
2930
SendUser,
3031
SendPw,
3132
SendPort,
33+
SendSecurity,
3234
SmtpCertificateChecks,
3335
ServerFlags,
3436

src/configure/auto_mozilla.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
//! Documentation: https://developer.mozilla.org/en-US/docs/Mozilla/Thunderbird/Autoconfiguration */
44
use quick_xml::events::{BytesEnd, BytesStart, BytesText};
55

6-
use crate::constants::*;
76
use crate::context::Context;
87
use crate::login_param::LoginParam;
8+
use crate::provider::Socket;
99

1010
use super::read_url::read_url;
1111
use super::Error;
@@ -136,13 +136,13 @@ fn moz_autoconfigure_text_cb<B: std::io::BufRead>(
136136
MozConfigTag::Sockettype => {
137137
let val_lower = val.to_lowercase();
138138
if val_lower == "ssl" {
139-
moz_ac.out.server_flags |= DC_LP_IMAP_SOCKET_SSL as i32
139+
moz_ac.out.mail_security = Socket::SSL;
140140
}
141141
if val_lower == "starttls" {
142-
moz_ac.out.server_flags |= DC_LP_IMAP_SOCKET_STARTTLS as i32
142+
moz_ac.out.mail_security = Socket::STARTTLS;
143143
}
144144
if val_lower == "plain" {
145-
moz_ac.out.server_flags |= DC_LP_IMAP_SOCKET_PLAIN as i32
145+
moz_ac.out.mail_security = Socket::Plain;
146146
}
147147
}
148148
_ => {}
@@ -154,13 +154,13 @@ fn moz_autoconfigure_text_cb<B: std::io::BufRead>(
154154
MozConfigTag::Sockettype => {
155155
let val_lower = val.to_lowercase();
156156
if val_lower == "ssl" {
157-
moz_ac.out.server_flags |= DC_LP_SMTP_SOCKET_SSL as i32
157+
moz_ac.out.send_security = Socket::SSL;
158158
}
159159
if val_lower == "starttls" {
160-
moz_ac.out.server_flags |= DC_LP_SMTP_SOCKET_STARTTLS as i32
160+
moz_ac.out.send_security = Socket::STARTTLS;
161161
}
162162
if val_lower == "plain" {
163-
moz_ac.out.server_flags |= DC_LP_SMTP_SOCKET_PLAIN as i32
163+
moz_ac.out.send_security = Socket::Plain;
164164
}
165165
}
166166
_ => {}

src/configure/auto_outlook.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
33
use quick_xml::events::BytesEnd;
44

5-
use crate::constants::*;
65
use crate::context::Context;
76
use crate::login_param::LoginParam;
7+
use crate::provider::Socket;
88

99
use super::read_url::read_url;
1010
use super::Error;
@@ -146,19 +146,19 @@ fn outlk_autodiscover_endtag_cb(event: &BytesEnd, outlk_ad: &mut OutlookAutodisc
146146
std::mem::replace(&mut outlk_ad.config_server, String::new());
147147
outlk_ad.out.mail_port = port;
148148
if ssl_on {
149-
outlk_ad.out.server_flags |= DC_LP_IMAP_SOCKET_SSL as i32
149+
outlk_ad.out.mail_security = Socket::SSL
150150
} else if ssl_off {
151-
outlk_ad.out.server_flags |= DC_LP_IMAP_SOCKET_PLAIN as i32
151+
outlk_ad.out.mail_security = Socket::Plain
152152
}
153153
outlk_ad.out_imap_set = true
154154
} else if type_ == "smtp" && !outlk_ad.out_smtp_set {
155155
outlk_ad.out.send_server =
156156
std::mem::replace(&mut outlk_ad.config_server, String::new());
157157
outlk_ad.out.send_port = outlk_ad.config_port;
158158
if ssl_on {
159-
outlk_ad.out.server_flags |= DC_LP_SMTP_SOCKET_SSL as i32
159+
outlk_ad.out.send_security = Socket::SSL
160160
} else if ssl_off {
161-
outlk_ad.out.server_flags |= DC_LP_SMTP_SOCKET_PLAIN as i32
161+
outlk_ad.out.send_security = Socket::Plain
162162
}
163163
outlk_ad.out_smtp_set = true
164164
}

src/configure/mod.rs

+22-57
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::imap::Imap;
1616
use crate::login_param::{CertificateChecks, LoginParam, LoginParamNew, ServerParams};
1717
use crate::message::Message;
1818
use crate::oauth2::*;
19+
use crate::provider::Socket;
1920
use crate::smtp::Smtp;
2021
use crate::{chat, e2ee, provider};
2122

@@ -201,12 +202,6 @@ async fn configure(ctx: &Context, param: &mut LoginParam) -> Result<()> {
201202
if param.send_pw.is_empty() {
202203
param.send_pw = param.mail_pw.clone()
203204
}
204-
if !dc_exactly_one_bit_set(param.server_flags & DC_LP_IMAP_SOCKET_FLAGS as i32) {
205-
param.server_flags &= !(DC_LP_IMAP_SOCKET_FLAGS as i32);
206-
}
207-
if !dc_exactly_one_bit_set(param.server_flags & (DC_LP_SMTP_SOCKET_FLAGS as i32)) {
208-
param.server_flags &= !(DC_LP_SMTP_SOCKET_FLAGS as i32);
209-
}
210205
if !dc_exactly_one_bit_set(param.server_flags & DC_LP_AUTH_FLAGS as i32) {
211206
param.server_flags &= !(DC_LP_AUTH_FLAGS as i32);
212207
param.server_flags |= DC_LP_AUTH_NORMAL as i32
@@ -393,22 +388,14 @@ pub fn loginparam_new_to_old(context: &Context, servers: &LoginParamNew) -> Opti
393388
p.mail_server = imap.hostname.to_string();
394389
p.mail_user = imap.apply_username_pattern(addr.clone());
395390
p.mail_port = imap.port as i32;
391+
p.mail_security = imap.socket;
396392
p.imap_certificate_checks = CertificateChecks::AcceptInvalidCertificates;
397-
p.server_flags |= match imap.socket {
398-
provider::Socket::Plain => DC_LP_SMTP_SOCKET_PLAIN,
399-
provider::Socket::STARTTLS => DC_LP_IMAP_SOCKET_STARTTLS,
400-
provider::Socket::SSL => DC_LP_IMAP_SOCKET_SSL,
401-
};
402393

403394
p.send_server = smtp.hostname.to_string();
404395
p.send_user = smtp.apply_username_pattern(addr.clone());
405396
p.send_port = smtp.port as i32;
397+
p.send_security = smtp.socket;
406398
p.smtp_certificate_checks = CertificateChecks::AcceptInvalidCertificates;
407-
p.server_flags |= match smtp.socket {
408-
provider::Socket::Plain => DC_LP_SMTP_SOCKET_PLAIN,
409-
provider::Socket::STARTTLS => DC_LP_SMTP_SOCKET_STARTTLS,
410-
provider::Socket::SSL => DC_LP_SMTP_SOCKET_SSL,
411-
};
412399

413400
info!(context, "offline autoconfig found: {}", p);
414401
return Some(p);
@@ -423,12 +410,7 @@ pub fn loginparam_old_to_new(p: LoginParam) -> LoginParamNew {
423410
addr: p.addr.clone(),
424411
imap: vec![ServerParams {
425412
protocol: Protocol::IMAP,
426-
socket: match p.server_flags & DC_LP_IMAP_SOCKET_FLAGS as i32 {
427-
DC_LP_IMAP_SOCKET_PLAIN => provider::Socket::Plain,
428-
DC_LP_IMAP_SOCKET_STARTTLS => provider::Socket::STARTTLS,
429-
DC_LP_IMAP_SOCKET_SSL => provider::Socket::SSL,
430-
_ => Default::default(),
431-
},
413+
socket: p.mail_security,
432414
port: p.mail_port as u16,
433415
hostname: p.mail_server,
434416
username_pattern: if p.mail_user.contains('@') {
@@ -439,12 +421,7 @@ pub fn loginparam_old_to_new(p: LoginParam) -> LoginParamNew {
439421
}],
440422
smtp: vec![ServerParams {
441423
protocol: Protocol::SMTP,
442-
socket: match p.server_flags & DC_LP_SMTP_SOCKET_FLAGS as i32 {
443-
DC_LP_SMTP_SOCKET_PLAIN => provider::Socket::Plain,
444-
DC_LP_SMTP_SOCKET_STARTTLS => provider::Socket::STARTTLS,
445-
DC_LP_SMTP_SOCKET_SSL => provider::Socket::SSL,
446-
_ => Default::default(),
447-
},
424+
socket: p.send_security,
448425
port: p.send_port as u16,
449426
hostname: p.send_server,
450427
username_pattern: if p.send_user.contains('@') {
@@ -493,10 +470,10 @@ async fn try_imap_ports(
493470
) -> Result<LoginParam> {
494471
// Try to infer port from socket security.
495472
if param.mail_port == 0 {
496-
if 0 != param.server_flags & DC_LP_IMAP_SOCKET_SSL {
473+
if param.mail_security == Socket::SSL {
497474
param.mail_port = 993
498475
}
499-
if 0 != param.server_flags & (DC_LP_IMAP_SOCKET_STARTTLS | DC_LP_IMAP_SOCKET_PLAIN) {
476+
if param.mail_security == Socket::STARTTLS || param.mail_security == Socket::Plain {
500477
param.mail_port = 143
501478
}
502479
}
@@ -507,29 +484,25 @@ async fn try_imap_ports(
507484
// Try common secure combinations.
508485

509486
// Try TLS over port 993
510-
param.server_flags &= !(DC_LP_IMAP_SOCKET_FLAGS as i32);
511-
param.server_flags |= DC_LP_IMAP_SOCKET_SSL as i32;
487+
param.mail_security = Socket::SSL;
512488
param.mail_port = 993;
513489
if let Ok(login_param) = try_imap_usernames(context, param.clone(), imap).await {
514490
return Ok(login_param);
515491
}
516492

517493
// Try STARTTLS over port 143
518-
param.server_flags &= !(DC_LP_IMAP_SOCKET_FLAGS as i32);
519-
param.server_flags |= DC_LP_IMAP_SOCKET_STARTTLS as i32;
494+
param.mail_security = Socket::STARTTLS;
520495
param.mail_port = 143;
521496
try_imap_usernames(context, param, imap).await
522-
} else if 0 == param.server_flags & DC_LP_SMTP_SOCKET_FLAGS as i32 {
497+
} else if param.send_security == Socket::Automatic {
523498
// Try TLS over user-provided port.
524-
param.server_flags &= !(DC_LP_IMAP_SOCKET_FLAGS as i32);
525-
param.server_flags |= DC_LP_IMAP_SOCKET_SSL as i32;
499+
param.mail_security = Socket::SSL;
526500
if let Ok(login_param) = try_imap_usernames(context, param.clone(), imap).await {
527501
return Ok(login_param);
528502
}
529503

530504
// Try STARTTLS over user-provided port.
531-
param.server_flags &= !(DC_LP_IMAP_SOCKET_FLAGS as i32);
532-
param.server_flags |= DC_LP_IMAP_SOCKET_STARTTLS as i32;
505+
param.mail_security = Socket::STARTTLS;
533506
try_imap_usernames(context, param, imap).await
534507
} else {
535508
try_imap_usernames(context, param, imap).await
@@ -614,15 +587,11 @@ async fn try_smtp_ports(
614587
) -> Result<LoginParam> {
615588
// Try to infer port from socket security.
616589
if param.send_port == 0 {
617-
if 0 != param.server_flags & DC_LP_SMTP_SOCKET_STARTTLS as i32 {
618-
param.send_port = 587;
619-
}
620-
if 0 != param.server_flags & DC_LP_SMTP_SOCKET_PLAIN as i32 {
621-
param.send_port = 25;
622-
}
623-
if 0 != param.server_flags & DC_LP_SMTP_SOCKET_SSL as i32 {
624-
param.send_port = 465;
625-
}
590+
param.send_port = match param.send_security {
591+
Socket::Automatic => 0,
592+
Socket::STARTTLS | Socket::Plain => 587,
593+
Socket::SSL => 465,
594+
};
626595
}
627596

628597
if param.send_port == 0 {
@@ -631,29 +600,25 @@ async fn try_smtp_ports(
631600
// Try common secure combinations.
632601

633602
// Try TLS over port 465.
634-
param.server_flags &= !(DC_LP_SMTP_SOCKET_FLAGS as i32);
635-
param.server_flags |= DC_LP_SMTP_SOCKET_SSL as i32;
603+
param.send_security = Socket::SSL;
636604
param.send_port = 465;
637605
if let Ok(login_param) = try_smtp_usernames(context, param.clone(), smtp).await {
638606
return Ok(login_param);
639607
}
640608

641609
// Try STARTTLS over port 587.
642-
param.server_flags &= !(DC_LP_SMTP_SOCKET_FLAGS as i32);
643-
param.server_flags |= DC_LP_SMTP_SOCKET_STARTTLS as i32;
610+
param.send_security = Socket::STARTTLS;
644611
param.send_port = 587;
645612
try_smtp_usernames(context, param, smtp).await
646-
} else if 0 == param.server_flags & DC_LP_SMTP_SOCKET_FLAGS as i32 {
613+
} else if param.send_security == Socket::Automatic {
647614
// Try TLS over user-provided port.
648-
param.server_flags &= !(DC_LP_SMTP_SOCKET_FLAGS as i32);
649-
param.server_flags |= DC_LP_SMTP_SOCKET_SSL as i32;
615+
param.send_security = Socket::SSL;
650616
if let Ok(param) = try_smtp_usernames(context, param.clone(), smtp).await {
651617
return Ok(param);
652618
}
653619

654620
// Try STARTTLS over user-provided port.
655-
param.server_flags &= !(DC_LP_SMTP_SOCKET_FLAGS as i32);
656-
param.server_flags |= DC_LP_SMTP_SOCKET_STARTTLS as i32;
621+
param.send_security = Socket::STARTTLS;
657622
try_smtp_usernames(context, param, smtp).await
658623
} else {
659624
try_smtp_usernames(context, param, smtp).await

0 commit comments

Comments
 (0)