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"
43 #include "empathy-tp-group.h"
45 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
46 #include "empathy-debug.h"
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 APTCHARS "-A-Za-z0-9,-."
62 #define USERCHARS "-A-Za-z0-9"
63 #define PASSCHARS "-A-Za-z0-9,?;.:/!%$^*&~\"#'"
64 #define HOSTCHARS "-A-Za-z0-9_"
65 #define PATHCHARS "-A-Za-z0-9_$.+!*(),;:@&=?/~#%"
66 #define SCHEME "(news:|telnet:|nntp:|file:/|https?:|ftps?:|webcal:)"
67 #define USER "[" USERCHARS "]+(:["PASSCHARS "]+)?"
68 #define URLPATH "/[" PATHCHARS "]*[^]'.}>) \t\r\n,\\\"]"
70 static regex_t dingus[EMPATHY_REGEX_ALL];
75 static gboolean inited = FALSE;
76 const gchar *expression;
83 for (i = 0; i < EMPATHY_REGEX_ALL; i++) {
85 case EMPATHY_REGEX_AS_IS:
87 SCHEME "//(" USER "@)?[" HOSTCHARS ".]+"
88 "(:[0-9]+)?(" URLPATH ")?";
90 case EMPATHY_REGEX_BROWSER:
92 "(www|ftp)[" HOSTCHARS "]*\\.[" HOSTCHARS ".]+"
93 "(:[0-9]+)?(" URLPATH ")?";
95 case EMPATHY_REGEX_APT:
97 "apt://[" APTCHARS "]*";
99 case EMPATHY_REGEX_EMAIL:
101 "(mailto:)?[a-z0-9][a-z0-9.-]*@[a-z0-9]"
102 "[a-z0-9-]*(\\.[a-z0-9][a-z0-9-]*)+";
104 case EMPATHY_REGEX_OTHER:
106 "news:[-A-Z\\^_a-z{|}~!\"#$%&'()*+,./0-9;:=?`]+"
107 "@[" HOSTCHARS ".]+(:[0-9]+)?";
110 /* Silence the compiler. */
115 memset (&dingus[i], 0, sizeof (regex_t));
116 regcomp (&dingus[i], expression, REG_EXTENDED | REG_ICASE);
123 empathy_regex_match (EmpathyRegExType type,
128 regmatch_t matches[1];
130 gint num_matches = 0;
134 g_return_val_if_fail (type >= 0 || type <= EMPATHY_REGEX_ALL, 0);
138 while (!ret && type != EMPATHY_REGEX_ALL) {
139 ret = regexec (&dingus[type], msg + offset, 1, matches, 0);
145 s = matches[0].rm_so + offset;
146 offset = matches[0].rm_eo + offset;
148 g_array_append_val (start, s);
149 g_array_append_val (end, offset);
153 if (type != EMPATHY_REGEX_ALL) {
154 DEBUG ("Found %d matches for regex type:%d", num_matches, type);
158 /* If EMPATHY_REGEX_ALL then we run ALL regex's on the string. */
159 for (i = 0; i < EMPATHY_REGEX_ALL; i++, ret = 0) {
161 ret = regexec (&dingus[i], msg + offset, 1, matches, 0);
167 s = matches[0].rm_so + offset;
168 offset = matches[0].rm_eo + offset;
170 g_array_append_val (start, s);
171 g_array_append_val (end, offset);
176 DEBUG ("Found %d matches for ALL regex types", num_matches);
182 empathy_strcasecmp (const gchar *s1,
185 return empathy_strncasecmp (s1, s2, -1);
189 empathy_strncasecmp (const gchar *s1,
196 u1 = g_utf8_casefold (s1, n);
197 u2 = g_utf8_casefold (s2, n);
199 ret_val = g_utf8_collate (u1, u2);
207 empathy_xml_validate (xmlDoc *doc,
208 const gchar *dtd_filename)
210 gchar *path, *escaped;
215 path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "libempathy",
217 if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
219 path = g_build_filename (DATADIR, "empathy", dtd_filename, NULL);
221 DEBUG ("Loading dtd file %s", path);
223 /* The list of valid chars is taken from libxml. */
224 escaped = xmlURIEscapeStr (path, ":@&=+$,/?;");
227 memset (&cvp, 0, sizeof (cvp));
228 dtd = xmlParseDTD (NULL, escaped);
229 ret = xmlValidateDtd (&cvp, doc, dtd);
238 empathy_xml_node_get_child (xmlNodePtr node,
239 const gchar *child_name)
243 g_return_val_if_fail (node != NULL, NULL);
244 g_return_val_if_fail (child_name != NULL, NULL);
246 for (l = node->children; l; l = l->next) {
247 if (l->name && strcmp (l->name, child_name) == 0) {
256 empathy_xml_node_get_child_content (xmlNodePtr node,
257 const gchar *child_name)
261 g_return_val_if_fail (node != NULL, NULL);
262 g_return_val_if_fail (child_name != NULL, NULL);
264 l = empathy_xml_node_get_child (node, child_name);
266 return xmlNodeGetContent (l);
273 empathy_xml_node_find_child_prop_value (xmlNodePtr node,
274 const gchar *prop_name,
275 const gchar *prop_value)
278 xmlNodePtr found = NULL;
280 g_return_val_if_fail (node != NULL, NULL);
281 g_return_val_if_fail (prop_name != NULL, NULL);
282 g_return_val_if_fail (prop_value != NULL, NULL);
284 for (l = node->children; l && !found; l = l->next) {
287 if (!xmlHasProp (l, prop_name)) {
291 prop = xmlGetProp (l, prop_name);
292 if (prop && strcmp (prop, prop_value) == 0) {
303 empathy_account_hash (gconstpointer key)
305 g_return_val_if_fail (MC_IS_ACCOUNT (key), 0);
307 return g_str_hash (mc_account_get_unique_name (MC_ACCOUNT (key)));
311 empathy_account_equal (gconstpointer a,
317 g_return_val_if_fail (MC_IS_ACCOUNT (a), FALSE);
318 g_return_val_if_fail (MC_IS_ACCOUNT (b), FALSE);
320 name_a = mc_account_get_unique_name (MC_ACCOUNT (a));
321 name_b = mc_account_get_unique_name (MC_ACCOUNT (b));
323 return g_str_equal (name_a, name_b);
327 empathy_mission_control_new (void)
329 static MissionControl *mc = NULL;
332 mc = mission_control_new (tp_get_bus ());
333 g_object_add_weak_pointer (G_OBJECT (mc), (gpointer) &mc);
342 empathy_presence_get_default_message (McPresence presence)
345 case MC_PRESENCE_AVAILABLE:
346 return _("Available");
347 case MC_PRESENCE_DO_NOT_DISTURB:
349 case MC_PRESENCE_AWAY:
350 case MC_PRESENCE_EXTENDED_AWAY:
352 case MC_PRESENCE_HIDDEN:
354 case MC_PRESENCE_OFFLINE:
355 case MC_PRESENCE_UNSET:
358 g_assert_not_reached ();
365 empathy_presence_to_str (McPresence presence)
368 case MC_PRESENCE_AVAILABLE:
370 case MC_PRESENCE_DO_NOT_DISTURB:
372 case MC_PRESENCE_AWAY:
374 case MC_PRESENCE_EXTENDED_AWAY:
376 case MC_PRESENCE_HIDDEN:
378 case MC_PRESENCE_OFFLINE:
380 case MC_PRESENCE_UNSET:
383 g_assert_not_reached ();
390 empathy_presence_from_str (const gchar *str)
392 if (strcmp (str, "available") == 0) {
393 return MC_PRESENCE_AVAILABLE;
394 } else if ((strcmp (str, "dnd") == 0) || (strcmp (str, "busy") == 0)) {
395 return MC_PRESENCE_DO_NOT_DISTURB;
396 } else if ((strcmp (str, "away") == 0) || (strcmp (str, "brb") == 0)) {
397 return MC_PRESENCE_AWAY;
398 } else if ((strcmp (str, "xa") == 0) || (strcmp (str, "ext_away") == 0)) {
399 return MC_PRESENCE_EXTENDED_AWAY;
400 } else if (strcmp (str, "hidden") == 0) {
401 return MC_PRESENCE_HIDDEN;
402 } else if (strcmp (str, "offline") == 0) {
403 return MC_PRESENCE_OFFLINE;
404 } else if (strcmp (str, "unset") == 0) {
405 return MC_PRESENCE_UNSET;
408 return MC_PRESENCE_AVAILABLE;
412 empathy_file_lookup (const gchar *filename, const gchar *subdir)
420 path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), subdir, filename, NULL);
421 if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
423 path = g_build_filename (DATADIR, "empathy", filename, NULL);
430 EmpathyRunUntilReadyFunc func;
437 run_until_ready_cb (RunUntilReadyData *data)
439 if (!data->func || data->func (data->object, data->user_data)) {
440 DEBUG ("Object %p is ready", data->object);
441 g_main_loop_quit (data->loop);
446 object_is_ready (GObject *object,
451 g_object_get (object, "ready", &ready, NULL);
457 empathy_run_until_ready_full (gpointer object,
459 EmpathyRunUntilReadyFunc func,
463 RunUntilReadyData data;
466 g_return_if_fail (G_IS_OBJECT (object));
467 g_return_if_fail (signal != NULL);
469 if (func && func (object, user_data)) {
473 DEBUG ("Starting run until ready for object %p", object);
476 data.user_data = user_data;
477 data.object = object;
478 data.loop = g_main_loop_new (NULL, FALSE);
480 signal_id = g_signal_connect_swapped (object, signal,
481 G_CALLBACK (run_until_ready_cb),
487 g_main_loop_run (data.loop);
493 g_signal_handler_disconnect (object, signal_id);
494 g_main_loop_unref (data.loop);
498 empathy_run_until_ready (gpointer object)
500 empathy_run_until_ready_full (object, "notify::ready", object_is_ready,
505 empathy_channel_get_account (TpChannel *channel)
507 TpConnection *connection;
511 g_object_get (channel, "connection", &connection, NULL);
512 mc = empathy_mission_control_new ();
513 account = mission_control_get_account_for_tpconnection (mc, connection, NULL);
514 g_object_unref (connection);
520 typedef void (*AccountStatusChangedFunc) (MissionControl *mc,
521 TpConnectionStatus status,
523 TpConnectionStatusReason reason,
524 const gchar *unique_name,
528 AccountStatusChangedFunc handler;
530 GClosureNotify free_func;
532 } AccountStatusChangedData;
535 TpConnectionStatus status;
537 TpConnectionStatusReason reason;
539 AccountStatusChangedData *data;
543 account_status_changed_data_free (gpointer ptr,
546 AccountStatusChangedData *data = ptr;
548 if (data->free_func) {
549 data->free_func (data->user_data, closure);
551 g_object_unref (data->mc);
552 g_slice_free (AccountStatusChangedData, data);
556 account_status_changed_invoke_callback (gpointer data)
558 InvocationData *invocation_data = data;
560 invocation_data->data->handler (invocation_data->data->mc,
561 invocation_data->status,
562 invocation_data->presence,
563 invocation_data->reason,
564 invocation_data->unique_name,
565 invocation_data->data->user_data);
567 g_free (invocation_data->unique_name);
568 g_slice_free (InvocationData, invocation_data);
574 account_status_changed_cb (MissionControl *mc,
575 TpConnectionStatus status,
577 TpConnectionStatusReason reason,
578 const gchar *unique_name,
579 AccountStatusChangedData *data)
581 InvocationData *invocation_data;
583 invocation_data = g_slice_new (InvocationData);
584 invocation_data->status = status;
585 invocation_data->presence = presence;
586 invocation_data->reason = reason;
587 invocation_data->unique_name = g_strdup (unique_name);
588 invocation_data->data = data;
590 g_idle_add_full (G_PRIORITY_HIGH,
591 account_status_changed_invoke_callback,
592 invocation_data, NULL);
596 empathy_connect_to_account_status_changed (MissionControl *mc,
599 GClosureNotify free_func)
601 AccountStatusChangedData *data;
603 g_return_val_if_fail (IS_MISSIONCONTROL (mc), NULL);
604 g_return_val_if_fail (handler != NULL, NULL);
606 data = g_slice_new (AccountStatusChangedData);
607 data->handler = (AccountStatusChangedFunc) handler;
608 data->user_data = user_data;
609 data->free_func = free_func;
610 data->mc = g_object_ref (mc);
612 dbus_g_proxy_connect_signal (DBUS_G_PROXY (mc), "AccountStatusChanged",
613 G_CALLBACK (account_status_changed_cb),
614 data, account_status_changed_data_free);
620 empathy_disconnect_account_status_changed (gpointer token)
622 AccountStatusChangedData *data = token;
624 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (data->mc),
625 "AccountStatusChanged",
626 G_CALLBACK (account_status_changed_cb),
631 empathy_proxy_hash (gconstpointer key)
633 TpProxy *proxy = TP_PROXY (key);
635 g_return_val_if_fail (TP_IS_PROXY (proxy), 0);
637 return g_str_hash (proxy->object_path) +
638 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);
648 g_return_val_if_fail (TP_IS_PROXY (proxy_a), FALSE);
649 g_return_val_if_fail (TP_IS_PROXY (proxy_b), FALSE);
651 return g_str_equal (proxy_a->object_path, proxy_b->object_path) &&
652 g_str_equal (proxy_a->bus_name, proxy_b->bus_name);