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

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 {
chat
} 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
} else {

View File

@@ -138,7 +138,7 @@ pub enum Modifier {
Created,
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[derive(Debug, PartialEq, Eq, Clone, Copy, FromPrimitive)]
#[repr(u8)]
pub enum VerifiedStatus {
/// 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 l2 = dc_loginparam_read(context, &context.sql, "configured_");
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 deaddrop_msgs = dc_get_deaddrop_msg_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 rand::{thread_rng, Rng};
use crate::chat::*;
use crate::chat;
use crate::config::Config;
use crate::constants::*;
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(),
))
{
let chat_id = dc_create_chat_by_contact_id(context, 1i32 as uint32_t);
if !(chat_id == 0i32 as libc::c_uint) {
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));
@@ -157,8 +156,7 @@ pub unsafe fn dc_initiate_key_transfer(context: &Context) -> *mut libc::c_char {
.unwrap()
.shall_stop_ongoing
{
let msg_id = dc_send_msg(context, chat_id, msg);
if msg_id != 0 {
if let Ok(msg_id) = chat::send_msg(context, chat_id, msg) {
dc_msg_unref(msg);
msg = ptr::null_mut();
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);
let name_f = entry.file_name();
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()
|| 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 crate::chat::*;
use crate::chat;
use crate::constants::*;
use crate::context::Context;
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 {
// 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 ((*mimefactory.msg).type_0 == Viewtype::Image
|| (*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 {
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 {
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::events::{BytesEnd, BytesStart, BytesText};
use crate::chat::*;
use crate::chat;
use crate::constants::Event;
use crate::constants::*;
use crate::context::*;
@@ -104,7 +104,7 @@ pub unsafe fn dc_send_locations_to_chat(
(*msg).text =
Some(context.stock_system_msg(StockMessage::MsgLocationEnabled, "", "", 0));
(*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 {
let stock_str = CString::new(context.stock_system_msg(
StockMessage::MsgLocationDisabled,
@@ -113,7 +113,7 @@ pub unsafe fn dc_send_locations_to_chat(
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(
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);
(*msg).hidden = 1;
(*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);
}
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],
).is_ok() {
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(
Event::CHAT_MODIFIED,
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_meaning = 1i32
} 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_meaning = 0i32
} else {

View File

@@ -10,7 +10,7 @@ use mmime::mailmime_write_mem::*;
use mmime::mmapstring::*;
use mmime::other::*;
use crate::chat::*;
use crate::chat::{self, Chat};
use crate::constants::*;
use crate::contact::*;
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);
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);
let chat = (*factory).chat.as_ref().unwrap();
load_from(factory);
(*factory).req_mdn = 0;
if 0 != dc_chat_is_self_talk(chat) {
if chat.is_self_talk() {
clist_insert_after(
(*factory).recipients_names,
(*(*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);
/* add attachment part */
if msgtype_has_file((*msg).type_0) {
if chat::msgtype_has_file((*msg).type_0) {
if !is_file_size_okay(msg) {
let error: *mut libc::c_char = dc_mprintf(
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 mut mime_sub: *mut mailmime = 0 as *mut mailmime;
let content: *mut mailmime_content;
let pathNfilename = (*msg)
.param
.get(Param::File)
.map(|s| s.strdup())
.unwrap_or_else(|| std::ptr::null_mut());
let path_filename = (*msg).param.get(Param::File);
let mut mimetype = (*msg)
.param
.get(Param::MimeType)
.map(|s| s.strdup())
.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_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 {
let ts = chrono::Utc.timestamp((*msg).timestamp_sort as i64, 0);
@@ -1175,7 +1173,7 @@ unsafe fn build_body_file(
.to_string();
filename_to_send = res.strdup();
} 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 {
if base_name.is_null() {
base_name = b"image\x00" as *const u8 as *const libc::c_char
@@ -1199,7 +1197,7 @@ unsafe fn build_body_file(
},
)
} else {
filename_to_send = dc_get_filename(pathNfilename)
filename_to_send = dc_get_filename(path_filename)
}
if mimetype.is_null() {
if suffix.is_null() {
@@ -1292,17 +1290,16 @@ unsafe fn build_body_file(
) as *mut libc::c_void,
);
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() {
*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(filename_to_send as *mut libc::c_void);
free(filename_encoded as *mut libc::c_void);
free(suffix as *mut libc::c_void);
mime_sub
}

View File

@@ -1,7 +1,7 @@
use std::ffi::CString;
use std::path::Path;
use crate::chat::*;
use crate::chat::{self, Chat};
use crate::constants::*;
use crate::contact::*;
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 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_c.as_ptr());
file_abs = dc_get_abs_path((*msg).context, file_rel);
}
}
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 let Some(file) = (*msg).param.get(Param::File) {
let file_c = CString::yolo(file);
ret = dc_get_filename(file_c.as_ptr());
ret = dc_get_filename(file);
}
}
if !ret.is_null() {
@@ -757,7 +755,7 @@ pub unsafe fn dc_msg_get_summary<'a>(
let chat = if let Some(chat) = chat {
chat
} 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
} else {
@@ -921,7 +919,7 @@ pub unsafe fn dc_msg_is_increation(msg: *const dc_msg_t) -> libc::c_int {
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
} else {
0
@@ -1252,7 +1250,7 @@ pub unsafe fn dc_mdn_from_ext(
(S=Sender, R=Recipient)
*/
// 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 {
dc_update_msg_state(context, *ret_msg_id, DC_STATE_OUT_MDN_RCVD);
read_by_all = 1;
@@ -1392,14 +1390,12 @@ mod tests {
let res = ctx.set_config(Config::ConfiguredAddr, Some("self@example.com"));
assert!(res.is_ok());
let chat = dc_create_chat_by_contact_id(ctx, contact);
assert!(chat != 0, "failed to create chat");
let chat = chat::create_by_contact_id(ctx, contact).unwrap();
let msg = dc_msg_new(ctx, Viewtype::Text);
assert!(!msg.is_null());
let msg_id = dc_prepare_msg(ctx, chat, msg);
assert!(msg_id != 0);
let msg_id = chat::prepare_msg(ctx, chat, msg).unwrap();
let msg2 = dc_get_msg(ctx, msg_id);
assert!(!msg2.is_null());

View File

@@ -1,6 +1,6 @@
use percent_encoding::percent_decode_str;
use crate::chat::*;
use crate::chat;
use crate::constants::Blocked;
use crate::contact::*;
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)
.map(|(id, _)| id)
.unwrap_or_default();
dc_create_or_lookup_nchat_by_contact_id(
let (id, _) = chat::create_or_lookup_by_contact_id(
context,
(*qr_parsed).id,
Blocked::Deaddrop,
&mut chat_id,
None,
);
)
.unwrap_or_default();
chat_id = id;
device_msg = dc_mprintf(
b"%s verified.\x00" as *const u8 as *const libc::c_char,
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)
}
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 sha2::{Digest, Sha256};
use crate::chat::*;
use crate::chat::{self, Chat};
use crate::constants::*;
use crate::contact::*;
use crate::context::Context;
@@ -408,15 +408,9 @@ unsafe fn add_parts(
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(
context,
*from_id,
&mut test_normal_chat_id,
&mut test_normal_chat_id_blocked,
);
let (test_normal_chat_id, test_normal_chat_id_blocked) =
chat::lookup_by_contact_id(context, *from_id).unwrap_or_default();
// 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
@@ -444,7 +438,7 @@ unsafe fn add_parts(
&mut chat_id_blocked,
);
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;
}
}
@@ -472,17 +466,15 @@ unsafe fn add_parts(
*chat_id = test_normal_chat_id;
chat_id_blocked = test_normal_chat_id_blocked;
} else if 0 != allow_creation {
dc_create_or_lookup_nchat_by_contact_id(
context,
*from_id,
create_blocked,
chat_id,
Some(&mut chat_id_blocked),
);
let (id, bl) =
chat::create_or_lookup_by_contact_id(context, *from_id, create_blocked)
.unwrap_or_default();
*chat_id = id;
chat_id_blocked = bl;
}
if 0 != *chat_id && Blocked::Not != chat_id_blocked {
if Blocked::Not == create_blocked {
dc_unblock_chat(context, *chat_id);
chat::unblock(context, *chat_id);
chat_id_blocked = Blocked::Not;
} 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,
@@ -532,7 +524,7 @@ unsafe fn add_parts(
&mut 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;
}
}
@@ -542,18 +534,17 @@ unsafe fn add_parts(
} else {
Blocked::Deaddrop
};
dc_create_or_lookup_nchat_by_contact_id(
context,
*to_id,
create_blocked,
chat_id,
Some(&mut chat_id_blocked),
);
let (id, bl) =
chat::create_or_lookup_by_contact_id(context, *to_id, create_blocked)
.unwrap_or_default();
*chat_id = id;
chat_id_blocked = bl;
if 0 != *chat_id
&& Blocked::Not != chat_id_blocked
&& Blocked::Not == create_blocked
{
dc_unblock_chat(context, *chat_id);
chat::unblock(context, *chat_id);
chat_id_blocked = Blocked::Not;
}
}
@@ -562,15 +553,13 @@ unsafe fn add_parts(
if to_ids.is_empty() && 0 != to_self {
// from_id==to_id==DC_CONTACT_ID_SELF - this is a self-sent messages,
// maybe an Autocrypt Setup Messag
dc_create_or_lookup_nchat_by_contact_id(
context,
1,
Blocked::Not,
chat_id,
Some(&mut chat_id_blocked),
);
let (id, bl) = chat::create_or_lookup_by_contact_id(context, 1, Blocked::Not)
.unwrap_or_default();
*chat_id = id;
chat_id_blocked = bl;
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;
}
}
@@ -593,7 +582,7 @@ unsafe fn add_parts(
);
// 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
// (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);
// 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,
grpid,
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 -
// 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;
}
// 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
.sql
@@ -1385,13 +1374,13 @@ unsafe fn create_or_lookup_group(
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() {
chat.param.remove(Param::ProfileImage);
} else {
chat.param.set(Param::ProfileImage, as_str(grpimage));
}
dc_chat_update_param(&mut chat);
chat.update_param().unwrap();
send_EVENT_CHAT_MODIFIED = 1;
}
@@ -1419,14 +1408,14 @@ unsafe fn create_or_lookup_group(
)
.ok();
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 !Contact::addr_equals_contact(context, &self_addr, from_id as u32)
&& (skip.is_null()
|| !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() {
@@ -1434,11 +1423,11 @@ unsafe fn create_or_lookup_group(
&& (skip.is_null()
|| !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;
dc_reset_gossiped_timestamp(context, chat_id);
chat::reset_gossiped_timestamp(context, chat_id);
}
if 0 != send_EVENT_CHAT_MODIFIED {
@@ -1448,7 +1437,7 @@ unsafe fn create_or_lookup_group(
// check the number of receivers -
// 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 {
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 {
// 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.
@@ -1617,7 +1606,7 @@ unsafe fn create_or_lookup_adhoc_group(
chat_id = create_group_record(context, grpid, grpname, create_blocked, 0);
chat_id_blocked = create_blocked;
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);

View File

@@ -4,7 +4,7 @@ use mmime::mailimf_types::*;
use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};
use crate::aheader::EncryptPreference;
use crate::chat::*;
use crate::chat::{self, Chat};
use crate::constants::*;
use crate::contact::*;
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();
qr = if 0 != group_chat_id {
if let Ok(chat) = dc_get_chat(context, group_chat_id) {
group_name = dc_chat_get_name(&chat);
if let Ok(chat) = Chat::load_from_db(context, group_chat_id) {
group_name = chat.get_name();
group_name_urlencoded = dc_urlencode(group_name);
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 {
error!(context, 0, "Unknown QR code.",);
} 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 {
error!(context, 0, "Unknown contact.",);
} 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 0 != join_vg {
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
} else {
ret_chat_id = contact_chat_id as libc::c_int
@@ -284,12 +285,13 @@ unsafe fn send_handshake_msg(
} else {
(*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);
}
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 {
contacts[0]
} else {
@@ -306,7 +308,7 @@ unsafe fn fingerprint_equals_sender(
return 0;
}
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 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 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 contact_chat_id: uint32_t = 0i32 as uint32_t;
let mut contact_chat_id_blocked = Blocked::Not;
let contact_chat_id: u32;
let contact_chat_id_blocked: Blocked;
let mut grpid: *mut libc::c_char = 0 as *mut libc::c_char;
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)
as libc::c_int;
dc_create_or_lookup_nchat_by_contact_id(
context,
contact_id,
Blocked::Not,
&mut contact_chat_id,
Some(&mut contact_chat_id_blocked),
);
let (id, bl) = chat::create_or_lookup_by_contact_id(context, contact_id, Blocked::Not)
.unwrap_or_default();
contact_chat_id = id;
contact_chat_id_blocked = bl;
if Blocked::Not != contact_chat_id_blocked {
dc_unblock_chat(context, contact_chat_id);
chat::unblock(context, contact_chat_id);
}
ret = 0x2i32;
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 {
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,
grpid,
None,
@@ -584,7 +584,7 @@ pub unsafe fn dc_handle_securejoin_handshake(
error!(context, 0, "Chat {} not found.", as_str(grpid),);
current_block = 4378276786830486580;
} else {
dc_add_contact_to_chat_ex(
chat::add_contact_to_chat_ex(
context,
group_chat_id,
contact_id,
@@ -647,7 +647,12 @@ pub unsafe fn dc_handle_securejoin_handshake(
let mut vg_expect_encrypted: libc::c_int = 1i32;
if 0 != join_vg {
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 {
vg_expect_encrypted = 0i32
}
@@ -803,7 +808,7 @@ unsafe fn secure_connection_established(context: &Context, contact_chat_id: uint
};
let msg =
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(
Event::CHAT_MODIFIED,
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();
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));
}
@@ -906,8 +911,6 @@ unsafe fn encrypted_and_signed(
}
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
// - 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
@@ -924,13 +927,10 @@ pub unsafe fn dc_handle_degrade_event(context: &Context, peerstate: &Peerstate)
)
.unwrap_or_default();
if contact_id > 0 {
dc_create_or_lookup_nchat_by_contact_id(
context,
contact_id as u32,
Blocked::Deaddrop,
&mut contact_chat_id,
None,
);
let (contact_chat_id, _) =
chat::create_or_lookup_by_contact_id(context, contact_id as u32, Blocked::Deaddrop)
.unwrap_or_default();
let peeraddr: &str = match peerstate.addr {
Some(ref addr) => &addr,
None => "",
@@ -939,7 +939,7 @@ pub unsafe fn dc_handle_degrade_event(context: &Context, peerstate: &Peerstate)
context.stock_string_repl_str(StockMessage::ContactSetupChanged, peeraddr),
)
.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(
Event::CHAT_MODIFIED,
contact_chat_id as uintptr_t,

View File

@@ -1,5 +1,6 @@
use std::borrow::Cow;
use std::ffi::{CStr, CString};
use std::path::Path;
use std::str::FromStr;
use std::time::SystemTime;
use std::{fmt, fs, ptr};
@@ -637,7 +638,7 @@ pub unsafe fn dc_smeared_time(context: &Context) -> i64 {
now
}
pub unsafe fn dc_create_smeared_timestamp(context: &Context) -> i64 {
pub fn dc_create_smeared_timestamp(context: &Context) -> i64 {
let now = time();
let mut ret = now;
@@ -652,7 +653,7 @@ pub unsafe fn dc_create_smeared_timestamp(context: &Context) -> i64 {
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 */
let now = time();
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(pathNfilename: *const libc::c_char) -> *mut libc::c_char {
let mut p: *const libc::c_char = strrchr(pathNfilename, '/' as i32);
if p.is_null() {
p = strrchr(pathNfilename, '\\' as i32)
}
if !p.is_null() {
p = p.offset(1isize);
dc_strdup(p)
pub unsafe fn dc_get_filename(path_filename: impl AsRef<str>) -> *mut libc::c_char {
if let Some(p) = Path::new(path_filename.as_ref()).file_name() {
p.to_string_lossy().strdup()
} else {
dc_strdup(pathNfilename)
ptr::null_mut()
}
}
@@ -869,12 +864,15 @@ unsafe fn dc_split_filename(
ret_basename: *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",
(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)
- 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 */
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 p1: *mut libc::c_char = strrchr(basename, '.' as i32);
if !p1.is_null() {
@@ -897,16 +895,12 @@ unsafe fn dc_split_filename(
// the returned suffix is lower-case
#[allow(non_snake_case)]
pub unsafe fn dc_get_filesuffix_lc(pathNfilename: *const libc::c_char) -> *mut libc::c_char {
if !pathNfilename.is_null() {
let mut p: *const libc::c_char = strrchr(pathNfilename, '.' as i32);
if !p.is_null() {
p = p.offset(1isize);
return dc_strlower(p);
}
pub unsafe fn dc_get_filesuffix_lc(path_filename: impl AsRef<str>) -> *mut libc::c_char {
if let Some(p) = Path::new(path_filename.as_ref()).extension() {
p.to_string_lossy().to_lowercase().strdup()
} else {
ptr::null_mut()
}
ptr::null_mut()
}
/// 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(
context: &Context,
pathNfilename: *const libc::c_char,
path_filename: impl AsRef<str>,
) -> *mut libc::c_char {
if pathNfilename.is_null() {
return ptr::null_mut();
}
let starts = strncmp(
pathNfilename,
b"$BLOBDIR\x00" as *const u8 as *const libc::c_char,
8,
) == 0;
let starts = path_filename.as_ref().starts_with("$BLOBDIR");
if starts && !context.has_blobdir() {
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() {
dc_str_replace(
&mut pathNfilename_abs,
&mut path_filename_abs,
b"$BLOBDIR\x00" as *const u8 as *const libc::c_char,
context.get_blobdir(),
);
}
pathNfilename_abs
path_filename_abs
}
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
}
pub unsafe fn dc_is_blobdir_path(context: &Context, path: *const libc::c_char) -> bool {
strncmp(path, context.get_blobdir(), strlen(context.get_blobdir())) == 0
|| strncmp(path, b"$BLOBDIR\x00" as *const u8 as *const libc::c_char, 8) == 0
pub fn dc_is_blobdir_path(context: &Context, path: impl AsRef<str>) -> bool {
path.as_ref().starts_with(as_str(context.get_blobdir()))
|| path.as_ref().starts_with("$BLOBDIR")
}
unsafe fn dc_make_rel_path(context: &Context, path: *mut *mut libc::c_char) {
if path.is_null() || (*path).is_null() {
return;
fn dc_make_rel_path(context: &Context, path: &mut String) {
if path.starts_with(as_str(context.get_blobdir())) {
*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 filename: *mut libc::c_char = ptr::null_mut();
let mut blobdir_path: *mut libc::c_char = ptr::null_mut();
if !(path.is_null() || (*path).is_null()) {
if dc_is_blobdir_path(context, *path) {
dc_make_rel_path(context, path);
success = true;
} else {
filename = dc_get_filename(*path);
if !(filename.is_null()
|| {
blobdir_path = dc_get_fine_pathNfilename(
let mut filename = ptr::null_mut();
let mut blobdir_path = ptr::null_mut();
if dc_is_blobdir_path(context, &path) {
dc_make_rel_path(context, path);
success = true;
} else {
filename = unsafe { dc_get_filename(&path) };
if !(filename.is_null()
|| {
blobdir_path = unsafe {
dc_get_fine_pathNfilename(
context,
b"$BLOBDIR\x00" as *const u8 as *const libc::c_char,
filename,
);
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;
)
};
blobdir_path.is_null()
}
|| !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);
free(filename as *mut libc::c_void);
unsafe {
free(blobdir_path.cast());
free(filename.cast());
}
success
}

View File

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