refactor(msg): use rust based allocations

This commit is contained in:
dignifiedquire
2019-08-18 21:26:53 +02:00
parent a906faeb35
commit cb6c8ac78b
13 changed files with 982 additions and 1227 deletions

View File

@@ -505,7 +505,6 @@ int dc_chat_is_sending_locations (const dc_chat_t*);
dc_msg_t* dc_msg_new (dc_context_t*, int viewtype);
void dc_msg_unref (dc_msg_t*);
void dc_msg_empty (dc_msg_t*);
uint32_t dc_msg_get_id (const dc_msg_t*);
uint32_t dc_msg_get_from_id (const dc_msg_t*);
uint32_t dc_msg_get_chat_id (const dc_msg_t*);

View File

@@ -354,12 +354,13 @@ pub unsafe extern "C" fn dc_get_chat_id_by_contact_id(
pub unsafe extern "C" fn dc_prepare_msg(
context: *mut dc_context_t,
chat_id: u32,
msg: *mut dc_msg::dc_msg_t,
msg: *mut dc_msg_t,
) -> u32 {
assert!(!context.is_null());
assert!(!msg.is_null());
let context = &*context;
let context = &mut *context;
let msg = &mut *msg;
chat::prepare_msg(context, chat_id, msg)
.unwrap_or_log_default(context, "Failed to prepare message")
}
@@ -368,11 +369,12 @@ pub unsafe extern "C" fn dc_prepare_msg(
pub unsafe extern "C" fn dc_send_msg(
context: *mut dc_context_t,
chat_id: u32,
msg: *mut dc_msg::dc_msg_t,
msg: *mut dc_msg_t,
) -> u32 {
assert!(!context.is_null());
assert!(!msg.is_null());
let context = &*context;
let context = &mut *context;
let msg = &mut *msg;
chat::send_msg(context, chat_id, msg).unwrap_or_log_default(context, "Failed to send message")
}
@@ -396,10 +398,11 @@ pub unsafe extern "C" fn dc_send_text_msg(
pub unsafe extern "C" fn dc_set_draft(
context: *mut dc_context_t,
chat_id: u32,
msg: *mut dc_msg::dc_msg_t,
msg: *mut dc_msg_t,
) {
assert!(!context.is_null());
let context = &*context;
let msg = if msg.is_null() { None } else { Some(&mut *msg) };
chat::set_draft(context, chat_id, msg)
}
@@ -408,11 +411,11 @@ pub unsafe extern "C" fn dc_set_draft(
pub unsafe extern "C" fn dc_get_draft<'a>(
context: *mut dc_context_t,
chat_id: u32,
) -> *mut dc_msg::dc_msg_t<'a> {
) -> *mut dc_msg_t<'a> {
assert!(!context.is_null());
let context = &*context;
chat::get_draft(context, chat_id)
chat::get_draft(context, chat_id).into_raw()
}
#[no_mangle]
@@ -778,11 +781,11 @@ pub unsafe extern "C" fn dc_star_msgs(
pub unsafe extern "C" fn dc_get_msg<'a>(
context: *mut dc_context_t,
msg_id: u32,
) -> *mut dc_msg::dc_msg_t<'a> {
) -> *mut dc_msg_t<'a> {
assert!(!context.is_null());
let context = &*context;
dc_msg::dc_get_msg(context, msg_id)
dc_msg::dc_get_msg(context, msg_id).into_raw()
}
#[no_mangle]
@@ -1398,299 +1401,325 @@ pub unsafe extern "C" fn dc_chat_is_sending_locations(chat: *mut dc_chat_t) -> l
// dc_msg_t
#[no_mangle]
pub type dc_msg_t<'a> = dc_msg::dc_msg_t<'a>;
pub type dc_msg_t<'a> = dc_msg::Message<'a>;
#[no_mangle]
pub unsafe extern "C" fn dc_msg_new<'a>(
context: *mut dc_context_t,
viewtype: libc::c_int,
) -> *mut dc_msg::dc_msg_t<'a> {
) -> *mut dc_msg_t<'a> {
assert!(!context.is_null());
let context = &*context;
let viewtype = from_prim(viewtype).expect(&format!("invalid viewtype = {}", viewtype));
dc_msg::dc_msg_new(context, viewtype)
Box::into_raw(Box::new(dc_msg::dc_msg_new(context, viewtype)))
}
#[no_mangle]
pub unsafe extern "C" fn dc_msg_unref(msg: *mut dc_msg::dc_msg_t) {
pub unsafe extern "C" fn dc_msg_unref(msg: *mut dc_msg_t) {
assert!(!msg.is_null());
dc_msg::dc_msg_unref(msg)
Box::from_raw(msg);
}
#[no_mangle]
pub unsafe extern "C" fn dc_msg_empty(msg: *mut dc_msg::dc_msg_t) {
assert!(!msg.is_null());
dc_msg::dc_msg_empty(msg)
}
#[no_mangle]
pub unsafe extern "C" fn dc_msg_get_id(msg: *mut dc_msg::dc_msg_t) -> u32 {
pub unsafe extern "C" fn dc_msg_get_id(msg: *mut dc_msg_t) -> u32 {
assert!(!msg.is_null());
let msg = &*msg;
dc_msg::dc_msg_get_id(msg)
}
#[no_mangle]
pub unsafe extern "C" fn dc_msg_get_from_id(msg: *mut dc_msg::dc_msg_t) -> u32 {
pub unsafe extern "C" fn dc_msg_get_from_id(msg: *mut dc_msg_t) -> u32 {
assert!(!msg.is_null());
let msg = &*msg;
dc_msg::dc_msg_get_from_id(msg)
}
#[no_mangle]
pub unsafe extern "C" fn dc_msg_get_chat_id(msg: *mut dc_msg::dc_msg_t) -> u32 {
pub unsafe extern "C" fn dc_msg_get_chat_id(msg: *mut dc_msg_t) -> u32 {
assert!(!msg.is_null());
let msg = &*msg;
dc_msg::dc_msg_get_chat_id(msg)
}
#[no_mangle]
pub unsafe extern "C" fn dc_msg_get_viewtype(msg: *mut dc_msg::dc_msg_t) -> libc::c_int {
pub unsafe extern "C" fn dc_msg_get_viewtype(msg: *mut dc_msg_t) -> libc::c_int {
assert!(!msg.is_null());
let msg = &*msg;
dc_msg::dc_msg_get_viewtype(msg)
.to_i64()
.expect("impossible: Viewtype -> i64 conversion failed") as libc::c_int
}
#[no_mangle]
pub unsafe extern "C" fn dc_msg_get_state(msg: *mut dc_msg::dc_msg_t) -> libc::c_int {
pub unsafe extern "C" fn dc_msg_get_state(msg: *mut dc_msg_t) -> libc::c_int {
assert!(!msg.is_null());
let msg = &*msg;
dc_msg::dc_msg_get_state(msg) as libc::c_int
}
#[no_mangle]
pub unsafe extern "C" fn dc_msg_get_timestamp(msg: *mut dc_msg::dc_msg_t) -> i64 {
pub unsafe extern "C" fn dc_msg_get_timestamp(msg: *mut dc_msg_t) -> i64 {
assert!(!msg.is_null());
let msg = &*msg;
dc_msg::dc_msg_get_timestamp(msg)
}
#[no_mangle]
pub unsafe extern "C" fn dc_msg_get_received_timestamp(msg: *mut dc_msg::dc_msg_t) -> i64 {
pub unsafe extern "C" fn dc_msg_get_received_timestamp(msg: *mut dc_msg_t) -> i64 {
assert!(!msg.is_null());
let msg = &*msg;
dc_msg::dc_msg_get_received_timestamp(msg)
}
#[no_mangle]
pub unsafe extern "C" fn dc_msg_get_sort_timestamp(msg: *mut dc_msg::dc_msg_t) -> i64 {
pub unsafe extern "C" fn dc_msg_get_sort_timestamp(msg: *mut dc_msg_t) -> i64 {
assert!(!msg.is_null());
let msg = &*msg;
dc_msg::dc_msg_get_sort_timestamp(msg)
}
#[no_mangle]
pub unsafe extern "C" fn dc_msg_get_text(msg: *mut dc_msg::dc_msg_t) -> *mut libc::c_char {
pub unsafe extern "C" fn dc_msg_get_text(msg: *mut dc_msg_t) -> *mut libc::c_char {
assert!(!msg.is_null());
let msg = &*msg;
dc_msg::dc_msg_get_text(msg)
}
#[no_mangle]
pub unsafe extern "C" fn dc_msg_get_file(msg: *mut dc_msg::dc_msg_t) -> *mut libc::c_char {
pub unsafe extern "C" fn dc_msg_get_file(msg: *mut dc_msg_t) -> *mut libc::c_char {
assert!(!msg.is_null());
let msg = &*msg;
dc_msg::dc_msg_get_file(msg)
}
#[no_mangle]
pub unsafe extern "C" fn dc_msg_get_filename(msg: *mut dc_msg::dc_msg_t) -> *mut libc::c_char {
pub unsafe extern "C" fn dc_msg_get_filename(msg: *mut dc_msg_t) -> *mut libc::c_char {
assert!(!msg.is_null());
let msg = &*msg;
dc_msg::dc_msg_get_filename(msg)
}
#[no_mangle]
pub unsafe extern "C" fn dc_msg_get_filemime(msg: *mut dc_msg::dc_msg_t) -> *mut libc::c_char {
pub unsafe extern "C" fn dc_msg_get_filemime(msg: *mut dc_msg_t) -> *mut libc::c_char {
assert!(!msg.is_null());
let msg = &*msg;
dc_msg::dc_msg_get_filemime(msg)
}
#[no_mangle]
pub unsafe extern "C" fn dc_msg_get_filebytes(msg: *mut dc_msg::dc_msg_t) -> u64 {
pub unsafe extern "C" fn dc_msg_get_filebytes(msg: *mut dc_msg_t) -> u64 {
assert!(!msg.is_null());
let msg = &*msg;
dc_msg::dc_msg_get_filebytes(msg)
}
#[no_mangle]
pub unsafe extern "C" fn dc_msg_get_width(msg: *mut dc_msg::dc_msg_t) -> libc::c_int {
pub unsafe extern "C" fn dc_msg_get_width(msg: *mut dc_msg_t) -> libc::c_int {
assert!(!msg.is_null());
let msg = &*msg;
dc_msg::dc_msg_get_width(msg)
}
#[no_mangle]
pub unsafe extern "C" fn dc_msg_get_height(msg: *mut dc_msg::dc_msg_t) -> libc::c_int {
pub unsafe extern "C" fn dc_msg_get_height(msg: *mut dc_msg_t) -> libc::c_int {
assert!(!msg.is_null());
let msg = &*msg;
dc_msg::dc_msg_get_height(msg)
}
#[no_mangle]
pub unsafe extern "C" fn dc_msg_get_duration(msg: *mut dc_msg::dc_msg_t) -> libc::c_int {
pub unsafe extern "C" fn dc_msg_get_duration(msg: *mut dc_msg_t) -> libc::c_int {
assert!(!msg.is_null());
let msg = &*msg;
dc_msg::dc_msg_get_duration(msg)
}
#[no_mangle]
pub unsafe extern "C" fn dc_msg_get_showpadlock(msg: *mut dc_msg::dc_msg_t) -> libc::c_int {
pub unsafe extern "C" fn dc_msg_get_showpadlock(msg: *mut dc_msg_t) -> libc::c_int {
assert!(!msg.is_null());
let msg = &*msg;
dc_msg::dc_msg_get_showpadlock(msg)
}
#[no_mangle]
pub unsafe extern "C" fn dc_msg_get_summary<'a>(
msg: *mut dc_msg::dc_msg_t<'a>,
msg: *mut dc_msg_t<'a>,
chat: *mut dc_chat_t<'a>,
) -> *mut dc_lot_t {
assert!(!msg.is_null());
let chat = if chat.is_null() { None } else { Some(&*chat) };
let msg = &mut *msg;
let lot = dc_msg::dc_msg_get_summary(msg, chat);
Box::into_raw(Box::new(lot))
}
#[no_mangle]
pub unsafe extern "C" fn dc_msg_get_summarytext(
msg: *mut dc_msg::dc_msg_t,
msg: *mut dc_msg_t,
approx_characters: libc::c_int,
) -> *mut libc::c_char {
assert!(!msg.is_null());
let msg = &mut *msg;
dc_msg::dc_msg_get_summarytext(msg, approx_characters.try_into().unwrap())
}
#[no_mangle]
pub unsafe extern "C" fn dc_msg_has_deviating_timestamp(msg: *mut dc_msg::dc_msg_t) -> libc::c_int {
pub unsafe extern "C" fn dc_msg_has_deviating_timestamp(msg: *mut dc_msg_t) -> libc::c_int {
assert!(!msg.is_null());
let msg = &*msg;
dc_msg::dc_msg_has_deviating_timestamp(msg)
}
#[no_mangle]
pub unsafe extern "C" fn dc_msg_has_location(msg: *mut dc_msg::dc_msg_t) -> libc::c_int {
pub unsafe extern "C" fn dc_msg_has_location(msg: *mut dc_msg_t) -> libc::c_int {
assert!(!msg.is_null());
let msg = &*msg;
dc_msg::dc_msg_has_location(msg) as libc::c_int
}
#[no_mangle]
pub unsafe extern "C" fn dc_msg_is_sent(msg: *mut dc_msg::dc_msg_t) -> libc::c_int {
pub unsafe extern "C" fn dc_msg_is_sent(msg: *mut dc_msg_t) -> libc::c_int {
assert!(!msg.is_null());
let msg = &*msg;
dc_msg::dc_msg_is_sent(msg)
}
#[no_mangle]
pub unsafe extern "C" fn dc_msg_is_starred(msg: *mut dc_msg::dc_msg_t) -> libc::c_int {
pub unsafe extern "C" fn dc_msg_is_starred(msg: *mut dc_msg_t) -> libc::c_int {
assert!(!msg.is_null());
let msg = &*msg;
dc_msg::dc_msg_is_starred(msg).into()
}
#[no_mangle]
pub unsafe extern "C" fn dc_msg_is_forwarded(msg: *mut dc_msg::dc_msg_t) -> libc::c_int {
pub unsafe extern "C" fn dc_msg_is_forwarded(msg: *mut dc_msg_t) -> libc::c_int {
assert!(!msg.is_null());
let msg = &*msg;
dc_msg::dc_msg_is_forwarded(msg)
}
#[no_mangle]
pub unsafe extern "C" fn dc_msg_is_info(msg: *mut dc_msg::dc_msg_t) -> libc::c_int {
pub unsafe extern "C" fn dc_msg_is_info(msg: *mut dc_msg_t) -> libc::c_int {
assert!(!msg.is_null());
let msg = &*msg;
dc_msg::dc_msg_is_info(msg)
}
#[no_mangle]
pub unsafe extern "C" fn dc_msg_is_increation(msg: *mut dc_msg::dc_msg_t) -> libc::c_int {
pub unsafe extern "C" fn dc_msg_is_increation(msg: *mut dc_msg_t) -> libc::c_int {
assert!(!msg.is_null());
let msg = &*msg;
dc_msg::dc_msg_is_increation(msg)
}
#[no_mangle]
pub unsafe extern "C" fn dc_msg_is_setupmessage(msg: *mut dc_msg::dc_msg_t) -> libc::c_int {
pub unsafe extern "C" fn dc_msg_is_setupmessage(msg: *mut dc_msg_t) -> libc::c_int {
assert!(!msg.is_null());
let msg = &*msg;
dc_msg::dc_msg_is_setupmessage(msg) as libc::c_int
}
#[no_mangle]
pub unsafe extern "C" fn dc_msg_get_setupcodebegin(
msg: *mut dc_msg::dc_msg_t,
) -> *mut libc::c_char {
pub unsafe extern "C" fn dc_msg_get_setupcodebegin(msg: *mut dc_msg_t) -> *mut libc::c_char {
assert!(!msg.is_null());
let msg = &*msg;
dc_msg::dc_msg_get_setupcodebegin(msg)
}
#[no_mangle]
pub unsafe extern "C" fn dc_msg_set_text(msg: *mut dc_msg::dc_msg_t, text: *mut libc::c_char) {
pub unsafe extern "C" fn dc_msg_set_text(msg: *mut dc_msg_t, text: *mut libc::c_char) {
assert!(!msg.is_null());
let msg = &mut *msg;
// TODO: {text} equal to NULL is treated as "", which is strange. Does anyone rely on it?
dc_msg::dc_msg_set_text(msg, text)
}
#[no_mangle]
pub unsafe extern "C" fn dc_msg_set_file(
msg: *mut dc_msg::dc_msg_t,
msg: *mut dc_msg_t,
file: *mut libc::c_char,
filemime: *mut libc::c_char,
) {
assert!(!msg.is_null());
let msg = &mut *msg;
dc_msg::dc_msg_set_file(msg, file, filemime)
}
#[no_mangle]
pub unsafe extern "C" fn dc_msg_set_dimension(
msg: *mut dc_msg::dc_msg_t,
msg: *mut dc_msg_t,
width: libc::c_int,
height: libc::c_int,
) {
assert!(!msg.is_null());
let msg = &mut *msg;
dc_msg::dc_msg_set_dimension(msg, width, height)
}
#[no_mangle]
pub unsafe extern "C" fn dc_msg_set_duration(msg: *mut dc_msg::dc_msg_t, duration: libc::c_int) {
pub unsafe extern "C" fn dc_msg_set_duration(msg: *mut dc_msg_t, duration: libc::c_int) {
assert!(!msg.is_null());
let msg = &mut *msg;
dc_msg::dc_msg_set_duration(msg, duration)
}
#[no_mangle]
pub unsafe extern "C" fn dc_msg_set_location(
msg: *mut dc_msg::dc_msg_t,
msg: *mut dc_msg_t,
latitude: libc::c_double,
longitude: libc::c_double,
) {
assert!(!msg.is_null());
let msg = &mut *msg;
dc_msg::dc_msg_set_location(msg, latitude, longitude)
}
#[no_mangle]
pub unsafe extern "C" fn dc_msg_latefiling_mediasize(
msg: *mut dc_msg::dc_msg_t,
msg: *mut dc_msg_t,
width: libc::c_int,
height: libc::c_int,
duration: libc::c_int,
) {
assert!(!msg.is_null());
let msg = &mut *msg;
dc_msg::dc_msg_latefiling_mediasize(msg, width, height, duration)
}
@@ -1869,7 +1898,7 @@ fn as_opt_str<'a>(s: *const libc::c_char) -> Option<&'a str> {
Some(dc_tools::as_str(s))
}
pub trait ResultExt<T: Default> {
pub trait ResultExt<T> {
fn unwrap_or_log_default(self, context: &context::Context, message: &str) -> T;
fn log_err(&self, context: &context::Context, message: &str);
}
@@ -1898,3 +1927,16 @@ unsafe fn strdup_opt(s: Option<impl AsRef<str>>) -> *mut libc::c_char {
None => ptr::null_mut(),
}
}
pub trait ResultNullableExt<T> {
fn into_raw(self) -> *mut T;
}
impl<T, E> ResultNullableExt<T> for Result<T, E> {
fn into_raw(self) -> *mut T {
match self {
Ok(t) => Box::into_raw(Box::new(t)),
Err(_) => ptr::null_mut(),
}
}
}

View File

@@ -13,6 +13,7 @@ use deltachat::dc_location::*;
use deltachat::dc_msg::*;
use deltachat::dc_receive_imf::*;
use deltachat::dc_tools::*;
use deltachat::error::Error;
use deltachat::job::*;
use deltachat::lot::LotState;
use deltachat::peerstate::*;
@@ -210,7 +211,7 @@ unsafe fn poke_spec(context: &Context, spec: *const libc::c_char) -> libc::c_int
success
}
unsafe fn log_msg(context: &Context, prefix: impl AsRef<str>, msg: *mut dc_msg_t) {
unsafe fn log_msg(context: &Context, prefix: impl AsRef<str>, msg: &Message) {
let contact = Contact::get_by_id(context, dc_msg_get_from_id(msg)).expect("invalid contact");
let contact_name = contact.get_name();
let contact_id = contact.get_id();
@@ -260,7 +261,7 @@ unsafe fn log_msg(context: &Context, prefix: impl AsRef<str>, msg: *mut dc_msg_t
free(msgtext as *mut libc::c_void);
}
unsafe fn log_msglist(context: &Context, msglist: &Vec<u32>) {
unsafe fn log_msglist(context: &Context, msglist: &Vec<u32>) -> Result<(), Error> {
let mut lines_out = 0;
for &msg_id in msglist {
if msg_id == 9 as libc::c_uint {
@@ -279,9 +280,8 @@ unsafe fn log_msglist(context: &Context, msglist: &Vec<u32>) {
);
lines_out += 1
}
let msg = dc_get_msg(context, msg_id);
log_msg(context, "Msg", msg);
dc_msg_unref(msg);
let msg = dc_get_msg(context, msg_id)?;
log_msg(context, "Msg", &msg);
}
}
if lines_out > 0 {
@@ -290,6 +290,7 @@ unsafe fn log_msglist(context: &Context, msglist: &Vec<u32>) {
0, "--------------------------------------------------------------------------------"
);
}
Ok(())
}
unsafe fn log_contactlist(context: &Context, contacts: &Vec<u32>) {
@@ -496,9 +497,9 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
"get-setupcodebegin" => {
ensure!(!arg1.is_empty(), "Argument <msg-id> missing.");
let msg_id: u32 = arg1.parse()?;
let msg: *mut dc_msg_t = dc_get_msg(context, msg_id);
if dc_msg_is_setupmessage(msg) {
let setupcodebegin = dc_msg_get_setupcodebegin(msg);
let msg = dc_get_msg(context, msg_id)?;
if dc_msg_is_setupmessage(&msg) {
let setupcodebegin = dc_msg_get_setupcodebegin(&msg);
println!(
"The setup code for setup message Msg#{} starts with: {}",
msg_id,
@@ -508,7 +509,6 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
} else {
bail!("Msg#{} is no setup message.", msg_id,);
}
dc_msg_unref(msg);
}
"continue-key-transfer" => {
ensure!(
@@ -688,12 +688,11 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
""
},
);
log_msglist(context, &msglist);
let draft = chat::get_draft(context, sel_chat.get_id());
if !draft.is_null() {
log_msg(context, "Draft", draft);
dc_msg_unref(draft);
log_msglist(context, &msglist)?;
if let Ok(draft) = chat::get_draft(context, sel_chat.get_id()) {
log_msg(context, "Draft", &draft);
}
println!(
"{} messages.",
chat::get_msg_cnt(context, sel_chat.get_id())
@@ -861,7 +860,7 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
ensure!(sel_chat.is_some(), "No chat selected.");
ensure!(!arg1.is_empty() && !arg2.is_empty(), "No file given.");
let msg_0 = dc_msg_new(
let mut msg = dc_msg_new(
context,
if arg0 == "sendimage" {
Viewtype::Image
@@ -869,10 +868,9 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
Viewtype::File
},
);
dc_msg_set_file(msg_0, arg1_c, 0 as *const libc::c_char);
dc_msg_set_text(msg_0, arg2_c);
chat::send_msg(context, sel_chat.as_ref().unwrap().get_id(), msg_0)?;
dc_msg_unref(msg_0);
dc_msg_set_file(&mut msg, arg1_c, 0 as *const libc::c_char);
dc_msg_set_text(&mut msg, arg2_c);
chat::send_msg(context, sel_chat.as_ref().unwrap().get_id(), &mut msg)?;
}
"listmsgs" => {
ensure!(!arg1.is_empty(), "Argument <query> missing.");
@@ -885,24 +883,23 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
let msglist = dc_search_msgs(context, chat, arg1_c);
log_msglist(context, &msglist);
log_msglist(context, &msglist)?;
println!("{} messages.", msglist.len());
}
"draft" => {
ensure!(sel_chat.is_some(), "No chat selected.");
if !arg1.is_empty() {
let draft_0 = dc_msg_new(context, Viewtype::Text);
dc_msg_set_text(draft_0, arg1_c);
chat::set_draft(context, sel_chat.as_ref().unwrap().get_id(), draft_0);
dc_msg_unref(draft_0);
println!("Draft saved.");
} else {
let mut draft = dc_msg_new(context, Viewtype::Text);
dc_msg_set_text(&mut draft, arg1_c);
chat::set_draft(
context,
sel_chat.as_ref().unwrap().get_id(),
0 as *mut dc_msg_t,
Some(&mut draft),
);
println!("Draft saved.");
} else {
chat::set_draft(context, sel_chat.as_ref().unwrap().get_id(), None);
println!("Draft deleted.");
}
}
@@ -949,7 +946,7 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
"listfresh" => {
let msglist = dc_get_fresh_msgs(context);
log_msglist(context, &msglist);
log_msglist(context, &msglist)?;
print!("{} fresh messages.", msglist.len());
}
"forward" => {

View File

@@ -272,7 +272,7 @@ impl<'a> Chat<'a> {
unsafe fn prepare_msg_raw(
&mut self,
context: &Context,
msg: *mut dc_msg_t,
msg: &mut Message,
timestamp: i64,
) -> Result<u32, Error> {
let mut do_guarantee_e2ee: libc::c_int;
@@ -351,11 +351,7 @@ impl<'a> Chat<'a> {
.get_config_int(context, "e2ee_enabled")
.unwrap_or_else(|| 1);
if 0 != e2ee_enabled
&& (*msg)
.param
.get_int(Param::ForcePlaintext)
.unwrap_or_default()
== 0
&& msg.param.get_int(Param::ForcePlaintext).unwrap_or_default() == 0
{
let mut can_encrypt = 1;
let mut all_mutual = 1;
@@ -409,9 +405,9 @@ impl<'a> Chat<'a> {
}
}
if 0 != do_guarantee_e2ee {
(*msg).param.set_int(Param::GuranteeE2ee, 1);
msg.param.set_int(Param::GuranteeE2ee, 1);
}
(*msg).param.remove(Param::ErroneousE2ee);
msg.param.remove(Param::ErroneousE2ee);
if !self.is_self_talk()
&& self
.get_parent_mime_headers(
@@ -466,7 +462,7 @@ impl<'a> Chat<'a> {
// add independent location to database
if (*msg).param.exists(Param::SetLatitude) {
if msg.param.exists(Param::SetLatitude) {
if sql::execute(
context,
&context.sql,
@@ -477,14 +473,8 @@ impl<'a> Chat<'a> {
timestamp,
DC_CONTACT_ID_SELF as i32,
self.id as i32,
(*msg)
.param
.get_float(Param::SetLatitude)
.unwrap_or_default(),
(*msg)
.param
.get_float(Param::SetLongitude)
.unwrap_or_default(),
msg.param.get_float(Param::SetLatitude).unwrap_or_default(),
msg.param.get_float(Param::SetLongitude).unwrap_or_default(),
],
)
.is_ok()
@@ -513,11 +503,11 @@ impl<'a> Chat<'a> {
1i32,
to_id as i32,
timestamp,
(*msg).type_0,
(*msg).state,
(*msg).text,
(*msg).param.to_string(),
(*msg).hidden,
msg.type_0,
msg.state,
msg.text,
msg.param.to_string(),
msg.hidden,
to_string(new_in_reply_to),
to_string(new_references),
location_id as i32,
@@ -574,27 +564,20 @@ impl<'a> Chat<'a> {
pub fn create_by_msg_id(context: &Context, msg_id: u32) -> Result<u32, Error> {
let mut chat_id = 0;
let mut send_event = false;
let msg = unsafe { dc_msg_new_untyped(context) };
if dc_msg_load_from_db(msg, context, msg_id) {
if let Ok(chat) = Chat::load_from_db(context, unsafe { (*msg).chat_id }) {
if let Ok(msg) = dc_msg_load_from_db(context, msg_id) {
if let Ok(chat) = Chat::load_from_db(context, msg.chat_id) {
if chat.id > DC_CHAT_ID_LAST_SPECIAL as u32 {
chat_id = chat.id;
if chat.blocked != Blocked::Not {
unblock(context, chat.id);
send_event = true;
}
Contact::scaleup_origin_by_id(
context,
unsafe { (*msg).from_id },
Origin::CreateChat,
);
Contact::scaleup_origin_by_id(context, msg.from_id, Origin::CreateChat);
}
}
}
unsafe { dc_msg_unref(msg) };
if send_event {
context.call_cb(Event::MSGS_CHANGED, 0, 0);
}
@@ -721,20 +704,19 @@ pub fn get_by_contact_id(context: &Context, contact_id: u32) -> Result<u32, Erro
pub fn prepare_msg<'a>(
context: &'a Context,
chat_id: u32,
mut msg: *mut dc_msg_t<'a>,
msg: &mut Message<'a>,
) -> Result<u32, Error> {
ensure!(!msg.is_null(), "No message provided");
ensure!(
chat_id > DC_CHAT_ID_LAST_SPECIAL as u32,
"Cannot prepare message for special chat"
);
unsafe { (*msg).state = MessageState::OutPreparing };
msg.state = MessageState::OutPreparing;
let msg_id = prepare_msg_common(context, chat_id, msg)?;
context.call_cb(
Event::MSGS_CHANGED,
unsafe { (*msg).chat_id as uintptr_t },
unsafe { (*msg).id as uintptr_t },
msg.chat_id as uintptr_t,
msg.id as uintptr_t,
);
Ok(msg_id)
@@ -755,12 +737,8 @@ pub fn msgtype_has_file(msgtype: Viewtype) -> bool {
fn prepare_msg_common<'a>(
context: &'a Context,
chat_id: u32,
msg: *mut dc_msg_t<'a>,
msg: &mut Message<'a>,
) -> Result<u32, Error> {
ensure!(!msg.is_null(), "No message provided");
let msg = unsafe { &mut *msg };
msg.id = 0;
msg.context = context;
@@ -881,39 +859,37 @@ pub fn unarchive(context: &Context, chat_id: u32) -> Result<(), Error> {
pub unsafe fn send_msg<'a>(
context: &'a Context,
chat_id: u32,
msg: *mut dc_msg_t<'a>,
msg: &mut Message<'a>,
) -> Result<u32, Error> {
ensure!(!msg.is_null(), "Invalid message");
if (*msg).state != MessageState::OutPreparing {
if msg.state != MessageState::OutPreparing {
// automatically prepare normal messages
prepare_msg_common(context, chat_id, msg)?;
} else {
// update message state of separately prepared messages
ensure!(
chat_id == 0 || chat_id == (*msg).chat_id,
chat_id == 0 || chat_id == msg.chat_id,
"Inconsistent chat ID"
);
dc_update_msg_state(context, (*msg).id, MessageState::OutPending);
dc_update_msg_state(context, msg.id, MessageState::OutPending);
}
ensure!(
job_send_msg(context, (*msg).id) != 0,
job_send_msg(context, msg.id) != 0,
"Failed to initiate send job"
);
context.call_cb(
Event::MSGS_CHANGED,
(*msg).chat_id as uintptr_t,
(*msg).id as uintptr_t,
msg.chat_id as uintptr_t,
msg.id as uintptr_t,
);
if (*msg).param.exists(Param::SetLatitude) {
if msg.param.exists(Param::SetLatitude) {
context.call_cb(Event::LOCATION_CHANGED, DC_CONTACT_ID_SELF, 0);
}
if 0 == chat_id {
let forwards = (*msg).param.get(Param::PrepForwards);
let forwards = msg.param.get(Param::PrepForwards);
if let Some(forwards) = forwards {
for forward in forwards.split(' ') {
let id: i32 = forward.parse().unwrap_or_default();
@@ -921,20 +897,18 @@ pub unsafe fn send_msg<'a>(
// avoid hanging if user tampers with db
break;
} else {
let copy = dc_get_msg(context, id as u32);
if !copy.is_null() {
if let Ok(mut copy) = dc_get_msg(context, id as u32) {
// TODO: handle cleanup and return early instead
send_msg(context, 0, copy).unwrap();
send_msg(context, 0, &mut copy).unwrap();
}
dc_msg_unref(copy);
}
}
(*msg).param.remove(Param::PrepForwards);
msg.param.remove(Param::PrepForwards);
dc_msg_save_param_to_disk(msg);
}
}
Ok((*msg).id)
Ok(msg.id)
}
pub unsafe fn send_text_msg(
@@ -949,11 +923,12 @@ pub unsafe fn send_text_msg(
);
let mut msg = dc_msg_new(context, Viewtype::Text);
(*msg).text = Some(text_to_send);
send_msg(context, chat_id, msg)
msg.text = Some(text_to_send);
send_msg(context, chat_id, &mut msg)
}
pub unsafe fn set_draft(context: &Context, chat_id: u32, msg: *mut dc_msg_t) {
// passing `None` as message jsut deletes the draft
pub unsafe fn set_draft(context: &Context, chat_id: u32, msg: Option<&mut Message>) {
if chat_id <= DC_CHAT_ID_LAST_SPECIAL as u32 {
return;
}
@@ -962,31 +937,32 @@ pub unsafe fn set_draft(context: &Context, chat_id: u32, msg: *mut dc_msg_t) {
};
}
// similar to as dc_set_draft() but does not emit an event
#[allow(non_snake_case)]
unsafe fn set_draft_raw(context: &Context, chat_id: u32, msg: *mut dc_msg_t) -> bool {
unsafe fn set_draft_raw(context: &Context, chat_id: u32, mut msg: Option<&mut Message>) -> bool {
let mut OK_TO_CONTINUE = true;
// similar to as dc_set_draft() but does not emit an event
let prev_draft_msg_id: u32;
let mut sth_changed = false;
prev_draft_msg_id = get_draft_msg_id(context, chat_id);
let prev_draft_msg_id = get_draft_msg_id(context, chat_id);
if 0 != prev_draft_msg_id {
dc_delete_msg_from_db(context, prev_draft_msg_id);
sth_changed = true;
}
// save new draft
if !msg.is_null() {
if (*msg).type_0 == Viewtype::Text {
OK_TO_CONTINUE = (*msg).text.as_ref().map_or(false, |s| !s.is_empty());
} else if msgtype_has_file((*msg).type_0) {
if let Some(path_filename) = (*msg).param.get(Param::File) {
if let Some(ref mut msg) = msg {
// save new draft
if msg.type_0 == Viewtype::Text {
OK_TO_CONTINUE = msg.text.as_ref().map_or(false, |s| !s.is_empty());
} else if msgtype_has_file(msg.type_0) {
if let Some(path_filename) = msg.param.get(Param::File) {
let mut path_filename = path_filename.to_string();
if 0 != dc_msg_is_increation(msg) && !dc_is_blobdir_path(context, &path_filename) {
OK_TO_CONTINUE = false;
} else if !dc_make_rel_and_copy(context, &mut path_filename) {
OK_TO_CONTINUE = false;
} else {
(*msg).param.set(Param::File, path_filename);
msg.param.set(Param::File, path_filename);
}
}
} else {
@@ -1002,10 +978,10 @@ unsafe fn set_draft_raw(context: &Context, chat_id: u32, msg: *mut dc_msg_t) ->
chat_id as i32,
1,
time(),
(*msg).type_0,
msg.type_0,
MessageState::OutDraft,
(*msg).text.as_ref().map(String::as_str).unwrap_or(""),
(*msg).param.to_string(),
msg.text.as_ref().map(String::as_str).unwrap_or(""),
msg.param.to_string(),
1,
],
)
@@ -1015,7 +991,6 @@ unsafe fn set_draft_raw(context: &Context, chat_id: u32, msg: *mut dc_msg_t) ->
}
}
}
sth_changed
}
@@ -1031,21 +1006,12 @@ fn get_draft_msg_id(context: &Context, chat_id: u32) -> u32 {
.unwrap_or_default() as u32
}
pub unsafe fn get_draft(context: &Context, chat_id: u32) -> *mut dc_msg_t {
if chat_id <= DC_CHAT_ID_LAST_SPECIAL as u32 {
return ptr::null_mut();
}
pub unsafe fn get_draft(context: &Context, chat_id: u32) -> Result<Message, Error> {
ensure!(chat_id > DC_CHAT_ID_LAST_SPECIAL as u32, "Invalid chat ID");
let draft_msg_id = get_draft_msg_id(context, chat_id);
if draft_msg_id == 0 {
return ptr::null_mut();
}
let draft_msg = dc_msg_new_untyped(context);
if !dc_msg_load_from_db(draft_msg, context, draft_msg_id) {
dc_msg_unref(draft_msg);
return ptr::null_mut();
}
ensure!(draft_msg_id != 0, "Invalid draft message ID");
draft_msg
dc_msg_load_from_db(context, draft_msg_id)
}
pub fn get_chat_msgs(context: &Context, chat_id: u32, flags: u32, marker1before: u32) -> Vec<u32> {
@@ -1235,15 +1201,15 @@ pub unsafe fn get_next_media(
msg_type3: Viewtype,
) -> u32 {
let mut ret = 0;
let msg: *mut dc_msg_t = dc_msg_new_untyped(context);
if dc_msg_load_from_db(msg, context, curr_msg_id) {
if let Ok(msg) = dc_msg_load_from_db(context, curr_msg_id) {
let list = get_chat_media(
context,
(*msg).chat_id,
msg.chat_id,
if msg_type != Viewtype::Unknown {
msg_type
} else {
(*msg).type_0
msg.type_0
},
msg_type2,
msg_type3,
@@ -1263,8 +1229,6 @@ pub unsafe fn get_next_media(
}
}
}
dc_msg_unref(msg);
ret
}
@@ -1400,10 +1364,9 @@ pub unsafe fn create_group_chat(
if chat_id != 0 {
if 0 != add_to_chat_contacts_table(context, chat_id, 1) {
let draft_msg = dc_msg_new(context, Viewtype::Text);
dc_msg_set_text(draft_msg, draft_txt.as_ptr());
set_draft_raw(context, chat_id, draft_msg);
dc_msg_unref(draft_msg);
let mut draft_msg = dc_msg_new(context, Viewtype::Text);
dc_msg_set_text(&mut draft_msg, draft_txt.as_ptr());
set_draft_raw(context, chat_id, Some(&mut draft_msg));
}
context.call_cb(Event::MSGS_CHANGED, 0 as uintptr_t, 0 as uintptr_t);
@@ -1506,21 +1469,21 @@ pub unsafe fn add_contact_to_chat_ex(
}
if OK_TO_CONTINUE {
if chat.param.get_int(Param::Unpromoted).unwrap_or_default() == 0 {
(*msg).type_0 = Viewtype::Text;
(*msg).text = Some(context.stock_system_msg(
msg.type_0 = Viewtype::Text;
msg.text = Some(context.stock_system_msg(
StockMessage::MsgAddMember,
contact.get_addr(),
"",
DC_CONTACT_ID_SELF as u32,
));
(*msg).param.set_int(Param::Cmd, 4);
(*msg).param.set(Param::Arg, contact.get_addr());
(*msg).param.set_int(Param::Arg2, flags);
(*msg).id = send_msg(context, chat_id, msg).unwrap_or_default();
msg.param.set_int(Param::Cmd, 4);
msg.param.set(Param::Arg, contact.get_addr());
msg.param.set_int(Param::Arg2, flags);
msg.id = send_msg(context, chat_id, &mut msg).unwrap_or_default();
context.call_cb(
Event::MSGS_CHANGED,
chat_id as uintptr_t,
(*msg).id as uintptr_t,
msg.id as uintptr_t,
);
}
context.call_cb(Event::MSGS_CHANGED, chat_id as uintptr_t, 0 as uintptr_t);
@@ -1530,8 +1493,6 @@ pub unsafe fn add_contact_to_chat_ex(
}
}
dc_msg_unref(msg);
success
}
@@ -1619,30 +1580,30 @@ pub unsafe fn remove_contact_from_chat(
/* we should respect this - whatever we send to the group, it gets discarded anyway! */
if let Ok(contact) = Contact::get_by_id(context, contact_id) {
if chat.param.get_int(Param::Unpromoted).unwrap_or_default() == 0 {
(*msg).type_0 = Viewtype::Text;
msg.type_0 = Viewtype::Text;
if contact.id == DC_CONTACT_ID_SELF as u32 {
set_group_explicitly_left(context, chat.grpid).unwrap();
(*msg).text = Some(context.stock_system_msg(
msg.text = Some(context.stock_system_msg(
StockMessage::MsgGroupLeft,
"",
"",
DC_CONTACT_ID_SELF as u32,
));
} else {
(*msg).text = Some(context.stock_system_msg(
msg.text = Some(context.stock_system_msg(
StockMessage::MsgDelMember,
contact.get_addr(),
"",
DC_CONTACT_ID_SELF as u32,
));
}
(*msg).param.set_int(Param::Cmd, 5);
(*msg).param.set(Param::Arg, contact.get_addr());
(*msg).id = send_msg(context, chat_id, msg).unwrap_or_default();
msg.param.set_int(Param::Cmd, 5);
msg.param.set(Param::Arg, contact.get_addr());
msg.id = send_msg(context, chat_id, &mut msg).unwrap_or_default();
context.call_cb(
Event::MSGS_CHANGED,
chat_id as uintptr_t,
(*msg).id as uintptr_t,
msg.id as uintptr_t,
);
}
}
@@ -1660,8 +1621,6 @@ pub unsafe fn remove_contact_from_chat(
}
}
dc_msg_unref(msg);
if !success {
bail!("Failed to remove contact");
}
@@ -1728,22 +1687,22 @@ pub unsafe fn set_chat_name(
.is_ok()
{
if chat.param.get_int(Param::Unpromoted).unwrap_or_default() == 0 {
(*msg).type_0 = Viewtype::Text;
(*msg).text = Some(context.stock_system_msg(
msg.type_0 = Viewtype::Text;
msg.text = Some(context.stock_system_msg(
StockMessage::MsgGrpName,
&chat.name,
new_name.as_ref(),
DC_CONTACT_ID_SELF as u32,
));
(*msg).param.set_int(Param::Cmd, 2);
msg.param.set_int(Param::Cmd, 2);
if !chat.name.is_empty() {
(*msg).param.set(Param::Arg, &chat.name);
msg.param.set(Param::Arg, &chat.name);
}
(*msg).id = send_msg(context, chat_id, msg).unwrap_or_default();
msg.id = send_msg(context, chat_id, &mut msg).unwrap_or_default();
context.call_cb(
Event::MSGS_CHANGED,
chat_id as uintptr_t,
(*msg).id as uintptr_t,
msg.id as uintptr_t,
);
}
context.call_cb(
@@ -1756,8 +1715,6 @@ pub unsafe fn set_chat_name(
}
}
dc_msg_unref(msg);
if !success {
bail!("Failed to set name");
}
@@ -1806,12 +1763,12 @@ pub unsafe fn set_chat_profile_image(
}
if chat.update_param().is_ok() {
if chat.param.get_int(Param::Unpromoted).unwrap_or_default() == 0 {
(*msg).param.set_int(Param::Cmd, 3);
msg.param.set_int(Param::Cmd, 3);
if let Some(ref new_image_rel) = new_image_rel {
(*msg).param.set(Param::Arg, new_image_rel);
msg.param.set(Param::Arg, new_image_rel);
}
(*msg).type_0 = Viewtype::Text;
(*msg).text = Some(context.stock_system_msg(
msg.type_0 = Viewtype::Text;
msg.text = Some(context.stock_system_msg(
if new_image_rel.is_some() {
StockMessage::MsgGrpImgChanged
} else {
@@ -1821,11 +1778,11 @@ pub unsafe fn set_chat_profile_image(
"",
DC_CONTACT_ID_SELF as u32,
));
(*msg).id = send_msg(context, chat_id, msg).unwrap_or_default();
msg.id = send_msg(context, chat_id, &mut msg).unwrap_or_default();
context.call_cb(
Event::MSGS_CHANGED,
chat_id as uintptr_t,
(*msg).id as uintptr_t,
msg.id as uintptr_t,
);
}
context.call_cb(
@@ -1838,8 +1795,6 @@ pub unsafe fn set_chat_profile_image(
}
}
dc_msg_unref(msg);
if !success {
bail!("Failed to set profile image");
}
@@ -1857,7 +1812,6 @@ pub unsafe fn forward_msgs(
return;
}
let msg = dc_msg_new_untyped(context);
let mut created_db_entries = Vec::new();
let mut curr_timestamp: i64;
@@ -1887,45 +1841,45 @@ pub unsafe fn forward_msgs(
for id in ids {
let src_msg_id = id;
if !dc_msg_load_from_db(msg, context, src_msg_id as u32) {
let msg = dc_msg_load_from_db(context, src_msg_id as u32);
if msg.is_err() {
break;
}
let original_param = (*msg).param.clone();
if (*msg).from_id != DC_CONTACT_ID_SELF as u32 {
(*msg).param.set_int(Param::Forwarded, 1);
let mut msg = msg.unwrap();
let original_param = msg.param.clone();
if msg.from_id != DC_CONTACT_ID_SELF as u32 {
msg.param.set_int(Param::Forwarded, 1);
}
(*msg).param.remove(Param::GuranteeE2ee);
(*msg).param.remove(Param::ForcePlaintext);
(*msg).param.remove(Param::Cmd);
msg.param.remove(Param::GuranteeE2ee);
msg.param.remove(Param::ForcePlaintext);
msg.param.remove(Param::Cmd);
let new_msg_id: u32;
if (*msg).state == MessageState::OutPreparing {
if msg.state == MessageState::OutPreparing {
let fresh9 = curr_timestamp;
curr_timestamp = curr_timestamp + 1;
new_msg_id = chat
.prepare_msg_raw(context, msg, fresh9)
.prepare_msg_raw(context, &mut msg, fresh9)
.unwrap_or_default();
let save_param = (*msg).param.clone();
(*msg).param = original_param;
(*msg).id = src_msg_id as u32;
let save_param = msg.param.clone();
msg.param = original_param;
msg.id = src_msg_id as u32;
if let Some(old_fwd) = (*msg).param.get(Param::PrepForwards) {
if let Some(old_fwd) = msg.param.get(Param::PrepForwards) {
let new_fwd = format!("{} {}", old_fwd, new_msg_id);
(*msg).param.set(Param::PrepForwards, new_fwd);
msg.param.set(Param::PrepForwards, new_fwd);
} else {
(*msg)
.param
.set(Param::PrepForwards, new_msg_id.to_string());
msg.param.set(Param::PrepForwards, new_msg_id.to_string());
}
dc_msg_save_param_to_disk(msg);
(*msg).param = save_param;
dc_msg_save_param_to_disk(&mut msg);
msg.param = save_param;
} else {
(*msg).state = MessageState::OutPending;
msg.state = MessageState::OutPending;
let fresh10 = curr_timestamp;
curr_timestamp = curr_timestamp + 1;
new_msg_id = chat
.prepare_msg_raw(context, msg, fresh10)
.prepare_msg_raw(context, &mut msg, fresh10)
.unwrap_or_default();
job_send_msg(context, new_msg_id);
}
@@ -1941,7 +1895,6 @@ pub unsafe fn forward_msgs(
created_db_entries[i + 1] as uintptr_t,
);
}
dc_msg_unref(msg);
}
pub fn get_chat_contact_cnt(context: &Context, chat_id: u32) -> libc::c_int {

View File

@@ -275,29 +275,36 @@ impl<'a> Chatlist<'a> {
let mut lastcontact = None;
let lastmsg = if 0 != lastmsg_id {
let lastmsg = dc_msg_new_untyped(self.context);
dc_msg_load_from_db(lastmsg, self.context, lastmsg_id);
if let Ok(lastmsg) = dc_msg_load_from_db(self.context, lastmsg_id) {
if lastmsg.from_id != 1 as libc::c_uint
&& (chat.typ == Chattype::Group || chat.typ == Chattype::VerifiedGroup)
{
lastcontact = Contact::load_from_db(self.context, lastmsg.from_id).ok();
}
if (*lastmsg).from_id != 1 as libc::c_uint
&& (chat.typ == Chattype::Group || chat.typ == Chattype::VerifiedGroup)
{
lastcontact = Contact::load_from_db(self.context, (*lastmsg).from_id).ok();
Some(lastmsg)
} else {
None
}
lastmsg
} else {
std::ptr::null_mut()
None
};
if chat.id == DC_CHAT_ID_ARCHIVED_LINK as u32 {
ret.text2 = None;
} else if lastmsg.is_null() || (*lastmsg).from_id == DC_CONTACT_ID_UNDEFINED as u32 {
} else if lastmsg.is_none()
|| lastmsg.as_ref().unwrap().from_id == DC_CONTACT_ID_UNDEFINED as u32
{
ret.text2 = Some(self.context.stock_str(StockMessage::NoMessages).to_string());
} else {
ret.fill(lastmsg, chat, lastcontact.as_ref(), self.context);
ret.fill(
&mut lastmsg.unwrap(),
chat,
lastcontact.as_ref(),
self.context,
);
}
dc_msg_unref(lastmsg);
ret
}
}

View File

@@ -102,7 +102,7 @@ pub unsafe fn dc_imex_has_backup(
pub unsafe fn dc_initiate_key_transfer(context: &Context) -> *mut libc::c_char {
let mut setup_file_name: *mut libc::c_char = ptr::null_mut();
let mut msg: *mut dc_msg_t = ptr::null_mut();
let mut msg: Message;
if dc_alloc_ongoing(context) == 0 {
return std::ptr::null_mut();
}
@@ -140,14 +140,13 @@ pub unsafe fn dc_initiate_key_transfer(context: &Context) -> *mut libc::c_char {
{
if let Ok(chat_id) = chat::create_by_contact_id(context, 1) {
msg = dc_msg_new_untyped(context);
(*msg).type_0 = Viewtype::File;
(*msg).param.set(Param::File, as_str(setup_file_name));
msg.type_0 = Viewtype::File;
msg.param.set(Param::File, as_str(setup_file_name));
(*msg)
.param
msg.param
.set(Param::MimeType, "application/autocrypt-setup");
(*msg).param.set_int(Param::Cmd, 6);
(*msg).param.set_int(Param::ForcePlaintext, 2);
msg.param.set_int(Param::Cmd, 6);
msg.param.set_int(Param::ForcePlaintext, 2);
if !context
.running_state
@@ -156,9 +155,7 @@ pub unsafe fn dc_initiate_key_transfer(context: &Context) -> *mut libc::c_char {
.unwrap()
.shall_stop_ongoing
{
if let Ok(msg_id) = chat::send_msg(context, chat_id, msg) {
dc_msg_unref(msg);
msg = ptr::null_mut();
if let Ok(msg_id) = chat::send_msg(context, chat_id, &mut msg) {
info!(context, 0, "Wait for setup message being sent ...",);
loop {
if context
@@ -171,13 +168,12 @@ pub unsafe fn dc_initiate_key_transfer(context: &Context) -> *mut libc::c_char {
break;
}
std::thread::sleep(std::time::Duration::from_secs(1));
msg = dc_get_msg(context, msg_id);
if 0 != dc_msg_is_sent(msg) {
info!(context, 0, "... setup message sent.",);
break;
if let Ok(msg) = dc_get_msg(context, msg_id) {
if 0 != dc_msg_is_sent(&msg) {
info!(context, 0, "... setup message sent.",);
break;
}
}
dc_msg_unref(msg);
msg = 0 as *mut dc_msg_t
}
}
}
@@ -187,7 +183,6 @@ pub unsafe fn dc_initiate_key_transfer(context: &Context) -> *mut libc::c_char {
}
}
free(setup_file_name as *mut libc::c_void);
dc_msg_unref(msg);
dc_free_ongoing(context);
setup_code.strdup()
@@ -284,18 +279,17 @@ pub unsafe fn dc_continue_key_transfer(
setup_code: *const libc::c_char,
) -> libc::c_int {
let mut success: libc::c_int = 0i32;
let mut msg: *mut dc_msg_t = ptr::null_mut();
let mut filename: *mut libc::c_char = ptr::null_mut();
let mut filecontent: *mut libc::c_char = ptr::null_mut();
let mut filebytes: size_t = 0i32 as size_t;
let mut armored_key: *mut libc::c_char = ptr::null_mut();
let mut norm_sc: *mut libc::c_char = ptr::null_mut();
if !(msg_id <= 9i32 as libc::c_uint || setup_code.is_null()) {
msg = dc_get_msg(context, msg_id);
if msg.is_null()
|| !dc_msg_is_setupmessage(msg)
let msg = dc_get_msg(context, msg_id);
if msg.is_err()
|| !dc_msg_is_setupmessage(msg.as_ref().unwrap())
|| {
filename = dc_msg_get_file(msg);
filename = dc_msg_get_file(msg.as_ref().unwrap());
filename.is_null()
}
|| *filename.offset(0isize) as libc::c_int == 0i32
@@ -331,7 +325,6 @@ pub unsafe fn dc_continue_key_transfer(
free(armored_key as *mut libc::c_void);
free(filecontent as *mut libc::c_void);
free(filename as *mut libc::c_void);
dc_msg_unref(msg);
free(norm_sc as *mut libc::c_void);
success

View File

@@ -70,7 +70,7 @@ impl dc_kml_t {
// location streaming
pub unsafe fn dc_send_locations_to_chat(context: &Context, chat_id: uint32_t, seconds: i64) {
let now = time();
let mut msg: *mut dc_msg_t = 0 as *mut dc_msg_t;
let mut msg: Message;
let is_sending_locations_before: bool;
if !(seconds < 0 || chat_id <= 9i32 as libc::c_uint) {
is_sending_locations_before = dc_is_sending_locations_to_chat(context, chat_id);
@@ -91,10 +91,10 @@ pub unsafe fn dc_send_locations_to_chat(context: &Context, chat_id: uint32_t, se
{
if 0 != seconds && !is_sending_locations_before {
msg = dc_msg_new(context, Viewtype::Text);
(*msg).text =
msg.text =
Some(context.stock_system_msg(StockMessage::MsgLocationEnabled, "", "", 0));
(*msg).param.set_int(Param::Cmd, 8);
chat::send_msg(context, chat_id, msg).unwrap();
msg.param.set_int(Param::Cmd, 8);
chat::send_msg(context, chat_id, &mut msg).unwrap();
} else if 0 == seconds && is_sending_locations_before {
let stock_str =
context.stock_system_msg(StockMessage::MsgLocationDisabled, "", "", 0);
@@ -117,7 +117,6 @@ pub unsafe fn dc_send_locations_to_chat(context: &Context, chat_id: uint32_t, se
}
}
}
dc_msg_unref(msg);
}
/*******************************************************************************
@@ -680,11 +679,10 @@ pub unsafe fn dc_job_do_DC_JOB_MAYBE_SEND_LOCATIONS(context: &Context, _job: &Jo
// (might not be 100%, however, as positions are sent combined later
// and dc_set_location() is typically called periodically, this is ok)
let mut msg = dc_msg_new(context, Viewtype::Text);
(*msg).hidden = 1;
(*msg).param.set_int(Param::Cmd, 9);
msg.hidden = true;
msg.param.set_int(Param::Cmd, 9);
// TODO: handle cleanup on error
chat::send_msg(context, chat_id as u32, msg).unwrap();
dc_msg_unref(msg);
chat::send_msg(context, chat_id as u32, &mut msg).unwrap();
}
Ok(())
},

File diff suppressed because it is too large Load Diff

View File

@@ -18,29 +18,27 @@ pub unsafe fn dc_do_heuristics_moves(context: &Context, folder: &str, msg_id: u3
return;
}
let msg = dc_msg_new_load(context, msg_id);
if dc_msg_is_setupmessage(msg) {
// do not move setup messages;
// there may be a non-delta device that wants to handle it
dc_msg_unref(msg);
return;
}
if let Ok(msg) = dc_msg_new_load(context, msg_id) {
if dc_msg_is_setupmessage(&msg) {
// do not move setup messages;
// there may be a non-delta device that wants to handle it
return;
}
if dc_is_mvbox(context, folder) {
dc_update_msg_move_state(context, (*msg).rfc724_mid, MoveState::Stay);
}
if dc_is_mvbox(context, folder) {
dc_update_msg_move_state(context, msg.rfc724_mid, MoveState::Stay);
}
// 1 = dc message, 2 = reply to dc message
if 0 != (*msg).is_dc_message {
job_add(
context,
Action::MoveMsg,
(*msg).id as libc::c_int,
Params::new(),
0,
);
dc_update_msg_move_state(context, (*msg).rfc724_mid, MoveState::Moving);
// 1 = dc message, 2 = reply to dc message
if 0 != msg.is_dc_message {
job_add(
context,
Action::MoveMsg,
msg.id as libc::c_int,
Params::new(),
0,
);
dc_update_msg_move_state(context, msg.rfc724_mid, MoveState::Moving);
}
}
dc_msg_unref(msg);
}

File diff suppressed because it is too large Load Diff

View File

@@ -293,38 +293,37 @@ unsafe fn send_handshake_msg(
fingerprint: *const libc::c_char,
grpid: impl AsRef<str>,
) {
let mut msg: *mut dc_msg_t = dc_msg_new_untyped(context);
(*msg).type_0 = Viewtype::Text;
(*msg).text = Some(format!("Secure-Join: {}", to_string(step)));
(*msg).hidden = 1;
(*msg).param.set_int(Param::Cmd, 7);
let mut msg = dc_msg_new_untyped(context);
msg.type_0 = Viewtype::Text;
msg.text = Some(format!("Secure-Join: {}", to_string(step)));
msg.hidden = true;
msg.param.set_int(Param::Cmd, 7);
if step.is_null() {
(*msg).param.remove(Param::Arg);
msg.param.remove(Param::Arg);
} else {
(*msg).param.set(Param::Arg, as_str(step));
msg.param.set(Param::Arg, as_str(step));
}
if !param2.as_ref().is_empty() {
(*msg).param.set(Param::Arg2, param2);
msg.param.set(Param::Arg2, param2);
}
if !fingerprint.is_null() {
(*msg).param.set(Param::Arg3, as_str(fingerprint));
msg.param.set(Param::Arg3, as_str(fingerprint));
}
if !grpid.as_ref().is_empty() {
(*msg).param.set(Param::Arg4, grpid.as_ref());
msg.param.set(Param::Arg4, grpid.as_ref());
}
if strcmp(step, b"vg-request\x00" as *const u8 as *const libc::c_char) == 0i32
|| strcmp(step, b"vc-request\x00" as *const u8 as *const libc::c_char) == 0i32
{
(*msg).param.set_int(
msg.param.set_int(
Param::ForcePlaintext,
ForcePlaintext::AddAutocryptHeader as i32,
);
} else {
(*msg).param.set_int(Param::GuranteeE2ee, 1);
msg.param.set_int(Param::GuranteeE2ee, 1);
}
// TODO. handle cleanup on error
chat::send_msg(context, contact_chat_id, msg).unwrap();
dc_msg_unref(msg);
chat::send_msg(context, contact_chat_id, &mut msg).unwrap();
}
unsafe fn chat_id_2_contact_id(context: &Context, contact_chat_id: uint32_t) -> uint32_t {

View File

@@ -226,7 +226,6 @@ impl Job {
#[allow(non_snake_case)]
fn do_DC_JOB_MOVE_MSG(&mut self, context: &Context) {
let ok_to_continue;
let msg = unsafe { dc_msg_new_untyped(context) };
let mut dest_uid = 0;
let inbox = context.inbox.read().unwrap();
@@ -243,7 +242,7 @@ impl Job {
ok_to_continue = true;
}
if ok_to_continue {
if dc_msg_load_from_db(msg, context, self.foreign_id) {
if let Ok(msg) = dc_msg_load_from_db(context, self.foreign_id) {
if context
.sql
.get_config_int(context, "folders_configured")
@@ -254,8 +253,6 @@ impl Job {
}
let dest_folder = context.sql.get_config(context, "configured_mvbox_folder");
let msg = unsafe { &mut *msg };
if let Some(dest_folder) = dest_folder {
let server_folder = msg.server_folder.as_ref().unwrap();
@@ -278,71 +275,65 @@ impl Job {
}
}
}
unsafe { dc_msg_unref(msg) };
}
#[allow(non_snake_case)]
fn do_DC_JOB_DELETE_MSG_ON_IMAP(&mut self, context: &Context) {
let mut delete_from_server = 1;
let msg = unsafe { dc_msg_new_untyped(context) };
let inbox = context.inbox.read().unwrap();
if !(!dc_msg_load_from_db(msg, context, self.foreign_id)
|| unsafe { (*msg).rfc724_mid.is_null() }
|| unsafe { *(*msg).rfc724_mid.offset(0isize) as libc::c_int == 0 })
{
let ok_to_continue1;
/* eg. device messages have no Message-ID */
if dc_rfc724_mid_cnt(context, unsafe { (*msg).rfc724_mid }) != 1 {
info!(
context,
0, "The message is deleted from the server when all parts are deleted.",
);
delete_from_server = 0i32
}
/* if this is the last existing part of the message, we delete the message from the server */
if 0 != delete_from_server {
let ok_to_continue;
if !inbox.is_connected() {
connect_to_inbox(context, &inbox);
if let Ok(mut msg) = dc_msg_load_from_db(context, self.foreign_id) {
if !(msg.rfc724_mid.is_null()
|| unsafe { *msg.rfc724_mid.offset(0isize) as libc::c_int == 0 })
{
let ok_to_continue1;
/* eg. device messages have no Message-ID */
if dc_rfc724_mid_cnt(context, msg.rfc724_mid) != 1 {
info!(
context,
0, "The message is deleted from the server when all parts are deleted.",
);
delete_from_server = 0i32
}
/* if this is the last existing part of the message, we delete the message from the server */
if 0 != delete_from_server {
let ok_to_continue;
if !inbox.is_connected() {
self.try_again_later(3i32, None);
ok_to_continue = false;
connect_to_inbox(context, &inbox);
if !inbox.is_connected() {
self.try_again_later(3i32, None);
ok_to_continue = false;
} else {
ok_to_continue = true;
}
} else {
ok_to_continue = true;
}
} else {
ok_to_continue = true;
}
if ok_to_continue {
let mid = unsafe { CStr::from_ptr((*msg).rfc724_mid).to_str().unwrap() };
let server_folder = unsafe { (*msg).server_folder.as_ref().unwrap() };
if 0 == inbox.delete_msg(context, mid, server_folder, unsafe {
&mut (*msg).server_uid
}) {
self.try_again_later(-1i32, None);
ok_to_continue1 = false;
if ok_to_continue {
let mid = unsafe { CStr::from_ptr(msg.rfc724_mid).to_str().unwrap() };
let server_folder = msg.server_folder.as_ref().unwrap();
if 0 == inbox.delete_msg(context, mid, server_folder, &mut msg.server_uid) {
self.try_again_later(-1i32, None);
ok_to_continue1 = false;
} else {
ok_to_continue1 = true;
}
} else {
ok_to_continue1 = true;
ok_to_continue1 = false;
}
} else {
ok_to_continue1 = false;
ok_to_continue1 = true;
}
if ok_to_continue1 {
dc_delete_msg_from_db(context, msg.id);
}
} else {
ok_to_continue1 = true;
}
if ok_to_continue1 {
unsafe { dc_delete_msg_from_db(context, (*msg).id) };
}
}
unsafe { dc_msg_unref(msg) }
}
#[allow(non_snake_case)]
fn do_DC_JOB_MARKSEEN_MSG_ON_IMAP(&mut self, context: &Context) {
let ok_to_continue;
let msg = unsafe { dc_msg_new_untyped(context) };
let inbox = context.inbox.read().unwrap();
if !inbox.is_connected() {
@@ -357,32 +348,29 @@ impl Job {
ok_to_continue = true;
}
if ok_to_continue {
if dc_msg_load_from_db(msg, context, self.foreign_id) {
let server_folder = unsafe { (*msg).server_folder.as_ref().unwrap() };
match inbox.set_seen(context, server_folder, unsafe { (*msg).server_uid })
as libc::c_uint
{
if let Ok(msg) = dc_msg_load_from_db(context, self.foreign_id) {
let server_folder = msg.server_folder.as_ref().unwrap();
match inbox.set_seen(context, server_folder, msg.server_uid) as libc::c_uint {
0 => {}
1 => {
self.try_again_later(3i32, None);
}
_ => {
if 0 != unsafe { (*msg).param.get_int(Param::WantsMdn).unwrap_or_default() }
if 0 != msg.param.get_int(Param::WantsMdn).unwrap_or_default()
&& 0 != context
.sql
.get_config_int(context, "mdns_enabled")
.unwrap_or_else(|| 1)
{
let folder = unsafe { (*msg).server_folder.as_ref().unwrap() };
let folder = msg.server_folder.as_ref().unwrap();
match inbox.set_mdnsent(context, folder, unsafe { (*msg).server_uid })
as libc::c_uint
match inbox.set_mdnsent(context, folder, msg.server_uid) as libc::c_uint
{
1 => {
self.try_again_later(3i32, None);
}
3 => {
send_mdn(context, unsafe { (*msg).id });
send_mdn(context, msg.id);
}
0 | 2 | _ => {}
}
@@ -391,7 +379,6 @@ impl Job {
}
}
}
unsafe { dc_msg_unref(msg) };
}
#[allow(non_snake_case)]
@@ -674,55 +661,38 @@ pub fn job_action_exists(context: &Context, action: Action) -> bool {
#[allow(non_snake_case)]
pub unsafe fn job_send_msg(context: &Context, msg_id: uint32_t) -> libc::c_int {
let mut success = 0;
let mut mimefactory = dc_mimefactory_t {
from_addr: ptr::null_mut(),
from_displayname: ptr::null_mut(),
selfstatus: ptr::null_mut(),
recipients_names: ptr::null_mut(),
recipients_addr: ptr::null_mut(),
timestamp: 0,
rfc724_mid: ptr::null_mut(),
loaded: DC_MF_NOTHING_LOADED,
msg: ptr::null_mut(),
chat: None,
increation: 0,
in_reply_to: ptr::null_mut(),
references: ptr::null_mut(),
req_mdn: 0,
out: ptr::null_mut(),
out_encrypted: 0,
out_gossiped: 0,
out_last_added_location_id: 0,
error: ptr::null_mut(),
context,
};
/* load message data */
if 0 == dc_mimefactory_load_msg(&mut mimefactory, msg_id) || mimefactory.from_addr.is_null() {
let mimefactory = dc_mimefactory_load_msg(context, msg_id);
if mimefactory.is_err() || mimefactory.as_ref().unwrap().from_addr.is_null() {
warn!(
context,
0, "Cannot load data to send, maybe the message is deleted in between.",
);
} else {
let mut mimefactory = mimefactory.unwrap();
// no redo, no IMAP. moreover, as the data does not exist, there is no need in calling dc_set_msg_failed()
if chat::msgtype_has_file((*mimefactory.msg).type_0) {
if let Some(pathNfilename) = (*mimefactory.msg).param.get(Param::File) {
if ((*mimefactory.msg).type_0 == Viewtype::Image
|| (*mimefactory.msg).type_0 == Viewtype::Gif)
&& !(*mimefactory.msg).param.exists(Param::Width)
if chat::msgtype_has_file(mimefactory.msg.type_0) {
let file_param = mimefactory
.msg
.param
.get(Param::File)
.map(|s| s.to_string());
if let Some(pathNfilename) = file_param {
if (mimefactory.msg.type_0 == Viewtype::Image
|| mimefactory.msg.type_0 == Viewtype::Gif)
&& !mimefactory.msg.param.exists(Param::Width)
{
(*mimefactory.msg).param.set_int(Param::Width, 0);
(*mimefactory.msg).param.set_int(Param::Height, 0);
mimefactory.msg.param.set_int(Param::Width, 0);
mimefactory.msg.param.set_int(Param::Height, 0);
if let Some(buf) = dc_read_file_safe(context, pathNfilename) {
if let Ok((width, height)) = dc_get_filemeta(&buf) {
(*mimefactory.msg).param.set_int(Param::Width, width as i32);
(*mimefactory.msg)
.param
.set_int(Param::Height, height as i32);
mimefactory.msg.param.set_int(Param::Width, width as i32);
mimefactory.msg.param.set_int(Param::Height, height as i32);
}
}
dc_msg_save_param_to_disk(mimefactory.msg);
dc_msg_save_param_to_disk(&mut mimefactory.msg);
}
}
}
@@ -730,7 +700,8 @@ pub unsafe fn job_send_msg(context: &Context, msg_id: uint32_t) -> libc::c_int {
if 0 == dc_mimefactory_render(&mut mimefactory) {
dc_set_msg_failed(context, msg_id, as_opt_str(mimefactory.error));
} else if 0
!= (*mimefactory.msg)
!= mimefactory
.msg
.param
.get_int(Param::GuranteeE2ee)
.unwrap_or_default()
@@ -741,7 +712,7 @@ pub unsafe fn job_send_msg(context: &Context, msg_id: uint32_t) -> libc::c_int {
0,
"e2e encryption unavailable {} - {:?}",
msg_id,
(*mimefactory.msg).param.get_int(Param::GuranteeE2ee),
mimefactory.msg.param.get_int(Param::GuranteeE2ee),
);
dc_set_msg_failed(
context,
@@ -765,32 +736,32 @@ pub unsafe fn job_send_msg(context: &Context, msg_id: uint32_t) -> libc::c_int {
);
}
if 0 != mimefactory.out_gossiped {
chat::set_gossiped_timestamp(context, (*mimefactory.msg).chat_id, time());
chat::set_gossiped_timestamp(context, mimefactory.msg.chat_id, time());
}
if 0 != mimefactory.out_last_added_location_id {
dc_set_kml_sent_timestamp(context, (*mimefactory.msg).chat_id, time());
if 0 == (*mimefactory.msg).hidden {
dc_set_kml_sent_timestamp(context, mimefactory.msg.chat_id, time());
if !mimefactory.msg.hidden {
dc_set_msg_location_id(
context,
(*mimefactory.msg).id,
mimefactory.msg.id,
mimefactory.out_last_added_location_id,
);
}
}
if 0 != mimefactory.out_encrypted
&& (*mimefactory.msg)
&& mimefactory
.msg
.param
.get_int(Param::GuranteeE2ee)
.unwrap_or_default()
== 0
{
(*mimefactory.msg).param.set_int(Param::GuranteeE2ee, 1);
dc_msg_save_param_to_disk(mimefactory.msg);
mimefactory.msg.param.set_int(Param::GuranteeE2ee, 1);
dc_msg_save_param_to_disk(&mut mimefactory.msg);
}
success = add_smtp_job(context, Action::SendMsgToSmtp, &mut mimefactory);
}
}
dc_mimefactory_empty(&mut mimefactory);
success
}
@@ -979,9 +950,7 @@ fn job_perform(context: &Context, thread: Thread, probe_network: bool) {
}
} else {
if job.action == Action::SendMsgToSmtp {
unsafe {
dc_set_msg_failed(context, job.foreign_id, job.pending_error.as_ref())
};
dc_set_msg_failed(context, job.foreign_id, job.pending_error.as_ref());
}
job.delete(context);
}
@@ -1034,33 +1003,10 @@ fn connect_to_inbox(context: &Context, inbox: &Imap) -> libc::c_int {
}
fn send_mdn(context: &Context, msg_id: uint32_t) {
let mut mimefactory = dc_mimefactory_t {
from_addr: ptr::null_mut(),
from_displayname: ptr::null_mut(),
selfstatus: ptr::null_mut(),
recipients_names: ptr::null_mut(),
recipients_addr: ptr::null_mut(),
timestamp: 0,
rfc724_mid: ptr::null_mut(),
loaded: DC_MF_NOTHING_LOADED,
msg: ptr::null_mut(),
chat: None,
increation: 0,
in_reply_to: ptr::null_mut(),
references: ptr::null_mut(),
req_mdn: 0,
out: ptr::null_mut(),
out_encrypted: 0,
out_gossiped: 0,
out_last_added_location_id: 0,
error: ptr::null_mut(),
context,
};
if !(0 == unsafe { dc_mimefactory_load_mdn(&mut mimefactory, msg_id) }
|| 0 == unsafe { dc_mimefactory_render(&mut mimefactory) })
{
add_smtp_job(context, Action::SendMdn, &mut mimefactory);
if let Ok(mut mimefactory) = unsafe { dc_mimefactory_load_mdn(context, msg_id) } {
if 0 != unsafe { dc_mimefactory_render(&mut mimefactory) } {
add_smtp_job(context, Action::SendMdn, &mut mimefactory);
}
}
}
@@ -1116,7 +1062,7 @@ fn add_smtp_job(context: &Context, action: Action, mimefactory: &dc_mimefactory_
(if mimefactory.loaded as libc::c_uint
== DC_MF_MSG_LOADED as libc::c_int as libc::c_uint
{
unsafe { (*mimefactory.msg).id }
mimefactory.msg.id
} else {
0
}) as libc::c_int,

View File

@@ -20,7 +20,7 @@ extern crate strum_macros;
#[macro_use]
mod log;
#[macro_use]
mod error;
pub mod error;
mod aheader;
pub mod chat;