lm_proxy.c
changeset 0 84fdfb0344c9
child 4 5770be2d5f3f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lm_proxy.c	Sun Feb 01 21:28:57 2009 +0200
@@ -0,0 +1,194 @@
+
+#include <lua.h>
+#include <lauxlib.h>
+#include <glib.h>
+#include <loudmouth/loudmouth.h>
+
+#include "util.h"
+#include "lm_types.h"
+
+/// lm.proxy
+/// Object, containing information about proxy-server for connection.
+/// Create object, set it's parameters, and then attach to connection
+/// with 'proxy' method.
+
+/// proxy type
+/// Stirng, specifying proxy-server type. The only type supported for now is http.
+/// V: http, none
+const string2enum_t llm_proxy_type[] = {
+	{ "http", LM_PROXY_TYPE_HTTP },
+	{ "none", LM_PROXY_TYPE_NONE },
+	{ NULL,   LM_PROXY_TYPE_HTTP },
+};
+
+/// lm.proxy.new
+/// Creates new proxy object.
+/// Note, you should specify either none of args or both.
+/// A: proxy type, string (optional proxy server name), integer (optional server port)
+/// R: lm proxy object
+static int llm_proxy_new (lua_State *L)
+{
+	int type = luaL_checkenum (L, 1, llm_proxy_type);
+	LmProxy *proxy;
+	if (lua_gettop (L) > 0) {
+		proxy = lm_proxy_new_with_server (type, luaL_checkstring (L, 2), luaL_checkint (L, 3));
+		lua_pop (L, 3);
+	} else {
+		proxy = lm_proxy_new (type);
+		lua_pop (L, 1);
+	}
+	llm_proxy_bless (L, proxy);
+	lm_proxy_unref (proxy); // XXX
+	return 1;
+}
+
+/// lm.proxy.bless
+/// Blesses given pointer to lm proxy object.
+/// A: lightuserdata (C lm proxy object)
+/// R: lm proxy object
+static int llm_proxy_bless_lua (lua_State *L)
+{
+	luaL_argcheck (L, lua_islightuserdata (L, 1), 1, "lm proxy lightuserdata expected");
+	llm_proxy_bless (L, lua_touserdata (L, 1));
+	lua_remove (L, -2);
+	return 1;
+}
+
+/// proxy:type
+/// Gets or sets proxy server type.
+/// A: proxy type (optional)
+/// R: proxy type (when called with no args)
+static int llm_proxy_kind (lua_State *L)
+{
+	llm_proxy_t *proxy = luaL_checklm_proxy (L, 1);
+	if (lua_gettop (L) > 1) { // Set
+		lm_proxy_set_type (proxy->proxy, luaL_checkenum (L, 2, llm_proxy_type));
+		lua_pop (L, 2);
+		return 0;
+	} else { // Get
+		luaL_pushenum (L, lm_proxy_get_type (proxy->proxy), llm_proxy_type);
+		lua_remove (L, -2);
+		return 1;
+	}
+}
+
+/// proxy:server
+/// Gets or sets proxy server name.
+/// A: string (optional)
+/// R: string (when called with no args)
+static int llm_proxy_server (lua_State *L)
+{
+	llm_proxy_t *object = luaL_checklm_proxy (L, 1);
+	if (lua_gettop (L) > 1) { // Set
+		lm_proxy_set_server (object->proxy, luaL_checkstring (L, 2));
+		lua_pop (L, 2);
+		return 0;
+	} else { // Get
+		lua_pushstring (L, lm_proxy_get_server (object->proxy));
+		lua_remove (L, -2);
+		return 1;
+	}
+}
+
+/// proxy:port
+/// Gets or sets proxy server port.
+/// A: integer (optional)
+/// R: integer (when called with no args)
+static int llm_proxy_port (lua_State *L)
+{
+	llm_proxy_t *object = luaL_checklm_proxy (L, 1);
+	if (lua_gettop (L) > 1) { // Set
+		lm_proxy_set_port (object->proxy, luaL_checkint (L, 2));
+		lua_pop (L, 2);
+		return 0;
+	} else { // Get
+		lua_pushnumber (L, lm_proxy_get_port (object->proxy));
+		lua_remove (L, -2);
+		return 1;
+	}
+}
+
+/// proxy:username
+/// Gets or sets username to authenticate to proxy server with.
+/// A: string (optional)
+/// R: string (when called with no args)
+static int llm_proxy_username (lua_State *L)
+{
+	llm_proxy_t *object = luaL_checklm_proxy (L, 1);
+	if (lua_gettop (L) > 1) { // Set
+		lm_proxy_set_username (object->proxy, luaL_checkstring (L, 2));
+		lua_pop (L, 2);
+		return 0;
+	} else { // Get
+		lua_pushstring (L, lm_proxy_get_username (object->proxy));
+		lua_remove (L, -2);
+		return 1;
+	}
+}
+
+/// proxy:password
+/// Gets or sets password to authenticate to proxy server with.
+/// A: string (optional)
+/// R: string (when called with no args)
+static int llm_proxy_password (lua_State *L)
+{
+	llm_proxy_t *object = luaL_checklm_proxy (L, 1);
+	if (lua_gettop (L) > 1) { // Set
+		lm_proxy_set_password (object->proxy, luaL_checkstring (L, 2));
+		lua_pop (L, 2);
+		return 0;
+	} else { // Get
+		lua_pushstring (L, lm_proxy_get_password (object->proxy));
+		lua_remove (L, -2);
+		return 1;
+	}
+}
+
+/// proxy:pointer
+/// Returns pointer to underlying C structure.
+/// R: lightuserdata
+static int llm_proxy_pointer (lua_State *L)
+{
+	llm_proxy_t *object = luaL_checklm_proxy (L, 1);
+	lua_pushlightuserdata (L, object->proxy);
+	lua_remove (L, -2);
+	return 1;
+}
+
+static int llm_proxy_gc (lua_State *L)
+{
+	llm_proxy_t *object = luaL_checklm_proxy (L, 1);
+	lm_proxy_unref (object->proxy);
+	lua_pop (L, 1);
+	return 0;
+}
+
+static const luaL_Reg llm_proxy_reg_f[] = {
+	{ "new",   llm_proxy_new       },
+	{ "bless", llm_proxy_bless_lua },
+	{ NULL,    NULL                },
+};
+
+static const luaL_Reg llm_proxy_reg_m[] = {
+	{ "port",     llm_proxy_port     },
+	{ "server",   llm_proxy_server   },
+	{ "type",     llm_proxy_kind     },
+	{ "username", llm_proxy_username },
+	{ "password", llm_proxy_password },
+	{ "pointer",  llm_proxy_pointer  },
+	{ "__gc",     llm_proxy_gc       },
+	{ NULL,       NULL               },
+};
+
+int luaopen_lm_proxy (lua_State *L)
+{
+	luaL_newmetatable (L, "loudmouth.proxy");
+	lua_pushstring (L, "__index");
+	lua_pushvalue (L, -2);
+	lua_settable (L, -3);
+	luaL_register (L, NULL, llm_proxy_reg_m);
+	lua_pop (L, 1);
+	luaL_register (L, "lm.proxy", llm_proxy_reg_f);
+	return 1;
+}
+