1
0
Fork 0
mirror of https://github.com/deltachat/deltachat-core.git synced 2025-10-05 10:39:27 +02:00
This commit is contained in:
B. Petersen 2018-07-06 00:27:52 +02:00
parent 53629e01cf
commit c9a9e42ee4
42 changed files with 2762 additions and 2762 deletions

View file

@ -42,28 +42,28 @@ your library */
*/
int dc_reset_tables(dc_context_t* context, int bits)
{
if( context == NULL || context->magic != DC_CONTEXT_MAGIC ) {
if (context == NULL || context->magic != DC_CONTEXT_MAGIC) {
return 0;
}
dc_log_info(context, 0, "Resetting tables (%i)...", bits);
if( bits & 1 ) {
if (bits & 1) {
dc_sqlite3_execute(context->sql, "DELETE FROM jobs;");
dc_log_info(context, 0, "(1) Jobs reset.");
}
if( bits & 2 ) {
if (bits & 2) {
dc_sqlite3_execute(context->sql, "DELETE FROM acpeerstates;");
dc_log_info(context, 0, "(2) Peerstates reset.");
}
if( bits & 4 ) {
if (bits & 4) {
dc_sqlite3_execute(context->sql, "DELETE FROM keypairs;");
dc_log_info(context, 0, "(4) Private keypairs reset.");
}
if( bits & 8 ) {
if (bits & 8) {
dc_sqlite3_execute(context->sql, "DELETE FROM contacts WHERE id>" DC_STRINGIFY(DC_CONTACT_ID_LAST_SPECIAL) ";"); /* the other IDs are reserved - leave these rows to make sure, the IDs are not used by normal contacts*/
dc_sqlite3_execute(context->sql, "DELETE FROM chats WHERE id>" DC_STRINGIFY(DC_CHAT_ID_LAST_SPECIAL) ";");
dc_sqlite3_execute(context->sql, "DELETE FROM chats_contacts;");
@ -91,7 +91,7 @@ int dc_reset_tables(dc_context_t* context, int bits)
*/
static int dc_cleanup_contacts(dc_context_t* context)
{
if( context == NULL || context->magic != DC_CONTEXT_MAGIC ) {
if (context == NULL || context->magic != DC_CONTEXT_MAGIC) {
return 0;
}
@ -109,11 +109,11 @@ static int dc_poke_eml_file(dc_context_t* context, const char* filename)
char* data = NULL;
size_t data_bytes;
if( context == NULL || context->magic != DC_CONTEXT_MAGIC ) {
if (context == NULL || context->magic != DC_CONTEXT_MAGIC) {
return 0;
}
if( dc_read_file(filename, (void**)&data, &data_bytes, context) == 0 ) {
if (dc_read_file(filename, (void**)&data, &data_bytes, context) == 0) {
goto cleanup;
}
@ -135,21 +135,21 @@ static int poke_public_key(dc_context_t* context, const char* addr, const char*
dc_apeerstate_t* peerstate = dc_apeerstate_new(context);
int success = 0;
if( addr==NULL || public_key_file==NULL || peerstate==NULL || header==NULL ) {
if (addr==NULL || public_key_file==NULL || peerstate==NULL || header==NULL) {
goto cleanup;
}
/* create a fake autocrypt header */
header->addr = dc_strdup(addr);
header->prefer_encrypt = DC_PE_MUTUAL;
if( !dc_key_set_from_file(header->public_key, public_key_file, context)
|| !dc_pgp_is_valid_key(context, header->public_key) ) {
if (!dc_key_set_from_file(header->public_key, public_key_file, context)
|| !dc_pgp_is_valid_key(context, header->public_key)) {
dc_log_warning(context, 0, "No valid key found in \"%s\".", public_key_file);
goto cleanup;
}
/* update/create peerstate */
if( dc_apeerstate_load_by_addr(peerstate, context->sql, addr) ) {
if (dc_apeerstate_load_by_addr(peerstate, context->sql, addr)) {
dc_apeerstate_apply_header(peerstate, header, time(NULL));
dc_apeerstate_save_to_db(peerstate, context->sql, 0);
}
@ -187,62 +187,62 @@ static int poke_spec(dc_context_t* context, const char* spec)
int read_cnt = 0;
char* name;
if( context == NULL ) {
if (context == NULL) {
return 0;
}
if( !dc_sqlite3_is_open(context->sql) ) {
if (!dc_sqlite3_is_open(context->sql)) {
dc_log_error(context, 0, "Import: Database not opened.");
goto cleanup;
}
/* if `spec` is given, remember it for later usage; if it is not given, try to use the last one */
if( spec )
if (spec)
{
real_spec = dc_strdup(spec);
dc_sqlite3_set_config(context->sql, "import_spec", real_spec);
}
else {
real_spec = dc_sqlite3_get_config(context->sql, "import_spec", NULL); /* may still NULL */
if( real_spec == NULL ) {
if (real_spec == NULL) {
dc_log_error(context, 0, "Import: No file or folder given.");
goto cleanup;
}
}
suffix = dc_get_filesuffix_lc(real_spec);
if( suffix && strcmp(suffix, "eml")==0 ) {
if (suffix && strcmp(suffix, "eml")==0) {
/* import a single file */
if( dc_poke_eml_file(context, real_spec) ) { /* errors are logged in any case */
if (dc_poke_eml_file(context, real_spec)) { /* errors are logged in any case */
read_cnt++;
}
}
else if( suffix && (strcmp(suffix, "pem")==0||strcmp(suffix, "asc")==0) ) {
else if (suffix && (strcmp(suffix, "pem")==0||strcmp(suffix, "asc")==0)) {
/* import a publix key */
char* separator = strchr(real_spec, ' ');
if( separator==NULL ) {
if (separator==NULL) {
dc_log_error(context, 0, "Import: Key files must be specified as \"<addr> <key-file>\".");
goto cleanup;
}
*separator = 0;
if( poke_public_key(context, real_spec, separator+1) ) {
if (poke_public_key(context, real_spec, separator+1)) {
read_cnt++;
}
*separator = ' ';
}
else {
/* import a directory */
if( (dir=opendir(real_spec))==NULL ) {
if ((dir=opendir(real_spec))==NULL) {
dc_log_error(context, 0, "Import: Cannot open directory \"%s\".", real_spec);
goto cleanup;
}
while( (dir_entry=readdir(dir))!=NULL ) {
while ((dir_entry=readdir(dir))!=NULL) {
name = dir_entry->d_name; /* name without path; may also be `.` or `..` */
if( strlen(name)>=4 && strcmp(&name[strlen(name)-4], ".eml")==0 ) {
if (strlen(name)>=4 && strcmp(&name[strlen(name)-4], ".eml")==0) {
char* path_plus_name = dc_mprintf("%s/%s", real_spec, name);
dc_log_info(context, 0, "Import: %s", path_plus_name);
if( dc_poke_eml_file(context, path_plus_name) ) { /* no abort on single errors errors are logged in any case */
if (dc_poke_eml_file(context, path_plus_name)) { /* no abort on single errors errors are logged in any case */
read_cnt++;
}
free(path_plus_name);
@ -251,14 +251,14 @@ static int poke_spec(dc_context_t* context, const char* spec)
}
dc_log_info(context, 0, "Import: %i items read from \"%s\".", read_cnt, real_spec);
if( read_cnt > 0 ) {
if (read_cnt > 0) {
context->cb(context, DC_EVENT_MSGS_CHANGED, 0, 0); /* even if read_cnt>0, the number of messages added to the database may be 0. While we regard this issue using IMAP, we ignore it here. */
}
success = 1;
cleanup:
if( dir ) { closedir(dir); }
if (dir) { closedir(dir); }
free(real_spec);
free(suffix);
return success;
@ -268,14 +268,14 @@ cleanup:
static void log_msglist(dc_context_t* context, dc_array_t* msglist)
{
int i, cnt = dc_array_get_cnt(msglist), lines_out = 0;
for( i = 0; i < cnt; i++ )
for (i = 0; i < cnt; i++)
{
uint32_t msg_id = dc_array_get_id(msglist, i);
if( msg_id == DC_MSG_ID_DAYMARKER ) {
if (msg_id == DC_MSG_ID_DAYMARKER) {
dc_log_info(context, 0, "--------------------------------------------------------------------------------"); lines_out++;
}
else if( msg_id > 0 ) {
if( lines_out==0 ) { dc_log_info(context, 0, "--------------------------------------------------------------------------------"); lines_out++; }
else if (msg_id > 0) {
if (lines_out==0) { dc_log_info(context, 0, "--------------------------------------------------------------------------------"); lines_out++; }
dc_msg_t* msg = dc_get_msg(context, msg_id);
dc_contact_t* contact = dc_get_contact(context, dc_msg_get_from_id(msg));
@ -283,7 +283,7 @@ static void log_msglist(dc_context_t* context, dc_array_t* msglist)
int contact_id = dc_contact_get_id(contact);
const char* statestr = "";
switch( dc_msg_get_state(msg) ) {
switch (dc_msg_get_state(msg)) {
case DC_STATE_OUT_PENDING: statestr = " o"; break;
case DC_STATE_OUT_DELIVERED: statestr = ""; break;
case DC_STATE_OUT_MDN_RCVD: statestr = " √√"; break;
@ -312,7 +312,7 @@ static void log_msglist(dc_context_t* context, dc_array_t* msglist)
}
}
if( lines_out > 0 ) { dc_log_info(context, 0, "--------------------------------------------------------------------------------"); }
if (lines_out > 0) { dc_log_info(context, 0, "--------------------------------------------------------------------------------"); }
}
@ -322,20 +322,20 @@ static void log_contactlist(dc_context_t* context, dc_array_t* contacts)
dc_contact_t* contact = NULL;
dc_apeerstate_t* peerstate = dc_apeerstate_new(context);
for( i = 0; i < cnt; i++ ) {
for (i = 0; i < cnt; i++) {
uint32_t contact_id = dc_array_get_id(contacts, i);
char* line = NULL;
char* line2 = NULL;
if( (contact=dc_get_contact(context, contact_id))!=NULL ) {
if ((contact=dc_get_contact(context, contact_id))!=NULL) {
char* name = dc_contact_get_name(contact);
char* addr = dc_contact_get_addr(contact);
int verified_state = dc_contact_is_verified(contact);
const char* verified_str = verified_state? (verified_state==2? " √√":""): "";
line = dc_mprintf("%s%s <%s>", (name&&name[0])? name : "<name unset>", verified_str, (addr&&addr[0])? addr : "addr unset");
int peerstate_ok = dc_apeerstate_load_by_addr(peerstate, context->sql, addr);
if( peerstate_ok && contact_id != DC_CONTACT_ID_SELF ) {
if (peerstate_ok && contact_id != DC_CONTACT_ID_SELF) {
char* pe = NULL;
switch( peerstate->prefer_encrypt ) {
switch (peerstate->prefer_encrypt) {
case DC_PE_MUTUAL: pe = dc_strdup("mutual"); break;
case DC_PE_NOPREFERENCE: pe = dc_strdup("no-preference"); break;
case DC_PE_RESET: pe = dc_strdup("reset"); break;
@ -371,8 +371,8 @@ void dc_cmdline_skip_auth()
static const char* chat_prefix(const dc_chat_t* chat)
{
if( chat->type == DC_CHAT_TYPE_GROUP ) { return "Group"; }
else if( chat->type == DC_CHAT_TYPE_VERIFIED_GROUP ) { return "VerifiedGroup"; }
if (chat->type == DC_CHAT_TYPE_GROUP) { return "Group"; }
else if (chat->type == DC_CHAT_TYPE_VERIFIED_GROUP) { return "VerifiedGroup"; }
else { return "Single"; }
}
@ -386,11 +386,11 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
dc_chat_t* sel_chat = NULL;
if( context == NULL || cmdline == NULL || cmdline[0]==0 ) {
if (context == NULL || cmdline == NULL || cmdline[0]==0) {
goto cleanup;
}
if( context->cmdline_sel_chat_id ) {
if (context->cmdline_sel_chat_id) {
sel_chat = dc_get_chat(context, context->cmdline_sel_chat_id);
}
@ -398,12 +398,12 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
(the first argument may contain spaces, if this is undesired we split further arguments form if below. */
cmd = dc_strdup(cmdline);
arg1 = strchr(cmd, ' ');
if( arg1 ) { *arg1 = 0; arg1++; }
if (arg1) { *arg1 = 0; arg1++; }
/* execute command */
if( strcmp(cmd, "help")==0 || strcmp(cmd, "?")==0 )
if (strcmp(cmd, "help")==0 || strcmp(cmd, "?")==0)
{
if( arg1 && strcmp(arg1, "imex")==0 )
if (arg1 && strcmp(arg1, "imex")==0)
{
ret = dc_strdup(
"====================Import/Export commands==\n"
@ -484,11 +484,11 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
);
}
}
else if( !s_is_auth )
else if (!s_is_auth)
{
if( strcmp(cmd, "auth")==0 ) {
if (strcmp(cmd, "auth")==0) {
char* is_pw = dc_get_config(context, "mail_pw", "");
if( strcmp(arg1, is_pw)==0 ) {
if (strcmp(arg1, is_pw)==0) {
s_is_auth = 1;
ret = COMMAND_SUCCEEDED;
}
@ -500,7 +500,7 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
ret = dc_strdup("Please authorize yourself using: auth <password>");
}
}
else if( strcmp(cmd, "auth")==0 )
else if (strcmp(cmd, "auth")==0)
{
ret = dc_strdup("Already authorized.");
}
@ -510,9 +510,9 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
* Database commands
******************************************************************************/
else if( strcmp(cmd, "open")==0 )
else if (strcmp(cmd, "open")==0)
{
if( arg1 ) {
if (arg1) {
dc_close(context);
ret = dc_open(context, arg1, NULL)? COMMAND_SUCCEEDED : COMMAND_FAILED;
}
@ -520,23 +520,23 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
ret = dc_strdup("ERROR: Argument <file> missing.");
}
}
else if( strcmp(cmd, "close")==0 )
else if (strcmp(cmd, "close")==0)
{
dc_close(context);
ret = COMMAND_SUCCEEDED;
}
else if( strcmp(cmd, "initiate-key-transfer")==0 )
else if (strcmp(cmd, "initiate-key-transfer")==0)
{
char* setup_code = dc_initiate_key_transfer(context);
ret = setup_code? dc_mprintf("Setup code for the transferred setup message: %s", setup_code) : COMMAND_FAILED;
free(setup_code);
}
else if( strcmp(cmd, "get-setupcodebegin")==0 )
else if (strcmp(cmd, "get-setupcodebegin")==0)
{
if( arg1 ) {
if (arg1) {
uint32_t msg_id = (uint32_t)atoi(arg1);
dc_msg_t* msg = dc_get_msg(context, msg_id);
if( dc_msg_is_setupmessage(msg) ) {
if (dc_msg_is_setupmessage(msg)) {
char* setupcodebegin = dc_msg_get_setupcodebegin(msg);
ret = dc_mprintf("The setup code for setup message Msg#%i starts with: %s", msg_id, setupcodebegin);
free(setupcodebegin);
@ -550,11 +550,11 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
ret = dc_strdup("ERROR: Argument <msg-id> missing.");
}
}
else if( strcmp(cmd, "continue-key-transfer")==0 )
else if (strcmp(cmd, "continue-key-transfer")==0)
{
char* arg2 = NULL;
if( arg1 ) { arg2 = strrchr(arg1, ' '); }
if( arg1 && arg2 ) {
if (arg1) { arg2 = strrchr(arg1, ' '); }
if (arg1 && arg2) {
*arg2 = 0; arg2++;
ret = dc_continue_key_transfer(context, atoi(arg1), arg2)? COMMAND_SUCCEEDED : COMMAND_FAILED;
}
@ -562,21 +562,21 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
ret = dc_strdup("ERROR: Arguments <msg-id> <setup-code> expected.");
}
}
else if( strcmp(cmd, "has-backup")==0 )
else if (strcmp(cmd, "has-backup")==0)
{
ret = dc_imex_has_backup(context, context->blobdir);
if( ret == NULL ) {
if (ret == NULL) {
ret = dc_strdup("No backup found.");
}
}
else if( strcmp(cmd, "export-backup")==0 )
else if (strcmp(cmd, "export-backup")==0)
{
dc_imex(context, DC_IMEX_EXPORT_BACKUP, context->blobdir, NULL);
ret = COMMAND_SUCCEEDED;
}
else if( strcmp(cmd, "import-backup")==0 )
else if (strcmp(cmd, "import-backup")==0)
{
if( arg1 ) {
if (arg1) {
dc_imex(context, DC_IMEX_IMPORT_BACKUP, arg1, NULL);
ret = COMMAND_SUCCEEDED;
}
@ -584,23 +584,23 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
ret = dc_strdup("ERROR: Argument <backup-file> missing.");
}
}
else if( strcmp(cmd, "export-keys")==0 )
else if (strcmp(cmd, "export-keys")==0)
{
dc_imex(context, DC_IMEX_EXPORT_SELF_KEYS, context->blobdir, NULL);
ret = COMMAND_SUCCEEDED;
}
else if( strcmp(cmd, "import-keys")==0 )
else if (strcmp(cmd, "import-keys")==0)
{
dc_imex(context, DC_IMEX_IMPORT_SELF_KEYS, context->blobdir, NULL);
ret = COMMAND_SUCCEEDED;
}
else if( strcmp(cmd, "export-setup")==0 )
else if (strcmp(cmd, "export-setup")==0)
{
char* setup_code = dc_create_setup_code(context);
char* file_name = dc_mprintf("%s/autocrypt-setup-message.html", context->blobdir);
char* file_content = NULL;
if( (file_content=dc_render_setup_file(context, setup_code)) != NULL
&& dc_write_file(file_name, file_content, strlen(file_content), context) ) {
if ((file_content=dc_render_setup_file(context, setup_code)) != NULL
&& dc_write_file(file_name, file_content, strlen(file_content), context)) {
ret = dc_mprintf("Setup message written to: %s\nSetup code: %s", file_name, setup_code);
}
else {
@ -610,15 +610,15 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
free(file_name);
free(setup_code);
}
else if( strcmp(cmd, "poke")==0 )
else if (strcmp(cmd, "poke")==0)
{
ret = poke_spec(context, arg1)? COMMAND_SUCCEEDED : COMMAND_FAILED;
}
else if( strcmp(cmd, "reset")==0 )
else if (strcmp(cmd, "reset")==0)
{
if( arg1 ) {
if (arg1) {
int bits = atoi(arg1);
if( bits > 15 ) {
if (bits > 15) {
ret = dc_strdup("ERROR: <bits> must be lower than 16.");
}
else {
@ -629,11 +629,11 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
ret = dc_strdup("ERROR: Argument <bits> missing: 1=jobs, 2=peerstates, 4=private keys, 8=rest but server config");
}
}
else if( strcmp(cmd, "set")==0 )
else if (strcmp(cmd, "set")==0)
{
if( arg1 ) {
if (arg1) {
char* arg2 = strchr(arg1, ' ');
if( arg2 ) {
if (arg2) {
*arg2 = 0;
arg2++;
}
@ -643,11 +643,11 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
ret = dc_strdup("ERROR: Argument <key> missing.");
}
}
else if( strcmp(cmd, "get")==0 )
else if (strcmp(cmd, "get")==0)
{
if( arg1 ) {
if (arg1) {
char* val = dc_get_config(context, arg1, "<unset>");
if( val ) {
if (val) {
ret = dc_mprintf("%s=%s", arg1, val);
free(val);
}
@ -659,10 +659,10 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
ret = dc_strdup("ERROR: Argument <key> missing.");
}
}
else if( strcmp(cmd, "info")==0 )
else if (strcmp(cmd, "info")==0)
{
ret = dc_get_info(context);
if( ret == NULL ) {
if (ret == NULL) {
ret = COMMAND_FAILED;
}
}
@ -671,15 +671,15 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
* Chat commands
******************************************************************************/
else if( strcmp(cmd, "listchats")==0 || strcmp(cmd, "listarchived")==0 || strcmp(cmd, "chats")==0 )
else if (strcmp(cmd, "listchats")==0 || strcmp(cmd, "listarchived")==0 || strcmp(cmd, "chats")==0)
{
int listflags = strcmp(cmd, "listarchived")==0? DC_GCL_ARCHIVED_ONLY : 0;
dc_chatlist_t* chatlist = dc_get_chatlist(context, listflags, arg1, 0);
if( chatlist ) {
if (chatlist) {
int i, cnt = dc_chatlist_get_cnt(chatlist);
if( cnt>0 ) {
if (cnt>0) {
dc_log_info(context, 0, "================================================================================");
for( i = cnt-1; i >= 0; i-- )
for (i = cnt-1; i >= 0; i--)
{
dc_chat_t* chat = dc_get_chat(context, dc_chatlist_get_chat_id(chatlist, i));
@ -694,10 +694,10 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
dc_lot_t* lot = dc_chatlist_get_summary(chatlist, i, chat);
const char* statestr = "";
if( dc_chat_get_archived(chat) ) {
if (dc_chat_get_archived(chat)) {
statestr = " [Archived]";
}
else switch( dc_lot_get_state(lot) ) {
else switch (dc_lot_get_state(lot)) {
case DC_STATE_OUT_PENDING: statestr = " o"; break;
case DC_STATE_OUT_DELIVERED: statestr = ""; break;
case DC_STATE_OUT_MDN_RCVD: statestr = " √√"; break;
@ -731,31 +731,31 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
ret = COMMAND_FAILED;
}
}
else if( strcmp(cmd, "chat")==0 )
else if (strcmp(cmd, "chat")==0)
{
if( arg1 && arg1[0] ) {
if (arg1 && arg1[0]) {
/* select a chat (argument 1 = ID of chat to select) */
if( sel_chat ) { dc_chat_unref(sel_chat); sel_chat = NULL; }
if (sel_chat) { dc_chat_unref(sel_chat); sel_chat = NULL; }
context->cmdline_sel_chat_id = atoi(arg1);
sel_chat = dc_get_chat(context, context->cmdline_sel_chat_id); /* may be NULL */
if( sel_chat==NULL ) {
if (sel_chat==NULL) {
context->cmdline_sel_chat_id = 0;
}
}
/* show chat */
if( sel_chat ) {
if (sel_chat) {
dc_array_t* msglist = dc_get_chat_msgs(context, dc_chat_get_id(sel_chat), DC_GCM_ADDDAYMARKER, 0);
char* temp2 = dc_chat_get_subtitle(sel_chat);
char* temp_name = dc_chat_get_name(sel_chat);
dc_log_info(context, 0, "%s#%i: %s [%s]", chat_prefix(sel_chat), dc_chat_get_id(sel_chat), temp_name, temp2);
free(temp_name);
free(temp2);
if( msglist ) {
if (msglist) {
log_msglist(context, msglist);
dc_array_unref(msglist);
}
if( dc_chat_get_draft_timestamp(sel_chat) ) {
if (dc_chat_get_draft_timestamp(sel_chat)) {
char* timestr = dc_timestamp_to_str(dc_chat_get_draft_timestamp(sel_chat));
char* drafttext = dc_chat_get_draft(sel_chat);
dc_log_info(context, 0, "Draft: %s [%s]", drafttext, timestr);
@ -769,9 +769,9 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
ret = dc_strdup("No chat selected.");
}
}
else if( strcmp(cmd, "createchat")==0 )
else if (strcmp(cmd, "createchat")==0)
{
if( arg1 ) {
if (arg1) {
int contact_id = atoi(arg1);
int chat_id = dc_create_chat_by_contact_id(context, contact_id);
ret = chat_id!=0? dc_mprintf("Single#%lu created successfully.", chat_id) : COMMAND_FAILED;
@ -780,12 +780,12 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
ret = dc_strdup("ERROR: Argument <contact-id> missing.");
}
}
else if( strcmp(cmd, "createchatbymsg")==0 )
else if (strcmp(cmd, "createchatbymsg")==0)
{
if( arg1 ) {
if (arg1) {
int msg_id = atoi(arg1);
int chat_id = dc_create_chat_by_msg_id(context, msg_id);
if( chat_id != 0 ) {
if (chat_id != 0) {
dc_chat_t* chat = dc_get_chat(context, chat_id);
ret = dc_mprintf("%s#%lu created successfully.", chat_prefix(chat), chat_id);
dc_chat_unref(chat);
@ -798,9 +798,9 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
ret = dc_strdup("ERROR: Argument <msg-id> missing.");
}
}
else if( strcmp(cmd, "creategroup")==0 )
else if (strcmp(cmd, "creategroup")==0)
{
if( arg1 ) {
if (arg1) {
int chat_id = dc_create_group_chat(context, 0, arg1);
ret = chat_id!=0? dc_mprintf("Group#%lu created successfully.", chat_id) : COMMAND_FAILED;
}
@ -808,9 +808,9 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
ret = dc_strdup("ERROR: Argument <name> missing.");
}
}
else if( strcmp(cmd, "createverified")==0 )
else if (strcmp(cmd, "createverified")==0)
{
if( arg1 ) {
if (arg1) {
int chat_id = dc_create_group_chat(context, 1, arg1);
ret = chat_id!=0? dc_mprintf("VerifiedGroup#%lu created successfully.", chat_id) : COMMAND_FAILED;
}
@ -818,12 +818,12 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
ret = dc_strdup("ERROR: Argument <name> missing.");
}
}
else if( strcmp(cmd, "addmember")==0 )
else if (strcmp(cmd, "addmember")==0)
{
if( sel_chat ) {
if( arg1 ) {
if (sel_chat) {
if (arg1) {
int contact_id = atoi(arg1);
if( dc_add_contact_to_chat(context, dc_chat_get_id(sel_chat), contact_id) ) {
if (dc_add_contact_to_chat(context, dc_chat_get_id(sel_chat), contact_id)) {
ret = dc_strdup("Contact added to chat.");
}
else {
@ -838,12 +838,12 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
ret = dc_strdup("No chat selected.");
}
}
else if( strcmp(cmd, "removemember")==0 )
else if (strcmp(cmd, "removemember")==0)
{
if( sel_chat ) {
if( arg1 ) {
if (sel_chat) {
if (arg1) {
int contact_id = atoi(arg1);
if( dc_remove_contact_from_chat(context, dc_chat_get_id(sel_chat), contact_id) ) {
if (dc_remove_contact_from_chat(context, dc_chat_get_id(sel_chat), contact_id)) {
ret = dc_strdup("Contact added to chat.");
}
else {
@ -858,10 +858,10 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
ret = dc_strdup("No chat selected.");
}
}
else if( strcmp(cmd, "groupname")==0 )
else if (strcmp(cmd, "groupname")==0)
{
if( sel_chat ) {
if( arg1 && arg1[0] ) {
if (sel_chat) {
if (arg1 && arg1[0]) {
ret = dc_set_chat_name(context, dc_chat_get_id(sel_chat), arg1)? COMMAND_SUCCEEDED : COMMAND_FAILED;
}
else {
@ -872,20 +872,20 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
ret = dc_strdup("No chat selected.");
}
}
else if( strcmp(cmd, "groupimage")==0 )
else if (strcmp(cmd, "groupimage")==0)
{
if( sel_chat ) {
if (sel_chat) {
ret = dc_set_chat_profile_image(context, dc_chat_get_id(sel_chat), (arg1&&arg1[0])?arg1:NULL)? COMMAND_SUCCEEDED : COMMAND_FAILED;
}
else {
ret = dc_strdup("No chat selected.");
}
}
else if( strcmp(cmd, "chatinfo")==0 )
else if (strcmp(cmd, "chatinfo")==0)
{
if( sel_chat ) {
if (sel_chat) {
dc_array_t* contacts = dc_get_chat_contacts(context, dc_chat_get_id(sel_chat));
if( contacts ) {
if (contacts) {
dc_log_info(context, 0, "Memberlist:");
log_contactlist(context, contacts);
ret = dc_mprintf("%i contacts.", (int)dc_array_get_cnt(contacts));
@ -898,11 +898,11 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
ret = dc_strdup("No chat selected.");
}
}
else if( strcmp(cmd, "send")==0 )
else if (strcmp(cmd, "send")==0)
{
if( sel_chat ) {
if( arg1 && arg1[0] ) {
if( dc_send_text_msg(context, dc_chat_get_id(sel_chat), arg1) ) {
if (sel_chat) {
if (arg1 && arg1[0]) {
if (dc_send_text_msg(context, dc_chat_get_id(sel_chat), arg1)) {
ret = dc_strdup("Message sent.");
}
else {
@ -917,11 +917,11 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
ret = dc_strdup("No chat selected.");
}
}
else if( strcmp(cmd, "sendimage")==0 )
else if (strcmp(cmd, "sendimage")==0)
{
if( sel_chat ) {
if( arg1 && arg1[0] ) {
if( dc_send_image_msg(context, dc_chat_get_id(sel_chat), arg1, NULL, 0, 0) ) {
if (sel_chat) {
if (arg1 && arg1[0]) {
if (dc_send_image_msg(context, dc_chat_get_id(sel_chat), arg1, NULL, 0, 0)) {
ret = dc_strdup("Image sent.");
}
else {
@ -936,11 +936,11 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
ret = dc_strdup("No chat selected.");
}
}
else if( strcmp(cmd, "sendfile")==0 )
else if (strcmp(cmd, "sendfile")==0)
{
if( sel_chat ) {
if( arg1 && arg1[0] ) {
if( dc_send_file_msg(context, dc_chat_get_id(sel_chat), arg1, NULL) ) {
if (sel_chat) {
if (arg1 && arg1[0]) {
if (dc_send_file_msg(context, dc_chat_get_id(sel_chat), arg1, NULL)) {
ret = dc_strdup("File sent.");
}
else {
@ -955,11 +955,11 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
ret = dc_strdup("No chat selected.");
}
}
else if( strcmp(cmd, "listmsgs")==0 )
else if (strcmp(cmd, "listmsgs")==0)
{
if( arg1 ) {
if (arg1) {
dc_array_t* msglist = dc_search_msgs(context, sel_chat? dc_chat_get_id(sel_chat) : 0, arg1);
if( msglist ) {
if (msglist) {
log_msglist(context, msglist);
ret = dc_mprintf("%i messages.", (int)dc_array_get_cnt(msglist));
dc_array_unref(msglist);
@ -969,10 +969,10 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
ret = dc_strdup("ERROR: Argument <query> missing.");
}
}
else if( strcmp(cmd, "draft")==0 )
else if (strcmp(cmd, "draft")==0)
{
if( sel_chat ) {
if( arg1 && arg1[0] ) {
if (sel_chat) {
if (arg1 && arg1[0]) {
dc_set_draft(context, dc_chat_get_id(sel_chat), arg1);
ret = dc_strdup("Draft saved.");
}
@ -985,13 +985,13 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
ret = dc_strdup("No chat selected.");
}
}
else if( strcmp(cmd, "listmedia")==0 )
else if (strcmp(cmd, "listmedia")==0)
{
if( sel_chat ) {
if (sel_chat) {
dc_array_t* images = dc_get_chat_media(context, dc_chat_get_id(sel_chat), DC_MSG_IMAGE, DC_MSG_VIDEO);
int i, icnt = dc_array_get_cnt(images);
ret = dc_mprintf("%i images or videos: ", icnt);
for( i = 0; i < icnt; i++ ) {
for (i = 0; i < icnt; i++) {
char* temp = dc_mprintf("%s%sMsg#%i", i? ", ":"", ret, (int)dc_array_get_id(images, i));
free(ret);
ret = temp;
@ -1002,9 +1002,9 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
ret = dc_strdup("No chat selected.");
}
}
else if( strcmp(cmd, "archive")==0 || strcmp(cmd, "unarchive")==0 )
else if (strcmp(cmd, "archive")==0 || strcmp(cmd, "unarchive")==0)
{
if( arg1 ) {
if (arg1) {
int chat_id = atoi(arg1);
dc_archive_chat(context, chat_id, strcmp(cmd, "archive")==0? 1 : 0);
ret = COMMAND_SUCCEEDED;
@ -1013,9 +1013,9 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
ret = dc_strdup("ERROR: Argument <chat-id> missing.");
}
}
else if( strcmp(cmd, "delchat")==0 )
else if (strcmp(cmd, "delchat")==0)
{
if( arg1 ) {
if (arg1) {
int chat_id = atoi(arg1);
dc_delete_chat(context, chat_id);
ret = COMMAND_SUCCEEDED;
@ -1030,9 +1030,9 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
* Message commands
******************************************************************************/
else if( strcmp(cmd, "msginfo")==0 )
else if (strcmp(cmd, "msginfo")==0)
{
if( arg1 ) {
if (arg1) {
int id = atoi(arg1);
ret = dc_get_msg_info(context, id);
}
@ -1040,20 +1040,20 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
ret = dc_strdup("ERROR: Argument <msg-id> missing.");
}
}
else if( strcmp(cmd, "listfresh")==0 )
else if (strcmp(cmd, "listfresh")==0)
{
dc_array_t* msglist = dc_get_fresh_msgs(context);
if( msglist ) {
if (msglist) {
log_msglist(context, msglist);
ret = dc_mprintf("%i fresh messages.", (int)dc_array_get_cnt(msglist));
dc_array_unref(msglist);
}
}
else if( strcmp(cmd, "forward")==0 )
else if (strcmp(cmd, "forward")==0)
{
char* arg2 = NULL;
if( arg1 ) { arg2 = strrchr(arg1, ' '); }
if( arg1 && arg2 ) {
if (arg1) { arg2 = strrchr(arg1, ' '); }
if (arg1 && arg2) {
*arg2 = 0; arg2++;
uint32_t msg_ids[1], chat_id = atoi(arg2);
msg_ids[0] = atoi(arg1);
@ -1064,9 +1064,9 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
ret = dc_strdup("ERROR: Arguments <msg-id> <chat-id> expected.");
}
}
else if( strcmp(cmd, "markseen")==0 )
else if (strcmp(cmd, "markseen")==0)
{
if( arg1 ) {
if (arg1) {
uint32_t msg_ids[1];
msg_ids[0] = atoi(arg1);
dc_markseen_msgs(context, msg_ids, 1);
@ -1076,9 +1076,9 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
ret = dc_strdup("ERROR: Argument <msg-id> missing.");
}
}
else if( strcmp(cmd, "star")==0 || strcmp(cmd, "unstar")==0 )
else if (strcmp(cmd, "star")==0 || strcmp(cmd, "unstar")==0)
{
if( arg1 ) {
if (arg1) {
uint32_t msg_ids[1];
msg_ids[0] = atoi(arg1);
dc_star_msgs(context, msg_ids, 1, strcmp(cmd, "star")==0? 1 : 0);
@ -1088,9 +1088,9 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
ret = dc_strdup("ERROR: Argument <msg-id> missing.");
}
}
else if( strcmp(cmd, "delmsg")==0 )
else if (strcmp(cmd, "delmsg")==0)
{
if( arg1 ) {
if (arg1) {
uint32_t ids[1];
ids[0] = atoi(arg1);
dc_delete_msgs(context, ids, 1);
@ -1106,10 +1106,10 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
* Contact commands
******************************************************************************/
else if( strcmp(cmd, "listcontacts")==0 || strcmp(cmd, "contacts")==0 || strcmp(cmd, "listverified")==0 )
else if (strcmp(cmd, "listcontacts")==0 || strcmp(cmd, "contacts")==0 || strcmp(cmd, "listverified")==0)
{
dc_array_t* contacts = dc_get_contacts(context, strcmp(cmd, "listverified")==0? DC_GCL_VERIFIED_ONLY : 0, arg1);
if( contacts ) {
if (contacts) {
log_contactlist(context, contacts);
ret = dc_mprintf("%i contacts.", (int)dc_array_get_cnt(contacts));
dc_array_unref(contacts);
@ -1118,27 +1118,27 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
ret = COMMAND_FAILED;
}
}
else if( strcmp(cmd, "addcontact")==0 )
else if (strcmp(cmd, "addcontact")==0)
{
char* arg2 = NULL;
if( arg1 ) { arg2 = strrchr(arg1, ' '); }
if( arg1 && arg2 ) {
if (arg1) { arg2 = strrchr(arg1, ' '); }
if (arg1 && arg2) {
*arg2 = 0; arg2++;
char* book = dc_mprintf("%s\n%s", arg1, arg2);
dc_add_address_book(context, book);
ret = COMMAND_SUCCEEDED;
free(book);
}
else if( arg1 ) {
else if (arg1) {
ret = dc_create_contact(context, NULL, arg1)? COMMAND_SUCCEEDED : COMMAND_FAILED;
}
else {
ret = dc_strdup("ERROR: Arguments [<name>] <addr> expected.");
}
}
else if( strcmp(cmd, "contactinfo")==0 )
else if (strcmp(cmd, "contactinfo")==0)
{
if( arg1 ) {
if (arg1) {
int contact_id = atoi(arg1);
dc_strbuilder_t strbuilder;
dc_strbuilder_init(&strbuilder, 0);
@ -1155,10 +1155,10 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
dc_chatlist_t* chatlist = dc_get_chatlist(context, 0, NULL, contact_id);
int chatlist_cnt = dc_chatlist_get_cnt(chatlist);
if( chatlist_cnt > 0 ) {
if (chatlist_cnt > 0) {
dc_strbuilder_catf(&strbuilder, "\n\n%i chats shared with Contact#%i: ", chatlist_cnt, contact_id);
for( int i = 0; i < chatlist_cnt; i++ ) {
if( i ) { dc_strbuilder_cat(&strbuilder, ", "); }
for (int i = 0; i < chatlist_cnt; i++) {
if (i) { dc_strbuilder_cat(&strbuilder, ", "); }
dc_chat_t* chat = dc_get_chat(context, dc_chatlist_get_chat_id(chatlist, i));
dc_strbuilder_catf(&strbuilder, "%s#%i", chat_prefix(chat), dc_chat_get_id(chat));
@ -1173,16 +1173,16 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
ret = dc_strdup("ERROR: Argument <contact-id> missing.");
}
}
else if( strcmp(cmd, "delcontact")==0 )
else if (strcmp(cmd, "delcontact")==0)
{
if( arg1 ) {
if (arg1) {
ret = dc_delete_contact(context, atoi(arg1))? COMMAND_SUCCEEDED : COMMAND_FAILED;
}
else {
ret = dc_strdup("ERROR: Argument <contact-id> missing.");
}
}
else if( strcmp(cmd, "cleanupcontacts")==0 )
else if (strcmp(cmd, "cleanupcontacts")==0)
{
ret = dc_cleanup_contacts(context)? COMMAND_SUCCEEDED : COMMAND_FAILED;
}
@ -1191,14 +1191,14 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
* Misc.
******************************************************************************/
else if( strcmp(cmd, "getqr")==0 )
else if (strcmp(cmd, "getqr")==0)
{
ret = dc_get_securejoin_qr(context, arg1? atoi(arg1) : 0);
if( ret == NULL || ret[0]==0 ) { free(ret); ret = COMMAND_FAILED; }
if (ret == NULL || ret[0]==0) { free(ret); ret = COMMAND_FAILED; }
}
else if( strcmp(cmd, "checkqr")==0 )
else if (strcmp(cmd, "checkqr")==0)
{
if( arg1 ) {
if (arg1) {
dc_lot_t* res = dc_check_qr(context, arg1);
ret = dc_mprintf("state=%i, id=%i, text1=%s, text2=%s", (int)res->state, res->id, res->text1? res->text1:"", res->text2? res->text2:"");
dc_lot_unref(res);
@ -1207,9 +1207,9 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
ret = dc_strdup("ERROR: Argument <qr-content> missing.");
}
}
else if ( strcmp(cmd, "event")==0 )
else if ( strcmp(cmd, "event")==0)
{
if( arg1 ) {
if (arg1) {
int event = atoi(arg1);
uintptr_t r = context->cb(context, event, 0, 0);
ret = dc_mprintf("Sending event %i, received value %i.", (int)event, (int)r);
@ -1218,11 +1218,11 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
ret = dc_strdup("ERROR: Argument <id> missing.");
}
}
else if( strcmp(cmd, "fileinfo")==0 )
else if (strcmp(cmd, "fileinfo")==0)
{
if( arg1 ) {
if (arg1) {
unsigned char* buf = NULL; size_t buf_bytes; uint32_t w, h;
if( dc_read_file(arg1, (void**)&buf, &buf_bytes, context) ) {
if (dc_read_file(arg1, (void**)&buf, &buf_bytes, context)) {
dc_get_filemeta(buf, buf_bytes, &w, &h);
ret = dc_mprintf("width=%i, height=%i", (int)w, (int)h);
}
@ -1241,16 +1241,16 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
}
cleanup:
if( ret == COMMAND_SUCCEEDED ) {
if (ret == COMMAND_SUCCEEDED) {
ret = dc_strdup("Command executed successfully.");
}
else if( ret == COMMAND_FAILED ) {
else if (ret == COMMAND_FAILED) {
ret = dc_strdup("ERROR: Command failed.");
}
else if( ret == COMMAND_UNKNOWN ) {
else if (ret == COMMAND_UNKNOWN) {
ret = dc_mprintf("ERROR: Unknown command \"%s\", type ? for help.", cmd);
}
if( sel_chat ) { dc_chat_unref(sel_chat); sel_chat = NULL; }
if (sel_chat) { dc_chat_unref(sel_chat); sel_chat = NULL; }
free(cmd);
return ret;
}

View file

@ -49,14 +49,14 @@ static int s_do_log_info = 1;
static uintptr_t receive_event(dc_context_t* context, int event, uintptr_t data1, uintptr_t data2)
{
switch( event )
switch (event)
{
case DC_EVENT_GET_STRING:
case DC_EVENT_GET_QUANTITY_STRING:
break; /* do not show the event as this would fill the screen */
case DC_EVENT_INFO:
if( s_do_log_info ) {
if (s_do_log_info) {
printf("%s\n", (char*)data2);
}
break;
@ -75,7 +75,7 @@ static uintptr_t receive_event(dc_context_t* context, int event, uintptr_t data1
char* tempFile = dc_get_fine_pathNfilename(context->blobdir, "curl.result");
char* cmd = dc_mprintf("curl --silent --location --fail --insecure %s > %s", (char*)data1, tempFile); /* --location = follow redirects */
int error = system(cmd);
if( error == 0 ) { /* -1=system() error, !0=curl errors forced by -f, 0=curl success */
if (error == 0) { /* -1=system() error, !0=curl errors forced by -f, 0=curl success */
size_t bytes = 0;
dc_read_file(tempFile, (void**)&ret, &bytes, context);
}
@ -131,12 +131,12 @@ static void* imap_thread_entry_point (void* entry_arg)
{
dc_context_t* context = (dc_context_t*)entry_arg;
while( 1 ) {
while (1) {
// perform_jobs(), fetch() and idle()
// MUST be called from the same single thread and MUST be called sequentially.
dc_perform_imap_jobs(context);
dc_perform_imap_fetch(context);
if( imap_foreground ) {
if (imap_foreground) {
dc_perform_imap_idle(context); // this may take hours ...
}
else {
@ -154,9 +154,9 @@ static void* smtp_thread_entry_point (void* entry_arg)
{
dc_context_t* context = (dc_context_t*)entry_arg;
while( 1 ) {
while (1) {
dc_perform_smtp_jobs(context);
if( imap_foreground ) {
if (imap_foreground) {
dc_perform_smtp_idle(context); // this may take hours ...
}
else {
@ -171,11 +171,11 @@ static void* smtp_thread_entry_point (void* entry_arg)
static void start_threads(dc_context_t* context)
{
if( !imap_thread ) {
if (!imap_thread) {
pthread_create(&imap_thread, NULL, imap_thread_entry_point, context);
}
if( !smtp_thread ) {
if (!smtp_thread) {
pthread_create(&smtp_thread, NULL, smtp_thread_entry_point, context);
}
}
@ -188,7 +188,7 @@ static void stop_threads(dc_context_t* context)
dc_interrupt_smtp_idle(context);
// wait until the threads are finished
while( imap_thread || smtp_thread ) {
while (imap_thread || smtp_thread) {
usleep(100*1000);
}
}
@ -205,8 +205,8 @@ static char* read_cmd(void)
static char cmdbuffer[1024];
fgets(cmdbuffer, 1000, stdin);
while( strlen(cmdbuffer)>0
&& (cmdbuffer[strlen(cmdbuffer)-1]=='\n' || cmdbuffer[strlen(cmdbuffer)-1]==' ') )
while (strlen(cmdbuffer)>0
&& (cmdbuffer[strlen(cmdbuffer)-1]=='\n' || cmdbuffer[strlen(cmdbuffer)-1]==' '))
{
cmdbuffer[strlen(cmdbuffer)-1] = '\0';
}
@ -223,12 +223,12 @@ int main(int argc, char ** argv)
dc_cmdline_skip_auth(context); /* disable the need to enter the command `auth <password>` for all mailboxes. */
/* open database from the commandline (if omitted, it can be opened using the `open`-command) */
if( argc == 2 ) {
if( !dc_open(context, argv[1], NULL) ) {
if (argc == 2) {
if (!dc_open(context, argv[1], NULL)) {
printf("ERROR: Cannot open %s.\n", argv[1]);
}
}
else if( argc != 1 ) {
else if (argc != 1) {
printf("ERROR: Bad arguments\n");
}
@ -239,47 +239,47 @@ int main(int argc, char ** argv)
printf("Delta Chat Core is awaiting your commands.\n");
/* wait for command */
while(1)
while (1)
{
/* read command */
const char* cmdline = read_cmd();
free(cmd);
cmd = dc_strdup(cmdline);
char* arg1 = strchr(cmd, ' ');
if( arg1 ) { *arg1 = 0; arg1++; }
if (arg1) { *arg1 = 0; arg1++; }
if( strcmp(cmd, "connect")==0 )
if (strcmp(cmd, "connect")==0)
{
imap_foreground = 1;
start_threads(context);
}
else if( strcmp(cmd, "disconnect")==0 )
else if (strcmp(cmd, "disconnect")==0)
{
stop_threads(context);
}
else if( strcmp(cmd, "poll")==0 )
else if (strcmp(cmd, "poll")==0)
{
imap_foreground = 1;
}
else if( strcmp(cmd, "configure")==0 )
else if (strcmp(cmd, "configure")==0)
{
imap_foreground = 1;
start_threads(context);
dc_configure(context);
}
else if( strcmp(cmd, "clear")==0 )
else if (strcmp(cmd, "clear")==0)
{
printf("\n\n\n\n"); /* insert some blank lines to visualize the break in the buffer */
printf("\e[1;1H\e[2J"); /* should work on ANSI terminals and on Windows 10. If not, well, then not. */
}
else if( strcmp(cmd, "getqr")==0 || strcmp(cmd, "getbadqr")==0 )
else if (strcmp(cmd, "getqr")==0 || strcmp(cmd, "getbadqr")==0)
{
imap_foreground = 1;
start_threads(context);
char* qrstr = dc_get_securejoin_qr(context, arg1? atoi(arg1) : 0);
if( qrstr && qrstr[0] ) {
if( strcmp(cmd, "getbadqr")==0 && strlen(qrstr)>40 ) {
for( int i = 12; i < 22; i++ ) { qrstr[i] = '0'; }
if (qrstr && qrstr[0]) {
if (strcmp(cmd, "getbadqr")==0 && strlen(qrstr)>40) {
for (int i = 12; i < 22; i++) { qrstr[i] = '0'; }
}
printf("%s\n", qrstr);
char* syscmd = dc_mprintf("qrencode -t ansiutf8 \"%s\" -o -", qrstr); /* `-t ansiutf8`=use back/write, `-t utf8`=use terminal colors */
@ -288,26 +288,26 @@ int main(int argc, char ** argv)
}
free(qrstr);
}
else if( strcmp(cmd, "joinqr")==0 )
else if (strcmp(cmd, "joinqr")==0)
{
imap_foreground = 1;
start_threads(context);
if( arg1 ) {
if (arg1) {
dc_join_securejoin(context, arg1);
}
}
else if( strcmp(cmd, "exit")==0 )
else if (strcmp(cmd, "exit")==0)
{
break;
}
else if( cmd[0] == 0 )
else if (cmd[0] == 0)
{
; /* nothing typed */
}
else
{
char* execute_result = dc_cmdline(context, cmdline);
if( execute_result ) {
if (execute_result) {
printf("%s\n", execute_result);
free(execute_result);
}

View file

@ -506,12 +506,12 @@ void stress_functions(dc_context_t* context)
assert( dc_array_get_cnt(arr) == 0 );
int i;
for( i = 0; i < TEST_CNT; i++ ) {
for (i = 0; i < TEST_CNT; i++) {
dc_array_add_id(arr, i+1*2);
}
assert( dc_array_get_cnt(arr) == TEST_CNT );
for( i = 0; i< TEST_CNT; i++ ) {
for (i = 0; i< TEST_CNT; i++) {
assert( dc_array_get_id(arr, i) == i+1*2 );
}
assert( dc_array_get_id(arr, -1) == 0 ); /* test out-of-range access */
@ -720,7 +720,7 @@ void stress_functions(dc_context_t* context)
free(buf);
}
if( dc_is_configured(context) )
if (dc_is_configured(context) )
{
char *setupcode = NULL, *setupfile = NULL;
@ -902,7 +902,7 @@ void stress_functions(dc_context_t* context)
assert( strcmp(fingerprint, "1234567890ABCDABCDEFABCDEF") == 0 );
}
if( dc_is_configured(context) )
if (dc_is_configured(context))
{
char* qr = dc_get_securejoin_qr(context, 0);
assert( strlen(qr)>55 && strncmp(qr, "OPENPGP4FPR:", 12)==0 && strncmp(&qr[52], "#a=", 3)==0 );

View file

@ -38,7 +38,7 @@
*/
void dc_aheader_empty(dc_aheader_t* aheader)
{
if( aheader == NULL ) {
if (aheader == NULL) {
return;
}
@ -47,7 +47,7 @@ void dc_aheader_empty(dc_aheader_t* aheader)
free(aheader->addr);
aheader->addr = NULL;
if( aheader->public_key->binary ) {
if (aheader->public_key->binary) {
dc_key_unref(aheader->public_key);
aheader->public_key = dc_key_new();
}
@ -69,7 +69,7 @@ char* dc_aheader_render(const dc_aheader_t* aheader)
dc_strbuilder_t ret;
dc_strbuilder_init(&ret, 0);
if( aheader==NULL || aheader->addr==NULL || aheader->public_key->binary==NULL || aheader->public_key->type!=DC_KEY_PUBLIC ) {
if (aheader==NULL || aheader->addr==NULL || aheader->public_key->binary==NULL || aheader->public_key->type!=DC_KEY_PUBLIC) {
goto cleanup;
}
@ -77,7 +77,7 @@ char* dc_aheader_render(const dc_aheader_t* aheader)
dc_strbuilder_cat(&ret, aheader->addr);
dc_strbuilder_cat(&ret, "; ");
if( aheader->prefer_encrypt==DC_PE_MUTUAL ) {
if (aheader->prefer_encrypt==DC_PE_MUTUAL) {
dc_strbuilder_cat(&ret, "prefer-encrypt=mutual; ");
}
@ -85,7 +85,7 @@ char* dc_aheader_render(const dc_aheader_t* aheader)
/* adds a whitespace every 78 characters, this allows libEtPan to wrap the lines according to RFC 5322
(which may insert a linebreak before every whitespace) */
if( (keybase64_wrapped = dc_key_render_base64(aheader->public_key, 78, " ", 0/*no checksum*/)) == NULL ) {
if ((keybase64_wrapped = dc_key_render_base64(aheader->public_key, 78, " ", 0/*no checksum*/)) == NULL) {
goto cleanup;
}
@ -94,7 +94,7 @@ char* dc_aheader_render(const dc_aheader_t* aheader)
success = 1;
cleanup:
if( !success ) { free(ret.buf); ret.buf = NULL; }
if (!success) { free(ret.buf); ret.buf = NULL; }
free(keybase64_wrapped);
return ret.buf; /* NULL on errors, this may happen for various reasons */
}
@ -108,45 +108,45 @@ cleanup:
static int add_attribute(dc_aheader_t* aheader, const char* name, const char* value /*may be NULL*/)
{
/* returns 0 if the attribute will result in an invalid header, 1 if the attribute is okay */
if( strcasecmp(name, "addr")==0 )
if (strcasecmp(name, "addr")==0)
{
if( value == NULL
if (value == NULL
|| strlen(value) < 3 || strchr(value, '@')==NULL || strchr(value, '.')==NULL /* rough check if email-address is valid */
|| aheader->addr /* email already given */ ) {
|| aheader->addr /* email already given */) {
return 0;
}
aheader->addr = dc_normalize_addr(value);
return 1;
}
#if 0 /* autocrypt 11/2017 no longer uses the type attribute and it will make the autocrypt header invalid */
else if( strcasecmp(name, "type")==0 )
else if (strcasecmp(name, "type")==0)
{
if( value == NULL ) {
if (value == NULL) {
return 0; /* attribute with no value results in an invalid header */
}
if( strcasecmp(value, "1")==0 || strcasecmp(value, "0" /*deprecated*/)==0 || strcasecmp(value, "p" /*deprecated*/)==0 ) {
if (strcasecmp(value, "1")==0 || strcasecmp(value, "0" /*deprecated*/)==0 || strcasecmp(value, "p" /*deprecated*/)==0) {
return 1; /* PGP-type */
}
return 0; /* unknown types result in an invalid header */
}
#endif
else if( strcasecmp(name, "prefer-encrypt")==0 )
else if (strcasecmp(name, "prefer-encrypt")==0)
{
if( value && strcasecmp(value, "mutual")==0 ) {
if (value && strcasecmp(value, "mutual")==0) {
aheader->prefer_encrypt = DC_PE_MUTUAL;
return 1;
}
return 1; /* An Autocrypt level 0 client that sees the attribute with any other value (or that does not see the attribute at all) should interpret the value as nopreference.*/
}
else if( strcasecmp(name, "keydata")==0 )
else if (strcasecmp(name, "keydata")==0)
{
if( value == NULL
|| aheader->public_key->binary || aheader->public_key->bytes ) {
if (value == NULL
|| aheader->public_key->binary || aheader->public_key->bytes) {
return 0; /* there is already a k*/
}
return dc_key_set_from_base64(aheader->public_key, value, DC_KEY_PUBLIC);
}
else if( name[0]=='_' )
else if (name[0]=='_')
{
/* Autocrypt-Level0: unknown attributes starting with an underscore can be safely ignored */
return 1;
@ -174,7 +174,7 @@ int dc_aheader_set_from_string(dc_aheader_t* aheader, const char* header_str__)
dc_aheader_empty(aheader);
if( aheader == NULL || header_str__ == NULL ) {
if (aheader == NULL || header_str__ == NULL) {
goto cleanup;
}
@ -182,25 +182,25 @@ int dc_aheader_set_from_string(dc_aheader_t* aheader, const char* header_str__)
header_str = dc_strdup(header_str__);
p = header_str;
while( *p )
while (*p)
{
p += strspn(p, AHEADER_WS "=;"); /* forward to first attribute name beginning */
beg_attr_name = p;
beg_attr_value = NULL;
p += strcspn(p, AHEADER_WS "=;"); /* get end of attribute name (an attribute may have no value) */
if( p != beg_attr_name )
if (p != beg_attr_name)
{
/* attribute found */
after_attr_name = p;
p += strspn(p, AHEADER_WS); /* skip whitespace between attribute name and possible `=` */
if( *p == '=' )
if (*p == '=')
{
p += strspn(p, AHEADER_WS "="); /* skip spaces and equal signs */
/* read unquoted attribute value until the first semicolon */
beg_attr_value = p;
p += strcspn(p, ";");
if( *p != '\0' ) {
if (*p != '\0') {
*p = '\0';
p++;
}
@ -211,20 +211,20 @@ int dc_aheader_set_from_string(dc_aheader_t* aheader, const char* header_str__)
p += strspn(p, AHEADER_WS ";");
}
*after_attr_name = '\0';
if( !add_attribute(aheader, beg_attr_name, beg_attr_value) ) {
if (!add_attribute(aheader, beg_attr_name, beg_attr_value)) {
goto cleanup; /* a bad attribute makes the whole header invalid */
}
}
}
/* all needed data found? */
if( aheader->addr && aheader->public_key->binary ) {
if (aheader->addr && aheader->public_key->binary) {
success = 1;
}
cleanup:
free(header_str);
if( !success ) { dc_aheader_empty(aheader); }
if (!success) { dc_aheader_empty(aheader); }
return success;
}
@ -241,7 +241,7 @@ dc_aheader_t* dc_aheader_new()
{
dc_aheader_t* aheader = NULL;
if( (aheader=calloc(1, sizeof(dc_aheader_t)))==NULL ) {
if ((aheader=calloc(1, sizeof(dc_aheader_t)))==NULL) {
exit(37); /* cannot allocate little memory, unrecoverable error */
}
@ -256,7 +256,7 @@ dc_aheader_t* dc_aheader_new()
*/
void dc_aheader_unref(dc_aheader_t* aheader)
{
if( aheader==NULL ) {
if (aheader==NULL) {
return;
}
@ -274,30 +274,30 @@ dc_aheader_t* dc_aheader_new_from_imffields(const char* wanted_from, const struc
clistiter* cur;
dc_aheader_t* fine_header = NULL;
if( wanted_from == NULL || header == NULL ) {
if (wanted_from == NULL || header == NULL) {
return 0;
}
for( cur = clist_begin(header->fld_list); cur!=NULL ; cur=clist_next(cur) )
for (cur = clist_begin(header->fld_list); cur!=NULL ; cur=clist_next(cur))
{
struct mailimf_field* field = (struct mailimf_field*)clist_content(cur);
if( field && field->fld_type == MAILIMF_FIELD_OPTIONAL_FIELD )
if (field && field->fld_type == MAILIMF_FIELD_OPTIONAL_FIELD)
{
struct mailimf_optional_field* optional_field = field->fld_data.fld_optional_field;
if( optional_field && optional_field->fld_name && strcasecmp(optional_field->fld_name, "Autocrypt")==0 )
if (optional_field && optional_field->fld_name && strcasecmp(optional_field->fld_name, "Autocrypt")==0)
{
/* header found, check if it is valid and matched the wanted address */
dc_aheader_t* test = dc_aheader_new();
if( !dc_aheader_set_from_string(test, optional_field->fld_value)
|| strcasecmp(test->addr, wanted_from)!=0 ) {
if (!dc_aheader_set_from_string(test, optional_field->fld_value)
|| strcasecmp(test->addr, wanted_from)!=0) {
dc_aheader_unref(test);
test = NULL;
}
if( fine_header == NULL ) {
if (fine_header == NULL) {
fine_header = test; /* may still be NULL */
}
else if( test ) {
else if (test) {
dc_aheader_unref(fine_header);
dc_aheader_unref(test);
return NULL; /* more than one valid header for the same address results in an error, see Autocrypt Level 1 */

View file

@ -33,7 +33,7 @@
static void dc_apeerstate_empty(dc_apeerstate_t* peerstate)
{
if( peerstate == NULL ) {
if (peerstate == NULL) {
return;
}
@ -54,19 +54,19 @@ static void dc_apeerstate_empty(dc_apeerstate_t* peerstate)
free(peerstate->verified_key_fingerprint);
peerstate->verified_key_fingerprint = NULL;
if( peerstate->public_key ) {
if (peerstate->public_key) {
dc_key_unref(peerstate->public_key);
peerstate->public_key = NULL;
}
peerstate->gossip_timestamp = 0;
if( peerstate->gossip_key ) {
if (peerstate->gossip_key) {
dc_key_unref(peerstate->gossip_key);
peerstate->gossip_key = NULL;
}
if( peerstate->verified_key ) {
if (peerstate->verified_key) {
dc_key_unref(peerstate->verified_key);
peerstate->verified_key = NULL;
}
@ -90,17 +90,17 @@ static void dc_apeerstate_set_from_stmt(dc_apeerstate_t* peerstate, sqlite3_stmt
#define VERIFIED_KEY_COL 9
peerstate->verified_key_fingerprint = dc_strdup((char*)sqlite3_column_text(stmt, 10));
if( sqlite3_column_type(stmt, PUBLIC_KEY_COL)!=SQLITE_NULL ) {
if (sqlite3_column_type(stmt, PUBLIC_KEY_COL)!=SQLITE_NULL) {
peerstate->public_key = dc_key_new();
dc_key_set_from_stmt(peerstate->public_key, stmt, PUBLIC_KEY_COL, DC_KEY_PUBLIC);
}
if( sqlite3_column_type(stmt, GOSSIP_KEY_COL)!=SQLITE_NULL ) {
if (sqlite3_column_type(stmt, GOSSIP_KEY_COL)!=SQLITE_NULL) {
peerstate->gossip_key = dc_key_new();
dc_key_set_from_stmt(peerstate->gossip_key, stmt, GOSSIP_KEY_COL, DC_KEY_PUBLIC);
}
if( sqlite3_column_type(stmt, VERIFIED_KEY_COL)!=SQLITE_NULL ) {
if (sqlite3_column_type(stmt, VERIFIED_KEY_COL)!=SQLITE_NULL) {
peerstate->verified_key = dc_key_new();
dc_key_set_from_stmt(peerstate->verified_key, stmt, VERIFIED_KEY_COL, DC_KEY_PUBLIC);
}
@ -112,7 +112,7 @@ int dc_apeerstate_load_by_addr(dc_apeerstate_t* peerstate, dc_sqlite3_t* sql, co
int success = 0;
sqlite3_stmt* stmt = NULL;
if( peerstate==NULL || sql == NULL || addr == NULL ) {
if (peerstate==NULL || sql == NULL || addr == NULL) {
goto cleanup;
}
@ -123,7 +123,7 @@ int dc_apeerstate_load_by_addr(dc_apeerstate_t* peerstate, dc_sqlite3_t* sql, co
" FROM acpeerstates "
" WHERE addr=? COLLATE NOCASE;");
sqlite3_bind_text(stmt, 1, addr, -1, SQLITE_STATIC);
if( sqlite3_step(stmt) != SQLITE_ROW ) {
if (sqlite3_step(stmt) != SQLITE_ROW) {
goto cleanup;
}
dc_apeerstate_set_from_stmt(peerstate, stmt);
@ -141,7 +141,7 @@ int dc_apeerstate_load_by_fingerprint(dc_apeerstate_t* peerstate, dc_sqlite3_t*
int success = 0;
sqlite3_stmt* stmt = NULL;
if( peerstate==NULL || sql == NULL || fingerprint == NULL ) {
if (peerstate==NULL || sql == NULL || fingerprint == NULL) {
goto cleanup;
}
@ -156,7 +156,7 @@ int dc_apeerstate_load_by_fingerprint(dc_apeerstate_t* peerstate, dc_sqlite3_t*
sqlite3_bind_text(stmt, 1, fingerprint, -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 2, fingerprint, -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 3, fingerprint, -1, SQLITE_STATIC);
if( sqlite3_step(stmt) != SQLITE_ROW ) {
if (sqlite3_step(stmt) != SQLITE_ROW) {
goto cleanup;
}
dc_apeerstate_set_from_stmt(peerstate, stmt);
@ -174,11 +174,11 @@ int dc_apeerstate_save_to_db(const dc_apeerstate_t* peerstate, dc_sqlite3_t* sql
int success = 0;
sqlite3_stmt* stmt = NULL;
if( peerstate==NULL || sql==NULL || peerstate->addr==NULL ) {
if (peerstate==NULL || sql==NULL || peerstate->addr==NULL) {
return 0;
}
if( create ) {
if (create) {
stmt = dc_sqlite3_prepare(sql, "INSERT INTO acpeerstates (addr) VALUES(?);");
sqlite3_bind_text(stmt, 1, peerstate->addr, -1, SQLITE_STATIC);
sqlite3_step(stmt);
@ -186,7 +186,7 @@ int dc_apeerstate_save_to_db(const dc_apeerstate_t* peerstate, dc_sqlite3_t* sql
stmt = NULL;
}
if( (peerstate->to_save&DC_SAVE_ALL) || create )
if ((peerstate->to_save&DC_SAVE_ALL) || create)
{
stmt = dc_sqlite3_prepare(sql,
"UPDATE acpeerstates "
@ -204,13 +204,13 @@ int dc_apeerstate_save_to_db(const dc_apeerstate_t* peerstate, dc_sqlite3_t* sql
sqlite3_bind_blob (stmt, 9, peerstate->verified_key? peerstate->verified_key->binary : NULL/*results in sqlite3_bind_null()*/, peerstate->verified_key? peerstate->verified_key->bytes : 0, SQLITE_STATIC);
sqlite3_bind_text (stmt,10, peerstate->verified_key_fingerprint, -1, SQLITE_STATIC);
sqlite3_bind_text (stmt,11, peerstate->addr, -1, SQLITE_STATIC);
if( sqlite3_step(stmt) != SQLITE_DONE ) {
if (sqlite3_step(stmt) != SQLITE_DONE) {
goto cleanup;
}
sqlite3_finalize(stmt);
stmt = NULL;
}
else if( peerstate->to_save&DC_SAVE_TIMESTAMPS )
else if (peerstate->to_save&DC_SAVE_TIMESTAMPS)
{
stmt = dc_sqlite3_prepare(sql,
"UPDATE acpeerstates SET last_seen=?, last_seen_autocrypt=?, gossip_timestamp=? WHERE addr=?;");
@ -218,7 +218,7 @@ int dc_apeerstate_save_to_db(const dc_apeerstate_t* peerstate, dc_sqlite3_t* sql
sqlite3_bind_int64(stmt, 2, peerstate->last_seen_autocrypt);
sqlite3_bind_int64(stmt, 3, peerstate->gossip_timestamp);
sqlite3_bind_text (stmt, 4, peerstate->addr, -1, SQLITE_STATIC);
if( sqlite3_step(stmt) != SQLITE_DONE ) {
if (sqlite3_step(stmt) != SQLITE_DONE) {
goto cleanup;
}
sqlite3_finalize(stmt);
@ -242,7 +242,7 @@ dc_apeerstate_t* dc_apeerstate_new(dc_context_t* context)
{
dc_apeerstate_t* peerstate = NULL;
if( (peerstate=calloc(1, sizeof(dc_apeerstate_t)))==NULL ) {
if ((peerstate=calloc(1, sizeof(dc_apeerstate_t)))==NULL) {
exit(43); /* cannot allocate little memory, unrecoverable error */
}
@ -254,7 +254,7 @@ dc_apeerstate_t* dc_apeerstate_new(dc_context_t* context)
void dc_apeerstate_unref(dc_apeerstate_t* peerstate)
{
if( peerstate==NULL ) {
if (peerstate==NULL) {
return;
}
@ -282,7 +282,7 @@ char* dc_apeerstate_render_gossip_header(const dc_apeerstate_t* peerstate, int m
char* ret = NULL;
dc_aheader_t* autocryptheader = dc_aheader_new();
if( peerstate == NULL || peerstate->addr == NULL ) {
if (peerstate == NULL || peerstate->addr == NULL) {
goto cleanup;
}
@ -318,19 +318,19 @@ cleanup:
*/
dc_key_t* dc_apeerstate_peek_key(const dc_apeerstate_t* peerstate, int min_verified)
{
if( peerstate == NULL
if ( peerstate == NULL
|| (peerstate->public_key && (peerstate->public_key->binary==NULL || peerstate->public_key->bytes<=0))
|| (peerstate->gossip_key && (peerstate->gossip_key->binary==NULL || peerstate->gossip_key->bytes<=0))
|| (peerstate->verified_key && (peerstate->verified_key->binary==NULL || peerstate->verified_key->bytes<=0)) ) {
|| (peerstate->verified_key && (peerstate->verified_key->binary==NULL || peerstate->verified_key->bytes<=0))) {
return NULL;
}
if( min_verified )
if (min_verified)
{
return peerstate->verified_key;
}
if( peerstate->public_key )
if (peerstate->public_key)
{
return peerstate->public_key;
}
@ -346,7 +346,7 @@ dc_key_t* dc_apeerstate_peek_key(const dc_apeerstate_t* peerstate, int min_verif
int dc_apeerstate_init_from_header(dc_apeerstate_t* peerstate, const dc_aheader_t* header, time_t message_time)
{
if( peerstate == NULL || header == NULL ) {
if (peerstate == NULL || header == NULL) {
return 0;
}
@ -367,7 +367,7 @@ int dc_apeerstate_init_from_header(dc_apeerstate_t* peerstate, const dc_aheader_
int dc_apeerstate_init_from_gossip(dc_apeerstate_t* peerstate, const dc_aheader_t* gossip_header, time_t message_time)
{
if( peerstate == NULL || gossip_header == NULL ) {
if (peerstate == NULL || gossip_header == NULL) {
return 0;
}
@ -386,11 +386,11 @@ int dc_apeerstate_init_from_gossip(dc_apeerstate_t* peerstate, const dc_aheader_
int dc_apeerstate_degrade_encryption(dc_apeerstate_t* peerstate, time_t message_time)
{
if( peerstate==NULL ) {
if (peerstate==NULL) {
return 0;
}
if( peerstate->prefer_encrypt == DC_PE_MUTUAL ) {
if (peerstate->prefer_encrypt == DC_PE_MUTUAL) {
peerstate->degrade_event |= DC_DE_ENCRYPTION_PAUSED;
}
@ -404,23 +404,23 @@ int dc_apeerstate_degrade_encryption(dc_apeerstate_t* peerstate, time_t message_
void dc_apeerstate_apply_header(dc_apeerstate_t* peerstate, const dc_aheader_t* header, time_t message_time)
{
if( peerstate==NULL || header==NULL
if (peerstate==NULL || header==NULL
|| peerstate->addr==NULL
|| header->addr==NULL || header->public_key->binary==NULL
|| strcasecmp(peerstate->addr, header->addr)!=0 ) {
|| strcasecmp(peerstate->addr, header->addr)!=0) {
return;
}
if( message_time > peerstate->last_seen_autocrypt )
if (message_time > peerstate->last_seen_autocrypt)
{
peerstate->last_seen = message_time;
peerstate->last_seen_autocrypt = message_time;
peerstate->to_save |= DC_SAVE_TIMESTAMPS;
if( (header->prefer_encrypt==DC_PE_MUTUAL || header->prefer_encrypt==DC_PE_NOPREFERENCE) /*this also switches from DC_PE_RESET to DC_PE_NOPREFERENCE, which is just fine as the function is only called _if_ the Autocrypt:-header is preset at all */
&& header->prefer_encrypt != peerstate->prefer_encrypt )
if ((header->prefer_encrypt==DC_PE_MUTUAL || header->prefer_encrypt==DC_PE_NOPREFERENCE) /*this also switches from DC_PE_RESET to DC_PE_NOPREFERENCE, which is just fine as the function is only called _if_ the Autocrypt:-header is preset at all */
&& header->prefer_encrypt != peerstate->prefer_encrypt)
{
if( peerstate->prefer_encrypt == DC_PE_MUTUAL && header->prefer_encrypt != DC_PE_MUTUAL ) {
if (peerstate->prefer_encrypt == DC_PE_MUTUAL && header->prefer_encrypt != DC_PE_MUTUAL) {
peerstate->degrade_event |= DC_DE_ENCRYPTION_PAUSED;
}
@ -428,11 +428,11 @@ void dc_apeerstate_apply_header(dc_apeerstate_t* peerstate, const dc_aheader_t*
peerstate->to_save |= DC_SAVE_ALL;
}
if( peerstate->public_key == NULL ) {
if (peerstate->public_key == NULL) {
peerstate->public_key = dc_key_new();
}
if( !dc_key_equals(peerstate->public_key, header->public_key) )
if (!dc_key_equals(peerstate->public_key, header->public_key))
{
dc_key_set_from_key(peerstate->public_key, header->public_key);
dc_apeerstate_recalc_fingerprint(peerstate);
@ -444,23 +444,23 @@ void dc_apeerstate_apply_header(dc_apeerstate_t* peerstate, const dc_aheader_t*
void dc_apeerstate_apply_gossip(dc_apeerstate_t* peerstate, const dc_aheader_t* gossip_header, time_t message_time)
{
if( peerstate==NULL || gossip_header==NULL
if (peerstate==NULL || gossip_header==NULL
|| peerstate->addr==NULL
|| gossip_header->addr==NULL || gossip_header->public_key->binary==NULL
|| strcasecmp(peerstate->addr, gossip_header->addr)!=0 ) {
|| strcasecmp(peerstate->addr, gossip_header->addr)!=0) {
return;
}
if( message_time > peerstate->gossip_timestamp )
if (message_time > peerstate->gossip_timestamp)
{
peerstate->gossip_timestamp = message_time;
peerstate->to_save |= DC_SAVE_TIMESTAMPS;
if( peerstate->gossip_key == NULL ) {
if (peerstate->gossip_key == NULL) {
peerstate->gossip_key = dc_key_new();
}
if( !dc_key_equals(peerstate->gossip_key, gossip_header->public_key) )
if (!dc_key_equals(peerstate->gossip_key, gossip_header->public_key))
{
dc_key_set_from_key(peerstate->gossip_key, gossip_header->public_key);
dc_apeerstate_recalc_fingerprint(peerstate);
@ -484,43 +484,43 @@ int dc_apeerstate_recalc_fingerprint(dc_apeerstate_t* peerstate)
int success = 0;
char* old_public_fingerprint = NULL, *old_gossip_fingerprint = NULL;
if( peerstate == NULL ) {
if (peerstate == NULL) {
goto cleanup;
}
if( peerstate->public_key )
if (peerstate->public_key)
{
old_public_fingerprint = peerstate->public_key_fingerprint;
peerstate->public_key_fingerprint = dc_key_get_fingerprint(peerstate->public_key); /* returns the empty string for errors, however, this should be saved as well as it represents an erroneous key */
if( old_public_fingerprint == NULL
if (old_public_fingerprint == NULL
|| old_public_fingerprint[0] == 0
|| peerstate->public_key_fingerprint == NULL
|| peerstate->public_key_fingerprint[0] == 0
|| strcasecmp(old_public_fingerprint, peerstate->public_key_fingerprint) != 0 )
|| strcasecmp(old_public_fingerprint, peerstate->public_key_fingerprint) != 0)
{
peerstate->to_save |= DC_SAVE_ALL;
if( old_public_fingerprint && old_public_fingerprint[0] ) { // no degrade event when we recveive just the initial fingerprint
if (old_public_fingerprint && old_public_fingerprint[0]) { // no degrade event when we recveive just the initial fingerprint
peerstate->degrade_event |= DC_DE_FINGERPRINT_CHANGED;
}
}
}
if( peerstate->gossip_key )
if (peerstate->gossip_key)
{
old_gossip_fingerprint = peerstate->gossip_key_fingerprint;
peerstate->gossip_key_fingerprint = dc_key_get_fingerprint(peerstate->gossip_key); /* returns the empty string for errors, however, this should be saved as well as it represents an erroneous key */
if( old_gossip_fingerprint == NULL
if (old_gossip_fingerprint == NULL
|| old_gossip_fingerprint[0] == 0
|| peerstate->gossip_key_fingerprint == NULL
|| peerstate->gossip_key_fingerprint[0] == 0
|| strcasecmp(old_gossip_fingerprint, peerstate->gossip_key_fingerprint) != 0 )
|| strcasecmp(old_gossip_fingerprint, peerstate->gossip_key_fingerprint) != 0)
{
peerstate->to_save |= DC_SAVE_ALL;
if( old_gossip_fingerprint && old_gossip_fingerprint[0] ) { // no degrade event when we recveive just the initial fingerprint
if (old_gossip_fingerprint && old_gossip_fingerprint[0]) { // no degrade event when we recveive just the initial fingerprint
peerstate->degrade_event |= DC_DE_FINGERPRINT_CHANGED;
}
}
@ -559,17 +559,17 @@ int dc_apeerstate_set_verified(dc_apeerstate_t* peerstate, int which_key, const
{
int success = 0;
if( peerstate == NULL
if (peerstate == NULL
|| (which_key!=DC_PS_GOSSIP_KEY && which_key!=DC_PS_PUBLIC_KEY)
|| (verified!=DC_BIDIRECT_VERIFIED) ) {
|| (verified!=DC_BIDIRECT_VERIFIED)) {
goto cleanup;
}
if( which_key == DC_PS_PUBLIC_KEY
if (which_key == DC_PS_PUBLIC_KEY
&& peerstate->public_key_fingerprint != NULL
&& peerstate->public_key_fingerprint[0] != 0
&& fingerprint[0] != 0
&& strcasecmp(peerstate->public_key_fingerprint, fingerprint) == 0 )
&& strcasecmp(peerstate->public_key_fingerprint, fingerprint) == 0)
{
peerstate->to_save |= DC_SAVE_ALL;
peerstate->verified_key = dc_key_ref(peerstate->public_key);
@ -577,11 +577,11 @@ int dc_apeerstate_set_verified(dc_apeerstate_t* peerstate, int which_key, const
success = 1;
}
if( which_key == DC_PS_GOSSIP_KEY
if (which_key == DC_PS_GOSSIP_KEY
&& peerstate->gossip_key_fingerprint != NULL
&& peerstate->gossip_key_fingerprint[0] != 0
&& fingerprint[0] != 0
&& strcasecmp(peerstate->gossip_key_fingerprint, fingerprint) == 0 )
&& strcasecmp(peerstate->gossip_key_fingerprint, fingerprint) == 0)
{
peerstate->to_save |= DC_SAVE_ALL;
peerstate->verified_key = dc_key_ref(peerstate->gossip_key);
@ -596,13 +596,13 @@ cleanup:
int dc_apeerstate_has_verified_key(const dc_apeerstate_t* peerstate, const dc_hash_t* fingerprints)
{
if( peerstate == NULL || fingerprints == NULL ) {
if (peerstate == NULL || fingerprints == NULL) {
return 0;
}
if( peerstate->verified_key
if (peerstate->verified_key
&& peerstate->verified_key_fingerprint
&& dc_hash_find_str(fingerprints, peerstate->verified_key_fingerprint) ) {
&& dc_hash_find_str(fingerprints, peerstate->verified_key_fingerprint)) {
return 1;
}

View file

@ -41,7 +41,7 @@ dc_array_t* dc_array_new(dc_context_t* context, size_t initsize)
dc_array_t* array;
array = (dc_array_t*) malloc(sizeof(dc_array_t));
if( array==NULL ) {
if (array==NULL) {
exit(47);
}
@ -50,7 +50,7 @@ dc_array_t* dc_array_new(dc_context_t* context, size_t initsize)
array->count = 0;
array->allocated = initsize < 1? 1 : initsize;
array->array = malloc(array->allocated * sizeof(uintptr_t));
if( array->array==NULL ) {
if (array->array==NULL) {
exit(48);
}
@ -70,7 +70,7 @@ dc_array_t* dc_array_new(dc_context_t* context, size_t initsize)
*/
void dc_array_unref(dc_array_t* array)
{
if( array==NULL || array->magic != DC_ARRAY_MAGIC ) {
if (array==NULL || array->magic != DC_ARRAY_MAGIC) {
return;
}
@ -95,11 +95,11 @@ void dc_array_free_ptr(dc_array_t* array)
{
size_t i;
if( array==NULL || array->magic != DC_ARRAY_MAGIC ) {
if (array==NULL || array->magic != DC_ARRAY_MAGIC) {
return;
}
for( i = 0; i < array->count; i++ ) {
for (i = 0; i < array->count; i++) {
free((void*)array->array[i]);
array->array[i] = 0;
}
@ -121,7 +121,7 @@ dc_array_t* dc_array_duplicate(const dc_array_t* array)
{
dc_array_t* ret = NULL;
if( array==NULL || array->magic != DC_ARRAY_MAGIC ) {
if (array==NULL || array->magic != DC_ARRAY_MAGIC) {
return NULL;
}
@ -152,7 +152,7 @@ static int cmp_intptr_t(const void* p1, const void* p2)
*/
void dc_array_sort_ids(dc_array_t* array)
{
if( array == NULL || array->magic != DC_ARRAY_MAGIC || array->count <= 1 ) {
if (array == NULL || array->magic != DC_ARRAY_MAGIC || array->count <= 1) {
return;
}
qsort(array->array, array->count, sizeof(uintptr_t), cmp_intptr_t);
@ -179,7 +179,7 @@ static int cmp_strings_t(const void* p1, const void* p2)
*/
void dc_array_sort_strings(dc_array_t* array)
{
if( array == NULL || array->magic != DC_ARRAY_MAGIC || array->count <= 1 ) {
if (array == NULL || array->magic != DC_ARRAY_MAGIC || array->count <= 1) {
return;
}
qsort(array->array, array->count, sizeof(char*), cmp_strings_t);
@ -197,7 +197,7 @@ void dc_array_sort_strings(dc_array_t* array)
*/
void dc_array_empty(dc_array_t* array)
{
if( array == NULL || array->magic != DC_ARRAY_MAGIC ) {
if (array == NULL || array->magic != DC_ARRAY_MAGIC) {
return;
}
@ -218,13 +218,13 @@ void dc_array_empty(dc_array_t* array)
*/
void dc_array_add_uint(dc_array_t* array, uintptr_t item)
{
if( array == NULL || array->magic != DC_ARRAY_MAGIC ) {
if (array == NULL || array->magic != DC_ARRAY_MAGIC) {
return;
}
if( array->count == array->allocated ) {
if (array->count == array->allocated) {
int newsize = (array->allocated * 2) + 10;
if( (array->array=realloc(array->array, newsize*sizeof(uintptr_t)))==NULL ) {
if ((array->array=realloc(array->array, newsize*sizeof(uintptr_t)))==NULL) {
exit(49);
}
array->allocated = newsize;
@ -280,7 +280,7 @@ void dc_array_add_ptr(dc_array_t* array, void* item)
*/
size_t dc_array_get_cnt(const dc_array_t* array)
{
if( array == NULL || array->magic != DC_ARRAY_MAGIC ) {
if (array == NULL || array->magic != DC_ARRAY_MAGIC) {
return 0;
}
@ -301,7 +301,7 @@ size_t dc_array_get_cnt(const dc_array_t* array)
*/
uintptr_t dc_array_get_uint(const dc_array_t* array, size_t index)
{
if( array == NULL || array->magic != DC_ARRAY_MAGIC || index < 0 || index >= array->count ) {
if (array == NULL || array->magic != DC_ARRAY_MAGIC || index < 0 || index >= array->count) {
return 0;
}
@ -321,7 +321,7 @@ uintptr_t dc_array_get_uint(const dc_array_t* array, size_t index)
*/
uint32_t dc_array_get_id(const dc_array_t* array, size_t index)
{
if( array == NULL || array->magic != DC_ARRAY_MAGIC || index < 0 || index >= array->count ) {
if (array == NULL || array->magic != DC_ARRAY_MAGIC || index < 0 || index >= array->count) {
return 0;
}
@ -341,7 +341,7 @@ uint32_t dc_array_get_id(const dc_array_t* array, size_t index)
*/
void* dc_array_get_ptr(const dc_array_t* array, size_t index)
{
if( array == NULL || array->magic != DC_ARRAY_MAGIC || index < 0 || index >= array->count ) {
if (array == NULL || array->magic != DC_ARRAY_MAGIC || index < 0 || index >= array->count) {
return 0;
}
@ -362,16 +362,16 @@ void* dc_array_get_ptr(const dc_array_t* array, size_t index)
*/
int dc_array_search_id(const dc_array_t* array, uint32_t needle, size_t* ret_index)
{
if( array == NULL || array->magic != DC_ARRAY_MAGIC ) {
if (array == NULL || array->magic != DC_ARRAY_MAGIC) {
return 0;
}
uintptr_t* data = array->array;
size_t i, cnt = array->count;
for( i=0; i<cnt; i++ )
for (i=0; i<cnt; i++)
{
if( data[i] == needle ) {
if( ret_index ) {
if (data[i] == needle) {
if (ret_index) {
*ret_index = i;
}
return 1;
@ -394,7 +394,7 @@ int dc_array_search_id(const dc_array_t* array, uint32_t needle, size_t* ret_ind
*/
const uintptr_t* dc_array_get_raw(const dc_array_t* array)
{
if( array == NULL || array->magic != DC_ARRAY_MAGIC ) {
if (array == NULL || array->magic != DC_ARRAY_MAGIC) {
return NULL;
}
return array->array;
@ -407,7 +407,7 @@ char* dc_arr_to_string(const uint32_t* arr, int cnt)
char* ret = NULL;
const char* sep = ",";
if( arr==NULL || cnt <= 0 ) {
if (arr==NULL || cnt <= 0) {
return dc_strdup("");
}
@ -415,10 +415,10 @@ char* dc_arr_to_string(const uint32_t* arr, int cnt)
#define INT_ARR_TO_STR(a, c) { \
int i; \
ret = malloc((c)*(11+strlen(sep))/*sign,10 digits,sep*/+1/*terminating zero*/); \
if( ret == NULL ) { exit(35); } \
if (ret == NULL) { exit(35); } \
ret[0] = 0; \
for( i=0; i<(c); i++ ) { \
if( i ) { \
for (i=0; i<(c); i++) { \
if (i) { \
strcat(ret, sep); \
} \
sprintf(&ret[strlen(ret)], "%lu", (unsigned long)(a)[i]); \
@ -435,7 +435,7 @@ char* dc_array_get_string(const dc_array_t* array, const char* sep)
{
char* ret = NULL;
if( array == NULL || array->magic != DC_ARRAY_MAGIC || sep==NULL ) {
if (array == NULL || array->magic != DC_ARRAY_MAGIC || sep==NULL) {
return dc_strdup("");
}

View file

@ -43,7 +43,7 @@ dc_chat_t* dc_chat_new(dc_context_t* context)
{
dc_chat_t* chat = NULL;
if( context == NULL || (chat=calloc(1, sizeof(dc_chat_t)))==NULL ) {
if (context == NULL || (chat=calloc(1, sizeof(dc_chat_t)))==NULL) {
exit(14); /* cannot allocate little memory, unrecoverable error */
}
@ -67,7 +67,7 @@ dc_chat_t* dc_chat_new(dc_context_t* context)
*/
void dc_chat_unref(dc_chat_t* chat)
{
if( chat==NULL || chat->magic != DC_CHAT_MAGIC ) {
if (chat==NULL || chat->magic != DC_CHAT_MAGIC) {
return;
}
@ -89,7 +89,7 @@ void dc_chat_unref(dc_chat_t* chat)
*/
void dc_chat_empty(dc_chat_t* chat)
{
if( chat == NULL || chat->magic != DC_CHAT_MAGIC ) {
if (chat == NULL || chat->magic != DC_CHAT_MAGIC) {
return;
}
@ -136,7 +136,7 @@ void dc_chat_empty(dc_chat_t* chat)
*/
uint32_t dc_chat_get_id(dc_chat_t* chat)
{
if( chat == NULL || chat->magic != DC_CHAT_MAGIC ) {
if (chat == NULL || chat->magic != DC_CHAT_MAGIC) {
return 0;
}
@ -167,7 +167,7 @@ uint32_t dc_chat_get_id(dc_chat_t* chat)
*/
int dc_chat_get_type(dc_chat_t* chat)
{
if( chat == NULL || chat->magic != DC_CHAT_MAGIC ) {
if (chat == NULL || chat->magic != DC_CHAT_MAGIC) {
return DC_CHAT_TYPE_UNDEFINED;
}
return chat->type;
@ -191,7 +191,7 @@ int dc_chat_get_type(dc_chat_t* chat)
*/
char* dc_chat_get_name(dc_chat_t* chat)
{
if( chat == NULL || chat->magic != DC_CHAT_MAGIC ) {
if (chat == NULL || chat->magic != DC_CHAT_MAGIC) {
return dc_strdup("Err");
}
@ -216,15 +216,15 @@ char* dc_chat_get_subtitle(dc_chat_t* chat)
/* returns either the address or the number of chat members */
char* ret = NULL;
if( chat == NULL || chat->magic != DC_CHAT_MAGIC ) {
if (chat == NULL || chat->magic != DC_CHAT_MAGIC) {
return dc_strdup("Err");
}
if( chat->type == DC_CHAT_TYPE_SINGLE && dc_param_exists(chat->param, DC_PARAM_SELFTALK) )
if (chat->type == DC_CHAT_TYPE_SINGLE && dc_param_exists(chat->param, DC_PARAM_SELFTALK))
{
ret = dc_stock_str(chat->context, DC_STR_SELFTALK_SUBTITLE);
}
else if( chat->type == DC_CHAT_TYPE_SINGLE )
else if (chat->type == DC_CHAT_TYPE_SINGLE)
{
int r;
sqlite3_stmt* stmt = dc_sqlite3_prepare(chat->context->sql,
@ -234,16 +234,16 @@ char* dc_chat_get_subtitle(dc_chat_t* chat)
sqlite3_bind_int(stmt, 1, chat->id);
r = sqlite3_step(stmt);
if( r == SQLITE_ROW ) {
if (r == SQLITE_ROW) {
ret = dc_strdup((const char*)sqlite3_column_text(stmt, 0));
}
sqlite3_finalize(stmt);
}
else if( DC_CHAT_TYPE_IS_MULTI(chat->type) )
else if (DC_CHAT_TYPE_IS_MULTI(chat->type))
{
int cnt = 0;
if( chat->id == DC_CHAT_ID_DEADDROP )
if (chat->id == DC_CHAT_ID_DEADDROP)
{
ret = dc_stock_str(chat->context, DC_STR_DEADDROP); /* typically, the subtitle for the deaddropn is not displayed at all */
}
@ -272,7 +272,7 @@ char* dc_chat_get_subtitle(dc_chat_t* chat)
*/
char* dc_chat_get_profile_image(dc_chat_t* chat)
{
if( chat == NULL || chat->magic != DC_CHAT_MAGIC ) {
if (chat == NULL || chat->magic != DC_CHAT_MAGIC) {
return NULL;
}
@ -295,7 +295,7 @@ char* dc_chat_get_profile_image(dc_chat_t* chat)
*/
char* dc_chat_get_draft(dc_chat_t* chat)
{
if( chat == NULL || chat->magic != DC_CHAT_MAGIC ) {
if (chat == NULL || chat->magic != DC_CHAT_MAGIC) {
return NULL;
}
return dc_strdup_keep_null(chat->draft_text); /* may be NULL */
@ -316,7 +316,7 @@ char* dc_chat_get_draft(dc_chat_t* chat)
*/
time_t dc_chat_get_draft_timestamp(dc_chat_t* chat)
{
if( chat == NULL || chat->magic != DC_CHAT_MAGIC ) {
if (chat == NULL || chat->magic != DC_CHAT_MAGIC) {
return 0;
}
return chat->draft_timestamp;
@ -342,7 +342,7 @@ time_t dc_chat_get_draft_timestamp(dc_chat_t* chat)
*/
int dc_chat_get_archived(dc_chat_t* chat)
{
if( chat == NULL || chat->magic != DC_CHAT_MAGIC ) {
if (chat == NULL || chat->magic != DC_CHAT_MAGIC) {
return 0;
}
return chat->archived;
@ -366,7 +366,7 @@ int dc_chat_get_archived(dc_chat_t* chat)
*/
int dc_chat_is_unpromoted(dc_chat_t* chat)
{
if( chat == NULL || chat->magic != DC_CHAT_MAGIC ) {
if (chat == NULL || chat->magic != DC_CHAT_MAGIC) {
return 0;
}
return dc_param_get_int(chat->param, DC_PARAM_UNPROMOTED, 0);
@ -386,7 +386,7 @@ int dc_chat_is_unpromoted(dc_chat_t* chat)
*/
int dc_chat_is_verified(dc_chat_t* chat)
{
if( chat == NULL || chat->magic != DC_CHAT_MAGIC ) {
if (chat == NULL || chat->magic != DC_CHAT_MAGIC) {
return 0;
}
return (chat->type==DC_CHAT_TYPE_VERIFIED_GROUP);
@ -405,7 +405,7 @@ int dc_chat_is_verified(dc_chat_t* chat)
*/
int dc_chat_is_self_talk(dc_chat_t* chat)
{
if( chat == NULL || chat->magic != DC_CHAT_MAGIC ) {
if (chat == NULL || chat->magic != DC_CHAT_MAGIC) {
return 0;
}
return dc_param_exists(chat->param, DC_PARAM_SELFTALK);
@ -435,7 +435,7 @@ static int dc_chat_set_from_stmt(dc_chat_t* chat, sqlite3_stmt* row)
int row_offset = 0;
const char* draft_text = NULL;
if( chat == NULL || chat->magic != DC_CHAT_MAGIC || row == NULL ) {
if (chat == NULL || chat->magic != DC_CHAT_MAGIC || row == NULL) {
return 0;
}
@ -454,7 +454,7 @@ static int dc_chat_set_from_stmt(dc_chat_t* chat, sqlite3_stmt* row)
/* We leave a NULL-pointer for the very usual situation of "no draft".
Also make sure, draft_text and draft_timestamp are set together */
if( chat->draft_timestamp && draft_text && draft_text[0] ) {
if (chat->draft_timestamp && draft_text && draft_text[0]) {
chat->draft_text = dc_strdup(draft_text);
}
else {
@ -462,21 +462,21 @@ static int dc_chat_set_from_stmt(dc_chat_t* chat, sqlite3_stmt* row)
}
/* correct the title of some special groups */
if( chat->id == DC_CHAT_ID_DEADDROP ) {
if (chat->id == DC_CHAT_ID_DEADDROP) {
free(chat->name);
chat->name = dc_stock_str(chat->context, DC_STR_DEADDROP);
}
else if( chat->id == DC_CHAT_ID_ARCHIVED_LINK ) {
else if (chat->id == DC_CHAT_ID_ARCHIVED_LINK) {
free(chat->name);
char* tempname = dc_stock_str(chat->context, DC_STR_ARCHIVEDCHATS);
chat->name = dc_mprintf("%s (%i)", tempname, dc_get_archived_count(chat->context));
free(tempname);
}
else if( chat->id == DC_CHAT_ID_STARRED ) {
else if (chat->id == DC_CHAT_ID_STARRED) {
free(chat->name);
chat->name = dc_stock_str(chat->context, DC_STR_STARREDMSGS);
}
else if( dc_param_exists(chat->param, DC_PARAM_SELFTALK) ) {
else if (dc_param_exists(chat->param, DC_PARAM_SELFTALK)) {
free(chat->name);
chat->name = dc_stock_str(chat->context, DC_STR_SELF);
}
@ -504,7 +504,7 @@ int dc_chat_load_from_db(dc_chat_t* chat, uint32_t chat_id)
int success = 0;
sqlite3_stmt* stmt = NULL;
if( chat==NULL || chat->magic != DC_CHAT_MAGIC ) {
if (chat==NULL || chat->magic != DC_CHAT_MAGIC) {
goto cleanup;
}
@ -514,11 +514,11 @@ int dc_chat_load_from_db(dc_chat_t* chat, uint32_t chat_id)
"SELECT " CHAT_FIELDS " FROM chats c WHERE c.id=?;");
sqlite3_bind_int(stmt, 1, chat_id);
if( sqlite3_step(stmt) != SQLITE_ROW ) {
if (sqlite3_step(stmt) != SQLITE_ROW) {
goto cleanup;
}
if( !dc_chat_set_from_stmt(chat, stmt) ) {
if (!dc_chat_set_from_stmt(chat, stmt)) {
goto cleanup;
}

View file

@ -39,13 +39,13 @@ dc_chatlist_t* dc_chatlist_new(dc_context_t* context)
{
dc_chatlist_t* chatlist = NULL;
if( (chatlist=calloc(1, sizeof(dc_chatlist_t)))==NULL ) {
if ((chatlist=calloc(1, sizeof(dc_chatlist_t)))==NULL) {
exit(20);
}
chatlist->magic = DC_CHATLIST_MAGIC;
chatlist->context = context;
if( (chatlist->chatNlastmsg_ids=dc_array_new(context, 128))==NULL ) {
if ((chatlist->chatNlastmsg_ids=dc_array_new(context, 128))==NULL) {
exit(32);
}
@ -65,7 +65,7 @@ dc_chatlist_t* dc_chatlist_new(dc_context_t* context)
*/
void dc_chatlist_unref(dc_chatlist_t* chatlist)
{
if( chatlist==NULL || chatlist->magic != DC_CHATLIST_MAGIC ) {
if (chatlist==NULL || chatlist->magic != DC_CHATLIST_MAGIC) {
return;
}
@ -87,7 +87,7 @@ void dc_chatlist_unref(dc_chatlist_t* chatlist)
*/
void dc_chatlist_empty(dc_chatlist_t* chatlist)
{
if( chatlist == NULL || chatlist->magic != DC_CHATLIST_MAGIC ) {
if (chatlist == NULL || chatlist->magic != DC_CHATLIST_MAGIC) {
return;
}
@ -107,7 +107,7 @@ void dc_chatlist_empty(dc_chatlist_t* chatlist)
*/
size_t dc_chatlist_get_cnt(dc_chatlist_t* chatlist)
{
if( chatlist == NULL || chatlist->magic != DC_CHATLIST_MAGIC ) {
if (chatlist == NULL || chatlist->magic != DC_CHATLIST_MAGIC) {
return 0;
}
@ -131,7 +131,7 @@ size_t dc_chatlist_get_cnt(dc_chatlist_t* chatlist)
*/
uint32_t dc_chatlist_get_chat_id(dc_chatlist_t* chatlist, size_t index)
{
if( chatlist == NULL || chatlist->magic != DC_CHATLIST_MAGIC || chatlist->chatNlastmsg_ids == NULL || index >= chatlist->cnt ) {
if (chatlist == NULL || chatlist->magic != DC_CHATLIST_MAGIC || chatlist->chatNlastmsg_ids == NULL || index >= chatlist->cnt) {
return 0;
}
@ -155,7 +155,7 @@ uint32_t dc_chatlist_get_chat_id(dc_chatlist_t* chatlist, size_t index)
*/
uint32_t dc_chatlist_get_msg_id(dc_chatlist_t* chatlist, size_t index)
{
if( chatlist == NULL || chatlist->magic != DC_CHATLIST_MAGIC || chatlist->chatNlastmsg_ids == NULL || index >= chatlist->cnt ) {
if (chatlist == NULL || chatlist->magic != DC_CHATLIST_MAGIC || chatlist->chatNlastmsg_ids == NULL || index >= chatlist->cnt) {
return 0;
}
@ -205,41 +205,41 @@ dc_lot_t* dc_chatlist_get_summary(dc_chatlist_t* chatlist, size_t index, dc_chat
dc_contact_t* lastcontact = NULL;
dc_chat_t* chat_to_delete = NULL;
if( chatlist == NULL || chatlist->magic != DC_CHATLIST_MAGIC || index >= chatlist->cnt ) {
if (chatlist == NULL || chatlist->magic != DC_CHATLIST_MAGIC || index >= chatlist->cnt) {
ret->text2 = dc_strdup("ErrBadChatlistIndex");
goto cleanup;
}
lastmsg_id = dc_array_get_id(chatlist->chatNlastmsg_ids, index*DC_CHATLIST_IDS_PER_RESULT+1);
if( chat==NULL ) {
if (chat==NULL) {
chat = dc_chat_new(chatlist->context);
chat_to_delete = chat;
if( !dc_chat_load_from_db(chat, dc_array_get_id(chatlist->chatNlastmsg_ids, index*DC_CHATLIST_IDS_PER_RESULT)) ) {
if (!dc_chat_load_from_db(chat, dc_array_get_id(chatlist->chatNlastmsg_ids, index*DC_CHATLIST_IDS_PER_RESULT))) {
ret->text2 = dc_strdup("ErrCannotReadChat");
goto cleanup;
}
}
if( lastmsg_id )
if (lastmsg_id)
{
lastmsg = dc_msg_new();
dc_msg_load_from_db(lastmsg, chatlist->context, lastmsg_id);
if( lastmsg->from_id != DC_CONTACT_ID_SELF && DC_CHAT_TYPE_IS_MULTI(chat->type) )
if (lastmsg->from_id != DC_CONTACT_ID_SELF && DC_CHAT_TYPE_IS_MULTI(chat->type))
{
lastcontact = dc_contact_new(chatlist->context);
dc_contact_load_from_db(lastcontact, chatlist->context->sql, lastmsg->from_id);
}
}
if( chat->id == DC_CHAT_ID_ARCHIVED_LINK )
if (chat->id == DC_CHAT_ID_ARCHIVED_LINK)
{
ret->text2 = dc_strdup(NULL);
}
else if( chat->draft_timestamp
else if (chat->draft_timestamp
&& chat->draft_text
&& (lastmsg==NULL || chat->draft_timestamp>lastmsg->timestamp) )
&& (lastmsg==NULL || chat->draft_timestamp>lastmsg->timestamp))
{
/* show the draft as the last message */
ret->text1 = dc_stock_str(chatlist->context, DC_STR_DRAFT);
@ -250,7 +250,7 @@ dc_lot_t* dc_chatlist_get_summary(dc_chatlist_t* chatlist, size_t index, dc_chat
ret->timestamp = chat->draft_timestamp;
}
else if( lastmsg == NULL || lastmsg->from_id == 0 )
else if (lastmsg == NULL || lastmsg->from_id == 0)
{
/* no messages */
ret->text2 = dc_stock_str(chatlist->context, DC_STR_NOMESSAGES);
@ -280,7 +280,7 @@ cleanup:
*/
dc_context_t* dc_chatlist_get_context(dc_chatlist_t* chatlist)
{
if( chatlist == NULL || chatlist->magic != DC_CHATLIST_MAGIC ) {
if (chatlist == NULL || chatlist->magic != DC_CHATLIST_MAGIC) {
return NULL;
}
return chatlist->context;
@ -303,7 +303,7 @@ int dc_chatlist_load_from_db(dc_chatlist_t* chatlist, int listflags, const char*
sqlite3_stmt* stmt = NULL;
char* strLikeCmd = NULL, *query = NULL;
if( chatlist == NULL || chatlist->magic != DC_CHATLIST_MAGIC || chatlist->context == NULL ) {
if (chatlist == NULL || chatlist->magic != DC_CHATLIST_MAGIC || chatlist->context == NULL) {
goto cleanup;
}
@ -322,25 +322,25 @@ int dc_chatlist_load_from_db(dc_chatlist_t* chatlist, int listflags, const char*
// for the deaddrop, however, they should really be hidden, however, _currently_ the deaddrop is not
// shown at all permanent in the chatlist.
if( query_contact_id )
if (query_contact_id)
{
// show chats shared with a given contact
stmt = dc_sqlite3_prepare(chatlist->context->sql,
QUR1 " AND c.id IN(SELECT chat_id FROM chats_contacts WHERE contact_id=?) " QUR2);
sqlite3_bind_int(stmt, 1, query_contact_id);
}
else if( listflags & DC_GCL_ARCHIVED_ONLY )
else if (listflags & DC_GCL_ARCHIVED_ONLY)
{
/* show archived chats */
stmt = dc_sqlite3_prepare(chatlist->context->sql,
QUR1 " AND c.archived=1 " QUR2);
}
else if( query__==NULL )
else if (query__==NULL)
{
/* show normal chatlist */
if( !(listflags & DC_GCL_NO_SPECIALS) ) {
if (!(listflags & DC_GCL_NO_SPECIALS)) {
uint32_t last_deaddrop_fresh_msg_id = dc_get_last_deaddrop_fresh_msg(chatlist->context);
if( last_deaddrop_fresh_msg_id > 0 ) {
if (last_deaddrop_fresh_msg_id > 0) {
dc_array_add_id(chatlist->chatNlastmsg_ids, DC_CHAT_ID_DEADDROP); /* show deaddrop with the last fresh message */
dc_array_add_id(chatlist->chatNlastmsg_ids, last_deaddrop_fresh_msg_id);
}
@ -355,7 +355,7 @@ int dc_chatlist_load_from_db(dc_chatlist_t* chatlist, int listflags, const char*
/* show chatlist filtered by a search string, this includes archived and unarchived */
query = dc_strdup(query__);
dc_trim(query);
if( query[0]==0 ) {
if (query[0]==0) {
success = 1; /*empty result*/
goto cleanup;
}
@ -365,13 +365,13 @@ int dc_chatlist_load_from_db(dc_chatlist_t* chatlist, int listflags, const char*
sqlite3_bind_text(stmt, 1, strLikeCmd, -1, SQLITE_STATIC);
}
while( sqlite3_step(stmt) == SQLITE_ROW )
while (sqlite3_step(stmt) == SQLITE_ROW)
{
dc_array_add_id(chatlist->chatNlastmsg_ids, sqlite3_column_int(stmt, 0));
dc_array_add_id(chatlist->chatNlastmsg_ids, sqlite3_column_int(stmt, 1));
}
if( add_archived_link_item && dc_get_archived_count(chatlist->context)>0 )
if (add_archived_link_item && dc_get_archived_count(chatlist->context)>0)
{
dc_array_add_id(chatlist->chatNlastmsg_ids, DC_CHAT_ID_ARCHIVED_LINK);
dc_array_add_id(chatlist->chatNlastmsg_ids, 0);

View file

@ -40,7 +40,7 @@ static char* read_autoconf_file(dc_context_t* context, const char* url)
char* filecontent = NULL;
dc_log_info(context, 0, "Testing %s ...", url);
filecontent = (char*)context->cb(context, DC_EVENT_HTTP_GET, (uintptr_t)url, 0);
if( filecontent == NULL ) {
if (filecontent == NULL) {
dc_log_info(context, 0, "Can't read file."); /* this is not a warning or an error, we're just testing */
return NULL;
}
@ -90,18 +90,18 @@ static void moz_autoconfigure_starttag_cb(void* userdata, const char* tag, char*
moz_autoconfigure_t* moz_ac = (moz_autoconfigure_t*)userdata;
const char* p1;
if( strcmp(tag, "incomingserver")==0 ) {
if (strcmp(tag, "incomingserver")==0) {
moz_ac->tag_server = (moz_ac->out_imap_set==0 && (p1=dc_attr_find(attr, "type"))!=NULL && strcasecmp(p1, "imap")==0)? MOZ_SERVER_IMAP : 0;
moz_ac->tag_config = 0;
}
else if( strcmp(tag, "outgoingserver") == 0 ) {
else if (strcmp(tag, "outgoingserver") == 0) {
moz_ac->tag_server = moz_ac->out_smtp_set==0? MOZ_SERVER_SMTP : 0;
moz_ac->tag_config = 0;
}
else if( strcmp(tag, "hostname") == 0 ) { moz_ac->tag_config = MOZ_HOSTNAME; }
else if( strcmp(tag, "port") == 0 ) { moz_ac->tag_config = MOZ_PORT; }
else if( strcmp(tag, "sockettype") == 0 ) { moz_ac->tag_config = MOZ_SOCKETTYPE; }
else if( strcmp(tag, "username") == 0 ) { moz_ac->tag_config = MOZ_USERNAME; }
else if (strcmp(tag, "hostname") == 0 ) { moz_ac->tag_config = MOZ_HOSTNAME; }
else if (strcmp(tag, "port") == 0 ) { moz_ac->tag_config = MOZ_PORT; }
else if (strcmp(tag, "sockettype") == 0) { moz_ac->tag_config = MOZ_SOCKETTYPE; }
else if (strcmp(tag, "username") == 0 ) { moz_ac->tag_config = MOZ_USERNAME; }
}
@ -115,27 +115,27 @@ static void moz_autoconfigure_text_cb(void* userdata, const char* text, int len)
dc_str_replace(&val, "%EMAILLOCALPART%", moz_ac->in_emaillocalpart);
dc_str_replace(&val, "%EMAILDOMAIN%", moz_ac->in_emaildomain);
if( moz_ac->tag_server == MOZ_SERVER_IMAP ) {
switch( moz_ac->tag_config ) {
if (moz_ac->tag_server == MOZ_SERVER_IMAP) {
switch (moz_ac->tag_config) {
case MOZ_HOSTNAME: free(moz_ac->out->mail_server); moz_ac->out->mail_server = val; val = NULL; break;
case MOZ_PORT: moz_ac->out->mail_port = atoi(val); break;
case MOZ_USERNAME: free(moz_ac->out->mail_user); moz_ac->out->mail_user = val; val = NULL; break;
case MOZ_SOCKETTYPE:
if( strcasecmp(val, "ssl")==0 ) { moz_ac->out->server_flags |=DC_LP_IMAP_SOCKET_SSL; }
if( strcasecmp(val, "starttls")==0 ) { moz_ac->out->server_flags |=DC_LP_IMAP_SOCKET_STARTTLS; }
if( strcasecmp(val, "plain")==0 ) { moz_ac->out->server_flags |=DC_LP_IMAP_SOCKET_PLAIN; }
if (strcasecmp(val, "ssl")==0) { moz_ac->out->server_flags |=DC_LP_IMAP_SOCKET_SSL; }
if (strcasecmp(val, "starttls")==0) { moz_ac->out->server_flags |=DC_LP_IMAP_SOCKET_STARTTLS; }
if (strcasecmp(val, "plain")==0) { moz_ac->out->server_flags |=DC_LP_IMAP_SOCKET_PLAIN; }
break;
}
}
else if( moz_ac->tag_server == MOZ_SERVER_SMTP ) {
switch( moz_ac->tag_config ) {
else if (moz_ac->tag_server == MOZ_SERVER_SMTP) {
switch (moz_ac->tag_config) {
case MOZ_HOSTNAME: free(moz_ac->out->send_server); moz_ac->out->send_server = val; val = NULL; break;
case MOZ_PORT: moz_ac->out->send_port = atoi(val); break;
case MOZ_USERNAME: free(moz_ac->out->send_user); moz_ac->out->send_user = val; val = NULL; break;
case MOZ_SOCKETTYPE:
if( strcasecmp(val, "ssl")==0 ) { moz_ac->out->server_flags |=DC_LP_SMTP_SOCKET_SSL; }
if( strcasecmp(val, "starttls")==0 ) { moz_ac->out->server_flags |=DC_LP_SMTP_SOCKET_STARTTLS; }
if( strcasecmp(val, "plain")==0 ) { moz_ac->out->server_flags |=DC_LP_SMTP_SOCKET_PLAIN; }
if (strcasecmp(val, "ssl")==0) { moz_ac->out->server_flags |=DC_LP_SMTP_SOCKET_SSL; }
if (strcasecmp(val, "starttls")==0) { moz_ac->out->server_flags |=DC_LP_SMTP_SOCKET_STARTTLS; }
if (strcasecmp(val, "plain")==0) { moz_ac->out->server_flags |=DC_LP_SMTP_SOCKET_PLAIN; }
break;
}
}
@ -148,12 +148,12 @@ static void moz_autoconfigure_endtag_cb(void* userdata, const char* tag)
{
moz_autoconfigure_t* moz_ac = (moz_autoconfigure_t*)userdata;
if( strcmp(tag, "incomingserver")==0 ) {
if (strcmp(tag, "incomingserver")==0) {
moz_ac->tag_server = 0;
moz_ac->tag_config = 0;
moz_ac->out_imap_set = 1;
}
else if( strcmp(tag, "outgoingserver")==0 ) {
else if (strcmp(tag, "outgoingserver")==0) {
moz_ac->tag_server = 0;
moz_ac->tag_config = 0;
moz_ac->out_smtp_set = 1;
@ -171,12 +171,12 @@ static dc_loginparam_t* moz_autoconfigure(dc_context_t* context, const char* url
memset(&moz_ac, 0, sizeof(moz_autoconfigure_t));
if( (xml_raw=read_autoconf_file(context, url))==NULL ) {
if ((xml_raw=read_autoconf_file(context, url))==NULL) {
goto cleanup;
}
moz_ac.in = param_in;
moz_ac.in_emaillocalpart = dc_strdup(param_in->addr); char* p = strchr(moz_ac.in_emaillocalpart, '@'); if( p == NULL ) { goto cleanup; } *p = 0;
moz_ac.in_emaillocalpart = dc_strdup(param_in->addr); char* p = strchr(moz_ac.in_emaillocalpart, '@'); if (p == NULL) { goto cleanup; } *p = 0;
moz_ac.in_emaildomain = dc_strdup(p+1);
moz_ac.out = dc_loginparam_new();
@ -186,10 +186,10 @@ static dc_loginparam_t* moz_autoconfigure(dc_context_t* context, const char* url
dc_saxparser_set_text_handler(&saxparser, moz_autoconfigure_text_cb);
dc_saxparser_parse (&saxparser, xml_raw);
if( moz_ac.out->mail_server == NULL
if (moz_ac.out->mail_server == NULL
|| moz_ac.out->mail_port == 0
|| moz_ac.out->send_server == NULL
|| moz_ac.out->send_port == 0 )
|| moz_ac.out->send_port == 0)
{
{ char* r = dc_loginparam_get_readable(moz_ac.out); dc_log_warning(context, 0, "Bad or incomplete autoconfig: %s", r); free(r); }
@ -237,7 +237,7 @@ typedef struct outlk_autodiscover_t
static void outlk_clean_config(outlk_autodiscover_t* outlk_ad)
{
int i;
for( i = 0; i < _OUTLK_COUNT_; i++ ) {
for (i = 0; i < _OUTLK_COUNT_; i++) {
free(outlk_ad->config[i]);
outlk_ad->config[i] = NULL;
}
@ -248,12 +248,12 @@ static void outlk_autodiscover_starttag_cb(void* userdata, const char* tag, char
{
outlk_autodiscover_t* outlk_ad = (outlk_autodiscover_t*)userdata;
if( strcmp(tag, "protocol") == 0 ) { outlk_clean_config(outlk_ad); } /* this also cleans "redirecturl", however, this is not problem as the protocol block is only valid for action "settings". */
else if( strcmp(tag, "type") == 0 ) { outlk_ad->tag_config = OUTLK_TYPE; }
else if( strcmp(tag, "server") == 0 ) { outlk_ad->tag_config = OUTLK_SERVER; }
else if( strcmp(tag, "port") == 0 ) { outlk_ad->tag_config = OUTLK_PORT; }
else if( strcmp(tag, "ssl") == 0 ) { outlk_ad->tag_config = OUTLK_SSL; }
else if( strcmp(tag, "redirecturl") == 0 ) { outlk_ad->tag_config = OUTLK_REDIRECTURL; }
if (strcmp(tag, "protocol") == 0 ) { outlk_clean_config(outlk_ad); } /* this also cleans "redirecturl", however, this is not problem as the protocol block is only valid for action "settings". */
else if (strcmp(tag, "type") == 0 ) { outlk_ad->tag_config = OUTLK_TYPE; }
else if (strcmp(tag, "server") == 0 ) { outlk_ad->tag_config = OUTLK_SERVER; }
else if (strcmp(tag, "port") == 0 ) { outlk_ad->tag_config = OUTLK_PORT; }
else if (strcmp(tag, "ssl") == 0 ) { outlk_ad->tag_config = OUTLK_SSL; }
else if (strcmp(tag, "redirecturl") == 0) { outlk_ad->tag_config = OUTLK_REDIRECTURL; }
}
@ -273,27 +273,27 @@ static void outlk_autodiscover_endtag_cb(void* userdata, const char* tag)
{
outlk_autodiscover_t* outlk_ad = (outlk_autodiscover_t*)userdata;
if( strcmp(tag, "protocol")==0 )
if (strcmp(tag, "protocol")==0)
{
/* copy collected confituration to out (we have to delay this as we do not know when the <type> tag appears in the sax-stream) */
if( outlk_ad->config[OUTLK_TYPE] )
if (outlk_ad->config[OUTLK_TYPE])
{
int port = dc_atoi_null_is_0(outlk_ad->config[OUTLK_PORT]),
ssl_on = (outlk_ad->config[OUTLK_SSL] && strcasecmp(outlk_ad->config[OUTLK_SSL], "on" )==0),
ssl_on = (outlk_ad->config[OUTLK_SSL] && strcasecmp(outlk_ad->config[OUTLK_SSL], "on")==0),
ssl_off = (outlk_ad->config[OUTLK_SSL] && strcasecmp(outlk_ad->config[OUTLK_SSL], "off")==0);
if( strcasecmp(outlk_ad->config[OUTLK_TYPE], "imap")==0 && outlk_ad->out_imap_set==0 ) {
if (strcasecmp(outlk_ad->config[OUTLK_TYPE], "imap")==0 && outlk_ad->out_imap_set==0) {
outlk_ad->out->mail_server = dc_strdup_keep_null(outlk_ad->config[OUTLK_SERVER]);
outlk_ad->out->mail_port = port;
if( ssl_on ) { outlk_ad->out->server_flags |= DC_LP_IMAP_SOCKET_SSL; }
else if( ssl_off ) { outlk_ad->out->server_flags |= DC_LP_IMAP_SOCKET_PLAIN; }
if (ssl_on) { outlk_ad->out->server_flags |= DC_LP_IMAP_SOCKET_SSL; }
else if (ssl_off) { outlk_ad->out->server_flags |= DC_LP_IMAP_SOCKET_PLAIN; }
outlk_ad->out_imap_set = 1;
}
else if( strcasecmp(outlk_ad->config[OUTLK_TYPE], "smtp")==0 && outlk_ad->out_smtp_set==0 ) {
else if (strcasecmp(outlk_ad->config[OUTLK_TYPE], "smtp")==0 && outlk_ad->out_smtp_set==0) {
outlk_ad->out->send_server = dc_strdup_keep_null(outlk_ad->config[OUTLK_SERVER]);
outlk_ad->out->send_port = port;
if( ssl_on ) { outlk_ad->out->server_flags |= DC_LP_SMTP_SOCKET_SSL; }
else if( ssl_off ) { outlk_ad->out->server_flags |= DC_LP_SMTP_SOCKET_PLAIN; }
if (ssl_on) { outlk_ad->out->server_flags |= DC_LP_SMTP_SOCKET_SSL; }
else if (ssl_off) { outlk_ad->out->server_flags |= DC_LP_SMTP_SOCKET_PLAIN; }
outlk_ad->out_smtp_set = 1;
}
}
@ -310,11 +310,11 @@ static dc_loginparam_t* outlk_autodiscover(dc_context_t* context, const char* ur
outlk_autodiscover_t outlk_ad;
int i;
for( i = 0; i < 10 /* follow up to 10 xml-redirects (http-redirects are followed in read_autoconf_file() */; i++ )
for (i = 0; i < 10 /* follow up to 10 xml-redirects (http-redirects are followed in read_autoconf_file() */; i++)
{
memset(&outlk_ad, 0, sizeof(outlk_autodiscover_t));
if( (xml_raw=read_autoconf_file(context, url))==NULL ) {
if ((xml_raw=read_autoconf_file(context, url))==NULL) {
goto cleanup;
}
@ -327,7 +327,7 @@ static dc_loginparam_t* outlk_autodiscover(dc_context_t* context, const char* ur
dc_saxparser_set_text_handler(&saxparser, outlk_autodiscover_text_cb);
dc_saxparser_parse (&saxparser, xml_raw);
if( outlk_ad.config[OUTLK_REDIRECTURL] && outlk_ad.config[OUTLK_REDIRECTURL][0] ) {
if (outlk_ad.config[OUTLK_REDIRECTURL] && outlk_ad.config[OUTLK_REDIRECTURL][0]) {
free(url);
url = dc_strdup(outlk_ad.config[OUTLK_REDIRECTURL]);
dc_loginparam_unref(outlk_ad.out);
@ -339,10 +339,10 @@ static dc_loginparam_t* outlk_autodiscover(dc_context_t* context, const char* ur
}
}
if( outlk_ad.out->mail_server == NULL
if (outlk_ad.out->mail_server == NULL
|| outlk_ad.out->mail_port == 0
|| outlk_ad.out->send_server == NULL
|| outlk_ad.out->send_port == 0 )
|| outlk_ad.out->send_port == 0)
{
{ char* r = dc_loginparam_get_readable(outlk_ad.out); dc_log_warning(context, 0, "Bad or incomplete autoconfig: %s", r); free(r); }
dc_loginparam_unref(outlk_ad.out); /* autoconfig failed for the given URL */
@ -373,20 +373,20 @@ void dc_job_do_DC_JOB_CONFIGURE_IMAP(dc_context_t* context, dc_job_t* job)
char* param_addr_urlencoded = NULL;
dc_loginparam_t* param_autoconfig = NULL;
if( context == NULL || context->magic != DC_CONTEXT_MAGIC ) {
if (context == NULL || context->magic != DC_CONTEXT_MAGIC) {
goto cleanup;
}
if( !dc_alloc_ongoing(context) ) {
if (!dc_alloc_ongoing(context)) {
goto cleanup;
}
ongoing_allocated_here = 1;
#define PROGRESS(p) \
if( context->shall_stop_ongoing ) { goto cleanup; } \
if (context->shall_stop_ongoing) { goto cleanup; } \
context->cb(context, DC_EVENT_CONFIGURE_PROGRESS, (p)<1? 1 : ((p)>999? 999 : (p)), 0);
if( !dc_sqlite3_is_open(context->sql) ) {
if (!dc_sqlite3_is_open(context->sql)) {
dc_log_error(context, 0, "Cannot configure, database not opened.");
goto cleanup;
}
@ -404,7 +404,7 @@ void dc_job_do_DC_JOB_CONFIGURE_IMAP(dc_context_t* context, dc_job_t* job)
PROGRESS(0)
if( context->cb(context, DC_EVENT_IS_OFFLINE, 0, 0)!=0 ) {
if (context->cb(context, DC_EVENT_IS_OFFLINE, 0, 0)!=0) {
dc_log_error(context, DC_ERROR_NO_NETWORK, NULL);
goto cleanup;
}
@ -418,14 +418,14 @@ void dc_job_do_DC_JOB_CONFIGURE_IMAP(dc_context_t* context, dc_job_t* job)
dc_loginparam_read(param, context->sql, "");
if( param->addr == NULL ) {
if (param->addr == NULL) {
dc_log_error(context, 0, "Please enter the email address.");
goto cleanup;
}
dc_trim(param->addr);
param_domain = strchr(param->addr, '@');
if( param_domain==NULL || param_domain[0]==0 ) {
if (param_domain==NULL || param_domain[0]==0) {
dc_log_error(context, 0, "Bad email-address.");
goto cleanup;
}
@ -435,7 +435,7 @@ void dc_job_do_DC_JOB_CONFIGURE_IMAP(dc_context_t* context, dc_job_t* job)
/* if no password is given, assume an empty password.
(in general, unset values are NULL, not the empty string, this allows to use eg. empty user names or empty passwords) */
if( param->mail_pw == NULL ) {
if (param->mail_pw == NULL) {
param->mail_pw = dc_strdup(NULL);
}
@ -445,18 +445,18 @@ void dc_job_do_DC_JOB_CONFIGURE_IMAP(dc_context_t* context, dc_job_t* job)
/* 2. Autoconfig
**************************************************************************/
if( param->mail_server == NULL
if (param->mail_server == NULL
&& param->mail_port == 0
/*&&param->mail_user == NULL -- the user can enter a loginname which is used by autoconfig then */
&& param->send_server == NULL
&& param->send_port == 0
&& param->send_user == NULL
/*&&param->send_pw == NULL -- the password cannot be auto-configured and is no criterion for autoconfig or not */
&& param->server_flags == 0 )
&& param->server_flags == 0)
{
/* A. Search configurations from the domain used in the email-address */
for( i = 0; i <= 1; i++ ) {
if( param_autoconfig==NULL ) {
for (i = 0; i <= 1; i++) {
if (param_autoconfig==NULL) {
char* url = dc_mprintf("%s://autoconfig.%s/mail/config-v1.1.xml?emailaddress=%s", i==0?"https":"http", param_domain, param_addr_urlencoded); /* Thunderbird may or may not use SSL */
param_autoconfig = moz_autoconfigure(context, url, param);
free(url);
@ -464,8 +464,8 @@ void dc_job_do_DC_JOB_CONFIGURE_IMAP(dc_context_t* context, dc_job_t* job)
}
}
for( i = 0; i <= 1; i++ ) {
if( param_autoconfig==NULL ) {
for (i = 0; i <= 1; i++) {
if (param_autoconfig==NULL) {
char* url = dc_mprintf("%s://%s/.well-known/autoconfig/mail/config-v1.1.xml?emailaddress=%s", i==0?"https":"http", param_domain, param_addr_urlencoded); // the doc does not mention `emailaddress=`, however, Thunderbird adds it, see https://releases.mozilla.org/pub/thunderbird/ , which makes some sense
param_autoconfig = moz_autoconfigure(context, url, param);
free(url);
@ -473,8 +473,8 @@ void dc_job_do_DC_JOB_CONFIGURE_IMAP(dc_context_t* context, dc_job_t* job)
}
}
for( i = 0; i <= 1; i++ ) {
if( param_autoconfig==NULL ) {
for (i = 0; i <= 1; i++) {
if (param_autoconfig==NULL) {
char* url = dc_mprintf("https://%s%s/autodiscover/autodiscover.xml", i==0?"":"autodiscover.", param_domain); /* Outlook uses always SSL but different domains */
param_autoconfig = outlk_autodiscover(context, url, param);
free(url);
@ -483,7 +483,7 @@ void dc_job_do_DC_JOB_CONFIGURE_IMAP(dc_context_t* context, dc_job_t* job)
}
/* B. If we have no configuration yet, search configuration in Thunderbird's centeral database */
if( param_autoconfig==NULL )
if (param_autoconfig==NULL)
{
char* url = dc_mprintf("https://autoconfig.thunderbird.net/v1.1/%s", param_domain); /* always SSL for Thunderbird's database */
param_autoconfig = moz_autoconfigure(context, url, param);
@ -492,11 +492,11 @@ void dc_job_do_DC_JOB_CONFIGURE_IMAP(dc_context_t* context, dc_job_t* job)
}
/* C. Do we have any result? */
if( param_autoconfig )
if (param_autoconfig)
{
{ char* r = dc_loginparam_get_readable(param_autoconfig); dc_log_info(context, 0, "Got autoconfig: %s", r); free(r); }
if( param_autoconfig->mail_user ) {
if (param_autoconfig->mail_user) {
free(param->mail_user);
param->mail_user= dc_strdup_keep_null(param_autoconfig->mail_user);
}
@ -516,7 +516,7 @@ void dc_job_do_DC_JOB_CONFIGURE_IMAP(dc_context_t* context, dc_job_t* job)
/* 3. Internal specials (eg. for uploading to chats-folder etc.)
**************************************************************************/
if( strcasecmp(param_domain, "gmail.com")==0 || strcasecmp(param_domain, "googlemail.com")==0 )
if (strcasecmp(param_domain, "gmail.com")==0 || strcasecmp(param_domain, "googlemail.com")==0)
{
/* NB: Checking GMa'l too often (<10 Minutes) may result in blocking, says https://github.com/itprojects/InboxPager/blob/HEAD/README.md#gmail-configuration
Also note https://www.google.com/settings/security/lesssecureapps */
@ -534,60 +534,60 @@ void dc_job_do_DC_JOB_CONFIGURE_IMAP(dc_context_t* context, dc_job_t* job)
#define TYPICAL_SMTP_STARTTLS_PORT 587 /* also used very often, SSL:STARTTLS is maybe 50:50 */
#define TYPICAL_SMTP_PLAIN_PORT 25
if( param->mail_server == NULL ) {
if (param->mail_server == NULL) {
param->mail_server = dc_mprintf("imap.%s", param_domain);
}
if( param->mail_port == 0 ) {
if (param->mail_port == 0) {
param->mail_port = (param->server_flags&(DC_LP_IMAP_SOCKET_STARTTLS|DC_LP_IMAP_SOCKET_PLAIN))? TYPICAL_IMAP_STARTTLS_PORT : TYPICAL_IMAP_SSL_PORT;
}
if( param->mail_user == NULL ) {
if (param->mail_user == NULL) {
param->mail_user = dc_strdup(param->addr);
}
if( param->send_server == NULL && param->mail_server ) {
if (param->send_server == NULL && param->mail_server) {
param->send_server = dc_strdup(param->mail_server);
if( strncmp(param->send_server, "imap.", 5)==0 ) {
if (strncmp(param->send_server, "imap.", 5)==0) {
memcpy(param->send_server, "smtp", 4);
}
}
if( param->send_port == 0 ) {
if (param->send_port == 0) {
param->send_port = (param->server_flags&DC_LP_SMTP_SOCKET_STARTTLS)? TYPICAL_SMTP_STARTTLS_PORT :
((param->server_flags&DC_LP_SMTP_SOCKET_PLAIN)? TYPICAL_SMTP_PLAIN_PORT : TYPICAL_SMTP_SSL_PORT);
}
if( param->send_user == NULL && param->mail_user ) {
if (param->send_user == NULL && param->mail_user) {
param->send_user = dc_strdup(param->mail_user);
}
if( param->send_pw == NULL && param->mail_pw ) {
if (param->send_pw == NULL && param->mail_pw) {
param->send_pw = dc_strdup(param->mail_pw);
}
if( !dc_exactly_one_bit_set(param->server_flags&DC_LP_AUTH_FLAGS) )
if (!dc_exactly_one_bit_set(param->server_flags&DC_LP_AUTH_FLAGS))
{
param->server_flags &= ~DC_LP_AUTH_FLAGS;
param->server_flags |= DC_LP_AUTH_NORMAL;
}
if( !dc_exactly_one_bit_set(param->server_flags&DC_LP_IMAP_SOCKET_FLAGS) )
if (!dc_exactly_one_bit_set(param->server_flags&DC_LP_IMAP_SOCKET_FLAGS))
{
param->server_flags &= ~DC_LP_IMAP_SOCKET_FLAGS;
param->server_flags |= (param->send_port==TYPICAL_IMAP_STARTTLS_PORT? DC_LP_IMAP_SOCKET_STARTTLS : DC_LP_IMAP_SOCKET_SSL);
}
if( !dc_exactly_one_bit_set(param->server_flags&DC_LP_SMTP_SOCKET_FLAGS) )
if (!dc_exactly_one_bit_set(param->server_flags&DC_LP_SMTP_SOCKET_FLAGS))
{
param->server_flags &= ~DC_LP_SMTP_SOCKET_FLAGS;
param->server_flags |= ( param->send_port==TYPICAL_SMTP_STARTTLS_PORT? DC_LP_SMTP_SOCKET_STARTTLS :
(param->send_port==TYPICAL_SMTP_PLAIN_PORT? DC_LP_SMTP_SOCKET_PLAIN: DC_LP_SMTP_SOCKET_SSL) );
(param->send_port==TYPICAL_SMTP_PLAIN_PORT? DC_LP_SMTP_SOCKET_PLAIN: DC_LP_SMTP_SOCKET_SSL));
}
/* do we have a complete configuration? */
if( param->addr == NULL
if (param->addr == NULL
|| param->mail_server == NULL
|| param->mail_port == 0
|| param->mail_user == NULL
@ -596,7 +596,7 @@ void dc_job_do_DC_JOB_CONFIGURE_IMAP(dc_context_t* context, dc_job_t* job)
|| param->send_port == 0
|| param->send_user == NULL
|| param->send_pw == NULL
|| param->server_flags == 0 )
|| param->server_flags == 0)
{
dc_log_error(context, 0, "Account settings incomplete.");
goto cleanup;
@ -607,7 +607,7 @@ void dc_job_do_DC_JOB_CONFIGURE_IMAP(dc_context_t* context, dc_job_t* job)
/* try to connect to IMAP */
{ char* r = dc_loginparam_get_readable(param); dc_log_info(context, 0, "Trying: %s", r); free(r); }
if( !dc_imap_connect(context->imap, param) ) {
if (!dc_imap_connect(context->imap, param)) {
goto cleanup;
}
@ -616,8 +616,8 @@ void dc_job_do_DC_JOB_CONFIGURE_IMAP(dc_context_t* context, dc_job_t* job)
PROGRESS(800)
/* 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( !dc_smtp_connect(context->smtp, param) ) {
if( param_autoconfig ) {
if (!dc_smtp_connect(context->smtp, param)) {
if (param_autoconfig) {
goto cleanup;
}
@ -628,7 +628,7 @@ void dc_job_do_DC_JOB_CONFIGURE_IMAP(dc_context_t* context, dc_job_t* job)
param->send_port = TYPICAL_SMTP_STARTTLS_PORT;
{ char* r = dc_loginparam_get_readable(param); dc_log_info(context, 0, "Trying: %s", r); free(r); }
if( !dc_smtp_connect(context->smtp, param) ) {
if (!dc_smtp_connect(context->smtp, param)) {
goto cleanup;
}
}
@ -657,16 +657,16 @@ void dc_job_do_DC_JOB_CONFIGURE_IMAP(dc_context_t* context, dc_job_t* job)
cleanup:
context->cb(context, DC_EVENT_CONFIGURE_PROGRESS, 950, 0);
if( imap_connected_here ) { dc_imap_disconnect(context->imap); }
if (imap_connected_here) { dc_imap_disconnect(context->imap); }
context->cb(context, DC_EVENT_CONFIGURE_PROGRESS, 960, 0);
if( smtp_connected_here ) { dc_smtp_disconnect(context->smtp); }
if (smtp_connected_here) { dc_smtp_disconnect(context->smtp); }
context->cb(context, DC_EVENT_CONFIGURE_PROGRESS, 970, 0);
dc_loginparam_unref(param);
dc_loginparam_unref(param_autoconfig);
free(param_addr_urlencoded);
if( ongoing_allocated_here ) { dc_free_ongoing(context); }
if (ongoing_allocated_here) { dc_free_ongoing(context); }
context->cb(context, DC_EVENT_CONFIGURE_PROGRESS, 980, 0);
context->cb(context, DC_EVENT_CONFIGURE_PROGRESS, 990, 0);
@ -699,7 +699,7 @@ cleanup:
* database and you can call use the connection directly:
*
* ```
* if( !dc_is_configured(context) ) {
* if (!dc_is_configured(context)) {
* dc_configure(context);
* // wait for progress events
* }
@ -725,11 +725,11 @@ void dc_configure(dc_context_t* context)
*/
int dc_is_configured(dc_context_t* context)
{
if( context == NULL || context->magic != DC_CONTEXT_MAGIC ) {
if (context == NULL || context->magic != DC_CONTEXT_MAGIC) {
return 0;
}
if( dc_imap_is_connected(context->imap) ) { /* if we're connected, we're also configured. this check will speed up the check as no database is involved */
if (dc_imap_is_connected(context->imap)) { /* if we're connected, we're also configured. this check will speed up the check as no database is involved */
return 1;
}
@ -743,11 +743,11 @@ int dc_is_configured(dc_context_t* context)
*/
int dc_alloc_ongoing(dc_context_t* context)
{
if( context == NULL || context->magic != DC_CONTEXT_MAGIC ) {
if (context == NULL || context->magic != DC_CONTEXT_MAGIC) {
return 0;
}
if( context->ongoing_running || context->shall_stop_ongoing == 0 ) {
if (context->ongoing_running || context->shall_stop_ongoing == 0) {
dc_log_warning(context, 0, "There is already another ongoing process running.");
return 0;
}
@ -764,7 +764,7 @@ int dc_alloc_ongoing(dc_context_t* context)
*/
void dc_free_ongoing(dc_context_t* context)
{
if( context == NULL || context->magic != DC_CONTEXT_MAGIC ) {
if (context == NULL || context->magic != DC_CONTEXT_MAGIC) {
return;
}
@ -799,11 +799,11 @@ void dc_free_ongoing(dc_context_t* context)
*/
void dc_stop_ongoing_process(dc_context_t* context)
{
if( context == NULL || context->magic != DC_CONTEXT_MAGIC ) {
if (context == NULL || context->magic != DC_CONTEXT_MAGIC) {
return;
}
if( context->ongoing_running && context->shall_stop_ongoing==0 )
if (context->ongoing_running && context->shall_stop_ongoing==0)
{
dc_log_info(context, 0, "Signaling the ongoing process to stop ASAP.");
context->shall_stop_ongoing = 1;

View file

@ -41,7 +41,7 @@ dc_contact_t* dc_contact_new(dc_context_t* context)
{
dc_contact_t* contact = NULL;
if( (contact=calloc(1, sizeof(dc_contact_t)))==NULL ) {
if ((contact=calloc(1, sizeof(dc_contact_t)))==NULL) {
exit(19); /* cannot allocate little memory, unrecoverable error */
}
@ -63,7 +63,7 @@ dc_contact_t* dc_contact_new(dc_context_t* context)
*/
void dc_contact_unref(dc_contact_t* contact)
{
if( contact==NULL || contact->magic != DC_CONTACT_MAGIC ) {
if (contact==NULL || contact->magic != DC_CONTACT_MAGIC) {
return;
}
@ -86,7 +86,7 @@ void dc_contact_unref(dc_contact_t* contact)
*/
void dc_contact_empty(dc_contact_t* contact)
{
if( contact == NULL || contact->magic != DC_CONTACT_MAGIC ) {
if (contact == NULL || contact->magic != DC_CONTACT_MAGIC) {
return;
}
@ -122,7 +122,7 @@ void dc_contact_empty(dc_contact_t* contact)
*/
uint32_t dc_contact_get_id(const dc_contact_t* contact)
{
if( contact == NULL || contact->magic != DC_CONTACT_MAGIC ) {
if (contact == NULL || contact->magic != DC_CONTACT_MAGIC) {
return 0;
}
return contact->id;
@ -140,7 +140,7 @@ uint32_t dc_contact_get_id(const dc_contact_t* contact)
*/
char* dc_contact_get_addr(const dc_contact_t* contact)
{
if( contact == NULL || contact->magic != DC_CONTACT_MAGIC ) {
if (contact == NULL || contact->magic != DC_CONTACT_MAGIC) {
return dc_strdup(NULL);
}
@ -164,7 +164,7 @@ char* dc_contact_get_addr(const dc_contact_t* contact)
*/
char* dc_contact_get_name(const dc_contact_t* contact)
{
if( contact == NULL || contact->magic != DC_CONTACT_MAGIC ) {
if (contact == NULL || contact->magic != DC_CONTACT_MAGIC) {
return dc_strdup(NULL);
}
@ -187,11 +187,11 @@ char* dc_contact_get_name(const dc_contact_t* contact)
*/
char* dc_contact_get_display_name(const dc_contact_t* contact)
{
if( contact == NULL || contact->magic != DC_CONTACT_MAGIC ) {
if (contact == NULL || contact->magic != DC_CONTACT_MAGIC) {
return dc_strdup(NULL);
}
if( contact->name && contact->name[0] ) {
if (contact->name && contact->name[0]) {
return dc_strdup(contact->name);
}
@ -218,11 +218,11 @@ char* dc_contact_get_display_name(const dc_contact_t* contact)
*/
char* dc_contact_get_name_n_addr(const dc_contact_t* contact)
{
if( contact == NULL || contact->magic != DC_CONTACT_MAGIC ) {
if (contact == NULL || contact->magic != DC_CONTACT_MAGIC) {
return dc_strdup(NULL);
}
if( contact->name && contact->name[0] ) {
if (contact->name && contact->name[0]) {
return dc_mprintf("%s (%s)", contact->name, contact->addr);
}
@ -243,11 +243,11 @@ char* dc_contact_get_name_n_addr(const dc_contact_t* contact)
*/
char* dc_contact_get_first_name(const dc_contact_t* contact)
{
if( contact == NULL || contact->magic != DC_CONTACT_MAGIC ) {
if (contact == NULL || contact->magic != DC_CONTACT_MAGIC) {
return dc_strdup(NULL);
}
if( contact->name && contact->name[0] ) {
if (contact->name && contact->name[0]) {
return dc_get_first_name(contact->name);
}
@ -268,7 +268,7 @@ char* dc_contact_get_first_name(const dc_contact_t* contact)
*/
int dc_contact_is_blocked(const dc_contact_t* contact)
{
if( contact == NULL || contact->magic != DC_CONTACT_MAGIC ) {
if (contact == NULL || contact->magic != DC_CONTACT_MAGIC) {
return 0;
}
return contact->blocked;
@ -279,11 +279,11 @@ int dc_contact_n_peerstate_are_verified(const dc_contact_t* contact, const dc_ap
{
int contact_verified = DC_NOT_VERIFIED;
if( contact == NULL || contact->magic != DC_CONTACT_MAGIC ) {
if (contact == NULL || contact->magic != DC_CONTACT_MAGIC) {
goto cleanup;
}
if( contact->id == DC_CONTACT_ID_SELF ) {
if (contact->id == DC_CONTACT_ID_SELF) {
contact_verified = DC_BIDIRECT_VERIFIED;
goto cleanup; // we're always sort of secured-verified as we could verify the key on this device any time with the key on this device
}
@ -313,13 +313,13 @@ int dc_contact_is_verified(const dc_contact_t* contact)
int contact_verified = DC_NOT_VERIFIED;
dc_apeerstate_t* peerstate = NULL;
if( contact == NULL || contact->magic != DC_CONTACT_MAGIC ) {
if (contact == NULL || contact->magic != DC_CONTACT_MAGIC) {
goto cleanup;
}
peerstate = dc_apeerstate_new(contact->context);
if( !dc_apeerstate_load_by_addr(peerstate, contact->context->sql, contact->addr) ) {
if (!dc_apeerstate_load_by_addr(peerstate, contact->context->sql, contact->addr)) {
goto cleanup;
}
@ -347,10 +347,10 @@ char* dc_get_first_name(const char* full_name)
{
char* first_name = dc_strdup(full_name);
char* p1 = strchr(first_name, ' ');
if( p1 ) {
if (p1) {
*p1 = 0;
dc_rtrim(first_name);
if( first_name[0] == 0 ) { /*empty result? use the original string in this case */
if (first_name[0] == 0) { /*empty result? use the original string in this case */
free(first_name);
first_name = dc_strdup(full_name);
}
@ -383,24 +383,24 @@ char* dc_get_first_name(const char* full_name)
*/
void dc_normalize_name(char* full_name)
{
if( full_name == NULL ) {
if (full_name == NULL) {
return; /* error, however, this can be treated as documented behaviour */
}
dc_trim(full_name); /* remove spaces around possible quotes */
int len = strlen(full_name);
if( len > 0 ) {
if (len > 0) {
char firstchar = full_name[0], lastchar = full_name[len-1];
if( (firstchar=='\'' && lastchar=='\'')
|| (firstchar=='"' && lastchar=='"' )
|| (firstchar=='<' && lastchar=='>' ) ) {
if ((firstchar=='\'' && lastchar=='\'')
|| (firstchar=='"' && lastchar=='"')
|| (firstchar=='<' && lastchar=='>')) {
full_name[0] = ' ';
full_name[len-1] = ' '; /* the string is trimmed later again */
}
}
char* p1 = strchr(full_name, ',');
if( p1 ) {
if (p1) {
*p1 = 0;
char* last_name = dc_strdup(full_name);
char* first_name = dc_strdup(p1+1);
@ -437,7 +437,7 @@ char* dc_normalize_addr(const char* email_addr__)
{
char* addr = dc_strdup(email_addr__);
dc_trim(addr);
if( strncmp(addr, "mailto:", 7)==0 ) {
if (strncmp(addr, "mailto:", 7)==0) {
char* old = addr;
addr = dc_strdup(&old[7]);
free(old);
@ -459,13 +459,13 @@ int dc_contact_load_from_db(dc_contact_t* contact, dc_sqlite3_t* sql, uint32_t c
int success = 0;
sqlite3_stmt* stmt = NULL;
if( contact == NULL || contact->magic != DC_CONTACT_MAGIC || sql == NULL ) {
if (contact == NULL || contact->magic != DC_CONTACT_MAGIC || sql == NULL) {
goto cleanup;
}
dc_contact_empty(contact);
if( contact_id == DC_CONTACT_ID_SELF )
if (contact_id == DC_CONTACT_ID_SELF)
{
contact->id = contact_id;
contact->name = dc_stock_str(contact->context, DC_STR_SELF);
@ -478,7 +478,7 @@ int dc_contact_load_from_db(dc_contact_t* contact, dc_sqlite3_t* sql, uint32_t c
" FROM contacts c "
" WHERE c.id=?;");
sqlite3_bind_int(stmt, 1, contact_id);
if( sqlite3_step(stmt) != SQLITE_ROW ) {
if (sqlite3_step(stmt) != SQLITE_ROW) {
goto cleanup;
}

File diff suppressed because it is too large Load diff

View file

@ -48,38 +48,38 @@ static void dehtml_starttag_cb(void* userdata, const char* tag, char** attr)
{
dehtml_t* dehtml = (dehtml_t*)userdata;
if( strcmp(tag, "p")==0 || strcmp(tag, "div")==0 || strcmp(tag, "table")==0 || strcmp(tag, "td")==0 )
if (strcmp(tag, "p")==0 || strcmp(tag, "div")==0 || strcmp(tag, "table")==0 || strcmp(tag, "td")==0)
{
dc_strbuilder_cat(&dehtml->strbuilder, "\n\n");
dehtml->add_text = DO_ADD_REMOVE_LINEENDS;
}
else if( strcmp(tag, "br")==0 )
else if (strcmp(tag, "br")==0)
{
dc_strbuilder_cat(&dehtml->strbuilder, "\n");
dehtml->add_text = DO_ADD_REMOVE_LINEENDS;
}
else if( strcmp(tag, "style")==0 || strcmp(tag, "script")==0 || strcmp(tag, "title")==0 )
else if (strcmp(tag, "style")==0 || strcmp(tag, "script")==0 || strcmp(tag, "title")==0)
{
dehtml->add_text = DO_NOT_ADD;
}
else if( strcmp(tag, "pre")==0 )
else if (strcmp(tag, "pre")==0)
{
dc_strbuilder_cat(&dehtml->strbuilder, "\n\n");
dehtml->add_text = DO_ADD_PRESERVE_LINEENDS;
}
else if( strcmp(tag, "a")==0 )
else if (strcmp(tag, "a")==0)
{
free(dehtml->last_href);
dehtml->last_href = dc_strdup_keep_null(dc_attr_find(attr, "href"));
if( dehtml->last_href ) {
if (dehtml->last_href) {
dc_strbuilder_cat(&dehtml->strbuilder, "[");
}
}
else if( strcmp(tag, "b")==0 || strcmp(tag, "strong")==0 )
else if (strcmp(tag, "b")==0 || strcmp(tag, "strong")==0)
{
dc_strbuilder_cat(&dehtml->strbuilder, "*");
}
else if( strcmp(tag, "i")==0 || strcmp(tag, "em")==0 )
else if (strcmp(tag, "i")==0 || strcmp(tag, "em")==0)
{
dc_strbuilder_cat(&dehtml->strbuilder, "_");
}
@ -90,21 +90,21 @@ static void dehtml_text_cb(void* userdata, const char* text, int len)
{
dehtml_t* dehtml = (dehtml_t*)userdata;
if( dehtml->add_text != DO_NOT_ADD )
if (dehtml->add_text != DO_NOT_ADD)
{
char* last_added = dc_strbuilder_cat(&dehtml->strbuilder, text);
if( dehtml->add_text==DO_ADD_REMOVE_LINEENDS )
if (dehtml->add_text==DO_ADD_REMOVE_LINEENDS)
{
unsigned char* p = (unsigned char*)last_added;
while( *p ) {
if( *p=='\n' ) {
while (*p) {
if (*p=='\n') {
int last_is_lineend = 1; /* avoid converting `text1<br>\ntext2` to `text1\n text2` (`\r` is removed later) */
const unsigned char* p2 = p-1;
while( p2>=(const unsigned char*)dehtml->strbuilder.buf ) {
if( *p2 == '\r' ) {
while (p2>=(const unsigned char*)dehtml->strbuilder.buf) {
if (*p2 == '\r') {
}
else if( *p2 == '\n' ) {
else if (*p2 == '\n') {
break;
}
else {
@ -126,16 +126,16 @@ static void dehtml_endtag_cb(void* userdata, const char* tag)
{
dehtml_t* dehtml = (dehtml_t*)userdata;
if( strcmp(tag, "p")==0 || strcmp(tag, "div")==0 || strcmp(tag, "table")==0 || strcmp(tag, "td")==0
if (strcmp(tag, "p")==0 || strcmp(tag, "div")==0 || strcmp(tag, "table")==0 || strcmp(tag, "td")==0
|| strcmp(tag, "style")==0 || strcmp(tag, "script")==0 || strcmp(tag, "title")==0
|| strcmp(tag, "pre")==0 )
|| strcmp(tag, "pre")==0)
{
dc_strbuilder_cat(&dehtml->strbuilder, "\n\n"); /* do not expect an starting block element (which, of course, should come right now) */
dehtml->add_text = DO_ADD_REMOVE_LINEENDS;
}
else if( strcmp(tag, "a")==0 )
else if (strcmp(tag, "a")==0)
{
if( dehtml->last_href ) {
if (dehtml->last_href) {
dc_strbuilder_cat(&dehtml->strbuilder, "](");
dc_strbuilder_cat(&dehtml->strbuilder, dehtml->last_href);
dc_strbuilder_cat(&dehtml->strbuilder, ")");
@ -143,11 +143,11 @@ static void dehtml_endtag_cb(void* userdata, const char* tag)
dehtml->last_href = NULL;
}
}
else if( strcmp(tag, "b")==0 || strcmp(tag, "strong")==0 )
else if (strcmp(tag, "b")==0 || strcmp(tag, "strong")==0)
{
dc_strbuilder_cat(&dehtml->strbuilder, "*");
}
else if( strcmp(tag, "i")==0 || strcmp(tag, "em")==0 )
else if (strcmp(tag, "i")==0 || strcmp(tag, "em")==0)
{
dc_strbuilder_cat(&dehtml->strbuilder, "_");
}
@ -157,7 +157,7 @@ static void dehtml_endtag_cb(void* userdata, const char* tag)
char* dc_dehtml(char* buf_terminated)
{
dc_trim(buf_terminated);
if( buf_terminated[0] == 0 ) {
if (buf_terminated[0] == 0) {
return dc_strdup(""); /* support at least empty HTML-messages; for empty messages, we'll replace the message by the subject later */
}
else {

View file

@ -120,7 +120,7 @@ static struct mailmime* new_data_part(void* data, size_t data_bytes, char* defau
goto free_mime;
}
}*/
if( data!=NULL && data_bytes>0 && mime->mm_type == MAILMIME_SINGLE ) {
if (data!=NULL && data_bytes>0 && mime->mm_type == MAILMIME_SINGLE) {
mailmime_set_body_text(mime, data, data_bytes);
}
@ -161,24 +161,24 @@ static struct mailmime* new_data_part(void* data, size_t data_bytes, char* defau
*/
static int contains_report(struct mailmime* mime)
{
if( mime->mm_type == MAILMIME_MULTIPLE )
if (mime->mm_type == MAILMIME_MULTIPLE)
{
if( mime->mm_content_type->ct_type->tp_type==MAILMIME_TYPE_COMPOSITE_TYPE
if (mime->mm_content_type->ct_type->tp_type==MAILMIME_TYPE_COMPOSITE_TYPE
&& mime->mm_content_type->ct_type->tp_data.tp_composite_type->ct_type == MAILMIME_COMPOSITE_TYPE_MULTIPART
&& strcmp(mime->mm_content_type->ct_subtype, "report")==0 ) {
&& strcmp(mime->mm_content_type->ct_subtype, "report")==0) {
return 1;
}
clistiter* cur;
for( cur=clist_begin(mime->mm_data.mm_multipart.mm_mp_list); cur!=NULL; cur=clist_next(cur)) {
if( contains_report((struct mailmime*)clist_content(cur)) ) {
for (cur=clist_begin(mime->mm_data.mm_multipart.mm_mp_list); cur!=NULL; cur=clist_next(cur)) {
if (contains_report((struct mailmime*)clist_content(cur))) {
return 1;
}
}
}
else if( mime->mm_type == MAILMIME_MESSAGE )
else if (mime->mm_type == MAILMIME_MESSAGE)
{
if( contains_report(mime->mm_data.mm_message.mm_msg_mime) ) {
if (contains_report(mime->mm_data.mm_message.mm_msg_mime)) {
return 1;
}
}
@ -198,14 +198,14 @@ static int load_or_generate_self_public_key(dc_context_t* context, dc_key_t* pub
int key_created = 0;
int success = 0, key_creation_here = 0;
if( context == NULL || context->magic != DC_CONTEXT_MAGIC || public_key == NULL ) {
if (context == NULL || context->magic != DC_CONTEXT_MAGIC || public_key == NULL) {
goto cleanup;
}
if( !dc_key_load_self_public(public_key, self_addr, context->sql) )
if (!dc_key_load_self_public(public_key, self_addr, context->sql))
{
/* create the keypair - this may take a moment, however, as this is in a thread, this is no big deal */
if( s_in_key_creation ) { goto cleanup; }
if (s_in_key_creation) { goto cleanup; }
key_creation_here = 1;
s_in_key_creation = 1;
@ -218,10 +218,10 @@ static int load_or_generate_self_public_key(dc_context_t* context, dc_key_t* pub
seed[3] = (uintptr_t)pthread_self(); /* thread ID */
dc_pgp_rand_seed(context, seed, sizeof(seed));
if( random_data_mime ) {
if (random_data_mime) {
MMAPString* random_data_mmap = NULL;
int col = 0;
if( (random_data_mmap=mmap_string_new(""))==NULL ) {
if ((random_data_mmap=mmap_string_new(""))==NULL) {
goto cleanup;
}
mailmime_write_mem(random_data_mmap, &col, random_data_mime);
@ -241,21 +241,21 @@ static int load_or_generate_self_public_key(dc_context_t* context, dc_key_t* pub
- a self signature
- an encryption-capable subkey Ke
- a binding signature over Ke by Kp
(see https://autocrypt.readthedocs.io/en/latest/level0.html#type-p-openpgp-based-key-data )*/
(see https://autocrypt.readthedocs.io/en/latest/level0.html#type-p-openpgp-based-key-data)*/
key_created = dc_pgp_create_keypair(context, self_addr, public_key, private_key);
if( !key_created ) {
if (!key_created) {
dc_log_warning(context, 0, "Cannot create keypair.");
goto cleanup;
}
if( !dc_pgp_is_valid_key(context, public_key)
|| !dc_pgp_is_valid_key(context, private_key) ) {
if (!dc_pgp_is_valid_key(context, public_key)
|| !dc_pgp_is_valid_key(context, private_key)) {
dc_log_warning(context, 0, "Generated keys are not valid.");
goto cleanup;
}
if( !dc_key_save_self_keypair(public_key, private_key, self_addr, 1/*set default*/, context->sql) ) {
if (!dc_key_save_self_keypair(public_key, private_key, self_addr, 1/*set default*/, context->sql)) {
dc_log_warning(context, 0, "Cannot save keypair.");
goto cleanup;
}
@ -269,7 +269,7 @@ static int load_or_generate_self_public_key(dc_context_t* context, dc_key_t* pub
success = 1;
cleanup:
if( key_creation_here ) { s_in_key_creation = 0; }
if (key_creation_here) { s_in_key_creation = 0; }
return success;
}
@ -282,16 +282,16 @@ int dc_ensure_secret_key_exists(dc_context_t* context)
dc_key_t* public_key = dc_key_new();
char* self_addr = NULL;
if( context==NULL || context->magic != DC_CONTEXT_MAGIC || public_key==NULL ) {
if (context==NULL || context->magic != DC_CONTEXT_MAGIC || public_key==NULL) {
goto cleanup;
}
if( (self_addr=dc_sqlite3_get_config(context->sql, "configured_addr", NULL))==NULL ) {
if ((self_addr=dc_sqlite3_get_config(context->sql, "configured_addr", NULL))==NULL) {
dc_log_warning(context, 0, "Cannot ensure secret key if context is not configured.");
goto cleanup;
}
if( !load_or_generate_self_public_key(context, public_key, self_addr, NULL/*no random text data for seeding available*/) ) {
if (!load_or_generate_self_public_key(context, public_key, self_addr, NULL/*no random text data for seeding available*/)) {
goto cleanup;
}
@ -325,45 +325,45 @@ void dc_e2ee_encrypt(dc_context_t* context, const clist* recipients_addr,
size_t ctext_bytes = 0;
dc_array_t* peerstates = dc_array_new(NULL, 10);
if( helper ) { memset(helper, 0, sizeof(dc_e2ee_helper_t)); }
if (helper) { memset(helper, 0, sizeof(dc_e2ee_helper_t)); }
if( context == NULL || context->magic != DC_CONTEXT_MAGIC || recipients_addr == NULL || in_out_message == NULL
if (context == NULL || context->magic != DC_CONTEXT_MAGIC || recipients_addr == NULL || in_out_message == NULL
|| in_out_message->mm_parent /* libEtPan's pgp_encrypt_mime() takes the parent as the new root. We just expect the root as being given to this function. */
|| autocryptheader == NULL || keyring==NULL || sign_key==NULL || plain == NULL || helper == NULL ) {
|| autocryptheader == NULL || keyring==NULL || sign_key==NULL || plain == NULL || helper == NULL) {
goto cleanup;
}
/* init autocrypt header from db */
autocryptheader->prefer_encrypt = DC_PE_NOPREFERENCE;
if( context->e2ee_enabled ) {
if (context->e2ee_enabled) {
autocryptheader->prefer_encrypt = DC_PE_MUTUAL;
}
autocryptheader->addr = dc_sqlite3_get_config(context->sql, "configured_addr", NULL);
if( autocryptheader->addr == NULL ) {
if (autocryptheader->addr == NULL) {
goto cleanup;
}
if( !load_or_generate_self_public_key(context, autocryptheader->public_key, autocryptheader->addr, in_out_message/*only for random-seed*/) ) {
if (!load_or_generate_self_public_key(context, autocryptheader->public_key, autocryptheader->addr, in_out_message/*only for random-seed*/)) {
goto cleanup;
}
/* load peerstate information etc. */
if( autocryptheader->prefer_encrypt==DC_PE_MUTUAL || e2ee_guaranteed )
if (autocryptheader->prefer_encrypt==DC_PE_MUTUAL || e2ee_guaranteed)
{
do_encrypt = 1;
clistiter* iter1;
for( iter1 = clist_begin(recipients_addr); iter1!=NULL ; iter1=clist_next(iter1) ) {
for (iter1 = clist_begin(recipients_addr); iter1!=NULL ; iter1=clist_next(iter1)) {
const char* recipient_addr = clist_content(iter1);
dc_apeerstate_t* peerstate = dc_apeerstate_new(context);
dc_key_t* key_to_use = NULL;
if( strcasecmp(recipient_addr, autocryptheader->addr) == 0 )
if (strcasecmp(recipient_addr, autocryptheader->addr) == 0)
{
; // encrypt to SELF, this key is added below
}
else if( dc_apeerstate_load_by_addr(peerstate, context->sql, recipient_addr)
else if (dc_apeerstate_load_by_addr(peerstate, context->sql, recipient_addr)
&& (key_to_use=dc_apeerstate_peek_key(peerstate, min_verified)) != NULL
&& (peerstate->prefer_encrypt==DC_PE_MUTUAL || e2ee_guaranteed) )
&& (peerstate->prefer_encrypt==DC_PE_MUTUAL || e2ee_guaranteed))
{
dc_keyring_add(keyring, key_to_use); /* we always add all recipients (even on IMAP upload) as otherwise forwarding may fail */
dc_array_add_ptr(peerstates, peerstate);
@ -377,23 +377,23 @@ void dc_e2ee_encrypt(dc_context_t* context, const clist* recipients_addr,
}
}
if( do_encrypt ) {
if (do_encrypt) {
dc_keyring_add(keyring, autocryptheader->public_key); /* we always add ourself as otherwise forwarded messages are not readable */
if( !dc_key_load_self_private(sign_key, autocryptheader->addr, context->sql) ) {
if (!dc_key_load_self_private(sign_key, autocryptheader->addr, context->sql)) {
do_encrypt = 0;
}
}
if( force_unencrypted ) {
if (force_unencrypted) {
do_encrypt = 0;
}
if( (imffields_unprotected=mailmime_find_mailimf_fields(in_out_message))==NULL ) {
if ((imffields_unprotected=mailmime_find_mailimf_fields(in_out_message))==NULL) {
goto cleanup;
}
/* encrypt message, if possible */
if( do_encrypt )
if (do_encrypt)
{
/* prepare part to encrypt */
mailprivacy_prepare_mime(in_out_message); /* encode quoted printable all text parts */
@ -406,10 +406,10 @@ void dc_e2ee_encrypt(dc_context_t* context, const clist* recipients_addr,
/* gossip keys */
int iCnt = dc_array_get_cnt(peerstates);
if( iCnt > 1 ) {
for( int i = 0; i < iCnt; i++ ) {
if (iCnt > 1) {
for (int i = 0; i < iCnt; i++) {
char* p = dc_apeerstate_render_gossip_header((dc_apeerstate_t*)dc_array_get_ptr(peerstates, i), min_verified);
if( p ) {
if (p) {
mailimf_fields_add(imffields_encrypted, mailimf_field_new_custom(strdup("Autocrypt-Gossip"), p/*takes ownership*/));
}
}
@ -417,26 +417,26 @@ void dc_e2ee_encrypt(dc_context_t* context, const clist* recipients_addr,
/* memoryhole headers */
clistiter* cur = clist_begin(imffields_unprotected->fld_list);
while( cur!=NULL ) {
while (cur!=NULL) {
int move_to_encrypted = 0;
struct mailimf_field* field = (struct mailimf_field*)clist_content(cur);
if( field ) {
if( field->fld_type == MAILIMF_FIELD_SUBJECT ) {
if (field) {
if (field->fld_type == MAILIMF_FIELD_SUBJECT) {
move_to_encrypted = 1;
}
else if( field->fld_type == MAILIMF_FIELD_OPTIONAL_FIELD ) {
else if (field->fld_type == MAILIMF_FIELD_OPTIONAL_FIELD) {
struct mailimf_optional_field* opt_field = field->fld_data.fld_optional_field;
if( opt_field && opt_field->fld_name ) {
if( strncmp(opt_field->fld_name, "Secure-Join", 11)==0
|| (strncmp(opt_field->fld_name, "Chat-", 5)==0 && strcmp(opt_field->fld_name, "Chat-Version")!=0)/*Chat-Version may be used for filtering and is not added to the encrypted part, however, this is subject to change*/ ) {
if (opt_field && opt_field->fld_name) {
if ( strncmp(opt_field->fld_name, "Secure-Join", 11)==0
|| (strncmp(opt_field->fld_name, "Chat-", 5)==0 && strcmp(opt_field->fld_name, "Chat-Version")!=0)/*Chat-Version may be used for filtering and is not added to the encrypted part, however, this is subject to change*/) {
move_to_encrypted = 1;
}
}
}
}
if( move_to_encrypted ) {
if (move_to_encrypted) {
mailimf_fields_add(imffields_encrypted, field);
cur = clist_delete(imffields_unprotected->fld_list, cur);
}
@ -454,12 +454,12 @@ void dc_e2ee_encrypt(dc_context_t* context, const clist* recipients_addr,
/* convert part to encrypt to plain text */
mailmime_write_mem(plain, &col, message_to_encrypt);
if( plain->str == NULL || plain->len<=0 ) {
if (plain->str == NULL || plain->len<=0) {
goto cleanup;
}
//char* t1=dc_null_terminate(plain->str,plain->len);printf("PLAIN:\n%s\n",t1);free(t1); // DEBUG OUTPUT
if( !dc_pgp_pk_encrypt(context, plain->str, plain->len, keyring, sign_key, 1/*use_armor*/, (void**)&ctext, &ctext_bytes) ) {
if (!dc_pgp_pk_encrypt(context, plain->str, plain->len, keyring, sign_key, 1/*use_armor*/, (void**)&ctext, &ctext_bytes)) {
goto cleanup;
}
helper->cdata_to_free = ctext;
@ -488,7 +488,7 @@ void dc_e2ee_encrypt(dc_context_t* context, const clist* recipients_addr,
}
char* p = dc_aheader_render(autocryptheader);
if( p == NULL ) {
if (p == NULL) {
goto cleanup;
}
mailimf_fields_add(imffields_unprotected, mailimf_field_new_custom(strdup("Autocrypt"), p/*takes ownership of pointer*/));
@ -497,30 +497,30 @@ cleanup:
dc_aheader_unref(autocryptheader);
dc_keyring_unref(keyring);
dc_key_unref(sign_key);
if( plain ) { mmap_string_free(plain); }
if (plain) { mmap_string_free(plain); }
for( int i=dc_array_get_cnt(peerstates)-1; i>=0; i-- ) { dc_apeerstate_unref((dc_apeerstate_t*)dc_array_get_ptr(peerstates, i)); }
for (int i=dc_array_get_cnt(peerstates)-1; i>=0; i--) { dc_apeerstate_unref((dc_apeerstate_t*)dc_array_get_ptr(peerstates, i)); }
dc_array_unref(peerstates);
}
void dc_e2ee_thanks(dc_e2ee_helper_t* helper)
{
if( helper == NULL ) {
if (helper == NULL) {
return;
}
free(helper->cdata_to_free);
helper->cdata_to_free = NULL;
if( helper->gossipped_addr )
if (helper->gossipped_addr)
{
dc_hash_clear(helper->gossipped_addr);
free(helper->gossipped_addr);
helper->gossipped_addr = NULL;
}
if( helper->signatures )
if (helper->signatures)
{
dc_hash_clear(helper->signatures);
free(helper->signatures);
@ -537,14 +537,14 @@ void dc_e2ee_thanks(dc_e2ee_helper_t* helper)
static int has_decrypted_pgp_armor(const char* str__, int str_bytes)
{
const unsigned char *str_end = (const unsigned char*)str__+str_bytes, *p=(const unsigned char*)str__;
while( p < str_end ) {
if( *p > ' ' ) {
while (p < str_end) {
if (*p > ' ') {
break;
}
p++;
str_bytes--;
}
if( str_bytes>27 && strncmp((const char*)p, "-----BEGIN PGP MESSAGE-----", 27)==0 ) {
if (str_bytes>27 && strncmp((const char*)p, "-----BEGIN PGP MESSAGE-----", 27)==0) {
return 1;
}
return 0;
@ -571,19 +571,19 @@ static int decrypt_part(dc_context_t* context,
/* get data pointer from `mime` */
mime_data = mime->mm_data.mm_single;
if( mime_data->dt_type != MAILMIME_DATA_TEXT /* MAILMIME_DATA_FILE indicates, the data is in a file; AFAIK this is not used on parsing */
if (mime_data->dt_type != MAILMIME_DATA_TEXT /* MAILMIME_DATA_FILE indicates, the data is in a file; AFAIK this is not used on parsing */
|| mime_data->dt_data.dt_text.dt_data == NULL
|| mime_data->dt_data.dt_text.dt_length <= 0 ) {
|| mime_data->dt_data.dt_text.dt_length <= 0) {
goto cleanup;
}
/* check headers in `mime` */
if( mime->mm_mime_fields != NULL ) {
if (mime->mm_mime_fields != NULL) {
clistiter* cur;
for( cur = clist_begin(mime->mm_mime_fields->fld_list); cur != NULL; cur = clist_next(cur) ) {
for (cur = clist_begin(mime->mm_mime_fields->fld_list); cur != NULL; cur = clist_next(cur)) {
struct mailmime_field* field = (struct mailmime_field*)clist_content(cur);
if( field ) {
if( field->fld_type == MAILMIME_FIELD_TRANSFER_ENCODING && field->fld_data.fld_encoding ) {
if (field) {
if (field->fld_type == MAILMIME_FIELD_TRANSFER_ENCODING && field->fld_data.fld_encoding) {
mime_transfer_encoding = field->fld_data.fld_encoding->enc_type;
}
}
@ -591,13 +591,13 @@ static int decrypt_part(dc_context_t* context,
}
/* regard `Content-Transfer-Encoding:` */
if( mime_transfer_encoding == MAILMIME_MECHANISM_7BIT
if (mime_transfer_encoding == MAILMIME_MECHANISM_7BIT
|| mime_transfer_encoding == MAILMIME_MECHANISM_8BIT
|| mime_transfer_encoding == MAILMIME_MECHANISM_BINARY )
|| mime_transfer_encoding == MAILMIME_MECHANISM_BINARY)
{
decoded_data = mime_data->dt_data.dt_text.dt_data;
decoded_data_bytes = mime_data->dt_data.dt_text.dt_length;
if( decoded_data == NULL || decoded_data_bytes <= 0 ) {
if (decoded_data == NULL || decoded_data_bytes <= 0) {
goto cleanup; /* no error - but no data */
}
}
@ -608,22 +608,22 @@ static int decrypt_part(dc_context_t* context,
r = mailmime_part_parse(mime_data->dt_data.dt_text.dt_data, mime_data->dt_data.dt_text.dt_length,
&current_index, mime_transfer_encoding,
&transfer_decoding_buffer, &decoded_data_bytes);
if( r != MAILIMF_NO_ERROR || transfer_decoding_buffer == NULL || decoded_data_bytes <= 0 ) {
if (r != MAILIMF_NO_ERROR || transfer_decoding_buffer == NULL || decoded_data_bytes <= 0) {
goto cleanup;
}
decoded_data = transfer_decoding_buffer;
}
/* encrypted, decoded data in decoded_data now ... */
if( !has_decrypted_pgp_armor(decoded_data, decoded_data_bytes) ) {
if (!has_decrypted_pgp_armor(decoded_data, decoded_data_bytes)) {
goto cleanup;
}
dc_hash_t* add_signatures = dc_hash_count(ret_valid_signatures)<=0?
ret_valid_signatures : NULL; /*if we already have fingerprints, do not add more; this ensures, only the fingerprints from the outer-most part are collected */
if( !dc_pgp_pk_decrypt(context, decoded_data, decoded_data_bytes, private_keyring, public_keyring_for_validate, 1, &plain_buf, &plain_bytes, add_signatures)
|| plain_buf==NULL || plain_bytes<=0 ) {
if (!dc_pgp_pk_decrypt(context, decoded_data, decoded_data_bytes, private_keyring, public_keyring_for_validate, 1, &plain_buf, &plain_bytes, add_signatures)
|| plain_buf==NULL || plain_bytes<=0) {
goto cleanup;
}
@ -632,8 +632,8 @@ static int decrypt_part(dc_context_t* context,
{
size_t index = 0;
struct mailmime* decrypted_mime = NULL;
if( mailmime_parse(plain_buf, plain_bytes, &index, &decrypted_mime)!=MAIL_NO_ERROR
|| decrypted_mime == NULL ) {
if (mailmime_parse(plain_buf, plain_bytes, &index, &decrypted_mime)!=MAIL_NO_ERROR
|| decrypted_mime == NULL) {
if(decrypted_mime) {mailmime_free(decrypted_mime);}
goto cleanup;
}
@ -648,7 +648,7 @@ static int decrypt_part(dc_context_t* context,
//s. mailprivacy_gnupg.c::pgp_decrypt()
cleanup:
if( transfer_decoding_buffer ) {
if (transfer_decoding_buffer) {
mmap_string_unref(transfer_decoding_buffer);
}
return sth_decrypted;
@ -661,33 +661,33 @@ static int decrypt_recursive(dc_context_t* context,
const dc_keyring_t* public_keyring_for_validate,
dc_hash_t* ret_valid_signatures,
struct mailimf_fields** ret_gossip_headers,
int* ret_has_unencrypted_parts )
int* ret_has_unencrypted_parts)
{
struct mailmime_content* ct;
clistiter* cur;
if( context == NULL || mime == NULL ) {
if (context == NULL || mime == NULL) {
return 0;
}
if( mime->mm_type == MAILMIME_MULTIPLE )
if (mime->mm_type == MAILMIME_MULTIPLE)
{
ct = mime->mm_content_type;
if( ct && ct->ct_subtype && strcmp(ct->ct_subtype, "encrypted")==0 ) {
if (ct && ct->ct_subtype && strcmp(ct->ct_subtype, "encrypted")==0) {
/* decrypt "multipart/encrypted" -- child parts are eg. "application/pgp-encrypted" (uninteresting, version only),
"application/octet-stream" (the interesting data part) and optional, unencrypted help files */
for( cur=clist_begin(mime->mm_data.mm_multipart.mm_mp_list); cur!=NULL; cur=clist_next(cur)) {
for (cur=clist_begin(mime->mm_data.mm_multipart.mm_mp_list); cur!=NULL; cur=clist_next(cur)) {
struct mailmime* decrypted_mime = NULL;
if( decrypt_part(context, (struct mailmime*)clist_content(cur), private_keyring, public_keyring_for_validate, ret_valid_signatures, &decrypted_mime) )
if (decrypt_part(context, (struct mailmime*)clist_content(cur), private_keyring, public_keyring_for_validate, ret_valid_signatures, &decrypted_mime))
{
/* remember the header containing potentially Autocrypt-Gossip */
if( *ret_gossip_headers == NULL /* use the outermost decrypted part */
&& dc_hash_count(ret_valid_signatures) > 0 /* do not trust the gossipped keys when the message cannot be validated eg. due to a bad signature */ )
if (*ret_gossip_headers == NULL /* use the outermost decrypted part */
&& dc_hash_count(ret_valid_signatures) > 0 /* do not trust the gossipped keys when the message cannot be validated eg. due to a bad signature */)
{
size_t dummy = 0;
struct mailimf_fields* test = NULL;
if( mailimf_envelope_and_optional_fields_parse(decrypted_mime->mm_mime_start, decrypted_mime->mm_length, &dummy, &test)==MAILIMF_NO_ERROR
&& test ) {
if (mailimf_envelope_and_optional_fields_parse(decrypted_mime->mm_mime_start, decrypted_mime->mm_length, &dummy, &test)==MAILIMF_NO_ERROR
&& test) {
*ret_gossip_headers = test;
}
}
@ -701,16 +701,16 @@ static int decrypt_recursive(dc_context_t* context,
*ret_has_unencrypted_parts = 1; // there is a part that could not be decrypted
}
else {
for( cur=clist_begin(mime->mm_data.mm_multipart.mm_mp_list); cur!=NULL; cur=clist_next(cur)) {
if( decrypt_recursive(context, (struct mailmime*)clist_content(cur), private_keyring, public_keyring_for_validate, ret_valid_signatures, ret_gossip_headers, ret_has_unencrypted_parts) ) {
for (cur=clist_begin(mime->mm_data.mm_multipart.mm_mp_list); cur!=NULL; cur=clist_next(cur)) {
if (decrypt_recursive(context, (struct mailmime*)clist_content(cur), private_keyring, public_keyring_for_validate, ret_valid_signatures, ret_gossip_headers, ret_has_unencrypted_parts)) {
return 1; /* sth. decrypted, start over from root searching for encrypted parts */
}
}
}
}
else if( mime->mm_type == MAILMIME_MESSAGE )
else if (mime->mm_type == MAILMIME_MESSAGE)
{
if( decrypt_recursive(context, mime->mm_data.mm_message.mm_msg_mime, private_keyring, public_keyring_for_validate, ret_valid_signatures, ret_gossip_headers, ret_has_unencrypted_parts) ) {
if (decrypt_recursive(context, mime->mm_data.mm_message.mm_msg_mime, private_keyring, public_keyring_for_validate, ret_valid_signatures, ret_gossip_headers, ret_has_unencrypted_parts)) {
return 1; /* sth. decrypted, start over from root searching for encrypted parts */
}
}
@ -729,28 +729,28 @@ static dc_hash_t* update_gossip_peerstates(dc_context_t* context, time_t message
dc_hash_t* recipients = NULL;
dc_hash_t* gossipped_addr = NULL;
for( cur1 = clist_begin(gossip_headers->fld_list); cur1!=NULL ; cur1=clist_next(cur1) )
for (cur1 = clist_begin(gossip_headers->fld_list); cur1!=NULL ; cur1=clist_next(cur1))
{
struct mailimf_field* field = (struct mailimf_field*)clist_content(cur1);
if( field->fld_type == MAILIMF_FIELD_OPTIONAL_FIELD )
if (field->fld_type == MAILIMF_FIELD_OPTIONAL_FIELD)
{
const struct mailimf_optional_field* optional_field = field->fld_data.fld_optional_field;
if( optional_field && optional_field->fld_name && strcasecmp(optional_field->fld_name, "Autocrypt-Gossip")==0 )
if (optional_field && optional_field->fld_name && strcasecmp(optional_field->fld_name, "Autocrypt-Gossip")==0)
{
dc_aheader_t* gossip_header = dc_aheader_new();
if( dc_aheader_set_from_string(gossip_header, optional_field->fld_value)
&& dc_pgp_is_valid_key(context, gossip_header->public_key) )
if (dc_aheader_set_from_string(gossip_header, optional_field->fld_value)
&& dc_pgp_is_valid_key(context, gossip_header->public_key))
{
/* found an Autocrypt-Gossip entry, create recipents list and check if addr matches */
if( recipients == NULL ) {
if (recipients == NULL) {
recipients = mailimf_get_recipients(imffields);
}
if( dc_hash_find(recipients, gossip_header->addr, strlen(gossip_header->addr)) )
if (dc_hash_find(recipients, gossip_header->addr, strlen(gossip_header->addr)))
{
/* valid recipient: update peerstate */
dc_apeerstate_t* peerstate = dc_apeerstate_new(context);
if( !dc_apeerstate_load_by_addr(peerstate, context->sql, gossip_header->addr) ) {
if (!dc_apeerstate_load_by_addr(peerstate, context->sql, gossip_header->addr)) {
dc_apeerstate_init_from_gossip(peerstate, gossip_header, message_time);
dc_apeerstate_save_to_db(peerstate, context->sql, 1/*create*/);
}
@ -759,7 +759,7 @@ static dc_hash_t* update_gossip_peerstates(dc_context_t* context, time_t message
dc_apeerstate_save_to_db(peerstate, context->sql, 0/*do not create*/);
}
if( peerstate->degrade_event ) {
if (peerstate->degrade_event) {
dc_handle_degrade_event(context, peerstate);
}
@ -767,7 +767,7 @@ static dc_hash_t* update_gossip_peerstates(dc_context_t* context, time_t message
// collect all gossipped addresses; we need them later to mark them as being
// verified when used in a verified group by a verified sender
if( gossipped_addr == NULL ) {
if (gossipped_addr == NULL) {
gossipped_addr = malloc(sizeof(dc_hash_t));
dc_hash_init(gossipped_addr, DC_HASH_STRING, 1/*copy key*/);
}
@ -783,7 +783,7 @@ static dc_hash_t* update_gossip_peerstates(dc_context_t* context, time_t message
}
}
if( recipients ) {
if (recipients) {
dc_hash_clear(recipients);
free(recipients);
}
@ -806,10 +806,10 @@ void dc_e2ee_decrypt(dc_context_t* context, struct mailmime* in_out_message,
dc_keyring_t* public_keyring_for_validate = dc_keyring_new();
struct mailimf_fields* gossip_headers = NULL;
if( helper ) { memset(helper, 0, sizeof(dc_e2ee_helper_t)); }
if (helper) { memset(helper, 0, sizeof(dc_e2ee_helper_t)); }
if( context==NULL || context->magic != DC_CONTEXT_MAGIC || in_out_message==NULL
|| helper == NULL || imffields==NULL ) {
if (context==NULL || context->magic != DC_CONTEXT_MAGIC || in_out_message==NULL
|| helper == NULL || imffields==NULL) {
goto cleanup;
}
@ -817,19 +817,19 @@ void dc_e2ee_decrypt(dc_context_t* context, struct mailmime* in_out_message,
- Set message_time and from (both may be unset)
- Get the autocrypt header, if any.
- Do not abort on errors - we should try at last the decyption below */
if( imffields )
if (imffields)
{
struct mailimf_field* field = mailimf_find_field(imffields, MAILIMF_FIELD_FROM);
if( field && field->fld_data.fld_from ) {
if (field && field->fld_data.fld_from) {
from = mailimf_find_first_addr(field->fld_data.fld_from->frm_mb_list);
}
field = mailimf_find_field(imffields, MAILIMF_FIELD_ORIG_DATE);
if( field && field->fld_data.fld_orig_date ) {
if (field && field->fld_data.fld_orig_date) {
struct mailimf_orig_date* orig_date = field->fld_data.fld_orig_date;
if( orig_date ) {
if (orig_date) {
message_time = dc_timestamp_from_date(orig_date->dt_date_time); /* is not yet checked against bad times! */
if( message_time != DC_INVALID_TIMESTAMP && message_time > time(NULL) ) {
if (message_time != DC_INVALID_TIMESTAMP && message_time > time(NULL)) {
message_time = time(NULL);
}
}
@ -837,8 +837,8 @@ void dc_e2ee_decrypt(dc_context_t* context, struct mailmime* in_out_message,
}
autocryptheader = dc_aheader_new_from_imffields(from, imffields);
if( autocryptheader ) {
if( !dc_pgp_is_valid_key(context, autocryptheader->public_key) ) {
if (autocryptheader) {
if (!dc_pgp_is_valid_key(context, autocryptheader->public_key)) {
dc_aheader_unref(autocryptheader);
autocryptheader = NULL;
}
@ -847,43 +847,43 @@ void dc_e2ee_decrypt(dc_context_t* context, struct mailmime* in_out_message,
/* modify the peerstate (eg. if there is a peer but not autocrypt header, stop encryption) */
/* apply Autocrypt:-header */
if( message_time > 0
&& from )
if (message_time > 0
&& from)
{
if( dc_apeerstate_load_by_addr(peerstate, context->sql, from) ) {
if( autocryptheader ) {
if (dc_apeerstate_load_by_addr(peerstate, context->sql, from)) {
if (autocryptheader) {
dc_apeerstate_apply_header(peerstate, autocryptheader, message_time);
dc_apeerstate_save_to_db(peerstate, context->sql, 0/*no not create*/);
}
else {
if( message_time > peerstate->last_seen_autocrypt
&& !contains_report(in_out_message) /*reports are ususally not encrpyted; do not degrade decryption then*/ ){
if (message_time > peerstate->last_seen_autocrypt
&& !contains_report(in_out_message) /*reports are ususally not encrpyted; do not degrade decryption then*/){
dc_apeerstate_degrade_encryption(peerstate, message_time);
dc_apeerstate_save_to_db(peerstate, context->sql, 0/*no not create*/);
}
}
}
else if( autocryptheader ) {
else if (autocryptheader) {
dc_apeerstate_init_from_header(peerstate, autocryptheader, message_time);
dc_apeerstate_save_to_db(peerstate, context->sql, 1/*create*/);
}
}
/* load private key for decryption */
if( (self_addr=dc_sqlite3_get_config(context->sql, "configured_addr", NULL))==NULL ) {
if ((self_addr=dc_sqlite3_get_config(context->sql, "configured_addr", NULL))==NULL) {
goto cleanup;
}
if( !dc_keyring_load_self_private_for_decrypting(private_keyring, self_addr, context->sql) ) {
if (!dc_keyring_load_self_private_for_decrypting(private_keyring, self_addr, context->sql)) {
goto cleanup;
}
/* if not yet done, load peer with public key for verification (should be last as the peer may be modified above) */
if( peerstate->last_seen == 0 ) {
if (peerstate->last_seen == 0) {
dc_apeerstate_load_by_addr(peerstate, context->sql, from);
}
if( peerstate->degrade_event ) {
if (peerstate->degrade_event) {
dc_handle_degrade_event(context, peerstate);
}
@ -897,19 +897,19 @@ void dc_e2ee_decrypt(dc_context_t* context, struct mailmime* in_out_message,
dc_hash_init(helper->signatures, DC_HASH_STRING, 1/*copy key*/);
int iterations = 0;
while( iterations < 10 ) {
while (iterations < 10) {
int has_unencrypted_parts = 0;
if( !decrypt_recursive(context, in_out_message, private_keyring,
if (!decrypt_recursive(context, in_out_message, private_keyring,
public_keyring_for_validate,
helper->signatures, &gossip_headers, &has_unencrypted_parts) ) {
helper->signatures, &gossip_headers, &has_unencrypted_parts)) {
break;
}
// if we're here, sth. was encrypted. if we're on top-level, and there are no
// additional unencrypted parts in the message the encryption was fine
// (signature is handled separately and returned as `signatures`)
if( iterations == 0
&& !has_unencrypted_parts ) {
if (iterations == 0
&& !has_unencrypted_parts) {
helper->encrypted = 1;
}
@ -917,14 +917,14 @@ void dc_e2ee_decrypt(dc_context_t* context, struct mailmime* in_out_message,
}
/* check for Autocrypt-Gossip */
if( gossip_headers ) {
if (gossip_headers) {
helper->gossipped_addr = update_gossip_peerstates(context, message_time, imffields, gossip_headers);
}
//mailmime_print(in_out_message);
cleanup:
if( gossip_headers ) { mailimf_fields_free(gossip_headers); }
if (gossip_headers) { mailimf_fields_free(gossip_headers); }
dc_aheader_unref(autocryptheader);
dc_apeerstate_unref(peerstate);
dc_keyring_unref(private_keyring);

View file

@ -39,7 +39,7 @@
*/
#define Addr(X) ((uintptr_t)X)
static void* sjhashMalloc(long bytes) { void* p=malloc(bytes); if( p) memset(p, 0, bytes); return p; }
static void* sjhashMalloc(long bytes) { void* p=malloc(bytes); if (p) memset(p, 0, bytes); return p; }
#define sjhashMallocRaw(a) malloc((a))
#define sjhashFree(a) free((a))
@ -76,7 +76,7 @@ static int sjhashStrNICmp(const char *zLeft, const char *zRight, int N)
register unsigned char *a, *b;
a = (unsigned char *)zLeft;
b = (unsigned char *)zRight;
while( N-- > 0 && *a!=0 && sjhashUpperToLower[*a]==sjhashUpperToLower[*b]) { a++; b++; }
while (N-- > 0 && *a!=0 && sjhashUpperToLower[*a]==sjhashUpperToLower[*b]) { a++; b++; }
return N<0 ? 0 : sjhashUpperToLower[*a] - sjhashUpperToLower[*b];
}
@ -88,8 +88,8 @@ static int sjhashStrNICmp(const char *zLeft, const char *zRight, int N)
static int sjhashNoCase(const char *z, int n)
{
int h = 0;
if( n<=0 ) n = strlen(z);
while( n > 0 ) {
if (n<=0) n = strlen(z);
while (n > 0) {
h = (h<<3) ^ h ^ sjhashUpperToLower[(unsigned char)*z++];
n--;
}
@ -112,11 +112,11 @@ static int sjhashNoCase(const char *z, int n)
*/
void dc_hash_init(dc_hash_t *pNew, int keyClass, int copyKey)
{
assert( pNew!=0 );
assert( keyClass>=DC_HASH_INT && keyClass<=DC_HASH_BINARY );
assert( pNew!=0);
assert( keyClass>=DC_HASH_INT && keyClass<=DC_HASH_BINARY);
pNew->keyClass = keyClass;
if( keyClass==DC_HASH_POINTER || keyClass==DC_HASH_INT ) copyKey = 0;
if (keyClass==DC_HASH_POINTER || keyClass==DC_HASH_INT) copyKey = 0;
pNew->copyKey = copyKey;
pNew->first = 0;
@ -135,19 +135,19 @@ void dc_hash_clear(dc_hash_t *pH)
{
dc_hashelem_t *elem; /* For looping over all elements of the table */
if( pH == NULL ) {
if (pH == NULL) {
return;
}
elem = pH->first;
pH->first = 0;
if( pH->ht ) sjhashFree(pH->ht);
if (pH->ht) sjhashFree(pH->ht);
pH->ht = 0;
pH->htsize = 0;
while( elem )
while (elem)
{
dc_hashelem_t *next_elem = elem->next;
if( pH->copyKey && elem->pKey )
if (pH->copyKey && elem->pKey)
{
sjhashFree(elem->pKey);
}
@ -183,8 +183,8 @@ static int ptrHash(const void *pKey, int nKey)
static int ptrCompare(const void *pKey1, int n1, const void *pKey2, int n2)
{
if( pKey1==pKey2 ) return 0;
if( pKey1<pKey2 ) return -1;
if (pKey1==pKey2) return 0;
if (pKey1<pKey2) return -1;
return 1;
}
@ -199,7 +199,7 @@ static int strHash(const void *pKey, int nKey)
static int strCompare(const void *pKey1, int n1, const void *pKey2, int n2)
{
if( n1!=n2 ) return 1;
if (n1!=n2) return 1;
return sjhashStrNICmp((const char*)pKey1,(const char*)pKey2,n1);
}
@ -211,7 +211,7 @@ static int binHash(const void *pKey, int nKey)
{
int h = 0;
const char *z = (const char *)pKey;
while( nKey-- > 0 )
while (nKey-- > 0)
{
h = (h<<3) ^ h ^ *(z++);
}
@ -220,7 +220,7 @@ static int binHash(const void *pKey, int nKey)
static int binCompare(const void *pKey1, int n1, const void *pKey2, int n2)
{
if( n1!=n2 ) return 1;
if (n1!=n2) return 1;
return memcmp(pKey1,pKey2,n1);
}
@ -237,7 +237,7 @@ static int binCompare(const void *pKey1, int n1, const void *pKey2, int n2)
*/
static int (*hashFunction(int keyClass))(const void*,int)
{
switch( keyClass )
switch (keyClass)
{
case DC_HASH_INT: return &intHash;
case DC_HASH_POINTER:return &ptrHash;
@ -254,7 +254,7 @@ static int (*hashFunction(int keyClass))(const void*,int)
*/
static int (*compareFunction(int keyClass))(const void*,int,const void*,int)
{
switch( keyClass )
switch (keyClass)
{
case DC_HASH_INT: return &intCompare;
case DC_HASH_POINTER: return &ptrCompare;
@ -275,18 +275,18 @@ static void insertElement(dc_hash_t *pH, /* The complete hash table */
{
dc_hashelem_t *pHead; /* First element already in pEntry */
pHead = pEntry->chain;
if( pHead )
if (pHead)
{
pNew->next = pHead;
pNew->prev = pHead->prev;
if( pHead->prev ) { pHead->prev->next = pNew; }
if (pHead->prev) { pHead->prev->next = pNew; }
else { pH->first = pNew; }
pHead->prev = pNew;
}
else
{
pNew->next = pH->first;
if( pH->first ) { pH->first->prev = pNew; }
if (pH->first) { pH->first->prev = pNew; }
pNew->prev = 0;
pH->first = pNew;
}
@ -306,10 +306,10 @@ static void rehash(dc_hash_t *pH, int new_size)
dc_hashelem_t *elem, *next_elem; /* For looping over existing elements */
int (*xHash)(const void*,int); /* The hash function */
assert( (new_size & (new_size-1))==0 );
new_ht = (struct _ht *)sjhashMalloc( new_size*sizeof(struct _ht) );
if( new_ht==0 ) return;
if( pH->ht ) sjhashFree(pH->ht);
assert( (new_size & (new_size-1))==0);
new_ht = (struct _ht *)sjhashMalloc( new_size*sizeof(struct _ht));
if (new_ht==0) return;
if (pH->ht) sjhashFree(pH->ht);
pH->ht = new_ht;
pH->htsize = new_size;
xHash = hashFunction(pH->keyClass);
@ -336,15 +336,15 @@ static dc_hashelem_t *findElementGivenHash(const dc_hash_t *pH, /* The pH to b
int count; /* Number of elements left to test */
int (*xCompare)(const void*,int,const void*,int); /* comparison function */
if( pH->ht )
if (pH->ht)
{
struct _ht *pEntry = &pH->ht[h];
elem = pEntry->chain;
count = pEntry->count;
xCompare = compareFunction(pH->keyClass);
while( count-- && elem )
while (count-- && elem)
{
if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 )
if ((*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0)
{
return elem;
}
@ -365,7 +365,7 @@ static void removeElementGivenHash(dc_hash_t *pH, /* The pH containing "
{
struct _ht *pEntry;
if( elem->prev )
if (elem->prev)
{
elem->prev->next = elem->next;
}
@ -374,31 +374,31 @@ static void removeElementGivenHash(dc_hash_t *pH, /* The pH containing "
pH->first = elem->next;
}
if( elem->next )
if (elem->next)
{
elem->next->prev = elem->prev;
}
pEntry = &pH->ht[h];
if( pEntry->chain==elem )
if (pEntry->chain==elem)
{
pEntry->chain = elem->next;
}
pEntry->count--;
if( pEntry->count<=0 )
if (pEntry->count<=0)
{
pEntry->chain = 0;
}
if( pH->copyKey && elem->pKey )
if (pH->copyKey && elem->pKey)
{
sjhashFree(elem->pKey);
}
sjhashFree( elem );
sjhashFree( elem);
pH->count--;
}
@ -414,11 +414,11 @@ void* dc_hash_find(const dc_hash_t *pH, const void *pKey, int nKey)
dc_hashelem_t *elem; /* The element that matches key */
int (*xHash)(const void*,int); /* The hash function */
if( pH==0 || pH->ht==0 ) return 0;
if (pH==0 || pH->ht==0) return 0;
xHash = hashFunction(pH->keyClass);
assert( xHash!=0 );
assert( xHash!=0);
h = (*xHash)(pKey,nKey);
assert( (pH->htsize & (pH->htsize-1))==0 );
assert( (pH->htsize & (pH->htsize-1))==0);
elem = findElementGivenHash(pH,pKey,nKey, h & (pH->htsize-1));
return elem ? elem->data : 0;
}
@ -448,18 +448,18 @@ void* dc_hash_insert(dc_hash_t *pH, const void *pKey, int nKey, void *data)
dc_hashelem_t *new_elem; /* New element added to the pH */
int (*xHash)(const void*,int); /* The hash function */
assert( pH!=0 );
assert( pH!=0);
xHash = hashFunction(pH->keyClass);
assert( xHash!=0 );
assert( xHash!=0);
hraw = (*xHash)(pKey, nKey);
assert( (pH->htsize & (pH->htsize-1))==0 );
assert( (pH->htsize & (pH->htsize-1))==0);
h = hraw & (pH->htsize-1);
elem = findElementGivenHash(pH,pKey,nKey,h);
if( elem )
if (elem)
{
void *old_data = elem->data;
if( data==0 )
if (data==0)
{
removeElementGivenHash(pH,elem,h);
}
@ -470,16 +470,16 @@ void* dc_hash_insert(dc_hash_t *pH, const void *pKey, int nKey, void *data)
return old_data;
}
if( data==0 ) return 0;
if (data==0) return 0;
new_elem = (dc_hashelem_t*)sjhashMalloc( sizeof(dc_hashelem_t) );
new_elem = (dc_hashelem_t*)sjhashMalloc( sizeof(dc_hashelem_t));
if( new_elem==0 ) return data;
if (new_elem==0) return data;
if( pH->copyKey && pKey!=0 )
if (pH->copyKey && pKey!=0)
{
new_elem->pKey = sjhashMallocRaw( nKey );
if( new_elem->pKey==0 )
new_elem->pKey = sjhashMallocRaw( nKey);
if (new_elem->pKey==0)
{
sjhashFree(new_elem);
return data;
@ -494,10 +494,10 @@ void* dc_hash_insert(dc_hash_t *pH, const void *pKey, int nKey, void *data)
new_elem->nKey = nKey;
pH->count++;
if( pH->htsize==0 )
if (pH->htsize==0)
{
rehash(pH,8);
if( pH->htsize==0 )
if (pH->htsize==0)
{
pH->count = 0;
sjhashFree(new_elem);
@ -505,13 +505,13 @@ void* dc_hash_insert(dc_hash_t *pH, const void *pKey, int nKey, void *data)
}
}
if( pH->count > pH->htsize )
if (pH->count > pH->htsize)
{
rehash(pH,pH->htsize*2);
}
assert( pH->htsize>0 );
assert( (pH->htsize & (pH->htsize-1))==0 );
assert( pH->htsize>0);
assert( (pH->htsize & (pH->htsize-1))==0);
h = hraw & (pH->htsize-1);
insertElement(pH, &pH->ht[h], new_elem);
new_elem->data = data;

File diff suppressed because it is too large Load diff

View file

@ -119,8 +119,8 @@ char* dc_render_setup_file(dc_context_t* context, const char* passphrase)
char* ret_setupfilecontent = NULL;
if( context==NULL || context->magic != DC_CONTEXT_MAGIC || passphrase==NULL
|| strlen(passphrase)<2 || curr_private_key==NULL ) {
if (context==NULL || context->magic != DC_CONTEXT_MAGIC || passphrase==NULL
|| strlen(passphrase)<2 || curr_private_key==NULL) {
goto cleanup;
}
@ -129,7 +129,7 @@ char* dc_render_setup_file(dc_context_t* context, const char* passphrase)
/* create the payload */
if( !dc_ensure_secret_key_exists(context) ) {
if (!dc_ensure_secret_key_exists(context)) {
goto cleanup;
}
@ -138,7 +138,7 @@ char* dc_render_setup_file(dc_context_t* context, const char* passphrase)
dc_key_load_self_private(curr_private_key, self_addr, context->sql);
char* payload_key_asc = dc_key_render_asc(curr_private_key, context->e2ee_enabled? "Autocrypt-Prefer-Encrypt: mutual\r\n" : NULL);
if( payload_key_asc == NULL ) {
if (payload_key_asc == NULL) {
goto cleanup;
}
@ -161,20 +161,20 @@ char* dc_render_setup_file(dc_context_t* context, const char* passphrase)
/* S2K */
#define SYMM_ALGO PGP_SA_AES_128
if( !pgp_crypt_any(&crypt_info, SYMM_ALGO) ) {
if (!pgp_crypt_any(&crypt_info, SYMM_ALGO)) {
goto cleanup;
}
int s2k_spec = PGP_S2KS_ITERATED_AND_SALTED; // 0=simple, 1=salted, 3=salted+iterated
int s2k_iter_id = 96; // 0=1024 iterations, 96=65536 iterations
#define HASH_ALG PGP_HASH_SHA256
if( (key = pgp_s2k_do(passphrase, crypt_info.keysize, s2k_spec, HASH_ALG, salt, s2k_iter_id)) == NULL ) {
if ((key = pgp_s2k_do(passphrase, crypt_info.keysize, s2k_spec, HASH_ALG, salt, s2k_iter_id)) == NULL) {
goto cleanup;
}
/* encrypt the payload using the key using AES-128 and put it into
OpenPGP's "Symmetric-Key Encrypted Session Key" (Tag 3, https://tools.ietf.org/html/rfc4880#section-5.3 ) followed by
OpenPGP's "Symmetrically Encrypted Data Packet" (Tag 18, https://tools.ietf.org/html/rfc4880#section-5.13 , better than Tag 9 ) */
OpenPGP's "Symmetric-Key Encrypted Session Key" (Tag 3, https://tools.ietf.org/html/rfc4880#section-5.3) followed by
OpenPGP's "Symmetrically Encrypted Data Packet" (Tag 18, https://tools.ietf.org/html/rfc4880#section-5.13 , better than Tag 9) */
pgp_setup_memory_write(&encr_output, &encr_mem, 128);
pgp_writer_push_armor_msg(encr_output);
@ -186,17 +186,17 @@ char* dc_render_setup_file(dc_context_t* context, const char* passphrase)
+ 1/*s2k_spec*/
+ 1/*S2 hash algo*/
+ ((s2k_spec==PGP_S2KS_SALTED || s2k_spec==PGP_S2KS_ITERATED_AND_SALTED)? PGP_SALT_SIZE : 0)/*the salt*/
+ ((s2k_spec==PGP_S2KS_ITERATED_AND_SALTED)? 1 : 0)/*number of iterations*/ );
+ ((s2k_spec==PGP_S2KS_ITERATED_AND_SALTED)? 1 : 0)/*number of iterations*/);
pgp_write_scalar (encr_output, 4, 1); // 1 octet: version
pgp_write_scalar (encr_output, SYMM_ALGO, 1); // 1 octet: symm. algo
pgp_write_scalar (encr_output, s2k_spec, 1); // 1 octet: s2k_spec
pgp_write_scalar (encr_output, HASH_ALG, 1); // 1 octet: S2 hash algo
if( s2k_spec==PGP_S2KS_SALTED || s2k_spec==PGP_S2KS_ITERATED_AND_SALTED ) {
if (s2k_spec==PGP_S2KS_SALTED || s2k_spec==PGP_S2KS_ITERATED_AND_SALTED) {
pgp_write (encr_output, salt, PGP_SALT_SIZE); // 8 octets: the salt
}
if( s2k_spec==PGP_S2KS_ITERATED_AND_SALTED ) {
if (s2k_spec==PGP_S2KS_ITERATED_AND_SALTED) {
pgp_write_scalar (encr_output, s2k_iter_id, 1); // 1 octet: number of iterations
}
@ -205,7 +205,7 @@ char* dc_render_setup_file(dc_context_t* context, const char* passphrase)
/* Tag 18 - PGP_PTAG_CT_SE_IP_DATA */
//pgp_write_symm_enc_data((const uint8_t*)payload_mem->buf, payload_mem->length, PGP_SA_AES_128, key, encr_output); //-- would generate Tag 9
{
uint8_t* iv = calloc(1, crypt_info.blocksize); if( iv == NULL) { goto cleanup; }
uint8_t* iv = calloc(1, crypt_info.blocksize); if (iv == NULL) { goto cleanup; }
crypt_info.set_iv(&crypt_info, iv);
free(iv);
@ -270,11 +270,11 @@ char* dc_render_setup_file(dc_context_t* context, const char* passphrase)
cleanup:
sqlite3_finalize(stmt);
if( payload_output ) { pgp_output_delete(payload_output); }
if( payload_mem ) { pgp_memory_free(payload_mem); }
if (payload_output) { pgp_output_delete(payload_output); }
if (payload_mem) { pgp_memory_free(payload_mem); }
if( encr_output ) { pgp_output_delete(encr_output); }
if( encr_mem ) { pgp_memory_free(encr_mem); }
if (encr_output) { pgp_output_delete(encr_output); }
if (encr_mem) { pgp_memory_free(encr_mem); }
dc_key_unref(curr_private_key);
free(encr_string);
@ -312,14 +312,14 @@ char* dc_decrypt_setup_file(dc_context_t* context, const char* passphrase, const
/* extract base64 from filecontent */
fc_buf = dc_strdup(filecontent);
if( !dc_split_armored_data(fc_buf, &fc_headerline, NULL, NULL, &fc_base64)
|| fc_headerline==NULL || strcmp(fc_headerline, "-----BEGIN PGP MESSAGE-----")!=0 || fc_base64==NULL ) {
if (!dc_split_armored_data(fc_buf, &fc_headerline, NULL, NULL, &fc_base64)
|| fc_headerline==NULL || strcmp(fc_headerline, "-----BEGIN PGP MESSAGE-----")!=0 || fc_base64==NULL) {
goto cleanup;
}
/* convert base64 to binary */
if( mailmime_base64_body_parse(fc_base64, strlen(fc_base64), &indx, &binary/*must be freed using mmap_string_unref()*/, &binary_bytes)!=MAILIMF_NO_ERROR
|| binary == NULL || binary_bytes == 0 ) {
if (mailmime_base64_body_parse(fc_base64, strlen(fc_base64), &indx, &binary/*must be freed using mmap_string_unref()*/, &binary_bytes)!=MAILIMF_NO_ERROR
|| binary == NULL || binary_bytes == 0) {
goto cleanup;
}
@ -328,15 +328,15 @@ char* dc_decrypt_setup_file(dc_context_t* context, const char* passphrase, const
io.outs = stdout;
io.errs = stderr;
io.res = stderr;
if( (outmem=pgp_decrypt_buf(&io, binary, binary_bytes, NULL, NULL, 0, 0, passphrase)) == NULL ) {
if ((outmem=pgp_decrypt_buf(&io, binary, binary_bytes, NULL, NULL, 0, 0, passphrase)) == NULL) {
goto cleanup;
}
payload = strndup((const char*)outmem->buf, outmem->length);
cleanup:
free(fc_buf);
if( binary ) { mmap_string_unref(binary); }
if( outmem ) { pgp_memory_free(outmem); }
if (binary) { mmap_string_unref(binary); }
if (outmem) { pgp_memory_free(outmem); }
return payload;
}
@ -363,16 +363,16 @@ char* dc_create_setup_code(dc_context_t* context)
dc_strbuilder_init(&ret, 0);
for( i = 0; i < CODE_ELEMS; i++ )
for (i = 0; i < CODE_ELEMS; i++)
{
do
{
if( !RAND_bytes((unsigned char*)&random_val, sizeof(uint16_t)) ) {
if (!RAND_bytes((unsigned char*)&random_val, sizeof(uint16_t))) {
dc_log_warning(context, 0, "Falling back to pseudo-number generation for the setup code.");
RAND_pseudo_bytes((unsigned char*)&random_val, sizeof(uint16_t));
}
}
while( random_val > 60000 ); /* make sure the modulo below does not reduce entropy (range is 0..65535, a module 10000 would make appearing values <=535 one time more often than other values) */
while (random_val > 60000); /* make sure the modulo below does not reduce entropy (range is 0..65535, a module 10000 would make appearing values <=535 one time more often than other values) */
random_val = random_val % 10000; /* force all blocks into the range 0..9999 */
@ -386,7 +386,7 @@ char* dc_create_setup_code(dc_context_t* context)
/* Function remove all special characters from the given code and brings it to the 9x4 form */
char* dc_normalize_setup_code(dc_context_t* context, const char* in)
{
if( in == NULL ) {
if (in == NULL) {
return NULL;
}
@ -395,11 +395,11 @@ char* dc_normalize_setup_code(dc_context_t* context, const char* in)
int outlen;
const char* p1 = in;
while( *p1 ) {
if( *p1 >= '0' && *p1 <= '9' ) {
while (*p1) {
if (*p1 >= '0' && *p1 <= '9') {
dc_strbuilder_catf(&out, "%c", *p1);
outlen = strlen(out.buf);
if( outlen==4 || outlen==9 || outlen==14 || outlen==19 || outlen==24 || outlen == 29 || outlen == 34 || outlen == 39 ) {
if (outlen==4 || outlen==9 || outlen==14 || outlen==19 || outlen==24 || outlen == 29 || outlen == 34 || outlen == 39) {
dc_strbuilder_cat(&out, "-");
}
}
@ -465,29 +465,29 @@ char* dc_initiate_key_transfer(dc_context_t* context)
dc_msg_t* msg = NULL;
uint32_t msg_id = 0;
if( !dc_alloc_ongoing(context) ) {
if (!dc_alloc_ongoing(context)) {
return 0; /* no cleanup as this would call dc_free_ongoing() */
}
#define CHECK_EXIT if( context->shall_stop_ongoing ) { goto cleanup; }
#define CHECK_EXIT if (context->shall_stop_ongoing) { goto cleanup; }
if( (setup_code=dc_create_setup_code(context)) == NULL ) { /* this may require a keypair to be created. this may take a second ... */
if ((setup_code=dc_create_setup_code(context)) == NULL) { /* this may require a keypair to be created. this may take a second ... */
goto cleanup;
}
CHECK_EXIT
if( (setup_file_content=dc_render_setup_file(context, setup_code))==NULL ) { /* encrypting may also take a while ... */
if ((setup_file_content=dc_render_setup_file(context, setup_code))==NULL) { /* encrypting may also take a while ... */
goto cleanup;
}
CHECK_EXIT
if( (setup_file_name=dc_get_fine_pathNfilename(context->blobdir, "autocrypt-setup-message.html")) == NULL
|| !dc_write_file(setup_file_name, setup_file_content, strlen(setup_file_content), context) ) {
if ((setup_file_name=dc_get_fine_pathNfilename(context->blobdir, "autocrypt-setup-message.html")) == NULL
|| !dc_write_file(setup_file_name, setup_file_content, strlen(setup_file_content), context)) {
goto cleanup;
}
if( (chat_id=dc_create_chat_by_contact_id(context, DC_CONTACT_ID_SELF))==0 ) {
if ((chat_id=dc_create_chat_by_contact_id(context, DC_CONTACT_ID_SELF))==0) {
goto cleanup;
}
@ -500,7 +500,7 @@ char* dc_initiate_key_transfer(dc_context_t* context)
CHECK_EXIT
if( (msg_id = dc_send_msg_object(context, chat_id, msg)) == 0 ) {
if ((msg_id = dc_send_msg_object(context, chat_id, msg)) == 0) {
goto cleanup;
}
@ -510,14 +510,14 @@ char* dc_initiate_key_transfer(dc_context_t* context)
/* wait until the message is really sent */
dc_log_info(context, 0, "Wait for setup message being sent ...");
while( 1 )
while (1)
{
CHECK_EXIT
sleep(1);
msg = dc_get_msg(context, msg_id);
if( dc_msg_is_sent(msg) ) {
if (dc_msg_is_sent(msg)) {
break;
}
dc_msg_unref(msg);
@ -529,7 +529,7 @@ char* dc_initiate_key_transfer(dc_context_t* context)
success = 1;
cleanup:
if( !success ) { free(setup_code); setup_code = NULL; }
if (!success) { free(setup_code); setup_code = NULL; }
free(setup_file_name);
free(setup_file_content);
dc_msg_unref(msg);
@ -549,15 +549,15 @@ static int set_self_key(dc_context_t* context, const char* armored, int set_defa
char* self_addr = NULL;
buf = dc_strdup(armored);
if( !dc_split_armored_data(buf, &buf_headerline, NULL, &buf_preferencrypt, &buf_base64)
|| strcmp(buf_headerline, "-----BEGIN PGP PRIVATE KEY BLOCK-----")!=0 || buf_base64 == NULL ) {
if (!dc_split_armored_data(buf, &buf_headerline, NULL, &buf_preferencrypt, &buf_base64)
|| strcmp(buf_headerline, "-----BEGIN PGP PRIVATE KEY BLOCK-----")!=0 || buf_base64 == NULL) {
dc_log_warning(context, 0, "File does not contain a private key."); /* do not log as error - this is quite normal after entering the bad setup code */
goto cleanup;
}
if( !dc_key_set_from_base64(private_key, buf_base64, DC_KEY_PRIVATE)
if (!dc_key_set_from_base64(private_key, buf_base64, DC_KEY_PRIVATE)
|| !dc_pgp_is_valid_key(context, private_key)
|| !dc_pgp_split_key(context, private_key, public_key) ) {
|| !dc_pgp_split_key(context, private_key, public_key)) {
dc_log_error(context, 0, "File does not contain a valid private key.");
goto cleanup;
}
@ -570,22 +570,22 @@ static int set_self_key(dc_context_t* context, const char* armored, int set_defa
sqlite3_finalize(stmt);
stmt = NULL;
if( set_default ) {
if (set_default) {
dc_sqlite3_execute(context->sql, "UPDATE keypairs SET is_default=0;"); /* if the new key should be the default key, all other should not */
}
self_addr = dc_sqlite3_get_config(context->sql, "configured_addr", NULL);
if( !dc_key_save_self_keypair(public_key, private_key, self_addr, set_default, context->sql) ) {
if (!dc_key_save_self_keypair(public_key, private_key, self_addr, set_default, context->sql)) {
dc_log_error(context, 0, "Cannot save keypair.");
goto cleanup;
}
/* if we also received an Autocrypt-Prefer-Encrypt header, handle this */
if( buf_preferencrypt ) {
if( strcmp(buf_preferencrypt, "nopreference")==0 ) {
if (buf_preferencrypt) {
if (strcmp(buf_preferencrypt, "nopreference")==0) {
dc_set_config_int(context, "e2ee_enabled", 0); /* use the top-level function as this also resets cached values */
}
else if( strcmp(buf_preferencrypt, "mutual")==0 ) {
else if (strcmp(buf_preferencrypt, "mutual")==0) {
dc_set_config_int(context, "e2ee_enabled", 1); /* use the top-level function as this also resets cached values */
}
}
@ -632,32 +632,32 @@ int dc_continue_key_transfer(dc_context_t* context, uint32_t msg_id, const char*
char* armored_key = NULL;
char* norm_sc = NULL;
if( context == NULL || context->magic != DC_CONTEXT_MAGIC || msg_id <= DC_MSG_ID_LAST_SPECIAL || setup_code == NULL ) {
if (context == NULL || context->magic != DC_CONTEXT_MAGIC || msg_id <= DC_MSG_ID_LAST_SPECIAL || setup_code == NULL) {
goto cleanup;
}
if( (msg=dc_get_msg(context, msg_id))==NULL || !dc_msg_is_setupmessage(msg)
|| (filename=dc_msg_get_file(msg))==NULL || filename[0]==0 ) {
if ((msg=dc_get_msg(context, msg_id))==NULL || !dc_msg_is_setupmessage(msg)
|| (filename=dc_msg_get_file(msg))==NULL || filename[0]==0) {
dc_log_error(context, 0, "Message is no Autocrypt Setup Message.");
goto cleanup;
}
if( !dc_read_file(filename, (void**)&filecontent, &filebytes, msg->context) || filecontent == NULL || filebytes <= 0 ) {
if (!dc_read_file(filename, (void**)&filecontent, &filebytes, msg->context) || filecontent == NULL || filebytes <= 0) {
dc_log_error(context, 0, "Cannot read Autocrypt Setup Message file.");
goto cleanup;
}
if( (norm_sc = dc_normalize_setup_code(context, setup_code))==NULL ) {
if ((norm_sc = dc_normalize_setup_code(context, setup_code))==NULL) {
dc_log_warning(context, 0, "Cannot normalize Setup Code.");
goto cleanup;
}
if( (armored_key=dc_decrypt_setup_file(context, norm_sc, filecontent)) == NULL ) {
if ((armored_key=dc_decrypt_setup_file(context, norm_sc, filecontent)) == NULL) {
dc_log_warning(context, 0, "Cannot decrypt Autocrypt Setup Message."); /* do not log as error - this is quite normal after entering the bad setup code */
goto cleanup;
}
if( !set_self_key(context, armored_key, 1/*set default*/) ) {
if (!set_self_key(context, armored_key, 1/*set default*/)) {
goto cleanup; /* error already logged */
}
@ -681,7 +681,7 @@ cleanup:
static void export_key_to_asc_file(dc_context_t* context, const char* dir, int id, const dc_key_t* key, int is_default)
{
char* file_name;
if( is_default ) {
if (is_default) {
file_name = dc_mprintf("%s/%s-key-default.asc", dir, key->type==DC_KEY_PUBLIC? "public" : "private");
}
else {
@ -689,7 +689,7 @@ static void export_key_to_asc_file(dc_context_t* context, const char* dir, int i
}
dc_log_info(context, 0, "Exporting key %s", file_name);
dc_delete_file(file_name, context);
if( dc_key_render_asc_to_file(key, file_name, context) ) {
if (dc_key_render_asc_to_file(key, file_name, context)) {
context->cb(context, DC_EVENT_IMEX_FILE_WRITTEN, (uintptr_t)file_name, 0);
dc_log_error(context, 0, "Cannot write key to %s", file_name);
}
@ -705,15 +705,15 @@ static int export_self_keys(dc_context_t* context, const char* dir)
dc_key_t* public_key = dc_key_new();
dc_key_t* private_key = dc_key_new();
if( (stmt=dc_sqlite3_prepare(context->sql, "SELECT id, public_key, private_key, is_default FROM keypairs;"))==NULL ) {
if ((stmt=dc_sqlite3_prepare(context->sql, "SELECT id, public_key, private_key, is_default FROM keypairs;"))==NULL) {
goto cleanup;
}
while( sqlite3_step(stmt)==SQLITE_ROW ) {
id = sqlite3_column_int( stmt, 0 );
while (sqlite3_step(stmt)==SQLITE_ROW) {
id = sqlite3_column_int( stmt, 0 );
dc_key_set_from_stmt(public_key, stmt, 1, DC_KEY_PUBLIC);
dc_key_set_from_stmt(private_key, stmt, 2, DC_KEY_PRIVATE);
is_default = sqlite3_column_int( stmt, 3 );
is_default = sqlite3_column_int( stmt, 3 );
export_key_to_asc_file(context, dir, id, public_key, is_default);
export_key_to_asc_file(context, dir, id, private_key, is_default);
}
@ -754,19 +754,19 @@ static int import_self_keys(dc_context_t* context, const char* dir_name)
char* buf2 = NULL;
const char* buf2_headerline; // a pointer inside buf2, MUST NOT be free()'d
if( context==NULL || context->magic != DC_CONTEXT_MAGIC || dir_name==NULL ) {
if (context==NULL || context->magic != DC_CONTEXT_MAGIC || dir_name==NULL) {
goto cleanup;
}
if( (dir_handle=opendir(dir_name))==NULL ) {
if ((dir_handle=opendir(dir_name))==NULL) {
dc_log_error(context, 0, "Import: Cannot open directory \"%s\".", dir_name);
goto cleanup;
}
while( (dir_entry=readdir(dir_handle))!=NULL )
while ((dir_entry=readdir(dir_handle))!=NULL)
{
free(suffix);
suffix = dc_get_filesuffix_lc(dir_entry->d_name);
if( suffix==NULL || strcmp(suffix, "asc")!=0 ) {
if (suffix==NULL || strcmp(suffix, "asc")!=0) {
continue;
}
@ -776,16 +776,16 @@ static int import_self_keys(dc_context_t* context, const char* dir_name)
free(buf);
buf = NULL;
if( !dc_read_file(path_plus_name, (void**)&buf, &buf_bytes, context)
|| buf_bytes < 50 ) {
if (!dc_read_file(path_plus_name, (void**)&buf, &buf_bytes, context)
|| buf_bytes < 50) {
continue;
}
private_key = buf;
free(buf2);
buf2 = dc_strdup(buf);
if( dc_split_armored_data(buf2, &buf2_headerline, NULL, NULL, NULL)
&& strcmp(buf2_headerline, "-----BEGIN PGP PUBLIC KEY BLOCK-----")==0 ) {
if (dc_split_armored_data(buf2, &buf2_headerline, NULL, NULL, NULL)
&& strcmp(buf2_headerline, "-----BEGIN PGP PUBLIC KEY BLOCK-----")==0) {
/* This file starts with a Public Key.
* However some programs (Thunderbird/Enigmail) put public and private key
* in the same file, so we check if there is a private key following */
@ -796,25 +796,25 @@ static int import_self_keys(dc_context_t* context, const char* dir_name)
}
set_default = 1;
if( strstr(dir_entry->d_name, "legacy")!=NULL ) {
if (strstr(dir_entry->d_name, "legacy")!=NULL) {
dc_log_info(context, 0, "Treating \"%s\" as a legacy private key.", path_plus_name);
set_default = 0; /* a key with "legacy" in its name is not made default; this may result in a keychain with _no_ default, however, this is no problem, as this will create a default key later */
}
if( !set_self_key(context, private_key, set_default) ) {
if (!set_self_key(context, private_key, set_default)) {
continue;
}
imported_count++;
}
if( imported_count == 0 ) {
if (imported_count == 0) {
dc_log_error(context, 0, "No private keys found in \"%s\".", dir_name);
goto cleanup;
}
cleanup:
if( dir_handle ) { closedir(dir_handle); }
if (dir_handle) { closedir(dir_handle); }
free(suffix);
free(path_plus_name);
free(buf);
@ -833,8 +833,8 @@ The macro avoids weird values of 0% or 100% while still working. */
#define FILE_PROGRESS \
processed_files_count++; \
int permille = (processed_files_count*1000)/total_files_count; \
if( permille < 10 ) { permille = 10; } \
if( permille > 990 ) { permille = 990; } \
if (permille < 10) { permille = 10; } \
if (permille > 990) { permille = 990; } \
context->cb(context, DC_EVENT_IMEX_PROGRESS, permille, 0);
@ -862,7 +862,7 @@ static int export_backup(dc_context_t* context, const char* dir)
char buffer[256];
timeinfo = localtime(&now);
strftime(buffer, 256, DC_BAK_PREFIX "-%Y-%m-%d." DC_BAK_SUFFIX, timeinfo);
if( (dest_pathNfilename=dc_get_fine_pathNfilename(dir, buffer))==NULL ) {
if ((dest_pathNfilename=dc_get_fine_pathNfilename(dir, buffer))==NULL) {
dc_log_error(context, 0, "Cannot get backup file name.");
goto cleanup;
}
@ -873,7 +873,7 @@ static int export_backup(dc_context_t* context, const char* dir)
closed = 1;
dc_log_info(context, 0, "Backup \"%s\" to \"%s\".", context->dbfile, dest_pathNfilename);
if( !dc_copy_file(context->dbfile, dest_pathNfilename, context) ) {
if (!dc_copy_file(context->dbfile, dest_pathNfilename, context)) {
goto cleanup; /* error already logged */
}
@ -881,43 +881,43 @@ static int export_backup(dc_context_t* context, const char* dir)
closed = 0;
/* add all files as blobs to the database copy (this does not require the source to be locked, neigher the destination as it is used only here) */
if( (dest_sql=dc_sqlite3_new(context/*for logging only*/))==NULL
|| !dc_sqlite3_open(dest_sql, dest_pathNfilename, 0) ) {
if ((dest_sql=dc_sqlite3_new(context/*for logging only*/))==NULL
|| !dc_sqlite3_open(dest_sql, dest_pathNfilename, 0)) {
goto cleanup; /* error already logged */
}
if( !dc_sqlite3_table_exists(dest_sql, "backup_blobs") ) {
if( !dc_sqlite3_execute(dest_sql, "CREATE TABLE backup_blobs (id INTEGER PRIMARY KEY, file_name, file_content);") ) {
if (!dc_sqlite3_table_exists(dest_sql, "backup_blobs")) {
if (!dc_sqlite3_execute(dest_sql, "CREATE TABLE backup_blobs (id INTEGER PRIMARY KEY, file_name, file_content);")) {
goto cleanup; /* error already logged */
}
}
/* scan directory, pass 1: collect file info */
total_files_count = 0;
if( (dir_handle=opendir(context->blobdir))==NULL ) {
if ((dir_handle=opendir(context->blobdir))==NULL) {
dc_log_error(context, 0, "Backup: Cannot get info for blob-directory \"%s\".", context->blobdir);
goto cleanup;
}
while( (dir_entry=readdir(dir_handle))!=NULL ) {
while ((dir_entry=readdir(dir_handle))!=NULL) {
total_files_count++;
}
closedir(dir_handle);
dir_handle = NULL;
if( total_files_count>0 )
if (total_files_count>0)
{
/* scan directory, pass 2: copy files */
if( (dir_handle=opendir(context->blobdir))==NULL ) {
if ((dir_handle=opendir(context->blobdir))==NULL) {
dc_log_error(context, 0, "Backup: Cannot copy from blob-directory \"%s\".", context->blobdir);
goto cleanup;
}
stmt = dc_sqlite3_prepare(dest_sql, "INSERT INTO backup_blobs (file_name, file_content) VALUES (?, ?);");
while( (dir_entry=readdir(dir_handle))!=NULL )
while ((dir_entry=readdir(dir_handle))!=NULL)
{
if( context->shall_stop_ongoing ) {
if (context->shall_stop_ongoing) {
delete_dest_file = 1;
goto cleanup;
}
@ -926,9 +926,9 @@ static int export_backup(dc_context_t* context, const char* dir)
char* name = dir_entry->d_name; /* name without path; may also be `.` or `..` */
int name_len = strlen(name);
if( (name_len==1 && name[0]=='.')
if ((name_len==1 && name[0]=='.')
|| (name_len==2 && name[0]=='.' && name[1]=='.')
|| (name_len > prefix_len && strncmp(name, DC_BAK_PREFIX, prefix_len)==0 && name_len > suffix_len && strncmp(&name[name_len-suffix_len-1], "." DC_BAK_SUFFIX, suffix_len)==0) ) {
|| (name_len > prefix_len && strncmp(name, DC_BAK_PREFIX, prefix_len)==0 && name_len > suffix_len && strncmp(&name[name_len-suffix_len-1], "." DC_BAK_SUFFIX, suffix_len)==0)) {
//dc_log_info(context, 0, "Backup: Skipping \"%s\".", name);
continue;
}
@ -937,13 +937,13 @@ static int export_backup(dc_context_t* context, const char* dir)
free(curr_pathNfilename);
curr_pathNfilename = dc_mprintf("%s/%s", context->blobdir, name);
free(buf);
if( !dc_read_file(curr_pathNfilename, &buf, &buf_bytes, context) || buf==NULL || buf_bytes<=0 ) {
if (!dc_read_file(curr_pathNfilename, &buf, &buf_bytes, context) || buf==NULL || buf_bytes<=0) {
continue;
}
sqlite3_bind_text(stmt, 1, name, -1, SQLITE_STATIC);
sqlite3_bind_blob(stmt, 2, buf, buf_bytes, SQLITE_STATIC);
if( sqlite3_step(stmt)!=SQLITE_DONE ) {
if (sqlite3_step(stmt)!=SQLITE_DONE) {
dc_log_error(context, 0, "Disk full? Cannot add file \"%s\" to backup.", curr_pathNfilename);
goto cleanup; /* this is not recoverable! writing to the sqlite database should work! */
}
@ -963,13 +963,13 @@ static int export_backup(dc_context_t* context, const char* dir)
success = 1;
cleanup:
if( dir_handle ) { closedir(dir_handle); }
if( closed ) { dc_sqlite3_open(context->sql, context->dbfile, 0); }
if (dir_handle) { closedir(dir_handle); }
if (closed) { dc_sqlite3_open(context->sql, context->dbfile, 0); }
sqlite3_finalize(stmt);
dc_sqlite3_close(dest_sql);
dc_sqlite3_unref(dest_sql);
if( delete_dest_file ) { dc_delete_file(dest_pathNfilename, context); }
if (delete_dest_file) { dc_delete_file(dest_pathNfilename, context); }
free(dest_pathNfilename);
free(curr_pathNfilename);
@ -986,9 +986,9 @@ cleanup:
static void ensure_no_slash(char* path)
{
int path_len = strlen(path);
if( path_len > 0 ) {
if( path[path_len-1] == '/'
|| path[path_len-1] == '\\' ) {
if (path_len > 0) {
if (path[path_len-1] == '/'
|| path[path_len-1] == '\\') {
path[path_len-1] = 0;
}
}
@ -1010,7 +1010,7 @@ static int import_backup(dc_context_t* context, const char* backup_to_import)
dc_log_info(context, 0, "Import \"%s\" to \"%s\".", backup_to_import, context->dbfile);
if( dc_is_configured(context) ) {
if (dc_is_configured(context)) {
dc_log_error(context, 0, "Cannot import backups to accounts in use.");
goto cleanup;
}
@ -1020,24 +1020,24 @@ static int import_backup(dc_context_t* context, const char* backup_to_import)
//dc_sqlite3_lock(context->sql); // TODO: check if this works while threads running
//locked = 1;
if( dc_sqlite3_is_open(context->sql) ) {
if (dc_sqlite3_is_open(context->sql)) {
dc_sqlite3_close(context->sql);
}
dc_delete_file(context->dbfile, context);
if( dc_file_exist(context->dbfile) ) {
if (dc_file_exist(context->dbfile)) {
dc_log_error(context, 0, "Cannot import backups: Cannot delete the old file.");
goto cleanup;
}
/* copy the database file */
if( !dc_copy_file(backup_to_import, context->dbfile, context) ) {
if (!dc_copy_file(backup_to_import, context->dbfile, context)) {
goto cleanup; /* error already logged */
}
/* re-open copied database file */
if( !dc_sqlite3_open(context->sql, context->dbfile, 0) ) {
if (!dc_sqlite3_open(context->sql, context->dbfile, 0)) {
goto cleanup;
}
@ -1049,9 +1049,9 @@ static int import_backup(dc_context_t* context, const char* backup_to_import)
stmt = NULL;
stmt = dc_sqlite3_prepare(context->sql, "SELECT file_name, file_content FROM backup_blobs ORDER BY id;");
while( sqlite3_step(stmt) == SQLITE_ROW )
while (sqlite3_step(stmt) == SQLITE_ROW)
{
if( context->shall_stop_ongoing ) {
if (context->shall_stop_ongoing) {
goto cleanup;
}
@ -1061,10 +1061,10 @@ static int import_backup(dc_context_t* context, const char* backup_to_import)
int file_bytes = sqlite3_column_bytes(stmt, 1);
const void* file_content = sqlite3_column_blob (stmt, 1);
if( file_bytes > 0 && file_content ) {
if (file_bytes > 0 && file_content) {
free(pathNfilename);
pathNfilename = dc_mprintf("%s/%s", context->blobdir, file_name);
if( !dc_write_file(pathNfilename, file_content, file_bytes, context) ) {
if (!dc_write_file(pathNfilename, file_content, file_bytes, context)) {
dc_log_error(context, 0, "Storage full? Cannot write file %s with %i bytes.", pathNfilename, file_bytes);
goto cleanup; /* otherwise the user may believe the stuff is imported correctly, but there are files missing ... */
}
@ -1080,7 +1080,7 @@ static int import_backup(dc_context_t* context, const char* backup_to_import)
/* rewrite references to the blobs */
repl_from = dc_sqlite3_get_config(context->sql, "backup_for", NULL);
if( repl_from && strlen(repl_from)>1 && context->blobdir && strlen(context->blobdir)>1 )
if (repl_from && strlen(repl_from)>1 && context->blobdir && strlen(context->blobdir)>1)
{
ensure_no_slash(repl_from);
repl_to = dc_strdup(context->blobdir);
@ -1088,8 +1088,8 @@ static int import_backup(dc_context_t* context, const char* backup_to_import)
dc_log_info(context, 0, "Rewriting paths from '%s' to '%s' ...", repl_from, repl_to);
assert( 'f' == DC_PARAM_FILE );
assert( 'i' == DC_PARAM_PROFILE_IMAGE );
assert( 'f' == DC_PARAM_FILE);
assert( 'i' == DC_PARAM_PROFILE_IMAGE);
char* q3 = sqlite3_mprintf("UPDATE msgs SET param=replace(param, 'f=%q/', 'f=%q/');", repl_from, repl_to); /* cannot use dc_mprintf() because of "%q" */
dc_sqlite3_execute(context->sql, q3);
@ -1112,7 +1112,7 @@ cleanup:
free(repl_to);
sqlite3_finalize(stmt);
// if( locked ) { dc_sqlite3_unlock(context->sql); } // TODO: check if this works while threads running
// if (locked) { dc_sqlite3_unlock(context->sql); } // TODO: check if this works while threads running
return success;
}
@ -1191,11 +1191,11 @@ void dc_job_do_DC_JOB_IMEX_IMAP(dc_context_t* context, dc_job_t* job)
char* param1 = NULL;
char* param2 = NULL;
if( context==NULL || context->magic != DC_CONTEXT_MAGIC || context->sql==NULL ) {
if (context==NULL || context->magic != DC_CONTEXT_MAGIC || context->sql==NULL) {
goto cleanup;
}
if( !dc_alloc_ongoing(context) ) {
if (!dc_alloc_ongoing(context)) {
goto cleanup;
}
ongoing_allocated_here = 1;
@ -1204,7 +1204,7 @@ void dc_job_do_DC_JOB_IMEX_IMAP(dc_context_t* context, dc_job_t* job)
param1 = dc_param_get (job->param, DC_PARAM_CMD_ARG, NULL);
param2 = dc_param_get (job->param, DC_PARAM_CMD_ARG2, NULL);
if( param1 == NULL ) {
if (param1 == NULL) {
dc_log_error(context, 0, "No Import/export dir/file given.");
goto cleanup;
}
@ -1212,14 +1212,14 @@ void dc_job_do_DC_JOB_IMEX_IMAP(dc_context_t* context, dc_job_t* job)
dc_log_info(context, 0, "Import/export process started.");
context->cb(context, DC_EVENT_IMEX_PROGRESS, 10, 0);
if( !dc_sqlite3_is_open(context->sql) ) {
if (!dc_sqlite3_is_open(context->sql)) {
dc_log_error(context, 0, "Import/export: Database not opened.");
goto cleanup;
}
if( what==DC_IMEX_EXPORT_SELF_KEYS || what==DC_IMEX_EXPORT_BACKUP ) {
if (what==DC_IMEX_EXPORT_SELF_KEYS || what==DC_IMEX_EXPORT_BACKUP) {
/* before we export anything, make sure the private key exists */
if( !dc_ensure_secret_key_exists(context) ) {
if (!dc_ensure_secret_key_exists(context)) {
dc_log_error(context, 0, "Import/export: Cannot create private key or private key not available.");
goto cleanup;
}
@ -1227,28 +1227,28 @@ void dc_job_do_DC_JOB_IMEX_IMAP(dc_context_t* context, dc_job_t* job)
dc_create_folder(param1, context);
}
switch( what )
switch (what)
{
case DC_IMEX_EXPORT_SELF_KEYS:
if( !export_self_keys(context, param1) ) {
if (!export_self_keys(context, param1)) {
goto cleanup;
}
break;
case DC_IMEX_IMPORT_SELF_KEYS:
if( !import_self_keys(context, param1) ) {
if (!import_self_keys(context, param1)) {
goto cleanup;
}
break;
case DC_IMEX_EXPORT_BACKUP:
if( !export_backup(context, param1) ) {
if (!export_backup(context, param1)) {
goto cleanup;
}
break;
case DC_IMEX_IMPORT_BACKUP:
if( !import_backup(context, param1) ) {
if (!import_backup(context, param1)) {
goto cleanup;
}
break;
@ -1265,7 +1265,7 @@ cleanup:
free(param1);
free(param2);
if( ongoing_allocated_here ) { dc_free_ongoing(context); }
if (ongoing_allocated_here) { dc_free_ongoing(context); }
context->cb(context, DC_EVENT_IMEX_PROGRESS, success? 1000 : 0, 0);
}
@ -1294,10 +1294,10 @@ cleanup:
* return 1;
* }
*
* if( !dc_is_configured(context) )
* if (!dc_is_configured(context))
* {
* char* file = NULL;
* if( (file=dc_imex_has_backup(context, dir))!=NULL && ask_user_whether_to_import() )
* if ((file=dc_imex_has_backup(context, dir))!=NULL && ask_user_whether_to_import())
* {
* dc_imex(context, DC_IMEX_IMPORT_BACKUP, file, NULL);
* // connect
@ -1307,7 +1307,7 @@ cleanup:
* do {
* ask_user_for_credentials();
* }
* while( !configure_succeeded() )
* while (!configure_succeeded())
* }
* free(file);
* }
@ -1330,31 +1330,31 @@ char* dc_imex_has_backup(dc_context_t* context, const char* dir_name)
char* curr_pathNfilename = NULL;
dc_sqlite3_t* test_sql = NULL;
if( context == NULL || context->magic != DC_CONTEXT_MAGIC ) {
if (context == NULL || context->magic != DC_CONTEXT_MAGIC) {
return NULL;
}
if( (dir_handle=opendir(dir_name))==NULL ) {
if ((dir_handle=opendir(dir_name))==NULL) {
dc_log_info(context, 0, "Backup check: Cannot open directory \"%s\".", dir_name); /* this is not an error - eg. the directory may not exist or the user has not given us access to read data from the storage */
goto cleanup;
}
while( (dir_entry=readdir(dir_handle))!=NULL ) {
while ((dir_entry=readdir(dir_handle))!=NULL) {
const char* name = dir_entry->d_name; /* name without path; may also be `.` or `..` */
int name_len = strlen(name);
if( name_len > prefix_len && strncmp(name, DC_BAK_PREFIX, prefix_len)==0
&& name_len > suffix_len && strncmp(&name[name_len-suffix_len-1], "." DC_BAK_SUFFIX, suffix_len)==0 )
if (name_len > prefix_len && strncmp(name, DC_BAK_PREFIX, prefix_len)==0
&& name_len > suffix_len && strncmp(&name[name_len-suffix_len-1], "." DC_BAK_SUFFIX, suffix_len)==0)
{
free(curr_pathNfilename);
curr_pathNfilename = dc_mprintf("%s/%s", dir_name, name);
dc_sqlite3_unref(test_sql);
if( (test_sql=dc_sqlite3_new(context/*for logging only*/))!=NULL
&& dc_sqlite3_open(test_sql, curr_pathNfilename, DC_OPEN_READONLY) )
if ((test_sql=dc_sqlite3_new(context/*for logging only*/))!=NULL
&& dc_sqlite3_open(test_sql, curr_pathNfilename, DC_OPEN_READONLY))
{
time_t curr_backup_time = dc_sqlite3_get_config_int(test_sql, "backup_time", 0); /* reading the backup time also checks if the database is readable and the table `config` exists */
if( curr_backup_time > 0
&& curr_backup_time > ret_backup_time/*use the newest if there are multiple backup*/ )
if (curr_backup_time > 0
&& curr_backup_time > ret_backup_time/*use the newest if there are multiple backup*/)
{
/* set return value to the tested database name */
free(ret);
@ -1367,7 +1367,7 @@ char* dc_imex_has_backup(dc_context_t* context, const char* dir_name)
}
cleanup:
if( dir_handle ) { closedir(dir_handle); }
if (dir_handle) { closedir(dir_handle); }
free(curr_pathNfilename);
dc_sqlite3_unref(test_sql);
return ret;
@ -1391,21 +1391,21 @@ int dc_check_password(dc_context_t* context, const char* test_pw)
dc_loginparam_t* loginparam = dc_loginparam_new();
int success = 0;
if( context==NULL || context->magic != DC_CONTEXT_MAGIC ) {
if (context==NULL || context->magic != DC_CONTEXT_MAGIC) {
goto cleanup;
}
dc_loginparam_read(loginparam, context->sql, "configured_");
if( (loginparam->mail_pw==NULL || loginparam->mail_pw[0]==0) && (test_pw==NULL || test_pw[0]==0) ) {
if ((loginparam->mail_pw==NULL || loginparam->mail_pw[0]==0) && (test_pw==NULL || test_pw[0]==0)) {
/* both empty or unset */
success = 1;
}
else if( loginparam->mail_pw==NULL || test_pw==NULL ) {
else if (loginparam->mail_pw==NULL || test_pw==NULL) {
/* one set, the other not */
success = 0;
}
else if( strcmp(loginparam->mail_pw, test_pw)==0 ) {
else if (strcmp(loginparam->mail_pw, test_pw)==0) {
/* string-compared passwords are equal */
success = 1;
}

View file

@ -43,24 +43,24 @@ static int connect_to_imap(dc_context_t* context, dc_job_t* job /*may be NULL if
int ret_connected = NOT_CONNECTED;
dc_loginparam_t* param = dc_loginparam_new();
if( context == NULL || context->magic != DC_CONTEXT_MAGIC || context->imap == NULL ) {
if (context == NULL || context->magic != DC_CONTEXT_MAGIC || context->imap == NULL) {
dc_log_warning(context, 0, "Cannot connect to IMAP: Bad parameters.");
goto cleanup;
}
if( dc_imap_is_connected(context->imap) ) {
if (dc_imap_is_connected(context->imap)) {
ret_connected = ALREADY_CONNECTED;
goto cleanup;
}
if( dc_sqlite3_get_config_int(context->sql, "configured", 0) == 0 ) {
if (dc_sqlite3_get_config_int(context->sql, "configured", 0) == 0) {
dc_log_warning(context, 0, "Not configured, cannot connect."); // this is no error, connect() is called eg. when the screen is switched on, it's okay if the caller does not check all circumstances here
goto cleanup;
}
dc_loginparam_read(param, context->sql, "configured_" /*the trailing underscore is correct*/);
if( !dc_imap_connect(context->imap, param) ) {
if (!dc_imap_connect(context->imap, param)) {
dc_job_try_again_later(job, DC_STANDARD_DELAY);
goto cleanup;
}
@ -82,25 +82,25 @@ static void dc_job_do_DC_JOB_SEND_MSG_TO_IMAP(dc_context_t* context, dc_job_t* j
dc_mimefactory_init(&mimefactory, context);
/* connect to IMAP-server */
if( !dc_imap_is_connected(context->imap) ) {
if (!dc_imap_is_connected(context->imap)) {
connect_to_imap(context, NULL);
if( !dc_imap_is_connected(context->imap) ) {
if (!dc_imap_is_connected(context->imap)) {
dc_job_try_again_later(job, DC_STANDARD_DELAY);
goto cleanup;
}
}
/* create message */
if( dc_mimefactory_load_msg(&mimefactory, job->foreign_id)==0
|| mimefactory.from_addr == NULL ) {
if (dc_mimefactory_load_msg(&mimefactory, job->foreign_id)==0
|| mimefactory.from_addr == NULL) {
goto cleanup; /* should not happen as we've sent the message to the SMTP server before */
}
if( !dc_mimefactory_render(&mimefactory) ) {
if (!dc_mimefactory_render(&mimefactory)) {
goto cleanup; /* should not happen as we've sent the message to the SMTP server before */
}
if( !dc_imap_append_msg(context->imap, mimefactory.msg->timestamp, mimefactory.out->str, mimefactory.out->len, &server_folder, &server_uid) ) {
if (!dc_imap_append_msg(context->imap, mimefactory.msg->timestamp, mimefactory.out->str, mimefactory.out->len, &server_folder, &server_uid)) {
dc_job_try_again_later(job, DC_AT_ONCE);
goto cleanup;
}
@ -120,28 +120,28 @@ static void dc_job_do_DC_JOB_DELETE_MSG_ON_IMAP(dc_context_t* context, dc_job_t*
dc_msg_t* msg = dc_msg_new();
sqlite3_stmt* stmt = NULL;
if( !dc_msg_load_from_db(msg, context, job->foreign_id)
|| msg->rfc724_mid == NULL || msg->rfc724_mid[0] == 0 /* eg. device messages have no Message-ID */ ) {
if (!dc_msg_load_from_db(msg, context, job->foreign_id)
|| msg->rfc724_mid == NULL || msg->rfc724_mid[0] == 0 /* eg. device messages have no Message-ID */) {
goto cleanup;
}
if( dc_rfc724_mid_cnt(context, msg->rfc724_mid) != 1 ) {
if (dc_rfc724_mid_cnt(context, msg->rfc724_mid) != 1) {
dc_log_info(context, 0, "The message is deleted from the server when all parts are deleted.");
delete_from_server = 0;
}
/* if this is the last existing part of the message, we delete the message from the server */
if( delete_from_server )
if (delete_from_server)
{
if( !dc_imap_is_connected(context->imap) ) {
if (!dc_imap_is_connected(context->imap)) {
connect_to_imap(context, NULL);
if( !dc_imap_is_connected(context->imap) ) {
if (!dc_imap_is_connected(context->imap)) {
dc_job_try_again_later(job, DC_STANDARD_DELAY);
goto cleanup;
}
}
if( !dc_imap_delete_msg(context->imap, msg->rfc724_mid, msg->server_folder, msg->server_uid) )
if (!dc_imap_delete_msg(context->imap, msg->rfc724_mid, msg->server_folder, msg->server_uid))
{
dc_job_try_again_later(job, DC_AT_ONCE);
goto cleanup;
@ -167,8 +167,8 @@ static void dc_job_do_DC_JOB_DELETE_MSG_ON_IMAP(dc_context_t* context, dc_job_t*
stmt = NULL;
char* pathNfilename = dc_param_get(msg->param, DC_PARAM_FILE, NULL);
if( pathNfilename ) {
if( strncmp(context->blobdir, pathNfilename, strlen(context->blobdir))==0 )
if (pathNfilename) {
if (strncmp(context->blobdir, pathNfilename, strlen(context->blobdir))==0)
{
char* strLikeFilename = dc_mprintf("%%f=%s%%", pathNfilename);
stmt = dc_sqlite3_prepare(context->sql,
@ -180,7 +180,7 @@ static void dc_job_do_DC_JOB_DELETE_MSG_ON_IMAP(dc_context_t* context, dc_job_t*
sqlite3_finalize(stmt);
stmt = NULL;
if( !file_used_by_other_msgs )
if (!file_used_by_other_msgs)
{
dc_delete_file(pathNfilename, context);
@ -189,12 +189,12 @@ static void dc_job_do_DC_JOB_DELETE_MSG_ON_IMAP(dc_context_t* context, dc_job_t*
free(increation_file);
char* filenameOnly = dc_get_filename(pathNfilename);
if( msg->type==DC_MSG_VOICE ) {
if (msg->type==DC_MSG_VOICE) {
char* waveform_file = dc_mprintf("%s/%s.waveform", context->blobdir, filenameOnly);
dc_delete_file(waveform_file, context);
free(waveform_file);
}
else if( msg->type==DC_MSG_VIDEO ) {
else if (msg->type==DC_MSG_VIDEO) {
char* preview_file = dc_mprintf("%s/%s-preview.jpg", context->blobdir, filenameOnly);
dc_delete_file(preview_file, context);
free(preview_file);
@ -219,39 +219,39 @@ static void dc_job_do_DC_JOB_MARKSEEN_MSG_ON_IMAP(dc_context_t* context, dc_job_
int in_ms_flags = 0;
int out_ms_flags = 0;
if( !dc_imap_is_connected(context->imap) ) {
if (!dc_imap_is_connected(context->imap)) {
connect_to_imap(context, NULL);
if( !dc_imap_is_connected(context->imap) ) {
if (!dc_imap_is_connected(context->imap)) {
dc_job_try_again_later(job, DC_STANDARD_DELAY);
goto cleanup;
}
}
if( !dc_msg_load_from_db(msg, context, job->foreign_id) ) {
if (!dc_msg_load_from_db(msg, context, job->foreign_id)) {
goto cleanup;
}
/* add an additional job for sending the MDN (here in a thread for fast ui resonses) (an extra job as the MDN has a lower priority) */
if( dc_param_get_int(msg->param, DC_PARAM_WANTS_MDN, 0) /* DC_PARAM_WANTS_MDN is set only for one part of a multipart-message */
&& dc_sqlite3_get_config_int(context->sql, "mdns_enabled", DC_MDNS_DEFAULT_ENABLED) ) {
if (dc_param_get_int(msg->param, DC_PARAM_WANTS_MDN, 0) /* DC_PARAM_WANTS_MDN is set only for one part of a multipart-message */
&& dc_sqlite3_get_config_int(context->sql, "mdns_enabled", DC_MDNS_DEFAULT_ENABLED)) {
in_ms_flags |= DC_MS_SET_MDNSent_FLAG;
}
if( msg->is_msgrmsg ) {
if (msg->is_msgrmsg) {
in_ms_flags |= DC_MS_ALSO_MOVE;
}
if( dc_imap_markseen_msg(context->imap, msg->server_folder, msg->server_uid,
in_ms_flags, &new_server_folder, &new_server_uid, &out_ms_flags) != 0 )
if (dc_imap_markseen_msg(context->imap, msg->server_folder, msg->server_uid,
in_ms_flags, &new_server_folder, &new_server_uid, &out_ms_flags) != 0)
{
if( (new_server_folder && new_server_uid) || out_ms_flags&DC_MS_MDNSent_JUST_SET )
if ((new_server_folder && new_server_uid) || out_ms_flags&DC_MS_MDNSent_JUST_SET)
{
if( new_server_folder && new_server_uid )
if (new_server_folder && new_server_uid)
{
dc_update_server_uid(context, msg->rfc724_mid, new_server_folder, new_server_uid);
}
if( out_ms_flags&DC_MS_MDNSent_JUST_SET )
if (out_ms_flags&DC_MS_MDNSent_JUST_SET)
{
dc_job_add(context, DC_JOB_SEND_MDN, msg->id, NULL, 0);
}
@ -276,15 +276,15 @@ static void dc_job_do_DC_JOB_MARKSEEN_MDN_ON_IMAP(dc_context_t* context, dc_job_
uint32_t new_server_uid = 0;
int out_ms_flags = 0;
if( !dc_imap_is_connected(context->imap) ) {
if (!dc_imap_is_connected(context->imap)) {
connect_to_imap(context, NULL);
if( !dc_imap_is_connected(context->imap) ) {
if (!dc_imap_is_connected(context->imap)) {
dc_job_try_again_later(job, DC_STANDARD_DELAY);
goto cleanup;
}
}
if( dc_imap_markseen_msg(context->imap, server_folder, server_uid, DC_MS_ALSO_MOVE, &new_server_folder, &new_server_uid, &out_ms_flags) == 0 ) {
if (dc_imap_markseen_msg(context->imap, server_folder, server_uid, DC_MS_ALSO_MOVE, &new_server_folder, &new_server_uid, &out_ms_flags) == 0) {
dc_job_try_again_later(job, DC_AT_ONCE);
}
@ -301,7 +301,7 @@ cleanup:
static void mark_as_error(dc_context_t* context, dc_msg_t* msg)
{
if( context==NULL || msg==NULL ) {
if (context==NULL || msg==NULL) {
return;
}
@ -316,47 +316,47 @@ static void dc_job_do_DC_JOB_SEND_MSG_TO_SMTP(dc_context_t* context, dc_job_t* j
dc_mimefactory_init(&mimefactory, context);
/* connect to SMTP server, if not yet done */
if( !dc_smtp_is_connected(context->smtp) ) {
if (!dc_smtp_is_connected(context->smtp)) {
dc_loginparam_t* loginparam = dc_loginparam_new();
dc_loginparam_read(loginparam, context->sql, "configured_");
int connected = dc_smtp_connect(context->smtp, loginparam);
dc_loginparam_unref(loginparam);
if( !connected ) {
if (!connected) {
dc_job_try_again_later(job, DC_STANDARD_DELAY);
goto cleanup;
}
}
/* load message data */
if( !dc_mimefactory_load_msg(&mimefactory, job->foreign_id)
|| mimefactory.from_addr == NULL ) {
if (!dc_mimefactory_load_msg(&mimefactory, job->foreign_id)
|| mimefactory.from_addr == NULL) {
dc_log_warning(context, 0, "Cannot load data to send, maybe the message is deleted in between.");
goto cleanup; /* no redo, no IMAP - there won't be more recipients next time (as the data does not exist, there is no need in calling mark_as_error()) */
}
/* check if the message is ready (normally, only video files may be delayed this way) */
if( mimefactory.increation ) {
if (mimefactory.increation) {
dc_log_info(context, 0, "File is in creation, retrying later.");
dc_job_try_again_later(job, DC_INCREATION_POLL);
goto cleanup;
}
/* send message - it's okay if there are no recipients, this is a group with only OURSELF; we only upload to IMAP in this case */
if( clist_count(mimefactory.recipients_addr) > 0 ) {
if( !dc_mimefactory_render(&mimefactory) ) {
if (clist_count(mimefactory.recipients_addr) > 0) {
if (!dc_mimefactory_render(&mimefactory)) {
mark_as_error(context, mimefactory.msg);
dc_log_error(context, 0, "Empty message."); /* should not happen */
goto cleanup; /* no redo, no IMAP - there won't be more recipients next time. */
}
/* have we guaranteed encryption but cannot fulfill it for any reason? Do not send the message then.*/
if( dc_param_get_int(mimefactory.msg->param, DC_PARAM_GUARANTEE_E2EE, 0) && !mimefactory.out_encrypted ) {
if (dc_param_get_int(mimefactory.msg->param, DC_PARAM_GUARANTEE_E2EE, 0) && !mimefactory.out_encrypted) {
mark_as_error(context, mimefactory.msg);
dc_log_error(context, 0, "End-to-end-encryption unavailable unexpectedly.");
goto cleanup; /* unrecoverable */
}
if( !dc_smtp_send_msg(context->smtp, mimefactory.recipients_addr, mimefactory.out->str, mimefactory.out->len) ) {
if (!dc_smtp_send_msg(context->smtp, mimefactory.recipients_addr, mimefactory.out->str, mimefactory.out->len)) {
dc_smtp_disconnect(context->smtp);
dc_job_try_again_later(job, DC_AT_ONCE); /* DC_AT_ONCE is only the _initial_ delay, if the second try failes, the delay gets larger */
goto cleanup;
@ -367,11 +367,11 @@ static void dc_job_do_DC_JOB_SEND_MSG_TO_SMTP(dc_context_t* context, dc_job_t* j
dc_sqlite3_begin_transaction(context->sql);
/* debug print? */
if( dc_sqlite3_get_config_int(context->sql, "save_eml", 0) ) {
if (dc_sqlite3_get_config_int(context->sql, "save_eml", 0)) {
char* emlname = dc_mprintf("%s/to-smtp-%i.eml", context->blobdir, (int)mimefactory.msg->id);
FILE* emlfileob = fopen(emlname, "w");
if( emlfileob ) {
if( mimefactory.out ) {
if (emlfileob) {
if (mimefactory.out) {
fwrite(mimefactory.out->str, 1, mimefactory.out->len, emlfileob);
}
fclose(emlfileob);
@ -380,14 +380,14 @@ static void dc_job_do_DC_JOB_SEND_MSG_TO_SMTP(dc_context_t* context, dc_job_t* j
}
dc_update_msg_state(context, mimefactory.msg->id, DC_STATE_OUT_DELIVERED);
if( mimefactory.out_encrypted && dc_param_get_int(mimefactory.msg->param, DC_PARAM_GUARANTEE_E2EE, 0)==0 ) {
if (mimefactory.out_encrypted && dc_param_get_int(mimefactory.msg->param, DC_PARAM_GUARANTEE_E2EE, 0)==0) {
dc_param_set_int(mimefactory.msg->param, DC_PARAM_GUARANTEE_E2EE, 1); /* can upgrade to E2EE - fine! */
dc_msg_save_param_to_disk(mimefactory.msg);
}
if( (context->imap->server_flags&DC_NO_EXTRA_IMAP_UPLOAD)==0
if ((context->imap->server_flags&DC_NO_EXTRA_IMAP_UPLOAD)==0
&& dc_param_get(mimefactory.chat->param, DC_PARAM_SELFTALK, 0)==0
&& dc_param_get_int(mimefactory.msg->param, DC_PARAM_CMD, 0)!=DC_CMD_SECUREJOIN_MESSAGE ) {
&& dc_param_get_int(mimefactory.msg->param, DC_PARAM_CMD, 0)!=DC_CMD_SECUREJOIN_MESSAGE) {
dc_job_add(context, DC_JOB_SEND_MSG_TO_IMAP, mimefactory.msg->id, NULL, 0); /* send message to IMAP in another job */
}
@ -408,31 +408,31 @@ static void dc_job_do_DC_JOB_SEND_MDN(dc_context_t* context, dc_job_t* job)
dc_mimefactory_t mimefactory;
dc_mimefactory_init(&mimefactory, context);
if( context == NULL || context->magic != DC_CONTEXT_MAGIC || job == NULL ) {
if (context == NULL || context->magic != DC_CONTEXT_MAGIC || job == NULL) {
return;
}
/* connect to SMTP server, if not yet done */
if( !dc_smtp_is_connected(context->smtp) )
if (!dc_smtp_is_connected(context->smtp))
{
dc_loginparam_t* loginparam = dc_loginparam_new();
dc_loginparam_read(loginparam, context->sql, "configured_");
int connected = dc_smtp_connect(context->smtp, loginparam);
dc_loginparam_unref(loginparam);
if( !connected ) {
if (!connected) {
dc_job_try_again_later(job, DC_STANDARD_DELAY);
goto cleanup;
}
}
if( !dc_mimefactory_load_mdn(&mimefactory, job->foreign_id)
|| !dc_mimefactory_render(&mimefactory) ) {
if (!dc_mimefactory_load_mdn(&mimefactory, job->foreign_id)
|| !dc_mimefactory_render(&mimefactory)) {
goto cleanup;
}
//char* t1=dc_null_terminate(mimefactory.out->str,mimefactory.out->len);printf("~~~~~MDN~~~~~\n%s\n~~~~~/MDN~~~~~",t1);free(t1); // DEBUG OUTPUT
if( !dc_smtp_send_msg(context->smtp, mimefactory.recipients_addr, mimefactory.out->str, mimefactory.out->len) ) {
if (!dc_smtp_send_msg(context->smtp, mimefactory.recipients_addr, mimefactory.out->str, mimefactory.out->len)) {
dc_smtp_disconnect(context->smtp);
dc_job_try_again_later(job, DC_AT_ONCE); /* DC_AT_ONCE is only the _initial_ delay, if the second try failes, the delay gets larger */
goto cleanup;
@ -452,11 +452,11 @@ static void dc_suspend_smtp_thread(dc_context_t* context, int suspend)
// the smtp-thread may be in perform_jobs() when this function is called,
// wait until we arrive in idle(). for simplicity, we do this by polling a variable
// (in fact, this is only needed when calling configure() is called)
if( suspend )
if (suspend)
{
while( 1 ) {
while (1) {
pthread_mutex_lock(&context->smtpidle_condmutex);
if( context->smtpidle_in_idleing ) {
if (context->smtpidle_in_idleing) {
context->perform_smtp_jobs_needed = 0;
pthread_mutex_unlock(&context->smtpidle_condmutex);
return;
@ -479,10 +479,10 @@ void dc_job_add(dc_context_t* context, int action, int foreign_id, const char* p
sqlite3_stmt* stmt;
int thread;
if( action >= DC_IMAP_THREAD && action < DC_IMAP_THREAD+1000 ) {
if (action >= DC_IMAP_THREAD && action < DC_IMAP_THREAD+1000) {
thread = DC_IMAP_THREAD;
}
else if( action >= DC_SMTP_THREAD && action < DC_SMTP_THREAD+1000 ) {
else if (action >= DC_SMTP_THREAD && action < DC_SMTP_THREAD+1000) {
thread = DC_SMTP_THREAD;
}
else {
@ -500,7 +500,7 @@ void dc_job_add(dc_context_t* context, int action, int foreign_id, const char* p
sqlite3_step(stmt);
sqlite3_finalize(stmt);
if( thread == DC_IMAP_THREAD ) {
if (thread == DC_IMAP_THREAD) {
dc_interrupt_imap_idle(context);
}
else {
@ -511,7 +511,7 @@ void dc_job_add(dc_context_t* context, int action, int foreign_id, const char* p
void dc_job_try_again_later(dc_job_t* job, int try_again)
{
if( job == NULL ) {
if (job == NULL) {
return;
}
@ -521,7 +521,7 @@ void dc_job_try_again_later(dc_job_t* job, int try_again)
void dc_job_kill_actions(dc_context_t* context, int action1, int action2)
{
if( context == NULL ) {
if (context == NULL) {
return;
}
@ -544,7 +544,7 @@ static void dc_job_perform(dc_context_t* context, int thread)
memset(&job, 0, sizeof(dc_job_t));
job.param = dc_param_new();
if( context == NULL || context->magic != DC_CONTEXT_MAGIC ) {
if (context == NULL || context->magic != DC_CONTEXT_MAGIC) {
goto cleanup;
}
@ -552,7 +552,7 @@ static void dc_job_perform(dc_context_t* context, int thread)
"SELECT id, action, foreign_id, param FROM jobs WHERE thread=? AND desired_timestamp<=? ORDER BY action DESC, added_timestamp;");
sqlite3_bind_int64(select_stmt, 1, thread);
sqlite3_bind_int64(select_stmt, 2, time(NULL));
while( sqlite3_step(select_stmt) == SQLITE_ROW )
while (sqlite3_step(select_stmt) == SQLITE_ROW)
{
job.job_id = sqlite3_column_int (select_stmt, 0);
job.action = sqlite3_column_int (select_stmt, 1);
@ -572,11 +572,11 @@ static void dc_job_perform(dc_context_t* context, int thread)
dc_suspend_smtp_thread(context, 1);
}
for( int tries = 0; tries <= 1; tries++ )
for (int tries = 0; tries <= 1; tries++)
{
job.try_again = DC_DONT_TRY_AGAIN; // this can be modified by a job using dc_job_try_again_later()
switch( job.action ) {
switch (job.action) {
case DC_JOB_SEND_MSG_TO_SMTP: dc_job_do_DC_JOB_SEND_MSG_TO_SMTP (context, &job); break;
case DC_JOB_SEND_MSG_TO_IMAP: dc_job_do_DC_JOB_SEND_MSG_TO_IMAP (context, &job); break;
case DC_JOB_DELETE_MSG_ON_IMAP: dc_job_do_DC_JOB_DELETE_MSG_ON_IMAP (context, &job); break;
@ -587,7 +587,7 @@ static void dc_job_perform(dc_context_t* context, int thread)
case DC_JOB_IMEX_IMAP: dc_job_do_DC_JOB_IMEX_IMAP (context, &job); break;
}
if( job.try_again != DC_AT_ONCE ) {
if (job.try_again != DC_AT_ONCE) {
break;
}
}
@ -596,12 +596,12 @@ static void dc_job_perform(dc_context_t* context, int thread)
dc_suspend_smtp_thread(context, 0);
goto cleanup;
}
else if( job.try_again == DC_INCREATION_POLL )
else if (job.try_again == DC_INCREATION_POLL)
{
// just try over next loop unconditionally, the ui typically interrupts idle when the file (video) is ready
dc_log_info(context, 0, "%s-job #%i not yet ready and will be delayed.", THREAD_STR, (int)job.job_id);
}
else if( job.try_again == DC_AT_ONCE || job.try_again == DC_STANDARD_DELAY )
else if (job.try_again == DC_AT_ONCE || job.try_again == DC_STANDARD_DELAY)
{
int tries = dc_param_get_int(job.param, DC_PARAM_TIMES, 0) + 1;
dc_param_set_int(job.param, DC_PARAM_TIMES, tries);
@ -617,8 +617,8 @@ static void dc_job_perform(dc_context_t* context, int thread)
// if the job did not succeeded AND this is a smtp-job AND we're online, try over after a mini-delay of one second.
// if we're not online, the ui calls interrupt idle as soon as we're online again.
// if nothing of this happens, after DC_SMTP_IDLE_SEC (60) we try again.
if( thread == DC_SMTP_THREAD
&& dc_is_online(context) )
if (thread == DC_SMTP_THREAD
&& dc_is_online(context))
{
pthread_mutex_lock(&context->smtpidle_condmutex);
context->perform_smtp_jobs_needed = DC_JOBS_NEEDED_AVOID_DOS;
@ -686,7 +686,7 @@ void dc_perform_imap_fetch(dc_context_t* context)
{
clock_t start = clock();
if( !connect_to_imap(context, NULL) ) {
if (!connect_to_imap(context, NULL)) {
return;
}
@ -694,8 +694,8 @@ void dc_perform_imap_fetch(dc_context_t* context)
dc_imap_fetch(context->imap);
if( context->imap->should_reconnect
&& context->cb(context, DC_EVENT_IS_OFFLINE, 0, 0)==0 )
if (context->imap->should_reconnect
&& context->cb(context, DC_EVENT_IS_OFFLINE, 0, 0)==0)
{
dc_log_info(context, 0, "IMAP-fetch aborted, starting over...");
dc_imap_fetch(context->imap);
@ -723,7 +723,7 @@ void dc_perform_imap_idle(dc_context_t* context)
connect_to_imap(context, NULL); // also idle if connection fails because of not-configured, no-network, whatever. dc_imap_idle() will handle this by the fake-idle and log a warning
pthread_mutex_lock(&context->imapidle_condmutex);
if( context->perform_imap_jobs_needed ) {
if (context->perform_imap_jobs_needed) {
dc_log_info(context, 0, "IMAP-IDLE will not be started because of waiting jobs.");
pthread_mutex_unlock(&context->imapidle_condmutex);
return;
@ -750,7 +750,7 @@ void dc_perform_imap_idle(dc_context_t* context)
*
* void* imap_thread_func(void* context)
* {
* while( true ) {
* while (true) {
* dc_perform_imap_jobs(context);
* dc_perform_imap_fetch(context);
* dc_perform_imap_idle(context);
@ -774,7 +774,7 @@ void dc_perform_imap_idle(dc_context_t* context)
*/
void dc_interrupt_imap_idle(dc_context_t* context)
{
if( context == NULL || context->magic != DC_CONTEXT_MAGIC || context->imap == NULL ) {
if (context == NULL || context->magic != DC_CONTEXT_MAGIC || context->imap == NULL) {
dc_log_warning(context, 0, "Interrupt IMAP-IDLE: Bad parameters.");
return;
}
@ -835,7 +835,7 @@ void dc_perform_smtp_jobs(dc_context_t* context)
*/
void dc_perform_smtp_idle(dc_context_t* context)
{
if( context == NULL || context->magic != DC_CONTEXT_MAGIC ) {
if (context == NULL || context->magic != DC_CONTEXT_MAGIC) {
dc_log_warning(context, 0, "Cannot perform SMTP-idle: Bad parameters.");
return;
}
@ -844,7 +844,7 @@ void dc_perform_smtp_idle(dc_context_t* context)
pthread_mutex_lock(&context->smtpidle_condmutex);
if( context->perform_smtp_jobs_needed == DC_JOBS_NEEDED_AT_ONCE )
if (context->perform_smtp_jobs_needed == DC_JOBS_NEEDED_AT_ONCE)
{
dc_log_info(context, 0, "SMTP-idle will not be started because of waiting jobs.");
}
@ -884,7 +884,7 @@ void dc_perform_smtp_idle(dc_context_t* context)
*
* void* smtp_thread_func(void* context)
* {
* while( true ) {
* while (true) {
* dc_perform_smtp_jobs(context);
* dc_perform_smtp_idle(context);
* }
@ -906,7 +906,7 @@ void dc_perform_smtp_idle(dc_context_t* context)
*/
void dc_interrupt_smtp_idle(dc_context_t* context)
{
if( context == NULL || context->magic != DC_CONTEXT_MAGIC ) {
if (context == NULL || context->magic != DC_CONTEXT_MAGIC) {
dc_log_warning(context, 0, "Interrupt SMTP-idle: Bad parameters.");
return;
}

View file

@ -36,7 +36,7 @@
void dc_wipe_secret_mem(void* buf, size_t buf_bytes)
{
/* wipe private keys or othere secrets with zeros so that secrets are no longer in RAM */
if( buf == NULL || buf_bytes <= 0 ) {
if (buf == NULL || buf_bytes <= 0) {
return;
}
@ -46,11 +46,11 @@ void dc_wipe_secret_mem(void* buf, size_t buf_bytes)
static void dc_key_empty(dc_key_t* key) /* only use before calling setters; take care when using this function together with reference counting, prefer new objects instead */
{
if( key == NULL ) {
if (key == NULL) {
return;
}
if( key->type==DC_KEY_PRIVATE ) {
if (key->type==DC_KEY_PRIVATE) {
dc_wipe_secret_mem(key->binary, key->bytes);
}
@ -65,7 +65,7 @@ dc_key_t* dc_key_new()
{
dc_key_t* key;
if( (key=calloc(1, sizeof(dc_key_t)))==NULL ) {
if ((key=calloc(1, sizeof(dc_key_t)))==NULL) {
exit(44); /* cannot allocate little memory, unrecoverable error */
}
key->_m_heap_refcnt = 1;
@ -75,7 +75,7 @@ dc_key_t* dc_key_new()
dc_key_t* dc_key_ref(dc_key_t* key)
{
if( key==NULL ) {
if (key==NULL) {
return NULL;
}
key->_m_heap_refcnt++;
@ -85,12 +85,12 @@ dc_key_t* dc_key_ref(dc_key_t* key)
void dc_key_unref(dc_key_t* key)
{
if( key==NULL ) {
if (key==NULL) {
return;
}
key->_m_heap_refcnt--;
if( key->_m_heap_refcnt != 0 ) {
if (key->_m_heap_refcnt != 0) {
return;
}
@ -102,11 +102,11 @@ void dc_key_unref(dc_key_t* key)
int dc_key_set_from_binary(dc_key_t* key, const void* data, int bytes, int type)
{
dc_key_empty(key);
if( key==NULL || data==NULL || bytes <= 0 ) {
if (key==NULL || data==NULL || bytes <= 0) {
return 0;
}
key->binary = malloc(bytes);
if( key->binary == NULL ) {
if (key->binary == NULL) {
exit(40);
}
memcpy(key->binary, data, bytes);
@ -119,7 +119,7 @@ int dc_key_set_from_binary(dc_key_t* key, const void* data, int bytes, int type)
int dc_key_set_from_key(dc_key_t* key, const dc_key_t* o)
{
dc_key_empty(key);
if( key==NULL || o==NULL ) {
if (key==NULL || o==NULL) {
return 0;
}
return dc_key_set_from_binary(key, o->binary, o->bytes, o->type);
@ -129,7 +129,7 @@ int dc_key_set_from_key(dc_key_t* key, const dc_key_t* o)
int dc_key_set_from_stmt(dc_key_t* key, sqlite3_stmt* stmt, int index, int type)
{
dc_key_empty(key);
if( key==NULL || stmt==NULL ) {
if (key==NULL || stmt==NULL) {
return 0;
}
return dc_key_set_from_binary(key, (unsigned char*)sqlite3_column_blob(stmt, index), sqlite3_column_bytes(stmt, index), type);
@ -143,12 +143,12 @@ int dc_key_set_from_base64(dc_key_t* key, const char* base64, int type)
dc_key_empty(key);
if( key==NULL || base64==NULL ) {
if (key==NULL || base64==NULL) {
return 0;
}
if( mailmime_base64_body_parse(base64, strlen(base64), &indx, &result/*must be freed using mmap_string_unref()*/, &result_len)!=MAILIMF_NO_ERROR
|| result == NULL || result_len == 0 ) {
if (mailmime_base64_body_parse(base64, strlen(base64), &indx, &result/*must be freed using mmap_string_unref()*/, &result_len)!=MAILIMF_NO_ERROR
|| result == NULL || result_len == 0) {
return 0; /* bad key */
}
@ -168,24 +168,24 @@ int dc_key_set_from_file(dc_key_t* key, const char* pathNfilename, dc_context_t*
dc_key_empty(key);
if( key==NULL || pathNfilename==NULL ) {
if (key==NULL || pathNfilename==NULL) {
goto cleanup;
}
if( !dc_read_file(pathNfilename, (void**)&buf, &buf_bytes, context)
|| buf_bytes < 50 ) {
if (!dc_read_file(pathNfilename, (void**)&buf, &buf_bytes, context)
|| buf_bytes < 50) {
goto cleanup; /* error is already loged */
}
if( !dc_split_armored_data(buf, &headerline, NULL, NULL, &base64)
|| headerline == NULL || base64 == NULL ) {
if (!dc_split_armored_data(buf, &headerline, NULL, NULL, &base64)
|| headerline == NULL || base64 == NULL) {
goto cleanup;
}
if( strcmp(headerline, "-----BEGIN PGP PUBLIC KEY BLOCK-----")==0 ) {
if (strcmp(headerline, "-----BEGIN PGP PUBLIC KEY BLOCK-----")==0) {
type = DC_KEY_PUBLIC;
}
else if( strcmp(headerline, "-----BEGIN PGP PRIVATE KEY BLOCK-----")==0 ) {
else if (strcmp(headerline, "-----BEGIN PGP PRIVATE KEY BLOCK-----")==0) {
type = DC_KEY_PRIVATE;
}
else {
@ -193,7 +193,7 @@ int dc_key_set_from_file(dc_key_t* key, const char* pathNfilename, dc_context_t*
goto cleanup;
}
if( !dc_key_set_from_base64(key, base64, type) ) {
if (!dc_key_set_from_base64(key, base64, type)) {
dc_log_warning(context, 0, "Bad data in key \"%s\".", pathNfilename);
goto cleanup;
}
@ -208,16 +208,16 @@ cleanup:
int dc_key_equals(const dc_key_t* key, const dc_key_t* o)
{
if( key==NULL || o==NULL
|| key->binary==NULL || key->bytes<=0 || o->binary==NULL || o->bytes<=0 ) {
if (key==NULL || o==NULL
|| key->binary==NULL || key->bytes<=0 || o->binary==NULL || o->bytes<=0) {
return 0; /*error*/
}
if( key->bytes != o->bytes ) {
if (key->bytes != o->bytes) {
return 0; /*different size -> the keys cannot be equal*/
}
if( key->type != o->type ) {
if (key->type != o->type) {
return 0; /* cannot compare public with private keys */
}
@ -235,8 +235,8 @@ int dc_key_save_self_keypair(const dc_key_t* public_key, const dc_key_t* private
int success = 0;
sqlite3_stmt* stmt = NULL;
if( public_key==NULL || private_key==NULL || addr==NULL || sql==NULL
|| public_key->binary==NULL || private_key->binary==NULL ) {
if (public_key==NULL || private_key==NULL || addr==NULL || sql==NULL
|| public_key->binary==NULL || private_key->binary==NULL) {
goto cleanup;
}
@ -247,7 +247,7 @@ int dc_key_save_self_keypair(const dc_key_t* public_key, const dc_key_t* private
sqlite3_bind_blob (stmt, 3, public_key->binary, public_key->bytes, SQLITE_STATIC);
sqlite3_bind_blob (stmt, 4, private_key->binary, private_key->bytes, SQLITE_STATIC);
sqlite3_bind_int64(stmt, 5, time(NULL));
if( sqlite3_step(stmt) != SQLITE_DONE ) {
if (sqlite3_step(stmt) != SQLITE_DONE) {
goto cleanup;
}
@ -264,7 +264,7 @@ int dc_key_load_self_public(dc_key_t* key, const char* self_addr, dc_sqlite3_t*
int success = 0;
sqlite3_stmt* stmt = NULL;
if( key==NULL || self_addr==NULL || sql==NULL ) {
if (key==NULL || self_addr==NULL || sql==NULL) {
goto cleanup;
}
@ -272,7 +272,7 @@ int dc_key_load_self_public(dc_key_t* key, const char* self_addr, dc_sqlite3_t*
stmt = dc_sqlite3_prepare(sql,
"SELECT public_key FROM keypairs WHERE addr=? AND is_default=1;");
sqlite3_bind_text (stmt, 1, self_addr, -1, SQLITE_STATIC);
if( sqlite3_step(stmt) != SQLITE_ROW ) {
if (sqlite3_step(stmt) != SQLITE_ROW) {
goto cleanup;
}
dc_key_set_from_stmt(key, stmt, 0, DC_KEY_PUBLIC);
@ -289,7 +289,7 @@ int dc_key_load_self_private(dc_key_t* key, const char* self_addr, dc_sqlite3_t*
int success = 0;
sqlite3_stmt* stmt = NULL;
if( key==NULL || self_addr==NULL || sql==NULL ) {
if (key==NULL || self_addr==NULL || sql==NULL) {
goto cleanup;
}
@ -297,7 +297,7 @@ int dc_key_load_self_private(dc_key_t* key, const char* self_addr, dc_sqlite3_t*
stmt = dc_sqlite3_prepare(sql,
"SELECT private_key FROM keypairs WHERE addr=? AND is_default=1;");
sqlite3_bind_text (stmt, 1, self_addr, -1, SQLITE_STATIC);
if( sqlite3_step(stmt) != SQLITE_ROW ) {
if (sqlite3_step(stmt) != SQLITE_ROW) {
goto cleanup;
}
dc_key_set_from_stmt(key, stmt, 0, DC_KEY_PRIVATE);
@ -337,16 +337,16 @@ char* dc_render_base64(const void* buf, size_t buf_bytes, int break_every, const
{
char* ret = NULL;
if( buf==NULL || buf_bytes<=0 ) {
if (buf==NULL || buf_bytes<=0) {
goto cleanup;
}
if( (ret = encode_base64((const char*)buf, buf_bytes))==NULL ) {
if ((ret = encode_base64((const char*)buf, buf_bytes))==NULL) {
goto cleanup;
}
#if 0
if( add_checksum == 1/*appended checksum*/ ) {
if (add_checksum == 1/*appended checksum*/) {
long checksum = crc_octets(buf, buf_bytes);
uint8_t c[3];
c[0] = (uint8_t)((checksum >> 16)&0xFF);
@ -360,13 +360,13 @@ char* dc_render_base64(const void* buf, size_t buf_bytes, int break_every, const
}
#endif
if( break_every>0 ) {
if (break_every>0) {
char* temp = ret;
ret = dc_insert_breaks(temp, break_every, break_chars);
free(temp);
}
if( add_checksum == 2/*checksum with break character*/ ) {
if (add_checksum == 2/*checksum with break character*/) {
long checksum = crc_octets(buf, buf_bytes);
uint8_t c[3];
c[0] = (uint8_t)((checksum >> 16)&0xFF);
@ -386,7 +386,7 @@ cleanup:
char* dc_key_render_base64(const dc_key_t* key, int break_every, const char* break_chars, int add_checksum)
{
if( key==NULL ) {
if (key==NULL) {
return NULL;
}
return dc_render_base64(key->binary, key->bytes, break_every, break_chars, add_checksum);
@ -398,11 +398,11 @@ char* dc_key_render_asc(const dc_key_t* key, const char* add_header_lines /*must
/* see RFC 4880, 6.2. Forming ASCII Armor, https://tools.ietf.org/html/rfc4880#section-6.2 */
char *base64 = NULL, *ret = NULL;
if( key==NULL ) {
if (key==NULL) {
goto cleanup;
}
if( (base64=dc_key_render_base64(key, 76, "\r\n", 2/*checksum in new line*/))==NULL ) { /* RFC: The encoded output stream must be represented in lines of no more than 76 characters each. */
if ((base64=dc_key_render_base64(key, 76, "\r\n", 2/*checksum in new line*/))==NULL) { /* RFC: The encoded output stream must be represented in lines of no more than 76 characters each. */
goto cleanup;
}
@ -423,16 +423,16 @@ int dc_key_render_asc_to_file(const dc_key_t* key, const char* file, dc_context_
int success = 0;
char* file_content = NULL;
if( key == NULL || file == NULL || context == NULL ) {
if (key == NULL || file == NULL || context == NULL) {
goto cleanup;
}
file_content = dc_key_render_asc(key, NULL);
if( file_content == NULL ) {
if (file_content == NULL) {
goto cleanup;
}
if( !dc_write_file(file, file_content, strlen(file_content), context) ) {
if (!dc_write_file(file, file_content, strlen(file_content), context)) {
dc_log_error(context, 0, "Cannot write key to %s", file);
goto cleanup;
}
@ -450,14 +450,14 @@ char* dc_format_fingerprint(const char* fingerprint)
dc_strbuilder_t ret;
dc_strbuilder_init(&ret, 0);
while( fingerprint[i] ) {
while (fingerprint[i]) {
dc_strbuilder_catf(&ret, "%c", fingerprint[i]);
i++;
if( i != fingerprint_len ) {
if( i%20 == 0 ) {
if (i != fingerprint_len) {
if (i%20 == 0) {
dc_strbuilder_cat(&ret, "\n");
}
else if( i%4 == 0 ) {
else if (i%4 == 0) {
dc_strbuilder_cat(&ret, " ");
}
}
@ -471,7 +471,7 @@ char* dc_format_fingerprint(const char* fingerprint)
40-characters-uppercase-hex format */
char* dc_normalize_fingerprint(const char* in)
{
if( in == NULL ) {
if (in == NULL) {
return NULL;
}
@ -479,8 +479,8 @@ char* dc_normalize_fingerprint(const char* in)
dc_strbuilder_init(&out, 0);
const char* p1 = in;
while( *p1 ) {
if( (*p1 >= '0' && *p1 <= '9') || (*p1 >= 'A' && *p1 <= 'F') || (*p1 >= 'a' && *p1 <= 'f') ) {
while (*p1) {
if ((*p1 >= '0' && *p1 <= '9') || (*p1 >= 'A' && *p1 <= 'F') || (*p1 >= 'a' && *p1 <= 'f')) {
dc_strbuilder_catf(&out, "%c", toupper(*p1)); /* make uppercase which is needed as we do not search case-insensitive, see comment in dc_sqlite3.c */
}
p1++;
@ -496,11 +496,11 @@ char* dc_key_get_fingerprint(const dc_key_t* key)
size_t fingerprint_bytes = 0;
char* fingerprint_hex = NULL;
if( key == NULL ) {
if (key == NULL) {
goto cleanup;
}
if( !dc_pgp_calc_fingerprint(key, &fingerprint_buf, &fingerprint_bytes) ) {
if (!dc_pgp_calc_fingerprint(key, &fingerprint_buf, &fingerprint_bytes)) {
goto cleanup;
}

View file

@ -36,7 +36,7 @@ dc_keyring_t* dc_keyring_new()
{
dc_keyring_t* keyring;
if( (keyring=calloc(1, sizeof(dc_keyring_t)))==NULL ) {
if ((keyring=calloc(1, sizeof(dc_keyring_t)))==NULL) {
exit(42); /* cannot allocate little memory, unrecoverable error */
}
return keyring;
@ -46,11 +46,11 @@ dc_keyring_t* dc_keyring_new()
void dc_keyring_unref(dc_keyring_t* keyring)
{
int i;
if( keyring == NULL ) {
if (keyring == NULL) {
return;
}
for( i = 0; i < keyring->count; i++ ) {
for (i = 0; i < keyring->count; i++) {
dc_key_unref(keyring->keys[i]);
}
free(keyring->keys);
@ -60,14 +60,14 @@ void dc_keyring_unref(dc_keyring_t* keyring)
void dc_keyring_add(dc_keyring_t* keyring, dc_key_t* to_add)
{
if( keyring==NULL || to_add==NULL ) {
if (keyring==NULL || to_add==NULL) {
return;
}
/* expand array, if needed */
if( keyring->count == keyring->allocated ) {
if (keyring->count == keyring->allocated) {
int newsize = (keyring->allocated * 2) + 10;
if( (keyring->keys=realloc(keyring->keys, newsize*sizeof(dc_key_t*)))==NULL ) {
if ((keyring->keys=realloc(keyring->keys, newsize*sizeof(dc_key_t*)))==NULL) {
exit(41);
}
keyring->allocated = newsize;
@ -80,16 +80,16 @@ void dc_keyring_add(dc_keyring_t* keyring, dc_key_t* to_add)
int dc_keyring_load_self_private_for_decrypting(dc_keyring_t* keyring, const char* self_addr, dc_sqlite3_t* sql)
{
if( keyring==NULL || self_addr==NULL || sql==NULL ) {
if (keyring==NULL || self_addr==NULL || sql==NULL) {
return 0;
}
sqlite3_stmt* stmt = dc_sqlite3_prepare(sql,
"SELECT private_key FROM keypairs ORDER BY addr=? DESC, is_default DESC;");
sqlite3_bind_text (stmt, 1, self_addr, -1, SQLITE_STATIC);
while( sqlite3_step(stmt) == SQLITE_ROW ) {
while (sqlite3_step(stmt) == SQLITE_ROW) {
dc_key_t* key = dc_key_new();
if( dc_key_set_from_stmt(key, stmt, 0, DC_KEY_PRIVATE) ) {
if (dc_key_set_from_stmt(key, stmt, 0, DC_KEY_PRIVATE)) {
dc_keyring_add(keyring, key);
}
dc_key_unref(key); /* unref in any case, dc_keyring_add() adds its own reference */

View file

@ -43,20 +43,20 @@ static void log_vprintf(dc_context_t* context, int event, int code, const char*
{
char* msg = NULL;
if( context==NULL || context->magic != DC_CONTEXT_MAGIC ) {
if (context==NULL || context->magic != DC_CONTEXT_MAGIC) {
return;
}
/* format message from variable parameters or translate very comming errors */
if( code == DC_ERROR_SELF_NOT_IN_GROUP )
if (code == DC_ERROR_SELF_NOT_IN_GROUP)
{
msg = dc_stock_str(context, DC_STR_SELFNOTINGRP);
}
else if( code == DC_ERROR_NO_NETWORK )
else if (code == DC_ERROR_NO_NETWORK)
{
msg = dc_stock_str(context, DC_STR_NONETWORK);
}
else if( msg_format )
else if (msg_format)
{
#define BUFSIZE 1024
char tempbuf[BUFSIZE+1];
@ -65,9 +65,9 @@ static void log_vprintf(dc_context_t* context, int event, int code, const char*
}
/* if we have still no message, create one based upon the code */
if( msg == NULL ) {
if( event == DC_EVENT_INFO ) { msg = dc_mprintf("Info: %i", (int)code); }
else if( event == DC_EVENT_WARNING ) { msg = dc_mprintf("Warning: %i", (int)code); }
if (msg == NULL) {
if (event == DC_EVENT_INFO) { msg = dc_mprintf("Info: %i", (int)code); }
else if (event == DC_EVENT_WARNING) { msg = dc_mprintf("Warning: %i", (int)code); }
else { msg = dc_mprintf("Error: %i", (int)code); }
}
@ -114,15 +114,15 @@ void dc_log_error(dc_context_t* context, int code, const char* msg, ...)
void dc_log_error_if(int* condition, dc_context_t* context, int code, const char* msg, ...)
{
if( condition == NULL || context==NULL || context->magic != DC_CONTEXT_MAGIC ) {
if (condition == NULL || context==NULL || context->magic != DC_CONTEXT_MAGIC) {
return;
}
va_list va;
va_start(va, msg);
if( *condition ) {
if (*condition) {
/* pop-up error, if we're offline, force a "not connected" error (the function is not used for other cases) */
if( context->cb(context, DC_EVENT_IS_OFFLINE, 0, 0)!=0 ) {
if (context->cb(context, DC_EVENT_IS_OFFLINE, 0, 0)!=0) {
log_vprintf(context, DC_EVENT_ERROR, DC_ERROR_NO_NETWORK, NULL, va);
}
else {

View file

@ -33,7 +33,7 @@ dc_loginparam_t* dc_loginparam_new()
{
dc_loginparam_t* loginparam = NULL;
if( (loginparam=calloc(1, sizeof(dc_loginparam_t)))==NULL ) {
if ((loginparam=calloc(1, sizeof(dc_loginparam_t)))==NULL) {
exit(22); /* cannot allocate little memory, unrecoverable error */
}
@ -43,7 +43,7 @@ dc_loginparam_t* dc_loginparam_new()
void dc_loginparam_unref(dc_loginparam_t* loginparam)
{
if( loginparam==NULL ) {
if (loginparam==NULL) {
return;
}
@ -54,7 +54,7 @@ void dc_loginparam_unref(dc_loginparam_t* loginparam)
void dc_loginparam_empty(dc_loginparam_t* loginparam)
{
if( loginparam == NULL ) {
if (loginparam == NULL) {
return; /* ok, but nothing to do */
}
@ -122,11 +122,11 @@ static char* get_readable_flags(int flags)
{
dc_strbuilder_t strbuilder;
dc_strbuilder_init(&strbuilder, 0);
#define CAT_FLAG(f, s) if( (1<<bit)==(f) ) { dc_strbuilder_cat(&strbuilder, (s)); flag_added = 1; }
#define CAT_FLAG(f, s) if ((1<<bit)==(f)) { dc_strbuilder_cat(&strbuilder, (s)); flag_added = 1; }
for( int bit = 0; bit <= 30; bit++ )
for (int bit = 0; bit <= 30; bit++)
{
if( flags&(1<<bit) )
if (flags&(1<<bit))
{
int flag_added = 0;
@ -144,13 +144,13 @@ static char* get_readable_flags(int flags)
CAT_FLAG(DC_NO_EXTRA_IMAP_UPLOAD, "NO_EXTRA_IMAP_UPLOAD ");
CAT_FLAG(DC_NO_MOVE_TO_CHATS, "NO_MOVE_TO_CHATS ");
if( !flag_added ) {
if (!flag_added) {
char* temp = dc_mprintf("0x%x ", 1<<bit); dc_strbuilder_cat(&strbuilder, temp); free(temp);
}
}
}
if( strbuilder.buf[0]==0 ) { dc_strbuilder_cat(&strbuilder, "0"); }
if (strbuilder.buf[0]==0) { dc_strbuilder_cat(&strbuilder, "0"); }
dc_trim(strbuilder.buf);
return strbuilder.buf;
}
@ -161,7 +161,7 @@ char* dc_loginparam_get_readable(const dc_loginparam_t* loginparam)
const char* unset = "0";
const char* pw = "***";
if( loginparam==NULL ) {
if (loginparam==NULL) {
return dc_strdup(NULL);
}

View file

@ -30,7 +30,7 @@ dc_lot_t* dc_lot_new()
{
dc_lot_t* lot = NULL;
if( (lot=calloc(1, sizeof(dc_lot_t)))==NULL ) {
if ((lot=calloc(1, sizeof(dc_lot_t)))==NULL) {
exit(27); /* cannot allocate little memory, unrecoverable error */
}
@ -53,7 +53,7 @@ dc_lot_t* dc_lot_new()
*/
void dc_lot_unref(dc_lot_t* set)
{
if( set==NULL || set->magic != DC_LOT_MAGIC ) {
if (set==NULL || set->magic != DC_LOT_MAGIC) {
return;
}
@ -65,7 +65,7 @@ void dc_lot_unref(dc_lot_t* set)
void dc_lot_empty(dc_lot_t* lot)
{
if( lot == NULL || lot->magic != DC_LOT_MAGIC ) {
if (lot == NULL || lot->magic != DC_LOT_MAGIC) {
return;
}
@ -100,7 +100,7 @@ void dc_lot_empty(dc_lot_t* lot)
*/
char* dc_lot_get_text1(dc_lot_t* lot)
{
if( lot == NULL || lot->magic != DC_LOT_MAGIC ) {
if (lot == NULL || lot->magic != DC_LOT_MAGIC) {
return NULL;
}
return dc_strdup_keep_null(lot->text1);
@ -118,7 +118,7 @@ char* dc_lot_get_text1(dc_lot_t* lot)
*/
char* dc_lot_get_text2(dc_lot_t* lot)
{
if( lot == NULL || lot->magic != DC_LOT_MAGIC ) {
if (lot == NULL || lot->magic != DC_LOT_MAGIC) {
return NULL;
}
return dc_strdup_keep_null(lot->text2);
@ -136,7 +136,7 @@ char* dc_lot_get_text2(dc_lot_t* lot)
*/
int dc_lot_get_text1_meaning(dc_lot_t* lot)
{
if( lot == NULL || lot->magic != DC_LOT_MAGIC ) {
if (lot == NULL || lot->magic != DC_LOT_MAGIC) {
return 0;
}
return lot->text1_meaning;
@ -154,7 +154,7 @@ int dc_lot_get_text1_meaning(dc_lot_t* lot)
*/
int dc_lot_get_state(dc_lot_t* lot)
{
if( lot == NULL || lot->magic != DC_LOT_MAGIC ) {
if (lot == NULL || lot->magic != DC_LOT_MAGIC) {
return 0;
}
return lot->state;
@ -170,7 +170,7 @@ int dc_lot_get_state(dc_lot_t* lot)
*/
uint32_t dc_lot_get_id(dc_lot_t* lot)
{
if( lot == NULL || lot->magic != DC_LOT_MAGIC ) {
if (lot == NULL || lot->magic != DC_LOT_MAGIC) {
return 0;
}
return lot->id;
@ -188,7 +188,7 @@ uint32_t dc_lot_get_id(dc_lot_t* lot)
*/
time_t dc_lot_get_timestamp(dc_lot_t* lot)
{
if( lot == NULL || lot->magic != DC_LOT_MAGIC ) {
if (lot == NULL || lot->magic != DC_LOT_MAGIC) {
return 0;
}
return lot->timestamp;
@ -197,13 +197,13 @@ time_t dc_lot_get_timestamp(dc_lot_t* lot)
void dc_lot_fill(dc_lot_t* lot, const dc_msg_t* msg, const dc_chat_t* chat, const dc_contact_t* contact, dc_context_t* context)
{
if( lot == NULL || lot->magic != DC_LOT_MAGIC || msg == NULL ) {
if (lot == NULL || lot->magic != DC_LOT_MAGIC || msg == NULL) {
return;
}
if( msg->from_id == DC_CONTACT_ID_SELF )
if (msg->from_id == DC_CONTACT_ID_SELF)
{
if( dc_msg_is_info(msg) ) {
if (dc_msg_is_info(msg)) {
lot->text1 = NULL;
lot->text1_meaning = 0;
}
@ -212,14 +212,14 @@ void dc_lot_fill(dc_lot_t* lot, const dc_msg_t* msg, const dc_chat_t* chat, cons
lot->text1_meaning = DC_TEXT1_SELF;
}
}
else if( chat == NULL )
else if (chat == NULL)
{
lot->text1 = NULL;
lot->text1_meaning = 0;
}
else if( DC_CHAT_TYPE_IS_MULTI(chat->type) )
else if (DC_CHAT_TYPE_IS_MULTI(chat->type))
{
if( dc_msg_is_info(msg) || contact==NULL ) {
if (dc_msg_is_info(msg) || contact==NULL) {
lot->text1 = NULL;
lot->text1_meaning = 0;
}

View file

@ -36,7 +36,7 @@
void dc_mimefactory_init(dc_mimefactory_t* factory, dc_context_t* context)
{
if( factory == NULL || context == NULL ) {
if (factory == NULL || context == NULL) {
return;
}
@ -47,7 +47,7 @@ void dc_mimefactory_init(dc_mimefactory_t* factory, dc_context_t* context)
void dc_mimefactory_empty(dc_mimefactory_t* factory)
{
if( factory == NULL ) {
if (factory == NULL) {
return;
}
@ -60,13 +60,13 @@ void dc_mimefactory_empty(dc_mimefactory_t* factory)
free(factory->selfstatus);
factory->selfstatus = NULL;
if( factory->recipients_names ) {
if (factory->recipients_names) {
clist_free_content(factory->recipients_names);
clist_free(factory->recipients_names);
factory->recipients_names = NULL;
}
if( factory->recipients_addr ) {
if (factory->recipients_addr) {
clist_free_content(factory->recipients_addr);
clist_free(factory->recipients_addr);
factory->recipients_addr = NULL;
@ -78,7 +78,7 @@ void dc_mimefactory_empty(dc_mimefactory_t* factory)
dc_chat_unref(factory->chat);
factory->chat = NULL;
if( factory->out ) {
if (factory->out) {
mmap_string_free(factory->out);
factory->out = NULL;
}
@ -95,7 +95,7 @@ static void load_from(dc_mimefactory_t* factory)
factory->from_displayname = dc_sqlite3_get_config(factory->context->sql, "displayname", NULL);
factory->selfstatus = dc_sqlite3_get_config(factory->context->sql, "selfstatus", NULL);
if( factory->selfstatus == NULL ) {
if (factory->selfstatus == NULL) {
factory->selfstatus = dc_stock_str(factory->context, DC_STR_STATUSLINE);
}
}
@ -106,9 +106,9 @@ int dc_mimefactory_load_msg(dc_mimefactory_t* factory, uint32_t msg_id)
int success = 0;
sqlite3_stmt* stmt = NULL;
if( factory == NULL || msg_id <= DC_MSG_ID_LAST_SPECIAL
if (factory == NULL || msg_id <= DC_MSG_ID_LAST_SPECIAL
|| factory->context == NULL
|| factory->msg /*call empty() before */ ) {
|| factory->msg /*call empty() before */) {
goto cleanup;
}
@ -119,14 +119,14 @@ int dc_mimefactory_load_msg(dc_mimefactory_t* factory, uint32_t msg_id)
factory->msg = dc_msg_new();
factory->chat = dc_chat_new(context);
if( dc_msg_load_from_db(factory->msg, context, msg_id)
&& dc_chat_load_from_db(factory->chat, factory->msg->chat_id) )
if (dc_msg_load_from_db(factory->msg, context, msg_id)
&& dc_chat_load_from_db(factory->chat, factory->msg->chat_id))
{
load_from(factory);
factory->req_mdn = 0;
if( dc_chat_is_self_talk(factory->chat) )
if (dc_chat_is_self_talk(factory->chat))
{
clist_append(factory->recipients_names, (void*)dc_strdup_keep_null(factory->from_displayname));
clist_append(factory->recipients_addr, (void*)dc_strdup(factory->from_addr));
@ -139,11 +139,11 @@ int dc_mimefactory_load_msg(dc_mimefactory_t* factory, uint32_t msg_id)
" LEFT JOIN contacts c ON cc.contact_id=c.id "
" WHERE cc.chat_id=? AND cc.contact_id>" DC_STRINGIFY(DC_CONTACT_ID_LAST_SPECIAL) ";");
sqlite3_bind_int(stmt, 1, factory->msg->chat_id);
while( sqlite3_step(stmt) == SQLITE_ROW )
while (sqlite3_step(stmt) == SQLITE_ROW)
{
const char* authname = (const char*)sqlite3_column_text(stmt, 0);
const char* addr = (const char*)sqlite3_column_text(stmt, 1);
if( clist_search_string_nocase(factory->recipients_addr, addr)==0 )
if (clist_search_string_nocase(factory->recipients_addr, addr)==0)
{
clist_append(factory->recipients_names, (void*)((authname&&authname[0])? dc_strdup(authname) : NULL));
clist_append(factory->recipients_addr, (void*)dc_strdup(addr));
@ -153,12 +153,12 @@ int dc_mimefactory_load_msg(dc_mimefactory_t* factory, uint32_t msg_id)
stmt = NULL;
int command = dc_param_get_int(factory->msg->param, DC_PARAM_CMD, 0);
if( command==DC_CMD_MEMBER_REMOVED_FROM_GROUP /* for added members, the list is just fine */) {
if (command==DC_CMD_MEMBER_REMOVED_FROM_GROUP /* for added members, the list is just fine */) {
char* email_to_remove = dc_param_get(factory->msg->param, DC_PARAM_CMD_ARG, NULL);
char* self_addr = dc_sqlite3_get_config(context->sql, "configured_addr", "");
if( email_to_remove && strcasecmp(email_to_remove, self_addr)!=0 )
if (email_to_remove && strcasecmp(email_to_remove, self_addr)!=0)
{
if( clist_search_string_nocase(factory->recipients_addr, email_to_remove)==0 )
if (clist_search_string_nocase(factory->recipients_addr, email_to_remove)==0)
{
clist_append(factory->recipients_names, NULL);
clist_append(factory->recipients_addr, (void*)email_to_remove);
@ -167,9 +167,9 @@ int dc_mimefactory_load_msg(dc_mimefactory_t* factory, uint32_t msg_id)
free(self_addr);
}
if( command!=DC_CMD_AUTOCRYPT_SETUP_MESSAGE
if (command!=DC_CMD_AUTOCRYPT_SETUP_MESSAGE
&& command!=DC_CMD_SECUREJOIN_MESSAGE
&& dc_sqlite3_get_config_int(context->sql, "mdns_enabled", DC_MDNS_DEFAULT_ENABLED) ) {
&& dc_sqlite3_get_config_int(context->sql, "mdns_enabled", DC_MDNS_DEFAULT_ENABLED)) {
factory->req_mdn = 1;
}
}
@ -190,7 +190,7 @@ int dc_mimefactory_load_msg(dc_mimefactory_t* factory, uint32_t msg_id)
"SELECT rfc724_mid FROM msgs WHERE timestamp=(SELECT max(timestamp) FROM msgs WHERE chat_id=? AND from_id!=?);");
sqlite3_bind_int (stmt, 1, factory->msg->chat_id);
sqlite3_bind_int (stmt, 2, DC_CONTACT_ID_SELF);
if( sqlite3_step(stmt) == SQLITE_ROW ) {
if (sqlite3_step(stmt) == SQLITE_ROW) {
factory->predecessor = dc_strdup_keep_null((const char*)sqlite3_column_text(stmt, 0));
}
sqlite3_finalize(stmt);
@ -208,18 +208,18 @@ int dc_mimefactory_load_msg(dc_mimefactory_t* factory, uint32_t msg_id)
"SELECT max(timestamp) FROM msgs WHERE chat_id=? AND id!=?");
sqlite3_bind_int (stmt, 1, factory->msg->chat_id);
sqlite3_bind_int (stmt, 2, factory->msg->id);
if( sqlite3_step(stmt) == SQLITE_ROW ) {
if (sqlite3_step(stmt) == SQLITE_ROW) {
prev_msg_time = sqlite3_column_int64(stmt, 0);
}
sqlite3_finalize(stmt);
stmt = NULL;
#define NEW_THREAD_THRESHOLD 24*60*60
if( prev_msg_time != 0 && factory->msg->timestamp - prev_msg_time < NEW_THREAD_THRESHOLD ) {
if (prev_msg_time != 0 && factory->msg->timestamp - prev_msg_time < NEW_THREAD_THRESHOLD) {
factory->references = dc_param_get(factory->chat->param, DC_PARAM_REFERENCES, NULL);
}
if( factory->references == NULL ) {
if (factory->references == NULL) {
factory->references = dc_create_dummy_references_mid();
dc_param_set(factory->chat->param, DC_PARAM_REFERENCES, factory->references);
dc_chat_update_param(factory->chat);
@ -231,7 +231,7 @@ int dc_mimefactory_load_msg(dc_mimefactory_t* factory, uint32_t msg_id)
factory->rfc724_mid = dc_strdup(factory->msg->rfc724_mid);
}
if( success ) {
if (success) {
factory->increation = dc_msg_is_increation(factory->msg);
}
@ -246,7 +246,7 @@ int dc_mimefactory_load_mdn(dc_mimefactory_t* factory, uint32_t msg_id)
int success = 0;
dc_contact_t* contact = dc_contact_new(factory->context);
if( factory == NULL ) {
if (factory == NULL) {
goto cleanup;
}
@ -256,21 +256,21 @@ int dc_mimefactory_load_mdn(dc_mimefactory_t* factory, uint32_t msg_id)
factory->recipients_addr = clist_new();
factory->msg = dc_msg_new();
if( !dc_sqlite3_get_config_int(context->sql, "mdns_enabled", DC_MDNS_DEFAULT_ENABLED) ) {
if (!dc_sqlite3_get_config_int(context->sql, "mdns_enabled", DC_MDNS_DEFAULT_ENABLED)) {
goto cleanup; /* MDNs not enabled - check this is late, in the job. the use may have changed its choice while offline ... */
}
if( !dc_msg_load_from_db(factory->msg, context, msg_id)
|| !dc_contact_load_from_db(contact, context->sql, factory->msg->from_id) ) {
if (!dc_msg_load_from_db(factory->msg, context, msg_id)
|| !dc_contact_load_from_db(contact, context->sql, factory->msg->from_id)) {
goto cleanup;
}
if( contact->blocked
|| factory->msg->chat_id<=DC_CHAT_ID_LAST_SPECIAL/* Do not send MDNs trash etc.; chats.blocked is already checked by the caller in dc_markseen_msgs() */ ) {
if (contact->blocked
|| factory->msg->chat_id<=DC_CHAT_ID_LAST_SPECIAL/* Do not send MDNs trash etc.; chats.blocked is already checked by the caller in dc_markseen_msgs() */) {
goto cleanup;
}
if( factory->msg->from_id <= DC_CONTACT_ID_LAST_SPECIAL ) {
if (factory->msg->from_id <= DC_CONTACT_ID_LAST_SPECIAL) {
goto cleanup;
}
@ -325,12 +325,12 @@ static struct mailmime* build_body_file(const dc_msg_t* msg, const char* base_na
char* filename_to_send = NULL;
char* filename_encoded = NULL;
if( pathNfilename == NULL ) {
if (pathNfilename == NULL) {
goto cleanup;
}
/* get file name to use for sending (for privacy purposes, we do not transfer the original filenames eg. for images; these names are normally not needed and contain timesamps, running numbers etc.) */
if( msg->type == DC_MSG_VOICE ) {
if (msg->type == DC_MSG_VOICE) {
struct tm wanted_struct;
memcpy(&wanted_struct, localtime(&msg->timestamp), sizeof(struct tm));
filename_to_send = dc_mprintf("voice-message_%04i-%02i-%02i_%02i-%02i-%02i.%s",
@ -338,10 +338,10 @@ static struct mailmime* build_body_file(const dc_msg_t* msg, const char* base_na
(int)wanted_struct.tm_hour, (int)wanted_struct.tm_min, (int)wanted_struct.tm_sec,
suffix? suffix : "dat");
}
else if( msg->type == DC_MSG_AUDIO ) {
else if (msg->type == DC_MSG_AUDIO) {
char* author = dc_param_get(msg->param, DC_PARAM_AUTHORNAME, NULL);
char* title = dc_param_get(msg->param, DC_PARAM_TRACKNAME, NULL);
if( author && author[0] && title && title[0] && suffix ) {
if (author && author[0] && title && title[0] && suffix) {
filename_to_send = dc_mprintf("%s - %s.%s", author, title, suffix); /* the separator ` - ` is used on the receiver's side to construct the information; we avoid using ID3-scanners for security purposes */
}
else {
@ -350,13 +350,13 @@ static struct mailmime* build_body_file(const dc_msg_t* msg, const char* base_na
free(author);
free(title);
}
else if( msg->type == DC_MSG_IMAGE || msg->type == DC_MSG_GIF ) {
if( base_name == NULL ) {
else if (msg->type == DC_MSG_IMAGE || msg->type == DC_MSG_GIF) {
if (base_name == NULL) {
base_name = "image";
}
filename_to_send = dc_mprintf("%s.%s", base_name, suffix? suffix : "dat");
}
else if( msg->type == DC_MSG_VIDEO ) {
else if (msg->type == DC_MSG_VIDEO) {
filename_to_send = dc_mprintf("video.%s", suffix? suffix : "dat");
}
else {
@ -364,14 +364,14 @@ static struct mailmime* build_body_file(const dc_msg_t* msg, const char* base_na
}
/* check mimetype */
if( mimetype == NULL && suffix != NULL ) {
if( strcmp(suffix, "png")==0 ) {
if (mimetype == NULL && suffix != NULL) {
if (strcmp(suffix, "png")==0) {
mimetype = dc_strdup("image/png");
}
else if( strcmp(suffix, "jpg")==0 || strcmp(suffix, "jpeg")==0 || strcmp(suffix, "jpe")==0 ) {
else if (strcmp(suffix, "jpg")==0 || strcmp(suffix, "jpeg")==0 || strcmp(suffix, "jpe")==0) {
mimetype = dc_strdup("image/jpeg");
}
else if( strcmp(suffix, "gif")==0 ) {
else if (strcmp(suffix, "gif")==0) {
mimetype = dc_strdup("image/gif");
}
else {
@ -379,7 +379,7 @@ static struct mailmime* build_body_file(const dc_msg_t* msg, const char* base_na
}
}
if( mimetype == NULL ) {
if (mimetype == NULL) {
goto cleanup;
}
@ -391,18 +391,18 @@ static struct mailmime* build_body_file(const dc_msg_t* msg, const char* base_na
mime_fields = mailmime_fields_new_filename(MAILMIME_DISPOSITION_TYPE_ATTACHMENT,
needs_ext? NULL : dc_strdup(filename_to_send), MAILMIME_MECHANISM_BASE64);
if( needs_ext ) {
for( clistiter* cur1 = clist_begin(mime_fields->fld_list); cur1 != NULL; cur1 = clist_next(cur1) ) {
if (needs_ext) {
for (clistiter* cur1 = clist_begin(mime_fields->fld_list); cur1 != NULL; cur1 = clist_next(cur1)) {
struct mailmime_field* field = (struct mailmime_field*)clist_content(cur1);
if( field && field->fld_type == MAILMIME_FIELD_DISPOSITION && field->fld_data.fld_disposition )
if (field && field->fld_type == MAILMIME_FIELD_DISPOSITION && field->fld_data.fld_disposition)
{
struct mailmime_disposition* file_disposition = field->fld_data.fld_disposition;
if( file_disposition )
if (file_disposition)
{
struct mailmime_disposition_parm* parm = mailmime_disposition_parm_new(
MAILMIME_DISPOSITION_PARM_PARAMETER, NULL, NULL, NULL, NULL, 0,
mailmime_parameter_new(strdup("filename*"), dc_encode_ext_header(filename_to_send)));
if( parm ) {
if (parm) {
clist_append(file_disposition->dsp_parms, parm);
}
}
@ -420,7 +420,7 @@ static struct mailmime* build_body_file(const dc_msg_t* msg, const char* base_na
mailmime_set_body_file(mime_sub, dc_strdup(pathNfilename));
if( ret_file_name_as_sent ) {
if (ret_file_name_as_sent) {
*ret_file_name_as_sent = dc_strdup(filename_to_send);
}
@ -440,11 +440,11 @@ static char* get_subject(const dc_chat_t* chat, const dc_msg_t* msg, int afwd_em
char *ret, *raw_subject = dc_msg_get_summarytext_by_raw(msg->type, msg->text, msg->param, DC_APPROX_SUBJECT_CHARS, context);
const char* fwd = afwd_email? "Fwd: " : "";
if( dc_param_get_int(msg->param, DC_PARAM_CMD, 0) == DC_CMD_AUTOCRYPT_SETUP_MESSAGE )
if (dc_param_get_int(msg->param, DC_PARAM_CMD, 0) == DC_CMD_AUTOCRYPT_SETUP_MESSAGE)
{
ret = dc_stock_str(context, DC_STR_AC_SETUP_MSG_SUBJECT); /* do not add the "Chat:" prefix for setup messages */
}
else if( DC_CHAT_TYPE_IS_MULTI(chat->type) )
else if (DC_CHAT_TYPE_IS_MULTI(chat->type))
{
ret = dc_mprintf(DC_CHAT_PREFIX " %s: %s%s", chat->name, fwd, raw_subject);
}
@ -460,9 +460,9 @@ static char* get_subject(const dc_chat_t* chat, const dc_msg_t* msg, int afwd_em
int dc_mimefactory_render(dc_mimefactory_t* factory)
{
if( factory == NULL
if (factory == NULL
|| factory->loaded == DC_MF_NOTHING_LOADED
|| factory->out/*call empty() before*/ ) {
|| factory->out/*call empty() before*/) {
return 0;
}
@ -490,10 +490,10 @@ int dc_mimefactory_render(dc_mimefactory_t* factory)
mailimf_mailbox_list_add(from, mailimf_mailbox_new(factory->from_displayname? dc_encode_header_words(factory->from_displayname) : NULL, dc_strdup(factory->from_addr)));
struct mailimf_address_list* to = NULL;
if( factory->recipients_names && factory->recipients_addr && clist_count(factory->recipients_addr)>0 ) {
if (factory->recipients_names && factory->recipients_addr && clist_count(factory->recipients_addr)>0) {
clistiter *iter1, *iter2;
to = mailimf_address_list_new_empty();
for( iter1=clist_begin(factory->recipients_names),iter2=clist_begin(factory->recipients_addr); iter1!=NULL&&iter2!=NULL; iter1=clist_next(iter1),iter2=clist_next(iter2)) {
for (iter1=clist_begin(factory->recipients_names),iter2=clist_begin(factory->recipients_addr); iter1!=NULL&&iter2!=NULL; iter1=clist_next(iter1),iter2=clist_next(iter2)) {
const char* name = clist_content(iter1);
const char* addr = clist_content(iter2);
mailimf_address_list_add(to, mailimf_address_new(MAILIMF_ADDRESS_MAILBOX, mailimf_mailbox_new(name? dc_encode_header_words(name) : NULL, dc_strdup(addr)), NULL));
@ -501,7 +501,7 @@ int dc_mimefactory_render(dc_mimefactory_t* factory)
}
clist* references_list = NULL;
if( factory->references ) {
if (factory->references) {
references_list = clist_new();
clist_append(references_list, (void*)dc_strdup(factory->references));
}
@ -521,11 +521,11 @@ int dc_mimefactory_render(dc_mimefactory_t* factory)
factory->context->os_name? factory->context->os_name : "")));
mailimf_fields_add(imf_fields, mailimf_field_new_custom(strdup("Chat-Version"), strdup("1.0"))); /* mark message as being sent by a messenger */
if( factory->predecessor ) {
if (factory->predecessor) {
mailimf_fields_add(imf_fields, mailimf_field_new_custom(strdup("Chat-Predecessor"), strdup(factory->predecessor)));
}
if( factory->req_mdn ) {
if (factory->req_mdn) {
/* we use "Chat-Disposition-Notification-To" as replies to "Disposition-Notification-To" are weired in many cases, are just freetext and/or do not follow any standard. */
mailimf_fields_add(imf_fields, mailimf_field_new_custom(strdup("Chat-Disposition-Notification-To"), strdup(factory->from_addr)));
}
@ -534,7 +534,7 @@ int dc_mimefactory_render(dc_mimefactory_t* factory)
mailmime_set_imf_fields(message, imf_fields);
}
if( factory->loaded == DC_MF_MSG_LOADED )
if (factory->loaded == DC_MF_MSG_LOADED)
{
/* Render a normal message
*********************************************************************/
@ -545,72 +545,72 @@ int dc_mimefactory_render(dc_mimefactory_t* factory)
struct mailmime* meta_part = NULL;
char* placeholdertext = NULL;
if( chat->type == DC_CHAT_TYPE_VERIFIED_GROUP ) {
if (chat->type == DC_CHAT_TYPE_VERIFIED_GROUP) {
mailimf_fields_add(imf_fields, mailimf_field_new_custom(strdup("Chat-Verified"), strdup("1")));
force_plaintext = 0;
e2ee_guaranteed = 1;
min_verified = DC_BIDIRECT_VERIFIED;
}
else {
if( (force_plaintext = dc_param_get_int(factory->msg->param, DC_PARAM_FORCE_PLAINTEXT, 0)) == 0 ) {
if ((force_plaintext = dc_param_get_int(factory->msg->param, DC_PARAM_FORCE_PLAINTEXT, 0)) == 0) {
e2ee_guaranteed = dc_param_get_int(factory->msg->param, DC_PARAM_GUARANTEE_E2EE, 0);
}
}
/* build header etc. */
int command = dc_param_get_int(msg->param, DC_PARAM_CMD, 0);
if( DC_CHAT_TYPE_IS_MULTI(chat->type) )
if (DC_CHAT_TYPE_IS_MULTI(chat->type))
{
mailimf_fields_add(imf_fields, mailimf_field_new_custom(strdup("Chat-Group-ID"), dc_strdup(chat->grpid)));
mailimf_fields_add(imf_fields, mailimf_field_new_custom(strdup("Chat-Group-Name"), dc_encode_header_words(chat->name)));
if( command == DC_CMD_MEMBER_REMOVED_FROM_GROUP )
if (command == DC_CMD_MEMBER_REMOVED_FROM_GROUP)
{
char* email_to_remove = dc_param_get(msg->param, DC_PARAM_CMD_ARG, NULL);
if( email_to_remove ) {
if (email_to_remove) {
mailimf_fields_add(imf_fields, mailimf_field_new_custom(strdup("Chat-Group-Member-Removed"), email_to_remove));
}
}
else if( command == DC_CMD_MEMBER_ADDED_TO_GROUP )
else if (command == DC_CMD_MEMBER_ADDED_TO_GROUP)
{
char* email_to_add = dc_param_get(msg->param, DC_PARAM_CMD_ARG, NULL);
if( email_to_add ) {
if (email_to_add) {
mailimf_fields_add(imf_fields, mailimf_field_new_custom(strdup("Chat-Group-Member-Added"), email_to_add));
grpimage = dc_param_get(chat->param, DC_PARAM_PROFILE_IMAGE, NULL);
}
if( dc_param_get_int(msg->param, DC_PARAM_CMD_ARG2, 0)&DC_FROM_HANDSHAKE ) {
if (dc_param_get_int(msg->param, DC_PARAM_CMD_ARG2, 0)&DC_FROM_HANDSHAKE) {
dc_log_info(msg->context, 0, "sending secure-join message '%s' >>>>>>>>>>>>>>>>>>>>>>>>>", "vg-member-added");
mailimf_fields_add(imf_fields, mailimf_field_new_custom(strdup("Secure-Join"), strdup("vg-member-added")));
}
}
else if( command == DC_CMD_GROUPNAME_CHANGED )
else if (command == DC_CMD_GROUPNAME_CHANGED)
{
mailimf_fields_add(imf_fields, mailimf_field_new_custom(strdup("Chat-Group-Name-Changed"), strdup("1")));
}
else if( command == DC_CMD_GROUPIMAGE_CHANGED )
else if (command == DC_CMD_GROUPIMAGE_CHANGED)
{
grpimage = dc_param_get(msg->param, DC_PARAM_CMD_ARG, NULL);
if( grpimage==NULL ) {
if (grpimage==NULL) {
mailimf_fields_add(imf_fields, mailimf_field_new_custom(strdup("Chat-Group-Image"), dc_strdup("0")));
}
}
}
if( command == DC_CMD_AUTOCRYPT_SETUP_MESSAGE ) {
if (command == DC_CMD_AUTOCRYPT_SETUP_MESSAGE) {
mailimf_fields_add(imf_fields, mailimf_field_new_custom(strdup("Autocrypt-Setup-Message"), strdup("v1")));
placeholdertext = dc_stock_str(factory->context, DC_STR_AC_SETUP_MSG_BODY);
}
if( command == DC_CMD_SECUREJOIN_MESSAGE ) {
if (command == DC_CMD_SECUREJOIN_MESSAGE) {
char* step = dc_param_get(msg->param, DC_PARAM_CMD_ARG, NULL);
if( step ) {
if (step) {
dc_log_info(msg->context, 0, "sending secure-join message '%s' >>>>>>>>>>>>>>>>>>>>>>>>>", step);
mailimf_fields_add(imf_fields, mailimf_field_new_custom(strdup("Secure-Join"), step/*mailimf takes ownership of string*/));
char* param2 = dc_param_get(msg->param, DC_PARAM_CMD_ARG2, NULL);
if( param2 ) {
if (param2) {
mailimf_fields_add(imf_fields, mailimf_field_new_custom(
(strcmp(step, "vg-request-with-auth")==0 || strcmp(step, "vc-request-with-auth")==0)?
strdup("Secure-Join-Auth") : strdup("Secure-Join-Invitenumber"),
@ -618,14 +618,14 @@ int dc_mimefactory_render(dc_mimefactory_t* factory)
}
char* fingerprint = dc_param_get(msg->param, DC_PARAM_CMD_ARG3, NULL);
if( fingerprint ) {
if (fingerprint) {
mailimf_fields_add(imf_fields, mailimf_field_new_custom(
strdup("Secure-Join-Fingerprint"),
fingerprint/*mailimf takes ownership of string*/));
}
char* grpid = dc_param_get(msg->param, DC_PARAM_CMD_ARG4, NULL);
if( grpid ) {
if (grpid) {
mailimf_fields_add(imf_fields, mailimf_field_new_custom(
strdup("Secure-Join-Group"),
grpid/*mailimf takes ownership of string*/));
@ -633,26 +633,26 @@ int dc_mimefactory_render(dc_mimefactory_t* factory)
}
}
if( grpimage )
if (grpimage)
{
dc_msg_t* meta = dc_msg_new();
meta->type = DC_MSG_IMAGE;
dc_param_set(meta->param, DC_PARAM_FILE, grpimage);
char* filename_as_sent = NULL;
if( (meta_part=build_body_file(meta, "group-image", &filename_as_sent))!=NULL ) {
if ((meta_part=build_body_file(meta, "group-image", &filename_as_sent))!=NULL) {
mailimf_fields_add(imf_fields, mailimf_field_new_custom(strdup("Chat-Group-Image"), filename_as_sent/*takes ownership*/));
}
dc_msg_unref(meta);
}
if( msg->type == DC_MSG_VOICE || msg->type == DC_MSG_AUDIO || msg->type == DC_MSG_VIDEO )
if (msg->type == DC_MSG_VOICE || msg->type == DC_MSG_AUDIO || msg->type == DC_MSG_VIDEO)
{
if( msg->type == DC_MSG_VOICE ) {
if (msg->type == DC_MSG_VOICE) {
mailimf_fields_add(imf_fields, mailimf_field_new_custom(strdup("Chat-Voice-Message"), strdup("1")));
}
int duration_ms = dc_param_get_int(msg->param, DC_PARAM_DURATION, 0);
if( duration_ms > 0 ) {
if (duration_ms > 0) {
mailimf_fields_add(imf_fields, mailimf_field_new_custom(strdup("Chat-Duration"), dc_mprintf("%i", (int)duration_ms)));
}
}
@ -663,15 +663,15 @@ int dc_mimefactory_render(dc_mimefactory_t* factory)
- it looks better */
afwd_email = dc_param_exists(msg->param, DC_PARAM_FORWARDED);
char* fwdhint = NULL;
if( afwd_email ) {
if (afwd_email) {
fwdhint = dc_strdup("---------- Forwarded message ----------" LINEEND "From: Delta Chat" LINEEND LINEEND); /* do not chage this! expected this way in the simplifier to detect forwarding! */
}
const char* final_text = NULL;
if( msg->type==DC_MSG_TEXT && msg->text && msg->text[0] ) { /* `text` may also contain data otherwise, eg. the filename of attachments */
if (msg->type==DC_MSG_TEXT && msg->text && msg->text[0]) { /* `text` may also contain data otherwise, eg. the filename of attachments */
final_text = msg->text;
}
else if( placeholdertext ) {
else if (placeholdertext) {
final_text = placeholdertext;
}
@ -690,24 +690,24 @@ int dc_mimefactory_render(dc_mimefactory_t* factory)
free(placeholdertext);
/* add attachment part */
if( DC_MSG_NEEDS_ATTACHMENT(msg->type) ) {
if (DC_MSG_NEEDS_ATTACHMENT(msg->type)) {
struct mailmime* file_part = build_body_file(msg, NULL, NULL);
if( file_part ) {
if (file_part) {
mailmime_smart_add_part(message, file_part);
parts++;
}
}
if( parts == 0 ) {
if (parts == 0) {
goto cleanup;
}
if( meta_part ) {
if (meta_part) {
mailmime_smart_add_part(message, meta_part); /* meta parts are only added if there are other parts */
parts++;
}
}
else if( factory->loaded == DC_MF_MDN_LOADED )
else if (factory->loaded == DC_MF_MDN_LOADED)
{
/* Render a MDN
*********************************************************************/
@ -719,7 +719,7 @@ int dc_mimefactory_render(dc_mimefactory_t* factory)
/* first body part: always human-readable, always REQUIRED by RFC 6522 */
char *p1 = NULL, *p2 = NULL;
if( dc_param_get_int(factory->msg->param, DC_PARAM_GUARANTEE_E2EE, 0) ) {
if (dc_param_get_int(factory->msg->param, DC_PARAM_GUARANTEE_E2EE, 0)) {
p1 = dc_stock_str(factory->context, DC_STR_ENCRYPTEDMSG); /* we SHOULD NOT spread encrypted subjects, date etc. in potentially unencrypted MDNs */
}
else {
@ -772,7 +772,7 @@ int dc_mimefactory_render(dc_mimefactory_t* factory)
/* Encrypt the message
*************************************************************************/
if( factory->loaded==DC_MF_MDN_LOADED ) {
if (factory->loaded==DC_MF_MDN_LOADED) {
char* e = dc_stock_str(factory->context, DC_STR_READRCPT); subject_str = dc_mprintf(DC_CHAT_PREFIX " %s", e); free(e);
}
else {
@ -782,11 +782,11 @@ int dc_mimefactory_render(dc_mimefactory_t* factory)
struct mailimf_subject* subject = mailimf_subject_new(dc_encode_header_words(subject_str));
mailimf_fields_add(imf_fields, mailimf_field_new(MAILIMF_FIELD_SUBJECT, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, subject, NULL, NULL, NULL));
if( force_plaintext != DC_FP_NO_AUTOCRYPT_HEADER ) {
if (force_plaintext != DC_FP_NO_AUTOCRYPT_HEADER) {
dc_e2ee_encrypt(factory->context, factory->recipients_addr, force_plaintext, e2ee_guaranteed, min_verified, message, &e2ee_helper);
}
if( e2ee_helper.encryption_successfull ) {
if (e2ee_helper.encryption_successfull) {
factory->out_encrypted = 1;
}
@ -799,7 +799,7 @@ int dc_mimefactory_render(dc_mimefactory_t* factory)
success = 1;
cleanup:
if( message ) {
if (message) {
mailmime_free(message);
}
dc_e2ee_thanks(&e2ee_helper); /* frees data referenced by "mailmime" but not freed by mailmime_free() */

File diff suppressed because it is too large Load diff

View file

@ -43,7 +43,7 @@ dc_msg_t* dc_msg_new()
{
dc_msg_t* msg = NULL;
if( (msg=calloc(1, sizeof(dc_msg_t)))==NULL ) {
if ((msg=calloc(1, sizeof(dc_msg_t)))==NULL) {
exit(15); /* cannot allocate little memory, unrecoverable error */
}
@ -65,7 +65,7 @@ dc_msg_t* dc_msg_new()
*/
void dc_msg_unref(dc_msg_t* msg)
{
if( msg==NULL || msg->magic != DC_MSG_MAGIC ) {
if (msg==NULL || msg->magic != DC_MSG_MAGIC) {
return;
}
@ -85,7 +85,7 @@ void dc_msg_unref(dc_msg_t* msg)
*/
void dc_msg_empty(dc_msg_t* msg)
{
if( msg == NULL || msg->magic != DC_MSG_MAGIC ) {
if (msg == NULL || msg->magic != DC_MSG_MAGIC) {
return;
}
@ -120,7 +120,7 @@ void dc_msg_empty(dc_msg_t* msg)
*/
uint32_t dc_msg_get_id(const dc_msg_t* msg)
{
if( msg == NULL || msg->magic != DC_MSG_MAGIC ) {
if (msg == NULL || msg->magic != DC_MSG_MAGIC) {
return 0;
}
return msg->id;
@ -143,7 +143,7 @@ uint32_t dc_msg_get_id(const dc_msg_t* msg)
*/
uint32_t dc_msg_get_from_id(const dc_msg_t* msg)
{
if( msg == NULL || msg->magic != DC_MSG_MAGIC ) {
if (msg == NULL || msg->magic != DC_MSG_MAGIC) {
return 0;
}
return msg->from_id;
@ -162,7 +162,7 @@ uint32_t dc_msg_get_from_id(const dc_msg_t* msg)
*/
uint32_t dc_msg_get_chat_id(const dc_msg_t* msg)
{
if( msg == NULL || msg->magic != DC_MSG_MAGIC ) {
if (msg == NULL || msg->magic != DC_MSG_MAGIC) {
return 0;
}
return msg->chat_blocked? DC_CHAT_ID_DEADDROP : msg->chat_id;
@ -180,7 +180,7 @@ uint32_t dc_msg_get_chat_id(const dc_msg_t* msg)
*/
int dc_msg_get_type(const dc_msg_t* msg)
{
if( msg == NULL || msg->magic != DC_MSG_MAGIC ) {
if (msg == NULL || msg->magic != DC_MSG_MAGIC) {
return DC_MSG_UNDEFINED;
}
return msg->type;
@ -216,7 +216,7 @@ int dc_msg_get_type(const dc_msg_t* msg)
*/
int dc_msg_get_state(const dc_msg_t* msg)
{
if( msg == NULL || msg->magic != DC_MSG_MAGIC ) {
if (msg == NULL || msg->magic != DC_MSG_MAGIC) {
return DC_STATE_UNDEFINED;
}
return msg->state;
@ -235,7 +235,7 @@ int dc_msg_get_state(const dc_msg_t* msg)
*/
time_t dc_msg_get_timestamp(const dc_msg_t* msg)
{
if( msg == NULL || msg->magic != DC_MSG_MAGIC ) {
if (msg == NULL || msg->magic != DC_MSG_MAGIC) {
return 0;
}
@ -266,7 +266,7 @@ char* dc_msg_get_text(const dc_msg_t* msg)
{
char* ret;
if( msg == NULL || msg->magic != DC_MSG_MAGIC ) {
if (msg == NULL || msg->magic != DC_MSG_MAGIC) {
return dc_strdup(NULL);
}
@ -293,7 +293,7 @@ char* dc_msg_get_file(const dc_msg_t* msg)
{
char* ret = NULL;
if( msg == NULL || msg->magic != DC_MSG_MAGIC ) {
if (msg == NULL || msg->magic != DC_MSG_MAGIC) {
goto cleanup;
}
@ -318,12 +318,12 @@ char* dc_msg_get_filename(const dc_msg_t* msg)
{
char* ret = NULL, *pathNfilename = NULL;
if( msg == NULL || msg->magic != DC_MSG_MAGIC ) {
if (msg == NULL || msg->magic != DC_MSG_MAGIC) {
goto cleanup;
}
pathNfilename = dc_param_get(msg->param, DC_PARAM_FILE, NULL);
if( pathNfilename == NULL ) {
if (pathNfilename == NULL) {
goto cleanup;
}
@ -349,19 +349,19 @@ char* dc_msg_get_filemime(const dc_msg_t* msg)
char* ret = NULL;
char* file = NULL;
if( msg == NULL || msg->magic != DC_MSG_MAGIC ) {
if (msg == NULL || msg->magic != DC_MSG_MAGIC) {
goto cleanup;
}
ret = dc_param_get(msg->param, DC_PARAM_MIMETYPE, NULL);
if( ret == NULL ) {
if (ret == NULL) {
file = dc_param_get(msg->param, DC_PARAM_FILE, NULL);
if( file == NULL ) {
if (file == NULL) {
goto cleanup;
}
dc_msg_guess_msgtype_from_suffix(file, NULL, &ret);
if( ret == NULL ) {
if (ret == NULL) {
ret = dc_strdup("application/octet-stream");
}
}
@ -387,12 +387,12 @@ uint64_t dc_msg_get_filebytes(const dc_msg_t* msg)
uint64_t ret = 0;
char* file = NULL;
if( msg == NULL || msg->magic != DC_MSG_MAGIC ) {
if (msg == NULL || msg->magic != DC_MSG_MAGIC) {
goto cleanup;
}
file = dc_param_get(msg->param, DC_PARAM_FILE, NULL);
if( file == NULL ) {
if (file == NULL) {
goto cleanup;
}
@ -427,13 +427,13 @@ dc_lot_t* dc_msg_get_mediainfo(const dc_msg_t* msg)
char* pathNfilename = NULL;
dc_contact_t* contact = NULL;
if( msg == NULL || msg->magic != DC_MSG_MAGIC || msg->context == NULL ) {
if (msg == NULL || msg->magic != DC_MSG_MAGIC || msg->context == NULL) {
goto cleanup;
}
if( msg->type == DC_MSG_VOICE )
if (msg->type == DC_MSG_VOICE)
{
if( (contact = dc_get_contact(msg->context, msg->from_id))==NULL ) {
if ((contact = dc_get_contact(msg->context, msg->from_id))==NULL) {
goto cleanup;
}
ret->text1 = dc_strdup((contact->name&&contact->name[0])? contact->name : contact->addr);
@ -443,18 +443,18 @@ dc_lot_t* dc_msg_get_mediainfo(const dc_msg_t* msg)
{
ret->text1 = dc_param_get(msg->param, DC_PARAM_AUTHORNAME, NULL);
ret->text2 = dc_param_get(msg->param, DC_PARAM_TRACKNAME, NULL);
if( ret->text1 && ret->text1[0] && ret->text2 && ret->text2[0] ) {
if (ret->text1 && ret->text1[0] && ret->text2 && ret->text2[0]) {
goto cleanup;
}
free(ret->text1); ret->text1 = NULL;
free(ret->text2); ret->text2 = NULL;
pathNfilename = dc_param_get(msg->param, DC_PARAM_FILE, NULL);
if( pathNfilename == NULL ) {
if (pathNfilename == NULL) {
goto cleanup;
}
dc_msg_get_authorNtitle_from_filename(pathNfilename, &ret->text1, &ret->text2);
if( ret->text1 == NULL && ret->text2 != NULL ) {
if (ret->text1 == NULL && ret->text2 != NULL) {
ret->text1 = dc_stock_str(msg->context, DC_STR_AUDIO);
}
}
@ -482,7 +482,7 @@ cleanup:
*/
int dc_msg_get_width(const dc_msg_t* msg)
{
if( msg == NULL || msg->magic != DC_MSG_MAGIC ) {
if (msg == NULL || msg->magic != DC_MSG_MAGIC) {
return 0;
}
return dc_param_get_int(msg->param, DC_PARAM_WIDTH, 0);
@ -505,7 +505,7 @@ int dc_msg_get_width(const dc_msg_t* msg)
*/
int dc_msg_get_height(const dc_msg_t* msg)
{
if( msg == NULL || msg->magic != DC_MSG_MAGIC ) {
if (msg == NULL || msg->magic != DC_MSG_MAGIC) {
return 0;
}
return dc_param_get_int(msg->param, DC_PARAM_HEIGHT, 0);
@ -525,7 +525,7 @@ int dc_msg_get_height(const dc_msg_t* msg)
*/
int dc_msg_get_duration(const dc_msg_t* msg)
{
if( msg == NULL || msg->magic != DC_MSG_MAGIC ) {
if (msg == NULL || msg->magic != DC_MSG_MAGIC) {
return 0;
}
return dc_param_get_int(msg->param, DC_PARAM_DURATION, 0);
@ -544,11 +544,11 @@ int dc_msg_get_showpadlock(const dc_msg_t* msg)
/* a padlock guarantees that the message is e2ee _and_ answers will be as well */
int show_encryption_state = 0;
if( msg == NULL || msg->magic != DC_MSG_MAGIC || msg->context == NULL ) {
if (msg == NULL || msg->magic != DC_MSG_MAGIC || msg->context == NULL) {
return 0;
}
if( msg->context->e2ee_enabled ) {
if (msg->context->e2ee_enabled) {
show_encryption_state = 1;
}
else {
@ -557,8 +557,8 @@ int dc_msg_get_showpadlock(const dc_msg_t* msg)
dc_chat_unref(chat);
}
if( show_encryption_state ) {
if( dc_param_get_int(msg->param, DC_PARAM_GUARANTEE_E2EE, 0) != 0 ) {
if (show_encryption_state) {
if (dc_param_get_int(msg->param, DC_PARAM_GUARANTEE_E2EE, 0) != 0) {
return 1;
}
}
@ -595,18 +595,18 @@ dc_lot_t* dc_msg_get_summary(const dc_msg_t* msg, const dc_chat_t* chat)
dc_contact_t* contact = NULL;
dc_chat_t* chat_to_delete = NULL;
if( msg==NULL || msg->magic != DC_MSG_MAGIC ) {
if (msg==NULL || msg->magic != DC_MSG_MAGIC) {
goto cleanup;
}
if( chat == NULL ) {
if( (chat_to_delete=dc_get_chat(msg->context, msg->chat_id)) == NULL ) {
if (chat == NULL) {
if ((chat_to_delete=dc_get_chat(msg->context, msg->chat_id)) == NULL) {
goto cleanup;
}
chat = chat_to_delete;
}
if( msg->from_id != DC_CONTACT_ID_SELF && DC_CHAT_TYPE_IS_MULTI(chat->type) ) {
if (msg->from_id != DC_CONTACT_ID_SELF && DC_CHAT_TYPE_IS_MULTI(chat->type)) {
contact = dc_get_contact(chat->context, msg->from_id);
}
@ -631,7 +631,7 @@ cleanup:
*/
char* dc_msg_get_summarytext(const dc_msg_t* msg, int approx_characters)
{
if( msg==NULL || msg->magic != DC_MSG_MAGIC ) {
if (msg==NULL || msg->magic != DC_MSG_MAGIC) {
return dc_strdup(NULL);
}
@ -651,7 +651,7 @@ char* dc_msg_get_summarytext(const dc_msg_t* msg, int approx_characters)
*/
int dc_msg_is_sent(const dc_msg_t* msg)
{
if( msg == NULL || msg->magic != DC_MSG_MAGIC ) {
if (msg == NULL || msg->magic != DC_MSG_MAGIC) {
return 0;
}
return (msg->state >= DC_STATE_OUT_DELIVERED)? 1 : 0;
@ -672,7 +672,7 @@ int dc_msg_is_sent(const dc_msg_t* msg)
*/
int dc_msg_is_starred(const dc_msg_t* msg)
{
if( msg == NULL || msg->magic != DC_MSG_MAGIC ) {
if (msg == NULL || msg->magic != DC_MSG_MAGIC) {
return 0;
}
return msg->starred? 1 : 0;
@ -697,7 +697,7 @@ int dc_msg_is_starred(const dc_msg_t* msg)
*/
int dc_msg_is_forwarded(const dc_msg_t* msg)
{
if( msg == NULL || msg->magic != DC_MSG_MAGIC ) {
if (msg == NULL || msg->magic != DC_MSG_MAGIC) {
return 0;
}
return dc_param_get_int(msg->param, DC_PARAM_FORWARDED, 0)? 1 : 0;
@ -722,15 +722,15 @@ int dc_msg_is_forwarded(const dc_msg_t* msg)
*/
int dc_msg_is_info(const dc_msg_t* msg)
{
if( msg == NULL || msg->magic != DC_MSG_MAGIC ) {
if (msg == NULL || msg->magic != DC_MSG_MAGIC) {
return 0;
}
int cmd = dc_param_get_int(msg->param, DC_PARAM_CMD, 0);
if( msg->from_id == DC_CONTACT_ID_DEVICE
if (msg->from_id == DC_CONTACT_ID_DEVICE
|| msg->to_id == DC_CONTACT_ID_DEVICE
|| (cmd && cmd != DC_CMD_AUTOCRYPT_SETUP_MESSAGE) ) {
|| (cmd && cmd != DC_CMD_AUTOCRYPT_SETUP_MESSAGE)) {
return 1;
}
@ -754,7 +754,7 @@ int dc_msg_is_info(const dc_msg_t* msg)
*/
int dc_msg_is_setupmessage(const dc_msg_t* msg)
{
if( msg == NULL || msg->magic != DC_MSG_MAGIC || msg->type != DC_MSG_FILE ) {
if (msg == NULL || msg->magic != DC_MSG_MAGIC || msg->type != DC_MSG_FILE) {
return 0;
}
@ -785,20 +785,20 @@ char* dc_msg_get_setupcodebegin(const dc_msg_t* msg)
const char* buf_setupcodebegin = NULL; // just a pointer inside buf, MUST NOT be free()'d
char* ret = NULL;
if( !dc_msg_is_setupmessage(msg) ) {
if (!dc_msg_is_setupmessage(msg)) {
goto cleanup;
}
if( (filename=dc_msg_get_file(msg))==NULL || filename[0]==0 ) {
if ((filename=dc_msg_get_file(msg))==NULL || filename[0]==0) {
goto cleanup;
}
if( !dc_read_file(filename, (void**)&buf, &buf_bytes, msg->context) || buf == NULL || buf_bytes <= 0 ) {
if (!dc_read_file(filename, (void**)&buf, &buf_bytes, msg->context) || buf == NULL || buf_bytes <= 0) {
goto cleanup;
}
if( !dc_split_armored_data(buf, &buf_headerline, &buf_setupcodebegin, NULL, NULL)
|| strcmp(buf_headerline, "-----BEGIN PGP MESSAGE-----")!=0 || buf_setupcodebegin==NULL ) {
if (!dc_split_armored_data(buf, &buf_headerline, &buf_setupcodebegin, NULL, NULL)
|| strcmp(buf_headerline, "-----BEGIN PGP MESSAGE-----")!=0 || buf_setupcodebegin==NULL) {
goto cleanup;
}
@ -847,7 +847,7 @@ static int dc_msg_set_from_stmt(dc_msg_t* msg, sqlite3_stmt* row, int row_offset
msg->hidden = sqlite3_column_int (row, row_offset++);
msg->chat_blocked = sqlite3_column_int (row, row_offset++);
if( msg->chat_blocked == 2 ) {
if (msg->chat_blocked == 2) {
dc_truncate_n_unwrap_str(msg->text, 256 /* 256 characters is about a half screen on a 5" smartphone display */,
0/*unwrap*/);
}
@ -867,7 +867,7 @@ int dc_msg_load_from_db(dc_msg_t* msg, dc_context_t* context, uint32_t id)
int success = 0;
sqlite3_stmt* stmt = NULL;
if( msg==NULL || msg->magic != DC_MSG_MAGIC || context==NULL || context->sql==NULL ) {
if (msg==NULL || msg->magic != DC_MSG_MAGIC || context==NULL || context->sql==NULL) {
goto cleanup;
}
@ -877,11 +877,11 @@ int dc_msg_load_from_db(dc_msg_t* msg, dc_context_t* context, uint32_t id)
" WHERE m.id=?;");
sqlite3_bind_int(stmt, 1, id);
if( sqlite3_step(stmt) != SQLITE_ROW ) {
if (sqlite3_step(stmt) != SQLITE_ROW) {
goto cleanup;
}
if( !dc_msg_set_from_stmt(msg, stmt, 0) ) { /* also calls dc_msg_empty() */
if (!dc_msg_set_from_stmt(msg, stmt, 0)) { /* also calls dc_msg_empty() */
goto cleanup;
}
@ -911,38 +911,38 @@ void dc_msg_guess_msgtype_from_suffix(const char* pathNfilename, int* ret_msgtyp
int dummy_msgtype = 0;
char* dummy_buf = NULL;
if( pathNfilename == NULL ) {
if (pathNfilename == NULL) {
goto cleanup;
}
if( ret_msgtype == NULL ) { ret_msgtype = &dummy_msgtype; }
if( ret_mime == NULL ) { ret_mime = &dummy_buf; }
if (ret_msgtype == NULL) { ret_msgtype = &dummy_msgtype; }
if (ret_mime == NULL) { ret_mime = &dummy_buf; }
*ret_msgtype = DC_MSG_UNDEFINED;
*ret_mime = NULL;
suffix = dc_get_filesuffix_lc(pathNfilename);
if( suffix == NULL ) {
if (suffix == NULL) {
goto cleanup;
}
if( strcmp(suffix, "mp3")==0 ) {
if (strcmp(suffix, "mp3")==0) {
*ret_msgtype = DC_MSG_AUDIO;
*ret_mime = dc_strdup("audio/mpeg");
}
else if( strcmp(suffix, "mp4")==0 ) {
else if (strcmp(suffix, "mp4")==0) {
*ret_msgtype = DC_MSG_VIDEO;
*ret_mime = dc_strdup("video/mp4");
}
else if( strcmp(suffix, "jpg")==0 || strcmp(suffix, "jpeg")==0 ) {
else if (strcmp(suffix, "jpg")==0 || strcmp(suffix, "jpeg")==0) {
*ret_msgtype = DC_MSG_IMAGE;
*ret_mime = dc_strdup("image/jpeg");
}
else if( strcmp(suffix, "png")==0 ) {
else if (strcmp(suffix, "png")==0) {
*ret_msgtype = DC_MSG_IMAGE;
*ret_mime = dc_strdup("image/png");
}
else if( strcmp(suffix, "gif")==0 ) {
else if (strcmp(suffix, "gif")==0) {
*ret_msgtype = DC_MSG_GIF;
*ret_mime = dc_strdup("image/gif");
}
@ -960,14 +960,14 @@ void dc_msg_get_authorNtitle_from_filename(const char* pathNfilename, char** ret
char *author = NULL, *title = NULL, *p;
dc_split_filename(pathNfilename, &title, NULL);
p = strstr(title, " - ");
if( p ) {
if (p) {
*p = 0;
author = title;
title = dc_strdup(&p[3]);
}
if( ret_author ) { *ret_author = author; } else { free(author); }
if( ret_title ) { *ret_title = title; } else { free(title); }
if (ret_author) { *ret_author = author; } else { free(author); }
if (ret_title) { *ret_title = title; } else { free(title); }
}
@ -977,7 +977,7 @@ char* dc_msg_get_summarytext_by_raw(int type, const char* text, dc_param_t* para
char* ret = NULL;
char* pathNfilename = NULL, *label = NULL, *value = NULL;
switch( type ) {
switch (type) {
case DC_MSG_IMAGE:
ret = dc_stock_str(context, DC_STR_IMAGE);
break;
@ -995,7 +995,7 @@ char* dc_msg_get_summarytext_by_raw(int type, const char* text, dc_param_t* para
break;
case DC_MSG_AUDIO:
if( (value=dc_param_get(param, DC_PARAM_TRACKNAME, NULL))==NULL ) { /* although we send files with "author - title" in the filename, existing files may follow other conventions, so this lookup is neccessary */
if ((value=dc_param_get(param, DC_PARAM_TRACKNAME, NULL))==NULL) { /* although we send files with "author - title" in the filename, existing files may follow other conventions, so this lookup is neccessary */
pathNfilename = dc_param_get(param, DC_PARAM_FILE, "ErrFilename");
dc_msg_get_authorNtitle_from_filename(pathNfilename, NULL, &value);
}
@ -1004,7 +1004,7 @@ char* dc_msg_get_summarytext_by_raw(int type, const char* text, dc_param_t* para
break;
case DC_MSG_FILE:
if( dc_param_get_int(param, DC_PARAM_CMD, 0)==DC_CMD_AUTOCRYPT_SETUP_MESSAGE ) {
if (dc_param_get_int(param, DC_PARAM_CMD, 0)==DC_CMD_AUTOCRYPT_SETUP_MESSAGE) {
ret = dc_stock_str(context, DC_STR_AC_SETUP_MSG_SUBJECT);
}
else {
@ -1016,7 +1016,7 @@ char* dc_msg_get_summarytext_by_raw(int type, const char* text, dc_param_t* para
break;
default:
if( text ) {
if (text) {
ret = dc_strdup(text);
dc_truncate_n_unwrap_str(ret, approx_characters, 1/*unwrap*/);
}
@ -1027,7 +1027,7 @@ char* dc_msg_get_summarytext_by_raw(int type, const char* text, dc_param_t* para
free(pathNfilename);
free(label);
free(value);
if( ret == NULL ) {
if (ret == NULL) {
ret = dc_strdup(NULL);
}
return ret;
@ -1053,16 +1053,16 @@ int dc_msg_is_increation(const dc_msg_t* msg)
/* surrounds dc_msg_is_increation() with locking and error checking */
int is_increation = 0;
if( msg==NULL || msg->magic!=DC_MSG_MAGIC || msg->context==NULL ) {
if (msg==NULL || msg->magic!=DC_MSG_MAGIC || msg->context==NULL) {
return 0;
}
if( DC_MSG_NEEDS_ATTACHMENT(msg->type) )
if (DC_MSG_NEEDS_ATTACHMENT(msg->type))
{
char* pathNfilename = dc_param_get(msg->param, DC_PARAM_FILE, NULL);
if( pathNfilename ) {
if (pathNfilename) {
char* totest = dc_mprintf("%s.increation", pathNfilename);
if( dc_file_exist(totest) ) {
if (dc_file_exist(totest)) {
is_increation = 1;
}
free(totest);
@ -1076,7 +1076,7 @@ int dc_msg_is_increation(const dc_msg_t* msg)
void dc_msg_save_param_to_disk(dc_msg_t* msg)
{
if( msg == NULL || msg->magic != DC_MSG_MAGIC || msg->context == NULL || msg->context->sql == NULL ) {
if (msg == NULL || msg->magic != DC_MSG_MAGIC || msg->context == NULL || msg->context->sql == NULL) {
return;
}
@ -1112,19 +1112,19 @@ void dc_msg_save_param_to_disk(dc_msg_t* msg)
*/
void dc_msg_latefiling_mediasize(dc_msg_t* msg, int width, int height, int duration)
{
if( msg == NULL || msg->magic != DC_MSG_MAGIC ) {
if (msg == NULL || msg->magic != DC_MSG_MAGIC) {
goto cleanup;
}
if( width > 0 ) {
if (width > 0) {
dc_param_set_int(msg->param, DC_PARAM_WIDTH, width);
}
if( height > 0 ) {
if (height > 0) {
dc_param_set_int(msg->param, DC_PARAM_HEIGHT, height);
}
if( duration > 0 ) {
if (duration > 0) {
dc_param_set_int(msg->param, DC_PARAM_DURATION, duration);
}

View file

@ -96,7 +96,7 @@ void dc_msg_guess_msgtype_from_suffix (const char* pathNfilename
void dc_msg_get_authorNtitle_from_filename (const char* pathNfilename, char** ret_author, char** ret_title);
#define DC_MSG_NEEDS_ATTACHMENT(a) ((a)==DC_MSG_IMAGE || (a)==DC_MSG_GIF || (a)==DC_MSG_AUDIO || (a)==DC_MSG_VOICE || (a)==DC_MSG_VIDEO || (a)==DC_MSG_FILE)
#define DC_MSG_MAKE_FILENAME_SEARCHABLE(a) ((a)==DC_MSG_AUDIO || (a)==DC_MSG_FILE || (a)==DC_MSG_VIDEO ) /* add filename.ext (without path) to text? this is needed for the fulltext search. The extension is useful to get all PDF, all MP3 etc. */
#define DC_MSG_MAKE_FILENAME_SEARCHABLE(a) ((a)==DC_MSG_AUDIO || (a)==DC_MSG_FILE || (a)==DC_MSG_VIDEO) /* add filename.ext (without path) to text? this is needed for the fulltext search. The extension is useful to get all PDF, all MP3 etc. */
#define DC_MSG_MAKE_SUFFIX_SEARCHABLE(a) ((a)==DC_MSG_IMAGE || (a)==DC_MSG_GIF || (a)==DC_MSG_VOICE)
#define DC_APPROX_SUBJECT_CHARS 32 /* as we do not cut inside words, this results in about 32-42 characters.

View file

@ -153,7 +153,7 @@ void dc_openssl_exit(void)
{
pthread_mutex_lock(&s_init_lock);
if (s_init_counter>0 )
if (s_init_counter>0)
{
s_init_counter--;
if (s_init_counter==0 && !s_init_not_required)

View file

@ -33,16 +33,16 @@ static char* find_param(char* haystack, int key, char** ret_p2)
/* let p1 point to the start of the */
p1 = haystack;
while( 1 ) {
if( p1 == NULL || *p1 == 0 ) {
while (1) {
if (p1 == NULL || *p1 == 0) {
return NULL;
}
else if( *p1 == key && p1[1] == '=' ) {
else if (*p1 == key && p1[1] == '=') {
break;
}
else {
p1 = strchr(p1, '\n'); /* if `\r\n` is used, this `\r` is also skipped by this*/
if( p1 ) {
if (p1) {
p1++;
}
}
@ -50,7 +50,7 @@ static char* find_param(char* haystack, int key, char** ret_p2)
/* let p2 point to the character _after_ the value - eiter `\n` or `\0` */
p2 = strchr(p1, '\n');
if( p2 == NULL ) {
if (p2 == NULL) {
p2 = &p1[strlen(p1)];
}
@ -70,7 +70,7 @@ dc_param_t* dc_param_new()
{
dc_param_t* param;
if( (param=calloc(1, sizeof(dc_param_t)))==NULL ) {
if ((param=calloc(1, sizeof(dc_param_t)))==NULL) {
exit(28); /* cannot allocate little memory, unrecoverable error */
}
@ -88,7 +88,7 @@ dc_param_t* dc_param_new()
*/
void dc_param_unref(dc_param_t* param)
{
if( param==NULL ) {
if (param==NULL) {
return;
}
@ -107,7 +107,7 @@ void dc_param_unref(dc_param_t* param)
*/
void dc_param_empty(dc_param_t* param)
{
if( param == NULL ) {
if (param == NULL) {
return;
}
@ -128,13 +128,13 @@ void dc_param_empty(dc_param_t* param)
*/
void dc_param_set_packed(dc_param_t* param, const char* packed)
{
if( param == NULL ) {
if (param == NULL) {
return;
}
dc_param_empty(param);
if( packed ) {
if (packed) {
free(param->packed);
param->packed = dc_strdup(packed);
}
@ -147,13 +147,13 @@ void dc_param_set_packed(dc_param_t* param, const char* packed)
*/
void dc_param_set_urlencoded(dc_param_t* param, const char* urlencoded)
{
if( param == NULL ) {
if (param == NULL) {
return;
}
dc_param_empty(param);
if( urlencoded ) {
if (urlencoded) {
free(param->packed);
param->packed = dc_strdup(urlencoded);
dc_str_replace(&param->packed, "&", "\n");
@ -173,7 +173,7 @@ int dc_param_exists(dc_param_t* param, int key)
{
char *p2;
if( param == NULL || key == 0 ) {
if (param == NULL || key == 0) {
return 0;
}
@ -194,12 +194,12 @@ char* dc_param_get(dc_param_t* param, int key, const char* def)
{
char *p1, *p2, bak, *ret;
if( param == NULL || key == 0 ) {
if (param == NULL || key == 0) {
return def? dc_strdup(def) : NULL;
}
p1 = find_param(param->packed, key, &p2);
if( p1 == NULL ) {
if (p1 == NULL) {
return def? dc_strdup(def) : NULL;
}
@ -225,12 +225,12 @@ char* dc_param_get(dc_param_t* param, int key, const char* def)
*/
int32_t dc_param_get_int(dc_param_t* param, int key, int32_t def)
{
if( param == NULL || key == 0 ) {
if (param == NULL || key == 0) {
return def;
}
char* str = dc_param_get(param, key, NULL);
if( str == NULL ) {
if (str == NULL) {
return def;
}
int32_t ret = atol(str);
@ -252,7 +252,7 @@ void dc_param_set(dc_param_t* param, int key, const char* value)
{
char *old1, *old2, *new1 = NULL;
if( param == NULL || key == 0 ) {
if (param == NULL || key == 0) {
return;
}
@ -260,14 +260,14 @@ void dc_param_set(dc_param_t* param, int key, const char* value)
old2 = NULL;
/* remove existing parameter from packed string, if any */
if( old1 ) {
if (old1) {
char *p1, *p2;
p1 = find_param(old1, key, &p2);
if( p1 != NULL ) {
if (p1 != NULL) {
*p1 = 0;
old2 = p2;
}
else if( value==NULL ) {
else if (value==NULL) {
return; /* parameter does not exist and should be cleared -> done. */
}
}
@ -275,11 +275,11 @@ void dc_param_set(dc_param_t* param, int key, const char* value)
dc_rtrim(old1); /* trim functions are null-pointer-safe */
dc_ltrim(old2);
if( old1 && old1[0]==0 ) { old1 = NULL; }
if( old2 && old2[0]==0 ) { old2 = NULL; }
if (old1 && old1[0]==0) { old1 = NULL; }
if (old2 && old2[0]==0) { old2 = NULL; }
/* create new string */
if( value ) {
if (value) {
new1 = dc_mprintf("%s%s%c=%s%s%s",
old1? old1 : "",
old1? "\n" : "",
@ -311,12 +311,12 @@ void dc_param_set(dc_param_t* param, int key, const char* value)
*/
void dc_param_set_int(dc_param_t* param, int key, int32_t value)
{
if( param == NULL || key == 0 ) {
if (param == NULL || key == 0) {
return;
}
char* value_str = dc_mprintf("%i", (int)value);
if( value_str == NULL ) {
if (value_str == NULL) {
return;
}
dc_param_set(param, key, value_str);

View file

@ -51,7 +51,7 @@ static pgp_io_t s_io;
void dc_pgp_init(void)
{
if( s_io_initialized ) {
if (s_io_initialized) {
return;
}
@ -71,7 +71,7 @@ void dc_pgp_exit(void)
void dc_pgp_rand_seed(dc_context_t* context, const void* buf, size_t bytes)
{
if( buf == NULL || bytes <= 0 ) {
if (buf == NULL || bytes <= 0) {
return;
}
@ -93,36 +93,36 @@ int dc_split_armored_data(char* buf, const char** ret_headerline, const char** r
char* base64 = NULL;
#define PGP_WS "\t\r\n "
if( ret_headerline ) { *ret_headerline = NULL; }
if( ret_setupcodebegin ) { *ret_setupcodebegin = NULL; }
if( ret_preferencrypt ) { *ret_preferencrypt = NULL; }
if( ret_base64 ) { *ret_base64 = NULL; }
if (ret_headerline) { *ret_headerline = NULL; }
if (ret_setupcodebegin) { *ret_setupcodebegin = NULL; }
if (ret_preferencrypt) { *ret_preferencrypt = NULL; }
if (ret_base64) { *ret_base64 = NULL; }
if( buf == NULL || ret_headerline == NULL ) {
if (buf == NULL || ret_headerline == NULL) {
goto cleanup;
}
dc_remove_cr_chars(buf);
while( *p1 ) {
if( *p1 == '\n' ) {
while (*p1) {
if (*p1 == '\n') {
/* line found ... */
line[line_chars] = 0;
if( headerline == NULL ) {
if (headerline == NULL) {
/* ... headerline */
dc_trim(line);
if( strncmp(line, "-----BEGIN ", 11)==0 && strncmp(&line[strlen(line)-5], "-----", 5)==0 ) {
if (strncmp(line, "-----BEGIN ", 11)==0 && strncmp(&line[strlen(line)-5], "-----", 5)==0) {
headerline = line;
if( ret_headerline ) {
if (ret_headerline) {
*ret_headerline = headerline;
}
}
}
else if( strspn(line, PGP_WS)==strlen(line) ) {
else if (strspn(line, PGP_WS)==strlen(line)) {
/* ... empty line: base64 starts on next line */
base64 = p1+1;
break;
}
else if( (p2=strchr(line, ':'))==NULL ) {
else if ((p2=strchr(line, ':'))==NULL) {
/* ... non-standard-header without empty line: base64 starts with this line */
line[line_chars] = '\n';
base64 = line;
@ -132,17 +132,17 @@ int dc_split_armored_data(char* buf, const char** ret_headerline, const char** r
/* header line */
*p2 = 0;
dc_trim(line);
if( strcasecmp(line, "Passphrase-Begin")==0 ) {
if (strcasecmp(line, "Passphrase-Begin")==0) {
p2++;
dc_trim(p2);
if( ret_setupcodebegin ) {
if (ret_setupcodebegin) {
*ret_setupcodebegin = p2;
}
}
else if( strcasecmp(line, "Autocrypt-Prefer-Encrypt")==0 ) {
else if (strcasecmp(line, "Autocrypt-Prefer-Encrypt")==0) {
p2++;
dc_trim(p2);
if( ret_preferencrypt ) {
if (ret_preferencrypt) {
*ret_preferencrypt = p2;
}
}
@ -159,20 +159,20 @@ int dc_split_armored_data(char* buf, const char** ret_headerline, const char** r
}
}
if( headerline == NULL || base64 == NULL ) {
if (headerline == NULL || base64 == NULL) {
goto cleanup;
}
/* now, line points to beginning of base64 data, search end */
if( (p1=strstr(base64, "-----END "/*the trailing space makes sure, this is not a normal base64 sequence*/))==NULL
|| strncmp(p1+9, headerline+11, strlen(headerline+11))!=0 ) {
if ((p1=strstr(base64, "-----END "/*the trailing space makes sure, this is not a normal base64 sequence*/))==NULL
|| strncmp(p1+9, headerline+11, strlen(headerline+11))!=0) {
goto cleanup;
}
*p1 = 0;
dc_trim(base64);
if( ret_base64 ) {
if (ret_base64) {
*ret_base64 = base64;
}
@ -303,8 +303,8 @@ int dc_pgp_create_keypair(dc_context_t* context, const char* addr, dc_key_t* ret
memset(&pubkey, 0, sizeof(pgp_key_t));
memset(&subkey, 0, sizeof(pgp_key_t));
if( context==NULL || addr==NULL || ret_public_key==NULL || ret_private_key==NULL
|| pubmem==NULL || secmem==NULL || pubout==NULL || secout==NULL ) {
if (context==NULL || addr==NULL || ret_public_key==NULL || ret_private_key==NULL
|| pubmem==NULL || secmem==NULL || pubout==NULL || secout==NULL) {
goto cleanup;
}
@ -325,8 +325,8 @@ int dc_pgp_create_keypair(dc_context_t* context, const char* addr, dc_key_t* ret
/* generate two keypairs */
if( !pgp_rsa_generate_keypair(&seckey, 3072/*bits*/, 65537UL/*e*/, NULL, NULL, NULL, 0)
|| !pgp_rsa_generate_keypair(&subkey, 3072/*bits*/, 65537UL/*e*/, NULL, NULL, NULL, 0) ) {
if (!pgp_rsa_generate_keypair(&seckey, 3072/*bits*/, 65537UL/*e*/, NULL, NULL, NULL, 0)
|| !pgp_rsa_generate_keypair(&subkey, 3072/*bits*/, 65537UL/*e*/, NULL, NULL, NULL, 0)) {
goto cleanup;
}
@ -371,14 +371,14 @@ int dc_pgp_create_keypair(dc_context_t* context, const char* addr, dc_key_t* ret
------------------------------------------------------------------------ */
pgp_writer_set_memory(pubout, pubmem);
if( !pgp_write_xfer_key(pubout, &pubkey, 0/*armored*/)
|| pubmem->buf == NULL || pubmem->length <= 0 ) {
if (!pgp_write_xfer_key(pubout, &pubkey, 0/*armored*/)
|| pubmem->buf == NULL || pubmem->length <= 0) {
goto cleanup;
}
pgp_writer_set_memory(secout, secmem);
if( !pgp_write_xfer_key(secout, &seckey, 0/*armored*/)
|| secmem->buf == NULL || secmem->length <= 0 ) {
if (!pgp_write_xfer_key(secout, &seckey, 0/*armored*/)
|| secmem->buf == NULL || secmem->length <= 0) {
goto cleanup;
}
@ -388,10 +388,10 @@ int dc_pgp_create_keypair(dc_context_t* context, const char* addr, dc_key_t* ret
success = 1;
cleanup:
if( pubout ) { pgp_output_delete(pubout); }
if( secout ) { pgp_output_delete(secout); }
if( pubmem ) { pgp_memory_free(pubmem); }
if( secmem ) { pgp_memory_free(secmem); }
if (pubout) { pgp_output_delete(pubout); }
if (secout) { pgp_output_delete(secout); }
if (pubmem) { pgp_memory_free(pubmem); }
if (secmem) { pgp_memory_free(secmem); }
pgp_key_free(&seckey); /* not: pgp_keydata_free() which will also free the pointer itself (we created it on the stack) */
pgp_key_free(&pubkey);
pgp_key_free(&subkey);
@ -412,26 +412,26 @@ int dc_pgp_is_valid_key(dc_context_t* context, const dc_key_t* raw_key)
pgp_keyring_t* private_keys = calloc(1, sizeof(pgp_keyring_t));
pgp_memory_t* keysmem = pgp_memory_new();
if( context==NULL || raw_key==NULL
if (context==NULL || raw_key==NULL
|| raw_key->binary == NULL || raw_key->bytes <= 0
|| public_keys==NULL || private_keys==NULL || keysmem==NULL ) {
|| public_keys==NULL || private_keys==NULL || keysmem==NULL) {
goto cleanup;
}
pgp_memory_add(keysmem, raw_key->binary, raw_key->bytes);
pgp_filter_keys_from_mem(&s_io, public_keys, private_keys, NULL, 0, keysmem); /* function returns 0 on any error in any packet - this does not mean, we cannot use the key. We check the details below therefore. */
if( raw_key->type == DC_KEY_PUBLIC && public_keys->keyc >= 1 ) {
if (raw_key->type == DC_KEY_PUBLIC && public_keys->keyc >= 1) {
key_is_valid = 1;
}
else if( raw_key->type == DC_KEY_PRIVATE && private_keys->keyc >= 1 ) {
else if (raw_key->type == DC_KEY_PRIVATE && private_keys->keyc >= 1) {
key_is_valid = 1;
}
cleanup:
if( keysmem ) { pgp_memory_free(keysmem); }
if( public_keys ) { pgp_keyring_purge(public_keys); free(public_keys); } /*pgp_keyring_free() frees the content, not the pointer itself*/
if( private_keys ) { pgp_keyring_purge(private_keys); free(private_keys); }
if (keysmem) { pgp_memory_free(keysmem); }
if (public_keys) { pgp_keyring_purge(public_keys); free(public_keys); } /*pgp_keyring_free() frees the content, not the pointer itself*/
if (private_keys) { pgp_keyring_purge(private_keys); free(private_keys); }
return key_is_valid;
}
@ -443,22 +443,22 @@ int dc_pgp_calc_fingerprint(const dc_key_t* raw_key, uint8_t** ret_fingerprint,
pgp_keyring_t* private_keys = calloc(1, sizeof(pgp_keyring_t));
pgp_memory_t* keysmem = pgp_memory_new();
if( raw_key==NULL || ret_fingerprint==NULL || *ret_fingerprint!=NULL || ret_fingerprint_bytes==NULL || *ret_fingerprint_bytes!=0
if (raw_key==NULL || ret_fingerprint==NULL || *ret_fingerprint!=NULL || ret_fingerprint_bytes==NULL || *ret_fingerprint_bytes!=0
|| raw_key->binary == NULL || raw_key->bytes <= 0
|| public_keys==NULL || private_keys==NULL || keysmem==NULL ) {
|| public_keys==NULL || private_keys==NULL || keysmem==NULL) {
goto cleanup;
}
pgp_memory_add(keysmem, raw_key->binary, raw_key->bytes);
pgp_filter_keys_from_mem(&s_io, public_keys, private_keys, NULL, 0, keysmem);
if( raw_key->type != DC_KEY_PUBLIC || public_keys->keyc <= 0 ) {
if (raw_key->type != DC_KEY_PUBLIC || public_keys->keyc <= 0) {
goto cleanup;
}
pgp_key_t* key0 = &public_keys->keys[0];
pgp_pubkey_t* pubkey0 = &key0->key.pubkey;
if( !pgp_fingerprint(&key0->pubkeyfpr, pubkey0, 0) ) {
if (!pgp_fingerprint(&key0->pubkeyfpr, pubkey0, 0)) {
goto cleanup;
}
@ -469,9 +469,9 @@ int dc_pgp_calc_fingerprint(const dc_key_t* raw_key, uint8_t** ret_fingerprint,
success = 1;
cleanup:
if( keysmem ) { pgp_memory_free(keysmem); }
if( public_keys ) { pgp_keyring_purge(public_keys); free(public_keys); } /*pgp_keyring_free() frees the content, not the pointer itself*/
if( private_keys ) { pgp_keyring_purge(private_keys); free(private_keys); }
if (keysmem) { pgp_memory_free(keysmem); }
if (public_keys) { pgp_keyring_purge(public_keys); free(public_keys); } /*pgp_keyring_free() frees the content, not the pointer itself*/
if (private_keys) { pgp_keyring_purge(private_keys); free(private_keys); }
return success;
}
@ -485,27 +485,27 @@ int dc_pgp_split_key(dc_context_t* context, const dc_key_t* private_in, dc_key_t
pgp_memory_t* pubmem = pgp_memory_new();
pgp_output_t* pubout = pgp_output_new();
if( context == NULL || private_in==NULL || ret_public_key==NULL
|| public_keys==NULL || private_keys==NULL || keysmem==NULL || pubmem==NULL || pubout==NULL ) {
if (context == NULL || private_in==NULL || ret_public_key==NULL
|| public_keys==NULL || private_keys==NULL || keysmem==NULL || pubmem==NULL || pubout==NULL) {
goto cleanup;
}
pgp_memory_add(keysmem, private_in->binary, private_in->bytes);
pgp_filter_keys_from_mem(&s_io, public_keys, private_keys, NULL, 0, keysmem);
if( private_in->type!=DC_KEY_PRIVATE || private_keys->keyc <= 0 ) {
if (private_in->type!=DC_KEY_PRIVATE || private_keys->keyc <= 0) {
dc_log_warning(context, 0, "Split key: Given key is no private key.");
goto cleanup;
}
if( public_keys->keyc <= 0 ) {
if (public_keys->keyc <= 0) {
dc_log_warning(context, 0, "Split key: Given key does not contain a public key.");
goto cleanup;
}
pgp_writer_set_memory(pubout, pubmem);
if( !pgp_write_xfer_key(pubout, &public_keys->keys[0], 0/*armored*/)
|| pubmem->buf == NULL || pubmem->length <= 0 ) {
if (!pgp_write_xfer_key(pubout, &public_keys->keys[0], 0/*armored*/)
|| pubmem->buf == NULL || pubmem->length <= 0) {
goto cleanup;
}
@ -514,11 +514,11 @@ int dc_pgp_split_key(dc_context_t* context, const dc_key_t* private_in, dc_key_t
success = 1;
cleanup:
if( pubout ) { pgp_output_delete(pubout); }
if( pubmem ) { pgp_memory_free(pubmem); }
if( keysmem ) { pgp_memory_free(keysmem); }
if( public_keys ) { pgp_keyring_purge(public_keys); free(public_keys); } /*pgp_keyring_free() frees the content, not the pointer itself*/
if( private_keys ) { pgp_keyring_purge(private_keys); free(private_keys); }
if (pubout) { pgp_output_delete(pubout); }
if (pubmem) { pgp_memory_free(pubmem); }
if (keysmem) { pgp_memory_free(keysmem); }
if (public_keys) { pgp_keyring_purge(public_keys); free(public_keys); } /*pgp_keyring_free() frees the content, not the pointer itself*/
if (private_keys) { pgp_keyring_purge(private_keys); free(private_keys); }
return success;
}
@ -544,9 +544,9 @@ int dc_pgp_pk_encrypt( dc_context_t* context,
pgp_memory_t* signedmem = NULL;
int i, success = 0;
if( context==NULL || plain_text==NULL || plain_bytes==0 || ret_ctext==NULL || ret_ctext_bytes==NULL
if (context==NULL || plain_text==NULL || plain_bytes==0 || ret_ctext==NULL || ret_ctext_bytes==NULL
|| raw_public_keys_for_encryption==NULL || raw_public_keys_for_encryption->count<=0
|| keysmem==NULL || public_keys==NULL || private_keys==NULL || dummy_keys==NULL ) {
|| keysmem==NULL || public_keys==NULL || private_keys==NULL || dummy_keys==NULL) {
goto cleanup;
}
@ -554,13 +554,13 @@ int dc_pgp_pk_encrypt( dc_context_t* context,
*ret_ctext_bytes = 0;
/* setup keys (the keys may come from pgp_filter_keys_fileread(), see also pgp_keyring_add(rcpts, key)) */
for( i = 0; i < raw_public_keys_for_encryption->count; i++ ) {
for (i = 0; i < raw_public_keys_for_encryption->count; i++) {
pgp_memory_clear(keysmem);
pgp_memory_add(keysmem, raw_public_keys_for_encryption->keys[i]->binary, raw_public_keys_for_encryption->keys[i]->bytes);
pgp_filter_keys_from_mem(&s_io, public_keys, private_keys/*should stay empty*/, NULL, 0, keysmem);
}
if( public_keys->keyc <=0 || private_keys->keyc!=0 ) {
if (public_keys->keyc <=0 || private_keys->keyc!=0) {
dc_log_warning(context, 0, "Encryption-keyring contains unexpected data (%i/%i)", public_keys->keyc, private_keys->keyc);
goto cleanup;
}
@ -571,11 +571,11 @@ int dc_pgp_pk_encrypt( dc_context_t* context,
size_t signed_bytes = 0;
int encrypt_raw_packet = 0;
if( raw_private_key_for_signing ) {
if (raw_private_key_for_signing) {
pgp_memory_clear(keysmem);
pgp_memory_add(keysmem, raw_private_key_for_signing->binary, raw_private_key_for_signing->bytes);
pgp_filter_keys_from_mem(&s_io, dummy_keys, private_keys, NULL, 0, keysmem);
if( private_keys->keyc <= 0 ) {
if (private_keys->keyc <= 0) {
dc_log_warning(context, 0, "No key for signing found.");
goto cleanup;
}
@ -583,7 +583,7 @@ int dc_pgp_pk_encrypt( dc_context_t* context,
pgp_key_t* sk0 = &private_keys->keys[0];
signedmem = pgp_sign_buf(&s_io, plain_text, plain_bytes, &sk0->key.seckey, time(NULL)/*birthtime*/, 0/*duration*/,
NULL/*hash, defaults to sha256*/, 0/*armored*/, 0/*cleartext*/);
if( signedmem == NULL ) {
if (signedmem == NULL) {
dc_log_warning(context, 0, "Signing failed.");
goto cleanup;
}
@ -598,7 +598,7 @@ int dc_pgp_pk_encrypt( dc_context_t* context,
}
pgp_memory_t* outmem = pgp_encrypt_buf(&s_io, signed_text, signed_bytes, public_keys, use_armor, NULL/*cipher*/, encrypt_raw_packet);
if( outmem == NULL ) {
if (outmem == NULL) {
dc_log_warning(context, 0, "Encryption failed.");
goto cleanup;
}
@ -610,11 +610,11 @@ int dc_pgp_pk_encrypt( dc_context_t* context,
success = 1;
cleanup:
if( keysmem ) { pgp_memory_free(keysmem); }
if( signedmem ) { pgp_memory_free(signedmem); }
if( public_keys ) { pgp_keyring_purge(public_keys); free(public_keys); } /*pgp_keyring_free() frees the content, not the pointer itself*/
if( private_keys ) { pgp_keyring_purge(private_keys); free(private_keys); }
if( dummy_keys ) { pgp_keyring_purge(dummy_keys); free(dummy_keys); }
if (keysmem) { pgp_memory_free(keysmem); }
if (signedmem) { pgp_memory_free(signedmem); }
if (public_keys) { pgp_keyring_purge(public_keys); free(public_keys); } /*pgp_keyring_free() frees the content, not the pointer itself*/
if (private_keys) { pgp_keyring_purge(private_keys); free(private_keys); }
if (dummy_keys) { pgp_keyring_purge(dummy_keys); free(dummy_keys); }
return success;
}
@ -638,9 +638,9 @@ int dc_pgp_pk_decrypt( dc_context_t* context,
pgp_memory_t* keysmem = pgp_memory_new();
int i, success = 0;
if( context==NULL || ctext==NULL || ctext_bytes==0 || ret_plain==NULL || ret_plain_bytes==NULL
if (context==NULL || ctext==NULL || ctext_bytes==0 || ret_plain==NULL || ret_plain_bytes==NULL
|| raw_private_keys_for_decryption==NULL || raw_private_keys_for_decryption->count<=0
|| vresult==NULL || keysmem==NULL || public_keys==NULL || private_keys==NULL ) {
|| vresult==NULL || keysmem==NULL || public_keys==NULL || private_keys==NULL) {
goto cleanup;
}
@ -648,19 +648,19 @@ int dc_pgp_pk_decrypt( dc_context_t* context,
*ret_plain_bytes = 0;
/* setup keys (the keys may come from pgp_filter_keys_fileread(), see also pgp_keyring_add(rcpts, key)) */
for( i = 0; i < raw_private_keys_for_decryption->count; i++ ) {
for (i = 0; i < raw_private_keys_for_decryption->count; i++) {
pgp_memory_clear(keysmem); /* a simple concatenate of private binary keys fails (works for public keys, however, we don't do it there either) */
pgp_memory_add(keysmem, raw_private_keys_for_decryption->keys[i]->binary, raw_private_keys_for_decryption->keys[i]->bytes);
pgp_filter_keys_from_mem(&s_io, dummy_keys/*should stay empty*/, private_keys, NULL, 0, keysmem);
}
if( private_keys->keyc<=0 ) {
if (private_keys->keyc<=0) {
dc_log_warning(context, 0, "Decryption-keyring contains unexpected data (%i/%i)", public_keys->keyc, private_keys->keyc);
goto cleanup;
}
if( raw_public_keys_for_validation ) {
for( i = 0; i < raw_public_keys_for_validation->count; i++ ) {
if (raw_public_keys_for_validation) {
for (i = 0; i < raw_public_keys_for_validation->count; i++) {
pgp_memory_clear(keysmem);
pgp_memory_add(keysmem, raw_public_keys_for_validation->keys[i]->binary, raw_public_keys_for_validation->keys[i]->bytes);
pgp_filter_keys_from_mem(&s_io, public_keys, dummy_keys/*should stay empty*/, NULL, 0, keysmem);
@ -671,7 +671,7 @@ int dc_pgp_pk_decrypt( dc_context_t* context,
{
pgp_memory_t* outmem = pgp_decrypt_and_validate_buf(&s_io, vresult, ctext, ctext_bytes, private_keys, public_keys,
use_armor, &recipients_key_ids, &recipients_count);
if( outmem == NULL ) {
if (outmem == NULL) {
dc_log_warning(context, 0, "Decryption failed.");
goto cleanup;
}
@ -680,20 +680,20 @@ int dc_pgp_pk_decrypt( dc_context_t* context,
free(outmem); /* do not use pgp_memory_free() as we took ownership of the buffer */
// collect the keys of the valid signatures
if( ret_signature_fingerprints )
if (ret_signature_fingerprints)
{
for( i = 0; i < vresult->validc; i++ )
for (i = 0; i < vresult->validc; i++)
{
unsigned from = 0;
pgp_key_t* key0 = pgp_getkeybyid(&s_io, public_keys, vresult->valid_sigs[i].signer_id, &from, NULL, NULL, 0, 0);
if( key0 ) {
if (key0) {
pgp_pubkey_t* pubkey0 = &key0->key.pubkey;
if( !pgp_fingerprint(&key0->pubkeyfpr, pubkey0, 0) ) {
if (!pgp_fingerprint(&key0->pubkeyfpr, pubkey0, 0)) {
goto cleanup;
}
char* fingerprint_hex = dc_binary_to_uc_hex(key0->pubkeyfpr.fingerprint, key0->pubkeyfpr.length);
if( fingerprint_hex ) {
if (fingerprint_hex) {
dc_hash_insert(ret_signature_fingerprints, fingerprint_hex, strlen(fingerprint_hex), (void*)1);
}
free(fingerprint_hex);
@ -705,11 +705,11 @@ int dc_pgp_pk_decrypt( dc_context_t* context,
success = 1;
cleanup:
if( keysmem ) { pgp_memory_free(keysmem); }
if( public_keys ) { pgp_keyring_purge(public_keys); free(public_keys); } /*pgp_keyring_free() frees the content, not the pointer itself*/
if( private_keys ) { pgp_keyring_purge(private_keys); free(private_keys); }
if( dummy_keys ) { pgp_keyring_purge(dummy_keys); free(dummy_keys); }
if( vresult ) { pgp_validate_result_free(vresult); }
if( recipients_key_ids ) { free(recipients_key_ids); }
if (keysmem) { pgp_memory_free(keysmem); }
if (public_keys) { pgp_keyring_purge(public_keys); free(public_keys); } /*pgp_keyring_free() frees the content, not the pointer itself*/
if (private_keys) { pgp_keyring_purge(private_keys); free(private_keys); }
if (dummy_keys) { pgp_keyring_purge(dummy_keys); free(dummy_keys); }
if (vresult) { pgp_validate_result_free(vresult); }
if (recipients_key_ids) { free(recipients_key_ids); }
return success;
}

View file

@ -59,7 +59,7 @@ dc_lot_t* dc_check_qr(dc_context_t* context, const char* qr)
qr_parsed->state = 0;
if( context==NULL || context->magic!=DC_CONTEXT_MAGIC || qr==NULL ) {
if (context==NULL || context->magic!=DC_CONTEXT_MAGIC || qr==NULL) {
goto cleanup;
}
@ -68,14 +68,14 @@ dc_lot_t* dc_check_qr(dc_context_t* context, const char* qr)
/* split parameters from the qr code
------------------------------------ */
if( strncasecmp(qr, DC_OPENPGP4FPR_SCHEME, strlen(DC_OPENPGP4FPR_SCHEME)) == 0 )
if (strncasecmp(qr, DC_OPENPGP4FPR_SCHEME, strlen(DC_OPENPGP4FPR_SCHEME)) == 0)
{
/* scheme: OPENPGP4FPR:FINGERPRINT#a=ADDR&n=NAME&i=INVITENUMBER&s=AUTH
or: OPENPGP4FPR:FINGERPRINT#a=ADDR&g=GROUPNAME&x=GROUPID&i=INVITENUMBER&s=AUTH */
payload = dc_strdup(&qr[strlen(DC_OPENPGP4FPR_SCHEME)]);
char* fragment = strchr(payload, '#'); /* must not be freed, only a pointer inside payload */
if( fragment )
if (fragment)
{
*fragment = 0;
fragment++;
@ -84,9 +84,9 @@ dc_lot_t* dc_check_qr(dc_context_t* context, const char* qr)
dc_param_set_urlencoded(param, fragment);
addr = dc_param_get(param, 'a', NULL);
if( addr ) {
if (addr) {
char* urlencoded = dc_param_get(param, 'n', NULL);
if(urlencoded ) {
if(urlencoded) {
name = dc_urldecode(urlencoded);
dc_normalize_name(name);
free(urlencoded);
@ -96,9 +96,9 @@ dc_lot_t* dc_check_qr(dc_context_t* context, const char* qr)
auth = dc_param_get(param, 's', NULL);
grpid = dc_param_get(param, 'x', NULL);
if( grpid ) {
if (grpid) {
urlencoded = dc_param_get(param, 'g', NULL);
if( urlencoded ) {
if (urlencoded) {
grpname = dc_urldecode(urlencoded);
free(urlencoded);
}
@ -110,34 +110,34 @@ dc_lot_t* dc_check_qr(dc_context_t* context, const char* qr)
fingerprint = dc_normalize_fingerprint(payload);
}
else if( strncasecmp(qr, MAILTO_SCHEME, strlen(MAILTO_SCHEME)) == 0 )
else if (strncasecmp(qr, MAILTO_SCHEME, strlen(MAILTO_SCHEME)) == 0)
{
/* scheme: mailto:addr...?subject=...&body=... */
payload = dc_strdup(&qr[strlen(MAILTO_SCHEME)]);
char* query = strchr(payload, '?'); /* must not be freed, only a pointer inside payload */
if( query ) {
if (query) {
*query = 0;
}
addr = dc_strdup(payload);
}
else if( strncasecmp(qr, SMTP_SCHEME, strlen(SMTP_SCHEME)) == 0 )
else if (strncasecmp(qr, SMTP_SCHEME, strlen(SMTP_SCHEME)) == 0)
{
/* scheme: `SMTP:addr...:subject...:body...` */
payload = dc_strdup(&qr[strlen(SMTP_SCHEME)]);
char* colon = strchr(payload, ':'); /* must not be freed, only a pointer inside payload */
if( colon ) {
if (colon) {
*colon = 0;
}
addr = dc_strdup(payload);
}
else if( strncasecmp(qr, MATMSG_SCHEME, strlen(MATMSG_SCHEME)) == 0 )
else if (strncasecmp(qr, MATMSG_SCHEME, strlen(MATMSG_SCHEME)) == 0)
{
/* scheme: `MATMSG:TO:addr...;SUB:subject...;BODY:body...;` - there may or may not be linebreaks after the fields */
char* to = strstr(qr, "TO:"); /* does not work when the text `TO:` is used in subject/body _and_ TO: is not the first field. we ignore this case. */
if( to ) {
if (to) {
addr = dc_strdup(&to[3]);
char* semicolon = strchr(addr, ';');
if( semicolon ) { *semicolon = 0; }
if (semicolon) { *semicolon = 0; }
}
else {
qr_parsed->state = DC_QR_ERROR;
@ -145,23 +145,23 @@ dc_lot_t* dc_check_qr(dc_context_t* context, const char* qr)
goto cleanup;
}
}
else if( strncasecmp(qr, VCARD_BEGIN, strlen(VCARD_BEGIN)) == 0 )
else if (strncasecmp(qr, VCARD_BEGIN, strlen(VCARD_BEGIN)) == 0)
{
/* scheme: `VCARD:BEGIN\nN:last name;first name;...;\nEMAIL:addr...;` */
carray* lines = dc_split_into_lines(qr);
for( int i = 0; i < carray_count(lines); i++ ) {
for (int i = 0; i < carray_count(lines); i++) {
char* key = (char*)carray_get(lines, i); dc_trim(key);
char* value = strchr(key, ':');
if( value ) {
if (value) {
*value = 0;
value++;
char* semicolon = strchr(key, ';'); if( semicolon ) { *semicolon = 0; } /* handle `EMAIL;type=work:` stuff */
if( strcasecmp(key, "EMAIL") == 0 ) {
semicolon = strchr(value, ';'); if( semicolon ) { *semicolon = 0; } /* use the first EMAIL */
char* semicolon = strchr(key, ';'); if (semicolon) { *semicolon = 0; } /* handle `EMAIL;type=work:` stuff */
if (strcasecmp(key, "EMAIL") == 0) {
semicolon = strchr(value, ';'); if (semicolon) { *semicolon = 0; } /* use the first EMAIL */
addr = dc_strdup(value);
}
else if( strcasecmp(key, "N") == 0 ) {
semicolon = strchr(value, ';'); if( semicolon ) { semicolon = strchr(semicolon+1, ';'); if( semicolon ) { *semicolon = 0; } } /* the N format is `lastname;prename;wtf;title` - skip everything after the second semicolon */
else if (strcasecmp(key, "N") == 0) {
semicolon = strchr(value, ';'); if (semicolon) { semicolon = strchr(semicolon+1, ';'); if (semicolon) { *semicolon = 0; } } /* the N format is `lastname;prename;wtf;title` - skip everything after the second semicolon */
name = dc_strdup(value);
dc_str_replace(&name, ";", ","); /* the format "lastname,prename" is handled by dc_normalize_name() */
dc_normalize_name(name);
@ -174,19 +174,19 @@ dc_lot_t* dc_check_qr(dc_context_t* context, const char* qr)
/* check the paramters
---------------------- */
if( addr ) {
if (addr) {
char* temp = dc_urldecode(addr); free(addr); addr = temp; /* urldecoding is needed at least for OPENPGP4FPR but should not hurt in the other cases */
temp = dc_normalize_addr(addr); free(addr); addr = temp;
if( strlen(addr) < 3 || strchr(addr, '@')==NULL || strchr(addr, '.')==NULL ) {
if (strlen(addr) < 3 || strchr(addr, '@')==NULL || strchr(addr, '.')==NULL) {
qr_parsed->state = DC_QR_ERROR;
qr_parsed->text1 = dc_strdup("Bad e-mail address.");
goto cleanup;
}
}
if( fingerprint ) {
if( strlen(fingerprint) != 40 ) {
if (fingerprint) {
if (strlen(fingerprint) != 40) {
qr_parsed->state = DC_QR_ERROR;
qr_parsed->text1 = dc_strdup("Bad fingerprint length in QR code.");
goto cleanup;
@ -196,15 +196,15 @@ dc_lot_t* dc_check_qr(dc_context_t* context, const char* qr)
/* let's see what we can do with the parameters
---------------------------------------------- */
if( fingerprint )
if (fingerprint)
{
/* fingerprint set ... */
if( addr == NULL || invitenumber == NULL || auth == NULL )
if (addr == NULL || invitenumber == NULL || auth == NULL)
{
// _only_ fingerprint set ...
// (we could also do this before/instead of a secure-join, however, this may require complicated questions in the ui)
if( dc_apeerstate_load_by_fingerprint(peerstate, context->sql, fingerprint) ) {
if (dc_apeerstate_load_by_fingerprint(peerstate, context->sql, fingerprint)) {
qr_parsed->state = DC_QR_FPR_OK;
qr_parsed->id = dc_add_or_lookup_contact(context, NULL, peerstate->addr, DC_ORIGIN_UNHANDLED_QR_SCAN, NULL);
@ -221,7 +221,7 @@ dc_lot_t* dc_check_qr(dc_context_t* context, const char* qr)
// fingerprint + addr set, secure-join requested
// do not comapre the fingerprint already - it may have changed - errors are catched later more proberly.
// (theroretically, there is also the state "addr=set, fingerprint=set, invitenumber=0", however, currently, we won't get into this state)
if( grpid && grpname ) {
if (grpid && grpname) {
qr_parsed->state = DC_QR_ASK_VERIFYGROUP;
qr_parsed->text1 = dc_strdup(grpname);
qr_parsed->text2 = dc_strdup(grpid);
@ -236,12 +236,12 @@ dc_lot_t* dc_check_qr(dc_context_t* context, const char* qr)
qr_parsed->auth = dc_strdup(auth);
}
}
else if( addr )
else if (addr)
{
qr_parsed->state = DC_QR_ADDR;
qr_parsed->id = dc_add_or_lookup_contact(context, name, addr, DC_ORIGIN_UNHANDLED_QR_SCAN, NULL);
}
else if( strstr(qr, "http://")==qr || strstr(qr, "https://")==qr )
else if (strstr(qr, "http://")==qr || strstr(qr, "https://")==qr)
{
qr_parsed->state = DC_QR_URL;
qr_parsed->text1 = dc_strdup(qr);
@ -252,7 +252,7 @@ dc_lot_t* dc_check_qr(dc_context_t* context, const char* qr)
qr_parsed->text1 = dc_strdup(qr);
}
if( device_msg ) {
if (device_msg) {
dc_add_device_msg(context, chat_id, device_msg);
}

File diff suppressed because it is too large Load diff

View file

@ -154,14 +154,14 @@ static char* xml_decode(char* s, char type)
}
}
for (s = r; ; ) {
while( *s && *s != '&' /*&& (*s != '%' || type != '%')*/ && !isspace(*s)) s++;
for (s = r; ;) {
while (*s && *s != '&' /*&& (*s != '%' || type != '%')*/ && !isspace(*s)) s++;
if( ! *s )
if (! *s)
{
break;
}
else if( type != 'c' && ! strncmp(s, "&#", 2) )
else if (type != 'c' && ! strncmp(s, "&#", 2))
{
/* character reference */
if (s[2] == 'x') c = strtol(s + 3, &e, 16); /* base 16 */
@ -178,8 +178,8 @@ static char* xml_decode(char* s, char type)
memmove(s, strchr(s, ';') + 1, strlen(strchr(s, ';')));
}
else if( (*s == '&' && (type == '&' || type == ' ' /*|| type == '*'*/))
/*|| (*s == '%' && type == '%')*/ )
else if ((*s == '&' && (type == '&' || type == ' ' /*|| type == '*'*/))
/*|| (*s == '%' && type == '%')*/)
{
/* entity reference */
for (b = 0; s_ent[b] && strncmp(s + 1, s_ent[b], strlen(s_ent[b])); b += 2)
@ -189,13 +189,13 @@ static char* xml_decode(char* s, char type)
if ((c = strlen(s_ent[b])) - 1 > (e = strchr(s, ';')) - s) {
/* the replacement is larger than the entity: enlarge buffer */
l = (d = (s - r)) + c + strlen(e); /* new length */
if( r == original_buf ) {
char* new_ret = malloc(l); if( new_ret == NULL ) { return r; }
if (r == original_buf) {
char* new_ret = malloc(l); if (new_ret == NULL) { return r; }
strcpy(new_ret, r);
r = new_ret;
}
else {
char* new_ret = realloc(r, l); if( new_ret == NULL ) { return r; }
char* new_ret = realloc(r, l); if (new_ret == NULL) { return r; }
r = new_ret;
}
e = strchr((s = r + d), ';'); /* fix up pointers */
@ -241,14 +241,14 @@ static void def_text_cb (void* userdata, const char* text, int len) { }
static void call_text_cb(dc_saxparser_t* saxparser, char* text, size_t len, char type)
{
if( text && len )
if (text && len)
{
char bak = text[len], *text_new;
text[len] = '\0';
text_new = xml_decode(text, type);
saxparser->text_cb(saxparser->userdata, text_new, len);
if( text != text_new ) { free(text_new); }
if (text != text_new) { free(text_new); }
text[len] = bak;
}
@ -262,9 +262,9 @@ static void do_free_attr(char** attr, int* free_attr)
#define FREE_KEY 0x01
#define FREE_VALUE 0x02
int i = 0;
while( attr[i] ) {
if( free_attr[i>>1]&FREE_KEY && attr[i] ) { free(attr[i]); }
if( free_attr[i>>1]&FREE_VALUE && attr[i+1] ) { free(attr[i+1]); }
while (attr[i]) {
if (free_attr[i>>1]&FREE_KEY && attr[i] ) { free(attr[i]); }
if (free_attr[i>>1]&FREE_VALUE && attr[i+1]) { free(attr[i+1]); }
i += 2;
}
attr[0] = NULL; /* set list to zero-length */
@ -278,13 +278,13 @@ static void do_free_attr(char** attr, int* free_attr)
const char* dc_attr_find(char** attr, const char* key)
{
if( attr && key ) {
if (attr && key) {
int i = 0;
while( attr[i] && strcmp(key, attr[i]) ) {
while (attr[i] && strcmp(key, attr[i])) {
i += 2;
}
if( attr[i] ) {
if (attr[i]) {
return attr[i + 1];
}
}
@ -303,7 +303,7 @@ void dc_saxparser_init(dc_saxparser_t* saxparser, void* userdata)
void dc_saxparser_set_tag_handler(dc_saxparser_t* saxparser, dc_saxparser_starttag_cb_t starttag_cb, dc_saxparser_endtag_cb_t endtag_cb)
{
if( saxparser == NULL ) {
if (saxparser == NULL) {
return;
}
@ -314,7 +314,7 @@ void dc_saxparser_set_tag_handler(dc_saxparser_t* saxparser, dc_saxparser_startt
void dc_saxparser_set_text_handler (dc_saxparser_t* saxparser, dc_saxparser_text_cb_t text_cb)
{
if( saxparser == NULL ) {
if (saxparser == NULL) {
return;
}
@ -332,36 +332,36 @@ void dc_saxparser_parse(dc_saxparser_t* saxparser, const char* buf_start__)
attr[0] = NULL; /* null-terminate list, this also terminates "free_values" */
if( saxparser == NULL ) {
if (saxparser == NULL) {
return;
}
buf_start = dc_strdup(buf_start__); /* we make a copy as we can easily null-terminate tag names and attributes "in place" */
last_text_start = buf_start;
p = buf_start;
while( *p )
while (*p)
{
if( *p == '<' )
if (*p == '<')
{
call_text_cb(saxparser, last_text_start, p - last_text_start, '&'); /* flush pending text */
p++;
if( strncmp(p, "!--", 3) == 0 )
if (strncmp(p, "!--", 3) == 0)
{
/* skip <!-- ... --> comment
**************************************************************/
p = strstr(p, "-->");
if( p == NULL ) { goto cleanup; }
if (p == NULL) { goto cleanup; }
p += 3;
}
else if( strncmp(p, "![CDATA[", 8) == 0 )
else if (strncmp(p, "![CDATA[", 8) == 0)
{
/* process <![CDATA[ ... ]]> text
**************************************************************/
char* text_beg = p + 8;
if( (p = strstr(p, "]]>"))!=NULL ) /* `]]>` itself is not allowed in CDATA and must be escaped by dividing into two CDATA parts */ {
if ((p = strstr(p, "]]>"))!=NULL) /* `]]>` itself is not allowed in CDATA and must be escaped by dividing into two CDATA parts */ {
call_text_cb(saxparser, text_beg, p-text_beg, 'c');
p += 3;
}
@ -370,18 +370,18 @@ void dc_saxparser_parse(dc_saxparser_t* saxparser, const char* buf_start__)
goto cleanup;
}
}
else if( strncmp(p, "!DOCTYPE", 8) == 0 )
else if (strncmp(p, "!DOCTYPE", 8) == 0)
{
/* skip <!DOCTYPE ...> or <!DOCTYPE name [ ... ]>
**************************************************************/
while( *p && *p != '[' && *p != '>' ) p++; /* search for [ or >, whatever comes first */
if( *p == 0 ) {
while (*p && *p != '[' && *p != '>' ) p++; /* search for [ or >, whatever comes first */
if (*p == 0) {
goto cleanup; /* unclosed doctype */
}
else if( *p == '[' ) {
else if (*p == '[') {
p = strstr(p, "]>"); /* search end of inline doctype */
if( p == NULL ) {
if (p == NULL) {
goto cleanup; /* unclosed inline doctype */
}
else {
@ -392,19 +392,19 @@ void dc_saxparser_parse(dc_saxparser_t* saxparser, const char* buf_start__)
p++;
}
}
else if( *p == '?' )
else if (*p == '?')
{
/* skip <? ... ?> processing instruction
**************************************************************/
p = strstr(p, "?>");
if( p == NULL ) { goto cleanup; } /* unclosed processing instruction */
if (p == NULL) { goto cleanup; } /* unclosed processing instruction */
p += 2;
}
else
{
p += strspn(p, XML_WS); /* skip whitespace between `<` and tagname */
if( *p == '/' )
if (*p == '/')
{
/* process </tag> end tag
**************************************************************/
@ -413,7 +413,7 @@ void dc_saxparser_parse(dc_saxparser_t* saxparser, const char* buf_start__)
p += strspn(p, XML_WS); /* skip whitespace between `/` and tagname */
char* beg_tag_name = p;
p += strcspn(p, XML_WS "/>"); /* find character after tagname */
if( p != beg_tag_name )
if (p != beg_tag_name)
{
bak = *p;
*p = '\0'; /* null-terminate tag name temporary, eg. a covered `>` may get important downwards */
@ -431,13 +431,13 @@ void dc_saxparser_parse(dc_saxparser_t* saxparser, const char* buf_start__)
char* beg_tag_name = p;
p += strcspn(p, XML_WS "/>"); /* find character after tagname */
if( p != beg_tag_name )
if (p != beg_tag_name)
{
char* after_tag_name = p;
/* scan for attributes */
int attr_index = 0;
while( isspace(*p) ) { p++; } /* forward to first attribute name beginning */
while (isspace(*p)) { p++; } /* forward to first attribute name beginning */
while (*p && *p!='/' && *p!='>')
{
char *beg_attr_name = p, *beg_attr_value = NULL, *beg_attr_value_new = NULL;
@ -447,23 +447,23 @@ void dc_saxparser_parse(dc_saxparser_t* saxparser, const char* buf_start__)
}
p += strcspn(p, XML_WS "=/>"); /* get end of attribute name */
if( p != beg_attr_name )
if (p != beg_attr_name)
{
/* attribute found */
char* after_attr_name = p;
p += strspn(p, XML_WS); /* skip whitespace between attribute name and possible `=` */
if( *p == '=' )
if (*p == '=')
{
p += strspn(p, XML_WS "="); /* skip spaces and equal signs */
char quote = *p;
if( quote == '"' || quote == '\'' )
if (quote == '"' || quote == '\'')
{
/* quoted attribute value */
p++;
beg_attr_value = p;
while( *p && *p != quote ) { p++; }
if( *p ) {
while (*p && *p != quote) { p++; }
if (*p) {
*p = '\0'; /* null terminate attribute val */
p++;
}
@ -479,7 +479,7 @@ void dc_saxparser_parse(dc_saxparser_t* saxparser, const char* buf_start__)
*p = '\0';
char* temp = dc_strdup(beg_attr_value);
beg_attr_value_new = xml_decode(temp, ' ');
if( beg_attr_value_new!=temp ) { free(temp); }
if (beg_attr_value_new!=temp) { free(temp); }
*p = bak;
}
}
@ -489,11 +489,11 @@ void dc_saxparser_parse(dc_saxparser_t* saxparser, const char* buf_start__)
}
/* add attribute */
if( attr_index < MAX_ATTR )
if (attr_index < MAX_ATTR)
{
char* beg_attr_name_new = beg_attr_name;
int free_bits = (beg_attr_value_new != beg_attr_value)? FREE_VALUE : 0;
if( after_attr_name == p ) {
if (after_attr_name == p) {
/* take care not to overwrite the current pointer (happens eg. for `<tag attrWithoutValue>` */
bak = *after_attr_name;
*after_attr_name = '\0';
@ -514,7 +514,7 @@ void dc_saxparser_parse(dc_saxparser_t* saxparser, const char* buf_start__)
}
}
while( isspace(*p) ) { p++; } /* forward to attribute name beginning */
while (isspace(*p)) { p++; } /* forward to attribute name beginning */
}
char bak = *after_tag_name; /* backup the character as it may be `/` or `>` which gets important downwards */
@ -525,7 +525,7 @@ void dc_saxparser_parse(dc_saxparser_t* saxparser, const char* buf_start__)
/* self-closing tag */
p += strspn(p, XML_WS); /* skip whitespace before possible `/` */
if( *p == '/' )
if (*p == '/')
{
p++;
*after_tag_name = 0;
@ -536,7 +536,7 @@ void dc_saxparser_parse(dc_saxparser_t* saxparser, const char* buf_start__)
} /* end of processing start-tag */
p = strchr(p, '>');
if( p == NULL ) { goto cleanup; } /* unclosed start-tag or end-tag */
if (p == NULL) { goto cleanup; } /* unclosed start-tag or end-tag */
p++;
} /* end of processing start-tag or end-tag */

View file

@ -42,7 +42,7 @@ void dc_handle_degrade_event(dc_context_t* context, dc_apeerstate_t* peerstate)
uint32_t contact_id = 0;
uint32_t contact_chat_id = 0;
if( context == NULL || peerstate == NULL ) {
if (context == NULL || peerstate == NULL) {
goto cleanup;
}
@ -52,7 +52,7 @@ void dc_handle_degrade_event(dc_context_t* context, dc_apeerstate_t* 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( peerstate->degrade_event & DC_DE_FINGERPRINT_CHANGED )
if (peerstate->degrade_event & DC_DE_FINGERPRINT_CHANGED)
{
stmt = dc_sqlite3_prepare(context->sql, "SELECT id FROM contacts WHERE addr=?;");
sqlite3_bind_text(stmt, 1, peerstate->addr, -1, SQLITE_STATIC);
@ -60,7 +60,7 @@ void dc_handle_degrade_event(dc_context_t* context, dc_apeerstate_t* peerstate)
contact_id = sqlite3_column_int(stmt, 0);
sqlite3_finalize(stmt);
if( contact_id == 0 ) {
if (contact_id == 0) {
goto cleanup;
}
@ -84,22 +84,22 @@ cleanup:
static int encrypted_and_signed(dc_mimeparser_t* mimeparser, const char* expected_fingerprint)
{
if( !mimeparser->e2ee_helper->encrypted ) {
if (!mimeparser->e2ee_helper->encrypted) {
dc_log_warning(mimeparser->context, 0, "Message not encrypted.");
return 0;
}
if( dc_hash_count(mimeparser->e2ee_helper->signatures)<=0 ) {
if (dc_hash_count(mimeparser->e2ee_helper->signatures)<=0) {
dc_log_warning(mimeparser->context, 0, "Message not signed.");
return 0;
}
if( expected_fingerprint == NULL ) {
if (expected_fingerprint == NULL) {
dc_log_warning(mimeparser->context, 0, "Fingerprint for comparison missing.");
return 0;
}
if( dc_hash_find_str(mimeparser->e2ee_helper->signatures, expected_fingerprint) == NULL ) {
if (dc_hash_find_str(mimeparser->e2ee_helper->signatures, expected_fingerprint) == NULL) {
dc_log_warning(mimeparser->context, 0, "Message does not match expected fingerprint %s.", expected_fingerprint);
return 0;
}
@ -114,12 +114,12 @@ static char* get_self_fingerprint(dc_context_t* context)
dc_key_t* self_key = dc_key_new();
char* fingerprint = NULL;
if( (self_addr = dc_sqlite3_get_config(context->sql, "configured_addr", NULL)) == NULL
|| !dc_key_load_self_public(self_key, self_addr, context->sql) ) {
if ((self_addr = dc_sqlite3_get_config(context->sql, "configured_addr", NULL)) == NULL
|| !dc_key_load_self_public(self_key, self_addr, context->sql)) {
goto cleanup;
}
if( (fingerprint=dc_key_get_fingerprint(self_key)) == NULL ) {
if ((fingerprint=dc_key_get_fingerprint(self_key)) == NULL) {
goto cleanup;
}
@ -135,7 +135,7 @@ static uint32_t chat_id_2_contact_id(dc_context_t* context, uint32_t contact_cha
uint32_t contact_id = 0;
dc_array_t* contacts = dc_get_chat_contacts(context, contact_chat_id);
if( dc_array_get_cnt(contacts) != 1 ) {
if (dc_array_get_cnt(contacts) != 1) {
goto cleanup;
}
@ -155,18 +155,18 @@ static int fingerprint_equals_sender(dc_context_t* context, const char* fingerpr
dc_apeerstate_t* peerstate = dc_apeerstate_new(context);
char* fingerprint_normalized = NULL;
if( dc_array_get_cnt(contacts) != 1 ) {
if (dc_array_get_cnt(contacts) != 1) {
goto cleanup;
}
if( !dc_contact_load_from_db(contact, context->sql, dc_array_get_id(contacts, 0))
|| !dc_apeerstate_load_by_addr(peerstate, context->sql, contact->addr) ) {
if (!dc_contact_load_from_db(contact, context->sql, dc_array_get_id(contacts, 0))
|| !dc_apeerstate_load_by_addr(peerstate, context->sql, contact->addr)) {
goto cleanup;
}
fingerprint_normalized = dc_normalize_fingerprint(fingerprint);
if( strcasecmp(fingerprint_normalized, peerstate->public_key_fingerprint) == 0 ) {
if (strcasecmp(fingerprint_normalized, peerstate->public_key_fingerprint) == 0) {
fingerprint_equal = 1;
}
@ -183,11 +183,11 @@ static int mark_peer_as_verified(dc_context_t* context, const char* fingerprint)
int success = 0;
dc_apeerstate_t* peerstate = dc_apeerstate_new(context);
if( !dc_apeerstate_load_by_fingerprint(peerstate, context->sql, fingerprint) ) {
if (!dc_apeerstate_load_by_fingerprint(peerstate, context->sql, fingerprint)) {
goto cleanup;
}
if( !dc_apeerstate_set_verified(peerstate, DC_PS_PUBLIC_KEY, fingerprint, DC_BIDIRECT_VERIFIED) ) {
if (!dc_apeerstate_set_verified(peerstate, DC_PS_PUBLIC_KEY, fingerprint, DC_BIDIRECT_VERIFIED)) {
goto cleanup;
}
@ -210,8 +210,8 @@ static const char* lookup_field(dc_mimeparser_t* mimeparser, const char* key)
{
const char* value = NULL;
struct mailimf_field* field = dc_mimeparser_lookup_field(mimeparser, key);
if( field == NULL || field->fld_type != MAILIMF_FIELD_OPTIONAL_FIELD
|| field->fld_data.fld_optional_field == NULL || (value=field->fld_data.fld_optional_field->fld_value) == NULL ) {
if (field == NULL || field->fld_type != MAILIMF_FIELD_OPTIONAL_FIELD
|| field->fld_data.fld_optional_field == NULL || (value=field->fld_data.fld_optional_field->fld_value) == NULL) {
return NULL;
}
return value;
@ -228,19 +228,19 @@ static void send_handshake_msg(dc_context_t* context, uint32_t contact_chat_id,
dc_param_set_int(msg->param, DC_PARAM_CMD, DC_CMD_SECUREJOIN_MESSAGE);
dc_param_set (msg->param, DC_PARAM_CMD_ARG, step);
if( param2 ) {
if (param2) {
dc_param_set(msg->param, DC_PARAM_CMD_ARG2, param2); // depening on step, this goes either to Secure-Join-Invitenumber or Secure-Join-Auth in mrmimefactory.c
}
if( fingerprint ) {
if (fingerprint) {
dc_param_set(msg->param, DC_PARAM_CMD_ARG3, fingerprint);
}
if( grpid ) {
if (grpid) {
dc_param_set(msg->param, DC_PARAM_CMD_ARG4, grpid);
}
if( strcmp(step, "vg-request")==0 || strcmp(step, "vc-request")==0 ) {
if (strcmp(step, "vg-request")==0 || strcmp(step, "vc-request")==0) {
dc_param_set_int(msg->param, DC_PARAM_FORCE_PLAINTEXT, DC_FP_ADD_AUTOCRYPT_HEADER); // the request message MUST NOT be encrypted - it may be that the key has changed and the message cannot be decrypted otherwise
}
else {
@ -331,7 +331,7 @@ char* dc_get_securejoin_qr(dc_context_t* context, uint32_t group_chat_id)
char* group_name = NULL;
char* group_name_urlencoded= NULL;
if( context == NULL || context->magic!=DC_CONTEXT_MAGIC ) {
if (context == NULL || context->magic!=DC_CONTEXT_MAGIC) {
goto cleanup;
}
@ -339,36 +339,36 @@ char* dc_get_securejoin_qr(dc_context_t* context, uint32_t group_chat_id)
// invitenumber will be used to allow starting the handshake, auth will be used to verify the fingerprint
invitenumber = dc_token_lookup(context, DC_TOKEN_INVITENUMBER, group_chat_id);
if( invitenumber == NULL ) {
if (invitenumber == NULL) {
invitenumber = dc_create_id();
dc_token_save(context, DC_TOKEN_INVITENUMBER, group_chat_id, invitenumber);
}
auth = dc_token_lookup(context, DC_TOKEN_AUTH, group_chat_id);
if( auth == NULL ) {
if (auth == NULL) {
auth = dc_create_id();
dc_token_save(context, DC_TOKEN_AUTH, group_chat_id, auth);
}
if( (self_addr = dc_sqlite3_get_config(context->sql, "configured_addr", NULL)) == NULL ) {
if ((self_addr = dc_sqlite3_get_config(context->sql, "configured_addr", NULL)) == NULL) {
dc_log_error(context, 0, "Not configured, cannot generate QR code.");
goto cleanup;
}
self_name = dc_sqlite3_get_config(context->sql, "displayname", "");
if( (fingerprint=get_self_fingerprint(context)) == NULL ) {
if ((fingerprint=get_self_fingerprint(context)) == NULL) {
goto cleanup;
}
self_addr_urlencoded = dc_urlencode(self_addr);
self_name_urlencoded = dc_urlencode(self_name);
if( group_chat_id )
if (group_chat_id)
{
// parameters used: a=g=x=i=s=
chat = dc_get_chat(context, group_chat_id);
if( chat == NULL || chat->type != DC_CHAT_TYPE_VERIFIED_GROUP ) {
if (chat == NULL || chat->type != DC_CHAT_TYPE_VERIFIED_GROUP) {
dc_log_error(context, 0, "Secure join is only available for verified groups.");
goto cleanup;
}
@ -436,24 +436,24 @@ uint32_t dc_join_securejoin(dc_context_t* context, const char* qr)
dc_ensure_secret_key_exists(context);
if( (ongoing_allocated=dc_alloc_ongoing(context)) == 0 ) {
if ((ongoing_allocated=dc_alloc_ongoing(context)) == 0) {
goto cleanup;
}
if( ((qr_scan=dc_check_qr(context, qr))==NULL)
|| (qr_scan->state!=DC_QR_ASK_VERIFYCONTACT && qr_scan->state!=DC_QR_ASK_VERIFYGROUP) ) {
if (((qr_scan=dc_check_qr(context, qr))==NULL)
|| (qr_scan->state!=DC_QR_ASK_VERIFYCONTACT && qr_scan->state!=DC_QR_ASK_VERIFYGROUP)) {
dc_log_error(context, 0, "Unknown QR code.");
goto cleanup;
}
if( (contact_chat_id=dc_create_chat_by_contact_id(context, qr_scan->id)) == 0 ) {
if ((contact_chat_id=dc_create_chat_by_contact_id(context, qr_scan->id)) == 0) {
dc_log_error(context, 0, "Unknown contact.");
goto cleanup;
}
CHECK_EXIT
if( context->cb(context, DC_EVENT_IS_OFFLINE, 0, 0)!=0 ) {
if (context->cb(context, DC_EVENT_IS_OFFLINE, 0, 0)!=0) {
dc_log_error(context, DC_ERROR_NO_NETWORK, NULL);
goto cleanup;
}
@ -467,7 +467,7 @@ uint32_t dc_join_securejoin(dc_context_t* context, const char* qr)
context->bobs_qr_scan = qr_scan;
UNLOCK_QR
if( fingerprint_equals_sender(context, qr_scan->fingerprint, contact_chat_id) ) {
if (fingerprint_equals_sender(context, qr_scan->fingerprint, contact_chat_id)) {
// the scanned fingerprint matches Alice's key, we can proceed to step 4b) directly and save two mails
dc_log_info(context, 0, "Taking protocol shortcut.");
context->bob_expects = DC_VC_CONTACT_CONFIRM;
@ -483,7 +483,7 @@ uint32_t dc_join_securejoin(dc_context_t* context, const char* qr)
qr_scan->invitenumber, NULL, NULL); // Bob -> Alice
}
while( 1 ) {
while (1) {
CHECK_EXIT
usleep(300*1000); // 0.3 seconds
@ -492,8 +492,8 @@ uint32_t dc_join_securejoin(dc_context_t* context, const char* qr)
cleanup:
context->bob_expects = 0;
if( context->bobs_status == DC_BOB_SUCCESS ) {
if( join_vg ) {
if (context->bobs_status == DC_BOB_SUCCESS) {
if (join_vg) {
ret_chat_id = dc_get_chat_id_by_grpid(context, qr_scan->text2, NULL, NULL);
}
else {
@ -507,7 +507,7 @@ cleanup:
dc_lot_unref(qr_scan);
if( ongoing_allocated ) { dc_free_ongoing(context); }
if (ongoing_allocated) { dc_free_ongoing(context); }
return ret_chat_id;
}
@ -537,24 +537,24 @@ int dc_handle_securejoin_handshake(dc_context_t* context, dc_mimeparser_t* mimep
int ret = 0;
dc_contact_t* contact = NULL;
if( context == NULL || mimeparser == NULL || contact_id <= DC_CONTACT_ID_LAST_SPECIAL ) {
if (context == NULL || mimeparser == NULL || contact_id <= DC_CONTACT_ID_LAST_SPECIAL) {
goto cleanup;
}
if( (step=lookup_field(mimeparser, "Secure-Join")) == NULL ) {
if ((step=lookup_field(mimeparser, "Secure-Join")) == NULL) {
goto cleanup;
}
dc_log_info(context, 0, ">>>>>>>>>>>>>>>>>>>>>>>>> secure-join message '%s' received", step);
join_vg = (strncmp(step, "vg-", 3)==0);
dc_create_or_lookup_nchat_by_contact_id(context, contact_id, DC_CHAT_NOT_BLOCKED, &contact_chat_id, &contact_chat_id_blocked);
if( contact_chat_id_blocked ) {
if (contact_chat_id_blocked) {
dc_unblock_chat(context, contact_chat_id);
}
ret = DC_HANDSHAKE_STOP_NORMAL_PROCESSING;
if( strcmp(step, "vg-request")==0 || strcmp(step, "vc-request")==0 )
if (strcmp(step, "vg-request")==0 || strcmp(step, "vc-request")==0)
{
/* =========================================================
==== Alice - the inviter side ====
@ -568,12 +568,12 @@ int dc_handle_securejoin_handshake(dc_context_t* context, dc_mimeparser_t* mimep
// verify that the `Secure-Join-Invitenumber:`-header matches invitenumber written to the QR code
const char* invitenumber = NULL;
if( (invitenumber=lookup_field(mimeparser, "Secure-Join-Invitenumber")) == NULL ) {
if ((invitenumber=lookup_field(mimeparser, "Secure-Join-Invitenumber")) == NULL) {
dc_log_warning(context, 0, "Secure-join denied (invitenumber missing)."); // do not raise an error, this might just be spam or come from an old request
goto cleanup;
}
if( dc_token_exists(context, DC_TOKEN_INVITENUMBER, invitenumber) == 0 ) {
if (dc_token_exists(context, DC_TOKEN_INVITENUMBER, invitenumber) == 0) {
dc_log_warning(context, 0, "Secure-join denied (bad invitenumber)."); // do not raise an error, this might just be spam or come from an old request
goto cleanup;
}
@ -585,7 +585,7 @@ int dc_handle_securejoin_handshake(dc_context_t* context, dc_mimeparser_t* mimep
send_handshake_msg(context, contact_chat_id, join_vg? "vg-auth-required" : "vc-auth-required",
NULL, NULL, NULL); // Alice -> Bob
}
else if( strcmp(step, "vg-auth-required")==0 || strcmp(step, "vc-auth-required")==0 )
else if (strcmp(step, "vg-auth-required")==0 || strcmp(step, "vc-auth-required")==0)
{
/* ==========================================================
==== Bob - the joiner's side =====
@ -596,24 +596,24 @@ int dc_handle_securejoin_handshake(dc_context_t* context, dc_mimeparser_t* mimep
LOCK_QR
if ( context->bobs_qr_scan==NULL
|| context->bob_expects!=DC_VC_AUTH_REQUIRED
|| (join_vg && context->bobs_qr_scan->state!=DC_QR_ASK_VERIFYGROUP) ) {
|| (join_vg && context->bobs_qr_scan->state!=DC_QR_ASK_VERIFYGROUP)) {
dc_log_warning(context, 0, "auth-required message out of sync.");
goto cleanup; // no error, just aborted somehow or a mail from another handshake
}
scanned_fingerprint_of_alice = dc_strdup(context->bobs_qr_scan->fingerprint);
auth = dc_strdup(context->bobs_qr_scan->auth);
if( join_vg ) {
if (join_vg) {
grpid = dc_strdup(context->bobs_qr_scan->text2);
}
UNLOCK_QR
if( !encrypted_and_signed(mimeparser, scanned_fingerprint_of_alice) ) {
if (!encrypted_and_signed(mimeparser, scanned_fingerprint_of_alice)) {
could_not_establish_secure_connection(context, contact_chat_id, mimeparser->e2ee_helper->encrypted? "No valid signature." : "Not encrypted.");
end_bobs_joining(context, DC_BOB_ERROR);
goto cleanup;
}
if( !fingerprint_equals_sender(context, scanned_fingerprint_of_alice, contact_chat_id) ) {
if (!fingerprint_equals_sender(context, scanned_fingerprint_of_alice, contact_chat_id)) {
// MitM?
could_not_establish_secure_connection(context, contact_chat_id, "Fingerprint mismatch on joiner-side.");
end_bobs_joining(context, DC_BOB_ERROR);
@ -630,7 +630,7 @@ int dc_handle_securejoin_handshake(dc_context_t* context, dc_mimeparser_t* mimep
send_handshake_msg(context, contact_chat_id, join_vg? "vg-request-with-auth" : "vc-request-with-auth",
auth, own_fingerprint, grpid); // Bob -> Alice
}
else if( strcmp(step, "vg-request-with-auth")==0 || strcmp(step, "vc-request-with-auth")==0 )
else if (strcmp(step, "vg-request-with-auth")==0 || strcmp(step, "vc-request-with-auth")==0)
{
/* ============================================================
==== Alice - the inviter side ====
@ -640,17 +640,17 @@ int dc_handle_securejoin_handshake(dc_context_t* context, dc_mimeparser_t* mimep
// verify that Secure-Join-Fingerprint:-header matches the fingerprint of Bob
const char* fingerprint = NULL;
if( (fingerprint=lookup_field(mimeparser, "Secure-Join-Fingerprint")) == NULL ) {
if ((fingerprint=lookup_field(mimeparser, "Secure-Join-Fingerprint")) == NULL) {
could_not_establish_secure_connection(context, contact_chat_id, "Fingerprint not provided.");
goto cleanup;
}
if( !encrypted_and_signed(mimeparser, fingerprint) ) {
if (!encrypted_and_signed(mimeparser, fingerprint)) {
could_not_establish_secure_connection(context, contact_chat_id, "Auth not encrypted.");
goto cleanup;
}
if( !fingerprint_equals_sender(context, fingerprint, contact_chat_id) ) {
if (!fingerprint_equals_sender(context, fingerprint, contact_chat_id)) {
// MitM?
could_not_establish_secure_connection(context, contact_chat_id, "Fingerprint mismatch on inviter-side.");
goto cleanup;
@ -660,17 +660,17 @@ int dc_handle_securejoin_handshake(dc_context_t* context, dc_mimeparser_t* mimep
// verify that the `Secure-Join-Auth:`-header matches the secret written to the QR code
const char* auth = NULL;
if( (auth=lookup_field(mimeparser, "Secure-Join-Auth")) == NULL ) {
if ((auth=lookup_field(mimeparser, "Secure-Join-Auth")) == NULL) {
could_not_establish_secure_connection(context, contact_chat_id, "Auth not provided.");
goto cleanup;
}
if( dc_token_exists(context, DC_TOKEN_AUTH, auth) == 0 ) {
if (dc_token_exists(context, DC_TOKEN_AUTH, auth) == 0) {
could_not_establish_secure_connection(context, contact_chat_id, "Auth invalid.");
goto cleanup;
}
if( !mark_peer_as_verified(context, fingerprint) ) {
if (!mark_peer_as_verified(context, fingerprint)) {
could_not_establish_secure_connection(context, contact_chat_id, "Fingerprint mismatch on inviter-side."); // should not happen, we've compared the fingerprint some lines above
goto cleanup;
}
@ -683,12 +683,12 @@ int dc_handle_securejoin_handshake(dc_context_t* context, dc_mimeparser_t* mimep
context->cb(context, DC_EVENT_CONTACTS_CHANGED, contact_id/*selected contact*/, 0);
context->cb(context, DC_EVENT_SECUREJOIN_INVITER_PROGRESS, contact_id, 600);
if( join_vg ) {
if (join_vg) {
// the vg-member-added message is special: this is a normal Chat-Group-Member-Added message with an additional Secure-Join header
grpid = dc_strdup(lookup_field(mimeparser, "Secure-Join-Group"));
int is_verified = 0;
uint32_t verified_chat_id = dc_get_chat_id_by_grpid(context, grpid, NULL, &is_verified);
if( verified_chat_id == 0 || !is_verified ) {
if (verified_chat_id == 0 || !is_verified) {
dc_log_error(context, 0, "Verified chat not found.");
goto cleanup;
}
@ -701,14 +701,14 @@ int dc_handle_securejoin_handshake(dc_context_t* context, dc_mimeparser_t* mimep
context->cb(context, DC_EVENT_SECUREJOIN_INVITER_PROGRESS, contact_id, 1000); // done contact joining
}
}
else if( strcmp(step, "vg-member-added")==0 || strcmp(step, "vc-contact-confirm")==0 )
else if (strcmp(step, "vg-member-added")==0 || strcmp(step, "vc-contact-confirm")==0)
{
/* ==========================================================
==== Bob - the joiner's side =====
==== Step 7 in "Setup verified contact" protocol =====
========================================================== */
if( join_vg ) {
if (join_vg) {
// vg-member-added is just part of a Chat-Group-Member-Added which should be kept in any way, eg. for multi-client
ret = DC_HANDSHAKE_CONTINUE_NORMAL_PROCESSING;
}
@ -726,13 +726,13 @@ int dc_handle_securejoin_handshake(dc_context_t* context, dc_mimeparser_t* mimep
scanned_fingerprint_of_alice = dc_strdup(context->bobs_qr_scan->fingerprint);
UNLOCK_QR
if( !encrypted_and_signed(mimeparser, scanned_fingerprint_of_alice) ) {
if (!encrypted_and_signed(mimeparser, scanned_fingerprint_of_alice)) {
could_not_establish_secure_connection(context, contact_chat_id, "Contact confirm message not encrypted.");
end_bobs_joining(context, DC_BOB_ERROR);
goto cleanup;
}
if( !mark_peer_as_verified(context, scanned_fingerprint_of_alice) ) {
if (!mark_peer_as_verified(context, scanned_fingerprint_of_alice)) {
could_not_establish_secure_connection(context, contact_chat_id, "Fingerprint mismatch on joiner-side."); // MitM? - key has changed since vc-auth-required message
goto cleanup;
}

View file

@ -38,8 +38,8 @@
static int is_empty_line(const char* buf)
{
const unsigned char* p1 = (const unsigned char*)buf; /* force unsigned - otherwise the `> ' '` comparison will fail */
while( *p1 ) {
if( *p1 > ' ' ) {
while (*p1) {
if (*p1 > ' ') {
return 0; /* at least one character found - buffer is not empty */
}
p1++;
@ -50,7 +50,7 @@ static int is_empty_line(const char* buf)
static int is_plain_quote(const char* buf)
{
if( buf[0] == '>' ) {
if (buf[0] == '>') {
return 1;
}
return 0;
@ -66,12 +66,12 @@ static int is_quoted_headline(const char* buf)
int buf_len = strlen(buf);
if( buf_len > 80 ) {
if (buf_len > 80) {
return 0; /* the buffer is too long to be a quoted headline (some mailprograms (eg. "Mail" from Stock Android)
forget to insert a line break between the answer and the quoted headline ...)) */
}
if( buf_len > 0 && buf[buf_len-1] == ':' ) {
if (buf_len > 0 && buf[buf_len-1] == ':') {
return 1; /* the buffer is a quoting headline in the meaning described above) */
}
@ -89,7 +89,7 @@ dc_simplify_t* dc_simplify_new()
{
dc_simplify_t* simplify = NULL;
if( (simplify=calloc(1, sizeof(dc_simplify_t)))==NULL ) {
if ((simplify=calloc(1, sizeof(dc_simplify_t)))==NULL) {
exit(31);
}
@ -99,7 +99,7 @@ dc_simplify_t* dc_simplify_new()
void dc_simplify_unref(dc_simplify_t* simplify)
{
if( simplify == NULL ) {
if (simplify == NULL) {
return;
}
@ -132,24 +132,24 @@ static char* dc_simplify_simplify_plain_text(dc_simplify_t* simplify, const char
If the line contains more characters, it is _not_ treated as the footer start mark (hi, Thorsten) */
{
int footer_mark = 0;
for( l = l_first; l <= l_last; l++ )
for (l = l_first; l <= l_last; l++)
{
/* hide standard footer, "-- " - we do not set is_cut_at_end if we find this mark */
line = (char*)carray_get(lines, l);
if( strcmp(line, "-- ")==0
|| strcmp(line, "-- ")==0 ) { /* quoted-printable may encode `-- ` to `-- =20` which is converted back to `-- ` ... */
if (strcmp(line, "-- ")==0
|| strcmp(line, "-- ")==0) { /* quoted-printable may encode `-- ` to `-- =20` which is converted back to `-- ` ... */
footer_mark = 1;
}
/* also hide some non-standard footers - they got is_cut_at_end set, however */
if( strcmp(line, "--")==0
if (strcmp(line, "--")==0
|| strcmp(line, "---")==0
|| strcmp(line, "----")==0 ) {
|| strcmp(line, "----")==0) {
footer_mark = 1;
simplify->is_cut_at_end = 1;
}
if( footer_mark ) {
if (footer_mark) {
l_last = l - 1; /* if l_last is -1, there are no lines */
break; /* done */
}
@ -157,13 +157,13 @@ static char* dc_simplify_simplify_plain_text(dc_simplify_t* simplify, const char
}
/* check for "forwarding header" */
if( (l_last-l_first+1) >= 3 ) {
if ((l_last-l_first+1) >= 3) {
char* line0 = (char*)carray_get(lines, l_first);
char* line1 = (char*)carray_get(lines, l_first+1);
char* line2 = (char*)carray_get(lines, l_first+2);
if( strcmp(line0, "---------- Forwarded message ----------")==0 /* do not chage this! sent exactly in this form in dc_chat.c! */
if (strcmp(line0, "---------- Forwarded message ----------")==0 /* do not chage this! sent exactly in this form in dc_chat.c! */
&& strncmp(line1, "From: ", 6)==0
&& line2[0] == 0 )
&& line2[0] == 0)
{
simplify->is_forwarded = 1; /* nothing is cutted, the forward state should displayed explicitly in the ui */
l_first += 3;
@ -172,14 +172,14 @@ static char* dc_simplify_simplify_plain_text(dc_simplify_t* simplify, const char
/* remove lines that typically introduce a full quote (eg. `----- Original message -----` - as we do not parse the text 100%, we may
also loose forwarded messages, however, the user has always the option to show the full mail text. */
for( l = l_first; l <= l_last; l++ )
for (l = l_first; l <= l_last; l++)
{
line = (char*)carray_get(lines, l);
if( strncmp(line, "-----", 5)==0
if (strncmp(line, "-----", 5)==0
|| strncmp(line, "_____", 5)==0
|| strncmp(line, "=====", 5)==0
|| strncmp(line, "*****", 5)==0
|| strncmp(line, "~~~~~", 5)==0 )
|| strncmp(line, "~~~~~", 5)==0)
{
l_last = l - 1; /* if l_last is -1, there are no lines */
simplify->is_cut_at_end = 1;
@ -191,30 +191,30 @@ static char* dc_simplify_simplify_plain_text(dc_simplify_t* simplify, const char
{
int l_lastQuotedLine = -1;
for( l = l_last; l >= l_first; l-- ) {
for (l = l_last; l >= l_first; l--) {
line = (char*)carray_get(lines, l);
if( is_plain_quote(line) ) {
if (is_plain_quote(line)) {
l_lastQuotedLine = l;
}
else if( !is_empty_line(line) ) {
else if (!is_empty_line(line)) {
break;
}
}
if( l_lastQuotedLine != -1 )
if (l_lastQuotedLine != -1)
{
l_last = l_lastQuotedLine-1; /* if l_last is -1, there are no lines */
simplify->is_cut_at_end = 1;
if( l_last > 0 ) {
if( is_empty_line((char*)carray_get(lines, l_last)) ) { /* allow one empty line between quote and quote headline (eg. mails from Jürgen) */
if (l_last > 0) {
if (is_empty_line((char*)carray_get(lines, l_last))) { /* allow one empty line between quote and quote headline (eg. mails from Jürgen) */
l_last--;
}
}
if( l_last > 0 ) {
if (l_last > 0) {
line = (char*)carray_get(lines, l_last);
if( is_quoted_headline(line) ) {
if (is_quoted_headline(line)) {
l_last--;
}
}
@ -226,13 +226,13 @@ static char* dc_simplify_simplify_plain_text(dc_simplify_t* simplify, const char
int l_lastQuotedLine = -1;
int hasQuotedHeadline = 0;
for( l = l_first; l <= l_last; l++ ) {
for (l = l_first; l <= l_last; l++) {
line = (char*)carray_get(lines, l);
if( is_plain_quote(line) ) {
if (is_plain_quote(line)) {
l_lastQuotedLine = l;
}
else if( !is_empty_line(line) ) {
if( is_quoted_headline(line) && !hasQuotedHeadline && l_lastQuotedLine == -1 ) {
else if (!is_empty_line(line)) {
if (is_quoted_headline(line) && !hasQuotedHeadline && l_lastQuotedLine == -1) {
hasQuotedHeadline = 1; /* continue, the line may be a headline */
}
else {
@ -241,7 +241,7 @@ static char* dc_simplify_simplify_plain_text(dc_simplify_t* simplify, const char
}
}
if( l_lastQuotedLine != -1 )
if (l_lastQuotedLine != -1)
{
l_first = l_lastQuotedLine + 1;
simplify->is_cut_at_begin = 1;
@ -252,27 +252,27 @@ static char* dc_simplify_simplify_plain_text(dc_simplify_t* simplify, const char
dc_strbuilder_t ret;
dc_strbuilder_init(&ret, strlen(buf_terminated));
if( simplify->is_cut_at_begin ) {
if (simplify->is_cut_at_begin) {
dc_strbuilder_cat(&ret, DC_EDITORIAL_ELLIPSE " ");
}
int pending_linebreaks = 0; /* we write empty lines only in case and non-empty line follows */
int content_lines_added = 0;
for( l = l_first; l <= l_last; l++ )
for (l = l_first; l <= l_last; l++)
{
line = (char*)carray_get(lines, l);
if( is_empty_line(line) )
if (is_empty_line(line))
{
pending_linebreaks++;
}
else
{
if( content_lines_added ) /* flush empty lines - except if we're at the start of the buffer */
if (content_lines_added) /* flush empty lines - except if we're at the start of the buffer */
{
if( pending_linebreaks > 2 ) { pending_linebreaks = 2; } /* ignore more than one empty line (however, regard normal line ends) */
while( pending_linebreaks ) {
if (pending_linebreaks > 2) { pending_linebreaks = 2; } /* ignore more than one empty line (however, regard normal line ends) */
while (pending_linebreaks) {
dc_strbuilder_cat(&ret, "\n");
pending_linebreaks--;
}
@ -284,8 +284,8 @@ static char* dc_simplify_simplify_plain_text(dc_simplify_t* simplify, const char
}
}
if( simplify->is_cut_at_end
&& (!simplify->is_cut_at_begin || content_lines_added) /* avoid two `[...]` without content */ ) {
if (simplify->is_cut_at_end
&& (!simplify->is_cut_at_begin || content_lines_added) /* avoid two `[...]` without content */) {
dc_strbuilder_cat(&ret, " " DC_EDITORIAL_ELLIPSE);
}
@ -305,7 +305,7 @@ char* dc_simplify_simplify(dc_simplify_t* simplify, const char* in_unterminated,
/* create a copy of the given buffer */
char* out = NULL, *temp = NULL;
if( simplify == NULL || in_unterminated == NULL || in_bytes <= 0 ) {
if (simplify == NULL || in_unterminated == NULL || in_bytes <= 0) {
return dc_strdup("");
}
@ -314,13 +314,13 @@ char* dc_simplify_simplify(dc_simplify_t* simplify, const char* in_unterminated,
simplify->is_cut_at_end = 0;
out = strndup((char*)in_unterminated, in_bytes); /* strndup() makes sure, the string is null-terminated */
if( out == NULL ) {
if (out == NULL) {
return dc_strdup("");
}
/* convert HTML to text, if needed */
if( is_html ) {
if( (temp = dc_dehtml(out)) != NULL ) { /* dc_dehtml() returns way too much lineends, however they're removed in the simplification below */
if (is_html) {
if ((temp = dc_dehtml(out)) != NULL) { /* dc_dehtml() returns way too much lineends, however they're removed in the simplification below */
free(out);
out = temp;
}
@ -328,7 +328,7 @@ char* dc_simplify_simplify(dc_simplify_t* simplify, const char* in_unterminated,
/* simplify the text in the buffer (characters to remove may be marked by `\r`) */
dc_remove_cr_chars(out); /* make comparisons easier, eg. for line `-- ` */
if( (temp = dc_simplify_simplify_plain_text(simplify, out)) != NULL ) {
if ((temp = dc_simplify_simplify_plain_text(simplify, out)) != NULL) {
free(out);
out = temp;
}

View file

@ -39,7 +39,7 @@
dc_smtp_t* dc_smtp_new(dc_context_t* context)
{
dc_smtp_t* smtp;
if( (smtp=calloc(1, sizeof(dc_smtp_t)))==NULL ) {
if ((smtp=calloc(1, sizeof(dc_smtp_t)))==NULL) {
exit(29);
}
@ -52,7 +52,7 @@ dc_smtp_t* dc_smtp_new(dc_context_t* context)
void dc_smtp_unref(dc_smtp_t* smtp)
{
if( smtp == NULL ) {
if (smtp == NULL) {
return;
}
dc_smtp_disconnect(smtp);
@ -96,22 +96,22 @@ int dc_smtp_connect(dc_smtp_t* smtp, const dc_loginparam_t* lp)
int success = 0;
int r, try_esmtp;
if( smtp == NULL || lp == NULL ) {
if (smtp == NULL || lp == NULL) {
return 0;
}
if( smtp->context->cb(smtp->context, DC_EVENT_IS_OFFLINE, 0, 0)!=0 ) {
if (smtp->context->cb(smtp->context, DC_EVENT_IS_OFFLINE, 0, 0)!=0) {
dc_log_error_if(&smtp->log_connect_errors, smtp->context, DC_ERROR_NO_NETWORK, NULL);
goto cleanup;
}
if( smtp->hEtpan ) {
if (smtp->hEtpan) {
dc_log_warning(smtp->context, 0, "SMTP already connected.");
success = 1; /* otherwise, the handle would get deleted */
goto cleanup;
}
if( lp->addr == NULL || lp->send_server == NULL || lp->send_port == 0 ) {
if (lp->addr == NULL || lp->send_server == NULL || lp->send_port == 0) {
dc_log_error_if(&smtp->log_connect_errors, smtp->context, 0, "SMTP bad parameters.");
goto cleanup;
}
@ -120,7 +120,7 @@ int dc_smtp_connect(dc_smtp_t* smtp, const dc_loginparam_t* lp)
smtp->from = dc_strdup(lp->addr);
smtp->hEtpan = mailsmtp_new(0, NULL);
if( smtp->hEtpan == NULL ) {
if (smtp->hEtpan == NULL) {
dc_log_error(smtp->context, 0, "SMTP-object creation failed.");
goto cleanup;
}
@ -131,16 +131,16 @@ int dc_smtp_connect(dc_smtp_t* smtp, const dc_loginparam_t* lp)
#endif
/* connect to SMTP server */
if( lp->server_flags&(DC_LP_SMTP_SOCKET_STARTTLS|DC_LP_SMTP_SOCKET_PLAIN) )
if (lp->server_flags&(DC_LP_SMTP_SOCKET_STARTTLS|DC_LP_SMTP_SOCKET_PLAIN))
{
if( (r=mailsmtp_socket_connect(smtp->hEtpan, lp->send_server, lp->send_port)) != MAILSMTP_NO_ERROR ) {
if ((r=mailsmtp_socket_connect(smtp->hEtpan, lp->send_server, lp->send_port)) != MAILSMTP_NO_ERROR) {
dc_log_error_if(&smtp->log_connect_errors, smtp->context, 0, "SMTP-Socket connection to %s:%i failed (%s)", lp->send_server, (int)lp->send_port, mailsmtp_strerror(r));
goto cleanup;
}
}
else
{
if( (r=mailsmtp_ssl_connect(smtp->hEtpan, lp->send_server, lp->send_port)) != MAILSMTP_NO_ERROR ) {
if ((r=mailsmtp_ssl_connect(smtp->hEtpan, lp->send_server, lp->send_port)) != MAILSMTP_NO_ERROR) {
dc_log_error_if(&smtp->log_connect_errors, smtp->context, 0, "SMPT-SSL connection to %s:%i failed (%s)", lp->send_server, (int)lp->send_port, mailsmtp_strerror(r));
goto cleanup;
}
@ -148,30 +148,30 @@ int dc_smtp_connect(dc_smtp_t* smtp, const dc_loginparam_t* lp)
try_esmtp = 1;
smtp->esmtp = 0;
if( try_esmtp && (r=mailesmtp_ehlo(smtp->hEtpan))==MAILSMTP_NO_ERROR ) {
if (try_esmtp && (r=mailesmtp_ehlo(smtp->hEtpan))==MAILSMTP_NO_ERROR) {
smtp->esmtp = 1;
}
else if( !try_esmtp || r==MAILSMTP_ERROR_NOT_IMPLEMENTED ) {
else if (!try_esmtp || r==MAILSMTP_ERROR_NOT_IMPLEMENTED) {
r = mailsmtp_helo(smtp->hEtpan);
}
if( r != MAILSMTP_NO_ERROR ) {
if (r != MAILSMTP_NO_ERROR) {
dc_log_error_if(&smtp->log_connect_errors, smtp->context, 0, "SMTP-helo failed (%s)", mailsmtp_strerror(r));
goto cleanup;
}
if( lp->server_flags&DC_LP_SMTP_SOCKET_STARTTLS )
if (lp->server_flags&DC_LP_SMTP_SOCKET_STARTTLS)
{
if( (r=mailsmtp_socket_starttls(smtp->hEtpan)) != MAILSMTP_NO_ERROR ) {
if ((r=mailsmtp_socket_starttls(smtp->hEtpan)) != MAILSMTP_NO_ERROR) {
dc_log_error_if(&smtp->log_connect_errors, smtp->context, 0, "SMTP-STARTTLS failed (%s)", mailsmtp_strerror(r));
goto cleanup;
}
smtp->esmtp = 0;
if( try_esmtp && (r=mailesmtp_ehlo(smtp->hEtpan))==MAILSMTP_NO_ERROR ) {
if (try_esmtp && (r=mailesmtp_ehlo(smtp->hEtpan))==MAILSMTP_NO_ERROR) {
smtp->esmtp = 1;
}
else if( !try_esmtp || r==MAILSMTP_ERROR_NOT_IMPLEMENTED ) {
else if (!try_esmtp || r==MAILSMTP_ERROR_NOT_IMPLEMENTED) {
r = mailsmtp_helo(smtp->hEtpan);
}
@ -181,7 +181,7 @@ int dc_smtp_connect(dc_smtp_t* smtp, const dc_loginparam_t* lp)
}
dc_log_info(smtp->context, 0, "SMTP-server %s:%i STARTTLS-connected.", lp->send_server, (int)lp->send_port);
}
else if( lp->server_flags&DC_LP_SMTP_SOCKET_PLAIN )
else if (lp->server_flags&DC_LP_SMTP_SOCKET_PLAIN)
{
dc_log_info(smtp->context, 0, "SMTP-server %s:%i connected.", lp->send_server, (int)lp->send_port);
}
@ -190,9 +190,9 @@ int dc_smtp_connect(dc_smtp_t* smtp, const dc_loginparam_t* lp)
dc_log_info(smtp->context, 0, "SMTP-server %s:%i SSL-connected.", lp->send_server, (int)lp->send_port);
}
if( lp->send_user )
if (lp->send_user)
{
if((r=mailsmtp_auth(smtp->hEtpan, lp->send_user, lp->send_pw))!=MAILSMTP_NO_ERROR ) {
if((r=mailsmtp_auth(smtp->hEtpan, lp->send_user, lp->send_pw))!=MAILSMTP_NO_ERROR) {
/*
* There are some Mailservers which do not correclty implement PLAIN auth (hMail)
* So here we try a workaround. See https://github.com/deltachat/deltachat-android/issues/67
@ -222,8 +222,8 @@ int dc_smtp_connect(dc_smtp_t* smtp, const dc_loginparam_t* lp)
success = 1;
cleanup:
if( !success ) {
if( smtp->hEtpan ) {
if (!success) {
if (smtp->hEtpan) {
mailsmtp_free(smtp->hEtpan);
smtp->hEtpan = NULL;
}
@ -235,11 +235,11 @@ cleanup:
void dc_smtp_disconnect(dc_smtp_t* smtp)
{
if( smtp == NULL ) {
if (smtp == NULL) {
return;
}
if( smtp->hEtpan ) {
if (smtp->hEtpan) {
//mailsmtp_quit(smtp->hEtpan); -- ?
mailsmtp_free(smtp->hEtpan);
smtp->hEtpan = NULL;
@ -257,22 +257,22 @@ int dc_smtp_send_msg(dc_smtp_t* smtp, const clist* recipients, const char* data_
int success = 0, r;
clistiter* iter;
if( smtp == NULL ) {
if (smtp == NULL) {
return 0;
}
if( recipients == NULL || clist_count(recipients)==0 || data_not_terminated == NULL || data_bytes == 0 ) {
if (recipients == NULL || clist_count(recipients)==0 || data_not_terminated == NULL || data_bytes == 0) {
return 1; /* "null message" send */
}
if( smtp->hEtpan==NULL ) {
if (smtp->hEtpan==NULL) {
goto cleanup;
}
/* set source */
if( (r=(smtp->esmtp?
if ((r=(smtp->esmtp?
mailesmtp_mail(smtp->hEtpan, smtp->from, 1, "etPanSMTPTest") :
mailsmtp_mail(smtp->hEtpan, smtp->from))) != MAILSMTP_NO_ERROR )
mailsmtp_mail(smtp->hEtpan, smtp->from))) != MAILSMTP_NO_ERROR)
{
// this error is very usual - we've simply lost the server connection and reconnect as soon as possible.
// so, we do not log the first time this happens
@ -284,9 +284,9 @@ int dc_smtp_send_msg(dc_smtp_t* smtp, const clist* recipients, const char* data_
smtp->log_usual_error = 0;
/* set recipients */
for( iter=clist_begin(recipients); iter!=NULL; iter=clist_next(iter)) {
for (iter=clist_begin(recipients); iter!=NULL; iter=clist_next(iter)) {
const char* rcpt = clist_content(iter);
if( (r = (smtp->esmtp?
if ((r = (smtp->esmtp?
mailesmtp_rcpt(smtp->hEtpan, rcpt, MAILSMTP_DSN_NOTIFY_FAILURE|MAILSMTP_DSN_NOTIFY_DELAY, NULL) :
mailsmtp_rcpt(smtp->hEtpan, rcpt))) != MAILSMTP_NO_ERROR) {
dc_log_error_if(&smtp->log_connect_errors, smtp->context, 0, "mailsmtp_rcpt: %s: %s", rcpt, mailsmtp_strerror(r));

View file

@ -59,7 +59,7 @@ void dc_sqlite3_log_error(dc_sqlite3_t* sql, const char* msg_format, ...)
va_list va;
va_start(va, msg_format);
msg = sqlite3_vmprintf(msg_format, va); if( msg == NULL ) { dc_log_error(sql->context, 0, "Bad log format string \"%s\".", msg_format); }
msg = sqlite3_vmprintf(msg_format, va); if (msg == NULL) { dc_log_error(sql->context, 0, "Bad log format string \"%s\".", msg_format); }
dc_log_error(sql->context, 0, "%s SQLite says: %s", msg, sql->cobj? sqlite3_errmsg(sql->cobj) : notSetUp);
sqlite3_free(msg);
va_end(va);
@ -70,14 +70,14 @@ sqlite3_stmt* dc_sqlite3_prepare(dc_sqlite3_t* sql, const char* querystr)
{
sqlite3_stmt* stmt = NULL;
if( sql == NULL || querystr == NULL || sql->cobj == NULL ) {
if (sql == NULL || querystr == NULL || sql->cobj == NULL) {
return NULL;
}
if( sqlite3_prepare_v2(sql->cobj,
if (sqlite3_prepare_v2(sql->cobj,
querystr, -1 /*read `querystr` up to the first null-byte*/,
&stmt,
NULL /*tail not interesting, we use only single statements*/) != SQLITE_OK )
NULL /*tail not interesting, we use only single statements*/) != SQLITE_OK)
{
dc_sqlite3_log_error(sql, "Query failed: %s", querystr);
return NULL;
@ -95,12 +95,12 @@ int dc_sqlite3_execute(dc_sqlite3_t* sql, const char* querystr)
int sqlState;
stmt = dc_sqlite3_prepare(sql, querystr);
if( stmt == NULL ) {
if (stmt == NULL) {
goto cleanup;
}
sqlState = sqlite3_step(stmt);
if( sqlState != SQLITE_DONE && sqlState != SQLITE_ROW ) {
if (sqlState != SQLITE_DONE && sqlState != SQLITE_ROW) {
dc_sqlite3_log_error(sql, "Cannot excecute \"%s\".", querystr);
goto cleanup;
}
@ -108,7 +108,7 @@ int dc_sqlite3_execute(dc_sqlite3_t* sql, const char* querystr)
success = 1;
cleanup:
if( stmt ) {
if (stmt) {
sqlite3_finalize(stmt);
}
return success;
@ -138,7 +138,7 @@ dc_sqlite3_t* dc_sqlite3_new(dc_context_t* context)
{
dc_sqlite3_t* sql = NULL;
if( (sql=calloc(1, sizeof(dc_sqlite3_t)))==NULL ) {
if ((sql=calloc(1, sizeof(dc_sqlite3_t)))==NULL) {
exit(24); /* cannot allocate little memory, unrecoverable error */
}
@ -150,11 +150,11 @@ dc_sqlite3_t* dc_sqlite3_new(dc_context_t* context)
void dc_sqlite3_unref(dc_sqlite3_t* sql)
{
if( sql == NULL ) {
if (sql == NULL) {
return;
}
if( sql->cobj ) {
if (sql->cobj) {
dc_sqlite3_close(sql);
}
@ -164,16 +164,16 @@ void dc_sqlite3_unref(dc_sqlite3_t* sql)
int dc_sqlite3_open(dc_sqlite3_t* sql, const char* dbfile, int flags)
{
if( sql == NULL || dbfile == NULL ) {
if (sql == NULL || dbfile == NULL) {
goto cleanup;
}
if( sqlite3_threadsafe() == 0 ) {
if (sqlite3_threadsafe() == 0) {
dc_log_error(sql->context, 0, "Sqlite3 compiled thread-unsafe; this is not supported.");
goto cleanup;
}
if( sql->cobj ) {
if (sql->cobj) {
dc_log_error(sql->context, 0, "Cannot open, database \"%s\" already opened.", dbfile);
goto cleanup;
}
@ -187,9 +187,9 @@ int dc_sqlite3_open(dc_sqlite3_t* sql, const char* dbfile, int flags)
// files, caching is not that important; we rely on the system defaults here
// (normally 2 MB cache, 1 KB page size on sqlite < 3.12.0, 4 KB for newer
// versions)
if( sqlite3_open_v2(dbfile, &sql->cobj,
if (sqlite3_open_v2(dbfile, &sql->cobj,
SQLITE_OPEN_FULLMUTEX | ((flags&DC_OPEN_READONLY)? SQLITE_OPEN_READONLY : (SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE)),
NULL) != SQLITE_OK ) {
NULL) != SQLITE_OK) {
dc_sqlite3_log_error(sql, "Cannot open database \"%s\".", dbfile); /* ususally, even for errors, the pointer is set up (if not, this is also checked by dc_sqlite3_log_error()) */
goto cleanup;
}
@ -201,12 +201,12 @@ int dc_sqlite3_open(dc_sqlite3_t* sql, const char* dbfile, int flags)
// (without a busy_timeout, sqlite3_step() would return SQLITE_BUSY at once)
sqlite3_busy_timeout(sql->cobj, 10*1000);
if( !(flags&DC_OPEN_READONLY) )
if (!(flags&DC_OPEN_READONLY))
{
int dbversion_before_update = 0;
/* Init tables to dbversion=0 */
if( !dc_sqlite3_table_exists(sql, "config") )
if (!dc_sqlite3_table_exists(sql, "config"))
{
dc_log_info(sql->context, 0, "First time init: creating tables in \"%s\".", dbfile);
@ -279,9 +279,9 @@ int dc_sqlite3_open(dc_sqlite3_t* sql, const char* dbfile, int flags)
" param TEXT DEFAULT '');");
dc_sqlite3_execute(sql, "CREATE INDEX jobs_index1 ON jobs (desired_timestamp);");
if( !dc_sqlite3_table_exists(sql, "config") || !dc_sqlite3_table_exists(sql, "contacts")
if (!dc_sqlite3_table_exists(sql, "config") || !dc_sqlite3_table_exists(sql, "contacts")
|| !dc_sqlite3_table_exists(sql, "chats") || !dc_sqlite3_table_exists(sql, "chats_contacts")
|| !dc_sqlite3_table_exists(sql, "msgs") || !dc_sqlite3_table_exists(sql, "jobs") )
|| !dc_sqlite3_table_exists(sql, "msgs") || !dc_sqlite3_table_exists(sql, "jobs"))
{
dc_sqlite3_log_error(sql, "Cannot create tables in new database \"%s\".", dbfile);
goto cleanup; /* cannot create the tables - maybe we cannot write? */
@ -300,7 +300,7 @@ int dc_sqlite3_open(dc_sqlite3_t* sql, const char* dbfile, int flags)
int recalc_fingerprints = 0;
#define NEW_DB_VERSION 1
if( dbversion < NEW_DB_VERSION )
if (dbversion < NEW_DB_VERSION)
{
dc_sqlite3_execute(sql, "CREATE TABLE leftgrps ("
" id INTEGER PRIMARY KEY,"
@ -313,7 +313,7 @@ int dc_sqlite3_open(dc_sqlite3_t* sql, const char* dbfile, int flags)
#undef NEW_DB_VERSION
#define NEW_DB_VERSION 2
if( dbversion < NEW_DB_VERSION )
if (dbversion < NEW_DB_VERSION)
{
dc_sqlite3_execute(sql, "ALTER TABLE contacts ADD COLUMN authname TEXT DEFAULT '';");
@ -323,7 +323,7 @@ int dc_sqlite3_open(dc_sqlite3_t* sql, const char* dbfile, int flags)
#undef NEW_DB_VERSION
#define NEW_DB_VERSION 7
if( dbversion < NEW_DB_VERSION )
if (dbversion < NEW_DB_VERSION)
{
dc_sqlite3_execute(sql, "CREATE TABLE keypairs ("
" id INTEGER PRIMARY KEY,"
@ -339,7 +339,7 @@ int dc_sqlite3_open(dc_sqlite3_t* sql, const char* dbfile, int flags)
#undef NEW_DB_VERSION
#define NEW_DB_VERSION 10
if( dbversion < NEW_DB_VERSION )
if (dbversion < NEW_DB_VERSION)
{
dc_sqlite3_execute(sql, "CREATE TABLE acpeerstates ("
" id INTEGER PRIMARY KEY,"
@ -356,7 +356,7 @@ int dc_sqlite3_open(dc_sqlite3_t* sql, const char* dbfile, int flags)
#undef NEW_DB_VERSION
#define NEW_DB_VERSION 12
if( dbversion < NEW_DB_VERSION )
if (dbversion < NEW_DB_VERSION)
{
dc_sqlite3_execute(sql, "CREATE TABLE msgs_mdns ("
" msg_id INTEGER, "
@ -369,7 +369,7 @@ int dc_sqlite3_open(dc_sqlite3_t* sql, const char* dbfile, int flags)
#undef NEW_DB_VERSION
#define NEW_DB_VERSION 17
if( dbversion < NEW_DB_VERSION )
if (dbversion < NEW_DB_VERSION)
{
dc_sqlite3_execute(sql, "ALTER TABLE chats ADD COLUMN archived INTEGER DEFAULT 0;");
dc_sqlite3_execute(sql, "CREATE INDEX chats_index2 ON chats (archived);");
@ -382,7 +382,7 @@ int dc_sqlite3_open(dc_sqlite3_t* sql, const char* dbfile, int flags)
#undef NEW_DB_VERSION
#define NEW_DB_VERSION 18
if( dbversion < NEW_DB_VERSION )
if (dbversion < NEW_DB_VERSION)
{
dc_sqlite3_execute(sql, "ALTER TABLE acpeerstates ADD COLUMN gossip_timestamp INTEGER DEFAULT 0;");
dc_sqlite3_execute(sql, "ALTER TABLE acpeerstates ADD COLUMN gossip_key;");
@ -393,7 +393,7 @@ int dc_sqlite3_open(dc_sqlite3_t* sql, const char* dbfile, int flags)
#undef NEW_DB_VERSION
#define NEW_DB_VERSION 27
if( dbversion < NEW_DB_VERSION )
if (dbversion < NEW_DB_VERSION)
{
dc_sqlite3_execute(sql, "DELETE FROM msgs WHERE chat_id=1 OR chat_id=2;"); /* chat.id=1 and chat.id=2 are the old deaddrops, the current ones are defined by chats.blocked=2 */
dc_sqlite3_execute(sql, "CREATE INDEX chats_contacts_index2 ON chats_contacts (contact_id);"); /* needed to find chat by contact list */
@ -406,7 +406,7 @@ int dc_sqlite3_open(dc_sqlite3_t* sql, const char* dbfile, int flags)
#undef NEW_DB_VERSION
#define NEW_DB_VERSION 34
if( dbversion < NEW_DB_VERSION )
if (dbversion < NEW_DB_VERSION)
{
dc_sqlite3_execute(sql, "ALTER TABLE msgs ADD COLUMN hidden INTEGER DEFAULT 0;");
dc_sqlite3_execute(sql, "ALTER TABLE msgs_mdns ADD COLUMN timestamp_sent INTEGER DEFAULT 0;");
@ -422,7 +422,7 @@ int dc_sqlite3_open(dc_sqlite3_t* sql, const char* dbfile, int flags)
#undef NEW_DB_VERSION
#define NEW_DB_VERSION 39
if( dbversion < NEW_DB_VERSION )
if (dbversion < NEW_DB_VERSION)
{
dc_sqlite3_execute(sql, "CREATE TABLE tokens ("
" id INTEGER PRIMARY KEY,"
@ -434,7 +434,7 @@ int dc_sqlite3_open(dc_sqlite3_t* sql, const char* dbfile, int flags)
dc_sqlite3_execute(sql, "ALTER TABLE acpeerstates ADD COLUMN verified_key_fingerprint TEXT DEFAULT '';"); /* do not add `COLLATE NOCASE` case-insensivity is not needed as we force uppercase on store - otoh case-sensivity may be neeed for other/upcoming fingerprint formats */
dc_sqlite3_execute(sql, "CREATE INDEX acpeerstates_index5 ON acpeerstates (verified_key_fingerprint);");
if( dbversion_before_update == 34 )
if (dbversion_before_update == 34)
{
// migrate database from the use of verified-flags to verified_key,
// _only_ version 34 (0.17.0) has the fields public_key_verified and gossip_key_verified
@ -449,7 +449,7 @@ int dc_sqlite3_open(dc_sqlite3_t* sql, const char* dbfile, int flags)
#undef NEW_DB_VERSION
#define NEW_DB_VERSION 40
if( dbversion < NEW_DB_VERSION )
if (dbversion < NEW_DB_VERSION)
{
dc_sqlite3_execute(sql, "ALTER TABLE jobs ADD COLUMN thread INTEGER DEFAULT 0;");
@ -459,13 +459,13 @@ int dc_sqlite3_open(dc_sqlite3_t* sql, const char* dbfile, int flags)
#undef NEW_DB_VERSION
// (2) updates that require high-level objects (the structure is complete now and all objects are usable)
if( recalc_fingerprints )
if (recalc_fingerprints)
{
sqlite3_stmt* stmt = dc_sqlite3_prepare(sql, "SELECT addr FROM acpeerstates;");
while( sqlite3_step(stmt) == SQLITE_ROW ) {
while (sqlite3_step(stmt) == SQLITE_ROW) {
dc_apeerstate_t* peerstate = dc_apeerstate_new(sql->context);
if( dc_apeerstate_load_by_addr(peerstate, sql, (const char*)sqlite3_column_text(stmt, 0))
&& dc_apeerstate_recalc_fingerprint(peerstate) ) {
if (dc_apeerstate_load_by_addr(peerstate, sql, (const char*)sqlite3_column_text(stmt, 0))
&& dc_apeerstate_recalc_fingerprint(peerstate)) {
dc_apeerstate_save_to_db(peerstate, sql, 0/*don't create*/);
}
dc_apeerstate_unref(peerstate);
@ -485,11 +485,11 @@ cleanup:
void dc_sqlite3_close(dc_sqlite3_t* sql)
{
if( sql == NULL ) {
if (sql == NULL) {
return;
}
if( sql->cobj )
if (sql->cobj)
{
sqlite3_close(sql->cobj);
sql->cobj = NULL;
@ -501,7 +501,7 @@ void dc_sqlite3_close(dc_sqlite3_t* sql)
int dc_sqlite3_is_open(const dc_sqlite3_t* sql)
{
if( sql == NULL || sql->cobj == NULL ) {
if (sql == NULL || sql->cobj == NULL) {
return 0;
}
return 1;
@ -515,17 +515,17 @@ int dc_sqlite3_table_exists(dc_sqlite3_t* sql, const char* name)
sqlite3_stmt* stmt = NULL;
int sqlState;
if( (querystr=sqlite3_mprintf("PRAGMA table_info(%s)", name)) == NULL ) { /* this statement cannot be used with binded variables */
if ((querystr=sqlite3_mprintf("PRAGMA table_info(%s)", name)) == NULL) { /* this statement cannot be used with binded variables */
dc_log_error(sql->context, 0, "dc_sqlite3_table_exists_(): Out of memory.");
goto cleanup;
}
if( (stmt=dc_sqlite3_prepare(sql, querystr)) == NULL ) {
if ((stmt=dc_sqlite3_prepare(sql, querystr)) == NULL) {
goto cleanup;
}
sqlState = sqlite3_step(stmt);
if( sqlState == SQLITE_ROW ) {
if (sqlState == SQLITE_ROW) {
ret = 1; /* the table exists. Other states are SQLITE_DONE or SQLITE_ERROR in both cases we return 0. */
}
@ -534,11 +534,11 @@ int dc_sqlite3_table_exists(dc_sqlite3_t* sql, const char* name)
/* error/cleanup */
cleanup:
if( stmt ) {
if (stmt) {
sqlite3_finalize(stmt);
}
if( querystr ) {
if (querystr) {
sqlite3_free(querystr);
}
@ -556,17 +556,17 @@ int dc_sqlite3_set_config(dc_sqlite3_t* sql, const char* key, const char* value)
int state;
sqlite3_stmt* stmt;
if( key == NULL ) {
if (key == NULL) {
dc_log_error(sql->context, 0, "dc_sqlite3_set_config(): Bad parameter.");
return 0;
}
if( !dc_sqlite3_is_open(sql) ) {
if (!dc_sqlite3_is_open(sql)) {
dc_log_error(sql->context, 0, "dc_sqlite3_set_config(): Database not ready.");
return 0;
}
if( value )
if (value)
{
/* insert/update key=value */
#define SELECT_v_FROM_config_k_STATEMENT "SELECT value FROM config WHERE keyname=?;"
@ -575,14 +575,14 @@ int dc_sqlite3_set_config(dc_sqlite3_t* sql, const char* key, const char* value)
state = sqlite3_step(stmt);
sqlite3_finalize(stmt);
if( state == SQLITE_DONE ) {
if (state == SQLITE_DONE) {
stmt = dc_sqlite3_prepare(sql, "INSERT INTO config (keyname, value) VALUES (?, ?);");
sqlite3_bind_text (stmt, 1, key, -1, SQLITE_STATIC);
sqlite3_bind_text (stmt, 2, value, -1, SQLITE_STATIC);
state = sqlite3_step(stmt);
sqlite3_finalize(stmt);
}
else if( state == SQLITE_ROW ) {
else if (state == SQLITE_ROW) {
stmt = dc_sqlite3_prepare(sql, "UPDATE config SET value=? WHERE keyname=?;");
sqlite3_bind_text (stmt, 1, value, -1, SQLITE_STATIC);
sqlite3_bind_text (stmt, 2, key, -1, SQLITE_STATIC);
@ -603,7 +603,7 @@ int dc_sqlite3_set_config(dc_sqlite3_t* sql, const char* key, const char* value)
sqlite3_finalize(stmt);
}
if( state != SQLITE_DONE ) {
if (state != SQLITE_DONE) {
dc_log_error(sql->context, 0, "dc_sqlite3_set_config(): Cannot change value.");
return 0;
}
@ -616,16 +616,16 @@ char* dc_sqlite3_get_config(dc_sqlite3_t* sql, const char* key, const char* def)
{
sqlite3_stmt* stmt;
if( !dc_sqlite3_is_open(sql) || key == NULL ) {
if (!dc_sqlite3_is_open(sql) || key == NULL) {
return dc_strdup_keep_null(def);
}
stmt = dc_sqlite3_prepare(sql, SELECT_v_FROM_config_k_STATEMENT);
sqlite3_bind_text(stmt, 1, key, -1, SQLITE_STATIC);
if( sqlite3_step(stmt) == SQLITE_ROW )
if (sqlite3_step(stmt) == SQLITE_ROW)
{
const unsigned char* ptr = sqlite3_column_text(stmt, 0); /* Do not pass the pointers returned from sqlite3_column_text(), etc. into sqlite3_free(). */
if( ptr )
if (ptr)
{
/* success, fall through below to free objects */
char* ret = dc_strdup((const char*)ptr);
@ -643,7 +643,7 @@ char* dc_sqlite3_get_config(dc_sqlite3_t* sql, const char* key, const char* def)
int32_t dc_sqlite3_get_config_int(dc_sqlite3_t* sql, const char* key, int32_t def)
{
char* str = dc_sqlite3_get_config(sql, key, NULL);
if( str == NULL ) {
if (str == NULL) {
return def;
}
int32_t ret = atol(str);
@ -655,7 +655,7 @@ int32_t dc_sqlite3_get_config_int(dc_sqlite3_t* sql, const char* key, int32_t de
int dc_sqlite3_set_config_int(dc_sqlite3_t* sql, const char* key, int32_t value)
{
char* value_str = dc_mprintf("%i", (int)value);
if( value_str == NULL ) {
if (value_str == NULL) {
return 0;
}
int ret = dc_sqlite3_set_config(sql, key, value_str);
@ -675,7 +675,7 @@ void dc_sqlite3_begin_transaction(dc_sqlite3_t* sql)
// all other calls to `BEGIN IMMEDIATE` will try over until sqlite3_busy_timeout() is reached.
// CAVE: This also implies that transactions MUST NOT be nested.
sqlite3_stmt* stmt = dc_sqlite3_prepare(sql, "BEGIN IMMEDIATE;");
if( sqlite3_step(stmt) != SQLITE_DONE ) {
if (sqlite3_step(stmt) != SQLITE_DONE) {
dc_sqlite3_log_error(sql, "Cannot begin transaction.");
}
sqlite3_finalize(stmt);
@ -685,7 +685,7 @@ void dc_sqlite3_begin_transaction(dc_sqlite3_t* sql)
void dc_sqlite3_rollback(dc_sqlite3_t* sql)
{
sqlite3_stmt* stmt = dc_sqlite3_prepare(sql, "ROLLBACK;");
if( sqlite3_step(stmt) != SQLITE_DONE ) {
if (sqlite3_step(stmt) != SQLITE_DONE) {
dc_sqlite3_log_error(sql, "Cannot rollback transaction.");
}
sqlite3_finalize(stmt);
@ -695,7 +695,7 @@ void dc_sqlite3_rollback(dc_sqlite3_t* sql)
void dc_sqlite3_commit(dc_sqlite3_t* sql)
{
sqlite3_stmt* stmt = dc_sqlite3_prepare(sql, "COMMIT;");
if( sqlite3_step(stmt) != SQLITE_DONE ) {
if (sqlite3_step(stmt) != SQLITE_DONE) {
dc_sqlite3_log_error(sql, "Cannot commit transaction.");
}
sqlite3_finalize(stmt);

View file

@ -35,7 +35,7 @@ errors from here. */
static char* default_string(int id, int qty)
{
switch( id ) {
switch (id) {
case DC_STR_NOMESSAGES: return dc_strdup("No messages.");
case DC_STR_SELF: return dc_strdup("Me");
case DC_STR_DRAFT: return dc_strdup("Draft");
@ -82,10 +82,10 @@ static char* default_string(int id, int qty)
char* dc_stock_str(dc_context_t* context, int id) /* get the string with the given ID, the result must be free()'d! */
{
char* ret = NULL;
if( context ) {
if (context) {
ret = (char*)context->cb(context, DC_EVENT_GET_STRING, id, 0);
}
if( ret == NULL ) {
if (ret == NULL) {
ret = default_string(id, 0);
}
return ret;
@ -121,10 +121,10 @@ char* dc_stock_str_repl_string2(dc_context_t* context, int id, const char* to_in
char* dc_stock_str_repl_pl(dc_context_t* context, int id, int cnt)
{
char* ret = NULL;
if( context ) {
if (context) {
ret = (char*)context->cb(context, DC_EVENT_GET_QUANTITY_STRING, id, cnt);
}
if( ret == NULL ) {
if (ret == NULL) {
ret = default_string(id, cnt);
}
return ret;

View file

@ -44,14 +44,14 @@
*/
void dc_strbuilder_init(dc_strbuilder_t* strbuilder, int init_bytes)
{
if( strbuilder==NULL ) {
if (strbuilder==NULL) {
return;
}
strbuilder->allocated = DC_MAX(init_bytes, 128); /* use a small default minimum, we may use _many_ of these objects at the same time */
strbuilder->buf = malloc(strbuilder->allocated);
if( strbuilder->buf==NULL ) {
if (strbuilder->buf==NULL) {
exit(38);
}
@ -78,20 +78,20 @@ void dc_strbuilder_init(dc_strbuilder_t* strbuilder, int init_bytes)
char* dc_strbuilder_cat(dc_strbuilder_t* strbuilder, const char* text)
{
// this function MUST NOT call logging functions as it is used to output the log
if( strbuilder==NULL || text==NULL ) {
if (strbuilder==NULL || text==NULL) {
return NULL;
}
int len = strlen(text);
if( len > strbuilder->free ) {
if (len > strbuilder->free) {
int add_bytes = DC_MAX(len, strbuilder->allocated);
int old_offset = (int)(strbuilder->eos - strbuilder->buf);
strbuilder->allocated = strbuilder->allocated + add_bytes;
strbuilder->buf = realloc(strbuilder->buf, strbuilder->allocated+add_bytes);
if( strbuilder->buf==NULL ) {
if (strbuilder->buf==NULL) {
exit(39);
}
@ -134,14 +134,14 @@ void dc_strbuilder_catf(dc_strbuilder_t* strbuilder, const char* format, ...)
char_cnt_without_zero = vsnprintf(testbuf, 0, format, argp);
va_end(argp);
if( char_cnt_without_zero < 0) {
if (char_cnt_without_zero < 0) {
va_end(argp_copy);
dc_strbuilder_cat(strbuilder, "ErrFmt");
return;
}
buf = malloc(char_cnt_without_zero+2 /* +1 would be enough, however, protect against off-by-one-errors */);
if( buf == NULL ) {
if (buf == NULL) {
va_end(argp_copy);
dc_strbuilder_cat(strbuilder, "ErrMem");
return;

View file

@ -64,12 +64,12 @@ char* dc_urlencode(const char *to_encode)
{
const char *pstr = to_encode;
if( to_encode == NULL ) {
if (to_encode == NULL) {
return dc_strdup("");
}
char *buf = malloc(strlen(to_encode) * 3 + 1), *pbuf = buf;
if( buf == NULL ) {
if (buf == NULL) {
exit(46);
}
@ -109,12 +109,12 @@ char* dc_urldecode(const char* to_decode)
{
const char *pstr = to_decode;
if( to_decode == NULL ) {
if (to_decode == NULL) {
return dc_strdup("");
}
char *buf = malloc(strlen(to_decode) + 1), *pbuf = buf;
if( buf == NULL ) {
if (buf == NULL) {
exit(50);
}
@ -157,9 +157,9 @@ static int to_be_quoted(const char * word, size_t size)
const char* cur = word;
size_t i;
for( i = 0; i < size; i++ )
for (i = 0; i < size; i++)
{
switch( *cur )
switch (*cur)
{
case ',':
case ':':
@ -183,7 +183,7 @@ static int to_be_quoted(const char * word, size_t size)
return 1;
default:
if( ((unsigned char)*cur) >= 128 ) {
if (((unsigned char)*cur) >= 128) {
return 1;
}
break;
@ -258,7 +258,7 @@ static int quote_word(const char* display_charset, MMAPString* mmapstr, const ch
#endif
do_quote_char = 0;
switch( *cur )
switch (*cur)
{
case ',':
case ':':
@ -364,7 +364,7 @@ char* dc_encode_header_words(const char* to_encode)
const char* cur = to_encode;
MMAPString* mmapstr = mmap_string_new("");
if( to_encode == NULL || mmapstr == NULL ) {
if (to_encode == NULL || mmapstr == NULL) {
goto cleanup;
}
@ -398,7 +398,7 @@ char* dc_encode_header_words(const char* to_encode)
if (quote_words)
{
if ( !quote_word(DEF_DISPLAY_CHARSET, mmapstr, begin, end - begin) ) {
if ( !quote_word(DEF_DISPLAY_CHARSET, mmapstr, begin, end - begin)) {
goto cleanup;
}
@ -433,7 +433,7 @@ char* dc_encode_header_words(const char* to_encode)
ret_str = strdup(mmapstr->str);
cleanup:
if( mmapstr ) {
if (mmapstr) {
mmap_string_free(mmapstr);
}
return ret_str;
@ -453,14 +453,14 @@ char* dc_decode_header_words(const char* in)
/* decode strings as. `=?UTF-8?Q?Bj=c3=b6rn_Petersen?=`)
if `in` is NULL, `out` is NULL as well; also returns NULL on errors */
if( in == NULL ) {
if (in == NULL) {
return NULL; /* no string given */
}
char* out = NULL;
size_t cur_token = 0;
int r = mailmime_encoded_phrase_parse(DEF_INCOMING_CHARSET, in, strlen(in), &cur_token, DEF_DISPLAY_CHARSET, &out);
if( r != MAILIMF_NO_ERROR || out == NULL ) {
if (r != MAILIMF_NO_ERROR || out == NULL) {
out = dc_strdup(in); /* error, make a copy of the original string (as we free it later) */
}
@ -641,7 +641,7 @@ char* dc_decode_modified_utf7(const char *to_decode, int change_spaces)
const char *src;
char *dst, *res;
if( to_decode == NULL ) {
if (to_decode == NULL) {
return dc_strdup("");
}
@ -753,10 +753,10 @@ char* dc_decode_modified_utf7(const char *to_decode, int change_spaces)
*/
int dc_needs_ext_header(const char* to_check)
{
if( to_check ) {
while( *to_check )
if (to_check) {
while (*to_check)
{
if( !isalnum(*to_check) && *to_check!='-' && *to_check!='_' && *to_check!='.' && *to_check!='~' ) {
if (!isalnum(*to_check) && *to_check!='-' && *to_check!='_' && *to_check!='.' && *to_check!='~') {
return 1;
}
to_check++;
@ -783,12 +783,12 @@ char* dc_encode_ext_header(const char* to_encode)
#define PREFIX "utf-8''"
const char *pstr = to_encode;
if( to_encode == NULL ) {
if (to_encode == NULL) {
return dc_strdup(PREFIX);
}
char *buf = malloc(strlen(PREFIX) + strlen(to_encode) * 3 + 1);
if( buf == NULL ) {
if (buf == NULL) {
exit(46);
}
@ -828,13 +828,13 @@ char* dc_decode_ext_header(const char* to_decode)
char *decoded = NULL, *charset = NULL;
const char *p2 = NULL;
if( to_decode == NULL ) {
if (to_decode == NULL) {
goto cleanup;
}
// get char set
if( (p2=strchr(to_decode, '\'')) == NULL
|| (p2 == to_decode) /*no empty charset allowed*/ ) {
if ((p2=strchr(to_decode, '\'')) == NULL
|| (p2 == to_decode) /*no empty charset allowed*/) {
goto cleanup;
}
@ -842,7 +842,7 @@ char* dc_decode_ext_header(const char* to_decode)
p2++;
// skip language
if( (p2=strchr(p2, '\'')) == NULL ) {
if ((p2=strchr(p2, '\'')) == NULL) {
goto cleanup;
}
@ -851,10 +851,10 @@ char* dc_decode_ext_header(const char* to_decode)
// decode text
decoded = dc_urldecode(p2);
if( charset!=NULL && strcmp(charset, "utf-8")!=0 && strcmp(charset, "UTF-8")!=0 ) {
if (charset!=NULL && strcmp(charset, "utf-8")!=0 && strcmp(charset, "UTF-8")!=0) {
char* converted = NULL;
int r = charconv("utf-8", charset, decoded, strlen(decoded), &converted);
if( r == MAIL_CHARCONV_NO_ERROR && converted != NULL ) {
if (r == MAIL_CHARCONV_NO_ERROR && converted != NULL) {
free(decoded);
decoded = converted;
}

View file

@ -28,7 +28,7 @@ void dc_token_save(dc_context_t* context, dc_tokennamespc_t namespc, uint32_t fo
{
sqlite3_stmt* stmt = NULL;
if( context == NULL || context->magic != DC_CONTEXT_MAGIC || token == NULL ) { // foreign_id may be 0
if (context == NULL || context->magic != DC_CONTEXT_MAGIC || token == NULL) { // foreign_id may be 0
goto cleanup;
}
@ -50,7 +50,7 @@ char* dc_token_lookup(dc_context_t* context, dc_tokennamespc_t namespc, uint32_t
char* token = NULL;
sqlite3_stmt* stmt = NULL;
if( context == NULL || context->magic != DC_CONTEXT_MAGIC ) {
if (context == NULL || context->magic != DC_CONTEXT_MAGIC) {
goto cleanup;
}
@ -73,7 +73,7 @@ int dc_token_exists(dc_context_t* context, dc_tokennamespc_t namespc, const char
int exists = 0;
sqlite3_stmt* stmt = NULL;
if( context == NULL || context->magic != DC_CONTEXT_MAGIC || token == NULL ) {
if (context == NULL || context->magic != DC_CONTEXT_MAGIC || token == NULL) {
goto cleanup;
}

View file

@ -52,13 +52,13 @@ int dc_exactly_one_bit_set(int v)
char* dc_strdup(const char* s) /* strdup(NULL) is undefined, save_strdup(NULL) returns an empty string in this case */
{
char* ret;
if( s ) {
if( (ret=strdup(s)) == NULL ) {
if (s) {
if ((ret=strdup(s)) == NULL) {
exit(16); /* cannot allocate (little) memory, unrecoverable error */
}
}
else {
if( (ret=(char*)calloc(1, 1)) == NULL ) {
if ((ret=(char*)calloc(1, 1)) == NULL) {
exit(17); /* cannot allocate little memory, unrecoverable error */
}
}
@ -83,15 +83,15 @@ void dc_ltrim(char* buf)
size_t len;
const unsigned char* cur;
if( buf && *buf ) {
if (buf && *buf) {
len = strlen(buf);
cur = (const unsigned char*)buf;
while( *cur && isspace(*cur) ) {
while (*cur && isspace(*cur)) {
cur++; len--;
}
if( (const unsigned char*)buf != cur ) {
if ((const unsigned char*)buf != cur) {
memmove(buf, cur, len + 1);
}
}
@ -103,11 +103,11 @@ void dc_rtrim(char* buf)
size_t len;
unsigned char* cur;
if( buf && *buf ) {
if (buf && *buf) {
len = strlen(buf);
cur = (unsigned char*)buf + len - 1;
while( cur != (unsigned char*)buf && isspace(*cur) ) {
while (cur != (unsigned char*)buf && isspace(*cur)) {
--cur, --len;
}
@ -152,16 +152,16 @@ int dc_str_replace(char** haystack, const char* needle, const char* replacement)
{
int replacements = 0, start_search_pos = 0, needle_len, replacement_len;
if( haystack==NULL || *haystack==NULL || needle == NULL || needle[0]==0 ) {
if (haystack==NULL || *haystack==NULL || needle == NULL || needle[0]==0) {
return 0;
}
needle_len = strlen(needle);
replacement_len = replacement? strlen(replacement) : 0;
while( 1 )
while (1)
{
char* p2 = strstr((*haystack)+start_search_pos, needle);
if( p2==NULL ) { break; }
if (p2==NULL) { break; }
start_search_pos = (p2-(*haystack))+replacement_len; /* avoid recursion and skip the replaced part */
*p2 = 0;
@ -179,11 +179,11 @@ int dc_str_replace(char** haystack, const char* needle, const char* replacement)
int dc_str_contains(const char* haystack, const const char* needle)
{
/* case-insensitive search of needle in haystack, return 1 if found, 0 if not */
if( haystack==NULL || needle == NULL ) {
if (haystack==NULL || needle == NULL) {
return 0;
}
if( strstr(haystack, needle)!=NULL ) {
if (strstr(haystack, needle)!=NULL) {
return 1;
}
@ -213,11 +213,11 @@ int dc_str_contains(const char* haystack, const const char* needle)
char* dc_null_terminate(const char* in, int bytes) /* the result must be free()'d */
{
char* out = malloc(bytes+1);
if( out==NULL ) {
if (out==NULL) {
exit(45);
}
if( in && bytes > 0 ) {
if (in && bytes > 0) {
strncpy(out, in, bytes);
}
out[bytes] = 0;
@ -244,15 +244,15 @@ char* dc_binary_to_uc_hex(const uint8_t* buf, size_t bytes)
char* hex = NULL;
int i;
if( buf == NULL || bytes <= 0 ) {
if (buf == NULL || bytes <= 0) {
goto cleanup;
}
if( (hex=calloc(sizeof(char), bytes*2+1))==NULL ) {
if ((hex=calloc(sizeof(char), bytes*2+1))==NULL) {
goto cleanup;
}
for( i = 0; i < bytes; i++ ) {
for (i = 0; i < bytes; i++) {
snprintf(&hex[i*2], 3, "%02X", (int)buf[i]);
}
@ -274,13 +274,13 @@ char* dc_mprintf(const char* format, ...)
char_cnt_without_zero = vsnprintf(testbuf, 0, format, argp);
va_end(argp);
if( char_cnt_without_zero < 0) {
if (char_cnt_without_zero < 0) {
va_end(argp_copy);
return dc_strdup("ErrFmt");
}
buf = malloc(char_cnt_without_zero+2 /* +1 would be enough, however, protect against off-by-one-errors */);
if( buf == NULL ) {
if (buf == NULL) {
va_end(argp_copy);
return dc_strdup("ErrMem");
}
@ -297,7 +297,7 @@ char* dc_mprintf(const char* format, ...)
sqlite_str = sqlite3_vmprintf(format, argp);
va_end(argp);
if( sqlite_str == NULL ) {
if (sqlite_str == NULL) {
return dc_strdup("ErrFmt"); /* error - the result must be free()'d */
}
@ -320,16 +320,16 @@ char* dc_mprintf(const char* format, ...)
void dc_remove_cr_chars(char* buf)
{
const char* p1 = buf; /* search for first `\r` */
while( *p1 ) {
if( *p1 == '\r' ) {
while (*p1) {
if (*p1 == '\r') {
break;
}
p1++;
}
char* p2 = (char*)p1; /* p1 is `\r` or null-byte; start removing `\r` */
while( *p1 ) {
if( *p1 != '\r' ) {
while (*p1) {
if (*p1 != '\r') {
*p2 = *p1;
p2++;
}
@ -363,27 +363,27 @@ void dc_unify_lineends(char* buf)
void dc_replace_bad_utf8_chars(char* buf)
{
if( buf==NULL ) {
if (buf==NULL) {
return;
}
unsigned char* p1 = (unsigned char*)buf; /* force unsigned - otherwise the `> ' '` comparison will fail */
int p1len = strlen(buf);
int c, i, ix, n, j;
for( i=0, ix=p1len; i < ix; i++ )
for (i=0, ix=p1len; i < ix; i++)
{
c = p1[i];
if( c > 0 && c <= 0x7f ) { n=0; } /* 0bbbbbbb */
else if( (c & 0xE0) == 0xC0 ) { n=1; } /* 110bbbbb */
else if( c==0xed && i<(ix-1) && (p1[i+1] & 0xa0)==0xa0) { goto error; } /* U+d800 to U+dfff */
else if( (c & 0xF0) == 0xE0 ) { n=2; } /* 1110bbbb */
else if( (c & 0xF8) == 0xF0) { n=3; } /* 11110bbb */
//else if( (c & 0xFC) == 0xF8) { n=4; } /* 111110bb - not valid in https://tools.ietf.org/html/rfc3629 */
//else if( (c & 0xFE) == 0xFC) { n=5; } /* 1111110b - not valid in https://tools.ietf.org/html/rfc3629 */
if (c > 0 && c <= 0x7f) { n=0; } /* 0bbbbbbb */
else if ((c & 0xE0) == 0xC0) { n=1; } /* 110bbbbb */
else if (c==0xed && i<(ix-1) && (p1[i+1] & 0xa0)==0xa0) { goto error; } /* U+d800 to U+dfff */
else if ((c & 0xF0) == 0xE0) { n=2; } /* 1110bbbb */
else if ((c & 0xF8) == 0xF0) { n=3; } /* 11110bbb */
//else if ((c & 0xFC) == 0xF8) { n=4; } /* 111110bb - not valid in https://tools.ietf.org/html/rfc3629 */
//else if ((c & 0xFE) == 0xFC) { n=5; } /* 1111110b - not valid in https://tools.ietf.org/html/rfc3629 */
else { goto error; }
for( j = 0; j < n && i < ix; j++ ) { /* n bytes matching 10bbbbbb follow ? */
if( (++i == ix) || (( p1[i] & 0xC0) != 0x80) ) {
for (j = 0; j < n && i < ix; j++) { /* n bytes matching 10bbbbbb follow ? */
if ((++i == ix) || (( p1[i] & 0xC0) != 0x80)) {
goto error;
}
}
@ -395,8 +395,8 @@ void dc_replace_bad_utf8_chars(char* buf)
error:
/* there are errors in the string -> replace potential errors by the character `_`
(to avoid problems in filenames, we do not use eg. `?`) */
while( *p1 ) {
if( *p1 > 0x7f ) {
while (*p1) {
if (*p1 > 0x7f) {
*p1 = '_';
}
p1++;
@ -408,8 +408,8 @@ error:
static size_t dc_utf8_strlen(const char* s)
{
size_t i = 0, j = 0;
while( s[i] ) {
if( (s[i]&0xC0) != 0x80 )
while (s[i]) {
if ((s[i]&0xC0) != 0x80)
j++;
i++;
}
@ -421,8 +421,8 @@ static size_t dc_utf8_strlen(const char* s)
static size_t dc_utf8_strnlen(const char* s, size_t n)
{
size_t i = 0, j = 0;
while( i < n ) {
if( (s[i]&0xC0) != 0x80 )
while (i < n) {
if ((s[i]&0xC0) != 0x80)
j++;
i++;
}
@ -438,27 +438,27 @@ void dc_truncate_n_unwrap_str(char* buf, int approx_characters, int do_unwrap)
const char* ellipse_utf8 = do_unwrap? " ..." : " " DC_EDITORIAL_ELLIPSE; /* a single line is truncated `...` instead of `[...]` (the former is typically also used by the UI to fit strings in a rectangle) */
int lastIsCharacter = 0;
unsigned char* p1 = (unsigned char*)buf; /* force unsigned - otherwise the `> ' '` comparison will fail */
while( *p1 ) {
if( *p1 > ' ' ) {
while (*p1) {
if (*p1 > ' ') {
lastIsCharacter = 1;
}
else {
if( lastIsCharacter ) {
if (lastIsCharacter) {
size_t used_bytes = (size_t)((uintptr_t)p1 - (uintptr_t)buf);
if( dc_utf8_strnlen(buf, used_bytes) >= approx_characters ) {
if (dc_utf8_strnlen(buf, used_bytes) >= approx_characters) {
size_t buf_bytes = strlen(buf);
if( buf_bytes-used_bytes >= strlen(ellipse_utf8) /* check if we have room for the ellipse */ ) {
if (buf_bytes-used_bytes >= strlen(ellipse_utf8) /* check if we have room for the ellipse */) {
strcpy((char*)p1, ellipse_utf8);
}
break;
}
lastIsCharacter = 0;
if( do_unwrap ) {
if (do_unwrap) {
*p1 = ' ';
}
}
else {
if( do_unwrap ) {
if (do_unwrap) {
*p1 = '\r'; /* removed below */
}
}
@ -467,7 +467,7 @@ void dc_truncate_n_unwrap_str(char* buf, int approx_characters, int do_unwrap)
p1++;
}
if( do_unwrap ) {
if (do_unwrap) {
dc_remove_cr_chars(buf);
}
}
@ -475,13 +475,13 @@ void dc_truncate_n_unwrap_str(char* buf, int approx_characters, int do_unwrap)
void dc_truncate_str(char* buf, int approx_chars)
{
if( approx_chars > 0 && strlen(buf) > approx_chars+strlen(DC_EDITORIAL_ELLIPSE) )
if (approx_chars > 0 && strlen(buf) > approx_chars+strlen(DC_EDITORIAL_ELLIPSE))
{
char* p = &buf[approx_chars]; /* null-terminate string at the desired length */
*p = 0;
if( strchr(buf, ' ')!=NULL ) {
while( p[-1] != ' ' && p[-1] != '\n' ) { /* rewind to the previous space, avoid half utf-8 characters */
if (strchr(buf, ' ')!=NULL) {
while (p[-1] != ' ' && p[-1] != '\n') { /* rewind to the previous space, avoid half utf-8 characters */
p--;
*p = 0;
}
@ -500,8 +500,8 @@ carray* dc_split_into_lines(const char* buf_terminated)
const char* p1 = buf_terminated;
const char* line_start = p1;
unsigned int l_indx;
while( *p1 ) {
if( *p1 == '\n' ) {
while (*p1) {
if (*p1 == '\n') {
carray_add(lines, (void*)strndup(line_start, line_chars), &l_indx);
p1++;
line_start = p1;
@ -520,9 +520,9 @@ carray* dc_split_into_lines(const char* buf_terminated)
void dc_free_splitted_lines(carray* lines)
{
if( lines ) {
if (lines) {
int i, cnt = carray_count(lines);
for( i = 0; i < cnt; i++ ) {
for (i = 0; i < cnt; i++) {
free(carray_get(lines, i));
}
carray_free(lines);
@ -535,7 +535,7 @@ char* dc_insert_breaks(const char* in, int break_every, const char* break_chars)
/* insert a space every n characters, the return must be free()'d.
this is useful to allow lines being wrapped according to RFC 5322 (adds linebreaks before spaces) */
if( in == NULL || break_every <= 0 || break_chars == NULL ) {
if (in == NULL || break_every <= 0 || break_chars == NULL) {
return dc_strdup(in);
}
@ -544,14 +544,14 @@ char* dc_insert_breaks(const char* in, int break_every, const char* break_chars)
out_len += (out_len/break_every+1)*break_chars_len + 1/*nullbyte*/;
char* out = malloc(out_len);
if( out == NULL ) { return NULL; }
if (out == NULL) { return NULL; }
const char* i = in;
char* o = out;
while( *i ) {
while (*i) {
*o++ = *i++;
chars_added++;
if( chars_added==break_every && *i ) {
if (chars_added==break_every && *i) {
strcpy(o, break_chars);
o+=break_chars_len;
chars_added = 0;
@ -570,7 +570,7 @@ char* dc_insert_breaks(const char* in, int break_every, const char* break_chars)
void clist_free_content(const clist* haystack)
{
clistiter* iter;
for( iter=clist_begin(haystack); iter!=NULL; iter=clist_next(iter) ) {
for (iter=clist_begin(haystack); iter!=NULL; iter=clist_next(iter)) {
free(iter->data);
iter->data = NULL;
}
@ -580,8 +580,8 @@ void clist_free_content(const clist* haystack)
int clist_search_string_nocase(const clist* haystack, const char* needle)
{
clistiter* iter;
for( iter=clist_begin(haystack); iter!=NULL; iter=clist_next(iter) ) {
if( strcasecmp((const char*)iter->data, needle)==0 ) {
for (iter=clist_begin(haystack); iter!=NULL; iter=clist_next(iter)) {
if (strcasecmp((const char*)iter->data, needle)==0) {
return 1;
}
}
@ -632,7 +632,7 @@ static time_t mkgmtime(struct tm * tmp) /* from mailcore2 */
*/
if(bits > 40) bits = 40;
t = (t < 0) ? 0 : ((time_t) 1 << bits);
for ( ; ; ) {
for ( ; ;) {
gmtime_r(&t, &mytm);
dir = tmcomp(&mytm, &yourtm);
if (dir != 0) {
@ -770,9 +770,9 @@ time_t dc_create_smeared_timestamp(dc_context_t* context)
time_t now = time(NULL);
time_t ret = now;
SMEAR_LOCK
if( ret <= context->last_smeared_timestamp ) {
if (ret <= context->last_smeared_timestamp) {
ret = context->last_smeared_timestamp+1;
if( (ret-now) > DC_MAX_SECONDS_TO_LEND_FROM_FUTURE ) {
if ((ret-now) > DC_MAX_SECONDS_TO_LEND_FROM_FUTURE) {
ret = now + DC_MAX_SECONDS_TO_LEND_FROM_FUTURE;
}
}
@ -800,7 +800,7 @@ time_t dc_smeared_time(dc_context_t* context)
/* function returns a corrected time(NULL) */
time_t now = time(NULL);
SMEAR_LOCK
if( context->last_smeared_timestamp >= now ) {
if (context->last_smeared_timestamp >= now) {
now = context->last_smeared_timestamp+1;
}
SMEAR_UNLOCK
@ -819,19 +819,19 @@ static char* encode_66bits_as_base64(uint32_t v1, uint32_t v2, uint32_t fill /*o
we save 5 character in each id compared to 64 bit hex encoding, for a typical group ID, these are 10 characters (grpid+msgid):
hex: 64 bit, 4 bits/character, length = 64/4 = 16 characters
base64: 64 bit, 6 bits/character, length = 64/6 = 11 characters (plus 2 additional bits) */
char* ret = malloc(12); if( ret==NULL ) { exit(34); }
char* ret = malloc(12); if (ret==NULL) { exit(34); }
static const char chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
ret[ 0] = chars[ (v1>>26) & 0x3F ];
ret[ 1] = chars[ (v1>>20) & 0x3F ];
ret[ 2] = chars[ (v1>>14) & 0x3F ];
ret[ 3] = chars[ (v1>> 8) & 0x3F ];
ret[ 4] = chars[ (v1>> 2) & 0x3F ];
ret[ 5] = chars[ ( (v1<< 4) & 0x30 ) | ( (v2>>28) & 0x0F ) ];
ret[ 5] = chars[ ( (v1<< 4) & 0x30) | ( (v2>>28) & 0x0F) ];
ret[ 6] = chars[ (v2>>22) & 0x3F ];
ret[ 7] = chars[ (v2>>16) & 0x3F ];
ret[ 8] = chars[ (v2>>10) & 0x3F ];
ret[ 9] = chars[ (v2>> 4) & 0x3F ];
ret[10] = chars[ ( (v2<< 2) & 0x3C ) | (fill & 0x03) ];
ret[10] = chars[ ( (v2<< 2) & 0x3C) | (fill & 0x03) ];
ret[11] = 0;
return ret;
}
@ -850,7 +850,7 @@ char* dc_create_id(void)
- for INCOMING messages, the ID is taken from the Chat-Group-ID-header or from the Message-ID in the In-Reply-To: or References:-Header
- the group-id should be a string with the characters [a-zA-Z0-9\-_] */
uint32_t buf[3];
if( !RAND_bytes((unsigned char*)&buf, sizeof(uint32_t)*3) ) {
if (!RAND_bytes((unsigned char*)&buf, sizeof(uint32_t)*3)) {
RAND_pseudo_bytes((unsigned char*)&buf, sizeof(uint32_t)*3);
}
return encode_66bits_as_base64(buf[0], buf[1], buf[2]/*only the lower 2 bits are taken from this value*/);
@ -879,11 +879,11 @@ char* dc_create_outgoing_rfc724_mid(const char* grpid, const char* from_addr)
char* ret = NULL;
const char* at_hostname = strchr(from_addr, '@');
if( at_hostname == NULL ) {
if (at_hostname == NULL) {
at_hostname = "@nohost";
}
if( grpid ) {
if (grpid) {
ret = dc_mprintf("Gr.%s.%s%s", grpid, rand2, at_hostname);
/* ^^^ `Gr.` must never change as this is used to identify group messages in normal-clients-replies. The dot is choosen as this is normally not used for random ID creation. */
}
@ -906,16 +906,16 @@ char* dc_create_incoming_rfc724_mid(time_t message_timestamp, uint32_t contact_i
- when fetching the same message again, this function should generate the same Message-ID
*/
if( message_timestamp == DC_INVALID_TIMESTAMP || contact_ids_to == NULL || dc_array_get_cnt(contact_ids_to)==0 ) {
if (message_timestamp == DC_INVALID_TIMESTAMP || contact_ids_to == NULL || dc_array_get_cnt(contact_ids_to)==0) {
return NULL;
}
/* find out the largest receiver ID (we could also take the smallest, but it should be unique) */
size_t i, icnt = dc_array_get_cnt(contact_ids_to);
uint32_t largest_id_to = 0;
for( i = 0; i < icnt; i++ ) {
for (i = 0; i < icnt; i++) {
uint32_t cur_id = dc_array_get_id(contact_ids_to, i);
if( cur_id > largest_id_to ) {
if (cur_id > largest_id_to) {
largest_id_to = cur_id;
}
}
@ -933,28 +933,28 @@ char* dc_extract_grpid_from_rfc724_mid(const char* mid)
char* grpid = NULL, *p1;
int grpid_len;
if( mid == NULL || strlen(mid)<8 || mid[0]!='G' || mid[1]!='r' || mid[2]!='.' ) {
if (mid == NULL || strlen(mid)<8 || mid[0]!='G' || mid[1]!='r' || mid[2]!='.') {
goto cleanup;
}
grpid = dc_strdup(&mid[3]);
p1 = strchr(grpid, '.');
if( p1 == NULL ) {
if (p1 == NULL) {
goto cleanup;
}
*p1 = 0;
#define DC_ALSO_VALID_ID_LEN 16 /* length returned by create_adhoc_grp_id() */
grpid_len = strlen(grpid);
if( grpid_len!=DC_CREATE_ID_LEN && grpid_len!=DC_ALSO_VALID_ID_LEN ) { /* strict length comparison, the 'Gr.' magic is weak enough */
if (grpid_len!=DC_CREATE_ID_LEN && grpid_len!=DC_ALSO_VALID_ID_LEN) { /* strict length comparison, the 'Gr.' magic is weak enough */
goto cleanup;
}
success = 1;
cleanup:
if( success == 0 ) { free(grpid); grpid = NULL; }
if (success == 0) { free(grpid); grpid = NULL; }
return success? grpid : NULL;
}
@ -962,11 +962,11 @@ cleanup:
char* dc_extract_grpid_from_rfc724_mid_list(const clist* list)
{
clistiter* cur;
if( list ) {
for( cur = clist_begin(list); cur!=NULL ; cur=clist_next(cur) ) {
if (list) {
for (cur = clist_begin(list); cur!=NULL ; cur=clist_next(cur)) {
const char* mid = clist_content(cur);
char* grpid = dc_extract_grpid_from_rfc724_mid(mid);
if( grpid ) {
if (grpid) {
return grpid;
}
}
@ -984,7 +984,7 @@ char* dc_extract_grpid_from_rfc724_mid_list(const clist* list)
int dc_file_exist(const char* pathNfilename)
{
struct stat st;
if( stat(pathNfilename, &st) == 0 ) {
if (stat(pathNfilename, &st) == 0) {
return 1; /* the size, however, may be 0 */
}
else {
@ -996,7 +996,7 @@ int dc_file_exist(const char* pathNfilename)
uint64_t dc_get_filebytes(const char* pathNfilename)
{
struct stat st;
if( stat(pathNfilename, &st) == 0 ) {
if (stat(pathNfilename, &st) == 0) {
return (uint64_t)st.st_size;
}
else {
@ -1008,11 +1008,11 @@ uint64_t dc_get_filebytes(const char* pathNfilename)
char* dc_get_filename(const char* pathNfilename)
{
const char* p = strrchr(pathNfilename, '/');
if( p==NULL ) {
if (p==NULL) {
p = strrchr(pathNfilename, '\\');
}
if( p ) {
if (p) {
p++;
return dc_strdup(p);
}
@ -1024,11 +1024,11 @@ char* dc_get_filename(const char* pathNfilename)
int dc_delete_file(const char* pathNfilename, dc_context_t* log/*may be NULL*/)
{
if( pathNfilename==NULL ) {
if (pathNfilename==NULL) {
return 0;
}
if( remove(pathNfilename)!=0 ) {
if (remove(pathNfilename)!=0) {
dc_log_warning(log, 0, "Cannot delete \"%s\".", pathNfilename);
return 0;
}
@ -1045,32 +1045,32 @@ int dc_copy_file(const char* src, const char* dest, dc_context_t* log/*may be NU
size_t bytes_read;
int anything_copied = 0;
if( src==NULL || dest==NULL ) {
if (src==NULL || dest==NULL) {
return 0;
}
if( (fd_src=open(src, O_RDONLY)) < 0 ) {
if ((fd_src=open(src, O_RDONLY)) < 0) {
dc_log_error(log, 0, "Cannot open source file \"%s\".", src);
goto cleanup;
}
if( (fd_dest=open(dest, O_WRONLY|O_CREAT|O_EXCL, 0666)) < 0 ) {
if ((fd_dest=open(dest, O_WRONLY|O_CREAT|O_EXCL, 0666)) < 0) {
dc_log_error(log, 0, "Cannot open destination file \"%s\".", dest);
goto cleanup;
}
while( (bytes_read=read(fd_src, buf, DC_COPY_BUF_SIZE)) > 0 ) {
while ((bytes_read=read(fd_src, buf, DC_COPY_BUF_SIZE)) > 0) {
if (write(fd_dest, buf, bytes_read) != bytes_read) {
dc_log_error(log, 0, "Cannot write %i bytes to \"%s\".", bytes_read, dest);
}
anything_copied = 1;
}
if( !anything_copied ) {
if (!anything_copied) {
/* not a single byte copied -> check if the source is empty, too */
close(fd_src);
fd_src = -1;
if( dc_get_filebytes(src)!=0 ) {
if (dc_get_filebytes(src)!=0) {
dc_log_error(log, 0, "Different size information for \"%s\".", bytes_read, dest);
goto cleanup;
}
@ -1079,8 +1079,8 @@ int dc_copy_file(const char* src, const char* dest, dc_context_t* log/*may be NU
success = 1;
cleanup:
if( fd_src >= 0 ) { close(fd_src); }
if( fd_dest >= 0 ) { close(fd_dest); }
if (fd_src >= 0) { close(fd_src); }
if (fd_dest >= 0) { close(fd_dest); }
return success;
}
@ -1089,7 +1089,7 @@ int dc_create_folder(const char* pathNfilename, dc_context_t* log)
{
struct stat st;
if (stat(pathNfilename, &st) == -1) {
if( mkdir(pathNfilename, 0755) != 0 ) {
if (mkdir(pathNfilename, 0755) != 0) {
dc_log_warning(log, 0, "Cannot create directory \"%s\".", pathNfilename);
return 0;
}
@ -1100,9 +1100,9 @@ int dc_create_folder(const char* pathNfilename, dc_context_t* log)
char* dc_get_filesuffix_lc(const char* pathNfilename)
{
if( pathNfilename ) {
if (pathNfilename) {
const char* p = strrchr(pathNfilename, '.'); /* use the last point, we're interesting the "main" type */
if( p ) {
if (p) {
p++;
return dc_strlower(p); /* in contrast to dc_split_filename() we return the lowercase suffix */
}
@ -1120,7 +1120,7 @@ void dc_split_filename(const char* pathNfilename, char** ret_basename, char** re
- the case of the returned suffix is preserved; this is to allow reconstruction of (similar) names */
char* basename = dc_get_filename(pathNfilename), *suffix;
char* p1 = strrchr(basename, '.');
if( p1 ) {
if (p1) {
suffix = dc_strdup(p1);
*p1 = 0;
}
@ -1129,8 +1129,8 @@ void dc_split_filename(const char* pathNfilename, char** ret_basename, char** re
}
/* return the given values */
if( ret_basename ) { *ret_basename = basename; } else { free(basename); }
if( ret_all_suffixes_incl_dot ) { *ret_all_suffixes_incl_dot = suffix; } else { free(suffix); }
if (ret_basename ) { *ret_basename = basename; } else { free(basename); }
if (ret_all_suffixes_incl_dot) { *ret_all_suffixes_incl_dot = suffix; } else { free(suffix); }
}
@ -1139,8 +1139,8 @@ void dc_validate_filename(char* filename)
{
/* function modifies the given buffer and replaces all characters not valid in filenames by a "-" */
char* p1 = filename;
while( *p1 ) {
if( *p1=='/' || *p1=='\\' || *p1==':' ) {
while (*p1) {
if (*p1=='/' || *p1=='\\' || *p1==':') {
*p1 = '-';
}
p1++;
@ -1159,8 +1159,8 @@ char* dc_get_fine_pathNfilename(const char* folder, const char* desired_filename
dc_validate_filename(filenameNsuffix);
dc_split_filename(filenameNsuffix, &basename, &dotNSuffix);
for( i = 0; i < 1000 /*no deadlocks, please*/; i++ ) {
if( i ) {
for (i = 0; i < 1000 /*no deadlocks, please*/; i++) {
if (i) {
time_t idx = i<100? i : now+i;
ret = dc_mprintf("%s/%s-%lu%s", folder, basename, (unsigned long)idx, dotNSuffix);
}
@ -1187,8 +1187,8 @@ int dc_write_file(const char* pathNfilename, const void* buf, size_t buf_bytes,
int success = 0;
FILE* f = fopen(pathNfilename, "wb");
if( f ) {
if( fwrite(buf, 1, buf_bytes, f) == buf_bytes ) {
if (f) {
if (fwrite(buf, 1, buf_bytes, f) == buf_bytes) {
success = 1;
}
else {
@ -1208,34 +1208,34 @@ int dc_read_file(const char* pathNfilename, void** buf, size_t* buf_bytes, dc_co
{
int success = 0;
if( pathNfilename==NULL || buf==NULL || buf_bytes==NULL ) {
if (pathNfilename==NULL || buf==NULL || buf_bytes==NULL) {
return 0; /* do not go to cleanup as this would dereference "buf" and "buf_bytes" */
}
*buf = NULL;
*buf_bytes = 0;
FILE* f = fopen(pathNfilename, "rb");
if( f==NULL ) { goto cleanup; }
if (f==NULL) { goto cleanup; }
fseek(f, 0, SEEK_END);
*buf_bytes = ftell(f);
fseek(f, 0, SEEK_SET);
if( *buf_bytes <= 0 ) { goto cleanup; }
if (*buf_bytes <= 0) { goto cleanup; }
*buf = malloc( (*buf_bytes) + 1 /*be pragmatic and terminate all files by a null - fine for texts and does not hurt for the rest */ );
if( *buf==NULL ) { goto cleanup; }
*buf = malloc( (*buf_bytes) + 1 /*be pragmatic and terminate all files by a null - fine for texts and does not hurt for the rest */);
if (*buf==NULL) { goto cleanup; }
((char*)*buf)[*buf_bytes /*we allocated one extra byte above*/] = 0;
if( fread(*buf, 1, *buf_bytes, f)!=*buf_bytes ) { goto cleanup; }
if (fread(*buf, 1, *buf_bytes, f)!=*buf_bytes) { goto cleanup; }
success = 1;
cleanup:
if( f ) {
if (f) {
fclose(f);
}
if( success==0 ) {
if (success==0) {
free(*buf);
*buf = NULL;
*buf_bytes = 0;
@ -1259,10 +1259,10 @@ int dc_get_filemeta(const void* buf_start, size_t buf_bytes, uint32_t* ret_width
}
/* For JPEGs, we need to check the first bytes of each DCT chunk. */
if( buf[0]==0xFF && buf[1]==0xD8 && buf[2]==0xFF )
if (buf[0]==0xFF && buf[1]==0xD8 && buf[2]==0xFF)
{
long pos = 2;
while( buf[pos]==0xFF )
while (buf[pos]==0xFF)
{
if (buf[pos+1]==0xC0 || buf[pos+1]==0xC1 || buf[pos+1]==0xC2 || buf[pos+1]==0xC3 || buf[pos+1]==0xC9 || buf[pos+1]==0xCA || buf[pos+1]==0xCB) {
*ret_height = (buf[pos+5]<<8) + buf[pos+6]; /* sic! height is first */
@ -1275,7 +1275,7 @@ int dc_get_filemeta(const void* buf_start, size_t buf_bytes, uint32_t* ret_width
}
/* GIF: first three bytes say "GIF", next three give version number. Then dimensions */
if( buf[0]=='G' && buf[1]=='I' && buf[2]=='F' )
if (buf[0]=='G' && buf[1]=='I' && buf[2]=='F')
{
*ret_width = buf[6] + (buf[7]<<8);
*ret_height = buf[8] + (buf[9]<<8);
@ -1283,8 +1283,8 @@ int dc_get_filemeta(const void* buf_start, size_t buf_bytes, uint32_t* ret_width
}
/* PNG: the first frame is by definition an IHDR frame, which gives dimensions */
if( buf[0]==0x89 && buf[1]=='P' && buf[2]=='N' && buf[3]=='G' && buf[4]==0x0D && buf[5]==0x0A && buf[6]==0x1A && buf[7]==0x0A
&& buf[12]=='I' && buf[13]=='H' && buf[14]=='D' && buf[15]=='R' )
if (buf[0]==0x89 && buf[1]=='P' && buf[2]=='N' && buf[3]=='G' && buf[4]==0x0D && buf[5]==0x0A && buf[6]==0x1A && buf[7]==0x0A
&& buf[12]=='I' && buf[13]=='H' && buf[14]=='D' && buf[15]=='R')
{
*ret_width = (buf[16]<<24) + (buf[17]<<16) + (buf[18]<<8) + (buf[19]<<0);
*ret_height = (buf[20]<<24) + (buf[21]<<16) + (buf[22]<<8) + (buf[23]<<0);

View file

@ -51,7 +51,7 @@ char* dc_uudecode_do(const char* text, char** ret_binary, size_t* ret_binary_byt
{
// CAVE: This function may be called in a loop until it returns NULL, so make sure not to create an invinitive look.
if( text == NULL || ret_binary == NULL || ret_binary_bytes == NULL || ret_filename == NULL ) {
if (text == NULL || ret_binary == NULL || ret_binary_bytes == NULL || ret_filename == NULL) {
goto cleanup; // bad parameters
}