mirror of
https://github.com/chatmail/core.git
synced 2026-05-02 04:46:29 +03:00
make dc_create_outgoing_rfc724_mid safe and simplify call sites
This commit is contained in:
23
src/chat.rs
23
src/chat.rs
@@ -1,6 +1,5 @@
|
|||||||
use std::ffi::CString;
|
use std::ffi::CString;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::ptr;
|
|
||||||
|
|
||||||
use crate::chatlist::*;
|
use crate::chatlist::*;
|
||||||
use crate::config::*;
|
use crate::config::*;
|
||||||
@@ -16,7 +15,6 @@ use crate::message::*;
|
|||||||
use crate::param::*;
|
use crate::param::*;
|
||||||
use crate::sql::{self, Sql};
|
use crate::sql::{self, Sql};
|
||||||
use crate::stock::StockMessage;
|
use crate::stock::StockMessage;
|
||||||
use crate::x::*;
|
|
||||||
|
|
||||||
/// An object representing a single chat in memory.
|
/// An object representing a single chat in memory.
|
||||||
/// Chat objects are created using eg. `Chat::load_from_db`
|
/// Chat objects are created using eg. `Chat::load_from_db`
|
||||||
@@ -272,7 +270,7 @@ impl Chat {
|
|||||||
Chattype::Group | Chattype::VerifiedGroup => Some(self.grpid.as_str()),
|
Chattype::Group | Chattype::VerifiedGroup => Some(self.grpid.as_str()),
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
dc_create_outgoing_rfc724_mid_safe(grpid, &from)
|
dc_create_outgoing_rfc724_mid(grpid, &from)
|
||||||
};
|
};
|
||||||
|
|
||||||
if self.typ == Chattype::Single {
|
if self.typ == Chattype::Single {
|
||||||
@@ -1840,12 +1838,7 @@ pub fn get_chat_id_by_grpid(context: &Context, grpid: impl AsRef<str>) -> (u32,
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_device_msg(context: &Context, chat_id: u32, text: impl AsRef<str>) {
|
pub fn add_device_msg(context: &Context, chat_id: u32, text: impl AsRef<str>) {
|
||||||
let rfc724_mid = unsafe {
|
let rfc724_mid = dc_create_outgoing_rfc724_mid(None, "@device");
|
||||||
dc_create_outgoing_rfc724_mid(
|
|
||||||
ptr::null(),
|
|
||||||
b"@device\x00" as *const u8 as *const libc::c_char,
|
|
||||||
)
|
|
||||||
};
|
|
||||||
|
|
||||||
if context.sql.execute(
|
if context.sql.execute(
|
||||||
"INSERT INTO msgs (chat_id,from_id,to_id, timestamp,type,state, txt,rfc724_mid) VALUES (?,?,?, ?,?,?, ?,?);",
|
"INSERT INTO msgs (chat_id,from_id,to_id, timestamp,type,state, txt,rfc724_mid) VALUES (?,?,?, ?,?,?, ?,?);",
|
||||||
@@ -1857,21 +1850,13 @@ pub fn add_device_msg(context: &Context, chat_id: u32, text: impl AsRef<str>) {
|
|||||||
Viewtype::Text,
|
Viewtype::Text,
|
||||||
MessageState::InNoticed,
|
MessageState::InNoticed,
|
||||||
text.as_ref(),
|
text.as_ref(),
|
||||||
as_str(rfc724_mid),
|
rfc724_mid,
|
||||||
]
|
]
|
||||||
).is_err() {
|
).is_err() {
|
||||||
unsafe { free(rfc724_mid as *mut libc::c_void) };
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let msg_id = sql::get_rowid(
|
let msg_id = sql::get_rowid(context, &context.sql, "msgs", "rfc724_mid", &rfc724_mid);
|
||||||
context,
|
|
||||||
&context.sql,
|
|
||||||
"msgs",
|
|
||||||
"rfc724_mid",
|
|
||||||
as_str(rfc724_mid),
|
|
||||||
);
|
|
||||||
unsafe { free(rfc724_mid as *mut libc::c_void) };
|
|
||||||
context.call_cb(Event::MsgsChanged { chat_id, msg_id });
|
context.call_cb(Event::MsgsChanged { chat_id, msg_id });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -330,7 +330,7 @@ pub unsafe fn dc_mimefactory_load_mdn<'a>(
|
|||||||
);
|
);
|
||||||
load_from(&mut factory);
|
load_from(&mut factory);
|
||||||
factory.timestamp = dc_create_smeared_timestamp(factory.context);
|
factory.timestamp = dc_create_smeared_timestamp(factory.context);
|
||||||
factory.rfc724_mid = dc_create_outgoing_rfc724_mid(0 as *const libc::c_char, factory.from_addr);
|
factory.rfc724_mid = dc_create_outgoing_rfc724_mid(None, as_str(factory.from_addr)).strdup();
|
||||||
factory.loaded = DC_MF_MDN_LOADED;
|
factory.loaded = DC_MF_MDN_LOADED;
|
||||||
|
|
||||||
Ok(factory)
|
Ok(factory)
|
||||||
|
|||||||
@@ -590,30 +590,8 @@ pub fn dc_create_incoming_rfc724_mid(
|
|||||||
/// Function generates a Message-ID that can be used for a new outgoing message.
|
/// Function generates a Message-ID that can be used for a new outgoing message.
|
||||||
/// - this function is called for all outgoing messages.
|
/// - this function is called for all outgoing messages.
|
||||||
/// - the message ID should be globally unique
|
/// - the message ID should be globally unique
|
||||||
/// - do not add a counter or any private data as as this may give unneeded information to the receiver
|
/// - do not add a counter or any private data as this leaks information unncessarily
|
||||||
pub unsafe fn dc_create_outgoing_rfc724_mid(
|
pub fn dc_create_outgoing_rfc724_mid(grpid: Option<&str>, from_addr: &str) -> String {
|
||||||
grpid: *const libc::c_char,
|
|
||||||
from_addr: *const libc::c_char,
|
|
||||||
) -> *mut libc::c_char {
|
|
||||||
let rand2 = dc_create_id();
|
|
||||||
|
|
||||||
let at_hostname = as_opt_str(strchr(from_addr, '@' as i32)).unwrap_or_else(|| "@nohost");
|
|
||||||
|
|
||||||
let ret = if !grpid.is_null() {
|
|
||||||
format!("Gr.{}.{}{}", as_str(grpid), rand2, at_hostname,)
|
|
||||||
} else {
|
|
||||||
let rand1 = dc_create_id();
|
|
||||||
format!("Mr.{}.{}{}", rand1, rand2, at_hostname)
|
|
||||||
};
|
|
||||||
|
|
||||||
ret.strdup()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Generate globally-unique message-id for a new outgoing message.
|
|
||||||
///
|
|
||||||
/// Note: Do not add a counter or any private data as as this may give
|
|
||||||
/// unneeded information to the receiver
|
|
||||||
pub fn dc_create_outgoing_rfc724_mid_safe(grpid: Option<&str>, from_addr: &str) -> String {
|
|
||||||
let hostname = from_addr
|
let hostname = from_addr
|
||||||
.find('@')
|
.find('@')
|
||||||
.map(|k| &from_addr[k..])
|
.map(|k| &from_addr[k..])
|
||||||
|
|||||||
Reference in New Issue
Block a user