From e6314bd1c75979f6ec71cd1301239bccca7618a4 Mon Sep 17 00:00:00 2001 From: jgeboski Date: Wed, 22 Jun 2016 14:54:52 -0400 Subject: Added an interface for the listing of existing chatrooms Several protocols can provide a list of existing chatrooms that a user is able join. This is crucial for the usage of several protocols, most notably Purple and Facebook. Plugins wishing to support this extended functionality must implement the new prpl->chat_list() function. This implemented function will in most cases send a remote request for the list of chatrooms. Once the list of chatrooms is obtained, a bee_chat_info_t GSList must be created and assigned to the im_connection->chatlist field. Then a call to the bee_chat_list_finish() is needed to display the list to the user. The chat list is maintained entirely by the plugin, so it is important to ensure all pointers related to the chat list remain valid until the chat list is set to NULL. This list is used internally by bitlbee to calculate indexes, which then allows the user to join a chat with an index, rather than some random identifier. It also important to ensure the list is properly freed whenever it is updated, or when the account is disconnect via the prpl->logout() function. On the user interface side of things, the 'chat list' subcommand was recommissioned. For a user to list the existing chat rooms: chat list Afterwards a user can join a chatroom in the list with its index. This extends the functionality of the 'chat add' subcommand by adding in support for the exclamation point operator to denote an index. chat add ! [channel] --- root_commands.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 75 insertions(+), 5 deletions(-) (limited to 'root_commands.c') diff --git a/root_commands.c b/root_commands.c index add9bceb..59974026 100644 --- a/root_commands.c +++ b/root_commands.c @@ -1234,8 +1234,10 @@ static void cmd_chat(irc_t *irc, char **cmd) account_t *acc; if (g_strcasecmp(cmd[1], "add") == 0) { - char *channel, *s; + bee_chat_info_t *ci; + char *channel, *room, *s; struct irc_channel *ic; + guint i; MIN_ARGS(3); @@ -1247,8 +1249,22 @@ static void cmd_chat(irc_t *irc, char **cmd) return; } + if (cmd[3][0] == '!') { + i = g_ascii_strtoull(cmd[3] + 1, NULL, 10); + ci = g_slist_nth_data(acc->ic->chatlist, i - 1); + + if (ci == NULL) { + irc_rootmsg(irc, "Invalid chatroom index"); + return; + } + + room = ci->title; + } else { + room = cmd[3]; + } + if (cmd[4] == NULL) { - channel = g_strdup(cmd[3]); + channel = g_strdup(room); if ((s = strchr(channel, '@'))) { *s = 0; } @@ -1268,7 +1284,7 @@ static void cmd_chat(irc_t *irc, char **cmd) set_setstr(&ic->set, "type", "chat") && set_setstr(&ic->set, "chat_type", "room") && set_setstr(&ic->set, "account", cmd[2]) && - set_setstr(&ic->set, "room", cmd[3])) { + set_setstr(&ic->set, "room", room)) { irc_rootmsg(irc, "Chatroom successfully added."); } else { if (ic) { @@ -1278,6 +1294,18 @@ static void cmd_chat(irc_t *irc, char **cmd) irc_rootmsg(irc, "Could not add chatroom."); } g_free(channel); + } else if (g_strcasecmp(cmd[1], "list") == 0) { + MIN_ARGS(2); + + if (!(acc = account_get(irc->b, cmd[2]))) { + irc_rootmsg(irc, "Invalid account"); + return; + } else if (!acc->prpl->chat_list) { + irc_rootmsg(irc, "Existing chatrooms not supported on that account."); + return; + } + + acc->prpl->chat_list(acc->ic, cmd[3]); } else if (g_strcasecmp(cmd[1], "with") == 0) { irc_user_t *iu; @@ -1292,8 +1320,7 @@ static void cmd_chat(irc_t *irc, char **cmd) } else { irc_rootmsg(irc, "Can't open a groupchat with %s.", cmd[2]); } - } else if (g_strcasecmp(cmd[1], "list") == 0 || - g_strcasecmp(cmd[1], "set") == 0 || + } else if (g_strcasecmp(cmd[1], "set") == 0 || g_strcasecmp(cmd[1], "del") == 0) { irc_rootmsg(irc, "Warning: The \002chat\002 command was mostly replaced with the \002channel\002 command."); @@ -1305,6 +1332,49 @@ static void cmd_chat(irc_t *irc, char **cmd) } } +void cmd_chat_list_finish(struct im_connection *ic) +{ + account_t *acc = ic->acc; + bee_chat_info_t *ci; + char *hformat, *iformat, *topic; + GSList *l; + GString *userc; + guint i = 0; + irc_t *irc = ic->bee->ui_data; + + if (ic->chatlist == NULL) { + irc_rootmsg(irc, "No existing chatrooms"); + return; + } + + if (strchr(irc->umode, 'b') != NULL) { + hformat = "%s\t%s\t%s\t%s"; + iformat = "%u\t%s\t%s\t%s"; + } else { + hformat = "%s %-20s %s %s"; + iformat = "%5u %-20.20s %5s %s"; + } + + irc_rootmsg(irc, hformat, "Index", "Title", "Users", "Topic"); + userc = g_string_new(NULL); + + for (l = ic->chatlist; l; l = l->next) { + ci = l->data; + topic = ci->topic ? ci->topic : ""; + + if (ci->userc >= 0) { + g_string_printf(userc, "%d", ci->userc); + } else { + g_string_assign(userc, "-"); + } + + irc_rootmsg(irc, iformat, ++i, ci->title, userc->str, topic); + } + + irc_rootmsg(irc, "%u %s chatrooms", i, acc->tag); + g_string_free(userc, TRUE); +} + static void cmd_group(irc_t *irc, char **cmd) { GSList *l; -- cgit v1.2.3