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-2008 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 <telepathy-glib/connection.h>
37 #include <telepathy-glib/channel.h>
38 #include <telepathy-glib/dbus.h>
40 #include "empathy-debug.h"
41 #include "empathy-utils.h"
42 #include "empathy-contact-factory.h"
43 #include "empathy-contact-manager.h"
44 #include "empathy-tp-group.h"
46 #define DEBUG_DOMAIN "Utils"
48 static void regex_init (void);
51 empathy_substring (const gchar *str,
55 return g_strndup (str + start, end - start);
59 * Regular Expression code to match urls.
61 #define USERCHARS "-A-Za-z0-9"
62 #define PASSCHARS "-A-Za-z0-9,?;.:/!%$^*&~\"#'"
63 #define HOSTCHARS "-A-Za-z0-9_"
64 #define PATHCHARS "-A-Za-z0-9_$.+!*(),;:@&=?/~#%"
65 #define SCHEME "(news:|telnet:|nntp:|file:/|https?:|ftps?:|webcal:)"
66 #define USER "[" USERCHARS "]+(:["PASSCHARS "]+)?"
67 #define URLPATH "/[" PATHCHARS "]*[^]'.}>) \t\r\n,\\\"]"
69 static regex_t dingus[EMPATHY_REGEX_ALL];
74 static gboolean inited = FALSE;
75 const gchar *expression;
82 for (i = 0; i < EMPATHY_REGEX_ALL; i++) {
84 case EMPATHY_REGEX_AS_IS:
86 SCHEME "//(" USER "@)?[" HOSTCHARS ".]+"
87 "(:[0-9]+)?(" URLPATH ")?";
89 case EMPATHY_REGEX_BROWSER:
91 "(www|ftp)[" HOSTCHARS "]*\\.[" HOSTCHARS ".]+"
92 "(:[0-9]+)?(" URLPATH ")?";
94 case EMPATHY_REGEX_EMAIL:
96 "(mailto:)?[a-z0-9][a-z0-9.-]*@[a-z0-9]"
97 "[a-z0-9-]*(\\.[a-z0-9][a-z0-9-]*)+";
99 case EMPATHY_REGEX_OTHER:
101 "news:[-A-Z\\^_a-z{|}~!\"#$%&'()*+,./0-9;:=?`]+"
102 "@[" HOSTCHARS ".]+(:[0-9]+)?";
105 /* Silence the compiler. */
110 memset (&dingus[i], 0, sizeof (regex_t));
111 regcomp (&dingus[i], expression, REG_EXTENDED | REG_ICASE);
118 empathy_regex_match (EmpathyRegExType type,
123 regmatch_t matches[1];
125 gint num_matches = 0;
129 g_return_val_if_fail (type >= 0 || type <= EMPATHY_REGEX_ALL, 0);
133 while (!ret && type != EMPATHY_REGEX_ALL) {
134 ret = regexec (&dingus[type], msg + offset, 1, matches, 0);
140 s = matches[0].rm_so + offset;
141 offset = matches[0].rm_eo + offset;
143 g_array_append_val (start, s);
144 g_array_append_val (end, offset);
148 if (type != EMPATHY_REGEX_ALL) {
149 empathy_debug (DEBUG_DOMAIN,
150 "Found %d matches for regex type:%d",
155 /* If EMPATHY_REGEX_ALL then we run ALL regex's on the string. */
156 for (i = 0; i < EMPATHY_REGEX_ALL; i++, ret = 0) {
158 ret = regexec (&dingus[i], msg + offset, 1, matches, 0);
164 s = matches[0].rm_so + offset;
165 offset = matches[0].rm_eo + offset;
167 g_array_append_val (start, s);
168 g_array_append_val (end, offset);
173 empathy_debug (DEBUG_DOMAIN,
174 "Found %d matches for ALL regex types",
181 empathy_strcasecmp (const gchar *s1,
184 return empathy_strncasecmp (s1, s2, -1);
188 empathy_strncasecmp (const gchar *s1,
195 u1 = g_utf8_casefold (s1, n);
196 u2 = g_utf8_casefold (s2, n);
198 ret_val = g_utf8_collate (u1, u2);
206 empathy_xml_validate (xmlDoc *doc,
207 const gchar *dtd_filename)
209 gchar *path, *escaped;
214 path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "libempathy",
216 if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
218 path = g_build_filename (DATADIR, "empathy", dtd_filename, NULL);
220 empathy_debug (DEBUG_DOMAIN, "Loading dtd file %s", path);
222 /* The list of valid chars is taken from libxml. */
223 escaped = xmlURIEscapeStr (path, ":@&=+$,/?;");
226 memset (&cvp, 0, sizeof (cvp));
227 dtd = xmlParseDTD (NULL, escaped);
228 ret = xmlValidateDtd (&cvp, doc, dtd);
237 empathy_xml_node_get_child (xmlNodePtr node,
238 const gchar *child_name)
242 g_return_val_if_fail (node != NULL, NULL);
243 g_return_val_if_fail (child_name != NULL, NULL);
245 for (l = node->children; l; l = l->next) {
246 if (l->name && strcmp (l->name, child_name) == 0) {
255 empathy_xml_node_get_child_content (xmlNodePtr node,
256 const gchar *child_name)
260 g_return_val_if_fail (node != NULL, NULL);
261 g_return_val_if_fail (child_name != NULL, NULL);
263 l = empathy_xml_node_get_child (node, child_name);
265 return xmlNodeGetContent (l);
272 empathy_xml_node_find_child_prop_value (xmlNodePtr node,
273 const gchar *prop_name,
274 const gchar *prop_value)
277 xmlNodePtr found = NULL;
279 g_return_val_if_fail (node != NULL, NULL);
280 g_return_val_if_fail (prop_name != NULL, NULL);
281 g_return_val_if_fail (prop_value != NULL, NULL);
283 for (l = node->children; l && !found; l = l->next) {
286 if (!xmlHasProp (l, prop_name)) {
290 prop = xmlGetProp (l, prop_name);
291 if (prop && strcmp (prop, prop_value) == 0) {
302 empathy_account_hash (gconstpointer key)
304 g_return_val_if_fail (MC_IS_ACCOUNT (key), 0);
306 return g_str_hash (mc_account_get_unique_name (MC_ACCOUNT (key)));
310 empathy_account_equal (gconstpointer a,
316 g_return_val_if_fail (MC_IS_ACCOUNT (a), FALSE);
317 g_return_val_if_fail (MC_IS_ACCOUNT (b), FALSE);
319 name_a = mc_account_get_unique_name (MC_ACCOUNT (a));
320 name_b = mc_account_get_unique_name (MC_ACCOUNT (b));
322 return g_str_equal (name_a, name_b);
326 empathy_mission_control_new (void)
328 static MissionControl *mc = NULL;
331 mc = mission_control_new (tp_get_bus ());
332 g_object_add_weak_pointer (G_OBJECT (mc), (gpointer) &mc);
341 empathy_call_with_contact (EmpathyContact *contact)
346 TpConnection *connection;
349 EmpathyContactFactory *factory;
350 EmpathyTpGroup *group;
351 EmpathyContact *self_contact;
352 GError *error = NULL;
354 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
356 /* StreamedMedia channels must have handle=0 and handle_type=none.
357 * To call a contact we have to add him in the group interface of the
358 * channel. MissionControl will detect the channel creation and
359 * dispatch it to the VoIP chandler automatically. */
361 mc = empathy_mission_control_new ();
362 account = empathy_contact_get_account (contact);
363 connection = mission_control_get_tpconnection (mc, account, NULL);
364 tp_connection_run_until_ready (connection, FALSE, NULL, NULL);
366 if (!tp_cli_connection_run_request_channel (connection, -1,
367 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
374 empathy_debug (DEBUG_DOMAIN,
375 "Couldn't request channel: %s",
376 error ? error->message : "No error given");
377 g_clear_error (&error);
379 g_object_unref (connection);
383 channel = tp_channel_new (connection,
384 object_path, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
385 TP_HANDLE_TYPE_NONE, 0, NULL);
387 group = empathy_tp_group_new (channel);
388 empathy_run_until_ready (group);
390 factory = empathy_contact_factory_new ();
391 self_contact = empathy_contact_factory_get_user (factory, account);
392 empathy_contact_run_until_ready (self_contact,
393 EMPATHY_CONTACT_READY_HANDLE,
396 empathy_tp_group_add_member (group, contact, "");
397 empathy_tp_group_add_member (group, self_contact, "");
399 g_object_unref (factory);
400 g_object_unref (self_contact);
401 g_object_unref (group);
403 g_object_unref (connection);
404 g_object_unref (channel);
405 g_free (object_path);
410 empathy_call_with_contact_id (McAccount *account, const gchar *contact_id)
413 EmpathyContactFactory *factory;
414 EmpathyContact *contact;
416 factory = empathy_contact_factory_new ();
417 contact = empathy_contact_factory_get_from_id (factory, account, contact_id);
418 empathy_contact_run_until_ready (contact, EMPATHY_CONTACT_READY_HANDLE, NULL);
420 empathy_call_with_contact (contact);
422 g_object_unref (contact);
423 g_object_unref (factory);
428 empathy_chat_with_contact (EmpathyContact *contact)
432 mc = empathy_mission_control_new ();
433 mission_control_request_channel (mc,
434 empathy_contact_get_account (contact),
435 TP_IFACE_CHANNEL_TYPE_TEXT,
436 empathy_contact_get_handle (contact),
437 TP_HANDLE_TYPE_CONTACT,
443 empathy_chat_with_contact_id (McAccount *account, const gchar *contact_id)
447 mc = empathy_mission_control_new ();
448 mission_control_request_channel_with_string_handle (mc,
450 TP_IFACE_CHANNEL_TYPE_TEXT,
452 TP_HANDLE_TYPE_CONTACT,
458 empathy_presence_get_default_message (McPresence presence)
461 case MC_PRESENCE_AVAILABLE:
462 return _("Available");
463 case MC_PRESENCE_DO_NOT_DISTURB:
465 case MC_PRESENCE_AWAY:
466 case MC_PRESENCE_EXTENDED_AWAY:
468 case MC_PRESENCE_HIDDEN:
470 case MC_PRESENCE_OFFLINE:
471 case MC_PRESENCE_UNSET:
474 g_assert_not_reached ();
481 empathy_presence_to_str (McPresence presence)
484 case MC_PRESENCE_AVAILABLE:
486 case MC_PRESENCE_DO_NOT_DISTURB:
488 case MC_PRESENCE_AWAY:
490 case MC_PRESENCE_EXTENDED_AWAY:
492 case MC_PRESENCE_HIDDEN:
494 case MC_PRESENCE_OFFLINE:
496 case MC_PRESENCE_UNSET:
499 g_assert_not_reached ();
506 empathy_presence_from_str (const gchar *str)
508 if (strcmp (str, "available") == 0) {
509 return MC_PRESENCE_AVAILABLE;
510 } else if ((strcmp (str, "dnd") == 0) || (strcmp (str, "busy") == 0)) {
511 return MC_PRESENCE_DO_NOT_DISTURB;
512 } else if ((strcmp (str, "away") == 0) || (strcmp (str, "brb") == 0)) {
513 return MC_PRESENCE_AWAY;
514 } else if ((strcmp (str, "xa") == 0) || (strcmp (str, "ext_away") == 0)) {
515 return MC_PRESENCE_EXTENDED_AWAY;
516 } else if (strcmp (str, "hidden") == 0) {
517 return MC_PRESENCE_HIDDEN;
518 } else if (strcmp (str, "offline") == 0) {
519 return MC_PRESENCE_OFFLINE;
520 } else if (strcmp (str, "unset") == 0) {
521 return MC_PRESENCE_UNSET;
524 return MC_PRESENCE_AVAILABLE;
528 empathy_file_lookup (const gchar *filename, const gchar *subdir)
536 path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), subdir, filename, NULL);
537 if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
539 path = g_build_filename (DATADIR, "empathy", filename, NULL);
546 EmpathyRunUntilReadyFunc func;
553 run_until_ready_cb (RunUntilReadyData *data)
555 if (!data->func || data->func (data->object, data->user_data)) {
556 empathy_debug (DEBUG_DOMAIN, "Object %p is ready", data->object);
557 g_main_loop_quit (data->loop);
562 object_is_ready (GObject *object,
567 g_object_get (object, "ready", &ready, NULL);
573 empathy_run_until_ready_full (gpointer object,
575 EmpathyRunUntilReadyFunc func,
579 RunUntilReadyData data;
582 g_return_if_fail (G_IS_OBJECT (object));
583 g_return_if_fail (signal != NULL);
585 if (func && func (object, user_data)) {
589 empathy_debug (DEBUG_DOMAIN, "Starting run until ready for object %p",
593 data.user_data = user_data;
594 data.object = object;
595 data.loop = g_main_loop_new (NULL, FALSE);
597 signal_id = g_signal_connect_swapped (object, signal,
598 G_CALLBACK (run_until_ready_cb),
604 g_main_loop_run (data.loop);
610 g_signal_handler_disconnect (object, signal_id);
611 g_main_loop_unref (data.loop);
615 empathy_run_until_ready (gpointer object)
617 empathy_run_until_ready_full (object, "notify::ready", object_is_ready,
622 empathy_channel_get_account (TpChannel *channel)
624 TpConnection *connection;
628 g_object_get (channel, "connection", &connection, NULL);
629 mc = empathy_mission_control_new ();
630 account = mission_control_get_account_for_tpconnection (mc, connection, NULL);
631 g_object_unref (connection);