From 4c77e80a7f43ef4ac0dd81db7b871d17504ec2ef Mon Sep 17 00:00:00 2001 From: jfreegman Date: Wed, 9 Aug 2023 10:39:27 -0400 Subject: [PATCH] Fix ID lookup error messages We now properly distinguish between ID lookups and domain name lookups and print the appropriate error message for each given case --- src/global_commands.c | 12 +++++++++--- src/name_lookup.c | 18 ++++++++++++------ src/name_lookup.h | 11 ++++++++--- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/global_commands.c b/src/global_commands.c index 1652e70..b33bea9 100644 --- a/src/global_commands.c +++ b/src/global_commands.c @@ -173,10 +173,16 @@ void cmd_add(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX char id_bin[TOX_ADDRESS_SIZE] = {0}; - const bool is_tox_id = (char_find(0, id, '@') == arg_length) && (arg_length >= TOX_ADDRESS_SIZE * 2); + const bool is_domain = char_find(0, id, '@') != arg_length; + const bool valid_id_size = arg_length >= TOX_ADDRESS_SIZE * 2; // arg_length may include invite message - if (!is_tox_id) { - name_lookup(self, m, id_bin, id, msg); + if (is_domain) { + if (!name_lookup(self, m, id_bin, id, msg)) { + return; + } + } else if (!valid_id_size) { + line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid Tox ID."); + return; } char xx[3]; diff --git a/src/name_lookup.c b/src/name_lookup.c index de9576f..6ff8c56 100644 --- a/src/name_lookup.c +++ b/src/name_lookup.c @@ -362,16 +362,20 @@ on_exit: return 0; } -void name_lookup(ToxWindow *self, Tox *m, const char *id_bin, const char *addr, const char *message) +/* Attempts to do a tox name lookup. + * + * Returns true on success. + */ +bool name_lookup(ToxWindow *self, Tox *m, const char *id_bin, const char *addr, const char *message) { if (t_data.disabled) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "nameservers list is empty or does not exist."); - return; + return false; } if (t_data.busy) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Please wait for previous name lookup to finish."); - return; + return false; } snprintf(t_data.id_bin, sizeof(t_data.id_bin), "%s", id_bin); @@ -384,22 +388,24 @@ void name_lookup(ToxWindow *self, Tox *m, const char *id_bin, const char *addr, if (pthread_attr_init(&lookup_thread.attr) != 0) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, RED, "Error: lookup thread attr failed to init"); clear_thread_data(); - return; + return false; } if (pthread_attr_setdetachstate(&lookup_thread.attr, PTHREAD_CREATE_DETACHED) != 0) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, RED, "Error: lookup thread attr failed to set"); pthread_attr_destroy(&lookup_thread.attr); clear_thread_data(); - return; + return false; } if (pthread_create(&lookup_thread.tid, &lookup_thread.attr, lookup_thread_func, NULL) != 0) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, RED, "Error: lookup thread failed to init"); pthread_attr_destroy(&lookup_thread.attr); clear_thread_data(); - return; + return false; } + + return true; } /* Initializes http based name lookups. Note: This function must be called only once before additional diff --git a/src/name_lookup.h b/src/name_lookup.h index e9b8ed4..2a0fba0 100644 --- a/src/name_lookup.h +++ b/src/name_lookup.h @@ -23,14 +23,19 @@ #ifndef NAME_LOOKUP #define NAME_LOOKUP -/* Initializes http based name lookups. Note: This function must be called only once before additional - * threads are spawned. +/* Initializes http based name lookups. + * + * Note: This function must be called only once before additional threads are spawned. * * Returns 0 on success. * Returns -1 on failure. */ int name_lookup_init(int curl_init_status); -int name_lookup(ToxWindow *self, Tox *m, const char *id_bin, const char *addr, const char *message); +/* Attempts to do a tox name lookup. + * + * Returns true on success. + */ +bool name_lookup(ToxWindow *self, Tox *m, const char *id_bin, const char *addr, const char *message); #endif /* NAME_LOOKUP */