From 919ffee4d0cb3d25c863e2b0fcdc02acf02eea81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Pettersson?= Date: Wed, 26 Aug 2015 20:02:36 +0200 Subject: [PATCH] Additional error handling. --- src/twc-commands.c | 109 ++++++++++++++++++++++++++++++--------- src/twc-completion.c | 12 +++-- src/twc-friend-request.c | 7 ++- src/twc-friend-request.h | 2 +- src/twc-profile.c | 2 +- 5 files changed, 100 insertions(+), 32 deletions(-) diff --git a/src/twc-commands.c b/src/twc-commands.c index 7df4b18..edcd628 100644 --- a/src/twc-commands.c +++ b/src/twc-commands.c @@ -162,11 +162,13 @@ 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_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 (tox_friend_get_public_key(profile->tox, friend_numbers[i], tox_id, NULL)) + { + twc_bin2hex(tox_id, TOX_PUBLIC_KEY_SIZE, hex_id); - if (weechat_strcasecmp(hex_id, search_string) == 0) - return friend_numbers[i]; + if (weechat_strcasecmp(hex_id, search_string) == 0) + return friend_numbers[i]; + } } char *name = twc_get_name_nt(profile->tox, friend_numbers[i]); @@ -407,20 +409,39 @@ twc_cmd_friend(void *data, struct t_gui_buffer *buffer, if (weechat_strcasecmp(argv[2], "all") == 0) { size_t index; + size_t count = 0; struct t_twc_list_item *item; twc_list_foreach(profile->friend_requests, index, item) { if (accept) - twc_friend_request_accept(item->friend_request); + { + if (twc_friend_request_accept(item->friend_request)) + { + ++count; + } + else + { + char hex_address[TOX_PUBLIC_KEY_SIZE * 2 + 1]; + twc_bin2hex(item->friend_request->tox_id, + TOX_PUBLIC_KEY_SIZE, + hex_address); + weechat_printf(profile->buffer, + "%sCould not accept friend request from %s", + weechat_prefix("error"), hex_address); + } + } else + { twc_friend_request_remove(item->friend_request); + ++count; + } } weechat_printf(profile->buffer, "%s%s %d friend requests.", weechat_prefix("network"), accept ? "Accepted" : "Declined", - index); + count); return WEECHAT_RC_OK; } @@ -442,18 +463,30 @@ twc_cmd_friend(void *data, struct t_gui_buffer *buffer, hex_address); if (accept) - twc_friend_request_accept(request); + { + if (twc_friend_request_accept(request)) + { + weechat_printf(profile->buffer, + "%sCould not accept friend request from %s", + weechat_prefix("error"), hex_address); + } + else + { + weechat_printf(profile->buffer, + "%sAccepted friend request from %s.", + weechat_prefix("network"), hex_address); + } + } else + { twc_friend_request_remove(request); + weechat_printf(profile->buffer, + "%sDeclined friend request from %s.", + weechat_prefix("network"), hex_address); + } twc_friend_request_free(request); - weechat_printf(profile->buffer, - "%s%s friend request from %s.", - weechat_prefix("network"), - accept ? "Accepted" : "Declined", - hex_address); - return WEECHAT_RC_OK; } } @@ -721,15 +754,29 @@ twc_cmd_name(void *data, struct t_gui_buffer *buffer, TWC_CHECK_PROFILE(profile); TWC_CHECK_PROFILE_LOADED(profile); - char *name = argv_eol[1]; + const char *name = argv_eol[1]; - int result = tox_self_set_name(profile->tox, (uint8_t *)name, strlen(name), NULL); - if (!result) + TOX_ERR_SET_INFO err; + tox_self_set_name(profile->tox, (uint8_t *)name, strlen(name), &err); + if (err != TOX_ERR_SET_INFO_OK) { + char *err_msg; + switch (err) + { + case TOX_ERR_SET_INFO_NULL: + err_msg = "no name given"; + break; + case TOX_ERR_SET_INFO_TOO_LONG: + err_msg = "name too long"; + break; + default: + err_msg = "unknown error"; + break; + } weechat_printf(profile->buffer, - "%s%s", + "%s%s%s", weechat_prefix("error"), - "Could not change name."); + "Could not change name: ", err_msg); return WEECHAT_RC_OK; } @@ -907,15 +954,29 @@ twc_cmd_statusmsg(void *data, struct t_gui_buffer *buffer, char *message = argc > 1 ? argv_eol[1] : " "; - bool result = tox_self_set_status_message(profile->tox, - (uint8_t *)message, - strlen(message), NULL); - if (!result) + TOX_ERR_SET_INFO err; + tox_self_set_status_message(profile->tox, + (uint8_t *)message, + strlen(message), &err); + if (err != TOX_ERR_SET_INFO_OK) { + char *err_msg; + switch (err) + { + case TOX_ERR_SET_INFO_NULL: + err_msg = "no status given"; + break; + case TOX_ERR_SET_INFO_TOO_LONG: + err_msg = "status too long"; + break; + default: + err_msg = "unknown error"; + break; + } weechat_printf(profile->buffer, - "%s%s", + "%s%s%s", weechat_prefix("error"), - "Could not set status message."); + "Could not set status message: ", err_msg); } return WEECHAT_RC_OK; diff --git a/src/twc-completion.c b/src/twc-completion.c index 5e516bb..37affca 100644 --- a/src/twc-completion.c +++ b/src/twc-completion.c @@ -68,11 +68,15 @@ twc_completion_friend(void *data, uint8_t tox_id[TOX_PUBLIC_KEY_SIZE]; char hex_id[TOX_PUBLIC_KEY_SIZE * 2 + 1]; - 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); + TOX_ERR_FRIEND_GET_PUBLIC_KEY err; + tox_friend_get_public_key(profile->tox, friend_numbers[i], tox_id, &err); + if (err == TOX_ERR_FRIEND_GET_PUBLIC_KEY_OK) + { + twc_bin2hex(tox_id, TOX_PUBLIC_KEY_SIZE, hex_id); - weechat_hook_completion_list_add(completion, hex_id, 0, - WEECHAT_LIST_POS_SORT); + weechat_hook_completion_list_add(completion, hex_id, 0, + WEECHAT_LIST_POS_SORT); + } } if (flags & TWC_COMPLETE_FRIEND_NAME) diff --git a/src/twc-friend-request.c b/src/twc-friend-request.c index f5d4242..b818cc2 100644 --- a/src/twc-friend-request.c +++ b/src/twc-friend-request.c @@ -64,11 +64,14 @@ twc_friend_request_add(struct t_twc_profile *profile, /** * Accept a friend request. Remove and free the request. */ -void +bool twc_friend_request_accept(struct t_twc_friend_request *request) { - tox_friend_add_norequest(request->profile->tox, request->tox_id, NULL); //do error handling + TOX_ERR_FRIEND_ADD err; + tox_friend_add_norequest(request->profile->tox, request->tox_id, &err); twc_friend_request_remove(request); + + return err == TOX_ERR_FRIEND_ADD_OK; } /** diff --git a/src/twc-friend-request.h b/src/twc-friend-request.h index 2470535..96e5f17 100644 --- a/src/twc-friend-request.h +++ b/src/twc-friend-request.h @@ -40,7 +40,7 @@ twc_friend_request_add(struct t_twc_profile *profile, const uint8_t *client_id, const char *message); -void +bool twc_friend_request_accept(struct t_twc_friend_request *request); void diff --git a/src/twc-profile.c b/src/twc-profile.c index 5afa012..8676b41 100644 --- a/src/twc-profile.c +++ b/src/twc-profile.c @@ -268,7 +268,7 @@ twc_profile_load(struct t_twc_profile *profile) } weechat_printf(profile->buffer, - "%s profile %s connecting", + "%sprofile %s connecting", weechat_prefix("network"), profile->name); // create Tox options object