From f7b086d38df4850b3dddcdeaebb7757a93e458de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Pettersson?= Date: Fri, 19 Sep 2014 19:15:19 +0200 Subject: [PATCH] Implemented /tox disconnect. --- src/tox-weechat-commands.c | 36 +++++++++++++++++++++++++++------ src/tox-weechat-gui.c | 14 ++++++++----- src/tox-weechat-identities.c | 32 +++++++++++++++++++++++++++++ src/tox-weechat-identities.h | 4 ++++ src/tox-weechat-tox-callbacks.c | 3 +-- 5 files changed, 76 insertions(+), 13 deletions(-) diff --git a/src/tox-weechat-commands.c b/src/tox-weechat-commands.c index d28f575..1a887ac 100644 --- a/src/tox-weechat-commands.c +++ b/src/tox-weechat-commands.c @@ -649,6 +649,27 @@ tox_weechat_cmd_tox(void *data, struct t_gui_buffer *buffer, return WEECHAT_RC_OK; } + 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) + { + 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; + } + return WEECHAT_RC_ERROR; } @@ -719,16 +740,19 @@ tox_weechat_commands_init() "list" " || create " " || delete -yes|-keepdata" - " || connect ", - " list: list all Tox identity\n" - " create: create a new Tox identity\n" - " delete: delete a Tox identity; requires either " + " || connect " + " || disconnect ", + " list: list all Tox identity\n" + " create: create a new Tox identity\n" + " delete: delete a Tox identity; requires either " "-yes to confirm deletion or -keepdata to delete the " "identity but keep the Tox data file\n" - "connect: connect a Tox identity to the network\n", + " connect: connect a Tox identity to the network\n" + "disconnect: connect a Tox identity to the network\n", "list" " || create" " || delete %(tox_identities) -yes|-keepdata" - " || connect %(tox_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 58aabfc..ed7bcca 100644 --- a/src/tox-weechat-gui.c +++ b/src/tox-weechat-gui.c @@ -42,6 +42,9 @@ bar_item_away(void *data, if (!identity) return NULL; + if (identity->tox == NULL || identity->tox_online == false) + return NULL; + char *status = NULL;; switch (tox_get_self_user_status(identity->tox)) { @@ -64,6 +67,10 @@ bar_item_input_prompt(void *data, struct t_hashtable *extra_info) { struct t_tox_weechat_identity *identity = tox_weechat_identity_for_buffer(buffer); + + if (identity->tox == NULL || identity->tox_online == false) + return NULL; + return tox_weechat_get_self_name_nt(identity->tox); } @@ -81,14 +88,11 @@ 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, - weechat_color("bar_delim"), - weechat_color("bar_fg"), - identity->tox_online ? "online" : "offline"); + identity_name); return strdup(string); } diff --git a/src/tox-weechat-identities.c b/src/tox-weechat-identities.c index 2843cb5..0aacc13 100644 --- a/src/tox-weechat-identities.c +++ b/src/tox-weechat-identities.c @@ -199,6 +199,7 @@ tox_weechat_identity_new(const char *name) identity->tox_do_timer = NULL; identity->chats = identity->last_chat = NULL; identity->friend_requests = identity->last_friend_request = NULL; + identity->tox_online = false; // set up config tox_weechat_config_init_identity(identity); @@ -301,6 +302,8 @@ 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); } @@ -317,6 +320,35 @@ tox_weechat_identity_autoconnect() } } +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) + { + 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); + } +} + struct t_tox_weechat_identity * tox_weechat_identity_name_search(const char *name) { diff --git a/src/tox-weechat-identities.h b/src/tox-weechat-identities.h index 3bb10af..93b6b6a 100644 --- a/src/tox-weechat-identities.h +++ b/src/tox-weechat-identities.h @@ -70,6 +70,10 @@ tox_weechat_identity_disconnect(struct t_tox_weechat_identity *identity); void tox_weechat_identity_autoconnect(); +void +tox_weechat_identity_set_online_status(struct t_tox_weechat_identity *identity, + bool online); + struct t_tox_weechat_identity * tox_weechat_identity_name_search(const char *name); diff --git a/src/tox-weechat-tox-callbacks.c b/src/tox-weechat-tox-callbacks.c index 09e4bec..cfaabaa 100644 --- a/src/tox-weechat-tox-callbacks.c +++ b/src/tox-weechat-tox-callbacks.c @@ -47,8 +47,7 @@ tox_weechat_do_timer_cb(void *data, int connected = tox_isconnected(identity->tox); if (connected ^ identity->tox_online) { - identity->tox_online = connected; - weechat_bar_item_update("buffer_plugin"); + tox_weechat_identity_set_online_status(identity, connected); } }