From 928134da7a7b9e2e045a74bd18b13e899cdebc2b Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Wed, 3 Jun 2015 04:32:59 -0400 Subject: [PATCH] implement banning/unbanning --- src/execute.c | 2 + src/group_commands.c | 95 +++++++++++++++++++++++++++++++++++++++----- src/group_commands.h | 2 + src/groupchat.c | 6 ++- src/help.c | 6 ++- 5 files changed, 96 insertions(+), 15 deletions(-) diff --git a/src/execute.c b/src/execute.c index e23a64f..146d7d2 100644 --- a/src/execute.c +++ b/src/execute.c @@ -85,6 +85,7 @@ static struct cmd_func chat_commands[] = { }; static struct cmd_func group_commands[] = { + { "/ban", cmd_ban }, { "/chatid", cmd_chatid }, { "/ignore", cmd_ignore }, { "/kick", cmd_kick }, @@ -95,6 +96,7 @@ static struct cmd_func group_commands[] = { { "/prune", cmd_prune }, { "/rejoin", cmd_rejoin }, { "/topic", cmd_set_topic }, + { "/unban", cmd_unban }, { "/unignore", cmd_unignore }, { "/unmod", cmd_unmod }, #ifdef AUDIO diff --git a/src/group_commands.c b/src/group_commands.c index 0c68e88..eb3f9b1 100644 --- a/src/group_commands.c +++ b/src/group_commands.c @@ -77,14 +77,8 @@ void cmd_ignore(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[ line_info_add(self, timefrmt, NULL, NULL, SYS_MSG, 1, BLUE, "-!- Ignoring %s", nick); } -void cmd_kick(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) +static void cmd_kickban_helper(ToxWindow *self, Tox *m, const char *nick, bool set_ban) { - if (argc < 1) { - line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Peer name must be specified."); - return; - } - - const char *nick = argv[1]; int peernumber = group_get_nick_peernumber(self->num, nick); if (peernumber == -1) { @@ -92,24 +86,103 @@ void cmd_kick(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MA return; } - int ret = tox_group_kick_peer(m, self->num, (uint32_t) peernumber); + const char *type_str = set_ban ? "ban" : "kick"; + int ret = tox_group_remove_peer(m, self->num, (uint32_t) peernumber, set_ban); switch (ret) { case 0: { - line_info_add(self, NULL, NULL, NULL, SYS_MSG, 1, RED, "You have kicked %s from the group.", nick); + type_str = set_ban ? "banned" : "kicked"; + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 1, RED, "You have %s %s from the group.", type_str, nick); return; } case -1: { - line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to kick %s from the group.", nick); + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to %s %s from the group.", type_str, nick); return; } case -2: { - line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "You do not have permission to kick %s.", nick); + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "You do not have permission to %s %s.", type_str, nick); return; } } } +void cmd_kick(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) +{ + if (argc < 1) { + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Peer name must be specified."); + return; + } + + cmd_kickban_helper(self, m, argv[1], false); +} + +void cmd_ban(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) +{ + if (argc < 1) { + int banlist_size = tox_group_get_ban_list_size(m, self->num); + + if (banlist_size == -1) { + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to get the ban list."); + return; + } + + if (banlist_size == 0) { + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Ban list is empty."); + return; + } + + struct Tox_Group_Ban *ban_list = malloc(banlist_size); + + if (ban_list == NULL) + return; + + int num_banned = tox_group_get_ban_list(m, self->num, ban_list); + + if (num_banned == -1) { + free(ban_list); + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to get the ban list."); + return; + } + + uint16_t i; + + for (i = 0; i < num_banned; ++i) { + struct tm tm_set = *localtime((const time_t *) &ban_list[i].time_set); + char time_str[64]; + strftime(time_str, sizeof(time_str), "%c", &tm_set); + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "[ID: %d] %s : %s : %s", ban_list[i].id, + ban_list[i].ip_address, ban_list[i].nick, time_str); + } + + free(ban_list); + return; + } + + cmd_kickban_helper(self, m, argv[1], true); +} + +void cmd_unban(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) +{ + if (argc < 1) { + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Ban ID must be specified."); + return; + } + + int ban_id = atoi(argv[1]); + + if (ban_id == 0 && strcmp(argv[1], "0")) { + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Ban ID must be a non-negative interger."); + return; + } + + if (tox_group_remove_ban_entry(m, self->num, ban_id) == -1) { + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Ban ID does not exist."); + return; + } + + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Ban list entry with id %d has been removed.", ban_id); +} + void cmd_mod(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { if (argc < 1) { diff --git a/src/group_commands.h b/src/group_commands.h index 9276e90..3dcc41e 100644 --- a/src/group_commands.h +++ b/src/group_commands.h @@ -26,6 +26,7 @@ #include "windows.h" #include "toxic.h" +void cmd_ban(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]); void cmd_chatid(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]); void cmd_ignore(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]); void cmd_kick(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]); @@ -36,6 +37,7 @@ void cmd_set_peerlimit(WINDOW *window, ToxWindow *self, Tox *m, int argc, char ( void cmd_set_privacy(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]); void cmd_rejoin(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]); void cmd_set_topic(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_unban(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]); void cmd_unignore(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]); void cmd_unmod(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]); diff --git a/src/groupchat.c b/src/groupchat.c index 7edb62c..a926081 100644 --- a/src/groupchat.c +++ b/src/groupchat.c @@ -70,9 +70,9 @@ extern struct user_settings *user_settings; extern struct Winthread Winthread; #ifdef AUDIO -#define AC_NUM_GROUP_COMMANDS 34 +#define AC_NUM_GROUP_COMMANDS 36 #else -#define AC_NUM_GROUP_COMMANDS 30 +#define AC_NUM_GROUP_COMMANDS 32 #endif /* AUDIO */ /* groupchat command names used for tab completion. */ @@ -80,6 +80,7 @@ static const char group_cmd_list[AC_NUM_GROUP_COMMANDS][MAX_CMDNAME_SIZE] = { { "/accept" }, { "/add" }, { "/avatar" }, + { "/ban" }, { "/chatid" }, { "/clear" }, { "/close" }, @@ -104,6 +105,7 @@ static const char group_cmd_list[AC_NUM_GROUP_COMMANDS][MAX_CMDNAME_SIZE] = { { "/requests" }, { "/status" }, { "/topic" }, + { "/unban" }, { "/unignore" }, { "/unmod" }, { "/whisper" }, diff --git a/src/help.c b/src/help.c index 21912ab..7bee632 100644 --- a/src/help.c +++ b/src/help.c @@ -245,13 +245,15 @@ static void help_draw_group(ToxWindow *self) wprintw(win, " /chatid : Print the group chat id to share with others.\n"); wprintw(win, " /ignore : Ignore peer\n"); + wprintw(win, " /unignore : Unignore peer \n"); wprintw(win, " /kick : Kick peer\n"); + wprintw(win, " /ban : Ban peer (leave nick blank to see ban list)\n"); + wprintw(win, " /unban : Unban entry\n"); wprintw(win, " /passwd : Set group password (leave blank to unset)\n"); wprintw(win, " /peerlimit : Set group peer limit\n"); wprintw(win, " /privacy : Set group privacy state: private|public\n"); wprintw(win, " /rejoin : Rejoin the group\n"); wprintw(win, " /topic : Set group topic (show current topic if no msg)\n"); - wprintw(win, " /unignore : Unignore peer \n"); wprintw(win, " /whisper : Send private message to nick\n\n"); #ifdef AUDIO @@ -311,7 +313,7 @@ void help_onKey(ToxWindow *self, wint_t key) break; case 'r': - help_init_window(self, 15, 80); + help_init_window(self, 17, 80); self->help->type = HELP_GROUP; break;