author | Myhailo Danylenko <isbear@ukrpost.net> |
Sun, 08 Mar 2009 00:48:19 +0200 | |
changeset 11 | a8c6460d612b |
parent 8 | 951f92c66821 |
child 12 | 63f06a23c235 |
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. |
|
72 |
/// A: string (to), message type, message sub type (optional) |
|
73 |
/// R: lm message object |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
74 |
static int new_lm_message (lua_State *L) |
0 | 75 |
{ |
76 |
const char *to = luaL_checkstring (L, 1); |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
77 |
int type = luaL_checkenum (L, 2, type_lm_message); |
0 | 78 |
LmMessage *message; |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
79 |
if (lua_gettop (L) > 2) |
0 | 80 |
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
|
81 |
luaL_checkenum (L, 3, sub_type_lm_message)); |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
82 |
else |
0 | 83 |
message = lm_message_new (to, type); |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
84 |
bless_lm_message (L, message); |
0 | 85 |
lm_message_unref (message); |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
86 |
D ("Message %X created", (int) message); |
0 | 87 |
return 1; |
88 |
} |
|
89 |
||
90 |
/// lm.message.bless |
|
91 |
/// Blesses given pointer to lm message object. |
|
92 |
/// A: lightuserdata (C lm message object) |
|
93 |
/// R: lm message object |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
94 |
static int bless_lua_lm_message (lua_State *L) |
0 | 95 |
{ |
96 |
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
|
97 |
bless_lm_message (L, lua_touserdata (L, 1)); |
0 | 98 |
return 1; |
99 |
} |
|
100 |
||
101 |
/// message:node |
|
102 |
/// Returns root node object of message. |
|
103 |
/// R: lm message node object |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
104 |
static int node_lm_message (lua_State *L) |
0 | 105 |
{ |
106 |
llm_message_t *object = luaL_checklm_message (L, 1); |
|
107 |
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
|
108 |
bless_lm_node (L, node); |
0 | 109 |
// XXX lm_message_node_unref (node); |
110 |
return 1; |
|
111 |
} |
|
112 |
||
113 |
/// message:type |
|
114 |
/// Returns two strings: message type and message sub type. |
|
115 |
/// R: message type, message sub type |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
116 |
static int kind_lm_message (lua_State *L) |
0 | 117 |
{ |
118 |
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
|
119 |
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
|
120 |
luaL_pushenum (L, lm_message_get_sub_type (object->message), sub_type_lm_message); |
0 | 121 |
return 2; |
122 |
} |
|
123 |
||
124 |
/// message:pointer |
|
125 |
/// Returns pointer to underlying C loudmouth structure. |
|
126 |
/// R: lightuserdata |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
127 |
static int pointer_lm_message (lua_State *L) |
0 | 128 |
{ |
129 |
llm_message_t *object = luaL_checklm_message (L, 1); |
|
130 |
lua_pushlightuserdata (L, object->message); |
|
131 |
return 1; |
|
132 |
} |
|
133 |
||
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
134 |
static int gc_lm_message (lua_State *L) |
0 | 135 |
{ |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
136 |
llm_message_t *message = luaL_checklm_message (L, 1); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
137 |
D ("Message %X gc called", (int) message); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
138 |
lm_message_unref (message->message); |
0 | 139 |
return 0; |
140 |
} |
|
141 |
||
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
142 |
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
|
143 |
{ "new", new_lm_message }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
144 |
{ "bless", bless_lua_lm_message }, |
0 | 145 |
{ NULL, NULL }, |
146 |
}; |
|
147 |
||
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
148 |
static const luaL_Reg reg_m_lm_message[] = { |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
149 |
{ "node", node_lm_message }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
150 |
{ "type", kind_lm_message }, |
7
5db1448eb857
Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
151 |
// 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
|
152 |
{ "next", next_lm_node }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
153 |
{ "prev", prev_lm_node }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
154 |
{ "children", children_lm_node }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
155 |
{ "parent", parent_lm_node }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
156 |
{ "value", value_lm_node }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
157 |
{ "child", child_lm_node }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
158 |
{ "find_child", find_child_lm_node }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
159 |
{ "attribute", attribute_lm_node }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
160 |
{ "raw", raw_lm_node }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
161 |
{ "xml", xml_lm_node }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
162 |
{ "path", path_lm_node }, |
7
5db1448eb857
Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
163 |
// End common methods |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
164 |
{ "pointer", pointer_lm_message }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
165 |
{ "__gc", gc_lm_message }, |
7
5db1448eb857
Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
166 |
{ NULL, NULL }, |
0 | 167 |
}; |
168 |
||
169 |
int luaopen_lm_message (lua_State *L) |
|
170 |
{ |
|
171 |
luaL_newmetatable (L, "loudmouth.message"); |
|
172 |
lua_pushstring (L, "__index"); |
|
173 |
lua_pushvalue (L, -2); |
|
174 |
lua_settable (L, -3); |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
175 |
luaL_register (L, NULL, reg_m_lm_message); |
0 | 176 |
lua_pop (L, 1); |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
177 |
luaL_register (L, "lm.message", reg_f_lm_message); |
0 | 178 |
return 1; |
179 |
} |
|
180 |