isbear@23: isbear@23: /* Copyright 2009 Myhailo Danylenko isbear@23: isbear@23: This file is part of lua-lm. isbear@23: isbear@23: lua-lm is free software: you can redistribute it and/or modify isbear@23: it under the terms of the GNU General Public License as published by isbear@23: the Free Software Foundation, either version 2 of the License, or isbear@23: (at your option) any later version. isbear@23: isbear@23: This program is distributed in the hope that it will be useful, isbear@23: but WITHOUT ANY WARRANTY; without even the implied warranty of isbear@23: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the isbear@23: GNU General Public License for more details. isbear@23: isbear@23: You should have received a copy of the GNU General Public License isbear@23: along with this program. If not, see . */ isbear@0: isbear@0: #include isbear@0: #include isbear@0: #include isbear@0: #include isbear@0: isbear@6: #include "config.h" isbear@0: #include "util.h" isbear@0: #include "lm_types.h" isbear@0: isbear@0: /// lm.proxy isbear@0: /// Object, containing information about proxy-server for connection. isbear@0: /// Create object, set it's parameters, and then attach to connection isbear@0: /// with 'proxy' method. isbear@0: isbear@0: /// proxy type isbear@0: /// Stirng, specifying proxy-server type. The only type supported for now is http. isbear@4: /// G: isbear@11: const string2enum_t type_lm_proxy[] = { isbear@0: { "http", LM_PROXY_TYPE_HTTP }, isbear@0: { "none", LM_PROXY_TYPE_NONE }, isbear@0: { NULL, LM_PROXY_TYPE_HTTP }, isbear@0: }; isbear@0: isbear@0: /// lm.proxy.new isbear@0: /// Creates new proxy object. isbear@0: /// Note, you should specify either none of args or both. isbear@0: /// A: proxy type, string (optional proxy server name), integer (optional server port) isbear@0: /// R: lm proxy object isbear@11: static int new_lm_proxy (lua_State *L) isbear@0: { isbear@11: int type = luaL_checkenum (L, 1, type_lm_proxy); isbear@0: LmProxy *proxy; isbear@6: if (lua_gettop (L) > 0) isbear@63: proxy = lm_proxy_new_with_server (type, luaL_checkstring (L, 2), luaL_checkinteger (L, 3)); isbear@6: else isbear@0: proxy = lm_proxy_new (type); isbear@11: bless_lm_proxy (L, proxy); isbear@0: lm_proxy_unref (proxy); // XXX isbear@38: D ("Proxy %p created", proxy); isbear@0: return 1; isbear@0: } isbear@0: isbear@0: /// lm.proxy.bless isbear@0: /// Blesses given pointer to lm proxy object. isbear@0: /// A: lightuserdata (C lm proxy object) isbear@0: /// R: lm proxy object isbear@11: static int bless_lua_lm_proxy (lua_State *L) isbear@0: { isbear@0: luaL_argcheck (L, lua_islightuserdata (L, 1), 1, "lm proxy lightuserdata expected"); isbear@11: bless_lm_proxy (L, lua_touserdata (L, 1)); isbear@0: return 1; isbear@0: } isbear@0: isbear@0: /// proxy:type isbear@0: /// Gets or sets proxy server type. isbear@0: /// A: proxy type (optional) isbear@6: /// R: proxy type (when called with no args) or lm proxy object isbear@11: static int kind_lm_proxy (lua_State *L) isbear@0: { isbear@0: llm_proxy_t *proxy = luaL_checklm_proxy (L, 1); isbear@0: if (lua_gettop (L) > 1) { // Set isbear@11: lm_proxy_set_type (proxy->proxy, luaL_checkenum (L, 2, type_lm_proxy)); isbear@6: lua_pop (L, 1); isbear@6: } else // Get isbear@11: luaL_pushenum (L, lm_proxy_get_type (proxy->proxy), type_lm_proxy); isbear@6: return 1; isbear@0: } isbear@0: isbear@0: /// proxy:server isbear@0: /// Gets or sets proxy server name. isbear@0: /// A: string (optional) isbear@6: /// R: string (when called with no args) or lm proxy object isbear@11: static int server_lm_proxy (lua_State *L) isbear@0: { isbear@0: llm_proxy_t *object = luaL_checklm_proxy (L, 1); isbear@0: if (lua_gettop (L) > 1) { // Set isbear@0: lm_proxy_set_server (object->proxy, luaL_checkstring (L, 2)); isbear@6: lua_pop (L, 1); isbear@6: } else // Get isbear@0: lua_pushstring (L, lm_proxy_get_server (object->proxy)); isbear@6: return 1; isbear@0: } isbear@0: isbear@0: /// proxy:port isbear@0: /// Gets or sets proxy server port. isbear@0: /// A: integer (optional) isbear@6: /// R: integer (when called with no args) or lm proxy object isbear@11: static int port_lm_proxy (lua_State *L) isbear@0: { isbear@0: llm_proxy_t *object = luaL_checklm_proxy (L, 1); isbear@0: if (lua_gettop (L) > 1) { // Set isbear@63: lm_proxy_set_port (object->proxy, luaL_checkinteger (L, 2)); isbear@6: lua_pop (L, 1); isbear@6: } else // Get isbear@0: lua_pushnumber (L, lm_proxy_get_port (object->proxy)); isbear@6: return 1; isbear@0: } isbear@0: isbear@0: /// proxy:username isbear@0: /// Gets or sets username to authenticate to proxy server with. isbear@0: /// A: string (optional) isbear@6: /// R: string (when called with no args) or lm proxy object isbear@11: static int username_lm_proxy (lua_State *L) isbear@0: { isbear@0: llm_proxy_t *object = luaL_checklm_proxy (L, 1); isbear@0: if (lua_gettop (L) > 1) { // Set isbear@0: lm_proxy_set_username (object->proxy, luaL_checkstring (L, 2)); isbear@6: lua_pop (L, 1); isbear@6: } else // Get isbear@0: lua_pushstring (L, lm_proxy_get_username (object->proxy)); isbear@6: return 1; isbear@0: } isbear@0: isbear@0: /// proxy:password isbear@0: /// Gets or sets password to authenticate to proxy server with. isbear@0: /// A: string (optional) isbear@6: /// R: string (when called with no args) or lm proxy object isbear@11: static int password_lm_proxy (lua_State *L) isbear@0: { isbear@0: llm_proxy_t *object = luaL_checklm_proxy (L, 1); isbear@0: if (lua_gettop (L) > 1) { // Set isbear@0: lm_proxy_set_password (object->proxy, luaL_checkstring (L, 2)); isbear@6: lua_pop (L, 1); isbear@6: } else // Get isbear@0: lua_pushstring (L, lm_proxy_get_password (object->proxy)); isbear@6: return 1; isbear@0: } isbear@0: isbear@0: /// proxy:pointer isbear@0: /// Returns pointer to underlying C structure. isbear@0: /// R: lightuserdata isbear@11: static int pointer_lm_proxy (lua_State *L) isbear@0: { isbear@0: llm_proxy_t *object = luaL_checklm_proxy (L, 1); isbear@0: lua_pushlightuserdata (L, object->proxy); isbear@0: return 1; isbear@0: } isbear@0: isbear@11: static int gc_lm_proxy (lua_State *L) isbear@0: { isbear@0: llm_proxy_t *object = luaL_checklm_proxy (L, 1); isbear@38: D ("Proxy %p gc called", object); isbear@0: lm_proxy_unref (object->proxy); isbear@0: return 0; isbear@0: } isbear@0: isbear@11: static const luaL_Reg reg_f_lm_proxy[] = { isbear@11: { "new", new_lm_proxy }, isbear@11: { "bless", bless_lua_lm_proxy }, isbear@12: { NULL, NULL }, isbear@0: }; isbear@0: isbear@11: static const luaL_Reg reg_m_lm_proxy[] = { isbear@11: { "port", port_lm_proxy }, isbear@11: { "server", server_lm_proxy }, isbear@11: { "type", kind_lm_proxy }, isbear@11: { "username", username_lm_proxy }, isbear@11: { "password", password_lm_proxy }, isbear@11: { "pointer", pointer_lm_proxy }, isbear@11: { "__gc", gc_lm_proxy }, isbear@12: { NULL, NULL }, isbear@0: }; isbear@0: isbear@0: int luaopen_lm_proxy (lua_State *L) isbear@0: { isbear@0: luaL_newmetatable (L, "loudmouth.proxy"); isbear@19: lua_pushvalue (L, -1); isbear@19: lua_setfield (L, -2, "__index"); isbear@54: luaL_setfuncs (L, reg_m_lm_proxy, 0); isbear@0: lua_pop (L, 1); isbear@16: lua_newtable (L); // XXX we can specify here exact amount of fields isbear@54: luaL_setfuncs (L, reg_f_lm_proxy, 0); isbear@0: return 1; isbear@0: } isbear@0: