Mercurial > hg
annotate mcabber/src/xmpp_helper.c @ 1642:cd67b29e04e3
Fix not execution of HOOK_INTERNAL
author | Myhailo Danylenko <isbear@ukrpost.net> |
---|---|
date | Mon, 02 Nov 2009 19:11:29 +0200 |
parents | 14690e624e9d |
children |
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" |
1607 | 33 #include "config.h" |
1598 | 34 |
35 time_t iqlast; // last message/status change time | |
36 | |
1604
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
37 extern char *imstatus_showmap[]; |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
38 |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
39 struct xmpp_error xmpp_errors[] = { |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
40 {XMPP_ERROR_REDIRECT, "302", |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
41 "Redirect", "redirect", "modify"}, |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
42 {XMPP_ERROR_BAD_REQUEST, "400", |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
43 "Bad Request", "bad-request", "modify"}, |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
44 {XMPP_ERROR_NOT_AUTHORIZED, "401", |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
45 "Not Authorized", "not-authorized", "auth"}, |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
46 {XMPP_ERROR_PAYMENT_REQUIRED, "402", |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
47 "Payment Required", "payment-required", "auth"}, |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
48 {XMPP_ERROR_FORBIDDEN, "403", |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
49 "Forbidden", "forbidden", "auth"}, |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
50 {XMPP_ERROR_NOT_FOUND, "404", |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
51 "Not Found", "item-not-found", "cancel"}, |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
52 {XMPP_ERROR_NOT_ALLOWED, "405", |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
53 "Not Allowed", "not-allowed", "cancel"}, |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
54 {XMPP_ERROR_NOT_ACCEPTABLE, "406", |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
55 "Not Acceptable", "not-acceptable", "modify"}, |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
56 {XMPP_ERROR_REGISTRATION_REQUIRED, "407", |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
57 "Registration required", "registration-required", "auth"}, |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
58 {XMPP_ERROR_REQUEST_TIMEOUT, "408", |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
59 "Request Timeout", "remote-server-timeout", "wait"}, |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
60 {XMPP_ERROR_CONFLICT, "409", |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
61 "Conflict", "conflict", "cancel"}, |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
62 {XMPP_ERROR_INTERNAL_SERVER_ERROR, "500", |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
63 "Internal Server Error", "internal-server-error", "wait"}, |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
64 {XMPP_ERROR_NOT_IMPLEMENTED, "501", |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
65 "Not Implemented", "feature-not-implemented", "cancel"}, |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
66 {XMPP_ERROR_REMOTE_SERVER_ERROR, "502", |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
67 "Remote Server Error", "service-unavailable", "wait"}, |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
68 {XMPP_ERROR_SERVICE_UNAVAILABLE, "503", |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
69 "Service Unavailable", "service-unavailable", "cancel"}, |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
70 {XMPP_ERROR_REMOTE_SERVER_TIMEOUT, "504", |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
71 "Remote Server Timeout", "remote-server-timeout", "wait"}, |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
72 {XMPP_ERROR_DISCONNECTED, "510", |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
73 "Disconnected", "service-unavailable", "cancel"}, |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
74 {0, NULL, NULL, NULL, NULL} |
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 |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
77 |
1607 | 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 | |
1598 | 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 | |
1604
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
193 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
|
194 LmMessageSubType type) |
1598 | 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 | |
1600 | 208 // entity_version(enum imstatus status) |
1598 | 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. | |
1600 | 213 const char *entity_version(enum imstatus status) |
1598 | 214 { |
1607 | 215 #ifndef MODULES_ENABLE |
1600 | 216 static char *ver, *ver_notavail; |
1607 | 217 #endif |
1598 | 218 |
1600 | 219 if (ver && (status != notavail)) |
1598 | 220 return ver; |
1600 | 221 if (ver_notavail) |
222 return ver_notavail; | |
1598 | 223 |
1600 | 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); | |
1602 | 236 caps_add_feature("", NS_RECEIPTS); |
1600 | 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); | |
1607 | 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 | |
1598 | 250 |
1600 | 251 if (status == notavail) { |
252 ver_notavail = caps_generate(); | |
253 return ver_notavail; | |
254 } | |
255 | |
256 ver = caps_generate(); | |
1598 | 257 return ver; |
258 } | |
259 | |
1604
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
260 LmMessageNode *lm_message_node_find_xmlns(LmMessageNode *node, |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
261 const char *xmlns) |
1598 | 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 | |
1604
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
273 time_t lm_message_node_get_timestamp(LmMessageNode *node) |
1598 | 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 | |
1604
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
291 LmMessage *lm_message_new_presence(enum imstatus st, |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
292 const char *recipient, |
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
293 const char *msg) |
1598 | 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 { | |
1604
351427ef0b4b
Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents:
1602
diff
changeset
|
337 int i; |
1598 | 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 | |
1599 | 400 /* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */ |