Refactored bookmarks to use option parser, allow bookmarking rooms with passwords

This commit is contained in:
James Booth
2014-05-10 00:50:43 +01:00
parent a519d25e4b
commit d2662a6f17
12 changed files with 243 additions and 288 deletions

View File

@@ -300,12 +300,19 @@ static struct cmd_t command_defs[] =
NULL } } },
{ "/bookmark",
cmd_bookmark, parse_args, 0, 4, NULL,
{ "/bookmark [add|list|remove] [room@server] [autojoin] [nick]",
"Manage bookmarks.",
{ "/bookmark [add|list|remove] [room@server] [autojoin] [nick]",
cmd_bookmark, parse_args, 1, 8, NULL,
{ "/bookmark list|add|update|remove|join [room@server] [nick value] [password value] [autojoin on|off]", "Manage bookmarks.",
{ "/bookmark list|add|update|remove|join [room@server] [nick value] [password value] [autojoin on|off]",
"-----------------------------------------------------------",
"Manage bookmarks.",
"list: List all bookmarks.",
"add: Add a bookmark for room@server with the following optional properties:",
" nick: Nickname used in the chat room",
" password: Password for private rooms, note this may be stored in plaintext on your server",
" autojoin: Whether to join the room automatically on login \"on\" or \"off\".",
"update: Update any of the above properties associated with the bookmark.",
"remove: Remove the bookmark for room@server.",
"join: Join room@server using the properties associated with the bookmark.",
NULL } } },
{ "/disco",
@@ -1057,9 +1064,11 @@ cmd_init(void)
autocomplete_add(who_ac, "any");
bookmark_ac = autocomplete_new();
autocomplete_add(bookmark_ac, "add");
autocomplete_add(bookmark_ac, "list");
autocomplete_add(bookmark_ac, "add");
autocomplete_add(bookmark_ac, "update");
autocomplete_add(bookmark_ac, "remove");
autocomplete_add(bookmark_ac, "join");
otr_ac = autocomplete_new();
autocomplete_add(otr_ac, "gen");
@@ -1651,11 +1660,15 @@ _bookmark_autocomplete(char *input, int *size)
return result;
}
result = autocomplete_param_with_func(input, size, "/bookmark list", bookmark_find);
result = autocomplete_param_with_func(input, size, "/bookmark remove", bookmark_find);
if (result != NULL) {
return result;
}
result = autocomplete_param_with_func(input, size, "/bookmark remove", bookmark_find);
result = autocomplete_param_with_func(input, size, "/bookmark join", bookmark_find);
if (result != NULL) {
return result;
}
result = autocomplete_param_with_func(input, size, "/bookmark update", bookmark_find);
if (result != NULL) {
return result;
}

View File

@@ -1734,77 +1734,76 @@ cmd_bookmark(gchar **args, struct cmd_help_t help)
return TRUE;
}
/* TODO: /bookmark list room@server */
if (strcmp(cmd, "list") == 0) {
const GList *bookmarks = bookmark_get_list();
cons_show_bookmarks(bookmarks);
} else {
gboolean autojoin = FALSE;
gchar *jid = NULL;
gchar *nick = NULL;
int idx = 1;
while (args[idx] != NULL) {
gchar *opt = args[idx];
if (strcmp(opt, "autojoin") == 0) {
autojoin = TRUE;
} else if (jid == NULL) {
jid = opt;
} else if (nick == NULL) {
nick = opt;
} else {
cons_show("Usage: %s", help.usage);
}
++idx;
char *jid = args[1];
if (jid == NULL) {
cons_show("Usage: %s", help.usage);
cons_show("");
return TRUE;
}
if (jid == NULL) {
win_type_t win_type = ui_current_win_type();
if (win_type == WIN_MUC) {
jid = ui_current_recipient();
nick = muc_get_room_nick(jid);
if (strcmp(cmd, "remove") == 0) {
gboolean removed = bookmark_remove(jid);
if (removed) {
cons_show("Bookmark removed for %s.", jid);
} else {
cons_show("No bookmark exists for %s.", jid);
}
return TRUE;
}
if (strcmp(cmd, "join") == 0) {
gboolean joined = bookmark_join(jid);
if (!joined) {
cons_show("No bookmark exists for %s.", jid);
}
return TRUE;
}
gchar *opt_keys[] = { "autojoin", "nick", "password", NULL };
gboolean parsed;
GHashTable *options = parse_options(&args[2], opt_keys, &parsed);
if (!parsed) {
cons_show("Usage: %s", help.usage);
cons_show("");
return TRUE;
}
char *nick = g_hash_table_lookup(options, "nick");
char *password = g_hash_table_lookup(options, "password");
char *autojoin = g_hash_table_lookup(options, "autojoin");
if (autojoin != NULL) {
if ((strcmp(autojoin, "on") != 0) && (strcmp(autojoin, "off") != 0)) {
cons_show("Usage: %s", help.usage);
cons_show("");
return TRUE;
}
}
if (strcmp(cmd, "add") == 0) {
gboolean added = bookmark_add(jid, nick, autojoin);
gboolean added = bookmark_add(jid, nick, password, autojoin);
if (added) {
GString *msg = g_string_new("Bookmark added for ");
g_string_append(msg, jid);
if (nick != NULL) {
g_string_append(msg, ", nickname: ");
g_string_append(msg, nick);
}
if (autojoin) {
g_string_append(msg, ", autojoin enabled");
}
g_string_append(msg, ".");
cons_show(msg->str);
g_string_free(msg, TRUE);
cons_show("Bookmark added for %s.", jid);
} else {
cons_show("Bookmark updated for %s.", jid);
cons_show("Bookmark already exists, use /bookmark update to edit.");
}
} else if (strcmp(cmd, "remove") == 0) {
gboolean removed = bookmark_remove(jid, autojoin);
if (removed) {
if (autojoin) {
cons_show("Autojoin disabled for %s.", jid);
} else {
cons_show("Bookmark removed for %s.", jid);
}
} else if (strcmp(cmd, "update") == 0) {
gboolean updated = bookmark_update(jid, nick, password, autojoin);
if (updated) {
cons_show("Bookmark updated.");
} else {
cons_show("No bookmark exists for %s.", jid);
}
} else {
cons_show("Usage: %s", help.usage);
}
options_destroy(options);
}
return TRUE;