lm_proxy.c
author Myhailo Danylenko <isbear@ukrpost.net>
Sat, 21 Feb 2009 12:43:51 +0200
changeset 7 5db1448eb857
parent 6 90073cbb535d
child 11 a8c6460d612b
permissions -rw-r--r--
Message have node methods


#include <lua.h>
#include <lauxlib.h>
#include <glib.h>
#include <loudmouth/loudmouth.h>

#include "config.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.
/// G:
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));
	else
		proxy = lm_proxy_new (type);
	llm_proxy_bless (L, proxy);
	lm_proxy_unref (proxy); // XXX
	D ("Proxy %X created", (int) proxy);
	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));
	return 1;
}

/// proxy:type
/// Gets or sets proxy server type.
/// A: proxy type (optional)
/// R: proxy type (when called with no args) or lm proxy object
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, 1);
	} else // Get
		luaL_pushenum (L, lm_proxy_get_type (proxy->proxy), llm_proxy_type);
	return 1;
}

/// proxy:server
/// Gets or sets proxy server name.
/// A: string (optional)
/// R: string (when called with no args) or lm proxy object
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, 1);
	} else // Get
		lua_pushstring (L, lm_proxy_get_server (object->proxy));
	return 1;
}

/// proxy:port
/// Gets or sets proxy server port.
/// A: integer (optional)
/// R: integer (when called with no args) or lm proxy object
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, 1);
	} else // Get
		lua_pushnumber (L, lm_proxy_get_port (object->proxy));
	return 1;
}

/// proxy:username
/// Gets or sets username to authenticate to proxy server with.
/// A: string (optional)
/// R: string (when called with no args) or lm proxy object
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, 1);
	} else // Get
		lua_pushstring (L, lm_proxy_get_username (object->proxy));
	return 1;
}

/// proxy:password
/// Gets or sets password to authenticate to proxy server with.
/// A: string (optional)
/// R: string (when called with no args) or lm proxy object
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, 1);
	} else // Get
		lua_pushstring (L, lm_proxy_get_password (object->proxy));
	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);
	return 1;
}

static int llm_proxy_gc (lua_State *L)
{
	llm_proxy_t *object = luaL_checklm_proxy (L, 1);
	D ("Proxy %X gc called", (int) object);
	lm_proxy_unref (object->proxy);
	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;
}