1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

Merge branch 'master' into plugins

This commit is contained in:
James Booth 2014-04-14 23:44:10 +01:00
commit 56a9ae6e53
7 changed files with 315 additions and 106 deletions

View File

@ -69,85 +69,37 @@ cmd_connect(gchar **args, struct cmd_help_t help)
cons_show("You are either connected already, or a login is in process.");
result = TRUE;
} else {
char *user = args[0];
char *opt1 = args[1];
char *opt1val = args[2];
char *opt2 = args[3];
char *opt2val = args[4];
char *lower = g_utf8_strdown(user, -1);
char *jid;
GList *opt_keys = NULL;
opt_keys = g_list_append(opt_keys, "server");
opt_keys = g_list_append(opt_keys, "port");
gboolean parsed;
GHashTable *options = parse_options(args, 1, opt_keys, &parsed);
if (!parsed) {
cons_show("Usage: %s", help.usage);
cons_show("");
return TRUE;
}
char *altdomain = g_hash_table_lookup(options, "server");
// parse options
char *altdomain = NULL;
int port = 0;
gboolean server_set = FALSE;
gboolean port_set = FALSE;
if (opt1 != NULL) {
if (opt1val == NULL) {
cons_show("Usage: %s", help.usage);
if (g_hash_table_contains(options, "port")) {
char *port_str = g_hash_table_lookup(options, "port");
if (_strtoi(port_str, &port, 1, 65535) != 0) {
port = 0;
cons_show("");
return TRUE;
}
if (strcmp(opt1, "server") == 0) {
altdomain = opt1val;
server_set = TRUE;
} else if (strcmp(opt1, "port") == 0) {
if (_strtoi(opt1val, &port, 1, 65535) != 0) {
port = 0;
cons_show("");
return TRUE;
} else {
port_set = TRUE;
}
} else {
cons_show("Usage: %s", help.usage);
cons_show("");
return TRUE;
}
if (opt2 != NULL) {
if (server_set && strcmp("server", opt2) == 0) {
cons_show("Usage: %s", help.usage);
cons_show("");
return TRUE;
}
if (port_set && strcmp("port", opt2) == 0) {
cons_show("Usage: %s", help.usage);
cons_show("");
return TRUE;
}
if (opt2val == NULL) {
cons_show("Usage: %s", help.usage);
cons_show("");
return TRUE;
}
if (strcmp(opt2, "server") == 0) {
if (server_set) {
cons_show("Usage: %s", help.usage);
return TRUE;
}
altdomain = opt2val;
server_set = TRUE;
} else if (strcmp(opt2, "port") == 0) {
if (port_set) {
cons_show("Usage: %s", help.usage);
return TRUE;
}
if (_strtoi(opt2val, &port, 1, 65535) != 0) {
port = 0;
cons_show("");
return TRUE;
} else {
port_set = TRUE;
}
} else {
cons_show("Usage: %s", help.usage);
cons_show("");
return TRUE;
}
}
}
options_destroy(options);
g_list_free(opt_keys);
char *user = args[0];
char *lower = g_utf8_strdown(user, -1);
char *jid;
ProfAccount *account = accounts_get_account(lower);
if (account != NULL) {
jid = account_create_full_jid(account);
@ -1603,7 +1555,6 @@ cmd_join(gchar **args, struct cmd_help_t help)
return TRUE;
}
int num_args = g_strv_length(args);
char *room = NULL;
char *nick = NULL;
char *passwd = NULL;
@ -1624,40 +1575,24 @@ cmd_join(gchar **args, struct cmd_help_t help)
}
// Additional args supplied
if (num_args > 1) {
char *opt1 = args[1];
char *opt1val = args[2];
char *opt2 = args[3];
char *opt2val = args[4];
if (opt1 != NULL) {
if (opt1val == NULL) {
cons_show("Usage: %s", help.usage);
cons_show("");
return TRUE;
}
if (strcmp(opt1, "nick") == 0) {
nick = opt1val;
} else if (strcmp(opt1, "password") == 0) {
passwd = opt1val;
} else {
cons_show("Usage: %s", help.usage);
cons_show("");
return TRUE;
}
if (opt2 != NULL) {
if (strcmp(opt2, "nick") == 0) {
nick = opt2val;
} else if (strcmp(opt2, "password") == 0) {
passwd = opt2val;
} else {
cons_show("Usage: %s", help.usage);
cons_show("");
return TRUE;
}
}
}
GList *opt_keys = NULL;
opt_keys = g_list_append(opt_keys, "nick");
opt_keys = g_list_append(opt_keys, "password");
gboolean parsed;
GHashTable *options = parse_options(args, 1, opt_keys, &parsed);
if (!parsed) {
cons_show("Usage: %s", help.usage);
cons_show("");
return TRUE;
}
nick = g_hash_table_lookup(options, "nick");
passwd = g_hash_table_lookup(options, "password");
options_destroy(options);
g_list_free(opt_keys);
// In the case that a nick wasn't provided by the optional args...
if (nick == NULL) {
nick = account->muc_nick;

View File

@ -372,3 +372,60 @@ get_start(char *string, int tokens)
return result_str;
}
GHashTable *
parse_options(gchar **args, int start, GList *keys, gboolean *res)
{
GHashTable *options = NULL;
// no options found, success
if (args[start] == NULL) {
options = g_hash_table_new(g_str_hash, g_str_equal);
*res = TRUE;
return options;
}
// validate options
int curr;
GList *found_keys = NULL;
for (curr = start; curr < g_strv_length(args); curr+= 2) {
// check if option valid
if (g_list_find_custom(keys, args[curr], (GCompareFunc)g_strcmp0) == NULL) {
*res = FALSE;
return options;
}
// check if duplicate
if (g_list_find_custom(found_keys, args[curr], (GCompareFunc)g_strcmp0) != NULL) {
*res = FALSE;
return options;
}
// check value given
if (args[curr+1] == NULL) {
*res = FALSE;
return options;
}
found_keys = g_list_append(found_keys, args[curr]);
}
g_list_free(found_keys);
// create map
options = g_hash_table_new(g_str_hash, g_str_equal);
*res = TRUE;
for (curr = start; curr < g_strv_length(args); curr+=2) {
g_hash_table_insert(options, args[curr], args[curr+1]);
}
return options;
}
void
options_destroy(GHashTable *options)
{
if (options != NULL) {
g_hash_table_destroy(options);
options = NULL;
}
}

View File

@ -29,5 +29,7 @@ gchar** parse_args(const char * const inp, int min, int max, gboolean *result);
gchar** parse_args_with_freetext(const char * const inp, int min, int max, gboolean *result);
int count_tokens(char *string);
char* get_start(char *string, int tokens);
GHashTable* parse_options(gchar **args, int start, GList *keys, gboolean *res);
void options_destroy(GHashTable *options);
#endif
#endif

View File

@ -1240,6 +1240,9 @@ _ui_room_join(char *room, gboolean focus)
ui_switch_win(num);
} else {
status_bar_active(num);
ProfWin *console = wins_get_console();
win_vprint_line(console, '!', COLOUR_ONLINE, "-> Autojoined %s (%d).", room, num);
win_update_virtual(console);
}
}

View File

@ -502,4 +502,198 @@ get_first_two_of_three_first_and_second_quoted(void **state)
char *result = get_start(inp, 3);
assert_string_equal("\"one\" \"two\" ", result);
}
void
parse_options_when_none_returns_empty_hasmap(void **state)
{
gchar *args[] = { "cmd1", "cmd2", NULL };
GList *keys = NULL;
keys = g_list_append(keys, "opt1");
gboolean res = FALSE;
GHashTable *options = parse_options(args, 2, keys, &res);
assert_true(options != NULL);
assert_int_equal(0, g_hash_table_size(options));
assert_true(res);
options_destroy(options);
g_list_free(keys);
}
void
parse_options_when_opt1_no_val_sets_error(void **state)
{
gchar *args[] = { "cmd1", "cmd2", "opt1", NULL };
GList *keys = NULL;
keys = g_list_append(keys, "opt1");
gboolean res = TRUE;
GHashTable *options = parse_options(args, 2, keys, &res);
assert_null(options);
assert_false(res);
options_destroy(options);
g_list_free(keys);
}
void
parse_options_when_one_returns_map(void **state)
{
gchar *args[] = { "cmd1", "cmd2", "opt1", "val1", NULL };
GList *keys = NULL;
keys = g_list_append(keys, "opt1");
gboolean res = FALSE;
GHashTable *options = parse_options(args, 2, keys, &res);
assert_int_equal(1, g_hash_table_size(options));
assert_true(g_hash_table_contains(options, "opt1"));
assert_string_equal("val1", g_hash_table_lookup(options, "opt1"));
assert_true(res);
options_destroy(options);
g_list_free(keys);
}
void
parse_options_when_opt2_no_val_sets_error(void **state)
{
gchar *args[] = { "cmd1", "cmd2", "opt1", "val1", "opt2", NULL };
GList *keys = NULL;
keys = g_list_append(keys, "opt1");
keys = g_list_append(keys, "opt2");
gboolean res = TRUE;
GHashTable *options = parse_options(args, 2, keys, &res);
assert_null(options);
assert_false(res);
options_destroy(options);
g_list_free(keys);
}
void
parse_options_when_two_returns_map(void **state)
{
gchar *args[] = { "cmd1", "cmd2", "opt1", "val1", "opt2", "val2", NULL };
GList *keys = NULL;
keys = g_list_append(keys, "opt1");
keys = g_list_append(keys, "opt2");
gboolean res = FALSE;
GHashTable *options = parse_options(args, 2, keys, &res);
assert_int_equal(2, g_hash_table_size(options));
assert_true(g_hash_table_contains(options, "opt1"));
assert_true(g_hash_table_contains(options, "opt2"));
assert_string_equal("val1", g_hash_table_lookup(options, "opt1"));
assert_string_equal("val2", g_hash_table_lookup(options, "opt2"));
assert_true(res);
options_destroy(options);
g_list_free(keys);
}
void
parse_options_when_opt3_no_val_sets_error(void **state)
{
gchar *args[] = { "cmd1", "cmd2", "opt1", "val1", "opt2", "val2", "opt3", NULL };
GList *keys = NULL;
keys = g_list_append(keys, "opt1");
keys = g_list_append(keys, "opt2");
keys = g_list_append(keys, "opt3");
gboolean res = TRUE;
GHashTable *options = parse_options(args, 2, keys, &res);
assert_null(options);
assert_false(res);
options_destroy(options);
g_list_free(keys);
}
void
parse_options_when_three_returns_map(void **state)
{
gchar *args[] = { "cmd1", "cmd2", "opt1", "val1", "opt2", "val2", "opt3", "val3", NULL };
GList *keys = NULL;
keys = g_list_append(keys, "opt1");
keys = g_list_append(keys, "opt2");
keys = g_list_append(keys, "opt3");
gboolean res = FALSE;
GHashTable *options = parse_options(args, 2, keys, &res);
assert_int_equal(3, g_hash_table_size(options));
assert_true(g_hash_table_contains(options, "opt1"));
assert_true(g_hash_table_contains(options, "opt2"));
assert_true(g_hash_table_contains(options, "opt3"));
assert_string_equal("val1", g_hash_table_lookup(options, "opt1"));
assert_string_equal("val2", g_hash_table_lookup(options, "opt2"));
assert_string_equal("val3", g_hash_table_lookup(options, "opt3"));
assert_true(res);
options_destroy(options);
g_list_free(keys);
}
void
parse_options_when_unknown_opt_sets_error(void **state)
{
gchar *args[] = { "cmd1", "cmd2", "opt1", "val1", "oops", "val2", "opt3", "val3", NULL };
GList *keys = NULL;
keys = g_list_append(keys, "opt1");
keys = g_list_append(keys, "opt2");
keys = g_list_append(keys, "opt3");
gboolean res = TRUE;
GHashTable *options = parse_options(args, 2, keys, &res);
assert_null(options);
assert_false(res);
options_destroy(options);
g_list_free(keys);
}
void
parse_options_with_duplicated_option_sets_error(void **state)
{
gchar *args[] = { "cmd1", "cmd2", "opt1", "val1", "opt2", "val2", "opt1", "val3", NULL };
GList *keys = NULL;
keys = g_list_append(keys, "opt1");
keys = g_list_append(keys, "opt2");
keys = g_list_append(keys, "opt3");
gboolean res = TRUE;
GHashTable *options = parse_options(args, 2, keys, &res);
assert_null(options);
assert_false(res);
options_destroy(options);
g_list_free(keys);
}

View File

@ -39,3 +39,12 @@ void get_first_two_of_three(void **state);
void get_first_two_of_three_first_quoted(void **state);
void get_first_two_of_three_second_quoted(void **state);
void get_first_two_of_three_first_and_second_quoted(void **state);
void parse_options_when_none_returns_empty_hasmap(void **state);
void parse_options_when_opt1_no_val_sets_error(void **state);
void parse_options_when_one_returns_map(void **state);
void parse_options_when_opt2_no_val_sets_error(void **state);
void parse_options_when_two_returns_map(void **state);
void parse_options_when_opt3_no_val_sets_error(void **state);
void parse_options_when_three_returns_map(void **state);
void parse_options_when_unknown_opt_sets_error(void **state);
void parse_options_with_duplicated_option_sets_error(void **state);

View File

@ -165,6 +165,15 @@ int main(int argc, char* argv[]) {
unit_test(get_first_two_of_three_first_quoted),
unit_test(get_first_two_of_three_second_quoted),
unit_test(get_first_two_of_three_first_and_second_quoted),
unit_test(parse_options_when_none_returns_empty_hasmap),
unit_test(parse_options_when_opt1_no_val_sets_error),
unit_test(parse_options_when_one_returns_map),
unit_test(parse_options_when_opt2_no_val_sets_error),
unit_test(parse_options_when_two_returns_map),
unit_test(parse_options_when_opt3_no_val_sets_error),
unit_test(parse_options_when_three_returns_map),
unit_test(parse_options_when_unknown_opt_sets_error),
unit_test(parse_options_with_duplicated_option_sets_error),
unit_test(empty_list_when_none_added),
unit_test(contains_one_element),