From e9f8293a0a2c2710c1ba7151455003dd32de77c9 Mon Sep 17 00:00:00 2001 From: jfreegman Date: Tue, 16 Nov 2021 11:47:51 -0500 Subject: [PATCH] Add topic lock command and callbacks --- src/execute.c | 1 + src/groupchat_commands.c | 50 ++++++++++++++++++++++++++++++++++++++++ src/groupchat_commands.h | 1 + src/groupchats.c | 19 +++++++++++++++ src/help.c | 3 ++- src/toxic.c | 1 + src/toxic.h | 1 + src/windows.c | 9 ++++++++ src/windows.h | 1 + 9 files changed, 85 insertions(+), 1 deletion(-) diff --git a/src/execute.c b/src/execute.c index e8036f7..b24dfef 100644 --- a/src/execute.c +++ b/src/execute.c @@ -128,6 +128,7 @@ static struct cmd_func groupchat_commands[] = { { "/ignore", cmd_ignore }, { "/kick", cmd_kick }, { "/list", cmd_list }, + { "/locktopic", cmd_set_topic_lock }, { "/mod", cmd_mod }, { "/passwd", cmd_set_passwd }, { "/peerlimit", cmd_set_peerlimit }, diff --git a/src/groupchat_commands.c b/src/groupchat_commands.c index 78451c5..64a4030 100644 --- a/src/groupchat_commands.c +++ b/src/groupchat_commands.c @@ -444,6 +444,56 @@ void cmd_set_privacy(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*a } } +void cmd_set_topic_lock(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) +{ + TOX_GROUP_TOPIC_LOCK topic_lock; + const char *tlock_str = NULL; + + if (argc < 1) { + TOX_ERR_GROUP_STATE_QUERIES err; + topic_lock = tox_group_get_topic_lock(m, self->num, &err); + + if (err != TOX_ERR_GROUP_STATE_QUERIES_OK) { + line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to retrieve topic lock (error %d).", err); + return; + } + + tlock_str = topic_lock == TOX_GROUP_TOPIC_LOCK_ENABLED ? "Enabled" : "Disabled"; + line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Topic lock is %s.", tlock_str); + return; + } + + tlock_str = argv[1]; + + if (strcasecmp(tlock_str, "on") != 0 && strcasecmp(tlock_str, "off") != 0) { + line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Topic lock must be \"on\" or \"off\"."); + return; + } + + topic_lock = strcasecmp(tlock_str, "on") == 0 ? TOX_GROUP_TOPIC_LOCK_ENABLED : TOX_GROUP_TOPIC_LOCK_DISABLED; + const char *display_str = (topic_lock == TOX_GROUP_TOPIC_LOCK_ENABLED) ? "enabled" : "disabled"; + + TOX_ERR_GROUP_FOUNDER_SET_TOPIC_LOCK err; + tox_group_founder_set_topic_lock(m, self->num, topic_lock, &err); + + switch (err) { + case TOX_ERR_GROUP_FOUNDER_SET_TOPIC_LOCK_OK: { + line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Topic lock has been %s.", display_str); + return; + } + + case TOX_ERR_GROUP_FOUNDER_SET_TOPIC_LOCK_PERMISSIONS: { + line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "You do not have permission to set the topic lock."); + return; + } + + default: { + line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Error setting topic lock (error %d).", err); + return; + } + } +} + void cmd_silence(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { if (argc < 1) { diff --git a/src/groupchat_commands.h b/src/groupchat_commands.h index 8a35875..8c6cc42 100644 --- a/src/groupchat_commands.h +++ b/src/groupchat_commands.h @@ -36,6 +36,7 @@ void cmd_prune(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[M void cmd_set_passwd(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]); void cmd_set_peerlimit(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]); void cmd_set_privacy(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_set_topic_lock(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_silence(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]); diff --git a/src/groupchats.c b/src/groupchats.c index 3193cb6..e807b5d 100644 --- a/src/groupchats.c +++ b/src/groupchats.c @@ -89,6 +89,7 @@ static const char *group_cmd_list[] = { "/join", "/kick", "/list", + "/locktopic", "/log", "/mod", "/myid", @@ -790,6 +791,23 @@ static void groupchat_onGroupPrivacyState(ToxWindow *self, Tox *m, uint32_t grou write_to_log(tmp_event, "The founder", ctx->log, true); } +static void groupchat_onGroupTopicLock(ToxWindow *self, Tox *m, uint32_t groupnumber, TOX_GROUP_TOPIC_LOCK topic_lock) +{ + ChatContext *ctx = self->chatwin; + + if (self->num != groupnumber || !get_groupchat(groupnumber)) { + return; + } + + const char *tlock_str = topic_lock == TOX_GROUP_TOPIC_LOCK_ENABLED ? "locked" : "unlocked"; + + line_info_add(self, true, NULL, NULL, SYS_MSG, 1, BLUE, "-!- The group founder has %s the topic.", tlock_str); + + char tmp_event[MAX_STR_SIZE]; + snprintf(tmp_event, sizeof(tmp_event), " %s the topic.", tlock_str); + write_to_log(tmp_event, "The founder", ctx->log, true); +} + static void groupchat_onGroupPassword(ToxWindow *self, Tox *m, uint32_t groupnumber, const char *password, size_t length) { @@ -1662,6 +1680,7 @@ static ToxWindow *new_group_chat(Tox *m, uint32_t groupnumber, const char *group ret->onGroupTopicChange = &groupchat_onGroupTopicChange; ret->onGroupPeerLimit = &groupchat_onGroupPeerLimit; ret->onGroupPrivacyState = &groupchat_onGroupPrivacyState; + ret->onGroupTopicLock = &groupchat_onGroupTopicLock; ret->onGroupPassword = &groupchat_onGroupPassword; ret->onGroupNickChange = &groupchat_onGroupNickChange; ret->onGroupStatusChange = &groupchat_onGroupStatusChange; diff --git a/src/help.c b/src/help.c index f4b8843..cd80448 100644 --- a/src/help.c +++ b/src/help.c @@ -303,6 +303,7 @@ static void help_draw_groupchats(ToxWindow *self) wprintw(win, " /unignore : Unignore an ignored peer\n"); wprintw(win, " /kick : Remove a peer from the group\n"); wprintw(win, " /list : Print a list of peers currently in the group\n"); + wprintw(win, " /locktopic : Set the topic lock: on | off\n"); wprintw(win, " /mod : Promote a peer to moderator\n"); wprintw(win, " /passwd : Set a password needed to join the group\n"); wprintw(win, " /peerlimit : Set the maximum number of peers that can join\n"); @@ -489,7 +490,7 @@ void help_onKey(ToxWindow *self, wint_t key) break; case L'r': - help_init_window(self, 24, 80); + help_init_window(self, 25, 80); self->help->type = HELP_GROUP; break; } diff --git a/src/toxic.c b/src/toxic.c index 3bba1a3..51761c9 100644 --- a/src/toxic.c +++ b/src/toxic.c @@ -837,6 +837,7 @@ static void init_tox_callbacks(Tox *m) tox_callback_group_topic(m, on_group_topic_change); tox_callback_group_peer_limit(m, on_group_peer_limit); tox_callback_group_privacy_state(m, on_group_privacy_state); + tox_callback_group_topic_lock(m, on_group_topic_lock); tox_callback_group_password(m, on_group_password); tox_callback_group_self_join(m, on_group_self_join); tox_callback_group_join_fail(m, on_group_rejected); diff --git a/src/toxic.h b/src/toxic.h index de79aa1..4e678cf 100644 --- a/src/toxic.h +++ b/src/toxic.h @@ -164,6 +164,7 @@ void on_group_topic_change(Tox *m, uint32_t groupnumber, uint32_t peernumber, co void *userdata); void on_group_peer_limit(Tox *m, uint32_t groupnumber, uint32_t peer_limit, void *userdata); void on_group_privacy_state(Tox *m, uint32_t groupnumber, TOX_GROUP_PRIVACY_STATE privacy_state, void *userdata); +void on_group_topic_lock(Tox *m, uint32_t groupnumber, TOX_GROUP_TOPIC_LOCK topic_lock, void *userdata); void on_group_password(Tox *m, uint32_t groupnumber, const uint8_t *password, size_t length, void *userdata); void on_group_nick_change(Tox *m, uint32_t groupnumber, uint32_t peernumber, const uint8_t *newname, size_t length, void *userdata); diff --git a/src/windows.c b/src/windows.c index 4c004e1..b3d8ea8 100644 --- a/src/windows.c +++ b/src/windows.c @@ -491,6 +491,15 @@ void on_group_privacy_state(Tox *m, uint32_t groupnumber, TOX_GROUP_PRIVACY_STAT } } +void on_group_topic_lock(Tox *m, uint32_t groupnumber, TOX_GROUP_TOPIC_LOCK topic_lock, void *userdata) +{ + for (size_t i = 0; i < MAX_WINDOWS_NUM; ++i) { + if (windows[i] != NULL && windows[i]->onGroupTopicLock != NULL) { + windows[i]->onGroupTopicLock(windows[i], m, groupnumber, topic_lock); + } + } +} + void on_group_password(Tox *m, uint32_t groupnumber, const uint8_t *password, size_t length, void *userdata) { for (size_t i = 0; i < MAX_WINDOWS_NUM; ++i) { diff --git a/src/windows.h b/src/windows.h index da43c36..75e838d 100644 --- a/src/windows.h +++ b/src/windows.h @@ -204,6 +204,7 @@ struct ToxWindow { void(*onGroupTopicChange)(ToxWindow *, Tox *, uint32_t, uint32_t, const char *, size_t); void(*onGroupPeerLimit)(ToxWindow *, Tox *, uint32_t, uint32_t); void(*onGroupPrivacyState)(ToxWindow *, Tox *, uint32_t, TOX_GROUP_PRIVACY_STATE); + void(*onGroupTopicLock)(ToxWindow *, Tox *, uint32_t, TOX_GROUP_TOPIC_LOCK); void(*onGroupPassword)(ToxWindow *, Tox *, uint32_t, const char *, size_t); void(*onGroupSelfJoin)(ToxWindow *, Tox *, uint32_t); void(*onGroupRejected)(ToxWindow *, Tox *, uint32_t, TOX_GROUP_JOIN_FAIL);