start reworking context references and smtp, imap

This commit is contained in:
dignifiedquire
2019-05-01 11:38:51 +02:00
parent 3e971d81d4
commit 72e9fe6150
33 changed files with 1228 additions and 1303 deletions

View File

@@ -34,85 +34,80 @@ pub fn dc_smtp_new() -> dc_smtp_t {
}
}
pub unsafe fn dc_smtp_unref(mut smtp: *mut dc_smtp_t) {
if smtp.is_null() {
return;
}
pub unsafe fn dc_smtp_unref(smtp: &mut dc_smtp_t) {
dc_smtp_disconnect(smtp);
free((*smtp).from as *mut libc::c_void);
free((*smtp).error as *mut libc::c_void);
free(smtp.from as *mut libc::c_void);
free(smtp.error as *mut libc::c_void);
free(smtp as *mut libc::c_void);
}
pub unsafe fn dc_smtp_disconnect(mut smtp: *mut dc_smtp_t) {
if smtp.is_null() {
return;
pub unsafe fn dc_smtp_disconnect(smtp: &mut dc_smtp_t) {
if !smtp.etpan.is_null() {
mailsmtp_free(smtp.etpan);
smtp.etpan = 0 as *mut mailsmtp;
}
if !(*smtp).etpan.is_null() {
mailsmtp_free((*smtp).etpan);
(*smtp).etpan = 0 as *mut mailsmtp
};
}
pub unsafe fn dc_smtp_is_connected(mut smtp: *const dc_smtp_t) -> libc::c_int {
return if !smtp.is_null() && !(*smtp).etpan.is_null() {
1i32
pub unsafe fn dc_smtp_is_connected(smtp: *const dc_smtp_t) -> libc::c_int {
if !smtp.etpan.is_null() {
1
} else {
0i32
};
}
pub unsafe fn dc_smtp_connect(
mut smtp: *mut dc_smtp_t,
mut lp: *const dc_loginparam_t,
) -> libc::c_int {
let mut current_block: u64;
let mut success: libc::c_int = 0i32;
let mut r: libc::c_int = 0i32;
let mut try_esmtp: libc::c_int = 0i32;
if smtp.is_null() || lp.is_null() {
return 0i32;
0
}
if !(*smtp).etpan.is_null() {
}
pub unsafe fn dc_smtp_connect(smtp: &mut dc_smtp_t, lp: *const dc_loginparam_t) -> libc::c_int {
let mut current_block: u64;
let mut success: libc::c_int = 0;
let mut r: libc::c_int = 0;
let mut try_esmtp: libc::c_int = 0;
if lp.is_null() {
return 0;
}
if !smtp.etpan.is_null() {
dc_log_warning(
(*smtp).context,
0i32,
smtp.context,
0,
b"SMTP already connected.\x00" as *const u8 as *const libc::c_char,
);
success = 1i32
} else if (*lp).addr.is_null() || (*lp).send_server.is_null() || (*lp).send_port == 0i32 {
success = 1;
} else if (*lp).addr.is_null() || (*lp).send_server.is_null() || (*lp).send_port == 0 {
dc_log_event_seq(
(*smtp).context,
smtp.context,
Event::ERROR_NETWORK,
&mut (*smtp).log_connect_errors as *mut libc::c_int,
&mut smtp.log_connect_errors as *mut libc::c_int,
b"SMTP bad parameters.\x00" as *const u8 as *const libc::c_char,
);
} else {
free((*smtp).from as *mut libc::c_void);
(*smtp).from = dc_strdup((*lp).addr);
(*smtp).etpan = mailsmtp_new(0i32 as size_t, None);
if (*smtp).etpan.is_null() {
free(smtp.from as *mut libc::c_void);
smtp.from = dc_strdup((*lp).addr);
smtp.etpan = mailsmtp_new(0 as size_t, None);
if smtp.etpan.is_null() {
dc_log_error(
(*smtp).context,
0i32,
smtp.context,
0,
b"SMTP-object creation failed.\x00" as *const u8 as *const libc::c_char,
);
} else {
mailsmtp_set_timeout((*smtp).etpan, 10i32 as time_t);
mailsmtp_set_timeout(smtp.etpan, 10 as time_t);
mailsmtp_set_progress_callback(
(*smtp).etpan,
smtp.etpan,
Some(body_progress),
smtp as *mut libc::c_void,
);
/* connect to SMTP server */
if 0 != (*lp).server_flags & (0x10000i32 | 0x40000i32) {
if 0 != (*lp).server_flags & (0x10000 | 0x40000) {
r = mailsmtp_socket_connect(
(*smtp).etpan,
smtp.etpan,
(*lp).send_server,
(*lp).send_port as uint16_t,
);
if r != MAILSMTP_NO_ERROR as libc::c_int {
dc_log_event_seq(
(*smtp).context,
smtp.context,
Event::ERROR_NETWORK,
&mut (*smtp).log_connect_errors as *mut libc::c_int,
&mut smtp.log_connect_errors as *mut libc::c_int,
b"SMTP-Socket connection to %s:%i failed (%s)\x00" as *const u8
as *const libc::c_char,
(*lp).send_server,
@@ -125,15 +120,15 @@ pub unsafe fn dc_smtp_connect(
}
} else {
r = mailsmtp_ssl_connect(
(*smtp).etpan,
smtp.etpan,
(*lp).send_server,
(*lp).send_port as uint16_t,
);
if r != MAILSMTP_NO_ERROR as libc::c_int {
dc_log_event_seq(
(*smtp).context,
smtp.context,
Event::ERROR_NETWORK,
&mut (*smtp).log_connect_errors as *mut libc::c_int,
&mut smtp.log_connect_errors as *mut libc::c_int,
b"SMTP-SSL connection to %s:%i failed (%s)\x00" as *const u8
as *const libc::c_char,
(*lp).send_server,
@@ -148,54 +143,54 @@ pub unsafe fn dc_smtp_connect(
match current_block {
12512295087047028901 => {}
_ => {
try_esmtp = 1i32;
(*smtp).esmtp = 0i32;
try_esmtp = 1;
smtp.esmtp = 0;
if 0 != try_esmtp && {
r = mailesmtp_ehlo((*smtp).etpan);
r = mailesmtp_ehlo(smtp.etpan);
r == MAILSMTP_NO_ERROR as libc::c_int
} {
(*smtp).esmtp = 1i32
smtp.esmtp = 1
} else if 0 == try_esmtp || r == MAILSMTP_ERROR_NOT_IMPLEMENTED as libc::c_int {
r = mailsmtp_helo((*smtp).etpan)
r = mailsmtp_helo(smtp.etpan)
}
if r != MAILSMTP_NO_ERROR as libc::c_int {
dc_log_event_seq(
(*smtp).context,
smtp.context,
Event::ERROR_NETWORK,
&mut (*smtp).log_connect_errors as *mut libc::c_int,
&mut smtp.log_connect_errors as *mut libc::c_int,
b"SMTP-helo failed (%s)\x00" as *const u8 as *const libc::c_char,
mailsmtp_strerror(r),
);
} else {
if 0 != (*lp).server_flags & 0x10000i32 {
r = mailsmtp_socket_starttls((*smtp).etpan);
if 0 != (*lp).server_flags & 0x10000 {
r = mailsmtp_socket_starttls(smtp.etpan);
if r != MAILSMTP_NO_ERROR as libc::c_int {
dc_log_event_seq(
(*smtp).context,
smtp.context,
Event::ERROR_NETWORK,
&mut (*smtp).log_connect_errors as *mut libc::c_int,
&mut smtp.log_connect_errors as *mut libc::c_int,
b"SMTP-STARTTLS failed (%s)\x00" as *const u8
as *const libc::c_char,
mailsmtp_strerror(r),
);
current_block = 12512295087047028901;
} else {
(*smtp).esmtp = 0i32;
smtp.esmtp = 0;
if 0 != try_esmtp && {
r = mailesmtp_ehlo((*smtp).etpan);
r = mailesmtp_ehlo(smtp.etpan);
r == MAILSMTP_NO_ERROR as libc::c_int
} {
(*smtp).esmtp = 1i32
smtp.esmtp = 1
} else if 0 == try_esmtp
|| r == MAILSMTP_ERROR_NOT_IMPLEMENTED as libc::c_int
{
r = mailsmtp_helo((*smtp).etpan)
r = mailsmtp_helo(smtp.etpan)
}
if r != MAILSMTP_NO_ERROR as libc::c_int {
dc_log_event_seq(
(*smtp).context,
smtp.context,
Event::ERROR_NETWORK,
&mut (*smtp).log_connect_errors as *mut libc::c_int,
&mut smtp.log_connect_errors as *mut libc::c_int,
b"SMTP-helo failed (%s)\x00" as *const u8
as *const libc::c_char,
mailsmtp_strerror(r),
@@ -203,8 +198,8 @@ pub unsafe fn dc_smtp_connect(
current_block = 12512295087047028901;
} else {
dc_log_info(
(*smtp).context,
0i32,
smtp.context,
0,
b"SMTP-server %s:%i STARTTLS-connected.\x00" as *const u8
as *const libc::c_char,
(*lp).send_server,
@@ -214,10 +209,10 @@ pub unsafe fn dc_smtp_connect(
}
}
} else {
if 0 != (*lp).server_flags & 0x40000i32 {
if 0 != (*lp).server_flags & 0x40000 {
dc_log_info(
(*smtp).context,
0i32,
smtp.context,
0,
b"SMTP-server %s:%i connected.\x00" as *const u8
as *const libc::c_char,
(*lp).send_server,
@@ -225,8 +220,8 @@ pub unsafe fn dc_smtp_connect(
);
} else {
dc_log_info(
(*smtp).context,
0i32,
smtp.context,
0,
b"SMTP-server %s:%i SSL-connected.\x00" as *const u8
as *const libc::c_char,
(*lp).send_server,
@@ -239,35 +234,35 @@ pub unsafe fn dc_smtp_connect(
12512295087047028901 => {}
_ => {
if !(*lp).send_user.is_null() {
if 0 != (*lp).server_flags & 0x2i32 {
if 0 != (*lp).server_flags & 0x2 {
dc_log_info(
(*smtp).context,
0i32,
smtp.context,
0,
b"SMTP-OAuth2 connect...\x00" as *const u8
as *const libc::c_char,
);
let mut access_token: *mut libc::c_char =
dc_get_oauth2_access_token(
(*smtp).context,
smtp.context,
(*lp).addr,
(*lp).send_pw,
0i32,
0,
);
r = mailsmtp_oauth2_authenticate(
(*smtp).etpan,
smtp.etpan,
(*lp).send_user,
access_token,
);
if r != MAILSMTP_NO_ERROR as libc::c_int {
free(access_token as *mut libc::c_void);
access_token = dc_get_oauth2_access_token(
(*smtp).context,
smtp.context,
(*lp).addr,
(*lp).send_pw,
0x1i32,
0x1,
);
r = mailsmtp_oauth2_authenticate(
(*smtp).etpan,
smtp.etpan,
(*lp).send_user,
access_token,
)
@@ -276,7 +271,7 @@ pub unsafe fn dc_smtp_connect(
current_block = 15462640364611497761;
} else {
r = mailsmtp_auth(
(*smtp).etpan,
smtp.etpan,
(*lp).send_user,
(*lp).send_pw,
);
@@ -285,12 +280,12 @@ pub unsafe fn dc_smtp_connect(
* There are some Mailservers which do not correclty implement PLAIN auth (hMail)
* So here we try a workaround. See https://github.com/deltachat/deltachat-android/issues/67
*/
if 0 != (*(*smtp).etpan).auth
if 0 != (*smtp.etpan).auth
& MAILSMTP_AUTH_PLAIN as libc::c_int
{
dc_log_info(
(*smtp).context,
0i32,
smtp.context,
0,
b"Trying SMTP-Login workaround \"%s\"...\x00"
as *const u8
as *const libc::c_char,
@@ -302,10 +297,10 @@ pub unsafe fn dc_smtp_connect(
hostname.as_mut_ptr(),
::std::mem::size_of::<[libc::c_char; 513]>(),
);
if err < 0i32 {
if err < 0 {
dc_log_error(
(*smtp).context,
0i32,
smtp.context,
0,
b"SMTP-Login: Cannot get hostname.\x00"
as *const u8
as *const libc::c_char,
@@ -313,7 +308,7 @@ pub unsafe fn dc_smtp_connect(
current_block = 12512295087047028901;
} else {
r = mailesmtp_auth_sasl(
(*smtp).etpan,
smtp.etpan,
b"PLAIN\x00" as *const u8
as *const libc::c_char,
hostname.as_mut_ptr(),
@@ -338,9 +333,9 @@ pub unsafe fn dc_smtp_connect(
_ => {
if r != MAILSMTP_NO_ERROR as libc::c_int {
dc_log_event_seq(
(*smtp).context,
smtp.context,
Event::ERROR_NETWORK,
&mut (*smtp).log_connect_errors
&mut smtp.log_connect_errors
as *mut libc::c_int,
b"SMTP-login failed for user %s (%s)\x00"
as *const u8
@@ -351,9 +346,9 @@ pub unsafe fn dc_smtp_connect(
current_block = 12512295087047028901;
} else {
dc_log_event(
(*smtp).context,
smtp.context,
Event::SMTP_CONNECTED,
0i32,
0,
b"SMTP-login as %s ok.\x00" as *const u8
as *const libc::c_char,
(*lp).send_user,
@@ -367,7 +362,7 @@ pub unsafe fn dc_smtp_connect(
}
match current_block {
12512295087047028901 => {}
_ => success = 1i32,
_ => success = 1,
}
}
}
@@ -377,13 +372,14 @@ pub unsafe fn dc_smtp_connect(
}
}
if 0 == success {
if !(*smtp).etpan.is_null() {
mailsmtp_free((*smtp).etpan);
(*smtp).etpan = 0 as *mut mailsmtp
if !smtp.etpan.is_null() {
mailsmtp_free(smtp.etpan);
smtp.etpan = 0 as *mut mailsmtp
}
}
return success;
success
}
unsafe extern "C" fn body_progress(
_current: size_t,
_maximum: size_t,
@@ -392,138 +388,129 @@ unsafe extern "C" fn body_progress(
}
pub unsafe fn dc_smtp_send_msg(
mut smtp: *mut dc_smtp_t,
mut recipients: *const clist,
mut data_not_terminated: *const libc::c_char,
mut data_bytes: size_t,
smtp: &mut dc_smtp_t,
recipients: *const clist,
data_not_terminated: *const libc::c_char,
data_bytes: size_t,
) -> libc::c_int {
let mut current_block: u64;
let mut success: libc::c_int = 0i32;
let mut r: libc::c_int = 0i32;
let mut success: libc::c_int = 0;
let mut r: libc::c_int = 0;
let mut iter: *mut clistiter = 0 as *mut clistiter;
if !smtp.is_null() {
if recipients.is_null()
|| (*recipients).count == 0i32
|| data_not_terminated.is_null()
|| data_bytes == 0
{
success = 1i32
} else if !(*smtp).etpan.is_null() {
// set source
// the `etPanSMTPTest` is the ENVID from RFC 3461 (SMTP DSNs), we should probably replace it by a random value
r = if 0 != (*smtp).esmtp {
mailesmtp_mail(
(*smtp).etpan,
(*smtp).from,
1i32,
b"etPanSMTPTest\x00" as *const u8 as *const libc::c_char,
)
} else {
mailsmtp_mail((*smtp).etpan, (*smtp).from)
};
if r != MAILSMTP_NO_ERROR as libc::c_int {
log_error(
smtp,
b"SMTP failed to start message\x00" as *const u8 as *const libc::c_char,
r,
);
} else {
// set recipients
// if the recipient is on the same server, this may fail at once.
// TODO: question is what to do if one recipient in a group fails
iter = (*recipients).first;
loop {
if iter.is_null() {
current_block = 12039483399334584727;
break;
if recipients.is_null()
|| (*recipients).count == 0
|| data_not_terminated.is_null()
|| data_bytes == 0
{
success = 1
} else if !smtp.etpan.is_null() {
// set source
// the `etPanSMTPTest` is the ENVID from RFC 3461 (SMTP DSNs), we should probably replace it by a random value
r = if 0 != smtp.esmtp {
mailesmtp_mail(
smtp.etpan,
smtp.from,
1,
b"etPanSMTPTest\x00" as *const u8 as *const libc::c_char,
)
} else {
mailsmtp_mail(smtp.etpan, smtp.from)
};
if r != MAILSMTP_NO_ERROR as libc::c_int {
log_error(
smtp,
b"SMTP failed to start message\x00" as *const u8 as *const libc::c_char,
r,
);
} else {
// set recipients
// if the recipient is on the same server, this may fail at once.
// TODO: question is what to do if one recipient in a group fails
iter = (*recipients).first;
loop {
if iter.is_null() {
current_block = 12039483399334584727;
break;
}
let mut rcpt: *const libc::c_char = (if !iter.is_null() {
(*iter).data
} else {
0 as *mut libc::c_void
}) as *const libc::c_char;
r = if 0 != smtp.esmtp {
mailesmtp_rcpt(smtp.etpan, rcpt, 2 | 4, 0 as *const libc::c_char)
} else {
mailsmtp_rcpt(smtp.etpan, rcpt)
};
if r != MAILSMTP_NO_ERROR as libc::c_int {
log_error(
smtp,
b"SMTP failed to add recipient\x00" as *const u8 as *const libc::c_char,
r,
);
current_block = 5498835644851925448;
break;
} else {
iter = if !iter.is_null() {
(*iter).next
} else {
0 as *mut clistcell_s
}
let mut rcpt: *const libc::c_char = (if !iter.is_null() {
(*iter).data
} else {
0 as *mut libc::c_void
})
as *const libc::c_char;
r = if 0 != (*smtp).esmtp {
mailesmtp_rcpt((*smtp).etpan, rcpt, 2i32 | 4i32, 0 as *const libc::c_char)
} else {
mailsmtp_rcpt((*smtp).etpan, rcpt)
};
}
}
match current_block {
5498835644851925448 => {}
_ => {
// message
r = mailsmtp_data(smtp.etpan);
if r != MAILSMTP_NO_ERROR as libc::c_int {
log_error(
smtp,
b"SMTP failed to add recipient\x00" as *const u8 as *const libc::c_char,
b"SMTP failed to set data\x00" as *const u8 as *const libc::c_char,
r,
);
current_block = 5498835644851925448;
break;
} else {
iter = if !iter.is_null() {
(*iter).next
} else {
0 as *mut clistcell_s
}
}
}
match current_block {
5498835644851925448 => {}
_ => {
// message
r = mailsmtp_data((*smtp).etpan);
r = mailsmtp_data_message(smtp.etpan, data_not_terminated, data_bytes);
if r != MAILSMTP_NO_ERROR as libc::c_int {
log_error(
smtp,
b"SMTP failed to set data\x00" as *const u8 as *const libc::c_char,
b"SMTP failed to send message\x00" as *const u8
as *const libc::c_char,
r,
);
} else {
r = mailsmtp_data_message(
(*smtp).etpan,
data_not_terminated,
data_bytes,
dc_log_event(
smtp.context,
Event::SMTP_MESSAGE_SENT,
0,
b"Message was sent to SMTP server\x00" as *const u8
as *const libc::c_char,
);
if r != MAILSMTP_NO_ERROR as libc::c_int {
log_error(
smtp,
b"SMTP failed to send message\x00" as *const u8
as *const libc::c_char,
r,
);
} else {
dc_log_event(
(*smtp).context,
Event::SMTP_MESSAGE_SENT,
0i32,
b"Message was sent to SMTP server\x00" as *const u8
as *const libc::c_char,
);
success = 1i32
}
success = 1;
}
}
}
}
}
}
return success;
success
}
unsafe fn log_error(
mut smtp: *mut dc_smtp_t,
mut what_failed: *const libc::c_char,
mut r: libc::c_int,
) {
unsafe fn log_error(smtp: &mut dc_smtp_t, what_failed: *const libc::c_char, r: libc::c_int) {
let mut error_msg: *mut libc::c_char = dc_mprintf(
b"%s: %s: %s\x00" as *const u8 as *const libc::c_char,
what_failed,
mailsmtp_strerror(r),
(*(*smtp).etpan).response,
(*smtp.etpan).response,
);
dc_log_warning(
(*smtp).context,
0i32,
smtp.context,
0,
b"%s\x00" as *const u8 as *const libc::c_char,
error_msg,
);
free((*smtp).error as *mut libc::c_void);
(*smtp).error = error_msg;
(*smtp).error_etpan = r;
free(smtp.error as *mut libc::c_void);
smtp.error = error_msg;
smtp.error_etpan = r;
}