Skip to content

Commit 1d534ff

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 5811248 commit 1d534ff

File tree

11 files changed

+88
-92
lines changed

11 files changed

+88
-92
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: 15 additions & 35 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.deref().unwrap_or(""),
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,7 +991,7 @@ 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 {
994+
if (*msg).text.is_none() {
1005995
OK_TO_CONTINUE = false;
1006996
}
1007997
} else if msgtype_has_file((*msg).type_0) {
@@ -1036,11 +1026,7 @@ unsafe fn set_draft_raw(context: &Context, chat_id: uint32_t, msg: *mut dc_msg_t
10361026
time(),
10371027
(*msg).type_0,
10381028
DC_STATE_OUT_DRAFT,
1039-
if !(*msg).text.is_null() {
1040-
as_str((*msg).text)
1041-
} else {
1042-
""
1043-
},
1029+
(*msg).text,
10441030
(*msg).param.to_string(),
10451031
1,
10461032
],
@@ -1616,14 +1602,12 @@ pub unsafe fn dc_add_contact_to_chat_ex(
16161602
if OK_TO_CONTINUE {
16171603
if (*chat).param.get_int(Param::Unpromoted).unwrap_or_default() == 0 {
16181604
(*msg).type_0 = Viewtype::Text;
1619-
(*msg).text = context
1605+
(*msg).text = Some(context
16201606
.stock_system_msg(
16211607
StockMessage::MsgAddMember,
16221608
as_str((*contact).addr),
16231609
"",
1624-
DC_CONTACT_ID_SELF as uint32_t,
1625-
)
1626-
.strdup();
1610+
DC_CONTACT_ID_SELF as uint32_t));
16271611
(*msg).param.set_int(Param::Cmd, 4);
16281612
if !(*contact).addr.is_null() {
16291613
(*msg).param.set(Param::Arg, as_str((*contact).addr));
@@ -1731,23 +1715,21 @@ pub unsafe fn dc_remove_contact_from_chat(
17311715
(*msg).type_0 = Viewtype::Text;
17321716
if (*contact).id == 1 as libc::c_uint {
17331717
dc_set_group_explicitly_left(context, (*chat).grpid);
1734-
(*msg).text = context
1718+
(*msg).text = Some(context
17351719
.stock_system_msg(
17361720
StockMessage::MsgGroupLeft,
17371721
"",
17381722
"",
17391723
DC_CONTACT_ID_SELF as u32,
1740-
)
1741-
.strdup();
1724+
));
17421725
} else {
1743-
(*msg).text = context
1726+
(*msg).text = Some(context
17441727
.stock_system_msg(
17451728
StockMessage::MsgDelMember,
17461729
as_str((*contact).addr),
17471730
"",
17481731
DC_CONTACT_ID_SELF as u32,
1749-
)
1750-
.strdup();
1732+
));
17511733
}
17521734
(*msg).param.set_int(Param::Cmd, 5);
17531735
if !(*contact).addr.is_null() {
@@ -1848,14 +1830,13 @@ pub unsafe fn dc_set_chat_name(
18481830
{
18491831
if (*chat).param.get_int(Param::Unpromoted).unwrap_or_default() == 0 {
18501832
(*msg).type_0 = Viewtype::Text;
1851-
(*msg).text = context
1833+
(*msg).text = Some(context
18521834
.stock_system_msg(
18531835
StockMessage::MsgGrpName,
18541836
as_str((*chat).name),
18551837
as_str(new_name),
18561838
DC_CONTACT_ID_SELF as u32,
1857-
)
1858-
.strdup();
1839+
));
18591840
(*msg).param.set_int(Param::Cmd, 2);
18601841
if !(*chat).name.is_null() {
18611842
(*msg).param.set(Param::Arg, as_str((*chat).name));
@@ -1922,7 +1903,7 @@ pub unsafe fn dc_set_chat_profile_image(
19221903
(*msg).param.set_int(Param::Cmd, 3);
19231904
(*msg).param.set(Param::Arg, as_str(new_image_rel));
19241905
(*msg).type_0 = Viewtype::Text;
1925-
(*msg).text = context
1906+
(*msg).text = Some(context
19261907
.stock_system_msg(
19271908
if !new_image_rel.is_null() {
19281909
StockMessage::MsgGrpImgChanged
@@ -1932,8 +1913,7 @@ pub unsafe fn dc_set_chat_profile_image(
19321913
"",
19331914
"",
19341915
DC_CONTACT_ID_SELF as uint32_t,
1935-
)
1936-
.strdup();
1916+
));
19371917
(*msg).id = dc_send_msg(context, chat_id, msg);
19381918
context.call_cb(
19391919
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: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::dc_tools::*;
66
use crate::stock::StockMessage;
77
use crate::types::*;
88
use crate::x::*;
9+
use std::ptr;
910

1011
/* * Structure behind dc_lot_t */
1112
#[derive(Copy, Clone)]
@@ -160,8 +161,16 @@ pub unsafe fn dc_lot_fill(
160161
(*lot).text1_meaning = 2i32
161162
}
162163
}
164+
165+
let msgtext_ptr = if let Some(ref text) = (*msg).text {
166+
text.strdup()
167+
} else { ptr::null_mut() };
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+
172+
free(msgtext_ptr.cast());
173+
165174
(*lot).timestamp = dc_msg_get_timestamp(msg);
166175
(*lot).state = (*msg).state;
167176
}

src/dc_mimefactory.rs

Lines changed: 21 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,13 @@ 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+
let msgtext_ptr = if let Some(ref text) = (*msg).text { text.strdup() } else { ptr::null_mut() };
1101+
1102+
let raw_subject = dc_msg_get_summarytext_by_raw((*msg).type_0, msgtext_ptr, &mut (*msg).param, 32, context);
1103+
1104+
free(msgtext_ptr.cast());
1105+
10991106
let fwd = if 0 != afwd_email {
11001107
b"Fwd: \x00" as *const u8 as *const libc::c_char
11011108
} else {

0 commit comments

Comments
 (0)