]> git.0d.be Git - empathy.git/blob - libempathy/empathy-utils.c
Adding new empathy_strdiff API stolen from telepathy-glib. It check if
[empathy.git] / libempathy / empathy-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 <libtelepathy/tp-helpers.h>
37
38 #include "empathy-debug.h"
39 #include "empathy-utils.h"
40 #include "empathy-contact-manager.h"
41
42 #define DEBUG_DOMAIN "Utils"
43
44 static void regex_init (void);
45
46 gchar *
47 empathy_substring (const gchar *str,
48                   gint         start,
49                   gint         end)
50 {
51         return g_strndup (str + start, end - start);
52 }
53
54 /*
55  * Regular Expression code to match urls.
56  */
57 #define USERCHARS "-A-Za-z0-9"
58 #define PASSCHARS "-A-Za-z0-9,?;.:/!%$^*&~\"#'"
59 #define HOSTCHARS "-A-Za-z0-9"
60 #define PATHCHARS "-A-Za-z0-9_$.+!*(),;:@&=?/~#%"
61 #define SCHEME    "(news:|telnet:|nntp:|file:/|https?:|ftps?:|webcal:)"
62 #define USER      "[" USERCHARS "]+(:["PASSCHARS "]+)?"
63 #define URLPATH   "/[" PATHCHARS "]*[^]'.}>) \t\r\n,\\\"]"
64
65 static regex_t dingus[EMPATHY_REGEX_ALL];
66
67 static void
68 regex_init (void)
69 {
70         static gboolean  inited = FALSE;
71         const gchar     *expression;
72         gint             i;
73
74         if (inited) {
75                 return;
76         }
77
78         for (i = 0; i < EMPATHY_REGEX_ALL; i++) {
79                 switch (i) {
80                 case EMPATHY_REGEX_AS_IS:
81                         expression =
82                                 SCHEME "//(" USER "@)?[" HOSTCHARS ".]+"
83                                 "(:[0-9]+)?(" URLPATH ")?";
84                         break;
85                 case EMPATHY_REGEX_BROWSER:
86                         expression =
87                                 "(www|ftp)[" HOSTCHARS "]*\\.[" HOSTCHARS ".]+"
88                                 "(:[0-9]+)?(" URLPATH ")?";
89                         break;
90                 case EMPATHY_REGEX_EMAIL:
91                         expression =
92                                 "(mailto:)?[a-z0-9][a-z0-9.-]*@[a-z0-9]"
93                                 "[a-z0-9-]*(\\.[a-z0-9][a-z0-9-]*)+";
94                         break;
95                 case EMPATHY_REGEX_OTHER:
96                         expression =
97                                 "news:[-A-Z\\^_a-z{|}~!\"#$%&'()*+,./0-9;:=?`]+"
98                                 "@[" HOSTCHARS ".]+(:[0-9]+)?";
99                         break;
100                 default:
101                         /* Silence the compiler. */
102                         expression = NULL;
103                         continue;
104                 }
105
106                 memset (&dingus[i], 0, sizeof (regex_t));
107                 regcomp (&dingus[i], expression, REG_EXTENDED | REG_ICASE);
108         }
109
110         inited = TRUE;
111 }
112
113 gint
114 empathy_regex_match (EmpathyRegExType  type,
115                     const gchar     *msg,
116                     GArray          *start,
117                     GArray          *end)
118 {
119         regmatch_t matches[1];
120         gint       ret = 0;
121         gint       num_matches = 0;
122         gint       offset = 0;
123         gint       i;
124
125         g_return_val_if_fail (type >= 0 || type <= EMPATHY_REGEX_ALL, 0);
126
127         regex_init ();
128
129         while (!ret && type != EMPATHY_REGEX_ALL) {
130                 ret = regexec (&dingus[type], msg + offset, 1, matches, 0);
131                 if (ret == 0) {
132                         gint s;
133
134                         num_matches++;
135
136                         s = matches[0].rm_so + offset;
137                         offset = matches[0].rm_eo + offset;
138
139                         g_array_append_val (start, s);
140                         g_array_append_val (end, offset);
141                 }
142         }
143
144         if (type != EMPATHY_REGEX_ALL) {
145                 empathy_debug (DEBUG_DOMAIN,
146                               "Found %d matches for regex type:%d",
147                               num_matches, type);
148                 return num_matches;
149         }
150
151         /* If EMPATHY_REGEX_ALL then we run ALL regex's on the string. */
152         for (i = 0; i < EMPATHY_REGEX_ALL; i++, ret = 0) {
153                 while (!ret) {
154                         ret = regexec (&dingus[i], msg + offset, 1, matches, 0);
155                         if (ret == 0) {
156                                 gint s;
157
158                                 num_matches++;
159
160                                 s = matches[0].rm_so + offset;
161                                 offset = matches[0].rm_eo + offset;
162
163                                 g_array_append_val (start, s);
164                                 g_array_append_val (end, offset);
165                         }
166                 }
167         }
168
169         empathy_debug (DEBUG_DOMAIN,
170                       "Found %d matches for ALL regex types",
171                       num_matches);
172
173         return num_matches;
174 }
175
176 gint
177 empathy_strcasecmp (const gchar *s1,
178                    const gchar *s2)
179 {
180         return empathy_strncasecmp (s1, s2, -1);
181 }
182
183 gint
184 empathy_strncasecmp (const gchar *s1,
185                     const gchar *s2,
186                     gsize        n)
187 {
188         gchar *u1, *u2;
189         gint   ret_val;
190
191         u1 = g_utf8_casefold (s1, n);
192         u2 = g_utf8_casefold (s2, n);
193
194         ret_val = g_utf8_collate (u1, u2);
195         g_free (u1);
196         g_free (u2);
197
198         return ret_val;
199 }
200
201 gboolean
202 empathy_xml_validate (xmlDoc      *doc,
203                      const gchar *dtd_filename)
204 {
205         gchar        *path, *escaped;
206         xmlValidCtxt  cvp;
207         xmlDtd       *dtd;
208         gboolean      ret;
209
210         path = g_build_filename (DATADIR, "empathy", dtd_filename, NULL);
211
212         /* The list of valid chars is taken from libxml. */
213         escaped = xmlURIEscapeStr (path, ":@&=+$,/?;");
214
215         g_free (path);
216
217         memset (&cvp, 0, sizeof (cvp));
218         dtd = xmlParseDTD (NULL, escaped);
219         ret = xmlValidateDtd (&cvp, doc, dtd);
220
221         xmlFree (escaped);
222         xmlFreeDtd (dtd);
223
224         return ret;
225 }
226
227 xmlNodePtr
228 empathy_xml_node_get_child (xmlNodePtr   node, 
229                            const gchar *child_name)
230 {
231         xmlNodePtr l;
232
233         g_return_val_if_fail (node != NULL, NULL);
234         g_return_val_if_fail (child_name != NULL, NULL);
235
236         for (l = node->children; l; l = l->next) {
237                 if (l->name && strcmp (l->name, child_name) == 0) {
238                         return l;
239                 }
240         }
241
242         return NULL;
243 }
244
245 xmlChar *
246 empathy_xml_node_get_child_content (xmlNodePtr   node, 
247                                    const gchar *child_name)
248 {
249         xmlNodePtr l;
250
251         g_return_val_if_fail (node != NULL, NULL);
252         g_return_val_if_fail (child_name != NULL, NULL);
253
254         l = empathy_xml_node_get_child (node, child_name);
255         if (l) {
256                 return xmlNodeGetContent (l);
257         }
258                 
259         return NULL;
260 }
261
262 xmlNodePtr
263 empathy_xml_node_find_child_prop_value (xmlNodePtr   node, 
264                                        const gchar *prop_name,
265                                        const gchar *prop_value)
266 {
267         xmlNodePtr l;
268         xmlNodePtr found = NULL;
269
270         g_return_val_if_fail (node != NULL, NULL);
271         g_return_val_if_fail (prop_name != NULL, NULL);
272         g_return_val_if_fail (prop_value != NULL, NULL);
273
274         for (l = node->children; l && !found; l = l->next) {
275                 xmlChar *prop;
276
277                 if (!xmlHasProp (l, prop_name)) {
278                         continue;
279                 }
280
281                 prop = xmlGetProp (l, prop_name);
282                 if (prop && strcmp (prop, prop_value) == 0) {
283                         found = l;
284                 }
285                 
286                 xmlFree (prop);
287         }
288                 
289         return found;
290 }
291
292 guint
293 empathy_account_hash (gconstpointer key)
294 {
295         return g_str_hash (mc_account_get_unique_name (MC_ACCOUNT (key)));
296 }
297
298 gboolean
299 empathy_account_equal (gconstpointer a,
300                       gconstpointer b)
301 {
302         const gchar *name_a;
303         const gchar *name_b;
304
305         name_a = mc_account_get_unique_name (MC_ACCOUNT (a));
306         name_b = mc_account_get_unique_name (MC_ACCOUNT (b));
307
308         return g_str_equal (name_a, name_b);
309 }
310
311 MissionControl *
312 empathy_mission_control_new (void)
313 {
314         static MissionControl *mc = NULL;
315
316         if (!mc) {
317                 mc = mission_control_new (tp_get_bus ());
318                 g_object_add_weak_pointer (G_OBJECT (mc), (gpointer) &mc);
319         } else {
320                 g_object_ref (mc);
321         }
322
323         return mc;
324 }
325
326 gchar *
327 empathy_inspect_channel (McAccount *account,
328                          TpChan    *tp_chan)
329 {
330         g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
331         g_return_val_if_fail (TELEPATHY_IS_CHAN (tp_chan), NULL);
332
333         return empathy_inspect_handle (account,
334                                        tp_chan->handle,
335                                        tp_chan->handle_type);
336 }
337
338 gchar *
339 empathy_inspect_handle (McAccount *account,
340                         guint      handle,
341                         guint      handle_type)
342 {
343         MissionControl  *mc;
344         TpConn          *tp_conn;
345         GArray          *handles;
346         gchar          **names;
347         gchar           *name;
348         GError          *error;
349
350         g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
351         g_return_val_if_fail (handle != 0, NULL);
352         g_return_val_if_fail (handle_type != 0, NULL);
353
354         mc = empathy_mission_control_new ();
355         tp_conn = mission_control_get_connection (mc, account, NULL);
356         g_object_unref (mc);
357
358         if (!tp_conn) {
359                 return NULL;
360         }
361
362         /* Get the handle's name */
363         handles = g_array_new (FALSE, FALSE, sizeof (guint));
364         g_array_append_val (handles, handle);
365         if (!tp_conn_inspect_handles (DBUS_G_PROXY (tp_conn),
366                                       handle_type,
367                                       handles,
368                                       &names,
369                                       &error)) {
370                 empathy_debug (DEBUG_DOMAIN, 
371                               "Couldn't get id: %s",
372                               error ? error->message : "No error given");
373
374                 g_clear_error (&error);
375                 g_array_free (handles, TRUE);
376                 g_object_unref (tp_conn);
377                 
378                 return NULL;
379         }
380
381         name = *names;
382         g_free (names);
383         g_object_unref (tp_conn);
384
385         return name;
386 }
387
388 /* Stolen from telepathy-glib */
389 gboolean
390 empathy_strdiff (const gchar *left, const gchar *right)
391 {
392   if ((NULL == left) != (NULL == right))
393     return TRUE;
394
395   else if (left == right)
396     return FALSE;
397
398   else
399     return (0 != strcmp (left, right));
400 }
401