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