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>
24 * Jonny Lamb <jonny.lamb@collabora.co.uk>
31 #include <sys/types.h>
35 #include <glib/gi18n.h>
37 #include <libxml/uri.h>
38 #include <telepathy-glib/connection.h>
39 #include <telepathy-glib/channel.h>
40 #include <telepathy-glib/dbus.h>
42 #include <extensions/extensions.h>
44 #include "empathy-utils.h"
45 #include "empathy-contact-factory.h"
46 #include "empathy-contact-manager.h"
48 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
49 #include "empathy-debug.h"
51 static void regex_init (void);
54 empathy_substring (const gchar *str,
58 return g_strndup (str + start, end - start);
62 * Regular Expression code to match urls.
64 #define APTCHARS "-A-Za-z0-9,-."
65 #define USERCHARS "-A-Za-z0-9"
66 #define PASSCHARS "-A-Za-z0-9,?;.:/!%$^*&~\"#'"
67 #define HOSTCHARS "-A-Za-z0-9_"
68 #define PATHCHARS "-A-Za-z0-9_$.+!*(),;:@&=?/~#%"
69 #define SCHEME "(news:|telnet:|nntp:|file:/|https?:|ftps?:|webcal:)"
70 #define USER "[" USERCHARS "]+(:["PASSCHARS "]+)?"
71 #define URLPATH "/[" PATHCHARS "]*[^]'.}>) \t\r\n,\\\"]"
73 static regex_t dingus[EMPATHY_REGEX_ALL];
78 static gboolean inited = FALSE;
79 const gchar *expression;
86 for (i = 0; i < EMPATHY_REGEX_ALL; i++) {
88 case EMPATHY_REGEX_AS_IS:
90 SCHEME "//(" USER "@)?[" HOSTCHARS ".]+"
91 "(:[0-9]+)?(" URLPATH ")?";
93 case EMPATHY_REGEX_BROWSER:
95 "(www|ftp)[" HOSTCHARS "]*\\.[" HOSTCHARS ".]+"
96 "(:[0-9]+)?(" URLPATH ")?";
98 case EMPATHY_REGEX_APT:
100 "apt://[" APTCHARS "]*";
102 case EMPATHY_REGEX_EMAIL:
104 "(mailto:)?[a-z0-9][a-z0-9._-]*@[a-z0-9]"
105 "[a-z0-9-]*(\\.[a-z0-9][a-z0-9-]*)+";
107 case EMPATHY_REGEX_OTHER:
109 "news:[-A-Z\\^_a-z{|}~!\"#$%&'()*+,./0-9;:=?`]+"
110 "@[" HOSTCHARS ".]+(:[0-9]+)?";
113 /* Silence the compiler. */
118 memset (&dingus[i], 0, sizeof (regex_t));
119 regcomp (&dingus[i], expression, REG_EXTENDED | REG_ICASE);
126 empathy_regex_match (EmpathyRegExType type,
131 regmatch_t matches[1];
133 gint num_matches = 0;
137 g_return_val_if_fail (type >= 0 || type <= EMPATHY_REGEX_ALL, 0);
141 while (!ret && type != EMPATHY_REGEX_ALL) {
142 ret = regexec (&dingus[type], msg + offset, 1, matches, 0);
148 s = matches[0].rm_so + offset;
149 offset = matches[0].rm_eo + offset;
151 g_array_append_val (start, s);
152 g_array_append_val (end, offset);
156 if (type != EMPATHY_REGEX_ALL) {
157 DEBUG ("Found %d matches for regex type:%d", num_matches, type);
161 /* If EMPATHY_REGEX_ALL then we run ALL regex's on the string. */
162 for (i = 0; i < EMPATHY_REGEX_ALL; i++, ret = 0) {
164 ret = regexec (&dingus[i], msg + offset, 1, matches, 0);
170 s = matches[0].rm_so + offset;
171 offset = matches[0].rm_eo + offset;
173 g_array_append_val (start, s);
174 g_array_append_val (end, offset);
179 DEBUG ("Found %d matches for ALL regex types", num_matches);
185 empathy_strcasecmp (const gchar *s1,
188 return empathy_strncasecmp (s1, s2, -1);
192 empathy_strncasecmp (const gchar *s1,
199 u1 = g_utf8_casefold (s1, n);
200 u2 = g_utf8_casefold (s2, n);
202 ret_val = g_utf8_collate (u1, u2);
210 empathy_xml_validate (xmlDoc *doc,
211 const gchar *dtd_filename)
213 gchar *path, *escaped;
218 path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "libempathy",
220 if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
222 path = g_build_filename (DATADIR, "empathy", dtd_filename, NULL);
224 DEBUG ("Loading dtd file %s", path);
226 /* The list of valid chars is taken from libxml. */
227 escaped = xmlURIEscapeStr (path, ":@&=+$,/?;");
230 memset (&cvp, 0, sizeof (cvp));
231 dtd = xmlParseDTD (NULL, escaped);
232 ret = xmlValidateDtd (&cvp, doc, dtd);
241 empathy_xml_node_get_child (xmlNodePtr node,
242 const gchar *child_name)
246 g_return_val_if_fail (node != NULL, NULL);
247 g_return_val_if_fail (child_name != NULL, NULL);
249 for (l = node->children; l; l = l->next) {
250 if (l->name && strcmp (l->name, child_name) == 0) {
259 empathy_xml_node_get_child_content (xmlNodePtr node,
260 const gchar *child_name)
264 g_return_val_if_fail (node != NULL, NULL);
265 g_return_val_if_fail (child_name != NULL, NULL);
267 l = empathy_xml_node_get_child (node, child_name);
269 return xmlNodeGetContent (l);
276 empathy_xml_node_find_child_prop_value (xmlNodePtr node,
277 const gchar *prop_name,
278 const gchar *prop_value)
281 xmlNodePtr found = NULL;
283 g_return_val_if_fail (node != NULL, NULL);
284 g_return_val_if_fail (prop_name != NULL, NULL);
285 g_return_val_if_fail (prop_value != NULL, NULL);
287 for (l = node->children; l && !found; l = l->next) {
290 if (!xmlHasProp (l, prop_name)) {
294 prop = xmlGetProp (l, prop_name);
295 if (prop && strcmp (prop, prop_value) == 0) {
306 empathy_account_hash (gconstpointer key)
308 g_return_val_if_fail (MC_IS_ACCOUNT (key), 0);
310 return g_str_hash (mc_account_get_unique_name (MC_ACCOUNT (key)));
314 empathy_account_equal (gconstpointer a,
320 g_return_val_if_fail (MC_IS_ACCOUNT (a), FALSE);
321 g_return_val_if_fail (MC_IS_ACCOUNT (b), FALSE);
323 name_a = mc_account_get_unique_name (MC_ACCOUNT (a));
324 name_b = mc_account_get_unique_name (MC_ACCOUNT (b));
326 return g_str_equal (name_a, name_b);
330 empathy_mission_control_new (void)
332 static MissionControl *mc = NULL;
335 mc = mission_control_new (tp_get_bus ());
336 g_object_add_weak_pointer (G_OBJECT (mc), (gpointer) &mc);
345 empathy_presence_get_default_message (McPresence presence)
348 case MC_PRESENCE_AVAILABLE:
349 return _("Available");
350 case MC_PRESENCE_DO_NOT_DISTURB:
352 case MC_PRESENCE_AWAY:
353 case MC_PRESENCE_EXTENDED_AWAY:
355 case MC_PRESENCE_HIDDEN:
357 case MC_PRESENCE_OFFLINE:
358 case MC_PRESENCE_UNSET:
361 g_assert_not_reached ();
368 empathy_presence_to_str (McPresence presence)
371 case MC_PRESENCE_AVAILABLE:
373 case MC_PRESENCE_DO_NOT_DISTURB:
375 case MC_PRESENCE_AWAY:
377 case MC_PRESENCE_EXTENDED_AWAY:
379 case MC_PRESENCE_HIDDEN:
381 case MC_PRESENCE_OFFLINE:
383 case MC_PRESENCE_UNSET:
386 g_assert_not_reached ();
393 empathy_presence_from_str (const gchar *str)
395 if (strcmp (str, "available") == 0) {
396 return MC_PRESENCE_AVAILABLE;
397 } else if ((strcmp (str, "dnd") == 0) || (strcmp (str, "busy") == 0)) {
398 return MC_PRESENCE_DO_NOT_DISTURB;
399 } else if ((strcmp (str, "away") == 0) || (strcmp (str, "brb") == 0)) {
400 return MC_PRESENCE_AWAY;
401 } else if ((strcmp (str, "xa") == 0) || (strcmp (str, "ext_away") == 0)) {
402 return MC_PRESENCE_EXTENDED_AWAY;
403 } else if (strcmp (str, "hidden") == 0) {
404 return MC_PRESENCE_HIDDEN;
405 } else if (strcmp (str, "offline") == 0) {
406 return MC_PRESENCE_OFFLINE;
407 } else if (strcmp (str, "unset") == 0) {
408 return MC_PRESENCE_UNSET;
411 return MC_PRESENCE_UNSET;
415 empathy_file_lookup (const gchar *filename, const gchar *subdir)
423 path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), subdir, filename, NULL);
424 if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
426 path = g_build_filename (DATADIR, "empathy", filename, NULL);
433 EmpathyRunUntilReadyFunc func;
440 run_until_ready_cb (RunUntilReadyData *data)
442 if (!data->func || data->func (data->object, data->user_data)) {
443 DEBUG ("Object %p is ready", data->object);
444 g_main_loop_quit (data->loop);
449 object_is_ready (GObject *object,
454 g_object_get (object, "ready", &ready, NULL);
460 empathy_run_until_ready_full (gpointer object,
462 EmpathyRunUntilReadyFunc func,
466 RunUntilReadyData data;
469 g_return_if_fail (G_IS_OBJECT (object));
470 g_return_if_fail (signal != NULL);
472 if (func && func (object, user_data)) {
476 DEBUG ("Starting run until ready for object %p", object);
479 data.user_data = user_data;
480 data.object = object;
481 data.loop = g_main_loop_new (NULL, FALSE);
483 signal_id = g_signal_connect_swapped (object, signal,
484 G_CALLBACK (run_until_ready_cb),
490 g_main_loop_run (data.loop);
496 g_signal_handler_disconnect (object, signal_id);
497 g_main_loop_unref (data.loop);
501 empathy_run_until_ready (gpointer object)
503 empathy_run_until_ready_full (object, "notify::ready", object_is_ready,
508 empathy_channel_get_account (TpChannel *channel)
510 TpConnection *connection;
514 g_object_get (channel, "connection", &connection, NULL);
515 mc = empathy_mission_control_new ();
516 account = mission_control_get_account_for_tpconnection (mc, connection, NULL);
517 g_object_unref (connection);
523 typedef void (*AccountStatusChangedFunc) (MissionControl *mc,
524 TpConnectionStatus status,
526 TpConnectionStatusReason reason,
527 const gchar *unique_name,
531 AccountStatusChangedFunc handler;
533 GClosureNotify free_func;
535 } AccountStatusChangedData;
538 TpConnectionStatus status;
540 TpConnectionStatusReason reason;
542 AccountStatusChangedData *data;
546 account_status_changed_data_free (gpointer ptr,
549 AccountStatusChangedData *data = ptr;
551 if (data->free_func) {
552 data->free_func (data->user_data, closure);
554 g_object_unref (data->mc);
555 g_slice_free (AccountStatusChangedData, data);
559 account_status_changed_invoke_callback (gpointer data)
561 InvocationData *invocation_data = data;
563 invocation_data->data->handler (invocation_data->data->mc,
564 invocation_data->status,
565 invocation_data->presence,
566 invocation_data->reason,
567 invocation_data->unique_name,
568 invocation_data->data->user_data);
570 g_free (invocation_data->unique_name);
571 g_slice_free (InvocationData, invocation_data);
577 account_status_changed_cb (MissionControl *mc,
578 TpConnectionStatus status,
580 TpConnectionStatusReason reason,
581 const gchar *unique_name,
582 AccountStatusChangedData *data)
584 InvocationData *invocation_data;
586 invocation_data = g_slice_new (InvocationData);
587 invocation_data->status = status;
588 invocation_data->presence = presence;
589 invocation_data->reason = reason;
590 invocation_data->unique_name = g_strdup (unique_name);
591 invocation_data->data = data;
593 g_idle_add_full (G_PRIORITY_HIGH,
594 account_status_changed_invoke_callback,
595 invocation_data, NULL);
599 empathy_connect_to_account_status_changed (MissionControl *mc,
602 GClosureNotify free_func)
604 AccountStatusChangedData *data;
606 g_return_val_if_fail (IS_MISSIONCONTROL (mc), NULL);
607 g_return_val_if_fail (handler != NULL, NULL);
609 data = g_slice_new (AccountStatusChangedData);
610 data->handler = (AccountStatusChangedFunc) handler;
611 data->user_data = user_data;
612 data->free_func = free_func;
613 data->mc = g_object_ref (mc);
615 dbus_g_proxy_connect_signal (DBUS_G_PROXY (mc), "AccountStatusChanged",
616 G_CALLBACK (account_status_changed_cb),
617 data, account_status_changed_data_free);
623 empathy_disconnect_account_status_changed (gpointer token)
625 AccountStatusChangedData *data = token;
627 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (data->mc),
628 "AccountStatusChanged",
629 G_CALLBACK (account_status_changed_cb),
634 empathy_proxy_hash (gconstpointer key)
636 TpProxy *proxy = TP_PROXY (key);
637 TpProxyClass *proxy_class = TP_PROXY_GET_CLASS (key);
639 g_return_val_if_fail (TP_IS_PROXY (proxy), 0);
640 g_return_val_if_fail (proxy_class->must_have_unique_name, 0);
642 return g_str_hash (proxy->object_path) ^ g_str_hash (proxy->bus_name);
646 empathy_proxy_equal (gconstpointer a,
649 TpProxy *proxy_a = TP_PROXY (a);
650 TpProxy *proxy_b = TP_PROXY (b);
651 TpProxyClass *proxy_a_class = TP_PROXY_GET_CLASS (a);
652 TpProxyClass *proxy_b_class = TP_PROXY_GET_CLASS (b);
654 g_return_val_if_fail (TP_IS_PROXY (proxy_a), FALSE);
655 g_return_val_if_fail (TP_IS_PROXY (proxy_b), FALSE);
656 g_return_val_if_fail (proxy_a_class->must_have_unique_name, 0);
657 g_return_val_if_fail (proxy_b_class->must_have_unique_name, 0);
659 return g_str_equal (proxy_a->object_path, proxy_b->object_path) &&
660 g_str_equal (proxy_a->bus_name, proxy_b->bus_name);
667 empathy_connection_callback_for_request_channel callback;
669 GDestroyNotify destroy;
671 gboolean suppress_handler;
673 } ConnectionRequestChannelData;
676 connection_request_channel_data_unref (gpointer user_data)
678 ConnectionRequestChannelData *data = (ConnectionRequestChannelData*) user_data;
680 if (--data->ref_count == 0) {
681 g_free (data->channel_type);
683 data->destroy (data->user_data);
685 g_slice_free (ConnectionRequestChannelData, data);
690 connection_request_channel_cb (TpConnection *connection,
691 const gchar *object_path,
694 GObject *weak_object)
696 ConnectionRequestChannelData *data = (ConnectionRequestChannelData*) user_data;
699 if (!data->callback) {
704 data->callback (connection, NULL, error, data->user_data, weak_object);
708 channel = tp_channel_new (connection, object_path,
713 data->callback (connection, channel, NULL, data->user_data, weak_object);
714 g_object_unref (channel);
718 connection_request_handles_cb (TpConnection *connection,
719 const GArray *handles,
722 GObject *weak_object)
724 ConnectionRequestChannelData *data = (ConnectionRequestChannelData*) user_data;
727 if (data->callback) {
728 data->callback (connection, NULL, error, data->user_data, weak_object);
733 data->handle = g_array_index (handles, guint, 0);
735 tp_cli_connection_call_request_channel (connection, data->timeout_ms,
739 data->suppress_handler,
740 connection_request_channel_cb,
742 (GDestroyNotify) connection_request_channel_data_unref,
747 empathy_connection_request_channel (TpConnection *connection,
749 const gchar *channel_type,
752 gboolean suppress_handler,
753 empathy_connection_callback_for_request_channel callback,
755 GDestroyNotify destroy,
756 GObject *weak_object)
758 const gchar *names[] = {name, NULL};
759 ConnectionRequestChannelData *data;
761 data = g_slice_new (ConnectionRequestChannelData);
762 data->timeout_ms = timeout_ms;
763 data->channel_type = g_strdup (channel_type);
764 data->handle_type = handle_type;
765 data->callback = callback;
766 data->user_data = user_data;
767 data->destroy = destroy;
769 data->suppress_handler = suppress_handler;
771 tp_cli_connection_call_request_handles (connection,
775 connection_request_handles_cb,
777 (GDestroyNotify) connection_request_channel_data_unref,
782 empathy_send_file (EmpathyContact *contact,
787 GInputStream *in_stream = NULL;
790 TpConnection *connection;
794 EmpathyTpFile *tp_file;
795 GError *error = NULL;
796 GValue value = { 0 };
799 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
800 g_return_val_if_fail (G_IS_FILE (gfile), NULL);
802 info = g_file_query_info (gfile,
803 G_FILE_ATTRIBUTE_STANDARD_SIZE ","
804 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
806 size = info ? g_file_info_get_size (info) : EMPATHY_TP_FILE_UNKNOWN_SIZE;
807 filename = g_file_get_basename (gfile);
808 in_stream = G_INPUT_STREAM (g_file_read (gfile, NULL, NULL));
809 mc = empathy_mission_control_new ();
810 account = empathy_contact_get_account (contact);
811 connection = mission_control_get_tpconnection (mc, account, NULL);
812 tp_connection_run_until_ready (connection, FALSE, NULL, NULL);
813 handle = empathy_contact_get_handle (contact);
815 DEBUG ("Sending %s from a stream to %s (size %llu, content-type %s)",
816 filename, empathy_contact_get_name (contact), size,
817 g_file_info_get_content_type (info));
819 if (!tp_cli_connection_run_request_channel (connection, -1,
820 EMP_IFACE_CHANNEL_TYPE_FILE,
821 TP_HANDLE_TYPE_CONTACT,
827 DEBUG ("Couldn't request channel: %s",
828 error ? error->message : "No error given");
829 g_clear_error (&error);
831 g_object_unref (connection);
835 channel = tp_channel_new (connection,
837 EMP_IFACE_CHANNEL_TYPE_FILE,
838 TP_HANDLE_TYPE_CONTACT,
842 /* FIXME: this should go in CreateChannel in the new requests API */
844 g_value_init (&value, G_TYPE_STRING);
845 g_value_set_string (&value, g_filename_display_basename (filename));
846 tp_cli_dbus_properties_call_set (TP_PROXY (channel), -1,
847 EMP_IFACE_CHANNEL_TYPE_FILE, "Filename",
848 &value, NULL, NULL, NULL, NULL);
849 g_value_reset (&value);
851 g_value_set_string (&value, g_file_info_get_content_type (info));
852 tp_cli_dbus_properties_call_set (TP_PROXY (channel), -1,
853 EMP_IFACE_CHANNEL_TYPE_FILE, "ContentType",
854 &value, NULL, NULL, NULL, NULL);
856 g_value_unset (&value);
858 g_value_init (&value, G_TYPE_UINT64);
859 g_value_set_uint64 (&value, size);
860 tp_cli_dbus_properties_call_set (TP_PROXY (channel), -1,
861 EMP_IFACE_CHANNEL_TYPE_FILE, "Size",
862 &value, NULL, NULL, NULL, NULL);
863 g_value_unset (&value);
865 tp_file = empathy_tp_file_new (account, channel);
868 empathy_tp_file_set_input_stream (tp_file, in_stream);
871 empathy_tp_file_offer (tp_file);
874 g_object_unref (connection);
875 g_object_unref (channel);
876 g_free (object_path);