more conversion..

This commit is contained in:
dignifiedquire
2019-06-08 23:13:08 +02:00
parent 2565fb27d5
commit 460eff0226
8 changed files with 542 additions and 833 deletions
+4 -6
View File
@@ -421,11 +421,7 @@ 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<impl AsRef<str>>,
) -> libc::c_int {
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) {
@@ -459,7 +455,7 @@ pub fn dc_set_config(
}
"selfstatus" => {
let def = unsafe { dc_stock_str(context, 13) };
let val = if value.is_none() || value.as_ref().unwrap().as_ref() == as_str(def) {
let val = if value.is_none() || value.unwrap() == as_str(def) {
None
} else {
value
@@ -609,6 +605,7 @@ pub unsafe fn dc_get_info(context: &Context) -> *mut libc::c_char {
context,
&context.sql.clone().read().unwrap(),
"SELECT COUNT(*) FROM keypairs;",
rusqlite::NO_PARAMS,
0,
);
@@ -616,6 +613,7 @@ pub unsafe fn dc_get_info(context: &Context) -> *mut libc::c_char {
context,
&context.sql.clone().read().unwrap(),
"SELECT COUNT(*) FROM acpeerstates;",
rusqlite::NO_PARAMS,
0,
);
+330 -409
View File
@@ -116,100 +116,86 @@ pub unsafe fn dc_unblock_chat(context: &Context, chat_id: uint32_t) {
dc_block_chat(context, chat_id, 0i32);
}
pub unsafe fn dc_block_chat(context: &Context, chat_id: uint32_t, new_blocking: libc::c_int) {
let stmt: *mut sqlite3_stmt;
stmt = dc_sqlite3_prepare(
pub fn dc_block_chat(context: &Context, chat_id: u32, new_blocking: libc::c_int) -> bool {
dc_sqlite3_execute(
context,
&context.sql.clone().read().unwrap(),
b"UPDATE chats SET blocked=? WHERE id=?;\x00" as *const u8 as *const libc::c_char,
);
sqlite3_bind_int(stmt, 1i32, new_blocking);
sqlite3_bind_int(stmt, 2i32, chat_id as libc::c_int);
sqlite3_step(stmt);
sqlite3_finalize(stmt);
"UPDATE chats SET blocked=? WHERE id=?;",
params![new_blocking, chat_id as i32],
)
}
pub unsafe fn dc_chat_load_from_db(chat: *mut dc_chat_t, chat_id: uint32_t) -> bool {
let mut success = false;
let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt;
if !(chat.is_null() || (*chat).magic != 0xc4a7c4a7u32) {
dc_chat_empty(chat);
stmt = dc_sqlite3_prepare(
(*chat).context,
&(*chat).context.sql.read().unwrap(),
b"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=?;\x00" as *const u8 as *const libc::c_char,
);
sqlite3_bind_int(stmt, 1, chat_id as libc::c_int);
pub fn dc_chat_load_from_db(chat: *mut dc_chat_t, chat_id: u32) -> bool {
if chat.is_null() || unsafe { (*chat).magic != 0xc4a7c4a7u32 } {
return false;
}
if !(sqlite3_step(stmt) != 100i32) {
if !(0 == set_from_stmt(chat, stmt)) {
success = true
unsafe { dc_chat_empty(chat) };
let context = unsafe { (*chat).context };
if let Some(mut stmt) = dc_sqlite3_prepare(
context,
&context.sql.read().unwrap(),
"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=?;",
) {
stmt.query_row(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()) }
};
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)?;
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()
} else {
false
}
sqlite3_finalize(stmt);
success
}
unsafe fn set_from_stmt(mut chat: *mut dc_chat_t, row: *mut sqlite3_stmt) -> libc::c_int {
let mut row_offset: libc::c_int = 0i32;
if chat.is_null() || (*chat).magic != 0xc4a7c4a7u32 || row.is_null() {
return 0i32;
}
dc_chat_empty(chat);
let fresh0 = row_offset;
row_offset = row_offset + 1;
(*chat).id = sqlite3_column_int(row, fresh0) as uint32_t;
let fresh1 = row_offset;
row_offset = row_offset + 1;
(*chat).type_0 = sqlite3_column_int(row, fresh1);
let fresh2 = row_offset;
row_offset = row_offset + 1;
(*chat).name = dc_strdup(sqlite3_column_text(row, fresh2) as *mut libc::c_char);
let fresh3 = row_offset;
row_offset = row_offset + 1;
(*chat).grpid = dc_strdup(sqlite3_column_text(row, fresh3) as *mut libc::c_char);
let fresh4 = row_offset;
row_offset = row_offset + 1;
dc_param_set_packed(
(*chat).param,
sqlite3_column_text(row, fresh4) as *mut libc::c_char,
);
let fresh5 = row_offset;
row_offset = row_offset + 1;
(*chat).archived = sqlite3_column_int(row, fresh5);
let fresh6 = row_offset;
row_offset = row_offset + 1;
(*chat).blocked = sqlite3_column_int(row, fresh6);
let fresh7 = row_offset;
row_offset = row_offset + 1;
(*chat).gossiped_timestamp = sqlite3_column_int64(row, fresh7) as i64;
let fresh8 = row_offset;
row_offset = row_offset + 1;
(*chat).is_sending_locations =
(sqlite3_column_int64(row, fresh8) as i64 > time()) as libc::c_int;
if (*chat).id == 1i32 as libc::c_uint {
free((*chat).name as *mut libc::c_void);
(*chat).name = dc_stock_str((*chat).context, 8i32)
} else if (*chat).id == 6i32 as libc::c_uint {
free((*chat).name as *mut libc::c_void);
let tempname: *mut libc::c_char = dc_stock_str((*chat).context, 40i32);
(*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);
} else if (*chat).id == 5i32 as libc::c_uint {
free((*chat).name as *mut libc::c_void);
(*chat).name = dc_stock_str((*chat).context, 41i32)
} else if 0 != dc_param_exists((*chat).param, 'K' as i32) {
free((*chat).name as *mut libc::c_void);
(*chat).name = dc_stock_str((*chat).context, 2i32)
}
row_offset
}
pub unsafe fn dc_create_chat_by_contact_id(context: &Context, contact_id: uint32_t) -> uint32_t {
@@ -265,8 +251,7 @@ pub unsafe fn dc_create_or_lookup_nchat_by_contact_id(
let mut chat_blocked: libc::c_int = 0i32;
let contact: *mut dc_contact_t;
let chat_name: *mut libc::c_char;
let mut q: *mut libc::c_char = 0 as *mut libc::c_char;
let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt;
if !ret_chat_id.is_null() {
*ret_chat_id = 0i32 as uint32_t
}
@@ -297,49 +282,36 @@ pub unsafe fn dc_create_or_lookup_nchat_by_contact_id(
} else {
(*contact).addr
};
q = sqlite3_mprintf(
b"INSERT INTO chats (type, name, param, blocked, grpid) VALUES(%i, %Q, %Q, %i, %Q)\x00"
as *const u8 as *const libc::c_char,
100i32,
chat_name,
if contact_id == 1i32 as libc::c_uint {
b"K=1\x00" as *const u8 as *const libc::c_char
} else {
b"\x00" as *const u8 as *const libc::c_char
},
create_blocked,
(*contact).addr,
);
stmt = dc_sqlite3_prepare(context, &context.sql.clone().read().unwrap(), q);
if !stmt.is_null() {
if !(sqlite3_step(stmt) != 101i32) {
chat_id = dc_sqlite3_get_rowid(
context,
&context.sql.clone().read().unwrap(),
b"chats\x00" as *const u8 as *const libc::c_char,
b"grpid\x00" as *const u8 as *const libc::c_char,
(*contact).addr,
);
sqlite3_free(q as *mut libc::c_void);
sqlite3_finalize(stmt);
q = sqlite3_mprintf(
b"INSERT INTO chats_contacts (chat_id, contact_id) VALUES(%i, %i)\x00"
as *const u8 as *const libc::c_char,
chat_id,
contact_id,
);
stmt = dc_sqlite3_prepare(context, &context.sql.clone().read().unwrap(), q);
if !(sqlite3_step(stmt) != 101i32) {
sqlite3_free(q as *mut libc::c_void);
q = 0 as *mut libc::c_char;
sqlite3_finalize(stmt);
stmt = 0 as *mut sqlite3_stmt
}
}
if dc_sqlite3_execute(
context,
&context.sql.clone().read().unwrap(),
"INSERT INTO chats (type, name, param, blocked, grpid) VALUES(?, ?, ?, ?, ?)",
params![
100,
as_str(chat_name),
if contact_id == 1 { "K=1" } else { "" },
create_blocked,
as_str((*contact).addr),
],
) {
let chat_id = dc_sqlite3_get_rowid(
context,
&context.sql.clone().read().unwrap(),
"chats",
"grpid",
as_str((*contact).addr),
);
dc_sqlite3_execute(
context,
&context.sql.clone().read().unwrap(),
"INSERT INTO chats_contacts (chat_id, contact_id) VALUES(?, ?)",
params![chat_id, contact_id],
);
}
}
sqlite3_free(q as *mut libc::c_void);
sqlite3_finalize(stmt);
dc_contact_unref(contact);
if !ret_chat_id.is_null() {
*ret_chat_id = chat_id
@@ -349,7 +321,7 @@ pub unsafe fn dc_create_or_lookup_nchat_by_contact_id(
};
}
pub unsafe fn dc_lookup_real_nchat_by_contact_id(
pub fn dc_lookup_real_nchat_by_contact_id(
context: &Context,
contact_id: uint32_t,
ret_chat_id: *mut uint32_t,
@@ -358,30 +330,29 @@ pub unsafe fn dc_lookup_real_nchat_by_contact_id(
/* checks for "real" chats or self-chat */
let stmt: *mut sqlite3_stmt;
if !ret_chat_id.is_null() {
*ret_chat_id = 0i32 as uint32_t
*ret_chat_id = 0;
}
if !ret_chat_blocked.is_null() {
*ret_chat_blocked = 0i32
*ret_chat_blocked = 0;
}
if context.sql.clone().read().unwrap().conn().is_none() {
return;
}
stmt =
dc_sqlite3_prepare(
context,
&context.sql.clone().read().unwrap(),
b"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=?;\x00"
as *const u8 as *const libc::c_char);
sqlite3_bind_int(stmt, 1i32, contact_id as libc::c_int);
if sqlite3_step(stmt) == 100i32 {
if !ret_chat_id.is_null() {
*ret_chat_id = sqlite3_column_int(stmt, 0i32) as uint32_t
}
if !ret_chat_blocked.is_null() {
*ret_chat_blocked = sqlite3_column_int(stmt, 1i32)
}
if let Some(mut stmt) = dc_sqlite3_prepare(
context,
&context.sql.clone().read().unwrap(),
"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=?;",
) {
stmt.query_row(
params![contact_id as i32],
|row| {
*ret_chat_id = row.get(0)?;
*ret_chat_blocked = row.get(1)?;
Ok(())
}
);
}
sqlite3_finalize(stmt);
}
pub unsafe fn dc_get_chat_id_by_contact_id(context: &Context, contact_id: uint32_t) -> uint32_t {
@@ -551,50 +522,48 @@ unsafe fn prepare_msg_raw(
b"Cannot send message; self not in group.\x00" as *const u8 as *const libc::c_char,
);
} else {
let from: *mut libc::c_char = dc_sqlite3_get_config(
let from = dc_sqlite3_get_config(
context,
&context.sql.clone().read().unwrap(),
b"configured_addr\x00" as *const u8 as *const libc::c_char,
0 as *const libc::c_char,
"configured_addr",
None,
);
if from.is_null() {
if from.is_none() {
dc_log_error(
context,
0i32,
0,
b"Cannot send message, not configured.\x00" as *const u8 as *const libc::c_char,
);
} else {
let from_c = to_cstring(from.unwrap());
new_rfc724_mid = dc_create_outgoing_rfc724_mid(
if (*chat).type_0 == 120i32 || (*chat).type_0 == 130i32 {
(*chat).grpid
} else {
0 as *mut libc::c_char
},
from,
from_c.as_ptr(),
);
free(from as *mut libc::c_void);
if (*chat).type_0 == 100i32 {
stmt = dc_sqlite3_prepare(
if (*chat).type_0 == 100 {
if let Some(id) = dc_sqlite3_query_row(
context,
&context.sql.clone().read().unwrap(),
b"SELECT contact_id FROM chats_contacts WHERE chat_id=?;\x00" as *const u8
as *const libc::c_char,
);
sqlite3_bind_int(stmt, 1i32, (*chat).id as libc::c_int);
if sqlite3_step(stmt) != 100i32 {
"SELECT contact_id FROM chats_contacts WHERE chat_id=?;",
params![(*chat).id as i32],
0,
) {
to_id = id;
current_block = 5689316957504528238;
} else {
dc_log_error(
context,
0i32,
0,
b"Cannot send message, contact for chat #%i not found.\x00" as *const u8
as *const libc::c_char,
(*chat).id,
);
current_block = 10477488590406205504;
} else {
to_id = sqlite3_column_int(stmt, 0i32) as uint32_t;
sqlite3_finalize(stmt);
stmt = 0 as *mut sqlite3_stmt;
current_block = 5689316957504528238;
}
} else {
if (*chat).type_0 == 120i32 || (*chat).type_0 == 130i32 {
@@ -612,54 +581,54 @@ unsafe fn prepare_msg_raw(
if we guarantee E2EE, and circumstances change
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 = 0i32;
do_guarantee_e2ee = 0;
e2ee_enabled = dc_sqlite3_get_config_int(
context,
&context.sql.clone().read().unwrap(),
b"e2ee_enabled\x00" as *const u8 as *const libc::c_char,
1i32,
"e2ee_enabled",
1,
);
if 0 != e2ee_enabled && dc_param_get_int((*msg).param, 'u' as i32, 0i32) == 0i32
{
let mut can_encrypt: libc::c_int = 1i32;
let mut all_mutual: libc::c_int = 1i32;
stmt =
dc_sqlite3_prepare(context, &context.sql.clone().read().unwrap(),
b"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;\x00"
as *const u8 as
*const libc::c_char);
sqlite3_bind_int(stmt, 1i32, (*chat).id as libc::c_int);
while sqlite3_step(stmt) == 100i32 {
if sqlite3_column_type(stmt, 0i32) == 5i32 {
dc_log_info(
context,
0i32,
b"[autocrypt] no peerstate for %s\x00" as *const u8
as *const libc::c_char,
sqlite3_column_text(stmt, 1i32),
);
can_encrypt = 0i32;
all_mutual = 0i32
} else {
let prefer_encrypted: libc::c_int = sqlite3_column_int(stmt, 0i32);
if prefer_encrypted != 1i32 {
dc_log_info(
context,
0i32,
b"[autocrypt] peerstate for %s is %s\x00" as *const u8
as *const libc::c_char,
sqlite3_column_text(stmt, 1i32),
if prefer_encrypted == 0i32 {
b"NOPREFERENCE\x00" as *const u8 as *const libc::c_char
} else {
b"RESET\x00" as *const u8 as *const libc::c_char
},
);
all_mutual = 0i32
if let Some(mut stmt) = dc_sqlite3_prepare(
context,
&context.sql.clone().read().unwrap(),
"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;",
) {
stmt.query_row(params![(*chat).id], |row| {
let state: String = row.get(1)?;
if let Some(prefer_encrypted) = row.get::<_, Option<i32>>(0)? {
if prefer_encrypted != 1 {
info!(
context,
0,
"[autocrypt] peerstate for {} is {}",
state,
if prefer_encrypted == 0 {
"NOPREFERENCE"
} else {
"RESET"
},
);
all_mutual = 0;
}
} else {
info!(context, 0, "[autocrypt] no peerstate for {}", state,);
can_encrypt = 0;
all_mutual = 0;
}
}
Ok(())
});
}
sqlite3_finalize(stmt);
if 0 != can_encrypt {
if 0 != all_mutual {
do_guarantee_e2ee = 1i32
@@ -732,96 +701,76 @@ unsafe fn prepare_msg_raw(
// add independent location to database
if 0 != dc_param_exists((*msg).param, DC_PARAM_SET_LATITUDE as libc::c_int) {
stmt = dc_sqlite3_prepare(
if dc_sqlite3_execute(
context,
&context.sql.clone().read().unwrap(),
b"INSERT INTO locations \
(timestamp,from_id,chat_id, latitude,longitude,independent)\
VALUES (?,?,?, ?,?,1);\x00" as *const u8
as *const libc::c_char,
);
sqlite3_bind_int64(stmt, 1, timestamp as i64);
sqlite3_bind_int(stmt, 2, DC_CONTACT_ID_SELF as libc::c_int);
sqlite3_bind_int(stmt, 3, (*chat).id as libc::c_int);
sqlite3_bind_double(
stmt,
4,
dc_param_get_float(
(*msg).param,
DC_PARAM_SET_LATITUDE as libc::c_int,
0.0,
),
);
sqlite3_bind_double(
stmt,
5,
dc_param_get_float(
(*msg).param,
DC_PARAM_SET_LONGITUDE as libc::c_int,
0.0,
),
);
sqlite3_step(stmt);
sqlite3_finalize(stmt);
location_id = dc_sqlite3_get_rowid2(
context,
&context.sql.clone().read().unwrap(),
b"locations\x00" as *const u8 as *const libc::c_char,
b"timestamp\x00" as *const u8 as *const libc::c_char,
timestamp as u64,
b"from_id\x00" as *const u8 as *const libc::c_char,
DC_CONTACT_ID_SELF as u32,
);
"INSERT INTO locations \
(timestamp,from_id,chat_id, latitude,longitude,independent)\
VALUES (?,?,?, ?,?,1);",
params![
timestamp,
DC_CONTACT_ID_SELF as i32,
(*chat).id as i32,
dc_param_get_float(
(*msg).param,
DC_PARAM_SET_LATITUDE as libc::c_int,
0.0,
),
dc_param_get_float(
(*msg).param,
DC_PARAM_SET_LONGITUDE as libc::c_int,
0.0,
),
],
) {
location_id = dc_sqlite3_get_rowid2(
context,
&context.sql.clone().read().unwrap(),
"locations",
"timestamp",
timestamp,
"from_id",
DC_CONTACT_ID_SELF as i32,
);
}
}
// add message to the database
stmt =
dc_sqlite3_prepare(
context,&context.sql.clone().read().unwrap(),
b"INSERT INTO msgs (rfc724_mid, chat_id, from_id, to_id, timestamp, type, state, txt, param, hidden, mime_in_reply_to, mime_references, location_id) VALUES (?,?,?,?,?, ?,?,?,?,?, ?,?,?);\x00"
as *const u8 as
*const libc::c_char);
sqlite3_bind_text(stmt, 1i32, new_rfc724_mid, -1i32, None);
sqlite3_bind_int(stmt, 2i32, (*chat).id as libc::c_int);
sqlite3_bind_int(stmt, 3i32, 1i32);
sqlite3_bind_int(stmt, 4i32, to_id as libc::c_int);
sqlite3_bind_int64(stmt, 5i32, timestamp as sqlite3_int64);
sqlite3_bind_int(stmt, 6i32, (*msg).type_0);
sqlite3_bind_int(stmt, 7i32, (*msg).state);
sqlite3_bind_text(
stmt,
8i32,
if !(*msg).text.is_null() {
(*msg).text
} else {
b"\x00" as *const u8 as *const libc::c_char
},
-1i32,
None,
);
sqlite3_bind_text(stmt, 9i32, (*(*msg).param).packed, -1i32, None);
sqlite3_bind_int(stmt, 10i32, (*msg).hidden);
sqlite3_bind_text(stmt, 11i32, new_in_reply_to, -1i32, None);
sqlite3_bind_text(stmt, 12i32, new_references, -1i32, None);
sqlite3_bind_int(stmt, 13i32, location_id as libc::c_int);
if sqlite3_step(stmt) != 101i32 {
dc_log_error(
context,
0i32,
b"Cannot send message, cannot insert to database.\x00" as *const u8
as *const libc::c_char,
(*chat).id,
);
} else {
if dc_sqlite3_execute(
context,
&context.sql.clone().read().unwrap(),
"INSERT INTO msgs (rfc724_mid, chat_id, from_id, to_id, timestamp, type, state, txt, param, hidden, mime_in_reply_to, mime_references, location_id) VALUES (?,?,?,?,?, ?,?,?,?,?, ?,?,?);",
params![
as_str(new_rfc724_mid),
(*chat).id as i32,
1i32,
to_id as i32,
timestamp,
(*msg).type_0,
(*msg).state,
if !(*msg).text.is_null() { Some(as_str((*msg).text)) } else { None },
if (*(*msg).param).packed.is_null() { None } else { Some(as_str((*(*msg).param).packed)) },
(*msg).hidden,
as_str(new_in_reply_to),
as_str(new_references),
location_id as i32,
]
) {
msg_id = dc_sqlite3_get_rowid(
context,
&context.sql.clone().read().unwrap(),
b"msgs\x00" as *const u8 as *const libc::c_char,
b"rfc724_mid\x00" as *const u8 as *const libc::c_char,
new_rfc724_mid,
)
"msgs",
"rfc724_mid",
as_str(new_rfc724_mid),
);
} else {
error!(
context,
0,
"Cannot send message, cannot insert to database (chat #{}).",
(*chat).id,
);
}
}
}
@@ -845,48 +794,52 @@ unsafe fn get_parent_mime_headers(
parent_in_reply_to: *mut *mut libc::c_char,
parent_references: *mut *mut libc::c_char,
) -> libc::c_int {
let mut success: libc::c_int = 0i32;
let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt;
let mut success = 0;
if !(chat.is_null()
|| parent_rfc724_mid.is_null()
|| parent_in_reply_to.is_null()
|| parent_references.is_null())
{
stmt =
dc_sqlite3_prepare(
(*chat).context,&mut (*chat).context.sql.clone().read().unwrap(),
b"SELECT rfc724_mid, mime_in_reply_to, mime_references FROM msgs WHERE timestamp=(SELECT max(timestamp) FROM msgs WHERE chat_id=? AND from_id!=?);\x00"
as *const u8 as *const libc::c_char);
sqlite3_bind_int(stmt, 1i32, (*chat).id as libc::c_int);
sqlite3_bind_int(stmt, 2i32, 1i32);
if sqlite3_step(stmt) == 100i32 {
*parent_rfc724_mid = dc_strdup(sqlite3_column_text(stmt, 0i32) as *const libc::c_char);
*parent_in_reply_to = dc_strdup(sqlite3_column_text(stmt, 1i32) as *const libc::c_char);
*parent_references = dc_strdup(sqlite3_column_text(stmt, 2i32) as *const libc::c_char);
success = 1i32
if let Some(mut stmt) = dc_sqlite3_prepare(
(*chat).context,
&mut (*chat).context.sql.clone().read().unwrap(),
"SELECT rfc724_mid, mime_in_reply_to, mime_references \
FROM msgs WHERE timestamp=(SELECT max(timestamp) \
FROM msgs WHERE chat_id=? AND from_id!=?);",
) {
success = stmt
.query_row(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());
Ok(())
})
.is_ok() as libc::c_int;
}
sqlite3_finalize(stmt);
stmt = 0 as *mut sqlite3_stmt;
if 0 == success {
stmt =
dc_sqlite3_prepare(
(*chat).context,&mut (*chat).context.sql.clone().read().unwrap(),
b"SELECT rfc724_mid, mime_in_reply_to, mime_references FROM msgs WHERE timestamp=(SELECT min(timestamp) FROM msgs WHERE chat_id=? AND from_id==?);\x00"
as *const u8 as *const libc::c_char);
sqlite3_bind_int(stmt, 1i32, (*chat).id as libc::c_int);
sqlite3_bind_int(stmt, 2i32, 1i32);
if sqlite3_step(stmt) == 100i32 {
*parent_rfc724_mid =
dc_strdup(sqlite3_column_text(stmt, 0i32) as *const libc::c_char);
*parent_in_reply_to =
dc_strdup(sqlite3_column_text(stmt, 1i32) as *const libc::c_char);
*parent_references =
dc_strdup(sqlite3_column_text(stmt, 2i32) as *const libc::c_char);
success = 1i32
if let Some(mut stmt) = dc_sqlite3_prepare(
(*chat).context,
&mut (*chat).context.sql.clone().read().unwrap(),
"SELECT rfc724_mid, mime_in_reply_to, mime_references \
FROM msgs WHERE timestamp=(SELECT min(timestamp) \
FROM msgs WHERE chat_id=? AND from_id==?);",
) {
success = stmt
.query_row(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());
Ok(())
})
.is_ok() as libc::c_int;
}
}
}
sqlite3_finalize(stmt);
success
}
@@ -907,44 +860,38 @@ unsafe fn last_msg_in_chat_encrypted(
chat_id: uint32_t,
) -> libc::c_int {
let mut last_is_encrypted: libc::c_int = 0i32;
let stmt: *mut sqlite3_stmt =
dc_sqlite3_prepare(
context,sql,
b"SELECT param FROM msgs WHERE timestamp=(SELECT MAX(timestamp) FROM msgs WHERE chat_id=?) ORDER BY id DESC;\x00"
as *const u8 as *const libc::c_char);
sqlite3_bind_int(stmt, 1i32, chat_id as libc::c_int);
if sqlite3_step(stmt) == 100i32 {
let msg_param: *mut dc_param_t = dc_param_new();
dc_param_set_packed(
msg_param,
sqlite3_column_text(stmt, 0i32) as *mut libc::c_char,
);
let packed: Option<String> = dc_sqlite3_query_row(
context,
sql,
"SELECT param \
FROM msgs WHERE timestamp=(SELECT MAX(timestamp) FROM msgs WHERE chat_id=?) \
ORDER BY id DESC;",
params![chat_id as i32],
0,
);
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());
if 0 != dc_param_exists(msg_param, 'c' as i32) {
last_is_encrypted = 1i32
last_is_encrypted = 1;
}
dc_param_unref(msg_param);
}
sqlite3_finalize(stmt);
last_is_encrypted
}
// TODO should return bool /rtn
pub unsafe fn dc_chat_update_param(chat: *mut dc_chat_t) -> libc::c_int {
let success: libc::c_int;
let stmt: *mut sqlite3_stmt = dc_sqlite3_prepare(
dc_sqlite3_execute(
(*chat).context,
&mut (*chat).context.sql.clone().read().unwrap(),
b"UPDATE chats SET param=? WHERE id=?\x00" as *const u8 as *const libc::c_char,
);
sqlite3_bind_text(stmt, 1i32, (*(*chat).param).packed, -1i32, None);
sqlite3_bind_int(stmt, 2i32, (*chat).id as libc::c_int);
success = if sqlite3_step(stmt) == 101i32 {
1i32
} else {
0i32
};
sqlite3_finalize(stmt);
success
"UPDATE chats SET param=? WHERE id=?",
params![as_str((*(*chat).param).packed), (*chat).id as i32],
) as libc::c_int
}
pub unsafe fn dc_is_contact_in_chat(
@@ -954,36 +901,22 @@ pub unsafe fn dc_is_contact_in_chat(
) -> libc::c_int {
/* this function works for group and for normal chats, however, it is more useful for group chats.
DC_CONTACT_ID_SELF may be used to check, if the user itself is in a group chat (DC_CONTACT_ID_SELF is not added to normal chats) */
let ret: libc::c_int;
let stmt: *mut sqlite3_stmt;
stmt = dc_sqlite3_prepare(
dc_sqlite3_execute(
context,
&context.sql.clone().read().unwrap(),
b"SELECT contact_id FROM chats_contacts WHERE chat_id=? AND contact_id=?;\x00" as *const u8
as *const libc::c_char,
);
sqlite3_bind_int(stmt, 1i32, chat_id as libc::c_int);
sqlite3_bind_int(stmt, 2i32, contact_id as libc::c_int);
ret = if sqlite3_step(stmt) == 100i32 {
1i32
} else {
0i32
};
sqlite3_finalize(stmt);
ret
"SELECT contact_id FROM chats_contacts WHERE chat_id=? AND contact_id=?;",
params![chat_id as i32, contact_id as i32],
) as libc::c_int
}
pub unsafe fn dc_unarchive_chat(context: &Context, chat_id: uint32_t) {
let stmt: *mut sqlite3_stmt = dc_sqlite3_prepare(
pub fn dc_unarchive_chat(context: &Context, chat_id: u32) {
dc_sqlite3_execute(
context,
&context.sql.clone().read().unwrap(),
b"UPDATE chats SET archived=0 WHERE id=?\x00" as *const u8 as *const libc::c_char,
"UPDATE chats SET archived=0 WHERE id=?",
params![chat_id as i32],
);
sqlite3_bind_int(stmt, 1i32, chat_id as libc::c_int);
sqlite3_step(stmt);
sqlite3_finalize(stmt);
}
pub unsafe fn dc_send_msg<'a>(
@@ -1121,32 +1054,23 @@ unsafe fn set_draft_raw(context: &Context, chat_id: uint32_t, msg: *mut dc_msg_t
match current_block {
14513523936503887211 => {}
_ => {
stmt =
dc_sqlite3_prepare(
context,&context.sql.clone().read().unwrap(),
b"INSERT INTO msgs (chat_id, from_id, timestamp, type, state, txt, param, hidden) VALUES (?,?,?, ?,?,?,?,?);\x00"
as *const u8 as
*const libc::c_char);
sqlite3_bind_int(stmt, 1i32, chat_id as libc::c_int);
sqlite3_bind_int(stmt, 2i32, 1i32);
sqlite3_bind_int64(stmt, 3i32, time() as sqlite3_int64);
sqlite3_bind_int(stmt, 4i32, (*msg).type_0);
sqlite3_bind_int(stmt, 5i32, 19i32);
sqlite3_bind_text(
stmt,
6i32,
if !(*msg).text.is_null() {
(*msg).text
} else {
b"\x00" as *const u8 as *const libc::c_char
},
-1i32,
None,
);
sqlite3_bind_text(stmt, 7i32, (*(*msg).param).packed, -1i32, None);
sqlite3_bind_int(stmt, 8i32, 1i32);
if !(sqlite3_step(stmt) != 101i32) {
sth_changed = 1i32
if dc_sqlite3_execute(
context,
&context.sql.clone().read().unwrap(),
"INSERT INTO msgs (chat_id, from_id, timestamp, type, state, txt, param, hidden) \
VALUES (?,?,?, ?,?,?,?,?);",
params![
chat_id as i32, 1, time(), (*msg).type_0, 19,
if !(*msg).text.is_null() {
as_str((*msg).text)
} else {
""
},
as_str((*(*msg).param).packed),
1,
]
) {
sth_changed = 1;
}
}
}
@@ -1156,20 +1080,17 @@ unsafe fn set_draft_raw(context: &Context, chat_id: uint32_t, msg: *mut dc_msg_t
sth_changed
}
unsafe fn get_draft_msg_id(context: &Context, chat_id: uint32_t) -> uint32_t {
let mut draft_msg_id: uint32_t = 0i32 as uint32_t;
let stmt: *mut sqlite3_stmt = dc_sqlite3_prepare(
fn get_draft_msg_id(context: &Context, chat_id: u32) -> u32 {
let draft_msg_id: i32 = dc_sqlite3_query_row(
context,
&context.sql.clone().read().unwrap(),
b"SELECT id FROM msgs WHERE chat_id=? AND state=?;\x00" as *const u8 as *const libc::c_char,
);
sqlite3_bind_int(stmt, 1i32, chat_id as libc::c_int);
sqlite3_bind_int(stmt, 2i32, 19i32);
if sqlite3_step(stmt) == 100i32 {
draft_msg_id = sqlite3_column_int(stmt, 0i32) as uint32_t
}
sqlite3_finalize(stmt);
draft_msg_id
"SELECT id FROM msgs WHERE chat_id=? AND state=?;",
params![chat_id as i32, 19],
0,
)
.unwrap_or_default();
draft_msg_id as u32
}
pub unsafe fn dc_get_draft(context: &Context, chat_id: uint32_t) -> *mut dc_msg_t {
@@ -1208,11 +1129,11 @@ pub unsafe fn dc_get_chat_msgs(
let cnv_to_local = dc_gm2local_offset();
if !ret.is_null() {
if chat_id == 1i32 as libc::c_uint {
let show_emails: libc::c_int = dc_sqlite3_get_config_int(
let show_emails = dc_sqlite3_get_config_int(
context,
&context.sql.clone().read().unwrap(),
b"show_emails\x00" as *const u8 as *const libc::c_char,
0i32,
"show_emails",
0,
);
stmt =
dc_sqlite3_prepare(
+24 -14
View File
@@ -798,7 +798,7 @@ pub fn dc_sqlite3_set_config(
context: &Context,
sql: &dc_sqlite3_t,
key: impl AsRef<str>,
value: Option<impl AsRef<str>>,
value: Option<&str>,
) -> libc::c_int {
let key = key.as_ref();
let mut state;
@@ -894,14 +894,19 @@ pub fn dc_sqlite3_get_config(
Some(def)
}
pub fn dc_sqlite3_execute(
pub fn dc_sqlite3_execute<P>(
context: &Context,
sql: &dc_sqlite3_t,
querystr: impl AsRef<str>,
) -> libc::c_int {
params: P,
) -> bool
where
P: IntoIterator,
P::Item: rusqlite::ToSql,
{
if let Some(stmt) = dc_sqlite3_prepare(context, sql, querystr) {
match stmt.execute() {
Ok(_) => 1,
match stmt.execute(params) {
Ok(_) => true,
Err(err) => {
error!(
context,
@@ -910,22 +915,27 @@ pub fn dc_sqlite3_execute(
querystr.as_ref(),
err
);
0
false
}
}
} else {
0
false
}
}
pub fn dc_sqlite3_query_row<T>(
pub fn dc_sqlite3_query_row<P, T>(
context: &Context,
sql: &dc_sqlite3_t,
query: &str,
params: P,
column: usize,
) -> Option<T> {
) -> Option<T>
where
P: IntoIterator,
P::Item: rusqlite::ToSql,
{
if let Some(ref conn) = sql.conn() {
match conn.query_row(query, NO_PARAMS, |row| row.get(column)) {
match conn.query_row(query, params, |row| row.get(column)) {
Ok(res) => Some(res),
Err(err) => {
error!(context, 0, "sql: Failed query_row: {}", err);
@@ -976,7 +986,7 @@ pub fn dc_sqlite3_table_exists(
pub fn dc_sqlite3_set_config_int64(
context: &Context,
sql: &dc_sqlite3_t,
key: *const libc::c_char,
key: impl AsRef<str>,
value: i64,
) -> libc::c_int {
dc_sqlite3_set_config(context, sql, key, format!("{}", value));
@@ -988,7 +998,7 @@ pub fn dc_sqlite3_get_config_int64(
key: impl AsRef<str>,
def: Option<i64>,
) -> i64 {
let ret = dc_sqlite3_get_config(context, sql, key, 0 as *const libc::c_char);
let ret = dc_sqlite3_get_config(context, sql, key, None);
ret.map(|r| r.parse().unwrap_or_default())
.unwrap_or_else(|_| def.unwrap_or_default())
}
@@ -1048,9 +1058,9 @@ pub fn dc_sqlite3_get_rowid2(
sql: &dc_sqlite3_t,
table: impl AsRef<str>,
field: impl AsRef<str>,
value: u64,
value: i64,
field2: impl AsRef<str>,
value2: u32,
value2: i32,
) -> uint32_t {
// same as dc_sqlite3_get_rowid() with a key over two columns
if let Some(ref conn) = sql.conn() {
+18 -20
View File
@@ -1597,29 +1597,27 @@ impl Imap {
}
}
unsafe {
dc_sqlite3_set_config_int(
dc_sqlite3_set_config_int(
context,
&context.sql.read().unwrap(),
"folders_configured",
3,
);
if let Some(ref mvbox_folder) = mvbox_folder {
dc_sqlite3_set_config(
context,
&context.sql.read().unwrap(),
b"folders_configured\x00" as *const u8 as *const libc::c_char,
3,
"configured_mvbox_folder",
Some(mvbox_folder),
);
}
if let Some(ref sentbox_folder) = sentbox_folder {
dc_sqlite3_set_config(
context,
&context.sql.read().unwrap(),
"configured_sentbox_folder",
Some(sentbox_folder.name()),
);
if let Some(ref mvbox_folder) = mvbox_folder {
dc_sqlite3_set_config(
context,
&context.sql.read().unwrap(),
b"configured_mvbox_folder\x00" as *const u8 as *const libc::c_char,
CString::new(mvbox_folder.clone()).unwrap().as_ptr(),
);
}
if let Some(ref sentbox_folder) = sentbox_folder {
dc_sqlite3_set_config(
context,
&context.sql.read().unwrap(),
b"configured_sentbox_folder\x00" as *const u8 as *const libc::c_char,
CString::new(sentbox_folder.name()).unwrap().as_ptr(),
);
}
}
}
+21 -89
View File
@@ -113,19 +113,6 @@ impl Key {
Self::from_slice(bytes, key_type)
}
pub fn from_stmt(
stmt: *mut sqlite3_stmt,
index: libc::c_int,
key_type: KeyType,
) -> Option<Self> {
assert!(!stmt.is_null(), "missing statement");
let data = unsafe { sqlite3_column_blob(stmt, index) as *const u8 };
let len = unsafe { sqlite3_column_bytes(stmt, index) };
Self::from_binary(data, len, key_type)
}
pub fn from_base64(encoded_data: &str, key_type: KeyType) -> Option<Self> {
// strip newlines and other whitespace
let cleaned: String = encoded_data.trim().split_whitespace().collect();
@@ -145,25 +132,16 @@ impl Key {
return None;
}
let stmt = unsafe {
dc_sqlite3_prepare(
context,
sql,
b"SELECT public_key FROM keypairs WHERE addr=? AND is_default=1;\x00" as *const u8
as *const libc::c_char,
)
};
unsafe { sqlite3_bind_text(stmt, 1, self_addr, -1, None) };
let addr = as_str(self_addr);
let key = if unsafe { sqlite3_step(stmt) } == 100 {
Self::from_stmt(stmt, 0, KeyType::Public)
} else {
None
};
unsafe { sqlite3_finalize(stmt) };
key
dc_sqlite3_query_row(
context,
sql,
"SELECT public_key FROM keypairs WHERE addr=? AND is_default=1;",
&[addr],
0,
)
.and_then(|blob| Self::from_slice(blob, KeyType::Public))
}
pub fn from_self_private(
@@ -175,24 +153,15 @@ impl Key {
return None;
}
let stmt = unsafe {
dc_sqlite3_prepare(
context,
sql,
b"SELECT private_key FROM keypairs WHERE addr=? AND is_default=1;\x00" as *const u8
as *const libc::c_char,
)
};
unsafe { sqlite3_bind_text(stmt, 1, self_addr, -1, None) };
let key = if unsafe { sqlite3_step(stmt) } == 100 {
Self::from_stmt(stmt, 0, KeyType::Private)
} else {
None
};
unsafe { sqlite3_finalize(stmt) };
key
let addr = as_str(self_addr);
dc_sqlite3_query_row(
context,
sql,
"SELECT private_key FROM keypairs WHERE addr=? AND is_default=1;",
&[addr],
0,
)
.and_then(|blob| Self::from_slice(blob, KeyType::Private))
}
pub fn to_bytes(&self) -> Vec<u8> {
@@ -338,49 +307,12 @@ pub fn dc_key_save_self_keypair(
return false;
}
let stmt = unsafe {
dc_sqlite3_prepare(
dc_sqlite3_execute(
context,
sql,
b"INSERT INTO keypairs (addr, is_default, public_key, private_key, created) VALUES (?,?,?,?,?);\x00"
as *const u8 as *const libc::c_char
"INSERT INTO keypairs (addr, is_default, public_key, private_key, created) VALUES (?,?,?,?,?);",
params![as_str(addr), is_default, public_key.to_bytes(), private_key.to_bytes(), time()],
)
};
unsafe {
sqlite3_bind_text(stmt, 1, addr, -1, None);
sqlite3_bind_int(stmt, 2, is_default)
};
let pub_bytes = public_key.to_bytes();
let sec_bytes = private_key.to_bytes();
unsafe {
sqlite3_bind_blob(
stmt,
3,
pub_bytes.as_ptr() as *const _,
pub_bytes.len() as libc::c_int,
None,
)
};
unsafe {
sqlite3_bind_blob(
stmt,
4,
sec_bytes.as_ptr() as *const _,
sec_bytes.len() as libc::c_int,
None,
)
};
unsafe { sqlite3_bind_int64(stmt, 5, time() as sqlite3_int64) };
let success = if unsafe { sqlite3_step(stmt) } == 101 {
true
} else {
false
};
unsafe { sqlite3_finalize(stmt) };
success
}
/// Make a fingerprint human-readable, in hex format.
+11 -17
View File
@@ -3,8 +3,8 @@ use std::borrow::Cow;
use crate::constants::*;
use crate::context::Context;
use crate::dc_sqlite3::*;
use crate::dc_tools::as_str;
use crate::key::*;
use crate::types::*;
#[derive(Default, Clone, Debug)]
pub struct Keyring<'a> {
@@ -38,22 +38,16 @@ impl<'a> Keyring<'a> {
if self_addr.is_null() {
return false;
}
let stmt = unsafe {
dc_sqlite3_prepare(
context,
sql,
b"SELECT private_key FROM keypairs ORDER BY addr=? DESC, is_default DESC;\x00"
as *const u8 as *const libc::c_char,
)
};
unsafe { sqlite3_bind_text(stmt, 1, self_addr, -1, None) };
while unsafe { sqlite3_step(stmt) == 100 } {
if let Some(key) = Key::from_stmt(stmt, 0, KeyType::Private) {
self.add_owned(key);
}
}
unsafe { sqlite3_finalize(stmt) };
true
dc_sqlite3_query_row(
context,
sql,
"SELECT private_key FROM keypairs ORDER BY addr=? DESC, is_default DESC;",
&[as_str(self_addr)],
0,
)
.and_then(|blob| Key::from_slice(blob, KeyType::Private))
.map(|key| self.add_owned(key))
.is_some()
}
}
+28 -57
View File
@@ -1,5 +1,4 @@
use std::collections::HashMap;
use std::ffi::CString;
use percent_encoding::{utf8_percent_encode, DEFAULT_ENCODE_SET};
use serde::Deserialize;
@@ -51,10 +50,11 @@ pub fn dc_get_oauth2_url(
redirect_uri: impl AsRef<str>,
) -> Option<String> {
if let Some(oauth2) = Oauth2::from_address(addr) {
set_config(
dc_sqlite3_set_config(
context,
&context.sql.clone().read().unwrap(),
"oauth2_pending_redirect_uri",
redirect_uri.as_ref(),
Some(redirect_uri.as_ref()),
);
let oauth2_url = replace_in_uri(&oauth2.get_code, "$CLIENT_ID", &oauth2.client_id);
let oauth2_url = replace_in_uri(&oauth2_url, "$REDIRECT_URI", redirect_uri.as_ref());
@@ -76,19 +76,21 @@ pub fn dc_get_oauth2_access_token(
if let Some(oauth2) = Oauth2::from_address(addr) {
let lock = context.oauth2_critical.clone();
let _l = lock.lock().unwrap();
let sql = context.sql.clone().read().unwrap();
// read generated token
if 0 == flags & 0x1 && !is_expired(context) {
let access_token = get_config(context, "oauth2_access_token");
let access_token = dc_sqlite3_get_config(context, &sql, "oauth2_access_token", None);
if access_token.is_some() {
// success
return access_token;
}
}
let refresh_token = get_config(context, "oauth2_refresh_token");
let refresh_token = dc_sqlite3_get_config(context, &sql, "oauth2_refresh_token", None);
let refresh_token_for =
get_config(context, "oauth2_refresh_token_for").unwrap_or_else(|| "unset".into());
dc_sqlite3_get_config(context, &sql, "oauth2_refresh_token_for", None)
.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 +99,7 @@ pub fn dc_get_oauth2_access_token(
0, "Generate OAuth2 refresh_token and access_token...",
);
(
get_config(context, "oauth2_pending_redirect_uri")
dc_sqlite3_get_config(context, &sql, "oauth2_pending_redirect_uri", None)
.unwrap_or_else(|| "unset".into()),
oauth2.init_token,
true,
@@ -108,7 +110,8 @@ pub fn dc_get_oauth2_access_token(
0, "Regenerate OAuth2 access_token by refresh_token...",
);
(
get_config(context, "oauth2_redirect_uri").unwrap_or_else(|| "unset".into()),
dc_sqlite3_get_config(context, &sql, "oauth2_redirect_uri", None)
.unwrap_or_else(|| "unset".into()),
oauth2.refresh_token,
false,
)
@@ -151,23 +154,33 @@ pub fn dc_get_oauth2_access_token(
println!("response: {:?}", &parsed);
let response = parsed.unwrap();
if let Some(ref token) = response.refresh_token {
set_config(context, "oauth2_refresh_token", token);
set_config(context, "oauth2_refresh_token_for", code.as_ref());
dc_sqlite3_set_config(context, &sql, "oauth2_refresh_token", Some(token));
dc_sqlite3_set_config(
context,
&sql,
"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 {
set_config(context, "oauth2_access_token", token);
dc_sqlite3_set_config(context, &sql, "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);
set_config_int64(context, "oauth2_timestamp_expires", expires_in);
dc_sqlite3_set_config_int64(context, &sql, "oauth2_timestamp_expires", expires_in);
if update_redirect_uri_on_success {
set_config(context, "oauth2_redirect_uri", redirect_uri.as_ref());
dc_sqlite3_set_config(
context,
&sql,
"oauth2_redirect_uri",
Some(redirect_uri.as_ref()),
);
}
} else {
warn!(context, 0, "Failed to find OAuth2 access token");
@@ -279,54 +292,12 @@ impl Oauth2 {
}
}
fn get_config(context: &Context, key: &str) -> Option<String> {
let key_c = CString::new(key).unwrap();
let res = unsafe {
dc_sqlite3_get_config(
context,
&context.sql.clone().read().unwrap(),
key_c.as_ptr(),
std::ptr::null(),
)
};
if res.is_null() {
return None;
}
Some(to_string(res))
}
fn set_config(context: &Context, key: &str, value: &str) {
let key_c = CString::new(key).unwrap();
let value_c = CString::new(value).unwrap();
unsafe {
dc_sqlite3_set_config(
context,
&context.sql.clone().read().unwrap(),
key_c.as_ptr(),
value_c.as_ptr(),
)
};
}
fn set_config_int64(context: &Context, key: &str, value: i64) {
let key_c = CString::new(key).unwrap();
unsafe {
dc_sqlite3_set_config_int64(
context,
&context.sql.clone().read().unwrap(),
key_c.as_ptr(),
value,
)
};
}
fn is_expired(context: &Context) -> bool {
let expire_timestamp = dc_sqlite3_get_config_int64(
context,
&context.sql.clone().read().unwrap(),
b"oauth2_timestamp_expires\x00" as *const u8 as *const libc::c_char,
0i32 as int64_t,
"oauth2_timestamp_expires",
Some(0),
);
if expire_timestamp <= 0 {
+106 -221
View File
@@ -1,5 +1,4 @@
use std::collections::HashSet;
use std::ffi::CString;
use std::fmt;
use num_traits::FromPrimitive;
@@ -9,7 +8,6 @@ use crate::constants::*;
use crate::context::Context;
use crate::dc_chat::*;
use crate::dc_sqlite3::*;
use crate::dc_tools::{to_cstring, to_string};
use crate::key::*;
use crate::types::*;
@@ -17,13 +15,13 @@ use crate::types::*;
pub struct Peerstate<'a> {
pub context: &'a Context,
pub addr: Option<String>,
pub last_seen: u64,
pub last_seen_autocrypt: u64,
pub last_seen: i64,
pub last_seen_autocrypt: i64,
pub prefer_encrypt: EncryptPreference,
pub public_key: Option<Key>,
pub public_key_fingerprint: Option<String>,
pub gossip_key: Option<Key>,
pub gossip_timestamp: u64,
pub gossip_timestamp: i64,
pub gossip_key_fingerprint: Option<String>,
verified_key: VerifiedKey,
pub verified_key_fingerprint: Option<String>,
@@ -141,7 +139,7 @@ impl<'a> Peerstate<'a> {
}
}
pub fn from_header(context: &'a Context, header: &Aheader, message_time: u64) -> Self {
pub fn from_header(context: &'a Context, header: &Aheader, message_time: i64) -> Self {
let mut res = Self::new(context);
res.addr = Some(header.addr.clone());
@@ -155,7 +153,7 @@ impl<'a> Peerstate<'a> {
res
}
pub fn from_gossip(context: &'a Context, gossip_header: &Aheader, message_time: u64) -> Self {
pub fn from_gossip(context: &'a Context, gossip_header: &Aheader, message_time: i64) -> Self {
let mut res = Self::new(context);
res.addr = Some(gossip_header.addr.clone());
@@ -170,20 +168,9 @@ impl<'a> Peerstate<'a> {
pub fn from_addr(context: &'a Context, sql: &dc_sqlite3_t, addr: &str) -> Option<Self> {
let mut res = None;
let stmt = unsafe {
dc_sqlite3_prepare(
context,
sql,
b"SELECT addr, last_seen, last_seen_autocrypt, prefer_encrypted, public_key, gossip_timestamp, gossip_key, public_key_fingerprint, gossip_key_fingerprint, verified_key, verified_key_fingerprint FROM acpeerstates WHERE addr=? COLLATE NOCASE;\x00"
as *const u8 as *const libc::c_char)
};
let addr_c = CString::new(addr.as_bytes()).unwrap();
unsafe { sqlite3_bind_text(stmt, 1, addr_c.as_ptr(), -1, None) };
if unsafe { sqlite3_step(stmt) } == 100 {
res = Some(Self::from_stmt(context, stmt));
}
let query = "SELECT addr, last_seen, last_seen_autocrypt, prefer_encrypted, public_key, gossip_timestamp, gossip_key, public_key_fingerprint, gossip_key_fingerprint, verified_key, verified_key_fingerprint FROM acpeerstates WHERE addr=? COLLATE NOCASE;";
unsafe { sqlite3_finalize(stmt) };
Self::from_stmt(context, query, &[addr]);
res
}
@@ -192,67 +179,67 @@ impl<'a> Peerstate<'a> {
sql: &dc_sqlite3_t,
fingerprint: &str,
) -> Option<Self> {
let mut res = None;
let query = "SELECT addr, last_seen, last_seen_autocrypt, prefer_encrypted, public_key, \
gossip_timestamp, gossip_key, public_key_fingerprint, gossip_key_fingerprint, \
verified_key, verified_key_fingerprint \
FROM acpeerstates \
WHERE public_key_fingerprint=? COLLATE NOCASE \
OR gossip_key_fingerprint=? COLLATE NOCASE \
ORDER BY public_key_fingerprint=? DESC;";
let stmt = unsafe {
dc_sqlite3_prepare(
context,
sql,
b"SELECT addr, last_seen, last_seen_autocrypt, prefer_encrypted, public_key, gossip_timestamp, gossip_key, public_key_fingerprint, gossip_key_fingerprint, verified_key, verified_key_fingerprint FROM acpeerstates WHERE public_key_fingerprint=? COLLATE NOCASE OR gossip_key_fingerprint=? COLLATE NOCASE ORDER BY public_key_fingerprint=? DESC;\x00"
as *const u8 as *const libc::c_char)
};
let fp_c = CString::new(fingerprint.as_bytes()).unwrap();
unsafe {
sqlite3_bind_text(stmt, 1, fp_c.as_ptr(), -1, None);
sqlite3_bind_text(stmt, 2, fp_c.as_ptr(), -1, None);
sqlite3_bind_text(stmt, 3, fp_c.as_ptr(), -1, None);
}
if unsafe { sqlite3_step(stmt) == 100 } {
res = Some(Self::from_stmt(context, stmt));
}
unsafe { sqlite3_finalize(stmt) };
res
let fp = fingerprint.as_bytes();
Self::from_stmt(context, query, params![fp, fp, fp])
}
fn from_stmt(context: &'a Context, stmt: *mut sqlite3_stmt) -> Self {
fn from_stmt<P>(context: &'a Context, query: &str, params: P) -> Option<Self>
where
P: IntoIterator,
P::Item: rusqlite::ToSql,
{
let mut res = Self::new(context);
res.addr = Some(to_string(unsafe {
sqlite3_column_text(stmt, 0) as *const _
}));
res.last_seen = unsafe { sqlite3_column_int64(stmt, 1) } as u64;
res.last_seen_autocrypt = unsafe { sqlite3_column_int64(stmt, 2) } as u64;
res.prefer_encrypt =
EncryptPreference::from_i32(unsafe { sqlite3_column_int(stmt, 3) }).unwrap_or_default();
res.gossip_timestamp = unsafe { sqlite3_column_int(stmt, 5) } as u64;
let pkf = to_string(unsafe { sqlite3_column_text(stmt, 7) as *const _ });
res.public_key_fingerprint = if pkf.is_empty() { None } else { Some(pkf) };
let gkf = to_string(unsafe { sqlite3_column_text(stmt, 8) as *const _ });
res.gossip_key_fingerprint = if gkf.is_empty() { None } else { Some(gkf) };
let vkf = to_string(unsafe { sqlite3_column_text(stmt, 10) as *const _ });
res.verified_key_fingerprint = if vkf.is_empty() { None } else { Some(vkf) };
if let Some(mut stmt) =
dc_sqlite3_prepare(context, &context.sql.clone().read().unwrap(), query)
{
stmt.query_row(params, |row| {
res.addr = Some(row.get(0)?);
res.last_seen = row.get(1)?;
res.last_seen_autocrypt = row.get(2)?;
res.prefer_encrypt = EncryptPreference::from_i32(row.get(3)?).unwrap_or_default();
res.gossip_timestamp = row.get(5)?;
let pkf: String = row.get(7)?;
res.public_key_fingerprint = if pkf.is_empty() { None } else { Some(pkf) };
let gkf: String = row.get(8)?;
res.gossip_key_fingerprint = if gkf.is_empty() { None } else { Some(gkf) };
let vkf: String = row.get(10)?;
res.verified_key_fingerprint = if vkf.is_empty() { None } else { Some(vkf) };
if unsafe { sqlite3_column_type(stmt, 4) } != 5 {
res.public_key = Key::from_stmt(stmt, 4, KeyType::Public);
}
if unsafe { sqlite3_column_type(stmt, 6) } != 5 {
res.gossip_key = Key::from_stmt(stmt, 6, KeyType::Public);
}
if unsafe { sqlite3_column_type(stmt, 9) } != 5 {
let vk = Key::from_stmt(stmt, 9, KeyType::Public);
res.verified_key = if vk == res.gossip_key {
VerifiedKey::Gossip
} else if vk == res.public_key {
VerifiedKey::Public
} else {
VerifiedKey::None
};
}
res.public_key = row
.get(4)
.ok()
.and_then(|blob: Vec<u8>| Key::from_slice(&blob, KeyType::Public));
res.gossip_key = row
.get(6)
.ok()
.and_then(|blob: Vec<u8>| Key::from_slice(&blob, KeyType::Public));
let vk = row
.get(9)
.ok()
.and_then(|blob: Vec<u8>| Key::from_slice(&blob, KeyType::Public));
res.verified_key = if vk == res.gossip_key {
VerifiedKey::Gossip
} else if vk == res.public_key {
VerifiedKey::Public
} else {
VerifiedKey::None
};
res
Ok(res)
})
.ok()
} else {
None
}
}
pub fn recalc_fingerprint(&mut self) {
@@ -287,7 +274,7 @@ impl<'a> Peerstate<'a> {
}
}
pub fn degrade_encryption(&mut self, message_time: u64) {
pub fn degrade_encryption(&mut self, message_time: i64) {
if self.prefer_encrypt == EncryptPreference::Mutual {
self.degrade_event = Some(DegradeEvent::EncryptionPaused);
}
@@ -297,7 +284,7 @@ impl<'a> Peerstate<'a> {
self.to_save = Some(ToSave::All);
}
pub fn apply_header(&mut self, header: &Aheader, message_time: u64) {
pub fn apply_header(&mut self, header: &Aheader, message_time: i64) {
if self.addr.is_none()
|| self.addr.as_ref().unwrap().to_lowercase() != header.addr.to_lowercase()
{
@@ -329,7 +316,7 @@ impl<'a> Peerstate<'a> {
}
}
pub fn apply_gossip(&mut self, gossip_header: &Aheader, message_time: u64) {
pub fn apply_gossip(&mut self, gossip_header: &Aheader, message_time: i64) {
if self.addr.is_none()
|| self.addr.as_ref().unwrap().to_lowercase() != gossip_header.addr.to_lowercase()
{
@@ -412,154 +399,52 @@ impl<'a> Peerstate<'a> {
}
if create {
let stmt = unsafe {
dc_sqlite3_prepare(
self.context,
sql,
b"INSERT INTO acpeerstates (addr) VALUES(?);\x00" as *const u8
as *const libc::c_char,
)
};
let addr_c = to_cstring(self.addr.as_ref().unwrap());
unsafe {
sqlite3_bind_text(stmt, 1, addr_c.as_ptr(), -1, None);
sqlite3_step(stmt);
sqlite3_finalize(stmt);
if !dc_sqlite3_execute(
self.context,
sql,
"INSERT INTO acpeerstates (addr) VALUES(?);",
params![self.addr.as_ref().unwrap()],
) {
return false;
}
}
if self.to_save == Some(ToSave::All) || create {
let stmt = unsafe {
dc_sqlite3_prepare(
self.context, sql,
b"UPDATE acpeerstates \
SET last_seen=?, last_seen_autocrypt=?, prefer_encrypted=?, \
public_key=?, gossip_timestamp=?, gossip_key=?, public_key_fingerprint=?, gossip_key_fingerprint=?, verified_key=?, verified_key_fingerprint=? \
WHERE addr=?;\x00"
as *const u8 as *const libc::c_char
)
};
unsafe { sqlite3_bind_int64(stmt, 1, self.last_seen as sqlite3_int64) };
unsafe { sqlite3_bind_int64(stmt, 2, self.last_seen_autocrypt as sqlite3_int64) };
unsafe { sqlite3_bind_int64(stmt, 3, self.prefer_encrypt as sqlite3_int64) };
let pub_bytes = self
.public_key
.as_ref()
.map(|k| k.to_bytes())
.unwrap_or_default();
let gossip_bytes = self
.gossip_key
.as_ref()
.map(|k| k.to_bytes())
.unwrap_or_default();
let ver_bytes = self
.verified_key()
.as_ref()
.map(|k| k.to_bytes())
.unwrap_or_default();
let pkc = self
.public_key_fingerprint
.as_ref()
.map(to_cstring)
.unwrap_or_default();
let pkc_ptr = if self.public_key_fingerprint.is_some() {
pkc.as_ptr()
} else {
std::ptr::null()
};
let gkc = self
.gossip_key_fingerprint
.as_ref()
.map(to_cstring)
.unwrap_or_default();
let gkc_ptr = if self.gossip_key_fingerprint.is_some() {
gkc.as_ptr()
} else {
std::ptr::null_mut()
};
let vkc = self
.verified_key_fingerprint
.as_ref()
.map(to_cstring)
.unwrap_or_default();
let vkc_ptr = if self.verified_key_fingerprint.is_some() {
vkc.as_ptr()
} else {
std::ptr::null_mut()
};
let addr: String = self.addr.clone().unwrap_or_default();
let addr_c = to_cstring(addr);
unsafe {
sqlite3_bind_blob(
stmt,
4,
pub_bytes.as_ptr() as *const _,
pub_bytes.len() as libc::c_int,
None,
)
};
unsafe { sqlite3_bind_int64(stmt, 5, self.gossip_timestamp as sqlite3_int64) };
unsafe {
sqlite3_bind_blob(
stmt,
6,
gossip_bytes.as_ptr() as *const _,
gossip_bytes.len() as libc::c_int,
None,
)
};
unsafe { sqlite3_bind_text(stmt, 7, pkc_ptr as *const _, -1, None) };
unsafe { sqlite3_bind_text(stmt, 8, gkc_ptr as *const _, -1, None) };
unsafe {
sqlite3_bind_blob(
stmt,
9,
ver_bytes.as_ptr() as *const _,
ver_bytes.len() as libc::c_int,
None,
)
};
unsafe { sqlite3_bind_text(stmt, 10, vkc_ptr as *const _, -1, None) };
unsafe { sqlite3_bind_text(stmt, 11, addr_c.as_ptr(), -1, None) };
if unsafe { sqlite3_step(stmt) } == 101 {
success = true;
}
unsafe { sqlite3_finalize(stmt) };
success = dc_sqlite3_execute(
self.context,
sql,
"UPDATE acpeerstates \
SET last_seen=?, last_seen_autocrypt=?, prefer_encrypted=?, \
public_key=?, gossip_timestamp=?, gossip_key=?, public_key_fingerprint=?, gossip_key_fingerprint=?, \
verified_key=?, verified_key_fingerprint=? \
WHERE addr=?;",
params![
self.last_seen,
self.last_seen_autocrypt,
self.prefer_encrypt as i64,
self.public_key.as_ref().map(|k| k.to_bytes()),
self.gossip_timestamp,
self.gossip_key.as_ref().map(|k| k.to_bytes()),
&self.public_key_fingerprint,
&self.gossip_key_fingerprint,
self.verified_key().map(|k| k.to_bytes()),
&self.verified_key_fingerprint,
&self.addr,
],
);
} else if self.to_save == Some(ToSave::Timestamps) {
let stmt = unsafe {
dc_sqlite3_prepare(
self.context,sql,
b"UPDATE acpeerstates SET last_seen=?, last_seen_autocrypt=?, gossip_timestamp=? WHERE addr=?;\x00"
as *const u8 as *const libc::c_char)
};
let c_addr = self.addr.as_ref().map(to_cstring).unwrap_or_default();
let addr_ptr = if self.addr.is_some() {
c_addr.as_ptr()
} else {
std::ptr::null()
};
unsafe { sqlite3_bind_int64(stmt, 1, self.last_seen as sqlite3_int64) };
unsafe { sqlite3_bind_int64(stmt, 2, self.last_seen_autocrypt as sqlite3_int64) };
unsafe { sqlite3_bind_int64(stmt, 3, self.gossip_timestamp as sqlite3_int64) };
unsafe { sqlite3_bind_text(stmt, 4, addr_ptr, -1, None) };
if unsafe { sqlite3_step(stmt) } == 101 {
success = true;
}
unsafe { sqlite3_finalize(stmt) };
success = dc_sqlite3_execute(
self.context,
sql,
"UPDATE acpeerstates SET last_seen=?, last_seen_autocrypt=?, gossip_timestamp=? \
WHERE addr=?;",
params![
self.last_seen,
self.last_seen_autocrypt,
self.gossip_timestamp,
&self.addr
],
);
}
if self.to_save == Some(ToSave::All) || create {
@@ -586,7 +471,7 @@ mod tests {
use super::*;
use pretty_assertions::assert_eq;
use std::ffi::CStr;
use std::ffi::{CStr, CString};
use tempfile::{tempdir, TempDir};
use crate::context::*;