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-utils.h"
41 #include "empathy-contact-factory.h"
42 #include "empathy-contact-manager.h"
44 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
45 #include "empathy-debug.h"
47 static void regex_init (void);
50 empathy_substring (const gchar *str,
54 return g_strndup (str + start, end - start);
58 * Regular Expression code to match urls.
60 #define APTCHARS "-A-Za-z0-9,-."
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_APT:
96 "apt://[" APTCHARS "]*";
98 case EMPATHY_REGEX_EMAIL:
100 "(mailto:)?[a-z0-9][a-z0-9.-]*@[a-z0-9]"
101 "[a-z0-9-]*(\\.[a-z0-9][a-z0-9-]*)+";
103 case EMPATHY_REGEX_OTHER:
105 "news:[-A-Z\\^_a-z{|}~!\"#$%&'()*+,./0-9;:=?`]+"
106 "@[" HOSTCHARS ".]+(:[0-9]+)?";
109 /* Silence the compiler. */
114 memset (&dingus[i], 0, sizeof (regex_t));
115 regcomp (&dingus[i], expression, REG_EXTENDED | REG_ICASE);
122 empathy_regex_match (EmpathyRegExType type,
127 regmatch_t matches[1];
129 gint num_matches = 0;
133 g_return_val_if_fail (type >= 0 || type <= EMPATHY_REGEX_ALL, 0);
137 while (!ret && type != EMPATHY_REGEX_ALL) {
138 ret = regexec (&dingus[type], msg + offset, 1, matches, 0);
144 s = matches[0].rm_so + offset;
145 offset = matches[0].rm_eo + offset;
147 g_array_append_val (start, s);
148 g_array_append_val (end, offset);
152 if (type != EMPATHY_REGEX_ALL) {
153 DEBUG ("Found %d matches for regex type:%d", num_matches, type);
157 /* If EMPATHY_REGEX_ALL then we run ALL regex's on the string. */
158 for (i = 0; i < EMPATHY_REGEX_ALL; i++, ret = 0) {
160 ret = regexec (&dingus[i], msg + offset, 1, matches, 0);
166 s = matches[0].rm_so + offset;
167 offset = matches[0].rm_eo + offset;
169 g_array_append_val (start, s);
170 g_array_append_val (end, offset);
175 DEBUG ("Found %d matches for ALL regex types", num_matches);
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 DEBUG ("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_presence_get_default_message (McPresence presence)
344 case MC_PRESENCE_AVAILABLE:
345 return _("Available");
346 case MC_PRESENCE_DO_NOT_DISTURB:
348 case MC_PRESENCE_AWAY:
349 case MC_PRESENCE_EXTENDED_AWAY:
351 case MC_PRESENCE_HIDDEN:
353 case MC_PRESENCE_OFFLINE:
354 case MC_PRESENCE_UNSET:
357 g_assert_not_reached ();
364 empathy_presence_to_str (McPresence presence)
367 case MC_PRESENCE_AVAILABLE:
369 case MC_PRESENCE_DO_NOT_DISTURB:
371 case MC_PRESENCE_AWAY:
373 case MC_PRESENCE_EXTENDED_AWAY:
375 case MC_PRESENCE_HIDDEN:
377 case MC_PRESENCE_OFFLINE:
379 case MC_PRESENCE_UNSET:
382 g_assert_not_reached ();
389 empathy_presence_from_str (const gchar *str)
391 if (strcmp (str, "available") == 0) {
392 return MC_PRESENCE_AVAILABLE;
393 } else if ((strcmp (str, "dnd") == 0) || (strcmp (str, "busy") == 0)) {
394 return MC_PRESENCE_DO_NOT_DISTURB;
395 } else if ((strcmp (str, "away") == 0) || (strcmp (str, "brb") == 0)) {
396 return MC_PRESENCE_AWAY;
397 } else if ((strcmp (str, "xa") == 0) || (strcmp (str, "ext_away") == 0)) {
398 return MC_PRESENCE_EXTENDED_AWAY;
399 } else if (strcmp (str, "hidden") == 0) {
400 return MC_PRESENCE_HIDDEN;
401 } else if (strcmp (str, "offline") == 0) {
402 return MC_PRESENCE_OFFLINE;
403 } else if (strcmp (str, "unset") == 0) {
404 return MC_PRESENCE_UNSET;
407 return MC_PRESENCE_UNSET;
411 empathy_file_lookup (const gchar *filename, const gchar *subdir)
419 path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), subdir, filename, NULL);
420 if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
422 path = g_build_filename (DATADIR, "empathy", filename, NULL);
429 EmpathyRunUntilReadyFunc func;
436 run_until_ready_cb (RunUntilReadyData *data)
438 if (!data->func || data->func (data->object, data->user_data)) {
439 DEBUG ("Object %p is ready", data->object);
440 g_main_loop_quit (data->loop);
445 object_is_ready (GObject *object,
450 g_object_get (object, "ready", &ready, NULL);
456 empathy_run_until_ready_full (gpointer object,
458 EmpathyRunUntilReadyFunc func,
462 RunUntilReadyData data;
465 g_return_if_fail (G_IS_OBJECT (object));
466 g_return_if_fail (signal != NULL);
468 if (func && func (object, user_data)) {
472 DEBUG ("Starting run until ready for object %p", object);
475 data.user_data = user_data;
476 data.object = object;
477 data.loop = g_main_loop_new (NULL, FALSE);
479 signal_id = g_signal_connect_swapped (object, signal,
480 G_CALLBACK (run_until_ready_cb),
486 g_main_loop_run (data.loop);
492 g_signal_handler_disconnect (object, signal_id);
493 g_main_loop_unref (data.loop);
497 empathy_run_until_ready (gpointer object)
499 empathy_run_until_ready_full (object, "notify::ready", object_is_ready,
504 empathy_channel_get_account (TpChannel *channel)
506 TpConnection *connection;
510 g_object_get (channel, "connection", &connection, NULL);
511 mc = empathy_mission_control_new ();
512 account = mission_control_get_account_for_tpconnection (mc, connection, NULL);
513 g_object_unref (connection);
519 typedef void (*AccountStatusChangedFunc) (MissionControl *mc,
520 TpConnectionStatus status,
522 TpConnectionStatusReason reason,
523 const gchar *unique_name,
527 AccountStatusChangedFunc handler;
529 GClosureNotify free_func;
531 } AccountStatusChangedData;
534 TpConnectionStatus status;
536 TpConnectionStatusReason reason;
538 AccountStatusChangedData *data;
542 account_status_changed_data_free (gpointer ptr,
545 AccountStatusChangedData *data = ptr;
547 if (data->free_func) {
548 data->free_func (data->user_data, closure);
550 g_object_unref (data->mc);
551 g_slice_free (AccountStatusChangedData, data);
555 account_status_changed_invoke_callback (gpointer data)
557 InvocationData *invocation_data = data;
559 invocation_data->data->handler (invocation_data->data->mc,
560 invocation_data->status,
561 invocation_data->presence,
562 invocation_data->reason,
563 invocation_data->unique_name,
564 invocation_data->data->user_data);
566 g_free (invocation_data->unique_name);
567 g_slice_free (InvocationData, invocation_data);
573 account_status_changed_cb (MissionControl *mc,
574 TpConnectionStatus status,
576 TpConnectionStatusReason reason,
577 const gchar *unique_name,
578 AccountStatusChangedData *data)
580 InvocationData *invocation_data;
582 invocation_data = g_slice_new (InvocationData);
583 invocation_data->status = status;
584 invocation_data->presence = presence;
585 invocation_data->reason = reason;
586 invocation_data->unique_name = g_strdup (unique_name);
587 invocation_data->data = data;
589 g_idle_add_full (G_PRIORITY_HIGH,
590 account_status_changed_invoke_callback,
591 invocation_data, NULL);
595 empathy_connect_to_account_status_changed (MissionControl *mc,
598 GClosureNotify free_func)
600 AccountStatusChangedData *data;
602 g_return_val_if_fail (IS_MISSIONCONTROL (mc), NULL);
603 g_return_val_if_fail (handler != NULL, NULL);
605 data = g_slice_new (AccountStatusChangedData);
606 data->handler = (AccountStatusChangedFunc) handler;
607 data->user_data = user_data;
608 data->free_func = free_func;
609 data->mc = g_object_ref (mc);
611 dbus_g_proxy_connect_signal (DBUS_G_PROXY (mc), "AccountStatusChanged",
612 G_CALLBACK (account_status_changed_cb),
613 data, account_status_changed_data_free);
619 empathy_disconnect_account_status_changed (gpointer token)
621 AccountStatusChangedData *data = token;
623 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (data->mc),
624 "AccountStatusChanged",
625 G_CALLBACK (account_status_changed_cb),
630 empathy_proxy_hash (gconstpointer key)
632 TpProxy *proxy = TP_PROXY (key);
633 TpProxyClass *proxy_class = TP_PROXY_GET_CLASS (key);
635 g_return_val_if_fail (TP_IS_PROXY (proxy), 0);
636 g_return_val_if_fail (proxy_class->must_have_unique_name, 0);
638 return g_str_hash (proxy->object_path) ^ g_str_hash (proxy->bus_name);
642 empathy_proxy_equal (gconstpointer a,
645 TpProxy *proxy_a = TP_PROXY (a);
646 TpProxy *proxy_b = TP_PROXY (b);
647 TpProxyClass *proxy_a_class = TP_PROXY_GET_CLASS (a);
648 TpProxyClass *proxy_b_class = TP_PROXY_GET_CLASS (b);
650 g_return_val_if_fail (TP_IS_PROXY (proxy_a), FALSE);
651 g_return_val_if_fail (TP_IS_PROXY (proxy_b), FALSE);
652 g_return_val_if_fail (proxy_a_class->must_have_unique_name, 0);
653 g_return_val_if_fail (proxy_b_class->must_have_unique_name, 0);
655 return g_str_equal (proxy_a->object_path, proxy_b->object_path) &&
656 g_str_equal (proxy_a->bus_name, proxy_b->bus_name);
663 empathy_connection_callback_for_request_channel callback;
665 GDestroyNotify destroy;
667 gboolean suppress_handler;
669 } ConnectionRequestChannelData;
672 connection_request_channel_data_unref (gpointer user_data)
674 ConnectionRequestChannelData *data = (ConnectionRequestChannelData*) user_data;
676 if (--data->ref_count == 0) {
677 g_free (data->channel_type);
679 data->destroy (data->user_data);
681 g_slice_free (ConnectionRequestChannelData, data);
686 connection_request_channel_cb (TpConnection *connection,
687 const gchar *object_path,
690 GObject *weak_object)
692 ConnectionRequestChannelData *data = (ConnectionRequestChannelData*) user_data;
695 if (!data->callback) {
700 data->callback (connection, NULL, error, data->user_data, weak_object);
704 channel = tp_channel_new (connection, object_path,
709 data->callback (connection, channel, NULL, data->user_data, weak_object);
710 g_object_unref (channel);
714 connection_request_handles_cb (TpConnection *connection,
715 const GArray *handles,
718 GObject *weak_object)
720 ConnectionRequestChannelData *data = (ConnectionRequestChannelData*) user_data;
723 if (data->callback) {
724 data->callback (connection, NULL, error, data->user_data, weak_object);
729 data->handle = g_array_index (handles, guint, 0);
731 tp_cli_connection_call_request_channel (connection, data->timeout_ms,
735 data->suppress_handler,
736 connection_request_channel_cb,
738 (GDestroyNotify) connection_request_channel_data_unref,
743 empathy_connection_request_channel (TpConnection *connection,
745 const gchar *channel_type,
748 gboolean suppress_handler,
749 empathy_connection_callback_for_request_channel callback,
751 GDestroyNotify destroy,
752 GObject *weak_object)
754 const gchar *names[] = {name, NULL};
755 ConnectionRequestChannelData *data;
757 data = g_slice_new (ConnectionRequestChannelData);
758 data->timeout_ms = timeout_ms;
759 data->channel_type = g_strdup (channel_type);
760 data->handle_type = handle_type;
761 data->callback = callback;
762 data->user_data = user_data;
763 data->destroy = destroy;
765 data->suppress_handler = suppress_handler;
767 tp_cli_connection_call_request_handles (connection,
771 connection_request_handles_cb,
773 (GDestroyNotify) connection_request_channel_data_unref,