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 <libgnomevfs/gnome-vfs.h>
36 #include <libxml/uri.h>
37 #include <telepathy-glib/connection.h>
38 #include <telepathy-glib/channel.h>
39 #include <telepathy-glib/dbus.h>
41 #include <extensions/extensions.h>
43 #include "empathy-utils.h"
44 #include "empathy-contact-factory.h"
45 #include "empathy-contact-manager.h"
47 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
48 #include "empathy-debug.h"
50 static void regex_init (void);
53 empathy_substring (const gchar *str,
57 return g_strndup (str + start, end - start);
61 * Regular Expression code to match urls.
63 #define APTCHARS "-A-Za-z0-9,-."
64 #define USERCHARS "-A-Za-z0-9"
65 #define PASSCHARS "-A-Za-z0-9,?;.:/!%$^*&~\"#'"
66 #define HOSTCHARS "-A-Za-z0-9_"
67 #define PATHCHARS "-A-Za-z0-9_$.+!*(),;:@&=?/~#%"
68 #define SCHEME "(news:|telnet:|nntp:|file:/|https?:|ftps?:|webcal:)"
69 #define USER "[" USERCHARS "]+(:["PASSCHARS "]+)?"
70 #define URLPATH "/[" PATHCHARS "]*[^]'.}>) \t\r\n,\\\"]"
72 static regex_t dingus[EMPATHY_REGEX_ALL];
77 static gboolean inited = FALSE;
78 const gchar *expression;
85 for (i = 0; i < EMPATHY_REGEX_ALL; i++) {
87 case EMPATHY_REGEX_AS_IS:
89 SCHEME "//(" USER "@)?[" HOSTCHARS ".]+"
90 "(:[0-9]+)?(" URLPATH ")?";
92 case EMPATHY_REGEX_BROWSER:
94 "(www|ftp)[" HOSTCHARS "]*\\.[" HOSTCHARS ".]+"
95 "(:[0-9]+)?(" URLPATH ")?";
97 case EMPATHY_REGEX_APT:
99 "apt://[" APTCHARS "]*";
101 case EMPATHY_REGEX_EMAIL:
103 "(mailto:)?[a-z0-9][a-z0-9._-]*@[a-z0-9]"
104 "[a-z0-9-]*(\\.[a-z0-9][a-z0-9-]*)+";
106 case EMPATHY_REGEX_OTHER:
108 "news:[-A-Z\\^_a-z{|}~!\"#$%&'()*+,./0-9;:=?`]+"
109 "@[" HOSTCHARS ".]+(:[0-9]+)?";
112 /* Silence the compiler. */
117 memset (&dingus[i], 0, sizeof (regex_t));
118 regcomp (&dingus[i], expression, REG_EXTENDED | REG_ICASE);
125 empathy_regex_match (EmpathyRegExType type,
130 regmatch_t matches[1];
132 gint num_matches = 0;
136 g_return_val_if_fail (type >= 0 || type <= EMPATHY_REGEX_ALL, 0);
140 while (!ret && type != EMPATHY_REGEX_ALL) {
141 ret = regexec (&dingus[type], msg + offset, 1, matches, 0);
147 s = matches[0].rm_so + offset;
148 offset = matches[0].rm_eo + offset;
150 g_array_append_val (start, s);
151 g_array_append_val (end, offset);
155 if (type != EMPATHY_REGEX_ALL) {
156 DEBUG ("Found %d matches for regex type:%d", num_matches, type);
160 /* If EMPATHY_REGEX_ALL then we run ALL regex's on the string. */
161 for (i = 0; i < EMPATHY_REGEX_ALL; i++, ret = 0) {
163 ret = regexec (&dingus[i], msg + offset, 1, matches, 0);
169 s = matches[0].rm_so + offset;
170 offset = matches[0].rm_eo + offset;
172 g_array_append_val (start, s);
173 g_array_append_val (end, offset);
178 DEBUG ("Found %d matches for ALL regex types", num_matches);
184 empathy_strcasecmp (const gchar *s1,
187 return empathy_strncasecmp (s1, s2, -1);
191 empathy_strncasecmp (const gchar *s1,
198 u1 = g_utf8_casefold (s1, n);
199 u2 = g_utf8_casefold (s2, n);
201 ret_val = g_utf8_collate (u1, u2);
209 empathy_xml_validate (xmlDoc *doc,
210 const gchar *dtd_filename)
212 gchar *path, *escaped;
217 path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "libempathy",
219 if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
221 path = g_build_filename (DATADIR, "empathy", dtd_filename, NULL);
223 DEBUG ("Loading dtd file %s", path);
225 /* The list of valid chars is taken from libxml. */
226 escaped = xmlURIEscapeStr (path, ":@&=+$,/?;");
229 memset (&cvp, 0, sizeof (cvp));
230 dtd = xmlParseDTD (NULL, escaped);
231 ret = xmlValidateDtd (&cvp, doc, dtd);
240 empathy_xml_node_get_child (xmlNodePtr node,
241 const gchar *child_name)
245 g_return_val_if_fail (node != NULL, NULL);
246 g_return_val_if_fail (child_name != NULL, NULL);
248 for (l = node->children; l; l = l->next) {
249 if (l->name && strcmp (l->name, child_name) == 0) {
258 empathy_xml_node_get_child_content (xmlNodePtr node,
259 const gchar *child_name)
263 g_return_val_if_fail (node != NULL, NULL);
264 g_return_val_if_fail (child_name != NULL, NULL);
266 l = empathy_xml_node_get_child (node, child_name);
268 return xmlNodeGetContent (l);
275 empathy_xml_node_find_child_prop_value (xmlNodePtr node,
276 const gchar *prop_name,
277 const gchar *prop_value)
280 xmlNodePtr found = NULL;
282 g_return_val_if_fail (node != NULL, NULL);
283 g_return_val_if_fail (prop_name != NULL, NULL);
284 g_return_val_if_fail (prop_value != NULL, NULL);
286 for (l = node->children; l && !found; l = l->next) {
289 if (!xmlHasProp (l, prop_name)) {
293 prop = xmlGetProp (l, prop_name);
294 if (prop && strcmp (prop, prop_value) == 0) {
305 empathy_account_hash (gconstpointer key)
307 g_return_val_if_fail (MC_IS_ACCOUNT (key), 0);
309 return g_str_hash (mc_account_get_unique_name (MC_ACCOUNT (key)));
313 empathy_account_equal (gconstpointer a,
319 g_return_val_if_fail (MC_IS_ACCOUNT (a), FALSE);
320 g_return_val_if_fail (MC_IS_ACCOUNT (b), FALSE);
322 name_a = mc_account_get_unique_name (MC_ACCOUNT (a));
323 name_b = mc_account_get_unique_name (MC_ACCOUNT (b));
325 return g_str_equal (name_a, name_b);
329 empathy_mission_control_new (void)
331 static MissionControl *mc = NULL;
334 mc = mission_control_new (tp_get_bus ());
335 g_object_add_weak_pointer (G_OBJECT (mc), (gpointer) &mc);
344 empathy_presence_get_default_message (McPresence presence)
347 case MC_PRESENCE_AVAILABLE:
348 return _("Available");
349 case MC_PRESENCE_DO_NOT_DISTURB:
351 case MC_PRESENCE_AWAY:
352 case MC_PRESENCE_EXTENDED_AWAY:
354 case MC_PRESENCE_HIDDEN:
356 case MC_PRESENCE_OFFLINE:
357 case MC_PRESENCE_UNSET:
360 g_assert_not_reached ();
367 empathy_presence_to_str (McPresence presence)
370 case MC_PRESENCE_AVAILABLE:
372 case MC_PRESENCE_DO_NOT_DISTURB:
374 case MC_PRESENCE_AWAY:
376 case MC_PRESENCE_EXTENDED_AWAY:
378 case MC_PRESENCE_HIDDEN:
380 case MC_PRESENCE_OFFLINE:
382 case MC_PRESENCE_UNSET:
385 g_assert_not_reached ();
392 empathy_presence_from_str (const gchar *str)
394 if (strcmp (str, "available") == 0) {
395 return MC_PRESENCE_AVAILABLE;
396 } else if ((strcmp (str, "dnd") == 0) || (strcmp (str, "busy") == 0)) {
397 return MC_PRESENCE_DO_NOT_DISTURB;
398 } else if ((strcmp (str, "away") == 0) || (strcmp (str, "brb") == 0)) {
399 return MC_PRESENCE_AWAY;
400 } else if ((strcmp (str, "xa") == 0) || (strcmp (str, "ext_away") == 0)) {
401 return MC_PRESENCE_EXTENDED_AWAY;
402 } else if (strcmp (str, "hidden") == 0) {
403 return MC_PRESENCE_HIDDEN;
404 } else if (strcmp (str, "offline") == 0) {
405 return MC_PRESENCE_OFFLINE;
406 } else if (strcmp (str, "unset") == 0) {
407 return MC_PRESENCE_UNSET;
410 return MC_PRESENCE_UNSET;
414 empathy_file_lookup (const gchar *filename, const gchar *subdir)
422 path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), subdir, filename, NULL);
423 if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
425 path = g_build_filename (DATADIR, "empathy", filename, NULL);
432 EmpathyRunUntilReadyFunc func;
439 run_until_ready_cb (RunUntilReadyData *data)
441 if (!data->func || data->func (data->object, data->user_data)) {
442 DEBUG ("Object %p is ready", data->object);
443 g_main_loop_quit (data->loop);
448 object_is_ready (GObject *object,
453 g_object_get (object, "ready", &ready, NULL);
459 empathy_run_until_ready_full (gpointer object,
461 EmpathyRunUntilReadyFunc func,
465 RunUntilReadyData data;
468 g_return_if_fail (G_IS_OBJECT (object));
469 g_return_if_fail (signal != NULL);
471 if (func && func (object, user_data)) {
475 DEBUG ("Starting run until ready for object %p", object);
478 data.user_data = user_data;
479 data.object = object;
480 data.loop = g_main_loop_new (NULL, FALSE);
482 signal_id = g_signal_connect_swapped (object, signal,
483 G_CALLBACK (run_until_ready_cb),
489 g_main_loop_run (data.loop);
495 g_signal_handler_disconnect (object, signal_id);
496 g_main_loop_unref (data.loop);
500 empathy_run_until_ready (gpointer object)
502 empathy_run_until_ready_full (object, "notify::ready", object_is_ready,
507 empathy_channel_get_account (TpChannel *channel)
509 TpConnection *connection;
513 g_object_get (channel, "connection", &connection, NULL);
514 mc = empathy_mission_control_new ();
515 account = mission_control_get_account_for_tpconnection (mc, connection, NULL);
516 g_object_unref (connection);
522 typedef void (*AccountStatusChangedFunc) (MissionControl *mc,
523 TpConnectionStatus status,
525 TpConnectionStatusReason reason,
526 const gchar *unique_name,
530 AccountStatusChangedFunc handler;
532 GClosureNotify free_func;
534 } AccountStatusChangedData;
537 TpConnectionStatus status;
539 TpConnectionStatusReason reason;
541 AccountStatusChangedData *data;
545 account_status_changed_data_free (gpointer ptr,
548 AccountStatusChangedData *data = ptr;
550 if (data->free_func) {
551 data->free_func (data->user_data, closure);
553 g_object_unref (data->mc);
554 g_slice_free (AccountStatusChangedData, data);
558 account_status_changed_invoke_callback (gpointer data)
560 InvocationData *invocation_data = data;
562 invocation_data->data->handler (invocation_data->data->mc,
563 invocation_data->status,
564 invocation_data->presence,
565 invocation_data->reason,
566 invocation_data->unique_name,
567 invocation_data->data->user_data);
569 g_free (invocation_data->unique_name);
570 g_slice_free (InvocationData, invocation_data);
576 account_status_changed_cb (MissionControl *mc,
577 TpConnectionStatus status,
579 TpConnectionStatusReason reason,
580 const gchar *unique_name,
581 AccountStatusChangedData *data)
583 InvocationData *invocation_data;
585 invocation_data = g_slice_new (InvocationData);
586 invocation_data->status = status;
587 invocation_data->presence = presence;
588 invocation_data->reason = reason;
589 invocation_data->unique_name = g_strdup (unique_name);
590 invocation_data->data = data;
592 g_idle_add_full (G_PRIORITY_HIGH,
593 account_status_changed_invoke_callback,
594 invocation_data, NULL);
598 empathy_connect_to_account_status_changed (MissionControl *mc,
601 GClosureNotify free_func)
603 AccountStatusChangedData *data;
605 g_return_val_if_fail (IS_MISSIONCONTROL (mc), NULL);
606 g_return_val_if_fail (handler != NULL, NULL);
608 data = g_slice_new (AccountStatusChangedData);
609 data->handler = (AccountStatusChangedFunc) handler;
610 data->user_data = user_data;
611 data->free_func = free_func;
612 data->mc = g_object_ref (mc);
614 dbus_g_proxy_connect_signal (DBUS_G_PROXY (mc), "AccountStatusChanged",
615 G_CALLBACK (account_status_changed_cb),
616 data, account_status_changed_data_free);
622 empathy_disconnect_account_status_changed (gpointer token)
624 AccountStatusChangedData *data = token;
626 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (data->mc),
627 "AccountStatusChanged",
628 G_CALLBACK (account_status_changed_cb),
633 empathy_proxy_hash (gconstpointer key)
635 TpProxy *proxy = TP_PROXY (key);
636 TpProxyClass *proxy_class = TP_PROXY_GET_CLASS (key);
638 g_return_val_if_fail (TP_IS_PROXY (proxy), 0);
639 g_return_val_if_fail (proxy_class->must_have_unique_name, 0);
641 return g_str_hash (proxy->object_path) ^ g_str_hash (proxy->bus_name);
645 empathy_proxy_equal (gconstpointer a,
648 TpProxy *proxy_a = TP_PROXY (a);
649 TpProxy *proxy_b = TP_PROXY (b);
650 TpProxyClass *proxy_a_class = TP_PROXY_GET_CLASS (a);
651 TpProxyClass *proxy_b_class = TP_PROXY_GET_CLASS (b);
653 g_return_val_if_fail (TP_IS_PROXY (proxy_a), FALSE);
654 g_return_val_if_fail (TP_IS_PROXY (proxy_b), FALSE);
655 g_return_val_if_fail (proxy_a_class->must_have_unique_name, 0);
656 g_return_val_if_fail (proxy_b_class->must_have_unique_name, 0);
658 return g_str_equal (proxy_a->object_path, proxy_b->object_path) &&
659 g_str_equal (proxy_a->bus_name, proxy_b->bus_name);
666 empathy_connection_callback_for_request_channel callback;
668 GDestroyNotify destroy;
670 gboolean suppress_handler;
672 } ConnectionRequestChannelData;
675 connection_request_channel_data_unref (gpointer user_data)
677 ConnectionRequestChannelData *data = (ConnectionRequestChannelData*) user_data;
679 if (--data->ref_count == 0) {
680 g_free (data->channel_type);
682 data->destroy (data->user_data);
684 g_slice_free (ConnectionRequestChannelData, data);
689 connection_request_channel_cb (TpConnection *connection,
690 const gchar *object_path,
693 GObject *weak_object)
695 ConnectionRequestChannelData *data = (ConnectionRequestChannelData*) user_data;
698 if (!data->callback) {
703 data->callback (connection, NULL, error, data->user_data, weak_object);
707 channel = tp_channel_new (connection, object_path,
712 data->callback (connection, channel, NULL, data->user_data, weak_object);
713 g_object_unref (channel);
717 connection_request_handles_cb (TpConnection *connection,
718 const GArray *handles,
721 GObject *weak_object)
723 ConnectionRequestChannelData *data = (ConnectionRequestChannelData*) user_data;
726 if (data->callback) {
727 data->callback (connection, NULL, error, data->user_data, weak_object);
732 data->handle = g_array_index (handles, guint, 0);
734 tp_cli_connection_call_request_channel (connection, data->timeout_ms,
738 data->suppress_handler,
739 connection_request_channel_cb,
741 (GDestroyNotify) connection_request_channel_data_unref,
746 empathy_connection_request_channel (TpConnection *connection,
748 const gchar *channel_type,
751 gboolean suppress_handler,
752 empathy_connection_callback_for_request_channel callback,
754 GDestroyNotify destroy,
755 GObject *weak_object)
757 const gchar *names[] = {name, NULL};
758 ConnectionRequestChannelData *data;
760 data = g_slice_new (ConnectionRequestChannelData);
761 data->timeout_ms = timeout_ms;
762 data->channel_type = g_strdup (channel_type);
763 data->handle_type = handle_type;
764 data->callback = callback;
765 data->user_data = user_data;
766 data->destroy = destroy;
768 data->suppress_handler = suppress_handler;
770 tp_cli_connection_call_request_handles (connection,
774 connection_request_handles_cb,
776 (GDestroyNotify) connection_request_channel_data_unref,
781 empathy_send_file_from_stream (EmpathyContact *contact,
782 GInputStream *in_stream,
783 const gchar *filename,
788 TpConnection *connection;
793 GError *error = NULL;
794 GValue value = { 0 };
796 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
797 g_return_val_if_fail (G_IS_INPUT_STREAM (in_stream), NULL);
798 g_return_val_if_fail (filename != NULL, NULL);
800 DEBUG ("Sending %s from a stream to %s (size %llu)",
801 filename, empathy_contact_get_name (contact), size);
803 mc = empathy_mission_control_new ();
804 account = empathy_contact_get_account (contact);
805 connection = mission_control_get_tpconnection (mc, account, NULL);
806 tp_connection_run_until_ready (connection, FALSE, NULL, NULL);
807 handle = empathy_contact_get_handle (contact);
809 if (!tp_cli_connection_run_request_channel (connection, -1,
810 EMP_IFACE_CHANNEL_TYPE_FILE,
811 TP_HANDLE_TYPE_CONTACT,
817 DEBUG ("Couldn't request channel: %s",
818 error ? error->message : "No error given");
819 g_clear_error (&error);
821 g_object_unref (connection);
825 channel = tp_channel_new (connection,
827 EMP_IFACE_CHANNEL_TYPE_FILE,
828 TP_HANDLE_TYPE_CONTACT,
832 /* TODO: this should go in NewChannel in the new API */
834 g_value_init (&value, G_TYPE_STRING);
835 g_value_set_string (&value, g_filename_display_basename (filename));
836 tp_cli_dbus_properties_run_set (TP_PROXY (channel),
837 -1, EMP_IFACE_CHANNEL_TYPE_FILE, "Filename",
839 g_value_reset (&value);
841 g_value_set_string (&value, "foobar");
842 tp_cli_dbus_properties_run_set (TP_PROXY (channel),
843 -1, EMP_IFACE_CHANNEL_TYPE_FILE, "ContentMD5",
845 g_value_reset (&value);
847 g_value_set_string (&value, gnome_vfs_get_mime_type_for_name (filename));
848 tp_cli_dbus_properties_run_set (TP_PROXY (channel),
849 -1, EMP_IFACE_CHANNEL_TYPE_FILE, "ContentType",
852 g_value_unset (&value);
854 g_value_init (&value, G_TYPE_UINT64);
855 g_value_set_uint64 (&value, size);
856 tp_cli_dbus_properties_run_set (TP_PROXY (channel),
857 -1, EMP_IFACE_CHANNEL_TYPE_FILE, "Size",
859 g_value_unset (&value);
861 file = empathy_file_new (account, channel);
864 empathy_file_set_input_stream (file, in_stream);
868 g_object_unref (connection);
869 g_object_unref (channel);
870 g_free (object_path);
876 empathy_send_file (EmpathyContact *contact,
882 GInputStream *in_stream = NULL;
885 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
886 g_return_val_if_fail (G_IS_FILE (gfile), NULL);
888 filename = g_file_get_basename (gfile);
889 info = g_file_query_info (gfile, G_FILE_ATTRIBUTE_STANDARD_SIZE, 0, NULL, NULL);
890 size = info ? g_file_info_get_size (info) : EMPATHY_FILE_UNKNOWN_SIZE;
892 DEBUG ("Sending %s to %s",
893 filename, empathy_contact_get_name (contact));
895 in_stream = G_INPUT_STREAM (g_file_read (gfile, NULL, NULL));
896 file = empathy_send_file_from_stream (contact, in_stream, filename, size);
898 g_object_unref (in_stream);
901 g_object_unref (info);