author | Myhailo Danylenko <isbear@ukrpost.net> |
Wed, 20 May 2009 00:15:21 +0300 | |
changeset 25 | 1ce483608721 |
parent 23 | 13f03e604c8a |
child 38 | 34a2b880615c |
permissions | -rw-r--r-- |
23 | 1 |
|
2 |
/* Copyright 2009 Myhailo Danylenko |
|
3 |
||
4 |
This file is part of lua-lm. |
|
5 |
||
6 |
lua-lm is free software: you can redistribute it and/or modify |
|
7 |
it under the terms of the GNU General Public License as published by |
|
8 |
the Free Software Foundation, either version 2 of the License, or |
|
9 |
(at your option) any later version. |
|
10 |
||
11 |
This program is distributed in the hope that it will be useful, |
|
12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
GNU General Public License for more details. |
|
15 |
||
16 |
You should have received a copy of the GNU General Public License |
|
17 |
along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
|
0 | 18 |
|
19 |
#include <lua.h> |
|
20 |
#include <lauxlib.h> |
|
21 |
#include <glib.h> |
|
22 |
#include <loudmouth/loudmouth.h> |
|
23 |
||
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
24 |
#include "config.h" |
0 | 25 |
#include "util.h" |
26 |
#include "lm_types.h" |
|
27 |
||
28 |
/// lm.proxy |
|
29 |
/// Object, containing information about proxy-server for connection. |
|
30 |
/// Create object, set it's parameters, and then attach to connection |
|
31 |
/// with 'proxy' method. |
|
32 |
||
33 |
/// proxy type |
|
34 |
/// Stirng, specifying proxy-server type. The only type supported for now is http. |
|
4 | 35 |
/// G: |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
36 |
const string2enum_t type_lm_proxy[] = { |
0 | 37 |
{ "http", LM_PROXY_TYPE_HTTP }, |
38 |
{ "none", LM_PROXY_TYPE_NONE }, |
|
39 |
{ NULL, LM_PROXY_TYPE_HTTP }, |
|
40 |
}; |
|
41 |
||
42 |
/// lm.proxy.new |
|
43 |
/// Creates new proxy object. |
|
44 |
/// Note, you should specify either none of args or both. |
|
45 |
/// A: proxy type, string (optional proxy server name), integer (optional server port) |
|
46 |
/// R: lm proxy object |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
47 |
static int new_lm_proxy (lua_State *L) |
0 | 48 |
{ |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
49 |
int type = luaL_checkenum (L, 1, type_lm_proxy); |
0 | 50 |
LmProxy *proxy; |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
51 |
if (lua_gettop (L) > 0) |
0 | 52 |
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
|
53 |
else |
0 | 54 |
proxy = lm_proxy_new (type); |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
55 |
bless_lm_proxy (L, proxy); |
0 | 56 |
lm_proxy_unref (proxy); // XXX |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
57 |
D ("Proxy %X created", (int) proxy); |
0 | 58 |
return 1; |
59 |
} |
|
60 |
||
61 |
/// lm.proxy.bless |
|
62 |
/// Blesses given pointer to lm proxy object. |
|
63 |
/// A: lightuserdata (C lm proxy object) |
|
64 |
/// R: lm proxy object |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
65 |
static int bless_lua_lm_proxy (lua_State *L) |
0 | 66 |
{ |
67 |
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
|
68 |
bless_lm_proxy (L, lua_touserdata (L, 1)); |
0 | 69 |
return 1; |
70 |
} |
|
71 |
||
72 |
/// proxy:type |
|
73 |
/// Gets or sets proxy server type. |
|
74 |
/// A: proxy type (optional) |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
75 |
/// 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
|
76 |
static int kind_lm_proxy (lua_State *L) |
0 | 77 |
{ |
78 |
llm_proxy_t *proxy = luaL_checklm_proxy (L, 1); |
|
79 |
if (lua_gettop (L) > 1) { // Set |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
80 |
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
|
81 |
lua_pop (L, 1); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
82 |
} else // Get |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
83 |
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
|
84 |
return 1; |
0 | 85 |
} |
86 |
||
87 |
/// proxy:server |
|
88 |
/// Gets or sets proxy server name. |
|
89 |
/// A: string (optional) |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
90 |
/// 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
|
91 |
static int server_lm_proxy (lua_State *L) |
0 | 92 |
{ |
93 |
llm_proxy_t *object = luaL_checklm_proxy (L, 1); |
|
94 |
if (lua_gettop (L) > 1) { // Set |
|
95 |
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
|
96 |
lua_pop (L, 1); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
97 |
} else // Get |
0 | 98 |
lua_pushstring (L, lm_proxy_get_server (object->proxy)); |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
99 |
return 1; |
0 | 100 |
} |
101 |
||
102 |
/// proxy:port |
|
103 |
/// Gets or sets proxy server port. |
|
104 |
/// A: integer (optional) |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
105 |
/// 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
|
106 |
static int port_lm_proxy (lua_State *L) |
0 | 107 |
{ |
108 |
llm_proxy_t *object = luaL_checklm_proxy (L, 1); |
|
109 |
if (lua_gettop (L) > 1) { // Set |
|
110 |
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
|
111 |
lua_pop (L, 1); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
112 |
} else // Get |
0 | 113 |
lua_pushnumber (L, lm_proxy_get_port (object->proxy)); |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
114 |
return 1; |
0 | 115 |
} |
116 |
||
117 |
/// proxy:username |
|
118 |
/// Gets or sets username to authenticate to proxy server with. |
|
119 |
/// A: string (optional) |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
120 |
/// 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
|
121 |
static int username_lm_proxy (lua_State *L) |
0 | 122 |
{ |
123 |
llm_proxy_t *object = luaL_checklm_proxy (L, 1); |
|
124 |
if (lua_gettop (L) > 1) { // Set |
|
125 |
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
|
126 |
lua_pop (L, 1); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
127 |
} else // Get |
0 | 128 |
lua_pushstring (L, lm_proxy_get_username (object->proxy)); |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
129 |
return 1; |
0 | 130 |
} |
131 |
||
132 |
/// proxy:password |
|
133 |
/// Gets or sets password to authenticate to proxy server with. |
|
134 |
/// A: string (optional) |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
135 |
/// 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
|
136 |
static int password_lm_proxy (lua_State *L) |
0 | 137 |
{ |
138 |
llm_proxy_t *object = luaL_checklm_proxy (L, 1); |
|
139 |
if (lua_gettop (L) > 1) { // Set |
|
140 |
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
|
141 |
lua_pop (L, 1); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
142 |
} else // Get |
0 | 143 |
lua_pushstring (L, lm_proxy_get_password (object->proxy)); |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
144 |
return 1; |
0 | 145 |
} |
146 |
||
147 |
/// proxy:pointer |
|
148 |
/// Returns pointer to underlying C structure. |
|
149 |
/// R: lightuserdata |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
150 |
static int pointer_lm_proxy (lua_State *L) |
0 | 151 |
{ |
152 |
llm_proxy_t *object = luaL_checklm_proxy (L, 1); |
|
153 |
lua_pushlightuserdata (L, object->proxy); |
|
154 |
return 1; |
|
155 |
} |
|
156 |
||
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
157 |
static int gc_lm_proxy (lua_State *L) |
0 | 158 |
{ |
159 |
llm_proxy_t *object = luaL_checklm_proxy (L, 1); |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
160 |
D ("Proxy %X gc called", (int) object); |
0 | 161 |
lm_proxy_unref (object->proxy); |
162 |
return 0; |
|
163 |
} |
|
164 |
||
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
165 |
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
|
166 |
{ "new", new_lm_proxy }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
167 |
{ "bless", bless_lua_lm_proxy }, |
12 | 168 |
{ NULL, NULL }, |
0 | 169 |
}; |
170 |
||
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
171 |
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
|
172 |
{ "port", port_lm_proxy }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
173 |
{ "server", server_lm_proxy }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
174 |
{ "type", kind_lm_proxy }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
175 |
{ "username", username_lm_proxy }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
176 |
{ "password", password_lm_proxy }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
177 |
{ "pointer", pointer_lm_proxy }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
178 |
{ "__gc", gc_lm_proxy }, |
12 | 179 |
{ NULL, NULL }, |
0 | 180 |
}; |
181 |
||
182 |
int luaopen_lm_proxy (lua_State *L) |
|
183 |
{ |
|
184 |
luaL_newmetatable (L, "loudmouth.proxy"); |
|
19
d775d7289fe4
Use lua_pushliteral and lua_setfield
Myhailo Danylenko <isbear@ukrpost.net>
parents:
16
diff
changeset
|
185 |
lua_pushvalue (L, -1); |
d775d7289fe4
Use lua_pushliteral and lua_setfield
Myhailo Danylenko <isbear@ukrpost.net>
parents:
16
diff
changeset
|
186 |
lua_setfield (L, -2, "__index"); |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
187 |
luaL_register (L, NULL, reg_m_lm_proxy); |
0 | 188 |
lua_pop (L, 1); |
16
09b375e9ce32
Switch to new module organization scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
189 |
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
|
190 |
luaL_register (L, NULL, reg_f_lm_proxy); |
0 | 191 |
return 1; |
192 |
} |
|
193 |