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 <libtelepathy/tp-helpers.h>
37 #include <libtelepathy/tp-conn.h>
39 #include "empathy-debug.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_DOMAIN "Utils"
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 USERCHARS "-A-Za-z0-9"
61 #define PASSCHARS "-A-Za-z0-9,?;.:/!%$^*&~\"#'"
62 #define HOSTCHARS "-A-Za-z0-9_"
63 #define PATHCHARS "-A-Za-z0-9_$.+!*(),;:@&=?/~#%"
64 #define SCHEME "(news:|telnet:|nntp:|file:/|https?:|ftps?:|webcal:)"
65 #define USER "[" USERCHARS "]+(:["PASSCHARS "]+)?"
66 #define URLPATH "/[" PATHCHARS "]*[^]'.}>) \t\r\n,\\\"]"
68 static regex_t dingus[EMPATHY_REGEX_ALL];
73 static gboolean inited = FALSE;
74 const gchar *expression;
81 for (i = 0; i < EMPATHY_REGEX_ALL; i++) {
83 case EMPATHY_REGEX_AS_IS:
85 SCHEME "//(" USER "@)?[" HOSTCHARS ".]+"
86 "(:[0-9]+)?(" URLPATH ")?";
88 case EMPATHY_REGEX_BROWSER:
90 "(www|ftp)[" HOSTCHARS "]*\\.[" HOSTCHARS ".]+"
91 "(:[0-9]+)?(" URLPATH ")?";
93 case EMPATHY_REGEX_EMAIL:
95 "(mailto:)?[a-z0-9][a-z0-9.-]*@[a-z0-9]"
96 "[a-z0-9-]*(\\.[a-z0-9][a-z0-9-]*)+";
98 case EMPATHY_REGEX_OTHER:
100 "news:[-A-Z\\^_a-z{|}~!\"#$%&'()*+,./0-9;:=?`]+"
101 "@[" HOSTCHARS ".]+(:[0-9]+)?";
104 /* Silence the compiler. */
109 memset (&dingus[i], 0, sizeof (regex_t));
110 regcomp (&dingus[i], expression, REG_EXTENDED | REG_ICASE);
117 empathy_regex_match (EmpathyRegExType type,
122 regmatch_t matches[1];
124 gint num_matches = 0;
128 g_return_val_if_fail (type >= 0 || type <= EMPATHY_REGEX_ALL, 0);
132 while (!ret && type != EMPATHY_REGEX_ALL) {
133 ret = regexec (&dingus[type], msg + offset, 1, matches, 0);
139 s = matches[0].rm_so + offset;
140 offset = matches[0].rm_eo + offset;
142 g_array_append_val (start, s);
143 g_array_append_val (end, offset);
147 if (type != EMPATHY_REGEX_ALL) {
148 empathy_debug (DEBUG_DOMAIN,
149 "Found %d matches for regex type:%d",
154 /* If EMPATHY_REGEX_ALL then we run ALL regex's on the string. */
155 for (i = 0; i < EMPATHY_REGEX_ALL; i++, ret = 0) {
157 ret = regexec (&dingus[i], msg + offset, 1, matches, 0);
163 s = matches[0].rm_so + offset;
164 offset = matches[0].rm_eo + offset;
166 g_array_append_val (start, s);
167 g_array_append_val (end, offset);
172 empathy_debug (DEBUG_DOMAIN,
173 "Found %d matches for ALL regex types",
180 empathy_strcasecmp (const gchar *s1,
183 return empathy_strncasecmp (s1, s2, -1);
187 empathy_strncasecmp (const gchar *s1,
194 u1 = g_utf8_casefold (s1, n);
195 u2 = g_utf8_casefold (s2, n);
197 ret_val = g_utf8_collate (u1, u2);
205 empathy_xml_validate (xmlDoc *doc,
206 const gchar *dtd_filename)
208 gchar *path, *escaped;
213 path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "libempathy",
215 if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
217 path = g_build_filename (DATADIR, "empathy", dtd_filename, NULL);
219 empathy_debug (DEBUG_DOMAIN, "Loading dtd file %s", path);
221 /* The list of valid chars is taken from libxml. */
222 escaped = xmlURIEscapeStr (path, ":@&=+$,/?;");
225 memset (&cvp, 0, sizeof (cvp));
226 dtd = xmlParseDTD (NULL, escaped);
227 ret = xmlValidateDtd (&cvp, doc, dtd);
236 empathy_xml_node_get_child (xmlNodePtr node,
237 const gchar *child_name)
241 g_return_val_if_fail (node != NULL, NULL);
242 g_return_val_if_fail (child_name != NULL, NULL);
244 for (l = node->children; l; l = l->next) {
245 if (l->name && strcmp (l->name, child_name) == 0) {
254 empathy_xml_node_get_child_content (xmlNodePtr node,
255 const gchar *child_name)
259 g_return_val_if_fail (node != NULL, NULL);
260 g_return_val_if_fail (child_name != NULL, NULL);
262 l = empathy_xml_node_get_child (node, child_name);
264 return xmlNodeGetContent (l);
271 empathy_xml_node_find_child_prop_value (xmlNodePtr node,
272 const gchar *prop_name,
273 const gchar *prop_value)
276 xmlNodePtr found = NULL;
278 g_return_val_if_fail (node != NULL, NULL);
279 g_return_val_if_fail (prop_name != NULL, NULL);
280 g_return_val_if_fail (prop_value != NULL, NULL);
282 for (l = node->children; l && !found; l = l->next) {
285 if (!xmlHasProp (l, prop_name)) {
289 prop = xmlGetProp (l, prop_name);
290 if (prop && strcmp (prop, prop_value) == 0) {
301 empathy_account_hash (gconstpointer key)
303 g_return_val_if_fail (MC_IS_ACCOUNT (key), 0);
305 return g_str_hash (mc_account_get_unique_name (MC_ACCOUNT (key)));
309 empathy_account_equal (gconstpointer a,
315 g_return_val_if_fail (MC_IS_ACCOUNT (a), FALSE);
316 g_return_val_if_fail (MC_IS_ACCOUNT (b), FALSE);
318 name_a = mc_account_get_unique_name (MC_ACCOUNT (a));
319 name_b = mc_account_get_unique_name (MC_ACCOUNT (b));
321 return g_str_equal (name_a, name_b);
325 empathy_mission_control_new (void)
327 static MissionControl *mc = NULL;
330 mc = mission_control_new (tp_get_bus ());
331 g_object_add_weak_pointer (G_OBJECT (mc), (gpointer) &mc);
340 empathy_inspect_channel (McAccount *account,
343 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
344 g_return_val_if_fail (TELEPATHY_IS_CHAN (tp_chan), NULL);
346 return empathy_inspect_handle (account,
348 tp_chan->handle_type);
352 empathy_inspect_handle (McAccount *account,
361 GError *error = NULL;
363 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
364 g_return_val_if_fail (handle != 0, NULL);
365 g_return_val_if_fail (handle_type != 0, NULL);
367 mc = empathy_mission_control_new ();
368 tp_conn = mission_control_get_connection (mc, account, NULL);
375 /* Get the handle's name */
376 handles = g_array_new (FALSE, FALSE, sizeof (guint));
377 g_array_append_val (handles, handle);
378 if (!tp_conn_inspect_handles (DBUS_G_PROXY (tp_conn),
383 empathy_debug (DEBUG_DOMAIN,
384 "Couldn't get id: %s",
385 error ? error->message : "No error given");
387 g_clear_error (&error);
388 g_array_free (handles, TRUE);
389 g_object_unref (tp_conn);
394 g_array_free (handles, TRUE);
397 g_object_unref (tp_conn);
403 empathy_call_with_contact (EmpathyContact *contact)
410 const gchar *bus_name;
412 EmpathyContactFactory *factory;
413 EmpathyTpGroup *group;
414 EmpathyContact *self_contact;
415 GError *error = NULL;
417 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
419 /* StreamedMedia channels must have handle=0 and handle_type=none.
420 * To call a contact we have to add him in the group interface of the
421 * channel. MissionControl will detect the channel creation and
422 * dispatch it to the VoIP chandler automatically. */
424 mc = empathy_mission_control_new ();
425 account = empathy_contact_get_account (contact);
426 tp_conn = mission_control_get_connection (mc, account, NULL);
427 /* FIXME: Should be async */
428 if (!tp_conn_request_channel (DBUS_G_PROXY (tp_conn),
429 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
435 empathy_debug (DEBUG_DOMAIN,
436 "Couldn't request channel: %s",
437 error ? error->message : "No error given");
438 g_clear_error (&error);
440 g_object_unref (tp_conn);
444 bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (tp_conn));
445 new_chan = tp_chan_new (tp_get_bus (),
448 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
452 group = empathy_tp_group_new (account, new_chan);
453 factory = empathy_contact_factory_new ();
454 self_contact = empathy_contact_factory_get_user (factory, account);
455 empathy_tp_group_add_member (group, contact, "");
456 empathy_tp_group_add_member (group, self_contact, "");
458 g_object_unref (factory);
459 g_object_unref (self_contact);
460 g_object_unref (group);
462 g_object_unref (tp_conn);
463 g_object_unref (new_chan);
464 g_free (object_path);
470 got_handle_cb (EmpathyContact *contact,
471 GParamSpec *property,
472 EmpathyContactFactory *factory)
474 g_signal_handlers_disconnect_by_func (contact,
478 empathy_call_with_contact (contact);
479 g_object_unref (factory);
480 g_object_unref (contact);
485 empathy_call_with_contact_id (McAccount *account, const gchar *contact_id)
488 EmpathyContactFactory *factory;
489 EmpathyContact *contact;
491 factory = empathy_contact_factory_new ();
492 contact = empathy_contact_factory_get_from_id (factory, account, contact_id);
494 if (empathy_contact_get_handle (contact) != 0) {
495 empathy_call_with_contact (contact);
496 g_object_unref (contact);
497 g_object_unref (factory);
499 g_signal_connect (contact, "notify::handle",
500 G_CALLBACK (got_handle_cb),
507 empathy_chat_with_contact (EmpathyContact *contact)
511 mc = empathy_mission_control_new ();
512 mission_control_request_channel (mc,
513 empathy_contact_get_account (contact),
514 TP_IFACE_CHANNEL_TYPE_TEXT,
515 empathy_contact_get_handle (contact),
516 TP_HANDLE_TYPE_CONTACT,
522 empathy_chat_with_contact_id (McAccount *account, const gchar *contact_id)
526 mc = empathy_mission_control_new ();
527 mission_control_request_channel_with_string_handle (mc,
529 TP_IFACE_CHANNEL_TYPE_TEXT,
531 TP_HANDLE_TYPE_CONTACT,
537 empathy_presence_get_default_message (McPresence presence)
540 case MC_PRESENCE_AVAILABLE:
541 return _("Available");
542 case MC_PRESENCE_DO_NOT_DISTURB:
544 case MC_PRESENCE_AWAY:
545 case MC_PRESENCE_EXTENDED_AWAY:
547 case MC_PRESENCE_HIDDEN:
549 case MC_PRESENCE_OFFLINE:
550 case MC_PRESENCE_UNSET:
553 g_assert_not_reached ();
560 empathy_presence_to_str (McPresence presence)
563 case MC_PRESENCE_AVAILABLE:
565 case MC_PRESENCE_DO_NOT_DISTURB:
567 case MC_PRESENCE_AWAY:
569 case MC_PRESENCE_EXTENDED_AWAY:
571 case MC_PRESENCE_HIDDEN:
573 case MC_PRESENCE_OFFLINE:
575 case MC_PRESENCE_UNSET:
578 g_assert_not_reached ();
585 empathy_presence_from_str (const gchar *str)
587 if (strcmp (str, "available") == 0) {
588 return MC_PRESENCE_AVAILABLE;
589 } else if ((strcmp (str, "dnd") == 0) || (strcmp (str, "busy") == 0)) {
590 return MC_PRESENCE_DO_NOT_DISTURB;
591 } else if ((strcmp (str, "away") == 0) || (strcmp (str, "brb") == 0)) {
592 return MC_PRESENCE_AWAY;
593 } else if ((strcmp (str, "xa") == 0) || (strcmp (str, "ext_away") == 0)) {
594 return MC_PRESENCE_EXTENDED_AWAY;
595 } else if (strcmp (str, "hidden") == 0) {
596 return MC_PRESENCE_HIDDEN;
597 } else if (strcmp (str, "offline") == 0) {
598 return MC_PRESENCE_OFFLINE;
599 } else if (strcmp (str, "unset") == 0) {
600 return MC_PRESENCE_UNSET;
603 return MC_PRESENCE_AVAILABLE;