src/nexus.c
changeset 98 f357f835f0d5
parent 93 42ade8285570
child 100 cfb7776bd6f0
--- a/src/nexus.c	Tue Mar 31 20:57:07 2009 +0300
+++ b/src/nexus.c	Tue Mar 31 22:09:53 2009 +0300
@@ -18,6 +18,7 @@
  */
 enum option_code {
     OPT_HELP            = 'h',
+    OPT_DEFAULTS        = 'D',
     OPT_NETWORK         = 'n',
     OPT_CHANNEL         = 'c',
     OPT_MODULE          = 'm',
@@ -35,6 +36,7 @@
  */
 static struct option options[] = {
     {"help",            0,  NULL,   OPT_HELP        },
+    {"defaults",        1,  NULL,   OPT_DEFAULTS    },
     {"network",         1,  NULL,   OPT_NETWORK     },
     {"channel",         1,  NULL,   OPT_CHANNEL     },
     {"module",          1,  NULL,   OPT_MODULE      },
@@ -54,6 +56,7 @@
     printf("Usage: %s [OPTIONS]\n", exe);
     printf("\n");
     printf(" --help / -h            display this message\n");
+    printf(" --defaults / -D        set the IRC client default info using '<nickname>:<username>:<realname>'\n");
     printf(" --network / -n         add an IRC network using '<name>:<hostname>[:<port>[:ssl]]' format\n");
     printf(" --channel / -c         add an IRC channel using '<network>:<channel>' format\n");
     printf(" --module / -m          add a module using '<name>:<path>' format\n");
@@ -87,21 +90,49 @@
 }
 
 /**
+ * Parse and apply a --defaults option, setting the resulting defaults to our irc_client
+ */
+static err_t apply_defaults (struct nexus *nexus, char *opt, struct error_info *err)
+{
+    struct irc_client_defaults defaults = {
+        .register_info      = { NULL, NULL, NULL },
+        .service            = DEFAULT_PORT,
+        .service_ssl        = DEFAULT_PORT_SSL,
+    };
+
+    // parse the required fields
+    if ((defaults.register_info.nickname = strsep(&opt, ":")) == NULL)
+        RETURN_SET_ERROR_STR(err, ERR_CMD_OPT, "missing <nickname> field for --defaults");
+    
+    if ((defaults.register_info.username = strsep(&opt, ":")) == NULL)
+        RETURN_SET_ERROR_STR(err, ERR_CMD_OPT, "missing <username> field for --defaults");
+    
+    if ((defaults.register_info.realname = strsep(&opt, ":")) == NULL)
+        RETURN_SET_ERROR_STR(err, ERR_CMD_OPT, "missing <realname> field for --defaults");
+
+    // trailing garbage?
+    if (opt)
+        RETURN_SET_ERROR_STR(err, ERR_CMD_OPT, "trailing values for --channel");
+    
+    // apply them
+    log_info("using default nick/user/real-name: %s/%s/%s", 
+            defaults.register_info.nickname, defaults.register_info.username, defaults.register_info.realname);
+
+    irc_client_set_defaults(nexus->client, &defaults);
+
+    // ok
+    return SET_ERROR(err, SUCCESS);
+}
+
+/**
  * Parse and apply a --network option, adding the given network to our irc_client.
  */
 static err_t apply_network (struct nexus *nexus, char *opt, struct error_info *err)
 {
-    struct irc_net_info info = {
-        .network            = NULL,
-        .hostname           = NULL,
-        .service            = DEFAULT_PORT,
-        .use_ssl            = false,
-        .register_info      = {
-            .nickname       = "SpBotDev",
-            .username       = "spbot-dev",
-            .realname       = "SpBot (development version)"
-        }
-    };
+    struct irc_net_info info;
+
+    // init to zero
+    memset(&info, 0, sizeof(info));
 
     // parse the required fields
     if ((info.network = strsep(&opt, ":")) == NULL)
@@ -280,6 +311,12 @@
                     return ERROR_CODE(err);
 
                 break;
+            
+            case OPT_DEFAULTS:
+                if (apply_defaults(nexus, optarg, err))
+                    return ERROR_CODE(err);
+
+                break;
 
             case OPT_CHANNEL:
                 if (apply_channel(nexus, optarg, err))