author | Senko Rasic <senko.rasic@collabora.co.uk> |
Sun, 30 Nov 2008 10:20:08 +0100 | |
changeset 575 | daad23d59b56 |
parent 557 | 47f6169c0f08 |
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[] = { |
|
215
9d68d8a77e7b
Added lm-message.[ch] changes to sasl branch
Mikael Hallendal <micke@imendio.com>
parents:
99
diff
changeset
|
34 |
{ LM_MESSAGE_TYPE_MESSAGE, "message" }, |
9d68d8a77e7b
Added lm-message.[ch] changes to sasl branch
Mikael Hallendal <micke@imendio.com>
parents:
99
diff
changeset
|
35 |
{ LM_MESSAGE_TYPE_PRESENCE, "presence" }, |
9d68d8a77e7b
Added lm-message.[ch] changes to sasl branch
Mikael Hallendal <micke@imendio.com>
parents:
99
diff
changeset
|
36 |
{ LM_MESSAGE_TYPE_IQ, "iq" }, |
9d68d8a77e7b
Added lm-message.[ch] changes to sasl branch
Mikael Hallendal <micke@imendio.com>
parents:
99
diff
changeset
|
37 |
{ LM_MESSAGE_TYPE_STREAM, "stream:stream" }, |
9d68d8a77e7b
Added lm-message.[ch] changes to sasl branch
Mikael Hallendal <micke@imendio.com>
parents:
99
diff
changeset
|
38 |
{ LM_MESSAGE_TYPE_STREAM_FEATURES, "stream:features" }, |
331
884c0d7edefc
Properly detect message type; don't rely on lookup table ordering if the type is explicitly specified.
Senko Rasic <senko.rasic@collabora.co.uk>
parents:
274
diff
changeset
|
39 |
{ LM_MESSAGE_TYPE_STREAM_ERROR, "stream:error" }, |
215
9d68d8a77e7b
Added lm-message.[ch] changes to sasl branch
Mikael Hallendal <micke@imendio.com>
parents:
99
diff
changeset
|
40 |
{ LM_MESSAGE_TYPE_AUTH, "auth" }, |
9d68d8a77e7b
Added lm-message.[ch] changes to sasl branch
Mikael Hallendal <micke@imendio.com>
parents:
99
diff
changeset
|
41 |
{ LM_MESSAGE_TYPE_CHALLENGE, "challenge" }, |
9d68d8a77e7b
Added lm-message.[ch] changes to sasl branch
Mikael Hallendal <micke@imendio.com>
parents:
99
diff
changeset
|
42 |
{ LM_MESSAGE_TYPE_RESPONSE, "response" }, |
9d68d8a77e7b
Added lm-message.[ch] changes to sasl branch
Mikael Hallendal <micke@imendio.com>
parents:
99
diff
changeset
|
43 |
{ LM_MESSAGE_TYPE_SUCCESS, "success" }, |
9d68d8a77e7b
Added lm-message.[ch] changes to sasl branch
Mikael Hallendal <micke@imendio.com>
parents:
99
diff
changeset
|
44 |
{ LM_MESSAGE_TYPE_FAILURE, "failure" }, |
274
c32a7011e435
Adding StartTLS support.
Senko Rasic <senko@phyrexia.(none)>
parents:
215
diff
changeset
|
45 |
{ LM_MESSAGE_TYPE_PROCEED, "proceed" }, |
331
884c0d7edefc
Properly detect message type; don't rely on lookup table ordering if the type is explicitly specified.
Senko Rasic <senko.rasic@collabora.co.uk>
parents:
274
diff
changeset
|
46 |
{ LM_MESSAGE_TYPE_STARTTLS, "starttls" }, |
884c0d7edefc
Properly detect message type; don't rely on lookup table ordering if the type is explicitly specified.
Senko Rasic <senko.rasic@collabora.co.uk>
parents:
274
diff
changeset
|
47 |
{ LM_MESSAGE_TYPE_UNKNOWN, NULL } |
1 | 48 |
}; |
49 |
||
50 |
static struct SubTypeNames |
|
51 |
{ |
|
52 |
LmMessageSubType type; |
|
53 |
const gchar *name; |
|
54 |
} sub_type_names[] = { |
|
23
b8a56b77da30
2003-07-29 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
8
diff
changeset
|
55 |
{ LM_MESSAGE_SUB_TYPE_NORMAL, "normal" }, |
1 | 56 |
{ LM_MESSAGE_SUB_TYPE_CHAT, "chat" }, |
57 |
{ LM_MESSAGE_SUB_TYPE_GROUPCHAT, "groupchat" }, |
|
58 |
{ LM_MESSAGE_SUB_TYPE_HEADLINE, "headline" }, |
|
59 |
{ LM_MESSAGE_SUB_TYPE_UNAVAILABLE, "unavailable" }, |
|
60 |
{ LM_MESSAGE_SUB_TYPE_PROBE, "probe" }, |
|
61 |
{ LM_MESSAGE_SUB_TYPE_SUBSCRIBE, "subscribe" }, |
|
62 |
{ LM_MESSAGE_SUB_TYPE_UNSUBSCRIBE, "unsubscribe" }, |
|
63 |
{ LM_MESSAGE_SUB_TYPE_SUBSCRIBED, "subscribed" }, |
|
64 |
{ LM_MESSAGE_SUB_TYPE_UNSUBSCRIBED, "unsubscribed" }, |
|
65 |
{ LM_MESSAGE_SUB_TYPE_GET, "get" }, |
|
66 |
{ LM_MESSAGE_SUB_TYPE_SET, "set" }, |
|
67 |
{ LM_MESSAGE_SUB_TYPE_RESULT, "result" }, |
|
68 |
{ LM_MESSAGE_SUB_TYPE_ERROR, "error" } |
|
69 |
}; |
|
70 |
||
71 |
struct LmMessagePriv { |
|
72 |
LmMessageType type; |
|
73 |
LmMessageSubType sub_type; |
|
74 |
gint ref_count; |
|
75 |
}; |
|
76 |
||
77 |
static LmMessageType |
|
78 |
message_type_from_string (const gchar *type_str) |
|
79 |
{ |
|
80 |
gint i; |
|
81 |
||
82 |
if (!type_str) { |
|
83 |
return LM_MESSAGE_TYPE_UNKNOWN; |
|
84 |
} |
|
85 |
||
86 |
for (i = LM_MESSAGE_TYPE_MESSAGE; |
|
331
884c0d7edefc
Properly detect message type; don't rely on lookup table ordering if the type is explicitly specified.
Senko Rasic <senko.rasic@collabora.co.uk>
parents:
274
diff
changeset
|
87 |
i < LM_MESSAGE_TYPE_UNKNOWN; |
1 | 88 |
++i) { |
7
d8eae83450ac
2003-06-13 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
4
diff
changeset
|
89 |
if (strcmp (type_str, type_names[i].name) == 0) { |
331
884c0d7edefc
Properly detect message type; don't rely on lookup table ordering if the type is explicitly specified.
Senko Rasic <senko.rasic@collabora.co.uk>
parents:
274
diff
changeset
|
90 |
return type_names[i].type; |
1 | 91 |
} |
92 |
} |
|
93 |
||
94 |
return LM_MESSAGE_TYPE_UNKNOWN; |
|
95 |
} |
|
96 |
||
97 |
||
98 |
const gchar * |
|
99 |
_lm_message_type_to_string (LmMessageType type) |
|
100 |
{ |
|
101 |
if (type < LM_MESSAGE_TYPE_MESSAGE || |
|
274
c32a7011e435
Adding StartTLS support.
Senko Rasic <senko@phyrexia.(none)>
parents:
215
diff
changeset
|
102 |
type > LM_MESSAGE_TYPE_STARTTLS) { |
1 | 103 |
type = LM_MESSAGE_TYPE_UNKNOWN; |
104 |
} |
|
105 |
||
106 |
return type_names[type].name; |
|
107 |
} |
|
108 |
||
109 |
static LmMessageSubType |
|
110 |
message_sub_type_from_string (const gchar *type_str) |
|
111 |
{ |
|
112 |
gint i; |
|
113 |
||
114 |
if (!type_str) { |
|
115 |
return LM_MESSAGE_SUB_TYPE_NOT_SET; |
|
116 |
} |
|
117 |
||
23
b8a56b77da30
2003-07-29 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
8
diff
changeset
|
118 |
for (i = LM_MESSAGE_SUB_TYPE_NORMAL; |
1 | 119 |
i <= LM_MESSAGE_SUB_TYPE_ERROR; |
120 |
++i) { |
|
121 |
if (g_ascii_strcasecmp (type_str, |
|
122 |
sub_type_names[i].name) == 0) { |
|
123 |
return i; |
|
124 |
} |
|
125 |
} |
|
126 |
||
127 |
return LM_MESSAGE_SUB_TYPE_NOT_SET; |
|
128 |
} |
|
129 |
||
130 |
const gchar * |
|
131 |
_lm_message_sub_type_to_string (LmMessageSubType type) |
|
132 |
{ |
|
23
b8a56b77da30
2003-07-29 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
8
diff
changeset
|
133 |
if (type < LM_MESSAGE_SUB_TYPE_NORMAL || |
1 | 134 |
type > LM_MESSAGE_SUB_TYPE_ERROR) { |
135 |
return NULL; |
|
136 |
} |
|
137 |
||
138 |
return sub_type_names[type].name; |
|
139 |
} |
|
140 |
||
141 |
static LmMessageSubType |
|
142 |
message_sub_type_when_unset (LmMessageType type) { |
|
143 |
LmMessageSubType sub_type = LM_MESSAGE_SUB_TYPE_NORMAL; |
|
144 |
||
145 |
switch (type) { |
|
146 |
case LM_MESSAGE_TYPE_MESSAGE: |
|
23
b8a56b77da30
2003-07-29 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
8
diff
changeset
|
147 |
/* 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
|
148 |
* 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
|
149 |
* will not know if it's set or not. |
b8a56b77da30
2003-07-29 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
8
diff
changeset
|
150 |
*/ |
b8a56b77da30
2003-07-29 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
8
diff
changeset
|
151 |
sub_type = LM_MESSAGE_SUB_TYPE_NOT_SET; |
1 | 152 |
break; |
153 |
case LM_MESSAGE_TYPE_PRESENCE: |
|
154 |
sub_type = LM_MESSAGE_SUB_TYPE_AVAILABLE; |
|
155 |
break; |
|
156 |
case LM_MESSAGE_TYPE_IQ: |
|
157 |
sub_type = LM_MESSAGE_SUB_TYPE_GET; |
|
158 |
break; |
|
159 |
default: |
|
160 |
break; |
|
161 |
} |
|
162 |
||
163 |
return sub_type; |
|
164 |
} |
|
165 |
||
166 |
LmMessage * |
|
167 |
_lm_message_new_from_node (LmMessageNode *node) |
|
168 |
{ |
|
169 |
LmMessage *m; |
|
170 |
LmMessageType type; |
|
171 |
LmMessageSubType sub_type; |
|
172 |
const gchar *sub_type_str; |
|
173 |
||
174 |
type = message_type_from_string (node->name); |
|
175 |
||
176 |
if (type == LM_MESSAGE_TYPE_UNKNOWN) { |
|
177 |
return NULL; |
|
178 |
} |
|
179 |
||
180 |
sub_type_str = lm_message_node_get_attribute (node, "type"); |
|
181 |
if (sub_type_str) { |
|
182 |
sub_type = message_sub_type_from_string (sub_type_str); |
|
183 |
} else { |
|
184 |
sub_type = message_sub_type_when_unset (type); |
|
185 |
} |
|
186 |
||
187 |
m = g_new0 (LmMessage, 1); |
|
188 |
m->priv = g_new0 (LmMessagePriv, 1); |
|
189 |
||
190 |
PRIV(m)->ref_count = 1; |
|
191 |
PRIV(m)->type = type; |
|
192 |
PRIV(m)->sub_type = sub_type; |
|
193 |
||
194 |
m->node = lm_message_node_ref (node); |
|
195 |
||
196 |
return m; |
|
197 |
} |
|
198 |
||
199 |
/** |
|
200 |
* lm_message_new: |
|
201 |
* @to: receipient jid |
|
202 |
* @type: message type |
|
203 |
* |
|
204 |
* Creates a new #LmMessage which can be sent with lm_connection_send() or |
|
205 |
* lm_connection_send_with_reply(). If @to is %NULL the message is sent to the |
|
206 |
* server. The returned message should be unreferenced with lm_message_unref() |
|
207 |
* when caller is finished with it. |
|
208 |
* |
|
209 |
* Return value: a newly created #LmMessage |
|
210 |
**/ |
|
211 |
LmMessage * |
|
212 |
lm_message_new (const gchar *to, LmMessageType type) |
|
213 |
{ |
|
79 | 214 |
LmMessage *m; |
215 |
gchar *id; |
|
216 |
||
217 |
m = g_new0 (LmMessage, 1); |
|
1 | 218 |
m->priv = g_new0 (LmMessagePriv, 1); |
219 |
||
220 |
PRIV(m)->ref_count = 1; |
|
221 |
PRIV(m)->type = type; |
|
222 |
PRIV(m)->sub_type = message_sub_type_when_unset (type); |
|
223 |
||
224 |
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
|
225 |
|
557
47f6169c0f08
Don't append the 'id'-tag in opening stream headers. [#30]
Will Thompson <will.thompson@collabora.co.uk>
parents:
555
diff
changeset
|
226 |
if (type != LM_MESSAGE_TYPE_STREAM) { |
47f6169c0f08
Don't append the 'id'-tag in opening stream headers. [#30]
Will Thompson <will.thompson@collabora.co.uk>
parents:
555
diff
changeset
|
227 |
id = _lm_utils_generate_id (); |
47f6169c0f08
Don't append the 'id'-tag in opening stream headers. [#30]
Will Thompson <will.thompson@collabora.co.uk>
parents:
555
diff
changeset
|
228 |
lm_message_node_set_attribute (m->node, "id", id); |
47f6169c0f08
Don't append the 'id'-tag in opening stream headers. [#30]
Will Thompson <will.thompson@collabora.co.uk>
parents:
555
diff
changeset
|
229 |
g_free (id); |
47f6169c0f08
Don't append the 'id'-tag in opening stream headers. [#30]
Will Thompson <will.thompson@collabora.co.uk>
parents:
555
diff
changeset
|
230 |
} |
1 | 231 |
|
232 |
if (to) { |
|
233 |
lm_message_node_set_attribute (m->node, "to", to); |
|
234 |
} |
|
235 |
||
236 |
if (type == LM_MESSAGE_TYPE_IQ) { |
|
237 |
lm_message_node_set_attribute (m->node, "type", "get"); |
|
238 |
} |
|
239 |
||
240 |
return m; |
|
241 |
} |
|
242 |
||
243 |
/** |
|
244 |
* lm_message_new_with_sub_type: |
|
245 |
* @to: receipient jid |
|
246 |
* @type: message type |
|
247 |
* @sub_type: message sub type |
|
248 |
* |
|
249 |
* Creates a new #LmMessage with sub type set. See lm_message_new() for more |
|
250 |
* information. |
|
251 |
* |
|
252 |
* Return value: a newly created #LmMessage |
|
253 |
**/ |
|
254 |
LmMessage * |
|
255 |
lm_message_new_with_sub_type (const gchar *to, |
|
256 |
LmMessageType type, |
|
257 |
LmMessageSubType sub_type) |
|
258 |
{ |
|
79 | 259 |
LmMessage *m; |
1 | 260 |
const gchar *type_str; |
261 |
||
79 | 262 |
m = lm_message_new (to, type); |
263 |
||
1 | 264 |
type_str = _lm_message_sub_type_to_string (sub_type); |
265 |
||
266 |
if (type_str) { |
|
267 |
lm_message_node_set_attributes (m->node, |
|
268 |
"type", type_str, NULL); |
|
269 |
PRIV(m)->sub_type = sub_type; |
|
270 |
} |
|
271 |
||
272 |
return m; |
|
273 |
} |
|
274 |
||
275 |
/** |
|
276 |
* lm_message_get_type: |
|
277 |
* @message: an #LmMessage |
|
278 |
* |
|
279 |
* Fetches the type of @message. |
|
280 |
* |
|
281 |
* Return value: the message type |
|
282 |
**/ |
|
283 |
LmMessageType |
|
284 |
lm_message_get_type (LmMessage *message) |
|
285 |
{ |
|
286 |
g_return_val_if_fail (message != NULL, LM_MESSAGE_TYPE_UNKNOWN); |
|
287 |
||
288 |
return PRIV(message)->type; |
|
289 |
} |
|
290 |
||
291 |
/** |
|
292 |
* lm_message_get_sub_type: |
|
555
695c909d0c45
Fixed up the documentation for 1.4.3 [#27]
Mikael Hallendal <micke@imendio.com>
parents:
331
diff
changeset
|
293 |
* @message: an #LmMessage |
1 | 294 |
* |
295 |
* Fetches the sub type of @message. |
|
296 |
* |
|
297 |
* Return value: the message sub type |
|
298 |
**/ |
|
8
33d64cf345be
2003-06-15 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
7
diff
changeset
|
299 |
LmMessageSubType |
1 | 300 |
lm_message_get_sub_type (LmMessage *message) |
301 |
{ |
|
302 |
g_return_val_if_fail (message != NULL, LM_MESSAGE_TYPE_UNKNOWN); |
|
303 |
||
304 |
return PRIV(message)->sub_type; |
|
305 |
} |
|
306 |
||
307 |
/** |
|
308 |
* lm_message_get_node: |
|
309 |
* @message: an #LmMessage |
|
310 |
* |
|
311 |
* Retrieves the root node from @message. |
|
312 |
* |
|
313 |
* Return value: an #LmMessageNode |
|
314 |
**/ |
|
315 |
LmMessageNode * |
|
316 |
lm_message_get_node (LmMessage *message) |
|
317 |
{ |
|
318 |
g_return_val_if_fail (message != NULL, NULL); |
|
319 |
||
320 |
return message->node; |
|
321 |
} |
|
322 |
||
323 |
/** |
|
324 |
* lm_message_ref: |
|
325 |
* @message: an #LmMessage |
|
326 |
* |
|
327 |
* Adds a reference to @message. |
|
328 |
* |
|
329 |
* Return value: the message |
|
330 |
**/ |
|
331 |
LmMessage * |
|
332 |
lm_message_ref (LmMessage *message) |
|
333 |
{ |
|
334 |
g_return_val_if_fail (message != NULL, NULL); |
|
335 |
||
336 |
PRIV(message)->ref_count++; |
|
337 |
||
338 |
return message; |
|
339 |
} |
|
340 |
||
341 |
/** |
|
342 |
* lm_message_unref: |
|
343 |
* @message: an #LmMessage |
|
344 |
* |
|
345 |
* Removes a reference from @message. When no more references are present the |
|
346 |
* message is freed. |
|
347 |
**/ |
|
348 |
void |
|
349 |
lm_message_unref (LmMessage *message) |
|
350 |
{ |
|
351 |
g_return_if_fail (message != NULL); |
|
352 |
||
353 |
PRIV(message)->ref_count--; |
|
354 |
||
355 |
if (PRIV(message)->ref_count == 0) { |
|
356 |
lm_message_node_unref (message->node); |
|
79 | 357 |
g_free (message->priv); |
1 | 358 |
g_free (message); |
359 |
} |
|
360 |
} |