author | Mikael Hallendal <micke@imendio.com> |
Sun, 25 Feb 2007 00:13:01 +0100 | |
changeset 252 | 6aefcc6153f4 |
parent 99 | 81e9d035de02 |
child 215 | 9d68d8a77e7b |
permissions | -rw-r--r-- |
1 | 1 |
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ |
2 |
/* |
|
99
81e9d035de02
2004-10-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
84
diff
changeset
|
3 |
* Copyright (C) 2003 Imendio AB |
1 | 4 |
* |
5 |
* This program is free software; you can redistribute it and/or |
|
6 |
* modify it under the terms of the GNU Lesser General Public License as |
|
7 |
* published by the Free Software Foundation; either version 2 of the |
|
8 |
* License, or (at your option) any later version. |
|
9 |
* |
|
10 |
* This program is distributed in the hope that it will be useful, |
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 |
* Lesser General Public License for more details. |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU Lesser General Public |
|
16 |
* License along with this program; if not, write to the |
|
17 |
* Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
|
18 |
* Boston, MA 02111-1307, USA. |
|
19 |
*/ |
|
20 |
||
21 |
#include <config.h> |
|
22 |
#include <string.h> |
|
23 |
||
24 |
#include "lm-internals.h" |
|
25 |
#include "lm-message.h" |
|
26 |
||
27 |
#define PRIV(o) ((LmMessage *)o)->priv |
|
28 |
||
29 |
static struct TypeNames |
|
30 |
{ |
|
31 |
LmMessageType type; |
|
32 |
const gchar *name; |
|
33 |
} type_names[] = { |
|
34 |
{ LM_MESSAGE_TYPE_MESSAGE, "message" }, |
|
35 |
{ LM_MESSAGE_TYPE_PRESENCE, "presence" }, |
|
36 |
{ LM_MESSAGE_TYPE_IQ, "iq" }, |
|
37 |
{ LM_MESSAGE_TYPE_STREAM, "stream:stream" }, |
|
38 |
{ LM_MESSAGE_TYPE_STREAM_ERROR, "stream:error" } |
|
39 |
}; |
|
40 |
||
41 |
static struct SubTypeNames |
|
42 |
{ |
|
43 |
LmMessageSubType type; |
|
44 |
const gchar *name; |
|
45 |
} sub_type_names[] = { |
|
23
b8a56b77da30
2003-07-29 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
8
diff
changeset
|
46 |
{ LM_MESSAGE_SUB_TYPE_NORMAL, "normal" }, |
1 | 47 |
{ LM_MESSAGE_SUB_TYPE_CHAT, "chat" }, |
48 |
{ LM_MESSAGE_SUB_TYPE_GROUPCHAT, "groupchat" }, |
|
49 |
{ LM_MESSAGE_SUB_TYPE_HEADLINE, "headline" }, |
|
50 |
{ LM_MESSAGE_SUB_TYPE_UNAVAILABLE, "unavailable" }, |
|
51 |
{ LM_MESSAGE_SUB_TYPE_PROBE, "probe" }, |
|
52 |
{ LM_MESSAGE_SUB_TYPE_SUBSCRIBE, "subscribe" }, |
|
53 |
{ LM_MESSAGE_SUB_TYPE_UNSUBSCRIBE, "unsubscribe" }, |
|
54 |
{ LM_MESSAGE_SUB_TYPE_SUBSCRIBED, "subscribed" }, |
|
55 |
{ LM_MESSAGE_SUB_TYPE_UNSUBSCRIBED, "unsubscribed" }, |
|
56 |
{ LM_MESSAGE_SUB_TYPE_GET, "get" }, |
|
57 |
{ LM_MESSAGE_SUB_TYPE_SET, "set" }, |
|
58 |
{ LM_MESSAGE_SUB_TYPE_RESULT, "result" }, |
|
59 |
{ LM_MESSAGE_SUB_TYPE_ERROR, "error" } |
|
60 |
}; |
|
61 |
||
62 |
struct LmMessagePriv { |
|
63 |
LmMessageType type; |
|
64 |
LmMessageSubType sub_type; |
|
65 |
gint ref_count; |
|
66 |
}; |
|
67 |
||
68 |
static LmMessageType |
|
69 |
message_type_from_string (const gchar *type_str) |
|
70 |
{ |
|
71 |
gint i; |
|
72 |
||
73 |
if (!type_str) { |
|
74 |
return LM_MESSAGE_TYPE_UNKNOWN; |
|
75 |
} |
|
76 |
||
77 |
for (i = LM_MESSAGE_TYPE_MESSAGE; |
|
78 |
i <= LM_MESSAGE_TYPE_STREAM_ERROR; |
|
79 |
++i) { |
|
7
d8eae83450ac
2003-06-13 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
4
diff
changeset
|
80 |
if (strcmp (type_str, type_names[i].name) == 0) { |
1 | 81 |
return i; |
82 |
} |
|
83 |
} |
|
84 |
||
85 |
return LM_MESSAGE_TYPE_UNKNOWN; |
|
86 |
} |
|
87 |
||
88 |
||
89 |
const gchar * |
|
90 |
_lm_message_type_to_string (LmMessageType type) |
|
91 |
{ |
|
92 |
if (type < LM_MESSAGE_TYPE_MESSAGE || |
|
93 |
type > LM_MESSAGE_TYPE_STREAM_ERROR) { |
|
94 |
type = LM_MESSAGE_TYPE_UNKNOWN; |
|
95 |
} |
|
96 |
||
97 |
return type_names[type].name; |
|
98 |
} |
|
99 |
||
100 |
static LmMessageSubType |
|
101 |
message_sub_type_from_string (const gchar *type_str) |
|
102 |
{ |
|
103 |
gint i; |
|
104 |
||
105 |
if (!type_str) { |
|
106 |
return LM_MESSAGE_SUB_TYPE_NOT_SET; |
|
107 |
} |
|
108 |
||
23
b8a56b77da30
2003-07-29 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
8
diff
changeset
|
109 |
for (i = LM_MESSAGE_SUB_TYPE_NORMAL; |
1 | 110 |
i <= LM_MESSAGE_SUB_TYPE_ERROR; |
111 |
++i) { |
|
112 |
if (g_ascii_strcasecmp (type_str, |
|
113 |
sub_type_names[i].name) == 0) { |
|
114 |
return i; |
|
115 |
} |
|
116 |
} |
|
117 |
||
118 |
return LM_MESSAGE_SUB_TYPE_NOT_SET; |
|
119 |
} |
|
120 |
||
121 |
const gchar * |
|
122 |
_lm_message_sub_type_to_string (LmMessageSubType type) |
|
123 |
{ |
|
23
b8a56b77da30
2003-07-29 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
8
diff
changeset
|
124 |
if (type < LM_MESSAGE_SUB_TYPE_NORMAL || |
1 | 125 |
type > LM_MESSAGE_SUB_TYPE_ERROR) { |
126 |
return NULL; |
|
127 |
} |
|
128 |
||
129 |
return sub_type_names[type].name; |
|
130 |
} |
|
131 |
||
132 |
static LmMessageSubType |
|
133 |
message_sub_type_when_unset (LmMessageType type) { |
|
134 |
LmMessageSubType sub_type = LM_MESSAGE_SUB_TYPE_NORMAL; |
|
135 |
||
136 |
switch (type) { |
|
137 |
case LM_MESSAGE_TYPE_MESSAGE: |
|
23
b8a56b77da30
2003-07-29 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
8
diff
changeset
|
138 |
/* A message without type should be handled like a message with |
b8a56b77da30
2003-07-29 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
8
diff
changeset
|
139 |
* type=normal, but we won't set it to that since then the user |
b8a56b77da30
2003-07-29 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
8
diff
changeset
|
140 |
* will not know if it's set or not. |
b8a56b77da30
2003-07-29 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
8
diff
changeset
|
141 |
*/ |
b8a56b77da30
2003-07-29 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
8
diff
changeset
|
142 |
sub_type = LM_MESSAGE_SUB_TYPE_NOT_SET; |
1 | 143 |
break; |
144 |
case LM_MESSAGE_TYPE_PRESENCE: |
|
145 |
sub_type = LM_MESSAGE_SUB_TYPE_AVAILABLE; |
|
146 |
break; |
|
147 |
case LM_MESSAGE_TYPE_IQ: |
|
148 |
sub_type = LM_MESSAGE_SUB_TYPE_GET; |
|
149 |
break; |
|
150 |
default: |
|
151 |
break; |
|
152 |
} |
|
153 |
||
154 |
return sub_type; |
|
155 |
} |
|
156 |
||
157 |
LmMessage * |
|
158 |
_lm_message_new_from_node (LmMessageNode *node) |
|
159 |
{ |
|
160 |
LmMessage *m; |
|
161 |
LmMessageType type; |
|
162 |
LmMessageSubType sub_type; |
|
163 |
const gchar *sub_type_str; |
|
164 |
||
165 |
type = message_type_from_string (node->name); |
|
166 |
||
167 |
if (type == LM_MESSAGE_TYPE_UNKNOWN) { |
|
168 |
return NULL; |
|
169 |
} |
|
170 |
||
171 |
sub_type_str = lm_message_node_get_attribute (node, "type"); |
|
172 |
if (sub_type_str) { |
|
173 |
sub_type = message_sub_type_from_string (sub_type_str); |
|
174 |
} else { |
|
175 |
sub_type = message_sub_type_when_unset (type); |
|
176 |
} |
|
177 |
||
178 |
m = g_new0 (LmMessage, 1); |
|
179 |
m->priv = g_new0 (LmMessagePriv, 1); |
|
180 |
||
181 |
PRIV(m)->ref_count = 1; |
|
182 |
PRIV(m)->type = type; |
|
183 |
PRIV(m)->sub_type = sub_type; |
|
184 |
||
185 |
m->node = lm_message_node_ref (node); |
|
186 |
||
187 |
return m; |
|
188 |
} |
|
189 |
||
190 |
/** |
|
191 |
* lm_message_new: |
|
192 |
* @to: receipient jid |
|
193 |
* @type: message type |
|
194 |
* |
|
195 |
* Creates a new #LmMessage which can be sent with lm_connection_send() or |
|
196 |
* lm_connection_send_with_reply(). If @to is %NULL the message is sent to the |
|
197 |
* server. The returned message should be unreferenced with lm_message_unref() |
|
198 |
* when caller is finished with it. |
|
199 |
* |
|
200 |
* Return value: a newly created #LmMessage |
|
201 |
**/ |
|
202 |
LmMessage * |
|
203 |
lm_message_new (const gchar *to, LmMessageType type) |
|
204 |
{ |
|
79 | 205 |
LmMessage *m; |
206 |
gchar *id; |
|
207 |
||
208 |
m = g_new0 (LmMessage, 1); |
|
1 | 209 |
m->priv = g_new0 (LmMessagePriv, 1); |
210 |
||
211 |
PRIV(m)->ref_count = 1; |
|
212 |
PRIV(m)->type = type; |
|
213 |
PRIV(m)->sub_type = message_sub_type_when_unset (type); |
|
214 |
||
215 |
m->node = _lm_message_node_new (_lm_message_type_to_string (type)); |
|
49
66fd32fc225e
2003-10-13 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
24
diff
changeset
|
216 |
|
79 | 217 |
id = _lm_utils_generate_id (); |
218 |
lm_message_node_set_attribute (m->node, "id", id); |
|
219 |
g_free (id); |
|
1 | 220 |
|
221 |
if (to) { |
|
222 |
lm_message_node_set_attribute (m->node, "to", to); |
|
223 |
} |
|
224 |
||
225 |
if (type == LM_MESSAGE_TYPE_IQ) { |
|
226 |
lm_message_node_set_attribute (m->node, "type", "get"); |
|
227 |
} |
|
228 |
||
229 |
return m; |
|
230 |
} |
|
231 |
||
232 |
/** |
|
233 |
* lm_message_new_with_sub_type: |
|
234 |
* @to: receipient jid |
|
235 |
* @type: message type |
|
236 |
* @sub_type: message sub type |
|
237 |
* |
|
238 |
* Creates a new #LmMessage with sub type set. See lm_message_new() for more |
|
239 |
* information. |
|
240 |
* |
|
241 |
* Return value: a newly created #LmMessage |
|
242 |
**/ |
|
243 |
LmMessage * |
|
244 |
lm_message_new_with_sub_type (const gchar *to, |
|
245 |
LmMessageType type, |
|
246 |
LmMessageSubType sub_type) |
|
247 |
{ |
|
79 | 248 |
LmMessage *m; |
1 | 249 |
const gchar *type_str; |
250 |
||
79 | 251 |
m = lm_message_new (to, type); |
252 |
||
1 | 253 |
type_str = _lm_message_sub_type_to_string (sub_type); |
254 |
||
255 |
if (type_str) { |
|
256 |
lm_message_node_set_attributes (m->node, |
|
257 |
"type", type_str, NULL); |
|
258 |
PRIV(m)->sub_type = sub_type; |
|
259 |
} |
|
260 |
||
261 |
return m; |
|
262 |
} |
|
263 |
||
264 |
/** |
|
265 |
* lm_message_get_type: |
|
266 |
* @message: an #LmMessage |
|
267 |
* |
|
268 |
* Fetches the type of @message. |
|
269 |
* |
|
270 |
* Return value: the message type |
|
271 |
**/ |
|
272 |
LmMessageType |
|
273 |
lm_message_get_type (LmMessage *message) |
|
274 |
{ |
|
275 |
g_return_val_if_fail (message != NULL, LM_MESSAGE_TYPE_UNKNOWN); |
|
276 |
||
277 |
return PRIV(message)->type; |
|
278 |
} |
|
279 |
||
280 |
/** |
|
281 |
* lm_message_get_sub_type: |
|
282 |
* @message: |
|
283 |
* |
|
284 |
* Fetches the sub type of @message. |
|
285 |
* |
|
286 |
* Return value: the message sub type |
|
287 |
**/ |
|
8
33d64cf345be
2003-06-15 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
7
diff
changeset
|
288 |
LmMessageSubType |
1 | 289 |
lm_message_get_sub_type (LmMessage *message) |
290 |
{ |
|
291 |
g_return_val_if_fail (message != NULL, LM_MESSAGE_TYPE_UNKNOWN); |
|
292 |
||
293 |
return PRIV(message)->sub_type; |
|
294 |
} |
|
295 |
||
296 |
/** |
|
297 |
* lm_message_get_node: |
|
298 |
* @message: an #LmMessage |
|
299 |
* |
|
300 |
* Retrieves the root node from @message. |
|
301 |
* |
|
302 |
* Return value: an #LmMessageNode |
|
303 |
**/ |
|
304 |
LmMessageNode * |
|
305 |
lm_message_get_node (LmMessage *message) |
|
306 |
{ |
|
307 |
g_return_val_if_fail (message != NULL, NULL); |
|
308 |
||
309 |
return message->node; |
|
310 |
} |
|
311 |
||
312 |
/** |
|
313 |
* lm_message_ref: |
|
314 |
* @message: an #LmMessage |
|
315 |
* |
|
316 |
* Adds a reference to @message. |
|
317 |
* |
|
318 |
* Return value: the message |
|
319 |
**/ |
|
320 |
LmMessage * |
|
321 |
lm_message_ref (LmMessage *message) |
|
322 |
{ |
|
323 |
g_return_val_if_fail (message != NULL, NULL); |
|
324 |
||
325 |
PRIV(message)->ref_count++; |
|
326 |
||
327 |
return message; |
|
328 |
} |
|
329 |
||
330 |
/** |
|
331 |
* lm_message_unref: |
|
332 |
* @message: an #LmMessage |
|
333 |
* |
|
334 |
* Removes a reference from @message. When no more references are present the |
|
335 |
* message is freed. |
|
336 |
**/ |
|
337 |
void |
|
338 |
lm_message_unref (LmMessage *message) |
|
339 |
{ |
|
340 |
g_return_if_fail (message != NULL); |
|
341 |
||
342 |
PRIV(message)->ref_count--; |
|
343 |
||
344 |
if (PRIV(message)->ref_count == 0) { |
|
345 |
lm_message_node_unref (message->node); |
|
79 | 346 |
g_free (message->priv); |
1 | 347 |
g_free (message); |
348 |
} |
|
349 |
} |