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 |
|
|
25 |
#include "xmpp_helper.h" |
|
26 |
|
|
27 |
time_t iqlast; // last message/status change time |
|
28 |
|
|
29 |
const gchar* lm_message_node_get_child_value(LmMessageNode *node, |
|
30 |
const gchar *child) |
|
31 |
{ |
|
32 |
LmMessageNode *tmp; |
|
33 |
tmp = lm_message_node_find_child(node, child); |
|
34 |
if (tmp) |
|
35 |
return lm_message_node_get_value(tmp); |
|
36 |
else return NULL; |
|
37 |
} |
|
38 |
|
|
39 |
static LmMessageNode *hidden = NULL; |
|
40 |
|
|
41 |
void lm_message_node_hide(LmMessageNode *node) |
|
42 |
{ |
|
43 |
LmMessageNode *parent = node->parent, *prev_sibling = node->prev; |
|
44 |
|
|
45 |
if (hidden) { |
|
46 |
hidden->children = hidden->next = hidden->prev = hidden->parent = NULL; |
|
47 |
lm_message_node_unref(hidden); |
|
48 |
} |
|
49 |
|
|
50 |
if (parent->children == node) |
|
51 |
parent->children = node->next; |
|
52 |
if (prev_sibling) |
|
53 |
prev_sibling->next = node->next; |
|
54 |
if (node->next) |
|
55 |
node->next->prev = prev_sibling; |
|
56 |
} |
|
57 |
|
|
58 |
//maybe not a good idea, because it uses internals of loudmouth... |
|
59 |
//it's used for rosternotes/bookmarks |
|
60 |
LmMessageNode *lm_message_node_new(const gchar *name, const gchar *xmlns) |
|
61 |
{ |
|
62 |
LmMessageNode *node; |
|
63 |
|
|
64 |
node = g_new0 (LmMessageNode, 1); |
|
65 |
node->name = g_strdup (name); |
|
66 |
node->value = NULL; |
|
67 |
node->raw_mode = FALSE; |
|
68 |
node->attributes = NULL; |
|
69 |
node->next = NULL; |
|
70 |
node->prev = NULL; |
|
71 |
node->parent = NULL; |
|
72 |
node->children = NULL; |
|
73 |
|
|
74 |
node->ref_count = 1; |
|
75 |
lm_message_node_set_attribute(node, "xmlns", xmlns); |
|
76 |
return node; |
|
77 |
} |
|
78 |
|
|
79 |
void lm_message_node_insert_childnode(LmMessageNode *node, |
|
80 |
LmMessageNode *child) |
|
81 |
{ |
|
82 |
LmMessageNode *x; |
|
83 |
lm_message_node_deep_ref(child); |
|
84 |
|
|
85 |
if (node->children == NULL) |
|
86 |
node->children = child; |
|
87 |
else { |
|
88 |
for (x = node->children; x->next; x = x->next) |
|
89 |
; |
|
90 |
x->next = child; |
|
91 |
} |
|
92 |
} |
|
93 |
|
|
94 |
void lm_message_node_deep_ref(LmMessageNode *node) |
|
95 |
{ |
|
96 |
if (node == NULL) |
|
97 |
return; |
|
98 |
lm_message_node_ref(node); |
|
99 |
lm_message_node_deep_ref(node->next); |
|
100 |
lm_message_node_deep_ref(node->children); |
|
101 |
} |
|
102 |
|
|
103 |
const gchar* lm_message_get_from(LmMessage *m) |
|
104 |
{ |
|
105 |
return lm_message_node_get_attribute(m->node, "from"); |
|
106 |
} |
|
107 |
|
|
108 |
const gchar* lm_message_get_id(LmMessage *m) |
|
109 |
{ |
|
110 |
return lm_message_node_get_attribute(m->node, "id"); |
|
111 |
} |
|
112 |
|
|
113 |
static LmMessage *lm_message_new_iq_from_query(LmMessage *m, |
|
114 |
LmMessageSubType type) |
|
115 |
{ |
|
116 |
LmMessage *new; |
|
117 |
const char *from = lm_message_node_get_attribute(m->node, "from"); |
|
118 |
const char *id = lm_message_node_get_attribute(m->node, "id"); |
|
119 |
|
|
120 |
new = lm_message_new_with_sub_type(from, LM_MESSAGE_TYPE_IQ, |
|
121 |
type); |
|
122 |
if (id) |
|
123 |
lm_message_node_set_attribute(new->node, "id", id); |
|
124 |
|
|
125 |
return new; |
|
126 |
} |
|
127 |
|
1600
|
128 |
// entity_version(enum imstatus status) |
1598
|
129 |
// Return a static version string for Entity Capabilities. |
|
130 |
// It should be specific to the client version, please change the id |
|
131 |
// if you alter mcabber's disco support (or add something to the version |
|
132 |
// number) so that it doesn't conflict with the official client. |
1600
|
133 |
const char *entity_version(enum imstatus status) |
1598
|
134 |
{ |
1600
|
135 |
static char *ver, *ver_notavail; |
1598
|
136 |
|
1600
|
137 |
if (ver && (status != notavail)) |
1598
|
138 |
return ver; |
1600
|
139 |
if (ver_notavail) |
|
140 |
return ver_notavail; |
1598
|
141 |
|
1600
|
142 |
caps_add(""); |
|
143 |
caps_set_identity("", "client", PACKAGE_STRING, "pc"); |
|
144 |
caps_add_feature("", NS_DISCO_INFO); |
|
145 |
caps_add_feature("", NS_MUC); |
|
146 |
// advertise ChatStates only if they aren't disabled |
|
147 |
if (!settings_opt_get_int("disable_chatstates")) |
|
148 |
caps_add_feature("", NS_CHATSTATES); |
|
149 |
caps_add_feature("", NS_TIME); |
|
150 |
caps_add_feature("", NS_XMPP_TIME); |
|
151 |
caps_add_feature("", NS_VERSION); |
|
152 |
caps_add_feature("", NS_PING); |
|
153 |
caps_add_feature("", NS_COMMANDS); |
1602
|
154 |
caps_add_feature("", NS_RECEIPTS); |
1600
|
155 |
if (!settings_opt_get_int("iq_last_disable") && |
|
156 |
(!settings_opt_get_int("iq_last_disable_when_notavail") || |
|
157 |
status != notavail)) |
|
158 |
caps_add_feature("", NS_LAST); |
1598
|
159 |
|
1600
|
160 |
if (status == notavail) { |
|
161 |
ver_notavail = caps_generate(); |
|
162 |
return ver_notavail; |
|
163 |
} |
|
164 |
|
|
165 |
ver = caps_generate(); |
1598
|
166 |
return ver; |
|
167 |
} |
|
168 |
|
|
169 |
inline static LmMessageNode *lm_message_node_find_xmlns(LmMessageNode *node, |
|
170 |
const char *xmlns) |
|
171 |
{ |
|
172 |
LmMessageNode *x; |
|
173 |
const char *p; |
|
174 |
|
|
175 |
for (x = node->children ; x; x = x->next) { |
|
176 |
if ((p = lm_message_node_get_attribute(x, "xmlns")) && !strcmp(p, xmlns)) |
|
177 |
break; |
|
178 |
} |
|
179 |
return x; |
|
180 |
} |
|
181 |
|
|
182 |
static time_t lm_message_node_get_timestamp(LmMessageNode *node) |
|
183 |
{ |
|
184 |
LmMessageNode *x; |
|
185 |
const char *p; |
|
186 |
|
|
187 |
x = lm_message_node_find_xmlns(node, NS_XMPP_DELAY); |
|
188 |
if (x && (!strcmp(x->name, "delay")) && |
|
189 |
(p = lm_message_node_get_attribute(x, "stamp")) != NULL) |
|
190 |
return from_iso8601(p, 1); |
|
191 |
x = lm_message_node_find_xmlns(node, NS_DELAY); |
|
192 |
if (x && (p = lm_message_node_get_attribute(x, "stamp")) != NULL) |
|
193 |
return from_iso8601(p, 1); |
|
194 |
return 0; |
|
195 |
} |
|
196 |
|
|
197 |
// lm_message_new_presence(status, recipient, message) |
|
198 |
// Create an xmlnode with default presence attributes |
|
199 |
// Note: the caller must free the node after use |
|
200 |
static LmMessage *lm_message_new_presence(enum imstatus st, |
|
201 |
const char *recipient, |
|
202 |
const char *msg) |
|
203 |
{ |
|
204 |
unsigned int prio; |
|
205 |
LmMessage *x = lm_message_new(recipient, LM_MESSAGE_TYPE_PRESENCE); |
|
206 |
|
|
207 |
switch(st) { |
|
208 |
case away: |
|
209 |
case notavail: |
|
210 |
case dontdisturb: |
|
211 |
case freeforchat: |
|
212 |
lm_message_node_add_child(x->node, "show", imstatus_showmap[st]); |
|
213 |
break; |
|
214 |
|
|
215 |
case invisible: |
|
216 |
lm_message_node_set_attribute(x->node, "type", "invisible"); |
|
217 |
break; |
|
218 |
|
|
219 |
case offline: |
|
220 |
lm_message_node_set_attribute(x->node, "type", "unavailable"); |
|
221 |
break; |
|
222 |
|
|
223 |
default: |
|
224 |
break; |
|
225 |
} |
|
226 |
|
|
227 |
if (st == away || st == notavail) |
|
228 |
prio = settings_opt_get_int("priority_away"); |
|
229 |
else |
|
230 |
prio = settings_opt_get_int("priority"); |
|
231 |
|
|
232 |
if (prio) { |
|
233 |
char strprio[8]; |
|
234 |
snprintf(strprio, 8, "%d", (int)prio); |
|
235 |
lm_message_node_add_child(x->node, "priority", strprio); |
|
236 |
} |
|
237 |
|
|
238 |
if (msg) |
|
239 |
lm_message_node_add_child(x->node, "status", msg); |
|
240 |
|
|
241 |
return x; |
|
242 |
} |
|
243 |
|
|
244 |
static const char *defaulterrormsg(guint code) |
|
245 |
{ |
|
246 |
int i = 0; |
|
247 |
|
|
248 |
for (i = 0; xmpp_errors[i].code; ++i) { |
|
249 |
if (xmpp_errors[i].code == code) |
|
250 |
return xmpp_errors[i].meaning; |
|
251 |
} |
|
252 |
return NULL; |
|
253 |
} |
|
254 |
|
|
255 |
// display_server_error(x) |
|
256 |
// Display the error to the user |
|
257 |
// x: error tag xmlnode pointer |
|
258 |
void display_server_error(LmMessageNode *x) |
|
259 |
{ |
|
260 |
const char *desc = NULL, *p=NULL, *s; |
|
261 |
char *sdesc, *tmp; |
|
262 |
int code = 0; |
|
263 |
|
|
264 |
if (!x) return; |
|
265 |
|
|
266 |
/* RFC3920: |
|
267 |
* The <error/> element: |
|
268 |
* o MUST contain a child element corresponding to one of the defined |
|
269 |
* stanza error conditions specified below; this element MUST be |
|
270 |
* qualified by the 'urn:ietf:params:xml:ns:xmpp-stanzas' namespace. |
|
271 |
*/ |
|
272 |
if (x->children) |
|
273 |
p = x->children->name; |
|
274 |
if (p) |
|
275 |
scr_LogPrint(LPRINT_LOGNORM, "Received error packet [%s]", p); |
|
276 |
|
|
277 |
// For backward compatibility |
|
278 |
if ((s = lm_message_node_get_attribute(x, "code")) != NULL) { |
|
279 |
code = atoi(s); |
|
280 |
// Default message |
|
281 |
desc = defaulterrormsg(code); |
|
282 |
} |
|
283 |
|
|
284 |
// Error tag data is better, if available |
|
285 |
s = lm_message_node_get_value(x); |
|
286 |
if (s && *s) desc = s; |
|
287 |
|
|
288 |
// And sometimes there is a text message |
|
289 |
s = lm_message_node_get_child_value(x, "text"); |
|
290 |
|
|
291 |
if (s && *s) desc = s; |
|
292 |
|
|
293 |
// If we still have no description, let's give up |
|
294 |
if (!desc) |
|
295 |
return; |
|
296 |
|
|
297 |
// Strip trailing newlines |
|
298 |
sdesc = g_strdup(desc); |
|
299 |
for (tmp = sdesc; *tmp; tmp++) ; |
|
300 |
if (tmp > sdesc) |
|
301 |
tmp--; |
|
302 |
while (tmp >= sdesc && (*tmp == '\n' || *tmp == '\r')) |
|
303 |
*tmp-- = '\0'; |
|
304 |
|
|
305 |
scr_LogPrint(LPRINT_LOGNORM, "Error code from server: %d %s", code, sdesc); |
|
306 |
g_free(sdesc); |
|
307 |
} |
|
308 |
|
1599
|
309 |
/* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */ |