From 7c81c9dcfc3ffe39d235698f1c1e75aeaac89cc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Pettersson?= Date: Sat, 20 Sep 2014 09:03:31 +0200 Subject: [PATCH] Fixed connecting/disconnecting. --- src/tox-weechat-commands.c | 68 +++++++++++++++------------- src/tox-weechat-gui.c | 14 +++--- src/tox-weechat-identities.c | 79 ++++++++++++++++++++------------- src/tox-weechat-identities.h | 3 ++ src/tox-weechat-tox-callbacks.c | 5 +-- 5 files changed, 94 insertions(+), 75 deletions(-) diff --git a/src/tox-weechat-commands.c b/src/tox-weechat-commands.c index 1a887ac..70d1218 100644 --- a/src/tox-weechat-commands.c +++ b/src/tox-weechat-commands.c @@ -628,43 +628,47 @@ tox_weechat_cmd_tox(void *data, struct t_gui_buffer *buffer, return WEECHAT_RC_OK; } - else if (argc == 3 && (weechat_strcasecmp(argv[1], "connect") == 0)) + else if (argc >= 3 && (weechat_strcasecmp(argv[1], "connect") == 0)) { - char *name = argv[2]; - - struct t_tox_weechat_identity *identity = tox_weechat_identity_name_search(name); - if (!identity) + for (int i = 2; i < argc; ++i) { - weechat_printf(NULL, - "%s%s: Identity \"%s\" does not exist.", - weechat_prefix("error"), - weechat_plugin->name, - name); - } - else - { - tox_weechat_identity_connect(identity); + char *name = argv[i]; + struct t_tox_weechat_identity *identity = tox_weechat_identity_name_search(name); + if (!identity) + { + weechat_printf(NULL, + "%s%s: Identity \"%s\" does not exist.", + weechat_prefix("error"), + weechat_plugin->name, + name); + } + else + { + tox_weechat_identity_connect(identity); + } } return WEECHAT_RC_OK; } - else if (argc == 3 && (weechat_strcasecmp(argv[1], "disconnect") == 0)) + else if (argc >= 3 && (weechat_strcasecmp(argv[1], "disconnect") == 0)) { - char *name = argv[2]; - - struct t_tox_weechat_identity *identity = tox_weechat_identity_name_search(name); - if (!identity) + for (int i = 2; i < argc; ++i) { - weechat_printf(NULL, - "%s%s: Identity \"%s\" does not exist.", - weechat_prefix("error"), - weechat_plugin->name, - name); - } - else - { - tox_weechat_identity_disconnect(identity); + char *name = argv[i]; + struct t_tox_weechat_identity *identity = tox_weechat_identity_name_search(name); + if (!identity) + { + weechat_printf(NULL, + "%s%s: Identity \"%s\" does not exist.", + weechat_prefix("error"), + weechat_plugin->name, + name); + } + else + { + tox_weechat_identity_disconnect(identity); + } } return WEECHAT_RC_OK; @@ -740,8 +744,8 @@ tox_weechat_commands_init() "list" " || create " " || delete -yes|-keepdata" - " || connect " - " || disconnect ", + " || connect [...]" + " || disconnect [...]", " list: list all Tox identity\n" " create: create a new Tox identity\n" " delete: delete a Tox identity; requires either " @@ -752,7 +756,7 @@ tox_weechat_commands_init() "list" " || create" " || delete %(tox_identities) -yes|-keepdata" - " || connect %(tox_disconnected_identities)" - " || disconnect %(tox_connected_identities)", + " || connect %(tox_disconnected_identities)|%*" + " || disconnect %(tox_connected_identities)|%*", tox_weechat_cmd_tox, NULL); } diff --git a/src/tox-weechat-gui.c b/src/tox-weechat-gui.c index ed7bcca..912a6c1 100644 --- a/src/tox-weechat-gui.c +++ b/src/tox-weechat-gui.c @@ -39,10 +39,7 @@ bar_item_away(void *data, { struct t_tox_weechat_identity *identity = tox_weechat_identity_for_buffer(buffer); - if (!identity) - return NULL; - - if (identity->tox == NULL || identity->tox_online == false) + if (!identity || !(identity->tox)) return NULL; char *status = NULL;; @@ -68,7 +65,7 @@ bar_item_input_prompt(void *data, { struct t_tox_weechat_identity *identity = tox_weechat_identity_for_buffer(buffer); - if (identity->tox == NULL || identity->tox_online == false) + if (!identity || !(identity->tox)) return NULL; return tox_weechat_get_self_name_nt(identity->tox); @@ -88,11 +85,14 @@ bar_item_buffer_plugin(void *data, struct t_gui_bar_item *item, const char *identity_name = identity->name; snprintf(string, sizeof(string), - "%s%s/%s%s", + "%s%s/%s%s%s/%s%s", plugin_name, weechat_color("bar_delim"), weechat_color("bar_fg"), - identity_name); + identity_name, + weechat_color("bar_delim"), + weechat_color("bar_fg"), + identity->tox_online ? "online" : "offline"); return strdup(string); } diff --git a/src/tox-weechat-identities.c b/src/tox-weechat-identities.c index 0aacc13..6a6679d 100644 --- a/src/tox-weechat-identities.c +++ b/src/tox-weechat-identities.c @@ -175,6 +175,14 @@ tox_weechat_bootstrap_tox(Tox *tox, const char *address, uint16_t port, const ch return result; } +void +tox_weechat_bootstrap_random_node(Tox *tox) +{ + int i = rand() % tox_weechat_bootstrap_count; + tox_weechat_bootstrap_tox(tox, tox_weechat_bootstrap_addresses[i], + tox_weechat_bootstrap_ports[i], + tox_weechat_bootstrap_keys[i]); +} struct t_tox_weechat_identity * tox_weechat_identity_new(const char *name) @@ -207,15 +215,6 @@ tox_weechat_identity_new(const char *name) return identity; } -void -tox_weechat_bootstrap_random_node(Tox *tox) -{ - int i = rand() % tox_weechat_bootstrap_count; - tox_weechat_bootstrap_tox(tox, tox_weechat_bootstrap_addresses[i], - tox_weechat_bootstrap_ports[i], - tox_weechat_bootstrap_keys[i]); -} - void tox_weechat_identity_connect(struct t_tox_weechat_identity *identity) { @@ -230,6 +229,12 @@ tox_weechat_identity_connect(struct t_tox_weechat_identity *identity) tox_weechat_identity_buffer_close_callback, identity); } + weechat_printf(identity->buffer, + "%s%s: identity %s connecting", + weechat_prefix("network"), + weechat_plugin->name, + identity->name); + // create Tox identity->tox = tox_new(NULL); @@ -302,10 +307,12 @@ tox_weechat_identity_disconnect(struct t_tox_weechat_identity *identity) free(path); } - tox_weechat_identity_set_online_status(identity, false); - // stop Tox timer weechat_unhook(identity->tox_do_timer); + + // have to refresh and hide bar items even if we were already offline + tox_weechat_identity_refresh_online_status(identity); + tox_weechat_identity_set_online_status(identity, false); } void @@ -320,32 +327,40 @@ tox_weechat_identity_autoconnect() } } +void +tox_weechat_identity_refresh_online_status(struct t_tox_weechat_identity *identity) +{ + weechat_bar_item_update("buffer_plugin"); + weechat_bar_item_update("input_prompt"); + weechat_bar_item_update("away"); +} + void tox_weechat_identity_set_online_status(struct t_tox_weechat_identity *identity, bool online) { - identity->tox_online = identity->tox && online; - - weechat_bar_item_update("input_prompt"); - weechat_bar_item_update("away"); - - struct t_gui_buffer *buffer = identity->buffer ?: NULL; - - if (identity->tox_online) + if (identity->tox_online ^ online) { - weechat_printf(buffer, - "%s%s: identity %s connected", - weechat_prefix("network"), - weechat_plugin->name, - identity->name); - } - else - { - weechat_printf(buffer, - "%s%s: identity %s disconnected", - weechat_prefix("network"), - weechat_plugin->name, - identity->name); + identity->tox_online = online; + tox_weechat_identity_refresh_online_status(identity); + + struct t_gui_buffer *buffer = identity->buffer ?: NULL; + if (identity->tox_online) + { + weechat_printf(buffer, + "%s%s: identity %s connected", + weechat_prefix("network"), + weechat_plugin->name, + identity->name); + } + else + { + weechat_printf(buffer, + "%s%s: identity %s disconnected", + weechat_prefix("network"), + weechat_plugin->name, + identity->name); + } } } diff --git a/src/tox-weechat-identities.h b/src/tox-weechat-identities.h index 93b6b6a..ea8a4e2 100644 --- a/src/tox-weechat-identities.h +++ b/src/tox-weechat-identities.h @@ -70,6 +70,9 @@ tox_weechat_identity_disconnect(struct t_tox_weechat_identity *identity); void tox_weechat_identity_autoconnect(); +void +tox_weechat_identity_refresh_online_status(struct t_tox_weechat_identity *identity); + void tox_weechat_identity_set_online_status(struct t_tox_weechat_identity *identity, bool online); diff --git a/src/tox-weechat-tox-callbacks.c b/src/tox-weechat-tox-callbacks.c index cfaabaa..31ec3ef 100644 --- a/src/tox-weechat-tox-callbacks.c +++ b/src/tox-weechat-tox-callbacks.c @@ -45,10 +45,7 @@ tox_weechat_do_timer_cb(void *data, // check connection status int connected = tox_isconnected(identity->tox); - if (connected ^ identity->tox_online) - { - tox_weechat_identity_set_online_status(identity, connected); - } + tox_weechat_identity_set_online_status(identity, connected); } return WEECHAT_RC_OK;