comparison mcabber/mcabber/xmpp_helper.c @ 1668:41c26b7d2890

Install mcabber headers * Change mcabber headers naming scheme * Move 'src/' -> 'mcabber/' * Add missing include <mcabber/config.h>'s * Create and install clean config.h version in 'include/' * Move "dirty" config.h version to 'mcabber/' * Add $(top_srcdir) to compiler include path * Update modules HOWTO
author Myhailo Danylenko <isbear@ukrpost.net>
date Mon, 18 Jan 2010 15:36:19 +0200
parents mcabber/src/xmpp_helper.c@14690e624e9d
children d080e7cda46c
comparison
equal deleted inserted replaced
1667:8af0e0ad20ad 1668:41c26b7d2890
1 /*
2 * xmpp_helper.c -- Jabber protocol helper functions
3 *
4 * Copyright (C) 2008-2009 Frank Zschockelt <mcabber@freakysoft.de>
5 * Copyright (C) 2005-2009 Mikael Berthe <mikael@lilotux.net>
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
25 #include <string.h>
26 #include <stdlib.h>
27
28 #include "xmpp_helper.h"
29 #include "settings.h"
30 #include "utils.h"
31 #include "caps.h"
32 #include "logprint.h"
33 #include "config.h"
34
35 time_t iqlast; // last message/status change time
36
37 extern char *imstatus_showmap[];
38
39 struct xmpp_error xmpp_errors[] = {
40 {XMPP_ERROR_REDIRECT, "302",
41 "Redirect", "redirect", "modify"},
42 {XMPP_ERROR_BAD_REQUEST, "400",
43 "Bad Request", "bad-request", "modify"},
44 {XMPP_ERROR_NOT_AUTHORIZED, "401",
45 "Not Authorized", "not-authorized", "auth"},
46 {XMPP_ERROR_PAYMENT_REQUIRED, "402",
47 "Payment Required", "payment-required", "auth"},
48 {XMPP_ERROR_FORBIDDEN, "403",
49 "Forbidden", "forbidden", "auth"},
50 {XMPP_ERROR_NOT_FOUND, "404",
51 "Not Found", "item-not-found", "cancel"},
52 {XMPP_ERROR_NOT_ALLOWED, "405",
53 "Not Allowed", "not-allowed", "cancel"},
54 {XMPP_ERROR_NOT_ACCEPTABLE, "406",
55 "Not Acceptable", "not-acceptable", "modify"},
56 {XMPP_ERROR_REGISTRATION_REQUIRED, "407",
57 "Registration required", "registration-required", "auth"},
58 {XMPP_ERROR_REQUEST_TIMEOUT, "408",
59 "Request Timeout", "remote-server-timeout", "wait"},
60 {XMPP_ERROR_CONFLICT, "409",
61 "Conflict", "conflict", "cancel"},
62 {XMPP_ERROR_INTERNAL_SERVER_ERROR, "500",
63 "Internal Server Error", "internal-server-error", "wait"},
64 {XMPP_ERROR_NOT_IMPLEMENTED, "501",
65 "Not Implemented", "feature-not-implemented", "cancel"},
66 {XMPP_ERROR_REMOTE_SERVER_ERROR, "502",
67 "Remote Server Error", "service-unavailable", "wait"},
68 {XMPP_ERROR_SERVICE_UNAVAILABLE, "503",
69 "Service Unavailable", "service-unavailable", "cancel"},
70 {XMPP_ERROR_REMOTE_SERVER_TIMEOUT, "504",
71 "Remote Server Timeout", "remote-server-timeout", "wait"},
72 {XMPP_ERROR_DISCONNECTED, "510",
73 "Disconnected", "service-unavailable", "cancel"},
74 {0, NULL, NULL, NULL, NULL}
75 };
76
77
78 #ifdef MODULES_ENABLE
79 static GSList *xmpp_additional_features = NULL;
80 static char *ver, *ver_notavail;
81
82 void xmpp_add_feature (const char *xmlns)
83 {
84 if (xmlns) {
85 ver = NULL;
86 ver_notavail = NULL;
87 xmpp_additional_features = g_slist_append(xmpp_additional_features,
88 g_strdup (xmlns));
89 }
90 }
91
92 void xmpp_del_feature (const char *xmlns)
93 {
94 GSList *feature = xmpp_additional_features;
95 while (feature) {
96 if (!strcmp(feature->data, xmlns)) {
97 ver = NULL;
98 ver_notavail = NULL;
99 g_free (feature->data);
100 xmpp_additional_features = g_slist_delete_link(xmpp_additional_features,
101 feature);
102 return;
103 }
104 feature = g_slist_next (feature);
105 }
106 }
107 #endif
108
109 const gchar* lm_message_node_get_child_value(LmMessageNode *node,
110 const gchar *child)
111 {
112 LmMessageNode *tmp;
113 tmp = lm_message_node_find_child(node, child);
114 if (tmp)
115 return lm_message_node_get_value(tmp);
116 else return NULL;
117 }
118
119 static LmMessageNode *hidden = NULL;
120
121 void lm_message_node_hide(LmMessageNode *node)
122 {
123 LmMessageNode *parent = node->parent, *prev_sibling = node->prev;
124
125 if (hidden) {
126 hidden->children = hidden->next = hidden->prev = hidden->parent = NULL;
127 lm_message_node_unref(hidden);
128 }
129
130 if (parent->children == node)
131 parent->children = node->next;
132 if (prev_sibling)
133 prev_sibling->next = node->next;
134 if (node->next)
135 node->next->prev = prev_sibling;
136 }
137
138 //maybe not a good idea, because it uses internals of loudmouth...
139 //it's used for rosternotes/bookmarks
140 LmMessageNode *lm_message_node_new(const gchar *name, const gchar *xmlns)
141 {
142 LmMessageNode *node;
143
144 node = g_new0 (LmMessageNode, 1);
145 node->name = g_strdup (name);
146 node->value = NULL;
147 node->raw_mode = FALSE;
148 node->attributes = NULL;
149 node->next = NULL;
150 node->prev = NULL;
151 node->parent = NULL;
152 node->children = NULL;
153
154 node->ref_count = 1;
155 lm_message_node_set_attribute(node, "xmlns", xmlns);
156 return node;
157 }
158
159 void lm_message_node_insert_childnode(LmMessageNode *node,
160 LmMessageNode *child)
161 {
162 LmMessageNode *x;
163 lm_message_node_deep_ref(child);
164
165 if (node->children == NULL)
166 node->children = child;
167 else {
168 for (x = node->children; x->next; x = x->next)
169 ;
170 x->next = child;
171 }
172 }
173
174 void lm_message_node_deep_ref(LmMessageNode *node)
175 {
176 if (node == NULL)
177 return;
178 lm_message_node_ref(node);
179 lm_message_node_deep_ref(node->next);
180 lm_message_node_deep_ref(node->children);
181 }
182
183 const gchar* lm_message_get_from(LmMessage *m)
184 {
185 return lm_message_node_get_attribute(m->node, "from");
186 }
187
188 const gchar* lm_message_get_id(LmMessage *m)
189 {
190 return lm_message_node_get_attribute(m->node, "id");
191 }
192
193 LmMessage *lm_message_new_iq_from_query(LmMessage *m,
194 LmMessageSubType type)
195 {
196 LmMessage *new;
197 const char *from = lm_message_node_get_attribute(m->node, "from");
198 const char *id = lm_message_node_get_attribute(m->node, "id");
199
200 new = lm_message_new_with_sub_type(from, LM_MESSAGE_TYPE_IQ,
201 type);
202 if (id)
203 lm_message_node_set_attribute(new->node, "id", id);
204
205 return new;
206 }
207
208 // entity_version(enum imstatus status)
209 // Return a static version string for Entity Capabilities.
210 // It should be specific to the client version, please change the id
211 // if you alter mcabber's disco support (or add something to the version
212 // number) so that it doesn't conflict with the official client.
213 const char *entity_version(enum imstatus status)
214 {
215 #ifndef MODULES_ENABLE
216 static char *ver, *ver_notavail;
217 #endif
218
219 if (ver && (status != notavail))
220 return ver;
221 if (ver_notavail)
222 return ver_notavail;
223
224 caps_add("");
225 caps_set_identity("", "client", PACKAGE_STRING, "pc");
226 caps_add_feature("", NS_DISCO_INFO);
227 caps_add_feature("", NS_MUC);
228 // advertise ChatStates only if they aren't disabled
229 if (!settings_opt_get_int("disable_chatstates"))
230 caps_add_feature("", NS_CHATSTATES);
231 caps_add_feature("", NS_TIME);
232 caps_add_feature("", NS_XMPP_TIME);
233 caps_add_feature("", NS_VERSION);
234 caps_add_feature("", NS_PING);
235 caps_add_feature("", NS_COMMANDS);
236 caps_add_feature("", NS_RECEIPTS);
237 if (!settings_opt_get_int("iq_last_disable") &&
238 (!settings_opt_get_int("iq_last_disable_when_notavail") ||
239 status != notavail))
240 caps_add_feature("", NS_LAST);
241 #ifdef MODULES_ENABLE
242 {
243 GSList *el = xmpp_additional_features;
244 while (el) {
245 caps_add_feature("", el->data);
246 el = g_slist_next (el);
247 }
248 }
249 #endif
250
251 if (status == notavail) {
252 ver_notavail = caps_generate();
253 return ver_notavail;
254 }
255
256 ver = caps_generate();
257 return ver;
258 }
259
260 LmMessageNode *lm_message_node_find_xmlns(LmMessageNode *node,
261 const char *xmlns)
262 {
263 LmMessageNode *x;
264 const char *p;
265
266 for (x = node->children ; x; x = x->next) {
267 if ((p = lm_message_node_get_attribute(x, "xmlns")) && !strcmp(p, xmlns))
268 break;
269 }
270 return x;
271 }
272
273 time_t lm_message_node_get_timestamp(LmMessageNode *node)
274 {
275 LmMessageNode *x;
276 const char *p;
277
278 x = lm_message_node_find_xmlns(node, NS_XMPP_DELAY);
279 if (x && (!strcmp(x->name, "delay")) &&
280 (p = lm_message_node_get_attribute(x, "stamp")) != NULL)
281 return from_iso8601(p, 1);
282 x = lm_message_node_find_xmlns(node, NS_DELAY);
283 if (x && (p = lm_message_node_get_attribute(x, "stamp")) != NULL)
284 return from_iso8601(p, 1);
285 return 0;
286 }
287
288 // lm_message_new_presence(status, recipient, message)
289 // Create an xmlnode with default presence attributes
290 // Note: the caller must free the node after use
291 LmMessage *lm_message_new_presence(enum imstatus st,
292 const char *recipient,
293 const char *msg)
294 {
295 unsigned int prio;
296 LmMessage *x = lm_message_new(recipient, LM_MESSAGE_TYPE_PRESENCE);
297
298 switch(st) {
299 case away:
300 case notavail:
301 case dontdisturb:
302 case freeforchat:
303 lm_message_node_add_child(x->node, "show", imstatus_showmap[st]);
304 break;
305
306 case invisible:
307 lm_message_node_set_attribute(x->node, "type", "invisible");
308 break;
309
310 case offline:
311 lm_message_node_set_attribute(x->node, "type", "unavailable");
312 break;
313
314 default:
315 break;
316 }
317
318 if (st == away || st == notavail)
319 prio = settings_opt_get_int("priority_away");
320 else
321 prio = settings_opt_get_int("priority");
322
323 if (prio) {
324 char strprio[8];
325 snprintf(strprio, 8, "%d", (int)prio);
326 lm_message_node_add_child(x->node, "priority", strprio);
327 }
328
329 if (msg)
330 lm_message_node_add_child(x->node, "status", msg);
331
332 return x;
333 }
334
335 static const char *defaulterrormsg(guint code)
336 {
337 int i;
338
339 for (i = 0; xmpp_errors[i].code; ++i) {
340 if (xmpp_errors[i].code == code)
341 return xmpp_errors[i].meaning;
342 }
343 return NULL;
344 }
345
346 // display_server_error(x)
347 // Display the error to the user
348 // x: error tag xmlnode pointer
349 void display_server_error(LmMessageNode *x)
350 {
351 const char *desc = NULL, *p=NULL, *s;
352 char *sdesc, *tmp;
353 int code = 0;
354
355 if (!x) return;
356
357 /* RFC3920:
358 * The <error/> element:
359 * o MUST contain a child element corresponding to one of the defined
360 * stanza error conditions specified below; this element MUST be
361 * qualified by the 'urn:ietf:params:xml:ns:xmpp-stanzas' namespace.
362 */
363 if (x->children)
364 p = x->children->name;
365 if (p)
366 scr_LogPrint(LPRINT_LOGNORM, "Received error packet [%s]", p);
367
368 // For backward compatibility
369 if ((s = lm_message_node_get_attribute(x, "code")) != NULL) {
370 code = atoi(s);
371 // Default message
372 desc = defaulterrormsg(code);
373 }
374
375 // Error tag data is better, if available
376 s = lm_message_node_get_value(x);
377 if (s && *s) desc = s;
378
379 // And sometimes there is a text message
380 s = lm_message_node_get_child_value(x, "text");
381
382 if (s && *s) desc = s;
383
384 // If we still have no description, let's give up
385 if (!desc)
386 return;
387
388 // Strip trailing newlines
389 sdesc = g_strdup(desc);
390 for (tmp = sdesc; *tmp; tmp++) ;
391 if (tmp > sdesc)
392 tmp--;
393 while (tmp >= sdesc && (*tmp == '\n' || *tmp == '\r'))
394 *tmp-- = '\0';
395
396 scr_LogPrint(LPRINT_LOGNORM, "Error code from server: %d %s", code, sdesc);
397 g_free(sdesc);
398 }
399
400 /* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */