implement SSL stuff for lua_client_connect
authorTero Marttila <terom@fixme.fi>
Sun, 19 Apr 2009 04:35:29 +0300
changeset 141 0b850238c588
parent 140 aa390e52eda8
child 142 dc2bb09d412c
implement SSL stuff for lua_client_connect
src/lua_irc.c
src/lua_objs.c
src/lua_objs.h
--- a/src/lua_irc.c	Sun Apr 19 04:04:42 2009 +0300
+++ b/src/lua_irc.c	Sun Apr 19 04:35:29 2009 +0300
@@ -151,15 +151,14 @@
 {
     struct lua_client *lua_client = lua_obj_get_obj(L, __func__, "evirc.client");
 
-    // read the args
-    // XXX: need to copy these, really
-    lua_client->defaults.register_info.nickname = luaL_checkstring(L, 2);
-    lua_client->defaults.register_info.username = luaL_checkstring(L, 3);
-    lua_client->defaults.register_info.realname = luaL_checkstring(L, 4);
+    // required args
+    lua_client->defaults.register_info.nickname = lua_arg_string(L, 2, "nickname", LUA_ARG_STRING_REQUIRED);
+    lua_client->defaults.register_info.username = lua_arg_string(L, 3, "username", LUA_ARG_STRING_REQUIRED);
+    lua_client->defaults.register_info.realname = lua_arg_string(L, 4, "realname", LUA_ARG_STRING_REQUIRED);
 
-    // set the non-args
-    lua_client->defaults.service = IRC_PORT;
-    lua_client->defaults.service_ssl = IRC_SSL_PORT;
+    // optional args
+    lua_client->defaults.service        = lua_arg_string(L, 5, "service", IRC_PORT);
+    lua_client->defaults.service_ssl    = lua_arg_string(L, 6, "service_ssl", IRC_SSL_PORT);
 
     // invoke
     irc_client_set_defaults(lua_client->client, &lua_client->defaults);
@@ -172,20 +171,40 @@
 {
     struct lua_client *lua_client = lua_obj_get_obj(L, __func__, "evirc.client");
     struct irc_net_info net_info;
+    const char *ssl_cafile = NULL, *ssl_cert = NULL, *ssl_pkey = NULL;
+    bool use_ssl = false, ssl_verify = false;
     struct irc_net *net;
     struct error_info err;
 
     // init net_info
     memset(&net_info, 0, sizeof(net_info));
 
-    // the network name
-    net_info.network = luaL_checkstring(L, 2);
-    
-    // the hostname
-    net_info.hostname = luaL_checkstring(L, 3);
+    // required args
+    net_info.network    = lua_arg_string(L, 2, "network",       LUA_ARG_STRING_REQUIRED);
+    net_info.hostname   = lua_arg_string(L, 3, "hostname",      LUA_ARG_STRING_REQUIRED);
 
-    // service remains default
-    net_info.service = "6667";
+    // optional args
+    net_info.service    = lua_arg_string(L, 4, "service",       NULL);
+
+    // ssl stuff
+    use_ssl             = lua_arg_bool  (L, 5, "use_ssl",       false);
+    ssl_cafile          = lua_arg_string(L, 6, "ssl_cafile",    NULL);
+    ssl_verify          = lua_arg_bool  (L, 7, "ssl_verify",    false);
+    ssl_cert            = lua_arg_string(L, 8, "ssl_cert",      NULL);
+    ssl_pkey            = lua_arg_string(L, 9, "ssl_pkey",      NULL);
+
+    // SSL?
+    if (use_ssl || ssl_cafile || ssl_verify || ssl_cert || ssl_pkey) {
+        // verify
+        if ((ssl_cert || ssl_pkey) && !(ssl_cert && ssl_pkey))
+            return luaL_error(L, "must give both ssl_cert and ssl_pkey");
+        
+        // create
+        if (sock_ssl_client_cred_create(&net_info.ssl_cred, ssl_cafile, ssl_verify, ssl_cert, ssl_pkey, &err))
+            return luaL_error(L, "sock_ssl_client_cred_create(ssl_cafile=%s, ssl_verify=%b, ssl_cert=%s, ssl_pkey=%s): %s", 
+                    ssl_cafile, ssl_verify, ssl_cert, ssl_pkey, error_msg(&err)
+                );
+    }
 
     // create it
     if (irc_client_add_net(lua_client->client, &net, &net_info, &err))
--- a/src/lua_objs.c	Sun Apr 19 04:04:42 2009 +0300
+++ b/src/lua_objs.c	Sun Apr 19 04:35:29 2009 +0300
@@ -66,6 +66,39 @@
     }
 }
 
+const char *lua_arg_string (lua_State *L, int index, const char *name, const char *def)
+{
+    const char *value;
+ 
+    // use default?
+    if (lua_isnoneornil(L, index) && def != (const char *) LUA_ARG_REQUIRED)
+        return def;
+    
+    // value given?
+    if ((value = lua_tostring(L, index)))
+        return value;
+   
+    // error
+    luaL_error(L, "missing value for required string argument <%d:%s>", index, name);
+}
+
+bool lua_arg_bool (lua_State *L, int index, const char *name, int def)
+{
+    bool value;
+ 
+    // use default?
+    if (lua_isnoneornil(L, index) && def != LUA_ARG_REQUIRED)
+        return def;
+   
+    // value given?
+    value = lua_toboolean(L, index);
+
+    return value;
+    
+    // error
+    // luaL_error(L, "missing value of required boolean argument <%d:%s>", index, name);
+}
+
 /**
  * Wrapper for module
  */
--- a/src/lua_objs.h	Sun Apr 19 04:04:42 2009 +0300
+++ b/src/lua_objs.h	Sun Apr 19 04:35:29 2009 +0300
@@ -32,6 +32,22 @@
 void* lua_obj_get_obj (lua_State *L, const char *func, const char *name);
 
 /**
+ * Used as the "invalid" default value
+ */
+#define LUA_ARG_REQUIRED (-1)
+#define LUA_ARG_STRING_REQUIRED ((const char *) (-1))
+
+/**
+ * Parse and return a string argument
+ */
+const char *lua_arg_string (lua_State *L, int index, const char *name, const char *def);
+
+/**
+ * Parse and return a boolean argument
+ */
+bool lua_arg_bool (lua_State *L, int index, const char *name, int def);
+
+/**
  * Registers our lua runtime objects into the given lua state.
  *
  * Call in protected mode.