lm_proxy.c
changeset 6 90073cbb535d
parent 4 5770be2d5f3f
child 11 a8c6460d612b
equal deleted inserted replaced
5:e617c9cf6dd3 6:90073cbb535d
     2 #include <lua.h>
     2 #include <lua.h>
     3 #include <lauxlib.h>
     3 #include <lauxlib.h>
     4 #include <glib.h>
     4 #include <glib.h>
     5 #include <loudmouth/loudmouth.h>
     5 #include <loudmouth/loudmouth.h>
     6 
     6 
       
     7 #include "config.h"
     7 #include "util.h"
     8 #include "util.h"
     8 #include "lm_types.h"
     9 #include "lm_types.h"
     9 
    10 
    10 /// lm.proxy
    11 /// lm.proxy
    11 /// Object, containing information about proxy-server for connection.
    12 /// Object, containing information about proxy-server for connection.
    28 /// R: lm proxy object
    29 /// R: lm proxy object
    29 static int llm_proxy_new (lua_State *L)
    30 static int llm_proxy_new (lua_State *L)
    30 {
    31 {
    31 	int type = luaL_checkenum (L, 1, llm_proxy_type);
    32 	int type = luaL_checkenum (L, 1, llm_proxy_type);
    32 	LmProxy *proxy;
    33 	LmProxy *proxy;
    33 	if (lua_gettop (L) > 0) {
    34 	if (lua_gettop (L) > 0)
    34 		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));
    35 		lua_pop (L, 3);
    36 	else
    36 	} else {
       
    37 		proxy = lm_proxy_new (type);
    37 		proxy = lm_proxy_new (type);
    38 		lua_pop (L, 1);
       
    39 	}
       
    40 	llm_proxy_bless (L, proxy);
    38 	llm_proxy_bless (L, proxy);
    41 	lm_proxy_unref (proxy); // XXX
    39 	lm_proxy_unref (proxy); // XXX
       
    40 	D ("Proxy %X created", (int) proxy);
    42 	return 1;
    41 	return 1;
    43 }
    42 }
    44 
    43 
    45 /// lm.proxy.bless
    44 /// lm.proxy.bless
    46 /// Blesses given pointer to lm proxy object.
    45 /// Blesses given pointer to lm proxy object.
    48 /// R: lm proxy object
    47 /// R: lm proxy object
    49 static int llm_proxy_bless_lua (lua_State *L)
    48 static int llm_proxy_bless_lua (lua_State *L)
    50 {
    49 {
    51 	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");
    52 	llm_proxy_bless (L, lua_touserdata (L, 1));
    51 	llm_proxy_bless (L, lua_touserdata (L, 1));
    53 	lua_remove (L, -2);
       
    54 	return 1;
    52 	return 1;
    55 }
    53 }
    56 
    54 
    57 /// proxy:type
    55 /// proxy:type
    58 /// Gets or sets proxy server type.
    56 /// Gets or sets proxy server type.
    59 /// A: proxy type (optional)
    57 /// A: proxy type (optional)
    60 /// R: proxy type (when called with no args)
    58 /// R: proxy type (when called with no args) or lm proxy object
    61 static int llm_proxy_kind (lua_State *L)
    59 static int llm_proxy_kind (lua_State *L)
    62 {
    60 {
    63 	llm_proxy_t *proxy = luaL_checklm_proxy (L, 1);
    61 	llm_proxy_t *proxy = luaL_checklm_proxy (L, 1);
    64 	if (lua_gettop (L) > 1) { // Set
    62 	if (lua_gettop (L) > 1) { // Set
    65 		lm_proxy_set_type (proxy->proxy, luaL_checkenum (L, 2, llm_proxy_type));
    63 		lm_proxy_set_type (proxy->proxy, luaL_checkenum (L, 2, llm_proxy_type));
    66 		lua_pop (L, 2);
    64 		lua_pop (L, 1);
    67 		return 0;
    65 	} else // Get
    68 	} else { // Get
       
    69 		luaL_pushenum (L, lm_proxy_get_type (proxy->proxy), llm_proxy_type);
    66 		luaL_pushenum (L, lm_proxy_get_type (proxy->proxy), llm_proxy_type);
    70 		lua_remove (L, -2);
    67 	return 1;
    71 		return 1;
       
    72 	}
       
    73 }
    68 }
    74 
    69 
    75 /// proxy:server
    70 /// proxy:server
    76 /// Gets or sets proxy server name.
    71 /// Gets or sets proxy server name.
    77 /// A: string (optional)
    72 /// A: string (optional)
    78 /// R: string (when called with no args)
    73 /// R: string (when called with no args) or lm proxy object
    79 static int llm_proxy_server (lua_State *L)
    74 static int llm_proxy_server (lua_State *L)
    80 {
    75 {
    81 	llm_proxy_t *object = luaL_checklm_proxy (L, 1);
    76 	llm_proxy_t *object = luaL_checklm_proxy (L, 1);
    82 	if (lua_gettop (L) > 1) { // Set
    77 	if (lua_gettop (L) > 1) { // Set
    83 		lm_proxy_set_server (object->proxy, luaL_checkstring (L, 2));
    78 		lm_proxy_set_server (object->proxy, luaL_checkstring (L, 2));
    84 		lua_pop (L, 2);
    79 		lua_pop (L, 1);
    85 		return 0;
    80 	} else // Get
    86 	} else { // Get
       
    87 		lua_pushstring (L, lm_proxy_get_server (object->proxy));
    81 		lua_pushstring (L, lm_proxy_get_server (object->proxy));
    88 		lua_remove (L, -2);
    82 	return 1;
    89 		return 1;
       
    90 	}
       
    91 }
    83 }
    92 
    84 
    93 /// proxy:port
    85 /// proxy:port
    94 /// Gets or sets proxy server port.
    86 /// Gets or sets proxy server port.
    95 /// A: integer (optional)
    87 /// A: integer (optional)
    96 /// R: integer (when called with no args)
    88 /// R: integer (when called with no args) or lm proxy object
    97 static int llm_proxy_port (lua_State *L)
    89 static int llm_proxy_port (lua_State *L)
    98 {
    90 {
    99 	llm_proxy_t *object = luaL_checklm_proxy (L, 1);
    91 	llm_proxy_t *object = luaL_checklm_proxy (L, 1);
   100 	if (lua_gettop (L) > 1) { // Set
    92 	if (lua_gettop (L) > 1) { // Set
   101 		lm_proxy_set_port (object->proxy, luaL_checkint (L, 2));
    93 		lm_proxy_set_port (object->proxy, luaL_checkint (L, 2));
   102 		lua_pop (L, 2);
    94 		lua_pop (L, 1);
   103 		return 0;
    95 	} else // Get
   104 	} else { // Get
       
   105 		lua_pushnumber (L, lm_proxy_get_port (object->proxy));
    96 		lua_pushnumber (L, lm_proxy_get_port (object->proxy));
   106 		lua_remove (L, -2);
    97 	return 1;
   107 		return 1;
       
   108 	}
       
   109 }
    98 }
   110 
    99 
   111 /// proxy:username
   100 /// proxy:username
   112 /// Gets or sets username to authenticate to proxy server with.
   101 /// Gets or sets username to authenticate to proxy server with.
   113 /// A: string (optional)
   102 /// A: string (optional)
   114 /// R: string (when called with no args)
   103 /// R: string (when called with no args) or lm proxy object
   115 static int llm_proxy_username (lua_State *L)
   104 static int llm_proxy_username (lua_State *L)
   116 {
   105 {
   117 	llm_proxy_t *object = luaL_checklm_proxy (L, 1);
   106 	llm_proxy_t *object = luaL_checklm_proxy (L, 1);
   118 	if (lua_gettop (L) > 1) { // Set
   107 	if (lua_gettop (L) > 1) { // Set
   119 		lm_proxy_set_username (object->proxy, luaL_checkstring (L, 2));
   108 		lm_proxy_set_username (object->proxy, luaL_checkstring (L, 2));
   120 		lua_pop (L, 2);
   109 		lua_pop (L, 1);
   121 		return 0;
   110 	} else // Get
   122 	} else { // Get
       
   123 		lua_pushstring (L, lm_proxy_get_username (object->proxy));
   111 		lua_pushstring (L, lm_proxy_get_username (object->proxy));
   124 		lua_remove (L, -2);
   112 	return 1;
   125 		return 1;
       
   126 	}
       
   127 }
   113 }
   128 
   114 
   129 /// proxy:password
   115 /// proxy:password
   130 /// Gets or sets password to authenticate to proxy server with.
   116 /// Gets or sets password to authenticate to proxy server with.
   131 /// A: string (optional)
   117 /// A: string (optional)
   132 /// R: string (when called with no args)
   118 /// R: string (when called with no args) or lm proxy object
   133 static int llm_proxy_password (lua_State *L)
   119 static int llm_proxy_password (lua_State *L)
   134 {
   120 {
   135 	llm_proxy_t *object = luaL_checklm_proxy (L, 1);
   121 	llm_proxy_t *object = luaL_checklm_proxy (L, 1);
   136 	if (lua_gettop (L) > 1) { // Set
   122 	if (lua_gettop (L) > 1) { // Set
   137 		lm_proxy_set_password (object->proxy, luaL_checkstring (L, 2));
   123 		lm_proxy_set_password (object->proxy, luaL_checkstring (L, 2));
   138 		lua_pop (L, 2);
   124 		lua_pop (L, 1);
   139 		return 0;
   125 	} else // Get
   140 	} else { // Get
       
   141 		lua_pushstring (L, lm_proxy_get_password (object->proxy));
   126 		lua_pushstring (L, lm_proxy_get_password (object->proxy));
   142 		lua_remove (L, -2);
   127 	return 1;
   143 		return 1;
       
   144 	}
       
   145 }
   128 }
   146 
   129 
   147 /// proxy:pointer
   130 /// proxy:pointer
   148 /// Returns pointer to underlying C structure.
   131 /// Returns pointer to underlying C structure.
   149 /// R: lightuserdata
   132 /// R: lightuserdata
   150 static int llm_proxy_pointer (lua_State *L)
   133 static int llm_proxy_pointer (lua_State *L)
   151 {
   134 {
   152 	llm_proxy_t *object = luaL_checklm_proxy (L, 1);
   135 	llm_proxy_t *object = luaL_checklm_proxy (L, 1);
   153 	lua_pushlightuserdata (L, object->proxy);
   136 	lua_pushlightuserdata (L, object->proxy);
   154 	lua_remove (L, -2);
       
   155 	return 1;
   137 	return 1;
   156 }
   138 }
   157 
   139 
   158 static int llm_proxy_gc (lua_State *L)
   140 static int llm_proxy_gc (lua_State *L)
   159 {
   141 {
   160 	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);
   161 	lm_proxy_unref (object->proxy);
   144 	lm_proxy_unref (object->proxy);
   162 	lua_pop (L, 1);
       
   163 	return 0;
   145 	return 0;
   164 }
   146 }
   165 
   147 
   166 static const luaL_Reg llm_proxy_reg_f[] = {
   148 static const luaL_Reg llm_proxy_reg_f[] = {
   167 	{ "new",   llm_proxy_new       },
   149 	{ "new",   llm_proxy_new       },