lm_proxy.c
changeset 11 a8c6460d612b
parent 6 90073cbb535d
child 12 63f06a23c235
equal deleted inserted replaced
10:aed141accdd9 11:a8c6460d612b
    14 /// with 'proxy' method.
    14 /// with 'proxy' method.
    15 
    15 
    16 /// proxy type
    16 /// proxy type
    17 /// Stirng, specifying proxy-server type. The only type supported for now is http.
    17 /// Stirng, specifying proxy-server type. The only type supported for now is http.
    18 /// G:
    18 /// G:
    19 const string2enum_t llm_proxy_type[] = {
    19 const string2enum_t type_lm_proxy[] = {
    20 	{ "http", LM_PROXY_TYPE_HTTP },
    20 	{ "http", LM_PROXY_TYPE_HTTP },
    21 	{ "none", LM_PROXY_TYPE_NONE },
    21 	{ "none", LM_PROXY_TYPE_NONE },
    22 	{ NULL,   LM_PROXY_TYPE_HTTP },
    22 	{ NULL,   LM_PROXY_TYPE_HTTP },
    23 };
    23 };
    24 
    24 
    25 /// lm.proxy.new
    25 /// lm.proxy.new
    26 /// Creates new proxy object.
    26 /// Creates new proxy object.
    27 /// Note, you should specify either none of args or both.
    27 /// Note, you should specify either none of args or both.
    28 /// A: proxy type, string (optional proxy server name), integer (optional server port)
    28 /// A: proxy type, string (optional proxy server name), integer (optional server port)
    29 /// R: lm proxy object
    29 /// R: lm proxy object
    30 static int llm_proxy_new (lua_State *L)
    30 static int new_lm_proxy (lua_State *L)
    31 {
    31 {
    32 	int type = luaL_checkenum (L, 1, llm_proxy_type);
    32 	int type = luaL_checkenum (L, 1, type_lm_proxy);
    33 	LmProxy *proxy;
    33 	LmProxy *proxy;
    34 	if (lua_gettop (L) > 0)
    34 	if (lua_gettop (L) > 0)
    35 		proxy = lm_proxy_new_with_server (type, luaL_checkstring (L, 2), luaL_checkint (L, 3));
    35 		proxy = lm_proxy_new_with_server (type, luaL_checkstring (L, 2), luaL_checkint (L, 3));
    36 	else
    36 	else
    37 		proxy = lm_proxy_new (type);
    37 		proxy = lm_proxy_new (type);
    38 	llm_proxy_bless (L, proxy);
    38 	bless_lm_proxy (L, proxy);
    39 	lm_proxy_unref (proxy); // XXX
    39 	lm_proxy_unref (proxy); // XXX
    40 	D ("Proxy %X created", (int) proxy);
    40 	D ("Proxy %X created", (int) proxy);
    41 	return 1;
    41 	return 1;
    42 }
    42 }
    43 
    43 
    44 /// lm.proxy.bless
    44 /// lm.proxy.bless
    45 /// Blesses given pointer to lm proxy object.
    45 /// Blesses given pointer to lm proxy object.
    46 /// A: lightuserdata (C lm proxy object)
    46 /// A: lightuserdata (C lm proxy object)
    47 /// R: lm proxy object
    47 /// R: lm proxy object
    48 static int llm_proxy_bless_lua (lua_State *L)
    48 static int bless_lua_lm_proxy (lua_State *L)
    49 {
    49 {
    50 	luaL_argcheck (L, lua_islightuserdata (L, 1), 1, "lm proxy lightuserdata expected");
    50 	luaL_argcheck (L, lua_islightuserdata (L, 1), 1, "lm proxy lightuserdata expected");
    51 	llm_proxy_bless (L, lua_touserdata (L, 1));
    51 	bless_lm_proxy (L, lua_touserdata (L, 1));
    52 	return 1;
    52 	return 1;
    53 }
    53 }
    54 
    54 
    55 /// proxy:type
    55 /// proxy:type
    56 /// Gets or sets proxy server type.
    56 /// Gets or sets proxy server type.
    57 /// A: proxy type (optional)
    57 /// A: proxy type (optional)
    58 /// R: proxy type (when called with no args) or lm proxy object
    58 /// R: proxy type (when called with no args) or lm proxy object
    59 static int llm_proxy_kind (lua_State *L)
    59 static int kind_lm_proxy (lua_State *L)
    60 {
    60 {
    61 	llm_proxy_t *proxy = luaL_checklm_proxy (L, 1);
    61 	llm_proxy_t *proxy = luaL_checklm_proxy (L, 1);
    62 	if (lua_gettop (L) > 1) { // Set
    62 	if (lua_gettop (L) > 1) { // Set
    63 		lm_proxy_set_type (proxy->proxy, luaL_checkenum (L, 2, llm_proxy_type));
    63 		lm_proxy_set_type (proxy->proxy, luaL_checkenum (L, 2, type_lm_proxy));
    64 		lua_pop (L, 1);
    64 		lua_pop (L, 1);
    65 	} else // Get
    65 	} else // Get
    66 		luaL_pushenum (L, lm_proxy_get_type (proxy->proxy), llm_proxy_type);
    66 		luaL_pushenum (L, lm_proxy_get_type (proxy->proxy), type_lm_proxy);
    67 	return 1;
    67 	return 1;
    68 }
    68 }
    69 
    69 
    70 /// proxy:server
    70 /// proxy:server
    71 /// Gets or sets proxy server name.
    71 /// Gets or sets proxy server name.
    72 /// A: string (optional)
    72 /// A: string (optional)
    73 /// R: string (when called with no args) or lm proxy object
    73 /// R: string (when called with no args) or lm proxy object
    74 static int llm_proxy_server (lua_State *L)
    74 static int server_lm_proxy (lua_State *L)
    75 {
    75 {
    76 	llm_proxy_t *object = luaL_checklm_proxy (L, 1);
    76 	llm_proxy_t *object = luaL_checklm_proxy (L, 1);
    77 	if (lua_gettop (L) > 1) { // Set
    77 	if (lua_gettop (L) > 1) { // Set
    78 		lm_proxy_set_server (object->proxy, luaL_checkstring (L, 2));
    78 		lm_proxy_set_server (object->proxy, luaL_checkstring (L, 2));
    79 		lua_pop (L, 1);
    79 		lua_pop (L, 1);
    84 
    84 
    85 /// proxy:port
    85 /// proxy:port
    86 /// Gets or sets proxy server port.
    86 /// Gets or sets proxy server port.
    87 /// A: integer (optional)
    87 /// A: integer (optional)
    88 /// R: integer (when called with no args) or lm proxy object
    88 /// R: integer (when called with no args) or lm proxy object
    89 static int llm_proxy_port (lua_State *L)
    89 static int port_lm_proxy (lua_State *L)
    90 {
    90 {
    91 	llm_proxy_t *object = luaL_checklm_proxy (L, 1);
    91 	llm_proxy_t *object = luaL_checklm_proxy (L, 1);
    92 	if (lua_gettop (L) > 1) { // Set
    92 	if (lua_gettop (L) > 1) { // Set
    93 		lm_proxy_set_port (object->proxy, luaL_checkint (L, 2));
    93 		lm_proxy_set_port (object->proxy, luaL_checkint (L, 2));
    94 		lua_pop (L, 1);
    94 		lua_pop (L, 1);
    99 
    99 
   100 /// proxy:username
   100 /// proxy:username
   101 /// Gets or sets username to authenticate to proxy server with.
   101 /// Gets or sets username to authenticate to proxy server with.
   102 /// A: string (optional)
   102 /// A: string (optional)
   103 /// R: string (when called with no args) or lm proxy object
   103 /// R: string (when called with no args) or lm proxy object
   104 static int llm_proxy_username (lua_State *L)
   104 static int username_lm_proxy (lua_State *L)
   105 {
   105 {
   106 	llm_proxy_t *object = luaL_checklm_proxy (L, 1);
   106 	llm_proxy_t *object = luaL_checklm_proxy (L, 1);
   107 	if (lua_gettop (L) > 1) { // Set
   107 	if (lua_gettop (L) > 1) { // Set
   108 		lm_proxy_set_username (object->proxy, luaL_checkstring (L, 2));
   108 		lm_proxy_set_username (object->proxy, luaL_checkstring (L, 2));
   109 		lua_pop (L, 1);
   109 		lua_pop (L, 1);
   114 
   114 
   115 /// proxy:password
   115 /// proxy:password
   116 /// Gets or sets password to authenticate to proxy server with.
   116 /// Gets or sets password to authenticate to proxy server with.
   117 /// A: string (optional)
   117 /// A: string (optional)
   118 /// R: string (when called with no args) or lm proxy object
   118 /// R: string (when called with no args) or lm proxy object
   119 static int llm_proxy_password (lua_State *L)
   119 static int password_lm_proxy (lua_State *L)
   120 {
   120 {
   121 	llm_proxy_t *object = luaL_checklm_proxy (L, 1);
   121 	llm_proxy_t *object = luaL_checklm_proxy (L, 1);
   122 	if (lua_gettop (L) > 1) { // Set
   122 	if (lua_gettop (L) > 1) { // Set
   123 		lm_proxy_set_password (object->proxy, luaL_checkstring (L, 2));
   123 		lm_proxy_set_password (object->proxy, luaL_checkstring (L, 2));
   124 		lua_pop (L, 1);
   124 		lua_pop (L, 1);
   128 }
   128 }
   129 
   129 
   130 /// proxy:pointer
   130 /// proxy:pointer
   131 /// Returns pointer to underlying C structure.
   131 /// Returns pointer to underlying C structure.
   132 /// R: lightuserdata
   132 /// R: lightuserdata
   133 static int llm_proxy_pointer (lua_State *L)
   133 static int pointer_lm_proxy (lua_State *L)
   134 {
   134 {
   135 	llm_proxy_t *object = luaL_checklm_proxy (L, 1);
   135 	llm_proxy_t *object = luaL_checklm_proxy (L, 1);
   136 	lua_pushlightuserdata (L, object->proxy);
   136 	lua_pushlightuserdata (L, object->proxy);
   137 	return 1;
   137 	return 1;
   138 }
   138 }
   139 
   139 
   140 static int llm_proxy_gc (lua_State *L)
   140 static int gc_lm_proxy (lua_State *L)
   141 {
   141 {
   142 	llm_proxy_t *object = luaL_checklm_proxy (L, 1);
   142 	llm_proxy_t *object = luaL_checklm_proxy (L, 1);
   143 	D ("Proxy %X gc called", (int) object);
   143 	D ("Proxy %X gc called", (int) object);
   144 	lm_proxy_unref (object->proxy);
   144 	lm_proxy_unref (object->proxy);
   145 	return 0;
   145 	return 0;
   146 }
   146 }
   147 
   147 
   148 static const luaL_Reg llm_proxy_reg_f[] = {
   148 static const luaL_Reg reg_f_lm_proxy[] = {
   149 	{ "new",   llm_proxy_new       },
   149 	{ "new",   new_lm_proxy       },
   150 	{ "bless", llm_proxy_bless_lua },
   150 	{ "bless", bless_lua_lm_proxy },
   151 	{ NULL,    NULL                },
   151 	{ NULL,    NULL                },
   152 };
   152 };
   153 
   153 
   154 static const luaL_Reg llm_proxy_reg_m[] = {
   154 static const luaL_Reg reg_m_lm_proxy[] = {
   155 	{ "port",     llm_proxy_port     },
   155 	{ "port",     port_lm_proxy     },
   156 	{ "server",   llm_proxy_server   },
   156 	{ "server",   server_lm_proxy   },
   157 	{ "type",     llm_proxy_kind     },
   157 	{ "type",     kind_lm_proxy     },
   158 	{ "username", llm_proxy_username },
   158 	{ "username", username_lm_proxy },
   159 	{ "password", llm_proxy_password },
   159 	{ "password", password_lm_proxy },
   160 	{ "pointer",  llm_proxy_pointer  },
   160 	{ "pointer",  pointer_lm_proxy  },
   161 	{ "__gc",     llm_proxy_gc       },
   161 	{ "__gc",     gc_lm_proxy       },
   162 	{ NULL,       NULL               },
   162 	{ NULL,       NULL               },
   163 };
   163 };
   164 
   164 
   165 int luaopen_lm_proxy (lua_State *L)
   165 int luaopen_lm_proxy (lua_State *L)
   166 {
   166 {
   167 	luaL_newmetatable (L, "loudmouth.proxy");
   167 	luaL_newmetatable (L, "loudmouth.proxy");
   168 	lua_pushstring (L, "__index");
   168 	lua_pushstring (L, "__index");
   169 	lua_pushvalue (L, -2);
   169 	lua_pushvalue (L, -2);
   170 	lua_settable (L, -3);
   170 	lua_settable (L, -3);
   171 	luaL_register (L, NULL, llm_proxy_reg_m);
   171 	luaL_register (L, NULL, reg_m_lm_proxy);
   172 	lua_pop (L, 1);
   172 	lua_pop (L, 1);
   173 	luaL_register (L, "lm.proxy", llm_proxy_reg_f);
   173 	luaL_register (L, "lm.proxy", reg_f_lm_proxy);
   174 	return 1;
   174 	return 1;
   175 }
   175 }
   176 
   176