From b376432f043d5afd17087305c620c5fe277d1129 Mon Sep 17 00:00:00 2001 From: Michael Raitza Date: Wed, 26 Aug 2015 16:48:33 +0200 Subject: [PATCH] Updated to new tox API. Fixed profile loading. --- src/twc-bootstrap.c | 5 +- src/twc-chat.c | 5 +- src/twc-commands.c | 55 +++---- src/twc-completion.c | 8 +- src/twc-friend-request.c | 2 +- src/twc-gui.c | 7 +- src/twc-message-queue.c | 25 ++-- src/twc-profile.c | 315 ++++++++++++++++++++------------------- src/twc-utils.c | 50 ++++--- 9 files changed, 252 insertions(+), 220 deletions(-) diff --git a/src/twc-bootstrap.c b/src/twc-bootstrap.c index f1d438f..ce2b2df 100644 --- a/src/twc-bootstrap.c +++ b/src/twc-bootstrap.c @@ -70,9 +70,10 @@ twc_bootstrap_tox(Tox *tox, const char *address, uint16_t port, { uint8_t binary_key[TOX_ADDRESS_SIZE]; twc_hex2bin(public_key, TOX_ADDRESS_SIZE, binary_key); + TOX_ERR_BOOTSTRAP err; - int result = tox_bootstrap_from_address(tox, address, port, - binary_key); + int result = tox_bootstrap(tox, address, port, + binary_key, &err); return result; } diff --git a/src/twc-chat.c b/src/twc-chat.c index 574842f..db3802b 100644 --- a/src/twc-chat.c +++ b/src/twc-chat.c @@ -103,7 +103,10 @@ struct t_twc_chat * twc_chat_new_friend(struct t_twc_profile *profile, int32_t friend_number) { uint8_t client_id[TOX_PUBLIC_KEY_SIZE]; - tox_get_client_id(profile->tox, friend_number, client_id); + TOX_ERR_FRIEND_GET_PUBLIC_KEY err; + tox_friend_get_public_key(profile->tox, friend_number, client_id, &err); + if (err != TOX_ERR_FRIEND_GET_PUBLIC_KEY_OK) + return NULL; char buffer_name[TOX_PUBLIC_KEY_SIZE * 2 + 1]; twc_bin2hex(client_id, TOX_PUBLIC_KEY_SIZE, buffer_name); diff --git a/src/twc-commands.c b/src/twc-commands.c index a9169cc..7df4b18 100644 --- a/src/twc-commands.c +++ b/src/twc-commands.c @@ -142,14 +142,14 @@ enum TWC_FRIEND_MATCH enum TWC_FRIEND_MATCH twc_match_friend(struct t_twc_profile *profile, const char *search_string) { - uint32_t friend_count = tox_count_friendlist(profile->tox); - int32_t *friend_numbers = malloc(sizeof(int32_t) * friend_count); - tox_get_friendlist(profile->tox, friend_numbers, friend_count); + uint32_t friend_count = tox_self_get_friend_list_size(profile->tox); + uint32_t *friend_numbers = malloc(sizeof(uint32_t) * friend_count); + tox_self_get_friend_list(profile->tox, friend_numbers); int32_t match = TWC_FRIEND_MATCH_NOMATCH; char *endptr; - unsigned long friend_number = strtoul(search_string, &endptr, 10); + uint32_t friend_number = (uint32_t)strtoul(search_string, &endptr, 10); if (endptr == search_string + strlen(search_string) && tox_friend_exists(profile->tox, friend_number)) return friend_number; @@ -162,7 +162,7 @@ twc_match_friend(struct t_twc_profile *profile, const char *search_string) uint8_t tox_id[TOX_PUBLIC_KEY_SIZE]; char hex_id[TOX_PUBLIC_KEY_SIZE * 2 + 1]; - tox_get_client_id(profile->tox, friend_numbers[i], tox_id); + tox_friend_get_public_key(profile->tox, friend_numbers[i], tox_id, NULL); // do error handling twc_bin2hex(tox_id, TOX_PUBLIC_KEY_SIZE, hex_id); if (weechat_strcasecmp(hex_id, search_string) == 0) @@ -227,9 +227,9 @@ twc_cmd_friend(void *data, struct t_gui_buffer *buffer, // /friend or /friend list if (argc == 1 || (argc == 2 && weechat_strcasecmp(argv[1], "list") == 0)) { - size_t friend_count = tox_count_friendlist(profile->tox); - int32_t friend_numbers[friend_count]; - tox_get_friendlist(profile->tox, friend_numbers, friend_count); + size_t friend_count = tox_self_get_friend_list_size(profile->tox); + uint32_t friend_numbers[friend_count]; + tox_self_get_friend_list(profile->tox, friend_numbers); if (friend_count == 0) { @@ -245,7 +245,7 @@ twc_cmd_friend(void *data, struct t_gui_buffer *buffer, for (size_t i = 0; i < friend_count; ++i) { - int32_t friend_number = friend_numbers[i]; + uint32_t friend_number = friend_numbers[i]; char *name = twc_get_name_nt(profile->tox, friend_number); char *hex_address = twc_get_friend_id_short(profile->tox, friend_number); @@ -306,7 +306,7 @@ twc_cmd_friend(void *data, struct t_gui_buffer *buffer, if (friend_number == TWC_FRIEND_MATCH_AMBIGUOUS) fail = true; else if (friend_number != TWC_FRIEND_MATCH_NOMATCH) - fail = tox_del_friend(profile->tox, friend_number) != 0; + fail = !tox_friend_delete(profile->tox, friend_number, NULL); if (fail) { @@ -318,12 +318,13 @@ twc_cmd_friend(void *data, struct t_gui_buffer *buffer, } } - TOX_ERR_FRIEND_ADD result = tox_add_friend(profile->tox, - (uint8_t *)address, - (uint8_t *)message, - strlen(message)); + TOX_ERR_FRIEND_ADD err; + (void)tox_friend_add(profile->tox, + (uint8_t *)address, + (uint8_t *)message, + strlen(message), &err); - switch (result) + switch (err) { case TOX_ERR_FRIEND_ADD_OK: weechat_printf(profile->buffer, @@ -363,7 +364,7 @@ twc_cmd_friend(void *data, struct t_gui_buffer *buffer, default: weechat_printf(profile->buffer, "%sCould not add friend (unknown error %d).", - weechat_prefix("error"), result); + weechat_prefix("error"), err); break; } @@ -377,7 +378,7 @@ twc_cmd_friend(void *data, struct t_gui_buffer *buffer, TWC_CHECK_FRIEND_NUMBER(profile, friend_number, argv[2]); char *name = twc_get_name_nt(profile->tox, friend_number); - if (tox_del_friend(profile->tox, friend_number) == 0) + if (tox_friend_delete(profile->tox, friend_number, NULL) == 0) { weechat_printf(profile->buffer, "%sRemoved %s from friend list.", @@ -693,7 +694,7 @@ twc_cmd_myid(void *data, struct t_gui_buffer *buffer, TWC_CHECK_PROFILE_LOADED(profile); uint8_t address[TOX_ADDRESS_SIZE]; - tox_get_address(profile->tox, address); + tox_self_get_address(profile->tox, address); char address_str[TOX_ADDRESS_SIZE * 2 + 1]; twc_bin2hex(address, TOX_ADDRESS_SIZE, address_str); @@ -722,8 +723,8 @@ twc_cmd_name(void *data, struct t_gui_buffer *buffer, char *name = argv_eol[1]; - int result = tox_set_name(profile->tox, (uint8_t *)name, strlen(name)); - if (result == -1) + int result = tox_self_set_name(profile->tox, (uint8_t *)name, strlen(name), NULL); + if (!result) { weechat_printf(profile->buffer, "%s%s", @@ -789,8 +790,8 @@ twc_cmd_nospam(void *data, struct t_gui_buffer *buffer, new_nospam = random(); } - uint32_t old_nospam = tox_get_nospam(profile->tox); - tox_set_nospam(profile->tox, new_nospam); + uint32_t old_nospam = tox_self_get_nospam(profile->tox); + tox_self_set_nospam(profile->tox, new_nospam); weechat_printf(profile->buffer, "%snew nospam has been set; this changes your Tox ID! To " @@ -887,7 +888,7 @@ twc_cmd_status(void *data, struct t_gui_buffer *buffer, else return WEECHAT_RC_ERROR; - tox_set_user_status(profile->tox, status); + tox_self_set_status(profile->tox, status); weechat_bar_item_update("away"); return WEECHAT_RC_OK; @@ -906,10 +907,10 @@ twc_cmd_statusmsg(void *data, struct t_gui_buffer *buffer, char *message = argc > 1 ? argv_eol[1] : " "; - int result = tox_set_status_message(profile->tox, - (uint8_t *)message, - strlen(message)); - if (result == -1) + bool result = tox_self_set_status_message(profile->tox, + (uint8_t *)message, + strlen(message), NULL); + if (!result) { weechat_printf(profile->buffer, "%s%s", diff --git a/src/twc-completion.c b/src/twc-completion.c index b351204..5e516bb 100644 --- a/src/twc-completion.c +++ b/src/twc-completion.c @@ -57,9 +57,9 @@ twc_completion_friend(void *data, if (!profile) return WEECHAT_RC_OK; - uint32_t friend_count = tox_count_friendlist(profile->tox); - int32_t *friend_numbers = malloc(sizeof(int32_t) * friend_count); - tox_get_friendlist(profile->tox, friend_numbers, friend_count); + uint32_t friend_count = tox_self_get_friend_list_size(profile->tox); + uint32_t *friend_numbers = malloc(sizeof(uint32_t) * friend_count); + tox_self_get_friend_list(profile->tox, friend_numbers); for (uint32_t i = 0; i < friend_count; ++i) { @@ -68,7 +68,7 @@ twc_completion_friend(void *data, uint8_t tox_id[TOX_PUBLIC_KEY_SIZE]; char hex_id[TOX_PUBLIC_KEY_SIZE * 2 + 1]; - tox_get_client_id(profile->tox, friend_numbers[i], tox_id); + tox_friend_get_public_key(profile->tox, friend_numbers[i], tox_id, NULL); // do error handling twc_bin2hex(tox_id, TOX_PUBLIC_KEY_SIZE, hex_id); weechat_hook_completion_list_add(completion, hex_id, 0, diff --git a/src/twc-friend-request.c b/src/twc-friend-request.c index 0bcd896..f5d4242 100644 --- a/src/twc-friend-request.c +++ b/src/twc-friend-request.c @@ -67,7 +67,7 @@ twc_friend_request_add(struct t_twc_profile *profile, void twc_friend_request_accept(struct t_twc_friend_request *request) { - tox_add_friend_norequest(request->profile->tox, request->tox_id); + tox_friend_add_norequest(request->profile->tox, request->tox_id, NULL); //do error handling twc_friend_request_remove(request); } diff --git a/src/twc-gui.c b/src/twc-gui.c index e8e1639..4c2ac43 100644 --- a/src/twc-gui.c +++ b/src/twc-gui.c @@ -40,9 +40,12 @@ twc_bar_item_away(void *data, if (!profile || !(profile->tox)) return NULL; - char *status = NULL;; - switch (tox_get_self_user_status(profile->tox)) + char *status; + switch (tox_self_get_status(profile->tox)) { + case TOX_USER_STATUS_NONE: + status = NULL; + break; case TOX_USER_STATUS_BUSY: status = strdup("busy"); break; diff --git a/src/twc-message-queue.c b/src/twc-message-queue.c index 26a41ae..24f66c4 100644 --- a/src/twc-message-queue.c +++ b/src/twc-message-queue.c @@ -75,7 +75,7 @@ twc_message_queue_add_friend_message(struct t_twc_profile *profile, // flush if friend is online if (profile->tox - && tox_get_friend_connection_status(profile->tox, friend_number) == 1) + && (tox_friend_get_connection_status(profile->tox, friend_number, NULL) != TOX_CONNECTION_NONE)) twc_message_queue_flush_friend(profile, friend_number); } @@ -96,24 +96,17 @@ twc_message_queue_flush_friend(struct t_twc_profile *profile, struct t_twc_queued_message *queued_message = item->queued_message; // TODO: store and deal with message IDs - uint32_t rc = 0; - switch(queued_message->message_type) - { - case TWC_MESSAGE_TYPE_MESSAGE: - rc = tox_send_message(profile->tox, + TOX_ERR_FRIEND_SEND_MESSAGE err; + (void)tox_friend_send_message(profile->tox, friend_number, + queued_message->message_type == TWC_MESSAGE_TYPE_MESSAGE? + TOX_MESSAGE_TYPE_NORMAL: + TOX_MESSAGE_TYPE_ACTION, (uint8_t *)queued_message->message, - strlen(queued_message->message)); - break; - case TWC_MESSAGE_TYPE_ACTION: - rc = tox_send_action(profile->tox, - friend_number, - (uint8_t *)queued_message->message, - strlen(queued_message->message)); - break; - } + strlen(queued_message->message), + &err); - if (rc == 0) + if (err != TOX_ERR_FRIEND_SEND_MESSAGE_OK) { // break if message send failed break; diff --git a/src/twc-profile.c b/src/twc-profile.c index 8071ab6..5afa012 100644 --- a/src/twc-profile.c +++ b/src/twc-profile.c @@ -51,13 +51,13 @@ struct t_config_option *twc_config_profile_default[TWC_PROFILE_NUM_OPTIONS]; char * twc_profile_expanded_data_path(struct t_twc_profile *profile) { - const char *weechat_dir = weechat_info_get ("weechat_dir", NULL); - const char *base_path = TWC_PROFILE_OPTION_STRING(profile, TWC_PROFILE_OPTION_SAVEFILE); - char *home_expanded = weechat_string_replace(base_path, "%h", weechat_dir); - char *full_path = weechat_string_replace(home_expanded, "%p", profile->name); - free(home_expanded); + const char *weechat_dir = weechat_info_get ("weechat_dir", NULL); + const char *base_path = TWC_PROFILE_OPTION_STRING(profile, TWC_PROFILE_OPTION_SAVEFILE); + char *home_expanded = weechat_string_replace(base_path, "%h", weechat_dir); + char *full_path = weechat_string_replace(home_expanded, "%p", profile->name); + free(home_expanded); - return full_path; + return full_path; } /** @@ -70,33 +70,33 @@ twc_profile_expanded_data_path(struct t_twc_profile *profile) int twc_profile_save_data_file(struct t_twc_profile *profile) { - if (!(profile->tox)) - return -1; + if (!(profile->tox)) + return -1; - char *full_path = twc_profile_expanded_data_path(profile); + char *full_path = twc_profile_expanded_data_path(profile); - // create containing folder if it doesn't exist - char *rightmost_slash = strrchr(full_path, '/'); - char *dir_path = weechat_strndup(full_path, rightmost_slash - full_path); - weechat_mkdir_parents(dir_path, 0755); - free(dir_path); + // create containing folder if it doesn't exist + char *rightmost_slash = strrchr(full_path, '/'); + char *dir_path = weechat_strndup(full_path, rightmost_slash - full_path); + weechat_mkdir_parents(dir_path, 0755); + free(dir_path); - // save Tox data to a buffer - uint32_t size = tox_get_savedata_size(profile->tox); - uint8_t *data = malloc(size); - tox_get_savedata(profile->tox, data); + // save Tox data to a buffer + size_t size = tox_get_savedata_size(profile->tox); + uint8_t *data = malloc(size); + tox_get_savedata(profile->tox, data); - // save buffer to a file - FILE *file = fopen(full_path, "w"); - if (file) + // save buffer to a file + FILE *file = fopen(full_path, "w"); + if (file) { - size_t saved_size = fwrite(data, sizeof(data[0]), size, file); - fclose(file); + size_t saved_size = fwrite(data, 1, size, file); + fclose(file); - return saved_size == size; + return saved_size == size; } - return -1; + return -1; } /** @@ -106,12 +106,12 @@ int twc_profile_buffer_close_callback(void *data, struct t_gui_buffer *buffer) { - struct t_twc_profile *profile = data; + struct t_twc_profile *profile = data; - profile->buffer = NULL; - twc_profile_unload(profile); + profile->buffer = NULL; + twc_profile_unload(profile); - return WEECHAT_RC_OK; + return WEECHAT_RC_OK; } /** @@ -120,7 +120,7 @@ twc_profile_buffer_close_callback(void *data, void twc_profile_init() { - twc_profiles = twc_list_new(); + twc_profiles = twc_list_new(); } /** @@ -129,30 +129,30 @@ twc_profile_init() struct t_twc_profile * twc_profile_new(const char *name) { - struct t_twc_profile *profile = malloc(sizeof(struct t_twc_profile)); - profile->name = strdup(name); - - // add to profile list - twc_list_item_new_data_add(twc_profiles, profile); - - // set up internal vars - profile->tox = NULL; - profile->buffer = NULL; - profile->tox_do_timer = NULL; - profile->tox_online = false; - - profile->chats = twc_list_new(); - profile->friend_requests = twc_list_new(); - profile->group_chat_invites = twc_list_new(); - profile->message_queues = weechat_hashtable_new(32, - WEECHAT_HASHTABLE_INTEGER, - WEECHAT_HASHTABLE_POINTER, - NULL, NULL); - - // set up config - twc_config_init_profile(profile); - - return profile; + struct t_twc_profile *profile = malloc(sizeof(struct t_twc_profile)); + profile->name = strdup(name); + + // add to profile list + twc_list_item_new_data_add(twc_profiles, profile); + + // set up internal vars + profile->tox = NULL; + profile->buffer = NULL; + profile->tox_do_timer = NULL; + profile->tox_online = false; + + profile->chats = twc_list_new(); + profile->friend_requests = twc_list_new(); + profile->group_chat_invites = twc_list_new(); + profile->message_queues = weechat_hashtable_new(32, + WEECHAT_HASHTABLE_INTEGER, + WEECHAT_HASHTABLE_POINTER, + NULL, NULL); + + // set up config + twc_config_init_profile(profile); + + return profile; } /** @@ -162,32 +162,32 @@ void twc_profile_set_options(struct Tox_Options *options, struct t_twc_profile *profile) { - tox_options_default(options); + tox_options_default(options); - const char *proxy_host = - TWC_PROFILE_OPTION_STRING(profile, TWC_PROFILE_OPTION_PROXY_ADDRESS); - if (proxy_host) - options->proxy_host = proxy_host; + const char *proxy_host = + TWC_PROFILE_OPTION_STRING(profile, TWC_PROFILE_OPTION_PROXY_ADDRESS); + if (proxy_host) + options->proxy_host = proxy_host; - switch (TWC_PROFILE_OPTION_INTEGER(profile, TWC_PROFILE_OPTION_PROXY_TYPE)) + switch (TWC_PROFILE_OPTION_INTEGER(profile, TWC_PROFILE_OPTION_PROXY_TYPE)) { - case TWC_PROXY_NONE: - options->proxy_type = TOX_PROXY_TYPE_NONE; - break; - case TWC_PROXY_SOCKS5: - options->proxy_type = TOX_PROXY_TYPE_SOCKS5; - break; - case TWC_PROXY_HTTP: - options->proxy_type = TOX_PROXY_TYPE_HTTP; - break; + case TWC_PROXY_NONE: + options->proxy_type = TOX_PROXY_TYPE_NONE; + break; + case TWC_PROXY_SOCKS5: + options->proxy_type = TOX_PROXY_TYPE_SOCKS5; + break; + case TWC_PROXY_HTTP: + options->proxy_type = TOX_PROXY_TYPE_HTTP; + break; } - options->proxy_port = - TWC_PROFILE_OPTION_INTEGER(profile, TWC_PROFILE_OPTION_PROXY_PORT); - options->udp_enabled = - TWC_PROFILE_OPTION_BOOLEAN(profile, TWC_PROFILE_OPTION_UDP); - options->ipv6_enabled = - TWC_PROFILE_OPTION_BOOLEAN(profile, TWC_PROFILE_OPTION_IPV6); + options->proxy_port = + TWC_PROFILE_OPTION_INTEGER(profile, TWC_PROFILE_OPTION_PROXY_PORT); + options->udp_enabled = + TWC_PROFILE_OPTION_BOOLEAN(profile, TWC_PROFILE_OPTION_UDP); + options->ipv6_enabled = + TWC_PROFILE_OPTION_BOOLEAN(profile, TWC_PROFILE_OPTION_IPV6); } void @@ -195,54 +195,54 @@ twc_tox_new_print_error(struct t_twc_profile *profile, struct Tox_Options *options, TOX_ERR_NEW error) { - switch (error) + switch (error) { - case TOX_ERR_NEW_MALLOC: - weechat_printf(profile->buffer, - "%scould not load Tox (malloc error)", - weechat_prefix("error")); - break; - case TOX_ERR_NEW_PORT_ALLOC: - weechat_printf(profile->buffer, - "%scould not load Tox (failed to allocate a port)", - weechat_prefix("error")); - break; - case TOX_ERR_NEW_PROXY_BAD_TYPE: - weechat_printf(profile->buffer, - "%scould not load Tox (internal error; bad proxy type)", - weechat_prefix("error")); - break; - case TOX_ERR_NEW_PROXY_BAD_HOST: - weechat_printf(profile->buffer, - "%scould not load Tox (invalid proxy host: \"%s\")", - weechat_prefix("error"), options->proxy_host); - break; - case TOX_ERR_NEW_PROXY_BAD_PORT: - weechat_printf(profile->buffer, - "%scould not load Tox (invalid proxy port: \"%d\")", - weechat_prefix("error"), options->proxy_port); - break; - case TOX_ERR_NEW_PROXY_NOT_FOUND: - weechat_printf(profile->buffer, - "%scould not load Tox (proxy host not found: \"%s\")", - weechat_prefix("error"), options->proxy_host); - break; - case TOX_ERR_NEW_LOAD_ENCRYPTED: - weechat_printf(profile->buffer, - "%scould not load Tox (encrypted data files are not yet supported)", - weechat_prefix("error")); - break; - case TOX_ERR_NEW_LOAD_BAD_FORMAT: - weechat_printf(profile->buffer, - "%scould not load Tox (invalid data file, some data " - "may have been loaded; use -force to try using it)", - weechat_prefix("error")); - break; - default: - weechat_printf(profile->buffer, - "%scould not load Tox (unknown error %d)", - weechat_prefix("error"), error); - break; + case TOX_ERR_NEW_MALLOC: + weechat_printf(profile->buffer, + "%scould not load Tox (malloc error)", + weechat_prefix("error")); + break; + case TOX_ERR_NEW_PORT_ALLOC: + weechat_printf(profile->buffer, + "%scould not load Tox (failed to allocate a port)", + weechat_prefix("error")); + break; + case TOX_ERR_NEW_PROXY_BAD_TYPE: + weechat_printf(profile->buffer, + "%scould not load Tox (internal error; bad proxy type)", + weechat_prefix("error")); + break; + case TOX_ERR_NEW_PROXY_BAD_HOST: + weechat_printf(profile->buffer, + "%scould not load Tox (invalid proxy host: \"%s\")", + weechat_prefix("error"), options->proxy_host); + break; + case TOX_ERR_NEW_PROXY_BAD_PORT: + weechat_printf(profile->buffer, + "%scould not load Tox (invalid proxy port: \"%d\")", + weechat_prefix("error"), options->proxy_port); + break; + case TOX_ERR_NEW_PROXY_NOT_FOUND: + weechat_printf(profile->buffer, + "%scould not load Tox (proxy host not found: \"%s\")", + weechat_prefix("error"), options->proxy_host); + break; + case TOX_ERR_NEW_LOAD_ENCRYPTED: + weechat_printf(profile->buffer, + "%scould not load Tox (encrypted data files are not yet supported)", + weechat_prefix("error")); + break; + case TOX_ERR_NEW_LOAD_BAD_FORMAT: + weechat_printf(profile->buffer, + "%scould not load Tox (invalid data file, some data " + "may have been loaded; use -force to try using it)", + weechat_prefix("error")); + break; + default: + weechat_printf(profile->buffer, + "%scould not load Tox (unknown error %d)", + weechat_prefix("error"), error); + break; } } @@ -258,14 +258,14 @@ twc_profile_load(struct t_twc_profile *profile) return TWC_RC_ERROR; if (!(profile->buffer)) - { - // create main buffer - profile->buffer = weechat_buffer_new(profile->name, - NULL, NULL, - twc_profile_buffer_close_callback, profile); - if (!(profile->buffer)) - return TWC_RC_ERROR; - } + { + // create main buffer + profile->buffer = weechat_buffer_new(profile->name, + NULL, NULL, + twc_profile_buffer_close_callback, profile); + if (!(profile->buffer)) + return TWC_RC_ERROR; + } weechat_printf(profile->buffer, "%s profile %s connecting", @@ -273,37 +273,51 @@ twc_profile_load(struct t_twc_profile *profile) // create Tox options object struct Tox_Options options; - twc_profile_set_options(&options, profile); + + tox_options_default(&options); + //twc_profile_set_options(options, profile); // print a proxy message if (options.proxy_type != TOX_PROXY_TYPE_NONE) - { - weechat_printf(profile->buffer, - "%susing %s proxy %s:%d", - weechat_prefix("network"), - options.proxy_type == TOX_PROXY_TYPE_HTTP ? "HTTP" : - TOX_PROXY_TYPE_SOCKS5 ? "SOCKS5" : - NULL, - options.proxy_host, options.proxy_port); - } + { + weechat_printf(profile->buffer, + "%susing %s proxy %s:%d", + weechat_prefix("network"), + options.proxy_type == TOX_PROXY_TYPE_HTTP ? "HTTP" : + TOX_PROXY_TYPE_SOCKS5 ? "SOCKS5" : + NULL, + options.proxy_host, options.proxy_port); + } // try loading data file char *path = twc_profile_expanded_data_path(profile); - uint8_t *data; - size_t data_size = 0; - enum t_twc_rc data_rc = twc_read_file(path, &data, &data_size); + FILE *file = NULL; + size_t data_size; + if (!(file = fopen(path, "r"))) + data_size = 0; + else { + fseek(file, 0, SEEK_END); + data_size = ftell(file); + } + uint8_t data[data_size]; - if (data_rc == TWC_RC_ERROR_MALLOC) - { - weechat_printf(profile->buffer, - "%scould not load Tox data file, aborting (malloc error)", - weechat_prefix("error")); - return TWC_RC_ERROR_MALLOC; + if (file) { + rewind(file); + if ((data_size != fread(&data, 1, data_size, file))) { + fclose(file); + weechat_printf(profile->buffer, "%scould not load Tox data file, aborting", + weechat_prefix("error")); + return TWC_RC_ERROR; + } + fclose(file); } + options.savedata_type = (data_size == 0)? TOX_SAVEDATA_TYPE_NONE: TOX_SAVEDATA_TYPE_TOX_SAVE; + options.savedata_data = data; + options.savedata_length = data_size; // create Tox TOX_ERR_NEW rc; - profile->tox = tox_new(&options, data, data_size, &rc); + profile->tox = tox_new(&options, &rc); if (rc != TOX_ERR_NEW_OK) { twc_tox_new_print_error(profile, &options, rc); @@ -354,6 +368,7 @@ twc_profile_load(struct t_twc_profile *profile) tox_callback_group_action(profile->tox, twc_group_action_callback, profile); tox_callback_group_namelist_change(profile->tox, twc_group_namelist_change_callback, profile); tox_callback_group_title(profile->tox, twc_group_title_callback, profile); + return TWC_RC_OK; } /** diff --git a/src/twc-utils.c b/src/twc-utils.c index 0e8cf40..1c05e90 100644 --- a/src/twc-utils.c +++ b/src/twc-utils.c @@ -81,14 +81,16 @@ twc_null_terminate(const uint8_t *str, size_t length) char * twc_get_name_nt(Tox *tox, int32_t friend_number) { - size_t length = tox_get_name_size(tox, friend_number); - uint8_t name[length]; + TOX_ERR_FRIEND_QUERY err; + size_t length = tox_friend_get_name_size(tox, friend_number, &err); + + if ((err != TOX_ERR_FRIEND_QUERY_OK) || + (length == 0)) + return twc_get_friend_id_short(tox, friend_number); - // if no name, return client ID instead - if (!length) - return twc_get_friend_id_short(tox, friend_number); + uint8_t name[length]; - tox_get_name(tox, friend_number, name); + tox_friend_get_name(tox, friend_number, name, &err); return twc_null_terminate(name, length); } @@ -98,9 +100,18 @@ twc_get_name_nt(Tox *tox, int32_t friend_number) char * twc_get_status_message_nt(Tox *tox, int32_t friend_number) { - size_t length = tox_get_status_message_size(tox, friend_number); + TOX_ERR_FRIEND_QUERY err; + size_t length = tox_friend_get_status_message_size(tox, friend_number, &err); + + if ((err != TOX_ERR_FRIEND_QUERY_OK) || + (length == SIZE_MAX)) { + char *msg = malloc(1); + *msg = 0; + return msg; + } + uint8_t message[length]; - tox_get_status_message(tox, friend_number, message, length); + tox_friend_get_status_message(tox, friend_number, message, &err); return twc_null_terminate(message, length); } @@ -127,9 +138,9 @@ twc_get_peer_name_nt(Tox *tox, int32_t group_number, int32_t peer_number) char * twc_get_self_name_nt(Tox *tox) { - size_t length = tox_get_self_name_size(tox); + size_t length = tox_self_get_name_size(tox); uint8_t name[length]; - tox_get_self_name(tox, name); + tox_self_get_name(tox, name); return twc_null_terminate(name, length); } @@ -141,11 +152,16 @@ char * twc_get_friend_id_short(Tox *tox, int32_t friend_number) { uint8_t client_id[TOX_PUBLIC_KEY_SIZE]; - tox_get_client_id(tox, friend_number, client_id); - + TOX_ERR_FRIEND_GET_PUBLIC_KEY err; size_t short_id_length = weechat_config_integer(twc_config_short_id_size); - char *hex_address = malloc(short_id_length + 1); + + tox_friend_get_public_key(tox, friend_number, client_id, &err); + + // return a zero public key on failure + if (err != TOX_ERR_FRIEND_GET_PUBLIC_KEY_OK) + memset(client_id, 0, TOX_PUBLIC_KEY_SIZE); + twc_bin2hex(client_id, short_id_length / 2, hex_address); @@ -193,18 +209,18 @@ enum t_twc_rc twc_read_file(const char *path, uint8_t **data, size_t *size) { FILE *file; - if (file = fopen(path, "r")) + if ((file = fopen(path, "r"))) { // get file size fseek(file, 0, SEEK_END); *size = ftell(file); rewind(file); - if (data = malloc(sizeof(*data) * *size)) + if ((data = malloc(sizeof(**data) * *size))) { - fread(data, sizeof(uint8_t), *size, file); + size_t rs = fread(data, sizeof(uint8_t), *size, file); fclose(file); - return TWC_RC_OK; + return rs == *size ? TWC_RC_OK : TWC_RC_ERROR; } else {