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 mc = empathy_mission_control_new ();
357 account = empathy_contact_get_account (contact);
358 connection = mission_control_get_tpconnection (mc, account, NULL);
359 tp_connection_run_until_ready (connection, FALSE, NULL, NULL);
362 /* We abuse of suppress_handler, TRUE means OUTGOING. The channel
363 * will be catched in EmpathyFilter */
364 if (!tp_cli_connection_run_request_channel (connection, -1,
365 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
372 empathy_debug (DEBUG_DOMAIN,
373 "Couldn't request channel: %s",
374 error ? error->message : "No error given");
375 g_clear_error (&error);
376 g_object_unref (connection);
380 channel = tp_channel_new (connection,
381 object_path, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
382 TP_HANDLE_TYPE_NONE, 0, NULL);
384 group = empathy_tp_group_new (channel);
385 empathy_run_until_ready (group);
387 factory = empathy_contact_factory_new ();
388 self_contact = empathy_contact_factory_get_user (factory, account);
389 empathy_contact_run_until_ready (self_contact,
390 EMPATHY_CONTACT_READY_HANDLE,
393 empathy_tp_group_add_member (group, contact, "");
394 empathy_tp_group_add_member (group, self_contact, "");
396 g_object_unref (factory);
397 g_object_unref (self_contact);
398 g_object_unref (group);
399 g_object_unref (connection);
400 g_object_unref (channel);
401 g_free (object_path);
406 empathy_call_with_contact_id (McAccount *account, const gchar *contact_id)
409 EmpathyContactFactory *factory;
410 EmpathyContact *contact;
412 factory = empathy_contact_factory_new ();
413 contact = empathy_contact_factory_get_from_id (factory, account, contact_id);
414 empathy_contact_run_until_ready (contact, EMPATHY_CONTACT_READY_HANDLE, NULL);
416 empathy_call_with_contact (contact);
418 g_object_unref (contact);
419 g_object_unref (factory);
424 empathy_chat_with_contact (EmpathyContact *contact)
428 TpConnection *connection;
430 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
432 mc = empathy_mission_control_new ();
433 account = empathy_contact_get_account (contact);
434 connection = mission_control_get_tpconnection (mc, account, NULL);
435 tp_connection_run_until_ready (connection, FALSE, NULL, NULL);
438 /* We abuse of suppress_handler, TRUE means OUTGOING. The channel
439 * will be catched in EmpathyFilter */
440 tp_cli_connection_call_request_channel (connection, -1,
441 TP_IFACE_CHANNEL_TYPE_TEXT,
442 TP_HANDLE_TYPE_CONTACT,
443 empathy_contact_get_handle (contact),
445 NULL, NULL, NULL, NULL);
446 g_object_unref (connection);
450 empathy_chat_with_contact_id (McAccount *account, const gchar *contact_id)
452 EmpathyContactFactory *factory;
453 EmpathyContact *contact;
455 factory = empathy_contact_factory_new ();
456 contact = empathy_contact_factory_get_from_id (factory, account, contact_id);
457 empathy_contact_run_until_ready (contact, EMPATHY_CONTACT_READY_HANDLE, NULL);
459 empathy_chat_with_contact (contact);
461 g_object_unref (contact);
462 g_object_unref (factory);
466 empathy_presence_get_default_message (McPresence presence)
469 case MC_PRESENCE_AVAILABLE:
470 return _("Available");
471 case MC_PRESENCE_DO_NOT_DISTURB:
473 case MC_PRESENCE_AWAY:
474 case MC_PRESENCE_EXTENDED_AWAY:
476 case MC_PRESENCE_HIDDEN:
478 case MC_PRESENCE_OFFLINE:
479 case MC_PRESENCE_UNSET:
482 g_assert_not_reached ();
489 empathy_presence_to_str (McPresence presence)
492 case MC_PRESENCE_AVAILABLE:
494 case MC_PRESENCE_DO_NOT_DISTURB:
496 case MC_PRESENCE_AWAY:
498 case MC_PRESENCE_EXTENDED_AWAY:
500 case MC_PRESENCE_HIDDEN:
502 case MC_PRESENCE_OFFLINE:
504 case MC_PRESENCE_UNSET:
507 g_assert_not_reached ();
514 empathy_presence_from_str (const gchar *str)
516 if (strcmp (str, "available") == 0) {
517 return MC_PRESENCE_AVAILABLE;
518 } else if ((strcmp (str, "dnd") == 0) || (strcmp (str, "busy") == 0)) {
519 return MC_PRESENCE_DO_NOT_DISTURB;
520 } else if ((strcmp (str, "away") == 0) || (strcmp (str, "brb") == 0)) {
521 return MC_PRESENCE_AWAY;
522 } else if ((strcmp (str, "xa") == 0) || (strcmp (str, "ext_away") == 0)) {
523 return MC_PRESENCE_EXTENDED_AWAY;
524 } else if (strcmp (str, "hidden") == 0) {
525 return MC_PRESENCE_HIDDEN;
526 } else if (strcmp (str, "offline") == 0) {
527 return MC_PRESENCE_OFFLINE;
528 } else if (strcmp (str, "unset") == 0) {
529 return MC_PRESENCE_UNSET;
532 return MC_PRESENCE_AVAILABLE;
536 empathy_file_lookup (const gchar *filename, const gchar *subdir)
544 path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), subdir, filename, NULL);
545 if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
547 path = g_build_filename (DATADIR, "empathy", filename, NULL);
554 EmpathyRunUntilReadyFunc func;
561 run_until_ready_cb (RunUntilReadyData *data)
563 if (!data->func || data->func (data->object, data->user_data)) {
564 empathy_debug (DEBUG_DOMAIN, "Object %p is ready", data->object);
565 g_main_loop_quit (data->loop);
570 object_is_ready (GObject *object,
575 g_object_get (object, "ready", &ready, NULL);
581 empathy_run_until_ready_full (gpointer object,
583 EmpathyRunUntilReadyFunc func,
587 RunUntilReadyData data;
590 g_return_if_fail (G_IS_OBJECT (object));
591 g_return_if_fail (signal != NULL);
593 if (func && func (object, user_data)) {
597 empathy_debug (DEBUG_DOMAIN, "Starting run until ready for object %p",
601 data.user_data = user_data;
602 data.object = object;
603 data.loop = g_main_loop_new (NULL, FALSE);
605 signal_id = g_signal_connect_swapped (object, signal,
606 G_CALLBACK (run_until_ready_cb),
612 g_main_loop_run (data.loop);
618 g_signal_handler_disconnect (object, signal_id);
619 g_main_loop_unref (data.loop);
623 empathy_run_until_ready (gpointer object)
625 empathy_run_until_ready_full (object, "notify::ready", object_is_ready,
630 empathy_channel_get_account (TpChannel *channel)
632 TpConnection *connection;
636 g_object_get (channel, "connection", &connection, NULL);
637 mc = empathy_mission_control_new ();
638 account = mission_control_get_account_for_tpconnection (mc, connection, NULL);
639 g_object_unref (connection);
645 typedef void (*AccountStatusChangedFunc) (MissionControl *mc,
646 TpConnectionStatus status,
648 TpConnectionStatusReason reason,
649 const gchar *unique_name,
653 AccountStatusChangedFunc handler;
655 GClosureNotify free_func;
657 } AccountStatusChangedData;
660 TpConnectionStatus status;
662 TpConnectionStatusReason reason;
664 AccountStatusChangedData *data;
668 account_status_changed_data_free (gpointer ptr,
671 AccountStatusChangedData *data = ptr;
673 if (data->free_func) {
674 data->free_func (data->user_data, closure);
676 g_object_unref (data->mc);
677 g_slice_free (AccountStatusChangedData, data);
681 account_status_changed_invoke_callback (gpointer data)
683 InvocationData *invocation_data = data;
685 invocation_data->data->handler (invocation_data->data->mc,
686 invocation_data->status,
687 invocation_data->presence,
688 invocation_data->reason,
689 invocation_data->unique_name,
690 invocation_data->data->user_data);
692 g_free (invocation_data->unique_name);
693 g_slice_free (InvocationData, invocation_data);
699 account_status_changed_cb (MissionControl *mc,
700 TpConnectionStatus status,
702 TpConnectionStatusReason reason,
703 const gchar *unique_name,
704 AccountStatusChangedData *data)
706 InvocationData *invocation_data;
708 invocation_data = g_slice_new (InvocationData);
709 invocation_data->status = status;
710 invocation_data->presence = presence;
711 invocation_data->reason = reason;
712 invocation_data->unique_name = g_strdup (unique_name);
713 invocation_data->data = data;
715 g_idle_add_full (G_PRIORITY_HIGH,
716 account_status_changed_invoke_callback,
717 invocation_data, NULL);
721 empathy_connect_to_account_status_changed (MissionControl *mc,
724 GClosureNotify free_func)
726 AccountStatusChangedData *data;
728 g_return_val_if_fail (IS_MISSIONCONTROL (mc), NULL);
729 g_return_val_if_fail (handler != NULL, NULL);
731 data = g_slice_new (AccountStatusChangedData);
732 data->handler = (AccountStatusChangedFunc) handler;
733 data->user_data = user_data;
734 data->free_func = free_func;
735 data->mc = g_object_ref (mc);
737 dbus_g_proxy_connect_signal (DBUS_G_PROXY (mc), "AccountStatusChanged",
738 G_CALLBACK (account_status_changed_cb),
739 data, account_status_changed_data_free);
745 empathy_disconnect_account_status_changed (gpointer token)
747 AccountStatusChangedData *data = token;
749 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (data->mc),
750 "AccountStatusChanged",
751 G_CALLBACK (account_status_changed_cb),