diff --git a/deltachat-ffi/deltachat.h b/deltachat-ffi/deltachat.h index c6ee50fd9..fb606d8fd 100644 --- a/deltachat-ffi/deltachat.h +++ b/deltachat-ffi/deltachat.h @@ -3816,6 +3816,21 @@ void dc_msg_set_text (dc_msg_t* msg, const char* text); void dc_msg_set_html (dc_msg_t* msg, const char* html); +/** + * Set different sender name for a message. + * This overrides the name set by the dc_set_config()-option `displayname`. + * + * Usually, this function is not needed + * when implementing pure messaging functions. + * However, it might be useful for bots eg. building bridges to other networks. + * + * @memberof dc_msg_t + * @param msg The message object. + * @param name The name to send along with the message. + */ +void dc_msg_set_override_sender_name(dc_msg_t* msg, const char* name); + + /** * Set the file associated with a message object. * This does not alter any information in the database diff --git a/deltachat-ffi/src/lib.rs b/deltachat-ffi/src/lib.rs index a06961ba0..1abfe635a 100644 --- a/deltachat-ffi/src/lib.rs +++ b/deltachat-ffi/src/lib.rs @@ -3028,6 +3028,21 @@ pub unsafe extern "C" fn dc_msg_set_html(msg: *mut dc_msg_t, html: *const libc:: ffi_msg.message.set_html(to_opt_string_lossy(html)) } +#[no_mangle] +pub unsafe extern "C" fn dc_msg_set_override_sender_name( + msg: *mut dc_msg_t, + name: *const libc::c_char, +) { + if msg.is_null() { + eprintln!("ignoring careless call to dc_msg_set_override_sender_name()"); + return; + } + let ffi_msg = &mut *msg; + ffi_msg + .message + .set_override_sender_name(to_opt_string_lossy(name)) +} + #[no_mangle] pub unsafe extern "C" fn dc_msg_set_file( msg: *mut dc_msg_t, diff --git a/src/message.rs b/src/message.rs index 15b87fe1f..6e90a6e11 100644 --- a/src/message.rs +++ b/src/message.rs @@ -806,6 +806,16 @@ impl Message { } } + /// Set different sender name for a message. + /// This overrides the name set by the `set_config()`-option `displayname`. + pub fn set_override_sender_name(&mut self, name: Option) { + if let Some(name) = name { + self.param.set(Param::OverrideSenderDisplayname, name); + } else { + self.param.remove(Param::OverrideSenderDisplayname); + } + } + pub fn set_dimension(&mut self, width: i32, height: i32) { self.param.set_int(Param::Width, width); self.param.set_int(Param::Height, height); @@ -2526,4 +2536,45 @@ mod tests { assert!(!msg.get_real_chat_id().is_special()); assert_eq!(msg.get_text().unwrap(), "hello".to_string()); } + + #[async_std::test] + async fn test_set_override_sender_name() { + // send message with overridden sender name + let alice = TestContext::new_alice().await; + let bob = TestContext::new_bob().await; + let chat = alice.create_chat(&bob).await; + let contact_id = *chat::get_chat_contacts(&alice, chat.id) + .await + .first() + .unwrap(); + let contact = Contact::load_from_db(&alice, contact_id).await.unwrap(); + + let mut msg = Message::new(Viewtype::Text); + msg.set_text(Some("bla blubb".to_string())); + msg.set_override_sender_name(Some("over ride".to_string())); + assert_eq!( + msg.get_override_sender_name(), + Some("over ride".to_string()) + ); + assert_eq!(msg.get_sender_name(&contact), "over ride".to_string()); + assert_ne!(contact.get_display_name(), "over ride".to_string()); + chat::send_msg(&alice, chat.id, &mut msg).await.unwrap(); + + // bob receives that message + let chat = bob.create_chat(&alice).await; + let contact_id = *chat::get_chat_contacts(&bob, chat.id) + .await + .first() + .unwrap(); + let contact = Contact::load_from_db(&bob, contact_id).await.unwrap(); + bob.recv_msg(&alice.pop_sent_msg().await).await; + let msg = bob.get_last_msg_in(chat.id).await; + assert_eq!(msg.text, Some("bla blubb".to_string())); + assert_eq!( + msg.get_override_sender_name(), + Some("over ride".to_string()) + ); + assert_eq!(msg.get_sender_name(&contact), "over ride".to_string()); + assert_ne!(contact.get_display_name(), "over ride".to_string()); + } } diff --git a/src/param.rs b/src/param.rs index ca771b562..dbd536883 100644 --- a/src/param.rs +++ b/src/param.rs @@ -23,7 +23,8 @@ pub enum Param { File = b'f', /// For messages: This name should be shown instead of contact.get_display_name() - /// (used if this is a mailinglist) + /// (used if this is a mailinglist + /// or explictly set using set_override_sender_name(), eg. by bots) OverrideSenderDisplayname = b'O', /// For Messages