lm_proxy.c
author Myhailo Danylenko <isbear@ukrpost.net>
Wed, 16 Mar 2016 01:53:56 +0200
changeset 66 a40beb82130c
parent 63 c17f3295f52c
permissions -rw-r--r--
node: Use new loudmouth feature - public attribute list (v0.9.7)

/* Copyright 2009 Myhailo Danylenko

This file is part of lua-lm.

lua-lm is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>. */

#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 type_lm_proxy[] = {
	{ "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 new_lm_proxy (lua_State *L)
{
	int type = luaL_checkenum (L, 1, type_lm_proxy);
	LmProxy *proxy;
	if (lua_gettop (L) > 0)
		proxy = lm_proxy_new_with_server (type, luaL_checkstring (L, 2), luaL_checkinteger (L, 3));
	else
		proxy = lm_proxy_new (type);
	bless_lm_proxy (L, proxy);
	lm_proxy_unref (proxy); // XXX
	D ("Proxy %p created", 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 bless_lua_lm_proxy (lua_State *L)
{
	luaL_argcheck (L, lua_islightuserdata (L, 1), 1, "lm proxy lightuserdata expected");
	bless_lm_proxy (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 kind_lm_proxy (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, type_lm_proxy));
		lua_pop (L, 1);
	} else // Get
		luaL_pushenum (L, lm_proxy_get_type (proxy->proxy), type_lm_proxy);
	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 server_lm_proxy (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 port_lm_proxy (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_checkinteger (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 username_lm_proxy (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 password_lm_proxy (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 pointer_lm_proxy (lua_State *L)
{
	llm_proxy_t *object = luaL_checklm_proxy (L, 1);
	lua_pushlightuserdata (L, object->proxy);
	return 1;
}

static int gc_lm_proxy (lua_State *L)
{
	llm_proxy_t *object = luaL_checklm_proxy (L, 1);
	D ("Proxy %p gc called", object);
	lm_proxy_unref (object->proxy);
	return 0;
}

static const luaL_Reg reg_f_lm_proxy[] = {
	{ "new",   new_lm_proxy       },
	{ "bless", bless_lua_lm_proxy },
	{ NULL,    NULL               },
};

static const luaL_Reg reg_m_lm_proxy[] = {
	{ "port",     port_lm_proxy     },
	{ "server",   server_lm_proxy   },
	{ "type",     kind_lm_proxy     },
	{ "username", username_lm_proxy },
	{ "password", password_lm_proxy },
	{ "pointer",  pointer_lm_proxy  },
	{ "__gc",     gc_lm_proxy       },
	{ NULL,       NULL              },
};

int luaopen_lm_proxy (lua_State *L)
{
	luaL_newmetatable (L, "loudmouth.proxy");
	lua_pushvalue (L, -1);
	lua_setfield (L, -2, "__index");
	luaL_setfuncs (L, reg_m_lm_proxy, 0);
	lua_pop (L, 1);
	lua_newtable (L); // XXX we can specify here exact amount of fields
	luaL_setfuncs (L, reg_f_lm_proxy, 0);
	return 1;
}