author | Myhailo Danylenko <isbear@ukrpost.net> |
Sat, 14 Mar 2009 02:41:26 +0200 | |
changeset 12 | 63f06a23c235 |
parent 11 | a8c6460d612b |
child 16 | 09b375e9ce32 |
permissions | -rw-r--r-- |
0 | 1 |
|
2 |
#include <lua.h> |
|
3 |
#include <lauxlib.h> |
|
4 |
#include <loudmouth/loudmouth.h> |
|
5 |
||
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
6 |
#include "config.h" |
0 | 7 |
#include "util.h" |
8 |
#include "lm_types.h" |
|
7
5db1448eb857
Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
9 |
#include "lm_message_node.h" |
0 | 10 |
|
11 |
/// lm.message |
|
12 |
/// Module, representing individual message. |
|
13 |
/// Message have a type and optionally a sub type. |
|
8 | 14 |
/// Message have a set common methods with message node, |
15 |
/// these are next, prev, children, parent, value, child, |
|
16 |
/// find_child, attribute, raw, xml and path. They just save |
|
17 |
/// you typing :node() each time and save memory for |
|
18 |
/// one node object. |
|
0 | 19 |
|
20 |
/// message type |
|
21 |
/// Message type (root tag type). |
|
4 | 22 |
/// G: |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
23 |
const string2enum_t type_lm_message[] = { |
0 | 24 |
{ "message", LM_MESSAGE_TYPE_MESSAGE }, |
25 |
{ "presence", LM_MESSAGE_TYPE_PRESENCE }, |
|
26 |
{ "iq", LM_MESSAGE_TYPE_IQ }, |
|
27 |
{ "stream", LM_MESSAGE_TYPE_STREAM }, |
|
28 |
{ "stream error", LM_MESSAGE_TYPE_STREAM_ERROR }, |
|
29 |
{ "stream features", LM_MESSAGE_TYPE_STREAM_FEATURES }, |
|
30 |
{ "auth", LM_MESSAGE_TYPE_AUTH }, |
|
31 |
{ "challenge", LM_MESSAGE_TYPE_CHALLENGE }, |
|
32 |
{ "response", LM_MESSAGE_TYPE_RESPONSE }, |
|
33 |
{ "success", LM_MESSAGE_TYPE_SUCCESS }, |
|
34 |
{ "failure", LM_MESSAGE_TYPE_FAILURE }, |
|
35 |
{ "proceed", LM_MESSAGE_TYPE_PROCEED }, |
|
36 |
{ "starttls", LM_MESSAGE_TYPE_STARTTLS }, |
|
37 |
{ "unknown", LM_MESSAGE_TYPE_UNKNOWN }, |
|
38 |
{ "stream:stream", LM_MESSAGE_TYPE_STREAM }, |
|
39 |
{ "stream:error", LM_MESSAGE_TYPE_STREAM_ERROR }, |
|
40 |
{ "stream:features", LM_MESSAGE_TYPE_STREAM_FEATURES }, |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
41 |
{ "stream_error", LM_MESSAGE_TYPE_STREAM_ERROR }, |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
42 |
{ "stream_features", LM_MESSAGE_TYPE_STREAM_FEATURES }, |
0 | 43 |
{ NULL, 0 }, |
44 |
}; |
|
45 |
||
46 |
/// message sub type |
|
47 |
/// Message subtype, not all combinations of type and subtype are possible. |
|
4 | 48 |
/// G: |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
49 |
const string2enum_t sub_type_lm_message[] = { |
0 | 50 |
{ "not set", LM_MESSAGE_SUB_TYPE_NOT_SET }, |
51 |
{ "available", LM_MESSAGE_SUB_TYPE_AVAILABLE }, |
|
52 |
{ "normal", LM_MESSAGE_SUB_TYPE_NORMAL }, |
|
53 |
{ "chat", LM_MESSAGE_SUB_TYPE_CHAT }, |
|
54 |
{ "groupchat", LM_MESSAGE_SUB_TYPE_GROUPCHAT }, |
|
55 |
{ "headline", LM_MESSAGE_SUB_TYPE_HEADLINE }, |
|
56 |
{ "unavailable", LM_MESSAGE_SUB_TYPE_UNAVAILABLE }, |
|
57 |
{ "probe", LM_MESSAGE_SUB_TYPE_PROBE }, |
|
58 |
{ "subscribe", LM_MESSAGE_SUB_TYPE_SUBSCRIBE }, |
|
59 |
{ "unsubscribe", LM_MESSAGE_SUB_TYPE_UNSUBSCRIBE }, |
|
60 |
{ "subscribed", LM_MESSAGE_SUB_TYPE_SUBSCRIBED }, |
|
61 |
{ "unsubscribed", LM_MESSAGE_SUB_TYPE_UNSUBSCRIBED }, |
|
62 |
{ "get", LM_MESSAGE_SUB_TYPE_GET }, |
|
63 |
{ "set", LM_MESSAGE_SUB_TYPE_SET }, |
|
64 |
{ "result", LM_MESSAGE_SUB_TYPE_RESULT }, |
|
65 |
{ "error", LM_MESSAGE_SUB_TYPE_ERROR }, |
|
66 |
{ "not_set", LM_MESSAGE_SUB_TYPE_NOT_SET }, |
|
67 |
{ NULL, 0 }, |
|
68 |
}; |
|
69 |
||
70 |
/// lm.message.new |
|
71 |
/// Creates new message object. |
|
12 | 72 |
/// Note: you can specify nil as to argument to send message to server. |
0 | 73 |
/// A: string (to), message type, message sub type (optional) |
74 |
/// R: lm message object |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
75 |
static int new_lm_message (lua_State *L) |
0 | 76 |
{ |
12 | 77 |
const char *to = NULL; |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
78 |
int type = luaL_checkenum (L, 2, type_lm_message); |
0 | 79 |
LmMessage *message; |
12 | 80 |
|
81 |
if (lua_type (L, 1) != LUA_TNIL) |
|
82 |
to = luaL_checkstring (L, 1); |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
83 |
if (lua_gettop (L) > 2) |
0 | 84 |
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
|
85 |
luaL_checkenum (L, 3, sub_type_lm_message)); |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
86 |
else |
0 | 87 |
message = lm_message_new (to, type); |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
88 |
bless_lm_message (L, message); |
0 | 89 |
lm_message_unref (message); |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
90 |
D ("Message %X created", (int) message); |
0 | 91 |
return 1; |
92 |
} |
|
93 |
||
94 |
/// lm.message.bless |
|
95 |
/// Blesses given pointer to lm message object. |
|
96 |
/// A: lightuserdata (C lm message object) |
|
97 |
/// R: lm message object |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
98 |
static int bless_lua_lm_message (lua_State *L) |
0 | 99 |
{ |
100 |
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
|
101 |
bless_lm_message (L, lua_touserdata (L, 1)); |
0 | 102 |
return 1; |
103 |
} |
|
104 |
||
105 |
/// message:node |
|
106 |
/// Returns root node object of message. |
|
107 |
/// R: lm message node object |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
108 |
static int node_lm_message (lua_State *L) |
0 | 109 |
{ |
110 |
llm_message_t *object = luaL_checklm_message (L, 1); |
|
111 |
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
|
112 |
bless_lm_node (L, node); |
0 | 113 |
// XXX lm_message_node_unref (node); |
114 |
return 1; |
|
115 |
} |
|
116 |
||
117 |
/// message:type |
|
118 |
/// Returns two strings: message type and message sub type. |
|
119 |
/// R: message type, message sub type |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
120 |
static int kind_lm_message (lua_State *L) |
0 | 121 |
{ |
122 |
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
|
123 |
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
|
124 |
luaL_pushenum (L, lm_message_get_sub_type (object->message), sub_type_lm_message); |
0 | 125 |
return 2; |
126 |
} |
|
127 |
||
128 |
/// message:pointer |
|
129 |
/// Returns pointer to underlying C loudmouth structure. |
|
130 |
/// R: lightuserdata |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
131 |
static int pointer_lm_message (lua_State *L) |
0 | 132 |
{ |
133 |
llm_message_t *object = luaL_checklm_message (L, 1); |
|
134 |
lua_pushlightuserdata (L, object->message); |
|
135 |
return 1; |
|
136 |
} |
|
137 |
||
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
138 |
static int gc_lm_message (lua_State *L) |
0 | 139 |
{ |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
140 |
llm_message_t *message = luaL_checklm_message (L, 1); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
141 |
D ("Message %X gc called", (int) message); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
142 |
lm_message_unref (message->message); |
0 | 143 |
return 0; |
144 |
} |
|
145 |
||
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
146 |
static const luaL_Reg reg_f_lm_message[] = { |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
147 |
{ "new", new_lm_message }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
148 |
{ "bless", bless_lua_lm_message }, |
12 | 149 |
{ NULL, NULL }, |
0 | 150 |
}; |
151 |
||
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
152 |
static const luaL_Reg reg_m_lm_message[] = { |
12 | 153 |
{ "node", node_lm_message }, |
154 |
{ "type", kind_lm_message }, |
|
7
5db1448eb857
Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
155 |
// These methods are common for message and message node |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
156 |
{ "next", next_lm_node }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
157 |
{ "prev", prev_lm_node }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
158 |
{ "children", children_lm_node }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
159 |
{ "parent", parent_lm_node }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
160 |
{ "value", value_lm_node }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
161 |
{ "child", child_lm_node }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
162 |
{ "find_child", find_child_lm_node }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
163 |
{ "attribute", attribute_lm_node }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
164 |
{ "raw", raw_lm_node }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
165 |
{ "xml", xml_lm_node }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
166 |
{ "path", path_lm_node }, |
7
5db1448eb857
Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
167 |
// End common methods |
12 | 168 |
{ "pointer", pointer_lm_message }, |
169 |
{ "__gc", gc_lm_message }, |
|
170 |
{ NULL, NULL }, |
|
0 | 171 |
}; |
172 |
||
173 |
int luaopen_lm_message (lua_State *L) |
|
174 |
{ |
|
175 |
luaL_newmetatable (L, "loudmouth.message"); |
|
176 |
lua_pushstring (L, "__index"); |
|
177 |
lua_pushvalue (L, -2); |
|
178 |
lua_settable (L, -3); |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
179 |
luaL_register (L, NULL, reg_m_lm_message); |
0 | 180 |
lua_pop (L, 1); |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
181 |
luaL_register (L, "lm.message", reg_f_lm_message); |
0 | 182 |
return 1; |
183 |
} |
|
184 |