diff --git a/src/command/commands.c b/src/command/commands.c index 1a4e8c83..25442b80 100644 --- a/src/command/commands.c +++ b/src/command/commands.c @@ -2264,6 +2264,7 @@ cmd_statuses(gchar **args, struct cmd_help_t help) strcmp(args[1], "online") != 0 && strcmp(args[1], "none") != 0) { cons_show("Usage: %s", help.usage); + return TRUE; } } @@ -2271,8 +2272,36 @@ cmd_statuses(gchar **args, struct cmd_help_t help) if (strcmp(args[0], "muc") == 0) { if (strcmp(args[1], "on") != 0 && strcmp(args[1], "off") != 0) { cons_show("Usage: %s", help.usage); + return TRUE; } } + + if (strcmp(args[0], "console") == 0) { + prefs_set_string(PREF_STATUSES_CONSOLE, args[1]); + if (strcmp(args[1], "all") == 0) { + cons_show("All presence updates will appear in the console."); + } else if (strcmp(args[1], "online") == 0) { + cons_show("Only online/offline presence updates will appear in the console."); + } else { + cons_show("Presence updates will not appear in the console."); + } + } + + if (strcmp(args[0], "chat") == 0) { + prefs_set_string(PREF_STATUSES_CHAT, args[1]); + if (strcmp(args[1], "all") == 0) { + cons_show("All presence updates will appear in chat windows."); + } else if (strcmp(args[1], "online") == 0) { + cons_show("Only online/offline presence updates will appear in chat windows."); + } else { + cons_show("Presence updates will not appear in chat windows."); + } + } + + if (strcmp(args[0], "muc") == 0) { + _cmd_set_boolean_preference(args[1], help, + "Chat room presence updates", PREF_STATUSES_MUC); + } return TRUE; } diff --git a/src/config/preferences.c b/src/config/preferences.c index 2e7a67ce..7f7e8b2d 100644 --- a/src/config/preferences.c +++ b/src/config/preferences.c @@ -293,6 +293,9 @@ _get_group(preference_t pref) case PREF_HISTORY: case PREF_MOUSE: case PREF_STATUSES: + case PREF_STATUSES_CONSOLE: + case PREF_STATUSES_CHAT: + case PREF_STATUSES_MUC: case PREF_OTR_WARN: return "ui"; case PREF_STATES: @@ -343,6 +346,12 @@ _get_key(preference_t pref) return "mouse"; case PREF_STATUSES: return "statuses"; + case PREF_STATUSES_CONSOLE: + return "statuses.console"; + case PREF_STATUSES_CHAT: + return "statuses.chat"; + case PREF_STATUSES_MUC: + return "statuses.muc"; case PREF_STATES: return "enabled"; case PREF_OUTTYPE: diff --git a/src/config/preferences.h b/src/config/preferences.h index 20b3c271..cc9d63e1 100644 --- a/src/config/preferences.h +++ b/src/config/preferences.h @@ -46,6 +46,9 @@ typedef enum { PREF_HISTORY, PREF_MOUSE, PREF_STATUSES, + PREF_STATUSES_CONSOLE, + PREF_STATUSES_CHAT, + PREF_STATUSES_MUC, PREF_STATES, PREF_OUTTYPE, PREF_NOTIFY_TYPING, diff --git a/tests/test_cmd_statuses.c b/tests/test_cmd_statuses.c index 1cc7a277..d3797d9c 100644 --- a/tests/test_cmd_statuses.c +++ b/tests/test_cmd_statuses.c @@ -1,3 +1,7 @@ +#define _XOPEN_SOURCE 600 +#include +#include +#include #include #include #include @@ -6,11 +10,49 @@ #include #include +#include "config/preferences.h" + #include "ui/ui.h" #include "ui/mock_ui.h" #include "command/commands.h" +static int unlink_cb(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf) +{ + int rv = remove(fpath); + + if (rv) + perror(fpath); + + return rv; +} + +static int rmrf(char *path) +{ + return nftw(path, unlink_cb, 64, FTW_DEPTH | FTW_PHYS); +} + +void create_config_dir(void **state) +{ + setenv("XDG_CONFIG_HOME", "./tests/files/xdg_config_home", 1); + gchar *xdg_config = xdg_get_config_home(); + + GString *profanity_dir = g_string_new(xdg_config); + g_string_append(profanity_dir, "/profanity"); + + if (!mkdir_recursive(profanity_dir->str)) { + assert_true(FALSE); + } + g_string_free(profanity_dir, TRUE); + + g_free(xdg_config); +} + +void delete_config_dir(void **state) +{ + rmrf("./tests/files"); +} + void cmd_statuses_shows_usage_when_bad_subcmd(void **state) { mock_cons_show(); @@ -70,3 +112,178 @@ void cmd_statuses_shows_usage_when_bad_muc_setting(void **state) free(help); } + +void cmd_statuses_console_sets_all(void **state) +{ + mock_cons_show(); + CommandHelp *help = malloc(sizeof(CommandHelp)); + gchar *args[] = { "console", "all", NULL }; + + expect_cons_show("All presence updates will appear in the console."); + + prefs_load(); + gboolean result = cmd_statuses(args, *help); + prefs_close(); + prefs_load(); + + char *setting = prefs_get_string(PREF_STATUSES_CONSOLE); + assert_non_null(setting); + assert_string_equal("all", setting); + assert_true(result); + + prefs_close(); + free(help); +} + +void cmd_statuses_console_sets_online(void **state) +{ + mock_cons_show(); + CommandHelp *help = malloc(sizeof(CommandHelp)); + gchar *args[] = { "console", "online", NULL }; + + expect_cons_show("Only online/offline presence updates will appear in the console."); + + prefs_load(); + gboolean result = cmd_statuses(args, *help); + prefs_close(); + prefs_load(); + + char *setting = prefs_get_string(PREF_STATUSES_CONSOLE); + assert_non_null(setting); + assert_string_equal("online", setting); + assert_true(result); + + prefs_close(); + free(help); +} + +void cmd_statuses_console_sets_none(void **state) +{ + mock_cons_show(); + CommandHelp *help = malloc(sizeof(CommandHelp)); + gchar *args[] = { "console", "none", NULL }; + + expect_cons_show("Presence updates will not appear in the console."); + + prefs_load(); + gboolean result = cmd_statuses(args, *help); + prefs_close(); + prefs_load(); + + char *setting = prefs_get_string(PREF_STATUSES_CONSOLE); + assert_non_null(setting); + assert_string_equal("none", setting); + assert_true(result); + + prefs_close(); + free(help); +} + +void cmd_statuses_chat_sets_all(void **state) +{ + mock_cons_show(); + CommandHelp *help = malloc(sizeof(CommandHelp)); + gchar *args[] = { "chat", "all", NULL }; + + expect_cons_show("All presence updates will appear in chat windows."); + + prefs_load(); + gboolean result = cmd_statuses(args, *help); + prefs_close(); + prefs_load(); + + char *setting = prefs_get_string(PREF_STATUSES_CHAT); + assert_non_null(setting); + assert_string_equal("all", setting); + assert_true(result); + + prefs_close(); + free(help); +} + +void cmd_statuses_chat_sets_online(void **state) +{ + mock_cons_show(); + CommandHelp *help = malloc(sizeof(CommandHelp)); + gchar *args[] = { "chat", "online", NULL }; + + expect_cons_show("Only online/offline presence updates will appear in chat windows."); + + prefs_load(); + gboolean result = cmd_statuses(args, *help); + prefs_close(); + prefs_load(); + + char *setting = prefs_get_string(PREF_STATUSES_CHAT); + assert_non_null(setting); + assert_string_equal("online", setting); + assert_true(result); + + prefs_close(); + free(help); +} + +void cmd_statuses_chat_sets_none(void **state) +{ + mock_cons_show(); + CommandHelp *help = malloc(sizeof(CommandHelp)); + gchar *args[] = { "chat", "none", NULL }; + + expect_cons_show("Presence updates will not appear in chat windows."); + + prefs_load(); + gboolean result = cmd_statuses(args, *help); + prefs_close(); + prefs_load(); + + char *setting = prefs_get_string(PREF_STATUSES_CHAT); + assert_non_null(setting); + assert_string_equal("none", setting); + assert_true(result); + + prefs_close(); + free(help); +} + +void cmd_statuses_muc_sets_on(void **state) +{ + mock_cons_show(); + CommandHelp *help = malloc(sizeof(CommandHelp)); + gchar *args[] = { "muc", "on", NULL }; + + expect_cons_show("Chat room presence updates enabled."); + + prefs_load(); + gboolean result = cmd_statuses(args, *help); + prefs_close(); + prefs_load(); + + gboolean setting = prefs_get_boolean(PREF_STATUSES_MUC); + assert_non_null(setting); + assert_true(setting); + assert_true(result); + + prefs_close(); + free(help); +} + +void cmd_statuses_muc_sets_off(void **state) +{ + mock_cons_show(); + CommandHelp *help = malloc(sizeof(CommandHelp)); + gchar *args[] = { "muc", "off", NULL }; + + expect_cons_show("Chat room presence updates disabled."); + + prefs_load(); + gboolean result = cmd_statuses(args, *help); + prefs_close(); + prefs_load(); + + gboolean setting = prefs_get_boolean(PREF_STATUSES_MUC); + assert_false(setting); + assert_true(result); + + prefs_close(); + free(help); +} diff --git a/tests/test_cmd_statuses.h b/tests/test_cmd_statuses.h index 473bd212..2ad74c44 100644 --- a/tests/test_cmd_statuses.h +++ b/tests/test_cmd_statuses.h @@ -1,4 +1,15 @@ +void create_config_dir(void **state); +void delete_config_dir(void **state); + void cmd_statuses_shows_usage_when_bad_subcmd(void **state); void cmd_statuses_shows_usage_when_bad_console_setting(void **state); void cmd_statuses_shows_usage_when_bad_chat_setting(void **state); void cmd_statuses_shows_usage_when_bad_muc_setting(void **state); +void cmd_statuses_console_sets_all(void **state); +void cmd_statuses_console_sets_online(void **state); +void cmd_statuses_console_sets_none(void **state); +void cmd_statuses_chat_sets_all(void **state); +void cmd_statuses_chat_sets_online(void **state); +void cmd_statuses_chat_sets_none(void **state); +void cmd_statuses_muc_sets_on(void **state); +void cmd_statuses_muc_sets_off(void **state); diff --git a/tests/testsuite.c b/tests/testsuite.c index 1c91e17a..642f3e80 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -329,6 +329,30 @@ int main(int argc, char* argv[]) { unit_test(cmd_statuses_shows_usage_when_bad_console_setting), unit_test(cmd_statuses_shows_usage_when_bad_chat_setting), unit_test(cmd_statuses_shows_usage_when_bad_muc_setting), + unit_test_setup_teardown(cmd_statuses_console_sets_all, + create_config_dir, + delete_config_dir), + unit_test_setup_teardown(cmd_statuses_console_sets_online, + create_config_dir, + delete_config_dir), + unit_test_setup_teardown(cmd_statuses_console_sets_none, + create_config_dir, + delete_config_dir), + unit_test_setup_teardown(cmd_statuses_chat_sets_all, + create_config_dir, + delete_config_dir), + unit_test_setup_teardown(cmd_statuses_chat_sets_online, + create_config_dir, + delete_config_dir), + unit_test_setup_teardown(cmd_statuses_chat_sets_none, + create_config_dir, + delete_config_dir), + unit_test_setup_teardown(cmd_statuses_muc_sets_on, + create_config_dir, + delete_config_dir), + unit_test_setup_teardown(cmd_statuses_muc_sets_off, + create_config_dir, + delete_config_dir), }; int bak, new;