cleanup and fix earlier introduced scoping error

This commit is contained in:
dignifiedquire
2019-09-15 12:08:07 +02:00
committed by holger krekel
parent 6be3c9a48a
commit e710836276
5 changed files with 205 additions and 228 deletions

View File

@@ -490,10 +490,9 @@ pub unsafe fn dc_normalize_setup_code(
pub unsafe fn dc_job_do_DC_JOB_IMEX_IMAP(context: &Context, job: &Job) { pub unsafe fn dc_job_do_DC_JOB_IMEX_IMAP(context: &Context, job: &Job) {
let mut ok_to_continue = true; let mut ok_to_continue = true;
let mut success: libc::c_int = 0; let mut success: libc::c_int = 0;
let what: libc::c_int;
if dc_alloc_ongoing(context) { if dc_alloc_ongoing(context) {
what = job.param.get_int(Param::Cmd).unwrap_or_default(); let what = job.param.get_int(Param::Cmd).unwrap_or_default();
let param1_s = job.param.get(Param::Arg).unwrap_or_default(); let param1_s = job.param.get(Param::Arg).unwrap_or_default();
let param1 = CString::yolo(param1_s); let param1 = CString::yolo(param1_s);
let _param2 = CString::yolo(job.param.get(Param::Arg2).unwrap_or_default()); let _param2 = CString::yolo(job.param.get(Param::Arg2).unwrap_or_default());

View File

@@ -11,7 +11,6 @@ use mmime::mailmime_types_helper::*;
use mmime::mailmime_write_mem::*; use mmime::mailmime_write_mem::*;
use mmime::mmapstring::*; use mmime::mmapstring::*;
use mmime::other::*; use mmime::other::*;
use num_traits::FromPrimitive;
use crate::chat::{self, Chat}; use crate::chat::{self, Chat};
use crate::constants::*; use crate::constants::*;
@@ -172,10 +171,10 @@ pub unsafe fn dc_mimefactory_load_msg(
) )
.unwrap(); .unwrap();
let command = factory.msg.param.get_int(Param::Cmd).unwrap_or_default(); let command = factory.msg.param.get_cmd();
let msg = &factory.msg; let msg = &factory.msg;
if command == 5 { if command == SystemMessage::MemberRemovedFromGroup {
let email_to_remove = msg.param.get(Param::Arg).unwrap_or_default(); let email_to_remove = msg.param.get(Param::Arg).unwrap_or_default();
let email_to_remove_c = email_to_remove.strdup(); let email_to_remove_c = email_to_remove.strdup();
@@ -199,8 +198,8 @@ pub unsafe fn dc_mimefactory_load_msg(
} }
} }
} }
if command != 6 if command != SystemMessage::AutocryptSetupMessage
&& command != 7 && command != SystemMessage::SecurejoinMessage
&& 0 != context && 0 != context
.sql .sql
.get_config_int(context, "mdns_enabled") .get_config_int(context, "mdns_enabled")
@@ -531,12 +530,7 @@ pub unsafe fn dc_mimefactory_render(context: &Context, factory: &mut dc_mimefact
} }
/* build header etc. */ /* build header etc. */
let command = factory let command = factory.msg.param.get_cmd();
.msg
.param
.get_int(Param::Cmd)
.and_then(SystemMessage::from_i32)
.unwrap_or_default();
if chat.typ == Chattype::Group || chat.typ == Chattype::VerifiedGroup { if chat.typ == Chattype::Group || chat.typ == Chattype::VerifiedGroup {
mailimf_fields_add( mailimf_fields_add(
imf_fields, imf_fields,
@@ -554,9 +548,7 @@ pub unsafe fn dc_mimefactory_render(context: &Context, factory: &mut dc_mimefact
), ),
); );
match command { if command == SystemMessage::MemberRemovedFromGroup {
SystemMessage::Unknown | SystemMessage::LocationOnly => {}
SystemMessage::MemberRemovedFromGroup => {
let email_to_remove = factory let email_to_remove = factory
.msg .msg
.param .param
@@ -575,8 +567,7 @@ pub unsafe fn dc_mimefactory_render(context: &Context, factory: &mut dc_mimefact
), ),
); );
} }
} } else if command == SystemMessage::MemberAddedToGroup {
SystemMessage::MemberAddedToGroup => {
let msg = &factory.msg; let msg = &factory.msg;
do_gossip = 1; do_gossip = 1;
let email_to_add = msg.param.get(Param::Arg).unwrap_or_default().strdup(); let email_to_add = msg.param.get(Param::Arg).unwrap_or_default().strdup();
@@ -603,14 +594,11 @@ pub unsafe fn dc_mimefactory_render(context: &Context, factory: &mut dc_mimefact
imf_fields, imf_fields,
mailimf_field_new_custom( mailimf_field_new_custom(
strdup(b"Secure-Join\x00" as *const u8 as *const libc::c_char), strdup(b"Secure-Join\x00" as *const u8 as *const libc::c_char),
strdup( strdup(b"vg-member-added\x00" as *const u8 as *const libc::c_char),
b"vg-member-added\x00" as *const u8 as *const libc::c_char,
),
), ),
); );
} }
} } else if command == SystemMessage::GroupNameChanged {
SystemMessage::GroupNameChanged => {
let msg = &factory.msg; let msg = &factory.msg;
let value_to_add = msg.param.get(Param::Arg).unwrap_or_default().strdup(); let value_to_add = msg.param.get(Param::Arg).unwrap_or_default().strdup();
@@ -618,48 +606,42 @@ pub unsafe fn dc_mimefactory_render(context: &Context, factory: &mut dc_mimefact
imf_fields, imf_fields,
mailimf_field_new_custom( mailimf_field_new_custom(
strdup( strdup(
b"Chat-Group-Name-Changed\x00" as *const u8 b"Chat-Group-Name-Changed\x00" as *const u8 as *const libc::c_char,
as *const libc::c_char,
), ),
value_to_add, value_to_add,
), ),
); );
} } else if command == SystemMessage::GroupImageChanged {
SystemMessage::GroupImageChanged => {
let msg = &factory.msg; let msg = &factory.msg;
grpimage = msg.param.get(Param::Arg); grpimage = msg.param.get(Param::Arg);
if grpimage.is_none() { if grpimage.is_none() {
mailimf_fields_add( mailimf_fields_add(
imf_fields, imf_fields,
mailimf_field_new_custom( mailimf_field_new_custom(
strdup( strdup(b"Chat-Group-Image\x00" as *const u8 as *const libc::c_char),
b"Chat-Group-Image\x00" as *const u8 as *const libc::c_char,
),
dc_strdup(b"0\x00" as *const u8 as *const libc::c_char), dc_strdup(b"0\x00" as *const u8 as *const libc::c_char),
), ),
); );
} }
} }
SystemMessage::LocationStreamingEnabled => { }
if command == SystemMessage::LocationStreamingEnabled {
mailimf_fields_add( mailimf_fields_add(
imf_fields, imf_fields,
mailimf_field_new_custom( mailimf_field_new_custom(
strdup(b"Chat-Content\x00" as *const u8 as *const libc::c_char), strdup(b"Chat-Content\x00" as *const u8 as *const libc::c_char),
strdup( strdup(
b"location-streaming-enabled\x00" as *const u8 b"location-streaming-enabled\x00" as *const u8 as *const libc::c_char,
as *const libc::c_char,
), ),
), ),
); );
} }
SystemMessage::AutocryptSetupMessage => { if command == SystemMessage::AutocryptSetupMessage {
mailimf_fields_add( mailimf_fields_add(
imf_fields, imf_fields,
mailimf_field_new_custom( mailimf_field_new_custom(
strdup( strdup(b"Autocrypt-Setup-Message\x00" as *const u8 as *const libc::c_char),
b"Autocrypt-Setup-Message\x00" as *const u8
as *const libc::c_char,
),
strdup(b"v1\x00" as *const u8 as *const libc::c_char), strdup(b"v1\x00" as *const u8 as *const libc::c_char),
), ),
); );
@@ -668,7 +650,7 @@ pub unsafe fn dc_mimefactory_render(context: &Context, factory: &mut dc_mimefact
.stock_str(StockMessage::AcSetupMsgBody) .stock_str(StockMessage::AcSetupMsgBody)
.strdup(); .strdup();
} }
SystemMessage::SecurejoinMessage => { if command == SystemMessage::SecurejoinMessage {
let msg = &factory.msg; let msg = &factory.msg;
let step = msg.param.get(Param::Arg).unwrap_or_default().strdup(); let step = msg.param.get(Param::Arg).unwrap_or_default().strdup();
if strlen(step) > 0 { if strlen(step) > 0 {
@@ -691,8 +673,7 @@ pub unsafe fn dc_mimefactory_render(context: &Context, factory: &mut dc_mimefact
mailimf_field_new_custom( mailimf_field_new_custom(
if strcmp( if strcmp(
step, step,
b"vg-request-with-auth\x00" as *const u8 b"vg-request-with-auth\x00" as *const u8 as *const libc::c_char,
as *const libc::c_char,
) == 0 ) == 0
|| strcmp( || strcmp(
step, step,
@@ -701,8 +682,7 @@ pub unsafe fn dc_mimefactory_render(context: &Context, factory: &mut dc_mimefact
) == 0 ) == 0
{ {
strdup( strdup(
b"Secure-Join-Auth\x00" as *const u8 b"Secure-Join-Auth\x00" as *const u8 as *const libc::c_char,
as *const libc::c_char,
) )
} else { } else {
strdup( strdup(
@@ -714,8 +694,7 @@ pub unsafe fn dc_mimefactory_render(context: &Context, factory: &mut dc_mimefact
), ),
); );
} }
let fingerprint = let fingerprint = msg.param.get(Param::Arg3).unwrap_or_default().strdup();
msg.param.get(Param::Arg3).unwrap_or_default().strdup();
if strlen(fingerprint) > 0 { if strlen(fingerprint) > 0 {
mailimf_fields_add( mailimf_fields_add(
imf_fields, imf_fields,
@@ -737,8 +716,7 @@ pub unsafe fn dc_mimefactory_render(context: &Context, factory: &mut dc_mimefact
imf_fields, imf_fields,
mailimf_field_new_custom( mailimf_field_new_custom(
strdup( strdup(
b"Secure-Join-Group\x00" as *const u8 b"Secure-Join-Group\x00" as *const u8 as *const libc::c_char,
as *const libc::c_char,
), ),
grpid, grpid,
), ),
@@ -746,8 +724,6 @@ pub unsafe fn dc_mimefactory_render(context: &Context, factory: &mut dc_mimefact
} }
} }
} }
}
}
if let Some(grpimage) = grpimage { if let Some(grpimage) = grpimage {
info!(factory.context, "setting group image '{}'", grpimage); info!(factory.context, "setting group image '{}'", grpimage);
@@ -1107,13 +1083,7 @@ unsafe fn get_subject(
} else { } else {
b"\x00" as *const u8 as *const libc::c_char b"\x00" as *const u8 as *const libc::c_char
}; };
if msg if msg.param.get_cmd() == SystemMessage::AutocryptSetupMessage {
.param
.get_int(Param::Cmd)
.and_then(SystemMessage::from_i32)
.unwrap_or_default()
== SystemMessage::AutocryptSetupMessage
{
ret = context.stock_str(StockMessage::AcSetupMsgSubject).strdup() ret = context.stock_str(StockMessage::AcSetupMsgSubject).strdup()
} else if chat.typ == Chattype::Group || chat.typ == Chattype::VerifiedGroup { } else if chat.typ == Chattype::Group || chat.typ == Chattype::VerifiedGroup {
ret = format!( ret = format!(

View File

@@ -111,6 +111,7 @@ impl<'a> MimeParser<'a> {
if self.lookup_optional_field("Chat-Version").is_some() { if self.lookup_optional_field("Chat-Version").is_some() {
self.is_send_by_messenger = true self.is_send_by_messenger = true
} }
if self.lookup_field("Autocrypt-Setup-Message").is_some() { if self.lookup_field("Autocrypt-Setup-Message").is_some() {
let has_setup_file = self let has_setup_file = self
.parts .parts
@@ -326,8 +327,7 @@ impl<'a> MimeParser<'a> {
} }
pub fn lookup_optional_field(&self, field_name: &str) -> Option<String> { pub fn lookup_optional_field(&self, field_name: &str) -> Option<String> {
if let Some(field) = self.lookup_field(field_name) { if let Some(field) = self.lookup_field_typ(field_name, MAILIMF_FIELD_OPTIONAL_FIELD) {
if unsafe { (*field).fld_type } == MAILIMF_FIELD_OPTIONAL_FIELD as libc::c_int {
let val = unsafe { (*field).fld_data.fld_optional_field }; let val = unsafe { (*field).fld_data.fld_optional_field };
if val.is_null() { if val.is_null() {
return None; return None;
@@ -335,7 +335,6 @@ impl<'a> MimeParser<'a> {
return Some(unsafe { to_string_lossy((*val).fld_value) }); return Some(unsafe { to_string_lossy((*val).fld_value) });
} }
} }
}
None None
} }
@@ -1063,22 +1062,28 @@ unsafe fn hash_header(out: &mut HashMap<String, *mut mailimf_field>, in_0: *cons
let field = cur as *mut mailimf_field; let field = cur as *mut mailimf_field;
// TODO match on enums /rtn // TODO match on enums /rtn
let key = match (*field).fld_type { let key = match (*field).fld_type as libc::c_uint {
1 => Some("Return-Path".to_string()), MAILIMF_FIELD_RETURN_PATH => Some("Return-Path".to_string()),
9 => Some("Date".to_string()), MAILIMF_FIELD_ORIG_DATE => Some("Date".to_string()),
10 => Some("From".to_string()), MAILIMF_FIELD_FROM => Some("From".to_string()),
11 => Some("Sender".to_string()), MAILIMF_FIELD_SENDER => Some("Sender".to_string()),
12 => Some("Reply-To".to_string()), MAILIMF_FIELD_REPLY_TO => Some("Reply-To".to_string()),
13 => Some("To".to_string()), MAILIMF_FIELD_TO => Some("To".to_string()),
14 => Some("Cc".to_string()), MAILIMF_FIELD_CC => Some("Cc".to_string()),
15 => Some("Bcc".to_string()), MAILIMF_FIELD_BCC => Some("Bcc".to_string()),
16 => Some("Message-ID".to_string()), MAILIMF_FIELD_MESSAGE_ID => Some("Message-ID".to_string()),
17 => Some("In-Reply-To".to_string()), MAILIMF_FIELD_IN_REPLY_TO => Some("In-Reply-To".to_string()),
18 => Some("References".to_string()), MAILIMF_FIELD_REFERENCES => Some("References".to_string()),
19 => Some("Subject".to_string()), MAILIMF_FIELD_SUBJECT => Some("Subject".to_string()),
22 => { MAILIMF_FIELD_OPTIONAL_FIELD => {
// MAILIMF_FIELD_OPTIONAL_FIELD // MAILIMF_FIELD_OPTIONAL_FIELD
let optional_field = (*field).fld_data.fld_optional_field; let optional_field = (*field).fld_data.fld_optional_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.
if !optional_field.is_null() { if !optional_field.is_null() {
Some(to_string_lossy((*optional_field).fld_name)) Some(to_string_lossy((*optional_field).fld_name))
} else { } else {
@@ -1088,12 +1093,6 @@ unsafe fn hash_header(out: &mut HashMap<String, *mut mailimf_field>, in_0: *cons
_ => None, _ => None,
}; };
if let Some(key) = key { if let Some(key) = key {
// 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.
if !out.contains_key(&key) || // key already exists, only overwrite known types (protected headers) if !out.contains_key(&key) || // key already exists, only overwrite known types (protected headers)
(*field).fld_type != MAILIMF_FIELD_OPTIONAL_FIELD as i32 || key.starts_with("Chat-") (*field).fld_type != MAILIMF_FIELD_OPTIONAL_FIELD as i32 || key.starts_with("Chat-")
{ {

View File

@@ -9,6 +9,7 @@ use crate::chat::{self, Chat};
use crate::constants::*; use crate::constants::*;
use crate::contact::*; use crate::contact::*;
use crate::context::*; use crate::context::*;
use crate::dc_mimeparser::SystemMessage;
use crate::dc_tools::*; use crate::dc_tools::*;
use crate::error::Error; use crate::error::Error;
use crate::events::Event; use crate::events::Event;
@@ -767,7 +768,7 @@ pub fn dc_msg_get_summarytext_by_raw(
Viewtype::Video => context.stock_str(StockMessage::Video).into_owned(), Viewtype::Video => context.stock_str(StockMessage::Video).into_owned(),
Viewtype::Voice => context.stock_str(StockMessage::VoiceMessage).into_owned(), Viewtype::Voice => context.stock_str(StockMessage::VoiceMessage).into_owned(),
Viewtype::Audio | Viewtype::File => { Viewtype::Audio | Viewtype::File => {
if param.get_int(Param::Cmd) == Some(6) { if param.get_cmd() == SystemMessage::AutocryptSetupMessage {
append_text = false; append_text = false;
context context
.stock_str(StockMessage::AcSetupMsgSubject) .stock_str(StockMessage::AcSetupMsgSubject)
@@ -793,7 +794,7 @@ pub fn dc_msg_get_summarytext_by_raw(
} }
} }
_ => { _ => {
if param.get_int(Param::Cmd) != Some(9) { if param.get_cmd() != SystemMessage::LocationOnly {
"".to_string() "".to_string()
} else { } else {
append_text = false; append_text = false;
@@ -839,10 +840,10 @@ pub fn dc_msg_is_forwarded(msg: &Message) -> bool {
} }
pub fn dc_msg_is_info(msg: &Message) -> bool { pub fn dc_msg_is_info(msg: &Message) -> bool {
let cmd = msg.param.get_int(Param::Cmd).unwrap_or_default(); let cmd = msg.param.get_cmd();
msg.from_id == 2i32 as libc::c_uint msg.from_id == 2i32 as libc::c_uint
|| msg.to_id == 2i32 as libc::c_uint || msg.to_id == 2i32 as libc::c_uint
|| 0 != cmd && cmd != 6i32 || cmd != SystemMessage::Unknown && cmd != SystemMessage::AutocryptSetupMessage
} }
pub fn dc_msg_is_increation(msg: &Message) -> bool { pub fn dc_msg_is_increation(msg: &Message) -> bool {
@@ -854,7 +855,7 @@ pub fn dc_msg_is_setupmessage(msg: &Message) -> bool {
return false; return false;
} }
msg.param.get_int(Param::Cmd) == Some(6) msg.param.get_cmd() == SystemMessage::AutocryptSetupMessage
} }
pub unsafe fn dc_msg_get_setupcodebegin(context: &Context, msg: &Message) -> *mut libc::c_char { pub unsafe fn dc_msg_get_setupcodebegin(context: &Context, msg: &Message) -> *mut libc::c_char {

View File

@@ -4,6 +4,7 @@ use std::str;
use num_traits::FromPrimitive; use num_traits::FromPrimitive;
use crate::dc_mimeparser::SystemMessage;
use crate::error; use crate::error;
/// Available param keys. /// Available param keys.
@@ -178,6 +179,13 @@ impl Params {
self.get(key).and_then(|s| s.parse().ok()) self.get(key).and_then(|s| s.parse().ok())
} }
/// Get the parameter behind `Param::Cmd` interpreted as `SystemMessage`.
pub fn get_cmd(&self) -> SystemMessage {
self.get_int(Param::Cmd)
.and_then(SystemMessage::from_i32)
.unwrap_or_default()
}
/// Get the given parameter and parse as `f64`. /// Get the given parameter and parse as `f64`.
pub fn get_float(&self, key: Param) -> Option<f64> { pub fn get_float(&self, key: Param) -> Option<f64> {
self.get(key).and_then(|s| s.parse().ok()) self.get(key).and_then(|s| s.parse().ok())