Skip to content

Commit 6dcc8d8

Browse files
Dmitry BogatovDmitry Bogatov
authored andcommitted
Change type of dc_msg_t.text to String
Also, remove `send-garbage' command from REPL, since it is not possible to send non-utf8 string anymore.
1 parent d2b23b7 commit 6dcc8d8

File tree

10 files changed

+81
-90
lines changed

10 files changed

+81
-90
lines changed

deltachat-ffi/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ pub unsafe extern "C" fn dc_send_text_msg(
374374
) -> u32 {
375375
assert!(!context.is_null());
376376
let context = &*context;
377+
let text_to_send = dc_tools::to_string_lossy(text_to_send);
377378

378379
dc_chat::dc_send_text_msg(context, chat_id, text_to_send)
379380
}

examples/repl/cmdline.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -914,29 +914,20 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
914914
ensure!(!sel_chat.is_null(), "No chat selected.");
915915
ensure!(!arg1.is_empty(), "No message text given.");
916916

917-
let msg = CString::yolo(format!("{} {}", arg1, arg2));
917+
let msg = format!("{} {}", arg1, arg2);
918918

919-
if 0 != dc_send_text_msg(context, dc_chat_get_id(sel_chat), msg.as_ptr()) {
919+
if 0 != dc_send_text_msg(context, dc_chat_get_id(sel_chat), msg) {
920920
println!("Message sent.");
921921
} else {
922922
bail!("Sending failed.");
923923
}
924924
}
925-
"send-garbage" => {
926-
ensure!(!sel_chat.is_null(), "No chat selected.");
927-
let msg = b"\xff\x00"; // NUL-terminated C-string, that is malformed utf-8
928-
if 0 != dc_send_text_msg(context, dc_chat_get_id(sel_chat), msg.as_ptr().cast()) {
929-
println!("Malformed utf-8 succesfully send. Not nice.");
930-
} else {
931-
bail!("Garbage sending failed, as expected.");
932-
}
933-
}
934925
"sendempty" => {
935926
ensure!(!sel_chat.is_null(), "No chat selected.");
936927
if 0 != dc_send_text_msg(
937928
context,
938929
dc_chat_get_id(sel_chat),
939-
b"\x00" as *const u8 as *const libc::c_char,
930+
"".into()
940931
) {
941932
println!("Message sent.");
942933
} else {

examples/repl/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ const DB_COMMANDS: [&'static str; 11] = [
291291
"housekeeping",
292292
];
293293

294-
const CHAT_COMMANDS: [&'static str; 25] = [
294+
const CHAT_COMMANDS: [&'static str; 24] = [
295295
"listchats",
296296
"listarchived",
297297
"chat",
@@ -309,7 +309,6 @@ const CHAT_COMMANDS: [&'static str; 25] = [
309309
"dellocations",
310310
"getlocations",
311311
"send",
312-
"send-garbage",
313312
"sendimage",
314313
"sendfile",
315314
"draft",

examples/simple.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@ fn main() {
9797
println!("sending a message");
9898
let contact_id = dc_create_contact(&ctx, std::ptr::null(), email.as_ptr());
9999
let chat_id = dc_create_chat_by_contact_id(&ctx, contact_id);
100-
let msg_text = CString::new("Hi, here is my first message!").unwrap();
101-
dc_send_text_msg(&ctx, chat_id, msg_text.as_ptr());
100+
dc_send_text_msg(&ctx, chat_id, "Hi, here is my first message!".into());
102101

103102
println!("fetching chats..");
104103
let chats = Chatlist::try_load(&ctx, 0, None, None).unwrap();

src/dc_chat.rs

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ unsafe fn prepare_msg_raw(
725725
timestamp,
726726
(*msg).type_0,
727727
(*msg).state,
728-
if !(*msg).text.is_null() { Some(as_str((*msg).text)) } else { None },
728+
(*msg).text,
729729
(*msg).param.to_string(),
730730
(*msg).hidden,
731731
to_string(new_in_reply_to),
@@ -950,7 +950,7 @@ pub unsafe fn dc_send_msg<'a>(
950950
pub unsafe fn dc_send_text_msg(
951951
context: &Context,
952952
chat_id: uint32_t,
953-
text_to_send: *const libc::c_char,
953+
text_to_send: String,
954954
) -> uint32_t {
955955
if chat_id <= 9 {
956956
warn!(
@@ -960,18 +960,8 @@ pub unsafe fn dc_send_text_msg(
960960
return 0;
961961
}
962962

963-
if text_to_send.is_null() {
964-
warn!(context, 0, "dc_send_text_msg: text_to_send is emtpy");
965-
return 0;
966-
}
967-
968-
if let Err(err) = as_str_safe(text_to_send) {
969-
warn!(context, 0, "{}", err);
970-
return 0;
971-
}
972-
973963
let mut msg = dc_msg_new(context, Viewtype::Text);
974-
(*msg).text = dc_strdup(text_to_send);
964+
(*msg).text = Some(text_to_send);
975965
let ret = dc_send_msg(context, chat_id, msg);
976966
dc_msg_unref(msg);
977967
ret
@@ -1001,8 +991,9 @@ unsafe fn set_draft_raw(context: &Context, chat_id: uint32_t, msg: *mut dc_msg_t
1001991
// save new draft
1002992
if !msg.is_null() {
1003993
if (*msg).type_0 == Viewtype::Text {
1004-
if (*msg).text.is_null() || *(*msg).text.offset(0isize) as libc::c_int == 0i32 {
1005-
OK_TO_CONTINUE = false;
994+
OK_TO_CONTINUE = match (*msg).text {
995+
None => false,
996+
Some(ref text) => !text.is_empty()
1006997
}
1007998
} else if msgtype_has_file((*msg).type_0) {
1008999
let mut pathNfilename = (*msg)
@@ -1036,11 +1027,7 @@ unsafe fn set_draft_raw(context: &Context, chat_id: uint32_t, msg: *mut dc_msg_t
10361027
time(),
10371028
(*msg).type_0,
10381029
DC_STATE_OUT_DRAFT,
1039-
if !(*msg).text.is_null() {
1040-
as_str((*msg).text)
1041-
} else {
1042-
""
1043-
},
1030+
(*msg).text,
10441031
(*msg).param.to_string(),
10451032
1,
10461033
],
@@ -1616,14 +1603,12 @@ pub unsafe fn dc_add_contact_to_chat_ex(
16161603
if OK_TO_CONTINUE {
16171604
if (*chat).param.get_int(Param::Unpromoted).unwrap_or_default() == 0 {
16181605
(*msg).type_0 = Viewtype::Text;
1619-
(*msg).text = context
1606+
(*msg).text = Some(context
16201607
.stock_system_msg(
16211608
StockMessage::MsgAddMember,
16221609
as_str((*contact).addr),
16231610
"",
1624-
DC_CONTACT_ID_SELF as uint32_t,
1625-
)
1626-
.strdup();
1611+
DC_CONTACT_ID_SELF as uint32_t));
16271612
(*msg).param.set_int(Param::Cmd, 4);
16281613
if !(*contact).addr.is_null() {
16291614
(*msg).param.set(Param::Arg, as_str((*contact).addr));
@@ -1731,23 +1716,21 @@ pub unsafe fn dc_remove_contact_from_chat(
17311716
(*msg).type_0 = Viewtype::Text;
17321717
if (*contact).id == 1 as libc::c_uint {
17331718
dc_set_group_explicitly_left(context, (*chat).grpid);
1734-
(*msg).text = context
1719+
(*msg).text = Some(context
17351720
.stock_system_msg(
17361721
StockMessage::MsgGroupLeft,
17371722
"",
17381723
"",
17391724
DC_CONTACT_ID_SELF as u32,
1740-
)
1741-
.strdup();
1725+
));
17421726
} else {
1743-
(*msg).text = context
1727+
(*msg).text = Some(context
17441728
.stock_system_msg(
17451729
StockMessage::MsgDelMember,
17461730
as_str((*contact).addr),
17471731
"",
17481732
DC_CONTACT_ID_SELF as u32,
1749-
)
1750-
.strdup();
1733+
));
17511734
}
17521735
(*msg).param.set_int(Param::Cmd, 5);
17531736
if !(*contact).addr.is_null() {
@@ -1848,14 +1831,13 @@ pub unsafe fn dc_set_chat_name(
18481831
{
18491832
if (*chat).param.get_int(Param::Unpromoted).unwrap_or_default() == 0 {
18501833
(*msg).type_0 = Viewtype::Text;
1851-
(*msg).text = context
1834+
(*msg).text = Some(context
18521835
.stock_system_msg(
18531836
StockMessage::MsgGrpName,
18541837
as_str((*chat).name),
18551838
as_str(new_name),
18561839
DC_CONTACT_ID_SELF as u32,
1857-
)
1858-
.strdup();
1840+
));
18591841
(*msg).param.set_int(Param::Cmd, 2);
18601842
if !(*chat).name.is_null() {
18611843
(*msg).param.set(Param::Arg, as_str((*chat).name));
@@ -1922,7 +1904,7 @@ pub unsafe fn dc_set_chat_profile_image(
19221904
(*msg).param.set_int(Param::Cmd, 3);
19231905
(*msg).param.set(Param::Arg, as_str(new_image_rel));
19241906
(*msg).type_0 = Viewtype::Text;
1925-
(*msg).text = context
1907+
(*msg).text = Some(context
19261908
.stock_system_msg(
19271909
if !new_image_rel.is_null() {
19281910
StockMessage::MsgGrpImgChanged
@@ -1932,8 +1914,7 @@ pub unsafe fn dc_set_chat_profile_image(
19321914
"",
19331915
"",
19341916
DC_CONTACT_ID_SELF as uint32_t,
1935-
)
1936-
.strdup();
1917+
));
19371918
(*msg).id = dc_send_msg(context, chat_id, msg);
19381919
context.call_cb(
19391920
Event::MSGS_CHANGED,

src/dc_location.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,8 @@ pub unsafe fn dc_send_locations_to_chat(
100100
{
101101
if 0 != seconds && !is_sending_locations_before {
102102
msg = dc_msg_new(context, Viewtype::Text);
103-
(*msg).text = context
104-
.stock_system_msg(StockMessage::MsgLocationEnabled, "", "", 0)
105-
.strdup();
103+
(*msg).text = Some(context
104+
.stock_system_msg(StockMessage::MsgLocationEnabled, "", "", 0));
106105
(*msg).param.set_int(Param::Cmd, 8);
107106
dc_send_msg(context, chat_id, msg);
108107
} else if 0 == seconds && is_sending_locations_before {

src/dc_lot.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use crate::dc_tools::*;
66
use crate::stock::StockMessage;
77
use crate::types::*;
88
use crate::x::*;
9+
use std::ptr;
10+
use std::ffi::CString;
911

1012
/* * Structure behind dc_lot_t */
1113
#[derive(Copy, Clone)]
@@ -160,8 +162,13 @@ pub unsafe fn dc_lot_fill(
160162
(*lot).text1_meaning = 2i32
161163
}
162164
}
165+
166+
let msgtext_c = (*msg).text.as_ref().map(|text| CString::yolo(text.clone()));
167+
let msgtext_ptr = msgtext_c.map_or(ptr::null(), |s| s.as_ptr());
168+
163169
(*lot).text2 =
164-
dc_msg_get_summarytext_by_raw((*msg).type_0, (*msg).text, &mut (*msg).param, 160, context);
170+
dc_msg_get_summarytext_by_raw((*msg).type_0, msgtext_ptr, &mut (*msg).param, 160, context);
171+
165172
(*lot).timestamp = dc_msg_get_timestamp(msg);
166173
(*lot).state = (*msg).state;
167174
}

src/dc_mimefactory.rs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use mmime::mailmime_types_helper::*;
99
use mmime::mailmime_write_mem::*;
1010
use mmime::mmapstring::*;
1111
use mmime::other::*;
12+
use std::ptr;
1213

1314
use crate::constants::*;
1415
use crate::context::Context;
@@ -799,12 +800,17 @@ pub unsafe fn dc_mimefactory_render(mut factory: *mut dc_mimefactory_t) -> libc:
799800
)
800801
}
801802

802-
let mut final_text: *const libc::c_char = 0 as *const libc::c_char;
803-
if !placeholdertext.is_null() {
804-
final_text = placeholdertext
805-
} else if !(*msg).text.is_null() && 0 != *(*msg).text.offset(0isize) as libc::c_int {
806-
final_text = (*msg).text
807-
}
803+
let final_text = {
804+
if !placeholdertext.is_null() {
805+
to_string(placeholdertext)
806+
} else if let Some(ref text) = (*msg).text {
807+
text.clone()
808+
} else {
809+
"".into()
810+
}
811+
};
812+
let final_text = CString::yolo(final_text);
813+
808814
let footer: *mut libc::c_char = (*factory).selfstatus;
809815
message_text = dc_mprintf(
810816
b"%s%s%s%s%s\x00" as *const u8 as *const libc::c_char,
@@ -813,12 +819,8 @@ pub unsafe fn dc_mimefactory_render(mut factory: *mut dc_mimefactory_t) -> libc:
813819
} else {
814820
b"\x00" as *const u8 as *const libc::c_char
815821
},
816-
if !final_text.is_null() {
817-
final_text
818-
} else {
819-
b"\x00" as *const u8 as *const libc::c_char
820-
},
821-
if !final_text.is_null()
822+
final_text.as_ptr(),
823+
if final_text != CString::yolo("")
822824
&& !footer.is_null()
823825
&& 0 != *footer.offset(0isize) as libc::c_int
824826
{
@@ -1094,8 +1096,15 @@ unsafe fn get_subject(
10941096
) -> *mut libc::c_char {
10951097
let context = (*chat).context;
10961098
let ret: *mut libc::c_char;
1097-
let raw_subject =
1098-
dc_msg_get_summarytext_by_raw((*msg).type_0, (*msg).text, &mut (*msg).param, 32, context);
1099+
1100+
1101+
let raw_subject = {
1102+
let msgtext_c = (*msg).text.as_ref().map(|text| CString::yolo(text.clone()));
1103+
let msgtext_ptr = msgtext_c.map_or(ptr::null(), |s| s.as_ptr());
1104+
1105+
dc_msg_get_summarytext_by_raw((*msg).type_0, msgtext_ptr, &mut (*msg).param, 32, context)
1106+
};
1107+
10991108
let fwd = if 0 != afwd_email {
11001109
b"Fwd: \x00" as *const u8 as *const libc::c_char
11011110
} else {

0 commit comments

Comments
 (0)