Mercurial > ~mikael > mcabber > hg
annotate mcabber/src/xmpp_helper.c @ 1604:351427ef0b4b
Remove #include's of C files
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Sun, 11 Oct 2009 15:59:53 +0200 |
parents | f4a2c6f767d1 |
children | 14690e624e9d |
rev | line source |
---|---|
1598 | 1 /* |
1599 | 2 * xmpp_helper.c -- Jabber protocol helper functions |
1598 | 3 * |
1599 | 4 * Copyright (C) 2008-2009 Frank Zschockelt <mcabber@freakysoft.de> |
5 * Copyright (C) 2005-2009 Mikael Berthe <mikael@lilotux.net> | |
1598 | 6 * Some parts initially came from the centericq project: |
7 * Copyright (C) 2002-2005 by Konstantin Klyagin <konst@konst.org.ua> | |
8 * | |
9 * This program is free software; you can redistribute it and/or modify | |
10 * it under the terms of the GNU General Public License as published by | |
11 * the Free Software Foundation; either version 2 of the License, or (at | |
12 * your option) any later version. | |
13 * | |
14 * This program is distributed in the hope that it will be useful, but | |
15 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 * General Public License for more details. | |
18 * | |
19 * You should have received a copy of the GNU General Public License | |
20 * along with this program; if not, write to the Free Software | |
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
22 * USA | |
23 */ | |
24 | |
1604
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
25 #include <string.h> |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
26 #include <stdlib.h> |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
27 |
1598 | 28 #include "xmpp_helper.h" |
1604
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
29 #include "settings.h" |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
30 #include "utils.h" |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
31 #include "caps.h" |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
32 #include "logprint.h" |
1598 | 33 |
34 time_t iqlast; // last message/status change time | |
35 | |
1604
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
36 extern char *imstatus_showmap[]; |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
37 |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
38 struct xmpp_error xmpp_errors[] = { |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
39 {XMPP_ERROR_REDIRECT, "302", |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
40 "Redirect", "redirect", "modify"}, |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
41 {XMPP_ERROR_BAD_REQUEST, "400", |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
42 "Bad Request", "bad-request", "modify"}, |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
43 {XMPP_ERROR_NOT_AUTHORIZED, "401", |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
44 "Not Authorized", "not-authorized", "auth"}, |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
45 {XMPP_ERROR_PAYMENT_REQUIRED, "402", |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
46 "Payment Required", "payment-required", "auth"}, |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
47 {XMPP_ERROR_FORBIDDEN, "403", |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
48 "Forbidden", "forbidden", "auth"}, |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
49 {XMPP_ERROR_NOT_FOUND, "404", |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
50 "Not Found", "item-not-found", "cancel"}, |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
51 {XMPP_ERROR_NOT_ALLOWED, "405", |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
52 "Not Allowed", "not-allowed", "cancel"}, |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
53 {XMPP_ERROR_NOT_ACCEPTABLE, "406", |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
54 "Not Acceptable", "not-acceptable", "modify"}, |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
55 {XMPP_ERROR_REGISTRATION_REQUIRED, "407", |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
56 "Registration required", "registration-required", "auth"}, |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
57 {XMPP_ERROR_REQUEST_TIMEOUT, "408", |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
58 "Request Timeout", "remote-server-timeout", "wait"}, |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
59 {XMPP_ERROR_CONFLICT, "409", |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
60 "Conflict", "conflict", "cancel"}, |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
61 {XMPP_ERROR_INTERNAL_SERVER_ERROR, "500", |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
62 "Internal Server Error", "internal-server-error", "wait"}, |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
63 {XMPP_ERROR_NOT_IMPLEMENTED, "501", |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
64 "Not Implemented", "feature-not-implemented", "cancel"}, |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
65 {XMPP_ERROR_REMOTE_SERVER_ERROR, "502", |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
66 "Remote Server Error", "service-unavailable", "wait"}, |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
67 {XMPP_ERROR_SERVICE_UNAVAILABLE, "503", |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
68 "Service Unavailable", "service-unavailable", "cancel"}, |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
69 {XMPP_ERROR_REMOTE_SERVER_TIMEOUT, "504", |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
70 "Remote Server Timeout", "remote-server-timeout", "wait"}, |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
71 {XMPP_ERROR_DISCONNECTED, "510", |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
72 "Disconnected", "service-unavailable", "cancel"}, |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
73 {0, NULL, NULL, NULL, NULL} |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
74 }; |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
75 |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
76 |
1598 | 77 const gchar* lm_message_node_get_child_value(LmMessageNode *node, |
78 const gchar *child) | |
79 { | |
80 LmMessageNode *tmp; | |
81 tmp = lm_message_node_find_child(node, child); | |
82 if (tmp) | |
83 return lm_message_node_get_value(tmp); | |
84 else return NULL; | |
85 } | |
86 | |
87 static LmMessageNode *hidden = NULL; | |
88 | |
89 void lm_message_node_hide(LmMessageNode *node) | |
90 { | |
91 LmMessageNode *parent = node->parent, *prev_sibling = node->prev; | |
92 | |
93 if (hidden) { | |
94 hidden->children = hidden->next = hidden->prev = hidden->parent = NULL; | |
95 lm_message_node_unref(hidden); | |
96 } | |
97 | |
98 if (parent->children == node) | |
99 parent->children = node->next; | |
100 if (prev_sibling) | |
101 prev_sibling->next = node->next; | |
102 if (node->next) | |
103 node->next->prev = prev_sibling; | |
104 } | |
105 | |
106 //maybe not a good idea, because it uses internals of loudmouth... | |
107 //it's used for rosternotes/bookmarks | |
108 LmMessageNode *lm_message_node_new(const gchar *name, const gchar *xmlns) | |
109 { | |
110 LmMessageNode *node; | |
111 | |
112 node = g_new0 (LmMessageNode, 1); | |
113 node->name = g_strdup (name); | |
114 node->value = NULL; | |
115 node->raw_mode = FALSE; | |
116 node->attributes = NULL; | |
117 node->next = NULL; | |
118 node->prev = NULL; | |
119 node->parent = NULL; | |
120 node->children = NULL; | |
121 | |
122 node->ref_count = 1; | |
123 lm_message_node_set_attribute(node, "xmlns", xmlns); | |
124 return node; | |
125 } | |
126 | |
127 void lm_message_node_insert_childnode(LmMessageNode *node, | |
128 LmMessageNode *child) | |
129 { | |
130 LmMessageNode *x; | |
131 lm_message_node_deep_ref(child); | |
132 | |
133 if (node->children == NULL) | |
134 node->children = child; | |
135 else { | |
136 for (x = node->children; x->next; x = x->next) | |
137 ; | |
138 x->next = child; | |
139 } | |
140 } | |
141 | |
142 void lm_message_node_deep_ref(LmMessageNode *node) | |
143 { | |
144 if (node == NULL) | |
145 return; | |
146 lm_message_node_ref(node); | |
147 lm_message_node_deep_ref(node->next); | |
148 lm_message_node_deep_ref(node->children); | |
149 } | |
150 | |
151 const gchar* lm_message_get_from(LmMessage *m) | |
152 { | |
153 return lm_message_node_get_attribute(m->node, "from"); | |
154 } | |
155 | |
156 const gchar* lm_message_get_id(LmMessage *m) | |
157 { | |
158 return lm_message_node_get_attribute(m->node, "id"); | |
159 } | |
160 | |
1604
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
161 LmMessage *lm_message_new_iq_from_query(LmMessage *m, |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
162 LmMessageSubType type) |
1598 | 163 { |
164 LmMessage *new; | |
165 const char *from = lm_message_node_get_attribute(m->node, "from"); | |
166 const char *id = lm_message_node_get_attribute(m->node, "id"); | |
167 | |
168 new = lm_message_new_with_sub_type(from, LM_MESSAGE_TYPE_IQ, | |
169 type); | |
170 if (id) | |
171 lm_message_node_set_attribute(new->node, "id", id); | |
172 | |
173 return new; | |
174 } | |
175 | |
1600 | 176 // entity_version(enum imstatus status) |
1598 | 177 // Return a static version string for Entity Capabilities. |
178 // It should be specific to the client version, please change the id | |
179 // if you alter mcabber's disco support (or add something to the version | |
180 // number) so that it doesn't conflict with the official client. | |
1600 | 181 const char *entity_version(enum imstatus status) |
1598 | 182 { |
1600 | 183 static char *ver, *ver_notavail; |
1598 | 184 |
1600 | 185 if (ver && (status != notavail)) |
1598 | 186 return ver; |
1600 | 187 if (ver_notavail) |
188 return ver_notavail; | |
1598 | 189 |
1600 | 190 caps_add(""); |
191 caps_set_identity("", "client", PACKAGE_STRING, "pc"); | |
192 caps_add_feature("", NS_DISCO_INFO); | |
193 caps_add_feature("", NS_MUC); | |
194 // advertise ChatStates only if they aren't disabled | |
195 if (!settings_opt_get_int("disable_chatstates")) | |
196 caps_add_feature("", NS_CHATSTATES); | |
197 caps_add_feature("", NS_TIME); | |
198 caps_add_feature("", NS_XMPP_TIME); | |
199 caps_add_feature("", NS_VERSION); | |
200 caps_add_feature("", NS_PING); | |
201 caps_add_feature("", NS_COMMANDS); | |
1602 | 202 caps_add_feature("", NS_RECEIPTS); |
1600 | 203 if (!settings_opt_get_int("iq_last_disable") && |
204 (!settings_opt_get_int("iq_last_disable_when_notavail") || | |
205 status != notavail)) | |
206 caps_add_feature("", NS_LAST); | |
1598 | 207 |
1600 | 208 if (status == notavail) { |
209 ver_notavail = caps_generate(); | |
210 return ver_notavail; | |
211 } | |
212 | |
213 ver = caps_generate(); | |
1598 | 214 return ver; |
215 } | |
216 | |
1604
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
217 LmMessageNode *lm_message_node_find_xmlns(LmMessageNode *node, |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
218 const char *xmlns) |
1598 | 219 { |
220 LmMessageNode *x; | |
221 const char *p; | |
222 | |
223 for (x = node->children ; x; x = x->next) { | |
224 if ((p = lm_message_node_get_attribute(x, "xmlns")) && !strcmp(p, xmlns)) | |
225 break; | |
226 } | |
227 return x; | |
228 } | |
229 | |
1604
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
230 time_t lm_message_node_get_timestamp(LmMessageNode *node) |
1598 | 231 { |
232 LmMessageNode *x; | |
233 const char *p; | |
234 | |
235 x = lm_message_node_find_xmlns(node, NS_XMPP_DELAY); | |
236 if (x && (!strcmp(x->name, "delay")) && | |
237 (p = lm_message_node_get_attribute(x, "stamp")) != NULL) | |
238 return from_iso8601(p, 1); | |
239 x = lm_message_node_find_xmlns(node, NS_DELAY); | |
240 if (x && (p = lm_message_node_get_attribute(x, "stamp")) != NULL) | |
241 return from_iso8601(p, 1); | |
242 return 0; | |
243 } | |
244 | |
245 // lm_message_new_presence(status, recipient, message) | |
246 // Create an xmlnode with default presence attributes | |
247 // Note: the caller must free the node after use | |
1604
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
248 LmMessage *lm_message_new_presence(enum imstatus st, |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
249 const char *recipient, |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
250 const char *msg) |
1598 | 251 { |
252 unsigned int prio; | |
253 LmMessage *x = lm_message_new(recipient, LM_MESSAGE_TYPE_PRESENCE); | |
254 | |
255 switch(st) { | |
256 case away: | |
257 case notavail: | |
258 case dontdisturb: | |
259 case freeforchat: | |
260 lm_message_node_add_child(x->node, "show", imstatus_showmap[st]); | |
261 break; | |
262 | |
263 case invisible: | |
264 lm_message_node_set_attribute(x->node, "type", "invisible"); | |
265 break; | |
266 | |
267 case offline: | |
268 lm_message_node_set_attribute(x->node, "type", "unavailable"); | |
269 break; | |
270 | |
271 default: | |
272 break; | |
273 } | |
274 | |
275 if (st == away || st == notavail) | |
276 prio = settings_opt_get_int("priority_away"); | |
277 else | |
278 prio = settings_opt_get_int("priority"); | |
279 | |
280 if (prio) { | |
281 char strprio[8]; | |
282 snprintf(strprio, 8, "%d", (int)prio); | |
283 lm_message_node_add_child(x->node, "priority", strprio); | |
284 } | |
285 | |
286 if (msg) | |
287 lm_message_node_add_child(x->node, "status", msg); | |
288 | |
289 return x; | |
290 } | |
291 | |
292 static const char *defaulterrormsg(guint code) | |
293 { | |
1604
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
294 int i; |
1598 | 295 |
296 for (i = 0; xmpp_errors[i].code; ++i) { | |
297 if (xmpp_errors[i].code == code) | |
298 return xmpp_errors[i].meaning; | |
299 } | |
300 return NULL; | |
301 } | |
302 | |
303 // display_server_error(x) | |
304 // Display the error to the user | |
305 // x: error tag xmlnode pointer | |
306 void display_server_error(LmMessageNode *x) | |
307 { | |
308 const char *desc = NULL, *p=NULL, *s; | |
309 char *sdesc, *tmp; | |
310 int code = 0; | |
311 | |
312 if (!x) return; | |
313 | |
314 /* RFC3920: | |
315 * The <error/> element: | |
316 * o MUST contain a child element corresponding to one of the defined | |
317 * stanza error conditions specified below; this element MUST be | |
318 * qualified by the 'urn:ietf:params:xml:ns:xmpp-stanzas' namespace. | |
319 */ | |
320 if (x->children) | |
321 p = x->children->name; | |
322 if (p) | |
323 scr_LogPrint(LPRINT_LOGNORM, "Received error packet [%s]", p); | |
324 | |
325 // For backward compatibility | |
326 if ((s = lm_message_node_get_attribute(x, "code")) != NULL) { | |
327 code = atoi(s); | |
328 // Default message | |
329 desc = defaulterrormsg(code); | |
330 } | |
331 | |
332 // Error tag data is better, if available | |
333 s = lm_message_node_get_value(x); | |
334 if (s && *s) desc = s; | |
335 | |
336 // And sometimes there is a text message | |
337 s = lm_message_node_get_child_value(x, "text"); | |
338 | |
339 if (s && *s) desc = s; | |
340 | |
341 // If we still have no description, let's give up | |
342 if (!desc) | |
343 return; | |
344 | |
345 // Strip trailing newlines | |
346 sdesc = g_strdup(desc); | |
347 for (tmp = sdesc; *tmp; tmp++) ; | |
348 if (tmp > sdesc) | |
349 tmp--; | |
350 while (tmp >= sdesc && (*tmp == '\n' || *tmp == '\r')) | |
351 *tmp-- = '\0'; | |
352 | |
353 scr_LogPrint(LPRINT_LOGNORM, "Error code from server: %d %s", code, sdesc); | |
354 g_free(sdesc); | |
355 } | |
356 | |
1599 | 357 /* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */ |