author | Myhailo Danylenko <isbear@ukrpost.net> |
Wed, 16 Mar 2016 01:53:56 +0200 | |
changeset 66 | a40beb82130c |
parent 63 | c17f3295f52c |
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); |
38
34a2b880615c
Fix debugging pointer format character
Myhailo Danylenko <isbear@ukrpost.net>
parents:
31
diff
changeset
|
91 |
D ("Connection %p created", 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 |
{ |
45 | 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 |
GError *err = NULL; |
|
161 |
llm_callback_t *cb; |
|
0 | 162 |
luaL_argcheck (L, lua_isfunction (L, 5), 5, "function expected"); |
163 |
||
164 |
cb = luaL_malloc (L, sizeof (llm_callback_t)); |
|
165 |
cb->reference = luaL_ref (L, LUA_REGISTRYINDEX); |
|
166 |
cb->L = L; |
|
167 |
||
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
168 |
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
|
169 |
(LmResultFunction) callback_lm_connection, cb, |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
170 |
(GDestroyNotify) llm_callback_destroy, &err)) { |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
171 |
lua_pop (L, 3); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
172 |
return 1; |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
173 |
} else { |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
174 |
lua_pushnil (L); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
175 |
lua_pushstring (L, err->message); |
9
50f55d494efb
Not use error log level, it is fatal
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
176 |
I ("Connection authentication failed: %s", err->message); |
30 | 177 |
g_error_free (err); |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
178 |
return 2; |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
179 |
} |
0 | 180 |
} |
181 |
||
182 |
/// connection:port |
|
183 |
/// Gets or sets server port to connect. |
|
184 |
/// A: integer (optional) |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
185 |
/// 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
|
186 |
static int port_lm_connection (lua_State *L) |
0 | 187 |
{ |
188 |
llm_connection_t *object = luaL_checklm_connection (L, 1); |
|
189 |
if (lua_gettop (L) > 1) { // Set |
|
63
c17f3295f52c
lua: Add compatibility with lua 5.3
Myhailo Danylenko <isbear@ukrpost.net>
parents:
54
diff
changeset
|
190 |
lm_connection_set_port (object->connection, luaL_checkinteger (L, 2)); |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
191 |
lua_pop (L, 1); |
0 | 192 |
} else { // Get |
193 |
lua_pushnumber (L, lm_connection_get_port (object->connection)); |
|
194 |
} |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
195 |
return 1; |
0 | 196 |
} |
197 |
||
198 |
/// connection:server |
|
199 |
/// Gets or sets server to connect to. |
|
200 |
/// A: string (optional, server name) |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
201 |
/// 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
|
202 |
static int server_lm_connection (lua_State *L) |
0 | 203 |
{ |
204 |
llm_connection_t *object = luaL_checklm_connection (L, 1); |
|
205 |
if (lua_gettop (L) > 1) { // Set |
|
206 |
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
|
207 |
lua_pop (L, 1); |
0 | 208 |
} else { // Get |
209 |
lua_pushstring (L, lm_connection_get_server (object->connection)); |
|
210 |
} |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
211 |
return 1; |
0 | 212 |
} |
213 |
||
214 |
/// connection:jid |
|
215 |
/// Gets or sets jid for connection. |
|
216 |
/// A: string (optional) |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
217 |
/// 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
|
218 |
static int jid_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_jid (object->connection, luaL_checkstring (L, 2)); |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
223 |
lua_pop (L, 1); |
0 | 224 |
} else { // Get |
225 |
lua_pushstring (L, lm_connection_get_jid (object->connection)); |
|
226 |
} |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
227 |
return 1; |
0 | 228 |
} |
229 |
||
230 |
/// connection:keep_alive_rate |
|
231 |
/// Gets or sets keep alive packets rate for connection. |
|
232 |
/// Note, that on some platforms there is no get function even in |
|
233 |
/// loudmouth versions, that should have it according to documentation. |
|
234 |
/// integer (optional, seconds) |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
235 |
/// 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
|
236 |
static int keep_alive_rate_lm_connection (lua_State *L) |
0 | 237 |
{ |
238 |
llm_connection_t *object = luaL_checklm_connection (L, 1); |
|
239 |
if (lua_gettop (L) > 1) { // Set |
|
63
c17f3295f52c
lua: Add compatibility with lua 5.3
Myhailo Danylenko <isbear@ukrpost.net>
parents:
54
diff
changeset
|
240 |
lm_connection_set_keep_alive_rate (object->connection, luaL_checkinteger (L, 2)); |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
241 |
lua_pop (L, 1); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
242 |
return 1; |
0 | 243 |
} else { // Get |
244 |
#ifdef HAVE_LM_CONNECTION_GET_KEEP_ALIVE_RATE |
|
245 |
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
|
246 |
return 1; |
0 | 247 |
#else |
9
50f55d494efb
Not use error log level, it is fatal
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
248 |
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
|
249 |
lua_pushnil (L); |
19
d775d7289fe4
Use lua_pushliteral and lua_setfield
Myhailo Danylenko <isbear@ukrpost.net>
parents:
16
diff
changeset
|
250 |
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
|
251 |
return 2; |
0 | 252 |
#endif |
253 |
} |
|
254 |
} |
|
255 |
||
256 |
/// connection:proxy |
|
257 |
/// Gets or sets proxy server for connection. |
|
258 |
/// A: lm proxy object (optional) |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
259 |
/// 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
|
260 |
static int proxy_lm_connection (lua_State *L) |
0 | 261 |
{ |
262 |
llm_connection_t *object = luaL_checklm_connection (L, 1); |
|
263 |
if (lua_gettop (L) > 1) { // Set |
|
264 |
llm_proxy_t *proxy = luaL_checklm_proxy (L, 2); |
|
265 |
lm_connection_set_proxy (object->connection, proxy->proxy); |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
266 |
lua_pop (L, 1); |
0 | 267 |
} else { // Get |
268 |
LmProxy *proxy = lm_connection_get_proxy (object->connection); |
|
269 |
lua_pop (L, 1); |
|
270 |
if (proxy) { |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
271 |
bless_lm_proxy (L, proxy); |
0 | 272 |
// XXX lm_proxy_unref (proxy); |
273 |
} else |
|
274 |
lua_pushnil (L); |
|
275 |
} |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
276 |
return 1; |
0 | 277 |
} |
278 |
||
279 |
/// connection:ssl |
|
280 |
/// Gets or sets ssl object for connection. |
|
281 |
/// A: lm ssl object (optional) |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
282 |
/// 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
|
283 |
static int ssl_lm_connection (lua_State *L) |
0 | 284 |
{ |
285 |
llm_connection_t *object = luaL_checklm_connection (L, 1); |
|
286 |
if (lua_gettop (L) > 1) { // Set |
|
287 |
llm_ssl_t *ssl = luaL_checklm_ssl (L, 2); |
|
288 |
lm_connection_set_ssl (object->connection, ssl->ssl); |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
289 |
lua_pop (L, 1); |
0 | 290 |
} else { // Get |
291 |
LmSSL *ssl = lm_connection_get_ssl (object->connection); |
|
292 |
lua_pop (L, 1); |
|
293 |
if (ssl) { |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
294 |
bless_lm_ssl (L, ssl); |
0 | 295 |
// XXX lm_ssl_unref (ssl); |
296 |
} else |
|
297 |
lua_pushnil (L); |
|
298 |
} |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
299 |
return 1; |
0 | 300 |
} |
301 |
||
302 |
/// connection:close |
|
303 |
/// Close connection. |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
304 |
/// 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
|
305 |
static int close_lm_connection (lua_State *L) |
0 | 306 |
{ |
307 |
llm_connection_t *object = luaL_checklm_connection (L, 1); |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
308 |
GError *err = NULL; |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
309 |
if (lm_connection_close (object->connection, &err)) |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
310 |
return 1; |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
311 |
else { |
9
50f55d494efb
Not use error log level, it is fatal
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
312 |
I ("Connection close failed: %s", err->message); |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
313 |
lua_pushnil (L); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
314 |
lua_pushstring (L, err->message); |
30 | 315 |
g_error_free (err); |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
316 |
return 2; |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
317 |
} |
0 | 318 |
} |
319 |
||
320 |
/// connection:status |
|
321 |
/// Returns string, describing connection state. |
|
322 |
/// R: connection state |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
323 |
static int status_lm_connection (lua_State *L) |
0 | 324 |
{ |
325 |
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
|
326 |
luaL_pushenum (L, lm_connection_get_state (connection->connection), state_lm_connection); |
0 | 327 |
return 1; |
328 |
} |
|
329 |
||
330 |
/// connection:send |
|
10
aed141accdd9
Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
331 |
/// 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
|
332 |
/// 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
|
333 |
/// 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
|
334 |
/// In short: |
aed141accdd9
Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
335 |
/// * connection, errmsg = connection:send ( "raw xml" ) |
aed141accdd9
Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
336 |
/// * connection, errmsg = connection:send ( message ) |
aed141accdd9
Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
337 |
/// * 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
|
338 |
/// * connection, errmsg = connection:send ( message, handler ) |
aed141accdd9
Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
339 |
/// If connection is nil, errmsg contains error message. |
aed141accdd9
Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
340 |
/// 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
|
341 |
/// 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
|
342 |
static int send_lm_connection (lua_State *L) |
0 | 343 |
{ |
344 |
llm_connection_t *object = luaL_checklm_connection (L, 1); |
|
345 |
int status; |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
346 |
GError *err = NULL; |
10
aed141accdd9
Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
347 |
if (lua_gettop (L) < 3) { // Send |
aed141accdd9
Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
348 |
if (lua_type (L, 2) == LUA_TSTRING) |
aed141accdd9
Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
349 |
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
|
350 |
else { |
aed141accdd9
Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
351 |
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
|
352 |
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
|
353 |
} |
aed141accdd9
Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
354 |
} 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
|
355 |
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
|
356 |
llm_callback_t *cb = luaL_malloc (L, sizeof (llm_callback_t)); |
0 | 357 |
LmMessageHandler *handler; |
358 |
||
359 |
cb->reference = luaL_ref (L, LUA_REGISTRYINDEX); |
|
10
aed141accdd9
Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
360 |
cb->L = L; |
0 | 361 |
handler = lm_message_handler_new ( |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
362 |
(LmHandleMessageFunction)callback_lm_handler, |
0 | 363 |
cb, (GDestroyNotify)llm_callback_destroy); |
364 |
status = lm_connection_send_with_reply (object->connection, |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
365 |
message->message, handler, &err); |
0 | 366 |
lm_message_handler_unref (handler); |
367 |
} else { // Send w/reply, handler |
|
10
aed141accdd9
Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
368 |
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
|
369 |
llm_handler_t *handler = luaL_checklm_handler (L, 3); |
0 | 370 |
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
|
371 |
handler->handler, &err); |
0 | 372 |
lua_pop (L, 1); |
373 |
}; |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
374 |
lua_pop (L, 1); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
375 |
if (status) |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
376 |
return 1; |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
377 |
else { |
9
50f55d494efb
Not use error log level, it is fatal
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
378 |
I ("Message sending failed: %s", err->message); |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
379 |
lua_pushnil (L); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
380 |
lua_pushstring (L, err->message); |
30 | 381 |
g_error_free (err); |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
382 |
return 2; |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
383 |
} |
0 | 384 |
} |
385 |
||
386 |
/// connection:handler |
|
387 |
/// Registers or unregisters handler function for a given type of messages. |
|
388 |
/// To unregister handler, omit the priority argument. |
|
30 | 389 |
/// To unregister reply handler, omit message type argument too. |
0 | 390 |
/// Handler function can be specified as plain function or message handler object. |
30 | 391 |
/// 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
|
392 |
/// R: lm connection object |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
393 |
static int handler_lm_connection (lua_State *L) |
0 | 394 |
{ |
395 |
llm_connection_t *object = luaL_checklm_connection (L, 1); |
|
30 | 396 |
int top = lua_gettop (L); |
397 |
if (top > 2) { // Have message type |
|
398 |
int type = luaL_checkenum (L, 3, type_lm_message); |
|
0 | 399 |
|
30 | 400 |
if (lua_gettop (L) > 3) { // Register |
401 |
int priority = luaL_checkenum (L, 4, priority_lm_handler); |
|
0 | 402 |
|
30 | 403 |
if (lua_isfunction (L, 2)) { // Function |
404 |
LmMessageHandler *handler; |
|
405 |
llm_callback_t *cb = luaL_malloc (L, sizeof (llm_callback_t)); |
|
406 |
lua_pushvalue (L, 2); |
|
407 |
cb->reference = luaL_ref (L, LUA_REGISTRYINDEX); |
|
408 |
cb->L = L; |
|
0 | 409 |
|
30 | 410 |
handler = lm_message_handler_new ( |
411 |
(LmHandleMessageFunction)callback_lm_handler, |
|
412 |
cb, (GDestroyNotify)llm_callback_destroy); |
|
413 |
lm_connection_register_message_handler (object->connection, |
|
414 |
handler, type, priority); |
|
415 |
lm_message_handler_unref (handler); |
|
416 |
} else { // Object |
|
417 |
llm_handler_t *handler = luaL_checklm_handler (L, 2); |
|
418 |
lm_connection_register_message_handler (object->connection, |
|
419 |
handler->handler, |
|
420 |
type, priority); |
|
421 |
} |
|
422 |
lua_pop (L, 1); |
|
423 |
} else { // Unregister message handler |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
424 |
llm_handler_t *handler = luaL_checklm_handler (L, 2); |
30 | 425 |
lm_connection_unregister_message_handler (object->connection, |
426 |
handler->handler, |
|
427 |
type); |
|
0 | 428 |
} |
429 |
lua_pop (L, 1); |
|
30 | 430 |
} else { // Unregister reply handler |
31
afcdbbce5002
Autodetect if unregister_reply_handler is present
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
431 |
#ifdef HAVE_LM_CONNECTION_UNREGISTER_REPLY_HANDLER |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
432 |
llm_handler_t *handler = luaL_checklm_handler (L, 2); |
30 | 433 |
lm_connection_unregister_reply_handler (object -> connection, |
434 |
handler -> handler); |
|
435 |
#else |
|
31
afcdbbce5002
Autodetect if unregister_reply_handler is present
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
436 |
W ("Sorry, your loudmouth have no unregister_reply_handler ()"); |
30 | 437 |
lua_pushnil (L); |
31
afcdbbce5002
Autodetect if unregister_reply_handler is present
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
438 |
lua_pushliteral (L, "Sorry, your loudmouth have no unregister_reply_handler ()"); |
30 | 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); |
|
38
34a2b880615c
Fix debugging pointer format character
Myhailo Danylenko <isbear@ukrpost.net>
parents:
31
diff
changeset
|
560 |
D ("Connection %p gc called", 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"); |
54
6bef2082e5f9
v0.9.2 Support for lua 5.2
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
599 |
luaL_setfuncs (L, reg_m_lm_connection, 0); |
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 |
54
6bef2082e5f9
v0.9.2 Support for lua 5.2
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
602 |
luaL_setfuncs (L, reg_f_lm_connection, 0); |
0 | 603 |
return 1; |
604 |
} |
|
605 |