diff --git a/cmdline/cmdline.c b/cmdline/cmdline.c index a6bd6692..b8ec5333 100644 --- a/cmdline/cmdline.c +++ b/cmdline/cmdline.c @@ -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 \" \"."); 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 : "", 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 "); } } - 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 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 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 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 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: 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 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 missing."); } } - else if( strcmp(cmd, "get")==0 ) + else if (strcmp(cmd, "get")==0) { - if( arg1 ) { + if (arg1) { char* val = dc_get_config(context, arg1, ""); - 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 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 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 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 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 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 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 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 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 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 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 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 [] 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 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 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 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 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; } diff --git a/cmdline/main.c b/cmdline/main.c index dd2e6bf1..926c4239 100644 --- a/cmdline/main.c +++ b/cmdline/main.c @@ -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 ` 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); } diff --git a/cmdline/stress.c b/cmdline/stress.c index 13317f7e..dcac4609 100644 --- a/cmdline/stress.c +++ b/cmdline/stress.c @@ -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 ); diff --git a/src/dc_aheader.c b/src/dc_aheader.c index 89acf43d..40b04904 100644 --- a/src/dc_aheader.c +++ b/src/dc_aheader.c @@ -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 */ diff --git a/src/dc_apeerstate.c b/src/dc_apeerstate.c index c1a7250e..cab45aab 100644 --- a/src/dc_apeerstate.c +++ b/src/dc_apeerstate.c @@ -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; } diff --git a/src/dc_array.c b/src/dc_array.c index 365c8b79..c9cbc2fc 100644 --- a/src/dc_array.c +++ b/src/dc_array.c @@ -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; imagic != 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(""); } diff --git a/src/dc_chat.c b/src/dc_chat.c index 0dab62ed..dfecd2ba 100644 --- a/src/dc_chat.c +++ b/src/dc_chat.c @@ -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; } diff --git a/src/dc_chatlist.c b/src/dc_chatlist.c index ebc90e0e..fb72622f 100644 --- a/src/dc_chatlist.c +++ b/src/dc_chatlist.c @@ -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); diff --git a/src/dc_configure.c b/src/dc_configure.c index e425980e..bc43a9ff 100644 --- a/src/dc_configure.c +++ b/src/dc_configure.c @@ -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 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 /*&¶m->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 /*&¶m->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; diff --git a/src/dc_contact.c b/src/dc_contact.c index f71ad9de..4d23a2e3 100644 --- a/src/dc_contact.c +++ b/src/dc_contact.c @@ -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; } diff --git a/src/dc_context.c b/src/dc_context.c index a8815b3b..108850ac 100644 --- a/src/dc_context.c +++ b/src/dc_context.c @@ -98,7 +98,7 @@ dc_context_t* dc_context_new(dc_callback_t cb, void* userdata, const char* os_na { dc_context_t* context = NULL; - if( (context=calloc(1, sizeof(dc_context_t)))==NULL ) { + if ((context=calloc(1, sizeof(dc_context_t)))==NULL) { exit(23); /* cannot allocate little memory, unrecoverable error */ } @@ -125,7 +125,7 @@ dc_context_t* dc_context_new(dc_callback_t cb, void* userdata, const char* os_na /* Random-seed. An additional seed with more random data is done just before key generation (the timespan between this call and the key generation time is typically random. Moreover, later, we add a hash of the first message data to the random-seed - (it would be okay to seed with even more sensible data, the seed values cannot be recovered from the PRNG output, see OpenSSL's RAND_seed() ) */ + (it would be okay to seed with even more sensible data, the seed values cannot be recovered from the PRNG output, see OpenSSL's RAND_seed()) */ uintptr_t seed[5]; seed[0] = (uintptr_t)time(NULL); /* time */ seed[1] = (uintptr_t)seed; /* stack */ @@ -148,13 +148,13 @@ dc_context_t* dc_context_new(dc_callback_t cb, void* userdata, const char* os_na */ void dc_context_unref(dc_context_t* context) { - if( context==NULL || context->magic != DC_CONTEXT_MAGIC ) { + if (context==NULL || context->magic != DC_CONTEXT_MAGIC) { return; } dc_pgp_exit(); - if( dc_is_open(context) ) { + if (dc_is_open(context)) { dc_close(context); } @@ -171,7 +171,7 @@ void dc_context_unref(dc_context_t* context) pthread_cond_destroy(&context->smtpidle_cond); pthread_mutex_destroy(&context->smtpidle_condmutex); - for( int i = 0; i < DC_LOG_RINGBUF_SIZE; i++ ) { + for (int i = 0; i < DC_LOG_RINGBUF_SIZE; i++) { free(context->log_ringbuf[i]); } @@ -190,7 +190,7 @@ void dc_context_unref(dc_context_t* context) */ void* dc_get_userdata(dc_context_t* context) { - if( context==NULL || context->magic != DC_CONTEXT_MAGIC ) { + if (context==NULL || context->magic != DC_CONTEXT_MAGIC) { return NULL; } return context->userdata; @@ -199,7 +199,7 @@ void* dc_get_userdata(dc_context_t* context) static void update_config_cache(dc_context_t* context, const char* key) { - if( key==NULL || strcmp(key, "e2ee_enabled")==0 ) { + if (key==NULL || strcmp(key, "e2ee_enabled")==0) { context->e2ee_enabled = dc_sqlite3_get_config_int(context->sql, "e2ee_enabled", DC_E2EE_DEFAULT_ENABLED); } } @@ -223,7 +223,7 @@ int dc_open(dc_context_t* context, const char* dbfile, const char* blobdir) { int success = 0; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC || dbfile == NULL ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC || dbfile == NULL) { goto cleanup; } @@ -231,7 +231,7 @@ int dc_open(dc_context_t* context, const char* dbfile, const char* blobdir) from which all configuration is read/written to. */ /* Create/open sqlite database */ - if( !dc_sqlite3_open(context->sql, dbfile, 0) ) { + if (!dc_sqlite3_open(context->sql, dbfile, 0)) { goto cleanup; } @@ -240,7 +240,7 @@ int dc_open(dc_context_t* context, const char* dbfile, const char* blobdir) /* set blob-directory (to avoid double slashed, the given directory should not end with an slash) */ - if( blobdir && blobdir[0] ) { + if (blobdir && blobdir[0]) { context->blobdir = dc_strdup(blobdir); } else { @@ -253,8 +253,8 @@ int dc_open(dc_context_t* context, const char* dbfile, const char* blobdir) success = 1; cleanup: - if( !success ) { - if( dc_sqlite3_is_open(context->sql) ) { + if (!success) { + if (dc_sqlite3_is_open(context->sql)) { dc_sqlite3_close(context->sql); } } @@ -272,14 +272,14 @@ cleanup: */ void dc_close(dc_context_t* context) { - if( context == NULL || context->magic != DC_CONTEXT_MAGIC ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC) { return; } dc_imap_disconnect(context->imap); dc_smtp_disconnect(context->smtp); - if( dc_sqlite3_is_open(context->sql) ) { + if (dc_sqlite3_is_open(context->sql)) { dc_sqlite3_close(context->sql); } @@ -300,7 +300,7 @@ void dc_close(dc_context_t* context) */ int dc_is_open(const dc_context_t* context) { - if( context == NULL || context->magic != DC_CONTEXT_MAGIC ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC) { return 0; /* error - database not opened */ } @@ -318,7 +318,7 @@ int dc_is_open(const dc_context_t* context) */ char* dc_get_blobdir(dc_context_t* context) { - if( context == NULL || context->magic != DC_CONTEXT_MAGIC ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC) { return dc_strdup(NULL); } return dc_strdup(context->blobdir); @@ -357,7 +357,7 @@ int dc_set_config(dc_context_t* context, const char* key, const char* value) { int ret; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC || key == NULL ) { /* "value" may be NULL */ + if (context == NULL || context->magic != DC_CONTEXT_MAGIC || key == NULL) { /* "value" may be NULL */ return 0; } @@ -380,7 +380,7 @@ int dc_set_config(dc_context_t* context, const char* key, const char* value) */ char* dc_get_config(dc_context_t* context, const char* key, const char* def) { - if( context == NULL || context->magic != DC_CONTEXT_MAGIC || key == NULL ) { /* "def" may be NULL */ + if (context == NULL || context->magic != DC_CONTEXT_MAGIC || key == NULL) { /* "def" may be NULL */ return dc_strdup_keep_null(def); } @@ -398,7 +398,7 @@ int dc_set_config_int(dc_context_t* context, const char* key, int32_t value) { int ret; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC || key == NULL ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC || key == NULL) { return 0; } @@ -416,7 +416,7 @@ int dc_set_config_int(dc_context_t* context, const char* key, int32_t value) */ int32_t dc_get_config_int(dc_context_t* context, const char* key, int32_t def) { - if( context == NULL || context->magic != DC_CONTEXT_MAGIC || key == NULL ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC || key == NULL) { return def; } @@ -455,7 +455,7 @@ char* dc_get_info(dc_context_t* context) dc_strbuilder_t ret; dc_strbuilder_init(&ret, 0); - if( context == NULL || context->magic != DC_CONTEXT_MAGIC ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC) { return dc_strdup("ErrBadPtr"); } @@ -491,7 +491,7 @@ char* dc_get_info(dc_context_t* context) pub_key_count = sqlite3_column_int(stmt, 0); sqlite3_finalize(stmt); - if( dc_key_load_self_public(self_public, l2->addr, context->sql) ) { + if (dc_key_load_self_public(self_public, l2->addr, context->sql)) { fingerprint_str = dc_key_get_formatted_fingerprint(self_public); } else { @@ -550,9 +550,9 @@ char* dc_get_info(dc_context_t* context) /* add log excerpt */ pthread_mutex_lock(&context->log_ringbuf_critical); /*take care not to log here! */ - for( int i = 0; i < DC_LOG_RINGBUF_SIZE; i++ ) { + for (int i = 0; i < DC_LOG_RINGBUF_SIZE; i++) { int j = (context->log_ringbuf_pos+i) % DC_LOG_RINGBUF_SIZE; - if( context->log_ringbuf[j] ) { + if (context->log_ringbuf[j]) { struct tm wanted_struct; memcpy(&wanted_struct, localtime(&context->log_ringbuf_times[j]), sizeof(struct tm)); temp = dc_mprintf("\n%02i:%02i:%02i ", (int)wanted_struct.tm_hour, (int)wanted_struct.tm_min, (int)wanted_struct.tm_sec); @@ -585,7 +585,7 @@ int dc_get_archived_count(dc_context_t* context) int ret = 0; sqlite3_stmt* stmt = dc_sqlite3_prepare(context->sql, "SELECT COUNT(*) FROM chats WHERE blocked=0 AND archived=1;"); - if( sqlite3_step(stmt) == SQLITE_ROW ) { + if (sqlite3_step(stmt) == SQLITE_ROW) { ret = sqlite3_column_int(stmt, 0); } sqlite3_finalize(stmt); @@ -619,18 +619,18 @@ dc_chatlist_t* dc_get_chatlist(dc_context_t* context, int listflags, const char* int success = 0; dc_chatlist_t* obj = dc_chatlist_new(context); - if( context == NULL || context->magic != DC_CONTEXT_MAGIC ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC) { goto cleanup; } - if( !dc_chatlist_load_from_db(obj, listflags, query_str, query_id) ) { + if (!dc_chatlist_load_from_db(obj, listflags, query_str, query_id)) { goto cleanup; } success = 1; cleanup: - if( success ) { + if (success) { return obj; } else { @@ -658,18 +658,18 @@ dc_chat_t* dc_get_chat(dc_context_t* context, uint32_t chat_id) int success = 0; dc_chat_t* obj = dc_chat_new(context); - if( context == NULL || context->magic != DC_CONTEXT_MAGIC ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC) { goto cleanup; } - if( !dc_chat_load_from_db(obj, chat_id) ) { + if (!dc_chat_load_from_db(obj, chat_id)) { goto cleanup; } success = 1; cleanup: - if( success ) { + if (success) { return obj; } else { @@ -696,7 +696,7 @@ void dc_marknoticed_chat(dc_context_t* context, uint32_t chat_id) "noticed" messages are not counted as being unread but are still waiting for being marked as "seen" using dc_markseen_msgs() */ sqlite3_stmt* stmt; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC) { return; } @@ -724,7 +724,7 @@ uint32_t dc_get_chat_id_by_contact_id(dc_context_t* context, uint32_t contact_id uint32_t chat_id = 0; int chat_id_blocked = 0; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC) { return 0; } @@ -742,14 +742,14 @@ uint32_t dc_get_chat_id_by_grpid(dc_context_t* context, const char* grpid, int* if(ret_blocked) { *ret_blocked = 0; } if(ret_verified) { *ret_verified = 0; } - if( context == NULL || grpid == NULL ) { + if (context == NULL || grpid == NULL) { goto cleanup; } stmt = dc_sqlite3_prepare(context->sql, "SELECT id, blocked, type FROM chats WHERE grpid=?;"); sqlite3_bind_text (stmt, 1, grpid, -1, SQLITE_STATIC); - if( sqlite3_step(stmt)==SQLITE_ROW ) { + if (sqlite3_step(stmt)==SQLITE_ROW) { chat_id = sqlite3_column_int(stmt, 0); if(ret_blocked) { *ret_blocked = sqlite3_column_int(stmt, 1); } if(ret_verified) { *ret_verified = (sqlite3_column_int(stmt, 2)==DC_CHAT_TYPE_VERIFIED_GROUP); } @@ -782,33 +782,33 @@ uint32_t dc_create_chat_by_contact_id(dc_context_t* context, uint32_t contact_id int chat_blocked = 0; int send_event = 0; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC) { return 0; } dc_lookup_real_nchat_by_contact_id(context, contact_id, &chat_id, &chat_blocked); - if( chat_id ) { - if( chat_blocked ) { + if (chat_id) { + if (chat_blocked) { dc_unblock_chat(context, chat_id); /* unblock chat (typically move it from the deaddrop to view) */ send_event = 1; } goto cleanup; /* success */ } - if( 0==dc_real_contact_exists(context, contact_id) && contact_id!=DC_CONTACT_ID_SELF ) { + if (0==dc_real_contact_exists(context, contact_id) && contact_id!=DC_CONTACT_ID_SELF) { dc_log_warning(context, 0, "Cannot create chat, contact %i does not exist.", (int)contact_id); goto cleanup; } dc_create_or_lookup_nchat_by_contact_id(context, contact_id, DC_CHAT_NOT_BLOCKED, &chat_id, NULL); - if( chat_id ) { + if (chat_id) { send_event = 1; } dc_scaleup_contact_origin(context, contact_id, DC_ORIGIN_CREATE_CHAT); cleanup: - if( send_event ) { + if (send_event) { context->cb(context, DC_EVENT_MSGS_CHANGED, 0, 0); } @@ -848,19 +848,19 @@ uint32_t dc_create_chat_by_msg_id(dc_context_t* context, uint32_t msg_id) dc_msg_t* msg = dc_msg_new(); dc_chat_t* chat = dc_chat_new(context); - if( context == NULL || context->magic != DC_CONTEXT_MAGIC ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC) { goto cleanup; } - if( !dc_msg_load_from_db(msg, context, msg_id) + if (!dc_msg_load_from_db(msg, context, msg_id) || !dc_chat_load_from_db(chat, msg->chat_id) - || chat->id <= DC_CHAT_ID_LAST_SPECIAL ) { + || chat->id <= DC_CHAT_ID_LAST_SPECIAL) { goto cleanup; } chat_id = chat->id; - if( chat->blocked ) { + if (chat->blocked) { dc_unblock_chat(context, chat->id); send_event = 1; } @@ -870,7 +870,7 @@ uint32_t dc_create_chat_by_msg_id(dc_context_t* context, uint32_t msg_id) cleanup: dc_msg_unref(msg); dc_chat_unref(chat); - if( send_event ) { + if (send_event) { context->cb(context, DC_EVENT_MSGS_CHANGED, 0, 0); } return chat_id; @@ -891,7 +891,7 @@ cleanup: */ dc_array_t* dc_get_chat_media(dc_context_t* context, uint32_t chat_id, int msg_type, int or_msg_type) { - if( context == NULL || context->magic != DC_CONTEXT_MAGIC ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC) { return NULL; } @@ -902,7 +902,7 @@ dc_array_t* dc_get_chat_media(dc_context_t* context, uint32_t chat_id, int msg_t sqlite3_bind_int(stmt, 1, chat_id); sqlite3_bind_int(stmt, 2, msg_type); sqlite3_bind_int(stmt, 3, or_msg_type>0? or_msg_type : msg_type); - while( sqlite3_step(stmt) == SQLITE_ROW ) { + while (sqlite3_step(stmt) == SQLITE_ROW) { dc_array_add_id(ret, sqlite3_column_int(stmt, 0)); } sqlite3_finalize(stmt); @@ -932,31 +932,31 @@ uint32_t dc_get_next_media(dc_context_t* context, uint32_t curr_msg_id, int dir) dc_array_t* list = NULL; int i, cnt; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC) { goto cleanup; } - if( !dc_msg_load_from_db(msg, context, curr_msg_id) ) { + if (!dc_msg_load_from_db(msg, context, curr_msg_id)) { goto cleanup; } - if( (list=dc_get_chat_media(context, msg->chat_id, msg->type, 0))==NULL ) { + if ((list=dc_get_chat_media(context, msg->chat_id, msg->type, 0))==NULL) { goto cleanup; } cnt = dc_array_get_cnt(list); - for( i = 0; i < cnt; i++ ) { - if( curr_msg_id == dc_array_get_id(list, i) ) + for (i = 0; i < cnt; i++) { + if (curr_msg_id == dc_array_get_id(list, i)) { - if( dir > 0 ) { + if (dir > 0) { /* get the next message from the current position */ - if( i+1 < cnt ) { + if (i+1 < cnt) { ret_msg_id = dc_array_get_id(list, i+1); } } - else if( dir < 0 ) { + else if (dir < 0) { /* get the previous message from the current position */ - if( i-1 >= 0 ) { + if (i-1 >= 0) { ret_msg_id = dc_array_get_id(list, i-1); } } @@ -997,11 +997,11 @@ dc_array_t* dc_get_chat_contacts(dc_context_t* context, uint32_t chat_id) dc_array_t* ret = dc_array_new(context, 100); sqlite3_stmt* stmt = NULL; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC) { goto cleanup; } - if( chat_id == DC_CHAT_ID_DEADDROP ) { + if (chat_id == DC_CHAT_ID_DEADDROP) { goto cleanup; /* we could also create a list for all contacts in the deaddrop by searching contacts belonging to chats with chats.blocked=2, however, currently this is not needed */ } @@ -1011,7 +1011,7 @@ dc_array_t* dc_get_chat_contacts(dc_context_t* context, uint32_t chat_id) " WHERE cc.chat_id=?" " ORDER BY c.id=1, LOWER(c.name||c.addr), c.id;"); sqlite3_bind_int(stmt, 1, chat_id); - while( sqlite3_step(stmt) == SQLITE_ROW ) { + while (sqlite3_step(stmt) == SQLITE_ROW) { dc_array_add_id(ret, sqlite3_column_int(stmt, 0)); } @@ -1035,7 +1035,7 @@ dc_array_t* dc_get_fresh_msgs(dc_context_t* context) dc_array_t* ret = dc_array_new(context, 128); sqlite3_stmt* stmt = NULL; - if( context==NULL || context->magic != DC_CONTEXT_MAGIC || ret == NULL ) { + if (context==NULL || context->magic != DC_CONTEXT_MAGIC || ret == NULL) { goto cleanup; } @@ -1050,7 +1050,7 @@ dc_array_t* dc_get_fresh_msgs(dc_context_t* context) " ORDER BY m.timestamp DESC,m.id DESC;"); /* the list starts with the newest messages*/ sqlite3_bind_int(stmt, 1, show_deaddrop? DC_CHAT_DEADDROP_BLOCKED : 0); - while( sqlite3_step(stmt) == SQLITE_ROW ) { + while (sqlite3_step(stmt) == SQLITE_ROW) { dc_array_add_id(ret, sqlite3_column_int(stmt, 0)); } @@ -1059,11 +1059,11 @@ dc_array_t* dc_get_fresh_msgs(dc_context_t* context) cleanup: sqlite3_finalize(stmt); - if( success ) { + if (success) { return ret; } else { - if( ret ) { + if (ret) { dc_array_unref(ret); } return NULL; @@ -1099,11 +1099,11 @@ dc_array_t* dc_get_chat_msgs(dc_context_t* context, uint32_t chat_id, uint32_t f long cnv_to_local = dc_gm2local_offset(); #define SECONDS_PER_DAY 86400 - if( context==NULL || context->magic != DC_CONTEXT_MAGIC || ret == NULL ) { + if (context==NULL || context->magic != DC_CONTEXT_MAGIC || ret == NULL) { goto cleanup; } - if( chat_id == DC_CHAT_ID_DEADDROP ) + if (chat_id == DC_CHAT_ID_DEADDROP) { stmt = dc_sqlite3_prepare(context->sql, "SELECT m.id, m.timestamp" @@ -1116,7 +1116,7 @@ dc_array_t* dc_get_chat_msgs(dc_context_t* context, uint32_t chat_id, uint32_t f " AND contacts.blocked=0" " ORDER BY m.timestamp,m.id;"); /* the list starts with the oldest message*/ } - else if( chat_id == DC_CHAT_ID_STARRED ) + else if (chat_id == DC_CHAT_ID_STARRED) { stmt = dc_sqlite3_prepare(context->sql, "SELECT m.id, m.timestamp" @@ -1140,20 +1140,20 @@ dc_array_t* dc_get_chat_msgs(dc_context_t* context, uint32_t chat_id, uint32_t f sqlite3_bind_int(stmt, 1, chat_id); } - while( sqlite3_step(stmt) == SQLITE_ROW ) + while (sqlite3_step(stmt) == SQLITE_ROW) { curr_id = sqlite3_column_int(stmt, 0); /* add user marker */ - if( curr_id == marker1before ) { + if (curr_id == marker1before) { dc_array_add_id(ret, DC_MSG_ID_MARKER1); } /* add daymarker, if needed */ - if( flags&DC_GCM_ADDDAYMARKER ) { + if (flags&DC_GCM_ADDDAYMARKER) { curr_local_timestamp = (time_t)sqlite3_column_int64(stmt, 1) + cnv_to_local; curr_day = curr_local_timestamp/SECONDS_PER_DAY; - if( curr_day != last_day ) { + if (curr_day != last_day) { dc_array_add_id(ret, DC_MSG_ID_DAYMARKER); last_day = curr_day; } @@ -1169,11 +1169,11 @@ cleanup: //dc_log_info(context, 0, "Message list for chat #%i created in %.3f ms.", chat_id, (double)(clock()-start)*1000.0/CLOCKS_PER_SEC); - if( success ) { + if (success) { return ret; } else { - if( ret ) { + if (ret) { dc_array_unref(ret); } return NULL; @@ -1207,13 +1207,13 @@ dc_array_t* dc_search_msgs(dc_context_t* context, uint32_t chat_id, const char* char* strLikeInText = NULL, *strLikeBeg=NULL, *real_query = NULL; sqlite3_stmt* stmt = NULL; - if( context==NULL || context->magic != DC_CONTEXT_MAGIC || ret == NULL || query == NULL ) { + if (context==NULL || context->magic != DC_CONTEXT_MAGIC || ret == NULL || query == NULL) { goto cleanup; } real_query = dc_strdup(query); dc_trim(real_query); - if( real_query[0]==0 ) { + if (real_query[0]==0) { success = 1; /*empty result*/ goto cleanup; } @@ -1222,12 +1222,12 @@ dc_array_t* dc_search_msgs(dc_context_t* context, uint32_t chat_id, const char* strLikeBeg = dc_mprintf("%s%%", real_query); /*for the name search, we use "Name%" which is fast as it can use the index ("%Name%" could not). */ /* Incremental search with "LIKE %query%" cannot take advantages from any index - ("query%" could for COLLATE NOCASE indexes, see http://www.sqlite.org/optoverview.html#like_opt ) + ("query%" could for COLLATE NOCASE indexes, see http://www.sqlite.org/optoverview.html#like_opt) An alternative may be the FULLTEXT sqlite stuff, however, this does not really help with incremental search. An extra table with all words and a COLLATE NOCASE indexes may help, however, this must be updated all the time and probably consumes more time than we can save in tenthousands of searches. For now, we just expect the following query to be fast enough :-) */ - if( chat_id ) { + if (chat_id) { stmt = dc_sqlite3_prepare(context->sql, "SELECT m.id, m.timestamp FROM msgs m" " LEFT JOIN contacts ct ON m.from_id=ct.id" @@ -1255,7 +1255,7 @@ dc_array_t* dc_search_msgs(dc_context_t* context, uint32_t chat_id, const char* sqlite3_bind_text(stmt, 3, strLikeBeg, -1, SQLITE_STATIC); } - while( sqlite3_step(stmt) == SQLITE_ROW ) { + while (sqlite3_step(stmt) == SQLITE_ROW) { dc_array_add_id(ret, sqlite3_column_int(stmt, 0)); } @@ -1269,11 +1269,11 @@ cleanup: //dc_log_info(context, 0, "Message list for search \"%s\" in chat #%i created in %.3f ms.", query, chat_id, (double)(clock()-start)*1000.0/CLOCKS_PER_SEC); - if( success ) { + if (success) { return ret; } else { - if( ret ) { + if (ret) { dc_array_unref(ret); } return NULL; @@ -1353,7 +1353,7 @@ uint32_t dc_get_last_deaddrop_fresh_msg(dc_context_t* context) " AND c.blocked=" DC_STRINGIFY(DC_CHAT_DEADDROP_BLOCKED) " ORDER BY m.timestamp DESC, m.id DESC;"); /* we have an index over the state-column, this should be sufficient as there are typically only few fresh messages */ - if( sqlite3_step(stmt) != SQLITE_ROW ) { + if (sqlite3_step(stmt) != SQLITE_ROW) { goto cleanup; } @@ -1370,13 +1370,13 @@ size_t dc_get_chat_cnt(dc_context_t* context) size_t ret = 0; sqlite3_stmt* stmt = NULL; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC || context->sql->cobj==NULL ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC || context->sql->cobj==NULL) { goto cleanup; /* no database, no chats - this is no error (needed eg. for information) */ } stmt = dc_sqlite3_prepare(context->sql, "SELECT COUNT(*) FROM chats WHERE id>" DC_STRINGIFY(DC_CHAT_ID_LAST_SPECIAL) " AND blocked=0;"); - if( sqlite3_step(stmt) != SQLITE_ROW ) { + if (sqlite3_step(stmt) != SQLITE_ROW) { goto cleanup; } @@ -1393,10 +1393,10 @@ void dc_lookup_real_nchat_by_contact_id(dc_context_t* context, uint32_t contact_ /* checks for "real" chats or self-chat */ sqlite3_stmt* stmt = NULL; - if( ret_chat_id ) { *ret_chat_id = 0; } - if( ret_chat_blocked ) { *ret_chat_blocked = 0; } + if (ret_chat_id) { *ret_chat_id = 0; } + if (ret_chat_blocked) { *ret_chat_blocked = 0; } - if( context == NULL || context->magic != DC_CONTEXT_MAGIC || context->sql->cobj==NULL ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC || context->sql->cobj==NULL) { return; /* no database, no chats - this is no error (needed eg. for information) */ } @@ -1406,9 +1406,9 @@ void dc_lookup_real_nchat_by_contact_id(dc_context_t* context, uint32_t contact_ " INNER JOIN chats_contacts j ON c.id=j.chat_id" " WHERE c.type=" DC_STRINGIFY(DC_CHAT_TYPE_SINGLE) " AND c.id>" DC_STRINGIFY(DC_CHAT_ID_LAST_SPECIAL) " AND j.contact_id=?;"); sqlite3_bind_int(stmt, 1, contact_id); - if( sqlite3_step(stmt) == SQLITE_ROW ) { - if( ret_chat_id ) { *ret_chat_id = sqlite3_column_int(stmt, 0); } - if( ret_chat_blocked ) { *ret_chat_blocked = sqlite3_column_int(stmt, 1); } + if (sqlite3_step(stmt) == SQLITE_ROW) { + if (ret_chat_id) { *ret_chat_id = sqlite3_column_int(stmt, 0); } + if (ret_chat_blocked) { *ret_chat_blocked = sqlite3_column_int(stmt, 1); } } sqlite3_finalize(stmt); } @@ -1423,27 +1423,27 @@ void dc_create_or_lookup_nchat_by_contact_id(dc_context_t* context, uint32_t con char* q = NULL; sqlite3_stmt* stmt = NULL; - if( ret_chat_id ) { *ret_chat_id = 0; } - if( ret_chat_blocked ) { *ret_chat_blocked = 0; } + if (ret_chat_id) { *ret_chat_id = 0; } + if (ret_chat_blocked) { *ret_chat_blocked = 0; } - if( context == NULL || context->magic != DC_CONTEXT_MAGIC || context->sql->cobj==NULL ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC || context->sql->cobj==NULL) { return; /* database not opened - error */ } - if( contact_id == 0 ) { + if (contact_id == 0) { return; } dc_lookup_real_nchat_by_contact_id(context, contact_id, &chat_id, &chat_blocked); - if( chat_id != 0 ) { - if( ret_chat_id ) { *ret_chat_id = chat_id; } - if( ret_chat_blocked ) { *ret_chat_blocked = chat_blocked; } + if (chat_id != 0) { + if (ret_chat_id) { *ret_chat_id = chat_id; } + if (ret_chat_blocked) { *ret_chat_blocked = chat_blocked; } return; /* soon success */ } /* get fine chat name */ contact = dc_contact_new(context); - if( !dc_contact_load_from_db(contact, context->sql, contact_id) ) { + if (!dc_contact_load_from_db(contact, context->sql, contact_id)) { goto cleanup; } @@ -1452,13 +1452,13 @@ void dc_create_or_lookup_nchat_by_contact_id(dc_context_t* context, uint32_t con /* create chat record; the grpid is only used to make dc_sqlite3_get_rowid() work (we cannot use last_insert_id() due multi-threading) */ q = sqlite3_mprintf("INSERT INTO chats (type, name, param, blocked, grpid) VALUES(%i, %Q, %Q, %i, %Q)", DC_CHAT_TYPE_SINGLE, chat_name, contact_id==DC_CONTACT_ID_SELF? "K=1" : "", create_blocked, contact->addr); - assert( DC_PARAM_SELFTALK == 'K' ); + assert( DC_PARAM_SELFTALK == 'K'); stmt = dc_sqlite3_prepare(context->sql, q); - if( stmt == NULL) { + if (stmt == NULL) { goto cleanup; } - if( sqlite3_step(stmt) != SQLITE_DONE ) { + if (sqlite3_step(stmt) != SQLITE_DONE) { goto cleanup; } @@ -1473,7 +1473,7 @@ void dc_create_or_lookup_nchat_by_contact_id(dc_context_t* context, uint32_t con q = sqlite3_mprintf("INSERT INTO chats_contacts (chat_id, contact_id) VALUES(%i, %i)", chat_id, contact_id); stmt = dc_sqlite3_prepare(context->sql, q); - if( sqlite3_step(stmt) != SQLITE_DONE ) { + if (sqlite3_step(stmt) != SQLITE_DONE) { goto cleanup; } @@ -1487,8 +1487,8 @@ cleanup: sqlite3_finalize(stmt); dc_contact_unref(contact); - if( ret_chat_id ) { *ret_chat_id = chat_id; } - if( ret_chat_blocked ) { *ret_chat_blocked = create_blocked; } + if (ret_chat_id) { *ret_chat_id = chat_id; } + if (ret_chat_blocked) { *ret_chat_blocked = create_blocked; } } @@ -1516,14 +1516,14 @@ int dc_get_total_msg_count(dc_context_t* context, uint32_t chat_id) int ret = 0; sqlite3_stmt* stmt = NULL; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC) { goto cleanup; } stmt = dc_sqlite3_prepare(context->sql, "SELECT COUNT(*) FROM msgs WHERE chat_id=?;"); sqlite3_bind_int(stmt, 1, chat_id); - if( sqlite3_step(stmt) != SQLITE_ROW ) { + if (sqlite3_step(stmt) != SQLITE_ROW) { goto cleanup; } @@ -1549,7 +1549,7 @@ int dc_get_fresh_msg_count(dc_context_t* context, uint32_t chat_id) int ret = 0; sqlite3_stmt* stmt = NULL; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC) { goto cleanup; } @@ -1560,7 +1560,7 @@ int dc_get_fresh_msg_count(dc_context_t* context, uint32_t chat_id) " AND chat_id=?;"); /* we have an index over the state-column, this should be sufficient as there are typically only few fresh messages */ sqlite3_bind_int(stmt, 1, chat_id); - if( sqlite3_step(stmt) != SQLITE_ROW ) { + if (sqlite3_step(stmt) != SQLITE_ROW) { goto cleanup; } @@ -1592,7 +1592,7 @@ cleanup: */ void dc_archive_chat(dc_context_t* context, uint32_t chat_id, int archive) { - if( context == NULL || context->magic != DC_CONTEXT_MAGIC || chat_id <= DC_CHAT_ID_LAST_SPECIAL || (archive!=0 && archive!=1) ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC || chat_id <= DC_CHAT_ID_LAST_SPECIAL || (archive!=0 && archive!=1)) { return; } @@ -1645,11 +1645,11 @@ void dc_delete_chat(dc_context_t* context, uint32_t chat_id) dc_chat_t* obj = dc_chat_new(context); char* q3 = NULL; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC || chat_id <= DC_CHAT_ID_LAST_SPECIAL ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC || chat_id <= DC_CHAT_ID_LAST_SPECIAL) { goto cleanup; } - if( !dc_chat_load_from_db(obj, chat_id) ) { + if (!dc_chat_load_from_db(obj, chat_id)) { goto cleanup; } @@ -1657,28 +1657,28 @@ void dc_delete_chat(dc_context_t* context, uint32_t chat_id) pending_transaction = 1; q3 = sqlite3_mprintf("DELETE FROM msgs_mdns WHERE msg_id IN (SELECT id FROM msgs WHERE chat_id=%i);", chat_id); - if( !dc_sqlite3_execute(context->sql, q3) ) { + if (!dc_sqlite3_execute(context->sql, q3)) { goto cleanup; } sqlite3_free(q3); q3 = NULL; q3 = sqlite3_mprintf("DELETE FROM msgs WHERE chat_id=%i;", chat_id); - if( !dc_sqlite3_execute(context->sql, q3) ) { + if (!dc_sqlite3_execute(context->sql, q3)) { goto cleanup; } sqlite3_free(q3); q3 = NULL; q3 = sqlite3_mprintf("DELETE FROM chats_contacts WHERE chat_id=%i;", chat_id); - if( !dc_sqlite3_execute(context->sql, q3) ) { + if (!dc_sqlite3_execute(context->sql, q3)) { goto cleanup; } sqlite3_free(q3); q3 = NULL; q3 = sqlite3_mprintf("DELETE FROM chats WHERE id=%i;", chat_id); - if( !dc_sqlite3_execute(context->sql, q3) ) { + if (!dc_sqlite3_execute(context->sql, q3)) { goto cleanup; } sqlite3_free(q3); @@ -1690,7 +1690,7 @@ void dc_delete_chat(dc_context_t* context, uint32_t chat_id) context->cb(context, DC_EVENT_MSGS_CHANGED, 0, 0); cleanup: - if( pending_transaction ) { dc_sqlite3_rollback(context->sql); } + if (pending_transaction) { dc_sqlite3_rollback(context->sql); } dc_chat_unref(obj); sqlite3_free(q3); } @@ -1710,10 +1710,10 @@ static int last_msg_in_chat_encrypted(dc_sqlite3_t* sql, uint32_t chat_id) " WHERE timestamp=(SELECT MAX(timestamp) FROM msgs WHERE chat_id=?) " " ORDER BY id DESC;"); sqlite3_bind_int(stmt, 1, chat_id); - if( sqlite3_step(stmt) == SQLITE_ROW ) { + if (sqlite3_step(stmt) == SQLITE_ROW) { dc_param_t* msg_param = dc_param_new(); dc_param_set_packed(msg_param, (char*)sqlite3_column_text(stmt, 0)); - if( dc_param_exists(msg_param, DC_PARAM_GUARANTEE_E2EE) ) { + if (dc_param_exists(msg_param, DC_PARAM_GUARANTEE_E2EE)) { last_is_encrypted = 1; } dc_param_unref(msg_param); @@ -1729,19 +1729,19 @@ static uint32_t dc_send_msg_raw(dc_context_t* context, dc_chat_t* chat, const dc sqlite3_stmt* stmt = NULL; uint32_t msg_id = 0, to_id = 0; - if( !DC_CHAT_TYPE_CAN_SEND(chat->type) ) { + if (!DC_CHAT_TYPE_CAN_SEND(chat->type)) { dc_log_error(context, 0, "Cannot send to chat type #%i.", chat->type); goto cleanup; } - if( DC_CHAT_TYPE_IS_MULTI(chat->type) && !dc_is_contact_in_chat(context, chat->id, DC_CONTACT_ID_SELF) ) { + if (DC_CHAT_TYPE_IS_MULTI(chat->type) && !dc_is_contact_in_chat(context, chat->id, DC_CONTACT_ID_SELF)) { dc_log_error(context, DC_ERROR_SELF_NOT_IN_GROUP, NULL); goto cleanup; } { char* from = dc_sqlite3_get_config(context->sql, "configured_addr", NULL); - if( from == NULL ) { + if (from == NULL) { dc_log_error(context, 0, "Cannot send message, not configured."); goto cleanup; } @@ -1749,12 +1749,12 @@ static uint32_t dc_send_msg_raw(dc_context_t* context, dc_chat_t* chat, const dc free(from); } - if( chat->type == DC_CHAT_TYPE_SINGLE ) + if (chat->type == DC_CHAT_TYPE_SINGLE) { stmt = dc_sqlite3_prepare(context->sql, "SELECT contact_id FROM chats_contacts WHERE chat_id=?;"); sqlite3_bind_int(stmt, 1, chat->id); - if( sqlite3_step(stmt) != SQLITE_ROW ) { + if (sqlite3_step(stmt) != SQLITE_ROW) { dc_log_error(context, 0, "Cannot send message, contact for chat #%i not found.", chat->id); goto cleanup; } @@ -1762,9 +1762,9 @@ static uint32_t dc_send_msg_raw(dc_context_t* context, dc_chat_t* chat, const dc sqlite3_finalize(stmt); stmt = NULL; } - else if( DC_CHAT_TYPE_IS_MULTI(chat->type) ) + else if (DC_CHAT_TYPE_IS_MULTI(chat->type)) { - if( dc_param_get_int(chat->param, DC_PARAM_UNPROMOTED, 0)==1 ) { + if (dc_param_get_int(chat->param, DC_PARAM_UNPROMOTED, 0)==1) { /* mark group as being no longer unpromoted */ dc_param_set(chat->param, DC_PARAM_UNPROMOTED, NULL); dc_chat_update_param(chat); @@ -1773,7 +1773,7 @@ static uint32_t dc_send_msg_raw(dc_context_t* context, dc_chat_t* chat, const dc /* check if we can guarantee E2EE for this message. If we can, we won't send the message without E2EE later (because of a reset, changed settings etc. - messages may be delayed significally if there is no network present) */ int do_guarantee_e2ee = 0; - if( context->e2ee_enabled && dc_param_get_int(msg->param, DC_PARAM_FORCE_PLAINTEXT, 0)==0 ) + if (context->e2ee_enabled && dc_param_get_int(msg->param, DC_PARAM_FORCE_PLAINTEXT, 0)==0) { int can_encrypt = 1, all_mutual = 1; /* be optimistic */ stmt = dc_sqlite3_prepare(context->sql, @@ -1784,16 +1784,16 @@ static uint32_t dc_send_msg_raw(dc_context_t* context, dc_chat_t* chat, const dc " WHERE cc.chat_id=? " /* take care that this statement returns NULL rows if there is no peerstates for a chat member! */ " AND cc.contact_id>" DC_STRINGIFY(DC_CONTACT_ID_LAST_SPECIAL) ";"); /* for DC_PARAM_SELFTALK this statement does not return any row */ sqlite3_bind_int(stmt, 1, chat->id); - while( sqlite3_step(stmt) == SQLITE_ROW ) + while (sqlite3_step(stmt) == SQLITE_ROW) { - if( sqlite3_column_type(stmt, 0)==SQLITE_NULL ) { + if (sqlite3_column_type(stmt, 0)==SQLITE_NULL) { can_encrypt = 0; all_mutual = 0; } else { /* the peerstate exist, so we have either public_key or gossip_key and can encrypt potentially */ int prefer_encrypted = sqlite3_column_int(stmt, 0); - if( prefer_encrypted != DC_PE_MUTUAL ) { + if (prefer_encrypted != DC_PE_MUTUAL) { all_mutual = 0; } } @@ -1801,20 +1801,20 @@ static uint32_t dc_send_msg_raw(dc_context_t* context, dc_chat_t* chat, const dc sqlite3_finalize(stmt); stmt = NULL; - if( can_encrypt ) + if (can_encrypt) { - if( all_mutual ) { + if (all_mutual) { do_guarantee_e2ee = 1; } else { - if( last_msg_in_chat_encrypted(context->sql, chat->id) ) { + if (last_msg_in_chat_encrypted(context->sql, chat->id)) { do_guarantee_e2ee = 1; } } } } - if( do_guarantee_e2ee ) { + if (do_guarantee_e2ee) { dc_param_set_int(msg->param, DC_PARAM_GUARANTEE_E2EE, 1); } dc_param_set(msg->param, DC_PARAM_ERRONEOUS_E2EE, NULL); /* reset eg. on forwarding */ @@ -1832,7 +1832,7 @@ static uint32_t dc_send_msg_raw(dc_context_t* context, dc_chat_t* chat, const dc sqlite3_bind_text (stmt, 8, msg->text? msg->text : "", -1, SQLITE_STATIC); sqlite3_bind_text (stmt, 9, msg->param->packed, -1, SQLITE_STATIC); sqlite3_bind_int (stmt, 10, msg->hidden); - if( sqlite3_step(stmt) != SQLITE_DONE ) { + if (sqlite3_step(stmt) != SQLITE_DONE) { dc_log_error(context, 0, "Cannot send message, cannot insert to database.", chat->id); goto cleanup; } @@ -1872,28 +1872,28 @@ uint32_t dc_send_msg_object(dc_context_t* context, uint32_t chat_id, dc_msg_t* m char* pathNfilename = NULL; dc_chat_t* chat = NULL; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC || msg == NULL || chat_id <= DC_CHAT_ID_LAST_SPECIAL ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC || msg == NULL || chat_id <= DC_CHAT_ID_LAST_SPECIAL) { return 0; } msg->id = 0; msg->context = context; - if( msg->type == DC_MSG_TEXT ) + if (msg->type == DC_MSG_TEXT) { ; /* the caller should check if the message text is empty */ } - else if( DC_MSG_NEEDS_ATTACHMENT(msg->type) ) + else if (DC_MSG_NEEDS_ATTACHMENT(msg->type)) { pathNfilename = dc_param_get(msg->param, DC_PARAM_FILE, NULL); - if( pathNfilename ) + if (pathNfilename) { /* Got an attachment. Take care, the file may not be ready in this moment! This is useful eg. if a video should be sent and already shown as "being processed" in the chat. In this case, the user should create an `.increation`; when the file is deleted later on, the message is sent. (we do not use a state in the database as this would make eg. forwarding such messages much more complicated) */ - if( msg->type == DC_MSG_FILE || msg->type == DC_MSG_IMAGE ) + if (msg->type == DC_MSG_FILE || msg->type == DC_MSG_IMAGE) { /* Correct the type, take care not to correct already very special formats as GIF or VOICE. Typical conversions: @@ -1902,19 +1902,19 @@ uint32_t dc_send_msg_object(dc_context_t* context, uint32_t chat_id, dc_msg_t* m int better_type = 0; char* better_mime = NULL; dc_msg_guess_msgtype_from_suffix(pathNfilename, &better_type, &better_mime); - if( better_type ) { + if (better_type) { msg->type = better_type; dc_param_set(msg->param, DC_PARAM_MIMETYPE, better_mime); } free(better_mime); } - if( (msg->type == DC_MSG_IMAGE || msg->type == DC_MSG_GIF) - && (dc_param_get_int(msg->param, DC_PARAM_WIDTH, 0)<=0 || dc_param_get_int(msg->param, DC_PARAM_HEIGHT, 0)<=0) ) { + if ((msg->type == DC_MSG_IMAGE || msg->type == DC_MSG_GIF) + && (dc_param_get_int(msg->param, DC_PARAM_WIDTH, 0)<=0 || dc_param_get_int(msg->param, DC_PARAM_HEIGHT, 0)<=0)) { /* set width/height of images, if not yet done */ unsigned char* buf = NULL; size_t buf_bytes; uint32_t w, h; - if( dc_read_file(pathNfilename, (void**)&buf, &buf_bytes, msg->context) ) { - if( dc_get_filemeta(buf, buf_bytes, &w, &h) ) { + if (dc_read_file(pathNfilename, (void**)&buf, &buf_bytes, msg->context)) { + if (dc_get_filemeta(buf, buf_bytes, &w, &h)) { dc_param_set_int(msg->param, DC_PARAM_WIDTH, w); dc_param_set_int(msg->param, DC_PARAM_HEIGHT, h); } @@ -1924,8 +1924,8 @@ uint32_t dc_send_msg_object(dc_context_t* context, uint32_t chat_id, dc_msg_t* m dc_log_info(context, 0, "Attaching \"%s\" for message type #%i.", pathNfilename, (int)msg->type); - if( msg->text ) { free(msg->text); } - if( msg->type == DC_MSG_AUDIO ) { + if (msg->text) { free(msg->text); } + if (msg->type == DC_MSG_AUDIO) { char* filename = dc_get_filename(pathNfilename); char* author = dc_param_get(msg->param, DC_PARAM_AUTHORNAME, ""); char* title = dc_param_get(msg->param, DC_PARAM_TRACKNAME, ""); @@ -1934,10 +1934,10 @@ uint32_t dc_send_msg_object(dc_context_t* context, uint32_t chat_id, dc_msg_t* m free(author); free(title); } - else if( DC_MSG_MAKE_FILENAME_SEARCHABLE(msg->type) ) { + else if (DC_MSG_MAKE_FILENAME_SEARCHABLE(msg->type)) { msg->text = dc_get_filename(pathNfilename); } - else if( DC_MSG_MAKE_SUFFIX_SEARCHABLE(msg->type) ) { + else if (DC_MSG_MAKE_SUFFIX_SEARCHABLE(msg->type)) { msg->text = dc_get_filesuffix_lc(pathNfilename); } } @@ -1958,9 +1958,9 @@ uint32_t dc_send_msg_object(dc_context_t* context, uint32_t chat_id, dc_msg_t* m context->smtp->log_connect_errors = 1; chat = dc_chat_new(context); - if( dc_chat_load_from_db(chat, chat_id) ) { + if (dc_chat_load_from_db(chat, chat_id)) { msg->id = dc_send_msg_raw(context, chat, msg, dc_create_smeared_timestamp(context)); - if( msg ->id == 0 ) { + if (msg ->id == 0) { goto cleanup; /* error already logged */ } } @@ -1995,7 +1995,7 @@ uint32_t dc_send_text_msg(dc_context_t* context, uint32_t chat_id, const char* t dc_msg_t* msg = dc_msg_new(); uint32_t ret = 0; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC || chat_id <= DC_CHAT_ID_LAST_SPECIAL || text_to_send == NULL ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC || chat_id <= DC_CHAT_ID_LAST_SPECIAL || text_to_send == NULL) { goto cleanup; } @@ -2034,7 +2034,7 @@ uint32_t dc_send_image_msg(dc_context_t* context, uint32_t chat_id, const char* dc_msg_t* msg = dc_msg_new(); uint32_t ret = 0; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC || chat_id <= DC_CHAT_ID_LAST_SPECIAL || file == NULL ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC || chat_id <= DC_CHAT_ID_LAST_SPECIAL || file == NULL) { goto cleanup; } @@ -2077,7 +2077,7 @@ uint32_t dc_send_video_msg(dc_context_t* context, uint32_t chat_id, const char* dc_msg_t* msg = dc_msg_new(); uint32_t ret = 0; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC || chat_id <= DC_CHAT_ID_LAST_SPECIAL || file == NULL ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC || chat_id <= DC_CHAT_ID_LAST_SPECIAL || file == NULL) { goto cleanup; } @@ -2119,7 +2119,7 @@ uint32_t dc_send_voice_msg(dc_context_t* context, uint32_t chat_id, const char* dc_msg_t* msg = dc_msg_new(); uint32_t ret = 0; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC || chat_id <= DC_CHAT_ID_LAST_SPECIAL || file == NULL ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC || chat_id <= DC_CHAT_ID_LAST_SPECIAL || file == NULL) { goto cleanup; } @@ -2160,7 +2160,7 @@ uint32_t dc_send_audio_msg(dc_context_t* context, uint32_t chat_id, const char* dc_msg_t* msg = dc_msg_new(); uint32_t ret = 0; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC || chat_id <= DC_CHAT_ID_LAST_SPECIAL || file == NULL ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC || chat_id <= DC_CHAT_ID_LAST_SPECIAL || file == NULL) { goto cleanup; } @@ -2200,7 +2200,7 @@ uint32_t dc_send_file_msg(dc_context_t* context, uint32_t chat_id, const char* f dc_msg_t* msg = dc_msg_new(); uint32_t ret = 0; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC || chat_id <= DC_CHAT_ID_LAST_SPECIAL || file == NULL ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC || chat_id <= DC_CHAT_ID_LAST_SPECIAL || file == NULL) { goto cleanup; } @@ -2243,15 +2243,15 @@ uint32_t dc_send_vcard_msg(dc_context_t* context, uint32_t chat_id, uint32_t con dc_contact_t* contact = NULL; char* text_to_send = NULL; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC || chat_id <= DC_CHAT_ID_LAST_SPECIAL ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC || chat_id <= DC_CHAT_ID_LAST_SPECIAL) { goto cleanup; } - if( (contact=dc_get_contact(context, contact_id)) == NULL ) { + if ((contact=dc_get_contact(context, contact_id)) == NULL) { goto cleanup; } - if( contact->authname && contact->authname[0] ) { + if (contact->authname && contact->authname[0]) { text_to_send = dc_mprintf("%s: %s", contact->authname, contact->addr); } else { @@ -2279,7 +2279,7 @@ void dc_add_device_msg(dc_context_t* context, uint32_t chat_id, const char* text sqlite3_stmt* stmt = NULL; char* rfc724_mid = dc_create_outgoing_rfc724_mid(NULL, "@device"); - if( context == NULL || context->magic != DC_CONTEXT_MAGIC || text == NULL ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC || text == NULL) { goto cleanup; } @@ -2293,7 +2293,7 @@ void dc_add_device_msg(dc_context_t* context, uint32_t chat_id, const char* text sqlite3_bind_int (stmt, 6, DC_STATE_IN_NOTICED); sqlite3_bind_text (stmt, 7, text, -1, SQLITE_STATIC); sqlite3_bind_text (stmt, 8, rfc724_mid, -1, SQLITE_STATIC); - if( sqlite3_step(stmt) != SQLITE_DONE ) { + if (sqlite3_step(stmt) != SQLITE_DONE) { goto cleanup; } msg_id = dc_sqlite3_get_rowid(context->sql, "msgs", "rfc724_mid", rfc724_mid); @@ -2326,7 +2326,7 @@ int dc_is_group_explicitly_left(dc_context_t* context, const char* grpid) void dc_set_group_explicitly_left(dc_context_t* context, const char* grpid) { - if( !dc_is_group_explicitly_left(context, grpid) ) + if (!dc_is_group_explicitly_left(context, grpid)) { sqlite3_stmt* stmt = dc_sqlite3_prepare(context->sql, "INSERT INTO leftgrps (grpid) VALUES(?);"); sqlite3_bind_text (stmt, 1, grpid, -1, SQLITE_STATIC); @@ -2342,8 +2342,8 @@ static int dc_real_group_exists(dc_context_t* context, uint32_t chat_id) sqlite3_stmt* stmt = NULL; int ret = 0; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC || context->sql->cobj==NULL - || chat_id <= DC_CHAT_ID_LAST_SPECIAL ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC || context->sql->cobj==NULL + || chat_id <= DC_CHAT_ID_LAST_SPECIAL) { return 0; } @@ -2352,7 +2352,7 @@ static int dc_real_group_exists(dc_context_t* context, uint32_t chat_id) " WHERE id=? " " AND (type=" DC_STRINGIFY(DC_CHAT_TYPE_GROUP) " OR type=" DC_STRINGIFY(DC_CHAT_TYPE_VERIFIED_GROUP) ");"); sqlite3_bind_int(stmt, 1, chat_id); - if( sqlite3_step(stmt) == SQLITE_ROW ) { + if (sqlite3_step(stmt) == SQLITE_ROW) { ret = 1; } sqlite3_finalize(stmt); @@ -2404,7 +2404,7 @@ uint32_t dc_create_group_chat(dc_context_t* context, int verified, const char* c char* draft_txt = NULL, *grpid = NULL; sqlite3_stmt* stmt = NULL; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC || chat_name==NULL || chat_name[0]==0 ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC || chat_name==NULL || chat_name[0]==0) { return 0; } @@ -2412,21 +2412,21 @@ uint32_t dc_create_group_chat(dc_context_t* context, int verified, const char* c grpid = dc_create_id(); stmt = dc_sqlite3_prepare(context->sql, - "INSERT INTO chats (type, name, draft_timestamp, draft_txt, grpid, param) VALUES(?, ?, ?, ?, ?, 'U=1');" /*U=DC_PARAM_UNPROMOTED*/ ); + "INSERT INTO chats (type, name, draft_timestamp, draft_txt, grpid, param) VALUES(?, ?, ?, ?, ?, 'U=1');" /*U=DC_PARAM_UNPROMOTED*/); sqlite3_bind_int (stmt, 1, verified? DC_CHAT_TYPE_VERIFIED_GROUP : DC_CHAT_TYPE_GROUP); sqlite3_bind_text (stmt, 2, chat_name, -1, SQLITE_STATIC); sqlite3_bind_int64(stmt, 3, time(NULL)); sqlite3_bind_text (stmt, 4, draft_txt, -1, SQLITE_STATIC); sqlite3_bind_text (stmt, 5, grpid, -1, SQLITE_STATIC); - if( sqlite3_step(stmt)!=SQLITE_DONE ) { + if ( sqlite3_step(stmt)!=SQLITE_DONE) { goto cleanup; } - if( (chat_id=dc_sqlite3_get_rowid(context->sql, "chats", "grpid", grpid)) == 0 ) { + if ((chat_id=dc_sqlite3_get_rowid(context->sql, "chats", "grpid", grpid)) == 0) { goto cleanup; } - if( dc_add_to_chat_contacts_table(context, chat_id, DC_CONTACT_ID_SELF) ) { + if (dc_add_to_chat_contacts_table(context, chat_id, DC_CONTACT_ID_SELF)) { goto cleanup; } @@ -2435,7 +2435,7 @@ cleanup: free(draft_txt); free(grpid); - if( chat_id ) { + if (chat_id) { context->cb(context, DC_EVENT_MSGS_CHANGED, 0, 0); } @@ -2465,32 +2465,32 @@ int dc_set_chat_name(dc_context_t* context, uint32_t chat_id, const char* new_na dc_msg_t* msg = dc_msg_new(); char* q3 = NULL; - if( context==NULL || context->magic != DC_CONTEXT_MAGIC || new_name==NULL || new_name[0]==0 || chat_id <= DC_CHAT_ID_LAST_SPECIAL ) { + if (context==NULL || context->magic != DC_CONTEXT_MAGIC || new_name==NULL || new_name[0]==0 || chat_id <= DC_CHAT_ID_LAST_SPECIAL) { goto cleanup; } - if( 0==dc_real_group_exists(context, chat_id) - || 0==dc_chat_load_from_db(chat, chat_id) ) { + if (0==dc_real_group_exists(context, chat_id) + || 0==dc_chat_load_from_db(chat, chat_id)) { goto cleanup; } - if( strcmp(chat->name, new_name)==0 ) { + if (strcmp(chat->name, new_name)==0) { success = 1; goto cleanup; /* name not modified */ } - if( !IS_SELF_IN_GROUP ) { + if (!IS_SELF_IN_GROUP) { dc_log_error(context, DC_ERROR_SELF_NOT_IN_GROUP, NULL); goto cleanup; /* we shoud respect this - whatever we send to the group, it gets discarded anyway! */ } q3 = sqlite3_mprintf("UPDATE chats SET name=%Q WHERE id=%i;", new_name, chat_id); - if( !dc_sqlite3_execute(context->sql, q3) ) { + if (!dc_sqlite3_execute(context->sql, q3)) { goto cleanup; } /* send a status mail to all group members, also needed for outself to allow multi-client */ - if( DO_SEND_STATUS_MAILS ) + if (DO_SEND_STATUS_MAILS) { msg->type = DC_MSG_TEXT; msg->text = dc_stock_str_repl_string2(context, DC_STR_MSGGRPNAME, chat->name, new_name); @@ -2533,27 +2533,27 @@ int dc_set_chat_profile_image(dc_context_t* context, uint32_t chat_id, const cha dc_chat_t* chat = dc_chat_new(context); dc_msg_t* msg = dc_msg_new(); - if( context==NULL || context->magic != DC_CONTEXT_MAGIC || chat_id <= DC_CHAT_ID_LAST_SPECIAL ) { + if (context==NULL || context->magic != DC_CONTEXT_MAGIC || chat_id <= DC_CHAT_ID_LAST_SPECIAL) { goto cleanup; } - if( 0==dc_real_group_exists(context, chat_id) - || 0==dc_chat_load_from_db(chat, chat_id) ) { + if (0==dc_real_group_exists(context, chat_id) + || 0==dc_chat_load_from_db(chat, chat_id)) { goto cleanup; } - if( !IS_SELF_IN_GROUP ) { + if (!IS_SELF_IN_GROUP) { dc_log_error(context, DC_ERROR_SELF_NOT_IN_GROUP, NULL); goto cleanup; /* we shoud respect this - whatever we send to the group, it gets discarded anyway! */ } dc_param_set(chat->param, DC_PARAM_PROFILE_IMAGE, new_image/*may be NULL*/); - if( !dc_chat_update_param(chat) ) { + if (!dc_chat_update_param(chat)) { goto cleanup; } /* send a status mail to all group members, also needed for outself to allow multi-client */ - if( DO_SEND_STATUS_MAILS ) + if (DO_SEND_STATUS_MAILS) { dc_param_set_int(msg->param, DC_PARAM_CMD, DC_CMD_GROUPIMAGE_CHANGED); dc_param_set (msg->param, DC_PARAM_CMD_ARG, new_image); @@ -2579,7 +2579,7 @@ int dc_get_chat_contact_count(dc_context_t* context, uint32_t chat_id) sqlite3_stmt* stmt = dc_sqlite3_prepare(context->sql, "SELECT COUNT(*) FROM chats_contacts WHERE chat_id=?;"); sqlite3_bind_int(stmt, 1, chat_id); - if( sqlite3_step(stmt) == SQLITE_ROW ) { + if (sqlite3_step(stmt) == SQLITE_ROW) { ret = sqlite3_column_int(stmt, 0); } sqlite3_finalize(stmt); @@ -2604,7 +2604,7 @@ int dc_is_contact_in_chat(dc_context_t* context, uint32_t chat_id, uint32_t cont int ret = 0; sqlite3_stmt* stmt = NULL; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC) { goto cleanup; } @@ -2629,35 +2629,35 @@ int dc_add_contact_to_chat_ex(dc_context_t* context, uint32_t chat_id, uint32_t dc_msg_t* msg = dc_msg_new(); char* self_addr = NULL; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC || contact == NULL || chat_id <= DC_CHAT_ID_LAST_SPECIAL ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC || contact == NULL || chat_id <= DC_CHAT_ID_LAST_SPECIAL) { goto cleanup; } - if( 0==dc_real_group_exists(context, chat_id) /*this also makes sure, not contacts are added to special or normal chats*/ + if (0==dc_real_group_exists(context, chat_id) /*this also makes sure, not contacts are added to special or normal chats*/ || (0==dc_real_contact_exists(context, contact_id) && contact_id!=DC_CONTACT_ID_SELF) - || 0==dc_chat_load_from_db(chat, chat_id) ) { + || 0==dc_chat_load_from_db(chat, chat_id)) { goto cleanup; } - if( !IS_SELF_IN_GROUP ) { + if (!IS_SELF_IN_GROUP) { dc_log_error(context, DC_ERROR_SELF_NOT_IN_GROUP, NULL); goto cleanup; /* we shoud respect this - whatever we send to the group, it gets discarded anyway! */ } - if( (flags&DC_FROM_HANDSHAKE) && dc_param_get_int(chat->param, DC_PARAM_UNPROMOTED, 0)==1 ) { + if ((flags&DC_FROM_HANDSHAKE) && dc_param_get_int(chat->param, DC_PARAM_UNPROMOTED, 0)==1) { // after a handshake, force sending the `Chat-Group-Member-Added` message dc_param_set(chat->param, DC_PARAM_UNPROMOTED, NULL); dc_chat_update_param(chat); } self_addr = dc_sqlite3_get_config(context->sql, "configured_addr", ""); - if( strcasecmp(contact->addr, self_addr)==0 ) { + if (strcasecmp(contact->addr, self_addr)==0) { goto cleanup; /* ourself is added using DC_CONTACT_ID_SELF, do not add it explicitly. if SELF is not in the group, members cannot be added at all. */ } - if( dc_is_contact_in_chat(context, chat_id, contact_id) ) + if (dc_is_contact_in_chat(context, chat_id, contact_id)) { - if( !(flags&DC_FROM_HANDSHAKE) ) { + if (!(flags&DC_FROM_HANDSHAKE)) { success = 1; goto cleanup; } @@ -2665,22 +2665,22 @@ int dc_add_contact_to_chat_ex(dc_context_t* context, uint32_t chat_id, uint32_t } else { - if( chat->type == DC_CHAT_TYPE_VERIFIED_GROUP ) + if (chat->type == DC_CHAT_TYPE_VERIFIED_GROUP) { - if( !dc_apeerstate_load_by_addr(peerstate, context->sql, contact->addr) - || dc_contact_n_peerstate_are_verified(contact, peerstate) != DC_BIDIRECT_VERIFIED ) { + if (!dc_apeerstate_load_by_addr(peerstate, context->sql, contact->addr) + || dc_contact_n_peerstate_are_verified(contact, peerstate) != DC_BIDIRECT_VERIFIED) { dc_log_error(context, 0, "Only bidirectional verified contacts can be added to verfied groups."); goto cleanup; } } - if( 0==dc_add_to_chat_contacts_table(context, chat_id, contact_id) ) { + if (0==dc_add_to_chat_contacts_table(context, chat_id, contact_id)) { goto cleanup; } } /* send a status mail to all group members */ - if( DO_SEND_STATUS_MAILS ) + if (DO_SEND_STATUS_MAILS) { msg->type = DC_MSG_TEXT; msg->text = dc_stock_str_repl_string(context, DC_STR_MSGADDMEMBER, (contact->authname&&contact->authname[0])? contact->authname : contact->addr); @@ -2748,28 +2748,28 @@ int dc_remove_contact_from_chat(dc_context_t* context, uint32_t chat_id, uint32_ dc_msg_t* msg = dc_msg_new(); char* q3 = NULL; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC || chat_id <= DC_CHAT_ID_LAST_SPECIAL || (contact_id<=DC_CONTACT_ID_LAST_SPECIAL && contact_id!=DC_CONTACT_ID_SELF) ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC || chat_id <= DC_CHAT_ID_LAST_SPECIAL || (contact_id<=DC_CONTACT_ID_LAST_SPECIAL && contact_id!=DC_CONTACT_ID_SELF)) { goto cleanup; /* we do not check if "contact_id" exists but just delete all records with the id from chats_contacts */ } /* this allows to delete pending references to deleted contacts. Of course, this should _not_ happen. */ - if( 0==dc_real_group_exists(context, chat_id) - || 0==dc_chat_load_from_db(chat, chat_id) ) { + if (0==dc_real_group_exists(context, chat_id) + || 0==dc_chat_load_from_db(chat, chat_id)) { goto cleanup; } - if( !IS_SELF_IN_GROUP ) { + if (!IS_SELF_IN_GROUP) { dc_log_error(context, DC_ERROR_SELF_NOT_IN_GROUP, NULL); goto cleanup; /* we shoud respect this - whatever we send to the group, it gets discarded anyway! */ } /* send a status mail to all group members - we need to do this before we update the database - otherwise the !IS_SELF_IN_GROUP__-check in dc_chat_send_msg() will fail. */ - if( contact ) + if (contact) { - if( DO_SEND_STATUS_MAILS ) + if (DO_SEND_STATUS_MAILS) { msg->type = DC_MSG_TEXT; - if( contact->id == DC_CONTACT_ID_SELF ) { + if (contact->id == DC_CONTACT_ID_SELF) { dc_set_group_explicitly_left(context, chat->grpid); msg->text = dc_stock_str(context, DC_STR_MSGGROUPLEFT); } @@ -2784,7 +2784,7 @@ int dc_remove_contact_from_chat(dc_context_t* context, uint32_t chat_id, uint32_ } q3 = sqlite3_mprintf("DELETE FROM chats_contacts WHERE chat_id=%i AND contact_id=%i;", chat_id, contact_id); - if( !dc_sqlite3_execute(context->sql, q3) ) { + if (!dc_sqlite3_execute(context->sql, q3)) { goto cleanup; } @@ -2811,8 +2811,8 @@ int dc_real_contact_exists(dc_context_t* context, uint32_t contact_id) sqlite3_stmt* stmt = NULL; int ret = 0; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC || context->sql->cobj==NULL - || contact_id <= DC_CONTACT_ID_LAST_SPECIAL ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC || context->sql->cobj==NULL + || contact_id <= DC_CONTACT_ID_LAST_SPECIAL) { goto cleanup; } @@ -2820,7 +2820,7 @@ int dc_real_contact_exists(dc_context_t* context, uint32_t contact_id) "SELECT id FROM contacts WHERE id=?;"); sqlite3_bind_int(stmt, 1, contact_id); - if( sqlite3_step(stmt) == SQLITE_ROW ) { + if (sqlite3_step(stmt) == SQLITE_ROW) { ret = 1; } @@ -2835,13 +2835,13 @@ size_t dc_get_real_contact_cnt(dc_context_t* context) size_t ret = 0; sqlite3_stmt* stmt = NULL; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC || context->sql->cobj==NULL ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC || context->sql->cobj==NULL) { goto cleanup; } stmt = dc_sqlite3_prepare(context->sql, "SELECT COUNT(*) FROM contacts WHERE id>?;"); sqlite3_bind_int(stmt, 1, DC_CONTACT_ID_LAST_SPECIAL); - if( sqlite3_step(stmt) != SQLITE_ROW ) { + if (sqlite3_step(stmt) != SQLITE_ROW) { goto cleanup; } @@ -2869,13 +2869,13 @@ uint32_t dc_add_or_lookup_contact( dc_context_t* context, char* row_addr = NULL; char* row_authname = NULL; - if( sth_modified == NULL ) { + if (sth_modified == NULL) { sth_modified = &dummy; } *sth_modified = 0; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC || addr__ == NULL || origin <= 0 ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC || addr__ == NULL || origin <= 0) { goto cleanup; } @@ -2884,7 +2884,7 @@ uint32_t dc_add_or_lookup_contact( dc_context_t* context, addr = dc_normalize_addr(addr__); /* rough check if email-address is valid */ - if( strlen(addr) < 3 || strchr(addr, '@')==NULL || strchr(addr, '.')==NULL ) { + if (strlen(addr) < 3 || strchr(addr, '@')==NULL || strchr(addr, '.')==NULL) { dc_log_warning(context, 0, "Bad address \"%s\" for contact \"%s\".", addr, name?name:""); goto cleanup; } @@ -2894,7 +2894,7 @@ uint32_t dc_add_or_lookup_contact( dc_context_t* context, stmt = dc_sqlite3_prepare(context->sql, "SELECT id, name, addr, origin, authname FROM contacts WHERE addr=? COLLATE NOCASE;"); sqlite3_bind_text(stmt, 1, (const char*)addr, -1, SQLITE_STATIC); - if( sqlite3_step(stmt) == SQLITE_ROW ) + if (sqlite3_step(stmt) == SQLITE_ROW) { int row_origin, update_addr = 0, update_name = 0, update_authname = 0; @@ -2907,9 +2907,9 @@ uint32_t dc_add_or_lookup_contact( dc_context_t* context, sqlite3_finalize (stmt); stmt = NULL; - if( name && name[0] ) { - if( row_name[0] ) { - if( origin>=row_origin && strcmp(name, row_name)!=0 ) { + if (name && name[0]) { + if (row_name[0]) { + if (origin>=row_origin && strcmp(name, row_name)!=0) { update_name = 1; } } @@ -2917,16 +2917,16 @@ uint32_t dc_add_or_lookup_contact( dc_context_t* context, update_name = 1; } - if( origin == DC_ORIGIN_INCOMING_UNKNOWN_FROM && strcmp(name, row_authname)!=0 ) { + if (origin == DC_ORIGIN_INCOMING_UNKNOWN_FROM && strcmp(name, row_authname)!=0) { update_authname = 1; } } - if( origin>=row_origin && strcmp(addr, row_addr)!=0 /*really compare case-sensitive here*/ ) { + if (origin>=row_origin && strcmp(addr, row_addr)!=0 /*really compare case-sensitive here*/) { update_addr = 1; } - if( update_name || update_authname || update_addr || origin>row_origin ) + if (update_name || update_authname || update_addr || origin>row_origin) { stmt = dc_sqlite3_prepare(context->sql, "UPDATE contacts SET name=?, addr=?, origin=?, authname=? WHERE id=?;"); @@ -2939,7 +2939,7 @@ uint32_t dc_add_or_lookup_contact( dc_context_t* context, sqlite3_finalize (stmt); stmt = NULL; - if( update_name ) + if (update_name) { /* Update the contact name also if it is used as a group name. This is one of the few duplicated data, however, getting the chat list is easier this way.*/ @@ -2964,7 +2964,7 @@ uint32_t dc_add_or_lookup_contact( dc_context_t* context, sqlite3_bind_text(stmt, 1, name? name : "", -1, SQLITE_STATIC); /* avoid NULL-fields in column */ sqlite3_bind_text(stmt, 2, addr, -1, SQLITE_STATIC); sqlite3_bind_int (stmt, 3, origin); - if( sqlite3_step(stmt) == SQLITE_DONE ) + if (sqlite3_step(stmt) == SQLITE_DONE) { row_id = dc_sqlite3_get_rowid(context->sql, "contacts", "addr", addr); *sth_modified = CONTACT_CREATED; @@ -2987,7 +2987,7 @@ cleanup: void dc_scaleup_contact_origin(dc_context_t* context, uint32_t contact_id, int origin) { - if( context == NULL || context->magic != DC_CONTEXT_MAGIC ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC) { return; } @@ -3006,8 +3006,8 @@ int dc_is_contact_blocked(dc_context_t* context, uint32_t contact_id) int is_blocked = 0; dc_contact_t* contact = dc_contact_new(context); - if( dc_contact_load_from_db(contact, context->sql, contact_id) ) { - if( contact->blocked ) { + if (dc_contact_load_from_db(contact, context->sql, contact_id)) { + if (contact->blocked) { is_blocked = 1; } } @@ -3020,16 +3020,16 @@ int dc_is_contact_blocked(dc_context_t* context, uint32_t contact_id) int dc_get_contact_origin(dc_context_t* context, uint32_t contact_id, int* ret_blocked) { int ret = 0; - int dummy; if( ret_blocked==NULL ) { ret_blocked = &dummy; } + int dummy; if (ret_blocked==NULL) { ret_blocked = &dummy; } dc_contact_t* contact = dc_contact_new(context); *ret_blocked = 0; - if( !dc_contact_load_from_db(contact, context->sql, contact_id) ) { /* we could optimize this by loading only the needed fields */ + if (!dc_contact_load_from_db(contact, context->sql, contact_id)) { /* we could optimize this by loading only the needed fields */ goto cleanup; } - if( contact->blocked ) { + if (contact->blocked) { *ret_blocked = 1; goto cleanup; } @@ -3068,7 +3068,7 @@ uint32_t dc_create_contact(dc_context_t* context, const char* name, const char* int sth_modified = 0; int blocked = 0; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC || addr == NULL || addr[0]==0 ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC || addr == NULL || addr[0]==0) { goto cleanup; } @@ -3078,7 +3078,7 @@ uint32_t dc_create_contact(dc_context_t* context, const char* name, const char* context->cb(context, DC_EVENT_CONTACTS_CHANGED, sth_modified==CONTACT_CREATED? contact_id : 0, 0); - if( blocked ) { + if (blocked) { dc_block_contact(context, contact_id, 0); } @@ -3112,23 +3112,23 @@ int dc_add_address_book(dc_context_t* context, const char* adr_book) /* format: size_t i, iCnt; int sth_modified, modify_cnt = 0; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC || adr_book == NULL ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC || adr_book == NULL) { goto cleanup; } - if( (lines=dc_split_into_lines(adr_book))==NULL ) { + if ((lines=dc_split_into_lines(adr_book))==NULL) { goto cleanup; } dc_sqlite3_begin_transaction(context->sql); iCnt = carray_count(lines); - for( i = 0; i+1 < iCnt; i += 2 ) { + for (i = 0; i+1 < iCnt; i += 2) { char* name = (char*)carray_get(lines, i); char* addr = (char*)carray_get(lines, i+1); dc_normalize_name(name); dc_add_or_lookup_contact(context, name, addr, DC_ORIGIN_ADRESS_BOOK, &sth_modified); - if( sth_modified ) { + if (sth_modified) { modify_cnt++; } } @@ -3168,15 +3168,15 @@ dc_array_t* dc_get_contacts(dc_context_t* context, uint32_t listflags, const cha char* s3strLikeCmd = NULL; sqlite3_stmt* stmt = NULL; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC) { goto cleanup; } self_addr = dc_sqlite3_get_config(context->sql, "configured_addr", ""); /* we add DC_CONTACT_ID_SELF explicitly; so avoid doubles if the address is present as a normal entry for some case */ - if( (listflags&DC_GCL_VERIFIED_ONLY) || query ) + if ((listflags&DC_GCL_VERIFIED_ONLY) || query) { - if( (s3strLikeCmd=sqlite3_mprintf("%%%s%%", query? query : ""))==NULL ) { + if ((s3strLikeCmd=sqlite3_mprintf("%%%s%%", query? query : ""))==NULL) { goto cleanup; } stmt = dc_sqlite3_prepare(context->sql, @@ -3192,7 +3192,7 @@ dc_array_t* dc_get_contacts(dc_context_t* context, uint32_t listflags, const cha self_name = dc_sqlite3_get_config(context->sql, "displayname", ""); self_name2 = dc_stock_str(context, DC_STR_SELF); - if( query==NULL || dc_str_contains(self_addr, query) || dc_str_contains(self_name, query) || dc_str_contains(self_name2, query) ) { + if (query==NULL || dc_str_contains(self_addr, query) || dc_str_contains(self_name, query) || dc_str_contains(self_name2, query)) { add_self = 1; } } @@ -3207,12 +3207,12 @@ dc_array_t* dc_get_contacts(dc_context_t* context, uint32_t listflags, const cha add_self = 1; } - while( sqlite3_step(stmt) == SQLITE_ROW ) { + while (sqlite3_step(stmt) == SQLITE_ROW) { dc_array_add_id(ret, sqlite3_column_int(stmt, 0)); } /* to the end of the list, add self - this is to be in sync with member lists and to allow the user to start a self talk */ - if( (listflags&DC_GCL_ADD_SELF) && add_self ) { + if ((listflags&DC_GCL_ADD_SELF) && add_self) { dc_array_add_id(ret, DC_CONTACT_ID_SELF); } @@ -3239,7 +3239,7 @@ dc_array_t* dc_get_blocked_contacts(dc_context_t* context) dc_array_t* ret = dc_array_new(context, 100); sqlite3_stmt* stmt = NULL; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC) { goto cleanup; } @@ -3248,7 +3248,7 @@ dc_array_t* dc_get_blocked_contacts(dc_context_t* context) " WHERE id>? AND blocked!=0" " ORDER BY LOWER(name||addr),id;"); sqlite3_bind_int(stmt, 1, DC_CONTACT_ID_LAST_SPECIAL); - while( sqlite3_step(stmt) == SQLITE_ROW ) { + while (sqlite3_step(stmt) == SQLITE_ROW) { dc_array_add_id(ret, sqlite3_column_int(stmt, 0)); } @@ -3270,7 +3270,7 @@ int dc_get_blocked_count(dc_context_t* context) int ret = 0; sqlite3_stmt* stmt = NULL; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC) { goto cleanup; } @@ -3278,7 +3278,7 @@ int dc_get_blocked_count(dc_context_t* context) "SELECT COUNT(*) FROM contacts" " WHERE id>? AND blocked!=0"); sqlite3_bind_int(stmt, 1, DC_CONTACT_ID_LAST_SPECIAL); - if( sqlite3_step(stmt) != SQLITE_ROW ) { + if (sqlite3_step(stmt) != SQLITE_ROW) { goto cleanup; } ret = sqlite3_column_int(stmt, 0); @@ -3306,7 +3306,7 @@ dc_contact_t* dc_get_contact(dc_context_t* context, uint32_t contact_id) { dc_contact_t* ret = dc_contact_new(context); - if( !dc_contact_load_from_db(ret, context->sql, contact_id) ) { + if (!dc_contact_load_from_db(ret, context->sql, contact_id)) { dc_contact_unref(ret); ret = NULL; } @@ -3327,7 +3327,7 @@ dc_contact_t* dc_get_contact(dc_context_t* context, uint32_t contact_id) */ void dc_marknoticed_contact(dc_context_t* context, uint32_t contact_id) { - if( context == NULL || context->magic != DC_CONTEXT_MAGIC ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC) { return; } @@ -3343,7 +3343,7 @@ void dc_block_chat(dc_context_t* context, uint32_t chat_id, int new_blocking) { sqlite3_stmt* stmt = NULL; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC) { return; } @@ -3378,18 +3378,18 @@ void dc_block_contact(dc_context_t* context, uint32_t contact_id, int new_blocki dc_contact_t* contact = dc_contact_new(context); sqlite3_stmt* stmt = NULL; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC || contact_id <= DC_CONTACT_ID_LAST_SPECIAL ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC || contact_id <= DC_CONTACT_ID_LAST_SPECIAL) { goto cleanup; } - if( dc_contact_load_from_db(contact, context->sql, contact_id) - && contact->blocked != new_blocking ) + if (dc_contact_load_from_db(contact, context->sql, contact_id) + && contact->blocked != new_blocking) { stmt = dc_sqlite3_prepare(context->sql, "UPDATE contacts SET blocked=? WHERE id=?;"); sqlite3_bind_int(stmt, 1, new_blocking); sqlite3_bind_int(stmt, 2, contact_id); - if( sqlite3_step(stmt)!=SQLITE_DONE ) { + if (sqlite3_step(stmt)!=SQLITE_DONE) { goto cleanup; } sqlite3_finalize(stmt); @@ -3404,7 +3404,7 @@ void dc_block_contact(dc_context_t* context, uint32_t contact_id, int new_blocki sqlite3_bind_int(stmt, 1, new_blocking); sqlite3_bind_int(stmt, 2, DC_CHAT_TYPE_SINGLE); sqlite3_bind_int(stmt, 3, contact_id); - if( sqlite3_step(stmt)!=SQLITE_DONE ) { + if (sqlite3_step(stmt)!=SQLITE_DONE) { goto cleanup; } @@ -3414,7 +3414,7 @@ void dc_block_contact(dc_context_t* context, uint32_t contact_id, int new_blocki send_event = 1; } - if( send_event ) { + if (send_event) { context->cb(context, DC_EVENT_CONTACTS_CHANGED, 0, 0); } @@ -3431,9 +3431,9 @@ static void cat_fingerprint(dc_strbuilder_t* ret, const char* addr, const char* dc_strbuilder_cat(ret, ":\n"); dc_strbuilder_cat(ret, (fingerprint_verified&&fingerprint_verified[0])? fingerprint_verified : fingerprint_unverified); - if( fingerprint_verified && fingerprint_verified[0] + if (fingerprint_verified && fingerprint_verified[0] && fingerprint_unverified && fingerprint_unverified[0] - && strcmp(fingerprint_verified, fingerprint_unverified)!=0 ) { + && strcmp(fingerprint_verified, fingerprint_unverified)!=0) { // might be that for verified chats the - older - verified gossiped key is used // and for normal chats the - newer - unverified key :/ dc_strbuilder_cat(ret, "\n\n"); @@ -3465,14 +3465,14 @@ char* dc_get_contact_encrinfo(dc_context_t* context, uint32_t contact_id) char* fingerprint_other_unverified = NULL; char* p = NULL; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC) { goto cleanup; } dc_strbuilder_t ret; dc_strbuilder_init(&ret, 0); - if( !dc_contact_load_from_db(contact, context->sql, contact_id) ) { + if (!dc_contact_load_from_db(contact, context->sql, contact_id)) { goto cleanup; } dc_apeerstate_load_by_addr(peerstate, context->sql, contact->addr); @@ -3480,12 +3480,12 @@ char* dc_get_contact_encrinfo(dc_context_t* context, uint32_t contact_id) dc_key_load_self_public(self_key, loginparam->addr, context->sql); - if( dc_apeerstate_peek_key(peerstate, DC_NOT_VERIFIED) ) + if (dc_apeerstate_peek_key(peerstate, DC_NOT_VERIFIED)) { // E2E available :) p = dc_stock_str(context, peerstate->prefer_encrypt == DC_PE_MUTUAL? DC_STR_E2E_PREFERRED : DC_STR_E2E_AVAILABLE); dc_strbuilder_cat(&ret, p); free(p); - if( self_key->binary == NULL ) { + if (self_key->binary == NULL) { dc_pgp_rand_seed(context, peerstate->addr, strlen(peerstate->addr) /*just some random data*/); dc_ensure_secret_key_exists(context); dc_key_load_self_public(self_key, loginparam->addr, context->sql); @@ -3499,7 +3499,7 @@ char* dc_get_contact_encrinfo(dc_context_t* context, uint32_t contact_id) fingerprint_other_verified = dc_key_get_formatted_fingerprint(dc_apeerstate_peek_key(peerstate, DC_BIDIRECT_VERIFIED)); fingerprint_other_unverified = dc_key_get_formatted_fingerprint(dc_apeerstate_peek_key(peerstate, DC_NOT_VERIFIED)); - if( strcmp(loginparam->addr, peerstate->addr)<0 ) { + if (strcmp(loginparam->addr, peerstate->addr)<0) { cat_fingerprint(&ret, loginparam->addr, fingerprint_self, NULL); cat_fingerprint(&ret, peerstate->addr, fingerprint_other_verified, fingerprint_other_unverified); } @@ -3511,8 +3511,8 @@ char* dc_get_contact_encrinfo(dc_context_t* context, uint32_t contact_id) else { // No E2E available - if( !(loginparam->server_flags&DC_LP_IMAP_SOCKET_PLAIN) - && !(loginparam->server_flags&DC_LP_SMTP_SOCKET_PLAIN) ) + if (!(loginparam->server_flags&DC_LP_IMAP_SOCKET_PLAIN) + && !(loginparam->server_flags&DC_LP_SMTP_SOCKET_PLAIN)) { p = dc_stock_str(context, DC_STR_ENCR_TRANSP); dc_strbuilder_cat(&ret, p); free(p); } @@ -3550,7 +3550,7 @@ int dc_delete_contact(dc_context_t* context, uint32_t contact_id) int success = 0; sqlite3_stmt* stmt = NULL; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC || contact_id <= DC_CONTACT_ID_LAST_SPECIAL ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC || contact_id <= DC_CONTACT_ID_LAST_SPECIAL) { goto cleanup; } @@ -3559,7 +3559,7 @@ int dc_delete_contact(dc_context_t* context, uint32_t contact_id) stmt = dc_sqlite3_prepare(context->sql, "SELECT COUNT(*) FROM chats_contacts WHERE contact_id=?;"); sqlite3_bind_int(stmt, 1, contact_id); - if( sqlite3_step(stmt) != SQLITE_ROW || sqlite3_column_int(stmt, 0) >= 1 ) { + if (sqlite3_step(stmt) != SQLITE_ROW || sqlite3_column_int(stmt, 0) >= 1) { goto cleanup; } sqlite3_finalize(stmt); @@ -3569,7 +3569,7 @@ int dc_delete_contact(dc_context_t* context, uint32_t contact_id) "SELECT COUNT(*) FROM msgs WHERE from_id=? OR to_id=?;"); sqlite3_bind_int(stmt, 1, contact_id); sqlite3_bind_int(stmt, 2, contact_id); - if( sqlite3_step(stmt) != SQLITE_ROW || sqlite3_column_int(stmt, 0) >= 1 ) { + if (sqlite3_step(stmt) != SQLITE_ROW || sqlite3_column_int(stmt, 0) >= 1) { goto cleanup; } sqlite3_finalize(stmt); @@ -3578,7 +3578,7 @@ int dc_delete_contact(dc_context_t* context, uint32_t contact_id) stmt = dc_sqlite3_prepare(context->sql, "DELETE FROM contacts WHERE id=?;"); sqlite3_bind_int(stmt, 1, contact_id); - if( sqlite3_step(stmt) != SQLITE_DONE ) { + if (sqlite3_step(stmt) != SQLITE_DONE) { goto cleanup; } @@ -3595,11 +3595,11 @@ cleanup: int dc_contact_addr_equals(dc_context_t* context, uint32_t contact_id, const char* other_addr) { int addr_are_equal = 0; - if( other_addr ) { + if (other_addr) { dc_contact_t* contact = dc_contact_new(context); - if( dc_contact_load_from_db(contact, context->sql, contact_id) ) { - if( contact->addr ) { - if( strcasecmp(contact->addr, other_addr)==0 ) { + if (dc_contact_load_from_db(contact, context->sql, contact_id)) { + if (contact->addr) { + if (strcasecmp(contact->addr, other_addr)==0) { addr_are_equal = 1; } } @@ -3642,7 +3642,7 @@ size_t dc_get_real_msg_cnt(dc_context_t* context) sqlite3_stmt* stmt = NULL; size_t ret = 0; - if( context->sql->cobj==NULL ) { + if (context->sql->cobj==NULL) { goto cleanup; } @@ -3653,7 +3653,7 @@ size_t dc_get_real_msg_cnt(dc_context_t* context) " WHERE m.id>" DC_STRINGIFY(DC_MSG_ID_LAST_SPECIAL) " AND m.chat_id>" DC_STRINGIFY(DC_CHAT_ID_LAST_SPECIAL) " AND c.blocked=0;"); - if( sqlite3_step(stmt) != SQLITE_ROW ) { + if (sqlite3_step(stmt) != SQLITE_ROW) { dc_sqlite3_log_error(context->sql, "dc_get_real_msg_cnt() failed."); goto cleanup; } @@ -3671,13 +3671,13 @@ size_t dc_get_deaddrop_msg_cnt(dc_context_t* context) sqlite3_stmt* stmt = NULL; size_t ret = 0; - if( context==NULL || context->magic != DC_CONTEXT_MAGIC || context->sql->cobj==NULL ) { + if (context==NULL || context->magic != DC_CONTEXT_MAGIC || context->sql->cobj==NULL) { goto cleanup; } stmt = dc_sqlite3_prepare(context->sql, "SELECT COUNT(*) FROM msgs m LEFT JOIN chats c ON c.id=m.chat_id WHERE c.blocked=2;"); - if( sqlite3_step(stmt) != SQLITE_ROW ) { + if (sqlite3_step(stmt) != SQLITE_ROW) { goto cleanup; } @@ -3695,14 +3695,14 @@ int dc_rfc724_mid_cnt(dc_context_t* context, const char* rfc724_mid) int ret = 0; sqlite3_stmt* stmt = NULL; - if( context==NULL || context->magic != DC_CONTEXT_MAGIC || context->sql->cobj==NULL ) { + if (context==NULL || context->magic != DC_CONTEXT_MAGIC || context->sql->cobj==NULL) { goto cleanup; } stmt = dc_sqlite3_prepare(context->sql, "SELECT COUNT(*) FROM msgs WHERE rfc724_mid=?;"); sqlite3_bind_text(stmt, 1, rfc724_mid, -1, SQLITE_STATIC); - if( sqlite3_step(stmt) != SQLITE_ROW ) { + if (sqlite3_step(stmt) != SQLITE_ROW) { goto cleanup; } @@ -3722,14 +3722,14 @@ uint32_t dc_rfc724_mid_exists(dc_context_t* context, const char* rfc724_mid, cha sqlite3_stmt* stmt = dc_sqlite3_prepare(context->sql, "SELECT server_folder, server_uid, id FROM msgs WHERE rfc724_mid=?;"); sqlite3_bind_text(stmt, 1, rfc724_mid, -1, SQLITE_STATIC); - if( sqlite3_step(stmt) != SQLITE_ROW ) { - if( ret_server_folder ) { *ret_server_folder = NULL; } - if( ret_server_uid ) { *ret_server_uid = 0; } + if (sqlite3_step(stmt) != SQLITE_ROW) { + if (ret_server_folder) { *ret_server_folder = NULL; } + if (ret_server_uid) { *ret_server_uid = 0; } goto cleanup; } - if( ret_server_folder ) { *ret_server_folder = dc_strdup((char*)sqlite3_column_text(stmt, 0)); } - if( ret_server_uid ) { *ret_server_uid = sqlite3_column_int(stmt, 1); /* may be 0 */ } + if (ret_server_folder) { *ret_server_folder = dc_strdup((char*)sqlite3_column_text(stmt, 0)); } + if (ret_server_uid) { *ret_server_uid = sqlite3_column_int(stmt, 1); /* may be 0 */ } ret = sqlite3_column_int(stmt, 2); cleanup: @@ -3765,18 +3765,18 @@ dc_msg_t* dc_get_msg(dc_context_t* context, uint32_t msg_id) int success = 0; dc_msg_t* obj = dc_msg_new(); - if( context == NULL || context->magic != DC_CONTEXT_MAGIC ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC) { goto cleanup; } - if( !dc_msg_load_from_db(obj, context, msg_id) ) { + if (!dc_msg_load_from_db(obj, context, msg_id)) { goto cleanup; } success = 1; cleanup: - if( success ) { + if (success) { return obj; } else { @@ -3812,7 +3812,7 @@ char* dc_get_msg_info(dc_context_t* context, uint32_t msg_id) char *rawtxt = NULL, *p; dc_strbuilder_init(&ret, 0); - if( context == NULL || context->magic != DC_CONTEXT_MAGIC ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC) { goto cleanup; } @@ -3822,7 +3822,7 @@ char* dc_get_msg_info(dc_context_t* context, uint32_t msg_id) stmt = dc_sqlite3_prepare(context->sql, "SELECT txt_raw FROM msgs WHERE id=?;"); sqlite3_bind_int(stmt, 1, msg_id); - if( sqlite3_step(stmt) != SQLITE_ROW ) { + if (sqlite3_step(stmt) != SQLITE_ROW) { p = dc_mprintf("Cannot load message #%i.", (int)msg_id); dc_strbuilder_cat(&ret, p); free(p); goto cleanup; } @@ -3832,7 +3832,7 @@ char* dc_get_msg_info(dc_context_t* context, uint32_t msg_id) #ifdef __ANDROID__ p = strchr(rawtxt, '\n'); - if( p ) { + if (p) { char* subject = rawtxt; *p = 0; p++; @@ -3849,13 +3849,13 @@ char* dc_get_msg_info(dc_context_t* context, uint32_t msg_id) p = dc_timestamp_to_str(dc_msg_get_timestamp(msg)); dc_strbuilder_cat(&ret, p); free(p); dc_strbuilder_cat(&ret, "\n"); - if( msg->from_id != DC_CONTACT_ID_SELF ) { + if (msg->from_id != DC_CONTACT_ID_SELF) { dc_strbuilder_cat(&ret, "Received: "); p = dc_timestamp_to_str(msg->timestamp_rcvd? msg->timestamp_rcvd : msg->timestamp); dc_strbuilder_cat(&ret, p); free(p); dc_strbuilder_cat(&ret, "\n"); } - if( msg->from_id == DC_CONTACT_ID_DEVICE || msg->to_id == DC_CONTACT_ID_DEVICE ) { + if (msg->from_id == DC_CONTACT_ID_DEVICE || msg->to_id == DC_CONTACT_ID_DEVICE) { goto cleanup; // device-internal message, no further details needed } @@ -3863,7 +3863,7 @@ char* dc_get_msg_info(dc_context_t* context, uint32_t msg_id) stmt = dc_sqlite3_prepare(context->sql, "SELECT contact_id, timestamp_sent FROM msgs_mdns WHERE msg_id=?;"); sqlite3_bind_int (stmt, 1, msg_id); - while( sqlite3_step(stmt) == SQLITE_ROW ) { + while (sqlite3_step(stmt) == SQLITE_ROW) { dc_strbuilder_cat(&ret, "Read: "); p = dc_timestamp_to_str(sqlite3_column_int64(stmt, 1)); dc_strbuilder_cat(&ret, p); free(p); dc_strbuilder_cat(&ret, " by "); @@ -3879,7 +3879,7 @@ char* dc_get_msg_info(dc_context_t* context, uint32_t msg_id) /* add state */ p = NULL; - switch( msg->state ) { + switch (msg->state) { case DC_STATE_IN_FRESH: p = dc_strdup("Fresh"); break; case DC_STATE_IN_NOTICED: p = dc_strdup("Noticed"); break; case DC_STATE_IN_SEEN: p = dc_strdup("Seen"); break; @@ -3894,23 +3894,23 @@ char* dc_get_msg_info(dc_context_t* context, uint32_t msg_id) p = NULL; int e2ee_errors; - if( (e2ee_errors=dc_param_get_int(msg->param, DC_PARAM_ERRONEOUS_E2EE, 0)) ) { - if( e2ee_errors&DC_E2EE_NO_VALID_SIGNATURE ) { + if ((e2ee_errors=dc_param_get_int(msg->param, DC_PARAM_ERRONEOUS_E2EE, 0))) { + if (e2ee_errors&DC_E2EE_NO_VALID_SIGNATURE) { p = dc_strdup("Encrypted, no valid signature"); } } - else if( dc_param_get_int(msg->param, DC_PARAM_GUARANTEE_E2EE, 0) ) { + else if (dc_param_get_int(msg->param, DC_PARAM_GUARANTEE_E2EE, 0)) { p = dc_strdup("Encrypted"); } - if( p ) { + if (p) { dc_strbuilder_catf(&ret, ", %s", p); free(p); } dc_strbuilder_cat(&ret, "\n"); /* add sender (only for info messages as the avatar may not be shown for them) */ - if( dc_msg_is_info(msg) ) { + if (dc_msg_is_info(msg)) { dc_strbuilder_cat(&ret, "Sender: "); p = dc_contact_get_name_n_addr(contact_from); dc_strbuilder_cat(&ret, p); free(p); dc_strbuilder_cat(&ret, "\n"); @@ -3918,13 +3918,13 @@ char* dc_get_msg_info(dc_context_t* context, uint32_t msg_id) /* add file info */ char* file = dc_param_get(msg->param, DC_PARAM_FILE, NULL); - if( file ) { + if (file) { p = dc_mprintf("\nFile: %s, %i bytes\n", file, (int)dc_get_filebytes(file)); dc_strbuilder_cat(&ret, p); free(p); } - if( msg->type != DC_MSG_TEXT ) { + if (msg->type != DC_MSG_TEXT) { p = NULL; - switch( msg->type ) { + switch (msg->type) { case DC_MSG_AUDIO: p = dc_strdup("Audio"); break; case DC_MSG_FILE: p = dc_strdup("File"); break; case DC_MSG_GIF: p = dc_strdup("GIF"); break; @@ -3938,17 +3938,17 @@ char* dc_get_msg_info(dc_context_t* context, uint32_t msg_id) } int w = dc_param_get_int(msg->param, DC_PARAM_WIDTH, 0), h = dc_param_get_int(msg->param, DC_PARAM_HEIGHT, 0); - if( w != 0 || h != 0 ) { + if (w != 0 || h != 0) { p = dc_mprintf("Dimension: %i x %i\n", w, h); dc_strbuilder_cat(&ret, p); free(p); } int duration = dc_param_get_int(msg->param, DC_PARAM_DURATION, 0); - if( duration != 0 ) { + if (duration != 0) { p = dc_mprintf("Duration: %i ms\n", duration); dc_strbuilder_cat(&ret, p); free(p); } /* add rawtext */ - if( rawtxt && rawtxt[0] ) { + if (rawtxt && rawtxt[0]) { dc_strbuilder_cat(&ret, "\n"); dc_strbuilder_cat(&ret, rawtxt); dc_strbuilder_cat(&ret, "\n"); @@ -3959,11 +3959,11 @@ char* dc_get_msg_info(dc_context_t* context, uint32_t msg_id) dc_strbuilder_cat(&ret, ""); #endif - if( msg->rfc724_mid && msg->rfc724_mid[0] ) { + if (msg->rfc724_mid && msg->rfc724_mid[0]) { dc_strbuilder_catf(&ret, "\nMessage-ID: %s", msg->rfc724_mid); } - if( msg->server_folder && msg->server_folder[0] ) { + if (msg->server_folder && msg->server_folder[0]) { dc_strbuilder_catf(&ret, "\nLast seen as: %s/%i", msg->server_folder, (int)msg->server_uid); } @@ -4001,7 +4001,7 @@ void dc_forward_msgs(dc_context_t* context, const uint32_t* msg_ids, int msg_cnt sqlite3_stmt* stmt = NULL; time_t curr_timestamp; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC || msg_ids==NULL || msg_cnt <= 0 || chat_id <= DC_CHAT_ID_LAST_SPECIAL ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC || msg_ids==NULL || msg_cnt <= 0 || chat_id <= DC_CHAT_ID_LAST_SPECIAL) { goto cleanup; } @@ -4012,7 +4012,7 @@ void dc_forward_msgs(dc_context_t* context, const uint32_t* msg_ids, int msg_cnt context->smtp->log_connect_errors = 1; - if( !dc_chat_load_from_db(chat, chat_id) ) { + if (!dc_chat_load_from_db(chat, chat_id)) { goto cleanup; } @@ -4021,10 +4021,10 @@ void dc_forward_msgs(dc_context_t* context, const uint32_t* msg_ids, int msg_cnt idsstr = dc_arr_to_string(msg_ids, msg_cnt); q3 = sqlite3_mprintf("SELECT id FROM msgs WHERE id IN(%s) ORDER BY timestamp,id", idsstr); stmt = dc_sqlite3_prepare(context->sql, q3); - while( sqlite3_step(stmt)==SQLITE_ROW ) + while (sqlite3_step(stmt)==SQLITE_ROW) { int src_msg_id = sqlite3_column_int(stmt, 0); - if( !dc_msg_load_from_db(msg, context, src_msg_id) ) { + if (!dc_msg_load_from_db(msg, context, src_msg_id)) { goto cleanup; } @@ -4041,10 +4041,10 @@ void dc_forward_msgs(dc_context_t* context, const uint32_t* msg_ids, int msg_cnt transaction_pending = 0; cleanup: - if( transaction_pending ) { dc_sqlite3_rollback(context->sql); } - if( created_db_entries ) { + if (transaction_pending) { dc_sqlite3_rollback(context->sql); } + if (created_db_entries) { size_t i, icnt = carray_count(created_db_entries); - for( i = 0; i < icnt; i += 2 ) { + for (i = 0; i < icnt; i += 2) { context->cb(context, DC_EVENT_MSGS_CHANGED, (uintptr_t)carray_get(created_db_entries, i), (uintptr_t)carray_get(created_db_entries, i+1)); } carray_free(created_db_entries); @@ -4074,7 +4074,7 @@ void dc_star_msgs(dc_context_t* context, const uint32_t* msg_ids, int msg_cnt, i { int i; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC || msg_ids == NULL || msg_cnt <= 0 || (star!=0 && star!=1) ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC || msg_ids == NULL || msg_cnt <= 0 || (star!=0 && star!=1)) { return; } @@ -4082,7 +4082,7 @@ void dc_star_msgs(dc_context_t* context, const uint32_t* msg_ids, int msg_cnt, i sqlite3_stmt* stmt = dc_sqlite3_prepare(context->sql, "UPDATE msgs SET starred=? WHERE id=?;"); - for( i = 0; i < msg_cnt; i++ ) + for (i = 0; i < msg_cnt; i++) { sqlite3_reset(stmt); sqlite3_bind_int(stmt, 1, star); @@ -4114,13 +4114,13 @@ void dc_delete_msgs(dc_context_t* context, const uint32_t* msg_ids, int msg_cnt) { int i; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC || msg_ids == NULL || msg_cnt <= 0 ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC || msg_ids == NULL || msg_cnt <= 0) { return; } dc_sqlite3_begin_transaction(context->sql); - for( i = 0; i < msg_cnt; i++ ) + for (i = 0; i < msg_cnt; i++) { dc_update_msg_chat_id(context, msg_ids[i], DC_CHAT_ID_TRASH); dc_job_add(context, DC_JOB_DELETE_MSG_ON_IMAP, msg_ids[i], NULL, 0); @@ -4156,7 +4156,7 @@ void dc_markseen_msgs(dc_context_t* context, const uint32_t* msg_ids, int msg_cn int curr_blocked = 0; sqlite3_stmt* stmt = NULL; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC || msg_ids == NULL || msg_cnt <= 0 ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC || msg_ids == NULL || msg_cnt <= 0) { goto cleanup; } @@ -4168,18 +4168,18 @@ void dc_markseen_msgs(dc_context_t* context, const uint32_t* msg_ids, int msg_cn " FROM msgs m " " LEFT JOIN chats c ON c.id=m.chat_id " " WHERE m.id=? AND m.chat_id>" DC_STRINGIFY(DC_CHAT_ID_LAST_SPECIAL)); - for( i = 0; i < msg_cnt; i++ ) + for (i = 0; i < msg_cnt; i++) { sqlite3_reset(stmt); sqlite3_bind_int(stmt, 1, msg_ids[i]); - if( sqlite3_step(stmt) != SQLITE_ROW ) { + if (sqlite3_step(stmt) != SQLITE_ROW) { continue; } curr_state = sqlite3_column_int(stmt, 0); curr_blocked = sqlite3_column_int(stmt, 1); - if( curr_blocked == 0 ) + if (curr_blocked == 0) { - if( curr_state == DC_STATE_IN_FRESH || curr_state == DC_STATE_IN_NOTICED ) { + if (curr_state == DC_STATE_IN_FRESH || curr_state == DC_STATE_IN_NOTICED) { dc_update_msg_state(context, msg_ids[i], DC_STATE_IN_SEEN); dc_log_info(context, 0, "Seen message #%i.", msg_ids[i]); dc_job_add(context, DC_JOB_MARKSEEN_MSG_ON_IMAP, msg_ids[i], NULL, 0); /* results in a call to dc_markseen_msg_on_imap() */ @@ -4189,7 +4189,7 @@ void dc_markseen_msgs(dc_context_t* context, const uint32_t* msg_ids, int msg_cn else { /* message may be in contact requests, mark as NOTICED, this does not force IMAP updated nor send MDNs */ - if( curr_state == DC_STATE_IN_FRESH ) { + if (curr_state == DC_STATE_IN_FRESH) { dc_update_msg_state(context, msg_ids[i], DC_STATE_IN_NOTICED); send_event = 1; } @@ -4200,12 +4200,12 @@ void dc_markseen_msgs(dc_context_t* context, const uint32_t* msg_ids, int msg_cn transaction_pending = 0; /* the event is needed eg. to remove the deaddrop from the chatlist */ - if( send_event ) { + if (send_event) { context->cb(context, DC_EVENT_MSGS_CHANGED, 0, 0); } cleanup: - if( transaction_pending ) { dc_sqlite3_rollback(context->sql); } + if (transaction_pending) { dc_sqlite3_rollback(context->sql); } sqlite3_finalize(stmt); } @@ -4216,8 +4216,8 @@ int dc_mdn_from_ext(dc_context_t* context, uint32_t from_id, const char* rfc724_ int read_by_all = 0; sqlite3_stmt* stmt = NULL; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC || from_id <= DC_CONTACT_ID_LAST_SPECIAL || rfc724_mid == NULL || ret_chat_id==NULL || ret_msg_id==NULL - || *ret_chat_id != 0 || *ret_msg_id != 0 ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC || from_id <= DC_CONTACT_ID_LAST_SPECIAL || rfc724_mid == NULL || ret_chat_id==NULL || ret_msg_id==NULL + || *ret_chat_id != 0 || *ret_msg_id != 0) { goto cleanup; } @@ -4227,7 +4227,7 @@ int dc_mdn_from_ext(dc_context_t* context, uint32_t from_id, const char* rfc724_ " WHERE rfc724_mid=? AND from_id=1 " " ORDER BY m.id;"); /* the ORDER BY makes sure, if one rfc724_mid is splitted into its parts, we always catch the same one. However, we do not send multiparts, we do not request MDNs for multiparts, and should not receive read requests for multiparts. So this is currently more theoretical. */ sqlite3_bind_text(stmt, 1, rfc724_mid, -1, SQLITE_STATIC); - if( sqlite3_step(stmt) != SQLITE_ROW ) { + if (sqlite3_step(stmt) != SQLITE_ROW) { goto cleanup; } *ret_msg_id = sqlite3_column_int(stmt, 0); @@ -4237,7 +4237,7 @@ int dc_mdn_from_ext(dc_context_t* context, uint32_t from_id, const char* rfc724_ sqlite3_finalize(stmt); stmt = NULL; - if( msg_state!=DC_STATE_OUT_PENDING && msg_state!=DC_STATE_OUT_DELIVERED ) { + if (msg_state!=DC_STATE_OUT_PENDING && msg_state!=DC_STATE_OUT_DELIVERED) { goto cleanup; /* eg. already marked as MDNS_RCVD. however, it is importent, that the message ID is set above as this will allow the caller eg. to move the message away */ } @@ -4250,7 +4250,7 @@ int dc_mdn_from_ext(dc_context_t* context, uint32_t from_id, const char* rfc724_ sqlite3_finalize(stmt); stmt = NULL; - if( !mdn_already_in_table ) { + if (!mdn_already_in_table) { stmt = dc_sqlite3_prepare(context->sql, "INSERT INTO msgs_mdns (msg_id, contact_id, timestamp_sent) VALUES (?, ?, ?);"); sqlite3_bind_int (stmt, 1, *ret_msg_id); @@ -4262,7 +4262,7 @@ int dc_mdn_from_ext(dc_context_t* context, uint32_t from_id, const char* rfc724_ } // Normal chat? that's quite easy. - if( chat_type == DC_CHAT_TYPE_SINGLE ) { + if (chat_type == DC_CHAT_TYPE_SINGLE) { dc_update_msg_state(context, *ret_msg_id, DC_STATE_OUT_MDN_RCVD); read_by_all = 1; goto cleanup; /* send event about new state */ @@ -4272,7 +4272,7 @@ int dc_mdn_from_ext(dc_context_t* context, uint32_t from_id, const char* rfc724_ stmt = dc_sqlite3_prepare(context->sql, "SELECT COUNT(*) FROM msgs_mdns WHERE msg_id=?;"); sqlite3_bind_int(stmt, 1, *ret_msg_id); - if( sqlite3_step(stmt) != SQLITE_ROW ) { + if (sqlite3_step(stmt) != SQLITE_ROW) { goto cleanup; /* error */ } int ist_cnt = sqlite3_column_int(stmt, 0); @@ -4292,7 +4292,7 @@ int dc_mdn_from_ext(dc_context_t* context, uint32_t from_id, const char* rfc724_ (S=Sender, R=Recipient) */ int soll_cnt = (dc_get_chat_contact_count(context, *ret_chat_id)+1/*for rounding, SELF is already included!*/) / 2; - if( ist_cnt < soll_cnt ) { + if (ist_cnt < soll_cnt) { goto cleanup; /* wait for more receipts */ } diff --git a/src/dc_dehtml.c b/src/dc_dehtml.c index e8eb8d40..181186f9 100644 --- a/src/dc_dehtml.c +++ b/src/dc_dehtml.c @@ -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
\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 { diff --git a/src/dc_e2ee.c b/src/dc_e2ee.c index 2e2e4efc..0203bbfb 100644 --- a/src/dc_e2ee.c +++ b/src/dc_e2ee.c @@ -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, ¤t_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); diff --git a/src/dc_hash.c b/src/dc_hash.c index 2cd5e29b..be9ab666 100644 --- a/src/dc_hash.c +++ b/src/dc_hash.c @@ -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 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; diff --git a/src/dc_imap.c b/src/dc_imap.c index 0f85e901..d5db8d18 100644 --- a/src/dc_imap.c +++ b/src/dc_imap.c @@ -42,15 +42,15 @@ static void unsetup_handle (dc_imap_t*); static int is_error(dc_imap_t* imap, int code) { - if( code == MAILIMAP_NO_ERROR /*0*/ + if (code == MAILIMAP_NO_ERROR /*0*/ || code == MAILIMAP_NO_ERROR_AUTHENTICATED /*1*/ - || code == MAILIMAP_NO_ERROR_NON_AUTHENTICATED /*2*/ ) + || code == MAILIMAP_NO_ERROR_NON_AUTHENTICATED /*2*/) { return 0; } - if( code == MAILIMAP_ERROR_STREAM /*4*/ - || code == MAILIMAP_ERROR_PARSE /*5*/ ) + if (code == MAILIMAP_ERROR_STREAM /*4*/ + || code == MAILIMAP_ERROR_PARSE /*5*/) { dc_log_info(imap->context, 0, "IMAP stream lost; we'll reconnect soon."); imap->should_reconnect = 1; @@ -98,17 +98,17 @@ static void get_config_lastseenuid(dc_imap_t* imap, const char* folder, uint32_t char* key = dc_mprintf("imap.mailbox.%s", folder); char* val1 = imap->get_config(imap, key, NULL), *val2 = NULL, *val3 = NULL; - if( val1 ) + if (val1) { /* the entry has the format `imap.mailbox.=:` */ val2 = strchr(val1, ':'); - if( val2 ) + if (val2) { *val2 = 0; val2++; val3 = strchr(val2, ':'); - if( val3 ) { *val3 = 0; /* ignore everything bethind an optional second colon to allow future enhancements */ } + if (val3) { *val3 = 0; /* ignore everything bethind an optional second colon to allow future enhancements */ } *uidvalidity = atol(val1); *lastseenuid = atol(val2); @@ -144,31 +144,31 @@ static int get_folder_meaning(const dc_imap_t* imap, struct mailimap_mbx_list_fl char* lower = NULL; int ret_meaning = MEANING_NORMAL; - if( !force_fallback && (imap->has_xlist || flags != NULL) ) + if (!force_fallback && (imap->has_xlist || flags != NULL)) { /* We check for flags if we get some (LIST may also return some, see https://tools.ietf.org/html/rfc6154 ) or if has_xlist is set. However, we also allow a NULL-pointer for "no flags" if has_xlist is true. */ - if( flags && flags->mbf_oflags ) + if (flags && flags->mbf_oflags) { clistiter* iter2; - for( iter2=clist_begin(flags->mbf_oflags); iter2!=NULL; iter2=clist_next(iter2) ) + for (iter2=clist_begin(flags->mbf_oflags); iter2!=NULL; iter2=clist_next(iter2)) { struct mailimap_mbx_list_oflag* oflag = (struct mailimap_mbx_list_oflag*)clist_content(iter2); - switch( oflag->of_type ) + switch (oflag->of_type) { case MAILIMAP_MBX_LIST_OFLAG_FLAG_EXT: - if( strcasecmp(oflag->of_flag_ext, "spam")==0 + if (strcasecmp(oflag->of_flag_ext, "spam")==0 || strcasecmp(oflag->of_flag_ext, "trash")==0 || strcasecmp(oflag->of_flag_ext, "drafts")==0 - || strcasecmp(oflag->of_flag_ext, "junk")==0 ) + || strcasecmp(oflag->of_flag_ext, "junk")==0) { ret_meaning = MEANING_IGNORE; } - else if( strcasecmp(oflag->of_flag_ext, "sent")==0 ) + else if (strcasecmp(oflag->of_flag_ext, "sent")==0) { ret_meaning = MEANING_SENT_OBJECTS; } - else if( strcasecmp(oflag->of_flag_ext, "inbox")==0 ) + else if (strcasecmp(oflag->of_flag_ext, "inbox")==0) { ret_meaning = MEANING_INBOX; } @@ -177,7 +177,7 @@ static int get_folder_meaning(const dc_imap_t* imap, struct mailimap_mbx_list_fl } } - if( ret_meaning == MEANING_NORMAL && strcasecmp(folder_name, "INBOX") == 0 ) { + if (ret_meaning == MEANING_NORMAL && strcasecmp(folder_name, "INBOX") == 0) { ret_meaning = MEANING_INBOX; } } @@ -185,7 +185,7 @@ static int get_folder_meaning(const dc_imap_t* imap, struct mailimap_mbx_list_fl { /* we have no flag list; try some known default names */ lower = dc_strlower(folder_name); - if( strcmp(lower, "spam") == 0 + if (strcmp(lower, "spam") == 0 || strcmp(lower, "junk") == 0 || strcmp(lower, "indésirables") == 0 /* fr */ @@ -202,15 +202,15 @@ static int get_folder_meaning(const dc_imap_t* imap, struct mailimap_mbx_list_fl || strcmp(lower, "brouillons") == 0 /* fr */ || strcmp(lower, "borradores") == 0 /* es */ || strcmp(lower, "utkast") == 0 /* sv */ - ) + ) { ret_meaning = MEANING_IGNORE; } - else if( strcmp(lower, "inbox") == 0 ) /* the "INBOX" foldername is IMAP-standard */ + else if (strcmp(lower, "inbox") == 0) /* the "INBOX" foldername is IMAP-standard */ { ret_meaning = MEANING_INBOX; } - else if( strcmp(lower, "sent")==0 || strcmp(lower, "sent objects")==0 || strcmp(lower, "gesendet")==0 ) + else if (strcmp(lower, "sent")==0 || strcmp(lower, "sent objects")==0 || strcmp(lower, "gesendet")==0) { ret_meaning = MEANING_SENT_OBJECTS; } @@ -236,34 +236,34 @@ static clist* list_folders(dc_imap_t* imap) clist * ret_list = clist_new(); int r, xlist_works = 0; - if( imap==NULL || imap->hEtpan==NULL ) { + if (imap==NULL || imap->hEtpan==NULL) { goto cleanup; } /* the "*" not only gives us the folders from the main directory, but also all subdirectories; so the resulting foldernames may contain delimiters as "folder/subdir/subsubdir" etc. However, as we do not really use folders, this is just fine (otherwise we'd implement this functinon recursively. */ - if( imap->has_xlist ) { + if (imap->has_xlist) { r = mailimap_xlist(imap->hEtpan, "", "*", &imap_list); } else { r = mailimap_list(imap->hEtpan, "", "*", &imap_list); } - if( is_error(imap, r) || imap_list==NULL ) { + if (is_error(imap, r) || imap_list==NULL) { imap_list = NULL; dc_log_warning(imap->context, 0, "Cannot get folder list."); goto cleanup; } - if( clist_count(imap_list)<=0 ) { + if (clist_count(imap_list)<=0) { dc_log_warning(imap->context, 0, "Folder list is empty."); goto cleanup; } //default IMAP delimiter if none is returned by the list command imap->imap_delimiter = '.'; - for( iter1 = clist_begin(imap_list); iter1 != NULL ; iter1 = clist_next(iter1) ) + for (iter1 = clist_begin(imap_list); iter1 != NULL ; iter1 = clist_next(iter1)) { struct mailimap_mailbox_list* imap_folder = (struct mailimap_mailbox_list*)clist_content(iter1); if (imap_folder->mb_delimiter) { @@ -273,7 +273,7 @@ static clist* list_folders(dc_imap_t* imap) dc_imapfolder_t* ret_folder = calloc(1, sizeof(dc_imapfolder_t)); - if( strcasecmp(imap_folder->mb_name, "INBOX")==0 ) { + if (strcasecmp(imap_folder->mb_name, "INBOX")==0) { /* Force upper case INBOX as we also use it directly this way; a unified name is needed as we use the folder name to remember the last uid. Servers may return any case, however, all variants MUST lead to the same INBOX, see RFC 3501 5.1 */ ret_folder->name_to_select = dc_strdup("INBOX"); @@ -285,7 +285,7 @@ static clist* list_folders(dc_imap_t* imap) ret_folder->name_utf8 = dc_decode_modified_utf7(imap_folder->mb_name, 0); ret_folder->meaning = get_folder_meaning(imap, imap_folder->mb_flag, ret_folder->name_utf8, false); - if( ret_folder->meaning == MEANING_IGNORE || ret_folder->meaning == MEANING_SENT_OBJECTS /*MEANING_INBOX is no hint for a working XLIST*/ ) { + if (ret_folder->meaning == MEANING_IGNORE || ret_folder->meaning == MEANING_SENT_OBJECTS /*MEANING_INBOX is no hint for a working XLIST*/) { xlist_works = 1; } @@ -294,8 +294,8 @@ static clist* list_folders(dc_imap_t* imap) /* at least my own server claims that it support XLIST but does not return folder flags. So, if we did not get a single flag, fall back to the default behaviour */ - if( !xlist_works ) { - for( iter1 = clist_begin(ret_list); iter1 != NULL ; iter1 = clist_next(iter1) ) + if (!xlist_works) { + for (iter1 = clist_begin(ret_list); iter1 != NULL ; iter1 = clist_next(iter1)) { dc_imapfolder_t* ret_folder = (struct dc_imapfolder_t*)clist_content(iter1); ret_folder->meaning = get_folder_meaning(imap, NULL, ret_folder->name_utf8, true); @@ -303,7 +303,7 @@ static clist* list_folders(dc_imap_t* imap) } cleanup: - if( imap_list ) { + if (imap_list) { mailimap_list_result_free(imap_list); } return ret_list; @@ -312,9 +312,9 @@ cleanup: static void free_folders(clist* folders) { - if( folders ) { + if (folders) { clistiter* iter1; - for( iter1 = clist_begin(folders); iter1 != NULL ; iter1 = clist_next(iter1) ) { + for (iter1 = clist_begin(folders); iter1 != NULL ; iter1 = clist_next(iter1)) { dc_imapfolder_t* ret_folder = (struct dc_imapfolder_t*)clist_content(iter1); free(ret_folder->name_to_select); free(ret_folder->name_utf8); @@ -332,11 +332,11 @@ static int init_chat_folders(dc_imap_t* imap) clistiter* iter1; char *normal_folder = NULL, *sent_folder = NULL, *chats_folder = NULL; - if( imap==NULL || imap->hEtpan==NULL ) { + if (imap==NULL || imap->hEtpan==NULL) { goto cleanup; } - if( imap->sent_folder && imap->sent_folder[0] ) { + if (imap->sent_folder && imap->sent_folder[0]) { success = 1; goto cleanup; } @@ -353,27 +353,27 @@ static int init_chat_folders(dc_imap_t* imap) char fallback_folder[64]; snprintf(fallback_folder, sizeof(fallback_folder), "INBOX%c%s", imap->imap_delimiter, DC_CHATS_FOLDER); - for( iter1 = clist_begin(folder_list); iter1 != NULL ; iter1 = clist_next(iter1) ) { + for (iter1 = clist_begin(folder_list); iter1 != NULL ; iter1 = clist_next(iter1)) { dc_imapfolder_t* folder = (struct dc_imapfolder_t*)clist_content(iter1); - if( strcmp(folder->name_utf8, DC_CHATS_FOLDER)==0 || strcmp(folder->name_utf8, fallback_folder)==0 ) { + if (strcmp(folder->name_utf8, DC_CHATS_FOLDER)==0 || strcmp(folder->name_utf8, fallback_folder)==0) { chats_folder = dc_strdup(folder->name_to_select); break; } - else if( folder->meaning == MEANING_SENT_OBJECTS ) { + else if (folder->meaning == MEANING_SENT_OBJECTS) { sent_folder = dc_strdup(folder->name_to_select); } - else if( folder->meaning == MEANING_NORMAL && normal_folder == NULL ) { + else if (folder->meaning == MEANING_NORMAL && normal_folder == NULL) { normal_folder = dc_strdup(folder->name_to_select); } } - if( chats_folder == NULL && (imap->server_flags&DC_NO_MOVE_TO_CHATS)==0 ) { + if (chats_folder == NULL && (imap->server_flags&DC_NO_MOVE_TO_CHATS)==0) { dc_log_info(imap->context, 0, "Creating IMAP-folder \"%s\"...", DC_CHATS_FOLDER); int r = mailimap_create(imap->hEtpan, DC_CHATS_FOLDER); - if( is_error(imap, r) ) { + if (is_error(imap, r)) { dc_log_warning(imap->context, 0, "Cannot create IMAP-folder, using trying INBOX subfolder."); r = mailimap_create(imap->hEtpan, fallback_folder); - if( is_error(imap, r) ) { + if (is_error(imap, r)) { /* continue on errors, we'll just use a different folder then */ dc_log_warning(imap->context, 0, "Cannot create IMAP-folder, using default."); } @@ -390,21 +390,21 @@ static int init_chat_folders(dc_imap_t* imap) /* Subscribe to the created folder. Otherwise, although a top-level folder, if clients use LSUB for listing, the created folder may be hidden. (we could also do this directly after creation, however, we forgot this in versions get_config(imap, "imap.subscribedToChats", NULL)==NULL ) { + if (chats_folder && imap->get_config(imap, "imap.subscribedToChats", NULL)==NULL) { mailimap_subscribe(imap->hEtpan, chats_folder); imap->set_config(imap, "imap.subscribedToChats", "1"); } - if( chats_folder ) { + if (chats_folder) { imap->moveto_folder = dc_strdup(chats_folder); imap->sent_folder = dc_strdup(chats_folder); success = 1; } - else if( sent_folder ) { + else if (sent_folder) { imap->sent_folder = dc_strdup(sent_folder); success = 1; } - else if( normal_folder ) { + else if (normal_folder) { imap->sent_folder = dc_strdup(normal_folder); success = 1; } @@ -420,11 +420,11 @@ cleanup: static int select_folder(dc_imap_t* imap, const char* folder /*may be NULL*/) { - if( imap == NULL ) { + if (imap == NULL) { return 0; } - if( imap->hEtpan==NULL ) { + if (imap->hEtpan==NULL) { imap->selected_folder[0] = 0; imap->selected_folder_needs_expunge = 0; return 0; @@ -432,13 +432,13 @@ static int select_folder(dc_imap_t* imap, const char* folder /*may be NULL*/) /* if there is a new folder and the new folder is equal to the selected one, there's nothing to do. if there is _no_ new folder, we continue as we might want to expunge below. */ - if( folder && strcmp(imap->selected_folder, folder)==0 ) { + if (folder && strcmp(imap->selected_folder, folder)==0) { return 1; } /* deselect existing folder, if needed (it's also done implicitly by SELECT, however, without EXPUNGE then) */ - if( imap->selected_folder_needs_expunge ) { - if( imap->selected_folder[0] ) { + if (imap->selected_folder_needs_expunge) { + if (imap->selected_folder[0]) { dc_log_info(imap->context, 0, "Expunge messages in \"%s\".", imap->selected_folder); mailimap_close(imap->hEtpan); /* a CLOSE-SELECT is considerably faster than an EXPUNGE-SELECT, see https://tools.ietf.org/html/rfc3501#section-6.4.2 */ } @@ -446,9 +446,9 @@ static int select_folder(dc_imap_t* imap, const char* folder /*may be NULL*/) } /* select new folder */ - if( folder ) { + if (folder) { int r = mailimap_select(imap->hEtpan, folder); - if( is_error(imap, r) || imap->hEtpan->imap_selection_info == NULL ) { + if (is_error(imap, r) || imap->hEtpan->imap_selection_info == NULL) { imap->selected_folder[0] = 0; return 0; } @@ -469,22 +469,22 @@ static uint32_t search_uid(dc_imap_t* imap, const char* message_id) clistiter *cur, *cur2; struct mailimap_search_key *key = mailimap_search_key_new_header(strdup("Message-ID"), dc_mprintf("<%s>", message_id)); uint32_t uid = 0; - for( cur = clist_begin(folders); cur != NULL ; cur = clist_next(cur) ) + for (cur = clist_begin(folders); cur != NULL ; cur = clist_next(cur)) { dc_imapfolder_t* folder = (dc_imapfolder_t*)clist_content(cur); - if( select_folder(imap, folder->name_to_select) ) + if (select_folder(imap, folder->name_to_select)) { int r = mailimap_uid_search(imap->hEtpan, "utf-8", key, &search_result); - if( !is_error(imap, r) && search_result ) { - if( (cur2=clist_begin(search_result)) != NULL ) { + if (!is_error(imap, r) && search_result) { + if ((cur2=clist_begin(search_result)) != NULL) { uint32_t* ptr_uid = (uint32_t *)clist_content(cur2); - if( ptr_uid ) { + if (ptr_uid) { uid = *ptr_uid; } } mailimap_search_result_free(search_result); search_result = NULL; - if( uid ) { + if (uid) { goto cleanup; } } @@ -492,8 +492,8 @@ static uint32_t search_uid(dc_imap_t* imap, const char* message_id) } cleanup: - if( search_result ) { mailimap_search_result_free(search_result); } - if( key ) { mailimap_search_key_free(key); } + if (search_result) { mailimap_search_result_free(search_result); } + if (key) { mailimap_search_key_free(key); } free_folders(folders); return uid; } @@ -508,14 +508,14 @@ static uint32_t peek_uid(struct mailimap_msg_att* msg_att) { /* search the UID in a list of attributes returned by a FETCH command */ clistiter* iter1; - for( iter1=clist_begin(msg_att->att_list); iter1!=NULL; iter1=clist_next(iter1) ) + for (iter1=clist_begin(msg_att->att_list); iter1!=NULL; iter1=clist_next(iter1)) { struct mailimap_msg_att_item* item = (struct mailimap_msg_att_item*)clist_content(iter1); - if( item ) + if (item) { - if( item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC ) + if (item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) { - if( item->att_data.att_static->att_type == MAILIMAP_MSG_ATT_UID ) + if (item->att_data.att_static->att_type == MAILIMAP_MSG_ATT_UID) { return item->att_data.att_static->att_data.att_uid; } @@ -532,9 +532,9 @@ static char* unquote_rfc724_mid(const char* in) /* remove < and > from the given message id */ char* out = dc_strdup(in); int out_len = strlen(out); - if( out_len > 2 ) { - if( out[0] == '<' ) { out[0] = ' '; } - if( out[out_len-1] == '>' ) { out[out_len-1] = ' '; } + if (out_len > 2) { + if (out[0] == '<') { out[0] = ' '; } + if (out[out_len-1] == '>') { out[out_len-1] = ' '; } dc_trim(out); } return out; @@ -543,23 +543,23 @@ static char* unquote_rfc724_mid(const char* in) static const char* peek_rfc724_mid(struct mailimap_msg_att* msg_att) { - if( msg_att == NULL ) { + if (msg_att == NULL) { return NULL; } /* search the UID in a list of attributes returned by a FETCH command */ clistiter* iter1; - for( iter1=clist_begin(msg_att->att_list); iter1!=NULL; iter1=clist_next(iter1) ) + for (iter1=clist_begin(msg_att->att_list); iter1!=NULL; iter1=clist_next(iter1)) { struct mailimap_msg_att_item* item = (struct mailimap_msg_att_item*)clist_content(iter1); - if( item ) + if (item) { - if( item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC ) + if (item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) { - if( item->att_data.att_static->att_type == MAILIMAP_MSG_ATT_ENVELOPE ) + if (item->att_data.att_static->att_type == MAILIMAP_MSG_ATT_ENVELOPE) { struct mailimap_envelope* env = item->att_data.att_static->att_data.att_env; - if( env && env->env_message_id ) { + if (env && env->env_message_id) { return env->env_message_id; } } @@ -574,30 +574,30 @@ static const char* peek_rfc724_mid(struct mailimap_msg_att* msg_att) static int peek_flag_keyword(struct mailimap_msg_att* msg_att, const char* flag_keyword) { /* search $MDNSent in a list of attributes returned by a FETCH command */ - if( msg_att == NULL || msg_att->att_list==NULL || flag_keyword == NULL ) { + if (msg_att == NULL || msg_att->att_list==NULL || flag_keyword == NULL) { return 0; } clistiter *iter1, *iter2; - for( iter1=clist_begin(msg_att->att_list); iter1!=NULL; iter1=clist_next(iter1) ) + for (iter1=clist_begin(msg_att->att_list); iter1!=NULL; iter1=clist_next(iter1)) { struct mailimap_msg_att_item* item = (struct mailimap_msg_att_item*)clist_content(iter1); - if( item ) + if (item) { - if( item->att_type == MAILIMAP_MSG_ATT_ITEM_DYNAMIC ) + if (item->att_type == MAILIMAP_MSG_ATT_ITEM_DYNAMIC) { - if( item->att_data.att_dyn->att_list /*I've seen NULL here ...*/ ) + if (item->att_data.att_dyn->att_list /*I've seen NULL here ...*/) { - for( iter2=clist_begin(item->att_data.att_dyn->att_list); iter2!=NULL ; iter2=clist_next(iter2)) + for (iter2=clist_begin(item->att_data.att_dyn->att_list); iter2!=NULL ; iter2=clist_next(iter2)) { struct mailimap_flag_fetch* flag_fetch =(struct mailimap_flag_fetch*) clist_content(iter2); - if( flag_fetch && flag_fetch->fl_type==MAILIMAP_FLAG_FETCH_OTHER ) + if (flag_fetch && flag_fetch->fl_type==MAILIMAP_FLAG_FETCH_OTHER) { struct mailimap_flag* flag = flag_fetch->fl_flag; - if( flag ) + if (flag) { - if( flag->fl_type == MAILIMAP_FLAG_KEYWORD && flag->fl_data.fl_keyword!=NULL - && strcmp(flag->fl_data.fl_keyword, flag_keyword)==0 ) { + if (flag->fl_type == MAILIMAP_FLAG_KEYWORD && flag->fl_data.fl_keyword!=NULL + && strcmp(flag->fl_data.fl_keyword, flag_keyword)==0) { return 1; /* flag found */ } } @@ -613,32 +613,32 @@ static int peek_flag_keyword(struct mailimap_msg_att* msg_att, const char* flag_ static void peek_body(struct mailimap_msg_att* msg_att, char** p_msg, size_t* p_msg_bytes, uint32_t* flags, int* deleted) { - if( msg_att == NULL ) { + if (msg_att == NULL) { return; } /* search body & Co. in a list of attributes returned by a FETCH command */ clistiter *iter1, *iter2; - for( iter1=clist_begin(msg_att->att_list); iter1!=NULL; iter1=clist_next(iter1) ) + for (iter1=clist_begin(msg_att->att_list); iter1!=NULL; iter1=clist_next(iter1)) { struct mailimap_msg_att_item* item = (struct mailimap_msg_att_item*)clist_content(iter1); - if( item ) + if (item) { - if( item->att_type == MAILIMAP_MSG_ATT_ITEM_DYNAMIC ) + if (item->att_type == MAILIMAP_MSG_ATT_ITEM_DYNAMIC) { - if( item->att_data.att_dyn->att_list /*I've seen NULL here ...*/ ) + if (item->att_data.att_dyn->att_list /*I've seen NULL here ...*/) { - for( iter2=clist_begin(item->att_data.att_dyn->att_list); iter2!=NULL ; iter2=clist_next(iter2)) + for (iter2=clist_begin(item->att_data.att_dyn->att_list); iter2!=NULL ; iter2=clist_next(iter2)) { struct mailimap_flag_fetch* flag_fetch =(struct mailimap_flag_fetch*) clist_content(iter2); - if( flag_fetch && flag_fetch->fl_type==MAILIMAP_FLAG_FETCH_OTHER ) + if (flag_fetch && flag_fetch->fl_type==MAILIMAP_FLAG_FETCH_OTHER) { struct mailimap_flag* flag = flag_fetch->fl_flag; - if( flag ) + if (flag) { - if( flag->fl_type == MAILIMAP_FLAG_SEEN ) { + if (flag->fl_type == MAILIMAP_FLAG_SEEN) { *flags |= DC_IMAP_SEEN; } - else if( flag->fl_type == MAILIMAP_FLAG_DELETED ) { + else if (flag->fl_type == MAILIMAP_FLAG_DELETED) { *deleted = 1; } } @@ -646,9 +646,9 @@ static void peek_body(struct mailimap_msg_att* msg_att, char** p_msg, size_t* p_ } } } - else if( item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC ) + else if (item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) { - if( item->att_data.att_static->att_type == MAILIMAP_MSG_ATT_BODY_SECTION ) + if (item->att_data.att_static->att_type == MAILIMAP_MSG_ATT_BODY_SECTION) { *p_msg = item->att_data.att_static->att_data.att_body_section->sec_body_part; *p_msg_bytes = item->att_data.att_static->att_data.att_body_section->sec_length; @@ -671,11 +671,11 @@ static int fetch_single_msg(dc_imap_t* imap, const char* folder, uint32_t server clist* fetch_result = NULL; clistiter* cur; - if( imap==NULL ) { + if (imap==NULL) { goto cleanup; } - if( imap->hEtpan==NULL ) { + if (imap->hEtpan==NULL) { goto cleanup; } @@ -686,23 +686,23 @@ static int fetch_single_msg(dc_imap_t* imap, const char* folder, uint32_t server mailimap_set_free(set); } - if( is_error(imap, r) || fetch_result == NULL ) { + if (is_error(imap, r) || fetch_result == NULL) { fetch_result = NULL; dc_log_warning(imap->context, 0, "Error #%i on fetching message #%i from folder \"%s\"; retry=%i.", (int)r, (int)server_uid, folder, (int)imap->should_reconnect); - if( imap->should_reconnect ) { + if (imap->should_reconnect) { retry_later = 1; /* maybe we should also retry on other errors, however, we should check this carefully, as this may result in a dead lock! */ } goto cleanup; /* this is an error that should be recovered; the caller should try over later to fetch the message again (if there is no such message, we simply get an empty result) */ } - if( (cur=clist_begin(fetch_result)) == NULL ) { + if ((cur=clist_begin(fetch_result)) == NULL) { dc_log_warning(imap->context, 0, "Message #%i does not exist in folder \"%s\".", (int)server_uid, folder); goto cleanup; /* server response is fine, however, there is no such message, do not try to fetch the message again */ } struct mailimap_msg_att* msg_att = (struct mailimap_msg_att*)clist_content(cur); peek_body(msg_att, &msg_content, &msg_bytes, &flags, &deleted); - if( msg_content == NULL || msg_bytes <= 0 || deleted ) { + if (msg_content == NULL || msg_bytes <= 0 || deleted) { /* dc_log_warning(imap->context, 0, "Message #%i in folder \"%s\" is empty or deleted.", (int)server_uid, folder); -- this is a quite usual situation, do not print a warning */ goto cleanup; } @@ -711,7 +711,7 @@ static int fetch_single_msg(dc_imap_t* imap, const char* folder, uint32_t server cleanup: - if( fetch_result ) { + if (fetch_result) { mailimap_fetch_list_free(fetch_result); } return retry_later? 0 : 1; @@ -728,32 +728,32 @@ static int fetch_from_single_folder(dc_imap_t* imap, const char* folder) clistiter* cur; struct mailimap_set* set; - if( imap==NULL ) { + if (imap==NULL) { goto cleanup; } - if( imap->hEtpan==NULL ) { + if (imap->hEtpan==NULL) { dc_log_info(imap->context, 0, "Cannot fetch from \"%s\" - not connected.", folder); goto cleanup; } - if( select_folder(imap, folder)==0 ) { + if (select_folder(imap, folder)==0) { dc_log_warning(imap->context, 0, "Cannot select folder \"%s\".", folder); goto cleanup; } /* compare last seen UIDVALIDITY against the current one */ get_config_lastseenuid(imap, folder, &uidvalidity, &lastseenuid); - if( uidvalidity != imap->hEtpan->imap_selection_info->sel_uidvalidity ) + if (uidvalidity != imap->hEtpan->imap_selection_info->sel_uidvalidity) { /* first time this folder is selected or UIDVALIDITY has changed, init lastseenuid and save it to config */ - if( imap->hEtpan->imap_selection_info->sel_uidvalidity <= 0 ) { + if (imap->hEtpan->imap_selection_info->sel_uidvalidity <= 0) { dc_log_error(imap->context, 0, "Cannot get UIDVALIDITY for folder \"%s\".", folder); goto cleanup; } - if( imap->hEtpan->imap_selection_info->sel_has_exists ) { - if( imap->hEtpan->imap_selection_info->sel_exists <= 0 ) { + if (imap->hEtpan->imap_selection_info->sel_has_exists) { + if (imap->hEtpan->imap_selection_info->sel_exists <= 0) { dc_log_info(imap->context, 0, "Folder \"%s\" is empty.", folder); goto cleanup; } @@ -769,7 +769,7 @@ static int fetch_from_single_folder(dc_imap_t* imap, const char* folder) r = mailimap_fetch(imap->hEtpan, set, imap->fetch_type_uid, &fetch_result); mailimap_set_free(set); - if( is_error(imap, r) || fetch_result==NULL || (cur=clist_begin(fetch_result))==NULL ) { + if (is_error(imap, r) || fetch_result==NULL || (cur=clist_begin(fetch_result))==NULL) { dc_log_info(imap->context, 0, "Empty result returned for folder \"%s\".", folder); goto cleanup; /* this might happen if the mailbox is empty an EXISTS does not work */ } @@ -778,13 +778,13 @@ static int fetch_from_single_folder(dc_imap_t* imap, const char* folder) lastseenuid = peek_uid(msg_att); mailimap_fetch_list_free(fetch_result); fetch_result = NULL; - if( lastseenuid <= 0 ) { + if (lastseenuid <= 0) { dc_log_error(imap->context, 0, "Cannot get largest UID for folder \"%s\"", folder); goto cleanup; } /* if the UIDVALIDITY has _changed_, decrease lastseenuid by one to avoid gaps (well add 1 below) */ - if( uidvalidity > 0 && lastseenuid > 1 ) { + if (uidvalidity > 0 && lastseenuid > 1) { lastseenuid -= 1; } @@ -798,10 +798,10 @@ static int fetch_from_single_folder(dc_imap_t* imap, const char* folder) r = mailimap_uid_fetch(imap->hEtpan, set, imap->fetch_type_uid, &fetch_result); mailimap_set_free(set); - if( is_error(imap, r) || fetch_result == NULL ) + if (is_error(imap, r) || fetch_result == NULL) { fetch_result = NULL; - if( r == MAILIMAP_ERROR_PROTOCOL ) { + if (r == MAILIMAP_ERROR_PROTOCOL) { dc_log_info(imap->context, 0, "Folder \"%s\" is empty", folder); goto cleanup; /* the folder is simply empty, this is no error */ } @@ -810,39 +810,39 @@ static int fetch_from_single_folder(dc_imap_t* imap, const char* folder) } /* go through all mails in folder (this is typically _fast_ as we already have the whole list) */ - for( cur = clist_begin(fetch_result); cur != NULL ; cur = clist_next(cur) ) + for (cur = clist_begin(fetch_result); cur != NULL ; cur = clist_next(cur)) { struct mailimap_msg_att* msg_att = (struct mailimap_msg_att*)clist_content(cur); /* mailimap_msg_att is a list of attributes: list is a list of message attributes */ uint32_t cur_uid = peek_uid(msg_att); - if( cur_uid > 0 - && cur_uid!=lastseenuid /* `UID FETCH :*` may include lastseenuid if "*" == lastseenuid */ ) + if (cur_uid > 0 + && cur_uid!=lastseenuid /* `UID FETCH :*` may include lastseenuid if "*" == lastseenuid */) { read_cnt++; - if( fetch_single_msg(imap, folder, cur_uid) == 0/* 0=try again later*/ ) { + if (fetch_single_msg(imap, folder, cur_uid) == 0/* 0=try again later*/) { read_errors++; } - else if( cur_uid > new_lastseenuid ) { + else if (cur_uid > new_lastseenuid) { new_lastseenuid = cur_uid; } } } - if( !read_errors && new_lastseenuid > 0 ) { + if (!read_errors && new_lastseenuid > 0) { set_config_lastseenuid(imap, folder, uidvalidity, new_lastseenuid); } /* done */ cleanup: - if( read_errors ) { + if (read_errors) { dc_log_warning(imap->context, 0, "%i mails read from \"%s\" with %i errors.", (int)read_cnt, folder, (int)read_errors); } else { dc_log_info(imap->context, 0, "%i mails read from \"%s\".", (int)read_cnt, folder); } - if( fetch_result ) { + if (fetch_result) { mailimap_fetch_list_free(fetch_result); } @@ -860,21 +860,21 @@ static int fetch_from_all_folders(dc_imap_t* imap) /* first, read the INBOX, this looks much better on the initial load as the INBOX has the most recent mails. Moreover, this is for speed reasons, as the other folders only have few new messages. */ - for( cur = clist_begin(folder_list); cur != NULL ; cur = clist_next(cur) ) + for (cur = clist_begin(folder_list); cur != NULL ; cur = clist_next(cur)) { dc_imapfolder_t* folder = (dc_imapfolder_t*)clist_content(cur); - if( folder->meaning == MEANING_INBOX ) { + if (folder->meaning == MEANING_INBOX) { total_cnt += fetch_from_single_folder(imap, folder->name_to_select); } } - for( cur = clist_begin(folder_list); cur != NULL ; cur = clist_next(cur) ) + for (cur = clist_begin(folder_list); cur != NULL ; cur = clist_next(cur)) { dc_imapfolder_t* folder = (dc_imapfolder_t*)clist_content(cur); - if( folder->meaning == MEANING_IGNORE ) { + if (folder->meaning == MEANING_IGNORE) { dc_log_info(imap->context, 0, "Ignoring \"%s\".", folder->name_utf8); } - else if( folder->meaning != MEANING_INBOX ) { + else if (folder->meaning != MEANING_INBOX) { total_cnt += fetch_from_single_folder(imap, folder->name_to_select); } } @@ -892,7 +892,7 @@ static int fetch_from_all_folders(dc_imap_t* imap) int dc_imap_fetch(dc_imap_t* imap) { - if( imap==NULL || !imap->connected ) { + if (imap==NULL || !imap->connected) { return 0; } @@ -900,7 +900,7 @@ int dc_imap_fetch(dc_imap_t* imap) #define FULL_FETCH_EVERY_SECONDS (22*60) - if( time(NULL) - imap->last_fullread_time > FULL_FETCH_EVERY_SECONDS ) { + if (time(NULL) - imap->last_fullread_time > FULL_FETCH_EVERY_SECONDS) { fetch_from_all_folders(imap); imap->last_fullread_time = time(NULL); } @@ -908,7 +908,7 @@ int dc_imap_fetch(dc_imap_t* imap) // as during the fetch commands, new messages may arrive, we fetch until we do not // get any more. if IDLE is called directly after, there is only a small chance that // messages are missed and delayed until the next IDLE call - while( fetch_from_single_folder(imap, "INBOX") > 0 ) { + while (fetch_from_single_folder(imap, "INBOX") > 0) { ; } @@ -926,7 +926,7 @@ static void fake_idle(dc_imap_t* imap) dc_log_info(imap->context, 0, "IMAP-fake-IDLEing..."); int do_fake_idle = 1; - while( do_fake_idle ) + while (do_fake_idle) { // wait a moment: every 5 seconds in the first 3 minutes after a new message, after that every 60 seconds seconds_to_wait = (time(NULL)-fake_idle_start_time < 3*60)? 5 : 60; @@ -936,9 +936,9 @@ static void fake_idle(dc_imap_t* imap) struct timespec wakeup_at; memset(&wakeup_at, 0, sizeof(wakeup_at)); wakeup_at.tv_sec = time(NULL)+seconds_to_wait; - while( imap->watch_condflag == 0 && r == 0 ) { + while (imap->watch_condflag == 0 && r == 0) { r = pthread_cond_timedwait(&imap->watch_cond, &imap->watch_condmutex, &wakeup_at); /* unlock mutex -> wait -> lock mutex */ - if( imap->watch_condflag ) { + if (imap->watch_condflag) { do_fake_idle = 0; } } @@ -946,15 +946,15 @@ static void fake_idle(dc_imap_t* imap) pthread_mutex_unlock(&imap->watch_condmutex); - if( do_fake_idle == 0 ) { + if (do_fake_idle == 0) { return; } // check for new messages. fetch_from_single_folder() has the side-effect that messages // are also downloaded, however, typically this would take place in the FETCH command // following IDLE otherwise, so this seems okay here. - if( setup_handle_if_needed(imap) ) { // the handle may not be set up if configure is not yet done - if( fetch_from_single_folder(imap, "INBOX") ) { + if (setup_handle_if_needed(imap)) { // the handle may not be set up if configure is not yet done + if (fetch_from_single_folder(imap, "INBOX")) { do_fake_idle = 0; } } @@ -972,13 +972,13 @@ void dc_imap_idle(dc_imap_t* imap) { int r, r2; - if( imap->can_idle ) + if (imap->can_idle) { setup_handle_if_needed(imap); - if( imap->idle_set_up==0 && imap->hEtpan && imap->hEtpan->imap_stream ) { + if (imap->idle_set_up==0 && imap->hEtpan && imap->hEtpan->imap_stream) { r = mailstream_setup_idle(imap->hEtpan->imap_stream); - if( is_error(imap, r) ) { + if (is_error(imap, r)) { dc_log_warning(imap->context, 0, "IMAP-IDLE: Cannot setup."); fake_idle(imap); return; @@ -986,14 +986,14 @@ void dc_imap_idle(dc_imap_t* imap) imap->idle_set_up = 1; } - if( !imap->idle_set_up || !select_folder(imap, "INBOX") ) { + if (!imap->idle_set_up || !select_folder(imap, "INBOX")) { dc_log_warning(imap->context, 0, "IMAP-IDLE not setup."); fake_idle(imap); return; } r = mailimap_idle(imap->hEtpan); - if( is_error(imap, r) ) { + if (is_error(imap, r)) { dc_log_warning(imap->context, 0, "IMAP-IDLE: Cannot start."); fake_idle(imap); return; @@ -1007,17 +1007,17 @@ void dc_imap_idle(dc_imap_t* imap) r = mailstream_wait_idle(imap->hEtpan->imap_stream, IDLE_DELAY_SECONDS); r2 = mailimap_idle_done(imap->hEtpan); - if( r == MAILSTREAM_IDLE_ERROR /*0*/ || r==MAILSTREAM_IDLE_CANCELLED /*4*/ ) { + if (r == MAILSTREAM_IDLE_ERROR /*0*/ || r==MAILSTREAM_IDLE_CANCELLED /*4*/) { dc_log_info(imap->context, 0, "IMAP-IDLE wait cancelled, r=%i, r2=%i; we'll reconnect soon.", r, r2); imap->should_reconnect = 1; } - else if( r == MAILSTREAM_IDLE_INTERRUPTED /*1*/ ) { + else if (r == MAILSTREAM_IDLE_INTERRUPTED /*1*/) { dc_log_info(imap->context, 0, "IMAP-IDLE interrupted."); } - else if( r == MAILSTREAM_IDLE_HASDATA /*2*/ ) { + else if (r == MAILSTREAM_IDLE_HASDATA /*2*/) { dc_log_info(imap->context, 0, "IMAP-IDLE has data."); } - else if( r == MAILSTREAM_IDLE_TIMEOUT /*3*/ ) { + else if (r == MAILSTREAM_IDLE_TIMEOUT /*3*/) { dc_log_info(imap->context, 0, "IMAP-IDLE timeout."); } else { @@ -1033,14 +1033,14 @@ void dc_imap_idle(dc_imap_t* imap) void dc_imap_interrupt_idle(dc_imap_t* imap) { - if( imap==NULL ) { // imap->hEtPan may be NULL + if (imap==NULL) { // imap->hEtPan may be NULL dc_log_warning(imap->context, 0, "Interrupt IMAP-IDLE: Bad parameter."); return; } - if( imap->can_idle ) + if (imap->can_idle) { - if( imap && imap->hEtpan && imap->hEtpan->imap_stream ) { + if (imap && imap->hEtpan && imap->hEtpan->imap_stream) { mailstream_interrupt_idle(imap->hEtpan->imap_stream); } } @@ -1062,20 +1062,20 @@ static int setup_handle_if_needed(dc_imap_t* imap) { int r, success = 0; - if( imap==NULL || imap->imap_server==NULL ) { + if (imap==NULL || imap->imap_server==NULL) { goto cleanup; } - if( imap->should_reconnect ) { + if (imap->should_reconnect) { unsetup_handle(imap); } - if( imap->hEtpan ) { + if (imap->hEtpan) { success = 1; goto cleanup; } - if( imap->context->cb(imap->context, DC_EVENT_IS_OFFLINE, 0, 0)!=0 ) { + if (imap->context->cb(imap->context, DC_EVENT_IS_OFFLINE, 0, 0)!=0) { dc_log_error_if(&imap->log_connect_errors, imap->context, DC_ERROR_NO_NETWORK, NULL); goto cleanup; } @@ -1084,18 +1084,18 @@ static int setup_handle_if_needed(dc_imap_t* imap) mailimap_set_timeout(imap->hEtpan, DC_IMAP_TIMEOUT_SEC); - if( imap->server_flags&(DC_LP_IMAP_SOCKET_STARTTLS|DC_LP_IMAP_SOCKET_PLAIN) ) + if (imap->server_flags&(DC_LP_IMAP_SOCKET_STARTTLS|DC_LP_IMAP_SOCKET_PLAIN)) { r = mailimap_socket_connect(imap->hEtpan, imap->imap_server, imap->imap_port); - if( is_error(imap, r) ) { + if (is_error(imap, r)) { dc_log_error_if(&imap->log_connect_errors, imap->context, 0, "Could not connect to IMAP-server %s:%i. (Error #%i)", imap->imap_server, (int)imap->imap_port, (int)r); goto cleanup; } - if( imap->server_flags&DC_LP_IMAP_SOCKET_STARTTLS ) + if (imap->server_flags&DC_LP_IMAP_SOCKET_STARTTLS) { r = mailimap_socket_starttls(imap->hEtpan); - if( is_error(imap, r) ) { + if (is_error(imap, r)) { dc_log_error_if(&imap->log_connect_errors, imap->context, 0, "Could not connect to IMAP-server %s:%i using STARTTLS. (Error #%i)", imap->imap_server, (int)imap->imap_port, (int)r); goto cleanup; } @@ -1109,7 +1109,7 @@ static int setup_handle_if_needed(dc_imap_t* imap) else { r = mailimap_ssl_connect(imap->hEtpan, imap->imap_server, imap->imap_port); - if( is_error(imap, r) ) { + if (is_error(imap, r)) { dc_log_error_if(&imap->log_connect_errors, imap->context, 0, "Could not connect to IMAP-server %s:%i using SSL. (Error #%i)", imap->imap_server, (int)imap->imap_port, (int)r); goto cleanup; } @@ -1117,7 +1117,7 @@ static int setup_handle_if_needed(dc_imap_t* imap) } /* TODO: There are more authorisation types, see mailcore2/MCIMAPSession.cpp, however, I'm not sure of they are really all needed */ - /*if( imap->server_flags&DC_LP_AUTH_XOAUTH2 ) + /*if (imap->server_flags&DC_LP_AUTH_XOAUTH2) { //TODO: Support XOAUTH2, we "just" need to get the token someway. If we do so, there is no more need for the user to enable //https://www.google.com/settings/security/lesssecureapps - however, maybe this is also not needed if the user had enabled 2-factor-authorisation. @@ -1134,7 +1134,7 @@ static int setup_handle_if_needed(dc_imap_t* imap) r = mailimap_login(imap->hEtpan, imap->imap_user, imap->imap_pw); } - if( is_error(imap, r) ) { + if (is_error(imap, r)) { char* msg = get_error_msg(imap, "Cannot login", r); dc_log_error_if(&imap->log_connect_errors, imap->context, 0, "%s", msg); free(msg); @@ -1146,7 +1146,7 @@ static int setup_handle_if_needed(dc_imap_t* imap) success = 1; cleanup: - if( success == 0 ) { + if (success == 0) { unsetup_handle(imap); } @@ -1157,18 +1157,18 @@ cleanup: static void unsetup_handle(dc_imap_t* imap) { - if( imap==NULL ) { + if (imap==NULL) { return; } - if( imap->hEtpan ) + if (imap->hEtpan) { - if( imap->idle_set_up ) { + if (imap->idle_set_up) { mailstream_unsetup_idle(imap->hEtpan->imap_stream); imap->idle_set_up = 0; } - if( imap->hEtpan->imap_stream != NULL ) { + if (imap->hEtpan->imap_stream != NULL) { mailstream_close(imap->hEtpan->imap_stream); /* not sure, if this is really needed, however, mailcore2 does the same */ imap->hEtpan->imap_stream = NULL; } @@ -1219,11 +1219,11 @@ int dc_imap_connect(dc_imap_t* imap, const dc_loginparam_t* lp) { int success = 0; - if( imap==NULL || lp==NULL || lp->mail_server==NULL || lp->mail_user==NULL || lp->mail_pw==NULL ) { + if (imap==NULL || lp==NULL || lp->mail_server==NULL || lp->mail_user==NULL || lp->mail_pw==NULL) { return 0; } - if( imap->connected ) { + if (imap->connected) { success = 1; goto cleanup; } @@ -1234,7 +1234,7 @@ int dc_imap_connect(dc_imap_t* imap, const dc_loginparam_t* lp) imap->imap_pw = dc_strdup(lp->mail_pw); imap->server_flags = lp->server_flags; - if( !setup_handle_if_needed(imap) ) { + if (!setup_handle_if_needed(imap)) { goto cleanup; } @@ -1247,19 +1247,19 @@ int dc_imap_connect(dc_imap_t* imap, const dc_loginparam_t* lp) #endif - if( !imap->skip_log_capabilities - && imap->hEtpan->imap_connection_info && imap->hEtpan->imap_connection_info->imap_capability ) + if (!imap->skip_log_capabilities + && imap->hEtpan->imap_connection_info && imap->hEtpan->imap_connection_info->imap_capability) { /* just log the whole capabilities list (the mailimap_has_*() function also use this list, so this is a good overview on problems) */ imap->skip_log_capabilities = 1; dc_strbuilder_t capinfostr; dc_strbuilder_init(&capinfostr, 0); clist* list = imap->hEtpan->imap_connection_info->imap_capability->cap_list; - if( list ) { + if (list) { clistiter* cur; for(cur = clist_begin(list) ; cur != NULL ; cur = clist_next(cur)) { struct mailimap_capability * cap = clist_content(cur); - if( cap && cap->cap_type == MAILIMAP_CAPABILITY_NAME ) { + if (cap && cap->cap_type == MAILIMAP_CAPABILITY_NAME) { dc_strbuilder_cat(&capinfostr, " "); dc_strbuilder_cat(&capinfostr, cap->cap_data.cap_name); } @@ -1273,7 +1273,7 @@ int dc_imap_connect(dc_imap_t* imap, const dc_loginparam_t* lp) success = 1; cleanup: - if( success == 0 ) { + if (success == 0) { unsetup_handle(imap); free_connect_param(imap); } @@ -1283,11 +1283,11 @@ cleanup: void dc_imap_disconnect(dc_imap_t* imap) { - if( imap==NULL ) { + if (imap==NULL) { return; } - if( imap->connected ) + if (imap->connected) { unsetup_handle(imap); free_connect_param(imap); @@ -1311,7 +1311,7 @@ dc_imap_t* dc_imap_new(dc_get_config_t get_config, dc_set_config_t set_config, d { dc_imap_t* imap = NULL; - if( (imap=calloc(1, sizeof(dc_imap_t)))==NULL ) { + if ((imap=calloc(1, sizeof(dc_imap_t)))==NULL) { exit(25); /* cannot allocate little memory, unrecoverable error */ } @@ -1360,7 +1360,7 @@ dc_imap_t* dc_imap_new(dc_get_config_t get_config, dc_set_config_t set_config, d void dc_imap_unref(dc_imap_t* imap) { - if( imap==NULL ) { + if (imap==NULL) { return; } @@ -1371,9 +1371,9 @@ void dc_imap_unref(dc_imap_t* imap) free(imap->selected_folder); - if( imap->fetch_type_uid ) { mailimap_fetch_type_free(imap->fetch_type_uid); } - if( imap->fetch_type_body ) { mailimap_fetch_type_free(imap->fetch_type_body); } - if( imap->fetch_type_flags ){ mailimap_fetch_type_free(imap->fetch_type_flags);} + if (imap->fetch_type_uid) { mailimap_fetch_type_free(imap->fetch_type_uid); } + if (imap->fetch_type_body) { mailimap_fetch_type_free(imap->fetch_type_body); } + if (imap->fetch_type_flags){ mailimap_fetch_type_free(imap->fetch_type_flags);} free(imap); } @@ -1388,22 +1388,22 @@ int dc_imap_append_msg(dc_imap_t* imap, time_t timestamp, const char* data_not_t *ret_server_folder = NULL; - if( imap==NULL ) { + if (imap==NULL) { goto cleanup; } - if( imap->hEtpan==NULL ) { + if (imap->hEtpan==NULL) { goto cleanup; } dc_log_info(imap->context, 0, "Appending message to IMAP-server..."); - if( !init_chat_folders(imap) ) { + if (!init_chat_folders(imap)) { dc_log_error_if(&imap->log_connect_errors, imap->context, 0, "Cannot find out IMAP-sent-folder."); goto cleanup; } - if( !select_folder(imap, imap->sent_folder) ) { + if (!select_folder(imap, imap->sent_folder)) { dc_log_error_if(&imap->log_connect_errors, imap->context, 0, "Cannot select IMAP-folder \"%s\".", imap->sent_folder); imap->sent_folder[0] = 0; /* force re-init */ goto cleanup; @@ -1413,13 +1413,13 @@ int dc_imap_append_msg(dc_imap_t* imap, time_t timestamp, const char* data_not_t mailimap_flag_list_add(flag_list, mailimap_flag_new_seen()); imap_date = dc_timestamp_to_mailimap_date_time(timestamp); - if( imap_date == NULL ) { + if (imap_date == NULL) { dc_log_error(imap->context, 0, "Bad date."); goto cleanup; } r = mailimap_uidplus_append(imap->hEtpan, imap->sent_folder, flag_list, imap_date, data_not_terminated, data_bytes, &ret_uidvalidity, ret_server_uid); - if( is_error(imap, r) ) { + if (is_error(imap, r)) { dc_log_error_if(&imap->log_connect_errors, imap->context, 0, "Cannot append message to \"%s\", error #%i.", imap->sent_folder, (int)r); goto cleanup; } @@ -1432,11 +1432,11 @@ int dc_imap_append_msg(dc_imap_t* imap, time_t timestamp, const char* data_not_t cleanup: - if( imap_date ) { + if (imap_date) { mailimap_date_time_free(imap_date); } - if( flag_list ) { + if (flag_list) { mailimap_flag_list_free(flag_list); } @@ -1451,7 +1451,7 @@ static int add_flag(dc_imap_t* imap, uint32_t server_uid, struct mailimap_flag* struct mailimap_store_att_flags* store_att_flags = NULL; struct mailimap_set* set = mailimap_set_new_single(server_uid); - if( imap==NULL || imap->hEtpan==NULL ) { + if (imap==NULL || imap->hEtpan==NULL) { goto cleanup; } @@ -1461,15 +1461,15 @@ static int add_flag(dc_imap_t* imap, uint32_t server_uid, struct mailimap_flag* store_att_flags = mailimap_store_att_flags_new_add_flags(flag_list); /* FLAGS.SILENT does not return the new value */ r = mailimap_uid_store(imap->hEtpan, set, store_att_flags); - if( is_error(imap, r) ) { + if (is_error(imap, r)) { goto cleanup; } cleanup: - if( store_att_flags ) { + if (store_att_flags) { mailimap_store_att_flags_free(store_att_flags); } - if( set ) { + if (set) { mailimap_set_free(set); } return imap->should_reconnect? 0 : 1; /* all non-connection states are treated as success - the mail may already be deleted or moved away on the server */ @@ -1484,51 +1484,51 @@ int dc_imap_markseen_msg(dc_imap_t* imap, const char* folder, uint32_t server_ui int r; struct mailimap_set* set = NULL; - if( imap==NULL || folder==NULL || server_uid==0 || ret_server_folder==NULL || ret_server_uid==NULL || ret_ms_flags==NULL - || *ret_server_folder!=NULL || *ret_server_uid!=0 || *ret_ms_flags!=0 ) { + if (imap==NULL || folder==NULL || server_uid==0 || ret_server_folder==NULL || ret_server_uid==NULL || ret_ms_flags==NULL + || *ret_server_folder!=NULL || *ret_server_uid!=0 || *ret_ms_flags!=0) { return 1; /* job done */ } - if( (set=mailimap_set_new_single(server_uid))==NULL ) { + if ((set=mailimap_set_new_single(server_uid))==NULL) { goto cleanup; } - if( imap->hEtpan==NULL ) { + if (imap->hEtpan==NULL) { goto cleanup; } dc_log_info(imap->context, 0, "Marking message %s/%i as seen...", folder, (int)server_uid); - if( select_folder(imap, folder)==0 ) { + if (select_folder(imap, folder)==0) { dc_log_warning(imap->context, 0, "Cannot select folder."); goto cleanup; } - if( add_flag(imap, server_uid, mailimap_flag_new_seen())==0 ) { + if (add_flag(imap, server_uid, mailimap_flag_new_seen())==0) { dc_log_warning(imap->context, 0, "Cannot mark message as seen."); goto cleanup; } dc_log_info(imap->context, 0, "Message marked as seen."); - if( (ms_flags&DC_MS_SET_MDNSent_FLAG) - && imap->hEtpan->imap_selection_info!=NULL && imap->hEtpan->imap_selection_info->sel_perm_flags!=NULL ) + if ((ms_flags&DC_MS_SET_MDNSent_FLAG) + && imap->hEtpan->imap_selection_info!=NULL && imap->hEtpan->imap_selection_info->sel_perm_flags!=NULL) { /* Check if the folder can handle the `$MDNSent` flag (see RFC 3503). If so, and not set: set the flags and return this information. If the folder cannot handle the `$MDNSent` flag, we risk duplicated MDNs; it's up to the receiving MUA to handle this then (eg. Delta Chat has no problem with this). */ int can_create_flag = 0; clistiter* iter; - for( iter=clist_begin(imap->hEtpan->imap_selection_info->sel_perm_flags); iter!=NULL; iter=clist_next(iter) ) + for (iter=clist_begin(imap->hEtpan->imap_selection_info->sel_perm_flags); iter!=NULL; iter=clist_next(iter)) { struct mailimap_flag_perm* fp = (struct mailimap_flag_perm*)clist_content(iter); - if( fp ) { - if( fp->fl_type==MAILIMAP_FLAG_PERM_ALL ) { + if (fp) { + if (fp->fl_type==MAILIMAP_FLAG_PERM_ALL) { can_create_flag = 1; break; } - else if( fp->fl_type==MAILIMAP_FLAG_PERM_FLAG && fp->fl_flag ) { + else if (fp->fl_type==MAILIMAP_FLAG_PERM_FLAG && fp->fl_flag) { struct mailimap_flag* fl = (struct mailimap_flag*)fp->fl_flag; - if( fl->fl_type==MAILIMAP_FLAG_KEYWORD && fl->fl_data.fl_keyword && strcmp(fl->fl_data.fl_keyword, "$MDNSent")==0 ) { + if (fl->fl_type==MAILIMAP_FLAG_KEYWORD && fl->fl_data.fl_keyword && strcmp(fl->fl_data.fl_keyword, "$MDNSent")==0) { can_create_flag = 1; break; } @@ -1536,14 +1536,14 @@ int dc_imap_markseen_msg(dc_imap_t* imap, const char* folder, uint32_t server_ui } } - if( can_create_flag ) + if (can_create_flag) { clist* fetch_result = NULL; r = mailimap_uid_fetch(imap->hEtpan, set, imap->fetch_type_flags, &fetch_result); - if( !is_error(imap, r) && fetch_result ) { + if (!is_error(imap, r) && fetch_result) { clistiter* cur=clist_begin(fetch_result); - if( cur ) { - if( !peek_flag_keyword((struct mailimap_msg_att*)clist_content(cur), "$MDNSent") ) { + if (cur) { + if (!peek_flag_keyword((struct mailimap_msg_att*)clist_content(cur), "$MDNSent")) { add_flag(imap, server_uid, mailimap_flag_new_flag_keyword(dc_strdup("$MDNSent"))); *ret_ms_flags |= DC_MS_MDNSent_JUST_SET; } @@ -1559,16 +1559,16 @@ int dc_imap_markseen_msg(dc_imap_t* imap, const char* folder, uint32_t server_ui } } - if( (ms_flags&DC_MS_ALSO_MOVE) && (imap->server_flags&DC_NO_MOVE_TO_CHATS)==0 ) + if ((ms_flags&DC_MS_ALSO_MOVE) && (imap->server_flags&DC_NO_MOVE_TO_CHATS)==0) { init_chat_folders(imap); - if( imap->moveto_folder && strcmp(folder, imap->moveto_folder)==0 ) + if (imap->moveto_folder && strcmp(folder, imap->moveto_folder)==0) { dc_log_info(imap->context, 0, "Message %s/%i is already in %s...", folder, (int)server_uid, imap->moveto_folder); /* avoid deadlocks as moving messages in the same folder may be result in a new server_uid and the state "fresh" - we will catch these messages again on the next poll, try to move them away and so on, see also (***) in dc_receive_imf.c */ } - else if( imap->moveto_folder ) + else if (imap->moveto_folder) { dc_log_info(imap->context, 0, "Moving message %s/%i to %s...", folder, (int)server_uid, imap->moveto_folder); @@ -1578,7 +1578,7 @@ int dc_imap_markseen_msg(dc_imap_t* imap, const char* folder, uint32_t server_ui struct mailimap_set* res_setsrc = NULL; struct mailimap_set* res_setdest = NULL; r = mailimap_uidplus_uid_move(imap->hEtpan, set, imap->moveto_folder, &res_uid, &res_setsrc, &res_setdest); /* the correct folder is already selected in add_flag() above */ - if( is_error(imap, r) ) { + if (is_error(imap, r)) { dc_log_info(imap->context, 0, "Cannot move message, fallback to COPY/DELETE %s/%i to %s...", folder, (int)server_uid, imap->moveto_folder); r = mailimap_uidplus_uid_copy(imap->hEtpan, set, imap->moveto_folder, &res_uid, &res_setsrc, &res_setdest); if (is_error(imap, r)) { @@ -1587,7 +1587,7 @@ int dc_imap_markseen_msg(dc_imap_t* imap, const char* folder, uint32_t server_ui } else { dc_log_info(imap->context, 0, "Deleting msg ..."); - if( add_flag(imap, server_uid, mailimap_flag_new_deleted())==0 ) { + if (add_flag(imap, server_uid, mailimap_flag_new_deleted())==0) { dc_log_warning(imap->context, 0, "Cannot mark message as \"Deleted\".");/* maybe the message is already deleted */ } @@ -1597,11 +1597,11 @@ int dc_imap_markseen_msg(dc_imap_t* imap, const char* folder, uint32_t server_ui } - if( res_setsrc ) { + if (res_setsrc) { mailimap_set_free(res_setsrc); } - if( res_setdest ) { + if (res_setdest) { clistiter* cur = clist_begin(res_setdest->set_list); if (cur != NULL) { struct mailimap_set_item* item; @@ -1620,7 +1620,7 @@ int dc_imap_markseen_msg(dc_imap_t* imap, const char* folder, uint32_t server_ui } cleanup: - if( set ) { + if (set) { mailimap_set_free(set); } return imap->should_reconnect? 0 : 1; @@ -1634,14 +1634,14 @@ int dc_imap_delete_msg(dc_imap_t* imap, const char* rfc724_mid, const char* fold char* is_rfc724_mid = NULL; char* new_folder = NULL; - if( imap==NULL || rfc724_mid==NULL || folder==NULL || folder[0]==0 ) { + if (imap==NULL || rfc724_mid==NULL || folder==NULL || folder[0]==0) { success = 1; /* job done, do not try over */ goto cleanup; } dc_log_info(imap->context, 0, "Marking message \"%s\", %s/%i for deletion...", rfc724_mid, folder, (int)server_uid); - if( select_folder(imap, folder)==0 ) { + if (select_folder(imap, folder)==0) { dc_log_warning(imap->context, 0, "Cannot select folder \"%s\".", folder); /* maybe the folder does no longer exist */ goto cleanup; } @@ -1650,18 +1650,18 @@ int dc_imap_delete_msg(dc_imap_t* imap, const char* rfc724_mid, const char* fold was moved around by other MUAs and in place of an UIDVALIDITY check) (we also detect messages moved around when we do a fetch-all, see dc_update_server_uid() in receive_imf(), however this may take a while) */ - if( server_uid ) + if (server_uid) { clistiter* cur = NULL; const char* is_quoted_rfc724_mid = NULL; struct mailimap_set* set = mailimap_set_new_single(server_uid); r = mailimap_uid_fetch(imap->hEtpan, set, imap->fetch_type_message_id, &fetch_result); mailimap_set_free(set); - if( is_error(imap, r) || fetch_result == NULL + if (is_error(imap, r) || fetch_result == NULL || (cur=clist_begin(fetch_result)) == NULL || (is_quoted_rfc724_mid=peek_rfc724_mid((struct mailimap_msg_att*)clist_content(cur)))==NULL || (is_rfc724_mid=unquote_rfc724_mid(is_quoted_rfc724_mid))==NULL - || strcmp(is_rfc724_mid, rfc724_mid)!=0 ) + || strcmp(is_rfc724_mid, rfc724_mid)!=0) { dc_log_warning(imap->context, 0, "UID not found in the given folder or does not match Message-ID."); server_uid = 0; @@ -1670,9 +1670,9 @@ int dc_imap_delete_msg(dc_imap_t* imap, const char* rfc724_mid, const char* fold /* server_uid is 0 now if it was not given or if it does not match the given message id; try to search for it in all folders (the message may be moved by another MUA to a folder we do not sync or the sync is a moment ago) */ - if( server_uid == 0 ) { + if (server_uid == 0) { dc_log_info(imap->context, 0, "Searching UID by Message-ID \"%s\"...", rfc724_mid); - if( (server_uid=search_uid(imap, rfc724_mid))==0 ) { + if ((server_uid=search_uid(imap, rfc724_mid))==0) { dc_log_warning(imap->context, 0, "Message-ID \"%s\" not found in any folder, cannot delete message.", rfc724_mid); goto cleanup; } @@ -1681,7 +1681,7 @@ int dc_imap_delete_msg(dc_imap_t* imap, const char* rfc724_mid, const char* fold /* mark the message for deletion */ - if( add_flag(imap, server_uid, mailimap_flag_new_deleted())==0 ) { + if (add_flag(imap, server_uid, mailimap_flag_new_deleted())==0) { dc_log_warning(imap->context, 0, "Cannot mark message as \"Deleted\"."); /* maybe the message is already deleted */ goto cleanup; } @@ -1693,7 +1693,7 @@ int dc_imap_delete_msg(dc_imap_t* imap, const char* rfc724_mid, const char* fold cleanup: - if( fetch_result ) { mailimap_fetch_list_free(fetch_result); } + if (fetch_result) { mailimap_fetch_list_free(fetch_result); } free(is_rfc724_mid); free(new_folder); diff --git a/src/dc_imex.c b/src/dc_imex.c index bb0b9656..2f3e1689 100644 --- a/src/dc_imex.c +++ b/src/dc_imex.c @@ -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; } diff --git a/src/dc_job.c b/src/dc_job.c index 60d65417..686fb208 100644 --- a/src/dc_job.c +++ b/src/dc_job.c @@ -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; } diff --git a/src/dc_key.c b/src/dc_key.c index 6ec993c9..07ef7022 100644 --- a/src/dc_key.c +++ b/src/dc_key.c @@ -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; } diff --git a/src/dc_keyring.c b/src/dc_keyring.c index 5a455c17..e5bae4e9 100644 --- a/src/dc_keyring.c +++ b/src/dc_keyring.c @@ -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 */ diff --git a/src/dc_log.c b/src/dc_log.c index 216f992a..eb072f7d 100644 --- a/src/dc_log.c +++ b/src/dc_log.c @@ -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 { diff --git a/src/dc_loginparam.c b/src/dc_loginparam.c index 869f8044..6c3b922a 100644 --- a/src/dc_loginparam.c +++ b/src/dc_loginparam.c @@ -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<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; } diff --git a/src/dc_mimefactory.c b/src/dc_mimefactory.c index be899e72..8e76fcd8 100644 --- a/src/dc_mimefactory.c +++ b/src/dc_mimefactory.c @@ -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() */ diff --git a/src/dc_mimeparser.c b/src/dc_mimeparser.c index 48f5d44d..d9328391 100644 --- a/src/dc_mimeparser.c +++ b/src/dc_mimeparser.c @@ -248,7 +248,7 @@ static void display_field(struct mailimf_field * field) case MAILIMF_FIELD_OPTIONAL_FIELD: { struct mailimf_optional_field* of = field->fld_data.fld_optional_field; - if( of ) { + if (of) { printf("%s: %s\n", of->fld_name? of->fld_name : "?", of->fld_value? of->fld_value : "?"); } } @@ -336,7 +336,7 @@ static void print_mime(struct mailmime * mime) { clistiter * cur; - if( mime == NULL ) { + if (mime == NULL) { printf("ERROR: NULL given to print_mime()\n"); return; } @@ -414,7 +414,7 @@ void mailmime_print(struct mailmime* mime) static void mailimf_get_recipients__add_addr(dc_hash_t* recipients, struct mailimf_mailbox* mb) { /* only used internally by mailimf_get_recipients() */ - if( mb ) { + if (mb) { char* addr_norm = dc_normalize_addr(mb->mb_addr_spec); dc_hash_insert(recipients, addr_norm, strlen(addr_norm), (void*)1); free(addr_norm); @@ -429,31 +429,31 @@ dc_hash_t* mailimf_get_recipients(struct mailimf_fields* imffields) dc_hash_init(recipients, DC_HASH_STRING, 1/*copy key*/); clistiter* cur1; - for( cur1 = clist_begin(imffields->fld_list); cur1!=NULL ; cur1=clist_next(cur1) ) + for (cur1 = clist_begin(imffields->fld_list); cur1!=NULL ; cur1=clist_next(cur1)) { struct mailimf_field* fld = (struct mailimf_field*)clist_content(cur1); struct mailimf_to* fld_to = NULL; struct mailimf_cc* fld_cc = NULL; struct mailimf_address_list* addr_list = NULL; - switch( fld->fld_type ) + switch (fld->fld_type) { - case MAILIMF_FIELD_TO: fld_to = fld->fld_data.fld_to; if( fld_to ) { addr_list = fld_to->to_addr_list; } break; - case MAILIMF_FIELD_CC: fld_cc = fld->fld_data.fld_cc; if( fld_cc ) { addr_list = fld_cc->cc_addr_list; } break; + case MAILIMF_FIELD_TO: fld_to = fld->fld_data.fld_to; if (fld_to) { addr_list = fld_to->to_addr_list; } break; + case MAILIMF_FIELD_CC: fld_cc = fld->fld_data.fld_cc; if (fld_cc) { addr_list = fld_cc->cc_addr_list; } break; } - if( addr_list ) { + if (addr_list) { clistiter* cur2; - for( cur2 = clist_begin(addr_list->ad_list); cur2!=NULL ; cur2=clist_next(cur2) ) { + for (cur2 = clist_begin(addr_list->ad_list); cur2!=NULL ; cur2=clist_next(cur2)) { struct mailimf_address* adr = (struct mailimf_address*)clist_content(cur2); - if( adr ) { - if( adr->ad_type == MAILIMF_ADDRESS_MAILBOX ) { + if (adr) { + if (adr->ad_type == MAILIMF_ADDRESS_MAILBOX) { mailimf_get_recipients__add_addr(recipients, adr->ad_data.ad_mailbox); } - else if( adr->ad_type == MAILIMF_ADDRESS_GROUP ) { + else if (adr->ad_type == MAILIMF_ADDRESS_GROUP) { struct mailimf_group* group = adr->ad_data.ad_group; - if( group && group->grp_mb_list ) { + if (group && group->grp_mb_list) { clistiter* cur3; - for( cur3 = clist_begin(group->grp_mb_list->mb_list); cur3!=NULL ; cur3=clist_next(cur3) ) { + for (cur3 = clist_begin(group->grp_mb_list->mb_list); cur3!=NULL ; cur3=clist_next(cur3)) { mailimf_get_recipients__add_addr(recipients, (struct mailimf_mailbox*)clist_content(cur3)); } } @@ -475,17 +475,17 @@ dc_hash_t* mailimf_get_recipients(struct mailimf_fields* imffields) struct mailmime_parameter* mailmime_find_ct_parameter(struct mailmime* mime, const char* name) { /* find a parameter in `Content-Type: foo/bar; name=value;` */ - if( mime==NULL || name==NULL - || mime->mm_content_type==NULL || mime->mm_content_type->ct_parameters==NULL ) + if (mime==NULL || name==NULL + || mime->mm_content_type==NULL || mime->mm_content_type->ct_parameters==NULL) { return NULL; } clistiter* cur; - for( cur = clist_begin(mime->mm_content_type->ct_parameters); cur != NULL; cur = clist_next(cur) ) { + for (cur = clist_begin(mime->mm_content_type->ct_parameters); cur != NULL; cur = clist_next(cur)) { struct mailmime_parameter* param = (struct mailmime_parameter*)clist_content(cur); - if( param && param->pa_name ) { - if( strcmp(param->pa_name, name)==0 ) { + if (param && param->pa_name) { + if (strcmp(param->pa_name, name)==0) { return param; } } @@ -503,18 +503,18 @@ int mailmime_transfer_decode(struct mailmime* mime, const char** ret_decoded_dat size_t decoded_data_bytes = 0; char* transfer_decoding_buffer = NULL; /* mmap_string_unref()'d if set */ - if( mime == NULL || ret_decoded_data == NULL || ret_decoded_data_bytes == NULL || ret_to_mmap_string_unref == NULL - || *ret_decoded_data != NULL || *ret_decoded_data_bytes != 0 || *ret_to_mmap_string_unref != NULL ) { + if (mime == NULL || ret_decoded_data == NULL || ret_decoded_data_bytes == NULL || ret_to_mmap_string_unref == NULL + || *ret_decoded_data != NULL || *ret_decoded_data_bytes != 0 || *ret_to_mmap_string_unref != NULL) { return 0; } mime_data = mime->mm_data.mm_single; - 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 && field->fld_type == MAILMIME_FIELD_TRANSFER_ENCODING && field->fld_data.fld_encoding ) { + if (field && field->fld_type == MAILMIME_FIELD_TRANSFER_ENCODING && field->fld_data.fld_encoding) { mime_transfer_encoding = field->fld_data.fld_encoding->enc_type; break; } @@ -522,13 +522,13 @@ int mailmime_transfer_decode(struct mailmime* mime, const char** ret_decoded_dat } /* 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) { return 0; /* no error - but no data */ } } @@ -539,7 +539,7 @@ int mailmime_transfer_decode(struct mailmime* mime, const char** ret_decoded_dat r = mailmime_part_parse(mime_data->dt_data.dt_text.dt_data, mime_data->dt_data.dt_text.dt_length, ¤t_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) { return 0; } decoded_data = transfer_decoding_buffer; @@ -554,7 +554,7 @@ int mailmime_transfer_decode(struct mailmime* mime, const char** ret_decoded_dat struct mailimf_fields* mailmime_find_mailimf_fields(struct mailmime* mime) { - if( mime == NULL ) { + if (mime == NULL) { return NULL; } @@ -562,7 +562,7 @@ struct mailimf_fields* mailmime_find_mailimf_fields(struct mailmime* mime) case MAILMIME_MULTIPLE: for (clistiter* cur=clist_begin(mime->mm_data.mm_multipart.mm_mp_list); cur!=NULL ; cur=clist_next(cur)) { struct mailimf_fields* header = mailmime_find_mailimf_fields(clist_content(cur)); - if( header ) { + if (header) { return header; } } @@ -579,13 +579,13 @@ char* mailimf_find_first_addr(const struct mailimf_mailbox_list* mb_list) { clistiter* cur; - if( mb_list == NULL ) { + if (mb_list == NULL) { return NULL; } - for( cur = clist_begin(mb_list->mb_list); cur!=NULL ; cur=clist_next(cur) ) { + for (cur = clist_begin(mb_list->mb_list); cur!=NULL ; cur=clist_next(cur)) { struct mailimf_mailbox* mb = (struct mailimf_mailbox*)clist_content(cur); - if( mb && mb->mb_addr_spec ) { + if (mb && mb->mb_addr_spec) { return dc_normalize_addr(mb->mb_addr_spec); } } @@ -595,17 +595,17 @@ char* mailimf_find_first_addr(const struct mailimf_mailbox_list* mb_list) struct mailimf_field* mailimf_find_field(struct mailimf_fields* header, int wanted_fld_type) { - if( header == NULL || header->fld_list == NULL ) { + if (header == NULL || header->fld_list == NULL) { return NULL; } clistiter* cur1; - for( cur1 = clist_begin(header->fld_list); cur1!=NULL ; cur1=clist_next(cur1) ) + for (cur1 = clist_begin(header->fld_list); cur1!=NULL ; cur1=clist_next(cur1)) { struct mailimf_field* field = (struct mailimf_field*)clist_content(cur1); - if( field ) + if (field) { - if( field->fld_type == wanted_fld_type ) { + if (field->fld_type == wanted_fld_type) { return field; } } @@ -618,18 +618,18 @@ struct mailimf_field* mailimf_find_field(struct mailimf_fields* header, int want struct mailimf_optional_field* mailimf_find_optional_field(struct mailimf_fields* header, const char* wanted_fld_name) { /* Note: the function does not return fields with no value set! */ - if( header == NULL || header->fld_list == NULL ) { + if (header == NULL || header->fld_list == NULL) { return NULL; } clistiter* cur1; - for( cur1 = clist_begin(header->fld_list); cur1!=NULL ; cur1=clist_next(cur1) ) + for (cur1 = clist_begin(header->fld_list); cur1!=NULL ; cur1=clist_next(cur1)) { struct mailimf_field* field = (struct mailimf_field*)clist_content(cur1); - 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 && optional_field->fld_value && strcasecmp(optional_field->fld_name, wanted_fld_name)==0 ) { + if (optional_field && optional_field->fld_name && optional_field->fld_value && strcasecmp(optional_field->fld_name, wanted_fld_name)==0) { return optional_field; } } @@ -641,13 +641,13 @@ struct mailimf_optional_field* mailimf_find_optional_field(struct mailimf_fields static int mailmime_is_attachment_disposition(struct mailmime* 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 && field->fld_type == MAILMIME_FIELD_DISPOSITION && field->fld_data.fld_disposition ) { - if( field->fld_data.fld_disposition->dsp_type - && field->fld_data.fld_disposition->dsp_type->dsp_type==MAILMIME_DISPOSITION_TYPE_ATTACHMENT ) + if (field && field->fld_type == MAILMIME_FIELD_DISPOSITION && field->fld_data.fld_disposition) { + if (field->fld_data.fld_disposition->dsp_type + && field->fld_data.fld_disposition->dsp_type->dsp_type==MAILMIME_DISPOSITION_TYPE_ATTACHMENT) { return 1; } @@ -676,27 +676,27 @@ static int mailmime_get_mime_type(struct mailmime* mime, int* msg_type) #define DC_MIMETYPE_AC_SETUP_FILE 111 struct mailmime_content* c = mime->mm_content_type; - int dummy; if( msg_type == NULL ) { msg_type = &dummy; } + int dummy; if (msg_type == NULL) { msg_type = &dummy; } *msg_type = DC_MSG_UNDEFINED; - if( c == NULL || c->ct_type == NULL ) { + if (c == NULL || c->ct_type == NULL) { return 0; } - switch( c->ct_type->tp_type ) + switch (c->ct_type->tp_type) { case MAILMIME_TYPE_DISCRETE_TYPE: - switch( c->ct_type->tp_data.tp_discrete_type->dt_type ) + switch (c->ct_type->tp_data.tp_discrete_type->dt_type) { case MAILMIME_DISCRETE_TYPE_TEXT: - if( mailmime_is_attachment_disposition(mime) ) { + if (mailmime_is_attachment_disposition(mime)) { ; /* DC_MIMETYPE_FILE is returned below - we leave text attachments as attachments as they may be too large to display as a normal message, eg. complete books. */ } - else if( strcmp(c->ct_subtype, "plain")==0 ) { + else if (strcmp(c->ct_subtype, "plain")==0) { *msg_type = DC_MSG_TEXT; return DC_MIMETYPE_TEXT_PLAIN; } - else if( strcmp(c->ct_subtype, "html")==0 ) { + else if (strcmp(c->ct_subtype, "html")==0) { *msg_type = DC_MSG_TEXT; return DC_MIMETYPE_TEXT_HTML; } @@ -704,10 +704,10 @@ static int mailmime_get_mime_type(struct mailmime* mime, int* msg_type) return DC_MIMETYPE_FILE; case MAILMIME_DISCRETE_TYPE_IMAGE: - if( strcmp(c->ct_subtype, "gif")==0 ) { + if (strcmp(c->ct_subtype, "gif")==0) { *msg_type = DC_MSG_GIF; } - else if( strcmp(c->ct_subtype, "svg+xml")==0 ) { + else if (strcmp(c->ct_subtype, "svg+xml")==0) { *msg_type = DC_MSG_FILE; return DC_MIMETYPE_FILE; } @@ -726,8 +726,8 @@ static int mailmime_get_mime_type(struct mailmime* mime, int* msg_type) default: *msg_type = DC_MSG_FILE; - if( c->ct_type->tp_data.tp_discrete_type->dt_type == MAILMIME_DISCRETE_TYPE_APPLICATION - && strcmp(c->ct_subtype, "autocrypt-setup")==0 ) { + if (c->ct_type->tp_data.tp_discrete_type->dt_type == MAILMIME_DISCRETE_TYPE_APPLICATION + && strcmp(c->ct_subtype, "autocrypt-setup")==0) { return DC_MIMETYPE_AC_SETUP_FILE; /* application/autocrypt-setup */ } return DC_MIMETYPE_FILE; @@ -735,31 +735,31 @@ static int mailmime_get_mime_type(struct mailmime* mime, int* msg_type) break; case MAILMIME_TYPE_COMPOSITE_TYPE: - if( c->ct_type->tp_data.tp_composite_type->ct_type == MAILMIME_COMPOSITE_TYPE_MULTIPART ) + if (c->ct_type->tp_data.tp_composite_type->ct_type == MAILMIME_COMPOSITE_TYPE_MULTIPART) { - if( strcmp(c->ct_subtype, "alternative")==0 ) { + if (strcmp(c->ct_subtype, "alternative")==0) { return DC_MIMETYPE_MP_ALTERNATIVE; } - else if( strcmp(c->ct_subtype, "related")==0 ) { + else if (strcmp(c->ct_subtype, "related")==0) { return DC_MIMETYPE_MP_RELATED; } - else if( strcmp(c->ct_subtype, "encrypted")==0 ) { + else if (strcmp(c->ct_subtype, "encrypted")==0) { return DC_MIMETYPE_MP_NOT_DECRYPTABLE; /* decryptable parts are already converted to other mime parts in dc_e2ee_decrypt() */ } - else if( strcmp(c->ct_subtype, "signed")==0 ) { + else if (strcmp(c->ct_subtype, "signed")==0) { return DC_MIMETYPE_MP_SIGNED; } - else if( strcmp(c->ct_subtype, "mixed")==0 ) { + else if (strcmp(c->ct_subtype, "mixed")==0) { return DC_MIMETYPE_MP_MIXED; } - else if( strcmp(c->ct_subtype, "report")==0 ) { + else if (strcmp(c->ct_subtype, "report")==0) { return DC_MIMETYPE_MP_REPORT; } else { return DC_MIMETYPE_MP_OTHER; } } - else if( c->ct_type->tp_data.tp_composite_type->ct_type == MAILMIME_COMPOSITE_TYPE_MESSAGE ) + else if (c->ct_type->tp_data.tp_composite_type->ct_type == MAILMIME_COMPOSITE_TYPE_MESSAGE) { /* Enacapsulated messages, see https://www.w3.org/Protocols/rfc1341/7_3_Message.html Also used as part "message/disposition-notification" of "multipart/report", which, however, will be handled separatedly. @@ -787,7 +787,7 @@ static dc_mimepart_t* dc_mimepart_new(void) { dc_mimepart_t* mimepart = NULL; - if( (mimepart=calloc(1, sizeof(dc_mimepart_t)))==NULL ) { + if ((mimepart=calloc(1, sizeof(dc_mimepart_t)))==NULL) { exit(33); } @@ -800,16 +800,16 @@ static dc_mimepart_t* dc_mimepart_new(void) static void dc_mimepart_unref(dc_mimepart_t* mimepart) { - if( mimepart == NULL ) { + if (mimepart == NULL) { return; } - if( mimepart->msg ) { + if (mimepart->msg) { free(mimepart->msg); mimepart->msg = NULL; } - if( mimepart->msg_raw ) { + if (mimepart->msg_raw) { free(mimepart->msg_raw); mimepart->msg_raw = NULL; } @@ -838,7 +838,7 @@ dc_mimeparser_t* dc_mimeparser_new(const char* blobdir, dc_context_t* context) { dc_mimeparser_t* mimeparser = NULL; - if( (mimeparser=calloc(1, sizeof(dc_mimeparser_t)))==NULL ) { + if ((mimeparser=calloc(1, sizeof(dc_mimeparser_t)))==NULL) { exit(30); } @@ -867,13 +867,13 @@ dc_mimeparser_t* dc_mimeparser_new(const char* blobdir, dc_context_t* context) */ void dc_mimeparser_unref(dc_mimeparser_t* mimeparser) { - if( mimeparser == NULL ) { + if (mimeparser == NULL) { return; } dc_mimeparser_empty(mimeparser); - if( mimeparser->parts ) { carray_free(mimeparser->parts); } - if( mimeparser->reports ) { carray_free(mimeparser->reports); } + if (mimeparser->parts) { carray_free(mimeparser->parts); } + if (mimeparser->reports) { carray_free(mimeparser->reports); } free(mimeparser->e2ee_helper); free(mimeparser); } @@ -893,16 +893,16 @@ void dc_mimeparser_unref(dc_mimeparser_t* mimeparser) */ void dc_mimeparser_empty(dc_mimeparser_t* mimeparser) { - if( mimeparser == NULL ) { + if (mimeparser == NULL) { return; } - if( mimeparser->parts ) + if (mimeparser->parts) { int i, cnt = carray_count(mimeparser->parts); - for( i = 0; i < cnt; i++ ) { + for (i = 0; i < cnt; i++) { dc_mimepart_t* part = (dc_mimepart_t*)carray_get(mimeparser->parts, i); - if( part ) { + if (part) { dc_mimepart_unref(part); } } @@ -912,7 +912,7 @@ void dc_mimeparser_empty(dc_mimeparser_t* mimeparser) mimeparser->header_root = NULL; /* a pointer somewhere to the MIME data, must NOT be freed */ dc_hash_clear(&mimeparser->header); - if( mimeparser->header_protected ) { + if (mimeparser->header_protected) { mailimf_fields_free(mimeparser->header_protected); /* allocated as needed, MUST be freed */ mimeparser->header_protected = NULL; } @@ -923,7 +923,7 @@ void dc_mimeparser_empty(dc_mimeparser_t* mimeparser) free(mimeparser->subject); mimeparser->subject = NULL; - if( mimeparser->mimeroot ) + if (mimeparser->mimeroot) { mailmime_free(mimeparser->mimeroot); mimeparser->mimeroot = NULL; @@ -931,7 +931,7 @@ void dc_mimeparser_empty(dc_mimeparser_t* mimeparser) mimeparser->is_forwarded = 0; - if( mimeparser->reports ) { + if (mimeparser->reports) { carray_set_size(mimeparser->reports, 0); } @@ -944,10 +944,10 @@ void dc_mimeparser_empty(dc_mimeparser_t* mimeparser) static void do_add_single_part(dc_mimeparser_t* parser, dc_mimepart_t* part) { /* add a single part to the list of parts, the parser takes the ownership of the part, so you MUST NOT unref it after calling this function. */ - if( parser->e2ee_helper->encrypted && dc_hash_count(parser->e2ee_helper->signatures)>0 ) { + if (parser->e2ee_helper->encrypted && dc_hash_count(parser->e2ee_helper->signatures)>0) { dc_param_set_int(part->param, DC_PARAM_GUARANTEE_E2EE, 1); } - else if( parser->e2ee_helper->encrypted ) { + else if (parser->e2ee_helper->encrypted) { dc_param_set_int(part->param, DC_PARAM_ERRONEOUS_E2EE, DC_E2EE_NO_VALID_SIGNATURE); } carray_add(parser->parts, (void*)part, NULL); @@ -962,12 +962,12 @@ static void do_add_single_file_part(dc_mimeparser_t* parser, int msg_type, int m char* pathNfilename = NULL; /* create a free file name to use */ - if( (pathNfilename=dc_get_fine_pathNfilename(parser->blobdir, desired_filename)) == NULL ) { + if ((pathNfilename=dc_get_fine_pathNfilename(parser->blobdir, desired_filename)) == NULL) { goto cleanup; } /* copy data to file */ - if( dc_write_file(pathNfilename, decoded_data, decoded_data_bytes, parser->context)==0 ) { + if (dc_write_file(pathNfilename, decoded_data, decoded_data_bytes, parser->context)==0) { goto cleanup; } @@ -976,23 +976,23 @@ static void do_add_single_file_part(dc_mimeparser_t* parser, int msg_type, int m part->int_mimetype = mime_type; part->bytes = decoded_data_bytes; dc_param_set(part->param, DC_PARAM_FILE, pathNfilename); - if( DC_MSG_MAKE_FILENAME_SEARCHABLE(msg_type) ) { + if (DC_MSG_MAKE_FILENAME_SEARCHABLE(msg_type)) { part->msg = dc_get_filename(pathNfilename); } - else if( DC_MSG_MAKE_SUFFIX_SEARCHABLE(msg_type) ) { + else if (DC_MSG_MAKE_SUFFIX_SEARCHABLE(msg_type)) { part->msg = dc_get_filesuffix_lc(pathNfilename); } - if( mime_type == DC_MIMETYPE_IMAGE ) { + if (mime_type == DC_MIMETYPE_IMAGE) { uint32_t w = 0, h = 0; - if( dc_get_filemeta(decoded_data, decoded_data_bytes, &w, &h) ) { + if (dc_get_filemeta(decoded_data, decoded_data_bytes, &w, &h)) { dc_param_set_int(part->param, DC_PARAM_WIDTH, w); dc_param_set_int(part->param, DC_PARAM_HEIGHT, h); } } /* split author/title from the original filename (if we do it from the real filename, we'll also get numbers appended by dc_get_fine_pathNfilename()) */ - if( msg_type == DC_MSG_AUDIO ) { + if (msg_type == DC_MSG_AUDIO) { char* author = NULL, *title = NULL; dc_msg_get_authorNtitle_from_filename(desired_filename, &author, &title); dc_param_set(part->param, DC_PARAM_AUTHORNAME, author); @@ -1027,7 +1027,7 @@ static int dc_mimeparser_add_single_part_if_known(dc_mimeparser_t* mimeparser, s size_t decoded_data_bytes = 0; dc_simplify_t* simplifier = NULL; - if( mime == NULL || mime->mm_data.mm_single == NULL ) { + if (mime == NULL || mime->mm_data.mm_single == NULL) { goto cleanup; } @@ -1036,39 +1036,39 @@ static int dc_mimeparser_add_single_part_if_known(dc_mimeparser_t* mimeparser, s /* 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; } /* regard `Content-Transfer-Encoding:` */ - if( !mailmime_transfer_decode(mime, &decoded_data, &decoded_data_bytes, &transfer_decoding_buffer) ) { + if (!mailmime_transfer_decode(mime, &decoded_data, &decoded_data_bytes, &transfer_decoding_buffer)) { goto cleanup; /* no always error - but no data */ } - switch( mime_type ) + switch (mime_type) { case DC_MIMETYPE_TEXT_PLAIN: case DC_MIMETYPE_TEXT_HTML: { - if( simplifier==NULL ) { + if (simplifier==NULL) { simplifier = dc_simplify_new(); - if( simplifier==NULL ) { + if (simplifier==NULL) { goto cleanup; } } const char* charset = mailmime_content_charset_get(mime->mm_content_type); /* get from `Content-Type: text/...; charset=utf-8`; must not be free()'d */ - 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) { size_t ret_bytes = 0; int r = charconv_buffer("utf-8", charset, decoded_data, decoded_data_bytes, &charset_buffer, &ret_bytes); - if( r != MAIL_CHARCONV_NO_ERROR ) { + if (r != MAIL_CHARCONV_NO_ERROR) { dc_log_warning(mimeparser->context, 0, "Cannot convert %i bytes from \"%s\" to \"utf-8\"; errorcode is %i.", /* if this warning comes up for usual character sets, maybe libetpan is compiled without iconv? */ (int)decoded_data_bytes, charset, (int)r); /* continue, however */ } - else if( charset_buffer==NULL || ret_bytes <= 0 ) { + else if (charset_buffer==NULL || ret_bytes <= 0) { goto cleanup; /* no error - but nothing to add */ } else { @@ -1083,10 +1083,10 @@ static int dc_mimeparser_add_single_part_if_known(dc_mimeparser_t* mimeparser, s char* uu_blob = NULL, *uu_filename = NULL, *new_txt = NULL; size_t uu_blob_bytes = 0; int uu_msg_type = 0, added_uu_parts = 0; - while( (new_txt=dc_uudecode_do(txt, &uu_blob, &uu_blob_bytes, &uu_filename)) != NULL ) + while ((new_txt=dc_uudecode_do(txt, &uu_blob, &uu_blob_bytes, &uu_filename)) != NULL) { dc_msg_guess_msgtype_from_suffix(uu_filename, &uu_msg_type, NULL); - if( uu_msg_type == 0 ) { + if (uu_msg_type == 0) { uu_msg_type = DC_MSG_FILE; } @@ -1097,7 +1097,7 @@ static int dc_mimeparser_add_single_part_if_known(dc_mimeparser_t* mimeparser, s free(uu_filename); uu_filename = NULL; added_uu_parts++; - if( added_uu_parts > 50/*fence against endless loops*/ ) { + if (added_uu_parts > 50/*fence against endless loops*/) { break; } } @@ -1107,7 +1107,7 @@ static int dc_mimeparser_add_single_part_if_known(dc_mimeparser_t* mimeparser, s char* simplified_txt = dc_simplify_simplify(simplifier, txt, strlen(txt), mime_type==DC_MIMETYPE_TEXT_HTML? 1 : 0); free(txt); txt = NULL; - if( simplified_txt && simplified_txt[0] ) + if (simplified_txt && simplified_txt[0]) { part = dc_mimepart_new(); part->type = DC_MSG_TEXT; @@ -1122,7 +1122,7 @@ static int dc_mimeparser_add_single_part_if_known(dc_mimeparser_t* mimeparser, s free(simplified_txt); } - if( simplifier->is_forwarded ) { + if (simplifier->is_forwarded) { mimeparser->is_forwarded = 1; } } @@ -1140,27 +1140,27 @@ static int dc_mimeparser_add_single_part_if_known(dc_mimeparser_t* mimeparser, s or `Content-Disposition: ... filename=...` */ dc_strbuilder_t filename_parts; dc_strbuilder_init(&filename_parts, 0); - for( clistiter* cur1 = clist_begin(mime->mm_mime_fields->fld_list); cur1 != NULL; cur1 = clist_next(cur1) ) + for (clistiter* cur1 = clist_begin(mime->mm_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) { - for( clistiter* cur2 = clist_begin(file_disposition->dsp_parms); cur2 != NULL; cur2 = clist_next(cur2) ) + for (clistiter* cur2 = clist_begin(file_disposition->dsp_parms); cur2 != NULL; cur2 = clist_next(cur2)) { struct mailmime_disposition_parm* dsp_param = (struct mailmime_disposition_parm*)clist_content(cur2); - if( dsp_param ) + if (dsp_param) { - if( dsp_param->pa_type==MAILMIME_DISPOSITION_PARM_PARAMETER + if (dsp_param->pa_type==MAILMIME_DISPOSITION_PARM_PARAMETER && dsp_param->pa_data.pa_parameter && dsp_param->pa_data.pa_parameter->pa_name - && strncmp(dsp_param->pa_data.pa_parameter->pa_name, "filename*", 9)==0 ) + && strncmp(dsp_param->pa_data.pa_parameter->pa_name, "filename*", 9)==0) { dc_strbuilder_cat(&filename_parts, dsp_param->pa_data.pa_parameter->pa_value); // we assume the filename*?* parts are in order, not seen anything else yet } - else if( dsp_param->pa_type==MAILMIME_DISPOSITION_PARM_FILENAME ) + else if (dsp_param->pa_type==MAILMIME_DISPOSITION_PARM_FILENAME) { desired_filename = dc_decode_header_words(dsp_param->pa_data.pa_filename); // this is used only if the parts buffer stays empty } @@ -1171,7 +1171,7 @@ static int dc_mimeparser_add_single_part_if_known(dc_mimeparser_t* mimeparser, s } } - if( strlen(filename_parts.buf) ) { + if (strlen(filename_parts.buf)) { free(desired_filename); desired_filename = dc_decode_ext_header(filename_parts.buf); } @@ -1179,16 +1179,16 @@ static int dc_mimeparser_add_single_part_if_known(dc_mimeparser_t* mimeparser, s free(filename_parts.buf); /* try to get file name from `Content-Type: ... name=...` */ - if( desired_filename==NULL ) { + if (desired_filename==NULL) { struct mailmime_parameter* param = mailmime_find_ct_parameter(mime, "name"); - if( param && param->pa_value && param->pa_value[0] ) { + if (param && param->pa_value && param->pa_value[0]) { desired_filename = dc_strdup(param->pa_value);// is already decoded, see #162 } } /* if there is still no filename, guess one */ - if( desired_filename==NULL ) { - if( mime->mm_content_type && mime->mm_content_type->ct_subtype ) { + if (desired_filename==NULL) { + if (mime->mm_content_type && mime->mm_content_type->ct_subtype) { desired_filename = dc_mprintf("file.%s", mime->mm_content_type->ct_subtype); } else { @@ -1209,8 +1209,8 @@ static int dc_mimeparser_add_single_part_if_known(dc_mimeparser_t* mimeparser, s /* add object? (we do not add all objetcs, eg. signatures etc. are ignored) */ cleanup: dc_simplify_unref(simplifier); - if( charset_buffer ) { charconv_buffer_free(charset_buffer); } - if( transfer_decoding_buffer ) { mmap_string_unref(transfer_decoding_buffer); } + if (charset_buffer) { charconv_buffer_free(charset_buffer); } + if (transfer_decoding_buffer) { mmap_string_unref(transfer_decoding_buffer); } free(file_suffix); free(desired_filename); dc_mimepart_unref(part); @@ -1224,25 +1224,25 @@ static int dc_mimeparser_parse_mime_recursive(dc_mimeparser_t* mimeparser, struc int any_part_added = 0; clistiter* cur = NULL; - if( mimeparser == NULL || mime == NULL ) { + if (mimeparser == NULL || mime == NULL) { return 0; } - if( mailmime_find_ct_parameter(mime, "protected-headers") ) + if (mailmime_find_ct_parameter(mime, "protected-headers")) { - if( mime->mm_type==MAILMIME_SINGLE + if (mime->mm_type==MAILMIME_SINGLE && mime->mm_content_type->ct_type->tp_type==MAILMIME_TYPE_DISCRETE_TYPE && mime->mm_content_type->ct_type->tp_data.tp_discrete_type->dt_type==MAILMIME_DISCRETE_TYPE_TEXT && mime->mm_content_type->ct_subtype - && strcmp(mime->mm_content_type->ct_subtype, "rfc822-headers")==0 ) { + && strcmp(mime->mm_content_type->ct_subtype, "rfc822-headers")==0) { dc_log_info(mimeparser->context, 0, "Protected headers found in text/rfc822-headers attachment: Will be ignored."); /* we want the protected headers in the normal header of the payload */ return 0; } - if( mimeparser->header_protected==NULL ) { /* use the most outer protected header - this is typically created in sync with the normal, unprotected header */ + if (mimeparser->header_protected==NULL) { /* use the most outer protected header - this is typically created in sync with the normal, unprotected header */ size_t dummy = 0; - if( mailimf_envelope_and_optional_fields_parse(mime->mm_mime_start, mime->mm_length, &dummy, &mimeparser->header_protected)!=MAILIMF_NO_ERROR - || mimeparser->header_protected==NULL ) { + if (mailimf_envelope_and_optional_fields_parse(mime->mm_mime_start, mime->mm_length, &dummy, &mimeparser->header_protected)!=MAILIMF_NO_ERROR + || mimeparser->header_protected==NULL) { dc_log_warning(mimeparser->context, 0, "Protected headers parsing error."); } } @@ -1251,41 +1251,41 @@ static int dc_mimeparser_parse_mime_recursive(dc_mimeparser_t* mimeparser, struc } } - switch( mime->mm_type ) + switch (mime->mm_type) { case MAILMIME_SINGLE: any_part_added = dc_mimeparser_add_single_part_if_known(mimeparser, mime); break; case MAILMIME_MULTIPLE: - switch( mailmime_get_mime_type(mime, NULL) ) + switch (mailmime_get_mime_type(mime, NULL)) { case DC_MIMETYPE_MP_ALTERNATIVE: /* add "best" part */ /* Most times, mutlipart/alternative contains true alternatives as text/plain and text/html. If we find a multipart/mixed inside mutlipart/alternative, we use this (happens eg in apple mail: "plaintext" as an alternative to "html+PDF attachment") */ - 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* childmime = (struct mailmime*)clist_content(cur); - if( mailmime_get_mime_type(childmime, NULL) == DC_MIMETYPE_MP_MIXED ) { + if (mailmime_get_mime_type(childmime, NULL) == DC_MIMETYPE_MP_MIXED) { any_part_added = dc_mimeparser_parse_mime_recursive(mimeparser, childmime); break; } } - if( !any_part_added ) { + if (!any_part_added) { /* search for text/plain and add this */ - 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* childmime = (struct mailmime*)clist_content(cur); - if( mailmime_get_mime_type(childmime, NULL) == DC_MIMETYPE_TEXT_PLAIN ) { + if (mailmime_get_mime_type(childmime, NULL) == DC_MIMETYPE_TEXT_PLAIN) { any_part_added = dc_mimeparser_parse_mime_recursive(mimeparser, childmime); break; } } } - if( !any_part_added ) { /* `text/plain` not found - use the first part */ - for( cur=clist_begin(mime->mm_data.mm_multipart.mm_mp_list); cur!=NULL; cur=clist_next(cur)) { - if( dc_mimeparser_parse_mime_recursive(mimeparser, (struct mailmime*)clist_content(cur)) ) { + if (!any_part_added) { /* `text/plain` not found - use the first part */ + for (cur=clist_begin(mime->mm_data.mm_multipart.mm_mp_list); cur!=NULL; cur=clist_next(cur)) { + if (dc_mimeparser_parse_mime_recursive(mimeparser, (struct mailmime*)clist_content(cur))) { any_part_added = 1; break; /* out of for() */ } @@ -1296,7 +1296,7 @@ static int dc_mimeparser_parse_mime_recursive(dc_mimeparser_t* mimeparser, struc case DC_MIMETYPE_MP_RELATED: /* add the "root part" - the other parts may be referenced which is not interesting for us (eg. embedded images) */ /* we assume he "root part" being the first one, which may not be always true ... however, most times it seems okay. */ cur=clist_begin(mime->mm_data.mm_multipart.mm_mp_list); - if( cur ) { + if (cur) { any_part_added = dc_mimeparser_parse_mime_recursive(mimeparser, (struct mailmime*)clist_content(cur)); } break; @@ -1322,18 +1322,18 @@ static int dc_mimeparser_parse_mime_recursive(dc_mimeparser_t* mimeparser, struc The second body part contains the control information necessary to verify the digital signature." We simpliy take the first body part and skip the rest. (see https://k9mail.github.io/2016/11/24/OpenPGP-Considerations-Part-I.html for background information why we use encrypted+signed) */ - if( (cur=clist_begin(mime->mm_data.mm_multipart.mm_mp_list)) != NULL ) + if ((cur=clist_begin(mime->mm_data.mm_multipart.mm_mp_list)) != NULL) { any_part_added = dc_mimeparser_parse_mime_recursive(mimeparser, (struct mailmime*)clist_content(cur)); } break; case DC_MIMETYPE_MP_REPORT: - if( clist_count(mime->mm_data.mm_multipart.mm_mp_list) >= 2 ) /* RFC 6522: the first part is for humans, the second for machines */ + if (clist_count(mime->mm_data.mm_multipart.mm_mp_list) >= 2) /* RFC 6522: the first part is for humans, the second for machines */ { struct mailmime_parameter* report_type = mailmime_find_ct_parameter(mime, "report-type"); - if( report_type && report_type->pa_value - && strcmp(report_type->pa_value, "disposition-notification") == 0 ) + if (report_type && report_type->pa_value + && strcmp(report_type->pa_value, "disposition-notification") == 0) { carray_add(mimeparser->reports, (void*)mime, NULL); } @@ -1354,27 +1354,27 @@ static int dc_mimeparser_parse_mime_recursive(dc_mimeparser_t* mimeparser, struc { struct mailmime* html_part = NULL; int plain_cnt = 0, html_cnt = 0; - 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* childmime = (struct mailmime*)clist_content(cur); - if( mailmime_get_mime_type(childmime, NULL) == DC_MIMETYPE_TEXT_PLAIN ) { + if (mailmime_get_mime_type(childmime, NULL) == DC_MIMETYPE_TEXT_PLAIN) { plain_cnt++; } - else if( mailmime_get_mime_type(childmime, NULL) == DC_MIMETYPE_TEXT_HTML ) { + else if (mailmime_get_mime_type(childmime, NULL) == DC_MIMETYPE_TEXT_HTML) { html_part = childmime; html_cnt++; } } - if( plain_cnt==1 && html_cnt==1 ) { + if (plain_cnt==1 && html_cnt==1) { dc_log_warning(mimeparser->context, 0, "HACK: multipart/mixed message found with PLAIN and HTML, we'll skip the HTML part as this seems to be unwanted."); skip_part = html_part; } } /* /HACK */ - 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* childmime = (struct mailmime*)clist_content(cur); - if( childmime != skip_part ) { - if( dc_mimeparser_parse_mime_recursive(mimeparser, childmime) ) { + if (childmime != skip_part) { + if (dc_mimeparser_parse_mime_recursive(mimeparser, childmime)) { any_part_added = 1; } } @@ -1385,12 +1385,12 @@ static int dc_mimeparser_parse_mime_recursive(dc_mimeparser_t* mimeparser, struc break; case MAILMIME_MESSAGE: - if( mimeparser->header_root == NULL ) + if (mimeparser->header_root == NULL) { mimeparser->header_root = mime->mm_data.mm_message.mm_fields; } - if( mime->mm_data.mm_message.mm_msg_mime ) + if (mime->mm_data.mm_message.mm_msg_mime) { any_part_added = dc_mimeparser_parse_mime_recursive(mimeparser, mime->mm_data.mm_message.mm_msg_mime); } @@ -1428,22 +1428,22 @@ static void hash_header(dc_hash_t* out, const struct mailimf_fields* in, dc_cont case MAILIMF_FIELD_OPTIONAL_FIELD: { const struct mailimf_optional_field* optional_field = field->fld_data.fld_optional_field; - if( optional_field ) { + if (optional_field) { key = optional_field->fld_name; } } break; } - if( key ) + if (key) { int key_len = strlen(key); - if( dc_hash_find(out, key, key_len) ) + if (dc_hash_find(out, key, key_len)) { /* key already in hash, do only overwrite known types */ - if( field->fld_type!=MAILIMF_FIELD_OPTIONAL_FIELD - || (key_len>5 && strncasecmp(key, "Chat-", 5)==0) ) + if (field->fld_type!=MAILIMF_FIELD_OPTIONAL_FIELD + || (key_len>5 && strncasecmp(key, "Chat-", 5)==0)) { //dc_log_info(context, 0, "Protected headers: Overwriting \"%s\".", key); dc_hash_insert(out, key, key_len, field); @@ -1484,7 +1484,7 @@ void dc_mimeparser_parse(dc_mimeparser_t* mimeparser, const char* body_not_termi /* parse body */ r = mailmime_parse(body_not_terminated, body_bytes, &index, &mimeparser->mimeroot); - if(r != MAILIMF_NO_ERROR || mimeparser->mimeroot == NULL ) { + if(r != MAILIMF_NO_ERROR || mimeparser->mimeroot == NULL) { goto cleanup; } @@ -1510,30 +1510,30 @@ void dc_mimeparser_parse(dc_mimeparser_t* mimeparser, const char* body_not_termi /* set some basic data */ { struct mailimf_field* field = dc_mimeparser_lookup_field(mimeparser, "Subject"); - if( field && field->fld_type == MAILIMF_FIELD_SUBJECT ) { + if (field && field->fld_type == MAILIMF_FIELD_SUBJECT) { mimeparser->subject = dc_decode_header_words(field->fld_data.fld_subject->sbj_value); } } - if( dc_mimeparser_lookup_optional_field2(mimeparser, "Chat-Version", "X-MrMsg") ) { + if (dc_mimeparser_lookup_optional_field2(mimeparser, "Chat-Version", "X-MrMsg")) { mimeparser->is_send_by_messenger = 1; } - if( dc_mimeparser_lookup_field(mimeparser, "Autocrypt-Setup-Message") ) { + if (dc_mimeparser_lookup_field(mimeparser, "Autocrypt-Setup-Message")) { /* Autocrypt-Setup-Message header found - check if there is an application/autocrypt-setup part */ int i, has_setup_file = 0; - for( i = 0; i < carray_count(mimeparser->parts); i++ ) { + for (i = 0; i < carray_count(mimeparser->parts); i++) { dc_mimepart_t* part = (dc_mimepart_t*)carray_get(mimeparser->parts, i); - if( part->int_mimetype==DC_MIMETYPE_AC_SETUP_FILE ) { + if (part->int_mimetype==DC_MIMETYPE_AC_SETUP_FILE) { has_setup_file = 1; } } - if( has_setup_file ) { + if (has_setup_file) { /* delete all parts but the application/autocrypt-setup part */ mimeparser->is_system_message = DC_CMD_AUTOCRYPT_SETUP_MESSAGE; - for( i = 0; i < carray_count(mimeparser->parts); i++ ) { + for (i = 0; i < carray_count(mimeparser->parts); i++) { dc_mimepart_t* part = (dc_mimepart_t*)carray_get(mimeparser->parts, i); - if( part->int_mimetype!=DC_MIMETYPE_AC_SETUP_FILE ) { + if (part->int_mimetype!=DC_MIMETYPE_AC_SETUP_FILE) { dc_mimepart_unref(part); carray_delete_slow(mimeparser->parts, i); i--; /* start over with the same index */ @@ -1544,33 +1544,33 @@ void dc_mimeparser_parse(dc_mimeparser_t* mimeparser, const char* body_not_termi } /* prepend subject to message? */ - if( mimeparser->subject ) + if (mimeparser->subject) { int prepend_subject = 1; - if( !mimeparser->decrypting_failed /* if decryption has failed, we always prepend the subject as this may contain cleartext hints from non-Delta MUAs. */ ) + if (!mimeparser->decrypting_failed /* if decryption has failed, we always prepend the subject as this may contain cleartext hints from non-Delta MUAs. */) { char* p = strchr(mimeparser->subject, ':'); - if( (p-mimeparser->subject) == 2 /*Re: etc.*/ + if ((p-mimeparser->subject) == 2 /*Re: etc.*/ || (p-mimeparser->subject) == 3 /*Fwd: etc.*/ || mimeparser->is_send_by_messenger - || strstr(mimeparser->subject, DC_CHAT_PREFIX)!=NULL ) { + || strstr(mimeparser->subject, DC_CHAT_PREFIX)!=NULL) { prepend_subject = 0; } } - if( prepend_subject ) + if (prepend_subject) { char* subj = dc_strdup(mimeparser->subject); char* p = strchr(subj, '['); /* do not add any tags as "[checked by XYZ]" */ - if( p ) { + if (p) { *p = 0; } dc_trim(subj); - if( subj[0] ) { + if (subj[0]) { int i, icnt = carray_count(mimeparser->parts); /* should be at least one - maybe empty - part */ - for( i = 0; i < icnt; i++ ) { + for (i = 0; i < icnt; i++) { dc_mimepart_t* part = (dc_mimepart_t*)carray_get(mimeparser->parts, i); - if( part->type == DC_MSG_TEXT ) { + if (part->type == DC_MSG_TEXT) { #define DC_NDASH "\xE2\x80\x93" char* new_txt = dc_mprintf("%s " DC_NDASH " %s", subj, part->msg); free(part->msg); @@ -1584,21 +1584,21 @@ void dc_mimeparser_parse(dc_mimeparser_t* mimeparser, const char* body_not_termi } /* add forward information to every part */ - if( mimeparser->is_forwarded ) { + if (mimeparser->is_forwarded) { int i, icnt = carray_count(mimeparser->parts); /* should be at least one - maybe empty - part */ - for( i = 0; i < icnt; i++ ) { + for (i = 0; i < icnt; i++) { dc_mimepart_t* part = (dc_mimepart_t*)carray_get(mimeparser->parts, i); dc_param_set_int(part->param, DC_PARAM_FORWARDED, 1); } } - if( carray_count(mimeparser->parts)==1 ) + if (carray_count(mimeparser->parts)==1) { /* mark audio as voice message, if appropriate (we have to do this on global level as we do not know the global header in the recursice parse). and read some additional parameters */ dc_mimepart_t* part = (dc_mimepart_t*)carray_get(mimeparser->parts, 0); - if( part->type == DC_MSG_AUDIO ) { - if( dc_mimeparser_lookup_optional_field2(mimeparser, "Chat-Voice-Message", "X-MrVoiceMessage") ) { + if (part->type == DC_MSG_AUDIO) { + if (dc_mimeparser_lookup_optional_field2(mimeparser, "Chat-Voice-Message", "X-MrVoiceMessage")) { free(part->msg); part->msg = strdup("ogg"); /* DC_MSG_AUDIO adds sets the whole filename which is useless. however, the extension is useful. */ part->type = DC_MSG_VOICE; @@ -1607,11 +1607,11 @@ void dc_mimeparser_parse(dc_mimeparser_t* mimeparser, const char* body_not_termi } } - if( part->type == DC_MSG_AUDIO || part->type == DC_MSG_VOICE || part->type == DC_MSG_VIDEO ) { + if (part->type == DC_MSG_AUDIO || part->type == DC_MSG_VOICE || part->type == DC_MSG_VIDEO) { const struct mailimf_optional_field* field = dc_mimeparser_lookup_optional_field2(mimeparser, "Chat-Duration", "X-MrDurationMs"); - if( field ) { + if (field) { int duration_ms = atoi(field->fld_value); - if( duration_ms > 0 && duration_ms < 24*60*60*1000 ) { + if (duration_ms > 0 && duration_ms < 24*60*60*1000) { dc_param_set_int(part->param, DC_PARAM_DURATION, duration_ms); } } @@ -1619,14 +1619,14 @@ void dc_mimeparser_parse(dc_mimeparser_t* mimeparser, const char* body_not_termi } /* some special system message? */ - if( dc_mimeparser_lookup_field(mimeparser, "Chat-Group-Image") - && carray_count(mimeparser->parts)>=1 ) { + if (dc_mimeparser_lookup_field(mimeparser, "Chat-Group-Image") + && carray_count(mimeparser->parts)>=1) { dc_mimepart_t* textpart = (dc_mimepart_t*)carray_get(mimeparser->parts, 0); - if( textpart->type == DC_MSG_TEXT ) { + if (textpart->type == DC_MSG_TEXT) { dc_param_set_int(textpart->param, DC_PARAM_CMD, DC_CMD_GROUPIMAGE_CHANGED); - if( carray_count(mimeparser->parts)>=2 ) { + if (carray_count(mimeparser->parts)>=2) { dc_mimepart_t* imgpart = (dc_mimepart_t*)carray_get(mimeparser->parts, 1); - if( imgpart->type == DC_MSG_IMAGE ) { + if (imgpart->type == DC_MSG_IMAGE) { imgpart->is_meta = 1; } } @@ -1634,31 +1634,31 @@ void dc_mimeparser_parse(dc_mimeparser_t* mimeparser, const char* body_not_termi } /* check, if the message asks for a MDN */ - if( !mimeparser->decrypting_failed ) + if (!mimeparser->decrypting_failed) { const struct mailimf_optional_field* dn_field = dc_mimeparser_lookup_optional_field(mimeparser, "Chat-Disposition-Notification-To"); /* 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. */ - if( dn_field && dc_mimeparser_get_last_nonmeta(mimeparser)/*just check if the mail is not empty*/ ) + if (dn_field && dc_mimeparser_get_last_nonmeta(mimeparser)/*just check if the mail is not empty*/) { struct mailimf_mailbox_list* mb_list = NULL; size_t index = 0; - if( mailimf_mailbox_list_parse(dn_field->fld_value, strlen(dn_field->fld_value), &index, &mb_list)==MAILIMF_NO_ERROR && mb_list ) + if (mailimf_mailbox_list_parse(dn_field->fld_value, strlen(dn_field->fld_value), &index, &mb_list)==MAILIMF_NO_ERROR && mb_list) { char* dn_to_addr = mailimf_find_first_addr(mb_list); - if( dn_to_addr ) + if (dn_to_addr) { struct mailimf_field* from_field = dc_mimeparser_lookup_field(mimeparser, "From"); /* we need From: as this MUST match Disposition-Notification-To: */ - if( from_field && from_field->fld_type==MAILIMF_FIELD_FROM && from_field->fld_data.fld_from ) + if (from_field && from_field->fld_type==MAILIMF_FIELD_FROM && from_field->fld_data.fld_from) { char* from_addr = mailimf_find_first_addr(from_field->fld_data.fld_from->frm_mb_list); - if( from_addr ) + if (from_addr) { - if( strcmp(from_addr, dn_to_addr)==0 ) + if (strcmp(from_addr, dn_to_addr)==0) { /* we mark _only_ the _last_ part to send a MDN (this avoids trouble with multi-part-messages who should send only one MDN. Moreover the last one is handy as it is the one typically displayed if the message is larger) */ dc_mimepart_t* part = dc_mimeparser_get_last_nonmeta(mimeparser); - if( part ) { + if (part) { dc_param_set_int(part->param, DC_PARAM_WANTS_MDN, 1); } } @@ -1674,7 +1674,7 @@ void dc_mimeparser_parse(dc_mimeparser_t* mimeparser, const char* body_not_termi /* Cleanup - and try to create at least an empty part if there are no parts yet */ cleanup: - if( !dc_mimeparser_has_nonmeta(mimeparser) && carray_count(mimeparser->reports)==0 ) { + if (!dc_mimeparser_has_nonmeta(mimeparser) && carray_count(mimeparser->reports)==0) { dc_mimepart_t* part = dc_mimepart_new(); part->type = DC_MSG_TEXT; part->msg = dc_strdup(mimeparser->subject? mimeparser->subject : "Empty message"); @@ -1718,7 +1718,7 @@ struct mailimf_field* dc_mimeparser_lookup_field(dc_mimeparser_t* mimeparser, co struct mailimf_optional_field* dc_mimeparser_lookup_optional_field(dc_mimeparser_t* mimeparser, const char* field_name) { struct mailimf_field* field = dc_hash_find_str(&mimeparser->header, field_name); - if( field && field->fld_type == MAILIMF_FIELD_OPTIONAL_FIELD ) { + if (field && field->fld_type == MAILIMF_FIELD_OPTIONAL_FIELD) { return field->fld_data.fld_optional_field; } return NULL; @@ -1751,11 +1751,11 @@ struct mailimf_optional_field* dc_mimeparser_lookup_optional_field2(dc_mimeparse */ dc_mimepart_t* dc_mimeparser_get_last_nonmeta(dc_mimeparser_t* mimeparser) { - if( mimeparser && mimeparser->parts ) { + if (mimeparser && mimeparser->parts) { int i, icnt = carray_count(mimeparser->parts); - for( i = icnt-1; i >= 0; i-- ) { + for (i = icnt-1; i >= 0; i--) { dc_mimepart_t* part = (dc_mimepart_t*)carray_get(mimeparser->parts, i); - if( part && !part->is_meta ) { + if (part && !part->is_meta) { return part; } } @@ -1814,18 +1814,18 @@ dc_mimepart_t* dc_mimeparser_get_last_nonmeta(dc_mimeparser_t* mimeparser) */ int dc_mimeparser_is_mailinglist_message(dc_mimeparser_t* mimeparser) { - if( mimeparser == NULL ) { + if (mimeparser == NULL) { return 0; } - if( dc_mimeparser_lookup_field(mimeparser, "List-Id") != NULL ) { + if (dc_mimeparser_lookup_field(mimeparser, "List-Id") != NULL) { return 1; /* mailing list identified by the presence of `List-ID` from RFC 2919 */ } struct mailimf_optional_field* precedence = dc_mimeparser_lookup_optional_field(mimeparser, "Precedence"); - if( precedence != NULL ) { - if( strcasecmp(precedence->fld_value, "list")==0 - || strcasecmp(precedence->fld_value, "bulk")==0 ) { + if (precedence != NULL) { + if (strcasecmp(precedence->fld_value, "list")==0 + || strcasecmp(precedence->fld_value, "bulk")==0) { return 1; /* mailing list identified by the presence of `Precedence: bulk` or `Precedence: list` from RFC 3834 */ } } @@ -1860,21 +1860,21 @@ int dc_mimeparser_sender_equals_recipient(dc_mimeparser_t* mimeparser) char* from_addr_norm = NULL; dc_hash_t* recipients = NULL; - if( mimeparser == NULL || mimeparser->header_root == NULL ) { + if (mimeparser == NULL || mimeparser->header_root == NULL) { goto cleanup; } /* get From: and check there is exactly one sender */ - if( (fld=mailimf_find_field(mimeparser->header_root, MAILIMF_FIELD_FROM)) == NULL + if ((fld=mailimf_find_field(mimeparser->header_root, MAILIMF_FIELD_FROM)) == NULL || (fld_from=fld->fld_data.fld_from) == NULL || fld_from->frm_mb_list == NULL || fld_from->frm_mb_list->mb_list == NULL - || clist_count(fld_from->frm_mb_list->mb_list) != 1 ) { + || clist_count(fld_from->frm_mb_list->mb_list) != 1) { goto cleanup; } mb = (struct mailimf_mailbox*)clist_content(clist_begin(fld_from->frm_mb_list->mb_list)); - if( mb == NULL ) { + if (mb == NULL) { goto cleanup; } @@ -1882,12 +1882,12 @@ int dc_mimeparser_sender_equals_recipient(dc_mimeparser_t* mimeparser) /* get To:/Cc: and check there is exactly one recipent */ recipients = mailimf_get_recipients(mimeparser->header_root); - if( dc_hash_count(recipients) != 1 ) { + if (dc_hash_count(recipients) != 1) { goto cleanup; } /* check if From: == To:/Cc: */ - if( dc_hash_find_str(recipients, from_addr_norm) ) { + if (dc_hash_find_str(recipients, from_addr_norm)) { sender_equals_recipient = 1; } diff --git a/src/dc_msg.c b/src/dc_msg.c index 925c4818..c5abb636 100644 --- a/src/dc_msg.c +++ b/src/dc_msg.c @@ -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); } diff --git a/src/dc_msg.h b/src/dc_msg.h index 52066e78..fc3a8228 100644 --- a/src/dc_msg.h +++ b/src/dc_msg.h @@ -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. diff --git a/src/dc_openssl.c b/src/dc_openssl.c index 864528d8..441b7ad9 100644 --- a/src/dc_openssl.c +++ b/src/dc_openssl.c @@ -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) diff --git a/src/dc_param.c b/src/dc_param.c index d896e4f4..f0c71782 100644 --- a/src/dc_param.c +++ b/src/dc_param.c @@ -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(¶m->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); diff --git a/src/dc_pgp.c b/src/dc_pgp.c index 9bca329e..2c203e3d 100644 --- a/src/dc_pgp.c +++ b/src/dc_pgp.c @@ -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; } diff --git a/src/dc_qr.c b/src/dc_qr.c index 5c7bb4c5..d2cbd8c7 100644 --- a/src/dc_qr.c +++ b/src/dc_qr.c @@ -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); } diff --git a/src/dc_receive_imf.c b/src/dc_receive_imf.c index ba737db4..4ca14939 100644 --- a/src/dc_receive_imf.c +++ b/src/dc_receive_imf.c @@ -40,27 +40,27 @@ static void add_or_lookup_contact_by_addr(dc_context_t* context, const char* dis { /* is addr_spec equal to SELF? */ int dummy; - if( check_self == NULL ) { check_self = &dummy; } + if (check_self == NULL) { check_self = &dummy; } - if( context == NULL || context->magic != DC_CONTEXT_MAGIC || addr_spec == NULL ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC || addr_spec == NULL) { return; } *check_self = 0; char* self_addr = dc_sqlite3_get_config(context->sql, "configured_addr", ""); - if( strcasecmp(self_addr, addr_spec)==0 ) { + if (strcasecmp(self_addr, addr_spec)==0) { *check_self = 1; } free(self_addr); - if( *check_self ) { + if (*check_self) { return; } /* add addr_spec if missing, update otherwise */ char* display_name_dec = NULL; - if( display_name_enc ) { + if (display_name_enc) { display_name_dec = dc_decode_header_words(display_name_enc); dc_normalize_name(display_name_dec); } @@ -69,8 +69,8 @@ static void add_or_lookup_contact_by_addr(dc_context_t* context, const char* dis free(display_name_dec); - if( row_id ) { - if( !dc_array_search_id(ids, row_id, NULL) ) { + if (row_id) { + if (!dc_array_search_id(ids, row_id, NULL)) { dc_array_add_id(ids, row_id); } } @@ -81,13 +81,13 @@ static void dc_add_or_lookup_contacts_by_mailbox_list(dc_context_t* context, con { clistiter* cur; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC || mb_list == NULL ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC || mb_list == NULL) { return; } - for( cur = clist_begin(mb_list->mb_list); cur!=NULL ; cur=clist_next(cur) ) { + for (cur = clist_begin(mb_list->mb_list); cur!=NULL ; cur=clist_next(cur)) { struct mailimf_mailbox* mb = (struct mailimf_mailbox*)clist_content(cur); - if( mb ) { + if (mb) { add_or_lookup_contact_by_addr(context, mb->mb_display_name, mb->mb_addr_spec, origin, ids, check_self); } } @@ -98,22 +98,22 @@ static void dc_add_or_lookup_contacts_by_address_list(dc_context_t* context, con { clistiter* cur; - if( context == NULL || context->magic != DC_CONTEXT_MAGIC || adr_list == NULL /*may be NULL eg. if bcc is given as `Bcc: \n` in the header */ ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC || adr_list == NULL /*may be NULL eg. if bcc is given as `Bcc: \n` in the header */) { return; } - for( cur = clist_begin(adr_list->ad_list); cur!=NULL ; cur=clist_next(cur) ) { + for (cur = clist_begin(adr_list->ad_list); cur!=NULL ; cur=clist_next(cur)) { struct mailimf_address* adr = (struct mailimf_address*)clist_content(cur); - if( adr ) { - if( adr->ad_type == MAILIMF_ADDRESS_MAILBOX ) { + if (adr) { + if (adr->ad_type == MAILIMF_ADDRESS_MAILBOX) { struct mailimf_mailbox* mb = adr->ad_data.ad_mailbox; /* can be NULL */ - if( mb ) { + if (mb) { add_or_lookup_contact_by_addr(context, mb->mb_display_name, mb->mb_addr_spec, origin, ids, check_self); } } - else if( adr->ad_type == MAILIMF_ADDRESS_GROUP ) { + else if (adr->ad_type == MAILIMF_ADDRESS_GROUP) { struct mailimf_group* group = adr->ad_data.ad_group; /* can be NULL */ - if( group && group->grp_mb_list /*can be NULL*/ ) { + if (group && group->grp_mb_list /*can be NULL*/) { dc_add_or_lookup_contacts_by_mailbox_list(context, group->grp_mb_list, origin, ids, check_self); } } @@ -130,7 +130,7 @@ static void dc_add_or_lookup_contacts_by_address_list(dc_context_t* context, con static int is_known_rfc724_mid(dc_context_t* context, const char* rfc724_mid) { int is_known = 0; - if( rfc724_mid ) { + if (rfc724_mid) { sqlite3_stmt* stmt = dc_sqlite3_prepare(context->sql, "SELECT m.id FROM msgs m " " LEFT JOIN chats c ON m.chat_id=c.id " @@ -138,7 +138,7 @@ static int is_known_rfc724_mid(dc_context_t* context, const char* rfc724_mid) " AND m.chat_id>" DC_STRINGIFY(DC_CHAT_ID_LAST_SPECIAL) " AND c.blocked=0;"); sqlite3_bind_text(stmt, 1, rfc724_mid, -1, SQLITE_STATIC); - if( sqlite3_step(stmt) == SQLITE_ROW ) { + if (sqlite3_step(stmt) == SQLITE_ROW) { is_known = 1; } sqlite3_finalize(stmt); @@ -149,10 +149,10 @@ static int is_known_rfc724_mid(dc_context_t* context, const char* rfc724_mid) static int is_known_rfc724_mid_in_list(dc_context_t* context, const clist* mid_list) { - if( mid_list ) { + if (mid_list) { clistiter* cur; - for( cur = clist_begin(mid_list); cur!=NULL ; cur=clist_next(cur) ) { - if( is_known_rfc724_mid(context, clist_content(cur)) ) { + for (cur = clist_begin(mid_list); cur!=NULL ; cur=clist_next(cur)) { + if (is_known_rfc724_mid(context, clist_content(cur))) { return 1; } } @@ -168,31 +168,31 @@ static int dc_is_reply_to_known_message(dc_context_t* context, dc_mimeparser_t* `In-Reply-To`/`References:` (to support non-Delta-Clients) or from `Chat-Predecessor:` (Delta clients, see comment in dc_chat.c) */ struct mailimf_optional_field* optional_field; - if( (optional_field=dc_mimeparser_lookup_optional_field2(mime_parser, "Chat-Predecessor", "X-MrPredecessor")) != NULL ) + if ((optional_field=dc_mimeparser_lookup_optional_field2(mime_parser, "Chat-Predecessor", "X-MrPredecessor")) != NULL) { - if( is_known_rfc724_mid(context, optional_field->fld_value) ) { + if (is_known_rfc724_mid(context, optional_field->fld_value)) { return 1; } } struct mailimf_field* field; - if( (field=dc_mimeparser_lookup_field(mime_parser, "In-Reply-To"))!=NULL - && field->fld_type == MAILIMF_FIELD_IN_REPLY_TO ) + if ((field=dc_mimeparser_lookup_field(mime_parser, "In-Reply-To"))!=NULL + && field->fld_type == MAILIMF_FIELD_IN_REPLY_TO) { struct mailimf_in_reply_to* fld_in_reply_to = field->fld_data.fld_in_reply_to; - if( fld_in_reply_to ) { - if( is_known_rfc724_mid_in_list(context, field->fld_data.fld_in_reply_to->mid_list) ) { + if (fld_in_reply_to) { + if (is_known_rfc724_mid_in_list(context, field->fld_data.fld_in_reply_to->mid_list)) { return 1; } } } - if( (field=dc_mimeparser_lookup_field(mime_parser, "References"))!=NULL - && field->fld_type == MAILIMF_FIELD_REFERENCES ) + if ((field=dc_mimeparser_lookup_field(mime_parser, "References"))!=NULL + && field->fld_type == MAILIMF_FIELD_REFERENCES) { struct mailimf_references* fld_references = field->fld_data.fld_references; - if( fld_references ) { - if( is_known_rfc724_mid_in_list(context, field->fld_data.fld_references->mid_list) ) { + if (fld_references) { + if (is_known_rfc724_mid_in_list(context, field->fld_data.fld_references->mid_list)) { return 1; } } @@ -210,14 +210,14 @@ static int dc_is_reply_to_known_message(dc_context_t* context, dc_mimeparser_t* static int is_msgrmsg_rfc724_mid(dc_context_t* context, const char* rfc724_mid) { int is_msgrmsg = 0; - if( rfc724_mid ) { + if (rfc724_mid) { sqlite3_stmt* stmt = dc_sqlite3_prepare(context->sql, "SELECT id FROM msgs " " WHERE rfc724_mid=? " " AND msgrmsg!=0 " " AND chat_id>" DC_STRINGIFY(DC_CHAT_ID_LAST_SPECIAL) ";"); sqlite3_bind_text(stmt, 1, rfc724_mid, -1, SQLITE_STATIC); - if( sqlite3_step(stmt) == SQLITE_ROW ) { + if (sqlite3_step(stmt) == SQLITE_ROW) { is_msgrmsg = 1; } sqlite3_finalize(stmt); @@ -228,10 +228,10 @@ static int is_msgrmsg_rfc724_mid(dc_context_t* context, const char* rfc724_mid) static int is_msgrmsg_rfc724_mid_in_list(dc_context_t* context, const clist* mid_list) { - if( mid_list ) { + if (mid_list) { clistiter* cur; - for( cur = clist_begin(mid_list); cur!=NULL ; cur=clist_next(cur) ) { - if( is_msgrmsg_rfc724_mid(context, clist_content(cur)) ) { + for (cur = clist_begin(mid_list); cur!=NULL ; cur=clist_next(cur)) { + if (is_msgrmsg_rfc724_mid(context, clist_content(cur))) { return 1; } } @@ -251,23 +251,23 @@ static int dc_is_reply_to_messenger_message(dc_context_t* context, dc_mimeparser - no check for the Chat-* headers (function is only called if it is no messenger message itself) */ struct mailimf_field* field; - if( (field=dc_mimeparser_lookup_field(mime_parser, "In-Reply-To"))!=NULL - && field->fld_type==MAILIMF_FIELD_IN_REPLY_TO ) + if ((field=dc_mimeparser_lookup_field(mime_parser, "In-Reply-To"))!=NULL + && field->fld_type==MAILIMF_FIELD_IN_REPLY_TO) { struct mailimf_in_reply_to* fld_in_reply_to = field->fld_data.fld_in_reply_to; - if( fld_in_reply_to ) { - if( is_msgrmsg_rfc724_mid_in_list(context, field->fld_data.fld_in_reply_to->mid_list) ) { + if (fld_in_reply_to) { + if (is_msgrmsg_rfc724_mid_in_list(context, field->fld_data.fld_in_reply_to->mid_list)) { return 1; } } } - if( (field=dc_mimeparser_lookup_field(mime_parser, "References"))!=NULL - && field->fld_type==MAILIMF_FIELD_REFERENCES ) + if ((field=dc_mimeparser_lookup_field(mime_parser, "References"))!=NULL + && field->fld_type==MAILIMF_FIELD_REFERENCES) { struct mailimf_references* fld_references = field->fld_data.fld_references; - if( fld_references ) { - if( is_msgrmsg_rfc724_mid_in_list(context, field->fld_data.fld_references->mid_list) ) { + if (fld_references) { + if (is_msgrmsg_rfc724_mid_in_list(context, field->fld_data.fld_references->mid_list)) { return 1; } } @@ -288,7 +288,7 @@ static void calc_timestamps(dc_context_t* context, uint32_t chat_id, uint32_t fr *rcvd_timestamp = time(NULL); *sent_timestamp = message_timestamp; - if( *sent_timestamp > *rcvd_timestamp /* no sending times in the future */ ) { + if (*sent_timestamp > *rcvd_timestamp /* no sending times in the future */) { *sent_timestamp = *rcvd_timestamp; } @@ -297,18 +297,18 @@ static void calc_timestamps(dc_context_t* context, uint32_t chat_id, uint32_t fr /* use the last message from another user (including SELF) as the MINIMUM for sort_timestamp; this is to force fresh messages popping up at the end of the list. (we do this check only for fresh messages, other messages may pop up whereever, this may happen eg. when restoring old messages or synchronizing different clients) */ - if( is_fresh_msg ) + if (is_fresh_msg) { sqlite3_stmt* stmt = dc_sqlite3_prepare(context->sql, "SELECT MAX(timestamp) FROM msgs WHERE chat_id=? and from_id!=? AND timestamp>=?"); sqlite3_bind_int (stmt, 1, chat_id); sqlite3_bind_int (stmt, 2, from_id); sqlite3_bind_int64(stmt, 3, *sort_timestamp); - if( sqlite3_step(stmt)==SQLITE_ROW ) + if (sqlite3_step(stmt)==SQLITE_ROW) { time_t last_msg_time = sqlite3_column_int64(stmt, 0); - if( last_msg_time > 0 /* may happen as we do not check against sqlite3_column_type()!=SQLITE_NULL */ ) { - if( *sort_timestamp <= last_msg_time ) { + if (last_msg_time > 0 /* may happen as we do not check against sqlite3_column_type()!=SQLITE_NULL */) { + if (*sort_timestamp <= last_msg_time) { *sort_timestamp = last_msg_time+1; /* this may result in several incoming messages having the same one-second-after-the-last-other-message-timestamp. however, this is no big deal as we do not try to recrete the order of bad-date-messages and as we always order by ID as second criterion */ @@ -319,7 +319,7 @@ static void calc_timestamps(dc_context_t* context, uint32_t chat_id, uint32_t fr } /* use the (smeared) current time as the MAXIMUM */ - if( *sort_timestamp >= dc_smeared_time(context) ) { + if (*sort_timestamp >= dc_smeared_time(context)) { *sort_timestamp = dc_create_smeared_timestamp(context); } } @@ -333,25 +333,25 @@ static dc_array_t* search_chat_ids_by_contact_ids(dc_context_t* context, const d char* contact_ids_str = NULL, *q3 = NULL; dc_array_t* chat_ids = dc_array_new(context, 23); - if( context == NULL || context->magic != DC_CONTEXT_MAGIC ) { + if (context == NULL || context->magic != DC_CONTEXT_MAGIC) { goto cleanup; } /* copy array, remove duplicates and SELF, sort by ID */ { int i, iCnt = dc_array_get_cnt(unsorted_contact_ids); - if( iCnt <= 0 ) { + if (iCnt <= 0) { goto cleanup; } - for( i = 0; i < iCnt; i++ ) { + for (i = 0; i < iCnt; i++) { uint32_t curr_id = dc_array_get_id(unsorted_contact_ids, i); - if( curr_id != DC_CONTACT_ID_SELF && !dc_array_search_id(contact_ids, curr_id, NULL) ) { + if (curr_id != DC_CONTACT_ID_SELF && !dc_array_search_id(contact_ids, curr_id, NULL)) { dc_array_add_id(contact_ids, curr_id); } } - if( dc_array_get_cnt(contact_ids)==0 ) { + if (dc_array_get_cnt(contact_ids)==0) { goto cleanup; } @@ -372,13 +372,13 @@ static dc_array_t* search_chat_ids_by_contact_ids(dc_context_t* context, const d { uint32_t last_chat_id = 0, matches = 0, mismatches = 0; - while( sqlite3_step(stmt)==SQLITE_ROW ) + while (sqlite3_step(stmt)==SQLITE_ROW) { uint32_t chat_id = sqlite3_column_int(stmt, 0); uint32_t contact_id = sqlite3_column_int(stmt, 1); - if( chat_id != last_chat_id ) { - if( matches == dc_array_get_cnt(contact_ids) && mismatches == 0 ) { + if (chat_id != last_chat_id) { + if (matches == dc_array_get_cnt(contact_ids) && mismatches == 0) { dc_array_add_id(chat_ids, last_chat_id); } last_chat_id = chat_id; @@ -386,7 +386,7 @@ static dc_array_t* search_chat_ids_by_contact_ids(dc_context_t* context, const d mismatches = 0; } - if( contact_id == dc_array_get_id(contact_ids, matches) ) { + if (contact_id == dc_array_get_id(contact_ids, matches)) { matches++; } else { @@ -394,7 +394,7 @@ static dc_array_t* search_chat_ids_by_contact_ids(dc_context_t* context, const d } } - if( matches == dc_array_get_cnt(contact_ids) && mismatches == 0 ) { + if (matches == dc_array_get_cnt(contact_ids) && mismatches == 0) { dc_array_add_id(chat_ids, last_chat_id); } } @@ -433,7 +433,7 @@ static char* create_adhoc_grp_id(dc_context_t* context, dc_array_t* member_ids / addr = dc_sqlite3_get_config(context->sql, "configured_addr", "no-self"); dc_strlower_in_place(addr); dc_array_add_ptr(member_addrs, addr); - while( sqlite3_step(stmt)==SQLITE_ROW ) { + while (sqlite3_step(stmt)==SQLITE_ROW) { addr = dc_strdup((const char*)sqlite3_column_text(stmt, 0)); dc_strlower_in_place(addr); dc_array_add_ptr(member_addrs, addr); @@ -442,8 +442,8 @@ static char* create_adhoc_grp_id(dc_context_t* context, dc_array_t* member_ids / /* build a single, comma-separated (cs) string from all addresses */ iCnt = dc_array_get_cnt(member_addrs); - for( i = 0; i < iCnt; i++ ) { - if( i ) { dc_strbuilder_cat(&member_cs, ","); } + for (i = 0; i < iCnt; i++) { + if (i) { dc_strbuilder_cat(&member_cs, ","); } dc_strbuilder_cat(&member_cs, (const char*)dc_array_get_ptr(member_addrs, i)); } @@ -459,7 +459,7 @@ static char* create_adhoc_grp_id(dc_context_t* context, dc_array_t* member_ids / /* output the first 8 bytes as 16 hex-characters - CAVE: if the lenght changes here, also adapt dc_extract_grpid_from_rfc724_mid() */ ret = calloc(1, 256); - for( i = 0; i < 8; i++ ) { + for (i = 0; i < 8; i++) { sprintf(&ret[i*2], "%02x", (int)binary_hash[i]); } @@ -486,7 +486,7 @@ static uint32_t create_group_record(dc_context_t* context, const char* grpid, co sqlite3_bind_text(stmt, 2, grpname, -1, SQLITE_STATIC); sqlite3_bind_text(stmt, 3, grpid, -1, SQLITE_STATIC); sqlite3_bind_int (stmt, 4, create_blocked); - if( sqlite3_step(stmt)!=SQLITE_DONE ) { + if (sqlite3_step(stmt)!=SQLITE_DONE) { goto cleanup; } chat_id = dc_sqlite3_get_rowid(context->sql, "chats", "grpid", grpid); @@ -518,19 +518,19 @@ static void create_or_lookup_adhoc_group(dc_context_t* context, dc_mimeparser_t* char* grpname = NULL; /* build member list from the given ids */ - if( dc_array_get_cnt(to_ids)==0 || dc_mimeparser_is_mailinglist_message(mime_parser) ) { + if (dc_array_get_cnt(to_ids)==0 || dc_mimeparser_is_mailinglist_message(mime_parser)) { goto cleanup; /* too few contacts or a mailinglist */ } member_ids = dc_array_duplicate(to_ids); - if( !dc_array_search_id(member_ids, from_id, NULL) ) { dc_array_add_id(member_ids, from_id); } - if( !dc_array_search_id(member_ids, DC_CONTACT_ID_SELF, NULL) ) { dc_array_add_id(member_ids, DC_CONTACT_ID_SELF); } - if( dc_array_get_cnt(member_ids) < 3 ) { + if (!dc_array_search_id(member_ids, from_id, NULL)) { dc_array_add_id(member_ids, from_id); } + if (!dc_array_search_id(member_ids, DC_CONTACT_ID_SELF, NULL)) { dc_array_add_id(member_ids, DC_CONTACT_ID_SELF); } + if (dc_array_get_cnt(member_ids) < 3) { goto cleanup; /* too few contacts given */ } /* check if the member list matches other chats, if so, choose the one with the most recent activity */ chat_ids = search_chat_ids_by_contact_ids(context, member_ids); - if( dc_array_get_cnt(chat_ids)>0 ) { + if (dc_array_get_cnt(chat_ids)>0) { chat_ids_str = dc_array_get_string(chat_ids, ","); q3 = sqlite3_mprintf("SELECT c.id, c.blocked " " FROM chats c " @@ -540,7 +540,7 @@ static void create_or_lookup_adhoc_group(dc_context_t* context, dc_mimeparser_t* " LIMIT 1;", chat_ids_str); stmt = dc_sqlite3_prepare(context->sql, q3); - if( sqlite3_step(stmt)==SQLITE_ROW ) { + if (sqlite3_step(stmt)==SQLITE_ROW) { chat_id = sqlite3_column_int(stmt, 0); chat_id_blocked = sqlite3_column_int(stmt, 1); goto cleanup; /* success, chat found */ @@ -552,12 +552,12 @@ static void create_or_lookup_adhoc_group(dc_context_t* context, dc_mimeparser_t* /* create a new ad-hoc group - there is no need to check if this group exists; otherwise we would have catched it above */ - if( (grpid = create_adhoc_grp_id(context, member_ids)) == NULL ) { + if ((grpid = create_adhoc_grp_id(context, member_ids)) == NULL) { goto cleanup; } /* use subject as initial chat name */ - if( mime_parser->subject && mime_parser->subject[0] ) { + if (mime_parser->subject && mime_parser->subject[0]) { grpname = dc_strdup(mime_parser->subject); } else { @@ -567,7 +567,7 @@ static void create_or_lookup_adhoc_group(dc_context_t* context, dc_mimeparser_t* /* create group record */ chat_id = create_group_record(context, grpid, grpname, create_blocked, 0); chat_id_blocked = create_blocked; - for( i = 0; i < dc_array_get_cnt(member_ids); i++ ) { + for (i = 0; i < dc_array_get_cnt(member_ids); i++) { dc_add_to_chat_contacts_table(context, chat_id, dc_array_get_id(member_ids, i)); } @@ -581,8 +581,8 @@ cleanup: free(grpname); sqlite3_finalize(stmt); sqlite3_free(q3); - if( ret_chat_id ) { *ret_chat_id = chat_id; } - if( ret_chat_id_blocked ) { *ret_chat_id_blocked = chat_id_blocked; } + if (ret_chat_id) { *ret_chat_id = chat_id; } + if (ret_chat_id_blocked) { *ret_chat_id_blocked = chat_id_blocked; } } @@ -597,21 +597,21 @@ static int check_verified_properties(dc_context_t* context, dc_mimeparser_t* mim sqlite3_stmt* stmt = NULL; // ensure, the contact is verified - if( !dc_contact_load_from_db(contact, context->sql, from_id) + if (!dc_contact_load_from_db(contact, context->sql, from_id) || !dc_apeerstate_load_by_addr(peerstate, context->sql, contact->addr) - || dc_contact_n_peerstate_are_verified(contact, peerstate) < DC_BIDIRECT_VERIFIED ) { + || dc_contact_n_peerstate_are_verified(contact, peerstate) < DC_BIDIRECT_VERIFIED) { dc_log_warning(context, 0, "Cannot verifiy group; sender is not verified."); goto cleanup; } // ensure, the message is encrypted - if( !mimeparser->e2ee_helper->encrypted ) { + if (!mimeparser->e2ee_helper->encrypted) { dc_log_warning(context, 0, "Cannot verifiy group; message is not encrypted properly."); goto cleanup; } // ensure, the message is signed with a verified key of the sender - if( !dc_apeerstate_has_verified_key(peerstate, mimeparser->e2ee_helper->signatures) ) { + if (!dc_apeerstate_has_verified_key(peerstate, mimeparser->e2ee_helper->signatures)) { dc_log_warning(context, 0, "Cannot verifiy group; message is not signed properly."); goto cleanup; } @@ -625,22 +625,22 @@ static int check_verified_properties(dc_context_t* context, dc_mimeparser_t* mim " WHERE c.id IN(%s) ", to_ids_str); stmt = dc_sqlite3_prepare(context->sql, q3); - while( sqlite3_step(stmt)==SQLITE_ROW ) + while (sqlite3_step(stmt)==SQLITE_ROW) { const char* to_addr = (const char*)sqlite3_column_text(stmt, 0); int is_verified = sqlite3_column_int (stmt, 1); - if( dc_hash_find_str(mimeparser->e2ee_helper->gossipped_addr, to_addr) - && dc_apeerstate_load_by_addr(peerstate, context->sql, to_addr) ) + if (dc_hash_find_str(mimeparser->e2ee_helper->gossipped_addr, to_addr) + && dc_apeerstate_load_by_addr(peerstate, context->sql, to_addr)) { // if we're here, we know the gossip key is verified: // - use the gossip-key as verified-key if there is no verified-key // - OR if the verified-key does not match public-key or gossip-key // (otherwise a verified key can _only_ be updated through QR scan which might be annoying, // see https://github.com/nextleap-project/countermitm/issues/46 for a discussion about this point) - if( !is_verified + if (!is_verified || (strcmp(peerstate->verified_key_fingerprint, peerstate->public_key_fingerprint)!=0 - && strcmp(peerstate->verified_key_fingerprint, peerstate->gossip_key_fingerprint)!=0) ) + && strcmp(peerstate->verified_key_fingerprint, peerstate->gossip_key_fingerprint)!=0)) { dc_log_info(context, 0, "Marking gossipped key %s as verified due to verified %s.", to_addr, contact->addr); dc_apeerstate_set_verified(peerstate, DC_PS_GOSSIP_KEY, peerstate->gossip_key_fingerprint, DC_BIDIRECT_VERIFIED); @@ -649,7 +649,7 @@ static int check_verified_properties(dc_context_t* context, dc_mimeparser_t* mim } } - if( !is_verified ) + if (!is_verified) { dc_log_warning(context, 0, "Cannot verifiy group; recipient %s is not gossipped.", to_addr); goto cleanup; @@ -707,38 +707,38 @@ static void create_or_lookup_group(dc_context_t* context, dc_mimeparser_t* mime_ struct mailimf_field* field = NULL; struct mailimf_optional_field* optional_field = NULL; - if( (optional_field=dc_mimeparser_lookup_optional_field2(mime_parser, "Chat-Group-ID", "X-MrGrpId"))!=NULL ) { + if ((optional_field=dc_mimeparser_lookup_optional_field2(mime_parser, "Chat-Group-ID", "X-MrGrpId"))!=NULL) { grpid = dc_strdup(optional_field->fld_value); } - if( grpid == NULL ) + if (grpid == NULL) { - if( (field=dc_mimeparser_lookup_field(mime_parser, "Message-ID"))!=NULL && field->fld_type==MAILIMF_FIELD_MESSAGE_ID ) { + if ((field=dc_mimeparser_lookup_field(mime_parser, "Message-ID"))!=NULL && field->fld_type==MAILIMF_FIELD_MESSAGE_ID) { struct mailimf_message_id* fld_message_id = field->fld_data.fld_message_id; - if( fld_message_id ) { + if (fld_message_id) { grpid = dc_extract_grpid_from_rfc724_mid(fld_message_id->mid_value); } } - if( grpid == NULL ) + if (grpid == NULL) { - if( (field=dc_mimeparser_lookup_field(mime_parser, "In-Reply-To"))!=NULL && field->fld_type==MAILIMF_FIELD_IN_REPLY_TO ) { + if ((field=dc_mimeparser_lookup_field(mime_parser, "In-Reply-To"))!=NULL && field->fld_type==MAILIMF_FIELD_IN_REPLY_TO) { struct mailimf_in_reply_to* fld_in_reply_to = field->fld_data.fld_in_reply_to; - if( fld_in_reply_to ) { + if (fld_in_reply_to) { grpid = dc_extract_grpid_from_rfc724_mid_list(fld_in_reply_to->mid_list); } } - if( grpid == NULL ) + if (grpid == NULL) { - if( (field=dc_mimeparser_lookup_field(mime_parser, "References"))!=NULL && field->fld_type==MAILIMF_FIELD_REFERENCES ) { + if ((field=dc_mimeparser_lookup_field(mime_parser, "References"))!=NULL && field->fld_type==MAILIMF_FIELD_REFERENCES) { struct mailimf_references* fld_references = field->fld_data.fld_references; - if( fld_references ) { + if (fld_references) { grpid = dc_extract_grpid_from_rfc724_mid_list(fld_references->mid_list); } } - if( grpid == NULL ) + if (grpid == NULL) { create_or_lookup_adhoc_group(context, mime_parser, create_blocked, from_id, to_ids, &chat_id, &chat_id_blocked); goto cleanup; @@ -747,32 +747,32 @@ static void create_or_lookup_group(dc_context_t* context, dc_mimeparser_t* mime_ } } - if( (optional_field=dc_mimeparser_lookup_optional_field2(mime_parser, "Chat-Group-Name", "X-MrGrpName"))!=NULL ) { + if ((optional_field=dc_mimeparser_lookup_optional_field2(mime_parser, "Chat-Group-Name", "X-MrGrpName"))!=NULL) { grpname = dc_decode_header_words(optional_field->fld_value); /* this is no changed groupname message */ } - if( (optional_field=dc_mimeparser_lookup_optional_field2(mime_parser, "Chat-Group-Member-Removed", "X-MrRemoveFromGrp"))!=NULL ) { + if ((optional_field=dc_mimeparser_lookup_optional_field2(mime_parser, "Chat-Group-Member-Removed", "X-MrRemoveFromGrp"))!=NULL) { X_MrRemoveFromGrp = optional_field->fld_value; mime_parser->is_system_message = DC_CMD_MEMBER_REMOVED_FROM_GROUP; } - else if( (optional_field=dc_mimeparser_lookup_optional_field2(mime_parser, "Chat-Group-Member-Added", "X-MrAddToGrp"))!=NULL ) { + else if ((optional_field=dc_mimeparser_lookup_optional_field2(mime_parser, "Chat-Group-Member-Added", "X-MrAddToGrp"))!=NULL) { X_MrAddToGrp = optional_field->fld_value; mime_parser->is_system_message = DC_CMD_MEMBER_ADDED_TO_GROUP; } - else if( (optional_field=dc_mimeparser_lookup_optional_field2(mime_parser, "Chat-Group-Name-Changed", "X-MrGrpNameChanged"))!=NULL ) { + else if ((optional_field=dc_mimeparser_lookup_optional_field2(mime_parser, "Chat-Group-Name-Changed", "X-MrGrpNameChanged"))!=NULL) { X_MrGrpNameChanged = 1; mime_parser->is_system_message = DC_CMD_GROUPNAME_CHANGED; } - else if( (optional_field=dc_mimeparser_lookup_optional_field(mime_parser, "Chat-Group-Image"))!=NULL ) { + else if ((optional_field=dc_mimeparser_lookup_optional_field(mime_parser, "Chat-Group-Image"))!=NULL) { X_MrGrpImageChanged = 1; mime_parser->is_system_message = DC_CMD_GROUPIMAGE_CHANGED; } } /* check, if we have a chat with this group ID */ - if( (chat_id=dc_get_chat_id_by_grpid(context, grpid, &chat_id_blocked, &chat_id_verified))!=0 ) { - if( chat_id_verified - && !check_verified_properties(context, mime_parser, from_id, to_ids) ) { + if ((chat_id=dc_get_chat_id_by_grpid(context, grpid, &chat_id_blocked, &chat_id_verified))!=0) { + if (chat_id_verified + && !check_verified_properties(context, mime_parser, from_id, to_ids)) { chat_id = 0; // force the creation of an unverified ad-hoc group. chat_id_blocked = 0; chat_id_verified = 0; @@ -785,7 +785,7 @@ static void create_or_lookup_group(dc_context_t* context, dc_mimeparser_t* mime_ /* check if the sender is a member of the existing group - if not, the message does not go to the group chat but to the normal chat with the sender */ - if( chat_id!=0 && !dc_is_contact_in_chat(context, chat_id, from_id) ) { + if (chat_id!=0 && !dc_is_contact_in_chat(context, chat_id, from_id)) { chat_id = 0; create_or_lookup_adhoc_group(context, mime_parser, create_blocked, from_id, to_ids, &chat_id, &chat_id_blocked); goto cleanup; @@ -795,17 +795,17 @@ static void create_or_lookup_group(dc_context_t* context, dc_mimeparser_t* mime_ int group_explicitly_left = dc_is_group_explicitly_left(context, grpid); self_addr = dc_sqlite3_get_config(context->sql, "configured_addr", ""); - if( chat_id == 0 + if (chat_id == 0 && !dc_mimeparser_is_mailinglist_message(mime_parser) && grpid && grpname && X_MrRemoveFromGrp==NULL /*otherwise, a pending "quit" message may pop up*/ - && (!group_explicitly_left || (X_MrAddToGrp&&strcasecmp(self_addr,X_MrAddToGrp)==0) ) /*re-create explicitly left groups only if ourself is re-added*/ - ) + && (!group_explicitly_left || (X_MrAddToGrp&&strcasecmp(self_addr,X_MrAddToGrp)==0)) /*re-create explicitly left groups only if ourself is re-added*/ + ) { int create_verified = 0; - if( dc_mimeparser_lookup_field(mime_parser, "Chat-Verified") ) { - if( check_verified_properties(context, mime_parser, from_id, to_ids) ) { + if (dc_mimeparser_lookup_field(mime_parser, "Chat-Verified")) { + if (check_verified_properties(context, mime_parser, from_id, to_ids)) { create_verified = 1; } } @@ -817,9 +817,9 @@ static void create_or_lookup_group(dc_context_t* context, dc_mimeparser_t* mime_ } /* again, check chat_id */ - if( chat_id <= DC_CHAT_ID_LAST_SPECIAL ) { + if (chat_id <= DC_CHAT_ID_LAST_SPECIAL) { chat_id = 0; - if( group_explicitly_left ) { + if (group_explicitly_left) { chat_id = DC_CHAT_ID_TRASH; /* we got a message for a chat we've deleted - do not show this even as a normal chat */ } else { @@ -829,11 +829,11 @@ static void create_or_lookup_group(dc_context_t* context, dc_mimeparser_t* mime_ } /* execute group commands */ - if( X_MrAddToGrp || X_MrRemoveFromGrp ) + if (X_MrAddToGrp || X_MrRemoveFromGrp) { recreate_member_list = 1; } - else if( X_MrGrpNameChanged && grpname && strlen(grpname) < 200 ) + else if (X_MrGrpNameChanged && grpname && strlen(grpname) < 200) { stmt = dc_sqlite3_prepare(context->sql, "UPDATE chats SET name=? WHERE id=?;"); sqlite3_bind_text(stmt, 1, grpname, -1, SQLITE_STATIC); @@ -843,16 +843,16 @@ static void create_or_lookup_group(dc_context_t* context, dc_mimeparser_t* mime_ context->cb(context, DC_EVENT_CHAT_MODIFIED, chat_id, 0); } - if( X_MrGrpImageChanged ) + if (X_MrGrpImageChanged) { int ok = 0; char* grpimage = NULL; - if( carray_count(mime_parser->parts)>=1 ) { + if (carray_count(mime_parser->parts)>=1) { dc_mimepart_t* textpart = (dc_mimepart_t*)carray_get(mime_parser->parts, 0); - if( textpart->type == DC_MSG_TEXT ) { - if( carray_count(mime_parser->parts)>=2 ) { + if (textpart->type == DC_MSG_TEXT) { + if (carray_count(mime_parser->parts)>=2) { dc_mimepart_t* imgpart = (dc_mimepart_t*)carray_get(mime_parser->parts, 1); - if( imgpart->type == DC_MSG_IMAGE ) { + if (imgpart->type == DC_MSG_IMAGE) { grpimage = dc_param_get(imgpart->param, DC_PARAM_FILE, NULL); ok = 1; } @@ -863,7 +863,7 @@ static void create_or_lookup_group(dc_context_t* context, dc_mimeparser_t* mime_ } } - if( ok ) { + if (ok) { dc_chat_t* chat = dc_chat_new(context); dc_log_info(context, 0, "New group image set to %s.", grpimage? "DELETED" : grpimage); dc_chat_load_from_db(chat, chat_id); @@ -877,7 +877,7 @@ static void create_or_lookup_group(dc_context_t* context, dc_mimeparser_t* mime_ /* add members to group/check members for recreation: we should add a timestamp */ - if( recreate_member_list ) + if (recreate_member_list) { const char* skip = X_MrRemoveFromGrp? X_MrRemoveFromGrp : NULL; @@ -886,37 +886,37 @@ static void create_or_lookup_group(dc_context_t* context, dc_mimeparser_t* mime_ sqlite3_step(stmt); sqlite3_finalize(stmt); - if( skip==NULL || strcasecmp(self_addr, skip) != 0 ) { + if (skip==NULL || strcasecmp(self_addr, skip) != 0) { dc_add_to_chat_contacts_table(context, chat_id, DC_CONTACT_ID_SELF); } - if( from_id > DC_CONTACT_ID_LAST_SPECIAL ) { - if( dc_contact_addr_equals(context, from_id, self_addr)==0 - && (skip==NULL || dc_contact_addr_equals(context, from_id, skip)==0) ) { + if (from_id > DC_CONTACT_ID_LAST_SPECIAL) { + if (dc_contact_addr_equals(context, from_id, self_addr)==0 + && (skip==NULL || dc_contact_addr_equals(context, from_id, skip)==0)) { dc_add_to_chat_contacts_table(context, chat_id, from_id); } } - for( i = 0; i < to_ids_cnt; i++ ) + for (i = 0; i < to_ids_cnt; i++) { uint32_t to_id = dc_array_get_id(to_ids, i); /* to_id is only once in to_ids and is non-special */ - if( dc_contact_addr_equals(context, to_id, self_addr)==0 - && (skip==NULL || dc_contact_addr_equals(context, to_id, skip)==0) ) { + if (dc_contact_addr_equals(context, to_id, self_addr)==0 + && (skip==NULL || dc_contact_addr_equals(context, to_id, skip)==0)) { dc_add_to_chat_contacts_table(context, chat_id, to_id); } } send_EVENT_CHAT_MODIFIED = 1; } - if( send_EVENT_CHAT_MODIFIED ) { + if (send_EVENT_CHAT_MODIFIED) { context->cb(context, DC_EVENT_CHAT_MODIFIED, chat_id, 0); } /* check the number of receivers - the only critical situation is if the user hits "Reply" instead of "Reply all" in a non-messenger-client */ - if( to_ids_cnt == 1 && mime_parser->is_send_by_messenger==0 ) { + if (to_ids_cnt == 1 && mime_parser->is_send_by_messenger==0) { int is_contact_cnt = dc_get_chat_contact_count(context, chat_id); - if( is_contact_cnt > 3 /* to_ids_cnt==1 may be "From: A, To: B, SELF" as SELF is not counted in to_ids_cnt. So everything up to 3 is no error. */ ) { + if (is_contact_cnt > 3 /* to_ids_cnt==1 may be "From: A, To: B, SELF" as SELF is not counted in to_ids_cnt. So everything up to 3 is no error. */) { chat_id = 0; create_or_lookup_adhoc_group(context, mime_parser, create_blocked, from_id, to_ids, &chat_id, &chat_id_blocked); goto cleanup; @@ -927,8 +927,8 @@ cleanup: free(grpid); free(grpname); free(self_addr); - if( ret_chat_id ) { *ret_chat_id = chat_id; } - if( ret_chat_id_blocked ) { *ret_chat_id_blocked = chat_id? chat_id_blocked : 0; } + if (ret_chat_id) { *ret_chat_id = chat_id; } + if (ret_chat_id_blocked) { *ret_chat_id_blocked = chat_id? chat_id_blocked : 0; } } @@ -978,7 +978,7 @@ void dc_receive_imf(dc_context_t* context, const char* imf_raw_not_terminated, s dc_log_info(context, 0, "Receiving message %s/%lu...", server_folder? server_folder:"?", server_uid); to_ids = dc_array_new(context, 16); - if( to_ids==NULL || created_db_entries==NULL || rr_event_to_send==NULL || mime_parser == NULL ) { + if (to_ids==NULL || created_db_entries==NULL || rr_event_to_send==NULL || mime_parser == NULL) { dc_log_info(context, 0, "Bad param."); goto cleanup; } @@ -996,7 +996,7 @@ void dc_receive_imf(dc_context_t* context, const char* imf_raw_not_terminated, s we use mailmime_parse() through dc_mimeparser (both call mailimf_struct_multiple_parse() somewhen, I did not found out anything that speaks against this approach yet) */ dc_mimeparser_parse(mime_parser, imf_raw_not_terminated, imf_raw_bytes); - if( dc_hash_count(&mime_parser->header)==0 ) { + if (dc_hash_count(&mime_parser->header)==0) { dc_log_info(context, 0, "No header."); goto cleanup; /* Error - even adding an empty record won't help as we do not know the message ID */ } @@ -1006,14 +1006,14 @@ void dc_receive_imf(dc_context_t* context, const char* imf_raw_not_terminated, s as it may even be confusing when _own_ messages sent from other devices with other e-mail-adresses appear as being sent from SELF we disabled this check for now */ #if 0 - if( !dc_mimeparser_lookup_field(mime_parser, "Return-Path") ) { + if (!dc_mimeparser_lookup_field(mime_parser, "Return-Path")) { incoming = 0; } #endif - if( (field=dc_mimeparser_lookup_field(mime_parser, "Date"))!=NULL && field->fld_type==MAILIMF_FIELD_ORIG_DATE ) { + if ((field=dc_mimeparser_lookup_field(mime_parser, "Date"))!=NULL && field->fld_type==MAILIMF_FIELD_ORIG_DATE) { struct mailimf_orig_date* orig_date = field->fld_data.fld_orig_date; - if( orig_date ) { + if (orig_date) { sent_timestamp = dc_timestamp_from_date(orig_date->dt_date_time); // is not yet checked against bad times! we do this later if we have the database information. } } @@ -1023,27 +1023,27 @@ void dc_receive_imf(dc_context_t* context, const char* imf_raw_not_terminated, s /* get From: and check if it is known (for known From:'s we add the other To:/Cc: in the 3rd pass) or if From: is equal to SELF (in this case, it is any outgoing messages, we do not check Return-Path any more as this is unreliable, see issue #150 */ - if( (field=dc_mimeparser_lookup_field(mime_parser, "From"))!=NULL + if ((field=dc_mimeparser_lookup_field(mime_parser, "From"))!=NULL && field->fld_type==MAILIMF_FIELD_FROM) { struct mailimf_from* fld_from = field->fld_data.fld_from; - if( fld_from ) + if (fld_from) { int check_self; dc_array_t* from_list = dc_array_new(context, 16); dc_add_or_lookup_contacts_by_mailbox_list(context, fld_from->frm_mb_list, DC_ORIGIN_INCOMING_UNKNOWN_FROM, from_list, &check_self); - if( check_self ) + if (check_self) { incoming = 0; - if( dc_mimeparser_sender_equals_recipient(mime_parser) ) + if (dc_mimeparser_sender_equals_recipient(mime_parser)) { from_id = DC_CONTACT_ID_SELF; } } else { - if( dc_array_get_cnt(from_list)>=1 ) /* if there is no from given, from_id stays 0 which is just fine. These messages are very rare, however, we have to add them to the database (they go to the "deaddrop" chat) to avoid a re-download from the server. See also [**] */ + if (dc_array_get_cnt(from_list)>=1) /* if there is no from given, from_id stays 0 which is just fine. These messages are very rare, however, we have to add them to the database (they go to the "deaddrop" chat) to avoid a re-download from the server. See also [**] */ { from_id = dc_array_get_id(from_list, 0); incoming_origin = dc_get_contact_origin(context, from_id, &from_id_blocked); @@ -1054,18 +1054,18 @@ void dc_receive_imf(dc_context_t* context, const char* imf_raw_not_terminated, s } /* Make sure, to_ids starts with the first To:-address (Cc: is added in the loop below pass) */ - if( (field=dc_mimeparser_lookup_field(mime_parser, "To"))!=NULL - && field->fld_type==MAILIMF_FIELD_TO ) + if ((field=dc_mimeparser_lookup_field(mime_parser, "To"))!=NULL + && field->fld_type==MAILIMF_FIELD_TO) { struct mailimf_to* fld_to = field->fld_data.fld_to; /* can be NULL */ - if( fld_to ) + if (fld_to) { dc_add_or_lookup_contacts_by_address_list(context, fld_to->to_addr_list /*!= NULL*/, outgoing? DC_ORIGIN_OUTGOING_TO : (incoming_origin>=DC_ORIGIN_MIN_VERIFIED? DC_ORIGIN_INCOMING_TO : DC_ORIGIN_INCOMING_UNKNOWN_TO), to_ids, &to_self); } } - if( dc_mimeparser_has_nonmeta(mime_parser) ) + if (dc_mimeparser_has_nonmeta(mime_parser)) { /********************************************************************** @@ -1075,10 +1075,10 @@ void dc_receive_imf(dc_context_t* context, const char* imf_raw_not_terminated, s /* collect the rest information, CC: is added to the to-list, BCC: is ignored (we should not add BCC to groups as this would split groups. We could add them as "known contacts", however, the benefit is very small and this may leak data that is expected to be hidden) */ - if( (field=dc_mimeparser_lookup_field(mime_parser, "Cc"))!=NULL && field->fld_type==MAILIMF_FIELD_CC ) + if ((field=dc_mimeparser_lookup_field(mime_parser, "Cc"))!=NULL && field->fld_type==MAILIMF_FIELD_CC) { struct mailimf_cc* fld_cc = field->fld_data.fld_cc; - if( fld_cc ) { + if (fld_cc) { dc_add_or_lookup_contacts_by_address_list(context, fld_cc->cc_addr_list, outgoing? DC_ORIGIN_OUTGOING_CC : (incoming_origin>=DC_ORIGIN_MIN_VERIFIED? DC_ORIGIN_INCOMING_CC : DC_ORIGIN_INCOMING_UNKNOWN_CC), to_ids, NULL); } @@ -1087,16 +1087,16 @@ void dc_receive_imf(dc_context_t* context, const char* imf_raw_not_terminated, s /* get Message-ID; if the header is lacking one, generate one based on fields that do never change. (missing Message-IDs may come if the mail was set from this account with another client that relies in the SMTP server to generate one. true eg. for the Webmailer used in all-inkl-KAS) */ - if( (field=dc_mimeparser_lookup_field(mime_parser, "Message-ID"))!=NULL && field->fld_type==MAILIMF_FIELD_MESSAGE_ID ) { + if ((field=dc_mimeparser_lookup_field(mime_parser, "Message-ID"))!=NULL && field->fld_type==MAILIMF_FIELD_MESSAGE_ID) { struct mailimf_message_id* fld_message_id = field->fld_data.fld_message_id; - if( fld_message_id ) { + if (fld_message_id) { rfc724_mid = dc_strdup(fld_message_id->mid_value); } } - if( rfc724_mid == NULL ) { + if (rfc724_mid == NULL) { rfc724_mid = dc_create_incoming_rfc724_mid(sort_timestamp, from_id, to_ids); - if( rfc724_mid == NULL ) { + if (rfc724_mid == NULL) { dc_log_info(context, 0, "Cannot create Message-ID."); goto cleanup; } @@ -1107,8 +1107,8 @@ void dc_receive_imf(dc_context_t* context, const char* imf_raw_not_terminated, s { char* old_server_folder = NULL; uint32_t old_server_uid = 0; - if( dc_rfc724_mid_exists(context, rfc724_mid, &old_server_folder, &old_server_uid) ) { - if( strcmp(old_server_folder, server_folder)!=0 || old_server_uid!=server_uid ) { + if (dc_rfc724_mid_exists(context, rfc724_mid, &old_server_folder, &old_server_uid)) { + if (strcmp(old_server_folder, server_folder)!=0 || old_server_uid!=server_uid) { dc_sqlite3_rollback(context->sql); transaction_pending = 0; dc_update_server_uid(context, rfc724_mid, server_folder, server_uid); @@ -1123,14 +1123,14 @@ void dc_receive_imf(dc_context_t* context, const char* imf_raw_not_terminated, s - outgoing messages introduce a chat with the first to: address if they are sent by a messenger - incoming messages introduce a chat only for known contacts if they are sent by a messenger (of course, the user can add other chats manually later) */ - if( incoming ) + if (incoming) { state = (flags&DC_IMAP_SEEN)? DC_STATE_IN_SEEN : DC_STATE_IN_FRESH; to_id = DC_CONTACT_ID_SELF; // handshake messages must be processed before chats are created (eg. contacs may be marked as verified) - assert( chat_id == 0 ); - if( dc_mimeparser_lookup_field(mime_parser, "Secure-Join") ) { + assert( chat_id == 0); + if (dc_mimeparser_lookup_field(mime_parser, "Secure-Join")) { dc_sqlite3_commit(context->sql); int handshake = dc_handle_securejoin_handshake(context, mime_parser, from_id); if (handshake & DC_HANDSHAKE_STOP_NORMAL_PROCESSING) { @@ -1148,32 +1148,32 @@ void dc_receive_imf(dc_context_t* context, const char* imf_raw_not_terminated, s /* get the chat_id - a chat_id here is no indicator that the chat is displayed in the normal list, it might also be blocked and displayed in the deaddrop as a result */ - if( chat_id == 0 ) + if (chat_id == 0) { /* try to create a group (groups appear automatically only if the _sender_ is known, see core issue #54) */ int create_blocked = ((test_normal_chat_id&&test_normal_chat_id_blocked==DC_CHAT_NOT_BLOCKED) || incoming_origin>=DC_ORIGIN_MIN_START_NEW_NCHAT/*always false, for now*/)? DC_CHAT_NOT_BLOCKED : DC_CHAT_DEADDROP_BLOCKED; create_or_lookup_group(context, mime_parser, create_blocked, from_id, to_ids, &chat_id, &chat_id_blocked); - if( chat_id && chat_id_blocked && !create_blocked ) { + if (chat_id && chat_id_blocked && !create_blocked) { dc_unblock_chat(context, chat_id); chat_id_blocked = 0; } } - if( chat_id == 0 ) + if (chat_id == 0) { /* check if the message belongs to a mailing list */ - if( dc_mimeparser_is_mailinglist_message(mime_parser) ) { + if (dc_mimeparser_is_mailinglist_message(mime_parser)) { chat_id = DC_CHAT_ID_TRASH; dc_log_info(context, 0, "Message belongs to a mailing list and is ignored."); } } - if( chat_id == 0 ) + if (chat_id == 0) { /* try to create a normal chat */ int create_blocked = (incoming_origin>=DC_ORIGIN_MIN_START_NEW_NCHAT/*always false, for now*/ || from_id==to_id)? DC_CHAT_NOT_BLOCKED : DC_CHAT_DEADDROP_BLOCKED; - if( test_normal_chat_id ) { + if (test_normal_chat_id) { chat_id = test_normal_chat_id; chat_id_blocked = test_normal_chat_id_blocked; } @@ -1181,12 +1181,12 @@ void dc_receive_imf(dc_context_t* context, const char* imf_raw_not_terminated, s dc_create_or_lookup_nchat_by_contact_id(context, from_id, create_blocked, &chat_id, &chat_id_blocked); } - if( chat_id && chat_id_blocked ) { - if( !create_blocked ) { + if (chat_id && chat_id_blocked) { + if (!create_blocked) { dc_unblock_chat(context, chat_id); chat_id_blocked = 0; } - else if( dc_is_reply_to_known_message(context, mime_parser) ) { + else if (dc_is_reply_to_known_message(context, mime_parser)) { dc_scaleup_contact_origin(context, from_id, DC_ORIGIN_INCOMING_REPLY_TO); /* we do not want any chat to be created implicitly. Because of the origin-scale-up, the contact requests will pop up and this should be just fine. */ dc_log_info(context, 0, "Message is a reply to a known message, mark sender as known."); incoming_origin = DC_MAX(incoming_origin, DC_ORIGIN_INCOMING_REPLY_TO); @@ -1194,7 +1194,7 @@ void dc_receive_imf(dc_context_t* context, const char* imf_raw_not_terminated, s } } - if( chat_id == 0 ) + if (chat_id == 0) { /* maybe from_id is null or sth. else is suspicious, move message to trash */ chat_id = DC_CHAT_ID_TRASH; @@ -1203,8 +1203,8 @@ void dc_receive_imf(dc_context_t* context, const char* imf_raw_not_terminated, s /* degrade state for unknown senders and non-delta messages (the latter may be removed if we run into spam problems, currently this is fine) (noticed messages do count as being unread; therefore, the deaddrop will not popup in the chatlist) */ - if( chat_id_blocked && state == DC_STATE_IN_FRESH ) { - if( incoming_originis_send_by_messenger==0 ) { + if (chat_id_blocked && state == DC_STATE_IN_FRESH) { + if (incoming_originis_send_by_messenger==0) { state = DC_STATE_IN_NOTICED; } } @@ -1213,41 +1213,41 @@ void dc_receive_imf(dc_context_t* context, const char* imf_raw_not_terminated, s { state = DC_STATE_OUT_DELIVERED; /* the mail is on the IMAP server, probably it is also delivered. We cannot recreate other states (read, error). */ from_id = DC_CONTACT_ID_SELF; - if( dc_array_get_cnt(to_ids) >= 1 ) { + if (dc_array_get_cnt(to_ids) >= 1) { to_id = dc_array_get_id(to_ids, 0); - if( chat_id == 0 ) + if (chat_id == 0) { create_or_lookup_group(context, mime_parser, DC_CHAT_NOT_BLOCKED, from_id, to_ids, &chat_id, &chat_id_blocked); - if( chat_id && chat_id_blocked ) { + if (chat_id && chat_id_blocked) { dc_unblock_chat(context, chat_id); chat_id_blocked = 0; } } - if( chat_id == 0 ) + if (chat_id == 0) { int create_blocked = (mime_parser->is_send_by_messenger && !dc_is_contact_blocked(context, to_id))? DC_CHAT_NOT_BLOCKED : DC_CHAT_DEADDROP_BLOCKED; dc_create_or_lookup_nchat_by_contact_id(context, to_id, create_blocked, &chat_id, &chat_id_blocked); - if( chat_id && chat_id_blocked && !create_blocked ) { + if (chat_id && chat_id_blocked && !create_blocked) { dc_unblock_chat(context, chat_id); chat_id_blocked = 0; } } } - if( chat_id == 0 ) { - if( dc_array_get_cnt(to_ids) == 0 && to_self ) { + if (chat_id == 0) { + if (dc_array_get_cnt(to_ids) == 0 && to_self) { /* from_id == to_id == DC_CONTACT_ID_SELF - this is a self-sent messages, maybe an Autocrypt Setup Message */ dc_create_or_lookup_nchat_by_contact_id(context, DC_CONTACT_ID_SELF, DC_CHAT_NOT_BLOCKED, &chat_id, &chat_id_blocked); - if( chat_id && chat_id_blocked ) { + if (chat_id && chat_id_blocked) { dc_unblock_chat(context, chat_id); chat_id_blocked = 0; } } } - if( chat_id == 0 ) { + if (chat_id == 0) { chat_id = DC_CHAT_ID_TRASH; } } @@ -1263,13 +1263,13 @@ void dc_receive_imf(dc_context_t* context, const char* imf_raw_not_terminated, s /* if the message is not sent by a messenger, check if it is sent at least as a reply to a messenger message (later, we move these replies to the folder used for DeltaChat messages) */ int msgrmsg = mime_parser->is_send_by_messenger; /* 1 or 0 for yes/no */ - if( msgrmsg ) + if (msgrmsg) { dc_log_info(context, 0, "Message sent by another messenger."); } else { - if( dc_is_reply_to_messenger_message(context, mime_parser) ) + if (dc_is_reply_to_messenger_message(context, mime_parser)) { dc_log_info(context, 0, "Message is a reply to a messenger message."); msgrmsg = 2; /* 2=no, but is reply to messenger message */ @@ -1283,18 +1283,18 @@ void dc_receive_imf(dc_context_t* context, const char* imf_raw_not_terminated, s stmt = dc_sqlite3_prepare(context->sql, "INSERT INTO msgs (rfc724_mid,server_folder,server_uid,chat_id,from_id, to_id,timestamp,timestamp_sent,timestamp_rcvd,type, state,msgrmsg,txt,txt_raw,param, bytes,hidden)" " VALUES (?,?,?,?,?, ?,?,?,?,?, ?,?,?,?,?, ?,?);"); - for( i = 0; i < icnt; i++ ) + for (i = 0; i < icnt; i++) { dc_mimepart_t* part = (dc_mimepart_t*)carray_get(mime_parser->parts, i); - if( part->is_meta ) { + if (part->is_meta) { continue; } - if( part->type == DC_MSG_TEXT ) { + if (part->type == DC_MSG_TEXT) { txt_raw = dc_mprintf("%s\n\n%s", mime_parser->subject? mime_parser->subject : "", part->msg_raw); } - if( mime_parser->is_system_message ) { + if (mime_parser->is_system_message) { dc_param_set_int(part->param, DC_PARAM_CMD, mime_parser->is_system_message); } @@ -1316,7 +1316,7 @@ void dc_receive_imf(dc_context_t* context, const char* imf_raw_not_terminated, s sqlite3_bind_text (stmt, 15, part->param->packed, -1, SQLITE_STATIC); sqlite3_bind_int (stmt, 16, part->bytes); sqlite3_bind_int (stmt, 17, hidden); - if( sqlite3_step(stmt) != SQLITE_DONE ) { + if (sqlite3_step(stmt) != SQLITE_DONE) { dc_log_info(context, 0, "Cannot write DB."); goto cleanup; /* i/o error - there is nothing more we can do - in other cases, we try to write at least an empty record */ } @@ -1324,7 +1324,7 @@ void dc_receive_imf(dc_context_t* context, const char* imf_raw_not_terminated, s free(txt_raw); txt_raw = NULL; - if( first_dblocal_id == 0 ) { + if (first_dblocal_id == 0) { first_dblocal_id = dc_sqlite3_get_rowid(context->sql, "msgs", "rfc724_mid", rfc724_mid); // rfc724_mid is unique only for the first insert } @@ -1335,18 +1335,18 @@ void dc_receive_imf(dc_context_t* context, const char* imf_raw_not_terminated, s dc_log_info(context, 0, "Message has %i parts and is assigned to chat #%i.", icnt, chat_id); /* check event to send */ - if( chat_id == DC_CHAT_ID_TRASH ) + if (chat_id == DC_CHAT_ID_TRASH) { create_event_to_send = 0; } - else if( incoming && state==DC_STATE_IN_FRESH ) + else if (incoming && state==DC_STATE_IN_FRESH) { - if( from_id_blocked ) { + if (from_id_blocked) { create_event_to_send = 0; } - else if( chat_id_blocked ) { + else if (chat_id_blocked) { create_event_to_send = DC_EVENT_MSGS_CHANGED; - /*if( dc_sqlite3_get_config_int(context->sql, "show_deaddrop", 0)!=0 ) { + /*if (dc_sqlite3_get_config_int(context->sql, "show_deaddrop", 0)!=0) { create_event_to_send = DC_EVENT_INCOMING_MSG; }*/ } @@ -1358,13 +1358,13 @@ void dc_receive_imf(dc_context_t* context, const char* imf_raw_not_terminated, s else { // there are no non-meta data in message, do some basic calculations so that the varaiables are correct in the further processing - if( sent_timestamp > time(NULL) ) { + if (sent_timestamp > time(NULL)) { sent_timestamp = time(NULL); } } - if( carray_count(mime_parser->reports) > 0 ) + if (carray_count(mime_parser->reports) > 0) { /****************************************************************** * Handle reports (mainly MDNs) @@ -1372,52 +1372,52 @@ void dc_receive_imf(dc_context_t* context, const char* imf_raw_not_terminated, s int mdns_enabled = dc_sqlite3_get_config_int(context->sql, "mdns_enabled", DC_MDNS_DEFAULT_ENABLED); icnt = carray_count(mime_parser->reports); - for( i = 0; i < icnt; i++ ) + for (i = 0; i < icnt; i++) { int mdn_consumed = 0; struct mailmime* report_root = carray_get(mime_parser->reports, i); struct mailmime_parameter* report_type = mailmime_find_ct_parameter(report_root, "report-type"); - if( report_root==NULL || report_type==NULL || report_type->pa_value==NULL ) { + if (report_root==NULL || report_type==NULL || report_type->pa_value==NULL) { continue; } - if( strcmp(report_type->pa_value, "disposition-notification") == 0 - && clist_count(report_root->mm_data.mm_multipart.mm_mp_list) >= 2 /* the first part is for humans, the second for machines */ ) + if (strcmp(report_type->pa_value, "disposition-notification") == 0 + && clist_count(report_root->mm_data.mm_multipart.mm_mp_list) >= 2 /* the first part is for humans, the second for machines */) { - if( mdns_enabled /*to get a clear functionality, do not show incoming MDNs if the options is disabled*/ ) + if (mdns_enabled /*to get a clear functionality, do not show incoming MDNs if the options is disabled*/) { struct mailmime* report_data = (struct mailmime*)clist_content(clist_next(clist_begin(report_root->mm_data.mm_multipart.mm_mp_list))); - if( report_data + if (report_data && report_data->mm_content_type->ct_type->tp_type==MAILMIME_TYPE_COMPOSITE_TYPE && report_data->mm_content_type->ct_type->tp_data.tp_composite_type->ct_type==MAILMIME_COMPOSITE_TYPE_MESSAGE - && strcmp(report_data->mm_content_type->ct_subtype, "disposition-notification")==0 ) + && strcmp(report_data->mm_content_type->ct_subtype, "disposition-notification")==0) { /* we received a MDN (although the MDN is only a header, we parse it as a complete mail) */ const char* report_body = NULL; size_t report_body_bytes = 0; char* to_mmap_string_unref = NULL; - if( mailmime_transfer_decode(report_data, &report_body, &report_body_bytes, &to_mmap_string_unref) ) + if (mailmime_transfer_decode(report_data, &report_body, &report_body_bytes, &to_mmap_string_unref)) { struct mailmime* report_parsed = NULL; size_t dummy = 0; - if( mailmime_parse(report_body, report_body_bytes, &dummy, &report_parsed)==MAIL_NO_ERROR - && report_parsed!=NULL ) + if (mailmime_parse(report_body, report_body_bytes, &dummy, &report_parsed)==MAIL_NO_ERROR + && report_parsed!=NULL) { struct mailimf_fields* report_fields = mailmime_find_mailimf_fields(report_parsed); - if( report_fields ) + if (report_fields) { struct mailimf_optional_field* of_disposition = mailimf_find_optional_field(report_fields, "Disposition"); /* MUST be preset, _if_ preset, we assume a sort of attribution and do not go into details */ struct mailimf_optional_field* of_org_msgid = mailimf_find_optional_field(report_fields, "Original-Message-ID"); /* can't live without */ - if( of_disposition && of_disposition->fld_value && of_org_msgid && of_org_msgid->fld_value ) + if (of_disposition && of_disposition->fld_value && of_org_msgid && of_org_msgid->fld_value) { char* rfc724_mid = NULL; dummy = 0; - if( mailimf_msg_id_parse(of_org_msgid->fld_value, strlen(of_org_msgid->fld_value), &dummy, &rfc724_mid)==MAIL_NO_ERROR - && rfc724_mid!=NULL ) + if (mailimf_msg_id_parse(of_org_msgid->fld_value, strlen(of_org_msgid->fld_value), &dummy, &rfc724_mid)==MAIL_NO_ERROR + && rfc724_mid!=NULL) { uint32_t chat_id = 0; uint32_t msg_id = 0; - if( dc_mdn_from_ext(context, from_id, rfc724_mid, sent_timestamp, &chat_id, &msg_id) ) { + if (dc_mdn_from_ext(context, from_id, rfc724_mid, sent_timestamp, &chat_id, &msg_id)) { carray_add(rr_event_to_send, (void*)(uintptr_t)chat_id, NULL); carray_add(rr_event_to_send, (void*)(uintptr_t)msg_id, NULL); } @@ -1429,7 +1429,7 @@ void dc_receive_imf(dc_context_t* context, const char* imf_raw_not_terminated, s mailmime_free(report_parsed); } - if( to_mmap_string_unref ) { mmap_string_unref(to_mmap_string_unref); } + if (to_mmap_string_unref) { mmap_string_unref(to_mmap_string_unref); } } } } @@ -1443,7 +1443,7 @@ void dc_receive_imf(dc_context_t* context, const char* imf_raw_not_terminated, s CAVE: we rely on dc_imap_markseen_msg() not to move messages that are already in the correct folder. otherwise, the moved message get a new server_uid and is "fresh" again and we will be here again to move it away - a classical deadlock, see also (***) in dc_imap.c */ - if( mime_parser->is_send_by_messenger || mdn_consumed ) { + if (mime_parser->is_send_by_messenger || mdn_consumed) { char* jobparam = dc_mprintf("%c=%s\n%c=%lu", DC_PARAM_SERVER_FOLDER, server_folder, DC_PARAM_SERVER_UID, server_uid); dc_job_add(context, DC_JOB_MARKSEEN_MDN_ON_IMAP, 0, jobparam, 0); free(jobparam); @@ -1455,17 +1455,17 @@ void dc_receive_imf(dc_context_t* context, const char* imf_raw_not_terminated, s } /* 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/%s-%i.eml", context->blobdir, server_folder, (int)first_dblocal_id /*may be 0 for MDNs*/); FILE* emlfileob = fopen(emlname, "w"); - if( emlfileob ) { + if (emlfileob) { fwrite(imf_raw_not_terminated, 1, imf_raw_bytes, emlfileob); fclose(emlfileob); } free(emlname); } - if( add_delete_job ) { + if (add_delete_job) { dc_job_add(context, DC_JOB_DELETE_MSG_ON_IMAP, first_dblocal_id, NULL, 0); } @@ -1473,25 +1473,25 @@ void dc_receive_imf(dc_context_t* context, const char* imf_raw_not_terminated, s transaction_pending = 0; cleanup: - if( transaction_pending ) { dc_sqlite3_rollback(context->sql); } + if (transaction_pending) { dc_sqlite3_rollback(context->sql); } dc_mimeparser_unref(mime_parser); free(rfc724_mid); dc_array_unref(to_ids); - if( created_db_entries ) { - if( create_event_to_send ) { + if (created_db_entries) { + if (create_event_to_send) { size_t i, icnt = carray_count(created_db_entries); - for( i = 0; i < icnt; i += 2 ) { + for (i = 0; i < icnt; i += 2) { context->cb(context, create_event_to_send, (uintptr_t)carray_get(created_db_entries, i), (uintptr_t)carray_get(created_db_entries, i+1)); } } carray_free(created_db_entries); } - if( rr_event_to_send ) { + if (rr_event_to_send) { size_t i, icnt = carray_count(rr_event_to_send); - for( i = 0; i < icnt; i += 2 ) { + for (i = 0; i < icnt; i += 2) { context->cb(context, DC_EVENT_MSG_READ, (uintptr_t)carray_get(rr_event_to_send, i), (uintptr_t)carray_get(rr_event_to_send, i+1)); } carray_free(rr_event_to_send); diff --git a/src/dc_saxparser.c b/src/dc_saxparser.c index 513c6e7b..cb1b6d3d 100644 --- a/src/dc_saxparser.c +++ b/src/dc_saxparser.c @@ -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 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 or **************************************************************/ - 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 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 `` */ 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 */ diff --git a/src/dc_securejoin.c b/src/dc_securejoin.c index e1fcbe76..6f88024a 100644 --- a/src/dc_securejoin.c +++ b/src/dc_securejoin.c @@ -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; } diff --git a/src/dc_simplify.c b/src/dc_simplify.c index d41cddae..f2a2c9eb 100644 --- a/src/dc_simplify.c +++ b/src/dc_simplify.c @@ -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; } diff --git a/src/dc_smtp.c b/src/dc_smtp.c index f9c68f98..804fdbcc 100644 --- a/src/dc_smtp.c +++ b/src/dc_smtp.c @@ -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)); diff --git a/src/dc_sqlite3.c b/src/dc_sqlite3.c index fc8d7b07..be365671 100644 --- a/src/dc_sqlite3.c +++ b/src/dc_sqlite3.c @@ -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); diff --git a/src/dc_stock.c b/src/dc_stock.c index 2b798e8c..d05a83ad 100644 --- a/src/dc_stock.c +++ b/src/dc_stock.c @@ -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; diff --git a/src/dc_strbuilder.c b/src/dc_strbuilder.c index fc1dd913..4b4528e7 100644 --- a/src/dc_strbuilder.c +++ b/src/dc_strbuilder.c @@ -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; diff --git a/src/dc_strencode.c b/src/dc_strencode.c index aba48357..6fd8b14d 100644 --- a/src/dc_strencode.c +++ b/src/dc_strencode.c @@ -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; } diff --git a/src/dc_token.c b/src/dc_token.c index 23d2b508..ee4b87ca 100644 --- a/src/dc_token.c +++ b/src/dc_token.c @@ -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; } diff --git a/src/dc_tools.c b/src/dc_tools.c index 98068208..7186c422 100644 --- a/src/dc_tools.c +++ b/src/dc_tools.c @@ -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); diff --git a/src/dc_uudecode.c b/src/dc_uudecode.c index 9adf9626..5ff89aae 100644 --- a/src/dc_uudecode.c +++ b/src/dc_uudecode.c @@ -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 }