author | Myhailo Danylenko <isbear@ukrpost.net> |
Sun, 17 Jan 2010 02:58:44 +0200 | |
changeset 31 | afcdbbce5002 |
parent 23 | 13f03e604c8a |
child 37 | 6e425a8e245e |
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 <loudmouth/loudmouth.h> |
|
17 | 22 |
#include <string.h> |
0 | 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" |
|
7
5db1448eb857
Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
27 |
#include "lm_message_node.h" |
0 | 28 |
|
29 |
/// lm.message |
|
30 |
/// Module, representing individual message. |
|
31 |
/// Message have a type and optionally a sub type. |
|
8 | 32 |
/// Message have a set common methods with message node, |
18
6effa1929af7
Children node method removed
Myhailo Danylenko <isbear@ukrpost.net>
parents:
17
diff
changeset
|
33 |
/// these are name, next, prev, parent, value, child, |
8 | 34 |
/// find_child, attribute, raw, xml and path. They just save |
35 |
/// you typing :node() each time and save memory for |
|
36 |
/// one node object. |
|
0 | 37 |
|
38 |
/// message type |
|
39 |
/// Message type (root tag type). |
|
4 | 40 |
/// G: |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
41 |
const string2enum_t type_lm_message[] = { |
0 | 42 |
{ "message", LM_MESSAGE_TYPE_MESSAGE }, |
43 |
{ "presence", LM_MESSAGE_TYPE_PRESENCE }, |
|
44 |
{ "iq", LM_MESSAGE_TYPE_IQ }, |
|
45 |
{ "stream", LM_MESSAGE_TYPE_STREAM }, |
|
46 |
{ "stream error", LM_MESSAGE_TYPE_STREAM_ERROR }, |
|
47 |
{ "stream features", LM_MESSAGE_TYPE_STREAM_FEATURES }, |
|
48 |
{ "auth", LM_MESSAGE_TYPE_AUTH }, |
|
49 |
{ "challenge", LM_MESSAGE_TYPE_CHALLENGE }, |
|
50 |
{ "response", LM_MESSAGE_TYPE_RESPONSE }, |
|
51 |
{ "success", LM_MESSAGE_TYPE_SUCCESS }, |
|
52 |
{ "failure", LM_MESSAGE_TYPE_FAILURE }, |
|
53 |
{ "proceed", LM_MESSAGE_TYPE_PROCEED }, |
|
54 |
{ "starttls", LM_MESSAGE_TYPE_STARTTLS }, |
|
55 |
{ "unknown", LM_MESSAGE_TYPE_UNKNOWN }, |
|
56 |
{ "stream:stream", LM_MESSAGE_TYPE_STREAM }, |
|
57 |
{ "stream:error", LM_MESSAGE_TYPE_STREAM_ERROR }, |
|
58 |
{ "stream:features", LM_MESSAGE_TYPE_STREAM_FEATURES }, |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
59 |
{ "stream_error", LM_MESSAGE_TYPE_STREAM_ERROR }, |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
60 |
{ "stream_features", LM_MESSAGE_TYPE_STREAM_FEATURES }, |
17 | 61 |
{ NULL, LM_MESSAGE_TYPE_MESSAGE }, |
0 | 62 |
}; |
63 |
||
64 |
/// message sub type |
|
65 |
/// Message subtype, not all combinations of type and subtype are possible. |
|
4 | 66 |
/// G: |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
67 |
const string2enum_t sub_type_lm_message[] = { |
0 | 68 |
{ "not set", LM_MESSAGE_SUB_TYPE_NOT_SET }, |
69 |
{ "available", LM_MESSAGE_SUB_TYPE_AVAILABLE }, |
|
70 |
{ "normal", LM_MESSAGE_SUB_TYPE_NORMAL }, |
|
71 |
{ "chat", LM_MESSAGE_SUB_TYPE_CHAT }, |
|
72 |
{ "groupchat", LM_MESSAGE_SUB_TYPE_GROUPCHAT }, |
|
73 |
{ "headline", LM_MESSAGE_SUB_TYPE_HEADLINE }, |
|
74 |
{ "unavailable", LM_MESSAGE_SUB_TYPE_UNAVAILABLE }, |
|
75 |
{ "probe", LM_MESSAGE_SUB_TYPE_PROBE }, |
|
76 |
{ "subscribe", LM_MESSAGE_SUB_TYPE_SUBSCRIBE }, |
|
77 |
{ "unsubscribe", LM_MESSAGE_SUB_TYPE_UNSUBSCRIBE }, |
|
78 |
{ "subscribed", LM_MESSAGE_SUB_TYPE_SUBSCRIBED }, |
|
79 |
{ "unsubscribed", LM_MESSAGE_SUB_TYPE_UNSUBSCRIBED }, |
|
80 |
{ "get", LM_MESSAGE_SUB_TYPE_GET }, |
|
81 |
{ "set", LM_MESSAGE_SUB_TYPE_SET }, |
|
82 |
{ "result", LM_MESSAGE_SUB_TYPE_RESULT }, |
|
83 |
{ "error", LM_MESSAGE_SUB_TYPE_ERROR }, |
|
84 |
{ "not_set", LM_MESSAGE_SUB_TYPE_NOT_SET }, |
|
17 | 85 |
{ NULL, LM_MESSAGE_SUB_TYPE_NOT_SET }, |
0 | 86 |
}; |
87 |
||
88 |
/// lm.message.new |
|
89 |
/// Creates new message object. |
|
12 | 90 |
/// Note: you can specify nil as to argument to send message to server. |
0 | 91 |
/// A: string (to), message type, message sub type (optional) |
92 |
/// R: lm message object |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
93 |
static int new_lm_message (lua_State *L) |
0 | 94 |
{ |
12 | 95 |
const char *to = NULL; |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
96 |
int type = luaL_checkenum (L, 2, type_lm_message); |
0 | 97 |
LmMessage *message; |
12 | 98 |
|
99 |
if (lua_type (L, 1) != LUA_TNIL) |
|
100 |
to = luaL_checkstring (L, 1); |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
101 |
if (lua_gettop (L) > 2) |
0 | 102 |
message = lm_message_new_with_sub_type (to, type, |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
103 |
luaL_checkenum (L, 3, sub_type_lm_message)); |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
104 |
else |
0 | 105 |
message = lm_message_new (to, type); |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
106 |
bless_lm_message (L, message); |
0 | 107 |
lm_message_unref (message); |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
108 |
D ("Message %X created", (int) message); |
0 | 109 |
return 1; |
110 |
} |
|
111 |
||
17 | 112 |
/// message table |
113 |
/// Table describes xml structure of the message, the only exception is mtype key of root table. |
|
114 |
/// mtype is a string of form "<message type>-<message sub type>", eg "iq-set". |
|
115 |
/// Best way to learn how this table is organized, is just to look at next example: |
|
116 |
/// [ lm.message.create { mtype = 'iq-result', to = 'foo@bar.xyz', |
|
117 |
/// command = { xmlns = 'http://jabber.org/protocol/commands', node = 'http://jabber.org/protocol/rc#set-status', status = 'executing', sessionid = 'set-status:aaa3', |
|
118 |
/// x = { xmlns = 'jabber:x:data', type = 'form', |
|
119 |
/// title = { "Change Status" }, |
|
120 |
/// instructions = { "Choose the status and status message" }, |
|
121 |
/// field = {{ type = 'hidden', var = 'FORM_TYPE', |
|
122 |
/// value = { "http://jabber.org/protocol/rc" }, |
|
123 |
/// },{ type = 'list-single', label = 'Status', var = 'status', |
|
124 |
/// required = { }, |
|
125 |
/// value = { "online" }, |
|
126 |
/// option = {{ label = 'Chat', |
|
127 |
/// value = { "chat" }, |
|
128 |
/// },{ label = 'Online', |
|
129 |
/// value = { "online" }, |
|
130 |
/// },{ label = 'Away', |
|
131 |
/// value = { "away" }, |
|
132 |
/// },{ label = 'Extended Away', |
|
133 |
/// value = { "xa" }, |
|
134 |
/// },{ label = 'Do Not Disturb', |
|
135 |
/// value = { "dnd" }, |
|
136 |
/// },{ label = 'Invisible', |
|
137 |
/// value = { "invisible" }, |
|
138 |
/// },{ label = 'Offline', |
|
139 |
/// value = { "offline" }, |
|
140 |
/// }}, |
|
141 |
/// },{ type = 'text-single', label = 'Priority', var = 'status-priority', |
|
142 |
/// value = { "5" }, |
|
143 |
/// },{ type = 'text-multi', label = 'Message', var = 'status-message' }}, |
|
144 |
/// }, |
|
145 |
/// }, |
|
146 |
/// } |
|
147 |
/// ] |
|
148 |
static void fill_lm_node (lua_State *L, LmMessageNode *node, int index) |
|
149 |
{ |
|
150 |
int top = lua_gettop (L); // 0 |
|
151 |
for (lua_pushnil (L); lua_next (L, index) != 0; lua_pop (L, lua_gettop (L) - top - 1)) // 2 value |
|
152 |
if (lua_type (L, top + 2) == LUA_TTABLE) { |
|
153 |
const char *name = lua_tostring (L, top + 1); |
|
154 |
lua_pushinteger (L, 1); // 3 1 |
|
155 |
lua_gettable (L, top + 2); // 3 value[1] |
|
156 |
if (lua_type (L, top + 3) == LUA_TTABLE) { |
|
157 |
int i = 1; |
|
158 |
do { |
|
159 |
fill_lm_node (L, lm_message_node_add_child (node, name, NULL), top + 3); |
|
160 |
lua_pop (L, 1); // 2 value |
|
161 |
lua_pushinteger (L, ++i); // 3 i |
|
162 |
lua_gettable (L, top + 2); // 3 value[i] |
|
163 |
} while (lua_type (L, top + 3) == LUA_TTABLE); |
|
164 |
} else |
|
165 |
fill_lm_node (L, lm_message_node_add_child (node, name, NULL), top + 2); |
|
166 |
} else if (lua_type (L, top + 1) == LUA_TNUMBER && lua_tointeger (L, top + 1) == 1) |
|
167 |
lm_message_node_set_value (node, lua_tostring (L, top + 2)); |
|
168 |
else |
|
169 |
lm_message_node_set_attribute (node, lua_tostring (L, top + 1), lua_tostring (L, top + 2)); |
|
170 |
} |
|
171 |
||
172 |
/// lm.message.create |
|
173 |
/// Creates new message object and fills it from message table. |
|
174 |
/// Note, that table fields are not checked for their types, so, on wrong input results may be undefined. |
|
175 |
/// A: message table |
|
176 |
/// R: lm message object |
|
177 |
static int create_lm_message (lua_State *L) |
|
178 |
{ |
|
179 |
const char *mtype; |
|
180 |
const char *st; |
|
181 |
const char *to = NULL; |
|
182 |
LmMessage *message; |
|
183 |
luaL_checktype (L, 1, LUA_TTABLE); |
|
184 |
||
185 |
lua_getfield (L, 1, "mtype"); |
|
186 |
mtype = lua_tostring (L, -1); |
|
187 |
st = strchr (mtype, '-'); |
|
188 |
||
189 |
lua_getfield (L, 1, "to"); |
|
190 |
if (lua_type (L, -1) == LUA_TSTRING) |
|
191 |
to = lua_tostring (L, -1); |
|
192 |
||
193 |
if (st) { |
|
194 |
LmMessageType mt; |
|
195 |
lua_pushlstring (L, mtype, st - mtype); |
|
196 |
mt = luaL_checkenum (L, -1, type_lm_message); |
|
197 |
lua_pop (L, 1); |
|
198 |
message = lm_message_new_with_sub_type (to, mt, string2enum (st + 1, sub_type_lm_message)); |
|
199 |
} else |
|
200 |
message = lm_message_new (to, luaL_checkenum (L, -2, type_lm_message)); |
|
201 |
||
202 |
lua_pop (L, 2); |
|
203 |
lua_pushnil (L); |
|
204 |
lua_setfield (L, 1, "mtype"); |
|
205 |
lua_pushnil (L); |
|
206 |
lua_setfield (L, 1, "to"); |
|
207 |
||
208 |
fill_lm_node (L, lm_message_get_node (message), 1); |
|
209 |
||
210 |
bless_lm_message (L, message); |
|
211 |
lm_message_unref (message); |
|
212 |
D ("Message %X created", (int) message); |
|
213 |
||
214 |
return 1; |
|
215 |
} |
|
216 |
||
0 | 217 |
/// lm.message.bless |
218 |
/// Blesses given pointer to lm message object. |
|
219 |
/// A: lightuserdata (C lm message object) |
|
220 |
/// R: lm message object |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
221 |
static int bless_lua_lm_message (lua_State *L) |
0 | 222 |
{ |
223 |
luaL_argcheck (L, lua_islightuserdata (L, 1), 1, "lm message lightuserdata expected"); |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
224 |
bless_lm_message (L, lua_touserdata (L, 1)); |
0 | 225 |
return 1; |
226 |
} |
|
227 |
||
228 |
/// message:node |
|
229 |
/// Returns root node object of message. |
|
230 |
/// R: lm message node object |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
231 |
static int node_lm_message (lua_State *L) |
0 | 232 |
{ |
233 |
llm_message_t *object = luaL_checklm_message (L, 1); |
|
234 |
LmMessageNode *node = lm_message_get_node (object->message); |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
235 |
bless_lm_node (L, node); |
0 | 236 |
// XXX lm_message_node_unref (node); |
237 |
return 1; |
|
238 |
} |
|
239 |
||
240 |
/// message:type |
|
241 |
/// Returns two strings: message type and message sub type. |
|
242 |
/// R: message type, message sub type |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
243 |
static int kind_lm_message (lua_State *L) |
0 | 244 |
{ |
245 |
llm_message_t *object = luaL_checklm_message (L, 1); |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
246 |
luaL_pushenum (L, lm_message_get_type (object->message), type_lm_message); |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
247 |
luaL_pushenum (L, lm_message_get_sub_type (object->message), sub_type_lm_message); |
0 | 248 |
return 2; |
249 |
} |
|
250 |
||
251 |
/// message:pointer |
|
252 |
/// Returns pointer to underlying C loudmouth structure. |
|
253 |
/// R: lightuserdata |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
254 |
static int pointer_lm_message (lua_State *L) |
0 | 255 |
{ |
256 |
llm_message_t *object = luaL_checklm_message (L, 1); |
|
257 |
lua_pushlightuserdata (L, object->message); |
|
258 |
return 1; |
|
259 |
} |
|
260 |
||
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
261 |
static int gc_lm_message (lua_State *L) |
0 | 262 |
{ |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
263 |
llm_message_t *message = luaL_checklm_message (L, 1); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
264 |
D ("Message %X gc called", (int) message); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
265 |
lm_message_unref (message->message); |
0 | 266 |
return 0; |
267 |
} |
|
268 |
||
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
269 |
static const luaL_Reg reg_f_lm_message[] = { |
17 | 270 |
{ "new", new_lm_message }, |
271 |
{ "create", create_lm_message }, |
|
272 |
{ "bless", bless_lua_lm_message }, |
|
273 |
{ NULL, NULL }, |
|
0 | 274 |
}; |
275 |
||
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
276 |
static const luaL_Reg reg_m_lm_message[] = { |
12 | 277 |
{ "node", node_lm_message }, |
278 |
{ "type", kind_lm_message }, |
|
7
5db1448eb857
Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
279 |
// These methods are common for message and message node |
17 | 280 |
{ "name", name_lm_node }, |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
281 |
{ "next", next_lm_node }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
282 |
{ "prev", prev_lm_node }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
283 |
{ "parent", parent_lm_node }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
284 |
{ "value", value_lm_node }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
285 |
{ "child", child_lm_node }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
286 |
{ "find_child", find_child_lm_node }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
287 |
{ "attribute", attribute_lm_node }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
288 |
{ "raw", raw_lm_node }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
289 |
{ "xml", xml_lm_node }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
290 |
{ "path", path_lm_node }, |
7
5db1448eb857
Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
291 |
// End common methods |
12 | 292 |
{ "pointer", pointer_lm_message }, |
293 |
{ "__gc", gc_lm_message }, |
|
294 |
{ NULL, NULL }, |
|
0 | 295 |
}; |
296 |
||
297 |
int luaopen_lm_message (lua_State *L) |
|
298 |
{ |
|
299 |
luaL_newmetatable (L, "loudmouth.message"); |
|
19
d775d7289fe4
Use lua_pushliteral and lua_setfield
Myhailo Danylenko <isbear@ukrpost.net>
parents:
18
diff
changeset
|
300 |
lua_pushvalue (L, -1); |
d775d7289fe4
Use lua_pushliteral and lua_setfield
Myhailo Danylenko <isbear@ukrpost.net>
parents:
18
diff
changeset
|
301 |
lua_setfield (L, -2, "__index"); |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
302 |
luaL_register (L, NULL, reg_m_lm_message); |
0 | 303 |
lua_pop (L, 1); |
16
09b375e9ce32
Switch to new module organization scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
304 |
lua_newtable (L); // XXX we can specify here exact amount of fields |
09b375e9ce32
Switch to new module organization scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
305 |
luaL_register (L, NULL, reg_f_lm_message); |
0 | 306 |
return 1; |
307 |
} |
|
308 |