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); |
|
154 if (!settings_opt_get_int("iq_last_disable") && |
|
155 (!settings_opt_get_int("iq_last_disable_when_notavail") || |
|
156 status != notavail)) |
|
157 caps_add_feature("", NS_LAST); |
1598
|
158 |
1600
|
159 if (status == notavail) { |
|
160 ver_notavail = caps_generate(); |
|
161 return ver_notavail; |
|
162 } |
|
163 |
|
164 ver = caps_generate(); |
1598
|
165 return ver; |
|
166 } |
|
167 |
|
168 inline static LmMessageNode *lm_message_node_find_xmlns(LmMessageNode *node, |
|
169 const char *xmlns) |
|
170 { |
|
171 LmMessageNode *x; |
|
172 const char *p; |
|
173 |
|
174 for (x = node->children ; x; x = x->next) { |
|
175 if ((p = lm_message_node_get_attribute(x, "xmlns")) && !strcmp(p, xmlns)) |
|
176 break; |
|
177 } |
|
178 return x; |
|
179 } |
|
180 |
|
181 static time_t lm_message_node_get_timestamp(LmMessageNode *node) |
|
182 { |
|
183 LmMessageNode *x; |
|
184 const char *p; |
|
185 |
|
186 x = lm_message_node_find_xmlns(node, NS_XMPP_DELAY); |
|
187 if (x && (!strcmp(x->name, "delay")) && |
|
188 (p = lm_message_node_get_attribute(x, "stamp")) != NULL) |
|
189 return from_iso8601(p, 1); |
|
190 x = lm_message_node_find_xmlns(node, NS_DELAY); |
|
191 if (x && (p = lm_message_node_get_attribute(x, "stamp")) != NULL) |
|
192 return from_iso8601(p, 1); |
|
193 return 0; |
|
194 } |
|
195 |
|
196 // lm_message_new_presence(status, recipient, message) |
|
197 // Create an xmlnode with default presence attributes |
|
198 // Note: the caller must free the node after use |
|
199 static LmMessage *lm_message_new_presence(enum imstatus st, |
|
200 const char *recipient, |
|
201 const char *msg) |
|
202 { |
|
203 unsigned int prio; |
|
204 LmMessage *x = lm_message_new(recipient, LM_MESSAGE_TYPE_PRESENCE); |
|
205 |
|
206 switch(st) { |
|
207 case away: |
|
208 case notavail: |
|
209 case dontdisturb: |
|
210 case freeforchat: |
|
211 lm_message_node_add_child(x->node, "show", imstatus_showmap[st]); |
|
212 break; |
|
213 |
|
214 case invisible: |
|
215 lm_message_node_set_attribute(x->node, "type", "invisible"); |
|
216 break; |
|
217 |
|
218 case offline: |
|
219 lm_message_node_set_attribute(x->node, "type", "unavailable"); |
|
220 break; |
|
221 |
|
222 default: |
|
223 break; |
|
224 } |
|
225 |
|
226 if (st == away || st == notavail) |
|
227 prio = settings_opt_get_int("priority_away"); |
|
228 else |
|
229 prio = settings_opt_get_int("priority"); |
|
230 |
|
231 if (prio) { |
|
232 char strprio[8]; |
|
233 snprintf(strprio, 8, "%d", (int)prio); |
|
234 lm_message_node_add_child(x->node, "priority", strprio); |
|
235 } |
|
236 |
|
237 if (msg) |
|
238 lm_message_node_add_child(x->node, "status", msg); |
|
239 |
|
240 return x; |
|
241 } |
|
242 |
|
243 static const char *defaulterrormsg(guint code) |
|
244 { |
|
245 int i = 0; |
|
246 |
|
247 for (i = 0; xmpp_errors[i].code; ++i) { |
|
248 if (xmpp_errors[i].code == code) |
|
249 return xmpp_errors[i].meaning; |
|
250 } |
|
251 return NULL; |
|
252 } |
|
253 |
|
254 // display_server_error(x) |
|
255 // Display the error to the user |
|
256 // x: error tag xmlnode pointer |
|
257 void display_server_error(LmMessageNode *x) |
|
258 { |
|
259 const char *desc = NULL, *p=NULL, *s; |
|
260 char *sdesc, *tmp; |
|
261 int code = 0; |
|
262 |
|
263 if (!x) return; |
|
264 |
|
265 /* RFC3920: |
|
266 * The <error/> element: |
|
267 * o MUST contain a child element corresponding to one of the defined |
|
268 * stanza error conditions specified below; this element MUST be |
|
269 * qualified by the 'urn:ietf:params:xml:ns:xmpp-stanzas' namespace. |
|
270 */ |
|
271 if (x->children) |
|
272 p = x->children->name; |
|
273 if (p) |
|
274 scr_LogPrint(LPRINT_LOGNORM, "Received error packet [%s]", p); |
|
275 |
|
276 // For backward compatibility |
|
277 if ((s = lm_message_node_get_attribute(x, "code")) != NULL) { |
|
278 code = atoi(s); |
|
279 // Default message |
|
280 desc = defaulterrormsg(code); |
|
281 } |
|
282 |
|
283 // Error tag data is better, if available |
|
284 s = lm_message_node_get_value(x); |
|
285 if (s && *s) desc = s; |
|
286 |
|
287 // And sometimes there is a text message |
|
288 s = lm_message_node_get_child_value(x, "text"); |
|
289 |
|
290 if (s && *s) desc = s; |
|
291 |
|
292 // If we still have no description, let's give up |
|
293 if (!desc) |
|
294 return; |
|
295 |
|
296 // Strip trailing newlines |
|
297 sdesc = g_strdup(desc); |
|
298 for (tmp = sdesc; *tmp; tmp++) ; |
|
299 if (tmp > sdesc) |
|
300 tmp--; |
|
301 while (tmp >= sdesc && (*tmp == '\n' || *tmp == '\r')) |
|
302 *tmp-- = '\0'; |
|
303 |
|
304 scr_LogPrint(LPRINT_LOGNORM, "Error code from server: %d %s", code, sdesc); |
|
305 g_free(sdesc); |
|
306 } |
|
307 |
1599
|
308 /* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */ |