about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2014-04-14 23:15:39 +0100
committerJames Booth <boothj5@gmail.com>2014-04-14 23:15:39 +0100
commit428d9eb936db2436ae72f2bf18afba635b15cbeb (patch)
tree0789d7158e68df3b67ce80cd4a630b9c0d18d865
parent241973700697abb60104c656a0aacfd182eb2316 (diff)
downloadprofani-tty-428d9eb936db2436ae72f2bf18afba635b15cbeb.tar.gz
Refactored cmd_connect to use parse_options
-rw-r--r--src/command/commands.c95
1 files changed, 23 insertions, 72 deletions
diff --git a/src/command/commands.c b/src/command/commands.c
index 0fc8574b..c03d294d 100644
--- a/src/command/commands.c
+++ b/src/command/commands.c
@@ -68,85 +68,36 @@ 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);
-                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);
+        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 (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);
+
+        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);