author | Myhailo Danylenko <isbear@ukrpost.net> |
Fri, 20 Feb 2009 23:07:43 +0200 | |
changeset 6 | 90073cbb535d |
parent 4 | 5770be2d5f3f |
child 9 | 50f55d494efb |
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: |
0 | 21 |
const string2enum_t llm_connection_state[] = { |
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: |
0 | 34 |
const string2enum_t llm_handler_priority[] = { |
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 |
|
62 |
static int llm_connection_new (lua_State *L) |
|
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 |
} |
|
72 |
llm_connection_bless (L, connection); |
|
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 |
|
83 |
static int llm_connection_bless_lua (lua_State *L) |
|
84 |
{ |
|
85 |
luaL_argcheck (L, lua_islightuserdata (L, 1), 1, "loudmouth connection lightuserdata expected"); |
|
86 |
llm_connection_bless (L, (LmConnection *) lua_touserdata (L, 1)); |
|
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) |
|
94 |
static void llm_connection_callback (LmConnection *connection, int success, llm_callback_t *cb) |
|
95 |
{ |
|
96 |
lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->reference); |
|
97 |
llm_connection_bless (cb->L, connection); |
|
98 |
// XXX lm_connection_unref (connection); |
|
99 |
lua_pushboolean (cb->L, success); |
|
100 |
if (lua_pcall (cb->L, 2, 0, 0)) { |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
101 |
E ("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) |
0 | 110 |
static int llm_connection_open (lua_State *L) |
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 |
||
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
121 |
if (lm_connection_open (object->connection, (LmResultFunction) llm_connection_callback, |
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); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
127 |
W ("Connection opening failed"); |
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) |
0 | 136 |
static int llm_connection_authenticate (lua_State *L) |
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, |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
152 |
(LmResultFunction) llm_connection_callback, cb, |
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); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
159 |
W ("Connection authentication failed: %s", err->message); |
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 |
0 | 168 |
static int llm_connection_port (lua_State *L) |
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 |
0 | 184 |
static int llm_connection_server (lua_State *L) |
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 |
0 | 200 |
static int llm_connection_jid (lua_State *L) |
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) |
0 | 218 |
static int llm_connection_keep_alive_rate (lua_State *L) |
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 |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
230 |
E ("Sorry, your loudmouth have no get_keep_alive_rate ()"); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
231 |
lua_pushnil (L); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
232 |
lua_pushstring (L, "Sorry, your loudmouth have no get_keep_alive_rate ()"); |
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 |
0 | 242 |
static int llm_connection_proxy (lua_State *L) |
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) { |
|
253 |
llm_proxy_bless (L, proxy); |
|
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 |
0 | 265 |
static int llm_connection_ssl (lua_State *L) |
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) { |
|
276 |
llm_ssl_bless (L, ssl); |
|
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) |
0 | 287 |
static int llm_connection_close (lua_State *L) |
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 { |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
294 |
W ("Connection close failed: %s", err->message); |
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 |
|
304 |
static int llm_connection_status (lua_State *L) |
|
305 |
{ |
|
306 |
llm_connection_t *connection = luaL_checklm_connection (L, 1); |
|
1 | 307 |
luaL_pushenum (L, lm_connection_get_state (connection->connection), llm_connection_state); |
0 | 308 |
return 1; |
309 |
} |
|
310 |
||
311 |
/// connection:send |
|
312 |
/// Sends message (object). If specified, handler function will be called upon |
|
313 |
/// receiving of response to that message. |
|
314 |
/// Handler function can be either message handler object or just a function. |
|
315 |
/// A: lm message object, message handler callback function or lm message handler object (optional) |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
316 |
/// R: lm connection object or nil, string (error message) |
0 | 317 |
static int llm_connection_send (lua_State *L) |
318 |
{ |
|
319 |
llm_connection_t *object = luaL_checklm_connection (L, 1); |
|
320 |
llm_message_t *message = luaL_checklm_message (L, 2); |
|
321 |
int status; |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
322 |
GError *err = NULL; |
0 | 323 |
if (lua_gettop (L) < 3) // Send |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
324 |
status = lm_connection_send (object->connection, message->message, &err); |
0 | 325 |
else if (lua_isfunction (L, 3)) { // Send w/reply, func |
326 |
llm_callback_t *cb = luaL_malloc (L, sizeof (llm_callback_t)); |
|
327 |
LmMessageHandler *handler; |
|
328 |
||
329 |
cb->reference = luaL_ref (L, LUA_REGISTRYINDEX); |
|
330 |
cb->L = L; |
|
331 |
handler = lm_message_handler_new ( |
|
332 |
(LmHandleMessageFunction)llm_message_handler_callback, |
|
333 |
cb, (GDestroyNotify)llm_callback_destroy); |
|
334 |
status = lm_connection_send_with_reply (object->connection, |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
335 |
message->message, handler, &err); |
0 | 336 |
lm_message_handler_unref (handler); |
337 |
} else { // Send w/reply, handler |
|
338 |
llm_message_handler_t *handler = luaL_checklm_message_handler (L, 3); |
|
339 |
status = lm_connection_send_with_reply (object->connection, message->message, |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
340 |
handler->message_handler, &err); |
0 | 341 |
lua_pop (L, 1); |
342 |
}; |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
343 |
lua_pop (L, 1); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
344 |
if (status) |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
345 |
return 1; |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
346 |
else { |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
347 |
W ("Message sending failed: %s", err->message); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
348 |
lua_pushnil (L); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
349 |
lua_pushstring (L, err->message); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
350 |
return 2; |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
351 |
} |
0 | 352 |
} |
353 |
||
354 |
/// connection:send_raw |
|
355 |
/// Sends arbitrary string to opened connection. |
|
356 |
/// A: string |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
357 |
/// R: lm connection object or nil, string (error message) |
0 | 358 |
static int llm_connection_send_raw (lua_State *L) |
359 |
{ |
|
360 |
llm_connection_t *object = luaL_checklm_connection (L, 1); |
|
361 |
const char *string = luaL_checkstring (L, 2); |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
362 |
GError *err = NULL; |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
363 |
if (lm_connection_send_raw (object->connection, string, NULL)) { |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
364 |
lua_pop (L, 1); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
365 |
return 1; |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
366 |
} else { |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
367 |
W ("Raw message sending failed: %s", err->message); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
368 |
lua_pushnil (L); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
369 |
lua_pushstring (L, err->message); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
370 |
return 2; |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
371 |
} |
0 | 372 |
} |
373 |
||
374 |
/// connection:handler |
|
375 |
/// Registers or unregisters handler function for a given type of messages. |
|
376 |
/// To unregister handler, omit the priority argument. |
|
377 |
/// Handler function can be specified as plain function or message handler object. |
|
378 |
/// Though, you can unregister only a message handler object. |
|
379 |
/// 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
|
380 |
/// R: lm connection object |
0 | 381 |
static int llm_connection_handler (lua_State *L) |
382 |
{ |
|
383 |
llm_connection_t *object = luaL_checklm_connection (L, 1); |
|
384 |
int type = luaL_checkenum (L, 3, llm_message_type); |
|
385 |
||
386 |
if (lua_gettop (L) > 3) { // Register |
|
387 |
int priority = luaL_checkenum (L, 4, llm_handler_priority); |
|
388 |
||
389 |
if (lua_isfunction (L, 2)) { // Function |
|
390 |
LmMessageHandler *handler; |
|
391 |
llm_callback_t *cb = luaL_malloc (L, sizeof (llm_callback_t)); |
|
392 |
lua_pushvalue (L, 2); |
|
393 |
cb->reference = luaL_ref (L, LUA_REGISTRYINDEX); |
|
394 |
cb->L = L; |
|
395 |
||
396 |
handler = lm_message_handler_new ( |
|
397 |
(LmHandleMessageFunction)llm_message_handler_callback, |
|
398 |
cb, (GDestroyNotify)llm_callback_destroy); |
|
399 |
lm_connection_register_message_handler (object->connection, |
|
400 |
handler, type, priority); |
|
401 |
lm_message_handler_unref (handler); |
|
402 |
} else { // Object |
|
403 |
llm_message_handler_t *handler = luaL_checklm_message_handler (L, 2); |
|
404 |
lm_connection_register_message_handler (object->connection, |
|
405 |
handler->message_handler, |
|
406 |
type, priority); |
|
407 |
} |
|
408 |
lua_pop (L, 1); |
|
409 |
} else { // Unregister |
|
410 |
llm_message_handler_t *handler = luaL_checklm_message_handler (L, 2); |
|
411 |
lm_connection_unregister_message_handler (object->connection, |
|
412 |
handler->message_handler, |
|
413 |
type); |
|
414 |
} |
|
415 |
lua_pop (L, 2); |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
416 |
return 1; |
0 | 417 |
} |
418 |
||
419 |
/// disconnect callback function |
|
420 |
/// Function, that will be called when disconnection occurs. |
|
421 |
/// A: lm connection object, disconnect reason |
|
422 |
void llm_disconnect_callback (LmConnection *connection, LmDisconnectReason reason, llm_callback_t *cb) |
|
423 |
{ |
|
424 |
lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->reference); |
|
425 |
llm_connection_bless (cb->L, connection); |
|
426 |
// XXX lm_connection_unref (connection); |
|
427 |
luaL_pushenum (cb->L, reason, llm_disconnect_reason); |
|
428 |
if (lua_pcall (cb->L, 2, 0, 0)) { |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
429 |
E ("Disconnect callback error: %s", lua_tostring (cb->L, -1)); |
0 | 430 |
lua_pop (cb->L, 1); |
431 |
} |
|
432 |
} |
|
433 |
||
434 |
/// connection:ondisconnect |
|
435 |
/// Sets callback, that will be called on connection disconnect. |
|
436 |
/// A: disconnect callback function |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
437 |
/// R: lm connection object |
0 | 438 |
static int llm_connection_ondisconnect (lua_State *L) |
439 |
{ |
|
440 |
llm_connection_t *object = luaL_checklm_connection (L, 1); |
|
441 |
llm_callback_t *cb; |
|
442 |
luaL_argcheck (L, lua_isfunction (L, 2), 2, "function expected"); |
|
443 |
||
444 |
cb = luaL_malloc (L, sizeof (llm_callback_t)); |
|
445 |
cb->reference = luaL_ref (L, LUA_REGISTRYINDEX); |
|
446 |
cb->L = L; |
|
447 |
||
448 |
lm_connection_set_disconnect_function (object->connection, |
|
449 |
(LmDisconnectFunction)llm_disconnect_callback, |
|
450 |
cb, (GDestroyNotify)llm_callback_destroy); |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
451 |
return 1; |
0 | 452 |
} |
453 |
||
454 |
/// connection:open_wait |
|
455 |
/// 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
|
456 |
/// R: lm connection object or nil, string (error message) |
0 | 457 |
static int llm_connection_open_wait (lua_State *L) |
458 |
{ |
|
459 |
llm_connection_t *object = luaL_checklm_connection (L, 1); |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
460 |
GError *err = NULL; |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
461 |
if (lm_connection_open_and_block (object->connection, &err)) |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
462 |
return 1; |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
463 |
else { |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
464 |
W ("Synchronous connection opening failed: %s", err->message); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
465 |
lua_pushnil (L); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
466 |
lua_pushstring (L, err->message); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
467 |
return 2; |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
468 |
} |
0 | 469 |
} |
470 |
||
471 |
/// connection:authenticate_wait |
|
472 |
/// Synchronous authentication call, that will wait until the end of authentication. |
|
473 |
/// A: string (username), string (password), string (resource) |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
474 |
/// R: lm connection object or nil, string (error message) |
0 | 475 |
static int llm_connection_authenticate_wait (lua_State *L) |
476 |
{ |
|
477 |
llm_connection_t *object = luaL_checklm_connection (L, 1); |
|
478 |
const char *username = luaL_checkstring (L, 2); |
|
479 |
const char *password = luaL_checkstring (L, 3); |
|
480 |
const char *resource = luaL_checkstring (L, 4); |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
481 |
GError *err = NULL; |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
482 |
if (lm_connection_authenticate_and_block (object->connection, username, |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
483 |
password, resource, &err)) |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
484 |
return 1; |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
485 |
else { |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
486 |
W ("Synchronous authentication failed: %s", err->message); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
487 |
lua_pushnil (L); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
488 |
lua_pushstring (L, err->message); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
489 |
return 2; |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
490 |
} |
0 | 491 |
} |
492 |
||
493 |
/// connection:send_wait |
|
494 |
/// Synchronous call, that will send message and wait for reply to it. |
|
495 |
/// A: lm message object |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
496 |
/// R: lm message object or nil, string (error message) |
0 | 497 |
static int llm_connection_send_wait (lua_State *L) |
498 |
{ |
|
499 |
llm_connection_t *object = luaL_checklm_connection (L, 1); |
|
500 |
llm_message_t *message = luaL_checklm_message (L, 2); |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
501 |
GError *err = NULL; |
0 | 502 |
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
|
503 |
message->message, &err); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
504 |
if (!new) { |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
505 |
W ("Synchronous message sending failed: %s", err->message); |
0 | 506 |
lua_pushnil (L); |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
507 |
lua_pushstring (L, err->message); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
508 |
return 2; |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
509 |
} else { |
0 | 510 |
llm_message_bless (L, new); |
511 |
lm_message_unref (new); // XXX |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
512 |
return 1; |
0 | 513 |
} |
514 |
} |
|
515 |
||
516 |
/// connection:pointer |
|
517 |
/// Returns pointer to underlying C loudmouth structure. |
|
518 |
/// R: lightuserdata |
|
519 |
static int llm_connection_pointer (lua_State *L) |
|
520 |
{ |
|
521 |
llm_connection_t *object = luaL_checklm_connection (L, 1); |
|
522 |
lua_pushlightuserdata (L, object->connection); |
|
523 |
lua_remove (L, -2); |
|
524 |
return 1; |
|
525 |
} |
|
526 |
||
527 |
static int llm_connection_gc (lua_State *L) |
|
528 |
{ |
|
529 |
llm_connection_t *object = luaL_checklm_connection (L, 1); |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
530 |
D ("Connection %X gc called", (int) object); |
0 | 531 |
lm_connection_unref (object->connection); |
532 |
return 0; |
|
533 |
} |
|
534 |
||
535 |
static const luaL_Reg llm_connection_reg_f[] = { |
|
536 |
{ "new", llm_connection_new }, |
|
537 |
{ "bless", llm_connection_bless_lua }, |
|
538 |
{ NULL, NULL }, |
|
539 |
}; |
|
540 |
||
541 |
static const luaL_Reg llm_connection_reg_m[] = { |
|
542 |
{ "open", llm_connection_open }, |
|
543 |
{ "close", llm_connection_close }, |
|
544 |
{ "authenticate", llm_connection_authenticate }, |
|
545 |
{ "port", llm_connection_port }, |
|
546 |
{ "server", llm_connection_server }, |
|
547 |
{ "jid", llm_connection_jid }, |
|
548 |
{ "keep_alive_rate", llm_connection_keep_alive_rate }, |
|
549 |
{ "state", llm_connection_status }, |
|
550 |
{ "proxy", llm_connection_proxy }, |
|
551 |
{ "ssl", llm_connection_ssl }, |
|
552 |
{ "send", llm_connection_send }, |
|
553 |
{ "send_raw", llm_connection_send_raw }, |
|
554 |
{ "handler", llm_connection_handler }, |
|
555 |
{ "ondisconnect", llm_connection_ondisconnect }, |
|
556 |
{ "open_wait", llm_connection_open_wait }, |
|
557 |
{ "authenticate_wait", llm_connection_authenticate_wait }, |
|
558 |
{ "send_wait", llm_connection_send_wait }, |
|
559 |
{ "pointer", llm_connection_pointer }, |
|
560 |
{ "__gc", llm_connection_gc }, |
|
561 |
{ NULL, NULL }, |
|
562 |
}; |
|
563 |
||
564 |
int luaopen_lm_connection (lua_State *L) |
|
565 |
{ |
|
566 |
luaL_newmetatable (L, "loudmouth.connection"); |
|
567 |
lua_pushstring (L, "__index"); |
|
568 |
lua_pushvalue (L, -2); |
|
569 |
lua_settable (L, -3); |
|
570 |
luaL_register (L, NULL, llm_connection_reg_m); |
|
571 |
lua_pop (L, 1); |
|
572 |
luaL_register (L, "lm.connection", llm_connection_reg_f); |
|
573 |
return 1; |
|
574 |
} |
|
575 |