refactor: use enum for system messages

This commit is contained in:
dignifiedquire
2019-09-14 23:55:35 +02:00
committed by holger krekel
parent 0346dd15d9
commit c0747bf68d
5 changed files with 224 additions and 176 deletions

View File

@@ -7,6 +7,7 @@ use crate::config::*;
use crate::constants::*; use crate::constants::*;
use crate::contact::*; use crate::contact::*;
use crate::context::Context; use crate::context::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;
@@ -1669,7 +1670,8 @@ pub fn set_chat_profile_image(
if chat.update_param(context).is_ok() { if chat.update_param(context).is_ok() {
if chat.is_promoted() { if chat.is_promoted() {
let mut msg = dc_msg_new_untyped(); let mut msg = dc_msg_new_untyped();
msg.param.set_int(Param::Cmd, DC_CMD_GROUPIMAGE_CHANGED); msg.param
.set_int(Param::Cmd, SystemMessage::GroupImageChanged as i32);
msg.type_0 = Viewtype::Text; msg.type_0 = Viewtype::Text;
msg.text = Some(context.stock_system_msg( msg.text = Some(context.stock_system_msg(
if new_image_rel == "" { if new_image_rel == "" {

View File

@@ -314,12 +314,3 @@ pub enum KeyType {
Public = 0, Public = 0,
Private = 1, Private = 1,
} }
pub const DC_CMD_GROUPNAME_CHANGED: libc::c_int = 2;
pub const DC_CMD_GROUPIMAGE_CHANGED: libc::c_int = 3;
pub const DC_CMD_MEMBER_ADDED_TO_GROUP: libc::c_int = 4;
pub const DC_CMD_MEMBER_REMOVED_FROM_GROUP: libc::c_int = 5;
pub const DC_CMD_AUTOCRYPT_SETUP_MESSAGE: libc::c_int = 6;
pub const DC_CMD_SECUREJOIN_MESSAGE: libc::c_int = 7;
pub const DC_CMD_LOCATION_STREAMING_ENABLED: libc::c_int = 8;
pub const DC_CMD_LOCATION_ONLY: libc::c_int = 9;

View File

@@ -11,11 +11,13 @@ 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::*;
use crate::contact::*; use crate::contact::*;
use crate::context::{get_version_str, Context}; use crate::context::{get_version_str, Context};
use crate::dc_mimeparser::SystemMessage;
use crate::dc_strencode::*; use crate::dc_strencode::*;
use crate::dc_tools::*; use crate::dc_tools::*;
use crate::e2ee::*; use crate::e2ee::*;
@@ -529,7 +531,12 @@ pub unsafe fn dc_mimefactory_render(context: &Context, factory: &mut dc_mimefact
} }
/* build header etc. */ /* build header etc. */
let command = factory.msg.param.get_int(Param::Cmd).unwrap_or_default(); let command = factory
.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,
@@ -546,181 +553,202 @@ pub unsafe fn dc_mimefactory_render(context: &Context, factory: &mut dc_mimefact
dc_encode_header_words(name.as_ptr()), dc_encode_header_words(name.as_ptr()),
), ),
); );
if command == DC_CMD_MEMBER_REMOVED_FROM_GROUP {
let email_to_remove = factory
.msg
.param
.get(Param::Arg)
.unwrap_or_default()
.strdup();
if strlen(email_to_remove) > 0 {
mailimf_fields_add(
imf_fields,
mailimf_field_new_custom(
strdup(
b"Chat-Group-Member-Removed\x00" as *const u8
as *const libc::c_char,
),
email_to_remove,
),
);
}
} else if command == DC_CMD_MEMBER_ADDED_TO_GROUP {
let msg = &factory.msg;
do_gossip = 1;
let email_to_add = msg.param.get(Param::Arg).unwrap_or_default().strdup();
if strlen(email_to_add) > 0 {
mailimf_fields_add(
imf_fields,
mailimf_field_new_custom(
strdup(
b"Chat-Group-Member-Added\x00" as *const u8
as *const libc::c_char,
),
email_to_add,
),
);
grpimage = chat.param.get(Param::ProfileImage);
}
if 0 != msg.param.get_int(Param::Arg2).unwrap_or_default() & 0x1 {
info!(
context,
"sending secure-join message \'{}\' >>>>>>>>>>>>>>>>>>>>>>>>>",
"vg-member-added",
);
mailimf_fields_add(
imf_fields,
mailimf_field_new_custom(
strdup(b"Secure-Join\x00" as *const u8 as *const libc::c_char),
strdup(b"vg-member-added\x00" as *const u8 as *const libc::c_char),
),
);
}
} else if command == DC_CMD_GROUPNAME_CHANGED {
let msg = &factory.msg;
let value_to_add = msg.param.get(Param::Arg).unwrap_or_default().strdup(); match command {
mailimf_fields_add( SystemMessage::Unknown | SystemMessage::LocationOnly => {}
imf_fields, SystemMessage::MemberRemovedFromGroup => {
mailimf_field_new_custom( let email_to_remove = factory
strdup( .msg
b"Chat-Group-Name-Changed\x00" as *const u8 as *const libc::c_char, .param
), .get(Param::Arg)
value_to_add, .unwrap_or_default()
), .strdup();
); if strlen(email_to_remove) > 0 {
} else if command == DC_CMD_GROUPIMAGE_CHANGED { mailimf_fields_add(
let msg = &factory.msg; imf_fields,
grpimage = msg.param.get(Param::Arg); mailimf_field_new_custom(
if grpimage.is_none() {
mailimf_fields_add(
imf_fields,
mailimf_field_new_custom(
strdup(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),
),
);
}
}
}
if command == DC_CMD_LOCATION_STREAMING_ENABLED {
mailimf_fields_add(
imf_fields,
mailimf_field_new_custom(
strdup(b"Chat-Content\x00" as *const u8 as *const libc::c_char),
strdup(
b"location-streaming-enabled\x00" as *const u8 as *const libc::c_char,
),
),
);
}
if command == DC_CMD_AUTOCRYPT_SETUP_MESSAGE {
mailimf_fields_add(
imf_fields,
mailimf_field_new_custom(
strdup(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),
),
);
placeholdertext = factory
.context
.stock_str(StockMessage::AcSetupMsgBody)
.strdup();
}
if command == DC_CMD_SECUREJOIN_MESSAGE {
let msg = &factory.msg;
let step = msg.param.get(Param::Arg).unwrap_or_default().strdup();
if strlen(step) > 0 {
info!(
context,
"sending secure-join message \'{}\' >>>>>>>>>>>>>>>>>>>>>>>>>",
as_str(step),
);
mailimf_fields_add(
imf_fields,
mailimf_field_new_custom(
strdup(b"Secure-Join\x00" as *const u8 as *const libc::c_char),
step,
),
);
let param2 = msg.param.get(Param::Arg2).unwrap_or_default().strdup();
if strlen(param2) > 0 {
mailimf_fields_add(
imf_fields,
mailimf_field_new_custom(
if strcmp(
step,
b"vg-request-with-auth\x00" as *const u8 as *const libc::c_char,
) == 0
|| strcmp(
step,
b"vc-request-with-auth\x00" as *const u8
as *const libc::c_char,
) == 0
{
strdup( strdup(
b"Secure-Join-Auth\x00" as *const u8 as *const libc::c_char, b"Chat-Group-Member-Removed\x00" as *const u8
)
} else {
strdup(
b"Secure-Join-Invitenumber\x00" as *const u8
as *const libc::c_char, as *const libc::c_char,
) ),
}, email_to_remove,
param2, ),
), );
); }
} }
let fingerprint = msg.param.get(Param::Arg3).unwrap_or_default().strdup(); SystemMessage::MemberAddedToGroup => {
if strlen(fingerprint) > 0 { let msg = &factory.msg;
do_gossip = 1;
let email_to_add = msg.param.get(Param::Arg).unwrap_or_default().strdup();
if strlen(email_to_add) > 0 {
mailimf_fields_add(
imf_fields,
mailimf_field_new_custom(
strdup(
b"Chat-Group-Member-Added\x00" as *const u8
as *const libc::c_char,
),
email_to_add,
),
);
grpimage = chat.param.get(Param::ProfileImage);
}
if 0 != msg.param.get_int(Param::Arg2).unwrap_or_default() & 0x1 {
info!(
context,
"sending secure-join message \'{}\' >>>>>>>>>>>>>>>>>>>>>>>>>",
"vg-member-added",
);
mailimf_fields_add(
imf_fields,
mailimf_field_new_custom(
strdup(b"Secure-Join\x00" as *const u8 as *const libc::c_char),
strdup(
b"vg-member-added\x00" as *const u8 as *const libc::c_char,
),
),
);
}
}
SystemMessage::GroupNameChanged => {
let msg = &factory.msg;
let value_to_add = msg.param.get(Param::Arg).unwrap_or_default().strdup();
mailimf_fields_add( mailimf_fields_add(
imf_fields, imf_fields,
mailimf_field_new_custom( mailimf_field_new_custom(
strdup( strdup(
b"Secure-Join-Fingerprint\x00" as *const u8 b"Chat-Group-Name-Changed\x00" as *const u8
as *const libc::c_char, as *const libc::c_char,
), ),
fingerprint, value_to_add,
), ),
); );
} }
let grpid = match msg.param.get(Param::Arg4) { SystemMessage::GroupImageChanged => {
Some(id) => id.strdup(), let msg = &factory.msg;
None => std::ptr::null_mut(), grpimage = msg.param.get(Param::Arg);
}; if grpimage.is_none() {
if !grpid.is_null() { mailimf_fields_add(
imf_fields,
mailimf_field_new_custom(
strdup(
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),
),
);
}
}
SystemMessage::LocationStreamingEnabled => {
mailimf_fields_add(
imf_fields,
mailimf_field_new_custom(
strdup(b"Chat-Content\x00" as *const u8 as *const libc::c_char),
strdup(
b"location-streaming-enabled\x00" as *const u8
as *const libc::c_char,
),
),
);
}
SystemMessage::AutocryptSetupMessage => {
mailimf_fields_add( mailimf_fields_add(
imf_fields, imf_fields,
mailimf_field_new_custom( mailimf_field_new_custom(
strdup( strdup(
b"Secure-Join-Group\x00" as *const u8 as *const libc::c_char, b"Autocrypt-Setup-Message\x00" as *const u8
as *const libc::c_char,
), ),
grpid, strdup(b"v1\x00" as *const u8 as *const libc::c_char),
), ),
); );
placeholdertext = factory
.context
.stock_str(StockMessage::AcSetupMsgBody)
.strdup();
}
SystemMessage::SecurejoinMessage => {
let msg = &factory.msg;
let step = msg.param.get(Param::Arg).unwrap_or_default().strdup();
if strlen(step) > 0 {
info!(
context,
"sending secure-join message \'{}\' >>>>>>>>>>>>>>>>>>>>>>>>>",
as_str(step),
);
mailimf_fields_add(
imf_fields,
mailimf_field_new_custom(
strdup(b"Secure-Join\x00" as *const u8 as *const libc::c_char),
step,
),
);
let param2 = msg.param.get(Param::Arg2).unwrap_or_default().strdup();
if strlen(param2) > 0 {
mailimf_fields_add(
imf_fields,
mailimf_field_new_custom(
if strcmp(
step,
b"vg-request-with-auth\x00" as *const u8
as *const libc::c_char,
) == 0
|| strcmp(
step,
b"vc-request-with-auth\x00" as *const u8
as *const libc::c_char,
) == 0
{
strdup(
b"Secure-Join-Auth\x00" as *const u8
as *const libc::c_char,
)
} else {
strdup(
b"Secure-Join-Invitenumber\x00" as *const u8
as *const libc::c_char,
)
},
param2,
),
);
}
let fingerprint =
msg.param.get(Param::Arg3).unwrap_or_default().strdup();
if strlen(fingerprint) > 0 {
mailimf_fields_add(
imf_fields,
mailimf_field_new_custom(
strdup(
b"Secure-Join-Fingerprint\x00" as *const u8
as *const libc::c_char,
),
fingerprint,
),
);
}
let grpid = match msg.param.get(Param::Arg4) {
Some(id) => id.strdup(),
None => std::ptr::null_mut(),
};
if !grpid.is_null() {
mailimf_fields_add(
imf_fields,
mailimf_field_new_custom(
strdup(
b"Secure-Join-Group\x00" as *const u8
as *const libc::c_char,
),
grpid,
),
);
}
}
} }
} }
} }
if let Some(grpimage) = grpimage { if let Some(grpimage) = grpimage {
info!(factory.context, "setting group image '{}'", grpimage); info!(factory.context, "setting group image '{}'", grpimage);
let mut meta = dc_msg_new_untyped(); let mut meta = dc_msg_new_untyped();
@@ -1079,7 +1107,13 @@ 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.param.get_int(Param::Cmd).unwrap_or_default() == DC_CMD_AUTOCRYPT_SETUP_MESSAGE { if msg
.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

@@ -3,6 +3,7 @@ use std::ffi::{CStr, CString};
use std::ptr; use std::ptr;
use charset::Charset; use charset::Charset;
use deltachat_derive::{FromSql, ToSql};
use mmime::clist::*; use mmime::clist::*;
use mmime::mailimf::*; use mmime::mailimf::*;
use mmime::mailimf_types::*; use mmime::mailimf_types::*;
@@ -39,11 +40,31 @@ pub struct MimeParser<'a> {
pub e2ee_helper: E2eeHelper, pub e2ee_helper: E2eeHelper,
pub is_forwarded: bool, pub is_forwarded: bool,
pub reports: Vec<*mut mailmime>, pub reports: Vec<*mut mailmime>,
pub is_system_message: libc::c_int, pub is_system_message: SystemMessage,
pub location_kml: Option<location::Kml>, pub location_kml: Option<location::Kml>,
pub message_kml: Option<location::Kml>, pub message_kml: Option<location::Kml>,
} }
#[derive(Debug, Display, Clone, Copy, PartialEq, Eq, FromPrimitive, ToPrimitive, ToSql, FromSql)]
#[repr(i32)]
pub enum SystemMessage {
Unknown = 0,
GroupNameChanged = 2,
GroupImageChanged = 3,
MemberAddedToGroup = 4,
MemberRemovedFromGroup = 5,
AutocryptSetupMessage = 6,
SecurejoinMessage = 7,
LocationStreamingEnabled = 8,
LocationOnly = 9,
}
impl Default for SystemMessage {
fn default() -> Self {
SystemMessage::Unknown
}
}
impl<'a> MimeParser<'a> { impl<'a> MimeParser<'a> {
pub fn new(context: &'a Context) -> Self { pub fn new(context: &'a Context) -> Self {
MimeParser { MimeParser {
@@ -59,7 +80,7 @@ impl<'a> MimeParser<'a> {
is_forwarded: false, is_forwarded: false,
context, context,
reports: Vec::new(), reports: Vec::new(),
is_system_message: 0, is_system_message: SystemMessage::Unknown,
location_kml: None, location_kml: None,
message_kml: None, message_kml: None,
} }
@@ -97,7 +118,7 @@ impl<'a> MimeParser<'a> {
.any(|p| p.mimetype == DC_MIMETYPE_AC_SETUP_FILE); .any(|p| p.mimetype == DC_MIMETYPE_AC_SETUP_FILE);
if has_setup_file { if has_setup_file {
self.is_system_message = 6i32; self.is_system_message = SystemMessage::AutocryptSetupMessage;
// TODO: replace the following code with this // TODO: replace the following code with this
// once drain_filter stabilizes. // once drain_filter stabilizes.
@@ -127,7 +148,7 @@ impl<'a> MimeParser<'a> {
b"location-streaming-enabled\x00" as *const u8 as *const libc::c_char, b"location-streaming-enabled\x00" as *const u8 as *const libc::c_char,
) == 0i32 ) == 0i32
{ {
self.is_system_message = 8i32; self.is_system_message = SystemMessage::LocationStreamingEnabled;
} }
} }
} }

View File

@@ -389,7 +389,7 @@ unsafe fn add_parts(
// incoming non-chat messages may be discarded; // incoming non-chat messages may be discarded;
// maybe this can be optimized later, by checking the state before the message body is downloaded // maybe this can be optimized later, by checking the state before the message body is downloaded
let mut allow_creation = 1; let mut allow_creation = 1;
if mime_parser.is_system_message != DC_CMD_AUTOCRYPT_SETUP_MESSAGE && msgrmsg == 0 { if mime_parser.is_system_message != SystemMessage::AutocryptSetupMessage && msgrmsg == 0 {
let show_emails = context let show_emails = context
.sql .sql
.get_config_int(context, "show_emails") .get_config_int(context, "show_emails")
@@ -673,9 +673,9 @@ unsafe fn add_parts(
msg_raw.as_ptr(), msg_raw.as_ptr(),
) )
} }
if 0 != mime_parser.is_system_message { if mime_parser.is_system_message != SystemMessage::Unknown {
part.param part.param
.set_int(Param::Cmd, mime_parser.is_system_message); .set_int(Param::Cmd, mime_parser.is_system_message as i32);
} }
stmt.execute(params![ stmt.execute(params![
@@ -1080,7 +1080,7 @@ unsafe fn create_or_lookup_group(
}; };
}; };
if mime_parser.is_system_message == DC_CMD_LOCATION_STREAMING_ENABLED { if mime_parser.is_system_message == SystemMessage::LocationStreamingEnabled {
better_msg = better_msg =
context.stock_system_msg(StockMessage::MsgLocationEnabled, "", "", from_id as u32) context.stock_system_msg(StockMessage::MsgLocationEnabled, "", "", from_id as u32)
} }
@@ -1154,7 +1154,7 @@ unsafe fn create_or_lookup_group(
} }
if let Some(optional_field) = mime_parser.lookup_optional_field("Chat-Group-Member-Removed") { if let Some(optional_field) = mime_parser.lookup_optional_field("Chat-Group-Member-Removed") {
X_MrRemoveFromGrp = (*optional_field).fld_value; X_MrRemoveFromGrp = (*optional_field).fld_value;
mime_parser.is_system_message = DC_CMD_MEMBER_REMOVED_FROM_GROUP; mime_parser.is_system_message = SystemMessage::MemberRemovedFromGroup;
let left_group = (Contact::lookup_id_by_addr(context, as_str(X_MrRemoveFromGrp)) let left_group = (Contact::lookup_id_by_addr(context, as_str(X_MrRemoveFromGrp))
== from_id as u32) as libc::c_int; == from_id as u32) as libc::c_int;
better_msg = context.stock_system_msg( better_msg = context.stock_system_msg(
@@ -1170,7 +1170,7 @@ unsafe fn create_or_lookup_group(
} else { } else {
if let Some(optional_field) = mime_parser.lookup_optional_field("Chat-Group-Member-Added") { if let Some(optional_field) = mime_parser.lookup_optional_field("Chat-Group-Member-Added") {
X_MrAddToGrp = (*optional_field).fld_value; X_MrAddToGrp = (*optional_field).fld_value;
mime_parser.is_system_message = DC_CMD_MEMBER_ADDED_TO_GROUP; mime_parser.is_system_message = SystemMessage::MemberAddedToGroup;
if let Some(optional_field) = mime_parser.lookup_optional_field("Chat-Group-Image") { if let Some(optional_field) = mime_parser.lookup_optional_field("Chat-Group-Image") {
// fld_value is a pointer somewhere into mime_parser, must not be freed // fld_value is a pointer somewhere into mime_parser, must not be freed
X_MrGrpImageChanged = as_str((*optional_field).fld_value).to_string(); X_MrGrpImageChanged = as_str((*optional_field).fld_value).to_string();
@@ -1186,7 +1186,7 @@ unsafe fn create_or_lookup_group(
mime_parser.lookup_optional_field("Chat-Group-Name-Changed") mime_parser.lookup_optional_field("Chat-Group-Name-Changed")
{ {
X_MrGrpNameChanged = 1; X_MrGrpNameChanged = 1;
mime_parser.is_system_message = DC_CMD_GROUPNAME_CHANGED; mime_parser.is_system_message = SystemMessage::GroupNameChanged;
better_msg = context.stock_system_msg( better_msg = context.stock_system_msg(
StockMessage::MsgGrpName, StockMessage::MsgGrpName,
as_str((*optional_field).fld_value), as_str((*optional_field).fld_value),
@@ -1198,7 +1198,7 @@ unsafe fn create_or_lookup_group(
{ {
// fld_value is a pointer somewhere into mime_parser, must not be freed // fld_value is a pointer somewhere into mime_parser, must not be freed
X_MrGrpImageChanged = as_str((*optional_field).fld_value).to_string(); X_MrGrpImageChanged = as_str((*optional_field).fld_value).to_string();
mime_parser.is_system_message = DC_CMD_GROUPIMAGE_CHANGED; mime_parser.is_system_message = SystemMessage::GroupImageChanged;
better_msg = context.stock_system_msg( better_msg = context.stock_system_msg(
if X_MrGrpImageChanged == "0" { if X_MrGrpImageChanged == "0" {
StockMessage::MsgGrpImgDeleted StockMessage::MsgGrpImgDeleted