refactor(chat): first round of method renaming and restructuring

This commit is contained in:
dignifiedquire
2019-08-15 23:36:24 +02:00
parent ddfd067e97
commit 001880e1f0
19 changed files with 1335 additions and 1418 deletions

View File

@@ -322,7 +322,11 @@ pub unsafe extern "C" fn dc_create_chat_by_msg_id(context: *mut dc_context_t, ms
assert!(!context.is_null()); assert!(!context.is_null());
let context = &*context; let context = &*context;
chat::dc_create_chat_by_msg_id(context, msg_id) log_err_default(
chat::create_by_msg_id(context, msg_id),
context,
"Failed to create chat",
)
} }
#[no_mangle] #[no_mangle]
@@ -333,7 +337,11 @@ pub unsafe extern "C" fn dc_create_chat_by_contact_id(
assert!(!context.is_null()); assert!(!context.is_null());
let context = &*context; let context = &*context;
chat::dc_create_chat_by_contact_id(context, contact_id) log_err_default(
chat::create_by_contact_id(context, contact_id),
context,
"Failed to create chat",
)
} }
#[no_mangle] #[no_mangle]
@@ -344,7 +352,11 @@ pub unsafe extern "C" fn dc_get_chat_id_by_contact_id(
assert!(!context.is_null()); assert!(!context.is_null());
let context = &*context; let context = &*context;
chat::dc_get_chat_id_by_contact_id(context, contact_id) log_err_default(
chat::get_by_contact_id(context, contact_id),
context,
"Failed to get chat",
)
} }
#[no_mangle] #[no_mangle]
@@ -357,7 +369,11 @@ pub unsafe extern "C" fn dc_prepare_msg(
assert!(!msg.is_null()); assert!(!msg.is_null());
let context = &*context; let context = &*context;
chat::dc_prepare_msg(context, chat_id, msg) log_err_default(
chat::prepare_msg(context, chat_id, msg),
context,
"Failed to prepare message",
)
} }
#[no_mangle] #[no_mangle]
@@ -370,7 +386,11 @@ pub unsafe extern "C" fn dc_send_msg(
assert!(!msg.is_null()); assert!(!msg.is_null());
let context = &*context; let context = &*context;
chat::dc_send_msg(context, chat_id, msg) log_err_default(
chat::send_msg(context, chat_id, msg),
context,
"Failed to send message",
)
} }
#[no_mangle] #[no_mangle]
@@ -384,7 +404,11 @@ pub unsafe extern "C" fn dc_send_text_msg(
let context = &*context; let context = &*context;
let text_to_send = dc_tools::to_string_lossy(text_to_send); let text_to_send = dc_tools::to_string_lossy(text_to_send);
chat::dc_send_text_msg(context, chat_id, text_to_send) log_err_default(
chat::send_text_msg(context, chat_id, text_to_send),
context,
"Failed to send text message",
)
} }
#[no_mangle] #[no_mangle]
@@ -396,7 +420,7 @@ pub unsafe extern "C" fn dc_set_draft(
assert!(!context.is_null()); assert!(!context.is_null());
let context = &*context; let context = &*context;
chat::dc_set_draft(context, chat_id, msg) chat::set_draft(context, chat_id, msg)
} }
#[no_mangle] #[no_mangle]
@@ -407,7 +431,7 @@ pub unsafe extern "C" fn dc_get_draft<'a>(
assert!(!context.is_null()); assert!(!context.is_null());
let context = &*context; let context = &*context;
chat::dc_get_draft(context, chat_id) chat::get_draft(context, chat_id)
} }
#[no_mangle] #[no_mangle]
@@ -420,7 +444,7 @@ pub unsafe extern "C" fn dc_get_chat_msgs(
assert!(!context.is_null()); assert!(!context.is_null());
let context = &*context; let context = &*context;
chat::dc_get_chat_msgs(context, chat_id, flags, marker1before) chat::get_chat_msgs(context, chat_id, flags, marker1before)
} }
#[no_mangle] #[no_mangle]
@@ -428,7 +452,7 @@ pub unsafe extern "C" fn dc_get_msg_cnt(context: *mut dc_context_t, chat_id: u32
assert!(!context.is_null()); assert!(!context.is_null());
let context = &*context; let context = &*context;
chat::dc_get_msg_cnt(context, chat_id) chat::get_msg_cnt(context, chat_id) as libc::c_int
} }
#[no_mangle] #[no_mangle]
@@ -439,7 +463,7 @@ pub unsafe extern "C" fn dc_get_fresh_msg_cnt(
assert!(!context.is_null()); assert!(!context.is_null());
let context = &*context; let context = &*context;
chat::dc_get_fresh_msg_cnt(context, chat_id) chat::get_fresh_msg_cnt(context, chat_id) as libc::c_int
} }
#[no_mangle] #[no_mangle]
@@ -457,7 +481,11 @@ pub unsafe extern "C" fn dc_marknoticed_chat(context: *mut dc_context_t, chat_id
assert!(!context.is_null()); assert!(!context.is_null());
let context = &*context; let context = &*context;
chat::dc_marknoticed_chat(context, chat_id); log_err(
chat::marknoticed_chat(context, chat_id),
context,
"Failed marknoticed chat",
);
} }
#[no_mangle] #[no_mangle]
@@ -465,7 +493,11 @@ pub unsafe extern "C" fn dc_marknoticed_all_chats(context: *mut dc_context_t) {
assert!(!context.is_null()); assert!(!context.is_null());
let context = &*context; let context = &*context;
chat::dc_marknoticed_all_chats(context); log_err(
chat::marknoticed_all_chats(context),
context,
"Failed marknoticed all chats",
);
} }
fn from_prim<S, T>(s: S) -> Option<T> fn from_prim<S, T>(s: S) -> Option<T>
@@ -493,7 +525,7 @@ pub unsafe extern "C" fn dc_get_chat_media(
let or_msg_type3 = let or_msg_type3 =
from_prim(or_msg_type3).expect(&format!("incorrect or_msg_type3 = {}", or_msg_type3)); from_prim(or_msg_type3).expect(&format!("incorrect or_msg_type3 = {}", or_msg_type3));
chat::dc_get_chat_media(context, chat_id, msg_type, or_msg_type2, or_msg_type3) chat::get_chat_media(context, chat_id, msg_type, or_msg_type2, or_msg_type3)
} }
#[no_mangle] #[no_mangle]
@@ -514,7 +546,7 @@ pub unsafe extern "C" fn dc_get_next_media(
let or_msg_type3 = let or_msg_type3 =
from_prim(or_msg_type3).expect(&format!("incorrect or_msg_type3 = {}", or_msg_type3)); from_prim(or_msg_type3).expect(&format!("incorrect or_msg_type3 = {}", or_msg_type3));
chat::dc_get_next_media(context, msg_id, dir, msg_type, or_msg_type2, or_msg_type3) chat::get_next_media(context, msg_id, dir, msg_type, or_msg_type2, or_msg_type3)
} }
#[no_mangle] #[no_mangle]
@@ -526,7 +558,19 @@ pub unsafe extern "C" fn dc_archive_chat(
assert!(!context.is_null()); assert!(!context.is_null());
let context = &*context; let context = &*context;
chat::dc_archive_chat(context, chat_id, archive); let archive = if archive == 0 {
false
} else if archive == 1 {
true
} else {
return;
};
log_err(
chat::archive(context, chat_id, archive),
context,
"Failed archive chat",
);
} }
#[no_mangle] #[no_mangle]
@@ -535,7 +579,11 @@ pub unsafe extern "C" fn dc_delete_chat(context: *mut dc_context_t, chat_id: u32
let context = &*context; let context = &*context;
// TODO: update to indicate public api success/failure of deletion // TODO: update to indicate public api success/failure of deletion
chat::dc_delete_chat(context, chat_id); log_err(
chat::delete(context, chat_id),
context,
"Failed chat delete",
);
} }
#[no_mangle] #[no_mangle]
@@ -546,7 +594,7 @@ pub unsafe extern "C" fn dc_get_chat_contacts(
assert!(!context.is_null()); assert!(!context.is_null());
let context = &*context; let context = &*context;
dc_array_t::from(chat::dc_get_chat_contacts(context, chat_id)).into_raw() dc_array_t::from(chat::get_chat_contacts(context, chat_id)).into_raw()
} }
#[no_mangle] #[no_mangle]
@@ -570,7 +618,7 @@ pub unsafe extern "C" fn dc_get_chat<'a>(
assert!(!context.is_null()); assert!(!context.is_null());
let context = &*context; let context = &*context;
match chat::dc_get_chat(context, chat_id) { match chat::Chat::load_from_db(context, chat_id) {
Ok(chat) => Box::into_raw(Box::new(chat)), Ok(chat) => Box::into_raw(Box::new(chat)),
Err(_) => std::ptr::null_mut(), Err(_) => std::ptr::null_mut(),
} }
@@ -586,7 +634,17 @@ pub unsafe extern "C" fn dc_create_group_chat(
assert!(!name.is_null()); assert!(!name.is_null());
let context = &*context; let context = &*context;
chat::dc_create_group_chat(context, verified, name) let verified = if let Some(s) = contact::VerifiedStatus::from_i32(verified) {
s
} else {
return 0;
};
log_err_default(
chat::create_group_chat(context, verified, name),
context,
"Failed to create group chat",
)
} }
#[no_mangle] #[no_mangle]
@@ -598,7 +656,7 @@ pub unsafe extern "C" fn dc_is_contact_in_chat(
assert!(!context.is_null()); assert!(!context.is_null());
let context = &*context; let context = &*context;
chat::dc_is_contact_in_chat(context, chat_id, contact_id) chat::is_contact_in_chat(context, chat_id, contact_id)
} }
#[no_mangle] #[no_mangle]
@@ -610,7 +668,7 @@ pub unsafe extern "C" fn dc_add_contact_to_chat(
assert!(!context.is_null()); assert!(!context.is_null());
let context = &*context; let context = &*context;
chat::dc_add_contact_to_chat(context, chat_id, contact_id) chat::add_contact_to_chat(context, chat_id, contact_id)
} }
#[no_mangle] #[no_mangle]
@@ -622,7 +680,7 @@ pub unsafe extern "C" fn dc_remove_contact_from_chat(
assert!(!context.is_null()); assert!(!context.is_null());
let context = &*context; let context = &*context;
chat::dc_remove_contact_from_chat(context, chat_id, contact_id) chat::remove_contact_from_chat(context, chat_id, contact_id)
} }
#[no_mangle] #[no_mangle]
@@ -636,7 +694,7 @@ pub unsafe extern "C" fn dc_set_chat_name(
assert!(chat_id > constants::DC_CHAT_ID_LAST_SPECIAL as u32); assert!(chat_id > constants::DC_CHAT_ID_LAST_SPECIAL as u32);
let context = &*context; let context = &*context;
chat::dc_set_chat_name(context, chat_id, name) chat::set_chat_name(context, chat_id, name)
} }
#[no_mangle] #[no_mangle]
@@ -649,7 +707,7 @@ pub unsafe extern "C" fn dc_set_chat_profile_image(
assert!(chat_id > constants::DC_CHAT_ID_LAST_SPECIAL as u32); assert!(chat_id > constants::DC_CHAT_ID_LAST_SPECIAL as u32);
let context = &*context; let context = &*context;
chat::dc_set_chat_profile_image(context, chat_id, image) chat::set_chat_profile_image(context, chat_id, image)
} }
#[no_mangle] #[no_mangle]
@@ -701,7 +759,7 @@ pub unsafe extern "C" fn dc_forward_msgs(
assert!(chat_id > constants::DC_CHAT_ID_LAST_SPECIAL as u32); assert!(chat_id > constants::DC_CHAT_ID_LAST_SPECIAL as u32);
let context = &*context; let context = &*context;
chat::dc_forward_msgs(context, msg_ids, msg_cnt, chat_id) chat::forward_msgs(context, msg_ids, msg_cnt, chat_id)
} }
#[no_mangle] #[no_mangle]
@@ -1275,7 +1333,7 @@ pub unsafe extern "C" fn dc_chat_get_id(chat: *mut dc_chat_t) -> u32 {
assert!(!chat.is_null()); assert!(!chat.is_null());
let chat = &*chat; let chat = &*chat;
chat::dc_chat_get_id(chat) chat.get_id()
} }
#[no_mangle] #[no_mangle]
@@ -1283,7 +1341,7 @@ pub unsafe extern "C" fn dc_chat_get_type(chat: *mut dc_chat_t) -> libc::c_int {
assert!(!chat.is_null()); assert!(!chat.is_null());
let chat = &*chat; let chat = &*chat;
chat::dc_chat_get_type(chat) as libc::c_int chat.get_type() as libc::c_int
} }
#[no_mangle] #[no_mangle]
@@ -1291,7 +1349,7 @@ pub unsafe extern "C" fn dc_chat_get_name(chat: *mut dc_chat_t) -> *mut libc::c_
assert!(!chat.is_null()); assert!(!chat.is_null());
let chat = &*chat; let chat = &*chat;
chat::dc_chat_get_name(chat) chat.get_name()
} }
#[no_mangle] #[no_mangle]
@@ -1299,7 +1357,7 @@ pub unsafe extern "C" fn dc_chat_get_subtitle(chat: *mut dc_chat_t) -> *mut libc
assert!(!chat.is_null()); assert!(!chat.is_null());
let chat = &*chat; let chat = &*chat;
chat::dc_chat_get_subtitle(chat) chat.get_subtitle()
} }
#[no_mangle] #[no_mangle]
@@ -1307,7 +1365,7 @@ pub unsafe extern "C" fn dc_chat_get_profile_image(chat: *mut dc_chat_t) -> *mut
assert!(!chat.is_null()); assert!(!chat.is_null());
let chat = &*chat; let chat = &*chat;
chat::dc_chat_get_profile_image(chat) chat.get_profile_image()
} }
#[no_mangle] #[no_mangle]
@@ -1315,7 +1373,7 @@ pub unsafe extern "C" fn dc_chat_get_color(chat: *mut dc_chat_t) -> u32 {
assert!(!chat.is_null()); assert!(!chat.is_null());
let chat = &*chat; let chat = &*chat;
chat::dc_chat_get_color(chat) chat.get_color()
} }
#[no_mangle] #[no_mangle]
@@ -1323,7 +1381,7 @@ pub unsafe extern "C" fn dc_chat_get_archived(chat: *mut dc_chat_t) -> libc::c_i
assert!(!chat.is_null()); assert!(!chat.is_null());
let chat = &*chat; let chat = &*chat;
chat::dc_chat_get_archived(chat) as libc::c_int chat.is_archived() as libc::c_int
} }
#[no_mangle] #[no_mangle]
@@ -1331,7 +1389,7 @@ pub unsafe extern "C" fn dc_chat_is_unpromoted(chat: *mut dc_chat_t) -> libc::c_
assert!(!chat.is_null()); assert!(!chat.is_null());
let chat = &*chat; let chat = &*chat;
chat::dc_chat_is_unpromoted(chat) chat.is_unpromoted() as libc::c_int
} }
#[no_mangle] #[no_mangle]
@@ -1339,7 +1397,7 @@ pub unsafe extern "C" fn dc_chat_is_self_talk(chat: *mut dc_chat_t) -> libc::c_i
assert!(!chat.is_null()); assert!(!chat.is_null());
let chat = &*chat; let chat = &*chat;
chat::dc_chat_is_self_talk(chat) chat.is_self_talk() as libc::c_int
} }
#[no_mangle] #[no_mangle]
@@ -1347,7 +1405,7 @@ pub unsafe extern "C" fn dc_chat_is_verified(chat: *mut dc_chat_t) -> libc::c_in
assert!(!chat.is_null()); assert!(!chat.is_null());
let chat = &*chat; let chat = &*chat;
chat::dc_chat_is_verified(chat) chat.is_verified() as libc::c_int
} }
#[no_mangle] #[no_mangle]
@@ -1355,7 +1413,7 @@ pub unsafe extern "C" fn dc_chat_is_sending_locations(chat: *mut dc_chat_t) -> l
assert!(!chat.is_null()); assert!(!chat.is_null());
let chat = &*chat; let chat = &*chat;
chat::dc_chat_is_sending_locations(chat) as libc::c_int chat.is_sending_locations() as libc::c_int
} }
// dc_msg_t // dc_msg_t
@@ -1836,3 +1894,23 @@ fn as_opt_str<'a>(s: *const libc::c_char) -> Option<&'a str> {
Some(dc_tools::as_str(s)) Some(dc_tools::as_str(s))
} }
fn log_err<T>(res: Result<T, error::Error>, context: &context::Context, msg: &str) {
if let Err(err) = res {
error!(context, 0, "{}: {}", msg, err);
}
}
fn log_err_default<T: Default>(
res: Result<T, error::Error>,
context: &context::Context,
msg: &str,
) -> T {
match res {
Ok(t) => t,
Err(err) => {
error!(context, 0, "{}: {}", msg, err);
Default::default()
}
}
}

View File

@@ -1,7 +1,7 @@
use std::ffi::CString; use std::ffi::CString;
use std::str::FromStr; use std::str::FromStr;
use deltachat::chat::*; use deltachat::chat::{self, Chat};
use deltachat::chatlist::*; use deltachat::chatlist::*;
use deltachat::config; use deltachat::config;
use deltachat::constants::*; use deltachat::constants::*;
@@ -151,7 +151,7 @@ unsafe fn poke_spec(context: &Context, spec: *const libc::c_char) -> libc::c_int
} }
if ok_to_continue { if ok_to_continue {
let ok_to_continue2; let ok_to_continue2;
suffix = dc_get_filesuffix_lc(real_spec); suffix = dc_get_filesuffix_lc(as_str(real_spec));
if !suffix.is_null() && strcmp(suffix, b"eml\x00" as *const u8 as *const libc::c_char) == 0 if !suffix.is_null() && strcmp(suffix, b"eml\x00" as *const u8 as *const libc::c_char) == 0
{ {
if 0 != dc_poke_eml_file(context, real_spec) { if 0 != dc_poke_eml_file(context, real_spec) {
@@ -356,7 +356,7 @@ fn chat_prefix(chat: &Chat) -> &'static str {
pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::Error> { pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::Error> {
let chat_id = *context.cmdline_sel_chat_id.read().unwrap(); let chat_id = *context.cmdline_sel_chat_id.read().unwrap();
let mut sel_chat = if chat_id > 0 { let mut sel_chat = if chat_id > 0 {
dc_get_chat(context, chat_id).ok() Chat::load_from_db(context, chat_id).ok()
} else { } else {
None None
}; };
@@ -606,23 +606,23 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
); );
for i in (0..cnt).rev() { for i in (0..cnt).rev() {
let chat = dc_get_chat(context, chatlist.get_chat_id(i))?; let chat = Chat::load_from_db(context, chatlist.get_chat_id(i))?;
let temp_subtitle = dc_chat_get_subtitle(&chat); let temp_subtitle = chat.get_subtitle();
let temp_name = dc_chat_get_name(&chat); let temp_name = chat.get_name();
info!( info!(
context, context,
0, 0,
"{}#{}: {} [{}] [{} fresh]", "{}#{}: {} [{}] [{} fresh]",
chat_prefix(&chat), chat_prefix(&chat),
dc_chat_get_id(&chat) as libc::c_int, chat.get_id(),
as_str(temp_name), as_str(temp_name),
as_str(temp_subtitle), as_str(temp_subtitle),
dc_get_fresh_msg_cnt(context, dc_chat_get_id(&chat)) as libc::c_int, chat::get_fresh_msg_cnt(context, chat.get_id()),
); );
free(temp_subtitle as *mut libc::c_void); free(temp_subtitle as *mut libc::c_void);
free(temp_name as *mut libc::c_void); free(temp_name as *mut libc::c_void);
let lot = chatlist.get_summary(i, Some(&chat)); let lot = chatlist.get_summary(i, Some(&chat));
let statestr = if dc_chat_get_archived(&chat) { let statestr = if chat.is_archived() {
" [Archived]" " [Archived]"
} else { } else {
match dc_lot_get_state(lot) { match dc_lot_get_state(lot) {
@@ -645,7 +645,7 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
to_string(text2), to_string(text2),
statestr, statestr,
&timestr, &timestr,
if dc_chat_is_sending_locations(&chat) { if chat.is_sending_locations() {
"📍" "📍"
} else { } else {
"" ""
@@ -672,25 +672,25 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
if !arg1.is_empty() { if !arg1.is_empty() {
let chat_id = arg1.parse()?; let chat_id = arg1.parse()?;
println!("Selecting chat #{}", chat_id); println!("Selecting chat #{}", chat_id);
sel_chat = Some(dc_get_chat(context, chat_id)?); sel_chat = Some(Chat::load_from_db(context, chat_id)?);
*context.cmdline_sel_chat_id.write().unwrap() = chat_id; *context.cmdline_sel_chat_id.write().unwrap() = chat_id;
} }
ensure!(sel_chat.is_some(), "Failed to select chat"); ensure!(sel_chat.is_some(), "Failed to select chat");
let sel_chat = sel_chat.as_ref().unwrap(); let sel_chat = sel_chat.as_ref().unwrap();
let msglist = dc_get_chat_msgs(context, dc_chat_get_id(sel_chat), 0x1, 0); let msglist = chat::get_chat_msgs(context, sel_chat.get_id(), 0x1, 0);
let temp2 = dc_chat_get_subtitle(sel_chat); let temp2 = sel_chat.get_subtitle();
let temp_name = dc_chat_get_name(sel_chat); let temp_name = sel_chat.get_name();
info!( info!(
context, context,
0, 0,
"{}#{}: {} [{}]{}", "{}#{}: {} [{}]{}",
chat_prefix(sel_chat), chat_prefix(sel_chat),
dc_chat_get_id(sel_chat), sel_chat.get_id(),
as_str(temp_name), as_str(temp_name),
as_str(temp2), as_str(temp2),
if dc_chat_is_sending_locations(sel_chat) { if sel_chat.is_sending_locations() {
"📍" "📍"
} else { } else {
"" ""
@@ -702,65 +702,52 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
log_msglist(context, msglist); log_msglist(context, msglist);
dc_array_unref(msglist); dc_array_unref(msglist);
} }
let draft = dc_get_draft(context, dc_chat_get_id(sel_chat)); let draft = chat::get_draft(context, sel_chat.get_id());
if !draft.is_null() { if !draft.is_null() {
log_msg(context, "Draft", draft); log_msg(context, "Draft", draft);
dc_msg_unref(draft); dc_msg_unref(draft);
} }
println!( println!(
"{} messages.", "{} messages.",
dc_get_msg_cnt(context, dc_chat_get_id(sel_chat)) chat::get_msg_cnt(context, sel_chat.get_id())
); );
dc_marknoticed_chat(context, dc_chat_get_id(sel_chat)); chat::marknoticed_chat(context, sel_chat.get_id())?;
} }
"createchat" => { "createchat" => {
ensure!(!arg1.is_empty(), "Argument <contact-id> missing."); ensure!(!arg1.is_empty(), "Argument <contact-id> missing.");
let contact_id: libc::c_int = arg1.parse()?; let contact_id: libc::c_int = arg1.parse()?;
let chat_id: libc::c_int = let chat_id = chat::create_by_contact_id(context, contact_id as uint32_t)?;
dc_create_chat_by_contact_id(context, contact_id as uint32_t) as libc::c_int;
if chat_id != 0 { println!("Single#{} created successfully.", chat_id,);
println!("Single#{} created successfully.", chat_id,);
} else {
bail!("Failed to create chat");
}
} }
"createchatbymsg" => { "createchatbymsg" => {
ensure!(!arg1.is_empty(), "Argument <msg-id> missing"); ensure!(!arg1.is_empty(), "Argument <msg-id> missing");
let msg_id_0: libc::c_int = arg1.parse()?; let msg_id: u32 = arg1.parse()?;
let chat_id_0 = dc_create_chat_by_msg_id(context, msg_id_0 as uint32_t) as libc::c_int; let chat_id = chat::create_by_msg_id(context, msg_id)?;
if chat_id_0 != 0 { let chat = Chat::load_from_db(context, chat_id)?;
let chat = dc_get_chat(context, chat_id_0 as uint32_t)?;
println!("{}#{} created successfully.", chat_prefix(&chat), chat_id_0,); println!("{}#{} created successfully.", chat_prefix(&chat), chat_id,);
} else {
bail!("");
}
} }
"creategroup" => { "creategroup" => {
ensure!(!arg1.is_empty(), "Argument <name> missing."); ensure!(!arg1.is_empty(), "Argument <name> missing.");
let chat_id_1: libc::c_int = dc_create_group_chat(context, 0, arg1_c) as libc::c_int; let chat_id = chat::create_group_chat(context, VerifiedStatus::Unverified, arg1_c)?;
if chat_id_1 != 0 {
println!("Group#{} created successfully.", chat_id_1,); println!("Group#{} created successfully.", chat_id);
} else {
bail!("Failed to create group");
}
} }
"createverified" => { "createverified" => {
ensure!(!arg1.is_empty(), "Argument <name> missing."); ensure!(!arg1.is_empty(), "Argument <name> missing.");
let chat_id_2: libc::c_int = dc_create_group_chat(context, 1, arg1_c) as libc::c_int; let chat_id = chat::create_group_chat(context, VerifiedStatus::Verified, arg1_c)?;
if chat_id_2 != 0 {
println!("VerifiedGroup#{} created successfully.", chat_id_2,); println!("VerifiedGroup#{} created successfully.", chat_id);
} else {
bail!("Failed to create verified group");
}
} }
"addmember" => { "addmember" => {
ensure!(sel_chat.is_some(), "No chat selected"); ensure!(sel_chat.is_some(), "No chat selected");
ensure!(!arg1.is_empty(), "Argument <contact-id> missing."); ensure!(!arg1.is_empty(), "Argument <contact-id> missing.");
let contact_id_0: libc::c_int = arg1.parse()?; let contact_id_0: libc::c_int = arg1.parse()?;
if 0 != dc_add_contact_to_chat( if 0 != chat::add_contact_to_chat(
context, context,
dc_chat_get_id(sel_chat.as_ref().unwrap()), sel_chat.as_ref().unwrap().get_id(),
contact_id_0 as uint32_t, contact_id_0 as uint32_t,
) { ) {
println!("Contact added to chat."); println!("Contact added to chat.");
@@ -772,9 +759,9 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
ensure!(sel_chat.is_some(), "No chat selected."); ensure!(sel_chat.is_some(), "No chat selected.");
ensure!(!arg1.is_empty(), "Argument <contact-id> missing."); ensure!(!arg1.is_empty(), "Argument <contact-id> missing.");
let contact_id_1: libc::c_int = arg1.parse()?; let contact_id_1: libc::c_int = arg1.parse()?;
if 0 != dc_remove_contact_from_chat( if 0 != chat::remove_contact_from_chat(
context, context,
dc_chat_get_id(sel_chat.as_ref().unwrap()), sel_chat.as_ref().unwrap().get_id(),
contact_id_1 as uint32_t, contact_id_1 as uint32_t,
) { ) {
println!("Contact added to chat."); println!("Contact added to chat.");
@@ -785,7 +772,7 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
"groupname" => { "groupname" => {
ensure!(sel_chat.is_some(), "No chat selected."); ensure!(sel_chat.is_some(), "No chat selected.");
ensure!(!arg1.is_empty(), "Argument <name> missing."); ensure!(!arg1.is_empty(), "Argument <name> missing.");
if 0 != dc_set_chat_name(context, dc_chat_get_id(sel_chat.as_ref().unwrap()), arg1_c) { if 0 != chat::set_chat_name(context, sel_chat.as_ref().unwrap().get_id(), arg1_c) {
println!("Chat name set"); println!("Chat name set");
} else { } else {
bail!("Failed to set chat name"); bail!("Failed to set chat name");
@@ -795,9 +782,9 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
ensure!(sel_chat.is_some(), "No chat selected."); ensure!(sel_chat.is_some(), "No chat selected.");
ensure!(!arg1.is_empty(), "Argument <image> missing."); ensure!(!arg1.is_empty(), "Argument <image> missing.");
if 0 != dc_set_chat_profile_image( if 0 != chat::set_chat_profile_image(
context, context,
dc_chat_get_id(sel_chat.as_ref().unwrap()), sel_chat.as_ref().unwrap().get_id(),
if !arg1.is_empty() { if !arg1.is_empty() {
arg1_c arg1_c
} else { } else {
@@ -812,18 +799,14 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
"chatinfo" => { "chatinfo" => {
ensure!(sel_chat.is_some(), "No chat selected."); ensure!(sel_chat.is_some(), "No chat selected.");
let contacts = let contacts = chat::get_chat_contacts(context, sel_chat.as_ref().unwrap().get_id());
dc_get_chat_contacts(context, dc_chat_get_id(sel_chat.as_ref().unwrap()));
info!(context, 0, "Memberlist:"); info!(context, 0, "Memberlist:");
log_contactlist(context, &contacts); log_contactlist(context, &contacts);
println!( println!(
"{} contacts\nLocation streaming: {}", "{} contacts\nLocation streaming: {}",
contacts.len(), contacts.len(),
dc_is_sending_locations_to_chat( dc_is_sending_locations_to_chat(context, sel_chat.as_ref().unwrap().get_id()),
context,
dc_chat_get_id(sel_chat.as_ref().unwrap())
),
); );
} }
"getlocations" => { "getlocations" => {
@@ -832,7 +815,7 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
let contact_id = arg1.parse().unwrap_or_default(); let contact_id = arg1.parse().unwrap_or_default();
let locations = dc_get_locations( let locations = dc_get_locations(
context, context,
dc_chat_get_id(sel_chat.as_ref().unwrap()), sel_chat.as_ref().unwrap().get_id(),
contact_id, contact_id,
0, 0,
0, 0,
@@ -864,10 +847,10 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
ensure!(!arg1.is_empty(), "No timeout given."); ensure!(!arg1.is_empty(), "No timeout given.");
let seconds = arg1.parse()?; let seconds = arg1.parse()?;
dc_send_locations_to_chat(context, dc_chat_get_id(sel_chat.as_ref().unwrap()), seconds); dc_send_locations_to_chat(context, sel_chat.as_ref().unwrap().get_id(), seconds);
println!( println!(
"Locations will be sent to Chat#{} for {} seconds. Use 'setlocation <lat> <lng>' to play around.", "Locations will be sent to Chat#{} for {} seconds. Use 'setlocation <lat> <lng>' to play around.",
dc_chat_get_id(sel_chat.as_ref().unwrap()), sel_chat.as_ref().unwrap().get_id(),
seconds seconds
); );
} }
@@ -895,23 +878,11 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
let msg = format!("{} {}", arg1, arg2); let msg = format!("{} {}", arg1, arg2);
if 0 != dc_send_text_msg(context, dc_chat_get_id(sel_chat.as_ref().unwrap()), msg) { chat::send_text_msg(context, sel_chat.as_ref().unwrap().get_id(), msg)?;
println!("Message sent.");
} else {
bail!("Sending failed.");
}
} }
"sendempty" => { "sendempty" => {
ensure!(sel_chat.is_some(), "No chat selected."); ensure!(sel_chat.is_some(), "No chat selected.");
if 0 != dc_send_text_msg( chat::send_text_msg(context, sel_chat.as_ref().unwrap().get_id(), "".into())?;
context,
dc_chat_get_id(sel_chat.as_ref().unwrap()),
"".into(),
) {
println!("Message sent.");
} else {
bail!("Sending failed.");
}
} }
"sendimage" | "sendfile" => { "sendimage" | "sendfile" => {
ensure!(sel_chat.is_some(), "No chat selected."); ensure!(sel_chat.is_some(), "No chat selected.");
@@ -927,14 +898,14 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
); );
dc_msg_set_file(msg_0, arg1_c, 0 as *const libc::c_char); dc_msg_set_file(msg_0, arg1_c, 0 as *const libc::c_char);
dc_msg_set_text(msg_0, arg2_c); dc_msg_set_text(msg_0, arg2_c);
dc_send_msg(context, dc_chat_get_id(sel_chat.as_ref().unwrap()), msg_0); chat::send_msg(context, sel_chat.as_ref().unwrap().get_id(), msg_0)?;
dc_msg_unref(msg_0); dc_msg_unref(msg_0);
} }
"listmsgs" => { "listmsgs" => {
ensure!(!arg1.is_empty(), "Argument <query> missing."); ensure!(!arg1.is_empty(), "Argument <query> missing.");
let chat = if let Some(ref sel_chat) = sel_chat { let chat = if let Some(ref sel_chat) = sel_chat {
dc_chat_get_id(sel_chat) sel_chat.get_id()
} else { } else {
0 as libc::c_uint 0 as libc::c_uint
}; };
@@ -953,13 +924,13 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
if !arg1.is_empty() { if !arg1.is_empty() {
let draft_0 = dc_msg_new(context, Viewtype::Text); let draft_0 = dc_msg_new(context, Viewtype::Text);
dc_msg_set_text(draft_0, arg1_c); dc_msg_set_text(draft_0, arg1_c);
dc_set_draft(context, dc_chat_get_id(sel_chat.as_ref().unwrap()), draft_0); chat::set_draft(context, sel_chat.as_ref().unwrap().get_id(), draft_0);
dc_msg_unref(draft_0); dc_msg_unref(draft_0);
println!("Draft saved."); println!("Draft saved.");
} else { } else {
dc_set_draft( chat::set_draft(
context, context,
dc_chat_get_id(sel_chat.as_ref().unwrap()), sel_chat.as_ref().unwrap().get_id(),
0 as *mut dc_msg_t, 0 as *mut dc_msg_t,
); );
println!("Draft deleted."); println!("Draft deleted.");
@@ -968,9 +939,9 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
"listmedia" => { "listmedia" => {
ensure!(sel_chat.is_some(), "No chat selected."); ensure!(sel_chat.is_some(), "No chat selected.");
let images = dc_get_chat_media( let images = chat::get_chat_media(
context, context,
dc_chat_get_id(sel_chat.as_ref().unwrap()), sel_chat.as_ref().unwrap().get_id(),
Viewtype::Image, Viewtype::Image,
Viewtype::Gif, Viewtype::Gif,
Viewtype::Video, Viewtype::Video,
@@ -991,12 +962,16 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
"archive" | "unarchive" => { "archive" | "unarchive" => {
ensure!(!arg1.is_empty(), "Argument <chat-id> missing."); ensure!(!arg1.is_empty(), "Argument <chat-id> missing.");
let chat_id = arg1.parse()?; let chat_id = arg1.parse()?;
dc_archive_chat(context, chat_id, if arg0 == "archive" { 1 } else { 0 }); chat::archive(
context,
chat_id,
if arg0 == "archive" { true } else { false },
)?;
} }
"delchat" => { "delchat" => {
ensure!(!arg1.is_empty(), "Argument <chat-id> missing."); ensure!(!arg1.is_empty(), "Argument <chat-id> missing.");
let chat_id = arg1.parse()?; let chat_id = arg1.parse()?;
dc_delete_chat(context, chat_id); chat::delete(context, chat_id)?;
} }
"msginfo" => { "msginfo" => {
ensure!(!arg1.is_empty(), "Argument <msg-id> missing."); ensure!(!arg1.is_empty(), "Argument <msg-id> missing.");
@@ -1021,7 +996,7 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
let mut msg_ids = [0; 1]; let mut msg_ids = [0; 1];
let chat_id = arg2.parse()?; let chat_id = arg2.parse()?;
msg_ids[0] = arg1.parse()?; msg_ids[0] = arg1.parse()?;
dc_forward_msgs(context, msg_ids.as_mut_ptr(), 1, chat_id); chat::forward_msgs(context, msg_ids.as_mut_ptr(), 1, chat_id);
} }
"markseen" => { "markseen" => {
ensure!(!arg1.is_empty(), "Argument <msg-id> missing."); ensure!(!arg1.is_empty(), "Argument <msg-id> missing.");
@@ -1091,8 +1066,8 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
if 0 != i { if 0 != i {
res += ", "; res += ", ";
} }
let chat = dc_get_chat(context, chatlist.get_chat_id(i))?; let chat = Chat::load_from_db(context, chatlist.get_chat_id(i))?;
res += &format!("{}#{}", chat_prefix(&chat), dc_chat_get_id(&chat)); res += &format!("{}#{}", chat_prefix(&chat), chat.get_id());
} }
} }

View File

@@ -5,7 +5,7 @@ use std::sync::{Arc, RwLock};
use std::{thread, time}; use std::{thread, time};
use tempfile::tempdir; use tempfile::tempdir;
use deltachat::chat::*; use deltachat::chat;
use deltachat::chatlist::*; use deltachat::chatlist::*;
use deltachat::config; use deltachat::config;
use deltachat::constants::Event; use deltachat::constants::Event;
@@ -96,8 +96,8 @@ fn main() {
println!("sending a message"); println!("sending a message");
let contact_id = let contact_id =
Contact::create(&ctx, "dignifiedquire", "dignifiedquire@gmail.com").unwrap(); Contact::create(&ctx, "dignifiedquire", "dignifiedquire@gmail.com").unwrap();
let chat_id = dc_create_chat_by_contact_id(&ctx, contact_id); let chat_id = chat::create_by_contact_id(&ctx, contact_id).unwrap();
dc_send_text_msg(&ctx, chat_id, "Hi, here is my first message!".into()); chat::send_text_msg(&ctx, chat_id, "Hi, here is my first message!".into()).unwrap();
println!("fetching chats.."); println!("fetching chats..");
let chats = Chatlist::try_load(&ctx, 0, None, None).unwrap(); let chats = Chatlist::try_load(&ctx, 0, None, None).unwrap();

File diff suppressed because it is too large Load Diff

View File

@@ -265,7 +265,7 @@ impl<'a> Chatlist<'a> {
let chat = if let Some(chat) = chat { let chat = if let Some(chat) = chat {
chat chat
} else { } else {
if let Ok(chat) = dc_get_chat(self.context, self.ids[index].0) { if let Ok(chat) = Chat::load_from_db(self.context, self.ids[index].0) {
chat_loaded = chat; chat_loaded = chat;
&chat_loaded &chat_loaded
} else { } else {

View File

@@ -138,7 +138,7 @@ pub enum Modifier {
Created, Created,
} }
#[derive(Debug, PartialEq, Eq, Clone, Copy)] #[derive(Debug, PartialEq, Eq, Clone, Copy, FromPrimitive)]
#[repr(u8)] #[repr(u8)]
pub enum VerifiedStatus { pub enum VerifiedStatus {
/// Contact is not verified. /// Contact is not verified.

View File

@@ -338,7 +338,7 @@ pub unsafe fn dc_get_info(context: &Context) -> *mut libc::c_char {
let l = dc_loginparam_read(context, &context.sql, ""); let l = dc_loginparam_read(context, &context.sql, "");
let l2 = dc_loginparam_read(context, &context.sql, "configured_"); let l2 = dc_loginparam_read(context, &context.sql, "configured_");
let displayname = context.sql.get_config(context, "displayname"); let displayname = context.sql.get_config(context, "displayname");
let chats = dc_get_chat_cnt(context) as usize; let chats = get_chat_cnt(context) as usize;
let real_msgs = dc_get_real_msg_cnt(context) as usize; let real_msgs = dc_get_real_msg_cnt(context) as usize;
let deaddrop_msgs = dc_get_deaddrop_msg_cnt(context) as usize; let deaddrop_msgs = dc_get_deaddrop_msg_cnt(context) as usize;
let contacts = Contact::get_real_cnt(context) as usize; let contacts = Contact::get_real_cnt(context) as usize;

View File

@@ -6,7 +6,7 @@ use mmime::mmapstring::*;
use mmime::other::*; use mmime::other::*;
use rand::{thread_rng, Rng}; use rand::{thread_rng, Rng};
use crate::chat::*; use crate::chat;
use crate::config::Config; use crate::config::Config;
use crate::constants::*; use crate::constants::*;
use crate::context::Context; use crate::context::Context;
@@ -138,8 +138,7 @@ pub unsafe fn dc_initiate_key_transfer(context: &Context) -> *mut libc::c_char {
setup_file_content_c.as_bytes().len(), setup_file_content_c.as_bytes().len(),
)) ))
{ {
let chat_id = dc_create_chat_by_contact_id(context, 1i32 as uint32_t); if let Ok(chat_id) = chat::create_by_contact_id(context, 1) {
if !(chat_id == 0i32 as libc::c_uint) {
msg = dc_msg_new_untyped(context); msg = dc_msg_new_untyped(context);
(*msg).type_0 = Viewtype::File; (*msg).type_0 = Viewtype::File;
(*msg).param.set(Param::File, as_str(setup_file_name)); (*msg).param.set(Param::File, as_str(setup_file_name));
@@ -157,8 +156,7 @@ pub unsafe fn dc_initiate_key_transfer(context: &Context) -> *mut libc::c_char {
.unwrap() .unwrap()
.shall_stop_ongoing .shall_stop_ongoing
{ {
let msg_id = dc_send_msg(context, chat_id, msg); if let Ok(msg_id) = chat::send_msg(context, chat_id, msg) {
if msg_id != 0 {
dc_msg_unref(msg); dc_msg_unref(msg);
msg = ptr::null_mut(); msg = ptr::null_mut();
info!(context, 0, "Wait for setup message being sent ...",); info!(context, 0, "Wait for setup message being sent ...",);
@@ -970,7 +968,7 @@ unsafe fn import_self_keys(context: &Context, dir_name: *const libc::c_char) ->
free(suffix as *mut libc::c_void); free(suffix as *mut libc::c_void);
let name_f = entry.file_name(); let name_f = entry.file_name();
let name_c = name_f.to_c_string().unwrap(); let name_c = name_f.to_c_string().unwrap();
suffix = dc_get_filesuffix_lc(name_c.as_ptr()); suffix = dc_get_filesuffix_lc(name_f.to_string_lossy());
if suffix.is_null() if suffix.is_null()
|| strcmp(suffix, b"asc\x00" as *const u8 as *const libc::c_char) != 0 || strcmp(suffix, b"asc\x00" as *const u8 as *const libc::c_char) != 0
{ {

View File

@@ -6,7 +6,7 @@ use std::time::Duration;
use rand::{thread_rng, Rng}; use rand::{thread_rng, Rng};
use crate::chat::*; use crate::chat;
use crate::constants::*; use crate::constants::*;
use crate::context::Context; use crate::context::Context;
use crate::dc_configure::*; use crate::dc_configure::*;
@@ -1035,7 +1035,7 @@ pub unsafe fn dc_job_send_msg(context: &Context, msg_id: uint32_t) -> libc::c_in
); );
} else { } else {
// no redo, no IMAP. moreover, as the data does not exist, there is no need in calling dc_set_msg_failed() // no redo, no IMAP. moreover, as the data does not exist, there is no need in calling dc_set_msg_failed()
if msgtype_has_file((*mimefactory.msg).type_0) { if chat::msgtype_has_file((*mimefactory.msg).type_0) {
if let Some(pathNfilename) = (*mimefactory.msg).param.get(Param::File) { if let Some(pathNfilename) = (*mimefactory.msg).param.get(Param::File) {
if ((*mimefactory.msg).type_0 == Viewtype::Image if ((*mimefactory.msg).type_0 == Viewtype::Image
|| (*mimefactory.msg).type_0 == Viewtype::Gif) || (*mimefactory.msg).type_0 == Viewtype::Gif)
@@ -1096,7 +1096,7 @@ pub unsafe fn dc_job_send_msg(context: &Context, msg_id: uint32_t) -> libc::c_in
); );
} }
if 0 != mimefactory.out_gossiped { if 0 != mimefactory.out_gossiped {
dc_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 { if 0 != mimefactory.out_last_added_location_id {
dc_set_kml_sent_timestamp(context, (*mimefactory.msg).chat_id, time()); dc_set_kml_sent_timestamp(context, (*mimefactory.msg).chat_id, time());

View File

@@ -3,7 +3,7 @@ use std::ffi::CString;
use quick_xml; use quick_xml;
use quick_xml::events::{BytesEnd, BytesStart, BytesText}; use quick_xml::events::{BytesEnd, BytesStart, BytesText};
use crate::chat::*; use crate::chat;
use crate::constants::Event; use crate::constants::Event;
use crate::constants::*; use crate::constants::*;
use crate::context::*; use crate::context::*;
@@ -104,7 +104,7 @@ pub unsafe fn dc_send_locations_to_chat(
(*msg).text = (*msg).text =
Some(context.stock_system_msg(StockMessage::MsgLocationEnabled, "", "", 0)); Some(context.stock_system_msg(StockMessage::MsgLocationEnabled, "", "", 0));
(*msg).param.set_int(Param::Cmd, 8); (*msg).param.set_int(Param::Cmd, 8);
dc_send_msg(context, chat_id, msg); chat::send_msg(context, chat_id, msg).unwrap();
} else if 0 == seconds && is_sending_locations_before { } else if 0 == seconds && is_sending_locations_before {
let stock_str = CString::new(context.stock_system_msg( let stock_str = CString::new(context.stock_system_msg(
StockMessage::MsgLocationDisabled, StockMessage::MsgLocationDisabled,
@@ -113,7 +113,7 @@ pub unsafe fn dc_send_locations_to_chat(
0, 0,
)) ))
.unwrap(); .unwrap();
dc_add_device_msg(context, chat_id, stock_str.as_ptr()); chat::add_device_msg(context, chat_id, stock_str.as_ptr());
} }
context.call_cb( context.call_cb(
Event::CHAT_MODIFIED, Event::CHAT_MODIFIED,
@@ -697,7 +697,8 @@ pub unsafe fn dc_job_do_DC_JOB_MAYBE_SEND_LOCATIONS(context: &Context, _job: *mu
let mut msg = dc_msg_new(context, Viewtype::Text); let mut msg = dc_msg_new(context, Viewtype::Text);
(*msg).hidden = 1; (*msg).hidden = 1;
(*msg).param.set_int(Param::Cmd, 9); (*msg).param.set_int(Param::Cmd, 9);
dc_send_msg(context, chat_id as u32, msg); // TODO: handle cleanup on error
chat::send_msg(context, chat_id as u32, msg).unwrap();
dc_msg_unref(msg); dc_msg_unref(msg);
} }
Ok(()) Ok(())
@@ -736,7 +737,7 @@ pub unsafe fn dc_job_do_DC_JOB_MAYBE_SEND_LOC_ENDED(context: &Context, job: &mut
params![chat_id as i32], params![chat_id as i32],
).is_ok() { ).is_ok() {
let stock_str = CString::new(context.stock_system_msg(StockMessage::MsgLocationDisabled, "", "", 0)).unwrap(); let stock_str = CString::new(context.stock_system_msg(StockMessage::MsgLocationDisabled, "", "", 0)).unwrap();
dc_add_device_msg(context, chat_id, stock_str.as_ptr()); chat::add_device_msg(context, chat_id, stock_str.as_ptr());
context.call_cb( context.call_cb(
Event::CHAT_MODIFIED, Event::CHAT_MODIFIED,
chat_id as usize, chat_id as usize,

View File

@@ -135,7 +135,7 @@ pub unsafe fn dc_lot_fill(
(*lot).text1 = context.stock_str(StockMessage::Draft).strdup(); (*lot).text1 = context.stock_str(StockMessage::Draft).strdup();
(*lot).text1_meaning = 1i32 (*lot).text1_meaning = 1i32
} else if (*msg).from_id == 1i32 as libc::c_uint { } else if (*msg).from_id == 1i32 as libc::c_uint {
if 0 != dc_msg_is_info(msg) || 0 != dc_chat_is_self_talk(chat) { if 0 != dc_msg_is_info(msg) || chat.is_self_talk() {
(*lot).text1 = 0 as *mut libc::c_char; (*lot).text1 = 0 as *mut libc::c_char;
(*lot).text1_meaning = 0i32 (*lot).text1_meaning = 0i32
} else { } else {

View File

@@ -10,7 +10,7 @@ use mmime::mailmime_write_mem::*;
use mmime::mmapstring::*; use mmime::mmapstring::*;
use mmime::other::*; use mmime::other::*;
use crate::chat::*; use crate::chat::{self, Chat};
use crate::constants::*; use crate::constants::*;
use crate::contact::*; use crate::contact::*;
use crate::context::{dc_get_version_str, Context}; use crate::context::{dc_get_version_str, Context};
@@ -112,14 +112,14 @@ pub unsafe fn dc_mimefactory_load_msg(
(*factory).msg = dc_msg_new_untyped(context); (*factory).msg = dc_msg_new_untyped(context);
if dc_msg_load_from_db((*factory).msg, context, msg_id) { if dc_msg_load_from_db((*factory).msg, context, msg_id) {
if let Ok(chat) = dc_chat_load_from_db(context, (*(*factory).msg).chat_id) { if let Ok(chat) = Chat::load_from_db(context, (*(*factory).msg).chat_id) {
(*factory).chat = Some(chat); (*factory).chat = Some(chat);
let chat = (*factory).chat.as_ref().unwrap(); let chat = (*factory).chat.as_ref().unwrap();
load_from(factory); load_from(factory);
(*factory).req_mdn = 0; (*factory).req_mdn = 0;
if 0 != dc_chat_is_self_talk(chat) { if chat.is_self_talk() {
clist_insert_after( clist_insert_after(
(*factory).recipients_names, (*factory).recipients_names,
(*(*factory).recipients_names).last, (*(*factory).recipients_names).last,
@@ -830,7 +830,7 @@ pub unsafe fn dc_mimefactory_render(mut factory: *mut dc_mimefactory_t) -> libc:
free(placeholdertext as *mut libc::c_void); free(placeholdertext as *mut libc::c_void);
/* add attachment part */ /* add attachment part */
if msgtype_has_file((*msg).type_0) { if chat::msgtype_has_file((*msg).type_0) {
if !is_file_size_okay(msg) { if !is_file_size_okay(msg) {
let error: *mut libc::c_char = dc_mprintf( let error: *mut libc::c_char = dc_mprintf(
b"Message exceeds the recommended %i MB.\x00" as *const u8 b"Message exceeds the recommended %i MB.\x00" as *const u8
@@ -1146,22 +1146,20 @@ unsafe fn build_body_file(
let mime_fields: *mut mailmime_fields; let mime_fields: *mut mailmime_fields;
let mut mime_sub: *mut mailmime = 0 as *mut mailmime; let mut mime_sub: *mut mailmime = 0 as *mut mailmime;
let content: *mut mailmime_content; let content: *mut mailmime_content;
let pathNfilename = (*msg) let path_filename = (*msg).param.get(Param::File);
.param
.get(Param::File)
.map(|s| s.strdup())
.unwrap_or_else(|| std::ptr::null_mut());
let mut mimetype = (*msg) let mut mimetype = (*msg)
.param .param
.get(Param::MimeType) .get(Param::MimeType)
.map(|s| s.strdup()) .map(|s| s.strdup())
.unwrap_or_else(|| std::ptr::null_mut()); .unwrap_or_else(|| std::ptr::null_mut());
let suffix = dc_get_filesuffix_lc(pathNfilename);
let mut filename_to_send = 0 as *mut libc::c_char; let mut filename_to_send = 0 as *mut libc::c_char;
let mut filename_encoded = 0 as *mut libc::c_char; let mut filename_encoded = 0 as *mut libc::c_char;
if !pathNfilename.is_null() { if let Some(ref path_filename) = path_filename {
let suffix = dc_get_filesuffix_lc(path_filename);
if (*msg).type_0 == Viewtype::Voice { if (*msg).type_0 == Viewtype::Voice {
let ts = chrono::Utc.timestamp((*msg).timestamp_sort as i64, 0); let ts = chrono::Utc.timestamp((*msg).timestamp_sort as i64, 0);
@@ -1175,7 +1173,7 @@ unsafe fn build_body_file(
.to_string(); .to_string();
filename_to_send = res.strdup(); filename_to_send = res.strdup();
} else if (*msg).type_0 == Viewtype::Audio { } else if (*msg).type_0 == Viewtype::Audio {
filename_to_send = dc_get_filename(pathNfilename) filename_to_send = dc_get_filename(path_filename)
} else if (*msg).type_0 == Viewtype::Image || (*msg).type_0 == Viewtype::Gif { } else if (*msg).type_0 == Viewtype::Image || (*msg).type_0 == Viewtype::Gif {
if base_name.is_null() { if base_name.is_null() {
base_name = b"image\x00" as *const u8 as *const libc::c_char base_name = b"image\x00" as *const u8 as *const libc::c_char
@@ -1199,7 +1197,7 @@ unsafe fn build_body_file(
}, },
) )
} else { } else {
filename_to_send = dc_get_filename(pathNfilename) filename_to_send = dc_get_filename(path_filename)
} }
if mimetype.is_null() { if mimetype.is_null() {
if suffix.is_null() { if suffix.is_null() {
@@ -1292,17 +1290,16 @@ unsafe fn build_body_file(
) as *mut libc::c_void, ) as *mut libc::c_void,
); );
mime_sub = mailmime_new_empty(content, mime_fields); mime_sub = mailmime_new_empty(content, mime_fields);
mailmime_set_body_file(mime_sub, dc_get_abs_path((*msg).context, pathNfilename)); mailmime_set_body_file(mime_sub, dc_get_abs_path((*msg).context, path_filename));
if !ret_file_name_as_sent.is_null() { if !ret_file_name_as_sent.is_null() {
*ret_file_name_as_sent = dc_strdup(filename_to_send) *ret_file_name_as_sent = dc_strdup(filename_to_send)
} }
} }
} }
free(pathNfilename as *mut libc::c_void);
free(mimetype as *mut libc::c_void); free(mimetype as *mut libc::c_void);
free(filename_to_send as *mut libc::c_void); free(filename_to_send as *mut libc::c_void);
free(filename_encoded as *mut libc::c_void); free(filename_encoded as *mut libc::c_void);
free(suffix as *mut libc::c_void);
mime_sub mime_sub
} }

View File

@@ -1,7 +1,7 @@
use std::ffi::CString; use std::ffi::CString;
use std::path::Path; use std::path::Path;
use crate::chat::*; use crate::chat::{self, Chat};
use crate::constants::*; use crate::constants::*;
use crate::contact::*; use crate::contact::*;
use crate::context::*; use crate::context::*;
@@ -309,8 +309,7 @@ pub unsafe fn dc_msg_get_file(msg: *const dc_msg_t) -> *mut libc::c_char {
if !msg.is_null() { if !msg.is_null() {
if let Some(file_rel) = (*msg).param.get(Param::File) { if let Some(file_rel) = (*msg).param.get(Param::File) {
let file_rel_c = CString::yolo(file_rel); file_abs = dc_get_abs_path((*msg).context, file_rel);
file_abs = dc_get_abs_path((*msg).context, file_rel_c.as_ptr());
} }
} }
if !file_abs.is_null() { if !file_abs.is_null() {
@@ -681,8 +680,7 @@ pub unsafe fn dc_msg_get_filename(msg: *const dc_msg_t) -> *mut libc::c_char {
if !msg.is_null() { if !msg.is_null() {
if let Some(file) = (*msg).param.get(Param::File) { if let Some(file) = (*msg).param.get(Param::File) {
let file_c = CString::yolo(file); ret = dc_get_filename(file);
ret = dc_get_filename(file_c.as_ptr());
} }
} }
if !ret.is_null() { if !ret.is_null() {
@@ -757,7 +755,7 @@ pub unsafe fn dc_msg_get_summary<'a>(
let chat = if let Some(chat) = chat { let chat = if let Some(chat) = chat {
chat chat
} else { } else {
if let Ok(chat) = dc_get_chat((*msg).context, (*msg).chat_id) { if let Ok(chat) = Chat::load_from_db((*msg).context, (*msg).chat_id) {
chat_loaded = chat; chat_loaded = chat;
&chat_loaded &chat_loaded
} else { } else {
@@ -921,7 +919,7 @@ pub unsafe fn dc_msg_is_increation(msg: *const dc_msg_t) -> libc::c_int {
return 0; return 0;
} }
if msgtype_has_file((*msg).type_0) && (*msg).state == DC_STATE_OUT_PREPARING { if chat::msgtype_has_file((*msg).type_0) && (*msg).state == DC_STATE_OUT_PREPARING {
1 1
} else { } else {
0 0
@@ -1252,7 +1250,7 @@ pub unsafe fn dc_mdn_from_ext(
(S=Sender, R=Recipient) (S=Sender, R=Recipient)
*/ */
// for rounding, SELF is already included! // for rounding, SELF is already included!
let soll_cnt = (dc_get_chat_contact_cnt(context, *ret_chat_id) + 1) / 2; let soll_cnt = (chat::get_chat_contact_cnt(context, *ret_chat_id) + 1) / 2;
if ist_cnt >= soll_cnt { if ist_cnt >= soll_cnt {
dc_update_msg_state(context, *ret_msg_id, DC_STATE_OUT_MDN_RCVD); dc_update_msg_state(context, *ret_msg_id, DC_STATE_OUT_MDN_RCVD);
read_by_all = 1; read_by_all = 1;
@@ -1392,14 +1390,12 @@ mod tests {
let res = ctx.set_config(Config::ConfiguredAddr, Some("self@example.com")); let res = ctx.set_config(Config::ConfiguredAddr, Some("self@example.com"));
assert!(res.is_ok()); assert!(res.is_ok());
let chat = dc_create_chat_by_contact_id(ctx, contact); let chat = chat::create_by_contact_id(ctx, contact).unwrap();
assert!(chat != 0, "failed to create chat");
let msg = dc_msg_new(ctx, Viewtype::Text); let msg = dc_msg_new(ctx, Viewtype::Text);
assert!(!msg.is_null()); assert!(!msg.is_null());
let msg_id = dc_prepare_msg(ctx, chat, msg); let msg_id = chat::prepare_msg(ctx, chat, msg).unwrap();
assert!(msg_id != 0);
let msg2 = dc_get_msg(ctx, msg_id); let msg2 = dc_get_msg(ctx, msg_id);
assert!(!msg2.is_null()); assert!(!msg2.is_null());

View File

@@ -1,6 +1,6 @@
use percent_encoding::percent_decode_str; use percent_encoding::percent_decode_str;
use crate::chat::*; use crate::chat;
use crate::constants::Blocked; use crate::constants::Blocked;
use crate::contact::*; use crate::contact::*;
use crate::context::Context; use crate::context::Context;
@@ -231,13 +231,13 @@ pub unsafe fn dc_check_qr(context: &Context, qr: *const libc::c_char) -> *mut dc
Contact::add_or_lookup(context, "", addr, Origin::UnhandledQrScan) Contact::add_or_lookup(context, "", addr, Origin::UnhandledQrScan)
.map(|(id, _)| id) .map(|(id, _)| id)
.unwrap_or_default(); .unwrap_or_default();
dc_create_or_lookup_nchat_by_contact_id( let (id, _) = chat::create_or_lookup_by_contact_id(
context, context,
(*qr_parsed).id, (*qr_parsed).id,
Blocked::Deaddrop, Blocked::Deaddrop,
&mut chat_id, )
None, .unwrap_or_default();
); chat_id = id;
device_msg = dc_mprintf( device_msg = dc_mprintf(
b"%s verified.\x00" as *const u8 as *const libc::c_char, b"%s verified.\x00" as *const u8 as *const libc::c_char,
peerstate.addr, peerstate.addr,
@@ -288,7 +288,7 @@ pub unsafe fn dc_check_qr(context: &Context, qr: *const libc::c_char) -> *mut dc
(*qr_parsed).text1 = dc_strdup(qr) (*qr_parsed).text1 = dc_strdup(qr)
} }
if !device_msg.is_null() { if !device_msg.is_null() {
dc_add_device_msg(context, chat_id, device_msg); chat::add_device_msg(context, chat_id, device_msg);
} }
} }
} }

View File

@@ -10,7 +10,7 @@ use mmime::mmapstring::*;
use mmime::other::*; use mmime::other::*;
use sha2::{Digest, Sha256}; use sha2::{Digest, Sha256};
use crate::chat::*; use crate::chat::{self, Chat};
use crate::constants::*; use crate::constants::*;
use crate::contact::*; use crate::contact::*;
use crate::context::Context; use crate::context::Context;
@@ -408,15 +408,9 @@ unsafe fn add_parts(
state = DC_STATE_IN_SEEN; state = DC_STATE_IN_SEEN;
} }
} }
let mut test_normal_chat_id = 0;
let mut test_normal_chat_id_blocked = Blocked::Not;
dc_lookup_real_nchat_by_contact_id( let (test_normal_chat_id, test_normal_chat_id_blocked) =
context, chat::lookup_by_contact_id(context, *from_id).unwrap_or_default();
*from_id,
&mut test_normal_chat_id,
&mut test_normal_chat_id_blocked,
);
// get the chat_id - a chat_id here is no indicator that the chat is displayed in the normal list, // get the chat_id - a chat_id here is no indicator that the chat is displayed in the normal list,
// it might also be blocked and displayed in the deaddrop as a result // it might also be blocked and displayed in the deaddrop as a result
@@ -444,7 +438,7 @@ unsafe fn add_parts(
&mut chat_id_blocked, &mut chat_id_blocked,
); );
if 0 != *chat_id && Blocked::Not != chat_id_blocked && create_blocked == Blocked::Not { if 0 != *chat_id && Blocked::Not != chat_id_blocked && create_blocked == Blocked::Not {
dc_unblock_chat(context, *chat_id); chat::unblock(context, *chat_id);
chat_id_blocked = Blocked::Not; chat_id_blocked = Blocked::Not;
} }
} }
@@ -472,17 +466,15 @@ unsafe fn add_parts(
*chat_id = test_normal_chat_id; *chat_id = test_normal_chat_id;
chat_id_blocked = test_normal_chat_id_blocked; chat_id_blocked = test_normal_chat_id_blocked;
} else if 0 != allow_creation { } else if 0 != allow_creation {
dc_create_or_lookup_nchat_by_contact_id( let (id, bl) =
context, chat::create_or_lookup_by_contact_id(context, *from_id, create_blocked)
*from_id, .unwrap_or_default();
create_blocked, *chat_id = id;
chat_id, chat_id_blocked = bl;
Some(&mut chat_id_blocked),
);
} }
if 0 != *chat_id && Blocked::Not != chat_id_blocked { if 0 != *chat_id && Blocked::Not != chat_id_blocked {
if Blocked::Not == create_blocked { if Blocked::Not == create_blocked {
dc_unblock_chat(context, *chat_id); chat::unblock(context, *chat_id);
chat_id_blocked = Blocked::Not; chat_id_blocked = Blocked::Not;
} else if 0 != dc_is_reply_to_known_message(context, mime_parser) { } else if 0 != dc_is_reply_to_known_message(context, mime_parser) {
// we do not want any chat to be created implicitly. Because of the origin-scale-up, // we do not want any chat to be created implicitly. Because of the origin-scale-up,
@@ -532,7 +524,7 @@ unsafe fn add_parts(
&mut chat_id_blocked, &mut chat_id_blocked,
); );
if 0 != *chat_id && Blocked::Not != chat_id_blocked { if 0 != *chat_id && Blocked::Not != chat_id_blocked {
dc_unblock_chat(context, *chat_id); chat::unblock(context, *chat_id);
chat_id_blocked = Blocked::Not; chat_id_blocked = Blocked::Not;
} }
} }
@@ -542,18 +534,17 @@ unsafe fn add_parts(
} else { } else {
Blocked::Deaddrop Blocked::Deaddrop
}; };
dc_create_or_lookup_nchat_by_contact_id( let (id, bl) =
context, chat::create_or_lookup_by_contact_id(context, *to_id, create_blocked)
*to_id, .unwrap_or_default();
create_blocked, *chat_id = id;
chat_id, chat_id_blocked = bl;
Some(&mut chat_id_blocked),
);
if 0 != *chat_id if 0 != *chat_id
&& Blocked::Not != chat_id_blocked && Blocked::Not != chat_id_blocked
&& Blocked::Not == create_blocked && Blocked::Not == create_blocked
{ {
dc_unblock_chat(context, *chat_id); chat::unblock(context, *chat_id);
chat_id_blocked = Blocked::Not; chat_id_blocked = Blocked::Not;
} }
} }
@@ -562,15 +553,13 @@ unsafe fn add_parts(
if to_ids.is_empty() && 0 != to_self { if to_ids.is_empty() && 0 != to_self {
// from_id==to_id==DC_CONTACT_ID_SELF - this is a self-sent messages, // from_id==to_id==DC_CONTACT_ID_SELF - this is a self-sent messages,
// maybe an Autocrypt Setup Messag // maybe an Autocrypt Setup Messag
dc_create_or_lookup_nchat_by_contact_id( let (id, bl) = chat::create_or_lookup_by_contact_id(context, 1, Blocked::Not)
context, .unwrap_or_default();
1, *chat_id = id;
Blocked::Not, chat_id_blocked = bl;
chat_id,
Some(&mut chat_id_blocked),
);
if 0 != *chat_id && Blocked::Not != chat_id_blocked { if 0 != *chat_id && Blocked::Not != chat_id_blocked {
dc_unblock_chat(context, *chat_id); chat::unblock(context, *chat_id);
chat_id_blocked = Blocked::Not; chat_id_blocked = Blocked::Not;
} }
} }
@@ -593,7 +582,7 @@ unsafe fn add_parts(
); );
// unarchive chat // unarchive chat
dc_unarchive_chat(context, *chat_id); chat::unarchive(context, *chat_id).unwrap();
// if the mime-headers should be saved, find out its size // if the mime-headers should be saved, find out its size
// (the mime-header ends with an empty line) // (the mime-header ends with an empty line)
@@ -1236,7 +1225,7 @@ unsafe fn create_or_lookup_group(
set_better_msg(mime_parser, &better_msg); set_better_msg(mime_parser, &better_msg);
// check, if we have a chat with this group ID // check, if we have a chat with this group ID
chat_id = dc_get_chat_id_by_grpid( chat_id = chat::get_chat_id_by_grpid(
context, context,
grpid, grpid,
Some(&mut chat_id_blocked), Some(&mut chat_id_blocked),
@@ -1258,12 +1247,12 @@ unsafe fn create_or_lookup_group(
// check if the sender is a member of the existing group - // check if the sender is a member of the existing group -
// if not, we'll recreate the group list // if not, we'll recreate the group list
if chat_id != 0 && 0 == dc_is_contact_in_chat(context, chat_id, from_id as u32) { if chat_id != 0 && 0 == chat::is_contact_in_chat(context, chat_id, from_id as u32) {
recreate_member_list = 1; recreate_member_list = 1;
} }
// check if the group does not exist but should be created // check if the group does not exist but should be created
group_explicitly_left = dc_is_group_explicitly_left(context, grpid); group_explicitly_left = chat::is_group_explicitly_left(context, grpid);
let self_addr = context let self_addr = context
.sql .sql
@@ -1385,13 +1374,13 @@ unsafe fn create_or_lookup_group(
to_string(grpimage) to_string(grpimage)
}, },
); );
if let Ok(mut chat) = dc_chat_load_from_db(context, chat_id) { if let Ok(mut chat) = Chat::load_from_db(context, chat_id) {
if grpimage.is_null() { if grpimage.is_null() {
chat.param.remove(Param::ProfileImage); chat.param.remove(Param::ProfileImage);
} else { } else {
chat.param.set(Param::ProfileImage, as_str(grpimage)); chat.param.set(Param::ProfileImage, as_str(grpimage));
} }
dc_chat_update_param(&mut chat); chat.update_param().unwrap();
send_EVENT_CHAT_MODIFIED = 1; send_EVENT_CHAT_MODIFIED = 1;
} }
@@ -1419,14 +1408,14 @@ unsafe fn create_or_lookup_group(
) )
.ok(); .ok();
if skip.is_null() || !addr_cmp(&self_addr, as_str(skip)) { if skip.is_null() || !addr_cmp(&self_addr, as_str(skip)) {
dc_add_to_chat_contacts_table(context, chat_id, DC_CONTACT_ID_SELF as u32); chat::add_to_chat_contacts_table(context, chat_id, DC_CONTACT_ID_SELF as u32);
} }
if from_id > DC_CHAT_ID_LAST_SPECIAL as u32 { if from_id > DC_CHAT_ID_LAST_SPECIAL as u32 {
if !Contact::addr_equals_contact(context, &self_addr, from_id as u32) if !Contact::addr_equals_contact(context, &self_addr, from_id as u32)
&& (skip.is_null() && (skip.is_null()
|| !Contact::addr_equals_contact(context, to_string(skip), from_id as u32)) || !Contact::addr_equals_contact(context, to_string(skip), from_id as u32))
{ {
dc_add_to_chat_contacts_table(context, chat_id, from_id as u32); chat::add_to_chat_contacts_table(context, chat_id, from_id as u32);
} }
} }
for &to_id in to_ids.iter() { for &to_id in to_ids.iter() {
@@ -1434,11 +1423,11 @@ unsafe fn create_or_lookup_group(
&& (skip.is_null() && (skip.is_null()
|| !Contact::addr_equals_contact(context, to_string(skip), to_id)) || !Contact::addr_equals_contact(context, to_string(skip), to_id))
{ {
dc_add_to_chat_contacts_table(context, chat_id, to_id); chat::add_to_chat_contacts_table(context, chat_id, to_id);
} }
} }
send_EVENT_CHAT_MODIFIED = 1; send_EVENT_CHAT_MODIFIED = 1;
dc_reset_gossiped_timestamp(context, chat_id); chat::reset_gossiped_timestamp(context, chat_id);
} }
if 0 != send_EVENT_CHAT_MODIFIED { if 0 != send_EVENT_CHAT_MODIFIED {
@@ -1448,7 +1437,7 @@ unsafe fn create_or_lookup_group(
// check the number of receivers - // check the number of receivers -
// the only critical situation is if the user hits "Reply" instead of "Reply all" in a non-messenger-client */ // the only critical situation is if the user hits "Reply" instead of "Reply all" in a non-messenger-client */
if to_ids_cnt == 1 && mime_parser.is_send_by_messenger == 0 { if to_ids_cnt == 1 && mime_parser.is_send_by_messenger == 0 {
let is_contact_cnt = dc_get_chat_contact_cnt(context, chat_id); let is_contact_cnt = chat::get_chat_contact_cnt(context, chat_id);
if is_contact_cnt > 3 { if is_contact_cnt > 3 {
// to_ids_cnt==1 may be "From: A, To: B, SELF" as SELF is not counted in to_ids_cnt. // to_ids_cnt==1 may be "From: A, To: B, SELF" as SELF is not counted in to_ids_cnt.
// So everything up to 3 is no error. // So everything up to 3 is no error.
@@ -1617,7 +1606,7 @@ unsafe fn create_or_lookup_adhoc_group(
chat_id = create_group_record(context, grpid, grpname, create_blocked, 0); chat_id = create_group_record(context, grpid, grpname, create_blocked, 0);
chat_id_blocked = create_blocked; chat_id_blocked = create_blocked;
for &member_id in &member_ids { for &member_id in &member_ids {
dc_add_to_chat_contacts_table(context, chat_id, member_id); chat::add_to_chat_contacts_table(context, chat_id, member_id);
} }
context.call_cb(Event::CHAT_MODIFIED, chat_id as uintptr_t, 0 as uintptr_t); context.call_cb(Event::CHAT_MODIFIED, chat_id as uintptr_t, 0 as uintptr_t);

View File

@@ -4,7 +4,7 @@ use mmime::mailimf_types::*;
use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC}; use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};
use crate::aheader::EncryptPreference; use crate::aheader::EncryptPreference;
use crate::chat::*; use crate::chat::{self, Chat};
use crate::constants::*; use crate::constants::*;
use crate::contact::*; use crate::contact::*;
use crate::context::Context; use crate::context::Context;
@@ -88,8 +88,8 @@ pub unsafe fn dc_get_securejoin_qr(
let self_name_urlencoded = utf8_percent_encode(&self_name, NON_ALPHANUMERIC).to_string(); let self_name_urlencoded = utf8_percent_encode(&self_name, NON_ALPHANUMERIC).to_string();
qr = if 0 != group_chat_id { qr = if 0 != group_chat_id {
if let Ok(chat) = dc_get_chat(context, group_chat_id) { if let Ok(chat) = Chat::load_from_db(context, group_chat_id) {
group_name = dc_chat_get_name(&chat); group_name = chat.get_name();
group_name_urlencoded = dc_urlencode(group_name); group_name_urlencoded = dc_urlencode(group_name);
Some(format!( Some(format!(
@@ -152,7 +152,8 @@ pub unsafe fn dc_join_securejoin(context: &Context, qr: *const libc::c_char) ->
if qr_scan.is_null() || (*qr_scan).state != 200i32 && (*qr_scan).state != 202i32 { if qr_scan.is_null() || (*qr_scan).state != 200i32 && (*qr_scan).state != 202i32 {
error!(context, 0, "Unknown QR code.",); error!(context, 0, "Unknown QR code.",);
} else { } else {
contact_chat_id = dc_create_chat_by_contact_id(context, (*qr_scan).id); contact_chat_id =
chat::create_by_contact_id(context, (*qr_scan).id).unwrap_or_default();
if contact_chat_id == 0i32 as libc::c_uint { if contact_chat_id == 0i32 as libc::c_uint {
error!(context, 0, "Unknown contact.",); error!(context, 0, "Unknown contact.",);
} else if !(context } else if !(context
@@ -232,7 +233,7 @@ pub unsafe fn dc_join_securejoin(context: &Context, qr: *const libc::c_char) ->
if bob.status == 1 { if bob.status == 1 {
if 0 != join_vg { if 0 != join_vg {
ret_chat_id = ret_chat_id =
dc_get_chat_id_by_grpid(context, (*qr_scan).text2, None, 0 as *mut libc::c_int) chat::get_chat_id_by_grpid(context, (*qr_scan).text2, None, 0 as *mut libc::c_int)
as libc::c_int as libc::c_int
} else { } else {
ret_chat_id = contact_chat_id as libc::c_int ret_chat_id = contact_chat_id as libc::c_int
@@ -284,12 +285,13 @@ unsafe fn send_handshake_msg(
} else { } else {
(*msg).param.set_int(Param::GuranteeE2ee, 1); (*msg).param.set_int(Param::GuranteeE2ee, 1);
} }
dc_send_msg(context, contact_chat_id, msg); // TODO. handle cleanup on error
chat::send_msg(context, contact_chat_id, msg).unwrap();
dc_msg_unref(msg); dc_msg_unref(msg);
} }
unsafe fn chat_id_2_contact_id(context: &Context, contact_chat_id: uint32_t) -> uint32_t { unsafe fn chat_id_2_contact_id(context: &Context, contact_chat_id: uint32_t) -> uint32_t {
let contacts = dc_get_chat_contacts(context, contact_chat_id); let contacts = chat::get_chat_contacts(context, contact_chat_id);
if contacts.len() == 1 { if contacts.len() == 1 {
contacts[0] contacts[0]
} else { } else {
@@ -306,7 +308,7 @@ unsafe fn fingerprint_equals_sender(
return 0; return 0;
} }
let mut fingerprint_equal: libc::c_int = 0i32; let mut fingerprint_equal: libc::c_int = 0i32;
let contacts = dc_get_chat_contacts(context, contact_chat_id); let contacts = chat::get_chat_contacts(context, contact_chat_id);
if contacts.len() == 1 { if contacts.len() == 1 {
if let Ok(contact) = Contact::load_from_db(context, contacts[0]) { if let Ok(contact) = Contact::load_from_db(context, contacts[0]) {
@@ -339,8 +341,8 @@ pub unsafe fn dc_handle_securejoin_handshake(
let mut scanned_fingerprint_of_alice: *mut libc::c_char = 0 as *mut libc::c_char; let mut scanned_fingerprint_of_alice: *mut libc::c_char = 0 as *mut libc::c_char;
let mut auth: *mut libc::c_char = 0 as *mut libc::c_char; let mut auth: *mut libc::c_char = 0 as *mut libc::c_char;
let mut own_fingerprint: *mut libc::c_char = 0 as *mut libc::c_char; let mut own_fingerprint: *mut libc::c_char = 0 as *mut libc::c_char;
let mut contact_chat_id: uint32_t = 0i32 as uint32_t; let contact_chat_id: u32;
let mut contact_chat_id_blocked = Blocked::Not; let contact_chat_id_blocked: Blocked;
let mut grpid: *mut libc::c_char = 0 as *mut libc::c_char; let mut grpid: *mut libc::c_char = 0 as *mut libc::c_char;
let mut ret: libc::c_int = 0i32; let mut ret: libc::c_int = 0i32;
@@ -355,15 +357,13 @@ pub unsafe fn dc_handle_securejoin_handshake(
); );
join_vg = (strncmp(step, b"vg-\x00" as *const u8 as *const libc::c_char, 3) == 0) join_vg = (strncmp(step, b"vg-\x00" as *const u8 as *const libc::c_char, 3) == 0)
as libc::c_int; as libc::c_int;
dc_create_or_lookup_nchat_by_contact_id( let (id, bl) = chat::create_or_lookup_by_contact_id(context, contact_id, Blocked::Not)
context, .unwrap_or_default();
contact_id, contact_chat_id = id;
Blocked::Not, contact_chat_id_blocked = bl;
&mut contact_chat_id,
Some(&mut contact_chat_id_blocked),
);
if Blocked::Not != contact_chat_id_blocked { if Blocked::Not != contact_chat_id_blocked {
dc_unblock_chat(context, contact_chat_id); chat::unblock(context, contact_chat_id);
} }
ret = 0x2i32; ret = 0x2i32;
if strcmp(step, b"vg-request\x00" as *const u8 as *const libc::c_char) == 0i32 if strcmp(step, b"vg-request\x00" as *const u8 as *const libc::c_char) == 0i32
@@ -574,7 +574,7 @@ pub unsafe fn dc_handle_securejoin_handshake(
); );
if 0 != join_vg { if 0 != join_vg {
grpid = dc_strdup(lookup_field(mimeparser, "Secure-Join-Group")); grpid = dc_strdup(lookup_field(mimeparser, "Secure-Join-Group"));
let group_chat_id: uint32_t = dc_get_chat_id_by_grpid( let group_chat_id: uint32_t = chat::get_chat_id_by_grpid(
context, context,
grpid, grpid,
None, None,
@@ -584,7 +584,7 @@ pub unsafe fn dc_handle_securejoin_handshake(
error!(context, 0, "Chat {} not found.", as_str(grpid),); error!(context, 0, "Chat {} not found.", as_str(grpid),);
current_block = 4378276786830486580; current_block = 4378276786830486580;
} else { } else {
dc_add_contact_to_chat_ex( chat::add_contact_to_chat_ex(
context, context,
group_chat_id, group_chat_id,
contact_id, contact_id,
@@ -647,7 +647,12 @@ pub unsafe fn dc_handle_securejoin_handshake(
let mut vg_expect_encrypted: libc::c_int = 1i32; let mut vg_expect_encrypted: libc::c_int = 1i32;
if 0 != join_vg { if 0 != join_vg {
let mut is_verified_group: libc::c_int = 0i32; let mut is_verified_group: libc::c_int = 0i32;
dc_get_chat_id_by_grpid(context, grpid, None, &mut is_verified_group); chat::get_chat_id_by_grpid(
context,
grpid,
None,
&mut is_verified_group,
);
if 0 == is_verified_group { if 0 == is_verified_group {
vg_expect_encrypted = 0i32 vg_expect_encrypted = 0i32
} }
@@ -803,7 +808,7 @@ unsafe fn secure_connection_established(context: &Context, contact_chat_id: uint
}; };
let msg = let msg =
CString::new(context.stock_string_repl_str(StockMessage::ContactVerified, addr)).unwrap(); CString::new(context.stock_string_repl_str(StockMessage::ContactVerified, addr)).unwrap();
dc_add_device_msg(context, contact_chat_id, msg.as_ptr()); chat::add_device_msg(context, contact_chat_id, msg.as_ptr());
context.call_cb( context.call_cb(
Event::CHAT_MODIFIED, Event::CHAT_MODIFIED,
contact_chat_id as uintptr_t, contact_chat_id as uintptr_t,
@@ -844,7 +849,7 @@ unsafe fn could_not_establish_secure_connection(
}, },
); );
let msg_c = CString::new(msg.as_str()).unwrap(); let msg_c = CString::new(msg.as_str()).unwrap();
dc_add_device_msg(context, contact_chat_id, msg_c.as_ptr()); chat::add_device_msg(context, contact_chat_id, msg_c.as_ptr());
error!(context, 0, "{} ({})", msg, as_str(details)); error!(context, 0, "{} ({})", msg, as_str(details));
} }
@@ -906,8 +911,6 @@ unsafe fn encrypted_and_signed(
} }
pub unsafe fn dc_handle_degrade_event(context: &Context, peerstate: &Peerstate) { pub unsafe fn dc_handle_degrade_event(context: &Context, peerstate: &Peerstate) {
let mut contact_chat_id = 0;
// - we do not issue an warning for DC_DE_ENCRYPTION_PAUSED as this is quite normal // - we do not issue an warning for DC_DE_ENCRYPTION_PAUSED as this is quite normal
// - currently, we do not issue an extra warning for DC_DE_VERIFICATION_LOST - this always comes // - currently, we do not issue an extra warning for DC_DE_VERIFICATION_LOST - this always comes
// together with DC_DE_FINGERPRINT_CHANGED which is logged, the idea is not to bother // together with DC_DE_FINGERPRINT_CHANGED which is logged, the idea is not to bother
@@ -924,13 +927,10 @@ pub unsafe fn dc_handle_degrade_event(context: &Context, peerstate: &Peerstate)
) )
.unwrap_or_default(); .unwrap_or_default();
if contact_id > 0 { if contact_id > 0 {
dc_create_or_lookup_nchat_by_contact_id( let (contact_chat_id, _) =
context, chat::create_or_lookup_by_contact_id(context, contact_id as u32, Blocked::Deaddrop)
contact_id as u32, .unwrap_or_default();
Blocked::Deaddrop,
&mut contact_chat_id,
None,
);
let peeraddr: &str = match peerstate.addr { let peeraddr: &str = match peerstate.addr {
Some(ref addr) => &addr, Some(ref addr) => &addr,
None => "", None => "",
@@ -939,7 +939,7 @@ pub unsafe fn dc_handle_degrade_event(context: &Context, peerstate: &Peerstate)
context.stock_string_repl_str(StockMessage::ContactSetupChanged, peeraddr), context.stock_string_repl_str(StockMessage::ContactSetupChanged, peeraddr),
) )
.unwrap(); .unwrap();
dc_add_device_msg(context, contact_chat_id, msg.as_ptr()); chat::add_device_msg(context, contact_chat_id, msg.as_ptr());
context.call_cb( context.call_cb(
Event::CHAT_MODIFIED, Event::CHAT_MODIFIED,
contact_chat_id as uintptr_t, contact_chat_id as uintptr_t,

View File

@@ -1,5 +1,6 @@
use std::borrow::Cow; use std::borrow::Cow;
use std::ffi::{CStr, CString}; use std::ffi::{CStr, CString};
use std::path::Path;
use std::str::FromStr; use std::str::FromStr;
use std::time::SystemTime; use std::time::SystemTime;
use std::{fmt, fs, ptr}; use std::{fmt, fs, ptr};
@@ -637,7 +638,7 @@ pub unsafe fn dc_smeared_time(context: &Context) -> i64 {
now now
} }
pub unsafe fn dc_create_smeared_timestamp(context: &Context) -> i64 { pub fn dc_create_smeared_timestamp(context: &Context) -> i64 {
let now = time(); let now = time();
let mut ret = now; let mut ret = now;
@@ -652,7 +653,7 @@ pub unsafe fn dc_create_smeared_timestamp(context: &Context) -> i64 {
ret ret
} }
pub unsafe fn dc_create_smeared_timestamps(context: &Context, count: libc::c_int) -> i64 { pub fn dc_create_smeared_timestamps(context: &Context, count: libc::c_int) -> i64 {
/* get a range to timestamps that can be used uniquely */ /* get a range to timestamps that can be used uniquely */
let now = time(); let now = time();
let start = now + (if count < 5 { count } else { 5 }) as i64 - count as i64; let start = now + (if count < 5 { count } else { 5 }) as i64 - count as i64;
@@ -848,17 +849,11 @@ unsafe fn dc_validate_filename(filename: *mut libc::c_char) {
} }
} }
#[allow(non_snake_case)] pub unsafe fn dc_get_filename(path_filename: impl AsRef<str>) -> *mut libc::c_char {
pub unsafe fn dc_get_filename(pathNfilename: *const libc::c_char) -> *mut libc::c_char { if let Some(p) = Path::new(path_filename.as_ref()).file_name() {
let mut p: *const libc::c_char = strrchr(pathNfilename, '/' as i32); p.to_string_lossy().strdup()
if p.is_null() {
p = strrchr(pathNfilename, '\\' as i32)
}
if !p.is_null() {
p = p.offset(1isize);
dc_strdup(p)
} else { } else {
dc_strdup(pathNfilename) ptr::null_mut()
} }
} }
@@ -869,12 +864,15 @@ unsafe fn dc_split_filename(
ret_basename: *mut *mut libc::c_char, ret_basename: *mut *mut libc::c_char,
ret_all_suffixes_incl_dot: *mut *mut libc::c_char, ret_all_suffixes_incl_dot: *mut *mut libc::c_char,
) { ) {
if pathNfilename.is_null() {
return;
}
/* splits a filename into basename and all suffixes, eg. "/path/foo.tar.gz" is split into "foo.tar" and ".gz", /* splits a filename into basename and all suffixes, eg. "/path/foo.tar.gz" is split into "foo.tar" and ".gz",
(we use the _last_ dot which allows the usage inside the filename which are very usual; (we use the _last_ dot which allows the usage inside the filename which are very usual;
maybe the detection could be more intelligent, however, for the moment, it is just file) maybe the detection could be more intelligent, however, for the moment, it is just file)
- if there is no suffix, the returned suffix string is empty, eg. "/path/foobar" is split into "foobar" and "" - if there is no suffix, the returned suffix string is empty, eg. "/path/foobar" is split into "foobar" and ""
- the case of the returned suffix is preserved; this is to allow reconstruction of (similar) names */ - the case of the returned suffix is preserved; this is to allow reconstruction of (similar) names */
let basename: *mut libc::c_char = dc_get_filename(pathNfilename); let basename: *mut libc::c_char = dc_get_filename(as_str(pathNfilename));
let suffix: *mut libc::c_char; let suffix: *mut libc::c_char;
let p1: *mut libc::c_char = strrchr(basename, '.' as i32); let p1: *mut libc::c_char = strrchr(basename, '.' as i32);
if !p1.is_null() { if !p1.is_null() {
@@ -897,16 +895,12 @@ unsafe fn dc_split_filename(
// the returned suffix is lower-case // the returned suffix is lower-case
#[allow(non_snake_case)] #[allow(non_snake_case)]
pub unsafe fn dc_get_filesuffix_lc(pathNfilename: *const libc::c_char) -> *mut libc::c_char { pub unsafe fn dc_get_filesuffix_lc(path_filename: impl AsRef<str>) -> *mut libc::c_char {
if !pathNfilename.is_null() { if let Some(p) = Path::new(path_filename.as_ref()).extension() {
let mut p: *const libc::c_char = strrchr(pathNfilename, '.' as i32); p.to_string_lossy().to_lowercase().strdup()
if !p.is_null() { } else {
p = p.offset(1isize); ptr::null_mut()
return dc_strlower(p);
}
} }
ptr::null_mut()
} }
/// Returns the `(width, height)` of the given image buffer. /// Returns the `(width, height)` of the given image buffer.
@@ -936,34 +930,25 @@ pub fn dc_get_abs_path_safe<P: AsRef<std::path::Path>>(
} }
} }
#[allow(non_snake_case)]
pub unsafe fn dc_get_abs_path( pub unsafe fn dc_get_abs_path(
context: &Context, context: &Context,
pathNfilename: *const libc::c_char, path_filename: impl AsRef<str>,
) -> *mut libc::c_char { ) -> *mut libc::c_char {
if pathNfilename.is_null() { let starts = path_filename.as_ref().starts_with("$BLOBDIR");
return ptr::null_mut();
}
let starts = strncmp(
pathNfilename,
b"$BLOBDIR\x00" as *const u8 as *const libc::c_char,
8,
) == 0;
if starts && !context.has_blobdir() { if starts && !context.has_blobdir() {
return ptr::null_mut(); return ptr::null_mut();
} }
let mut pathNfilename_abs: *mut libc::c_char = dc_strdup(pathNfilename); let mut path_filename_abs = path_filename.as_ref().strdup();
if starts && context.has_blobdir() { if starts && context.has_blobdir() {
dc_str_replace( dc_str_replace(
&mut pathNfilename_abs, &mut path_filename_abs,
b"$BLOBDIR\x00" as *const u8 as *const libc::c_char, b"$BLOBDIR\x00" as *const u8 as *const libc::c_char,
context.get_blobdir(), context.get_blobdir(),
); );
} }
pathNfilename_abs path_filename_abs
} }
pub fn dc_file_exist(context: &Context, path: impl AsRef<std::path::Path>) -> bool { pub fn dc_file_exist(context: &Context, path: impl AsRef<std::path::Path>) -> bool {
@@ -1159,56 +1144,49 @@ pub unsafe fn dc_get_fine_pathNfilename(
ret ret
} }
pub unsafe fn dc_is_blobdir_path(context: &Context, path: *const libc::c_char) -> bool { pub fn dc_is_blobdir_path(context: &Context, path: impl AsRef<str>) -> bool {
strncmp(path, context.get_blobdir(), strlen(context.get_blobdir())) == 0 path.as_ref().starts_with(as_str(context.get_blobdir()))
|| strncmp(path, b"$BLOBDIR\x00" as *const u8 as *const libc::c_char, 8) == 0 || path.as_ref().starts_with("$BLOBDIR")
} }
unsafe fn dc_make_rel_path(context: &Context, path: *mut *mut libc::c_char) { fn dc_make_rel_path(context: &Context, path: &mut String) {
if path.is_null() || (*path).is_null() { if path.starts_with(as_str(context.get_blobdir())) {
return; *path = path.replace("$BLOBDIR", as_str(context.get_blobdir()));
} }
if strncmp(*path, context.get_blobdir(), strlen(context.get_blobdir())) == 0 {
dc_str_replace(
path,
context.get_blobdir(),
b"$BLOBDIR\x00" as *const u8 as *const libc::c_char,
);
};
} }
pub unsafe fn dc_make_rel_and_copy(context: &Context, path: *mut *mut libc::c_char) -> bool { pub fn dc_make_rel_and_copy(context: &Context, path: &mut String) -> bool {
let mut success = false; let mut success = false;
let mut filename: *mut libc::c_char = ptr::null_mut(); let mut filename = ptr::null_mut();
let mut blobdir_path: *mut libc::c_char = ptr::null_mut(); let mut blobdir_path = ptr::null_mut();
if !(path.is_null() || (*path).is_null()) { if dc_is_blobdir_path(context, &path) {
if dc_is_blobdir_path(context, *path) { dc_make_rel_path(context, path);
dc_make_rel_path(context, path); success = true;
success = true; } else {
} else { filename = unsafe { dc_get_filename(&path) };
filename = dc_get_filename(*path); if !(filename.is_null()
if !(filename.is_null() || {
|| { blobdir_path = unsafe {
blobdir_path = dc_get_fine_pathNfilename( dc_get_fine_pathNfilename(
context, context,
b"$BLOBDIR\x00" as *const u8 as *const libc::c_char, b"$BLOBDIR\x00" as *const u8 as *const libc::c_char,
filename, filename,
); )
blobdir_path.is_null() };
} blobdir_path.is_null()
|| !dc_copy_file(context, as_path(*path), as_path(blobdir_path)))
{
free(*path as *mut libc::c_void);
*path = blobdir_path;
blobdir_path = ptr::null_mut();
dc_make_rel_path(context, path);
success = true;
} }
|| !dc_copy_file(context, &path, as_path(blobdir_path)))
{
*path = to_string(blobdir_path);
blobdir_path = ptr::null_mut();
dc_make_rel_path(context, path);
success = true;
} }
} }
free(blobdir_path as *mut libc::c_void); unsafe {
free(filename as *mut libc::c_void); free(blobdir_path.cast());
free(filename.cast());
}
success success
} }

View File

@@ -466,7 +466,7 @@ impl<'a> Peerstate<'a> {
} }
if self.to_save == Some(ToSave::All) || create { if self.to_save == Some(ToSave::All) || create {
dc_reset_gossiped_timestamp(self.context, 0); reset_gossiped_timestamp(self.context, 0);
} }
success success

View File

@@ -6,7 +6,7 @@ use std::ffi::CString;
use mmime::mailimf_types::*; use mmime::mailimf_types::*;
use tempfile::{tempdir, TempDir}; use tempfile::{tempdir, TempDir};
use deltachat::chat::*; use deltachat::chat::{self, Chat};
use deltachat::config; use deltachat::config;
use deltachat::constants::*; use deltachat::constants::*;
use deltachat::contact::*; use deltachat::contact::*;
@@ -66,15 +66,9 @@ unsafe fn stress_functions(context: &Context) {
context.get_blobdir(), context.get_blobdir(),
b"foobar\x00" as *const u8 as *const libc::c_char, b"foobar\x00" as *const u8 as *const libc::c_char,
); );
assert!(dc_is_blobdir_path(context, abs_path)); assert!(dc_is_blobdir_path(context, as_str(abs_path)));
assert!(dc_is_blobdir_path( assert!(dc_is_blobdir_path(context, "$BLOBDIR/fofo",));
context, assert!(!dc_is_blobdir_path(context, "/BLOBDIR/fofo",));
b"$BLOBDIR/fofo\x00" as *const u8 as *const libc::c_char,
));
assert!(!dc_is_blobdir_path(
context,
b"/BLOBDIR/fofo\x00" as *const u8 as *const libc::c_char,
));
assert!(dc_file_exist(context, as_path(abs_path))); assert!(dc_file_exist(context, as_path(abs_path)));
free(abs_path as *mut libc::c_void); free(abs_path as *mut libc::c_void);
assert!(dc_copy_file(context, "$BLOBDIR/foobar", "$BLOBDIR/dada",)); assert!(dc_copy_file(context, "$BLOBDIR/foobar", "$BLOBDIR/dada",));
@@ -765,13 +759,13 @@ fn test_chat() {
let contact1 = Contact::create(&context.ctx, "bob", "bob@mail.de").unwrap(); let contact1 = Contact::create(&context.ctx, "bob", "bob@mail.de").unwrap();
assert_ne!(contact1, 0); assert_ne!(contact1, 0);
let chat_id = dc_create_chat_by_contact_id(&context.ctx, contact1); let chat_id = chat::create_by_contact_id(&context.ctx, contact1).unwrap();
assert!(chat_id > 9, "chat_id too small {}", chat_id); assert!(chat_id > 9, "chat_id too small {}", chat_id);
let chat = dc_chat_load_from_db(&context.ctx, chat_id).unwrap(); let chat = Chat::load_from_db(&context.ctx, chat_id).unwrap();
let chat2_id = dc_create_chat_by_contact_id(&context.ctx, contact1); let chat2_id = chat::create_by_contact_id(&context.ctx, contact1).unwrap();
assert_eq!(chat2_id, chat_id); assert_eq!(chat2_id, chat_id);
let chat2 = dc_chat_load_from_db(&context.ctx, chat2_id).unwrap(); let chat2 = Chat::load_from_db(&context.ctx, chat2_id).unwrap();
assert_eq!(as_str(chat2.name), as_str(chat.name)); assert_eq!(as_str(chat2.name), as_str(chat.name));
} }