1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2003-2007 Imendio AB
4 * Copyright (C) 2007 Collabora Ltd.
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.
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.
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.
21 * Authors: Richard Hult <richard@imendio.com>
22 * Martyn Russell <martyn@imendio.com>
23 * Xavier Claessens <xclaesse@gmail.com>
30 #include <sys/types.h>
33 #include <glib/gi18n.h>
35 #include <libxml/uri.h>
36 #include <libtelepathy/tp-helpers.h>
38 #include "empathy-debug.h"
39 #include "empathy-utils.h"
40 #include "empathy-contact-manager.h"
42 #define DEBUG_DOMAIN "Utils"
44 static void regex_init (void);
47 empathy_substring (const gchar *str,
51 return g_strndup (str + start, end - start);
55 * Regular Expression code to match urls.
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,\\\"]"
65 static regex_t dingus[EMPATHY_REGEX_ALL];
70 static gboolean inited = FALSE;
71 const gchar *expression;
78 for (i = 0; i < EMPATHY_REGEX_ALL; i++) {
80 case EMPATHY_REGEX_AS_IS:
82 SCHEME "//(" USER "@)?[" HOSTCHARS ".]+"
83 "(:[0-9]+)?(" URLPATH ")?";
85 case EMPATHY_REGEX_BROWSER:
87 "(www|ftp)[" HOSTCHARS "]*\\.[" HOSTCHARS ".]+"
88 "(:[0-9]+)?(" URLPATH ")?";
90 case EMPATHY_REGEX_EMAIL:
92 "(mailto:)?[a-z0-9][a-z0-9.-]*@[a-z0-9]"
93 "[a-z0-9-]*(\\.[a-z0-9][a-z0-9-]*)+";
95 case EMPATHY_REGEX_OTHER:
97 "news:[-A-Z\\^_a-z{|}~!\"#$%&'()*+,./0-9;:=?`]+"
98 "@[" HOSTCHARS ".]+(:[0-9]+)?";
101 /* Silence the compiler. */
106 memset (&dingus[i], 0, sizeof (regex_t));
107 regcomp (&dingus[i], expression, REG_EXTENDED | REG_ICASE);
114 empathy_regex_match (EmpathyRegExType type,
119 regmatch_t matches[1];
121 gint num_matches = 0;
125 g_return_val_if_fail (type >= 0 || type <= EMPATHY_REGEX_ALL, 0);
129 while (!ret && type != EMPATHY_REGEX_ALL) {
130 ret = regexec (&dingus[type], msg + offset, 1, matches, 0);
136 s = matches[0].rm_so + offset;
137 offset = matches[0].rm_eo + offset;
139 g_array_append_val (start, s);
140 g_array_append_val (end, offset);
144 if (type != EMPATHY_REGEX_ALL) {
145 empathy_debug (DEBUG_DOMAIN,
146 "Found %d matches for regex type:%d",
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) {
154 ret = regexec (&dingus[i], msg + offset, 1, matches, 0);
160 s = matches[0].rm_so + offset;
161 offset = matches[0].rm_eo + offset;
163 g_array_append_val (start, s);
164 g_array_append_val (end, offset);
169 empathy_debug (DEBUG_DOMAIN,
170 "Found %d matches for ALL regex types",
177 empathy_strcasecmp (const gchar *s1,
180 return empathy_strncasecmp (s1, s2, -1);
184 empathy_strncasecmp (const gchar *s1,
191 u1 = g_utf8_casefold (s1, n);
192 u2 = g_utf8_casefold (s2, n);
194 ret_val = g_utf8_collate (u1, u2);
201 /* Stolen from telepathy-glib */
203 empathy_strdiff (const gchar *left, const gchar *right)
205 if ((NULL == left) != (NULL == right))
208 else if (left == right)
212 return (0 != strcmp (left, right));
215 /* Stolen from telepathy-glib */
216 static inline gboolean
217 _esc_ident_bad (gchar c, gboolean is_first)
219 return ((c < 'a' || c > 'z') &&
220 (c < 'A' || c > 'Z') &&
221 (c < '0' || c > '9' || is_first));
224 /* Stolen from telepathy-glib */
226 empathy_escape_as_identifier (const gchar *name)
228 gboolean bad = FALSE;
231 const gchar *ptr, *first_ok;
233 g_return_val_if_fail (name != NULL, NULL);
235 for (ptr = name; *ptr; ptr++)
237 if (_esc_ident_bad (*ptr, ptr == name))
246 /* fast path if it's clean */
248 return g_strdup (name);
250 /* If strictly less than ptr, first_ok is the first uncopied safe character.
253 op = g_string_sized_new (len);
254 for (ptr = name; *ptr; ptr++)
256 if (_esc_ident_bad (*ptr, ptr == name))
258 /* copy preceding safe characters if any */
261 g_string_append_len (op, first_ok, ptr - first_ok);
263 /* escape the unsafe character */
264 g_string_append_printf (op, "_%02x", (unsigned char)(*ptr));
265 /* restart after it */
269 /* copy trailing safe characters if any */
272 g_string_append_len (op, first_ok, ptr - first_ok);
274 return g_string_free (op, FALSE);
278 empathy_xml_validate (xmlDoc *doc,
279 const gchar *dtd_filename)
281 gchar *path, *escaped;
286 path = g_build_filename (DATADIR, "empathy", dtd_filename, NULL);
288 /* The list of valid chars is taken from libxml. */
289 escaped = xmlURIEscapeStr (path, ":@&=+$,/?;");
293 memset (&cvp, 0, sizeof (cvp));
294 dtd = xmlParseDTD (NULL, escaped);
295 ret = xmlValidateDtd (&cvp, doc, dtd);
304 empathy_xml_node_get_child (xmlNodePtr node,
305 const gchar *child_name)
309 g_return_val_if_fail (node != NULL, NULL);
310 g_return_val_if_fail (child_name != NULL, NULL);
312 for (l = node->children; l; l = l->next) {
313 if (l->name && strcmp (l->name, child_name) == 0) {
322 empathy_xml_node_get_child_content (xmlNodePtr node,
323 const gchar *child_name)
327 g_return_val_if_fail (node != NULL, NULL);
328 g_return_val_if_fail (child_name != NULL, NULL);
330 l = empathy_xml_node_get_child (node, child_name);
332 return xmlNodeGetContent (l);
339 empathy_xml_node_find_child_prop_value (xmlNodePtr node,
340 const gchar *prop_name,
341 const gchar *prop_value)
344 xmlNodePtr found = NULL;
346 g_return_val_if_fail (node != NULL, NULL);
347 g_return_val_if_fail (prop_name != NULL, NULL);
348 g_return_val_if_fail (prop_value != NULL, NULL);
350 for (l = node->children; l && !found; l = l->next) {
353 if (!xmlHasProp (l, prop_name)) {
357 prop = xmlGetProp (l, prop_name);
358 if (prop && strcmp (prop, prop_value) == 0) {
369 empathy_account_hash (gconstpointer key)
371 return g_str_hash (mc_account_get_unique_name (MC_ACCOUNT (key)));
375 empathy_account_equal (gconstpointer a,
381 name_a = mc_account_get_unique_name (MC_ACCOUNT (a));
382 name_b = mc_account_get_unique_name (MC_ACCOUNT (b));
384 return g_str_equal (name_a, name_b);
388 empathy_mission_control_new (void)
390 static MissionControl *mc = NULL;
393 mc = mission_control_new (tp_get_bus ());
394 g_object_add_weak_pointer (G_OBJECT (mc), (gpointer) &mc);
403 empathy_inspect_channel (McAccount *account,
406 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
407 g_return_val_if_fail (TELEPATHY_IS_CHAN (tp_chan), NULL);
409 return empathy_inspect_handle (account,
411 tp_chan->handle_type);
415 empathy_inspect_handle (McAccount *account,
426 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
427 g_return_val_if_fail (handle != 0, NULL);
428 g_return_val_if_fail (handle_type != 0, NULL);
430 mc = empathy_mission_control_new ();
431 tp_conn = mission_control_get_connection (mc, account, NULL);
438 /* Get the handle's name */
439 handles = g_array_new (FALSE, FALSE, sizeof (guint));
440 g_array_append_val (handles, handle);
441 if (!tp_conn_inspect_handles (DBUS_G_PROXY (tp_conn),
446 empathy_debug (DEBUG_DOMAIN,
447 "Couldn't get id: %s",
448 error ? error->message : "No error given");
450 g_clear_error (&error);
451 g_array_free (handles, TRUE);
452 g_object_unref (tp_conn);
457 g_array_free (handles, TRUE);
460 g_object_unref (tp_conn);