author | Myhailo Danylenko <isbear@ukrpost.net> |
Mon, 27 Apr 2009 13:15:23 +0300 | |
changeset 19 | d775d7289fe4 |
parent 16 | 09b375e9ce32 |
child 23 | 13f03e604c8a |
permissions | -rw-r--r-- |
0 | 1 |
|
2 |
#include <lua.h> |
|
3 |
#include <lauxlib.h> |
|
4 |
#include <glib.h> // GDestroyNotify, GMainContext |
|
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 |
#include "lm_message.h" |
|
11 |
#include "lm_message_handler.h" |
|
12 |
||
13 |
/// lm.connection |
|
14 |
/// Central module, representing connection to the server. |
|
15 |
/// You should create a new connection object, then open it (establish |
|
16 |
/// connection), then authenticate to the server. |
|
17 |
||
18 |
/// connection state |
|
19 |
/// Stirng, representing current connection state. |
|
4 | 20 |
/// G: |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
21 |
const string2enum_t state_lm_connection[] = { |
0 | 22 |
{ "closed", LM_CONNECTION_STATE_CLOSED }, |
23 |
{ "opening", LM_CONNECTION_STATE_OPENING }, |
|
24 |
{ "open", LM_CONNECTION_STATE_OPEN }, |
|
25 |
{ "authenticating", LM_CONNECTION_STATE_AUTHENTICATING }, |
|
26 |
{ "authenticated", LM_CONNECTION_STATE_AUTHENTICATED }, |
|
27 |
{ NULL, 0 }, // XXX |
|
28 |
}; |
|
29 |
||
30 |
/// handler priority |
|
31 |
/// String, according to which handler will be placed into one |
|
32 |
/// of three handler groups. |
|
4 | 33 |
/// G: |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
34 |
const string2enum_t priority_lm_handler[] = { |
0 | 35 |
{ "last", LM_HANDLER_PRIORITY_LAST }, |
36 |
{ "normal", LM_HANDLER_PRIORITY_NORMAL }, |
|
37 |
{ "first", LM_HANDLER_PRIORITY_FIRST }, |
|
38 |
{ NULL, 0 }, // XXX |
|
39 |
}; |
|
40 |
||
41 |
/// disconnect reason |
|
42 |
/// String, indicating the reason of disconnection occured. |
|
4 | 43 |
/// G: |
44 |
static const string2enum_t llm_disconnect_reason[] = { |
|
0 | 45 |
{ "ok", LM_DISCONNECT_REASON_OK }, |
46 |
{ "ping time out", LM_DISCONNECT_REASON_PING_TIME_OUT }, |
|
47 |
{ "hup", LM_DISCONNECT_REASON_HUP }, |
|
48 |
{ "error", LM_DISCONNECT_REASON_ERROR }, |
|
49 |
{ "resource conflict", LM_DISCONNECT_REASON_RESOURCE_CONFLICT }, |
|
50 |
{ "invalid xml", LM_DISCONNECT_REASON_INVALID_XML }, |
|
51 |
{ "unknown", LM_DISCONNECT_REASON_UNKNOWN }, |
|
52 |
{ "ping_time_out", LM_DISCONNECT_REASON_PING_TIME_OUT }, |
|
53 |
{ "resource_conflict", LM_DISCONNECT_REASON_RESOURCE_CONFLICT }, |
|
54 |
{ "invalid_xml", LM_DISCONNECT_REASON_INVALID_XML }, |
|
55 |
{ NULL, 0 }, // XXX |
|
56 |
}; |
|
57 |
||
58 |
/// lm.connection.new |
|
59 |
/// Creates a new connection (closed). |
|
60 |
/// A: string (server name), lightuserdata (C glib main context object, optional) |
|
61 |
/// R: lm connection object |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
62 |
static int new_lm_connection (lua_State *L) |
0 | 63 |
{ |
64 |
const char *server = luaL_checkstring (L, 1); |
|
65 |
LmConnection *connection; |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
66 |
if (lua_gettop (L) < 2) |
0 | 67 |
connection = lm_connection_new (server); |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
68 |
else { |
0 | 69 |
luaL_argcheck (L, lua_islightuserdata (L, 2), 2, "glib main context lightuserdata expected"); |
70 |
connection = lm_connection_new_with_context (server, (GMainContext *) lua_touserdata (L, 2)); |
|
71 |
} |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
72 |
bless_lm_connection (L, connection); |
0 | 73 |
lm_connection_unref (connection); |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
74 |
D ("Connection %X created", (int) connection); |
0 | 75 |
return 1; |
76 |
} |
|
77 |
||
78 |
/// lm.connection.bless |
|
79 |
/// Blesses given pointer to lm connection object. |
|
80 |
/// Note: it adds a reference to connection. |
|
81 |
/// A: lightuserdata (C lm connection object) |
|
82 |
/// R: lm connection object |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
83 |
static int bless_lua_lm_connection (lua_State *L) |
0 | 84 |
{ |
85 |
luaL_argcheck (L, lua_islightuserdata (L, 1), 1, "loudmouth connection lightuserdata expected"); |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
86 |
bless_lm_connection (L, (LmConnection *) lua_touserdata (L, 1)); |
0 | 87 |
return 1; |
88 |
} |
|
89 |
||
90 |
/// connection callback function |
|
91 |
/// User function, that will be called on connection establishment operation end, |
|
92 |
/// eg. successful/unsuccessful opening or authentication. |
|
93 |
/// A: lm connection object, boolean (success) |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
94 |
static void callback_lm_connection (LmConnection *connection, int success, llm_callback_t *cb) |
0 | 95 |
{ |
96 |
lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->reference); |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
97 |
bless_lm_connection (cb->L, connection); |
0 | 98 |
// XXX lm_connection_unref (connection); |
99 |
lua_pushboolean (cb->L, success); |
|
100 |
if (lua_pcall (cb->L, 2, 0, 0)) { |
|
9
50f55d494efb
Not use error log level, it is fatal
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
101 |
W ("Connection callback error: %s", lua_tostring (cb->L, -1)); |
0 | 102 |
lua_pop (cb->L, 1); |
103 |
} |
|
104 |
} |
|
105 |
||
106 |
/// connection:open |
|
107 |
/// Opens connection to the server and then calls callback function. |
|
108 |
/// A: connection callback function |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
109 |
/// R: lm connection object or nil, string (error message) |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
110 |
static int open_lm_connection (lua_State *L) |
0 | 111 |
{ |
112 |
llm_connection_t *object = luaL_checklm_connection (L, 1); |
|
113 |
llm_callback_t *cb; |
|
114 |
luaL_argcheck (L, lua_isfunction (L, 2), 2, "function expected"); |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
115 |
GError *err; |
0 | 116 |
|
117 |
cb = luaL_malloc (L, sizeof (llm_callback_t)); |
|
118 |
cb->reference = luaL_ref (L, LUA_REGISTRYINDEX); |
|
119 |
cb->L = L; |
|
120 |
||
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
121 |
if (lm_connection_open (object->connection, (LmResultFunction) callback_lm_connection, |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
122 |
cb, (GDestroyNotify) llm_callback_destroy, &err)) |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
123 |
return 1; |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
124 |
else { |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
125 |
lua_pushnil (L); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
126 |
lua_pushstring (L, err->message); |
9
50f55d494efb
Not use error log level, it is fatal
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
127 |
I ("Connection opening failed"); |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
128 |
return 2; |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
129 |
} |
0 | 130 |
} |
131 |
||
132 |
/// connection:authenticate |
|
133 |
/// Tries to authenticate against opened connection, then calls callback function. |
|
134 |
/// A: string (username), string (password), string (resource), connection callback function |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
135 |
/// R: lm connection object or nil, string (error message) |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
136 |
static int authenticate_lm_connection (lua_State *L) |
0 | 137 |
{ |
138 |
llm_connection_t *object = luaL_checklm_connection (L, 1); |
|
139 |
const char *username = luaL_checkstring (L, 2); |
|
140 |
const char *password = luaL_checkstring (L, 3); |
|
141 |
const char *resource = luaL_checkstring (L, 4); |
|
142 |
llm_callback_t *cb; |
|
143 |
int status; |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
144 |
GError *err = NULL; |
0 | 145 |
luaL_argcheck (L, lua_isfunction (L, 5), 5, "function expected"); |
146 |
||
147 |
cb = luaL_malloc (L, sizeof (llm_callback_t)); |
|
148 |
cb->reference = luaL_ref (L, LUA_REGISTRYINDEX); |
|
149 |
cb->L = L; |
|
150 |
||
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
151 |
if (lm_connection_authenticate (object->connection, username, password, resource, |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
152 |
(LmResultFunction) callback_lm_connection, cb, |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
153 |
(GDestroyNotify) llm_callback_destroy, &err)) { |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
154 |
lua_pop (L, 3); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
155 |
return 1; |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
156 |
} else { |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
157 |
lua_pushnil (L); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
158 |
lua_pushstring (L, err->message); |
9
50f55d494efb
Not use error log level, it is fatal
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
159 |
I ("Connection authentication failed: %s", err->message); |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
160 |
return 2; |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
161 |
} |
0 | 162 |
} |
163 |
||
164 |
/// connection:port |
|
165 |
/// Gets or sets server port to connect. |
|
166 |
/// A: integer (optional) |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
167 |
/// R: integer (when called with no args) or lm connection object |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
168 |
static int port_lm_connection (lua_State *L) |
0 | 169 |
{ |
170 |
llm_connection_t *object = luaL_checklm_connection (L, 1); |
|
171 |
if (lua_gettop (L) > 1) { // Set |
|
172 |
lm_connection_set_port (object->connection, luaL_checkint (L, 2)); |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
173 |
lua_pop (L, 1); |
0 | 174 |
} else { // Get |
175 |
lua_pushnumber (L, lm_connection_get_port (object->connection)); |
|
176 |
} |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
177 |
return 1; |
0 | 178 |
} |
179 |
||
180 |
/// connection:server |
|
181 |
/// Gets or sets server to connect to. |
|
182 |
/// A: string (optional, server name) |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
183 |
/// R: string (when called with no args) or lm connection object |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
184 |
static int server_lm_connection (lua_State *L) |
0 | 185 |
{ |
186 |
llm_connection_t *object = luaL_checklm_connection (L, 1); |
|
187 |
if (lua_gettop (L) > 1) { // Set |
|
188 |
lm_connection_set_server (object->connection, luaL_checkstring (L, 2)); |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
189 |
lua_pop (L, 1); |
0 | 190 |
} else { // Get |
191 |
lua_pushstring (L, lm_connection_get_server (object->connection)); |
|
192 |
} |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
193 |
return 1; |
0 | 194 |
} |
195 |
||
196 |
/// connection:jid |
|
197 |
/// Gets or sets jid for connection. |
|
198 |
/// A: string (optional) |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
199 |
/// R: string (when called with no args) or lm connection object |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
200 |
static int jid_lm_connection (lua_State *L) |
0 | 201 |
{ |
202 |
llm_connection_t *object = luaL_checklm_connection (L, 1); |
|
203 |
if (lua_gettop (L) > 1) { // Set |
|
204 |
lm_connection_set_jid (object->connection, luaL_checkstring (L, 2)); |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
205 |
lua_pop (L, 1); |
0 | 206 |
} else { // Get |
207 |
lua_pushstring (L, lm_connection_get_jid (object->connection)); |
|
208 |
} |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
209 |
return 1; |
0 | 210 |
} |
211 |
||
212 |
/// connection:keep_alive_rate |
|
213 |
/// Gets or sets keep alive packets rate for connection. |
|
214 |
/// Note, that on some platforms there is no get function even in |
|
215 |
/// loudmouth versions, that should have it according to documentation. |
|
216 |
/// integer (optional, seconds) |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
217 |
/// integer (when called with no args) or lm connection object or nil, string (error message, when get function is not available in loudmouth) |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
218 |
static int keep_alive_rate_lm_connection (lua_State *L) |
0 | 219 |
{ |
220 |
llm_connection_t *object = luaL_checklm_connection (L, 1); |
|
221 |
if (lua_gettop (L) > 1) { // Set |
|
222 |
lm_connection_set_keep_alive_rate (object->connection, luaL_checkint (L, 2)); |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
223 |
lua_pop (L, 1); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
224 |
return 1; |
0 | 225 |
} else { // Get |
226 |
#ifdef HAVE_LM_CONNECTION_GET_KEEP_ALIVE_RATE |
|
227 |
lua_pushnumber (L, lm_connection_get_keep_alive_rate (object->connection)); |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
228 |
return 1; |
0 | 229 |
#else |
9
50f55d494efb
Not use error log level, it is fatal
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
230 |
W ("Sorry, your loudmouth have no get_keep_alive_rate ()"); |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
231 |
lua_pushnil (L); |
19
d775d7289fe4
Use lua_pushliteral and lua_setfield
Myhailo Danylenko <isbear@ukrpost.net>
parents:
16
diff
changeset
|
232 |
lua_pushliteral (L, "Sorry, your loudmouth have no get_keep_alive_rate ()"); |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
233 |
return 2; |
0 | 234 |
#endif |
235 |
} |
|
236 |
} |
|
237 |
||
238 |
/// connection:proxy |
|
239 |
/// Gets or sets proxy server for connection. |
|
240 |
/// A: lm proxy object (optional) |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
241 |
/// R: lm proxy object or nil (when called with no args) or lm connection object |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
242 |
static int proxy_lm_connection (lua_State *L) |
0 | 243 |
{ |
244 |
llm_connection_t *object = luaL_checklm_connection (L, 1); |
|
245 |
if (lua_gettop (L) > 1) { // Set |
|
246 |
llm_proxy_t *proxy = luaL_checklm_proxy (L, 2); |
|
247 |
lm_connection_set_proxy (object->connection, proxy->proxy); |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
248 |
lua_pop (L, 1); |
0 | 249 |
} else { // Get |
250 |
LmProxy *proxy = lm_connection_get_proxy (object->connection); |
|
251 |
lua_pop (L, 1); |
|
252 |
if (proxy) { |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
253 |
bless_lm_proxy (L, proxy); |
0 | 254 |
// XXX lm_proxy_unref (proxy); |
255 |
} else |
|
256 |
lua_pushnil (L); |
|
257 |
} |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
258 |
return 1; |
0 | 259 |
} |
260 |
||
261 |
/// connection:ssl |
|
262 |
/// Gets or sets ssl object for connection. |
|
263 |
/// A: lm ssl object (optional) |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
264 |
/// R: lm ssl object or nil (when called with no args) or lm connection object |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
265 |
static int ssl_lm_connection (lua_State *L) |
0 | 266 |
{ |
267 |
llm_connection_t *object = luaL_checklm_connection (L, 1); |
|
268 |
if (lua_gettop (L) > 1) { // Set |
|
269 |
llm_ssl_t *ssl = luaL_checklm_ssl (L, 2); |
|
270 |
lm_connection_set_ssl (object->connection, ssl->ssl); |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
271 |
lua_pop (L, 1); |
0 | 272 |
} else { // Get |
273 |
LmSSL *ssl = lm_connection_get_ssl (object->connection); |
|
274 |
lua_pop (L, 1); |
|
275 |
if (ssl) { |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
276 |
bless_lm_ssl (L, ssl); |
0 | 277 |
// XXX lm_ssl_unref (ssl); |
278 |
} else |
|
279 |
lua_pushnil (L); |
|
280 |
} |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
281 |
return 1; |
0 | 282 |
} |
283 |
||
284 |
/// connection:close |
|
285 |
/// Close connection. |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
286 |
/// R: lm connection object or nil, string (error message) |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
287 |
static int close_lm_connection (lua_State *L) |
0 | 288 |
{ |
289 |
llm_connection_t *object = luaL_checklm_connection (L, 1); |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
290 |
GError *err = NULL; |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
291 |
if (lm_connection_close (object->connection, &err)) |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
292 |
return 1; |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
293 |
else { |
9
50f55d494efb
Not use error log level, it is fatal
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
294 |
I ("Connection close failed: %s", err->message); |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
295 |
lua_pushnil (L); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
296 |
lua_pushstring (L, err->message); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
297 |
return 2; |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
298 |
} |
0 | 299 |
} |
300 |
||
301 |
/// connection:status |
|
302 |
/// Returns string, describing connection state. |
|
303 |
/// R: connection state |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
304 |
static int status_lm_connection (lua_State *L) |
0 | 305 |
{ |
306 |
llm_connection_t *connection = luaL_checklm_connection (L, 1); |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
307 |
luaL_pushenum (L, lm_connection_get_state (connection->connection), state_lm_connection); |
0 | 308 |
return 1; |
309 |
} |
|
310 |
||
311 |
/// connection:send |
|
10
aed141accdd9
Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
312 |
/// Sends message. If specified, handler function will be called upon receiving of response to this message. |
aed141accdd9
Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
313 |
/// Handler function can be either a message handler object or just a message handler function. |
aed141accdd9
Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
314 |
/// Message can be raw xml string. However, you cannot use it with handler function. |
aed141accdd9
Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
315 |
/// In short: |
aed141accdd9
Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
316 |
/// * connection, errmsg = connection:send ( "raw xml" ) |
aed141accdd9
Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
317 |
/// * connection, errmsg = connection:send ( message ) |
aed141accdd9
Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
318 |
/// * connection, errmsg = connection:send ( message, function ( connection, message ) end ) |
aed141accdd9
Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
319 |
/// * connection, errmsg = connection:send ( message, handler ) |
aed141accdd9
Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
320 |
/// If connection is nil, errmsg contains error message. |
aed141accdd9
Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
321 |
/// A: lm message object/string, message handler callback function/lm message handler object (optional) |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
322 |
/// R: lm connection object or nil, string (error message) |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
323 |
static int send_lm_connection (lua_State *L) |
0 | 324 |
{ |
325 |
llm_connection_t *object = luaL_checklm_connection (L, 1); |
|
326 |
int status; |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
327 |
GError *err = NULL; |
10
aed141accdd9
Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
328 |
if (lua_gettop (L) < 3) { // Send |
aed141accdd9
Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
329 |
if (lua_type (L, 2) == LUA_TSTRING) |
aed141accdd9
Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
330 |
status = lm_connection_send_raw (object->connection, lua_tostring (L, 2), &err); |
aed141accdd9
Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
331 |
else { |
aed141accdd9
Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
332 |
llm_message_t *message = luaL_checklm_message (L, 2); |
aed141accdd9
Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
333 |
status = lm_connection_send (object->connection, message->message, &err); |
aed141accdd9
Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
334 |
} |
aed141accdd9
Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
335 |
} else if (lua_isfunction (L, 3)) { // Send w/reply, func |
aed141accdd9
Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
336 |
llm_message_t *message = luaL_checklm_message (L, 2); |
aed141accdd9
Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
337 |
llm_callback_t *cb = luaL_malloc (L, sizeof (llm_callback_t)); |
0 | 338 |
LmMessageHandler *handler; |
339 |
||
340 |
cb->reference = luaL_ref (L, LUA_REGISTRYINDEX); |
|
10
aed141accdd9
Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
341 |
cb->L = L; |
0 | 342 |
handler = lm_message_handler_new ( |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
343 |
(LmHandleMessageFunction)callback_lm_handler, |
0 | 344 |
cb, (GDestroyNotify)llm_callback_destroy); |
345 |
status = lm_connection_send_with_reply (object->connection, |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
346 |
message->message, handler, &err); |
0 | 347 |
lm_message_handler_unref (handler); |
348 |
} else { // Send w/reply, handler |
|
10
aed141accdd9
Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
349 |
llm_message_t *message = luaL_checklm_message (L, 2); |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
350 |
llm_handler_t *handler = luaL_checklm_handler (L, 3); |
0 | 351 |
status = lm_connection_send_with_reply (object->connection, message->message, |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
352 |
handler->handler, &err); |
0 | 353 |
lua_pop (L, 1); |
354 |
}; |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
355 |
lua_pop (L, 1); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
356 |
if (status) |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
357 |
return 1; |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
358 |
else { |
9
50f55d494efb
Not use error log level, it is fatal
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
359 |
I ("Message sending failed: %s", err->message); |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
360 |
lua_pushnil (L); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
361 |
lua_pushstring (L, err->message); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
362 |
return 2; |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
363 |
} |
0 | 364 |
} |
365 |
||
366 |
/// connection:handler |
|
367 |
/// Registers or unregisters handler function for a given type of messages. |
|
368 |
/// To unregister handler, omit the priority argument. |
|
369 |
/// Handler function can be specified as plain function or message handler object. |
|
370 |
/// Though, you can unregister only a message handler object. |
|
371 |
/// A: message handler callback function or lm message handler object, message type, handler priority (optional) |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
372 |
/// R: lm connection object |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
373 |
static int handler_lm_connection (lua_State *L) |
0 | 374 |
{ |
375 |
llm_connection_t *object = luaL_checklm_connection (L, 1); |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
376 |
int type = luaL_checkenum (L, 3, type_lm_message); |
0 | 377 |
|
378 |
if (lua_gettop (L) > 3) { // Register |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
379 |
int priority = luaL_checkenum (L, 4, priority_lm_handler); |
0 | 380 |
|
381 |
if (lua_isfunction (L, 2)) { // Function |
|
382 |
LmMessageHandler *handler; |
|
383 |
llm_callback_t *cb = luaL_malloc (L, sizeof (llm_callback_t)); |
|
384 |
lua_pushvalue (L, 2); |
|
385 |
cb->reference = luaL_ref (L, LUA_REGISTRYINDEX); |
|
386 |
cb->L = L; |
|
387 |
||
388 |
handler = lm_message_handler_new ( |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
389 |
(LmHandleMessageFunction)callback_lm_handler, |
0 | 390 |
cb, (GDestroyNotify)llm_callback_destroy); |
391 |
lm_connection_register_message_handler (object->connection, |
|
392 |
handler, type, priority); |
|
393 |
lm_message_handler_unref (handler); |
|
394 |
} else { // Object |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
395 |
llm_handler_t *handler = luaL_checklm_handler (L, 2); |
0 | 396 |
lm_connection_register_message_handler (object->connection, |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
397 |
handler->handler, |
0 | 398 |
type, priority); |
399 |
} |
|
400 |
lua_pop (L, 1); |
|
401 |
} else { // Unregister |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
402 |
llm_handler_t *handler = luaL_checklm_handler (L, 2); |
0 | 403 |
lm_connection_unregister_message_handler (object->connection, |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
404 |
handler->handler, |
0 | 405 |
type); |
406 |
} |
|
407 |
lua_pop (L, 2); |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
408 |
return 1; |
0 | 409 |
} |
410 |
||
411 |
/// disconnect callback function |
|
412 |
/// Function, that will be called when disconnection occurs. |
|
413 |
/// A: lm connection object, disconnect reason |
|
414 |
void llm_disconnect_callback (LmConnection *connection, LmDisconnectReason reason, llm_callback_t *cb) |
|
415 |
{ |
|
416 |
lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->reference); |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
417 |
bless_lm_connection (cb->L, connection); |
0 | 418 |
// XXX lm_connection_unref (connection); |
419 |
luaL_pushenum (cb->L, reason, llm_disconnect_reason); |
|
420 |
if (lua_pcall (cb->L, 2, 0, 0)) { |
|
9
50f55d494efb
Not use error log level, it is fatal
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
421 |
W ("Disconnect callback error: %s", lua_tostring (cb->L, -1)); |
0 | 422 |
lua_pop (cb->L, 1); |
423 |
} |
|
424 |
} |
|
425 |
||
426 |
/// connection:ondisconnect |
|
427 |
/// Sets callback, that will be called on connection disconnect. |
|
428 |
/// A: disconnect callback function |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
429 |
/// R: lm connection object |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
430 |
static int ondisconnect_lm_connection (lua_State *L) |
0 | 431 |
{ |
432 |
llm_connection_t *object = luaL_checklm_connection (L, 1); |
|
433 |
llm_callback_t *cb; |
|
434 |
luaL_argcheck (L, lua_isfunction (L, 2), 2, "function expected"); |
|
435 |
||
436 |
cb = luaL_malloc (L, sizeof (llm_callback_t)); |
|
437 |
cb->reference = luaL_ref (L, LUA_REGISTRYINDEX); |
|
438 |
cb->L = L; |
|
439 |
||
440 |
lm_connection_set_disconnect_function (object->connection, |
|
441 |
(LmDisconnectFunction)llm_disconnect_callback, |
|
442 |
cb, (GDestroyNotify)llm_callback_destroy); |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
443 |
return 1; |
0 | 444 |
} |
445 |
||
446 |
/// connection:open_wait |
|
447 |
/// Synchronous open call, that will block until connection will be opened. |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
448 |
/// R: lm connection object or nil, string (error message) |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
449 |
static int open_wait_lm_connection (lua_State *L) |
0 | 450 |
{ |
451 |
llm_connection_t *object = luaL_checklm_connection (L, 1); |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
452 |
GError *err = NULL; |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
453 |
if (lm_connection_open_and_block (object->connection, &err)) |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
454 |
return 1; |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
455 |
else { |
9
50f55d494efb
Not use error log level, it is fatal
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
456 |
I ("Synchronous connection opening failed: %s", err->message); |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
457 |
lua_pushnil (L); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
458 |
lua_pushstring (L, err->message); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
459 |
return 2; |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
460 |
} |
0 | 461 |
} |
462 |
||
463 |
/// connection:authenticate_wait |
|
464 |
/// Synchronous authentication call, that will wait until the end of authentication. |
|
465 |
/// A: string (username), string (password), string (resource) |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
466 |
/// R: lm connection object or nil, string (error message) |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
467 |
static int authenticate_wait_lm_connection (lua_State *L) |
0 | 468 |
{ |
469 |
llm_connection_t *object = luaL_checklm_connection (L, 1); |
|
470 |
const char *username = luaL_checkstring (L, 2); |
|
471 |
const char *password = luaL_checkstring (L, 3); |
|
472 |
const char *resource = luaL_checkstring (L, 4); |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
473 |
GError *err = NULL; |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
474 |
if (lm_connection_authenticate_and_block (object->connection, username, |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
475 |
password, resource, &err)) |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
476 |
return 1; |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
477 |
else { |
9
50f55d494efb
Not use error log level, it is fatal
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
478 |
I ("Synchronous authentication failed: %s", err->message); |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
479 |
lua_pushnil (L); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
480 |
lua_pushstring (L, err->message); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
481 |
return 2; |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
482 |
} |
0 | 483 |
} |
484 |
||
485 |
/// connection:send_wait |
|
486 |
/// Synchronous call, that will send message and wait for reply to it. |
|
487 |
/// A: lm message object |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
488 |
/// R: lm message object or nil, string (error message) |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
489 |
static int send_wait_lm_connection (lua_State *L) |
0 | 490 |
{ |
491 |
llm_connection_t *object = luaL_checklm_connection (L, 1); |
|
492 |
llm_message_t *message = luaL_checklm_message (L, 2); |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
493 |
GError *err = NULL; |
0 | 494 |
LmMessage *new = lm_connection_send_with_reply_and_block (object->connection, |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
495 |
message->message, &err); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
496 |
if (!new) { |
9
50f55d494efb
Not use error log level, it is fatal
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
497 |
I ("Synchronous message sending failed: %s", err->message); |
0 | 498 |
lua_pushnil (L); |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
499 |
lua_pushstring (L, err->message); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
500 |
return 2; |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
501 |
} else { |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
502 |
bless_lm_message (L, new); |
0 | 503 |
lm_message_unref (new); // XXX |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
504 |
return 1; |
0 | 505 |
} |
506 |
} |
|
507 |
||
508 |
/// connection:pointer |
|
509 |
/// Returns pointer to underlying C loudmouth structure. |
|
510 |
/// R: lightuserdata |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
511 |
static int pointer_lm_connection (lua_State *L) |
0 | 512 |
{ |
513 |
llm_connection_t *object = luaL_checklm_connection (L, 1); |
|
514 |
lua_pushlightuserdata (L, object->connection); |
|
515 |
lua_remove (L, -2); |
|
516 |
return 1; |
|
517 |
} |
|
518 |
||
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
519 |
static int gc_lm_connection (lua_State *L) |
0 | 520 |
{ |
521 |
llm_connection_t *object = luaL_checklm_connection (L, 1); |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
522 |
D ("Connection %X gc called", (int) object); |
0 | 523 |
lm_connection_unref (object->connection); |
524 |
return 0; |
|
525 |
} |
|
526 |
||
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
527 |
static const luaL_Reg reg_f_lm_connection[] = { |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
528 |
{ "new", new_lm_connection }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
529 |
{ "bless", bless_lua_lm_connection }, |
12 | 530 |
{ NULL, NULL }, |
0 | 531 |
}; |
532 |
||
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
533 |
static const luaL_Reg reg_m_lm_connection[] = { |
12 | 534 |
{ "open", open_lm_connection }, |
535 |
{ "close", close_lm_connection }, |
|
536 |
{ "authenticate", authenticate_lm_connection }, |
|
13
8f502c1477ac
Forgot to register connection:status
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
537 |
{ "status", status_lm_connection }, |
12 | 538 |
{ "port", port_lm_connection }, |
539 |
{ "server", server_lm_connection }, |
|
540 |
{ "jid", jid_lm_connection }, |
|
541 |
{ "keep_alive_rate", keep_alive_rate_lm_connection }, |
|
542 |
{ "state", status_lm_connection }, |
|
543 |
{ "proxy", proxy_lm_connection }, |
|
544 |
{ "ssl", ssl_lm_connection }, |
|
545 |
{ "send", send_lm_connection }, |
|
546 |
{ "handler", handler_lm_connection }, |
|
547 |
{ "ondisconnect", ondisconnect_lm_connection }, |
|
548 |
{ "open_wait", open_wait_lm_connection }, |
|
549 |
{ "authenticate_wait", authenticate_wait_lm_connection }, |
|
550 |
{ "send_wait", send_wait_lm_connection }, |
|
551 |
{ "pointer", pointer_lm_connection }, |
|
552 |
{ "__gc", gc_lm_connection }, |
|
553 |
{ NULL, NULL }, |
|
0 | 554 |
}; |
555 |
||
556 |
int luaopen_lm_connection (lua_State *L) |
|
557 |
{ |
|
558 |
luaL_newmetatable (L, "loudmouth.connection"); |
|
19
d775d7289fe4
Use lua_pushliteral and lua_setfield
Myhailo Danylenko <isbear@ukrpost.net>
parents:
16
diff
changeset
|
559 |
lua_pushvalue (L, -1); |
d775d7289fe4
Use lua_pushliteral and lua_setfield
Myhailo Danylenko <isbear@ukrpost.net>
parents:
16
diff
changeset
|
560 |
lua_setfield (L, -2, "__index"); |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
561 |
luaL_register (L, NULL, reg_m_lm_connection); |
0 | 562 |
lua_pop (L, 1); |
16
09b375e9ce32
Switch to new module organization scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
563 |
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:
13
diff
changeset
|
564 |
luaL_register (L, NULL, reg_f_lm_connection); |
0 | 565 |
return 1; |
566 |
} |
|
567 |