lm_proxy.c
changeset 0 84fdfb0344c9
child 4 5770be2d5f3f
equal deleted inserted replaced
-1:000000000000 0:84fdfb0344c9
       
     1 
       
     2 #include <lua.h>
       
     3 #include <lauxlib.h>
       
     4 #include <glib.h>
       
     5 #include <loudmouth/loudmouth.h>
       
     6 
       
     7 #include "util.h"
       
     8 #include "lm_types.h"
       
     9 
       
    10 /// lm.proxy
       
    11 /// Object, containing information about proxy-server for connection.
       
    12 /// Create object, set it's parameters, and then attach to connection
       
    13 /// with 'proxy' method.
       
    14 
       
    15 /// proxy type
       
    16 /// Stirng, specifying proxy-server type. The only type supported for now is http.
       
    17 /// V: http, none
       
    18 const string2enum_t llm_proxy_type[] = {
       
    19 	{ "http", LM_PROXY_TYPE_HTTP },
       
    20 	{ "none", LM_PROXY_TYPE_NONE },
       
    21 	{ NULL,   LM_PROXY_TYPE_HTTP },
       
    22 };
       
    23 
       
    24 /// lm.proxy.new
       
    25 /// Creates new proxy object.
       
    26 /// Note, you should specify either none of args or both.
       
    27 /// A: proxy type, string (optional proxy server name), integer (optional server port)
       
    28 /// R: lm proxy object
       
    29 static int llm_proxy_new (lua_State *L)
       
    30 {
       
    31 	int type = luaL_checkenum (L, 1, llm_proxy_type);
       
    32 	LmProxy *proxy;
       
    33 	if (lua_gettop (L) > 0) {
       
    34 		proxy = lm_proxy_new_with_server (type, luaL_checkstring (L, 2), luaL_checkint (L, 3));
       
    35 		lua_pop (L, 3);
       
    36 	} else {
       
    37 		proxy = lm_proxy_new (type);
       
    38 		lua_pop (L, 1);
       
    39 	}
       
    40 	llm_proxy_bless (L, proxy);
       
    41 	lm_proxy_unref (proxy); // XXX
       
    42 	return 1;
       
    43 }
       
    44 
       
    45 /// lm.proxy.bless
       
    46 /// Blesses given pointer to lm proxy object.
       
    47 /// A: lightuserdata (C lm proxy object)
       
    48 /// R: lm proxy object
       
    49 static int llm_proxy_bless_lua (lua_State *L)
       
    50 {
       
    51 	luaL_argcheck (L, lua_islightuserdata (L, 1), 1, "lm proxy lightuserdata expected");
       
    52 	llm_proxy_bless (L, lua_touserdata (L, 1));
       
    53 	lua_remove (L, -2);
       
    54 	return 1;
       
    55 }
       
    56 
       
    57 /// proxy:type
       
    58 /// Gets or sets proxy server type.
       
    59 /// A: proxy type (optional)
       
    60 /// R: proxy type (when called with no args)
       
    61 static int llm_proxy_kind (lua_State *L)
       
    62 {
       
    63 	llm_proxy_t *proxy = luaL_checklm_proxy (L, 1);
       
    64 	if (lua_gettop (L) > 1) { // Set
       
    65 		lm_proxy_set_type (proxy->proxy, luaL_checkenum (L, 2, llm_proxy_type));
       
    66 		lua_pop (L, 2);
       
    67 		return 0;
       
    68 	} else { // Get
       
    69 		luaL_pushenum (L, lm_proxy_get_type (proxy->proxy), llm_proxy_type);
       
    70 		lua_remove (L, -2);
       
    71 		return 1;
       
    72 	}
       
    73 }
       
    74 
       
    75 /// proxy:server
       
    76 /// Gets or sets proxy server name.
       
    77 /// A: string (optional)
       
    78 /// R: string (when called with no args)
       
    79 static int llm_proxy_server (lua_State *L)
       
    80 {
       
    81 	llm_proxy_t *object = luaL_checklm_proxy (L, 1);
       
    82 	if (lua_gettop (L) > 1) { // Set
       
    83 		lm_proxy_set_server (object->proxy, luaL_checkstring (L, 2));
       
    84 		lua_pop (L, 2);
       
    85 		return 0;
       
    86 	} else { // Get
       
    87 		lua_pushstring (L, lm_proxy_get_server (object->proxy));
       
    88 		lua_remove (L, -2);
       
    89 		return 1;
       
    90 	}
       
    91 }
       
    92 
       
    93 /// proxy:port
       
    94 /// Gets or sets proxy server port.
       
    95 /// A: integer (optional)
       
    96 /// R: integer (when called with no args)
       
    97 static int llm_proxy_port (lua_State *L)
       
    98 {
       
    99 	llm_proxy_t *object = luaL_checklm_proxy (L, 1);
       
   100 	if (lua_gettop (L) > 1) { // Set
       
   101 		lm_proxy_set_port (object->proxy, luaL_checkint (L, 2));
       
   102 		lua_pop (L, 2);
       
   103 		return 0;
       
   104 	} else { // Get
       
   105 		lua_pushnumber (L, lm_proxy_get_port (object->proxy));
       
   106 		lua_remove (L, -2);
       
   107 		return 1;
       
   108 	}
       
   109 }
       
   110 
       
   111 /// proxy:username
       
   112 /// Gets or sets username to authenticate to proxy server with.
       
   113 /// A: string (optional)
       
   114 /// R: string (when called with no args)
       
   115 static int llm_proxy_username (lua_State *L)
       
   116 {
       
   117 	llm_proxy_t *object = luaL_checklm_proxy (L, 1);
       
   118 	if (lua_gettop (L) > 1) { // Set
       
   119 		lm_proxy_set_username (object->proxy, luaL_checkstring (L, 2));
       
   120 		lua_pop (L, 2);
       
   121 		return 0;
       
   122 	} else { // Get
       
   123 		lua_pushstring (L, lm_proxy_get_username (object->proxy));
       
   124 		lua_remove (L, -2);
       
   125 		return 1;
       
   126 	}
       
   127 }
       
   128 
       
   129 /// proxy:password
       
   130 /// Gets or sets password to authenticate to proxy server with.
       
   131 /// A: string (optional)
       
   132 /// R: string (when called with no args)
       
   133 static int llm_proxy_password (lua_State *L)
       
   134 {
       
   135 	llm_proxy_t *object = luaL_checklm_proxy (L, 1);
       
   136 	if (lua_gettop (L) > 1) { // Set
       
   137 		lm_proxy_set_password (object->proxy, luaL_checkstring (L, 2));
       
   138 		lua_pop (L, 2);
       
   139 		return 0;
       
   140 	} else { // Get
       
   141 		lua_pushstring (L, lm_proxy_get_password (object->proxy));
       
   142 		lua_remove (L, -2);
       
   143 		return 1;
       
   144 	}
       
   145 }
       
   146 
       
   147 /// proxy:pointer
       
   148 /// Returns pointer to underlying C structure.
       
   149 /// R: lightuserdata
       
   150 static int llm_proxy_pointer (lua_State *L)
       
   151 {
       
   152 	llm_proxy_t *object = luaL_checklm_proxy (L, 1);
       
   153 	lua_pushlightuserdata (L, object->proxy);
       
   154 	lua_remove (L, -2);
       
   155 	return 1;
       
   156 }
       
   157 
       
   158 static int llm_proxy_gc (lua_State *L)
       
   159 {
       
   160 	llm_proxy_t *object = luaL_checklm_proxy (L, 1);
       
   161 	lm_proxy_unref (object->proxy);
       
   162 	lua_pop (L, 1);
       
   163 	return 0;
       
   164 }
       
   165 
       
   166 static const luaL_Reg llm_proxy_reg_f[] = {
       
   167 	{ "new",   llm_proxy_new       },
       
   168 	{ "bless", llm_proxy_bless_lua },
       
   169 	{ NULL,    NULL                },
       
   170 };
       
   171 
       
   172 static const luaL_Reg llm_proxy_reg_m[] = {
       
   173 	{ "port",     llm_proxy_port     },
       
   174 	{ "server",   llm_proxy_server   },
       
   175 	{ "type",     llm_proxy_kind     },
       
   176 	{ "username", llm_proxy_username },
       
   177 	{ "password", llm_proxy_password },
       
   178 	{ "pointer",  llm_proxy_pointer  },
       
   179 	{ "__gc",     llm_proxy_gc       },
       
   180 	{ NULL,       NULL               },
       
   181 };
       
   182 
       
   183 int luaopen_lm_proxy (lua_State *L)
       
   184 {
       
   185 	luaL_newmetatable (L, "loudmouth.proxy");
       
   186 	lua_pushstring (L, "__index");
       
   187 	lua_pushvalue (L, -2);
       
   188 	lua_settable (L, -3);
       
   189 	luaL_register (L, NULL, llm_proxy_reg_m);
       
   190 	lua_pop (L, 1);
       
   191 	luaL_register (L, "lm.proxy", llm_proxy_reg_f);
       
   192 	return 1;
       
   193 }
       
   194