author | Myhailo Danylenko <isbear@ukrpost.net> |
Sun, 05 Apr 2009 14:55:51 +0300 | |
changeset 17 | ab4470465a0c |
parent 16 | 09b375e9ce32 |
child 19 | d775d7289fe4 |
permissions | -rw-r--r-- |
0 | 1 |
|
2 |
#include <lua.h> |
|
3 |
#include <lauxlib.h> |
|
4 |
#include <glib.h> |
|
5 |
#include <loudmouth/loudmouth.h> |
|
6 |
||
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
7 |
#include "config.h" |
0 | 8 |
#include "util.h" |
9 |
#include "lm_types.h" |
|
10 |
||
11 |
/// lm.proxy |
|
12 |
/// Object, containing information about proxy-server for connection. |
|
13 |
/// Create object, set it's parameters, and then attach to connection |
|
14 |
/// with 'proxy' method. |
|
15 |
||
16 |
/// proxy type |
|
17 |
/// Stirng, specifying proxy-server type. The only type supported for now is http. |
|
4 | 18 |
/// G: |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
19 |
const string2enum_t type_lm_proxy[] = { |
0 | 20 |
{ "http", LM_PROXY_TYPE_HTTP }, |
21 |
{ "none", LM_PROXY_TYPE_NONE }, |
|
22 |
{ NULL, LM_PROXY_TYPE_HTTP }, |
|
23 |
}; |
|
24 |
||
25 |
/// lm.proxy.new |
|
26 |
/// Creates new proxy object. |
|
27 |
/// Note, you should specify either none of args or both. |
|
28 |
/// A: proxy type, string (optional proxy server name), integer (optional server port) |
|
29 |
/// R: lm proxy object |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
30 |
static int new_lm_proxy (lua_State *L) |
0 | 31 |
{ |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
32 |
int type = luaL_checkenum (L, 1, type_lm_proxy); |
0 | 33 |
LmProxy *proxy; |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
34 |
if (lua_gettop (L) > 0) |
0 | 35 |
proxy = lm_proxy_new_with_server (type, luaL_checkstring (L, 2), luaL_checkint (L, 3)); |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
36 |
else |
0 | 37 |
proxy = lm_proxy_new (type); |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
38 |
bless_lm_proxy (L, proxy); |
0 | 39 |
lm_proxy_unref (proxy); // XXX |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
40 |
D ("Proxy %X created", (int) proxy); |
0 | 41 |
return 1; |
42 |
} |
|
43 |
||
44 |
/// lm.proxy.bless |
|
45 |
/// Blesses given pointer to lm proxy object. |
|
46 |
/// A: lightuserdata (C lm proxy object) |
|
47 |
/// R: lm proxy object |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
48 |
static int bless_lua_lm_proxy (lua_State *L) |
0 | 49 |
{ |
50 |
luaL_argcheck (L, lua_islightuserdata (L, 1), 1, "lm proxy lightuserdata expected"); |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
51 |
bless_lm_proxy (L, lua_touserdata (L, 1)); |
0 | 52 |
return 1; |
53 |
} |
|
54 |
||
55 |
/// proxy:type |
|
56 |
/// Gets or sets proxy server type. |
|
57 |
/// A: proxy type (optional) |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
58 |
/// R: proxy type (when called with no args) or lm proxy object |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
59 |
static int kind_lm_proxy (lua_State *L) |
0 | 60 |
{ |
61 |
llm_proxy_t *proxy = luaL_checklm_proxy (L, 1); |
|
62 |
if (lua_gettop (L) > 1) { // Set |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
63 |
lm_proxy_set_type (proxy->proxy, luaL_checkenum (L, 2, type_lm_proxy)); |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
64 |
lua_pop (L, 1); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
65 |
} else // Get |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
66 |
luaL_pushenum (L, lm_proxy_get_type (proxy->proxy), type_lm_proxy); |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
67 |
return 1; |
0 | 68 |
} |
69 |
||
70 |
/// proxy:server |
|
71 |
/// Gets or sets proxy server name. |
|
72 |
/// A: string (optional) |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
73 |
/// R: string (when called with no args) or lm proxy object |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
74 |
static int server_lm_proxy (lua_State *L) |
0 | 75 |
{ |
76 |
llm_proxy_t *object = luaL_checklm_proxy (L, 1); |
|
77 |
if (lua_gettop (L) > 1) { // Set |
|
78 |
lm_proxy_set_server (object->proxy, luaL_checkstring (L, 2)); |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
79 |
lua_pop (L, 1); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
80 |
} else // Get |
0 | 81 |
lua_pushstring (L, lm_proxy_get_server (object->proxy)); |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
82 |
return 1; |
0 | 83 |
} |
84 |
||
85 |
/// proxy:port |
|
86 |
/// Gets or sets proxy server port. |
|
87 |
/// A: integer (optional) |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
88 |
/// R: integer (when called with no args) or lm proxy object |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
89 |
static int port_lm_proxy (lua_State *L) |
0 | 90 |
{ |
91 |
llm_proxy_t *object = luaL_checklm_proxy (L, 1); |
|
92 |
if (lua_gettop (L) > 1) { // Set |
|
93 |
lm_proxy_set_port (object->proxy, luaL_checkint (L, 2)); |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
94 |
lua_pop (L, 1); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
95 |
} else // Get |
0 | 96 |
lua_pushnumber (L, lm_proxy_get_port (object->proxy)); |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
97 |
return 1; |
0 | 98 |
} |
99 |
||
100 |
/// proxy:username |
|
101 |
/// Gets or sets username to authenticate to proxy server with. |
|
102 |
/// A: string (optional) |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
103 |
/// R: string (when called with no args) or lm proxy object |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
104 |
static int username_lm_proxy (lua_State *L) |
0 | 105 |
{ |
106 |
llm_proxy_t *object = luaL_checklm_proxy (L, 1); |
|
107 |
if (lua_gettop (L) > 1) { // Set |
|
108 |
lm_proxy_set_username (object->proxy, luaL_checkstring (L, 2)); |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
109 |
lua_pop (L, 1); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
110 |
} else // Get |
0 | 111 |
lua_pushstring (L, lm_proxy_get_username (object->proxy)); |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
112 |
return 1; |
0 | 113 |
} |
114 |
||
115 |
/// proxy:password |
|
116 |
/// Gets or sets password to authenticate to proxy server with. |
|
117 |
/// A: string (optional) |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
118 |
/// R: string (when called with no args) or lm proxy object |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
119 |
static int password_lm_proxy (lua_State *L) |
0 | 120 |
{ |
121 |
llm_proxy_t *object = luaL_checklm_proxy (L, 1); |
|
122 |
if (lua_gettop (L) > 1) { // Set |
|
123 |
lm_proxy_set_password (object->proxy, luaL_checkstring (L, 2)); |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
124 |
lua_pop (L, 1); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
125 |
} else // Get |
0 | 126 |
lua_pushstring (L, lm_proxy_get_password (object->proxy)); |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
127 |
return 1; |
0 | 128 |
} |
129 |
||
130 |
/// proxy:pointer |
|
131 |
/// Returns pointer to underlying C structure. |
|
132 |
/// R: lightuserdata |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
133 |
static int pointer_lm_proxy (lua_State *L) |
0 | 134 |
{ |
135 |
llm_proxy_t *object = luaL_checklm_proxy (L, 1); |
|
136 |
lua_pushlightuserdata (L, object->proxy); |
|
137 |
return 1; |
|
138 |
} |
|
139 |
||
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
140 |
static int gc_lm_proxy (lua_State *L) |
0 | 141 |
{ |
142 |
llm_proxy_t *object = luaL_checklm_proxy (L, 1); |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
143 |
D ("Proxy %X gc called", (int) object); |
0 | 144 |
lm_proxy_unref (object->proxy); |
145 |
return 0; |
|
146 |
} |
|
147 |
||
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
148 |
static const luaL_Reg reg_f_lm_proxy[] = { |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
149 |
{ "new", new_lm_proxy }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
150 |
{ "bless", bless_lua_lm_proxy }, |
12 | 151 |
{ NULL, NULL }, |
0 | 152 |
}; |
153 |
||
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
154 |
static const luaL_Reg reg_m_lm_proxy[] = { |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
155 |
{ "port", port_lm_proxy }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
156 |
{ "server", server_lm_proxy }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
157 |
{ "type", kind_lm_proxy }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
158 |
{ "username", username_lm_proxy }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
159 |
{ "password", password_lm_proxy }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
160 |
{ "pointer", pointer_lm_proxy }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
161 |
{ "__gc", gc_lm_proxy }, |
12 | 162 |
{ NULL, NULL }, |
0 | 163 |
}; |
164 |
||
165 |
int luaopen_lm_proxy (lua_State *L) |
|
166 |
{ |
|
167 |
luaL_newmetatable (L, "loudmouth.proxy"); |
|
168 |
lua_pushstring (L, "__index"); |
|
169 |
lua_pushvalue (L, -2); |
|
170 |
lua_settable (L, -3); |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
171 |
luaL_register (L, NULL, reg_m_lm_proxy); |
0 | 172 |
lua_pop (L, 1); |
16
09b375e9ce32
Switch to new module organization scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
173 |
lua_newtable (L); // XXX we can specify here exact amount of fields |
09b375e9ce32
Switch to new module organization scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
174 |
luaL_register (L, NULL, reg_f_lm_proxy); |
0 | 175 |
return 1; |
176 |
} |
|
177 |