]> git.0d.be Git - empathy.git/blob - libempathy/gossip-utils.c
668898712030f44dc5afda4309b3b7139dd08aa5
[empathy.git] / libempathy / gossip-utils.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2003-2007 Imendio AB
4  * Copyright (C) 2007 Collabora Ltd.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public
17  * License along with this program; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
21  * Authors: Richard Hult <richard@imendio.com>
22  *          Martyn Russell <martyn@imendio.com>
23  *          Xavier Claessens <xclaesse@gmail.com>
24  */
25
26 #include "config.h"
27
28 #include <string.h>
29 #include <time.h>
30 #include <sys/types.h>
31 #include <regex.h>
32
33 #include <glib/gi18n.h>
34
35 #include <libxml/uri.h>
36 #include <libmissioncontrol/mc-account.h>
37
38 #include "gossip-debug.h"
39 #include "gossip-utils.h"
40 #include "gossip-paths.h"
41 #include "empathy-contact-manager.h"
42
43 #define DEBUG_DOMAIN "Utils"
44
45 static void regex_init (void);
46
47 gchar *
48 gossip_substring (const gchar *str,
49                   gint         start,
50                   gint         end)
51 {
52         return g_strndup (str + start, end - start);
53 }
54
55 /*
56  * Regular Expression code to match urls.
57  */
58 #define USERCHARS "-A-Za-z0-9"
59 #define PASSCHARS "-A-Za-z0-9,?;.:/!%$^*&~\"#'"
60 #define HOSTCHARS "-A-Za-z0-9"
61 #define PATHCHARS "-A-Za-z0-9_$.+!*(),;:@&=?/~#%"
62 #define SCHEME    "(news:|telnet:|nntp:|file:/|https?:|ftps?:|webcal:)"
63 #define USER      "[" USERCHARS "]+(:["PASSCHARS "]+)?"
64 #define URLPATH   "/[" PATHCHARS "]*[^]'.}>) \t\r\n,\\\"]"
65
66 static regex_t dingus[GOSSIP_REGEX_ALL];
67
68 static void
69 regex_init (void)
70 {
71         static gboolean  inited = FALSE;
72         const gchar     *expression;
73         gint             i;
74
75         if (inited) {
76                 return;
77         }
78
79         for (i = 0; i < GOSSIP_REGEX_ALL; i++) {
80                 switch (i) {
81                 case GOSSIP_REGEX_AS_IS:
82                         expression =
83                                 SCHEME "//(" USER "@)?[" HOSTCHARS ".]+"
84                                 "(:[0-9]+)?(" URLPATH ")?";
85                         break;
86                 case GOSSIP_REGEX_BROWSER:
87                         expression =
88                                 "(www|ftp)[" HOSTCHARS "]*\\.[" HOSTCHARS ".]+"
89                                 "(:[0-9]+)?(" URLPATH ")?";
90                         break;
91                 case GOSSIP_REGEX_EMAIL:
92                         expression =
93                                 "(mailto:)?[a-z0-9][a-z0-9.-]*@[a-z0-9]"
94                                 "[a-z0-9-]*(\\.[a-z0-9][a-z0-9-]*)+";
95                         break;
96                 case GOSSIP_REGEX_OTHER:
97                         expression =
98                                 "news:[-A-Z\\^_a-z{|}~!\"#$%&'()*+,./0-9;:=?`]+"
99                                 "@[" HOSTCHARS ".]+(:[0-9]+)?";
100                         break;
101                 default:
102                         /* Silence the compiler. */
103                         expression = NULL;
104                         continue;
105                 }
106
107                 memset (&dingus[i], 0, sizeof (regex_t));
108                 regcomp (&dingus[i], expression, REG_EXTENDED | REG_ICASE);
109         }
110
111         inited = TRUE;
112 }
113
114 gint
115 gossip_regex_match (GossipRegExType  type,
116                     const gchar     *msg,
117                     GArray          *start,
118                     GArray          *end)
119 {
120         regmatch_t matches[1];
121         gint       ret = 0;
122         gint       num_matches = 0;
123         gint       offset = 0;
124         gint       i;
125
126         g_return_val_if_fail (type >= 0 || type <= GOSSIP_REGEX_ALL, 0);
127
128         regex_init ();
129
130         while (!ret && type != GOSSIP_REGEX_ALL) {
131                 ret = regexec (&dingus[type], msg + offset, 1, matches, 0);
132                 if (ret == 0) {
133                         gint s;
134
135                         num_matches++;
136
137                         s = matches[0].rm_so + offset;
138                         offset = matches[0].rm_eo + offset;
139
140                         g_array_append_val (start, s);
141                         g_array_append_val (end, offset);
142                 }
143         }
144
145         if (type != GOSSIP_REGEX_ALL) {
146                 gossip_debug (DEBUG_DOMAIN,
147                               "Found %d matches for regex type:%d",
148                               num_matches, type);
149                 return num_matches;
150         }
151
152         /* If GOSSIP_REGEX_ALL then we run ALL regex's on the string. */
153         for (i = 0; i < GOSSIP_REGEX_ALL; i++, ret = 0) {
154                 while (!ret) {
155                         ret = regexec (&dingus[i], msg + offset, 1, matches, 0);
156                         if (ret == 0) {
157                                 gint s;
158
159                                 num_matches++;
160
161                                 s = matches[0].rm_so + offset;
162                                 offset = matches[0].rm_eo + offset;
163
164                                 g_array_append_val (start, s);
165                                 g_array_append_val (end, offset);
166                         }
167                 }
168         }
169
170         gossip_debug (DEBUG_DOMAIN,
171                       "Found %d matches for ALL regex types",
172                       num_matches);
173
174         return num_matches;
175 }
176
177 gint
178 gossip_strcasecmp (const gchar *s1,
179                    const gchar *s2)
180 {
181         return gossip_strncasecmp (s1, s2, -1);
182 }
183
184 gint
185 gossip_strncasecmp (const gchar *s1,
186                     const gchar *s2,
187                     gsize        n)
188 {
189         gchar *u1, *u2;
190         gint   ret_val;
191
192         u1 = g_utf8_casefold (s1, n);
193         u2 = g_utf8_casefold (s2, n);
194
195         ret_val = g_utf8_collate (u1, u2);
196         g_free (u1);
197         g_free (u2);
198
199         return ret_val;
200 }
201
202 gboolean
203 gossip_xml_validate (xmlDoc      *doc,
204                      const gchar *dtd_filename)
205 {
206         gchar        *path, *escaped;
207         xmlValidCtxt  cvp;
208         xmlDtd       *dtd;
209         gboolean      ret;
210
211         path = gossip_paths_get_dtd_path (dtd_filename);
212
213         /* The list of valid chars is taken from libxml. */
214         escaped = xmlURIEscapeStr (path, ":@&=+$,/?;");
215
216         g_free (path);
217
218         memset (&cvp, 0, sizeof (cvp));
219         dtd = xmlParseDTD (NULL, escaped);
220         ret = xmlValidateDtd (&cvp, doc, dtd);
221
222         xmlFree (escaped);
223         xmlFreeDtd (dtd);
224
225         return ret;
226 }
227
228 xmlNodePtr
229 gossip_xml_node_get_child (xmlNodePtr   node, 
230                            const gchar *child_name)
231 {
232         xmlNodePtr l;
233
234         g_return_val_if_fail (node != NULL, NULL);
235         g_return_val_if_fail (child_name != NULL, NULL);
236
237         for (l = node->children; l; l = l->next) {
238                 if (l->name && strcmp (l->name, child_name) == 0) {
239                         return l;
240                 }
241         }
242
243         return NULL;
244 }
245
246 xmlChar *
247 gossip_xml_node_get_child_content (xmlNodePtr   node, 
248                                    const gchar *child_name)
249 {
250         xmlNodePtr l;
251
252         g_return_val_if_fail (node != NULL, NULL);
253         g_return_val_if_fail (child_name != NULL, NULL);
254
255         l = gossip_xml_node_get_child (node, child_name);
256         if (l) {
257                 return xmlNodeGetContent (l);
258         }
259                 
260         return NULL;
261 }
262
263 xmlNodePtr
264 gossip_xml_node_find_child_prop_value (xmlNodePtr   node, 
265                                        const gchar *prop_name,
266                                        const gchar *prop_value)
267 {
268         xmlNodePtr l;
269         xmlNodePtr found = NULL;
270
271         g_return_val_if_fail (node != NULL, NULL);
272         g_return_val_if_fail (prop_name != NULL, NULL);
273         g_return_val_if_fail (prop_value != NULL, NULL);
274
275         for (l = node->children; l && !found; l = l->next) {
276                 xmlChar *prop;
277
278                 if (!xmlHasProp (l, prop_name)) {
279                         continue;
280                 }
281
282                 prop = xmlGetProp (l, prop_name);
283                 if (prop && strcmp (prop, prop_value) == 0) {
284                         found = l;
285                 }
286                 
287                 xmlFree (prop);
288         }
289                 
290         return found;
291 }
292
293 GType
294 gossip_dbus_type_to_g_type (const gchar *dbus_type_string)
295 {
296         if (dbus_type_string == NULL)
297                 return G_TYPE_NONE;
298
299         if (dbus_type_string[0] == 's') {
300                 return G_TYPE_STRING;
301         }
302         else if (dbus_type_string[0] == 'b') {
303                 return G_TYPE_BOOLEAN;
304         }
305         else if (dbus_type_string[0] == 'q') {
306                 return G_TYPE_UINT;
307         }
308         else if (dbus_type_string[0] == 'n') {
309                 return G_TYPE_INT;
310         }
311
312         g_assert_not_reached ();
313         return G_TYPE_NONE;
314 }
315
316 const gchar *
317 gossip_g_type_to_dbus_type (GType g_type)
318 {
319         switch (g_type) {
320         case G_TYPE_STRING:
321                 return "s";
322         case G_TYPE_BOOLEAN:
323                 return "b";
324         case G_TYPE_UINT:
325                 return "q";
326         case G_TYPE_INT:
327                 return "n";
328         default:
329                 g_assert_not_reached ();
330         }
331
332         return NULL;
333 }
334
335 gchar *
336 gossip_g_value_to_string (const GValue *value)
337 {
338         gchar  *return_string = NULL;
339         GValue  string_g_value = {0, };
340
341         g_value_init (&string_g_value, G_TYPE_STRING);
342         g_value_transform (value, &string_g_value);
343         return_string = g_value_dup_string (&string_g_value);
344         g_value_unset (&string_g_value);
345
346         return return_string;
347 }
348
349 GValue *
350 gossip_string_to_g_value (const gchar *str, GType type)
351 {
352         GValue *g_value;
353
354         g_value = g_new0 (GValue, 1);
355         g_value_init (g_value, type);
356
357         switch (type) {
358         case G_TYPE_STRING:
359                 g_value_set_string (g_value, str);
360                 break;
361         case G_TYPE_BOOLEAN:
362                 g_value_set_boolean (g_value, (str[0] == 'y' || str[0] == 'T'));
363                 break;
364         case G_TYPE_UINT:
365                 g_value_set_uint (g_value, atoi (str));
366                 break;
367         case G_TYPE_INT:
368                 g_value_set_int (g_value, atoi (str));
369                 break;
370         default:
371                 g_assert_not_reached ();
372         }
373
374         return g_value;
375 }
376
377 gboolean
378 gossip_g_value_equal (const GValue *value1,
379                       const GValue *value2)
380 {
381         GType type;
382
383         g_return_val_if_fail (value1 != NULL, FALSE);
384         g_return_val_if_fail (value2 != NULL, FALSE);
385
386         type = G_VALUE_TYPE (value1);
387         if (type != G_VALUE_TYPE (value2)) {
388                 return FALSE;
389         }
390
391         switch (type)
392         {
393         case G_TYPE_STRING: {
394                 const gchar *str1;
395                 const gchar *str2;
396
397                 str1 = g_value_get_string (value1);
398                 str2 = g_value_get_string (value2);
399                 return (str1 && str2 && strcmp (str1, str2) == 0) ||
400                        (G_STR_EMPTY (str1) && G_STR_EMPTY (str2));
401         }
402         case G_TYPE_BOOLEAN:
403                 return g_value_get_boolean (value1) == g_value_get_boolean (value2);
404         case G_TYPE_UINT:
405                 return g_value_get_uint (value1) == g_value_get_uint (value2);
406         case G_TYPE_INT:
407                 return g_value_get_int (value1) == g_value_get_int (value2);
408         default:
409                 g_warning ("Unsupported GType in value comparaison");
410         }
411
412         return FALSE;
413 }
414
415 guint
416 gossip_account_hash (gconstpointer key)
417 {
418         return g_str_hash (mc_account_get_unique_name (MC_ACCOUNT (key)));
419 }
420
421 gboolean
422 gossip_account_equal (gconstpointer a,
423                       gconstpointer b)
424 {
425         const gchar *name_a;
426         const gchar *name_b;
427
428         name_a = mc_account_get_unique_name (MC_ACCOUNT (a));
429         name_b = mc_account_get_unique_name (MC_ACCOUNT (b));
430
431         return g_str_equal (name_a, name_b);
432 }
433
434 GossipContact *
435 gossip_get_own_contact_from_contact (GossipContact  *contact)
436 {
437         EmpathyContactManager *manager;
438         McAccount             *account;
439         GossipContact         *own_contact;
440
441         g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), NULL);
442
443         manager = empathy_contact_manager_new ();
444         account = gossip_contact_get_account (contact);
445         own_contact = empathy_contact_manager_get_own (manager, account);
446         g_object_unref (manager);
447
448         return own_contact;
449 }
450