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"
41 #include "empathy-tp-group.h"
43 #define DEBUG_DOMAIN "Utils"
45 static void regex_init (void);
48 empathy_substring (const gchar *str,
52 return g_strndup (str + start, end - start);
56 * Regular Expression code to match urls.
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,\\\"]"
66 static regex_t dingus[EMPATHY_REGEX_ALL];
71 static gboolean inited = FALSE;
72 const gchar *expression;
79 for (i = 0; i < EMPATHY_REGEX_ALL; i++) {
81 case EMPATHY_REGEX_AS_IS:
83 SCHEME "//(" USER "@)?[" HOSTCHARS ".]+"
84 "(:[0-9]+)?(" URLPATH ")?";
86 case EMPATHY_REGEX_BROWSER:
88 "(www|ftp)[" HOSTCHARS "]*\\.[" HOSTCHARS ".]+"
89 "(:[0-9]+)?(" URLPATH ")?";
91 case EMPATHY_REGEX_EMAIL:
93 "(mailto:)?[a-z0-9][a-z0-9.-]*@[a-z0-9]"
94 "[a-z0-9-]*(\\.[a-z0-9][a-z0-9-]*)+";
96 case EMPATHY_REGEX_OTHER:
98 "news:[-A-Z\\^_a-z{|}~!\"#$%&'()*+,./0-9;:=?`]+"
99 "@[" HOSTCHARS ".]+(:[0-9]+)?";
102 /* Silence the compiler. */
107 memset (&dingus[i], 0, sizeof (regex_t));
108 regcomp (&dingus[i], expression, REG_EXTENDED | REG_ICASE);
115 empathy_regex_match (EmpathyRegExType type,
120 regmatch_t matches[1];
122 gint num_matches = 0;
126 g_return_val_if_fail (type >= 0 || type <= EMPATHY_REGEX_ALL, 0);
130 while (!ret && type != EMPATHY_REGEX_ALL) {
131 ret = regexec (&dingus[type], msg + offset, 1, matches, 0);
137 s = matches[0].rm_so + offset;
138 offset = matches[0].rm_eo + offset;
140 g_array_append_val (start, s);
141 g_array_append_val (end, offset);
145 if (type != EMPATHY_REGEX_ALL) {
146 empathy_debug (DEBUG_DOMAIN,
147 "Found %d matches for regex type:%d",
152 /* If EMPATHY_REGEX_ALL then we run ALL regex's on the string. */
153 for (i = 0; i < EMPATHY_REGEX_ALL; i++, ret = 0) {
155 ret = regexec (&dingus[i], msg + offset, 1, matches, 0);
161 s = matches[0].rm_so + offset;
162 offset = matches[0].rm_eo + offset;
164 g_array_append_val (start, s);
165 g_array_append_val (end, offset);
170 empathy_debug (DEBUG_DOMAIN,
171 "Found %d matches for ALL regex types",
178 empathy_strcasecmp (const gchar *s1,
181 return empathy_strncasecmp (s1, s2, -1);
185 empathy_strncasecmp (const gchar *s1,
192 u1 = g_utf8_casefold (s1, n);
193 u2 = g_utf8_casefold (s2, n);
195 ret_val = g_utf8_collate (u1, u2);
203 empathy_xml_validate (xmlDoc *doc,
204 const gchar *dtd_filename)
206 gchar *path, *escaped;
211 path = g_build_filename (UNINSTALLED_DTD_DIR, dtd_filename, NULL);
212 if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
214 path = g_build_filename (DATADIR, "empathy", dtd_filename, NULL);
216 empathy_debug (DEBUG_DOMAIN, "Loading dtd file %s", path);
218 /* The list of valid chars is taken from libxml. */
219 escaped = xmlURIEscapeStr (path, ":@&=+$,/?;");
222 memset (&cvp, 0, sizeof (cvp));
223 dtd = xmlParseDTD (NULL, escaped);
224 ret = xmlValidateDtd (&cvp, doc, dtd);
233 empathy_xml_node_get_child (xmlNodePtr node,
234 const gchar *child_name)
238 g_return_val_if_fail (node != NULL, NULL);
239 g_return_val_if_fail (child_name != NULL, NULL);
241 for (l = node->children; l; l = l->next) {
242 if (l->name && strcmp (l->name, child_name) == 0) {
251 empathy_xml_node_get_child_content (xmlNodePtr node,
252 const gchar *child_name)
256 g_return_val_if_fail (node != NULL, NULL);
257 g_return_val_if_fail (child_name != NULL, NULL);
259 l = empathy_xml_node_get_child (node, child_name);
261 return xmlNodeGetContent (l);
268 empathy_xml_node_find_child_prop_value (xmlNodePtr node,
269 const gchar *prop_name,
270 const gchar *prop_value)
273 xmlNodePtr found = NULL;
275 g_return_val_if_fail (node != NULL, NULL);
276 g_return_val_if_fail (prop_name != NULL, NULL);
277 g_return_val_if_fail (prop_value != NULL, NULL);
279 for (l = node->children; l && !found; l = l->next) {
282 if (!xmlHasProp (l, prop_name)) {
286 prop = xmlGetProp (l, prop_name);
287 if (prop && strcmp (prop, prop_value) == 0) {
298 empathy_account_hash (gconstpointer key)
300 g_return_val_if_fail (MC_IS_ACCOUNT (key), 0);
302 return g_str_hash (mc_account_get_unique_name (MC_ACCOUNT (key)));
306 empathy_account_equal (gconstpointer a,
312 g_return_val_if_fail (MC_IS_ACCOUNT (a), FALSE);
313 g_return_val_if_fail (MC_IS_ACCOUNT (b), FALSE);
315 name_a = mc_account_get_unique_name (MC_ACCOUNT (a));
316 name_b = mc_account_get_unique_name (MC_ACCOUNT (b));
318 return g_str_equal (name_a, name_b);
322 empathy_mission_control_new (void)
324 static MissionControl *mc = NULL;
327 mc = mission_control_new (tp_get_bus ());
328 g_object_add_weak_pointer (G_OBJECT (mc), (gpointer) &mc);
337 empathy_inspect_channel (McAccount *account,
340 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
341 g_return_val_if_fail (TELEPATHY_IS_CHAN (tp_chan), NULL);
343 return empathy_inspect_handle (account,
345 tp_chan->handle_type);
349 empathy_inspect_handle (McAccount *account,
358 GError *error = NULL;
360 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
361 g_return_val_if_fail (handle != 0, NULL);
362 g_return_val_if_fail (handle_type != 0, NULL);
364 mc = empathy_mission_control_new ();
365 tp_conn = mission_control_get_connection (mc, account, NULL);
372 /* Get the handle's name */
373 handles = g_array_new (FALSE, FALSE, sizeof (guint));
374 g_array_append_val (handles, handle);
375 if (!tp_conn_inspect_handles (DBUS_G_PROXY (tp_conn),
380 empathy_debug (DEBUG_DOMAIN,
381 "Couldn't get id: %s",
382 error ? error->message : "No error given");
384 g_clear_error (&error);
385 g_array_free (handles, TRUE);
386 g_object_unref (tp_conn);
391 g_array_free (handles, TRUE);
394 g_object_unref (tp_conn);
400 empathy_call_contact (EmpathyContact *contact)
407 const gchar *bus_name;
409 EmpathyTpGroup *group;
410 GError *error = NULL;
412 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
414 /* StreamedMedia channels must have handle=0 and handle_type=none.
415 * To call a contact we have to add him in the group interface of the
416 * channel. MissionControl will detect the channel creation and
417 * dispatch it to the VoIP chandler automatically. */
419 mc = empathy_mission_control_new ();
420 account = empathy_contact_get_account (contact);
421 tp_conn = mission_control_get_connection (mc, account, NULL);
422 /* FIXME: Should be async */
423 if (!tp_conn_request_channel (DBUS_G_PROXY (tp_conn),
424 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
430 empathy_debug (DEBUG_DOMAIN,
431 "Couldn't request channel: %s",
432 error ? error->message : "No error given");
433 g_clear_error (&error);
435 g_object_unref (tp_conn);
439 bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (tp_conn));
440 new_chan = tp_chan_new (tp_get_bus (),
443 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
447 group = empathy_tp_group_new (account, new_chan);
448 empathy_tp_group_add_member (group, contact, "");
450 g_object_unref (group);
452 g_object_unref (tp_conn);
453 g_object_unref (new_chan);
454 g_free (object_path);
459 empathy_chat_with_contact (EmpathyContact *contact)
463 mc = empathy_mission_control_new ();
464 mission_control_request_channel (mc,
465 empathy_contact_get_account (contact),
466 TP_IFACE_CHANNEL_TYPE_TEXT,
467 empathy_contact_get_handle (contact),
468 TP_HANDLE_TYPE_CONTACT,
474 empathy_chat_with_contact_id (McAccount *account, const gchar *contact_id)
478 mc = empathy_mission_control_new ();
479 mission_control_request_channel_with_string_handle (mc,
481 TP_IFACE_CHANNEL_TYPE_TEXT,
483 TP_HANDLE_TYPE_CONTACT,