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