mirror of
https://github.com/chatmail/core.git
synced 2026-04-13 03:30:29 +03:00
Compare commits
45 Commits
merge1
...
eventloggi
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6bc5d1b90e | ||
|
|
197d94ad9d | ||
|
|
7ce337c6d0 | ||
|
|
10148d2e43 | ||
|
|
686678c96c | ||
|
|
c116d6f73f | ||
|
|
a7c8ebc089 | ||
|
|
69dc237ee3 | ||
|
|
3dfd623db7 | ||
|
|
d1825956b2 | ||
|
|
30ca377586 | ||
|
|
df5464ea80 | ||
|
|
e4bf9956a5 | ||
|
|
d353d9d9d8 | ||
|
|
1ad45ed4d6 | ||
|
|
496e980a17 | ||
|
|
fa09e46ed9 | ||
|
|
d6de420b9a | ||
|
|
38eb708db8 | ||
|
|
7a59da5f8f | ||
|
|
f13a1d4a2f | ||
|
|
7b3a450918 | ||
|
|
169923b102 | ||
|
|
42688a0622 | ||
|
|
35f3c0edd1 | ||
|
|
f58b1d66c2 | ||
|
|
e7a236264a | ||
|
|
aaa5b820d9 | ||
|
|
e7f0745010 | ||
|
|
c68e7ae14e | ||
|
|
618087e5a7 | ||
|
|
245abb8384 | ||
|
|
a3e1042001 | ||
|
|
7b7ce9348f | ||
|
|
7a4808ba0d | ||
|
|
8f240f7153 | ||
|
|
7d0b5d8abb | ||
|
|
ee317cb1b5 | ||
|
|
7b736fe635 | ||
|
|
c7db15352a | ||
|
|
0c5015d92b | ||
|
|
ab2d2e7583 | ||
|
|
c11ac46dce | ||
|
|
8a0fc609e6 | ||
|
|
0b37167be8 |
2
.gitattributes
vendored
2
.gitattributes
vendored
@@ -2,7 +2,7 @@
|
||||
# ensures this even if the user has not set core.autocrlf.
|
||||
* text=auto
|
||||
|
||||
# binary files should be detected by git, however, to be sure, you can add them here explictly
|
||||
# binary files should be detected by git, however, to be sure, you can add them here explicitly
|
||||
*.png binary
|
||||
*.jpg binary
|
||||
*.gif binary
|
||||
|
||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,6 +1,5 @@
|
||||
/target
|
||||
**/*.rs.bk
|
||||
Cargo.lock
|
||||
|
||||
# ignore vi temporaries
|
||||
*~
|
||||
@@ -17,3 +16,5 @@ python/.tox
|
||||
*.egg-info
|
||||
__pycache__
|
||||
python/src/deltachat/capi*.so
|
||||
|
||||
python/liveconfig*
|
||||
|
||||
2954
Cargo.lock
generated
Normal file
2954
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
10
Cargo.toml
10
Cargo.toml
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "deltachat"
|
||||
version = "1.0.0-alpha.2"
|
||||
version = "1.0.0-alpha.3"
|
||||
authors = ["dignifiedquire <dignifiedquire@gmail.com>"]
|
||||
edition = "2018"
|
||||
license = "MPL"
|
||||
@@ -39,6 +39,10 @@ rusqlite = { version = "0.19", features = ["bundled"] }
|
||||
addr = "0.2.0"
|
||||
r2d2_sqlite = "0.11.0"
|
||||
r2d2 = "0.8.5"
|
||||
strum = "0.15.0"
|
||||
strum_macros = "0.15.0"
|
||||
thread-local-object = "0.1.0"
|
||||
backtrace = "0.3.33"
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile = "3.0"
|
||||
@@ -50,6 +54,10 @@ members = [
|
||||
"deltachat-ffi"
|
||||
]
|
||||
|
||||
[patch.crates-io]
|
||||
rusqlite = { git = "http://github.com/dignifiedquire/rusqlite", branch = "fix/text", features = ["bundled"] }
|
||||
|
||||
|
||||
[[example]]
|
||||
name = "simple"
|
||||
path = "examples/simple.rs"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "deltachat_ffi"
|
||||
version = "1.0.0-alpha.1"
|
||||
version = "1.0.0-alpha.3"
|
||||
description = "Deltachat FFI"
|
||||
authors = ["dignifiedquire <dignifiedquire@gmail.com>"]
|
||||
edition = "2018"
|
||||
@@ -24,3 +24,4 @@ default = ["vendored", "nightly", "ringbuf"]
|
||||
vendored = ["deltachat/vendored"]
|
||||
nightly = ["deltachat/nightly"]
|
||||
ringbuf = ["deltachat/ringbuf"]
|
||||
|
||||
|
||||
@@ -503,8 +503,8 @@ int dc_chat_is_sending_locations (const dc_chat_t*);
|
||||
#define DC_STATE_OUT_MDN_RCVD 28
|
||||
|
||||
|
||||
#define DC_MAX_GET_TEXT_LEN 30000 // approx. max. lenght returned by dc_msg_get_text()
|
||||
#define DC_MAX_GET_INFO_LEN 100000 // approx. max. lenght returned by dc_get_msg_info()
|
||||
#define DC_MAX_GET_TEXT_LEN 30000 // approx. max. length returned by dc_msg_get_text()
|
||||
#define DC_MAX_GET_INFO_LEN 100000 // approx. max. length returned by dc_get_msg_info()
|
||||
|
||||
|
||||
dc_msg_t* dc_msg_new (dc_context_t*, int viewtype);
|
||||
@@ -667,7 +667,7 @@ int64_t dc_lot_get_timestamp (const dc_lot_t*);
|
||||
* A voice message that was directly recorded by the user.
|
||||
* For all other audio messages, the type #DC_MSG_AUDIO should be used.
|
||||
* File and duration are set via dc_msg_set_file(), dc_msg_set_duration()
|
||||
* and retieved via dc_msg_get_file(), dc_msg_get_duration()
|
||||
* and retrieved via dc_msg_get_file(), dc_msg_get_duration()
|
||||
*/
|
||||
#define DC_MSG_VOICE 41
|
||||
|
||||
@@ -768,9 +768,9 @@ int64_t dc_lot_get_timestamp (const dc_lot_t*);
|
||||
* @}
|
||||
*/
|
||||
|
||||
#define DC_LP_AUTH_FLAGS (DC_LP_AUTH_OAUTH2|DC_LP_AUTH_NORMAL) // if none of these flags are set, the default is choosen
|
||||
#define DC_LP_IMAP_SOCKET_FLAGS (DC_LP_IMAP_SOCKET_STARTTLS|DC_LP_IMAP_SOCKET_SSL|DC_LP_IMAP_SOCKET_PLAIN) // if none of these flags are set, the default is choosen
|
||||
#define DC_LP_SMTP_SOCKET_FLAGS (DC_LP_SMTP_SOCKET_STARTTLS|DC_LP_SMTP_SOCKET_SSL|DC_LP_SMTP_SOCKET_PLAIN) // if none of these flags are set, the default is choosen
|
||||
#define DC_LP_AUTH_FLAGS (DC_LP_AUTH_OAUTH2|DC_LP_AUTH_NORMAL) // if none of these flags are set, the default is chosen
|
||||
#define DC_LP_IMAP_SOCKET_FLAGS (DC_LP_IMAP_SOCKET_STARTTLS|DC_LP_IMAP_SOCKET_SSL|DC_LP_IMAP_SOCKET_PLAIN) // if none of these flags are set, the default is chosen
|
||||
#define DC_LP_SMTP_SOCKET_FLAGS (DC_LP_SMTP_SOCKET_STARTTLS|DC_LP_SMTP_SOCKET_SSL|DC_LP_SMTP_SOCKET_PLAIN) // if none of these flags are set, the default is chosen
|
||||
|
||||
|
||||
|
||||
@@ -851,7 +851,7 @@ int64_t dc_lot_get_timestamp (const dc_lot_t*);
|
||||
* The library-user should report an error to the end-user.
|
||||
* Passed to the callback given to dc_context_new().
|
||||
*
|
||||
* As most things are asynchrounous, things may go wrong at any time and the user
|
||||
* As most things are asynchronous, things may go wrong at any time and the user
|
||||
* should not be disturbed by a dialog or so. Instead, use a bubble or so.
|
||||
*
|
||||
* However, for ongoing processes (eg. dc_configure())
|
||||
@@ -882,7 +882,7 @@ int64_t dc_lot_get_timestamp (const dc_lot_t*);
|
||||
*
|
||||
* Moreover, if the UI detects that the device is offline,
|
||||
* it is probably more useful to report this to the user
|
||||
* instread of the string from data2.
|
||||
* instead of the string from data2.
|
||||
*
|
||||
* @param data1 (int) 1=first/new network error, should be reported the user;
|
||||
* 0=subsequent network error, should be logged only
|
||||
|
||||
@@ -10,6 +10,8 @@
|
||||
#[macro_use]
|
||||
extern crate human_panic;
|
||||
|
||||
use std::str::FromStr;
|
||||
|
||||
use deltachat::*;
|
||||
|
||||
// TODO: constants
|
||||
@@ -91,7 +93,10 @@ pub unsafe extern "C" fn dc_set_config(
|
||||
assert!(!key.is_null(), "invalid key");
|
||||
let context = &*context;
|
||||
|
||||
context::dc_set_config(context, dc_tools::as_str(key), as_opt_str(value))
|
||||
match config::Config::from_str(dc_tools::as_str(key)) {
|
||||
Ok(key) => context.set_config(key, as_opt_str(value)).is_ok() as libc::c_int,
|
||||
Err(_) => 0,
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
@@ -103,7 +108,13 @@ pub unsafe extern "C" fn dc_get_config(
|
||||
assert!(!key.is_null(), "invalid key");
|
||||
let context = &*context;
|
||||
|
||||
into_cstring(context::dc_get_config(context, dc_tools::as_str(key)))
|
||||
match config::Config::from_str(dc_tools::as_str(key)) {
|
||||
Ok(key) => {
|
||||
let value = context.get_config(key).unwrap_or_default();
|
||||
dc_tools::to_cstring(value)
|
||||
}
|
||||
Err(_) => std::ptr::null_mut(),
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
@@ -126,7 +137,7 @@ pub unsafe extern "C" fn dc_get_oauth2_url(
|
||||
let addr = dc_tools::to_string(addr);
|
||||
let redirect = dc_tools::to_string(redirect);
|
||||
match oauth2::dc_get_oauth2_url(context, addr, redirect) {
|
||||
Some(res) => libc::strdup(dc_tools::to_cstring(res).as_ptr()),
|
||||
Some(res) => dc_tools::to_cstring(res),
|
||||
None => std::ptr::null_mut(),
|
||||
}
|
||||
}
|
||||
@@ -470,7 +481,8 @@ pub unsafe extern "C" fn dc_delete_chat(context: *mut dc_context_t, chat_id: u32
|
||||
assert!(!context.is_null());
|
||||
let context = &*context;
|
||||
|
||||
dc_chat::dc_delete_chat(context, chat_id)
|
||||
// TODO: update to indicate public api success/failure of deletion
|
||||
dc_chat::dc_delete_chat(context, chat_id);
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
@@ -1534,7 +1546,3 @@ fn as_opt_str<'a>(s: *const libc::c_char) -> Option<&'a str> {
|
||||
|
||||
Some(dc_tools::as_str(s))
|
||||
}
|
||||
|
||||
unsafe fn into_cstring(s: impl AsRef<str>) -> *mut libc::c_char {
|
||||
dc_tools::dc_strdup(dc_tools::to_cstring(s).as_ptr())
|
||||
}
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
use std::str::FromStr;
|
||||
|
||||
use deltachat::config;
|
||||
use deltachat::constants::*;
|
||||
use deltachat::context::*;
|
||||
use deltachat::dc_array::*;
|
||||
@@ -69,7 +72,7 @@ pub unsafe fn dc_reset_tables(context: &Context, bits: i32) -> i32 {
|
||||
sql::execute(
|
||||
context,
|
||||
&context.sql,
|
||||
"DELETE FROM config WHERE keyname LIKE \'imap.%\' OR keyname LIKE \'configured%\';",
|
||||
"DELETE FROM config WHERE keyname LIKE 'imap.%' OR keyname LIKE 'configured%';",
|
||||
params![],
|
||||
);
|
||||
sql::execute(context, &context.sql, "DELETE FROM leftgrps;", params![]);
|
||||
@@ -124,22 +127,19 @@ unsafe fn poke_spec(context: &Context, spec: *const libc::c_char) -> libc::c_int
|
||||
/* if `spec` is given, remember it for later usage; if it is not given, try to use the last one */
|
||||
if !spec.is_null() {
|
||||
real_spec = dc_strdup(spec);
|
||||
sql::set_config(
|
||||
context,
|
||||
&context.sql,
|
||||
"import_spec",
|
||||
Some(as_str(real_spec)),
|
||||
);
|
||||
context
|
||||
.sql
|
||||
.set_config(context, "import_spec", Some(as_str(real_spec)));
|
||||
current_block = 7149356873433890176;
|
||||
} else {
|
||||
let rs = sql::get_config(context, &context.sql, "import_spec", None);
|
||||
let rs = context.sql.get_config(context, "import_spec");
|
||||
if rs.is_none() {
|
||||
error!(context, 0, "Import: No file or folder given.");
|
||||
current_block = 8522321847195001863;
|
||||
} else {
|
||||
current_block = 7149356873433890176;
|
||||
}
|
||||
real_spec = strdup(to_cstring(rs.unwrap_or_default()).as_ptr());
|
||||
real_spec = to_cstring(rs.unwrap_or_default());
|
||||
}
|
||||
match current_block {
|
||||
8522321847195001863 => {}
|
||||
@@ -177,10 +177,10 @@ unsafe fn poke_spec(context: &Context, spec: *const libc::c_char) -> libc::c_int
|
||||
let path_plus_name = format!("{}/{}", as_str(real_spec), name);
|
||||
info!(context, 0, "Import: {}", path_plus_name);
|
||||
let path_plus_name_c = to_cstring(path_plus_name);
|
||||
|
||||
if 0 != dc_poke_eml_file(context, path_plus_name_c.as_ptr()) {
|
||||
if 0 != dc_poke_eml_file(context, path_plus_name_c) {
|
||||
read_cnt += 1
|
||||
}
|
||||
free(path_plus_name_c as *mut _);
|
||||
}
|
||||
}
|
||||
current_block = 1622411330066726685;
|
||||
@@ -380,18 +380,16 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
|
||||
let mut args = line.splitn(3, ' ');
|
||||
let arg0 = args.next().unwrap_or_default();
|
||||
let arg1 = args.next().unwrap_or_default();
|
||||
let arg1_c = to_cstring(arg1);
|
||||
let arg1_c_ptr = if arg1.is_empty() {
|
||||
let arg1_c = if arg1.is_empty() {
|
||||
std::ptr::null()
|
||||
} else {
|
||||
arg1_c.as_ptr()
|
||||
to_cstring(arg1) as *const _
|
||||
};
|
||||
let arg2 = args.next().unwrap_or_default();
|
||||
let arg2_c = to_cstring(arg2);
|
||||
let arg2_c_ptr = if arg2.is_empty() {
|
||||
let arg2_c = if arg2.is_empty() {
|
||||
std::ptr::null()
|
||||
} else {
|
||||
arg2_c.as_ptr()
|
||||
to_cstring(arg2) as *const _
|
||||
};
|
||||
|
||||
match arg0 {
|
||||
@@ -481,7 +479,9 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
|
||||
},
|
||||
"auth" => {
|
||||
if 0 == S_IS_AUTH {
|
||||
let is_pw = dc_get_config(context, "mail_pw");
|
||||
let is_pw = context
|
||||
.get_config(config::Config::MailPw)
|
||||
.unwrap_or_default();
|
||||
if arg1 == is_pw {
|
||||
S_IS_AUTH = 1;
|
||||
} else {
|
||||
@@ -495,7 +495,7 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
|
||||
ensure!(!arg1.is_empty(), "Argument <file> missing");
|
||||
dc_close(context);
|
||||
ensure!(
|
||||
0 != dc_open(context, arg1_c_ptr, 0 as *const libc::c_char),
|
||||
0 != dc_open(context, arg1_c, 0 as *const libc::c_char),
|
||||
"Open failed"
|
||||
);
|
||||
}
|
||||
@@ -536,7 +536,7 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
|
||||
!arg1.is_empty() && !arg2.is_empty(),
|
||||
"Arguments <msg-id> <setup-code> expected"
|
||||
);
|
||||
if 0 == dc_continue_key_transfer(context, arg1.parse().unwrap(), arg2_c_ptr) {
|
||||
if 0 == dc_continue_key_transfer(context, arg1.parse().unwrap(), arg2_c) {
|
||||
bail!("Continue key transfer failed");
|
||||
}
|
||||
}
|
||||
@@ -551,7 +551,7 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
|
||||
}
|
||||
"import-backup" => {
|
||||
ensure!(!arg1.is_empty(), "Argument <backup-file> missing.");
|
||||
dc_imex(context, 12, arg1_c_ptr, 0 as *const libc::c_char);
|
||||
dc_imex(context, 12, arg1_c, 0 as *const libc::c_char);
|
||||
}
|
||||
"export-keys" => {
|
||||
dc_imex(context, 1, context.get_blobdir(), 0 as *const libc::c_char);
|
||||
@@ -588,7 +588,7 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
|
||||
free(setup_code as *mut libc::c_void);
|
||||
}
|
||||
"poke" => {
|
||||
ensure!(0 != poke_spec(context, arg1_c_ptr), "Poke failed");
|
||||
ensure!(0 != poke_spec(context, arg1_c), "Poke failed");
|
||||
}
|
||||
"reset" => {
|
||||
ensure!(!arg1.is_empty(), "Argument <bits> missing: 1=jobs, 2=peerstates, 4=private keys, 8=rest but server config");
|
||||
@@ -601,15 +601,15 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
|
||||
}
|
||||
"set" => {
|
||||
ensure!(!arg1.is_empty(), "Argument <key> missing.");
|
||||
ensure!(
|
||||
0 != dc_set_config(context, &arg1, Some(&arg2)),
|
||||
"Set config failed"
|
||||
);
|
||||
let key = config::Config::from_str(&arg1)?;
|
||||
let value = if arg2.is_empty() { None } else { Some(arg2) };
|
||||
context.set_config(key, value)?;
|
||||
}
|
||||
"get" => {
|
||||
ensure!(!arg1.is_empty(), "Argument <key> missing.");
|
||||
let val = dc_get_config(context, &arg1);
|
||||
println!("{}={}", arg1, val);
|
||||
let key = config::Config::from_str(&arg1)?;
|
||||
let val = context.get_config(key);
|
||||
println!("{}={:?}", key, val);
|
||||
}
|
||||
"info" => {
|
||||
println!("{}", to_string(dc_get_info(context)));
|
||||
@@ -622,7 +622,7 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
|
||||
}
|
||||
"listchats" | "listarchived" | "chats" => {
|
||||
let listflags = if arg0 == "listarchived" { 0x01 } else { 0 };
|
||||
let chatlist = dc_get_chatlist(context, listflags, arg1_c_ptr, 0 as uint32_t);
|
||||
let chatlist = dc_get_chatlist(context, listflags, arg1_c, 0 as uint32_t);
|
||||
ensure!(!chatlist.is_null(), "Failed to retrieve chatlist");
|
||||
|
||||
let mut i: libc::c_int;
|
||||
@@ -780,8 +780,7 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
|
||||
}
|
||||
"creategroup" => {
|
||||
ensure!(!arg1.is_empty(), "Argument <name> missing.");
|
||||
let chat_id_1: libc::c_int =
|
||||
dc_create_group_chat(context, 0, arg1_c_ptr) as libc::c_int;
|
||||
let chat_id_1: libc::c_int = dc_create_group_chat(context, 0, arg1_c) as libc::c_int;
|
||||
if chat_id_1 != 0 {
|
||||
println!("Group#{} created successfully.", chat_id_1,);
|
||||
} else {
|
||||
@@ -790,8 +789,7 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
|
||||
}
|
||||
"createverified" => {
|
||||
ensure!(!arg1.is_empty(), "Argument <name> missing.");
|
||||
let chat_id_2: libc::c_int =
|
||||
dc_create_group_chat(context, 1, arg1_c_ptr) as libc::c_int;
|
||||
let chat_id_2: libc::c_int = dc_create_group_chat(context, 1, arg1_c) as libc::c_int;
|
||||
if chat_id_2 != 0 {
|
||||
println!("VerifiedGroup#{} created successfully.", chat_id_2,);
|
||||
} else {
|
||||
@@ -830,7 +828,7 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
|
||||
"groupname" => {
|
||||
ensure!(!sel_chat.is_null(), "No chat selected.");
|
||||
ensure!(!arg1.is_empty(), "Argument <name> missing.");
|
||||
if 0 != dc_set_chat_name(context, dc_chat_get_id(sel_chat), arg1_c_ptr) {
|
||||
if 0 != dc_set_chat_name(context, dc_chat_get_id(sel_chat), arg1_c) {
|
||||
println!("Chat name set");
|
||||
} else {
|
||||
bail!("Failed to set chat name");
|
||||
@@ -844,7 +842,7 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
|
||||
context,
|
||||
dc_chat_get_id(sel_chat),
|
||||
if !arg1.is_empty() {
|
||||
arg1_c_ptr
|
||||
arg1_c
|
||||
} else {
|
||||
std::ptr::null_mut()
|
||||
},
|
||||
@@ -909,7 +907,7 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
|
||||
|
||||
let seconds = arg1.parse().unwrap();
|
||||
dc_send_locations_to_chat(context, dc_chat_get_id(sel_chat), seconds);
|
||||
println!("Locations will be sent to Chat#{} for {} seconds. Use \'setlocation <lat> <lng>\' to play around.", dc_chat_get_id(sel_chat), seconds);
|
||||
println!("Locations will be sent to Chat#{} for {} seconds. Use 'setlocation <lat> <lng>' to play around.", dc_chat_get_id(sel_chat), seconds);
|
||||
}
|
||||
"setlocation" => {
|
||||
ensure!(
|
||||
@@ -935,9 +933,11 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
|
||||
|
||||
let msg = to_cstring(format!("{} {}", arg1, arg2));
|
||||
|
||||
if 0 != dc_send_text_msg(context, dc_chat_get_id(sel_chat), msg.as_ptr()) {
|
||||
if 0 != dc_send_text_msg(context, dc_chat_get_id(sel_chat), msg) {
|
||||
println!("Message sent.");
|
||||
free(msg as *mut _);
|
||||
} else {
|
||||
free(msg as *mut _);
|
||||
bail!("Sending failed.");
|
||||
}
|
||||
}
|
||||
@@ -958,8 +958,8 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
|
||||
ensure!(!arg1.is_empty() && !arg2.is_empty(), "No file given.");
|
||||
|
||||
let msg_0 = dc_msg_new(context, if arg0 == "sendimage" { 20 } else { 60 });
|
||||
dc_msg_set_file(msg_0, arg1_c_ptr, 0 as *const libc::c_char);
|
||||
dc_msg_set_text(msg_0, arg2_c_ptr);
|
||||
dc_msg_set_file(msg_0, arg1_c, 0 as *const libc::c_char);
|
||||
dc_msg_set_text(msg_0, arg2_c);
|
||||
dc_send_msg(context, dc_chat_get_id(sel_chat), msg_0);
|
||||
dc_msg_unref(msg_0);
|
||||
}
|
||||
@@ -972,7 +972,7 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
|
||||
0 as libc::c_uint
|
||||
};
|
||||
|
||||
let msglist_0 = dc_search_msgs(context, chat, arg1_c_ptr);
|
||||
let msglist_0 = dc_search_msgs(context, chat, arg1_c);
|
||||
|
||||
if !msglist_0.is_null() {
|
||||
log_msglist(context, msglist_0);
|
||||
@@ -985,7 +985,7 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
|
||||
|
||||
if !arg1.is_empty() {
|
||||
let draft_0 = dc_msg_new(context, 10);
|
||||
dc_msg_set_text(draft_0, arg1_c_ptr);
|
||||
dc_msg_set_text(draft_0, arg1_c);
|
||||
dc_set_draft(context, dc_chat_get_id(sel_chat), draft_0);
|
||||
dc_msg_unref(draft_0);
|
||||
println!("Draft saved.");
|
||||
@@ -1077,7 +1077,7 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
|
||||
} else {
|
||||
0x2
|
||||
},
|
||||
arg1_c_ptr,
|
||||
arg1_c,
|
||||
);
|
||||
if !contacts.is_null() {
|
||||
log_contactlist(context, contacts);
|
||||
@@ -1093,13 +1093,13 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
|
||||
if !arg2.is_empty() {
|
||||
let book = dc_mprintf(
|
||||
b"%s\n%s\x00" as *const u8 as *const libc::c_char,
|
||||
arg1_c_ptr,
|
||||
arg2_c_ptr,
|
||||
arg1_c,
|
||||
arg2_c,
|
||||
);
|
||||
dc_add_address_book(context, book);
|
||||
free(book as *mut libc::c_void);
|
||||
} else {
|
||||
if 0 == dc_create_contact(context, 0 as *const libc::c_char, arg1_c_ptr) {
|
||||
if 0 == dc_create_contact(context, 0 as *const libc::c_char, arg1_c) {
|
||||
bail!("Failed to create contact");
|
||||
}
|
||||
}
|
||||
@@ -1146,7 +1146,7 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
|
||||
}
|
||||
"checkqr" => {
|
||||
ensure!(!arg1.is_empty(), "Argument <qr-content> missing.");
|
||||
let res = dc_check_qr(context, arg1_c_ptr);
|
||||
let res = dc_check_qr(context, arg1_c);
|
||||
println!(
|
||||
"state={}, id={}, text1={}, text2={}",
|
||||
(*res).state as libc::c_int,
|
||||
@@ -1174,7 +1174,7 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
|
||||
|
||||
if 0 != dc_read_file(
|
||||
context,
|
||||
arg1_c_ptr,
|
||||
arg1_c,
|
||||
&mut buf as *mut *mut libc::c_uchar as *mut *mut libc::c_void,
|
||||
&mut buf_bytes,
|
||||
) {
|
||||
@@ -1192,5 +1192,8 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
|
||||
dc_chat_unref(sel_chat);
|
||||
}
|
||||
|
||||
free(arg1_c as *mut _);
|
||||
free(arg2_c as *mut _);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ use std::borrow::Cow::{self, Borrowed, Owned};
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::{Arc, Mutex, RwLock};
|
||||
|
||||
use deltachat::config;
|
||||
use deltachat::constants::*;
|
||||
use deltachat::context::*;
|
||||
use deltachat::dc_configure::*;
|
||||
@@ -397,11 +398,10 @@ fn main_0(args: Vec<String>) -> Result<(), failure::Error> {
|
||||
|
||||
if args.len() == 2 {
|
||||
if 0 == unsafe {
|
||||
dc_open(
|
||||
&mut context,
|
||||
to_cstring(&args[1]).as_ptr(),
|
||||
0 as *const libc::c_char,
|
||||
)
|
||||
let a = to_cstring(&args[1]);
|
||||
let res = dc_open(&mut context, a, 0 as *const _);
|
||||
free(a as *mut _);
|
||||
res
|
||||
} {
|
||||
println!("Error: Cannot open {}.", args[0],);
|
||||
}
|
||||
@@ -481,11 +481,10 @@ unsafe fn handle_cmd(line: &str, ctx: Arc<RwLock<Context>>) -> Result<ExitResult
|
||||
let mut args = line.splitn(2, ' ');
|
||||
let arg0 = args.next().unwrap_or_default();
|
||||
let arg1 = args.next().unwrap_or_default();
|
||||
let arg1_c = to_cstring(arg1);
|
||||
let arg1_c_ptr = if arg1.is_empty() {
|
||||
let arg1_c = if arg1.is_empty() {
|
||||
std::ptr::null()
|
||||
} else {
|
||||
arg1_c.as_ptr()
|
||||
to_cstring(arg1)
|
||||
};
|
||||
|
||||
match arg0 {
|
||||
@@ -514,10 +513,7 @@ unsafe fn handle_cmd(line: &str, ctx: Arc<RwLock<Context>>) -> Result<ExitResult
|
||||
dc_configure(&ctx.read().unwrap());
|
||||
}
|
||||
"oauth2" => {
|
||||
let addr = dc_get_config(&ctx.read().unwrap(), "addr");
|
||||
if addr.is_empty() {
|
||||
println!("oauth2: set addr first.");
|
||||
} else {
|
||||
if let Some(addr) = ctx.read().unwrap().get_config(config::Config::Addr) {
|
||||
let oauth2_url = dc_get_oauth2_url(
|
||||
&ctx.read().unwrap(),
|
||||
&addr,
|
||||
@@ -528,6 +524,8 @@ unsafe fn handle_cmd(line: &str, ctx: Arc<RwLock<Context>>) -> Result<ExitResult
|
||||
} else {
|
||||
println!("Open the following url, set mail_pw to the generated token and server_flags to 2:\n{}", oauth2_url.unwrap());
|
||||
}
|
||||
} else {
|
||||
println!("oauth2: set addr first.");
|
||||
}
|
||||
}
|
||||
"clear" => {
|
||||
@@ -559,13 +557,15 @@ unsafe fn handle_cmd(line: &str, ctx: Arc<RwLock<Context>>) -> Result<ExitResult
|
||||
"joinqr" => {
|
||||
start_threads(ctx.clone());
|
||||
if !arg0.is_empty() {
|
||||
dc_join_securejoin(&ctx.read().unwrap(), arg1_c_ptr);
|
||||
dc_join_securejoin(&ctx.read().unwrap(), arg1_c);
|
||||
}
|
||||
}
|
||||
"exit" => return Ok(ExitResult::Exit),
|
||||
_ => dc_cmdline(&ctx.read().unwrap(), line)?,
|
||||
}
|
||||
|
||||
free(arg1_c as *mut _);
|
||||
|
||||
Ok(ExitResult::Continue)
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ use std::sync::{Arc, RwLock};
|
||||
use std::{thread, time};
|
||||
use tempfile::tempdir;
|
||||
|
||||
use deltachat::config;
|
||||
use deltachat::constants::Event;
|
||||
use deltachat::context::*;
|
||||
use deltachat::dc_chat::*;
|
||||
@@ -85,8 +86,8 @@ fn main() {
|
||||
let args = std::env::args().collect::<Vec<String>>();
|
||||
assert_eq!(args.len(), 2, "missing password");
|
||||
let pw = args[1].clone();
|
||||
dc_set_config(&ctx, "addr", Some("d@testrun.org"));
|
||||
dc_set_config(&ctx, "mail_pw", Some(&pw));
|
||||
ctx.set_config(config::Config::Addr, Some("d@testrun.org"));
|
||||
ctx.set_config(config::Config::MailPw, Some(&pw));
|
||||
dc_configure(&ctx);
|
||||
|
||||
thread::sleep(duration);
|
||||
@@ -121,8 +122,8 @@ fn main() {
|
||||
}
|
||||
dc_chatlist_unref(chats);
|
||||
|
||||
*running.clone().write().unwrap() = false;
|
||||
println!("stopping threads");
|
||||
thread::sleep(duration);
|
||||
|
||||
// let msglist = dc_get_chat_msgs(&ctx, chat_id, 0, 0);
|
||||
// for i in 0..dc_array_get_cnt(msglist) {
|
||||
// let msg_id = dc_array_get_id(msglist, i);
|
||||
@@ -133,6 +134,9 @@ fn main() {
|
||||
// }
|
||||
// dc_array_unref(msglist);
|
||||
|
||||
println!("stopping threads");
|
||||
|
||||
*running.clone().write().unwrap() = false;
|
||||
deltachat::dc_job::dc_interrupt_imap_idle(&ctx);
|
||||
deltachat::dc_job::dc_interrupt_smtp_idle(&ctx);
|
||||
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -ex
|
||||
|
||||
cargo build -p deltachat_ffi
|
||||
rm -rf build/ src/deltachat/*.so
|
||||
DCC_RS_DEV=`pwd`/.. pip install -e .
|
||||
36
python/install_python_bindings.py
Executable file
36
python/install_python_bindings.py
Executable file
@@ -0,0 +1,36 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
"""
|
||||
setup a python binding development in-place install with cargo debug symbols.
|
||||
"""
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
|
||||
if __name__ == "__main__":
|
||||
os.environ["DCC_RS_TARGET"] = target = "release"
|
||||
|
||||
toml = os.path.join(os.getcwd(), "..", "Cargo.toml")
|
||||
assert os.path.exists(toml)
|
||||
with open(toml) as f:
|
||||
s = orig = f.read()
|
||||
s += "\n"
|
||||
s += "[profile.release]\n"
|
||||
s += "debug = true\n"
|
||||
with open(toml, "w") as f:
|
||||
f.write(s)
|
||||
print("temporarily modifying Cargo.toml to provide release build with debug symbols ")
|
||||
try:
|
||||
subprocess.check_call([
|
||||
"cargo", "build", "-p", "deltachat_ffi", "--" + target
|
||||
])
|
||||
finally:
|
||||
with open(toml, "w") as f:
|
||||
f.write(orig)
|
||||
print("\nreseted Cargo.toml to previous original state")
|
||||
|
||||
subprocess.check_call("rm -rf build/ src/deltachat/*.so" , shell=True)
|
||||
|
||||
subprocess.check_call([
|
||||
"pip", "install", "-e", "."
|
||||
])
|
||||
@@ -61,7 +61,10 @@ def set_context_callback(dc_context, func):
|
||||
|
||||
|
||||
def clear_context_callback(dc_context):
|
||||
_DC_CALLBACK_MAP.pop(dc_context, None)
|
||||
try:
|
||||
_DC_CALLBACK_MAP.pop(dc_context, None)
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
|
||||
def get_dc_event_name(integer, _DC_EVENTNAME_MAP={}):
|
||||
|
||||
@@ -25,6 +25,7 @@ def ffibuilder():
|
||||
else:
|
||||
raise NotImplementedError("Compilation not supported yet on Windows, can you help?")
|
||||
objs = [os.path.join(projdir, 'target', target, 'libdeltachat.a')]
|
||||
assert os.path.exists(objs[0]), objs
|
||||
incs = [os.path.join(projdir, 'deltachat-ffi')]
|
||||
else:
|
||||
libs = ['deltachat']
|
||||
|
||||
@@ -36,14 +36,17 @@ class Account(object):
|
||||
lib.dc_context_new(lib.py_dc_callback, ffi.NULL, ffi.NULL),
|
||||
_destroy_dc_context,
|
||||
)
|
||||
if eventlogging:
|
||||
self._evlogger = EventLogger(self._dc_context, logid)
|
||||
deltachat.set_context_callback(self._dc_context, self._process_event)
|
||||
self._threads = IOThreads(self._dc_context, self._evlogger._log_event)
|
||||
else:
|
||||
self._threads = IOThreads(self._dc_context)
|
||||
|
||||
if hasattr(db_path, "encode"):
|
||||
db_path = db_path.encode("utf8")
|
||||
if not lib.dc_open(self._dc_context, db_path, ffi.NULL):
|
||||
raise ValueError("Could not dc_open: {}".format(db_path))
|
||||
if eventlogging:
|
||||
self._evlogger = EventLogger(self._dc_context, logid)
|
||||
deltachat.set_context_callback(self._dc_context, self._process_event)
|
||||
self._threads = IOThreads(self._dc_context)
|
||||
self._configkeys = self.get_config("sys.config_keys").split()
|
||||
self._imex_completed = threading.Event()
|
||||
|
||||
@@ -340,7 +343,7 @@ class Account(object):
|
||||
|
||||
def shutdown(self, wait=True):
|
||||
""" stop threads and close and remove underlying dc_context and callbacks. """
|
||||
if hasattr(self, "_dc_context"):
|
||||
if hasattr(self, "_dc_context") and hasattr(self, "_threads"):
|
||||
self.stop_threads(wait=False) # to interrupt idle and tell python threads to stop
|
||||
lib.dc_close(self._dc_context)
|
||||
self.stop_threads(wait=wait) # to wait for threads
|
||||
@@ -362,10 +365,11 @@ class Account(object):
|
||||
|
||||
|
||||
class IOThreads:
|
||||
def __init__(self, dc_context):
|
||||
def __init__(self, dc_context, log_event=lambda *args: None):
|
||||
self._dc_context = dc_context
|
||||
self._thread_quitflag = False
|
||||
self._name2thread = {}
|
||||
self._log_event = log_event
|
||||
|
||||
def is_started(self):
|
||||
return len(self._name2thread) > 0
|
||||
@@ -391,17 +395,19 @@ class IOThreads:
|
||||
thread.join()
|
||||
|
||||
def imap_thread_run(self):
|
||||
self._log_event("py-bindings-info", 0, "IMAP THREAD START")
|
||||
while not self._thread_quitflag:
|
||||
lib.dc_perform_imap_jobs(self._dc_context)
|
||||
lib.dc_perform_imap_fetch(self._dc_context)
|
||||
lib.dc_perform_imap_idle(self._dc_context)
|
||||
print("IMAP_THREAD finished")
|
||||
self._log_event("py-bindings-info", 0, "IMAP THREAD FINISHED")
|
||||
|
||||
def smtp_thread_run(self):
|
||||
self._log_event("py-bindings-info", 0, "SMTP THREAD START")
|
||||
while not self._thread_quitflag:
|
||||
lib.dc_perform_smtp_jobs(self._dc_context)
|
||||
lib.dc_perform_smtp_idle(self._dc_context)
|
||||
print("SMTP_THREAD finished")
|
||||
self._log_event("py-bindings-info", 0, "SMTP THREAD FINISHED")
|
||||
|
||||
|
||||
class EventLogger:
|
||||
|
||||
@@ -1,12 +1,18 @@
|
||||
from __future__ import print_function
|
||||
import pytest
|
||||
import os
|
||||
from deltachat import const
|
||||
from deltachat import const, Account
|
||||
from datetime import datetime, timedelta
|
||||
from conftest import wait_configuration_progress, wait_successful_IMAP_SMTP_connection
|
||||
|
||||
|
||||
class TestOfflineAccount:
|
||||
def test_wrong_db(self, tmpdir):
|
||||
p = tmpdir.join("hello.db")
|
||||
p.write("123")
|
||||
with pytest.raises(ValueError):
|
||||
Account(p.strpath)
|
||||
|
||||
def test_getinfo(self, acfactory):
|
||||
ac1 = acfactory.get_unconfigured_account()
|
||||
d = ac1.get_info()
|
||||
|
||||
@@ -32,7 +32,11 @@ class TestInCreation:
|
||||
chat2.add_contact(c2)
|
||||
wait_msgs_changed(ac1, 0, 0) # why not chat id?
|
||||
ac1.forward_messages([prepared_original], chat2)
|
||||
# XXX there might be two EVENT_MSGS_CHANGED and only one of them
|
||||
# is the one caused by forwarding
|
||||
forwarded_id = wait_msgs_changed(ac1, chat2.id)
|
||||
if forwarded_id == 0:
|
||||
forwarded_id = wait_msgs_changed(ac1, chat2.id)
|
||||
forwarded_msg = ac1.get_message_by_id(forwarded_id)
|
||||
assert forwarded_msg.get_state().is_out_preparing()
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from __future__ import print_function
|
||||
import pytest
|
||||
from deltachat import capi, Account, const, set_context_callback, clear_context_callback
|
||||
from deltachat import capi, const, set_context_callback, clear_context_callback
|
||||
from deltachat.capi import ffi
|
||||
from deltachat.capi import lib
|
||||
from deltachat.account import EventLogger
|
||||
|
||||
|
||||
@@ -23,17 +23,32 @@ def test_dc_close_events():
|
||||
evlog.set_timeout(5)
|
||||
set_context_callback(ctx, lambda ctx, evt_name, data1, data2: evlog(evt_name, data1, data2))
|
||||
capi.lib.dc_close(ctx)
|
||||
# test that we get events from dc_close
|
||||
print(evlog.get_matching("DC_EVENT_INFO", check_error=False))
|
||||
print(evlog.get_matching("DC_EVENT_INFO", check_error=False))
|
||||
print(evlog.get_matching("DC_EVENT_INFO", check_error=False))
|
||||
print(evlog.get_matching("DC_EVENT_INFO", check_error=False))
|
||||
|
||||
def find(info_string):
|
||||
while 1:
|
||||
ev = evlog.get_matching("DC_EVENT_INFO", check_error=False)
|
||||
data2 = ev[2]
|
||||
if info_string in data2:
|
||||
return
|
||||
else:
|
||||
print("skipping event", *ev)
|
||||
|
||||
find("disconnecting INBOX-watch")
|
||||
find("disconnecting sentbox-thread")
|
||||
find("disconnecting mvbox-thread")
|
||||
find("disconnecting SMTP")
|
||||
find("Database closed")
|
||||
|
||||
|
||||
def test_wrong_db(tmpdir):
|
||||
tmpdir.join("hello.db").write("123")
|
||||
with pytest.raises(ValueError):
|
||||
Account(db_path=tmpdir.strpath)
|
||||
dc_context = ffi.gc(
|
||||
lib.dc_context_new(lib.py_dc_callback, ffi.NULL, ffi.NULL),
|
||||
lib.dc_context_unref,
|
||||
)
|
||||
p = tmpdir.join("hello.db")
|
||||
# write an invalid database file
|
||||
p.write("x123" * 10)
|
||||
assert not lib.dc_open(dc_context, p.strpath.encode("ascii"), ffi.NULL)
|
||||
|
||||
|
||||
def test_event_defines():
|
||||
|
||||
3
release.toml
Normal file
3
release.toml
Normal file
@@ -0,0 +1,3 @@
|
||||
pre-release-commit-message = "chore({{crate_name}}): release {{version}}"
|
||||
pro-release-commit-message = "chore({{crate_name}}): starting development cycle for {{next_version}}"
|
||||
no-dev-version = true
|
||||
182
src/config.rs
Normal file
182
src/config.rs
Normal file
@@ -0,0 +1,182 @@
|
||||
use strum::{EnumProperty, IntoEnumIterator};
|
||||
use strum_macros::{AsRefStr, Display, EnumIter, EnumProperty, EnumString};
|
||||
|
||||
use crate::constants::DC_VERSION_STR;
|
||||
use crate::context::Context;
|
||||
use crate::dc_job::*;
|
||||
use crate::dc_stock::*;
|
||||
use crate::dc_tools::*;
|
||||
use crate::error::Error;
|
||||
use crate::x::*;
|
||||
|
||||
/// The available configuration keys.
|
||||
#[derive(
|
||||
Debug, Clone, Copy, PartialEq, Eq, Display, EnumString, AsRefStr, EnumIter, EnumProperty,
|
||||
)]
|
||||
#[strum(serialize_all = "snake_case")]
|
||||
pub enum Config {
|
||||
Addr,
|
||||
MailServer,
|
||||
MailUser,
|
||||
MailPw,
|
||||
MailPort,
|
||||
SendServer,
|
||||
SendUser,
|
||||
SendPw,
|
||||
SendPort,
|
||||
ServerFlags,
|
||||
#[strum(props(default = "INBOX"))]
|
||||
ImapFolder,
|
||||
Displayname,
|
||||
Selfstatus,
|
||||
Selfavatar,
|
||||
#[strum(props(default = "1"))]
|
||||
E2eeEnabled,
|
||||
#[strum(props(default = "1"))]
|
||||
MdnsEnabled,
|
||||
InboxWatch,
|
||||
#[strum(props(default = "1"))]
|
||||
SentboxWatch,
|
||||
#[strum(props(default = "1"))]
|
||||
MvboxWatch,
|
||||
#[strum(props(default = "1"))]
|
||||
MvboxMove,
|
||||
#[strum(props(default = "0"))]
|
||||
ShowEmails,
|
||||
SaveMimeHeaders,
|
||||
ConfiguredAddr,
|
||||
ConfiguredMailServer,
|
||||
ConfiguredMailUser,
|
||||
ConfiguredMailPw,
|
||||
ConfiguredMailPort,
|
||||
ConfiguredSendServer,
|
||||
ConfiguredSendUser,
|
||||
ConfiguredSendPw,
|
||||
ConfiguredSendPort,
|
||||
ConfiguredServerFlags,
|
||||
Configured,
|
||||
// Deprecated
|
||||
#[strum(serialize = "sys.version")]
|
||||
SysVersion,
|
||||
#[strum(serialize = "sys.msgsize_max_recommended")]
|
||||
SysMsgsizeMaxRecommended,
|
||||
#[strum(serialize = "sys.config_keys")]
|
||||
SysConfigKeys,
|
||||
}
|
||||
|
||||
impl Context {
|
||||
/// Get a configuration key. Returns `None` if no value is set, and no default value found.
|
||||
pub fn get_config(&self, key: Config) -> Option<String> {
|
||||
let value = match key {
|
||||
Config::Selfavatar => {
|
||||
let rel_path = self.sql.get_config(self, key);
|
||||
rel_path.map(|p| {
|
||||
let v = unsafe {
|
||||
let n = to_cstring(p);
|
||||
let res = dc_get_abs_path(self, n);
|
||||
free(n as *mut libc::c_void);
|
||||
res
|
||||
};
|
||||
let r = to_string(v);
|
||||
unsafe { free(v as *mut _) };
|
||||
r
|
||||
})
|
||||
}
|
||||
Config::SysVersion => Some(std::str::from_utf8(DC_VERSION_STR).unwrap().into()),
|
||||
Config::SysMsgsizeMaxRecommended => Some(format!("{}", 24 * 1024 * 1024 / 4 * 3)),
|
||||
Config::SysConfigKeys => Some(get_config_keys_string()),
|
||||
_ => self.sql.get_config(self, key),
|
||||
};
|
||||
|
||||
if value.is_some() {
|
||||
return value;
|
||||
}
|
||||
|
||||
// Default values
|
||||
match key {
|
||||
Config::Selfstatus => {
|
||||
let s = unsafe { dc_stock_str(self, 13) };
|
||||
let res = to_string(s);
|
||||
unsafe { free(s as *mut _) };
|
||||
Some(res)
|
||||
}
|
||||
_ => key.get_str("default").map(|s| s.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Set the given config key.
|
||||
/// If `None` is passed as a value the value is cleared and set to the default if there is one.
|
||||
pub fn set_config(&self, key: Config, value: Option<&str>) -> Result<(), Error> {
|
||||
match key {
|
||||
Config::Selfavatar if value.is_some() => {
|
||||
let rel_path = std::fs::canonicalize(value.unwrap())?;
|
||||
self.sql
|
||||
.set_config(self, key, Some(&rel_path.to_string_lossy()))
|
||||
}
|
||||
Config::InboxWatch => {
|
||||
let ret = self.sql.set_config(self, key, value);
|
||||
unsafe { dc_interrupt_imap_idle(self) };
|
||||
ret
|
||||
}
|
||||
Config::SentboxWatch => {
|
||||
let ret = self.sql.set_config(self, key, value);
|
||||
unsafe { dc_interrupt_sentbox_idle(self) };
|
||||
ret
|
||||
}
|
||||
Config::MvboxWatch => {
|
||||
let ret = self.sql.set_config(self, key, value);
|
||||
unsafe { dc_interrupt_mvbox_idle(self) };
|
||||
ret
|
||||
}
|
||||
Config::Selfstatus => {
|
||||
let def = unsafe { dc_stock_str(self, 13) };
|
||||
let val = if value.is_none() || value.unwrap() == as_str(def) {
|
||||
None
|
||||
} else {
|
||||
value
|
||||
};
|
||||
|
||||
let ret = self.sql.set_config(self, key, val);
|
||||
unsafe { free(def as *mut libc::c_void) };
|
||||
ret
|
||||
}
|
||||
_ => self.sql.set_config(self, key, value),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns all available configuration keys concated together.
|
||||
fn get_config_keys_string() -> String {
|
||||
let keys = Config::iter().fold(String::new(), |mut acc, key| {
|
||||
acc += key.as_ref();
|
||||
acc += " ";
|
||||
acc
|
||||
});
|
||||
|
||||
format!(" {} ", keys)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
use std::str::FromStr;
|
||||
use std::string::ToString;
|
||||
|
||||
#[test]
|
||||
fn test_to_string() {
|
||||
assert_eq!(Config::MailServer.to_string(), "mail_server");
|
||||
assert_eq!(Config::from_str("mail_server"), Ok(Config::MailServer));
|
||||
|
||||
assert_eq!(Config::SysConfigKeys.to_string(), "sys.config_keys");
|
||||
assert_eq!(
|
||||
Config::from_str("sys.config_keys"),
|
||||
Ok(Config::SysConfigKeys)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_default_prop() {
|
||||
assert_eq!(Config::ImapFolder.get_str("default"), Some("INBOX"));
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
//! Constants
|
||||
|
||||
pub const DC_VERSION_STR: &'static [u8; 14] = b"1.0.0-alpha.1\x00";
|
||||
pub const DC_VERSION_STR: &'static [u8; 14] = b"1.0.0-alpha.3\x00";
|
||||
|
||||
pub const DC_MOVE_STATE_MOVING: u32 = 3;
|
||||
pub const DC_MOVE_STATE_STAY: u32 = 2;
|
||||
@@ -80,9 +80,9 @@ pub const DC_STATE_OUT_FAILED: usize = 24;
|
||||
pub const DC_STATE_OUT_DELIVERED: usize = 26;
|
||||
pub const DC_STATE_OUT_MDN_RCVD: usize = 28;
|
||||
|
||||
/// approx. max. lenght returned by dc_msg_get_text()
|
||||
/// approx. max. length returned by dc_msg_get_text()
|
||||
pub const DC_MAX_GET_TEXT_LEN: usize = 30000;
|
||||
/// approx. max. lenght returned by dc_get_msg_info()
|
||||
/// approx. max. length returned by dc_get_msg_info()
|
||||
pub const DC_MAX_GET_INFO_LEN: usize = 100000;
|
||||
|
||||
pub const DC_CONTACT_ID_SELF: usize = 1;
|
||||
@@ -119,7 +119,7 @@ pub const DC_MSG_AUDIO: usize = 40;
|
||||
/// A voice message that was directly recorded by the user.
|
||||
/// For all other audio messages, the type #DC_MSG_AUDIO should be used.
|
||||
/// File and duration are set via dc_msg_set_file(), dc_msg_set_duration()
|
||||
/// and retieved via dc_msg_get_file(), dc_msg_get_duration()
|
||||
/// and retrieved via dc_msg_get_file(), dc_msg_get_duration()
|
||||
pub const DC_MSG_VOICE: usize = 41;
|
||||
|
||||
/// Video messages.
|
||||
@@ -173,12 +173,12 @@ pub const DC_LP_SMTP_SOCKET_SSL: usize = 0x20000;
|
||||
/// If this flag is set, automatic configuration is skipped.
|
||||
pub const DC_LP_SMTP_SOCKET_PLAIN: usize = 0x40000;
|
||||
|
||||
/// if none of these flags are set, the default is choosen
|
||||
/// if none of these flags are set, the default is chosen
|
||||
pub const DC_LP_AUTH_FLAGS: usize = (DC_LP_AUTH_OAUTH2 | DC_LP_AUTH_NORMAL);
|
||||
/// if none of these flags are set, the default is choosen
|
||||
/// if none of these flags are set, the default is chosen
|
||||
pub const DC_LP_IMAP_SOCKET_FLAGS: usize =
|
||||
(DC_LP_IMAP_SOCKET_STARTTLS | DC_LP_IMAP_SOCKET_SSL | DC_LP_IMAP_SOCKET_PLAIN);
|
||||
/// if none of these flags are set, the default is choosen
|
||||
/// if none of these flags are set, the default is chosen
|
||||
pub const DC_LP_SMTP_SOCKET_FLAGS: usize =
|
||||
(DC_LP_SMTP_SOCKET_STARTTLS | DC_LP_SMTP_SOCKET_SSL | DC_LP_SMTP_SOCKET_PLAIN);
|
||||
|
||||
@@ -237,7 +237,7 @@ pub enum Event {
|
||||
/// The library-user should report an error to the end-user.
|
||||
/// Passed to the callback given to dc_context_new().
|
||||
///
|
||||
/// As most things are asynchrounous, things may go wrong at any time and the user
|
||||
/// As most things are asynchronous, things may go wrong at any time and the user
|
||||
/// should not be disturbed by a dialog or so. Instead, use a bubble or so.
|
||||
///
|
||||
/// However, for ongoing processes (eg. dc_configure())
|
||||
@@ -265,7 +265,7 @@ pub enum Event {
|
||||
///
|
||||
/// Moreover, if the UI detects that the device is offline,
|
||||
/// it is probably more useful to report this to the user
|
||||
/// instread of the string from data2.
|
||||
/// instead of the string from data2.
|
||||
///
|
||||
/// @param data1 (int) 1=first/new network error, should be reported the user;
|
||||
/// 0=subsequent network error, should be logged only
|
||||
|
||||
266
src/context.rs
266
src/context.rs
@@ -11,58 +11,14 @@ use crate::dc_lot::dc_lot_t;
|
||||
use crate::dc_move::*;
|
||||
use crate::dc_msg::*;
|
||||
use crate::dc_receive_imf::*;
|
||||
use crate::dc_stock::*;
|
||||
use crate::dc_tools::*;
|
||||
use crate::imap::*;
|
||||
use crate::key::*;
|
||||
use crate::smtp::*;
|
||||
use crate::sql::{self, Sql};
|
||||
use crate::sql::Sql;
|
||||
use crate::types::*;
|
||||
use crate::x::*;
|
||||
|
||||
const CONFIG_KEYS: [&'static str; 33] = [
|
||||
"addr",
|
||||
"mail_server",
|
||||
"mail_user",
|
||||
"mail_pw",
|
||||
"mail_port",
|
||||
"send_server",
|
||||
"send_user",
|
||||
"send_pw",
|
||||
"send_port",
|
||||
"server_flags",
|
||||
"imap_folder",
|
||||
"displayname",
|
||||
"selfstatus",
|
||||
"selfavatar",
|
||||
"e2ee_enabled",
|
||||
"mdns_enabled",
|
||||
"inbox_watch",
|
||||
"sentbox_watch",
|
||||
"mvbox_watch",
|
||||
"mvbox_move",
|
||||
"show_emails",
|
||||
"save_mime_headers",
|
||||
"configured_addr",
|
||||
"configured_mail_server",
|
||||
"configured_mail_user",
|
||||
"configured_mail_pw",
|
||||
"configured_mail_port",
|
||||
"configured_send_server",
|
||||
"configured_send_user",
|
||||
"configured_send_pw",
|
||||
"configured_send_port",
|
||||
"configured_server_flags",
|
||||
"configured",
|
||||
];
|
||||
|
||||
// deprecated
|
||||
const SYS_CONFIG_KEYS: [&'static str; 3] = [
|
||||
"sys.version",
|
||||
"sys.msgsize_max_recommended",
|
||||
"sys.config_keys",
|
||||
];
|
||||
|
||||
#[repr(C)]
|
||||
pub struct Context {
|
||||
pub userdata: *mut libc::c_void,
|
||||
@@ -304,7 +260,7 @@ unsafe fn cb_set_config(context: &Context, key: *const libc::c_char, value: *con
|
||||
} else {
|
||||
Some(as_str(value))
|
||||
};
|
||||
sql::set_config(context, &context.sql, as_str(key), v);
|
||||
context.sql.set_config(context, as_str(key), v);
|
||||
}
|
||||
|
||||
/* *
|
||||
@@ -319,17 +275,11 @@ unsafe fn cb_get_config(
|
||||
key: *const libc::c_char,
|
||||
def: *const libc::c_char,
|
||||
) -> *mut libc::c_char {
|
||||
let d = if def.is_null() {
|
||||
None
|
||||
} else {
|
||||
Some(as_str(def))
|
||||
};
|
||||
let res = sql::get_config(context, &context.sql, as_str(key), d);
|
||||
if let Some(res) = res {
|
||||
strdup(to_cstring(res).as_ptr())
|
||||
} else {
|
||||
std::ptr::null_mut()
|
||||
}
|
||||
let res = context
|
||||
.sql
|
||||
.get_config(context, as_str(key))
|
||||
.unwrap_or_else(|| to_string(def));
|
||||
to_cstring(res)
|
||||
}
|
||||
|
||||
pub unsafe fn dc_context_unref(context: &mut Context) {
|
||||
@@ -416,154 +366,45 @@ pub unsafe fn dc_get_blobdir(context: &Context) -> *mut libc::c_char {
|
||||
dc_strdup(*context.blobdir.clone().read().unwrap())
|
||||
}
|
||||
|
||||
pub fn dc_set_config(context: &Context, key: impl AsRef<str>, value: Option<&str>) -> libc::c_int {
|
||||
let mut ret = 0;
|
||||
|
||||
if !is_settable_config_key(key.as_ref()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
match key.as_ref() {
|
||||
"selfavatar" if value.is_some() => {
|
||||
let mut rel_path = unsafe { dc_strdup(to_cstring(value.unwrap()).as_ptr()) };
|
||||
if 0 != unsafe { dc_make_rel_and_copy(context, &mut rel_path) } {
|
||||
ret = sql::set_config(context, &context.sql, key, Some(as_str(rel_path)));
|
||||
}
|
||||
unsafe { free(rel_path as *mut libc::c_void) };
|
||||
}
|
||||
"inbox_watch" => {
|
||||
ret = sql::set_config(context, &context.sql, key, value);
|
||||
unsafe { dc_interrupt_imap_idle(context) };
|
||||
}
|
||||
"sentbox_watch" => {
|
||||
ret = sql::set_config(context, &context.sql, key, value);
|
||||
unsafe { dc_interrupt_sentbox_idle(context) };
|
||||
}
|
||||
"mvbox_watch" => {
|
||||
ret = sql::set_config(context, &context.sql, key, value);
|
||||
unsafe { dc_interrupt_mvbox_idle(context) };
|
||||
}
|
||||
"selfstatus" => {
|
||||
let def = unsafe { dc_stock_str(context, 13) };
|
||||
let val = if value.is_none() || value.unwrap() == as_str(def) {
|
||||
None
|
||||
} else {
|
||||
value
|
||||
};
|
||||
|
||||
ret = sql::set_config(context, &context.sql, key, val);
|
||||
unsafe { free(def as *mut libc::c_void) };
|
||||
}
|
||||
_ => {
|
||||
ret = sql::set_config(context, &context.sql, key, value);
|
||||
}
|
||||
}
|
||||
ret
|
||||
}
|
||||
|
||||
/* ******************************************************************************
|
||||
* INI-handling, Information
|
||||
******************************************************************************/
|
||||
|
||||
fn is_settable_config_key(key: impl AsRef<str>) -> bool {
|
||||
CONFIG_KEYS
|
||||
.into_iter()
|
||||
.find(|c| **c == key.as_ref())
|
||||
.is_some()
|
||||
}
|
||||
|
||||
pub fn dc_get_config(context: &Context, key: impl AsRef<str>) -> String {
|
||||
if key.as_ref().starts_with("sys") {
|
||||
return get_sys_config_str(key.as_ref());
|
||||
}
|
||||
|
||||
if !is_gettable_config_key(key.as_ref()) {
|
||||
return "".into();
|
||||
}
|
||||
|
||||
let value = match key.as_ref() {
|
||||
"selfavatar" => {
|
||||
let rel_path = sql::get_config(context, &context.sql, key.as_ref(), None);
|
||||
rel_path.map(|p| {
|
||||
let v = unsafe { dc_get_abs_path(context, to_cstring(p).as_ptr()) };
|
||||
let r = to_string(v);
|
||||
unsafe { free(v as *mut _) };
|
||||
r
|
||||
})
|
||||
}
|
||||
_ => sql::get_config(context, &context.sql, key.as_ref(), None),
|
||||
};
|
||||
|
||||
if value.is_some() {
|
||||
return value.unwrap();
|
||||
}
|
||||
|
||||
match key.as_ref() {
|
||||
"e2ee_enabled" => "1".into(),
|
||||
"mdns_enabled" => "1".into(),
|
||||
"imap_folder" => "INBOX".into(),
|
||||
"inbox_watch" => "1".into(),
|
||||
"sentbox_watch" | "mvbox_watch" | "mvbox_move" => "1".into(),
|
||||
"show_emails" => "0".into(),
|
||||
"selfstatus" => {
|
||||
let s = unsafe { dc_stock_str(context, 13) };
|
||||
let res = to_string(s);
|
||||
unsafe { free(s as *mut _) };
|
||||
res
|
||||
}
|
||||
_ => "".into(),
|
||||
}
|
||||
}
|
||||
|
||||
fn is_gettable_config_key(key: impl AsRef<str>) -> bool {
|
||||
SYS_CONFIG_KEYS
|
||||
.into_iter()
|
||||
.find(|c| **c == key.as_ref())
|
||||
.is_some()
|
||||
|| is_settable_config_key(key)
|
||||
}
|
||||
|
||||
fn get_sys_config_str(key: impl AsRef<str>) -> String {
|
||||
match key.as_ref() {
|
||||
"sys.version" => std::str::from_utf8(DC_VERSION_STR).unwrap().into(),
|
||||
"sys.msgsize_max_recommended" => format!("{}", 24 * 1024 * 1024 / 4 * 3),
|
||||
"sys.config_keys" => get_config_keys_str(),
|
||||
_ => "".into(),
|
||||
}
|
||||
}
|
||||
|
||||
fn get_config_keys_str() -> String {
|
||||
let keys = &CONFIG_KEYS[..].join(" ");
|
||||
let sys_keys = &SYS_CONFIG_KEYS[..].join(" ");
|
||||
|
||||
format!(" {} {} ", keys, sys_keys)
|
||||
}
|
||||
|
||||
pub unsafe fn dc_get_info(context: &Context) -> *mut libc::c_char {
|
||||
let unset = "0";
|
||||
let l = dc_loginparam_read(context, &context.sql, "");
|
||||
let l2 = dc_loginparam_read(context, &context.sql, "configured_");
|
||||
let displayname = sql::get_config(context, &context.sql, "displayname", None);
|
||||
let displayname = context.sql.get_config(context, "displayname");
|
||||
let chats = dc_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 = dc_get_real_contact_cnt(context) as usize;
|
||||
let is_configured = sql::get_config_int(context, &context.sql, "configured", 0);
|
||||
let dbversion = sql::get_config_int(context, &context.sql, "dbversion", 0);
|
||||
let e2ee_enabled = sql::get_config_int(context, &context.sql, "e2ee_enabled", 1);
|
||||
let mdns_enabled = sql::get_config_int(context, &context.sql, "mdns_enabled", 1);
|
||||
let is_configured = context
|
||||
.sql
|
||||
.get_config_int(context, "configured")
|
||||
.unwrap_or_default();
|
||||
let dbversion = context
|
||||
.sql
|
||||
.get_config_int(context, "dbversion")
|
||||
.unwrap_or_default();
|
||||
let e2ee_enabled = context
|
||||
.sql
|
||||
.get_config_int(context, "e2ee_enabled")
|
||||
.unwrap_or_else(|| 1);
|
||||
let mdns_enabled = context
|
||||
.sql
|
||||
.get_config_int(context, "mdns_enabled")
|
||||
.unwrap_or_else(|| 1);
|
||||
|
||||
let prv_key_cnt: Option<isize> = sql::query_row(
|
||||
let prv_key_cnt: Option<isize> = context.sql.query_row_col(
|
||||
context,
|
||||
&context.sql,
|
||||
"SELECT COUNT(*) FROM keypairs;",
|
||||
rusqlite::NO_PARAMS,
|
||||
0,
|
||||
);
|
||||
|
||||
let pub_key_cnt: Option<isize> = sql::query_row(
|
||||
let pub_key_cnt: Option<isize> = context.sql.query_row_col(
|
||||
context,
|
||||
&context.sql,
|
||||
"SELECT COUNT(*) FROM acpeerstates;",
|
||||
rusqlite::NO_PARAMS,
|
||||
0,
|
||||
@@ -578,23 +419,34 @@ pub unsafe fn dc_get_info(context: &Context) -> *mut libc::c_char {
|
||||
|
||||
let l_readable_str = dc_loginparam_get_readable(&l);
|
||||
let l2_readable_str = dc_loginparam_get_readable(&l2);
|
||||
let inbox_watch = sql::get_config_int(context, &context.sql, "inbox_watch", 1);
|
||||
let sentbox_watch = sql::get_config_int(context, &context.sql, "sentbox_watch", 1);
|
||||
let mvbox_watch = sql::get_config_int(context, &context.sql, "mvbox_watch", 1);
|
||||
let mvbox_move = sql::get_config_int(context, &context.sql, "mvbox_move", 1);
|
||||
let folders_configured = sql::get_config_int(context, &context.sql, "folders_configured", 0);
|
||||
let configured_sentbox_folder = sql::get_config(
|
||||
context,
|
||||
&context.sql,
|
||||
"configured_sentbox_folder",
|
||||
Some("<unset>"),
|
||||
);
|
||||
let configured_mvbox_folder = sql::get_config(
|
||||
context,
|
||||
&context.sql,
|
||||
"configured_mvbox_folder",
|
||||
Some("<unset>"),
|
||||
);
|
||||
let inbox_watch = context
|
||||
.sql
|
||||
.get_config_int(context, "inbox_watch")
|
||||
.unwrap_or_else(|| 1);
|
||||
let sentbox_watch = context
|
||||
.sql
|
||||
.get_config_int(context, "sentbox_watch")
|
||||
.unwrap_or_else(|| 1);
|
||||
let mvbox_watch = context
|
||||
.sql
|
||||
.get_config_int(context, "mvbox_watch")
|
||||
.unwrap_or_else(|| 1);
|
||||
let mvbox_move = context
|
||||
.sql
|
||||
.get_config_int(context, "mvbox_move")
|
||||
.unwrap_or_else(|| 1);
|
||||
let folders_configured = context
|
||||
.sql
|
||||
.get_config_int(context, "folders_configured")
|
||||
.unwrap_or_default();
|
||||
let configured_sentbox_folder = context
|
||||
.sql
|
||||
.get_config(context, "configured_sentbox_folder")
|
||||
.unwrap_or_else(|| "<unset>".to_string());
|
||||
let configured_mvbox_folder = context
|
||||
.sql
|
||||
.get_config(context, "configured_mvbox_folder")
|
||||
.unwrap_or_else(|| "<unset>".to_string());
|
||||
|
||||
let res = format!(
|
||||
"deltachat_core_version=v{}\n\
|
||||
@@ -654,8 +506,8 @@ pub unsafe fn dc_get_info(context: &Context) -> *mut libc::c_char {
|
||||
mvbox_watch,
|
||||
mvbox_move,
|
||||
folders_configured,
|
||||
configured_sentbox_folder.unwrap_or_default(),
|
||||
configured_mvbox_folder.unwrap_or_default(),
|
||||
configured_sentbox_folder,
|
||||
configured_mvbox_folder,
|
||||
mdns_enabled,
|
||||
e2ee_enabled,
|
||||
prv_key_cnt.unwrap_or_default(),
|
||||
@@ -663,7 +515,7 @@ pub unsafe fn dc_get_info(context: &Context) -> *mut libc::c_char {
|
||||
fingerprint_str,
|
||||
);
|
||||
|
||||
strdup(to_cstring(res).as_ptr())
|
||||
to_cstring(res)
|
||||
}
|
||||
|
||||
pub unsafe fn dc_get_version_str() -> *mut libc::c_char {
|
||||
@@ -756,7 +608,7 @@ pub fn dc_is_inbox(_context: &Context, folder_name: impl AsRef<str>) -> bool {
|
||||
}
|
||||
|
||||
pub fn dc_is_sentbox(context: &Context, folder_name: impl AsRef<str>) -> bool {
|
||||
let sentbox_name = sql::get_config(context, &context.sql, "configured_sentbox_folder", None);
|
||||
let sentbox_name = context.sql.get_config(context, "configured_sentbox_folder");
|
||||
if let Some(name) = sentbox_name {
|
||||
name == folder_name.as_ref()
|
||||
} else {
|
||||
@@ -765,7 +617,7 @@ pub fn dc_is_sentbox(context: &Context, folder_name: impl AsRef<str>) -> bool {
|
||||
}
|
||||
|
||||
pub fn dc_is_mvbox(context: &Context, folder_name: impl AsRef<str>) -> bool {
|
||||
let mvbox_name = sql::get_config(context, &context.sql, "configured_mvbox_folder", None);
|
||||
let mvbox_name = context.sql.get_config(context, "configured_mvbox_folder");
|
||||
|
||||
if let Some(name) = mvbox_name {
|
||||
name == folder_name.as_ref()
|
||||
|
||||
@@ -3,6 +3,8 @@ use crate::dc_tools::*;
|
||||
use crate::types::*;
|
||||
use crate::x::*;
|
||||
|
||||
const DC_ARRAY_MAGIC: uint32_t = 0x000a11aa;
|
||||
|
||||
/* * the structure behind dc_array_t */
|
||||
#[derive(Copy, Clone)]
|
||||
#[repr(C)]
|
||||
@@ -23,7 +25,7 @@ pub struct dc_array_t {
|
||||
* To free an array object, use dc_array_unref().
|
||||
*/
|
||||
pub unsafe fn dc_array_unref(mut array: *mut dc_array_t) {
|
||||
if array.is_null() || (*array).magic != 0xa11aai32 as libc::c_uint {
|
||||
if array.is_null() || (*array).magic != DC_ARRAY_MAGIC {
|
||||
return;
|
||||
}
|
||||
if (*array).type_0 == 1i32 {
|
||||
@@ -35,7 +37,7 @@ pub unsafe fn dc_array_unref(mut array: *mut dc_array_t) {
|
||||
}
|
||||
|
||||
pub unsafe fn dc_array_free_ptr(array: *mut dc_array_t) {
|
||||
if array.is_null() || (*array).magic != 0xa11aai32 as libc::c_uint {
|
||||
if array.is_null() || (*array).magic != DC_ARRAY_MAGIC {
|
||||
return;
|
||||
}
|
||||
let mut i: size_t = 0i32 as size_t;
|
||||
@@ -53,7 +55,7 @@ pub unsafe fn dc_array_free_ptr(array: *mut dc_array_t) {
|
||||
}
|
||||
|
||||
pub unsafe fn dc_array_add_uint(mut array: *mut dc_array_t, item: uintptr_t) {
|
||||
if array.is_null() || (*array).magic != 0xa11aai32 as libc::c_uint {
|
||||
if array.is_null() || (*array).magic != DC_ARRAY_MAGIC {
|
||||
return;
|
||||
}
|
||||
if (*array).count == (*array).allocated {
|
||||
@@ -78,21 +80,21 @@ pub unsafe fn dc_array_add_ptr(array: *mut dc_array_t, item: *mut libc::c_void)
|
||||
}
|
||||
|
||||
pub unsafe fn dc_array_get_cnt(array: *const dc_array_t) -> size_t {
|
||||
if array.is_null() || (*array).magic != 0xa11aai32 as libc::c_uint {
|
||||
if array.is_null() || (*array).magic != DC_ARRAY_MAGIC {
|
||||
return 0i32 as size_t;
|
||||
}
|
||||
(*array).count
|
||||
}
|
||||
|
||||
pub unsafe fn dc_array_get_uint(array: *const dc_array_t, index: size_t) -> uintptr_t {
|
||||
if array.is_null() || (*array).magic != 0xa11aai32 as libc::c_uint || index >= (*array).count {
|
||||
if array.is_null() || (*array).magic != DC_ARRAY_MAGIC || index >= (*array).count {
|
||||
return 0i32 as uintptr_t;
|
||||
}
|
||||
*(*array).array.offset(index as isize)
|
||||
}
|
||||
|
||||
pub unsafe fn dc_array_get_id(array: *const dc_array_t, index: size_t) -> uint32_t {
|
||||
if array.is_null() || (*array).magic != 0xa11aai32 as libc::c_uint || index >= (*array).count {
|
||||
if array.is_null() || (*array).magic != DC_ARRAY_MAGIC || index >= (*array).count {
|
||||
return 0i32 as uint32_t;
|
||||
}
|
||||
if (*array).type_0 == 1i32 {
|
||||
@@ -102,7 +104,7 @@ pub unsafe fn dc_array_get_id(array: *const dc_array_t, index: size_t) -> uint32
|
||||
}
|
||||
|
||||
pub unsafe fn dc_array_get_ptr(array: *const dc_array_t, index: size_t) -> *mut libc::c_void {
|
||||
if array.is_null() || (*array).magic != 0xa11aai32 as libc::c_uint || index >= (*array).count {
|
||||
if array.is_null() || (*array).magic != DC_ARRAY_MAGIC || index >= (*array).count {
|
||||
return 0 as *mut libc::c_void;
|
||||
}
|
||||
*(*array).array.offset(index as isize) as *mut libc::c_void
|
||||
@@ -110,7 +112,7 @@ pub unsafe fn dc_array_get_ptr(array: *const dc_array_t, index: size_t) -> *mut
|
||||
|
||||
pub unsafe fn dc_array_get_latitude(array: *const dc_array_t, index: size_t) -> libc::c_double {
|
||||
if array.is_null()
|
||||
|| (*array).magic != 0xa11aai32 as libc::c_uint
|
||||
|| (*array).magic != DC_ARRAY_MAGIC
|
||||
|| index >= (*array).count
|
||||
|| (*array).type_0 != 1i32
|
||||
|| *(*array).array.offset(index as isize) == 0
|
||||
@@ -122,7 +124,7 @@ pub unsafe fn dc_array_get_latitude(array: *const dc_array_t, index: size_t) ->
|
||||
|
||||
pub unsafe fn dc_array_get_longitude(array: *const dc_array_t, index: size_t) -> libc::c_double {
|
||||
if array.is_null()
|
||||
|| (*array).magic != 0xa11aai32 as libc::c_uint
|
||||
|| (*array).magic != DC_ARRAY_MAGIC
|
||||
|| index >= (*array).count
|
||||
|| (*array).type_0 != 1i32
|
||||
|| *(*array).array.offset(index as isize) == 0
|
||||
@@ -134,7 +136,7 @@ pub unsafe fn dc_array_get_longitude(array: *const dc_array_t, index: size_t) ->
|
||||
|
||||
pub unsafe fn dc_array_get_accuracy(array: *const dc_array_t, index: size_t) -> libc::c_double {
|
||||
if array.is_null()
|
||||
|| (*array).magic != 0xa11aai32 as libc::c_uint
|
||||
|| (*array).magic != DC_ARRAY_MAGIC
|
||||
|| index >= (*array).count
|
||||
|| (*array).type_0 != 1i32
|
||||
|| *(*array).array.offset(index as isize) == 0
|
||||
@@ -146,7 +148,7 @@ pub unsafe fn dc_array_get_accuracy(array: *const dc_array_t, index: size_t) ->
|
||||
|
||||
pub unsafe fn dc_array_get_timestamp(array: *const dc_array_t, index: size_t) -> i64 {
|
||||
if array.is_null()
|
||||
|| (*array).magic != 0xa11aai32 as libc::c_uint
|
||||
|| (*array).magic != DC_ARRAY_MAGIC
|
||||
|| index >= (*array).count
|
||||
|| (*array).type_0 != 1i32
|
||||
|| *(*array).array.offset(index as isize) == 0
|
||||
@@ -158,7 +160,7 @@ pub unsafe fn dc_array_get_timestamp(array: *const dc_array_t, index: size_t) ->
|
||||
|
||||
pub unsafe fn dc_array_get_chat_id(array: *const dc_array_t, index: size_t) -> uint32_t {
|
||||
if array.is_null()
|
||||
|| (*array).magic != 0xa11aai32 as libc::c_uint
|
||||
|| (*array).magic != DC_ARRAY_MAGIC
|
||||
|| index >= (*array).count
|
||||
|| (*array).type_0 != 1i32
|
||||
|| *(*array).array.offset(index as isize) == 0
|
||||
@@ -170,7 +172,7 @@ pub unsafe fn dc_array_get_chat_id(array: *const dc_array_t, index: size_t) -> u
|
||||
|
||||
pub unsafe fn dc_array_get_contact_id(array: *const dc_array_t, index: size_t) -> uint32_t {
|
||||
if array.is_null()
|
||||
|| (*array).magic != 0xa11aai32 as libc::c_uint
|
||||
|| (*array).magic != DC_ARRAY_MAGIC
|
||||
|| index >= (*array).count
|
||||
|| (*array).type_0 != 1i32
|
||||
|| *(*array).array.offset(index as isize) == 0
|
||||
@@ -182,7 +184,7 @@ pub unsafe fn dc_array_get_contact_id(array: *const dc_array_t, index: size_t) -
|
||||
|
||||
pub unsafe fn dc_array_get_msg_id(array: *const dc_array_t, index: size_t) -> uint32_t {
|
||||
if array.is_null()
|
||||
|| (*array).magic != 0xa11aai32 as libc::c_uint
|
||||
|| (*array).magic != DC_ARRAY_MAGIC
|
||||
|| index >= (*array).count
|
||||
|| (*array).type_0 != 1i32
|
||||
|| *(*array).array.offset(index as isize) == 0
|
||||
@@ -194,7 +196,7 @@ pub unsafe fn dc_array_get_msg_id(array: *const dc_array_t, index: size_t) -> ui
|
||||
|
||||
pub unsafe fn dc_array_get_marker(array: *const dc_array_t, index: size_t) -> *mut libc::c_char {
|
||||
if array.is_null()
|
||||
|| (*array).magic != 0xa11aai32 as libc::c_uint
|
||||
|| (*array).magic != DC_ARRAY_MAGIC
|
||||
|| index >= (*array).count
|
||||
|| (*array).type_0 != 1i32
|
||||
|| *(*array).array.offset(index as isize) == 0
|
||||
@@ -216,7 +218,7 @@ pub unsafe fn dc_array_get_marker(array: *const dc_array_t, index: size_t) -> *m
|
||||
*/
|
||||
pub unsafe fn dc_array_is_independent(array: *const dc_array_t, index: size_t) -> libc::c_int {
|
||||
if array.is_null()
|
||||
|| (*array).magic != 0xa11aai32 as libc::c_uint
|
||||
|| (*array).magic != DC_ARRAY_MAGIC
|
||||
|| index >= (*array).count
|
||||
|| (*array).type_0 != 1i32
|
||||
|| *(*array).array.offset(index as isize) == 0
|
||||
@@ -232,7 +234,7 @@ pub unsafe fn dc_array_search_id(
|
||||
needle: uint32_t,
|
||||
ret_index: *mut size_t,
|
||||
) -> bool {
|
||||
if array.is_null() || (*array).magic != 0xa11aai32 as libc::c_uint {
|
||||
if array.is_null() || (*array).magic != DC_ARRAY_MAGIC {
|
||||
return false;
|
||||
}
|
||||
let data: *mut uintptr_t = (*array).array;
|
||||
@@ -251,7 +253,7 @@ pub unsafe fn dc_array_search_id(
|
||||
}
|
||||
|
||||
pub unsafe fn dc_array_get_raw(array: *const dc_array_t) -> *const uintptr_t {
|
||||
if array.is_null() || (*array).magic != 0xa11aai32 as libc::c_uint {
|
||||
if array.is_null() || (*array).magic != DC_ARRAY_MAGIC {
|
||||
return 0 as *const uintptr_t;
|
||||
}
|
||||
(*array).array
|
||||
@@ -266,7 +268,7 @@ pub unsafe fn dc_array_new_typed(type_0: libc::c_int, initsize: size_t) -> *mut
|
||||
array = calloc(1, ::std::mem::size_of::<dc_array_t>()) as *mut dc_array_t;
|
||||
assert!(!array.is_null());
|
||||
|
||||
(*array).magic = 0xa11aai32 as uint32_t;
|
||||
(*array).magic = DC_ARRAY_MAGIC;
|
||||
(*array).count = 0i32 as size_t;
|
||||
(*array).allocated = if initsize < 1 { 1 } else { initsize };
|
||||
(*array).type_0 = type_0;
|
||||
@@ -282,7 +284,7 @@ pub unsafe fn dc_array_new_typed(type_0: libc::c_int, initsize: size_t) -> *mut
|
||||
}
|
||||
|
||||
pub unsafe fn dc_array_empty(mut array: *mut dc_array_t) {
|
||||
if array.is_null() || (*array).magic != 0xa11aai32 as libc::c_uint {
|
||||
if array.is_null() || (*array).magic != DC_ARRAY_MAGIC {
|
||||
return;
|
||||
}
|
||||
(*array).count = 0i32 as size_t;
|
||||
@@ -290,7 +292,7 @@ pub unsafe fn dc_array_empty(mut array: *mut dc_array_t) {
|
||||
|
||||
pub unsafe fn dc_array_duplicate(array: *const dc_array_t) -> *mut dc_array_t {
|
||||
let mut ret: *mut dc_array_t;
|
||||
if array.is_null() || (*array).magic != 0xa11aai32 as libc::c_uint {
|
||||
if array.is_null() || (*array).magic != DC_ARRAY_MAGIC {
|
||||
return 0 as *mut dc_array_t;
|
||||
}
|
||||
ret = dc_array_new((*array).allocated);
|
||||
@@ -306,7 +308,7 @@ pub unsafe fn dc_array_duplicate(array: *const dc_array_t) -> *mut dc_array_t {
|
||||
}
|
||||
|
||||
pub unsafe fn dc_array_sort_ids(array: *mut dc_array_t) {
|
||||
if array.is_null() || (*array).magic != 0xa11aai32 as libc::c_uint || (*array).count <= 1 {
|
||||
if array.is_null() || (*array).magic != DC_ARRAY_MAGIC || (*array).count <= 1 {
|
||||
return;
|
||||
}
|
||||
qsort(
|
||||
@@ -330,7 +332,7 @@ unsafe extern "C" fn cmp_intptr_t(p1: *const libc::c_void, p2: *const libc::c_vo
|
||||
}
|
||||
|
||||
pub unsafe fn dc_array_sort_strings(array: *mut dc_array_t) {
|
||||
if array.is_null() || (*array).magic != 0xa11aai32 as libc::c_uint || (*array).count <= 1 {
|
||||
if array.is_null() || (*array).magic != DC_ARRAY_MAGIC || (*array).count <= 1 {
|
||||
return;
|
||||
}
|
||||
qsort(
|
||||
@@ -355,7 +357,7 @@ pub unsafe fn dc_array_get_string(
|
||||
array: *const dc_array_t,
|
||||
sep: *const libc::c_char,
|
||||
) -> *mut libc::c_char {
|
||||
if array.is_null() || (*array).magic != 0xa11aai32 as libc::c_uint || sep.is_null() {
|
||||
if array.is_null() || (*array).magic != DC_ARRAY_MAGIC || sep.is_null() {
|
||||
return dc_strdup(b"\x00" as *const u8 as *const libc::c_char);
|
||||
}
|
||||
let cnt = (*array).count as usize;
|
||||
@@ -374,7 +376,7 @@ pub unsafe fn dc_array_get_string(
|
||||
}
|
||||
res
|
||||
});
|
||||
strdup(to_cstring(res).as_ptr())
|
||||
to_cstring(res)
|
||||
}
|
||||
|
||||
/// return comma-separated value-string from integer array
|
||||
@@ -396,7 +398,7 @@ pub unsafe fn dc_arr_to_string(arr: *const uint32_t, cnt: libc::c_int) -> *mut l
|
||||
res
|
||||
},
|
||||
);
|
||||
strdup(to_cstring(res).as_ptr())
|
||||
to_cstring(res)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -410,22 +412,17 @@ mod tests {
|
||||
let arr = dc_array_new(7 as size_t);
|
||||
assert_eq!(dc_array_get_cnt(arr), 0);
|
||||
|
||||
let mut i: libc::c_int = 0;
|
||||
while i < 1000 {
|
||||
for i in 0..1000 {
|
||||
dc_array_add_id(arr, (i + 2) as uint32_t);
|
||||
i += 1
|
||||
}
|
||||
|
||||
assert_eq!(dc_array_get_cnt(arr), 1000);
|
||||
|
||||
i = 0;
|
||||
|
||||
while i < 1000i32 {
|
||||
for i in 0..1000 {
|
||||
assert_eq!(
|
||||
dc_array_get_id(arr, i as size_t),
|
||||
(i + 1i32 * 2i32) as libc::c_uint
|
||||
);
|
||||
i += 1
|
||||
}
|
||||
|
||||
assert_eq!(dc_array_get_id(arr, -1i32 as size_t), 0);
|
||||
|
||||
587
src/dc_chat.rs
587
src/dc_chat.rs
@@ -107,77 +107,94 @@ pub fn dc_block_chat(context: &Context, chat_id: u32, new_blocking: libc::c_int)
|
||||
"UPDATE chats SET blocked=? WHERE id=?;",
|
||||
params![new_blocking, chat_id as i32],
|
||||
)
|
||||
.is_ok()
|
||||
}
|
||||
|
||||
pub fn dc_chat_load_from_db(chat: *mut Chat, chat_id: u32) -> bool {
|
||||
if chat.is_null() || unsafe { (*chat).magic != 0xc4a7c4a7u32 } {
|
||||
return false;
|
||||
}
|
||||
|
||||
unsafe { dc_chat_empty(chat) };
|
||||
|
||||
let context = unsafe { (*chat).context };
|
||||
|
||||
context
|
||||
.sql
|
||||
.query_row(
|
||||
"SELECT c.id,c.type,c.name, c.grpid,c.param,c.archived, \
|
||||
c.blocked, c.gossiped_timestamp, c.locations_send_until \
|
||||
FROM chats c WHERE c.id=?;",
|
||||
params![chat_id as i32],
|
||||
|row| {
|
||||
let c = unsafe { &mut *chat };
|
||||
let res = context.sql.query_row(
|
||||
"SELECT c.id,c.type,c.name, c.grpid,c.param,c.archived, \
|
||||
c.blocked, c.gossiped_timestamp, c.locations_send_until \
|
||||
FROM chats c WHERE c.id=?;",
|
||||
params![chat_id as i32],
|
||||
|row| {
|
||||
let c = unsafe { &mut *chat };
|
||||
|
||||
c.id = row.get(0)?;
|
||||
c.type_0 = row.get(1)?;
|
||||
c.name = {
|
||||
let raw: String = row.get(2)?;
|
||||
unsafe { strdup(to_cstring(raw).as_ptr()) }
|
||||
};
|
||||
c.grpid = {
|
||||
let raw: String = row.get(3)?;
|
||||
unsafe { strdup(to_cstring(raw).as_ptr()) }
|
||||
};
|
||||
c.id = row.get(0)?;
|
||||
c.type_0 = row.get(1)?;
|
||||
c.name = {
|
||||
let raw: String = row.get(2)?;
|
||||
unsafe { to_cstring(raw) }
|
||||
};
|
||||
c.grpid = {
|
||||
let raw: String = row.get(3)?;
|
||||
unsafe { to_cstring(raw) }
|
||||
};
|
||||
|
||||
let packed: String = row.get(4)?;
|
||||
unsafe { dc_param_set_packed((*chat).param, to_cstring(&packed).as_ptr()) };
|
||||
c.archived = row.get(5)?;
|
||||
c.blocked = row.get(6)?;
|
||||
c.gossiped_timestamp = row.get(7)?;
|
||||
c.is_sending_locations = row.get(8)?;
|
||||
let packed: String = row.get(4)?;
|
||||
unsafe {
|
||||
let p = to_cstring(&packed);
|
||||
dc_param_set_packed((*chat).param, p);
|
||||
free(p as *mut _);
|
||||
};
|
||||
c.archived = row.get(5)?;
|
||||
c.blocked = row.get::<_, Option<i32>>(6)?.unwrap_or_default();
|
||||
c.gossiped_timestamp = row.get(7)?;
|
||||
c.is_sending_locations = row.get(8)?;
|
||||
Ok(())
|
||||
},
|
||||
);
|
||||
|
||||
match c.id {
|
||||
1 => unsafe {
|
||||
free((*chat).name as *mut libc::c_void);
|
||||
(*chat).name = dc_stock_str((*chat).context, 8);
|
||||
},
|
||||
6 => unsafe {
|
||||
free((*chat).name as *mut libc::c_void);
|
||||
let tempname: *mut libc::c_char = dc_stock_str((*chat).context, 40);
|
||||
(*chat).name = dc_mprintf(
|
||||
b"%s (%i)\x00" as *const u8 as *const libc::c_char,
|
||||
tempname,
|
||||
dc_get_archived_cnt((*chat).context),
|
||||
);
|
||||
free(tempname as *mut libc::c_void);
|
||||
},
|
||||
5 => unsafe {
|
||||
free((*chat).name as *mut libc::c_void);
|
||||
(*chat).name = dc_stock_str((*chat).context, 41);
|
||||
},
|
||||
_ => {
|
||||
if 0 != unsafe { dc_param_exists((*chat).param, 'K' as i32) } {
|
||||
unsafe {
|
||||
free((*chat).name as *mut libc::c_void);
|
||||
(*chat).name = dc_stock_str((*chat).context, 2);
|
||||
}
|
||||
match res {
|
||||
Ok(_) => {
|
||||
let c = unsafe { &mut *chat };
|
||||
match c.id {
|
||||
1 => unsafe {
|
||||
free((*chat).name as *mut libc::c_void);
|
||||
(*chat).name = dc_stock_str((*chat).context, 8);
|
||||
},
|
||||
6 => unsafe {
|
||||
free((*chat).name as *mut libc::c_void);
|
||||
let tempname: *mut libc::c_char = dc_stock_str((*chat).context, 40);
|
||||
(*chat).name = dc_mprintf(
|
||||
b"%s (%i)\x00" as *const u8 as *const libc::c_char,
|
||||
tempname,
|
||||
dc_get_archived_cnt((*chat).context),
|
||||
);
|
||||
free(tempname as *mut libc::c_void);
|
||||
},
|
||||
5 => unsafe {
|
||||
free((*chat).name as *mut libc::c_void);
|
||||
(*chat).name = dc_stock_str((*chat).context, 41);
|
||||
},
|
||||
_ => {
|
||||
if 0 != unsafe { dc_param_exists((*chat).param, 'K' as i32) } {
|
||||
unsafe {
|
||||
free((*chat).name as *mut libc::c_void);
|
||||
(*chat).name = dc_stock_str((*chat).context, 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
},
|
||||
)
|
||||
.is_ok()
|
||||
}
|
||||
true
|
||||
}
|
||||
Err(err) => match err {
|
||||
QueryReturnedNoRows => false,
|
||||
_ => {
|
||||
error!(
|
||||
context,
|
||||
0, "chat: failed to load from db {}: {:?}", chat_id, err
|
||||
);
|
||||
false
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub unsafe fn dc_create_chat_by_contact_id(context: &Context, contact_id: uint32_t) -> uint32_t {
|
||||
@@ -260,15 +277,16 @@ pub unsafe fn dc_create_or_lookup_nchat_by_contact_id(
|
||||
if sql::execute(
|
||||
context,
|
||||
&context.sql,
|
||||
"INSERT INTO chats (type, name, param, blocked, grpid) VALUES(?, ?, ?, ?, ?)",
|
||||
params![
|
||||
format!(
|
||||
"INSERT INTO chats (type, name, param, blocked, grpid) VALUES({}, '{}', '{}', {}, '{}')",
|
||||
100,
|
||||
as_str(chat_name),
|
||||
if contact_id == 1 { "K=1" } else { "" },
|
||||
create_blocked,
|
||||
as_str((*contact).addr),
|
||||
],
|
||||
) {
|
||||
),
|
||||
params![],
|
||||
).is_ok() {
|
||||
chat_id = sql::get_rowid(
|
||||
context,
|
||||
&context.sql,
|
||||
@@ -280,8 +298,8 @@ pub unsafe fn dc_create_or_lookup_nchat_by_contact_id(
|
||||
sql::execute(
|
||||
context,
|
||||
&context.sql,
|
||||
"INSERT INTO chats_contacts (chat_id, contact_id) VALUES(?, ?)",
|
||||
params![chat_id, contact_id],
|
||||
format!("INSERT INTO chats_contacts (chat_id, contact_id) VALUES({}, {})", chat_id, contact_id),
|
||||
params![],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -315,7 +333,7 @@ pub fn dc_lookup_real_nchat_by_contact_id(
|
||||
if let Ok((id, blocked)) = context.sql.query_row(
|
||||
"SELECT c.id, c.blocked FROM chats c INNER JOIN chats_contacts j ON c.id=j.chat_id WHERE c.type=100 AND c.id>9 AND j.contact_id=?;",
|
||||
params![contact_id as i32],
|
||||
|row| Ok((row.get(0)?, row.get(1)?)),
|
||||
|row| Ok((row.get(0)?, row.get::<_, Option<i32>>(1)?.unwrap_or_default())),
|
||||
) {
|
||||
unsafe { *ret_chat_id = id };
|
||||
unsafe { *ret_chat_blocked = blocked };
|
||||
@@ -476,7 +494,7 @@ unsafe fn prepare_msg_raw(
|
||||
"Cannot send message; self not in group.",
|
||||
);
|
||||
} else {
|
||||
let from = sql::get_config(context, &context.sql, "configured_addr", None);
|
||||
let from = context.sql.get_config(context, "configured_addr");
|
||||
if from.is_none() {
|
||||
error!(context, 0, "Cannot send message, not configured.",);
|
||||
} else {
|
||||
@@ -487,13 +505,13 @@ unsafe fn prepare_msg_raw(
|
||||
} else {
|
||||
0 as *mut libc::c_char
|
||||
},
|
||||
from_c.as_ptr(),
|
||||
from_c,
|
||||
);
|
||||
free(from_c as *mut _);
|
||||
|
||||
if (*chat).type_0 == 100 {
|
||||
if let Some(id) = sql::query_row(
|
||||
if let Some(id) = context.sql.query_row_col(
|
||||
context,
|
||||
&context.sql,
|
||||
"SELECT contact_id FROM chats_contacts WHERE chat_id=?;",
|
||||
params![(*chat).id as i32],
|
||||
0,
|
||||
@@ -526,18 +544,22 @@ unsafe fn prepare_msg_raw(
|
||||
so that E2EE is no longer available at a later point (reset, changed settings),
|
||||
we do not send the message out at all */
|
||||
do_guarantee_e2ee = 0;
|
||||
e2ee_enabled = sql::get_config_int(context, &context.sql, "e2ee_enabled", 1);
|
||||
e2ee_enabled = context
|
||||
.sql
|
||||
.get_config_int(context, "e2ee_enabled")
|
||||
.unwrap_or_else(|| 1);
|
||||
if 0 != e2ee_enabled && dc_param_get_int((*msg).param, 'u' as i32, 0) == 0 {
|
||||
let mut can_encrypt: libc::c_int = 1;
|
||||
let mut all_mutual: libc::c_int = 1;
|
||||
let mut can_encrypt = 1;
|
||||
let mut all_mutual = 1;
|
||||
|
||||
context.sql.query_row(
|
||||
let res = context.sql.query_row(
|
||||
"SELECT ps.prefer_encrypted, c.addr \
|
||||
FROM chats_contacts cc \
|
||||
LEFT JOIN contacts c ON cc.contact_id=c.id \
|
||||
LEFT JOIN acpeerstates ps ON c.addr=ps.addr \
|
||||
WHERE cc.chat_id=? AND cc.contact_id>9;",
|
||||
params![(*chat).id], |row| {
|
||||
params![(*chat).id],
|
||||
|row| {
|
||||
let state: String = row.get(1)?;
|
||||
|
||||
if let Some(prefer_encrypted) = row.get::<_, Option<i32>>(0)? {
|
||||
@@ -561,8 +583,14 @@ unsafe fn prepare_msg_raw(
|
||||
all_mutual = 0;
|
||||
}
|
||||
Ok(())
|
||||
},
|
||||
);
|
||||
match res {
|
||||
Ok(_) => {}
|
||||
Err(err) => {
|
||||
warn!(context, 0, "chat: failed to load peerstates: {:?}", err);
|
||||
}
|
||||
).unwrap();
|
||||
}
|
||||
|
||||
if 0 != can_encrypt {
|
||||
if 0 != all_mutual {
|
||||
@@ -653,7 +681,9 @@ unsafe fn prepare_msg_raw(
|
||||
0.0,
|
||||
),
|
||||
],
|
||||
) {
|
||||
)
|
||||
.is_ok()
|
||||
{
|
||||
location_id = sql::get_rowid2(
|
||||
context,
|
||||
&context.sql,
|
||||
@@ -687,7 +717,7 @@ unsafe fn prepare_msg_raw(
|
||||
to_string(new_references),
|
||||
location_id as i32,
|
||||
]
|
||||
) {
|
||||
).is_ok() {
|
||||
msg_id = sql::get_rowid(
|
||||
context,
|
||||
&context.sql,
|
||||
@@ -741,9 +771,9 @@ unsafe fn get_parent_mime_headers(
|
||||
FROM msgs WHERE chat_id=? AND from_id!=?);",
|
||||
params![(*chat).id as i32, 1],
|
||||
|row| {
|
||||
*parent_rfc724_mid = dc_strdup(to_cstring(row.get::<_, String>(0)?).as_ptr());
|
||||
*parent_in_reply_to = dc_strdup(to_cstring(row.get::<_, String>(1)?).as_ptr());
|
||||
*parent_references = dc_strdup(to_cstring(row.get::<_, String>(2)?).as_ptr());
|
||||
*parent_rfc724_mid = to_cstring(row.get::<_, String>(0)?);
|
||||
*parent_in_reply_to = to_cstring(row.get::<_, String>(1)?);
|
||||
*parent_references = to_cstring(row.get::<_, String>(2)?);
|
||||
Ok(())
|
||||
},
|
||||
)
|
||||
@@ -759,12 +789,9 @@ unsafe fn get_parent_mime_headers(
|
||||
FROM msgs WHERE chat_id=? AND from_id==?);",
|
||||
params![(*chat).id as i32, 1],
|
||||
|row| {
|
||||
*parent_rfc724_mid =
|
||||
dc_strdup(to_cstring(row.get::<_, String>(0)?).as_ptr());
|
||||
*parent_in_reply_to =
|
||||
dc_strdup(to_cstring(row.get::<_, String>(1)?).as_ptr());
|
||||
*parent_references =
|
||||
dc_strdup(to_cstring(row.get::<_, String>(2)?).as_ptr());
|
||||
*parent_rfc724_mid = to_cstring(row.get::<_, String>(0)?);
|
||||
*parent_in_reply_to = to_cstring(row.get::<_, String>(1)?);
|
||||
*parent_references = to_cstring(row.get::<_, String>(2)?);
|
||||
Ok(())
|
||||
},
|
||||
)
|
||||
@@ -791,9 +818,8 @@ unsafe fn last_msg_in_chat_encrypted(
|
||||
chat_id: uint32_t,
|
||||
) -> libc::c_int {
|
||||
let mut last_is_encrypted: libc::c_int = 0i32;
|
||||
let packed: Option<String> = sql::query_row(
|
||||
let packed: Option<String> = sql.query_row_col(
|
||||
context,
|
||||
sql,
|
||||
"SELECT param \
|
||||
FROM msgs WHERE timestamp=(SELECT MAX(timestamp) FROM msgs WHERE chat_id=?) \
|
||||
ORDER BY id DESC;",
|
||||
@@ -804,7 +830,8 @@ unsafe fn last_msg_in_chat_encrypted(
|
||||
if let Some(packed) = packed {
|
||||
let msg_param = dc_param_new();
|
||||
let packed_c = to_cstring(packed);
|
||||
dc_param_set_packed(msg_param, packed_c.as_ptr());
|
||||
dc_param_set_packed(msg_param, packed_c);
|
||||
free(packed_c as *mut _);
|
||||
|
||||
if 0 != dc_param_exists(msg_param, 'c' as i32) {
|
||||
last_is_encrypted = 1;
|
||||
@@ -822,7 +849,8 @@ pub unsafe fn dc_chat_update_param(chat: *mut Chat) -> libc::c_int {
|
||||
&(*chat).context.sql,
|
||||
"UPDATE chats SET param=? WHERE id=?",
|
||||
params![to_string((*(*chat).param).packed), (*chat).id as i32],
|
||||
) as libc::c_int
|
||||
)
|
||||
.is_ok() as libc::c_int
|
||||
}
|
||||
|
||||
pub unsafe fn dc_is_contact_in_chat(
|
||||
@@ -857,20 +885,20 @@ pub unsafe fn dc_send_msg<'a>(
|
||||
msg: *mut dc_msg_t<'a>,
|
||||
) -> uint32_t {
|
||||
if msg.is_null() {
|
||||
return 0i32 as uint32_t;
|
||||
return 0;
|
||||
}
|
||||
if (*msg).state != 18i32 {
|
||||
if (*msg).state != 18 {
|
||||
if 0 == prepare_msg_common(context, chat_id, msg) {
|
||||
return 0i32 as uint32_t;
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
if chat_id != 0i32 as libc::c_uint && chat_id != (*msg).chat_id {
|
||||
return 0i32 as uint32_t;
|
||||
if chat_id != 0 && chat_id != (*msg).chat_id {
|
||||
return 0;
|
||||
}
|
||||
dc_update_msg_state(context, (*msg).id, 20i32);
|
||||
dc_update_msg_state(context, (*msg).id, 20);
|
||||
}
|
||||
if 0 == dc_job_send_msg(context, (*msg).id) {
|
||||
return 0i32 as uint32_t;
|
||||
return 0;
|
||||
}
|
||||
context.call_cb(
|
||||
Event::MSGS_CHANGED,
|
||||
@@ -883,19 +911,18 @@ pub unsafe fn dc_send_msg<'a>(
|
||||
}
|
||||
|
||||
if 0 == chat_id {
|
||||
let forwards: *mut libc::c_char =
|
||||
dc_param_get((*msg).param, 'P' as i32, 0 as *const libc::c_char);
|
||||
let forwards = dc_param_get((*msg).param, 'P' as i32, 0 as *const libc::c_char);
|
||||
if !forwards.is_null() {
|
||||
let mut p: *mut libc::c_char = forwards;
|
||||
let mut p = forwards;
|
||||
while 0 != *p {
|
||||
let id: int32_t = strtol(p, &mut p, 10i32) as int32_t;
|
||||
let id = strtol(p, &mut p, 10) as int32_t;
|
||||
if 0 == id {
|
||||
// avoid hanging if user tampers with db
|
||||
break;
|
||||
} else {
|
||||
let copy: *mut dc_msg_t = dc_get_msg(context, id as uint32_t);
|
||||
let copy = dc_get_msg(context, id as uint32_t);
|
||||
if !copy.is_null() {
|
||||
dc_send_msg(context, 0i32 as uint32_t, copy);
|
||||
dc_send_msg(context, 0 as uint32_t, copy);
|
||||
}
|
||||
dc_msg_unref(copy);
|
||||
}
|
||||
@@ -914,11 +941,11 @@ pub unsafe fn dc_send_text_msg(
|
||||
chat_id: uint32_t,
|
||||
text_to_send: *const libc::c_char,
|
||||
) -> uint32_t {
|
||||
let mut msg: *mut dc_msg_t = dc_msg_new(context, 10i32);
|
||||
let mut ret: uint32_t = 0i32 as uint32_t;
|
||||
if !(chat_id <= 9i32 as libc::c_uint || text_to_send.is_null()) {
|
||||
let mut msg = dc_msg_new(context, 10);
|
||||
let mut ret = 0;
|
||||
if !(chat_id <= 9 || text_to_send.is_null()) {
|
||||
(*msg).text = dc_strdup(text_to_send);
|
||||
ret = dc_send_msg(context, chat_id, msg)
|
||||
ret = dc_send_msg(context, chat_id, msg);
|
||||
}
|
||||
dc_msg_unref(msg);
|
||||
ret
|
||||
@@ -994,7 +1021,7 @@ unsafe fn set_draft_raw(context: &Context, chat_id: uint32_t, msg: *mut dc_msg_t
|
||||
to_string((*(*msg).param).packed),
|
||||
1,
|
||||
]
|
||||
) {
|
||||
).is_ok() {
|
||||
sth_changed = 1;
|
||||
}
|
||||
}
|
||||
@@ -1005,14 +1032,15 @@ unsafe fn set_draft_raw(context: &Context, chat_id: uint32_t, msg: *mut dc_msg_t
|
||||
}
|
||||
|
||||
fn get_draft_msg_id(context: &Context, chat_id: u32) -> u32 {
|
||||
let draft_msg_id: i32 = sql::query_row(
|
||||
context,
|
||||
&context.sql,
|
||||
"SELECT id FROM msgs WHERE chat_id=? AND state=?;",
|
||||
params![chat_id as i32, 19],
|
||||
0,
|
||||
)
|
||||
.unwrap_or_default();
|
||||
let draft_msg_id: i32 = context
|
||||
.sql
|
||||
.query_row_col(
|
||||
context,
|
||||
"SELECT id FROM msgs WHERE chat_id=? AND state=?;",
|
||||
params![chat_id as i32, 19],
|
||||
0,
|
||||
)
|
||||
.unwrap_or_default();
|
||||
|
||||
draft_msg_id as u32
|
||||
}
|
||||
@@ -1069,7 +1097,10 @@ pub unsafe fn dc_get_chat_msgs(
|
||||
};
|
||||
|
||||
let success = if chat_id == 1 {
|
||||
let show_emails = sql::get_config_int(context, &context.sql, "show_emails", 0);
|
||||
let show_emails = context
|
||||
.sql
|
||||
.get_config_int(context, "show_emails")
|
||||
.unwrap_or_default();
|
||||
context.sql.query_map(
|
||||
"SELECT m.id, m.timestamp FROM msgs m \
|
||||
LEFT JOIN chats ON m.chat_id=chats.id \
|
||||
@@ -1118,28 +1149,30 @@ pub unsafe fn dc_get_chat_msgs(
|
||||
}
|
||||
|
||||
pub fn dc_get_msg_cnt(context: &Context, chat_id: u32) -> libc::c_int {
|
||||
sql::query_row(
|
||||
context,
|
||||
&context.sql,
|
||||
"SELECT COUNT(*) FROM msgs WHERE chat_id=?;",
|
||||
params![chat_id as i32],
|
||||
0,
|
||||
)
|
||||
.unwrap_or_default()
|
||||
context
|
||||
.sql
|
||||
.query_row_col(
|
||||
context,
|
||||
"SELECT COUNT(*) FROM msgs WHERE chat_id=?;",
|
||||
params![chat_id as i32],
|
||||
0,
|
||||
)
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
pub fn dc_get_fresh_msg_cnt(context: &Context, chat_id: u32) -> libc::c_int {
|
||||
sql::query_row(
|
||||
context,
|
||||
&context.sql,
|
||||
"SELECT COUNT(*) FROM msgs \
|
||||
WHERE state=10 \
|
||||
AND hidden=0 \
|
||||
AND chat_id=?;",
|
||||
params![chat_id as i32],
|
||||
0,
|
||||
)
|
||||
.unwrap_or_default()
|
||||
context
|
||||
.sql
|
||||
.query_row_col(
|
||||
context,
|
||||
"SELECT COUNT(*) FROM msgs \
|
||||
WHERE state=10 \
|
||||
AND hidden=0 \
|
||||
AND chat_id=?;",
|
||||
params![chat_id as i32],
|
||||
0,
|
||||
)
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
pub fn dc_marknoticed_chat(context: &Context, chat_id: u32) -> bool {
|
||||
@@ -1153,13 +1186,15 @@ pub fn dc_marknoticed_chat(context: &Context, chat_id: u32) -> bool {
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if !sql::execute(
|
||||
if sql::execute(
|
||||
context,
|
||||
&context.sql,
|
||||
"UPDATE msgs \
|
||||
SET state=13 WHERE chat_id=? AND state=10;",
|
||||
params![chat_id as i32],
|
||||
) {
|
||||
)
|
||||
.is_err()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
context.call_cb(Event::MSGS_CHANGED, 0 as uintptr_t, 0 as uintptr_t);
|
||||
@@ -1179,13 +1214,15 @@ pub fn dc_marknoticed_all_chats(context: &Context) -> bool {
|
||||
return false;
|
||||
}
|
||||
|
||||
if !sql::execute(
|
||||
if sql::execute(
|
||||
context,
|
||||
&context.sql,
|
||||
"UPDATE msgs \
|
||||
SET state=13 WHERE state=10;",
|
||||
params![],
|
||||
) {
|
||||
)
|
||||
.is_err()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1285,21 +1322,25 @@ pub fn dc_archive_chat(context: &Context, chat_id: u32, archive: libc::c_int) ->
|
||||
return true;
|
||||
}
|
||||
if 0 != archive {
|
||||
if !sql::execute(
|
||||
if sql::execute(
|
||||
context,
|
||||
&context.sql,
|
||||
"UPDATE msgs SET state=13 WHERE chat_id=? AND state=10;",
|
||||
params![chat_id as i32],
|
||||
) {
|
||||
)
|
||||
.is_err()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if !sql::execute(
|
||||
if sql::execute(
|
||||
context,
|
||||
&context.sql,
|
||||
"UPDATE chats SET archived=? WHERE id=?;",
|
||||
params![archive, chat_id as i32],
|
||||
) {
|
||||
)
|
||||
.is_err()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
context.call_cb(Event::MSGS_CHANGED, 0 as uintptr_t, 0 as uintptr_t);
|
||||
@@ -1307,54 +1348,64 @@ pub fn dc_archive_chat(context: &Context, chat_id: u32, archive: libc::c_int) ->
|
||||
true
|
||||
}
|
||||
|
||||
pub fn dc_delete_chat(context: &Context, chat_id: u32) {
|
||||
pub fn dc_delete_chat(context: &Context, chat_id: u32) -> bool {
|
||||
/* Up to 2017-11-02 deleting a group also implied leaving it, see above why we have changed this. */
|
||||
if chat_id > 9 {
|
||||
return;
|
||||
if chat_id <= 9 {
|
||||
return false;
|
||||
}
|
||||
let obj = unsafe { dc_chat_new(context) };
|
||||
if !dc_chat_load_from_db(obj, chat_id) {
|
||||
unsafe { dc_chat_unref(obj) };
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
unsafe { dc_chat_unref(obj) };
|
||||
|
||||
if !sql::execute(
|
||||
if sql::execute(
|
||||
context,
|
||||
&context.sql,
|
||||
"DELETE FROM msgs_mdns WHERE msg_id IN (SELECT id FROM msgs WHERE chat_id=?);",
|
||||
params![chat_id as i32],
|
||||
) {
|
||||
return;
|
||||
)
|
||||
.is_err()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if !sql::execute(
|
||||
if sql::execute(
|
||||
context,
|
||||
&context.sql,
|
||||
"DELETE FROM msgs WHERE chat_id=?;",
|
||||
params![chat_id as i32],
|
||||
) {
|
||||
return;
|
||||
)
|
||||
.is_err()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if !sql::execute(
|
||||
if sql::execute(
|
||||
context,
|
||||
&context.sql,
|
||||
"DELETE FROM chats_contacts WHERE chat_id=?;",
|
||||
params![chat_id as i32],
|
||||
) {
|
||||
return;
|
||||
)
|
||||
.is_err()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if !sql::execute(
|
||||
if sql::execute(
|
||||
context,
|
||||
&context.sql,
|
||||
"DELETE FROM chats WHERE id=?;",
|
||||
params![chat_id as i32],
|
||||
) {
|
||||
return;
|
||||
)
|
||||
.is_err()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
context.call_cb(Event::MSGS_CHANGED, 0 as uintptr_t, 0 as uintptr_t);
|
||||
|
||||
dc_job_kill_action(context, 105);
|
||||
unsafe { dc_job_add(context, 105, 0, 0 as *const libc::c_char, 10) };
|
||||
|
||||
true
|
||||
}
|
||||
|
||||
pub fn dc_get_chat_contacts(context: &Context, chat_id: u32) -> *mut dc_array_t {
|
||||
@@ -1429,7 +1480,9 @@ pub unsafe fn dc_create_group_chat(
|
||||
as_str(chat_name),
|
||||
grpid
|
||||
],
|
||||
) {
|
||||
)
|
||||
.is_ok()
|
||||
{
|
||||
chat_id = sql::get_rowid(context, &context.sql, "chats", "grpid", grpid);
|
||||
if chat_id != 0 {
|
||||
if 0 != dc_add_to_chat_contacts_table(context, chat_id, 1) {
|
||||
@@ -1465,7 +1518,8 @@ pub fn dc_add_to_chat_contacts_table(
|
||||
&context.sql,
|
||||
"INSERT INTO chats_contacts (chat_id, contact_id) VALUES(?, ?)",
|
||||
params![chat_id as i32, contact_id as i32],
|
||||
) as libc::c_int
|
||||
)
|
||||
.is_ok() as libc::c_int
|
||||
}
|
||||
|
||||
pub unsafe fn dc_add_contact_to_chat(
|
||||
@@ -1504,12 +1558,14 @@ pub unsafe fn dc_add_contact_to_chat_ex(
|
||||
"Cannot add contact to group; self not in group.",
|
||||
);
|
||||
} else {
|
||||
/* we shoud respect this - whatever we send to the group, it gets discarded anyway! */
|
||||
/* we should respect this - whatever we send to the group, it gets discarded anyway! */
|
||||
if 0 != flags & 0x1 && dc_param_get_int((*chat).param, 'U' as i32, 0) == 1 {
|
||||
dc_param_set((*chat).param, 'U' as i32, 0 as *const libc::c_char);
|
||||
dc_chat_update_param(chat);
|
||||
}
|
||||
let self_addr = sql::get_config(context, &context.sql, "configured_addr", Some(""))
|
||||
let self_addr = context
|
||||
.sql
|
||||
.get_config(context, "configured_addr")
|
||||
.unwrap_or_default();
|
||||
if as_str((*contact).addr) != &self_addr {
|
||||
// ourself is added using DC_CONTACT_ID_SELF, do not add it explicitly.
|
||||
@@ -1662,7 +1718,7 @@ pub unsafe fn dc_remove_contact_from_chat(
|
||||
"Cannot remove contact from chat; self not in group.",
|
||||
);
|
||||
} else {
|
||||
/* we shoud respect this - whatever we send to the group, it gets discarded anyway! */
|
||||
/* we should respect this - whatever we send to the group, it gets discarded anyway! */
|
||||
if !contact.is_null() {
|
||||
if dc_param_get_int((*chat).param, 'U' as i32, 0) == 0 {
|
||||
(*msg).type_0 = 10;
|
||||
@@ -1699,7 +1755,9 @@ pub unsafe fn dc_remove_contact_from_chat(
|
||||
&context.sql,
|
||||
"DELETE FROM chats_contacts WHERE chat_id=? AND contact_id=?;",
|
||||
params![chat_id as i32, contact_id as i32],
|
||||
) {
|
||||
)
|
||||
.is_ok()
|
||||
{
|
||||
context.call_cb(Event::CHAT_MODIFIED, chat_id as uintptr_t, 0 as uintptr_t);
|
||||
success = 1;
|
||||
}
|
||||
@@ -1762,13 +1820,19 @@ pub unsafe fn dc_set_chat_name(
|
||||
"Cannot set chat name; self not in group",
|
||||
);
|
||||
} else {
|
||||
/* we shoud respect this - whatever we send to the group, it gets discarded anyway! */
|
||||
/* we should respect this - whatever we send to the group, it gets discarded anyway! */
|
||||
if sql::execute(
|
||||
context,
|
||||
&context.sql,
|
||||
"UPDATE chats SET name=? WHERE id={};",
|
||||
params![as_str(new_name), chat_id as i32],
|
||||
) {
|
||||
format!(
|
||||
"UPDATE chats SET name='{}' WHERE id={};",
|
||||
as_str(new_name),
|
||||
chat_id as i32
|
||||
),
|
||||
params![],
|
||||
)
|
||||
.is_ok()
|
||||
{
|
||||
if dc_param_get_int((*chat).param, 'U' as i32, 0i32) == 0i32 {
|
||||
(*msg).type_0 = 10i32;
|
||||
(*msg).text = dc_stock_system_msg(
|
||||
@@ -1825,7 +1889,7 @@ pub unsafe fn dc_set_chat_profile_image(
|
||||
"Cannot set chat profile image; self not in group.",
|
||||
);
|
||||
} else {
|
||||
/* we shoud respect this - whatever we send to the group, it gets discarded anyway! */
|
||||
/* we should respect this - whatever we send to the group, it gets discarded anyway! */
|
||||
if !new_image.is_null() {
|
||||
new_image_rel = dc_strdup(new_image);
|
||||
if 0 == dc_make_rel_and_copy(context, &mut new_image_rel) {
|
||||
@@ -1906,70 +1970,69 @@ pub unsafe fn dc_forward_msgs(
|
||||
curr_timestamp = dc_create_smeared_timestamps(context, msg_cnt);
|
||||
idsstr = dc_arr_to_string(msg_ids, msg_cnt);
|
||||
|
||||
context
|
||||
.sql
|
||||
.query_map(
|
||||
"SELECT id FROM msgs WHERE id IN(?) ORDER BY timestamp,id",
|
||||
params![as_str(idsstr)],
|
||||
|row| row.get::<_, i32>(0),
|
||||
|ids| {
|
||||
for id in ids {
|
||||
let src_msg_id = id?;
|
||||
if !dc_msg_load_from_db(msg, context, src_msg_id as u32) {
|
||||
break;
|
||||
}
|
||||
dc_param_set_packed(original_param, (*(*msg).param).packed);
|
||||
if (*msg).from_id != 1i32 as libc::c_uint {
|
||||
dc_param_set_int((*msg).param, 'a' as i32, 1i32);
|
||||
}
|
||||
dc_param_set((*msg).param, 'c' as i32, 0 as *const libc::c_char);
|
||||
dc_param_set((*msg).param, 'u' as i32, 0 as *const libc::c_char);
|
||||
dc_param_set((*msg).param, 'S' as i32, 0 as *const libc::c_char);
|
||||
let new_msg_id: uint32_t;
|
||||
if (*msg).state == 18i32 {
|
||||
let fresh9 = curr_timestamp;
|
||||
curr_timestamp = curr_timestamp + 1;
|
||||
new_msg_id = prepare_msg_raw(context, chat, msg, fresh9);
|
||||
let save_param: *mut dc_param_t = (*msg).param;
|
||||
(*msg).param = original_param;
|
||||
(*msg).id = src_msg_id as uint32_t;
|
||||
let old_fwd: *mut libc::c_char = dc_param_get(
|
||||
(*msg).param,
|
||||
'P' as i32,
|
||||
b"\x00" as *const u8 as *const libc::c_char,
|
||||
);
|
||||
let new_fwd: *mut libc::c_char = dc_mprintf(
|
||||
b"%s %d\x00" as *const u8 as *const libc::c_char,
|
||||
old_fwd,
|
||||
new_msg_id,
|
||||
);
|
||||
dc_param_set((*msg).param, 'P' as i32, new_fwd);
|
||||
dc_msg_save_param_to_disk(msg);
|
||||
free(new_fwd as *mut libc::c_void);
|
||||
free(old_fwd as *mut libc::c_void);
|
||||
(*msg).param = save_param
|
||||
} else {
|
||||
(*msg).state = 20i32;
|
||||
let fresh10 = curr_timestamp;
|
||||
curr_timestamp = curr_timestamp + 1;
|
||||
new_msg_id = prepare_msg_raw(context, chat, msg, fresh10);
|
||||
dc_job_send_msg(context, new_msg_id);
|
||||
}
|
||||
carray_add(
|
||||
created_db_entries,
|
||||
chat_id as uintptr_t as *mut libc::c_void,
|
||||
0 as *mut libc::c_uint,
|
||||
);
|
||||
carray_add(
|
||||
created_db_entries,
|
||||
new_msg_id as uintptr_t as *mut libc::c_void,
|
||||
0 as *mut libc::c_uint,
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
},
|
||||
)
|
||||
.unwrap(); // TODO: better error handling
|
||||
let ids = context.sql.query_map(
|
||||
format!(
|
||||
"SELECT id FROM msgs WHERE id IN({}) ORDER BY timestamp,id",
|
||||
as_str(idsstr)
|
||||
),
|
||||
params![],
|
||||
|row| row.get::<_, i32>(0),
|
||||
|ids| ids.collect::<Result<Vec<_>, _>>().map_err(Into::into),
|
||||
);
|
||||
|
||||
for id in ids.unwrap() {
|
||||
let src_msg_id = id;
|
||||
if !dc_msg_load_from_db(msg, context, src_msg_id as u32) {
|
||||
break;
|
||||
}
|
||||
dc_param_set_packed(original_param, (*(*msg).param).packed);
|
||||
if (*msg).from_id != 1i32 as libc::c_uint {
|
||||
dc_param_set_int((*msg).param, 'a' as i32, 1i32);
|
||||
}
|
||||
dc_param_set((*msg).param, 'c' as i32, 0 as *const libc::c_char);
|
||||
dc_param_set((*msg).param, 'u' as i32, 0 as *const libc::c_char);
|
||||
dc_param_set((*msg).param, 'S' as i32, 0 as *const libc::c_char);
|
||||
let new_msg_id: uint32_t;
|
||||
if (*msg).state == 18i32 {
|
||||
let fresh9 = curr_timestamp;
|
||||
curr_timestamp = curr_timestamp + 1;
|
||||
new_msg_id = prepare_msg_raw(context, chat, msg, fresh9);
|
||||
let save_param: *mut dc_param_t = (*msg).param;
|
||||
(*msg).param = original_param;
|
||||
(*msg).id = src_msg_id as uint32_t;
|
||||
let old_fwd: *mut libc::c_char = dc_param_get(
|
||||
(*msg).param,
|
||||
'P' as i32,
|
||||
b"\x00" as *const u8 as *const libc::c_char,
|
||||
);
|
||||
let new_fwd: *mut libc::c_char = dc_mprintf(
|
||||
b"%s %d\x00" as *const u8 as *const libc::c_char,
|
||||
old_fwd,
|
||||
new_msg_id,
|
||||
);
|
||||
dc_param_set((*msg).param, 'P' as i32, new_fwd);
|
||||
dc_msg_save_param_to_disk(msg);
|
||||
free(new_fwd as *mut libc::c_void);
|
||||
free(old_fwd as *mut libc::c_void);
|
||||
(*msg).param = save_param
|
||||
} else {
|
||||
(*msg).state = 20i32;
|
||||
let fresh10 = curr_timestamp;
|
||||
curr_timestamp = curr_timestamp + 1;
|
||||
new_msg_id = prepare_msg_raw(context, chat, msg, fresh10);
|
||||
dc_job_send_msg(context, new_msg_id);
|
||||
}
|
||||
carray_add(
|
||||
created_db_entries,
|
||||
chat_id as uintptr_t as *mut libc::c_void,
|
||||
0 as *mut libc::c_uint,
|
||||
);
|
||||
carray_add(
|
||||
created_db_entries,
|
||||
new_msg_id as uintptr_t as *mut libc::c_void,
|
||||
0 as *mut libc::c_uint,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if !created_db_entries.is_null() {
|
||||
@@ -2023,17 +2086,19 @@ pub unsafe fn dc_chat_get_subtitle(chat: *const Chat) -> *mut libc::c_char {
|
||||
if (*chat).type_0 == 100 && 0 != dc_param_exists((*chat).param, 'K' as i32) {
|
||||
ret = dc_stock_str((*chat).context, 50)
|
||||
} else if (*chat).type_0 == 100 {
|
||||
let ret_raw: String = sql::query_row(
|
||||
(*chat).context,
|
||||
&(*chat).context.sql,
|
||||
"SELECT c.addr FROM chats_contacts cc \
|
||||
LEFT JOIN contacts c ON c.id=cc.contact_id \
|
||||
WHERE cc.chat_id=?;",
|
||||
params![(*chat).id as i32],
|
||||
0,
|
||||
)
|
||||
.unwrap_or_else(|| "Err".into());
|
||||
ret = dc_strdup(to_cstring(ret_raw).as_ptr());
|
||||
let ret_raw: String = (*chat)
|
||||
.context
|
||||
.sql
|
||||
.query_row_col(
|
||||
(*chat).context,
|
||||
"SELECT c.addr FROM chats_contacts cc \
|
||||
LEFT JOIN contacts c ON c.id=cc.contact_id \
|
||||
WHERE cc.chat_id=?;",
|
||||
params![(*chat).id as i32],
|
||||
0,
|
||||
)
|
||||
.unwrap_or_else(|| "Err".into());
|
||||
ret = to_cstring(ret_raw);
|
||||
} else if (*chat).type_0 == 120 || (*chat).type_0 == 130 {
|
||||
if (*chat).id == 1 {
|
||||
ret = dc_stock_str((*chat).context, 8)
|
||||
@@ -2050,14 +2115,15 @@ pub unsafe fn dc_chat_get_subtitle(chat: *const Chat) -> *mut libc::c_char {
|
||||
}
|
||||
|
||||
pub fn dc_get_chat_contact_cnt(context: &Context, chat_id: u32) -> libc::c_int {
|
||||
sql::query_row(
|
||||
context,
|
||||
&context.sql,
|
||||
"SELECT COUNT(*) FROM chats_contacts WHERE chat_id=?;",
|
||||
params![chat_id as i32],
|
||||
0,
|
||||
)
|
||||
.unwrap_or_default()
|
||||
context
|
||||
.sql
|
||||
.query_row_col(
|
||||
context,
|
||||
"SELECT COUNT(*) FROM chats_contacts WHERE chat_id=?;",
|
||||
params![chat_id as i32],
|
||||
0,
|
||||
)
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
pub unsafe fn dc_chat_get_profile_image(chat: *const Chat) -> *mut libc::c_char {
|
||||
@@ -2148,14 +2214,15 @@ pub unsafe fn dc_chat_is_sending_locations(chat: *const Chat) -> libc::c_int {
|
||||
pub fn dc_get_chat_cnt(context: &Context) -> usize {
|
||||
if context.sql.is_open() {
|
||||
/* no database, no chats - this is no error (needed eg. for information) */
|
||||
sql::query_row::<_, isize>(
|
||||
context,
|
||||
&context.sql,
|
||||
"SELECT COUNT(*) FROM chats WHERE id>9 AND blocked=0;",
|
||||
params![],
|
||||
0,
|
||||
)
|
||||
.unwrap_or_default() as usize
|
||||
context
|
||||
.sql
|
||||
.query_row_col::<_, isize>(
|
||||
context,
|
||||
"SELECT COUNT(*) FROM chats WHERE id>9 AND blocked=0;",
|
||||
params![],
|
||||
0,
|
||||
)
|
||||
.unwrap_or_default() as usize
|
||||
} else {
|
||||
0
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ use crate::dc_lot::*;
|
||||
use crate::dc_msg::*;
|
||||
use crate::dc_stock::*;
|
||||
use crate::dc_tools::*;
|
||||
use crate::sql;
|
||||
use crate::types::*;
|
||||
use crate::x::*;
|
||||
|
||||
@@ -27,19 +26,14 @@ pub unsafe fn dc_get_chatlist<'a>(
|
||||
query_str: *const libc::c_char,
|
||||
query_id: uint32_t,
|
||||
) -> *mut dc_chatlist_t<'a> {
|
||||
let mut success: libc::c_int = 0i32;
|
||||
let obj: *mut dc_chatlist_t = dc_chatlist_new(context);
|
||||
let obj = dc_chatlist_new(context);
|
||||
|
||||
if !(0 == dc_chatlist_load_from_db(obj, listflags, query_str, query_id)) {
|
||||
success = 1i32
|
||||
if 0 != dc_chatlist_load_from_db(obj, listflags, query_str, query_id) {
|
||||
return obj;
|
||||
}
|
||||
|
||||
if 0 != success {
|
||||
return obj;
|
||||
} else {
|
||||
dc_chatlist_unref(obj);
|
||||
return 0 as *mut dc_chatlist_t;
|
||||
};
|
||||
dc_chatlist_unref(obj);
|
||||
return 0 as *mut dc_chatlist_t;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -122,8 +116,6 @@ unsafe fn dc_chatlist_load_from_db(
|
||||
query__: *const libc::c_char,
|
||||
query_contact_id: u32,
|
||||
) -> libc::c_int {
|
||||
//clock_t start = clock();
|
||||
|
||||
if chatlist.is_null() || (*chatlist).magic != 0xc4a71157u32 {
|
||||
return 0;
|
||||
}
|
||||
@@ -142,15 +134,33 @@ unsafe fn dc_chatlist_load_from_db(
|
||||
// for the deaddrop, however, they should really be hidden, however, _currently_ the deaddrop is not
|
||||
// shown at all permanent in the chatlist.
|
||||
|
||||
let process_fn = |row: &rusqlite::Row| {
|
||||
dc_array_add_id((*chatlist).chatNlastmsg_ids, row.get(0)?);
|
||||
dc_array_add_id((*chatlist).chatNlastmsg_ids, row.get(1)?);
|
||||
let process_row = |row: &rusqlite::Row| {
|
||||
let chat_id: i32 = row.get(0)?;
|
||||
// TODO: verify that it is okay for this to be Null
|
||||
let msg_id: i32 = row.get(1).unwrap_or_default();
|
||||
|
||||
Ok((chat_id, msg_id))
|
||||
};
|
||||
|
||||
let process_rows = |rows: rusqlite::MappedRows<_>| {
|
||||
for row in rows {
|
||||
let (id1, id2) = row?;
|
||||
|
||||
dc_array_add_id((*chatlist).chatNlastmsg_ids, id1 as u32);
|
||||
dc_array_add_id((*chatlist).chatNlastmsg_ids, id2 as u32);
|
||||
}
|
||||
Ok(())
|
||||
};
|
||||
|
||||
let success =
|
||||
if query_contact_id != 0 {
|
||||
(*chatlist).context.sql.query_map(
|
||||
// nb: the query currently shows messages from blocked contacts in groups.
|
||||
// however, for normal-groups, this is okay as the message is also returned by dc_get_chat_msgs()
|
||||
// (otherwise it would be hard to follow conversations, wa and tg do the same)
|
||||
// for the deaddrop, however, they should really be hidden, however, _currently_ the deaddrop is not
|
||||
// shown at all permanent in the chatlist.
|
||||
|
||||
let success = if query_contact_id != 0 {
|
||||
// show chats shared with a given contact
|
||||
(*chatlist).context.sql.query_map(
|
||||
"SELECT c.id, m.id FROM chats c LEFT JOIN msgs m \
|
||||
ON c.id=m.chat_id \
|
||||
AND m.timestamp=( SELECT MAX(timestamp) \
|
||||
@@ -159,114 +169,118 @@ unsafe fn dc_chatlist_load_from_db(
|
||||
AND c.blocked=0 AND c.id IN(SELECT chat_id FROM chats_contacts WHERE contact_id=?) \
|
||||
GROUP BY c.id ORDER BY IFNULL(m.timestamp,0) DESC, m.id DESC;",
|
||||
params![query_contact_id as i32],
|
||||
process_fn,
|
||||
|res| res.collect::<rusqlite::Result<Vec<_>>>().map_err(Into::into),
|
||||
process_row,
|
||||
process_rows,
|
||||
)
|
||||
} else if 0 != listflags & 0x1 {
|
||||
} else if 0 != listflags & 0x1 {
|
||||
// show archived chats
|
||||
(*chatlist).context.sql.query_map(
|
||||
"SELECT c.id, m.id FROM chats c LEFT JOIN msgs m \
|
||||
ON c.id=m.chat_id \
|
||||
AND m.timestamp=( SELECT MAX(timestamp) \
|
||||
FROM msgs WHERE chat_id=c.id \
|
||||
AND (hidden=0 OR (hidden=1 AND state=19))) WHERE c.id>9 \
|
||||
AND c.blocked=0 AND c.archived=1 GROUP BY c.id \
|
||||
ORDER BY IFNULL(m.timestamp,0) DESC, m.id DESC;",
|
||||
params![],
|
||||
process_row,
|
||||
process_rows,
|
||||
)
|
||||
} else if query__.is_null() {
|
||||
// show normal chatlist
|
||||
if 0 == listflags & 0x2 {
|
||||
let last_deaddrop_fresh_msg_id = get_last_deaddrop_fresh_msg((*chatlist).context);
|
||||
if last_deaddrop_fresh_msg_id > 0 {
|
||||
dc_array_add_id((*chatlist).chatNlastmsg_ids, 1);
|
||||
dc_array_add_id((*chatlist).chatNlastmsg_ids, last_deaddrop_fresh_msg_id);
|
||||
}
|
||||
add_archived_link_item = 1;
|
||||
}
|
||||
(*chatlist).context.sql.query_map(
|
||||
"SELECT c.id, m.id FROM chats c \
|
||||
LEFT JOIN msgs m \
|
||||
ON c.id=m.chat_id \
|
||||
AND m.timestamp=( SELECT MAX(timestamp) \
|
||||
FROM msgs WHERE chat_id=c.id \
|
||||
AND (hidden=0 OR (hidden=1 AND state=19))) WHERE c.id>9 \
|
||||
AND c.blocked=0 AND c.archived=0 \
|
||||
GROUP BY c.id \
|
||||
ORDER BY IFNULL(m.timestamp,0) DESC, m.id DESC;",
|
||||
params![],
|
||||
process_row,
|
||||
process_rows,
|
||||
)
|
||||
} else {
|
||||
let query = to_string(query__).trim().to_string();
|
||||
if query.is_empty() {
|
||||
return 1;
|
||||
} else {
|
||||
let strLikeCmd = format!("%{}%", query);
|
||||
(*chatlist).context.sql.query_map(
|
||||
"SELECT c.id, m.id FROM chats c LEFT JOIN msgs m \
|
||||
ON c.id=m.chat_id \
|
||||
AND m.timestamp=( SELECT MAX(timestamp) \
|
||||
FROM msgs WHERE chat_id=c.id \
|
||||
AND (hidden=0 OR (hidden=1 AND state=19))) WHERE c.id>9 \
|
||||
AND c.blocked=0 AND c.archived=1 GROUP BY c.id \
|
||||
ORDER BY IFNULL(m.timestamp,0) DESC, m.id DESC;",
|
||||
params![],
|
||||
process_fn,
|
||||
|res| {
|
||||
res.collect::<rusqlite::Result<Vec<_>>>()
|
||||
.map_err(Into::into)
|
||||
},
|
||||
AND c.blocked=0 AND c.name LIKE ? \
|
||||
GROUP BY c.id ORDER BY IFNULL(m.timestamp,0) DESC, m.id DESC;",
|
||||
params![strLikeCmd],
|
||||
process_row,
|
||||
process_rows,
|
||||
)
|
||||
} else if query__.is_null() {
|
||||
if 0 == listflags & 0x2 {
|
||||
let last_deaddrop_fresh_msg_id = get_last_deaddrop_fresh_msg((*chatlist).context);
|
||||
if last_deaddrop_fresh_msg_id > 0 {
|
||||
dc_array_add_id((*chatlist).chatNlastmsg_ids, 1);
|
||||
dc_array_add_id((*chatlist).chatNlastmsg_ids, last_deaddrop_fresh_msg_id);
|
||||
}
|
||||
add_archived_link_item = 1;
|
||||
}
|
||||
(*chatlist).context.sql.query_map(
|
||||
"SELECT c.id, m.id FROM chats c \
|
||||
LEFT JOIN msgs m \
|
||||
ON c.id=m.chat_id \
|
||||
AND m.timestamp=( SELECT MAX(timestamp) \
|
||||
FROM msgs WHERE chat_id=c.id \
|
||||
AND (hidden=0 OR (hidden=1 AND state=19))) WHERE c.id>9 \
|
||||
AND c.blocked=0 AND c.archived=0 \
|
||||
GROUP BY c.id \
|
||||
ORDER BY IFNULL(m.timestamp,0) DESC, m.id DESC;",
|
||||
params![],
|
||||
process_fn,
|
||||
|res| {
|
||||
res.collect::<rusqlite::Result<Vec<_>>>()
|
||||
.map_err(Into::into)
|
||||
},
|
||||
)
|
||||
} else {
|
||||
let query = to_string(query__).trim().to_string();
|
||||
if query.is_empty() {
|
||||
return 1;
|
||||
} else {
|
||||
let strLikeCmd = format!("%{}%", query);
|
||||
(*chatlist).context.sql.query_map(
|
||||
"SELECT c.id, m.id FROM chats c LEFT JOIN msgs m \
|
||||
ON c.id=m.chat_id \
|
||||
AND m.timestamp=( SELECT MAX(timestamp) \
|
||||
FROM msgs WHERE chat_id=c.id \
|
||||
AND (hidden=0 OR (hidden=1 AND state=19))) WHERE c.id>9 \
|
||||
AND c.blocked=0 AND c.name LIKE ? \
|
||||
GROUP BY c.id ORDER BY IFNULL(m.timestamp,0) DESC, m.id DESC;",
|
||||
params![strLikeCmd],
|
||||
process_fn,
|
||||
|res| {
|
||||
res.collect::<rusqlite::Result<Vec<_>>>()
|
||||
.map_err(Into::into)
|
||||
},
|
||||
)
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
if 0 != add_archived_link_item && dc_get_archived_cnt((*chatlist).context) > 0 {
|
||||
if dc_array_get_cnt((*chatlist).chatNlastmsg_ids) == 0 && 0 != listflags & 0x4 {
|
||||
dc_array_add_id((*chatlist).chatNlastmsg_ids, 7 as uint32_t);
|
||||
dc_array_add_id((*chatlist).chatNlastmsg_ids, 0 as uint32_t);
|
||||
dc_array_add_id((*chatlist).chatNlastmsg_ids, 7);
|
||||
dc_array_add_id((*chatlist).chatNlastmsg_ids, 0);
|
||||
}
|
||||
dc_array_add_id((*chatlist).chatNlastmsg_ids, 6 as uint32_t);
|
||||
dc_array_add_id((*chatlist).chatNlastmsg_ids, 0 as uint32_t);
|
||||
dc_array_add_id((*chatlist).chatNlastmsg_ids, 6);
|
||||
dc_array_add_id((*chatlist).chatNlastmsg_ids, 0);
|
||||
}
|
||||
(*chatlist).cnt = dc_array_get_cnt((*chatlist).chatNlastmsg_ids).wrapping_div(2);
|
||||
(*chatlist).cnt = dc_array_get_cnt((*chatlist).chatNlastmsg_ids) / 2;
|
||||
|
||||
success.is_ok() as libc::c_int
|
||||
match success {
|
||||
Ok(_) => 1,
|
||||
Err(err) => {
|
||||
error!(
|
||||
(*chatlist).context,
|
||||
0, "chatlist: failed to load from database: {:?}", err
|
||||
);
|
||||
0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Context functions to work with chatlist
|
||||
pub fn dc_get_archived_cnt(context: &Context) -> libc::c_int {
|
||||
sql::query_row(
|
||||
context,
|
||||
&context.sql,
|
||||
"SELECT COUNT(*) FROM chats WHERE blocked=0 AND archived=1;",
|
||||
params![],
|
||||
0,
|
||||
)
|
||||
.unwrap_or_default()
|
||||
context
|
||||
.sql
|
||||
.query_row_col(
|
||||
context,
|
||||
"SELECT COUNT(*) FROM chats WHERE blocked=0 AND archived=1;",
|
||||
params![],
|
||||
0,
|
||||
)
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
fn get_last_deaddrop_fresh_msg(context: &Context) -> u32 {
|
||||
// we have an index over the state-column, this should be sufficient as there are typically only few fresh messages
|
||||
sql::query_row(
|
||||
context,
|
||||
&context.sql,
|
||||
"SELECT m.id FROM msgs m LEFT JOIN chats c ON c.id=m.chat_id \
|
||||
WHERE m.state=10 \
|
||||
AND m.hidden=0 \
|
||||
AND c.blocked=2 \
|
||||
ORDER BY m.timestamp DESC, m.id DESC;",
|
||||
params![],
|
||||
0,
|
||||
)
|
||||
.unwrap_or_default()
|
||||
context
|
||||
.sql
|
||||
.query_row_col(
|
||||
context,
|
||||
"SELECT m.id FROM msgs m LEFT JOIN chats c ON c.id=m.chat_id \
|
||||
WHERE m.state=10 \
|
||||
AND m.hidden=0 \
|
||||
AND c.blocked=2 \
|
||||
ORDER BY m.timestamp DESC, m.id DESC;",
|
||||
params![],
|
||||
0,
|
||||
)
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
pub unsafe fn dc_chatlist_get_cnt(chatlist: *const dc_chatlist_t) -> size_t {
|
||||
|
||||
@@ -9,7 +9,6 @@ use crate::dc_saxparser::*;
|
||||
use crate::dc_tools::*;
|
||||
use crate::imap::*;
|
||||
use crate::oauth2::*;
|
||||
use crate::sql;
|
||||
use crate::types::*;
|
||||
use crate::x::*;
|
||||
|
||||
@@ -75,7 +74,12 @@ pub unsafe fn dc_has_ongoing(context: &Context) -> libc::c_int {
|
||||
}
|
||||
}
|
||||
pub fn dc_is_configured(context: &Context) -> libc::c_int {
|
||||
if sql::get_config_int(context, &context.sql, "configured", 0) > 0 {
|
||||
if context
|
||||
.sql
|
||||
.get_config_int(context, "configured")
|
||||
.unwrap_or_default()
|
||||
> 0
|
||||
{
|
||||
1
|
||||
} else {
|
||||
0
|
||||
@@ -102,6 +106,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &Context, _job: *mut dc_j
|
||||
let mut imap_connected_here = false;
|
||||
let mut smtp_connected_here = false;
|
||||
let mut ongoing_allocated_here = false;
|
||||
|
||||
let mut param_autoconfig = None;
|
||||
if !(0 == dc_alloc_ongoing(context)) {
|
||||
ongoing_allocated_here = true;
|
||||
@@ -167,12 +172,9 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &Context, _job: *mut dc_j
|
||||
.and_then(|e| e.parse().ok())
|
||||
{
|
||||
param.addr = oauth2_addr;
|
||||
sql::set_config(
|
||||
context,
|
||||
&context.sql,
|
||||
"addr",
|
||||
Some(param.addr.as_str()),
|
||||
);
|
||||
context
|
||||
.sql
|
||||
.set_config(context, "addr", Some(param.addr.as_str()));
|
||||
}
|
||||
if s.shall_stop_ongoing {
|
||||
current_block = 2927484062889439186;
|
||||
@@ -772,7 +774,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &Context, _job: *mut dc_j
|
||||
0 as uintptr_t,
|
||||
);
|
||||
/* try to connect to SMTP - if we did not got an autoconfig, the first try was SSL-465 and we do a second try with STARTTLS-587 */
|
||||
if context
|
||||
if !context
|
||||
.smtp
|
||||
.clone()
|
||||
.lock()
|
||||
@@ -811,7 +813,8 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &Context, _job: *mut dc_j
|
||||
context,
|
||||
0, "Trying: {}", r_3,
|
||||
);
|
||||
if context
|
||||
|
||||
if !context
|
||||
.smtp
|
||||
.clone()
|
||||
.lock()
|
||||
@@ -857,7 +860,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &Context, _job: *mut dc_j
|
||||
r_4
|
||||
);
|
||||
|
||||
if context
|
||||
if !context
|
||||
.smtp
|
||||
.clone()
|
||||
.lock()
|
||||
@@ -898,25 +901,25 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &Context, _job: *mut dc_j
|
||||
as uintptr_t,
|
||||
0 as uintptr_t,
|
||||
);
|
||||
flags
|
||||
=
|
||||
if 0
|
||||
!=
|
||||
sql::get_config_int(
|
||||
context, &context.sql,
|
||||
"mvbox_watch",
|
||||
1
|
||||
)
|
||||
||
|
||||
0
|
||||
!=
|
||||
sql::get_config_int(
|
||||
context,
|
||||
&context.sql,
|
||||
"mvbox_move",
|
||||
1
|
||||
)
|
||||
{
|
||||
flags = if 0
|
||||
!= context
|
||||
.sql
|
||||
.get_config_int(
|
||||
context,
|
||||
"mvbox_watch",
|
||||
)
|
||||
.unwrap_or_else(
|
||||
|| 1,
|
||||
)
|
||||
|| 0 != context
|
||||
.sql
|
||||
.get_config_int(
|
||||
context,
|
||||
"mvbox_move",
|
||||
)
|
||||
.unwrap_or_else(
|
||||
|| 1,
|
||||
) {
|
||||
0x1
|
||||
} else {
|
||||
0
|
||||
@@ -957,12 +960,13 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &Context, _job: *mut dc_j
|
||||
&context.sql,
|
||||
"configured_",
|
||||
);
|
||||
sql::set_config_int(
|
||||
context,
|
||||
&context.sql,
|
||||
"configured",
|
||||
1,
|
||||
);
|
||||
context
|
||||
.sql
|
||||
.set_config_int(
|
||||
context,
|
||||
"configured",
|
||||
1,
|
||||
);
|
||||
if !s.shall_stop_ongoing
|
||||
{
|
||||
context.call_cb(
|
||||
@@ -1035,22 +1039,39 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &Context, _job: *mut dc_j
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if imap_connected_here {
|
||||
// XXX why do we want to disconnect here?
|
||||
// context.inbox.read().unwrap().disconnect(context);
|
||||
info!(context, 0, "Skipping INBOX/IMAP disconnect");
|
||||
context.inbox.read().unwrap().disconnect(context);
|
||||
}
|
||||
if smtp_connected_here {
|
||||
// XXX why do we want to disconnect here?
|
||||
// context.smtp.clone().lock().unwrap().disconnect();
|
||||
info!(context, 0, "Skipping SMTP disconnect");
|
||||
context.smtp.clone().lock().unwrap().disconnect();
|
||||
}
|
||||
|
||||
/*
|
||||
if !success {
|
||||
// disconnect if configure did not succeed
|
||||
if imap_connected_here {
|
||||
// context.inbox.read().unwrap().disconnect(context);
|
||||
}
|
||||
if smtp_connected_here {
|
||||
// context.smtp.clone().lock().unwrap().disconnect();
|
||||
}
|
||||
} else {
|
||||
assert!(imap_connected_here && smtp_connected_here);
|
||||
info!(
|
||||
context,
|
||||
0, "Keeping IMAP/SMTP connections open after successful configuration"
|
||||
);
|
||||
}
|
||||
*/
|
||||
if ongoing_allocated_here {
|
||||
dc_free_ongoing(context);
|
||||
}
|
||||
|
||||
context.call_cb(Event::CONFIGURE_PROGRESS, if success { 1000 } else { 0 }, 0);
|
||||
context.call_cb(
|
||||
Event::CONFIGURE_PROGRESS,
|
||||
(if success { 1000 } else { 0 }) as uintptr_t,
|
||||
0 as uintptr_t,
|
||||
);
|
||||
}
|
||||
|
||||
pub unsafe fn dc_free_ongoing(context: &Context) {
|
||||
@@ -1077,12 +1098,14 @@ unsafe fn moz_autoconfigure(
|
||||
tag_config: 0,
|
||||
};
|
||||
|
||||
let xml_raw = read_autoconf_file(context, to_cstring(url).as_ptr());
|
||||
let url_c = to_cstring(url);
|
||||
let xml_raw = read_autoconf_file(context, url_c);
|
||||
free(url_c as *mut libc::c_void);
|
||||
if xml_raw.is_null() {
|
||||
return None;
|
||||
}
|
||||
|
||||
moz_ac.in_emaillocalpart = dc_strdup(to_cstring(¶m_in.addr).as_ptr());
|
||||
moz_ac.in_emaillocalpart = to_cstring(¶m_in.addr);
|
||||
let p = strchr(moz_ac.in_emaillocalpart, '@' as i32);
|
||||
|
||||
if p.is_null() {
|
||||
@@ -1139,11 +1162,13 @@ unsafe fn moz_autoconfigure_text_cb(
|
||||
let mut moz_ac: *mut moz_autoconfigure_t = userdata as *mut moz_autoconfigure_t;
|
||||
let mut val: *mut libc::c_char = dc_strdup(text);
|
||||
dc_trim(val);
|
||||
let addr = to_cstring(&(*moz_ac).in_0.addr);
|
||||
dc_str_replace(
|
||||
&mut val,
|
||||
b"%EMAILADDRESS%\x00" as *const u8 as *const libc::c_char,
|
||||
to_cstring(&(*moz_ac).in_0.addr).as_ptr(),
|
||||
addr,
|
||||
);
|
||||
free(addr as *mut libc::c_void);
|
||||
dc_str_replace(
|
||||
&mut val,
|
||||
b"%EMAILLOCALPART%\x00" as *const u8 as *const libc::c_char,
|
||||
@@ -1277,7 +1302,7 @@ fn read_autoconf_file(context: &Context, url: *const libc::c_char) -> *mut libc:
|
||||
.send()
|
||||
.and_then(|mut res| res.text())
|
||||
{
|
||||
Ok(res) => unsafe { libc::strdup(to_cstring(res).as_ptr()) },
|
||||
Ok(res) => unsafe { to_cstring(res) },
|
||||
Err(_err) => {
|
||||
info!(context, 0, "Can\'t read file.",);
|
||||
|
||||
@@ -1293,7 +1318,7 @@ unsafe fn outlk_autodiscover(
|
||||
) -> Option<dc_loginparam_t> {
|
||||
let current_block: u64;
|
||||
let mut xml_raw: *mut libc::c_char = 0 as *mut libc::c_char;
|
||||
let mut url = dc_strdup(to_cstring(url__).as_ptr());
|
||||
let mut url = to_cstring(url__);
|
||||
let mut outlk_ad = outlk_autodiscover_t {
|
||||
in_0: param_in,
|
||||
out: dc_loginparam_new(),
|
||||
@@ -1486,7 +1511,12 @@ pub fn dc_connect_to_configured_imap(context: &Context, imap: &Imap) -> libc::c_
|
||||
|
||||
if imap.is_connected() {
|
||||
ret_connected = 1
|
||||
} else if sql::get_config_int(context, &context.sql, "configured", 0) == 0 {
|
||||
} else if context
|
||||
.sql
|
||||
.get_config_int(context, "configured")
|
||||
.unwrap_or_default()
|
||||
== 0
|
||||
{
|
||||
warn!(context, 0, "Not configured, cannot connect.",);
|
||||
} else {
|
||||
let param = dc_loginparam_read(context, &context.sql, "configured_");
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use crate::aheader::EncryptPreference;
|
||||
use crate::config;
|
||||
use crate::constants::Event;
|
||||
use crate::context::Context;
|
||||
use crate::context::*;
|
||||
use crate::dc_array::*;
|
||||
use crate::dc_e2ee::*;
|
||||
use crate::dc_loginparam::*;
|
||||
@@ -34,7 +34,9 @@ pub fn dc_marknoticed_contact(context: &Context, contact_id: u32) {
|
||||
&context.sql,
|
||||
"UPDATE msgs SET state=13 WHERE from_id=? AND state=10;",
|
||||
params![contact_id as i32],
|
||||
) {
|
||||
)
|
||||
.is_ok()
|
||||
{
|
||||
context.call_cb(Event::MSGS_CHANGED, 0, 0);
|
||||
}
|
||||
}
|
||||
@@ -70,15 +72,16 @@ pub unsafe fn dc_lookup_contact_id_by_addr(
|
||||
|
||||
let addr_normalized_c = dc_addr_normalize(addr);
|
||||
let addr_normalized = as_str(addr_normalized_c);
|
||||
let addr_self =
|
||||
sql::get_config(context, &context.sql, "configured_addr", None).unwrap_or_default();
|
||||
let addr_self = context
|
||||
.sql
|
||||
.get_config(context, "configured_addr")
|
||||
.unwrap_or_default();
|
||||
|
||||
let contact_id = if addr_normalized == addr_self {
|
||||
1
|
||||
} else {
|
||||
sql::query_row(
|
||||
context.sql.query_row_col(
|
||||
context,
|
||||
&context.sql,
|
||||
"SELECT id FROM contacts WHERE addr=?1 COLLATE NOCASE AND id>?2 AND origin>=?3 AND blocked=0;",
|
||||
params![addr_normalized, 9, 0x100],
|
||||
0
|
||||
@@ -160,7 +163,9 @@ pub unsafe fn dc_block_contact(context: &Context, contact_id: uint32_t, new_bloc
|
||||
&context.sql,
|
||||
"UPDATE contacts SET blocked=? WHERE id=?;",
|
||||
params![new_blocking, contact_id as i32],
|
||||
) {
|
||||
)
|
||||
.is_ok()
|
||||
{
|
||||
// also (un)block all chats with _only_ this contact - we do not delete them to allow a
|
||||
// non-destructive blocking->unblocking.
|
||||
// (Maybe, beside normal chats (type=100) we should also block group chats with only this user.
|
||||
@@ -171,7 +176,7 @@ pub unsafe fn dc_block_contact(context: &Context, contact_id: uint32_t, new_bloc
|
||||
&context.sql,
|
||||
"UPDATE chats SET blocked=? WHERE type=? AND id IN (SELECT chat_id FROM chats_contacts WHERE contact_id=?);",
|
||||
params![new_blocking, 100, contact_id as i32],
|
||||
) {
|
||||
).is_ok() {
|
||||
dc_marknoticed_contact(context, contact_id);
|
||||
context.call_cb(
|
||||
Event::CONTACTS_CHANGED,
|
||||
@@ -254,7 +259,7 @@ pub unsafe fn dc_contact_empty(mut contact: *mut dc_contact_t) {
|
||||
/* address is in our address book */
|
||||
/* set on Alice's side for contacts like Bob that have scanned the QR code offered by her. Only means the contact has once been established using the "securejoin" procedure in the past, getting the current key verification status requires calling dc_contact_is_verified() ! */
|
||||
/* set on Bob's side for contacts scanned and verified from a QR code. Only means the contact has once been established using the "securejoin" procedure in the past, getting the current key verification status requires calling dc_contact_is_verified() ! */
|
||||
/* contact added mannually by dc_create_contact(), this should be the largets origin as otherwise the user cannot modify the names */
|
||||
/* contact added manually by dc_create_contact(), this should be the largets origin as otherwise the user cannot modify the names */
|
||||
/* contacts with at least this origin value are shown in the contact list */
|
||||
/* contacts with at least this origin value are verified and known not to be spam */
|
||||
/* contacts with at least this origin value start a new "normal" chat, defaults to off */
|
||||
@@ -272,12 +277,12 @@ pub unsafe fn dc_contact_load_from_db(
|
||||
if contact_id == 1 as libc::c_uint {
|
||||
(*contact).id = contact_id;
|
||||
(*contact).name = dc_stock_str((*contact).context, 2);
|
||||
(*contact).addr = dc_strdup(
|
||||
to_cstring(
|
||||
sql::get_config((*contact).context, sql, "configured_addr", Some(""))
|
||||
.unwrap_or_default(),
|
||||
)
|
||||
.as_ptr(),
|
||||
(*contact).addr = to_cstring(
|
||||
(*contact)
|
||||
.context
|
||||
.sql
|
||||
.get_config((*contact).context, "configured_addr")
|
||||
.unwrap_or_default(),
|
||||
);
|
||||
true
|
||||
} else {
|
||||
@@ -286,11 +291,11 @@ pub unsafe fn dc_contact_load_from_db(
|
||||
params![contact_id as i32],
|
||||
|row| {
|
||||
(*contact).id = contact_id;
|
||||
(*contact).name = dc_strdup(to_cstring(row.get::<_, String>(0)?).as_ptr());
|
||||
(*contact).addr = dc_strdup(to_cstring(row.get::<_, String>(1)?).as_ptr());
|
||||
(*contact).name = to_cstring(row.get::<_, String>(0)?);
|
||||
(*contact).addr = to_cstring(row.get::<_, String>(1)?);
|
||||
(*contact).origin = row.get(2)?;
|
||||
(*contact).blocked = row.get(3)?;
|
||||
(*contact).authname = dc_strdup(to_cstring(row.get::<_, String>(4)?).as_ptr());
|
||||
(*contact).blocked = row.get::<_, Option<i32>>(3)?.unwrap_or_default();
|
||||
(*contact).authname = to_cstring(row.get::<_, String>(4)?);
|
||||
Ok(())
|
||||
}
|
||||
).is_ok()
|
||||
@@ -331,8 +336,10 @@ pub fn dc_add_or_lookup_contact(
|
||||
|
||||
let addr_c = unsafe { dc_addr_normalize(addr__) };
|
||||
let addr = as_str(addr_c);
|
||||
let addr_self =
|
||||
sql::get_config(context, &context.sql, "configured_addr", Some("")).unwrap_or_default();
|
||||
let addr_self = context
|
||||
.sql
|
||||
.get_config(context, "configured_addr")
|
||||
.unwrap_or_default();
|
||||
|
||||
if addr == addr_self {
|
||||
return 1;
|
||||
@@ -429,7 +436,9 @@ pub fn dc_add_or_lookup_contact(
|
||||
&context.sql,
|
||||
"INSERT INTO contacts (name, addr, origin) VALUES(?, ?, ?);",
|
||||
params![to_string(name), addr, origin,],
|
||||
) {
|
||||
)
|
||||
.is_ok()
|
||||
{
|
||||
row_id = sql::get_rowid(context, &context.sql, "contacts", "addr", addr);
|
||||
unsafe { *sth_modified = 2 };
|
||||
} else {
|
||||
@@ -519,8 +528,10 @@ pub fn dc_get_contacts(
|
||||
listflags: u32,
|
||||
query: *const libc::c_char,
|
||||
) -> *mut dc_array_t {
|
||||
let self_addr =
|
||||
sql::get_config(context, &context.sql, "configured_addr", Some("")).unwrap_or_default();
|
||||
let self_addr = context
|
||||
.sql
|
||||
.get_config(context, "configured_addr")
|
||||
.unwrap_or_default();
|
||||
|
||||
let mut add_self = false;
|
||||
let ret = unsafe { dc_array_new(100) };
|
||||
@@ -558,8 +569,10 @@ pub fn dc_get_contacts(
|
||||
)
|
||||
.unwrap(); // TODO: Better error handling
|
||||
|
||||
let self_name =
|
||||
sql::get_config(context, &context.sql, "displayname", Some("")).unwrap_or_default();
|
||||
let self_name = context
|
||||
.sql
|
||||
.get_config(context, "displayname")
|
||||
.unwrap_or_default();
|
||||
|
||||
let self_name2 = unsafe { dc_stock_str(context, 2) };
|
||||
|
||||
@@ -595,14 +608,15 @@ pub fn dc_get_contacts(
|
||||
}
|
||||
|
||||
pub fn dc_get_blocked_cnt(context: &Context) -> libc::c_int {
|
||||
sql::query_row(
|
||||
context,
|
||||
&context.sql,
|
||||
"SELECT COUNT(*) FROM contacts WHERE id>? AND blocked!=0",
|
||||
params![9],
|
||||
0,
|
||||
)
|
||||
.unwrap_or_default()
|
||||
context
|
||||
.sql
|
||||
.query_row_col(
|
||||
context,
|
||||
"SELECT COUNT(*) FROM contacts WHERE id>? AND blocked!=0",
|
||||
params![9],
|
||||
0,
|
||||
)
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
pub fn dc_get_blocked_contacts(context: &Context) -> *mut dc_array_t {
|
||||
@@ -718,7 +732,7 @@ pub unsafe fn dc_get_contact_encrinfo(
|
||||
free(fingerprint_other_verified as *mut libc::c_void);
|
||||
free(fingerprint_other_unverified as *mut libc::c_void);
|
||||
|
||||
strdup(to_cstring(ret).as_ptr())
|
||||
to_cstring(ret)
|
||||
}
|
||||
|
||||
unsafe fn cat_fingerprint(
|
||||
@@ -757,24 +771,26 @@ pub fn dc_delete_contact(context: &Context, contact_id: u32) -> bool {
|
||||
return false;
|
||||
}
|
||||
|
||||
let count_contacts: i32 = sql::query_row(
|
||||
context,
|
||||
&context.sql,
|
||||
"SELECT COUNT(*) FROM chats_contacts WHERE contact_id=?;",
|
||||
params![contact_id as i32],
|
||||
0,
|
||||
)
|
||||
.unwrap_or_default();
|
||||
|
||||
let count_msgs: i32 = if count_contacts > 0 {
|
||||
sql::query_row(
|
||||
let count_contacts: i32 = context
|
||||
.sql
|
||||
.query_row_col(
|
||||
context,
|
||||
&context.sql,
|
||||
"SELECT COUNT(*) FROM msgs WHERE from_id=? OR to_id=?;",
|
||||
params![contact_id as i32, contact_id as i32],
|
||||
"SELECT COUNT(*) FROM chats_contacts WHERE contact_id=?;",
|
||||
params![contact_id as i32],
|
||||
0,
|
||||
)
|
||||
.unwrap_or_default()
|
||||
.unwrap_or_default();
|
||||
|
||||
let count_msgs: i32 = if count_contacts > 0 {
|
||||
context
|
||||
.sql
|
||||
.query_row_col(
|
||||
context,
|
||||
"SELECT COUNT(*) FROM msgs WHERE from_id=? OR to_id=?;",
|
||||
params![contact_id as i32, contact_id as i32],
|
||||
0,
|
||||
)
|
||||
.unwrap_or_default()
|
||||
} else {
|
||||
0
|
||||
};
|
||||
@@ -785,7 +801,9 @@ pub fn dc_delete_contact(context: &Context, contact_id: u32) -> bool {
|
||||
&context.sql,
|
||||
"DELETE FROM contacts WHERE id=?;",
|
||||
params![contact_id as i32],
|
||||
) {
|
||||
)
|
||||
.is_ok()
|
||||
{
|
||||
context.call_cb(Event::CONTACTS_CHANGED, 0, 0);
|
||||
true
|
||||
} else {
|
||||
@@ -882,9 +900,9 @@ pub fn dc_contact_get_profile_image(contact: *const dc_contact_t) -> *mut libc::
|
||||
}
|
||||
|
||||
if unsafe { (*contact).id } == 1 {
|
||||
let avatar = dc_get_config(unsafe { (*contact).context }, "selfavatar");
|
||||
if !avatar.is_empty() {
|
||||
image_abs = unsafe { dc_strdup(to_cstring(avatar).as_ptr()) };
|
||||
let context = unsafe { (*contact) }.context;
|
||||
if let Some(avatar) = context.get_config(config::Config::Selfavatar) {
|
||||
image_abs = unsafe { to_cstring(avatar) };
|
||||
}
|
||||
}
|
||||
// TODO: else get image_abs from contact param
|
||||
@@ -974,7 +992,7 @@ pub fn dc_addr_equals_self(context: &Context, addr: *const libc::c_char) -> libc
|
||||
|
||||
if !addr.is_null() {
|
||||
let normalized_addr = unsafe { dc_addr_normalize(addr) };
|
||||
if let Some(self_addr) = sql::get_config(context, &context.sql, "configured_addr", None) {
|
||||
if let Some(self_addr) = context.sql.get_config(context, "configured_addr") {
|
||||
ret = (as_str(normalized_addr) == self_addr) as libc::c_int;
|
||||
}
|
||||
unsafe { free(normalized_addr as *mut libc::c_void) };
|
||||
@@ -1014,14 +1032,15 @@ pub fn dc_get_real_contact_cnt(context: &Context) -> usize {
|
||||
return 0;
|
||||
}
|
||||
|
||||
sql::query_row::<_, isize>(
|
||||
context,
|
||||
&context.sql,
|
||||
"SELECT COUNT(*) FROM contacts WHERE id>?;",
|
||||
params![9],
|
||||
0,
|
||||
)
|
||||
.unwrap_or_default() as usize
|
||||
context
|
||||
.sql
|
||||
.query_row_col::<_, isize>(
|
||||
context,
|
||||
"SELECT COUNT(*) FROM contacts WHERE id>?;",
|
||||
params![9],
|
||||
0,
|
||||
)
|
||||
.unwrap_or_default() as usize
|
||||
}
|
||||
|
||||
pub unsafe fn dc_get_contact_origin(
|
||||
|
||||
@@ -53,7 +53,7 @@ pub unsafe fn dc_dehtml(buf_terminated: *mut libc::c_char) -> *mut libc::c_char
|
||||
dc_saxparser_parse(&mut saxparser, buf_terminated);
|
||||
free(dehtml.last_href as *mut libc::c_void);
|
||||
|
||||
strdup(to_cstring(dehtml.strbuilder).as_ptr())
|
||||
to_cstring(dehtml.strbuilder)
|
||||
}
|
||||
|
||||
unsafe fn dehtml_text_cb(
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use std::collections::HashSet;
|
||||
use std::ffi::{CStr, CString};
|
||||
use std::ffi::CStr;
|
||||
use std::str::FromStr;
|
||||
|
||||
use mmime::clist::*;
|
||||
@@ -24,7 +24,6 @@ use crate::key::*;
|
||||
use crate::keyring::*;
|
||||
use crate::peerstate::*;
|
||||
use crate::pgp::*;
|
||||
use crate::sql;
|
||||
use crate::types::*;
|
||||
use crate::x::*;
|
||||
|
||||
@@ -32,7 +31,7 @@ use crate::x::*;
|
||||
// attachments of 25 mb brutto should work on the majority of providers
|
||||
// (brutto examples: web.de=50, 1&1=40, t-online.de=32, gmail=25, posteo=50, yahoo=25, all-inkl=100).
|
||||
// as an upper limit, we double the size; the core won't send messages larger than this
|
||||
// to get the netto sizes, we substract 1 mb header-overhead and the base64-overhead.
|
||||
// to get the netto sizes, we subtract 1 mb header-overhead and the base64-overhead.
|
||||
// some defaults
|
||||
#[derive(Clone)]
|
||||
pub struct dc_e2ee_helper_t {
|
||||
@@ -81,13 +80,18 @@ pub unsafe fn dc_e2ee_encrypt(
|
||||
|| plain.is_null())
|
||||
{
|
||||
/* libEtPan's pgp_encrypt_mime() takes the parent as the new root. We just expect the root as being given to this function. */
|
||||
let prefer_encrypt = if 0 != sql::get_config_int(context, &context.sql, "e2ee_enabled", 1) {
|
||||
let prefer_encrypt = if 0
|
||||
!= context
|
||||
.sql
|
||||
.get_config_int(context, "e2ee_enabled")
|
||||
.unwrap_or_default()
|
||||
{
|
||||
EncryptPreference::Mutual
|
||||
} else {
|
||||
EncryptPreference::NoPreference
|
||||
};
|
||||
|
||||
let addr = sql::get_config(context, &context.sql, "configured_addr", None);
|
||||
let addr = context.sql.get_config(context, "configured_addr");
|
||||
|
||||
if let Some(addr) = addr {
|
||||
if let Some(public_key) =
|
||||
@@ -180,7 +184,7 @@ pub unsafe fn dc_e2ee_encrypt(
|
||||
b"Autocrypt-Gossip\x00" as *const u8
|
||||
as *const libc::c_char,
|
||||
),
|
||||
strdup(header.as_ptr()),
|
||||
header,
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -290,10 +294,8 @@ pub unsafe fn dc_e2ee_encrypt(
|
||||
sign_key.as_ref(),
|
||||
) {
|
||||
let ctext_bytes = ctext_v.len();
|
||||
let ctext_c = CString::new(ctext_v).unwrap();
|
||||
let ctext = strdup(ctext_c.as_ptr());
|
||||
|
||||
(*helper).cdata_to_free = ctext as *mut libc::c_void;
|
||||
let ctext = to_cstring(ctext_v);
|
||||
(*helper).cdata_to_free = ctext as *mut _;
|
||||
|
||||
/* create MIME-structure that will contain the encrypted text */
|
||||
let mut encrypted_part: *mut mailmime = new_data_part(
|
||||
@@ -350,13 +352,13 @@ pub unsafe fn dc_e2ee_encrypt(
|
||||
14181132614457621749 => {}
|
||||
_ => {
|
||||
let aheader = Aheader::new(addr, public_key, prefer_encrypt);
|
||||
let rendered = CString::new(aheader.to_string()).unwrap();
|
||||
let rendered = to_cstring(aheader.to_string());
|
||||
|
||||
mailimf_fields_add(
|
||||
imffields_unprotected,
|
||||
mailimf_field_new_custom(
|
||||
strdup(b"Autocrypt\x00" as *const u8 as *const libc::c_char),
|
||||
strdup(rendered.as_ptr()),
|
||||
rendered,
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -597,7 +599,7 @@ pub unsafe fn dc_e2ee_decrypt(
|
||||
}
|
||||
}
|
||||
/* load private key for decryption */
|
||||
let self_addr = sql::get_config(context, &context.sql, "configured_addr", None);
|
||||
let self_addr = context.sql.get_config(context, "configured_addr");
|
||||
if let Some(self_addr) = self_addr {
|
||||
if private_keyring.load_self_private_for_decrypting(context, self_addr, &context.sql) {
|
||||
if peerstate.as_ref().map(|p| p.last_seen).unwrap_or_else(|| 0) == 0 {
|
||||
@@ -931,13 +933,12 @@ unsafe fn decrypt_part(
|
||||
add_signatures,
|
||||
) {
|
||||
let plain_bytes = plain.len();
|
||||
let plain_c = CString::new(plain).unwrap();
|
||||
let plain_buf = strdup(plain_c.as_ptr());
|
||||
let plain_buf = plain.as_ptr() as *const libc::c_char;
|
||||
|
||||
let mut index: size_t = 0i32 as size_t;
|
||||
let mut decrypted_mime: *mut mailmime = 0 as *mut mailmime;
|
||||
if mailmime_parse(
|
||||
plain_buf as *const libc::c_char,
|
||||
plain_buf as *const _,
|
||||
plain_bytes,
|
||||
&mut index,
|
||||
&mut decrypted_mime,
|
||||
@@ -1006,7 +1007,7 @@ unsafe fn has_decrypted_pgp_armor(
|
||||
* that we could use the normal Autocrypt processing.
|
||||
*
|
||||
* @private
|
||||
* @param mime The mime struture to check
|
||||
* @param mime The mime structure to check
|
||||
* @return 1=multipart/report found in MIME, 0=no multipart/report found
|
||||
*/
|
||||
// TODO should return bool /rtn
|
||||
@@ -1066,7 +1067,7 @@ pub unsafe fn dc_ensure_secret_key_exists(context: &Context) -> libc::c_int {
|
||||
(this is to gain some extra-random-seed by the message content and the timespan between program start and message sending) */
|
||||
let mut success: libc::c_int = 0i32;
|
||||
|
||||
let self_addr = sql::get_config(context, &context.sql, "configured_addr", None);
|
||||
let self_addr = context.sql.get_config(context, "configured_addr");
|
||||
if self_addr.is_none() {
|
||||
warn!(
|
||||
context,
|
||||
|
||||
217
src/dc_imex.rs
217
src/dc_imex.rs
@@ -70,7 +70,8 @@ pub unsafe fn dc_imex_has_backup(
|
||||
let sql = Sql::new();
|
||||
if sql.open(context, &path, 0x1) {
|
||||
let curr_backup_time =
|
||||
sql::get_config_int(context, &sql, "backup_time", 0) as u64;
|
||||
sql.get_config_int(context, "backup_time")
|
||||
.unwrap_or_default() as u64;
|
||||
if curr_backup_time > newest_backup_time {
|
||||
newest_backup_path = Some(path);
|
||||
newest_backup_time = curr_backup_time;
|
||||
@@ -223,10 +224,15 @@ pub unsafe extern "C" fn dc_render_setup_file(
|
||||
passphrase_begin[2usize] = 0i32 as libc::c_char;
|
||||
/* create the payload */
|
||||
if !(0 == dc_ensure_secret_key_exists(context)) {
|
||||
let self_addr =
|
||||
sql::get_config(context, &context.sql, "configured_addr", None).unwrap_or_default();
|
||||
let self_addr = context
|
||||
.sql
|
||||
.get_config(context, "configured_addr")
|
||||
.unwrap_or_default();
|
||||
let curr_private_key = Key::from_self_private(context, self_addr, &context.sql);
|
||||
let e2ee_enabled = sql::get_config_int(context, &context.sql, "e2ee_enabled", 1);
|
||||
let e2ee_enabled = context
|
||||
.sql
|
||||
.get_config_int(context, "e2ee_enabled")
|
||||
.unwrap_or_else(|| 1);
|
||||
|
||||
let headers = if 0 != e2ee_enabled {
|
||||
Some(("Autocrypt-Prefer-Encrypt", "mutual"))
|
||||
@@ -303,7 +309,7 @@ pub unsafe fn dc_create_setup_code(_context: &Context) -> *mut libc::c_char {
|
||||
);
|
||||
}
|
||||
|
||||
strdup(to_cstring(ret).as_ptr())
|
||||
to_cstring(ret)
|
||||
}
|
||||
|
||||
// TODO should return bool /rtn
|
||||
@@ -387,29 +393,33 @@ fn set_self_key(
|
||||
let (private_key, public_key, header) = keys.unwrap();
|
||||
let preferencrypt = header.get("Autocrypt-Prefer-Encrypt");
|
||||
|
||||
if !sql::execute(
|
||||
if sql::execute(
|
||||
context,
|
||||
&context.sql,
|
||||
"DELETE FROM keypairs WHERE public_key=? OR private_key=?;",
|
||||
params![public_key.to_bytes(), private_key.to_bytes()],
|
||||
) {
|
||||
)
|
||||
.is_err()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if 0 != set_default {
|
||||
if !sql::execute(
|
||||
if sql::execute(
|
||||
context,
|
||||
&context.sql,
|
||||
"UPDATE keypairs SET is_default=0;",
|
||||
params![],
|
||||
) {
|
||||
)
|
||||
.is_err()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
error!(context, 0, "File does not contain a private key.",);
|
||||
}
|
||||
|
||||
let self_addr = sql::get_config(context, &context.sql, "configured_addr", None);
|
||||
let self_addr = context.sql.get_config(context, "configured_addr");
|
||||
|
||||
if self_addr.is_none() {
|
||||
error!(context, 0, "Missing self addr");
|
||||
@@ -430,8 +440,14 @@ fn set_self_key(
|
||||
|
||||
match preferencrypt.map(|s| s.as_str()) {
|
||||
Some("") => 0,
|
||||
Some("nopreference") => sql::set_config_int(context, &context.sql, "e2ee_enabled", 0),
|
||||
Some("mutual") => sql::set_config_int(context, &context.sql, "e2ee_enabled", 1),
|
||||
Some("nopreference") => context
|
||||
.sql
|
||||
.set_config_int(context, "e2ee_enabled", 0)
|
||||
.is_ok() as libc::c_int,
|
||||
Some("mutual") => context
|
||||
.sql
|
||||
.set_config_int(context, "e2ee_enabled", 1)
|
||||
.is_ok() as libc::c_int,
|
||||
_ => 1,
|
||||
}
|
||||
}
|
||||
@@ -523,7 +539,7 @@ pub unsafe fn dc_normalize_setup_code(
|
||||
p1 = p1.offset(1);
|
||||
}
|
||||
|
||||
strdup(to_cstring(out).as_ptr())
|
||||
to_cstring(out)
|
||||
}
|
||||
|
||||
pub unsafe fn dc_job_do_DC_JOB_IMEX_IMAP(context: &Context, job: *mut dc_job_t) {
|
||||
@@ -781,90 +797,84 @@ unsafe fn import_backup(context: &Context, backup_to_import: *const libc::c_char
|
||||
return 0;
|
||||
}
|
||||
|
||||
let total_files_cnt = sql::query_row::<_, isize>(
|
||||
context,
|
||||
&context.sql,
|
||||
"SELECT COUNT(*) FROM backup_blobs;",
|
||||
params![],
|
||||
0,
|
||||
)
|
||||
.unwrap_or_default() as usize;
|
||||
let total_files_cnt = context
|
||||
.sql
|
||||
.query_row_col::<_, isize>(context, "SELECT COUNT(*) FROM backup_blobs;", params![], 0)
|
||||
.unwrap_or_default() as usize;
|
||||
info!(
|
||||
context,
|
||||
0, "***IMPORT-in-progress: total_files_cnt={:?}", total_files_cnt,
|
||||
);
|
||||
|
||||
context
|
||||
.sql
|
||||
.query_map(
|
||||
"SELECT file_name, file_content FROM backup_blobs ORDER BY id;",
|
||||
params![],
|
||||
|row| {
|
||||
let name: String = row.get(0)?;
|
||||
let blob: Vec<u8> = row.get(1)?;
|
||||
let res = context.sql.query_map(
|
||||
"SELECT file_name, file_content FROM backup_blobs ORDER BY id;",
|
||||
params![],
|
||||
|row| {
|
||||
let name: String = row.get(0)?;
|
||||
let blob: Vec<u8> = row.get(1)?;
|
||||
|
||||
Ok((name, blob))
|
||||
},
|
||||
|files| {
|
||||
let mut loop_success = true;
|
||||
let mut processed_files_cnt = 0;
|
||||
Ok((name, blob))
|
||||
},
|
||||
|files| {
|
||||
let mut loop_success = true;
|
||||
let mut processed_files_cnt = 0;
|
||||
|
||||
for file in files {
|
||||
if file.is_err() {
|
||||
loop_success = false;
|
||||
break;
|
||||
}
|
||||
let (file_name, file_blob) = file.unwrap();
|
||||
for file in files {
|
||||
let (file_name, file_blob) = file?;
|
||||
|
||||
if context
|
||||
.running_state
|
||||
.clone()
|
||||
.read()
|
||||
.unwrap()
|
||||
.shall_stop_ongoing
|
||||
{
|
||||
loop_success = false;
|
||||
break;
|
||||
}
|
||||
processed_files_cnt += 1;
|
||||
let mut permille = processed_files_cnt * 1000 / total_files_cnt;
|
||||
if permille < 10 {
|
||||
permille = 10
|
||||
}
|
||||
if permille > 990 {
|
||||
permille = 990
|
||||
}
|
||||
context.call_cb(Event::IMEX_PROGRESS, permille as uintptr_t, 0);
|
||||
if file_blob.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
let pathNfilename = format!("{}/{}", as_str(context.get_blobdir()), file_name);
|
||||
if dc_write_file_safe(context, &pathNfilename, &file_blob) {
|
||||
continue;
|
||||
}
|
||||
|
||||
error!(
|
||||
context,
|
||||
0,
|
||||
"Storage full? Cannot write file {} with {} bytes.",
|
||||
&pathNfilename,
|
||||
file_blob.len(),
|
||||
);
|
||||
// otherwise the user may believe the stuff is imported correctly, but there are files missing ...
|
||||
if context
|
||||
.running_state
|
||||
.clone()
|
||||
.read()
|
||||
.unwrap()
|
||||
.shall_stop_ongoing
|
||||
{
|
||||
loop_success = false;
|
||||
break;
|
||||
}
|
||||
|
||||
if !loop_success {
|
||||
return Err(format_err!("fail").into());
|
||||
processed_files_cnt += 1;
|
||||
let mut permille = processed_files_cnt * 1000 / total_files_cnt;
|
||||
if permille < 10 {
|
||||
permille = 10
|
||||
}
|
||||
sql::execute(context, &context.sql, "DROP TABLE backup_blobs;", params![]);
|
||||
sql::try_execute(context, &context.sql, "VACUUM;");
|
||||
Ok(())
|
||||
},
|
||||
)
|
||||
.is_ok() as libc::c_int
|
||||
if permille > 990 {
|
||||
permille = 990
|
||||
}
|
||||
context.call_cb(Event::IMEX_PROGRESS, permille as uintptr_t, 0);
|
||||
if file_blob.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
let pathNfilename = format!("{}/{}", as_str(context.get_blobdir()), file_name);
|
||||
if dc_write_file_safe(context, &pathNfilename, &file_blob) {
|
||||
continue;
|
||||
}
|
||||
|
||||
error!(
|
||||
context,
|
||||
0,
|
||||
"Storage full? Cannot write file {} with {} bytes.",
|
||||
&pathNfilename,
|
||||
file_blob.len(),
|
||||
);
|
||||
// otherwise the user may believe the stuff is imported correctly, but there are files missing ...
|
||||
loop_success = false;
|
||||
break;
|
||||
}
|
||||
|
||||
if !loop_success {
|
||||
return Err(format_err!("fail").into());
|
||||
}
|
||||
Ok(())
|
||||
},
|
||||
);
|
||||
|
||||
res.and_then(|_| {
|
||||
sql::execute(context, &context.sql, "DROP TABLE backup_blobs;", params![])?;
|
||||
sql::try_execute(context, &context.sql, "VACUUM;")?;
|
||||
Ok(())
|
||||
})
|
||||
.is_ok() as libc::c_int
|
||||
}
|
||||
|
||||
/*******************************************************************************
|
||||
@@ -885,7 +895,8 @@ unsafe fn export_backup(context: &Context, dir: *const libc::c_char) -> libc::c_
|
||||
.format("delta-chat-%Y-%m-%d.bak")
|
||||
.to_string();
|
||||
let buffer = to_cstring(res);
|
||||
let dest_pathNfilename = dc_get_fine_pathNfilename(context, dir, buffer.as_ptr());
|
||||
let dest_pathNfilename = dc_get_fine_pathNfilename(context, dir, buffer);
|
||||
free(buffer as *mut _);
|
||||
if dest_pathNfilename.is_null() {
|
||||
error!(context, 0, "Cannot get backup file name.",);
|
||||
|
||||
@@ -912,12 +923,14 @@ unsafe fn export_backup(context: &Context, dir: *const libc::c_char) -> libc::c_
|
||||
let sql = Sql::new();
|
||||
if sql.open(context, as_path(dest_pathNfilename), 0) {
|
||||
if !sql.table_exists("backup_blobs") {
|
||||
if !sql::execute(
|
||||
if sql::execute(
|
||||
context,
|
||||
&sql,
|
||||
"CREATE TABLE backup_blobs (id INTEGER PRIMARY KEY, file_name, file_content);",
|
||||
params![],
|
||||
) {
|
||||
)
|
||||
.is_err()
|
||||
{
|
||||
/* error already logged */
|
||||
current_block = 11487273724841241105;
|
||||
} else {
|
||||
@@ -959,7 +972,7 @@ unsafe fn export_backup(context: &Context, dir: *const libc::c_char) -> libc::c_
|
||||
|
||||
sql.prepare(
|
||||
"INSERT INTO backup_blobs (file_name, file_content) VALUES (?, ?);",
|
||||
move |mut stmt| {
|
||||
move |mut stmt, _| {
|
||||
let mut processed_files_cnt = 0;
|
||||
for entry in dir_handle {
|
||||
if entry.is_err() {
|
||||
@@ -1040,12 +1053,10 @@ unsafe fn export_backup(context: &Context, dir: *const libc::c_char) -> libc::c_
|
||||
match current_block {
|
||||
11487273724841241105 => {}
|
||||
_ => {
|
||||
if 0 != sql::set_config_int(
|
||||
context,
|
||||
&sql,
|
||||
"backup_time",
|
||||
now as i32,
|
||||
) {
|
||||
if sql
|
||||
.set_config_int(context, "backup_time", now as i32)
|
||||
.is_ok()
|
||||
{
|
||||
context.call_cb(
|
||||
Event::IMEX_FILE_WRITTEN,
|
||||
dest_pathNfilename as uintptr_t,
|
||||
@@ -1084,6 +1095,7 @@ unsafe fn import_self_keys(context: &Context, dir_name: *const libc::c_char) ->
|
||||
let mut imported_cnt: libc::c_int = 0;
|
||||
let mut suffix: *mut libc::c_char = 0 as *mut libc::c_char;
|
||||
let mut path_plus_name: *mut libc::c_char = 0 as *mut libc::c_char;
|
||||
let mut name_c: *mut libc::c_char = 0 as *mut libc::c_char;
|
||||
let mut set_default: libc::c_int;
|
||||
let mut buf: *mut libc::c_char = 0 as *mut libc::c_char;
|
||||
let mut buf_bytes: size_t = 0 as size_t;
|
||||
@@ -1111,8 +1123,9 @@ unsafe fn import_self_keys(context: &Context, dir_name: *const libc::c_char) ->
|
||||
let entry = entry.unwrap();
|
||||
free(suffix as *mut libc::c_void);
|
||||
let name_f = entry.file_name();
|
||||
let name_c = to_cstring(name_f.to_string_lossy());
|
||||
suffix = dc_get_filesuffix_lc(name_c.as_ptr());
|
||||
free(name_c as *mut libc::c_void);
|
||||
name_c = to_cstring(name_f.to_string_lossy());
|
||||
suffix = dc_get_filesuffix_lc(name_c);
|
||||
if suffix.is_null()
|
||||
|| strcmp(suffix, b"asc\x00" as *const u8 as *const libc::c_char) != 0
|
||||
{
|
||||
@@ -1122,7 +1135,7 @@ unsafe fn import_self_keys(context: &Context, dir_name: *const libc::c_char) ->
|
||||
path_plus_name = dc_mprintf(
|
||||
b"%s/%s\x00" as *const u8 as *const libc::c_char,
|
||||
dir_name,
|
||||
name_c.as_ptr(),
|
||||
name_c,
|
||||
);
|
||||
info!(context, 0, "Checking: {}", as_str(path_plus_name));
|
||||
free(buf as *mut libc::c_void);
|
||||
@@ -1160,12 +1173,7 @@ unsafe fn import_self_keys(context: &Context, dir_name: *const libc::c_char) ->
|
||||
}
|
||||
}
|
||||
set_default = 1;
|
||||
if !strstr(
|
||||
name_c.as_ptr(),
|
||||
b"legacy\x00" as *const u8 as *const libc::c_char,
|
||||
)
|
||||
.is_null()
|
||||
{
|
||||
if !strstr(name_c, b"legacy\x00" as *const u8 as *const libc::c_char).is_null() {
|
||||
info!(
|
||||
context,
|
||||
0,
|
||||
@@ -1190,6 +1198,7 @@ unsafe fn import_self_keys(context: &Context, dir_name: *const libc::c_char) ->
|
||||
}
|
||||
}
|
||||
|
||||
free(name_c as *mut libc::c_void);
|
||||
free(suffix as *mut libc::c_void);
|
||||
free(path_plus_name as *mut libc::c_void);
|
||||
free(buf as *mut libc::c_void);
|
||||
|
||||
230
src/dc_job.rs
230
src/dc_job.rs
@@ -59,28 +59,15 @@ pub unsafe fn dc_perform_imap_jobs(context: &Context) {
|
||||
}
|
||||
|
||||
unsafe fn dc_job_perform(context: &Context, thread: libc::c_int, probe_network: libc::c_int) {
|
||||
let process_row = |row: &rusqlite::Row| {
|
||||
let job = dc_job_t {
|
||||
job_id: row.get(0)?,
|
||||
action: row.get(1)?,
|
||||
foreign_id: row.get(2)?,
|
||||
desired_timestamp: row.get(5)?,
|
||||
added_timestamp: row.get(4)?,
|
||||
tries: row.get(6)?,
|
||||
param: dc_param_new(),
|
||||
try_again: 0,
|
||||
pending_error: 0 as *mut libc::c_char,
|
||||
};
|
||||
|
||||
let packed: String = row.get(3)?;
|
||||
dc_param_set_packed(job.param, to_cstring(packed).as_ptr());
|
||||
Ok(job)
|
||||
};
|
||||
|
||||
let query = if probe_network == 0 {
|
||||
// processing for first-try and after backoff-timeouts:
|
||||
// process jobs in the order they were added.
|
||||
"SELECT id, action, foreign_id, param, added_timestamp, desired_timestamp, tries \
|
||||
FROM jobs WHERE thread=? AND desired_timestamp<=? ORDER BY action DESC, added_timestamp;"
|
||||
} else {
|
||||
// processing after call to dc_maybe_network():
|
||||
// process _all_ pending jobs that failed before
|
||||
// in the order of their backoff-times.
|
||||
"SELECT id, action, foreign_id, param, added_timestamp, desired_timestamp, tries \
|
||||
FROM jobs WHERE thread=? AND tries>0 ORDER BY desired_timestamp, action DESC;"
|
||||
};
|
||||
@@ -93,15 +80,42 @@ unsafe fn dc_job_perform(context: &Context, thread: libc::c_int, probe_network:
|
||||
params_probe
|
||||
};
|
||||
|
||||
let jobs: Vec<dc_job_t> = context
|
||||
.sql
|
||||
.query_map(query, params, process_row, |jobs| {
|
||||
jobs.collect::<Result<Vec<dc_job_t>, _>>()
|
||||
.map_err(Into::into)
|
||||
})
|
||||
.unwrap_or_default();
|
||||
let jobs: Result<Vec<dc_job_t>, _> = context.sql.query_map(
|
||||
query,
|
||||
params,
|
||||
|row| {
|
||||
let job = dc_job_t {
|
||||
job_id: row.get(0)?,
|
||||
action: row.get(1)?,
|
||||
foreign_id: row.get(2)?,
|
||||
desired_timestamp: row.get(5)?,
|
||||
added_timestamp: row.get(4)?,
|
||||
tries: row.get(6)?,
|
||||
param: dc_param_new(),
|
||||
try_again: 0,
|
||||
pending_error: 0 as *mut libc::c_char,
|
||||
};
|
||||
|
||||
for mut job in jobs {
|
||||
let packed: String = row.get(3)?;
|
||||
let packed_c = to_cstring(packed);
|
||||
dc_param_set_packed(job.param, packed_c);
|
||||
free(packed_c as *mut _);
|
||||
Ok(job)
|
||||
},
|
||||
|jobs| {
|
||||
let res = jobs
|
||||
.collect::<Result<Vec<dc_job_t>, _>>()
|
||||
.map_err(Into::into);
|
||||
res
|
||||
},
|
||||
);
|
||||
match jobs {
|
||||
Ok(ref res) => {}
|
||||
Err(ref err) => {
|
||||
info!(context, 0, "query failed: {:?}", err);
|
||||
}
|
||||
}
|
||||
for mut job in jobs.unwrap_or_default() {
|
||||
info!(
|
||||
context,
|
||||
0,
|
||||
@@ -111,49 +125,34 @@ unsafe fn dc_job_perform(context: &Context, thread: libc::c_int, probe_network:
|
||||
job.action,
|
||||
);
|
||||
|
||||
// some configuration jobs are "exclusive":
|
||||
// - they are always executed in the imap-thread and the smtp-thread is suspended during execution
|
||||
// - they may change the database handle change the database handle; we do not keep old pointers therefore
|
||||
// - they can be re-executed one time AT_ONCE, but they are not save in the database for later execution
|
||||
if 900 == job.action || 910 == job.action {
|
||||
dc_job_kill_action(context, job.action);
|
||||
dc_jobthread_suspend(context, &context.sentbox_thread.clone().read().unwrap(), 1);
|
||||
dc_jobthread_suspend(context, &context.mvbox_thread.clone().read().unwrap(), 1);
|
||||
dc_suspend_smtp_thread(context, 1);
|
||||
}
|
||||
|
||||
let mut tries = 0;
|
||||
while tries <= 1 {
|
||||
// this can be modified by a job using dc_job_try_again_later()
|
||||
job.try_again = 0;
|
||||
|
||||
match job.action {
|
||||
5901 => {
|
||||
dc_job_do_DC_JOB_SEND(context, &mut job);
|
||||
}
|
||||
110 => {
|
||||
dc_job_do_DC_JOB_DELETE_MSG_ON_IMAP(context, &mut job);
|
||||
}
|
||||
130 => {
|
||||
dc_job_do_DC_JOB_MARKSEEN_MSG_ON_IMAP(context, &mut job);
|
||||
}
|
||||
120 => {
|
||||
dc_job_do_DC_JOB_MARKSEEN_MDN_ON_IMAP(context, &mut job);
|
||||
}
|
||||
200 => {
|
||||
dc_job_do_DC_JOB_MOVE_MSG(context, &mut job);
|
||||
}
|
||||
5011 => {
|
||||
dc_job_do_DC_JOB_SEND(context, &mut job);
|
||||
}
|
||||
900 => {
|
||||
dc_job_do_DC_JOB_CONFIGURE_IMAP(context, &mut job);
|
||||
}
|
||||
910 => {
|
||||
dc_job_do_DC_JOB_IMEX_IMAP(context, &mut job);
|
||||
}
|
||||
5005 => {
|
||||
dc_job_do_DC_JOB_MAYBE_SEND_LOCATIONS(context, &mut job);
|
||||
}
|
||||
5007 => {
|
||||
dc_job_do_DC_JOB_MAYBE_SEND_LOC_ENDED(context, &mut job);
|
||||
}
|
||||
105 => {
|
||||
sql::housekeeping(context);
|
||||
}
|
||||
5901 => dc_job_do_DC_JOB_SEND(context, &mut job),
|
||||
110 => dc_job_do_DC_JOB_DELETE_MSG_ON_IMAP(context, &mut job),
|
||||
130 => dc_job_do_DC_JOB_MARKSEEN_MSG_ON_IMAP(context, &mut job),
|
||||
120 => dc_job_do_DC_JOB_MARKSEEN_MDN_ON_IMAP(context, &mut job),
|
||||
200 => dc_job_do_DC_JOB_MOVE_MSG(context, &mut job),
|
||||
5011 => dc_job_do_DC_JOB_SEND(context, &mut job),
|
||||
900 => dc_job_do_DC_JOB_CONFIGURE_IMAP(context, &mut job),
|
||||
910 => dc_job_do_DC_JOB_IMEX_IMAP(context, &mut job),
|
||||
5005 => dc_job_do_DC_JOB_MAYBE_SEND_LOCATIONS(context, &mut job),
|
||||
5007 => dc_job_do_DC_JOB_MAYBE_SEND_LOC_ENDED(context, &mut job),
|
||||
105 => sql::housekeeping(context),
|
||||
_ => {}
|
||||
}
|
||||
if job.try_again != -1 {
|
||||
@@ -175,6 +174,7 @@ unsafe fn dc_job_perform(context: &Context, thread: libc::c_int, probe_network:
|
||||
dc_suspend_smtp_thread(context, 0);
|
||||
break;
|
||||
} else if job.try_again == 2 {
|
||||
// just try over next loop unconditionally, the ui typically interrupts idle when the file (video) is ready
|
||||
info!(
|
||||
context,
|
||||
0,
|
||||
@@ -265,6 +265,7 @@ fn dc_job_update(context: &Context, job: &dc_job_t) -> bool {
|
||||
job.job_id as i32,
|
||||
],
|
||||
)
|
||||
.is_ok()
|
||||
}
|
||||
|
||||
unsafe fn dc_suspend_smtp_thread(context: &Context, suspend: libc::c_int) {
|
||||
@@ -290,7 +291,7 @@ unsafe fn dc_job_do_DC_JOB_SEND(context: &Context, job: &mut dc_job_t) {
|
||||
let loginparam = dc_loginparam_read(context, &context.sql, "configured_");
|
||||
let connected = context.smtp.lock().unwrap().connect(context, &loginparam);
|
||||
|
||||
if connected {
|
||||
if !connected {
|
||||
dc_job_try_again_later(job, 3i32, 0 as *const libc::c_char);
|
||||
current_block = 14216916617354591294;
|
||||
} else {
|
||||
@@ -361,14 +362,15 @@ unsafe fn dc_job_do_DC_JOB_SEND(context: &Context, job: &mut dc_job_t) {
|
||||
dc_delete_file(context, filename);
|
||||
if 0 != job.foreign_id {
|
||||
dc_update_msg_state(context, job.foreign_id, 26i32);
|
||||
let chat_id: i32 = sql::query_row(
|
||||
context,
|
||||
&context.sql,
|
||||
"SELECT chat_id FROM msgs WHERE id=?",
|
||||
params![job.foreign_id as i32],
|
||||
0,
|
||||
)
|
||||
.unwrap_or_default();
|
||||
let chat_id: i32 = context
|
||||
.sql
|
||||
.query_row_col(
|
||||
context,
|
||||
"SELECT chat_id FROM msgs WHERE id=?",
|
||||
params![job.foreign_id as i32],
|
||||
0,
|
||||
)
|
||||
.unwrap_or_default();
|
||||
context.call_cb(
|
||||
Event::MSG_DELIVERED,
|
||||
chat_id as uintptr_t,
|
||||
@@ -420,11 +422,15 @@ unsafe fn dc_job_do_DC_JOB_MOVE_MSG(context: &Context, job: &mut dc_job_t) {
|
||||
match current_block {
|
||||
2473556513754201174 => {
|
||||
if dc_msg_load_from_db(msg, context, job.foreign_id) {
|
||||
if sql::get_config_int(context, &context.sql, "folders_configured", 0) < 3 {
|
||||
if context
|
||||
.sql
|
||||
.get_config_int(context, "folders_configured")
|
||||
.unwrap_or_default()
|
||||
< 3
|
||||
{
|
||||
inbox.configure_folders(context, 0x1i32);
|
||||
}
|
||||
let dest_folder =
|
||||
sql::get_config(context, &context.sql, "configured_mvbox_folder", None);
|
||||
let dest_folder = context.sql.get_config(context, "configured_mvbox_folder");
|
||||
|
||||
if let Some(dest_folder) = dest_folder {
|
||||
let server_folder = as_str((*msg).server_folder);
|
||||
@@ -516,11 +522,15 @@ unsafe fn dc_job_do_DC_JOB_MARKSEEN_MDN_ON_IMAP(context: &Context, job: &mut dc_
|
||||
dc_job_try_again_later(job, 3i32, 0 as *const libc::c_char);
|
||||
}
|
||||
if 0 != dc_param_get_int(job.param, 'M' as i32, 0i32) {
|
||||
if sql::get_config_int(context, &context.sql, "folders_configured", 0) < 3 {
|
||||
if context
|
||||
.sql
|
||||
.get_config_int(context, "folders_configured")
|
||||
.unwrap_or_default()
|
||||
< 3
|
||||
{
|
||||
inbox.configure_folders(context, 0x1i32);
|
||||
}
|
||||
let dest_folder =
|
||||
sql::get_config(context, &context.sql, "configured_mvbox_folder", None);
|
||||
let dest_folder = context.sql.get_config(context, "configured_mvbox_folder");
|
||||
if let Some(dest_folder) = dest_folder {
|
||||
if 1 == inbox.mv(context, folder, uid, dest_folder, &mut dest_uid)
|
||||
as libc::c_uint
|
||||
@@ -565,12 +575,10 @@ unsafe fn dc_job_do_DC_JOB_MARKSEEN_MSG_ON_IMAP(context: &Context, job: &mut dc_
|
||||
}
|
||||
_ => {
|
||||
if 0 != dc_param_get_int((*msg).param, 'r' as i32, 0i32)
|
||||
&& 0 != sql::get_config_int(
|
||||
context,
|
||||
&context.sql,
|
||||
"mdns_enabled",
|
||||
1,
|
||||
)
|
||||
&& 0 != context
|
||||
.sql
|
||||
.get_config_int(context, "mdns_enabled")
|
||||
.unwrap_or_else(|| 1)
|
||||
{
|
||||
let folder =
|
||||
CStr::from_ptr((*msg).server_folder).to_str().unwrap();
|
||||
@@ -621,12 +629,10 @@ unsafe fn dc_job_do_DC_JOB_MARKSEEN_MSG_ON_IMAP(context: &Context, job: &mut dc_
|
||||
}
|
||||
_ => {
|
||||
if 0 != dc_param_get_int((*msg).param, 'r' as i32, 0i32)
|
||||
&& 0 != sql::get_config_int(
|
||||
context,
|
||||
&context.sql,
|
||||
"mdns_enabled",
|
||||
1,
|
||||
)
|
||||
&& 0 != context
|
||||
.sql
|
||||
.get_config_int(context, "mdns_enabled")
|
||||
.unwrap_or_else(|| 1)
|
||||
{
|
||||
let folder =
|
||||
CStr::from_ptr((*msg).server_folder).to_str().unwrap();
|
||||
@@ -906,6 +912,7 @@ pub fn dc_job_kill_action(context: &Context, action: libc::c_int) -> bool {
|
||||
"DELETE FROM jobs WHERE action=?;",
|
||||
params![action],
|
||||
)
|
||||
.is_ok()
|
||||
}
|
||||
|
||||
pub unsafe fn dc_perform_imap_fetch(context: &Context) {
|
||||
@@ -915,7 +922,12 @@ pub unsafe fn dc_perform_imap_fetch(context: &Context) {
|
||||
if 0 == connect_to_inbox(context, &inbox) {
|
||||
return;
|
||||
}
|
||||
if sql::get_config_int(context, &context.sql, "inbox_watch", 1) == 0 {
|
||||
if context
|
||||
.sql
|
||||
.get_config_int(context, "inbox_watch")
|
||||
.unwrap_or_else(|| 1)
|
||||
== 0
|
||||
{
|
||||
info!(context, 0, "INBOX-watch disabled.",);
|
||||
return;
|
||||
}
|
||||
@@ -951,7 +963,10 @@ pub fn dc_perform_imap_idle(context: &Context) {
|
||||
}
|
||||
|
||||
pub unsafe fn dc_perform_mvbox_fetch(context: &Context) {
|
||||
let use_network = sql::get_config_int(context, &context.sql, "mvbox_watch", 1);
|
||||
let use_network = context
|
||||
.sql
|
||||
.get_config_int(context, "mvbox_watch")
|
||||
.unwrap_or_else(|| 1);
|
||||
dc_jobthread_fetch(
|
||||
context,
|
||||
&mut context.mvbox_thread.clone().write().unwrap(),
|
||||
@@ -960,7 +975,10 @@ pub unsafe fn dc_perform_mvbox_fetch(context: &Context) {
|
||||
}
|
||||
|
||||
pub unsafe fn dc_perform_mvbox_idle(context: &Context) {
|
||||
let use_network = sql::get_config_int(context, &context.sql, "mvbox_watch", 1);
|
||||
let use_network = context
|
||||
.sql
|
||||
.get_config_int(context, "mvbox_watch")
|
||||
.unwrap_or_else(|| 1);
|
||||
|
||||
dc_jobthread_idle(
|
||||
context,
|
||||
@@ -974,7 +992,10 @@ pub unsafe fn dc_interrupt_mvbox_idle(context: &Context) {
|
||||
}
|
||||
|
||||
pub unsafe fn dc_perform_sentbox_fetch(context: &Context) {
|
||||
let use_network = sql::get_config_int(context, &context.sql, "sentbox_watch", 1);
|
||||
let use_network = context
|
||||
.sql
|
||||
.get_config_int(context, "sentbox_watch")
|
||||
.unwrap_or_else(|| 1);
|
||||
dc_jobthread_fetch(
|
||||
context,
|
||||
&mut context.sentbox_thread.clone().write().unwrap(),
|
||||
@@ -983,7 +1004,10 @@ pub unsafe fn dc_perform_sentbox_fetch(context: &Context) {
|
||||
}
|
||||
|
||||
pub unsafe fn dc_perform_sentbox_idle(context: &Context) {
|
||||
let use_network = sql::get_config_int(context, &context.sql, "sentbox_watch", 1);
|
||||
let use_network = context
|
||||
.sql
|
||||
.get_config_int(context, "sentbox_watch")
|
||||
.unwrap_or_else(|| 1);
|
||||
dc_jobthread_idle(
|
||||
context,
|
||||
&context.sentbox_thread.clone().read().unwrap(),
|
||||
@@ -1055,14 +1079,15 @@ pub unsafe fn dc_perform_smtp_idle(context: &Context) {
|
||||
}
|
||||
|
||||
unsafe fn get_next_wakeup_time(context: &Context, thread: libc::c_int) -> Duration {
|
||||
let t: i64 = sql::query_row(
|
||||
context,
|
||||
&context.sql,
|
||||
"SELECT MIN(desired_timestamp) FROM jobs WHERE thread=?;",
|
||||
params![thread],
|
||||
0,
|
||||
)
|
||||
.unwrap_or_default();
|
||||
let t: i64 = context
|
||||
.sql
|
||||
.query_row_col(
|
||||
context,
|
||||
"SELECT MIN(desired_timestamp) FROM jobs WHERE thread=?;",
|
||||
params![thread],
|
||||
0,
|
||||
)
|
||||
.unwrap_or_default();
|
||||
|
||||
let mut wakeup_time = Duration::new(10 * 60, 0);
|
||||
let now = time();
|
||||
@@ -1101,7 +1126,7 @@ pub fn dc_job_action_exists(context: &Context, action: libc::c_int) -> bool {
|
||||
|
||||
/* special case for DC_JOB_SEND_MSG_TO_SMTP */
|
||||
pub unsafe fn dc_job_send_msg(context: &Context, msg_id: uint32_t) -> libc::c_int {
|
||||
let mut success: libc::c_int = 0i32;
|
||||
let mut success = 0;
|
||||
let mut mimefactory = dc_mimefactory_t {
|
||||
from_addr: 0 as *mut libc::c_char,
|
||||
from_displayname: 0 as *mut libc::c_char,
|
||||
@@ -1238,9 +1263,10 @@ pub unsafe fn dc_job_send_msg(context: &Context, msg_id: uint32_t) -> libc::c_in
|
||||
0 as *const libc::c_char,
|
||||
0 as *const libc::c_char,
|
||||
);
|
||||
success = dc_add_smtp_job(context, 5901i32, &mut mimefactory)
|
||||
success = dc_add_smtp_job(context, 5901i32, &mut mimefactory);
|
||||
}
|
||||
}
|
||||
dc_mimefactory_empty(&mut mimefactory);
|
||||
return success;
|
||||
|
||||
success
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ use std::sync::{Arc, Condvar, Mutex};
|
||||
use crate::context::Context;
|
||||
use crate::dc_configure::*;
|
||||
use crate::imap::Imap;
|
||||
use crate::sql;
|
||||
use crate::x::*;
|
||||
|
||||
#[repr(C)]
|
||||
@@ -130,24 +129,30 @@ pub unsafe fn dc_jobthread_fetch(
|
||||
******************************************************************************/
|
||||
|
||||
unsafe fn connect_to_imap(context: &Context, jobthread: &dc_jobthread_t) -> libc::c_int {
|
||||
let mut ret_connected: libc::c_int;
|
||||
|
||||
if jobthread.imap.is_connected() {
|
||||
ret_connected = 1;
|
||||
} else {
|
||||
ret_connected = dc_connect_to_configured_imap(context, &jobthread.imap);
|
||||
if !(0 == ret_connected) {
|
||||
if sql::get_config_int(context, &context.sql, "folders_configured", 0) < 3 {
|
||||
jobthread.imap.configure_folders(context, 0x1);
|
||||
}
|
||||
let mvbox_name =
|
||||
sql::get_config(context, &context.sql, jobthread.folder_config_name, None);
|
||||
if let Some(name) = mvbox_name {
|
||||
jobthread.imap.set_watch_folder(name);
|
||||
} else {
|
||||
jobthread.imap.disconnect(context);
|
||||
ret_connected = 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
let mut ret_connected = dc_connect_to_configured_imap(context, &jobthread.imap);
|
||||
|
||||
if !(0 == ret_connected) {
|
||||
if context
|
||||
.sql
|
||||
.get_config_int(context, "folders_configured")
|
||||
.unwrap_or_default()
|
||||
< 3
|
||||
{
|
||||
jobthread.imap.configure_folders(context, 0x1);
|
||||
}
|
||||
|
||||
if let Some(mvbox_name) = context
|
||||
.sql
|
||||
.get_config(context, jobthread.folder_config_name)
|
||||
{
|
||||
jobthread.imap.set_watch_folder(mvbox_name);
|
||||
} else {
|
||||
jobthread.imap.disconnect(context);
|
||||
ret_connected = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -64,7 +64,9 @@ pub unsafe fn dc_send_locations_to_chat(
|
||||
},
|
||||
chat_id as i32,
|
||||
],
|
||||
) {
|
||||
)
|
||||
.is_ok()
|
||||
{
|
||||
if 0 != seconds && !is_sending_locations_before {
|
||||
msg = dc_msg_new(context, 10i32);
|
||||
(*msg).text = dc_stock_system_msg(
|
||||
@@ -213,8 +215,10 @@ pub fn dc_get_locations(
|
||||
if 0 != (*loc).msg_id {
|
||||
let txt: String = row.get(9)?;
|
||||
let txt_c = to_cstring(txt);
|
||||
if 0 != is_marker(txt_c.as_ptr()) {
|
||||
(*loc).marker = strdup(txt_c.as_ptr());
|
||||
if 0 != is_marker(txt_c) {
|
||||
(*loc).marker = txt_c;
|
||||
} else {
|
||||
free(txt_c as *mut _);
|
||||
}
|
||||
}
|
||||
Ok(loc)
|
||||
@@ -244,7 +248,7 @@ unsafe fn is_marker(txt: *const libc::c_char) -> libc::c_int {
|
||||
}
|
||||
|
||||
pub fn dc_delete_all_locations(context: &Context) -> bool {
|
||||
if !sql::execute(context, &context.sql, "DELETE FROM locations;", params![]) {
|
||||
if sql::execute(context, &context.sql, "DELETE FROM locations;", params![]).is_err() {
|
||||
return false;
|
||||
}
|
||||
context.call_cb(Event::LOCATION_CHANGED, 0, 0);
|
||||
@@ -261,13 +265,10 @@ pub fn dc_get_location_kml(
|
||||
let mut location_count: libc::c_int = 0;
|
||||
let mut ret = String::new();
|
||||
|
||||
let self_addr = sql::get_config(context, &context.sql, "configured_addr", Some(""));
|
||||
|
||||
if self_addr.is_none() {
|
||||
return std::ptr::null_mut();
|
||||
}
|
||||
|
||||
let self_addr = self_addr.unwrap();
|
||||
let self_addr = context
|
||||
.sql
|
||||
.get_config(context, "configured_addr")
|
||||
.unwrap_or_default();
|
||||
|
||||
if let Ok((locations_send_begin, locations_send_until, locations_last_sent)) = context.sql.query_row(
|
||||
"SELECT locations_send_begin, locations_send_until, locations_last_sent FROM chats WHERE id=?;",
|
||||
@@ -331,9 +332,9 @@ pub fn dc_get_location_kml(
|
||||
}
|
||||
|
||||
if 0 != success {
|
||||
unsafe { strdup(to_cstring(ret).as_ptr()) }
|
||||
unsafe { to_cstring(ret) }
|
||||
} else {
|
||||
0 as *mut libc::c_char
|
||||
std::ptr::null_mut()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -345,7 +346,7 @@ unsafe fn get_kml_timestamp(utc: i64) -> *mut libc::c_char {
|
||||
let res = chrono::NaiveDateTime::from_timestamp(utc, 0)
|
||||
.format("%Y-%m-%dT%H:%M:%SZ")
|
||||
.to_string();
|
||||
strdup(to_cstring(res).as_ptr())
|
||||
to_cstring(res)
|
||||
}
|
||||
|
||||
pub unsafe fn dc_get_message_kml(
|
||||
@@ -386,6 +387,7 @@ pub fn dc_set_kml_sent_timestamp(context: &Context, chat_id: u32, timestamp: i64
|
||||
"UPDATE chats SET locations_last_sent=? WHERE id=?;",
|
||||
params![timestamp, chat_id as i32],
|
||||
)
|
||||
.is_ok()
|
||||
}
|
||||
|
||||
pub fn dc_set_msg_location_id(context: &Context, msg_id: u32, location_id: u32) -> bool {
|
||||
@@ -395,6 +397,7 @@ pub fn dc_set_msg_location_id(context: &Context, msg_id: u32, location_id: u32)
|
||||
"UPDATE msgs SET location_id=? WHERE id=?;",
|
||||
params![location_id, msg_id as i32],
|
||||
)
|
||||
.is_ok()
|
||||
}
|
||||
|
||||
pub unsafe fn dc_save_locations(
|
||||
@@ -660,7 +663,7 @@ pub unsafe fn dc_job_do_DC_JOB_MAYBE_SEND_LOCATIONS(context: &Context, _job: *mu
|
||||
AND timestamp>? \
|
||||
AND independent=0 \
|
||||
ORDER BY timestamp;",
|
||||
|mut stmt_locations| {
|
||||
|mut stmt_locations, _| {
|
||||
for (chat_id, locations_send_begin, locations_last_sent) in
|
||||
rows.filter_map(|r| match r {
|
||||
Ok(Some(v)) => Some(v),
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use std::borrow::Cow;
|
||||
|
||||
use crate::context::Context;
|
||||
use crate::sql::{self, Sql};
|
||||
use crate::sql::Sql;
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
pub struct dc_loginparam_t {
|
||||
@@ -35,37 +35,38 @@ pub fn dc_loginparam_read(
|
||||
let prefix = prefix.as_ref();
|
||||
|
||||
let key = format!("{}addr", prefix);
|
||||
let addr = sql::get_config(context, sql, key, None)
|
||||
let addr = sql
|
||||
.get_config(context, key)
|
||||
.unwrap_or_default()
|
||||
.trim()
|
||||
.to_string();
|
||||
|
||||
let key = format!("{}mail_server", prefix);
|
||||
let mail_server = sql::get_config(context, sql, key, None).unwrap_or_default();
|
||||
let mail_server = sql.get_config(context, key).unwrap_or_default();
|
||||
|
||||
let key = format!("{}mail_port", prefix);
|
||||
let mail_port = sql::get_config_int(context, sql, key, 0);
|
||||
let mail_port = sql.get_config_int(context, key).unwrap_or_default();
|
||||
|
||||
let key = format!("{}mail_user", prefix);
|
||||
let mail_user = sql::get_config(context, sql, key, None).unwrap_or_default();
|
||||
let mail_user = sql.get_config(context, key).unwrap_or_default();
|
||||
|
||||
let key = format!("{}mail_pw", prefix);
|
||||
let mail_pw = sql::get_config(context, sql, key, None).unwrap_or_default();
|
||||
let mail_pw = sql.get_config(context, key).unwrap_or_default();
|
||||
|
||||
let key = format!("{}send_server", prefix);
|
||||
let send_server = sql::get_config(context, sql, key, None).unwrap_or_default();
|
||||
let send_server = sql.get_config(context, key).unwrap_or_default();
|
||||
|
||||
let key = format!("{}send_port", prefix);
|
||||
let send_port = sql::get_config_int(context, sql, key, 0);
|
||||
let send_port = sql.get_config_int(context, key).unwrap_or_default();
|
||||
|
||||
let key = format!("{}send_user", prefix);
|
||||
let send_user = sql::get_config(context, sql, key, None).unwrap_or_default();
|
||||
let send_user = sql.get_config(context, key).unwrap_or_default();
|
||||
|
||||
let key = format!("{}send_pw", prefix);
|
||||
let send_pw = sql::get_config(context, sql, key, None).unwrap_or_default();
|
||||
let send_pw = sql.get_config(context, key).unwrap_or_default();
|
||||
|
||||
let key = format!("{}server_flags", prefix);
|
||||
let server_flags = sql::get_config_int(context, sql, key, 0);
|
||||
let server_flags = sql.get_config_int(context, key).unwrap_or_default();
|
||||
|
||||
dc_loginparam_t {
|
||||
addr: addr.to_string(),
|
||||
@@ -90,34 +91,34 @@ pub fn dc_loginparam_write(
|
||||
let prefix = prefix.as_ref();
|
||||
|
||||
let key = format!("{}addr", prefix);
|
||||
sql::set_config(context, sql, key, Some(&loginparam.addr));
|
||||
sql.set_config(context, key, Some(&loginparam.addr));
|
||||
|
||||
let key = format!("{}mail_server", prefix);
|
||||
sql::set_config(context, sql, key, Some(&loginparam.mail_server));
|
||||
sql.set_config(context, key, Some(&loginparam.mail_server));
|
||||
|
||||
let key = format!("{}mail_port", prefix);
|
||||
sql::set_config_int(context, sql, key, loginparam.mail_port);
|
||||
sql.set_config_int(context, key, loginparam.mail_port);
|
||||
|
||||
let key = format!("{}mail_user", prefix);
|
||||
sql::set_config(context, sql, key, Some(&loginparam.mail_user));
|
||||
sql.set_config(context, key, Some(&loginparam.mail_user));
|
||||
|
||||
let key = format!("{}mail_pw", prefix);
|
||||
sql::set_config(context, sql, key, Some(&loginparam.mail_pw));
|
||||
sql.set_config(context, key, Some(&loginparam.mail_pw));
|
||||
|
||||
let key = format!("{}send_server", prefix);
|
||||
sql::set_config(context, sql, key, Some(&loginparam.send_server));
|
||||
sql.set_config(context, key, Some(&loginparam.send_server));
|
||||
|
||||
let key = format!("{}send_port", prefix);
|
||||
sql::set_config_int(context, sql, key, loginparam.send_port);
|
||||
sql.set_config_int(context, key, loginparam.send_port);
|
||||
|
||||
let key = format!("{}send_user", prefix);
|
||||
sql::set_config(context, sql, key, Some(&loginparam.send_user));
|
||||
sql.set_config(context, key, Some(&loginparam.send_user));
|
||||
|
||||
let key = format!("{}send_pw", prefix);
|
||||
sql::set_config(context, sql, key, Some(&loginparam.send_pw));
|
||||
sql.set_config(context, key, Some(&loginparam.send_pw));
|
||||
|
||||
let key = format!("{}server_flags", prefix);
|
||||
sql::set_config_int(context, sql, key, loginparam.server_flags);
|
||||
sql.set_config_int(context, key, loginparam.server_flags);
|
||||
}
|
||||
|
||||
fn unset_empty(s: &String) -> Cow<String> {
|
||||
|
||||
@@ -8,7 +8,7 @@ use mmime::mailmime_write_mem::*;
|
||||
use mmime::mmapstring::*;
|
||||
use mmime::other::*;
|
||||
|
||||
use crate::constants::DC_VERSION_STR;
|
||||
use crate::constants::*;
|
||||
use crate::context::Context;
|
||||
use crate::dc_chat::*;
|
||||
use crate::dc_contact::*;
|
||||
@@ -19,7 +19,6 @@ use crate::dc_param::*;
|
||||
use crate::dc_stock::*;
|
||||
use crate::dc_strencode::*;
|
||||
use crate::dc_tools::*;
|
||||
use crate::sql;
|
||||
use crate::types::*;
|
||||
use crate::x::*;
|
||||
|
||||
@@ -111,6 +110,7 @@ pub unsafe fn dc_mimefactory_load_msg(
|
||||
msg_id: uint32_t,
|
||||
) -> libc::c_int {
|
||||
if factory.is_null() || msg_id <= 9 || !(*factory).msg.is_null() {
|
||||
info!((*factory).context, 0, "mimefactory: null");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -125,9 +125,12 @@ pub unsafe fn dc_mimefactory_load_msg(
|
||||
if dc_msg_load_from_db((*factory).msg, context, msg_id)
|
||||
&& dc_chat_load_from_db((*factory).chat, (*(*factory).msg).chat_id)
|
||||
{
|
||||
info!(context, 0, "mimefactory: loaded msg and chat",);
|
||||
load_from(factory);
|
||||
(*factory).req_mdn = 0;
|
||||
if 0 != dc_chat_is_self_talk((*factory).chat) {
|
||||
info!(context, 0, "mimefactory: selftalk");
|
||||
|
||||
clist_insert_after(
|
||||
(*factory).recipients_names,
|
||||
(*(*factory).recipients_names).last,
|
||||
@@ -139,6 +142,7 @@ pub unsafe fn dc_mimefactory_load_msg(
|
||||
dc_strdup((*factory).from_addr) as *mut libc::c_void,
|
||||
);
|
||||
} else {
|
||||
info!(context, 0, "mimefactory: query map");
|
||||
context
|
||||
.sql
|
||||
.query_map(
|
||||
@@ -153,27 +157,24 @@ pub unsafe fn dc_mimefactory_load_msg(
|
||||
Ok((authname, addr))
|
||||
},
|
||||
|rows| {
|
||||
info!(context, 0, "mimefactory: processing rows");
|
||||
for row in rows {
|
||||
let (authname, addr) = row?;
|
||||
let addr_c = to_cstring(addr);
|
||||
if clist_search_string_nocase(
|
||||
(*factory).recipients_addr,
|
||||
addr_c.as_ptr(),
|
||||
) == 0
|
||||
{
|
||||
if clist_search_string_nocase((*factory).recipients_addr, addr_c) == 0 {
|
||||
clist_insert_after(
|
||||
(*factory).recipients_names,
|
||||
(*(*factory).recipients_names).last,
|
||||
if !authname.is_empty() {
|
||||
dc_strdup(to_cstring(authname).as_ptr())
|
||||
to_cstring(authname)
|
||||
} else {
|
||||
0 as *mut libc::c_char
|
||||
std::ptr::null_mut()
|
||||
} as *mut libc::c_void,
|
||||
);
|
||||
clist_insert_after(
|
||||
(*factory).recipients_addr,
|
||||
(*(*factory).recipients_addr).last,
|
||||
dc_strdup(addr_c.as_ptr()) as *mut libc::c_void,
|
||||
addr_c as *mut libc::c_void,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -190,7 +191,9 @@ pub unsafe fn dc_mimefactory_load_msg(
|
||||
0 as *const libc::c_char,
|
||||
);
|
||||
let email_to_remove = to_string(email_to_remove_c);
|
||||
let self_addr = sql::get_config(context, &context.sql, "configured_addr", Some(""))
|
||||
let self_addr = context
|
||||
.sql
|
||||
.get_config(context, "configured_addr")
|
||||
.unwrap_or_default();
|
||||
|
||||
if !email_to_remove.is_empty() && email_to_remove != self_addr {
|
||||
@@ -212,11 +215,15 @@ pub unsafe fn dc_mimefactory_load_msg(
|
||||
}
|
||||
if command != 6
|
||||
&& command != 7
|
||||
&& 0 != sql::get_config_int(context, &context.sql, "mdns_enabled", 1)
|
||||
&& 0 != context
|
||||
.sql
|
||||
.get_config_int(context, "mdns_enabled")
|
||||
.unwrap_or_else(|| 1)
|
||||
{
|
||||
(*factory).req_mdn = 1
|
||||
}
|
||||
}
|
||||
info!(context, 0, "mimefactory: loading in reply to");
|
||||
|
||||
let row = context.sql.query_row(
|
||||
"SELECT mime_in_reply_to, mime_references FROM msgs WHERE id=?",
|
||||
@@ -228,9 +235,17 @@ pub unsafe fn dc_mimefactory_load_msg(
|
||||
Ok((in_reply_to, references))
|
||||
},
|
||||
);
|
||||
if let Ok((in_reply_to, references)) = row {
|
||||
(*factory).in_reply_to = dc_strdup(to_cstring(in_reply_to).as_ptr());
|
||||
(*factory).references = dc_strdup(to_cstring(references).as_ptr());
|
||||
match row {
|
||||
Ok((in_reply_to, references)) => {
|
||||
(*factory).in_reply_to = to_cstring(in_reply_to);
|
||||
(*factory).references = to_cstring(references);
|
||||
}
|
||||
Err(err) => {
|
||||
error!(
|
||||
context,
|
||||
0, "mimefactory: failed to load mime_in_reply_to: {:?}", err
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
success = 1;
|
||||
@@ -246,41 +261,25 @@ pub unsafe fn dc_mimefactory_load_msg(
|
||||
}
|
||||
|
||||
unsafe fn load_from(mut factory: *mut dc_mimefactory_t) {
|
||||
(*factory).from_addr = strdup(
|
||||
to_cstring(
|
||||
sql::get_config(
|
||||
(*factory).context,
|
||||
&(*factory).context.sql,
|
||||
"configured_addr",
|
||||
None,
|
||||
)
|
||||
let context = (*factory).context;
|
||||
(*factory).from_addr = to_cstring(
|
||||
context
|
||||
.sql
|
||||
.get_config(context, "configured_addr")
|
||||
.unwrap_or_default(),
|
||||
)
|
||||
.as_ptr(),
|
||||
);
|
||||
(*factory).from_displayname = strdup(
|
||||
to_cstring(
|
||||
sql::get_config(
|
||||
(*factory).context,
|
||||
&(*factory).context.sql,
|
||||
"displayname",
|
||||
None,
|
||||
)
|
||||
|
||||
(*factory).from_displayname = to_cstring(
|
||||
context
|
||||
.sql
|
||||
.get_config(context, "displayname")
|
||||
.unwrap_or_default(),
|
||||
)
|
||||
.as_ptr(),
|
||||
);
|
||||
(*factory).selfstatus = strdup(
|
||||
to_cstring(
|
||||
sql::get_config(
|
||||
(*factory).context,
|
||||
&(*factory).context.sql,
|
||||
"selfstatus",
|
||||
None,
|
||||
)
|
||||
(*factory).selfstatus = to_cstring(
|
||||
context
|
||||
.sql
|
||||
.get_config(context, "selfstatus")
|
||||
.unwrap_or_default(),
|
||||
)
|
||||
.as_ptr(),
|
||||
);
|
||||
if (*factory).selfstatus.is_null() {
|
||||
(*factory).selfstatus = dc_stock_str((*factory).context, 13)
|
||||
@@ -301,12 +300,12 @@ pub unsafe fn dc_mimefactory_load_mdn(
|
||||
(*factory).recipients_names = clist_new();
|
||||
(*factory).recipients_addr = clist_new();
|
||||
(*factory).msg = dc_msg_new_untyped((*factory).context);
|
||||
if 0 != sql::get_config_int(
|
||||
(*factory).context,
|
||||
&(*factory).context.sql,
|
||||
"mdns_enabled",
|
||||
1,
|
||||
) {
|
||||
if 0 != (*factory)
|
||||
.context
|
||||
.sql
|
||||
.get_config_int((*factory).context, "mdns_enabled")
|
||||
.unwrap_or_else(|| 1)
|
||||
{
|
||||
// MDNs not enabled - check this is late, in the job. the use may have changed its choice while offline ...
|
||||
contact = dc_contact_new((*factory).context);
|
||||
if !(!dc_msg_load_from_db((*factory).msg, (*factory).context, msg_id)
|
||||
@@ -528,7 +527,7 @@ pub unsafe fn dc_mimefactory_render(mut factory: *mut dc_mimefactory_t) -> libc:
|
||||
let msg: *mut dc_msg_t = (*factory).msg;
|
||||
let mut meta_part: *mut mailmime = 0 as *mut mailmime;
|
||||
let mut placeholdertext: *mut libc::c_char = 0 as *mut libc::c_char;
|
||||
if (*chat).type_0 == 130 {
|
||||
if (*chat).type_0 == DC_CHAT_TYPE_VERIFIED_GROUP as libc::c_int {
|
||||
mailimf_fields_add(
|
||||
imf_fields,
|
||||
mailimf_field_new_custom(
|
||||
@@ -552,7 +551,9 @@ pub unsafe fn dc_mimefactory_render(mut factory: *mut dc_mimefactory_t) -> libc:
|
||||
}
|
||||
/* build header etc. */
|
||||
let command: libc::c_int = dc_param_get_int((*msg).param, 'S' as i32, 0);
|
||||
if (*chat).type_0 == 120 || (*chat).type_0 == 130 {
|
||||
if (*chat).type_0 == DC_CHAT_TYPE_GROUP as libc::c_int
|
||||
|| (*chat).type_0 == DC_CHAT_TYPE_VERIFIED_GROUP as libc::c_int
|
||||
{
|
||||
mailimf_fields_add(
|
||||
imf_fields,
|
||||
mailimf_field_new_custom(
|
||||
@@ -739,7 +740,7 @@ pub unsafe fn dc_mimefactory_render(mut factory: *mut dc_mimefactory_t) -> libc:
|
||||
}
|
||||
if !grpimage.is_null() {
|
||||
let mut meta: *mut dc_msg_t = dc_msg_new_untyped((*factory).context);
|
||||
(*meta).type_0 = 20;
|
||||
(*meta).type_0 = DC_MSG_IMAGE as libc::c_int;
|
||||
dc_param_set((*meta).param, 'f' as i32, grpimage);
|
||||
let mut filename_as_sent: *mut libc::c_char = 0 as *mut libc::c_char;
|
||||
meta_part = build_body_file(
|
||||
@@ -758,8 +759,11 @@ pub unsafe fn dc_mimefactory_render(mut factory: *mut dc_mimefactory_t) -> libc:
|
||||
}
|
||||
dc_msg_unref(meta);
|
||||
}
|
||||
if (*msg).type_0 == 41 || (*msg).type_0 == 40 || (*msg).type_0 == 50 {
|
||||
if (*msg).type_0 == 41 {
|
||||
if (*msg).type_0 == DC_MSG_VOICE as libc::c_int
|
||||
|| (*msg).type_0 == DC_MSG_AUDIO as libc::c_int
|
||||
|| (*msg).type_0 == DC_MSG_VIDEO as libc::c_int
|
||||
{
|
||||
if (*msg).type_0 == DC_MSG_VOICE as libc::c_int {
|
||||
mailimf_fields_add(
|
||||
imf_fields,
|
||||
mailimf_field_new_custom(
|
||||
@@ -834,12 +838,12 @@ pub unsafe fn dc_mimefactory_render(mut factory: *mut dc_mimefactory_t) -> libc:
|
||||
free(fwdhint as *mut libc::c_void);
|
||||
free(placeholdertext as *mut libc::c_void);
|
||||
/* add attachment part */
|
||||
if (*msg).type_0 == 20
|
||||
|| (*msg).type_0 == 21
|
||||
|| (*msg).type_0 == 40
|
||||
|| (*msg).type_0 == 41
|
||||
|| (*msg).type_0 == 50
|
||||
|| (*msg).type_0 == 60
|
||||
if (*msg).type_0 == DC_MSG_IMAGE as libc::c_int
|
||||
|| (*msg).type_0 == DC_MSG_GIF as libc::c_int
|
||||
|| (*msg).type_0 == DC_MSG_AUDIO as libc::c_int
|
||||
|| (*msg).type_0 == DC_MSG_VOICE as libc::c_int
|
||||
|| (*msg).type_0 == DC_MSG_VIDEO as libc::c_int
|
||||
|| (*msg).type_0 == DC_MSG_FILE as libc::c_int
|
||||
{
|
||||
if 0 == is_file_size_okay(msg) {
|
||||
let error: *mut libc::c_char = dc_mprintf(
|
||||
@@ -1092,7 +1096,9 @@ unsafe fn get_subject(
|
||||
};
|
||||
if dc_param_get_int((*msg).param, 'S' as i32, 0) == 6 {
|
||||
ret = dc_stock_str(context, 42)
|
||||
} else if (*chat).type_0 == 120 || (*chat).type_0 == 130 {
|
||||
} else if (*chat).type_0 == DC_CHAT_TYPE_GROUP as libc::c_int
|
||||
|| (*chat).type_0 == DC_CHAT_TYPE_VERIFIED_GROUP as libc::c_int
|
||||
{
|
||||
ret = dc_mprintf(
|
||||
b"Chat: %s: %s%s\x00" as *const u8 as *const libc::c_char,
|
||||
(*chat).name,
|
||||
@@ -1156,7 +1162,7 @@ unsafe fn build_body_file(
|
||||
let mut filename_to_send: *mut libc::c_char = 0 as *mut libc::c_char;
|
||||
let mut filename_encoded: *mut libc::c_char = 0 as *mut libc::c_char;
|
||||
if !pathNfilename.is_null() {
|
||||
if (*msg).type_0 == 41 {
|
||||
if (*msg).type_0 == DC_MSG_VOICE as libc::c_int {
|
||||
let ts = chrono::Utc.timestamp((*msg).timestamp_sort as i64, 0);
|
||||
|
||||
let suffix = if !suffix.is_null() {
|
||||
@@ -1167,10 +1173,12 @@ unsafe fn build_body_file(
|
||||
let res = ts
|
||||
.format(&format!("voice-message_%Y-%m-%d_%H-%M-%S.{}", suffix))
|
||||
.to_string();
|
||||
filename_to_send = strdup(to_cstring(res).as_ptr());
|
||||
} else if (*msg).type_0 == 40 {
|
||||
filename_to_send = to_cstring(res);
|
||||
} else if (*msg).type_0 == DC_MSG_AUDIO as libc::c_int {
|
||||
filename_to_send = dc_get_filename(pathNfilename)
|
||||
} else if (*msg).type_0 == 20 || (*msg).type_0 == 21 {
|
||||
} else if (*msg).type_0 == DC_MSG_IMAGE as libc::c_int
|
||||
|| (*msg).type_0 == DC_MSG_GIF as libc::c_int
|
||||
{
|
||||
if base_name.is_null() {
|
||||
base_name = b"image\x00" as *const u8 as *const libc::c_char
|
||||
}
|
||||
@@ -1183,7 +1191,7 @@ unsafe fn build_body_file(
|
||||
b"dat\x00" as *const u8 as *const libc::c_char
|
||||
},
|
||||
)
|
||||
} else if (*msg).type_0 == 50 {
|
||||
} else if (*msg).type_0 == DC_MSG_VIDEO as libc::c_int {
|
||||
filename_to_send = dc_mprintf(
|
||||
b"video.%s\x00" as *const u8 as *const libc::c_char,
|
||||
if !suffix.is_null() {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::ffi::{CStr, CString};
|
||||
use std::ffi::CStr;
|
||||
|
||||
use charset::Charset;
|
||||
use mmime::mailimf::*;
|
||||
@@ -833,6 +833,7 @@ unsafe fn hash_header(
|
||||
18 => key = b"References\x00" as *const u8 as *const libc::c_char,
|
||||
19 => key = b"Subject\x00" as *const u8 as *const libc::c_char,
|
||||
22 => {
|
||||
// MAILIMF_FIELD_OPTIONAL_FIELD
|
||||
let optional_field: *const mailimf_optional_field =
|
||||
(*field).fld_data.fld_optional_field;
|
||||
if !optional_field.is_null() {
|
||||
@@ -842,17 +843,16 @@ unsafe fn hash_header(
|
||||
_ => {}
|
||||
}
|
||||
if !key.is_null() {
|
||||
let key_len: libc::c_int = strlen(key) as libc::c_int;
|
||||
if out.contains_key(as_str(key)) {
|
||||
if (*field).fld_type != MAILIMF_FIELD_OPTIONAL_FIELD as libc::c_int
|
||||
|| key_len > 5i32
|
||||
&& strncasecmp(key, b"Chat-\x00" as *const u8 as *const libc::c_char, 5)
|
||||
== 0i32
|
||||
{
|
||||
out.insert(to_string(key), field);
|
||||
}
|
||||
} else {
|
||||
out.insert(to_string(key), field);
|
||||
// XXX the optional field sometimes contains invalid UTF8
|
||||
// which should not happen (according to the mime standard).
|
||||
// This might point to a bug in our mime parsing/processing
|
||||
// logic. As mmime/dc_mimeparser is scheduled fore replacement
|
||||
// anyway we just use a lossy conversion.
|
||||
let key_r = &to_string_lossy(key);
|
||||
if !out.contains_key(key_r) || // key already exists, only overwrite known types (protected headers)
|
||||
(*field).fld_type != MAILIMF_FIELD_OPTIONAL_FIELD as i32 || key_r.starts_with("Chat-")
|
||||
{
|
||||
out.insert(key_r.to_string(), field);
|
||||
}
|
||||
}
|
||||
cur1 = if !cur1.is_null() {
|
||||
@@ -1204,8 +1204,7 @@ unsafe fn dc_mimeparser_add_single_part_if_known(
|
||||
current_block = 8795901732489102124;
|
||||
} else {
|
||||
decoded_data_bytes = res.len();
|
||||
let res_c = CString::new(res.as_bytes()).unwrap();
|
||||
decoded_data = strdup(res_c.as_ptr());
|
||||
decoded_data = res.as_ptr() as *const libc::c_char;
|
||||
current_block = 17788412896529399552;
|
||||
}
|
||||
} else {
|
||||
@@ -1341,8 +1340,9 @@ unsafe fn dc_mimeparser_add_single_part_if_known(
|
||||
}
|
||||
if !filename_parts.is_empty() {
|
||||
free(desired_filename as *mut libc::c_void);
|
||||
desired_filename =
|
||||
dc_decode_ext_header(to_cstring(filename_parts).as_ptr());
|
||||
let parts_c = to_cstring(filename_parts);
|
||||
desired_filename = dc_decode_ext_header(parts_c);
|
||||
free(parts_c as *mut _);
|
||||
}
|
||||
if desired_filename.is_null() {
|
||||
let param = mailmime_find_ct_parameter(
|
||||
@@ -1440,7 +1440,7 @@ unsafe fn dc_mimeparser_add_single_part_if_known(
|
||||
}
|
||||
}
|
||||
}
|
||||
/* add object? (we do not add all objetcs, eg. signatures etc. are ignored) */
|
||||
/* add object? (we do not add all objects, eg. signatures etc. are ignored) */
|
||||
dc_simplify_unref(simplifier);
|
||||
if !transfer_decoding_buffer.is_null() {
|
||||
mmap_string_unref(transfer_decoding_buffer);
|
||||
|
||||
@@ -2,10 +2,14 @@ use crate::constants::*;
|
||||
use crate::context::*;
|
||||
use crate::dc_job::*;
|
||||
use crate::dc_msg::*;
|
||||
use crate::sql;
|
||||
|
||||
pub unsafe fn dc_do_heuristics_moves(context: &Context, folder: &str, msg_id: u32) {
|
||||
if sql::get_config_int(context, &context.sql, "mvbox_move", 1) == 0 {
|
||||
if context
|
||||
.sql
|
||||
.get_config_int(context, "mvbox_move")
|
||||
.unwrap_or_else(|| 1)
|
||||
== 0
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
157
src/dc_msg.rs
157
src/dc_msg.rs
@@ -52,9 +52,8 @@ pub unsafe fn dc_get_msg_info(context: &Context, msg_id: u32) -> *mut libc::c_ch
|
||||
dc_msg_load_from_db(msg, context, msg_id);
|
||||
dc_contact_load_from_db(contact_from, &context.sql, (*msg).from_id);
|
||||
|
||||
let rawtxt: Option<String> = sql::query_row(
|
||||
let rawtxt: Option<String> = context.sql.query_row_col(
|
||||
context,
|
||||
&context.sql,
|
||||
"SELECT txt_raw FROM msgs WHERE id=?;",
|
||||
params![msg_id as i32],
|
||||
0,
|
||||
@@ -64,7 +63,7 @@ pub unsafe fn dc_get_msg_info(context: &Context, msg_id: u32) -> *mut libc::c_ch
|
||||
ret += &format!("Cannot load message #{}.", msg_id as usize);
|
||||
dc_msg_unref(msg);
|
||||
dc_contact_unref(contact_from);
|
||||
return strdup(to_cstring(ret).as_ptr());
|
||||
return to_cstring(ret);
|
||||
}
|
||||
let rawtxt = rawtxt.unwrap();
|
||||
let rawtxt = dc_truncate_str(rawtxt.trim(), 100000);
|
||||
@@ -92,7 +91,7 @@ pub unsafe fn dc_get_msg_info(context: &Context, msg_id: u32) -> *mut libc::c_ch
|
||||
// device-internal message, no further details needed
|
||||
dc_msg_unref(msg);
|
||||
dc_contact_unref(contact_from);
|
||||
return strdup(to_cstring(ret).as_ptr());
|
||||
return to_cstring(ret);
|
||||
}
|
||||
|
||||
context
|
||||
@@ -210,7 +209,7 @@ pub unsafe fn dc_get_msg_info(context: &Context, msg_id: u32) -> *mut libc::c_ch
|
||||
|
||||
dc_msg_unref(msg);
|
||||
dc_contact_unref(contact_from);
|
||||
strdup(to_cstring(ret).as_ptr())
|
||||
to_cstring(ret)
|
||||
}
|
||||
|
||||
pub unsafe fn dc_msg_new_untyped<'a>(context: &'a Context) -> *mut dc_msg_t<'a> {
|
||||
@@ -225,8 +224,8 @@ pub unsafe fn dc_msg_new_untyped<'a>(context: &'a Context) -> *mut dc_msg_t<'a>
|
||||
* If you want an update, you have to recreate the object.
|
||||
*/
|
||||
// to check if a mail was sent, use dc_msg_is_sent()
|
||||
// approx. max. lenght returned by dc_msg_get_text()
|
||||
// approx. max. lenght returned by dc_get_msg_info()
|
||||
// approx. max. length returned by dc_msg_get_text()
|
||||
// approx. max. length returned by dc_get_msg_info()
|
||||
pub unsafe fn dc_msg_new<'a>(context: &'a Context, viewtype: libc::c_int) -> *mut dc_msg_t<'a> {
|
||||
let mut msg: *mut dc_msg_t;
|
||||
msg = calloc(1, ::std::mem::size_of::<dc_msg_t>()) as *mut dc_msg_t;
|
||||
@@ -428,7 +427,11 @@ pub unsafe fn dc_msg_get_timestamp(msg: *const dc_msg_t) -> i64 {
|
||||
}
|
||||
|
||||
pub fn dc_msg_load_from_db<'a>(msg: *mut dc_msg_t<'a>, context: &'a Context, id: u32) -> bool {
|
||||
context.sql.query_row(
|
||||
if msg.is_null() {
|
||||
return false;
|
||||
}
|
||||
|
||||
let res = context.sql.query_row(
|
||||
"SELECT \
|
||||
m.id,rfc724_mid,m.mime_in_reply_to,m.server_folder,m.server_uid,m.move_state,m.chat_id, \
|
||||
m.from_id,m.to_id,m.timestamp,m.timestamp_sent,m.timestamp_rcvd, m.type,m.state,m.msgrmsg,m.txt, \
|
||||
@@ -442,9 +445,12 @@ pub fn dc_msg_load_from_db<'a>(msg: *mut dc_msg_t<'a>, context: &'a Context, id:
|
||||
dc_msg_empty(msg);
|
||||
|
||||
(*msg).id = row.get::<_, i32>(0)? as u32;
|
||||
(*msg).rfc724_mid = dc_strdup(to_cstring(row.get::<_, String>(1)?).as_ptr());
|
||||
(*msg).in_reply_to = dc_strdup(to_cstring(row.get::<_, String>(2)?).as_ptr());
|
||||
(*msg).server_folder = dc_strdup(to_cstring(row.get::<_, String>(3)?).as_ptr());
|
||||
(*msg).rfc724_mid = to_cstring(row.get::<_, String>(1)?);
|
||||
(*msg).in_reply_to = match row.get::<_, Option<String>>(2)? {
|
||||
Some(s) => to_cstring(s),
|
||||
None => std::ptr::null_mut(),
|
||||
};
|
||||
(*msg).server_folder = to_cstring(row.get::<_, String>(3)?);
|
||||
(*msg).server_uid = row.get(4)?;
|
||||
(*msg).move_state = row.get(5)?;
|
||||
(*msg).chat_id = row.get(6)?;
|
||||
@@ -456,35 +462,42 @@ pub fn dc_msg_load_from_db<'a>(msg: *mut dc_msg_t<'a>, context: &'a Context, id:
|
||||
(*msg).type_0 = row.get(12)?;
|
||||
(*msg).state = row.get(13)?;
|
||||
(*msg).is_dc_message = row.get(14)?;
|
||||
(*msg).text = dc_strdup(to_cstring(row.get::<_, String>(15)?).as_ptr());
|
||||
(*msg).text = to_cstring(row.get::<_, String>(15).unwrap_or_default());
|
||||
dc_param_set_packed(
|
||||
(*msg).param,
|
||||
to_cstring(row.get::<_, String>(16)?).as_ptr()
|
||||
to_cstring(row.get::<_, String>(16)?)
|
||||
);
|
||||
(*msg).starred = row.get(17)?;
|
||||
(*msg).hidden = row.get(18)?;
|
||||
(*msg).location_id = row.get(19)?;
|
||||
(*msg).chat_blocked = row.get(20)?;
|
||||
(*msg).chat_blocked = row.get::<_, Option<i32>>(20)?.unwrap_or_default();
|
||||
if (*msg).chat_blocked == 2 {
|
||||
dc_truncate_n_unwrap_str((*msg).text, 256, 0);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
).is_ok()
|
||||
);
|
||||
|
||||
match res {
|
||||
Ok(_) => true,
|
||||
Err(err) => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub unsafe fn dc_get_mime_headers(context: &Context, msg_id: uint32_t) -> *mut libc::c_char {
|
||||
let headers: Option<String> = sql::query_row(
|
||||
let headers: Option<String> = context.sql.query_row_col(
|
||||
context,
|
||||
&context.sql,
|
||||
"SELECT mime_headers FROM msgs WHERE id=?;",
|
||||
params![msg_id as i32],
|
||||
0,
|
||||
);
|
||||
|
||||
if let Some(headers) = headers {
|
||||
dc_strdup_keep_null(to_cstring(headers).as_ptr())
|
||||
let h = to_cstring(headers);
|
||||
let res = dc_strdup_keep_null(h);
|
||||
free(h as *mut _);
|
||||
res
|
||||
} else {
|
||||
std::ptr::null_mut()
|
||||
}
|
||||
@@ -521,52 +534,55 @@ pub fn dc_update_msg_chat_id(context: &Context, msg_id: u32, chat_id: u32) -> bo
|
||||
"UPDATE msgs SET chat_id=? WHERE id=?;",
|
||||
params![chat_id as i32, msg_id as i32],
|
||||
)
|
||||
.is_ok()
|
||||
}
|
||||
|
||||
pub fn dc_markseen_msgs(context: &Context, msg_ids: *const u32, msg_cnt: usize) -> bool {
|
||||
if msg_ids.is_null() || msg_cnt <= 0 {
|
||||
return false;
|
||||
}
|
||||
context.sql.prepare(
|
||||
let msgs = context.sql.prepare(
|
||||
"SELECT m.state, c.blocked FROM msgs m LEFT JOIN chats c ON c.id=m.chat_id WHERE m.id=? AND m.chat_id>9",
|
||||
|mut stmt| {
|
||||
let mut send_event = false;
|
||||
|
||||
|mut stmt, _| {
|
||||
let mut res = Vec::with_capacity(msg_cnt);
|
||||
for i in 0..msg_cnt {
|
||||
// TODO: do I need to reset?
|
||||
let id = unsafe { *msg_ids.offset(i as isize) };
|
||||
if let Ok((curr_state, curr_blocked)) = stmt
|
||||
.query_row(params![id as i32], |row| {
|
||||
Ok((row.get::<_, i32>(0)?, row.get::<_, i32>(1)?))
|
||||
})
|
||||
{
|
||||
if curr_blocked == 0 {
|
||||
if curr_state == 10 || curr_state == 13 {
|
||||
dc_update_msg_state(context, id, 16);
|
||||
info!(context, 0, "Seen message #{}.", id);
|
||||
|
||||
unsafe { dc_job_add(
|
||||
context,
|
||||
130,
|
||||
id as i32,
|
||||
0 as *const libc::c_char,
|
||||
0,
|
||||
) };
|
||||
send_event = true;
|
||||
}
|
||||
} else if curr_state == 10 {
|
||||
dc_update_msg_state(context, id, 13);
|
||||
send_event = true;
|
||||
}
|
||||
}
|
||||
let (state, blocked) = stmt.query_row(params![id as i32], |row| {
|
||||
Ok((row.get::<_, i32>(0)?, row.get::<_, Option<i32>>(1)?.unwrap_or_default()))
|
||||
})?;
|
||||
res.push((id, state, blocked));
|
||||
}
|
||||
|
||||
if send_event {
|
||||
context.call_cb(Event::MSGS_CHANGED, 0, 0);
|
||||
}
|
||||
Ok(())
|
||||
Ok(res)
|
||||
}
|
||||
).is_ok()
|
||||
);
|
||||
|
||||
if msgs.is_err() {
|
||||
warn!(context, 0, "markseen_msgs failed: {:?}", msgs);
|
||||
return false;
|
||||
}
|
||||
let mut send_event = false;
|
||||
let msgs = msgs.unwrap();
|
||||
|
||||
for (id, curr_state, curr_blocked) in msgs.into_iter() {
|
||||
if curr_blocked == 0 {
|
||||
if curr_state == 10 || curr_state == 13 {
|
||||
dc_update_msg_state(context, id, 16);
|
||||
info!(context, 0, "Seen message #{}.", id);
|
||||
|
||||
unsafe { dc_job_add(context, 130, id as i32, 0 as *const libc::c_char, 0) };
|
||||
send_event = true;
|
||||
}
|
||||
} else if curr_state == 10 {
|
||||
dc_update_msg_state(context, id, 13);
|
||||
send_event = true;
|
||||
}
|
||||
}
|
||||
|
||||
if send_event {
|
||||
context.call_cb(Event::MSGS_CHANGED, 0, 0);
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
|
||||
pub fn dc_update_msg_state(context: &Context, msg_id: uint32_t, state: libc::c_int) -> bool {
|
||||
@@ -576,6 +592,7 @@ pub fn dc_update_msg_state(context: &Context, msg_id: uint32_t, state: libc::c_i
|
||||
"UPDATE msgs SET state=? WHERE id=?;",
|
||||
params![state, msg_id as i32],
|
||||
)
|
||||
.is_ok()
|
||||
}
|
||||
|
||||
pub fn dc_star_msgs(
|
||||
@@ -589,7 +606,7 @@ pub fn dc_star_msgs(
|
||||
}
|
||||
context
|
||||
.sql
|
||||
.prepare("UPDATE msgs SET starred=? WHERE id=?;", |mut stmt| {
|
||||
.prepare("UPDATE msgs SET starred=? WHERE id=?;", |mut stmt, _| {
|
||||
for i in 0..msg_cnt {
|
||||
stmt.execute(params![star, unsafe { *msg_ids.offset(i as isize) as i32 }])?;
|
||||
}
|
||||
@@ -677,7 +694,7 @@ pub unsafe fn dc_msg_get_text(msg: *const dc_msg_t) -> *mut libc::c_char {
|
||||
}
|
||||
|
||||
let res = dc_truncate_str(as_str((*msg).text), 30000);
|
||||
dc_strdup(to_cstring(res).as_ptr())
|
||||
to_cstring(res)
|
||||
}
|
||||
|
||||
pub unsafe fn dc_msg_get_filename(msg: *const dc_msg_t) -> *mut libc::c_char {
|
||||
@@ -1081,6 +1098,7 @@ pub unsafe fn dc_msg_save_param_to_disk(msg: *mut dc_msg_t) -> bool {
|
||||
"UPDATE msgs SET param=? WHERE id=?;",
|
||||
params![as_str((*(*msg).param).packed), (*msg).id as i32],
|
||||
)
|
||||
.is_ok()
|
||||
}
|
||||
|
||||
pub unsafe fn dc_msg_new_load<'a>(context: &'a Context, msg_id: uint32_t) -> *mut dc_msg_t<'a> {
|
||||
@@ -1120,9 +1138,8 @@ pub unsafe fn dc_msg_exists(context: &Context, msg_id: uint32_t) -> libc::c_int
|
||||
return 0;
|
||||
}
|
||||
|
||||
let chat_id: Option<i32> = sql::query_row(
|
||||
let chat_id: Option<i32> = context.sql.query_row_col(
|
||||
context,
|
||||
&context.sql,
|
||||
"SELECT chat_id FROM msgs WHERE id=?;",
|
||||
params![msg_id as i32],
|
||||
0,
|
||||
@@ -1150,6 +1167,7 @@ pub fn dc_update_msg_move_state(
|
||||
"UPDATE msgs SET move_state=? WHERE rfc724_mid=?;",
|
||||
params![state as i32, as_str(rfc724_mid)],
|
||||
)
|
||||
.is_ok()
|
||||
}
|
||||
|
||||
pub unsafe fn dc_set_msg_failed(context: &Context, msg_id: uint32_t, error: *const libc::c_char) {
|
||||
@@ -1169,7 +1187,9 @@ pub unsafe fn dc_set_msg_failed(context: &Context, msg_id: uint32_t, error: *con
|
||||
&context.sql,
|
||||
"UPDATE msgs SET state=?, param=? WHERE id=?;",
|
||||
params![(*msg).state, as_str((*(*msg).param).packed), msg_id as i32],
|
||||
) {
|
||||
)
|
||||
.is_ok()
|
||||
{
|
||||
context.call_cb(
|
||||
Event::MSG_FAILED,
|
||||
(*msg).chat_id as uintptr_t,
|
||||
@@ -1243,14 +1263,15 @@ pub unsafe fn dc_mdn_from_ext(
|
||||
read_by_all = 1;
|
||||
} else {
|
||||
/* send event about new state */
|
||||
let ist_cnt: i32 = sql::query_row(
|
||||
context,
|
||||
&context.sql,
|
||||
"SELECT COUNT(*) FROM msgs_mdns WHERE msg_id=?;",
|
||||
params![*ret_msg_id as i32],
|
||||
0,
|
||||
)
|
||||
.unwrap_or_default();
|
||||
let ist_cnt: i32 = context
|
||||
.sql
|
||||
.query_row_col(
|
||||
context,
|
||||
"SELECT COUNT(*) FROM msgs_mdns WHERE msg_id=?;",
|
||||
params![*ret_msg_id as i32],
|
||||
0,
|
||||
)
|
||||
.unwrap_or_default();
|
||||
/*
|
||||
Groupsize: Min. MDNs
|
||||
|
||||
@@ -1339,9 +1360,7 @@ pub fn dc_rfc724_mid_exists(
|
||||
&[as_str(rfc724_mid)],
|
||||
|row| {
|
||||
if !ret_server_folder.is_null() {
|
||||
unsafe {
|
||||
*ret_server_folder = dc_strdup(to_cstring(row.get::<_, String>(0)?).as_ptr())
|
||||
};
|
||||
unsafe { *ret_server_folder = to_cstring(row.get::<_, String>(0)?) };
|
||||
}
|
||||
if !ret_server_uid.is_null() {
|
||||
unsafe { *ret_server_uid = row.get(1)? };
|
||||
|
||||
@@ -18,7 +18,7 @@ pub const DC_PARAM_GUARANTEE_E2EE: char = 'c';
|
||||
pub const DC_PARAM_ERRONEOUS_E2EE: char = 'e';
|
||||
/// for msgs: force unencrypted message, either DC_FP_ADD_AUTOCRYPT_HEADER (1), DC_FP_NO_AUTOCRYPT_HEADER (2) or 0
|
||||
pub const DC_PARAM_FORCE_PLAINTEXT: char = 'u';
|
||||
/// for msgs: an incoming message which requestes a MDN (aka read receipt)
|
||||
/// for msgs: an incoming message which requests a MDN (aka read receipt)
|
||||
pub const DC_PARAM_WANTS_MDN: char = 'r';
|
||||
/// for msgs
|
||||
pub const DC_PARAM_FORWARDED: char = 'a';
|
||||
@@ -60,10 +60,10 @@ pub const DC_PARAM_SELFTALK: char = 'K';
|
||||
pub const DC_FP_ADD_AUTOCRYPT_HEADER: u8 = 1;
|
||||
pub const DC_FP_NO_AUTOCRYPT_HEADER: u8 = 2;
|
||||
|
||||
/// An object for handling key=value parameter lists; for the key, curently only
|
||||
/// An object for handling key=value parameter lists; for the key, currently only
|
||||
/// a single character is allowed.
|
||||
///
|
||||
/// The object is used eg. by Chat or dc_msg_t, for readable paramter names,
|
||||
/// The object is used eg. by Chat or dc_msg_t, for readable parameter names,
|
||||
/// these classes define some DC_PARAM_* constantats.
|
||||
///
|
||||
/// Only for library-internal use.
|
||||
|
||||
12
src/dc_qr.rs
12
src/dc_qr.rs
@@ -188,7 +188,7 @@ pub unsafe fn dc_check_qr(context: &Context, qr: *const libc::c_char) -> *mut dc
|
||||
match current_block {
|
||||
16562876845594826114 => {}
|
||||
_ => {
|
||||
/* check the paramters
|
||||
/* check the parameters
|
||||
---------------------- */
|
||||
if !addr.is_null() {
|
||||
/* urldecoding is needed at least for OPENPGP4FPR but should not hurt in the other cases */
|
||||
@@ -239,13 +239,8 @@ pub unsafe fn dc_check_qr(context: &Context, qr: *const libc::c_char) -> *mut dc
|
||||
if addr.is_null() || invitenumber.is_null() || auth.is_null() {
|
||||
if let Some(peerstate) = peerstate {
|
||||
(*qr_parsed).state = 210i32;
|
||||
let c_addr = peerstate
|
||||
.addr
|
||||
.as_ref()
|
||||
.map(to_cstring)
|
||||
.unwrap_or_default();
|
||||
let addr_ptr = if peerstate.addr.is_some() {
|
||||
c_addr.as_ptr()
|
||||
let addr_ptr = if let Some(ref addr) = peerstate.addr {
|
||||
to_cstring(addr)
|
||||
} else {
|
||||
std::ptr::null()
|
||||
};
|
||||
@@ -256,6 +251,7 @@ pub unsafe fn dc_check_qr(context: &Context, qr: *const libc::c_char) -> *mut dc
|
||||
0x80i32,
|
||||
0 as *mut libc::c_int,
|
||||
);
|
||||
free(addr_ptr as *mut _);
|
||||
dc_create_or_lookup_nchat_by_contact_id(
|
||||
context,
|
||||
(*qr_parsed).id,
|
||||
|
||||
@@ -216,10 +216,12 @@ pub unsafe fn dc_receive_imf(
|
||||
by checking the state before the message body is downloaded */
|
||||
let mut allow_creation: libc::c_int = 1;
|
||||
if msgrmsg == 0 {
|
||||
let show_emails: libc::c_int =
|
||||
sql::get_config_int(context, &context.sql, "show_emails", 0);
|
||||
let show_emails = context
|
||||
.sql
|
||||
.get_config_int(context, "show_emails")
|
||||
.unwrap_or_default();
|
||||
if show_emails == 0 {
|
||||
chat_id = 3 as uint32_t;
|
||||
chat_id = 3;
|
||||
allow_creation = 0
|
||||
} else if show_emails == 1 {
|
||||
allow_creation = 0
|
||||
@@ -403,8 +405,10 @@ pub unsafe fn dc_receive_imf(
|
||||
dc_unarchive_chat(context, chat_id);
|
||||
// if the mime-headers should be saved, find out its size
|
||||
// (the mime-header ends with an empty line)
|
||||
let save_mime_headers =
|
||||
sql::get_config_int(context, &context.sql, "save_mime_headers", 0);
|
||||
let save_mime_headers = context
|
||||
.sql
|
||||
.get_config_int(context, "save_mime_headers")
|
||||
.unwrap_or_default();
|
||||
field = dc_mimeparser_lookup_field(&mime_parser, "In-Reply-To");
|
||||
if !field.is_null()
|
||||
&& (*field).fld_type == MAILIMF_FIELD_IN_REPLY_TO as libc::c_int
|
||||
@@ -439,7 +443,7 @@ pub unsafe fn dc_receive_imf(
|
||||
timestamp_sent, timestamp_rcvd, type, state, msgrmsg, txt, txt_raw, param, \
|
||||
bytes, hidden, mime_headers, mime_in_reply_to, mime_references) \
|
||||
VALUES (?,?,?,?,?,?, ?,?,?,?,?,?, ?,?,?,?,?,?, ?,?);",
|
||||
|mut stmt| {
|
||||
|mut stmt, conn| {
|
||||
let mut i = 0;
|
||||
loop {
|
||||
if !(i < icnt) {
|
||||
@@ -499,16 +503,21 @@ pub unsafe fn dc_receive_imf(
|
||||
} else {
|
||||
""
|
||||
},
|
||||
// txt_raw might contain invalid utf8
|
||||
if !txt_raw.is_null() {
|
||||
as_str(txt_raw)
|
||||
to_string_lossy(txt_raw)
|
||||
} else {
|
||||
""
|
||||
String::new()
|
||||
},
|
||||
as_str((*(*part).param).packed),
|
||||
(*part).bytes,
|
||||
hidden,
|
||||
if 0 != save_mime_headers {
|
||||
Some(to_string(imf_raw_not_terminated))
|
||||
let body_string = unsafe {
|
||||
std::str::from_utf8(std::slice::from_raw_parts(imf_raw_not_terminated as *const u8, imf_raw_bytes)).unwrap()
|
||||
};
|
||||
|
||||
Some(body_string)
|
||||
} else {
|
||||
None
|
||||
},
|
||||
@@ -524,9 +533,9 @@ pub unsafe fn dc_receive_imf(
|
||||
} else {
|
||||
free(txt_raw as *mut libc::c_void);
|
||||
txt_raw = 0 as *mut libc::c_char;
|
||||
insert_msg_id = sql::get_rowid(
|
||||
insert_msg_id = sql::get_rowid_with_conn(
|
||||
context,
|
||||
&context.sql,
|
||||
conn,
|
||||
"msgs",
|
||||
"rfc724_mid",
|
||||
as_str(rfc724_mid),
|
||||
@@ -581,8 +590,10 @@ pub unsafe fn dc_receive_imf(
|
||||
16282941964262048061 => {}
|
||||
_ => {
|
||||
if carray_count(mime_parser.reports) > 0 as libc::c_uint {
|
||||
let mdns_enabled =
|
||||
sql::get_config_int(context, &context.sql, "mdns_enabled", 1);
|
||||
let mdns_enabled = context
|
||||
.sql
|
||||
.get_config_int(context, "mdns_enabled")
|
||||
.unwrap_or_else(|| 1);
|
||||
icnt = carray_count(mime_parser.reports) as size_t;
|
||||
i = 0 as size_t;
|
||||
while i < icnt {
|
||||
@@ -733,19 +744,15 @@ pub unsafe fn dc_receive_imf(
|
||||
}
|
||||
if 0 != mime_parser.is_send_by_messenger || 0 != mdn_consumed {
|
||||
let param = dc_param_new();
|
||||
dc_param_set(
|
||||
param,
|
||||
'Z' as i32,
|
||||
to_cstring(server_folder.as_ref()).as_ptr(),
|
||||
);
|
||||
let server_folder_c = to_cstring(server_folder.as_ref());
|
||||
dc_param_set(param, 'Z' as i32, server_folder_c);
|
||||
free(server_folder_c as *mut _);
|
||||
dc_param_set_int(param, 'z' as i32, server_uid as i32);
|
||||
if 0 != mime_parser.is_send_by_messenger
|
||||
&& 0 != sql::get_config_int(
|
||||
context,
|
||||
&context.sql,
|
||||
"mvbox_move",
|
||||
1,
|
||||
)
|
||||
&& 0 != context
|
||||
.sql
|
||||
.get_config_int(context, "mvbox_move")
|
||||
.unwrap_or_else(|| 1)
|
||||
{
|
||||
dc_param_set_int(param, 'M' as i32, 1);
|
||||
}
|
||||
@@ -823,6 +830,14 @@ pub unsafe fn dc_receive_imf(
|
||||
}
|
||||
}
|
||||
|
||||
info!(
|
||||
context,
|
||||
0,
|
||||
"received message {} has Message-Id: {}",
|
||||
server_uid,
|
||||
to_string(rfc724_mid)
|
||||
);
|
||||
|
||||
free(rfc724_mid as *mut libc::c_void);
|
||||
free(mime_in_reply_to as *mut libc::c_void);
|
||||
free(mime_references as *mut libc::c_void);
|
||||
@@ -860,9 +875,8 @@ unsafe fn calc_timestamps(
|
||||
}
|
||||
*sort_timestamp = message_timestamp;
|
||||
if 0 != is_fresh_msg {
|
||||
let last_msg_time: Option<i64> = sql::query_row(
|
||||
let last_msg_time: Option<i64> = context.sql.query_row_col(
|
||||
context,
|
||||
&context.sql,
|
||||
"SELECT MAX(timestamp) FROM msgs WHERE chat_id=? and from_id!=? AND timestamp>=?",
|
||||
params![chat_id as i32, from_id as i32, *sort_timestamp],
|
||||
0,
|
||||
@@ -881,7 +895,7 @@ unsafe fn calc_timestamps(
|
||||
}
|
||||
|
||||
/* the function tries extracts the group-id from the message and returns the
|
||||
corresponding chat_id. If the chat_id is not existant, it is created.
|
||||
corresponding chat_id. If the chat_id is not existent, it is created.
|
||||
If the message contains groups commands (name, profile image, changed members),
|
||||
they are executed as well.
|
||||
|
||||
@@ -1107,7 +1121,9 @@ unsafe fn create_or_lookup_group(
|
||||
}
|
||||
/* check if the group does not exist but should be created */
|
||||
group_explicitly_left = dc_is_group_explicitly_left(context, grpid);
|
||||
let self_addr = sql::get_config(context, &context.sql, "configured_addr", Some(""))
|
||||
let self_addr = context
|
||||
.sql
|
||||
.get_config(context, "configured_addr")
|
||||
.unwrap_or_default();
|
||||
if chat_id == 0 as libc::c_uint
|
||||
&& 0 == dc_mimeparser_is_mailinglist_message(mime_parser)
|
||||
@@ -1181,7 +1197,9 @@ unsafe fn create_or_lookup_group(
|
||||
&context.sql,
|
||||
"UPDATE chats SET name=? WHERE id=?;",
|
||||
params![as_str(grpname), chat_id as i32],
|
||||
) {
|
||||
)
|
||||
.is_ok()
|
||||
{
|
||||
context.call_cb(Event::CHAT_MODIFIED, chat_id as uintptr_t, 0);
|
||||
}
|
||||
}
|
||||
@@ -1361,11 +1379,14 @@ unsafe fn create_or_lookup_adhoc_group(
|
||||
if dc_array_get_cnt(chat_ids) > 0 {
|
||||
chat_ids_str = dc_array_get_string(chat_ids, b",\x00" as *const u8 as *const _);
|
||||
let res = context.sql.query_row(
|
||||
"SELECT c.id, c.blocked FROM chats c \
|
||||
LEFT JOIN msgs m ON m.chat_id=c.id WHERE c.id IN(?) ORDER BY m.timestamp DESC, m.id DESC LIMIT 1;",
|
||||
params![as_str(chat_ids_str)],
|
||||
format!(
|
||||
"SELECT c.id, c.blocked FROM chats c \
|
||||
LEFT JOIN msgs m ON m.chat_id=c.id WHERE c.id IN({}) ORDER BY m.timestamp DESC, m.id DESC LIMIT 1;",
|
||||
as_str(chat_ids_str),
|
||||
),
|
||||
params![],
|
||||
|row| {
|
||||
Ok((row.get::<_, i32>(0)?, row.get::<_, i32>(1)?))
|
||||
Ok((row.get::<_, i32>(0)?, row.get::<_, Option<i32>>(1)?.unwrap_or_default()))
|
||||
}
|
||||
);
|
||||
|
||||
@@ -1387,7 +1408,7 @@ unsafe fn create_or_lookup_adhoc_group(
|
||||
/* we do not check if the message is a reply to another group, this may result in
|
||||
chats with unclear member list. instead we create a new group in the following lines ... */
|
||||
/* create a new ad-hoc group
|
||||
- there is no need to check if this group exists; otherwise we would have catched it above */
|
||||
- there is no need to check if this group exists; otherwise we would have caught it above */
|
||||
grpid = create_adhoc_grp_id(context, member_ids);
|
||||
if !grpid.is_null() {
|
||||
if !mime_parser.subject.is_null()
|
||||
@@ -1445,7 +1466,7 @@ fn create_group_record(
|
||||
create_blocked: libc::c_int,
|
||||
create_verified: libc::c_int,
|
||||
) -> u32 {
|
||||
if !sql::execute(
|
||||
if sql::execute(
|
||||
context,
|
||||
&context.sql,
|
||||
"INSERT INTO chats (type, name, grpid, blocked) VALUES(?, ?, ?, ?);",
|
||||
@@ -1455,7 +1476,9 @@ fn create_group_record(
|
||||
as_str(grpid),
|
||||
create_blocked,
|
||||
],
|
||||
) {
|
||||
)
|
||||
.is_err()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1465,20 +1488,25 @@ fn create_group_record(
|
||||
unsafe fn create_adhoc_grp_id(context: &Context, member_ids: *mut dc_array_t) -> *mut libc::c_char {
|
||||
/* algorithm:
|
||||
- sort normalized, lowercased, e-mail addresses alphabetically
|
||||
- put all e-mail addresses into a single string, separate the addresss by a single comma
|
||||
- put all e-mail addresses into a single string, separate the address by a single comma
|
||||
- sha-256 this string (without possibly terminating null-characters)
|
||||
- encode the first 64 bits of the sha-256 output as lowercase hex (results in 16 characters from the set [0-9a-f])
|
||||
*/
|
||||
let member_ids_str = dc_array_get_string(member_ids, b",\x00" as *const u8 as *const _);
|
||||
let member_cs = sql::get_config(context, &context.sql, "configured_addr", Some("no-self"))
|
||||
.unwrap()
|
||||
let member_cs = context
|
||||
.sql
|
||||
.get_config(context, "configured_addr")
|
||||
.unwrap_or_else(|| "no-self".to_string())
|
||||
.to_lowercase();
|
||||
|
||||
let members = context
|
||||
.sql
|
||||
.query_map(
|
||||
"SELECT addr FROM contacts WHERE id IN(?) AND id!=1",
|
||||
params![as_str(member_ids_str)],
|
||||
format!(
|
||||
"SELECT addr FROM contacts WHERE id IN({}) AND id!=1",
|
||||
as_str(member_ids_str)
|
||||
),
|
||||
params![],
|
||||
|row| row.get::<_, String>(0),
|
||||
|rows| {
|
||||
let mut addrs = rows.collect::<Result<Vec<_>, _>>()?;
|
||||
@@ -1501,9 +1529,7 @@ fn hex_hash(s: impl AsRef<str>) -> *const libc::c_char {
|
||||
let bytes = s.as_ref().as_bytes();
|
||||
let result = Sha256::digest(bytes);
|
||||
let result_hex = hex::encode(&result[..8]);
|
||||
let result_cstring = to_cstring(result_hex);
|
||||
|
||||
unsafe { strdup(result_cstring.as_ptr()) }
|
||||
unsafe { to_cstring(result_hex) as *const _ }
|
||||
}
|
||||
|
||||
unsafe fn search_chat_ids_by_contact_ids(
|
||||
@@ -1535,8 +1561,11 @@ unsafe fn search_chat_ids_by_contact_ids(
|
||||
dc_array_get_string(contact_ids, b",\x00" as *const u8 as *const libc::c_char);
|
||||
|
||||
context.sql.query_map(
|
||||
"SELECT DISTINCT cc.chat_id, cc.contact_id FROM chats_contacts cc LEFT JOIN chats c ON c.id=cc.chat_id WHERE cc.chat_id IN(SELECT chat_id FROM chats_contacts WHERE contact_id IN(?)) AND c.type=120 AND cc.contact_id!=1 ORDER BY cc.chat_id, cc.contact_id;",
|
||||
params![as_str(contact_ids_str)],
|
||||
format!(
|
||||
"SELECT DISTINCT cc.chat_id, cc.contact_id FROM chats_contacts cc LEFT JOIN chats c ON c.id=cc.chat_id WHERE cc.chat_id IN(SELECT chat_id FROM chats_contacts WHERE contact_id IN({})) AND c.type=120 AND cc.contact_id!=1 ORDER BY cc.chat_id, cc.contact_id;",
|
||||
as_str(contact_ids_str)
|
||||
),
|
||||
params![],
|
||||
|row| Ok((row.get::<_, i32>(0)?, row.get::<_, i32>(1)?)),
|
||||
|rows| {
|
||||
let mut last_chat_id = 0;
|
||||
@@ -1584,8 +1613,7 @@ unsafe fn check_verified_properties(
|
||||
let contact = dc_contact_new(context);
|
||||
|
||||
let verify_fail = |reason: String| {
|
||||
*failure_reason =
|
||||
strdup(to_cstring(format!("{}. See \"Info\" for details.", reason)).as_ptr());
|
||||
*failure_reason = to_cstring(format!("{}. See \"Info\" for details.", reason));
|
||||
warn!(context, 0, "{}", reason);
|
||||
};
|
||||
|
||||
@@ -1631,65 +1659,61 @@ unsafe fn check_verified_properties(
|
||||
let to_ids_str = to_string(to_ids_str_c);
|
||||
free(to_ids_str_c as *mut libc::c_void);
|
||||
|
||||
let ok = context
|
||||
.sql
|
||||
.query_map(
|
||||
let rows = context.sql.query_map(
|
||||
format!(
|
||||
"SELECT c.addr, LENGTH(ps.verified_key_fingerprint) FROM contacts c \
|
||||
LEFT JOIN acpeerstates ps ON c.addr=ps.addr WHERE c.id IN(?) ",
|
||||
params![&to_ids_str],
|
||||
|row| Ok((row.get::<_, String>(0)?, row.get::<_, i32>(1)?)),
|
||||
|rows| {
|
||||
for row in rows {
|
||||
let (to_addr, mut is_verified) = row?;
|
||||
let mut peerstate = Peerstate::from_addr(context, &context.sql, &to_addr);
|
||||
if mimeparser.e2ee_helper.gossipped_addr.contains(&to_addr)
|
||||
&& peerstate.is_some()
|
||||
{
|
||||
let peerstate = peerstate.as_mut().unwrap();
|
||||
LEFT JOIN acpeerstates ps ON c.addr=ps.addr WHERE c.id IN({}) ",
|
||||
&to_ids_str,
|
||||
),
|
||||
params![],
|
||||
|row| Ok((row.get::<_, String>(0)?, row.get::<_, i32>(1)?)),
|
||||
|rows| rows.collect::<Result<Vec<_>, _>>().map_err(Into::into),
|
||||
);
|
||||
|
||||
// if we're here, we know the gossip key is verified:
|
||||
// - use the gossip-key as verified-key if there is no verified-key
|
||||
// - OR if the verified-key does not match public-key or gossip-key
|
||||
// (otherwise a verified key can _only_ be updated through QR scan which might be annoying,
|
||||
// see https://github.com/nextleap-project/countermitm/issues/46 for a discussion about this point)
|
||||
if 0 == is_verified
|
||||
|| peerstate.verified_key_fingerprint
|
||||
!= peerstate.public_key_fingerprint
|
||||
&& peerstate.verified_key_fingerprint
|
||||
!= peerstate.gossip_key_fingerprint
|
||||
{
|
||||
info!(
|
||||
context,
|
||||
0,
|
||||
"{} has verfied {}.",
|
||||
as_str((*contact).addr),
|
||||
to_addr,
|
||||
);
|
||||
let fp = peerstate.gossip_key_fingerprint.clone();
|
||||
if let Some(fp) = fp {
|
||||
peerstate.set_verified(0, &fp, 2);
|
||||
peerstate.save_to_db(&context.sql, false);
|
||||
is_verified = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if 0 == is_verified {
|
||||
verify_fail(format!(
|
||||
"{} is not a member of this verified group",
|
||||
to_addr
|
||||
));
|
||||
cleanup();
|
||||
return Err(failure::format_err!("not a valid memember").into());
|
||||
}
|
||||
if rows.is_err() {
|
||||
cleanup();
|
||||
return 0;
|
||||
}
|
||||
for (to_addr, mut is_verified) in rows.unwrap().into_iter() {
|
||||
let mut peerstate = Peerstate::from_addr(context, &context.sql, &to_addr);
|
||||
if mimeparser.e2ee_helper.gossipped_addr.contains(&to_addr) && peerstate.is_some() {
|
||||
let peerstate = peerstate.as_mut().unwrap();
|
||||
|
||||
// if we're here, we know the gossip key is verified:
|
||||
// - use the gossip-key as verified-key if there is no verified-key
|
||||
// - OR if the verified-key does not match public-key or gossip-key
|
||||
// (otherwise a verified key can _only_ be updated through QR scan which might be annoying,
|
||||
// see https://github.com/nextleap-project/countermitm/issues/46 for a discussion about this point)
|
||||
if 0 == is_verified
|
||||
|| peerstate.verified_key_fingerprint != peerstate.public_key_fingerprint
|
||||
&& peerstate.verified_key_fingerprint != peerstate.gossip_key_fingerprint
|
||||
{
|
||||
info!(
|
||||
context,
|
||||
0,
|
||||
"{} has verfied {}.",
|
||||
as_str((*contact).addr),
|
||||
to_addr,
|
||||
);
|
||||
let fp = peerstate.gossip_key_fingerprint.clone();
|
||||
if let Some(fp) = fp {
|
||||
peerstate.set_verified(0, &fp, 2);
|
||||
peerstate.save_to_db(&context.sql, false);
|
||||
is_verified = 1;
|
||||
}
|
||||
Ok(())
|
||||
},
|
||||
)
|
||||
.is_ok(); // TODO: Better default
|
||||
}
|
||||
}
|
||||
if 0 == is_verified {
|
||||
verify_fail(format!(
|
||||
"{} is not a member of this verified group",
|
||||
to_addr
|
||||
));
|
||||
cleanup();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
cleanup();
|
||||
|
||||
ok as libc::c_int
|
||||
1
|
||||
}
|
||||
|
||||
unsafe fn set_better_msg(mime_parser: &dc_mimeparser_t, better_msg: *mut *mut libc::c_char) {
|
||||
@@ -1967,7 +1991,10 @@ unsafe fn add_or_lookup_contact_by_addr(
|
||||
return;
|
||||
}
|
||||
*check_self = 0;
|
||||
let self_addr = sql::get_config(context, &context.sql, "configured_addr", Some("")).unwrap();
|
||||
let self_addr = context
|
||||
.sql
|
||||
.get_config(context, "configured_addr")
|
||||
.unwrap_or_default();
|
||||
|
||||
if dc_addr_cmp(self_addr, as_str(addr_spec)) {
|
||||
*check_self = 1;
|
||||
|
||||
@@ -20,7 +20,6 @@ use crate::dc_token::*;
|
||||
use crate::dc_tools::*;
|
||||
use crate::key::*;
|
||||
use crate::peerstate::*;
|
||||
use crate::sql;
|
||||
use crate::types::*;
|
||||
use crate::x::*;
|
||||
|
||||
@@ -52,7 +51,7 @@ pub unsafe fn dc_get_securejoin_qr(
|
||||
auth = dc_create_id();
|
||||
dc_token_save(context, DC_TOKEN_AUTH, group_chat_id, auth);
|
||||
}
|
||||
let self_addr = sql::get_config(context, &context.sql, "configured_addr", None);
|
||||
let self_addr = context.sql.get_config(context, "configured_addr");
|
||||
|
||||
let cleanup = |fingerprint, chat, group_name, group_name_urlencoded| {
|
||||
free(fingerprint as *mut libc::c_void);
|
||||
@@ -63,7 +62,7 @@ pub unsafe fn dc_get_securejoin_qr(
|
||||
free(group_name_urlencoded as *mut libc::c_void);
|
||||
|
||||
if let Some(qr) = qr {
|
||||
strdup(to_cstring(qr).as_ptr())
|
||||
to_cstring(qr)
|
||||
} else {
|
||||
std::ptr::null_mut()
|
||||
}
|
||||
@@ -75,7 +74,11 @@ pub unsafe fn dc_get_securejoin_qr(
|
||||
}
|
||||
|
||||
let self_addr = self_addr.unwrap();
|
||||
let self_name = sql::get_config(context, &context.sql, "displayname", Some("")).unwrap();
|
||||
let self_name = context
|
||||
.sql
|
||||
.get_config(context, "displayname")
|
||||
.unwrap_or_default();
|
||||
|
||||
fingerprint = get_self_fingerprint(context);
|
||||
|
||||
if fingerprint.is_null() {
|
||||
@@ -124,7 +127,7 @@ pub unsafe fn dc_get_securejoin_qr(
|
||||
}
|
||||
|
||||
fn get_self_fingerprint(context: &Context) -> *mut libc::c_char {
|
||||
if let Some(self_addr) = sql::get_config(context, &context.sql, "configured_addr", None) {
|
||||
if let Some(self_addr) = context.sql.get_config(context, "configured_addr") {
|
||||
if let Some(key) = Key::from_self_public(context, self_addr, &context.sql) {
|
||||
return key.fingerprint_c();
|
||||
}
|
||||
@@ -919,14 +922,15 @@ pub unsafe fn dc_handle_degrade_event(context: &Context, peerstate: &Peerstate)
|
||||
// with things they cannot fix, so the user is just kicked from the verified group
|
||||
// (and he will know this and can fix this)
|
||||
if Some(DegradeEvent::FingerprintChanged) == peerstate.degrade_event {
|
||||
let contact_id: i32 = sql::query_row(
|
||||
context,
|
||||
&context.sql,
|
||||
"SELECT id FROM contacts WHERE addr=?;",
|
||||
params![&peerstate.addr],
|
||||
0,
|
||||
)
|
||||
.unwrap_or_default();
|
||||
let contact_id: i32 = context
|
||||
.sql
|
||||
.query_row_col(
|
||||
context,
|
||||
"SELECT id FROM contacts WHERE addr=?;",
|
||||
params![&peerstate.addr],
|
||||
0,
|
||||
)
|
||||
.unwrap_or_default();
|
||||
if contact_id > 0 {
|
||||
dc_create_or_lookup_nchat_by_contact_id(
|
||||
context,
|
||||
@@ -935,15 +939,15 @@ pub unsafe fn dc_handle_degrade_event(context: &Context, peerstate: &Peerstate)
|
||||
&mut contact_chat_id,
|
||||
0 as *mut libc::c_int,
|
||||
);
|
||||
let c_addr = peerstate.addr.as_ref().map(to_cstring).unwrap_or_default();
|
||||
let c_addr_ptr = if peerstate.addr.is_some() {
|
||||
c_addr.as_ptr()
|
||||
let c_addr_ptr = if let Some(ref addr) = peerstate.addr {
|
||||
to_cstring(addr)
|
||||
} else {
|
||||
std::ptr::null_mut()
|
||||
};
|
||||
let msg = dc_stock_str_repl_string(context, 37, c_addr_ptr);
|
||||
dc_add_device_msg(context, contact_chat_id, msg);
|
||||
free(msg as *mut libc::c_void);
|
||||
free(c_addr_ptr as *mut _);
|
||||
context.call_cb(
|
||||
Event::CHAT_MODIFIED,
|
||||
contact_chat_id as uintptr_t,
|
||||
|
||||
@@ -225,7 +225,8 @@ unsafe fn dc_simplify_simplify_plain_text(
|
||||
pending_linebreaks -= 1
|
||||
}
|
||||
}
|
||||
ret += &to_string(line);
|
||||
// the incoming message might contain invalid UTF8
|
||||
ret += &to_string_lossy(line);
|
||||
content_lines_added += 1;
|
||||
pending_linebreaks = 1i32
|
||||
}
|
||||
@@ -238,7 +239,7 @@ unsafe fn dc_simplify_simplify_plain_text(
|
||||
}
|
||||
dc_free_splitted_lines(lines);
|
||||
|
||||
strdup(to_cstring(ret).as_ptr())
|
||||
to_cstring(ret)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -261,7 +262,7 @@ unsafe fn is_quoted_headline(buf: *const libc::c_char) -> bool {
|
||||
/* This function may be called for the line _directly_ before a quote.
|
||||
The function checks if the line contains sth. like "On 01.02.2016, xy@z wrote:" in various languages.
|
||||
- Currently, we simply check if the last character is a ':'.
|
||||
- Checking for the existance of an email address may fail (headlines may show the user's name instead of the address) */
|
||||
- Checking for the existence of an email address may fail (headlines may show the user's name instead of the address) */
|
||||
let buf_len: libc::c_int = strlen(buf) as libc::c_int;
|
||||
if buf_len > 80i32 {
|
||||
return false;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use std::ffi::{CStr, CString};
|
||||
use std::ffi::CStr;
|
||||
|
||||
use charset::Charset;
|
||||
use mmime::mailmime_decode::*;
|
||||
@@ -689,10 +689,9 @@ pub unsafe fn dc_decode_ext_header(to_decode: *const libc::c_char) -> *mut libc:
|
||||
std::slice::from_raw_parts(decoded as *const u8, strlen(decoded));
|
||||
|
||||
let (res, _, _) = encoding.decode(data);
|
||||
free(decoded as *mut libc::c_void);
|
||||
let res_c = CString::new(res.as_bytes()).unwrap();
|
||||
|
||||
decoded = strdup(res_c.as_ptr());
|
||||
free(decoded as *mut _);
|
||||
let r = std::ffi::CString::new(res.as_bytes()).unwrap();
|
||||
decoded = dc_strdup(r.as_ptr());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -712,7 +711,8 @@ unsafe fn print_hex(target: *mut libc::c_char, cur: *const libc::c_char) {
|
||||
|
||||
let bytes = std::slice::from_raw_parts(cur as *const _, strlen(cur));
|
||||
let raw = to_cstring(format!("={}", &hex::encode_upper(bytes)[..2]));
|
||||
libc::memcpy(target as *mut _, raw.as_ptr() as *const _, 4);
|
||||
libc::memcpy(target as *mut _, raw as *const _, 4);
|
||||
free(raw as *mut libc::c_void);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
use crate::context::Context;
|
||||
use crate::dc_tools::*;
|
||||
use crate::sql;
|
||||
use crate::x::strdup;
|
||||
|
||||
// Token namespaces
|
||||
pub type dc_tokennamespc_t = usize;
|
||||
@@ -26,6 +25,7 @@ pub fn dc_token_save(
|
||||
"INSERT INTO tokens (namespc, foreign_id, token, timestamp) VALUES (?, ?, ?, ?);",
|
||||
params![namespc as i32, foreign_id as i32, as_str(token), time()],
|
||||
)
|
||||
.is_ok()
|
||||
}
|
||||
|
||||
pub fn dc_token_lookup(
|
||||
@@ -33,17 +33,16 @@ pub fn dc_token_lookup(
|
||||
namespc: dc_tokennamespc_t,
|
||||
foreign_id: u32,
|
||||
) -> *mut libc::c_char {
|
||||
if let Some(token) = sql::query_row::<_, String>(
|
||||
context,
|
||||
&context.sql,
|
||||
"SELECT token FROM tokens WHERE namespc=? AND foreign_id=?;",
|
||||
params![namespc as i32, foreign_id as i32],
|
||||
0,
|
||||
) {
|
||||
unsafe { strdup(to_cstring(token).as_ptr()) }
|
||||
} else {
|
||||
std::ptr::null_mut()
|
||||
}
|
||||
context
|
||||
.sql
|
||||
.query_row_col::<_, String>(
|
||||
context,
|
||||
"SELECT token FROM tokens WHERE namespc=? AND foreign_id=?;",
|
||||
params![namespc as i32, foreign_id as i32],
|
||||
0,
|
||||
)
|
||||
.map(|s| unsafe { to_cstring(s) })
|
||||
.unwrap_or_else(|| std::ptr::null_mut())
|
||||
}
|
||||
|
||||
pub fn dc_token_exists(
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use std::borrow::Cow;
|
||||
use std::ffi::{CStr, CString};
|
||||
use std::fs;
|
||||
use std::time::SystemTime;
|
||||
|
||||
@@ -176,14 +177,14 @@ pub unsafe fn dc_trim(buf: *mut libc::c_char) {
|
||||
|
||||
/* the result must be free()'d */
|
||||
pub unsafe fn dc_strlower(in_0: *const libc::c_char) -> *mut libc::c_char {
|
||||
let raw = to_cstring(to_string(in_0).to_lowercase());
|
||||
strdup(raw.as_ptr())
|
||||
to_cstring(to_string(in_0).to_lowercase())
|
||||
}
|
||||
|
||||
pub unsafe fn dc_strlower_in_place(in_0: *mut libc::c_char) {
|
||||
let raw = to_cstring(to_string(in_0).to_lowercase());
|
||||
assert_eq!(strlen(in_0), strlen(raw.as_ptr()));
|
||||
memcpy(in_0 as *mut _, raw.as_ptr() as *const _, strlen(in_0));
|
||||
assert_eq!(strlen(in_0), strlen(raw));
|
||||
memcpy(in_0 as *mut _, raw as *const _, strlen(in_0));
|
||||
free(raw as *mut _);
|
||||
}
|
||||
|
||||
pub unsafe fn dc_str_contains(
|
||||
@@ -231,7 +232,7 @@ pub unsafe fn dc_binary_to_uc_hex(buf: *const uint8_t, bytes: size_t) -> *mut li
|
||||
|
||||
let buf = std::slice::from_raw_parts(buf, bytes);
|
||||
let raw = hex::encode_upper(buf);
|
||||
strdup(to_cstring(raw).as_ptr())
|
||||
to_cstring(raw)
|
||||
}
|
||||
|
||||
/* remove all \r characters from string */
|
||||
@@ -527,7 +528,7 @@ pub unsafe fn dc_str_from_clist(
|
||||
}
|
||||
}
|
||||
|
||||
strdup(to_cstring(res).as_ptr())
|
||||
to_cstring(res)
|
||||
}
|
||||
|
||||
pub unsafe fn dc_str_to_clist(
|
||||
@@ -563,7 +564,7 @@ pub unsafe fn dc_str_to_color(str: *const libc::c_char) -> libc::c_int {
|
||||
/* the colors must fulfill some criterions as:
|
||||
- contrast to black and to white
|
||||
- work as a text-color
|
||||
- being noticable on a typical map
|
||||
- being noticeable on a typical map
|
||||
- harmonize together while being different enough
|
||||
(therefore, we cannot just use random rgb colors :) */
|
||||
static mut colors: [uint32_t; 16] = [
|
||||
@@ -669,7 +670,7 @@ pub unsafe fn dc_timestamp_from_date(date_time: *mut mailimf_date_time) -> i64 {
|
||||
/* the return value must be free()'d */
|
||||
pub unsafe fn dc_timestamp_to_str(wanted: i64) -> *mut libc::c_char {
|
||||
let res = dc_timestamp_to_str_safe(wanted);
|
||||
strdup(to_cstring(res).as_ptr())
|
||||
to_cstring(res)
|
||||
}
|
||||
|
||||
pub fn dc_timestamp_to_str_safe(wanted: i64) -> String {
|
||||
@@ -1254,8 +1255,12 @@ pub unsafe fn dc_write_file(
|
||||
}
|
||||
|
||||
pub fn dc_write_file_safe(context: &Context, pathNfilename: impl AsRef<str>, buf: &[u8]) -> bool {
|
||||
let pathNfilename_abs =
|
||||
unsafe { dc_get_abs_path(context, to_cstring(pathNfilename.as_ref()).as_ptr()) };
|
||||
let pathNfilename_abs = unsafe {
|
||||
let n = to_cstring(pathNfilename.as_ref());
|
||||
let res = dc_get_abs_path(context, n);
|
||||
free(n as *mut _);
|
||||
res
|
||||
};
|
||||
if pathNfilename_abs.is_null() {
|
||||
return false;
|
||||
}
|
||||
@@ -1299,8 +1304,13 @@ pub unsafe fn dc_read_file(
|
||||
}
|
||||
|
||||
pub fn dc_read_file_safe(context: &Context, pathNfilename: impl AsRef<str>) -> Option<Vec<u8>> {
|
||||
let pathNfilename_abs =
|
||||
unsafe { dc_get_abs_path(context, to_cstring(pathNfilename.as_ref()).as_ptr()) };
|
||||
let pathNfilename_abs = unsafe {
|
||||
let n = to_cstring(pathNfilename.as_ref());
|
||||
let p = dc_get_abs_path(context, n);
|
||||
free(n as *mut _);
|
||||
p
|
||||
};
|
||||
|
||||
if pathNfilename_abs.is_null() {
|
||||
return None;
|
||||
}
|
||||
@@ -1506,20 +1516,20 @@ pub trait OsStrExt {
|
||||
///
|
||||
/// On windows when the string contains invalid Unicode
|
||||
/// `[Err]([CStringError::NotUnicode])` is returned.
|
||||
fn to_c_string(&self) -> Result<std::ffi::CString, CStringError>;
|
||||
fn to_c_string(&self) -> Result<CString, CStringError>;
|
||||
}
|
||||
|
||||
impl<T: AsRef<std::ffi::OsStr>> OsStrExt for T {
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
fn to_c_string(&self) -> Result<std::ffi::CString, CStringError> {
|
||||
fn to_c_string(&self) -> Result<CString, CStringError> {
|
||||
use std::os::unix::ffi::OsStrExt;
|
||||
std::ffi::CString::new(self.as_ref().as_bytes()).map_err(|err| match err {
|
||||
CString::new(self.as_ref().as_bytes()).map_err(|err| match err {
|
||||
std::ffi::NulError { .. } => CStringError::InteriorNullByte,
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
fn to_c_string(&self) -> Result<std::ffi::CString, CStringError> {
|
||||
fn to_c_string(&self) -> Result<CString, CStringError> {
|
||||
os_str_to_c_string_unicode(&self)
|
||||
}
|
||||
}
|
||||
@@ -1528,29 +1538,57 @@ impl<T: AsRef<std::ffi::OsStr>> OsStrExt for T {
|
||||
#[allow(dead_code)]
|
||||
fn os_str_to_c_string_unicode(
|
||||
os_str: &dyn AsRef<std::ffi::OsStr>,
|
||||
) -> Result<std::ffi::CString, CStringError> {
|
||||
) -> Result<CString, CStringError> {
|
||||
match os_str.as_ref().to_str() {
|
||||
Some(val) => std::ffi::CString::new(val.as_bytes()).map_err(|err| match err {
|
||||
Some(val) => CString::new(val.as_bytes()).map_err(|err| match err {
|
||||
std::ffi::NulError { .. } => CStringError::InteriorNullByte,
|
||||
}),
|
||||
None => Err(CStringError::NotUnicode),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_cstring<S: AsRef<str>>(s: S) -> std::ffi::CString {
|
||||
std::ffi::CString::new(s.as_ref()).unwrap()
|
||||
/// Needs to free the result after use!
|
||||
pub unsafe fn to_cstring<S: AsRef<str>>(s: S) -> *mut libc::c_char {
|
||||
let cstr = CString::new(s.as_ref()).expect("invalid string converted");
|
||||
dc_strdup(cstr.as_ref().as_ptr())
|
||||
}
|
||||
|
||||
pub fn to_string(s: *const libc::c_char) -> String {
|
||||
if s.is_null() {
|
||||
return "".into();
|
||||
}
|
||||
unsafe { std::ffi::CStr::from_ptr(s).to_str().unwrap().to_string() }
|
||||
|
||||
let cstr = unsafe { CStr::from_ptr(s) };
|
||||
|
||||
cstr.to_str().map(|s| s.to_string()).unwrap_or_else(|err| {
|
||||
panic!(
|
||||
"Non utf8 string: '{:?}' ({:?})",
|
||||
cstr.to_string_lossy(),
|
||||
err
|
||||
);
|
||||
})
|
||||
}
|
||||
|
||||
pub fn to_string_lossy(s: *const libc::c_char) -> String {
|
||||
if s.is_null() {
|
||||
return "".into();
|
||||
}
|
||||
|
||||
let cstr = unsafe { CStr::from_ptr(s) };
|
||||
|
||||
cstr.to_str()
|
||||
.map(|s| s.to_string())
|
||||
.unwrap_or_else(|_| cstr.to_string_lossy().to_string())
|
||||
}
|
||||
|
||||
pub fn as_str<'a>(s: *const libc::c_char) -> &'a str {
|
||||
assert!(!s.is_null(), "cannot be used on null pointers");
|
||||
unsafe { std::ffi::CStr::from_ptr(s).to_str().unwrap() }
|
||||
|
||||
let cstr = unsafe { CStr::from_ptr(s) };
|
||||
|
||||
cstr.to_str().unwrap_or_else(|err| {
|
||||
panic!("Non utf8 string: '{:?}' ({:?})", cstr.to_bytes(), err);
|
||||
})
|
||||
}
|
||||
|
||||
/// Convert a C `*char` pointer to a [std::path::Path] slice.
|
||||
@@ -1582,7 +1620,7 @@ pub fn as_path<'a>(s: *const libc::c_char) -> &'a std::path::Path {
|
||||
as_path_unicode(s)
|
||||
}
|
||||
|
||||
// Implmentation for as_path() on Windows.
|
||||
// Implementation for as_path() on Windows.
|
||||
//
|
||||
// Having this as a separate function means it can be tested on unix
|
||||
// too.
|
||||
@@ -1981,7 +2019,7 @@ mod tests {
|
||||
let some_dir = std::path::Path::new(&some_str);
|
||||
assert_eq!(
|
||||
some_dir.as_os_str().to_c_string().unwrap(),
|
||||
std::ffi::CString::new("/some/valid/utf8").unwrap()
|
||||
CString::new("/some/valid/utf8").unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -2006,7 +2044,7 @@ mod tests {
|
||||
let some_dir = std::path::Path::new(&some_str);
|
||||
assert_eq!(
|
||||
some_dir.as_os_str().to_c_string().unwrap(),
|
||||
std::ffi::CString::new("/some/valid/utf8").unwrap()
|
||||
CString::new("/some/valid/utf8").unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -2015,7 +2053,7 @@ mod tests {
|
||||
let some_str = std::ffi::OsString::from("foo");
|
||||
assert_eq!(
|
||||
os_str_to_c_string_unicode(&some_str).unwrap(),
|
||||
std::ffi::CString::new("foo").unwrap()
|
||||
CString::new("foo").unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -2025,7 +2063,7 @@ mod tests {
|
||||
let some_path = std::path::Path::new(&some_str);
|
||||
assert_eq!(
|
||||
os_str_to_c_string_unicode(&some_path).unwrap(),
|
||||
std::ffi::CString::new("/some/path").unwrap()
|
||||
CString::new("/some/path").unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -2040,15 +2078,15 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_as_path() {
|
||||
let some_path = std::ffi::CString::new("/some/path").unwrap();
|
||||
let some_path = CString::new("/some/path").unwrap();
|
||||
let ptr = some_path.as_ptr();
|
||||
assert_eq!(as_path(ptr), std::ffi::OsString::from("/some/path"))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_as_path_unicode_fn() {
|
||||
let some_path = std::ffi::CString::new("/some/path").unwrap();
|
||||
let some_path = CString::new("/some/path").unwrap();
|
||||
let ptr = some_path.as_ptr();
|
||||
assert_eq!(as_path_unicode(ptr), std::ffi::OsString::from("/some/path"))
|
||||
assert_eq!(as_path_unicode(ptr), std::ffi::OsString::from("/some/path"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,8 @@ pub enum Error {
|
||||
SqlAlreadyOpen,
|
||||
#[fail(display = "Sqlite: Failed to open")]
|
||||
SqlFailedToOpen,
|
||||
#[fail(display = "{:?}", _0)]
|
||||
Io(std::io::Error),
|
||||
}
|
||||
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
@@ -35,3 +37,9 @@ impl From<r2d2::Error> for Error {
|
||||
Error::ConnectionPool(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for Error {
|
||||
fn from(err: std::io::Error) -> Error {
|
||||
Error::Io(err)
|
||||
}
|
||||
}
|
||||
|
||||
91
src/imap.rs
91
src/imap.rs
@@ -1,4 +1,3 @@
|
||||
use std::ffi::CString;
|
||||
use std::net;
|
||||
use std::sync::{Arc, Condvar, Mutex, RwLock};
|
||||
use std::time::{Duration, SystemTime};
|
||||
@@ -6,10 +5,10 @@ use std::time::{Duration, SystemTime};
|
||||
use crate::constants::*;
|
||||
use crate::context::Context;
|
||||
use crate::dc_loginparam::*;
|
||||
use crate::dc_tools::as_str;
|
||||
use crate::dc_tools::{as_str, to_cstring};
|
||||
use crate::oauth2::dc_get_oauth2_access_token;
|
||||
use crate::sql;
|
||||
use crate::types::*;
|
||||
use crate::x::free;
|
||||
|
||||
pub const DC_IMAP_SEEN: usize = 0x0001;
|
||||
pub const DC_REGENERATE: usize = 0x01;
|
||||
@@ -472,24 +471,9 @@ impl Imap {
|
||||
fn unsetup_handle(&self, context: &Context) {
|
||||
info!(context, 0, "IMAP unsetup_handle starts");
|
||||
|
||||
// XXX the next line currently can block even if all threads
|
||||
// terminated already
|
||||
let session = self.session.lock().unwrap().take();
|
||||
info!(
|
||||
context,
|
||||
0, "IMAP unsetup_handle step1 (acquired session.lock)"
|
||||
);
|
||||
if session.is_some() {
|
||||
match session.unwrap().close() {
|
||||
Ok(_) => {}
|
||||
Err(err) => {
|
||||
eprintln!("failed to close connection: {:?}", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
info!(
|
||||
context,
|
||||
0, "IMAP unsetup_handle step 2 (closing down stream)."
|
||||
0, "IMAP unsetup_handle step 1 (closing down stream)."
|
||||
);
|
||||
let stream = self.stream.write().unwrap().take();
|
||||
if stream.is_some() {
|
||||
@@ -500,6 +484,19 @@ impl Imap {
|
||||
}
|
||||
}
|
||||
}
|
||||
info!(
|
||||
context,
|
||||
0, "IMAP unsetup_handle step 2 (acquiring session.lock)"
|
||||
);
|
||||
let session = self.session.lock().unwrap().take();
|
||||
if session.is_some() {
|
||||
match session.unwrap().close() {
|
||||
Ok(_) => {}
|
||||
Err(err) => {
|
||||
eprintln!("failed to close connection: {:?}", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
info!(context, 0, "IMAP unsetup_handle step 3 (clearing config).");
|
||||
self.config.write().unwrap().selected_folder = None;
|
||||
@@ -548,7 +545,7 @@ impl Imap {
|
||||
config.server_flags = server_flags;
|
||||
}
|
||||
|
||||
if self.setup_handle_if_needed(context) {
|
||||
if !self.setup_handle_if_needed(context) {
|
||||
self.free_connect_params();
|
||||
return false;
|
||||
}
|
||||
@@ -564,7 +561,6 @@ impl Imap {
|
||||
} else {
|
||||
let can_idle = caps.has("IDLE");
|
||||
let has_xlist = caps.has("XLIST");
|
||||
|
||||
let caps_list = caps.iter().fold(String::new(), |mut s, c| {
|
||||
s += " ";
|
||||
s += c;
|
||||
@@ -578,7 +574,6 @@ impl Imap {
|
||||
lp.mail_user,
|
||||
caps_list,
|
||||
);
|
||||
|
||||
self.config.write().unwrap().can_idle = can_idle;
|
||||
self.config.write().unwrap().has_xlist = has_xlist;
|
||||
*self.connected.lock().unwrap() = true;
|
||||
@@ -711,17 +706,19 @@ impl Imap {
|
||||
fn get_config_last_seen_uid<S: AsRef<str>>(&self, context: &Context, folder: S) -> (u32, u32) {
|
||||
let key = format!("imap.mailbox.{}", folder.as_ref());
|
||||
let val1 = unsafe {
|
||||
(self.get_config)(
|
||||
context,
|
||||
CString::new(key).unwrap().as_ptr(),
|
||||
0 as *const libc::c_char,
|
||||
)
|
||||
let key_c = to_cstring(key);
|
||||
let val = (self.get_config)(context, key_c, 0 as *const libc::c_char);
|
||||
free(key_c as *mut _);
|
||||
val
|
||||
};
|
||||
if val1.is_null() {
|
||||
return (0, 0);
|
||||
}
|
||||
let entry = as_str(val1);
|
||||
|
||||
if entry.is_empty() {
|
||||
return (0, 0);
|
||||
}
|
||||
// the entry has the format `imap.mailbox.<folder>=<uidvalidity>:<lastseenuid>`
|
||||
let mut parts = entry.split(':');
|
||||
(
|
||||
@@ -855,9 +852,11 @@ impl Imap {
|
||||
.message_id
|
||||
.expect("missing message id");
|
||||
|
||||
let message_id_c = CString::new(message_id).unwrap();
|
||||
if 0 == unsafe {
|
||||
(self.precheck_imf)(context, message_id_c.as_ptr(), folder.as_ref(), cur_uid)
|
||||
let message_id_c = to_cstring(message_id);
|
||||
let res = (self.precheck_imf)(context, message_id_c, folder.as_ref(), cur_uid);
|
||||
free(message_id_c as *mut _);
|
||||
res
|
||||
} {
|
||||
// check passed, go fetch the rest
|
||||
if self.fetch_single_msg(context, &folder, cur_uid) == 0 {
|
||||
@@ -926,11 +925,11 @@ impl Imap {
|
||||
let val = format!("{}:{}", uidvalidity, lastseenuid);
|
||||
|
||||
unsafe {
|
||||
(self.set_config)(
|
||||
context,
|
||||
CString::new(key).unwrap().as_ptr(),
|
||||
CString::new(val).unwrap().as_ptr(),
|
||||
)
|
||||
let key_c = to_cstring(key);
|
||||
let val_c = to_cstring(val);
|
||||
(self.set_config)(context, key_c, val_c);
|
||||
free(key_c as *mut _);
|
||||
free(val_c as *mut _);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1009,11 +1008,12 @@ impl Imap {
|
||||
let flags = if is_seen { DC_IMAP_SEEN } else { 0 };
|
||||
|
||||
if !is_deleted && msg.body().is_some() {
|
||||
let body = msg.body().unwrap();
|
||||
unsafe {
|
||||
(self.receive_imf)(
|
||||
context,
|
||||
msg.body().unwrap().as_ptr() as *const libc::c_char,
|
||||
msg.body().unwrap().len(),
|
||||
body.as_ptr() as *const libc::c_char,
|
||||
body.len(),
|
||||
folder.as_ref(),
|
||||
server_uid,
|
||||
flags as u32,
|
||||
@@ -1048,14 +1048,15 @@ impl Imap {
|
||||
let (sender, receiver) = std::sync::mpsc::channel();
|
||||
let v = self.watch.clone();
|
||||
|
||||
warn!(context, 0, "IMAP-IDLE SPAWNING");
|
||||
info!(context, 0, "IMAP-IDLE SPAWNING");
|
||||
std::thread::spawn(move || {
|
||||
let &(ref lock, ref cvar) = &*v;
|
||||
if let Some(ref mut session) = &mut *session.lock().unwrap() {
|
||||
let mut idle = match session.idle() {
|
||||
Ok(idle) => idle,
|
||||
Err(err) => {
|
||||
panic!("failed to setup idle: {:?}", err);
|
||||
eprintln!("failed to setup idle: {:?}", err);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1621,19 +1622,15 @@ impl Imap {
|
||||
}
|
||||
}
|
||||
|
||||
sql::set_config_int(context, &context.sql, "folders_configured", 3);
|
||||
context.sql.set_config_int(context, "folders_configured", 3);
|
||||
if let Some(ref mvbox_folder) = mvbox_folder {
|
||||
sql::set_config(
|
||||
context,
|
||||
&context.sql,
|
||||
"configured_mvbox_folder",
|
||||
Some(mvbox_folder),
|
||||
);
|
||||
context
|
||||
.sql
|
||||
.set_config(context, "configured_mvbox_folder", Some(mvbox_folder));
|
||||
}
|
||||
if let Some(ref sentbox_folder) = sentbox_folder {
|
||||
sql::set_config(
|
||||
context.sql.set_config(
|
||||
context,
|
||||
&context.sql,
|
||||
"configured_sentbox_folder",
|
||||
Some(sentbox_folder.name()),
|
||||
);
|
||||
|
||||
11
src/key.rs
11
src/key.rs
@@ -89,6 +89,9 @@ impl Key {
|
||||
}
|
||||
|
||||
pub fn from_slice(bytes: &[u8], key_type: KeyType) -> Option<Self> {
|
||||
if 0 == bytes.len() {
|
||||
return None;
|
||||
}
|
||||
let res: Result<Key, _> = match key_type {
|
||||
KeyType::Public => SignedPublicKey::from_bytes(Cursor::new(bytes)).map(Into::into),
|
||||
KeyType::Private => SignedSecretKey::from_bytes(Cursor::new(bytes)).map(Into::into),
|
||||
@@ -149,9 +152,8 @@ impl Key {
|
||||
) -> Option<Self> {
|
||||
let addr = self_addr.as_ref();
|
||||
|
||||
sql::query_row(
|
||||
sql.query_row_col(
|
||||
context,
|
||||
sql,
|
||||
"SELECT public_key FROM keypairs WHERE addr=? AND is_default=1;",
|
||||
&[addr],
|
||||
0,
|
||||
@@ -164,9 +166,8 @@ impl Key {
|
||||
self_addr: impl AsRef<str>,
|
||||
sql: &Sql,
|
||||
) -> Option<Self> {
|
||||
sql::query_row(
|
||||
sql.query_row_col(
|
||||
context,
|
||||
sql,
|
||||
"SELECT private_key FROM keypairs WHERE addr=? AND is_default=1;",
|
||||
&[self_addr.as_ref()],
|
||||
0,
|
||||
@@ -308,7 +309,7 @@ pub fn dc_key_save_self_keypair(
|
||||
sql,
|
||||
"INSERT INTO keypairs (addr, is_default, public_key, private_key, created) VALUES (?,?,?,?,?);",
|
||||
params![addr.as_ref(), is_default, public_key.to_bytes(), private_key.to_bytes(), time()],
|
||||
)
|
||||
).is_ok()
|
||||
}
|
||||
|
||||
/// Make a fingerprint human-readable, in hex format.
|
||||
|
||||
@@ -3,7 +3,7 @@ use std::borrow::Cow;
|
||||
use crate::constants::*;
|
||||
use crate::context::Context;
|
||||
use crate::key::*;
|
||||
use crate::sql::{self, Sql};
|
||||
use crate::sql::Sql;
|
||||
|
||||
#[derive(Default, Clone, Debug)]
|
||||
pub struct Keyring<'a> {
|
||||
@@ -33,9 +33,8 @@ impl<'a> Keyring<'a> {
|
||||
self_addr: impl AsRef<str>,
|
||||
sql: &Sql,
|
||||
) -> bool {
|
||||
sql::query_row(
|
||||
sql.query_row_col(
|
||||
context,
|
||||
sql,
|
||||
"SELECT private_key FROM keypairs ORDER BY addr=? DESC, is_default DESC;",
|
||||
&[self_addr.as_ref()],
|
||||
0,
|
||||
|
||||
@@ -21,6 +21,7 @@ extern crate rusqlite;
|
||||
mod log;
|
||||
|
||||
pub mod aheader;
|
||||
pub mod config;
|
||||
pub mod constants;
|
||||
pub mod context;
|
||||
pub mod error;
|
||||
|
||||
46
src/log.rs
46
src/log.rs
@@ -3,11 +3,14 @@ macro_rules! info {
|
||||
($ctx:expr, $data1:expr, $msg:expr) => {
|
||||
info!($ctx, $data1, $msg,)
|
||||
};
|
||||
($ctx:expr, $data1:expr, $msg:expr, $($args:expr),* $(,)?) => {{
|
||||
let formatted = format!($msg, $($args),*);
|
||||
let formatted_c = $crate::dc_tools::to_cstring(formatted);
|
||||
$ctx.call_cb($crate::constants::Event::INFO, $data1 as libc::uintptr_t,
|
||||
formatted_c.as_ptr() as libc::uintptr_t)
|
||||
($ctx:expr, $data1:expr, $msg:expr, $($args:expr),* $(,)?) => {
|
||||
#[allow(unused_unsafe)]
|
||||
unsafe {
|
||||
let formatted = format!($msg, $($args),*);
|
||||
let formatted_c = $crate::dc_tools::to_cstring(formatted);
|
||||
$ctx.call_cb($crate::constants::Event::INFO, $data1 as libc::uintptr_t,
|
||||
formatted_c as libc::uintptr_t);
|
||||
libc::free(formatted_c as *mut libc::c_void);
|
||||
}};
|
||||
}
|
||||
|
||||
@@ -17,11 +20,14 @@ macro_rules! warn {
|
||||
warn!($ctx, $data1, $msg,)
|
||||
};
|
||||
($ctx:expr, $data1:expr, $msg:expr, $($args:expr),* $(,)?) => {
|
||||
let formatted = format!($msg, $($args),*);
|
||||
let formatted_c = $crate::dc_tools::to_cstring(formatted);
|
||||
$ctx.call_cb($crate::constants::Event::WARNING, $data1 as libc::uintptr_t,
|
||||
formatted_c.as_ptr() as libc::uintptr_t)
|
||||
};
|
||||
#[allow(unused_unsafe)]
|
||||
unsafe {
|
||||
let formatted = format!($msg, $($args),*);
|
||||
let formatted_c = $crate::dc_tools::to_cstring(formatted);
|
||||
$ctx.call_cb($crate::constants::Event::WARNING, $data1 as libc::uintptr_t,
|
||||
formatted_c as libc::uintptr_t);
|
||||
libc::free(formatted_c as *mut libc::c_void) ;
|
||||
}};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
@@ -30,11 +36,14 @@ macro_rules! error {
|
||||
error!($ctx, $data1, $msg,)
|
||||
};
|
||||
($ctx:expr, $data1:expr, $msg:expr, $($args:expr),* $(,)?) => {
|
||||
#[allow(unused_unsafe)]
|
||||
unsafe {
|
||||
let formatted = format!($msg, $($args),*);
|
||||
let formatted_c = $crate::dc_tools::to_cstring(formatted);
|
||||
$ctx.call_cb($crate::constants::Event::ERROR, $data1 as libc::uintptr_t,
|
||||
formatted_c.as_ptr() as libc::uintptr_t)
|
||||
};
|
||||
formatted_c as libc::uintptr_t);
|
||||
libc::free(formatted_c as *mut libc::c_void);
|
||||
}};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
@@ -43,9 +52,12 @@ macro_rules! log_event {
|
||||
log_event!($ctx, $data1, $msg,)
|
||||
};
|
||||
($ctx:expr, $event:expr, $data1:expr, $msg:expr, $($args:expr),* $(,)?) => {
|
||||
let formatted = format!($msg, $($args),*);
|
||||
let formatted_c = $crate::dc_tools::to_cstring(formatted);
|
||||
$ctx.call_cb($event, $data1 as libc::uintptr_t,
|
||||
formatted_c.as_ptr() as libc::uintptr_t)
|
||||
};
|
||||
#[allow(unused_unsafe)]
|
||||
unsafe {
|
||||
let formatted = format!($msg, $($args),*);
|
||||
let formatted_c = $crate::dc_tools::to_cstring(formatted);
|
||||
$ctx.call_cb($event, $data1 as libc::uintptr_t,
|
||||
formatted_c as libc::uintptr_t);
|
||||
libc::free(formatted_c as *mut libc::c_void);
|
||||
}};
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ use serde::Deserialize;
|
||||
|
||||
use crate::context::Context;
|
||||
use crate::dc_tools::*;
|
||||
use crate::sql;
|
||||
|
||||
const OAUTH2_GMAIL: Oauth2 = Oauth2 {
|
||||
client_id: "959970109878-4mvtgf6feshskf7695nfln6002mom908.apps.googleusercontent.com",
|
||||
@@ -49,9 +48,8 @@ pub fn dc_get_oauth2_url(
|
||||
redirect_uri: impl AsRef<str>,
|
||||
) -> Option<String> {
|
||||
if let Some(oauth2) = Oauth2::from_address(addr) {
|
||||
sql::set_config(
|
||||
context.sql.set_config(
|
||||
context,
|
||||
&context.sql,
|
||||
"oauth2_pending_redirect_uri",
|
||||
Some(redirect_uri.as_ref()),
|
||||
);
|
||||
@@ -78,17 +76,18 @@ pub fn dc_get_oauth2_access_token(
|
||||
|
||||
// read generated token
|
||||
if 0 == flags & 0x1 && !is_expired(context) {
|
||||
let access_token = sql::get_config(context, &context.sql, "oauth2_access_token", None);
|
||||
let access_token = context.sql.get_config(context, "oauth2_access_token");
|
||||
if access_token.is_some() {
|
||||
// success
|
||||
return access_token;
|
||||
}
|
||||
}
|
||||
|
||||
let refresh_token = sql::get_config(context, &context.sql, "oauth2_refresh_token", None);
|
||||
let refresh_token_for =
|
||||
sql::get_config(context, &context.sql, "oauth2_refresh_token_for", None)
|
||||
.unwrap_or_else(|| "unset".into());
|
||||
let refresh_token = context.sql.get_config(context, "oauth2_refresh_token");
|
||||
let refresh_token_for = context
|
||||
.sql
|
||||
.get_config(context, "oauth2_refresh_token_for")
|
||||
.unwrap_or_else(|| "unset".into());
|
||||
|
||||
let (redirect_uri, token_url, update_redirect_uri_on_success) =
|
||||
if refresh_token.is_none() || refresh_token_for != code.as_ref() {
|
||||
@@ -97,7 +96,9 @@ pub fn dc_get_oauth2_access_token(
|
||||
0, "Generate OAuth2 refresh_token and access_token...",
|
||||
);
|
||||
(
|
||||
sql::get_config(context, &context.sql, "oauth2_pending_redirect_uri", None)
|
||||
context
|
||||
.sql
|
||||
.get_config(context, "oauth2_pending_redirect_uri")
|
||||
.unwrap_or_else(|| "unset".into()),
|
||||
oauth2.init_token,
|
||||
true,
|
||||
@@ -108,7 +109,9 @@ pub fn dc_get_oauth2_access_token(
|
||||
0, "Regenerate OAuth2 access_token by refresh_token...",
|
||||
);
|
||||
(
|
||||
sql::get_config(context, &context.sql, "oauth2_redirect_uri", None)
|
||||
context
|
||||
.sql
|
||||
.get_config(context, "oauth2_redirect_uri")
|
||||
.unwrap_or_else(|| "unset".into()),
|
||||
oauth2.refresh_token,
|
||||
false,
|
||||
@@ -152,38 +155,33 @@ pub fn dc_get_oauth2_access_token(
|
||||
println!("response: {:?}", &parsed);
|
||||
let response = parsed.unwrap();
|
||||
if let Some(ref token) = response.refresh_token {
|
||||
sql::set_config(context, &context.sql, "oauth2_refresh_token", Some(token));
|
||||
sql::set_config(
|
||||
context,
|
||||
&context.sql,
|
||||
"oauth2_refresh_token_for",
|
||||
Some(code.as_ref()),
|
||||
);
|
||||
context
|
||||
.sql
|
||||
.set_config(context, "oauth2_refresh_token", Some(token));
|
||||
context
|
||||
.sql
|
||||
.set_config(context, "oauth2_refresh_token_for", Some(code.as_ref()));
|
||||
}
|
||||
|
||||
// after that, save the access token.
|
||||
// if it's unset, we may get it in the next round as we have the refresh_token now.
|
||||
if let Some(ref token) = response.access_token {
|
||||
sql::set_config(context, &context.sql, "oauth2_access_token", Some(token));
|
||||
context
|
||||
.sql
|
||||
.set_config(context, "oauth2_access_token", Some(token));
|
||||
let expires_in = response
|
||||
.expires_in
|
||||
// refresh a bet before
|
||||
.map(|t| time() + t as i64 - 5)
|
||||
.unwrap_or_else(|| 0);
|
||||
sql::set_config_int64(
|
||||
context,
|
||||
&context.sql,
|
||||
"oauth2_timestamp_expires",
|
||||
expires_in,
|
||||
);
|
||||
context
|
||||
.sql
|
||||
.set_config_int64(context, "oauth2_timestamp_expires", expires_in);
|
||||
|
||||
if update_redirect_uri_on_success {
|
||||
sql::set_config(
|
||||
context,
|
||||
&context.sql,
|
||||
"oauth2_redirect_uri",
|
||||
Some(redirect_uri.as_ref()),
|
||||
);
|
||||
context
|
||||
.sql
|
||||
.set_config(context, "oauth2_redirect_uri", Some(redirect_uri.as_ref()));
|
||||
}
|
||||
} else {
|
||||
warn!(context, 0, "Failed to find OAuth2 access token");
|
||||
@@ -296,8 +294,10 @@ impl Oauth2 {
|
||||
}
|
||||
|
||||
fn is_expired(context: &Context) -> bool {
|
||||
let expire_timestamp =
|
||||
sql::get_config_int64(context, &context.sql, "oauth2_timestamp_expires", Some(0));
|
||||
let expire_timestamp = context
|
||||
.sql
|
||||
.get_config_int64(context, "oauth2_timestamp_expires")
|
||||
.unwrap_or_default();
|
||||
|
||||
if expire_timestamp <= 0 {
|
||||
return false;
|
||||
|
||||
@@ -387,12 +387,14 @@ impl<'a> Peerstate<'a> {
|
||||
}
|
||||
|
||||
if create {
|
||||
if !sql::execute(
|
||||
if sql::execute(
|
||||
self.context,
|
||||
sql,
|
||||
"INSERT INTO acpeerstates (addr) VALUES(?);",
|
||||
params![self.addr.as_ref().unwrap()],
|
||||
) {
|
||||
)
|
||||
.is_err()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -419,7 +421,7 @@ impl<'a> Peerstate<'a> {
|
||||
&self.verified_key_fingerprint,
|
||||
&self.addr,
|
||||
],
|
||||
);
|
||||
).is_ok();
|
||||
} else if self.to_save == Some(ToSave::Timestamps) {
|
||||
success = sql::execute(
|
||||
self.context,
|
||||
@@ -432,7 +434,8 @@ impl<'a> Peerstate<'a> {
|
||||
self.gossip_timestamp,
|
||||
&self.addr
|
||||
],
|
||||
);
|
||||
)
|
||||
.is_ok();
|
||||
}
|
||||
|
||||
if self.to_save == Some(ToSave::All) || create {
|
||||
@@ -459,10 +462,12 @@ mod tests {
|
||||
use super::*;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
use std::ffi::{CStr, CString};
|
||||
use std::ffi::CStr;
|
||||
use tempfile::{tempdir, TempDir};
|
||||
|
||||
use crate::context::*;
|
||||
use crate::dc_tools::to_cstring;
|
||||
use crate::x::free;
|
||||
|
||||
#[test]
|
||||
fn test_peerstate_save_to_db() {
|
||||
@@ -517,16 +522,16 @@ mod tests {
|
||||
unsafe fn create_test_context() -> TestContext {
|
||||
let mut ctx = dc_context_new(Some(cb), std::ptr::null_mut(), std::ptr::null_mut());
|
||||
let dir = tempdir().unwrap();
|
||||
let dbfile = CString::new(dir.path().join("db.sqlite").to_str().unwrap()).unwrap();
|
||||
let dbfile = to_cstring(dir.path().join("db.sqlite").to_str().unwrap());
|
||||
assert_eq!(
|
||||
dc_open(&mut ctx, dbfile.as_ptr(), std::ptr::null()),
|
||||
dc_open(&mut ctx, dbfile, std::ptr::null()),
|
||||
1,
|
||||
"Failed to open {}",
|
||||
CStr::from_ptr(dbfile.as_ptr() as *const libc::c_char)
|
||||
.to_str()
|
||||
.unwrap()
|
||||
CStr::from_ptr(dbfile as *const _).to_str().unwrap()
|
||||
);
|
||||
|
||||
free(dbfile as *mut _);
|
||||
|
||||
TestContext { ctx: ctx, dir: dir }
|
||||
}
|
||||
}
|
||||
|
||||
546
src/sql.rs
546
src/sql.rs
@@ -1,7 +1,8 @@
|
||||
use std::collections::HashSet;
|
||||
use std::sync::RwLock;
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
use rusqlite::{Connection, OpenFlags, Statement, NO_PARAMS};
|
||||
use thread_local_object::ThreadLocal;
|
||||
|
||||
use crate::constants::*;
|
||||
use crate::context::Context;
|
||||
@@ -16,12 +17,14 @@ const DC_OPEN_READONLY: usize = 0x01;
|
||||
/// A wrapper around the underlying Sqlite3 object.
|
||||
pub struct Sql {
|
||||
pool: RwLock<Option<r2d2::Pool<r2d2_sqlite::SqliteConnectionManager>>>,
|
||||
in_use: Arc<ThreadLocal<String>>,
|
||||
}
|
||||
|
||||
impl Sql {
|
||||
pub fn new() -> Sql {
|
||||
Sql {
|
||||
pool: RwLock::new(None),
|
||||
in_use: Arc::new(ThreadLocal::new()),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,11 +33,10 @@ impl Sql {
|
||||
}
|
||||
|
||||
pub fn close(&self, context: &Context) {
|
||||
let mut pool = self.pool.write().unwrap();
|
||||
if pool.is_some() {
|
||||
pool.take();
|
||||
// drop closes the connection
|
||||
}
|
||||
let _ = self.pool.write().unwrap().take();
|
||||
self.in_use.remove();
|
||||
// drop closes the connection
|
||||
|
||||
info!(context, 0, "Database closed.");
|
||||
}
|
||||
|
||||
@@ -55,6 +57,7 @@ impl Sql {
|
||||
P: IntoIterator,
|
||||
P::Item: rusqlite::ToSql,
|
||||
{
|
||||
self.start_stmt(sql.to_string());
|
||||
self.with_conn(|conn| conn.execute(sql, params).map_err(Into::into))
|
||||
}
|
||||
|
||||
@@ -62,22 +65,25 @@ impl Sql {
|
||||
where
|
||||
G: FnOnce(&Connection) -> Result<T>,
|
||||
{
|
||||
match &*self.pool.read().unwrap() {
|
||||
let res = match &*self.pool.read().unwrap() {
|
||||
Some(pool) => {
|
||||
let conn = pool.get()?;
|
||||
g(&conn)
|
||||
}
|
||||
None => Err(Error::SqlNoConnection),
|
||||
}
|
||||
};
|
||||
self.in_use.remove();
|
||||
res
|
||||
}
|
||||
|
||||
pub fn prepare<G, H>(&self, sql: &str, g: G) -> Result<H>
|
||||
where
|
||||
G: FnOnce(Statement<'_>) -> Result<H>,
|
||||
G: FnOnce(Statement<'_>, &Connection) -> Result<H>,
|
||||
{
|
||||
self.start_stmt(sql.to_string());
|
||||
self.with_conn(|conn| {
|
||||
let stmt = conn.prepare(sql)?;
|
||||
let res = g(stmt)?;
|
||||
let res = g(stmt, conn)?;
|
||||
Ok(res)
|
||||
})
|
||||
}
|
||||
@@ -86,6 +92,7 @@ impl Sql {
|
||||
where
|
||||
G: FnOnce(Statement<'_>, Statement<'_>, &Connection) -> Result<H>,
|
||||
{
|
||||
self.start_stmt(format!("{} - {}", sql1, sql2));
|
||||
self.with_conn(|conn| {
|
||||
let stmt1 = conn.prepare(sql1)?;
|
||||
let stmt2 = conn.prepare(sql2)?;
|
||||
@@ -98,15 +105,22 @@ impl Sql {
|
||||
/// Prepares and executes the statement and maps a function over the resulting rows.
|
||||
/// Then executes the second function over the returned iterator and returns the
|
||||
/// result of that function.
|
||||
pub fn query_map<T, P, F, G, H>(&self, sql: &str, params: P, f: F, mut g: G) -> Result<H>
|
||||
pub fn query_map<T, P, F, G, H>(
|
||||
&self,
|
||||
sql: impl AsRef<str>,
|
||||
params: P,
|
||||
f: F,
|
||||
mut g: G,
|
||||
) -> Result<H>
|
||||
where
|
||||
P: IntoIterator,
|
||||
P::Item: rusqlite::ToSql,
|
||||
F: FnMut(&rusqlite::Row) -> rusqlite::Result<T>,
|
||||
G: FnMut(rusqlite::MappedRows<F>) -> Result<H>,
|
||||
{
|
||||
self.start_stmt(sql.as_ref().to_string());
|
||||
self.with_conn(|conn| {
|
||||
let mut stmt = conn.prepare(sql)?;
|
||||
let mut stmt = conn.prepare(sql.as_ref())?;
|
||||
let res = stmt.query_map(params, f)?;
|
||||
g(res)
|
||||
})
|
||||
@@ -119,6 +133,7 @@ impl Sql {
|
||||
P: IntoIterator,
|
||||
P::Item: rusqlite::ToSql,
|
||||
{
|
||||
self.start_stmt(sql.to_string());
|
||||
self.with_conn(|conn| {
|
||||
let mut stmt = conn.prepare(sql)?;
|
||||
let res = stmt.exists(params)?;
|
||||
@@ -126,35 +141,158 @@ impl Sql {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn query_row<T, P, F>(&self, sql: &str, params: P, f: F) -> Result<T>
|
||||
pub fn query_row<T, P, F>(&self, sql: impl AsRef<str>, params: P, f: F) -> Result<T>
|
||||
where
|
||||
P: IntoIterator,
|
||||
P::Item: rusqlite::ToSql,
|
||||
F: FnOnce(&rusqlite::Row) -> rusqlite::Result<T>,
|
||||
{
|
||||
self.with_conn(|conn| conn.query_row(sql, params, f).map_err(Into::into))
|
||||
self.start_stmt(sql.as_ref().to_string());
|
||||
self.with_conn(|conn| conn.query_row(sql.as_ref(), params, f).map_err(Into::into))
|
||||
}
|
||||
|
||||
pub fn table_exists(&self, name: impl AsRef<str>) -> bool {
|
||||
self.with_conn(|conn| Ok(table_exists(conn, name)))
|
||||
self.with_conn(|conn| table_exists(conn, name))
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
pub fn query_row_col<P, T>(
|
||||
&self,
|
||||
context: &Context,
|
||||
query: &str,
|
||||
params: P,
|
||||
column: usize,
|
||||
) -> Option<T>
|
||||
where
|
||||
P: IntoIterator,
|
||||
P::Item: rusqlite::ToSql,
|
||||
T: rusqlite::types::FromSql,
|
||||
{
|
||||
match self.query_row(query, params, |row| row.get::<_, T>(column)) {
|
||||
Ok(res) => Some(res),
|
||||
Err(Error::Sql(rusqlite::Error::QueryReturnedNoRows)) => None,
|
||||
Err(Error::Sql(rusqlite::Error::InvalidColumnType(
|
||||
_,
|
||||
_,
|
||||
rusqlite::types::Type::Null,
|
||||
))) => None,
|
||||
Err(err) => {
|
||||
error!(context, 0, "sql: Failed query_row: {}", err);
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Set private configuration options.
|
||||
/// Setting `None` deletes the value.
|
||||
pub fn set_config(
|
||||
&self,
|
||||
context: &Context,
|
||||
key: impl AsRef<str>,
|
||||
value: Option<&str>,
|
||||
) -> Result<()> {
|
||||
if !self.is_open() {
|
||||
error!(context, 0, "set_config(): Database not ready.");
|
||||
return Err(Error::SqlNoConnection);
|
||||
}
|
||||
|
||||
let key = key.as_ref();
|
||||
let res = if let Some(ref value) = value {
|
||||
let exists = self.exists("SELECT value FROM config WHERE keyname=?;", params![key])?;
|
||||
if exists {
|
||||
execute(
|
||||
context,
|
||||
self,
|
||||
"UPDATE config SET value=? WHERE keyname=?;",
|
||||
params![value, key],
|
||||
)
|
||||
} else {
|
||||
execute(
|
||||
context,
|
||||
self,
|
||||
"INSERT INTO config (keyname, value) VALUES (?, ?);",
|
||||
params![key, value],
|
||||
)
|
||||
}
|
||||
} else {
|
||||
execute(
|
||||
context,
|
||||
self,
|
||||
"DELETE FROM config WHERE keyname=?;",
|
||||
params![key],
|
||||
)
|
||||
};
|
||||
|
||||
match res {
|
||||
Ok(_) => Ok(()),
|
||||
Err(err) => {
|
||||
error!(context, 0, "set_config(): Cannot change value. {:?}", &err);
|
||||
Err(err.into())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Get configuration options from the database.
|
||||
pub fn get_config(&self, context: &Context, key: impl AsRef<str>) -> Option<String> {
|
||||
if !self.is_open() || key.as_ref().is_empty() {
|
||||
return None;
|
||||
}
|
||||
self.query_row_col(
|
||||
context,
|
||||
"SELECT value FROM config WHERE keyname=?;",
|
||||
params![key.as_ref()],
|
||||
0,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn set_config_int(
|
||||
&self,
|
||||
context: &Context,
|
||||
key: impl AsRef<str>,
|
||||
value: i32,
|
||||
) -> Result<()> {
|
||||
self.set_config(context, key, Some(&format!("{}", value)))
|
||||
}
|
||||
|
||||
pub fn get_config_int(&self, context: &Context, key: impl AsRef<str>) -> Option<i32> {
|
||||
self.get_config(context, key).and_then(|s| s.parse().ok())
|
||||
}
|
||||
|
||||
pub fn set_config_int64(
|
||||
&self,
|
||||
context: &Context,
|
||||
key: impl AsRef<str>,
|
||||
value: i64,
|
||||
) -> Result<()> {
|
||||
self.set_config(context, key, Some(&format!("{}", value)))
|
||||
}
|
||||
|
||||
pub fn get_config_int64(&self, context: &Context, key: impl AsRef<str>) -> Option<i64> {
|
||||
self.get_config(context, key).and_then(|r| r.parse().ok())
|
||||
}
|
||||
|
||||
fn start_stmt(&self, stmt: impl AsRef<str>) {
|
||||
if let Some(query) = self.in_use.get_cloned() {
|
||||
let bt = backtrace::Backtrace::new();
|
||||
eprintln!("old query: {}", query);
|
||||
eprintln!("Connection is already used from this thread: {:?}", bt);
|
||||
panic!("Connection is already used from this thread");
|
||||
}
|
||||
|
||||
self.in_use.set(stmt.as_ref().to_string());
|
||||
}
|
||||
}
|
||||
|
||||
fn table_exists(conn: &Connection, name: impl AsRef<str>) -> bool {
|
||||
fn table_exists(conn: &Connection, name: impl AsRef<str>) -> Result<bool> {
|
||||
let mut exists = false;
|
||||
conn.pragma(None, "table_info", &format!("{}", name.as_ref()), |_row| {
|
||||
// will only be executed if the info was found
|
||||
exists = true;
|
||||
Ok(())
|
||||
})
|
||||
.expect("bad sqlite state");
|
||||
|
||||
exists
|
||||
})?;
|
||||
Ok(exists)
|
||||
}
|
||||
|
||||
// Return 1 -> success
|
||||
// Return 0 -> failure
|
||||
fn open(
|
||||
context: &Context,
|
||||
sql: &Sql,
|
||||
@@ -210,12 +348,12 @@ fn open(
|
||||
sql.execute(
|
||||
"CREATE TABLE contacts (\
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT, \
|
||||
name TEXT DEFAULT \'\', \
|
||||
addr TEXT DEFAULT \'\' COLLATE NOCASE, \
|
||||
name TEXT DEFAULT '', \
|
||||
addr TEXT DEFAULT '' COLLATE NOCASE, \
|
||||
origin INTEGER DEFAULT 0, \
|
||||
blocked INTEGER DEFAULT 0, \
|
||||
last_seen INTEGER DEFAULT 0, \
|
||||
param TEXT DEFAULT \'\');",
|
||||
param TEXT DEFAULT '');",
|
||||
params![],
|
||||
)?;
|
||||
sql.execute(
|
||||
@@ -228,21 +366,21 @@ fn open(
|
||||
)?;
|
||||
sql.execute(
|
||||
"INSERT INTO contacts (id,name,origin) VALUES \
|
||||
(1,\'self\',262144), (2,\'device\',262144), (3,\'rsvd\',262144), \
|
||||
(4,\'rsvd\',262144), (5,\'rsvd\',262144), (6,\'rsvd\',262144), \
|
||||
(7,\'rsvd\',262144), (8,\'rsvd\',262144), (9,\'rsvd\',262144);",
|
||||
(1,'self',262144), (2,'device',262144), (3,'rsvd',262144), \
|
||||
(4,'rsvd',262144), (5,'rsvd',262144), (6,'rsvd',262144), \
|
||||
(7,'rsvd',262144), (8,'rsvd',262144), (9,'rsvd',262144);",
|
||||
params![],
|
||||
)?;
|
||||
sql.execute(
|
||||
"CREATE TABLE chats (\
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT, \
|
||||
type INTEGER DEFAULT 0, \
|
||||
name TEXT DEFAULT \'\', \
|
||||
name TEXT DEFAULT '', \
|
||||
draft_timestamp INTEGER DEFAULT 0, \
|
||||
draft_txt TEXT DEFAULT \'\', \
|
||||
draft_txt TEXT DEFAULT '', \
|
||||
blocked INTEGER DEFAULT 0, \
|
||||
grpid TEXT DEFAULT \'\', \
|
||||
param TEXT DEFAULT \'\');",
|
||||
grpid TEXT DEFAULT '', \
|
||||
param TEXT DEFAULT '');",
|
||||
params![],
|
||||
)?;
|
||||
sql.execute("CREATE INDEX chats_index1 ON chats (grpid);", params![])?;
|
||||
@@ -256,16 +394,16 @@ fn open(
|
||||
)?;
|
||||
sql.execute(
|
||||
"INSERT INTO chats (id,type,name) VALUES \
|
||||
(1,120,\'deaddrop\'), (2,120,\'rsvd\'), (3,120,\'trash\'), \
|
||||
(4,120,\'msgs_in_creation\'), (5,120,\'starred\'), (6,120,\'archivedlink\'), \
|
||||
(7,100,\'rsvd\'), (8,100,\'rsvd\'), (9,100,\'rsvd\');",
|
||||
(1,120,'deaddrop'), (2,120,'rsvd'), (3,120,'trash'), \
|
||||
(4,120,'msgs_in_creation'), (5,120,'starred'), (6,120,'archivedlink'), \
|
||||
(7,100,'rsvd'), (8,100,'rsvd'), (9,100,'rsvd');",
|
||||
params![],
|
||||
)?;
|
||||
sql.execute(
|
||||
"CREATE TABLE msgs (\
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT, \
|
||||
rfc724_mid TEXT DEFAULT \'\', \
|
||||
server_folder TEXT DEFAULT \'\', \
|
||||
rfc724_mid TEXT DEFAULT '', \
|
||||
server_folder TEXT DEFAULT '', \
|
||||
server_uid INTEGER DEFAULT 0, \
|
||||
chat_id INTEGER DEFAULT 0, \
|
||||
from_id INTEGER DEFAULT 0, \
|
||||
@@ -275,9 +413,9 @@ fn open(
|
||||
state INTEGER DEFAULT 0, \
|
||||
msgrmsg INTEGER DEFAULT 1, \
|
||||
bytes INTEGER DEFAULT 0, \
|
||||
txt TEXT DEFAULT \'\', \
|
||||
txt_raw TEXT DEFAULT \'\', \
|
||||
param TEXT DEFAULT \'\');",
|
||||
txt TEXT DEFAULT '', \
|
||||
txt_raw TEXT DEFAULT '', \
|
||||
param TEXT DEFAULT '');",
|
||||
params![],
|
||||
)?;
|
||||
sql.execute("CREATE INDEX msgs_index1 ON msgs (rfc724_mid);", params![])?;
|
||||
@@ -286,9 +424,9 @@ fn open(
|
||||
sql.execute("CREATE INDEX msgs_index4 ON msgs (state);", params![])?;
|
||||
sql.execute(
|
||||
"INSERT INTO msgs (id,msgrmsg,txt) VALUES \
|
||||
(1,0,\'marker1\'), (2,0,\'rsvd\'), (3,0,\'rsvd\'), \
|
||||
(4,0,\'rsvd\'), (5,0,\'rsvd\'), (6,0,\'rsvd\'), (7,0,\'rsvd\'), \
|
||||
(8,0,\'rsvd\'), (9,0,\'daymarker\');",
|
||||
(1,0,'marker1'), (2,0,'rsvd'), (3,0,'rsvd'), \
|
||||
(4,0,'rsvd'), (5,0,'rsvd'), (6,0,'rsvd'), (7,0,'rsvd'), \
|
||||
(8,0,'rsvd'), (9,0,'daymarker');",
|
||||
params![],
|
||||
)?;
|
||||
sql.execute(
|
||||
@@ -298,7 +436,7 @@ fn open(
|
||||
desired_timestamp INTEGER DEFAULT 0, \
|
||||
action INTEGER, \
|
||||
foreign_id INTEGER, \
|
||||
param TEXT DEFAULT \'\');",
|
||||
param TEXT DEFAULT '');",
|
||||
params![],
|
||||
)?;
|
||||
sql.execute(
|
||||
@@ -321,11 +459,11 @@ fn open(
|
||||
// cannot create the tables - maybe we cannot write?
|
||||
return Err(Error::SqlFailedToOpen);
|
||||
} else {
|
||||
set_config_int(context, sql, "dbversion", 0);
|
||||
sql.set_config_int(context, "dbversion", 0);
|
||||
}
|
||||
} else {
|
||||
exists_before_update = 1;
|
||||
dbversion_before_update = get_config_int(context, sql, "dbversion", 0);
|
||||
dbversion_before_update = sql.get_config_int(context, "dbversion").unwrap_or_default();
|
||||
}
|
||||
|
||||
// (1) update low-level database structure.
|
||||
@@ -338,7 +476,7 @@ fn open(
|
||||
|
||||
if dbversion < 1 {
|
||||
sql.execute(
|
||||
"CREATE TABLE leftgrps ( id INTEGER PRIMARY KEY, grpid TEXT DEFAULT \'\');",
|
||||
"CREATE TABLE leftgrps ( id INTEGER PRIMARY KEY, grpid TEXT DEFAULT '');",
|
||||
params![],
|
||||
)?;
|
||||
sql.execute(
|
||||
@@ -346,21 +484,21 @@ fn open(
|
||||
params![],
|
||||
)?;
|
||||
dbversion = 1;
|
||||
set_config_int(context, sql, "dbversion", 1);
|
||||
sql.set_config_int(context, "dbversion", 1);
|
||||
}
|
||||
if dbversion < 2 {
|
||||
sql.execute(
|
||||
"ALTER TABLE contacts ADD COLUMN authname TEXT DEFAULT \'\';",
|
||||
"ALTER TABLE contacts ADD COLUMN authname TEXT DEFAULT '';",
|
||||
params![],
|
||||
)?;
|
||||
dbversion = 2;
|
||||
set_config_int(context, sql, "dbversion", 2);
|
||||
sql.set_config_int(context, "dbversion", 2);
|
||||
}
|
||||
if dbversion < 7 {
|
||||
sql.execute(
|
||||
"CREATE TABLE keypairs (\
|
||||
id INTEGER PRIMARY KEY, \
|
||||
addr TEXT DEFAULT \'\' COLLATE NOCASE, \
|
||||
addr TEXT DEFAULT '' COLLATE NOCASE, \
|
||||
is_default INTEGER DEFAULT 0, \
|
||||
private_key, \
|
||||
public_key, \
|
||||
@@ -368,13 +506,13 @@ fn open(
|
||||
params![],
|
||||
)?;
|
||||
dbversion = 7;
|
||||
set_config_int(context, sql, "dbversion", 7);
|
||||
sql.set_config_int(context, "dbversion", 7);
|
||||
}
|
||||
if dbversion < 10 {
|
||||
sql.execute(
|
||||
"CREATE TABLE acpeerstates (\
|
||||
id INTEGER PRIMARY KEY, \
|
||||
addr TEXT DEFAULT \'\' COLLATE NOCASE, \
|
||||
addr TEXT DEFAULT '' COLLATE NOCASE, \
|
||||
last_seen INTEGER DEFAULT 0, \
|
||||
last_seen_autocrypt INTEGER DEFAULT 0, \
|
||||
public_key, \
|
||||
@@ -386,7 +524,7 @@ fn open(
|
||||
params![],
|
||||
)?;
|
||||
dbversion = 10;
|
||||
set_config_int(context, sql, "dbversion", 10);
|
||||
sql.set_config_int(context, "dbversion", 10);
|
||||
}
|
||||
if dbversion < 12 {
|
||||
sql.execute(
|
||||
@@ -398,7 +536,7 @@ fn open(
|
||||
params![],
|
||||
)?;
|
||||
dbversion = 12;
|
||||
set_config_int(context, sql, "dbversion", 12);
|
||||
sql.set_config_int(context, "dbversion", 12);
|
||||
}
|
||||
if dbversion < 17 {
|
||||
sql.execute(
|
||||
@@ -412,7 +550,7 @@ fn open(
|
||||
)?;
|
||||
sql.execute("CREATE INDEX msgs_index5 ON msgs (starred);", params![])?;
|
||||
dbversion = 17;
|
||||
set_config_int(context, sql, "dbversion", 17);
|
||||
sql.set_config_int(context, "dbversion", 17);
|
||||
}
|
||||
if dbversion < 18 {
|
||||
sql.execute(
|
||||
@@ -421,7 +559,7 @@ fn open(
|
||||
)?;
|
||||
sql.execute("ALTER TABLE acpeerstates ADD COLUMN gossip_key;", params![])?;
|
||||
dbversion = 18;
|
||||
set_config_int(context, sql, "dbversion", 18);
|
||||
sql.set_config_int(context, "dbversion", 18);
|
||||
}
|
||||
if dbversion < 27 {
|
||||
sql.execute("DELETE FROM msgs WHERE chat_id=1 OR chat_id=2;", params![])?;
|
||||
@@ -438,7 +576,7 @@ fn open(
|
||||
params![],
|
||||
)?;
|
||||
dbversion = 27;
|
||||
set_config_int(context, sql, "dbversion", 27);
|
||||
sql.set_config_int(context, "dbversion", 27);
|
||||
}
|
||||
if dbversion < 34 {
|
||||
sql.execute(
|
||||
@@ -450,11 +588,11 @@ fn open(
|
||||
params![],
|
||||
)?;
|
||||
sql.execute(
|
||||
"ALTER TABLE acpeerstates ADD COLUMN public_key_fingerprint TEXT DEFAULT \'\';",
|
||||
"ALTER TABLE acpeerstates ADD COLUMN public_key_fingerprint TEXT DEFAULT '';",
|
||||
params![],
|
||||
)?;
|
||||
sql.execute(
|
||||
"ALTER TABLE acpeerstates ADD COLUMN gossip_key_fingerprint TEXT DEFAULT \'\';",
|
||||
"ALTER TABLE acpeerstates ADD COLUMN gossip_key_fingerprint TEXT DEFAULT '';",
|
||||
params![],
|
||||
)?;
|
||||
sql.execute(
|
||||
@@ -467,11 +605,11 @@ fn open(
|
||||
)?;
|
||||
recalc_fingerprints = 1;
|
||||
dbversion = 34;
|
||||
set_config_int(context, sql, "dbversion", 34);
|
||||
sql.set_config_int(context, "dbversion", 34);
|
||||
}
|
||||
if dbversion < 39 {
|
||||
sql.execute(
|
||||
"CREATE TABLE tokens ( id INTEGER PRIMARY KEY, namespc INTEGER DEFAULT 0, foreign_id INTEGER DEFAULT 0, token TEXT DEFAULT \'\', timestamp INTEGER DEFAULT 0);",
|
||||
"CREATE TABLE tokens ( id INTEGER PRIMARY KEY, namespc INTEGER DEFAULT 0, foreign_id INTEGER DEFAULT 0, token TEXT DEFAULT '', timestamp INTEGER DEFAULT 0);",
|
||||
params![]
|
||||
)?;
|
||||
sql.execute(
|
||||
@@ -479,7 +617,7 @@ fn open(
|
||||
params![],
|
||||
)?;
|
||||
sql.execute(
|
||||
"ALTER TABLE acpeerstates ADD COLUMN verified_key_fingerprint TEXT DEFAULT \'\';",
|
||||
"ALTER TABLE acpeerstates ADD COLUMN verified_key_fingerprint TEXT DEFAULT '';",
|
||||
params![],
|
||||
)?;
|
||||
sql.execute(
|
||||
@@ -497,7 +635,7 @@ fn open(
|
||||
)?;
|
||||
}
|
||||
dbversion = 39;
|
||||
set_config_int(context, sql, "dbversion", 39);
|
||||
sql.set_config_int(context, "dbversion", 39);
|
||||
}
|
||||
if dbversion < 40 {
|
||||
sql.execute(
|
||||
@@ -505,22 +643,22 @@ fn open(
|
||||
params![],
|
||||
)?;
|
||||
dbversion = 40;
|
||||
set_config_int(context, sql, "dbversion", 40);
|
||||
sql.set_config_int(context, "dbversion", 40);
|
||||
}
|
||||
if dbversion < 41 {
|
||||
update_file_paths = 1;
|
||||
dbversion = 41;
|
||||
set_config_int(context, sql, "dbversion", 41);
|
||||
sql.set_config_int(context, "dbversion", 41);
|
||||
}
|
||||
if dbversion < 42 {
|
||||
sql.execute("UPDATE msgs SET txt=\'\' WHERE type!=10", params![])?;
|
||||
sql.execute("UPDATE msgs SET txt='' WHERE type!=10", params![])?;
|
||||
dbversion = 42;
|
||||
set_config_int(context, sql, "dbversion", 42);
|
||||
sql.set_config_int(context, "dbversion", 42);
|
||||
}
|
||||
if dbversion < 44 {
|
||||
sql.execute("ALTER TABLE msgs ADD COLUMN mime_headers TEXT;", params![])?;
|
||||
dbversion = 44;
|
||||
set_config_int(context, sql, "dbversion", 44);
|
||||
sql.set_config_int(context, "dbversion", 44);
|
||||
}
|
||||
if dbversion < 46 {
|
||||
sql.execute(
|
||||
@@ -532,7 +670,7 @@ fn open(
|
||||
params![],
|
||||
)?;
|
||||
dbversion = 46;
|
||||
set_config_int(context, sql, "dbversion", 46);
|
||||
sql.set_config_int(context, "dbversion", 46);
|
||||
}
|
||||
if dbversion < 47 {
|
||||
info!(context, 0, "[migration] v47");
|
||||
@@ -541,7 +679,7 @@ fn open(
|
||||
params![],
|
||||
)?;
|
||||
dbversion = 47;
|
||||
set_config_int(context, sql, "dbversion", 47);
|
||||
sql.set_config_int(context, "dbversion", 47);
|
||||
}
|
||||
if dbversion < 48 {
|
||||
info!(context, 0, "[migration] v48");
|
||||
@@ -555,7 +693,7 @@ fn open(
|
||||
assert_eq!(DC_MOVE_STATE_MOVING as libc::c_int, 3);
|
||||
|
||||
dbversion = 48;
|
||||
set_config_int(context, sql, "dbversion", 48);
|
||||
sql.set_config_int(context, "dbversion", 48);
|
||||
}
|
||||
if dbversion < 49 {
|
||||
info!(context, 0, "[migration] v49");
|
||||
@@ -564,15 +702,15 @@ fn open(
|
||||
params![],
|
||||
)?;
|
||||
dbversion = 49;
|
||||
set_config_int(context, sql, "dbversion", 49);
|
||||
sql.set_config_int(context, "dbversion", 49);
|
||||
}
|
||||
if dbversion < 50 {
|
||||
info!(context, 0, "[migration] v50");
|
||||
if 0 != exists_before_update {
|
||||
set_config_int(context, sql, "show_emails", 2);
|
||||
sql.set_config_int(context, "show_emails", 2);
|
||||
}
|
||||
dbversion = 50;
|
||||
set_config_int(context, sql, "dbversion", 50);
|
||||
sql.set_config_int(context, "dbversion", 50);
|
||||
}
|
||||
if dbversion < 53 {
|
||||
info!(context, 0, "[migration] v53");
|
||||
@@ -605,7 +743,7 @@ fn open(
|
||||
params![],
|
||||
)?;
|
||||
dbversion = 53;
|
||||
set_config_int(context, sql, "dbversion", 53);
|
||||
sql.set_config_int(context, "dbversion", 53);
|
||||
}
|
||||
if dbversion < 54 {
|
||||
info!(context, 0, "[migration] v54");
|
||||
@@ -615,7 +753,7 @@ fn open(
|
||||
)?;
|
||||
sql.execute("CREATE INDEX msgs_index6 ON msgs (location_id);", params![])?;
|
||||
dbversion = 54;
|
||||
set_config_int(context, sql, "dbversion", 54);
|
||||
sql.set_config_int(context, "dbversion", 54);
|
||||
}
|
||||
if dbversion < 55 {
|
||||
sql.execute(
|
||||
@@ -623,7 +761,7 @@ fn open(
|
||||
params![],
|
||||
)?;
|
||||
|
||||
set_config_int(context, sql, "dbversion", 55);
|
||||
sql.set_config_int(context, "dbversion", 55);
|
||||
}
|
||||
|
||||
if 0 != recalc_fingerprints {
|
||||
@@ -650,18 +788,14 @@ fn open(
|
||||
|
||||
info!(context, 0, "[open] update file paths");
|
||||
|
||||
let repl_from = get_config(
|
||||
context,
|
||||
sql,
|
||||
"backup_for",
|
||||
Some(as_str(context.get_blobdir())),
|
||||
)
|
||||
.unwrap();
|
||||
let repl_from = sql
|
||||
.get_config(context, "backup_for")
|
||||
.unwrap_or_else(|| to_string(context.get_blobdir()));
|
||||
|
||||
let repl_from = dc_ensure_no_slash_safe(&repl_from);
|
||||
sql.execute(
|
||||
&format!(
|
||||
"UPDATE msgs SET param=replace(param, \'f={}/\', \'f=$BLOBDIR/\')",
|
||||
"UPDATE msgs SET param=replace(param, 'f={}/', 'f=$BLOBDIR/')",
|
||||
repl_from
|
||||
),
|
||||
NO_PARAMS,
|
||||
@@ -669,13 +803,13 @@ fn open(
|
||||
|
||||
sql.execute(
|
||||
&format!(
|
||||
"UPDATE chats SET param=replace(param, \'i={}/\', \'i=$BLOBDIR/\');",
|
||||
"UPDATE chats SET param=replace(param, 'i={}/', 'i=$BLOBDIR/');",
|
||||
repl_from
|
||||
),
|
||||
NO_PARAMS,
|
||||
)?;
|
||||
|
||||
set_config(context, sql, "backup_for", None);
|
||||
sql.set_config(context, "backup_for", None);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -684,164 +818,39 @@ fn open(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// handle configurations, private
|
||||
pub fn set_config(
|
||||
context: &Context,
|
||||
sql: &Sql,
|
||||
key: impl AsRef<str>,
|
||||
value: Option<&str>,
|
||||
) -> libc::c_int {
|
||||
if !sql.is_open() {
|
||||
error!(context, 0, "set_config(): Database not ready.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
let key = key.as_ref();
|
||||
let good;
|
||||
|
||||
if let Some(ref value) = value {
|
||||
let exists = sql
|
||||
.exists("SELECT value FROM config WHERE keyname=?;", params![key])
|
||||
.unwrap_or_default();
|
||||
if exists {
|
||||
good = execute(
|
||||
context,
|
||||
sql,
|
||||
"UPDATE config SET value=? WHERE keyname=?;",
|
||||
params![value, key],
|
||||
);
|
||||
} else {
|
||||
good = execute(
|
||||
context,
|
||||
sql,
|
||||
"INSERT INTO config (keyname, value) VALUES (?, ?);",
|
||||
params![key, value],
|
||||
);
|
||||
}
|
||||
} else {
|
||||
good = execute(
|
||||
context,
|
||||
sql,
|
||||
"DELETE FROM config WHERE keyname=?;",
|
||||
params![key],
|
||||
);
|
||||
}
|
||||
|
||||
if !good {
|
||||
error!(context, 0, "set_config(): Cannot change value.",);
|
||||
return 0;
|
||||
}
|
||||
|
||||
1
|
||||
}
|
||||
|
||||
pub fn get_config(
|
||||
context: &Context,
|
||||
sql: &Sql,
|
||||
key: impl AsRef<str>,
|
||||
def: Option<&str>,
|
||||
) -> Option<String> {
|
||||
if !sql.is_open() || key.as_ref().is_empty() {
|
||||
return None;
|
||||
}
|
||||
query_row(
|
||||
context,
|
||||
sql,
|
||||
"SELECT value FROM config WHERE keyname=?;",
|
||||
params![key.as_ref()],
|
||||
0,
|
||||
)
|
||||
.or_else(|| def.map(|s| s.to_string()))
|
||||
}
|
||||
|
||||
pub fn execute<P>(context: &Context, sql: &Sql, querystr: impl AsRef<str>, params: P) -> bool
|
||||
pub fn execute<P>(context: &Context, sql: &Sql, querystr: impl AsRef<str>, params: P) -> Result<()>
|
||||
where
|
||||
P: IntoIterator,
|
||||
P::Item: rusqlite::ToSql,
|
||||
{
|
||||
match sql.execute(querystr.as_ref(), params) {
|
||||
Ok(_) => true,
|
||||
Ok(_) => Ok(()),
|
||||
Err(err) => {
|
||||
error!(context, 0, "execute failed: {:?}", err);
|
||||
false
|
||||
error!(
|
||||
context,
|
||||
0,
|
||||
"execute failed: {:?} for {}",
|
||||
&err,
|
||||
querystr.as_ref()
|
||||
);
|
||||
Err(err.into())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO Remove the Option<> from the return type.
|
||||
pub fn query_row<P, T>(
|
||||
context: &Context,
|
||||
sql: &Sql,
|
||||
query: &str,
|
||||
params: P,
|
||||
column: usize,
|
||||
) -> Option<T>
|
||||
where
|
||||
P: IntoIterator,
|
||||
P::Item: rusqlite::ToSql,
|
||||
T: rusqlite::types::FromSql,
|
||||
{
|
||||
match sql.query_row(query, params, |row| row.get::<_, T>(column)) {
|
||||
Ok(res) => Some(res),
|
||||
Err(Error::Sql(rusqlite::Error::QueryReturnedNoRows)) => None,
|
||||
Err(Error::Sql(rusqlite::Error::InvalidColumnType(_, _, rusqlite::types::Type::Null))) => {
|
||||
None
|
||||
}
|
||||
Err(err) => {
|
||||
error!(context, 0, "sql: Failed query_row: {}", err);
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_config_int(
|
||||
context: &Context,
|
||||
sql: &Sql,
|
||||
key: impl AsRef<str>,
|
||||
value: i32,
|
||||
) -> libc::c_int {
|
||||
set_config(context, sql, key, Some(&format!("{}", value)))
|
||||
}
|
||||
|
||||
pub fn get_config_int(context: &Context, sql: &Sql, key: impl AsRef<str>, def: i32) -> i32 {
|
||||
get_config(context, sql, key, None)
|
||||
.and_then(|s| s.parse().ok())
|
||||
.unwrap_or_else(|| def)
|
||||
}
|
||||
|
||||
pub fn set_config_int64(
|
||||
context: &Context,
|
||||
sql: &Sql,
|
||||
key: impl AsRef<str>,
|
||||
value: i64,
|
||||
) -> libc::c_int {
|
||||
set_config(context, sql, key, Some(&format!("{}", value)))
|
||||
}
|
||||
|
||||
pub fn get_config_int64(
|
||||
context: &Context,
|
||||
sql: &Sql,
|
||||
key: impl AsRef<str>,
|
||||
def: Option<i64>,
|
||||
) -> i64 {
|
||||
let ret = get_config(context, sql, key, None);
|
||||
ret.map(|r| r.parse().unwrap_or_default())
|
||||
.unwrap_or_else(|| def.unwrap_or_default())
|
||||
}
|
||||
|
||||
pub fn try_execute(context: &Context, sql: &Sql, querystr: impl AsRef<str>) -> libc::c_int {
|
||||
pub fn try_execute(context: &Context, sql: &Sql, querystr: impl AsRef<str>) -> Result<()> {
|
||||
// same as execute() but does not pass error to ui
|
||||
match sql.execute(querystr.as_ref(), params![]) {
|
||||
Ok(_) => 1,
|
||||
Ok(_) => Ok(()),
|
||||
Err(err) => {
|
||||
warn!(
|
||||
context,
|
||||
0,
|
||||
"Try-execute for \"{}\" failed: {}",
|
||||
querystr.as_ref(),
|
||||
err,
|
||||
&err,
|
||||
);
|
||||
0
|
||||
Err(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -852,10 +861,22 @@ pub fn get_rowid(
|
||||
table: impl AsRef<str>,
|
||||
field: impl AsRef<str>,
|
||||
value: impl AsRef<str>,
|
||||
) -> u32 {
|
||||
sql.start_stmt("get rowid".to_string());
|
||||
sql.with_conn(|conn| Ok(get_rowid_with_conn(context, conn, table, field, value)))
|
||||
.unwrap_or_else(|_| 0)
|
||||
}
|
||||
|
||||
pub fn get_rowid_with_conn(
|
||||
context: &Context,
|
||||
conn: &Connection,
|
||||
table: impl AsRef<str>,
|
||||
field: impl AsRef<str>,
|
||||
value: impl AsRef<str>,
|
||||
) -> u32 {
|
||||
// alternative to sqlite3_last_insert_rowid() which MUST NOT be used due to race conditions, see comment above.
|
||||
// the ORDER BY ensures, this function always returns the most recent id,
|
||||
// eg. if a Message-ID is splitted into different messages.
|
||||
// eg. if a Message-ID is split into different messages.
|
||||
let query = format!(
|
||||
"SELECT id FROM {} WHERE {}='{}' ORDER BY id DESC",
|
||||
table.as_ref(),
|
||||
@@ -863,7 +884,7 @@ pub fn get_rowid(
|
||||
value.as_ref()
|
||||
);
|
||||
|
||||
match sql.query_row(&query, NO_PARAMS, |row| row.get::<_, u32>(0)) {
|
||||
match conn.query_row(&query, NO_PARAMS, |row| row.get::<_, u32>(0)) {
|
||||
Ok(id) => id,
|
||||
Err(err) => {
|
||||
error!(
|
||||
@@ -874,7 +895,6 @@ pub fn get_rowid(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_rowid2(
|
||||
context: &Context,
|
||||
sql: &Sql,
|
||||
@@ -884,6 +904,7 @@ pub fn get_rowid2(
|
||||
field2: impl AsRef<str>,
|
||||
value2: i32,
|
||||
) -> u32 {
|
||||
sql.start_stmt("get rowid2".to_string());
|
||||
sql.with_conn(|conn| {
|
||||
Ok(get_rowid2_with_conn(
|
||||
context, conn, table, field, value, field2, value2,
|
||||
@@ -983,31 +1004,36 @@ pub fn housekeeping(context: &Context) {
|
||||
}
|
||||
let entry = entry.unwrap();
|
||||
let name_f = entry.file_name();
|
||||
let name_c = to_cstring(name_f.to_string_lossy());
|
||||
let name_c = unsafe { to_cstring(name_f.to_string_lossy()) };
|
||||
|
||||
if unsafe {
|
||||
is_file_in_use(&mut files_in_use, 0 as *const libc::c_char, name_c.as_ptr())
|
||||
} || unsafe {
|
||||
is_file_in_use(
|
||||
&mut files_in_use,
|
||||
b".increation\x00" as *const u8 as *const libc::c_char,
|
||||
name_c.as_ptr(),
|
||||
)
|
||||
} || unsafe {
|
||||
is_file_in_use(
|
||||
&mut files_in_use,
|
||||
b".waveform\x00" as *const u8 as *const libc::c_char,
|
||||
name_c.as_ptr(),
|
||||
)
|
||||
} || unsafe {
|
||||
is_file_in_use(
|
||||
&mut files_in_use,
|
||||
b"-preview.jpg\x00" as *const u8 as *const libc::c_char,
|
||||
name_c.as_ptr(),
|
||||
)
|
||||
} {
|
||||
if unsafe { is_file_in_use(&mut files_in_use, 0 as *const libc::c_char, name_c) }
|
||||
|| unsafe {
|
||||
is_file_in_use(
|
||||
&mut files_in_use,
|
||||
b".increation\x00" as *const u8 as *const libc::c_char,
|
||||
name_c,
|
||||
)
|
||||
}
|
||||
|| unsafe {
|
||||
is_file_in_use(
|
||||
&mut files_in_use,
|
||||
b".waveform\x00" as *const u8 as *const libc::c_char,
|
||||
name_c,
|
||||
)
|
||||
}
|
||||
|| unsafe {
|
||||
is_file_in_use(
|
||||
&mut files_in_use,
|
||||
b"-preview.jpg\x00" as *const u8 as *const libc::c_char,
|
||||
name_c,
|
||||
)
|
||||
}
|
||||
{
|
||||
unsafe { free(name_c as *mut _) };
|
||||
continue;
|
||||
}
|
||||
unsafe { free(name_c as *mut _) };
|
||||
|
||||
unreferenced_count += 1;
|
||||
|
||||
match std::fs::metadata(entry.path()) {
|
||||
@@ -1039,8 +1065,11 @@ pub fn housekeeping(context: &Context) {
|
||||
unreferenced_count,
|
||||
entry.file_name()
|
||||
);
|
||||
let path = to_cstring(entry.path().to_str().unwrap());
|
||||
unsafe { dc_delete_file(context, path.as_ptr()) };
|
||||
unsafe {
|
||||
let path = to_cstring(entry.path().to_str().unwrap());
|
||||
dc_delete_file(context, path);
|
||||
free(path as *mut _);
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
@@ -1098,14 +1127,16 @@ fn maybe_add_from_param(
|
||||
context
|
||||
.sql
|
||||
.query_row(query, NO_PARAMS, |row| {
|
||||
let v = to_cstring(row.get::<_, String>(0)?);
|
||||
unsafe {
|
||||
dc_param_set_packed(param, v.as_ptr() as *const libc::c_char);
|
||||
let file = dc_param_get(param, param_id, 0 as *const libc::c_char);
|
||||
let v = to_cstring(row.get::<_, String>(0)?);
|
||||
dc_param_set_packed(param, v as *const _);
|
||||
let file = dc_param_get(param, param_id, 0 as *const _);
|
||||
if !file.is_null() {
|
||||
maybe_add_file(files_in_use, as_str(file));
|
||||
free(file as *mut libc::c_void);
|
||||
}
|
||||
|
||||
free(v as *mut _);
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
@@ -1139,7 +1170,6 @@ mod test {
|
||||
maybe_add_file(&mut files, "$BLOBDIR/world.txt");
|
||||
maybe_add_file(&mut files, "world2.txt");
|
||||
|
||||
println!("{:?}", files);
|
||||
assert!(unsafe {
|
||||
is_file_in_use(
|
||||
&mut files,
|
||||
|
||||
29
src/top_evil_rs.py
Executable file
29
src/top_evil_rs.py
Executable file
@@ -0,0 +1,29 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
|
||||
import os
|
||||
|
||||
if __name__ == "__main__":
|
||||
filestats = []
|
||||
for fn in os.listdir():
|
||||
if fn.endswith(".rs"):
|
||||
s = open(fn).read()
|
||||
unsafe = s.count("unsafe")
|
||||
free = s.count("free(")
|
||||
gotoblocks = s.count("current_block =")
|
||||
filestats.append((fn, unsafe, free, gotoblocks))
|
||||
|
||||
sum_unsafe, sum_free, sum_gotoblocks = 0, 0, 0
|
||||
|
||||
for fn, unsafe, free, gotoblocks in reversed(sorted(filestats, key=lambda x: sum(x[1:]))):
|
||||
print("{0: <30} unsafe: {1: >3} free: {2: >3} goto-blocks: {3: >3}".format(fn, unsafe, free, gotoblocks))
|
||||
sum_unsafe += unsafe
|
||||
sum_free += free
|
||||
sum_gotoblocks += gotoblocks
|
||||
|
||||
|
||||
print()
|
||||
print("total unsafe:", sum_unsafe)
|
||||
print("total free:", sum_free)
|
||||
print("total gotoblocks:", sum_gotoblocks)
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
//! Stress some functions for testing; if used as a lib, this file is obsolete.
|
||||
|
||||
use std::collections::HashSet;
|
||||
use std::ffi::CString;
|
||||
|
||||
use mmime::mailimf_types::*;
|
||||
use tempfile::{tempdir, TempDir};
|
||||
|
||||
use deltachat::config;
|
||||
use deltachat::constants::*;
|
||||
use deltachat::context::*;
|
||||
use deltachat::dc_array::*;
|
||||
@@ -174,7 +174,7 @@ unsafe fn stress_functions(context: &Context) {
|
||||
"content"
|
||||
);
|
||||
|
||||
free(buf);
|
||||
free(buf as *mut _);
|
||||
assert_ne!(
|
||||
0,
|
||||
dc_delete_file(
|
||||
@@ -247,7 +247,7 @@ unsafe fn stress_functions(context: &Context) {
|
||||
free(fn1 as *mut libc::c_void);
|
||||
}
|
||||
|
||||
let res = dc_get_config(context, "sys.config_keys");
|
||||
let res = context.get_config(config::Config::SysConfigKeys).unwrap();
|
||||
|
||||
assert!(!res.contains(" probably_never_a_key "));
|
||||
assert!(res.contains(" addr "));
|
||||
@@ -690,7 +690,7 @@ fn test_encryption_decryption() {
|
||||
assert!(ctext.starts_with("-----BEGIN PGP MESSAGE-----"));
|
||||
|
||||
let ctext_signed_bytes = ctext.len();
|
||||
let ctext_signed = CString::new(ctext).unwrap();
|
||||
let ctext_signed = to_cstring(ctext);
|
||||
|
||||
let ctext = dc_pgp_pk_encrypt(
|
||||
original_text as *const libc::c_void,
|
||||
@@ -703,7 +703,7 @@ fn test_encryption_decryption() {
|
||||
assert!(ctext.starts_with("-----BEGIN PGP MESSAGE-----"));
|
||||
|
||||
let ctext_unsigned_bytes = ctext.len();
|
||||
let ctext_unsigned = CString::new(ctext).unwrap();
|
||||
let ctext_unsigned = to_cstring(ctext);
|
||||
|
||||
let mut keyring = Keyring::default();
|
||||
keyring.add_owned(private_key);
|
||||
@@ -717,7 +717,7 @@ fn test_encryption_decryption() {
|
||||
let mut valid_signatures: HashSet<String> = Default::default();
|
||||
|
||||
let plain = dc_pgp_pk_decrypt(
|
||||
ctext_signed.as_ptr() as *const _,
|
||||
ctext_signed as *const _,
|
||||
ctext_signed_bytes,
|
||||
&keyring,
|
||||
&public_keyring,
|
||||
@@ -732,7 +732,7 @@ fn test_encryption_decryption() {
|
||||
|
||||
let empty_keyring = Keyring::default();
|
||||
let plain = dc_pgp_pk_decrypt(
|
||||
ctext_signed.as_ptr() as *const _,
|
||||
ctext_signed as *const _,
|
||||
ctext_signed_bytes,
|
||||
&keyring,
|
||||
&empty_keyring,
|
||||
@@ -745,7 +745,7 @@ fn test_encryption_decryption() {
|
||||
valid_signatures.clear();
|
||||
|
||||
let plain = dc_pgp_pk_decrypt(
|
||||
ctext_signed.as_ptr() as *const _,
|
||||
ctext_signed as *const _,
|
||||
ctext_signed_bytes,
|
||||
&keyring,
|
||||
&public_keyring2,
|
||||
@@ -760,7 +760,7 @@ fn test_encryption_decryption() {
|
||||
public_keyring2.add_ref(&public_key);
|
||||
|
||||
let plain = dc_pgp_pk_decrypt(
|
||||
ctext_signed.as_ptr() as *const _,
|
||||
ctext_signed as *const _,
|
||||
ctext_signed_bytes,
|
||||
&keyring,
|
||||
&public_keyring2,
|
||||
@@ -773,13 +773,15 @@ fn test_encryption_decryption() {
|
||||
valid_signatures.clear();
|
||||
|
||||
let plain = dc_pgp_pk_decrypt(
|
||||
ctext_unsigned.as_ptr() as *const _,
|
||||
ctext_unsigned as *const _,
|
||||
ctext_unsigned_bytes,
|
||||
&keyring,
|
||||
&public_keyring,
|
||||
Some(&mut valid_signatures),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
free(ctext_unsigned as *mut _);
|
||||
assert_eq!(std::str::from_utf8(&plain).unwrap(), as_str(original_text),);
|
||||
|
||||
valid_signatures.clear();
|
||||
@@ -790,13 +792,15 @@ fn test_encryption_decryption() {
|
||||
public_keyring.add_ref(&public_key);
|
||||
|
||||
let plain = dc_pgp_pk_decrypt(
|
||||
ctext_signed.as_ptr() as *const _,
|
||||
ctext_signed as *const _,
|
||||
ctext_signed_bytes,
|
||||
&keyring,
|
||||
&public_keyring,
|
||||
None,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
free(ctext_signed as *mut _);
|
||||
assert_eq!(std::str::from_utf8(&plain).unwrap(), as_str(original_text),);
|
||||
}
|
||||
}
|
||||
@@ -819,14 +823,14 @@ struct TestContext {
|
||||
unsafe fn create_test_context() -> TestContext {
|
||||
let mut ctx = dc_context_new(Some(cb), std::ptr::null_mut(), std::ptr::null_mut());
|
||||
let dir = tempdir().unwrap();
|
||||
let dbfile = CString::new(dir.path().join("db.sqlite").to_str().unwrap()).unwrap();
|
||||
let dbfile = to_cstring(dir.path().join("db.sqlite").to_str().unwrap());
|
||||
assert_eq!(
|
||||
dc_open(&mut ctx, dbfile.as_ptr(), std::ptr::null()),
|
||||
dc_open(&mut ctx, dbfile, std::ptr::null()),
|
||||
1,
|
||||
"Failed to open {}",
|
||||
as_str(dbfile.as_ptr() as *const libc::c_char)
|
||||
as_str(dbfile as *const libc::c_char)
|
||||
);
|
||||
|
||||
free(dbfile as *mut _);
|
||||
TestContext { ctx: ctx, dir: dir }
|
||||
}
|
||||
|
||||
@@ -951,24 +955,29 @@ fn test_stress_tests() {
|
||||
fn test_get_contacts() {
|
||||
unsafe {
|
||||
let context = create_test_context();
|
||||
let contacts = dc_get_contacts(&context.ctx, 0, to_cstring("some2").as_ptr());
|
||||
let name = to_cstring("some2");
|
||||
let contacts = dc_get_contacts(&context.ctx, 0, name);
|
||||
assert_eq!(dc_array_get_cnt(contacts), 0);
|
||||
dc_array_unref(contacts);
|
||||
free(name as *mut _);
|
||||
|
||||
let id = dc_create_contact(
|
||||
&context.ctx,
|
||||
to_cstring("bob").as_ptr(),
|
||||
to_cstring("bob@mail.de").as_ptr(),
|
||||
);
|
||||
let name = to_cstring("bob");
|
||||
let email = to_cstring("bob@mail.de");
|
||||
let id = dc_create_contact(&context.ctx, name, email);
|
||||
assert_ne!(id, 0);
|
||||
|
||||
let contacts = dc_get_contacts(&context.ctx, 0, to_cstring("bob").as_ptr());
|
||||
let contacts = dc_get_contacts(&context.ctx, 0, name);
|
||||
assert_eq!(dc_array_get_cnt(contacts), 1);
|
||||
dc_array_unref(contacts);
|
||||
|
||||
let contacts = dc_get_contacts(&context.ctx, 0, to_cstring("alice").as_ptr());
|
||||
let name2 = to_cstring("alice");
|
||||
let contacts = dc_get_contacts(&context.ctx, 0, name2);
|
||||
assert_eq!(dc_array_get_cnt(contacts), 0);
|
||||
dc_array_unref(contacts);
|
||||
|
||||
free(name as *mut _);
|
||||
free(name2 as *mut _);
|
||||
free(email as *mut _);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -976,11 +985,12 @@ fn test_get_contacts() {
|
||||
fn test_chat() {
|
||||
unsafe {
|
||||
let context = create_test_context();
|
||||
let contact1 = dc_create_contact(
|
||||
&context.ctx,
|
||||
to_cstring("bob").as_ptr(),
|
||||
to_cstring("bob@mail.de").as_ptr(),
|
||||
);
|
||||
let name = to_cstring("bob");
|
||||
let email = to_cstring("bob@mail.de");
|
||||
|
||||
let contact1 = dc_create_contact(&context.ctx, name, email);
|
||||
free(name as *mut _);
|
||||
free(email as *mut _);
|
||||
assert_ne!(contact1, 0);
|
||||
|
||||
let chat_id = dc_create_chat_by_contact_id(&context.ctx, contact1);
|
||||
@@ -997,6 +1007,21 @@ fn test_chat() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_wrong_db() {
|
||||
unsafe {
|
||||
let mut ctx = dc_context_new(Some(cb), std::ptr::null_mut(), std::ptr::null_mut());
|
||||
let dir = tempdir().unwrap();
|
||||
let dbfile = dir.path().join("db.sqlite");
|
||||
std::fs::write(&dbfile, b"123").unwrap();
|
||||
|
||||
let dbfile_c = to_cstring(dbfile.to_str().unwrap());
|
||||
let res = dc_open(&mut ctx, dbfile_c, std::ptr::null());
|
||||
free(dbfile_c as *mut _);
|
||||
assert_eq!(res, 0);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_arr_to_string() {
|
||||
let arr2: [uint32_t; 4] = [
|
||||
|
||||
Reference in New Issue
Block a user