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 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 <libmissioncontrol/mc-account.h>
37
#include <libtelepathy/tp-helpers.h>
39
#include "gossip-debug.h"
40
#include "gossip-utils.h"
41
#include "gossip-paths.h"
42
#include "empathy-contact-manager.h"
44
#define DEBUG_DOMAIN "Utils"
46
static void regex_init (void);
49
gossip_substring (const gchar *str,
53
return g_strndup (str + start, end - start);
57
* Regular Expression code to match urls.
59
#define USERCHARS "-A-Za-z0-9"
60
#define PASSCHARS "-A-Za-z0-9,?;.:/!%$^*&~\"#'"
61
#define HOSTCHARS "-A-Za-z0-9"
62
#define PATHCHARS "-A-Za-z0-9_$.+!*(),;:@&=?/~#%"
63
#define SCHEME "(news:|telnet:|nntp:|file:/|https?:|ftps?:|webcal:)"
64
#define USER "[" USERCHARS "]+(:["PASSCHARS "]+)?"
65
#define URLPATH "/[" PATHCHARS "]*[^]'.}>) \t\r\n,\\\"]"
67
static regex_t dingus[GOSSIP_REGEX_ALL];
72
static gboolean inited = FALSE;
73
const gchar *expression;
80
for (i = 0; i < GOSSIP_REGEX_ALL; i++) {
82
case GOSSIP_REGEX_AS_IS:
84
SCHEME "//(" USER "@)?[" HOSTCHARS ".]+"
85
"(:[0-9]+)?(" URLPATH ")?";
87
case GOSSIP_REGEX_BROWSER:
89
"(www|ftp)[" HOSTCHARS "]*\\.[" HOSTCHARS ".]+"
90
"(:[0-9]+)?(" URLPATH ")?";
92
case GOSSIP_REGEX_EMAIL:
94
"(mailto:)?[a-z0-9][a-z0-9.-]*@[a-z0-9]"
95
"[a-z0-9-]*(\\.[a-z0-9][a-z0-9-]*)+";
97
case GOSSIP_REGEX_OTHER:
99
"news:[-A-Z\\^_a-z{|}~!\"#$%&'()*+,./0-9;:=?`]+"
100
"@[" HOSTCHARS ".]+(:[0-9]+)?";
103
/* Silence the compiler. */
108
memset (&dingus[i], 0, sizeof (regex_t));
109
regcomp (&dingus[i], expression, REG_EXTENDED | REG_ICASE);
116
gossip_regex_match (GossipRegExType type,
121
regmatch_t matches[1];
123
gint num_matches = 0;
127
g_return_val_if_fail (type >= 0 || type <= GOSSIP_REGEX_ALL, 0);
131
while (!ret && type != GOSSIP_REGEX_ALL) {
132
ret = regexec (&dingus[type], msg + offset, 1, matches, 0);
138
s = matches[0].rm_so + offset;
139
offset = matches[0].rm_eo + offset;
141
g_array_append_val (start, s);
142
g_array_append_val (end, offset);
146
if (type != GOSSIP_REGEX_ALL) {
147
gossip_debug (DEBUG_DOMAIN,
148
"Found %d matches for regex type:%d",
153
/* If GOSSIP_REGEX_ALL then we run ALL regex's on the string. */
154
for (i = 0; i < GOSSIP_REGEX_ALL; i++, ret = 0) {
156
ret = regexec (&dingus[i], msg + offset, 1, matches, 0);
162
s = matches[0].rm_so + offset;
163
offset = matches[0].rm_eo + offset;
165
g_array_append_val (start, s);
166
g_array_append_val (end, offset);
171
gossip_debug (DEBUG_DOMAIN,
172
"Found %d matches for ALL regex types",
179
gossip_strcasecmp (const gchar *s1,
182
return gossip_strncasecmp (s1, s2, -1);
186
gossip_strncasecmp (const gchar *s1,
193
u1 = g_utf8_casefold (s1, n);
194
u2 = g_utf8_casefold (s2, n);
196
ret_val = g_utf8_collate (u1, u2);
204
gossip_xml_validate (xmlDoc *doc,
205
const gchar *dtd_filename)
207
gchar *path, *escaped;
212
path = gossip_paths_get_dtd_path (dtd_filename);
214
/* The list of valid chars is taken from libxml. */
215
escaped = xmlURIEscapeStr (path, ":@&=+$,/?;");
219
memset (&cvp, 0, sizeof (cvp));
220
dtd = xmlParseDTD (NULL, escaped);
221
ret = xmlValidateDtd (&cvp, doc, dtd);
230
gossip_xml_node_get_child (xmlNodePtr node,
231
const gchar *child_name)
235
g_return_val_if_fail (node != NULL, NULL);
236
g_return_val_if_fail (child_name != NULL, NULL);
238
for (l = node->children; l; l = l->next) {
239
if (l->name && strcmp (l->name, child_name) == 0) {
248
gossip_xml_node_get_child_content (xmlNodePtr node,
249
const gchar *child_name)
253
g_return_val_if_fail (node != NULL, NULL);
254
g_return_val_if_fail (child_name != NULL, NULL);
256
l = gossip_xml_node_get_child (node, child_name);
258
return xmlNodeGetContent (l);
265
gossip_xml_node_find_child_prop_value (xmlNodePtr node,
266
const gchar *prop_name,
267
const gchar *prop_value)
270
xmlNodePtr found = NULL;
272
g_return_val_if_fail (node != NULL, NULL);
273
g_return_val_if_fail (prop_name != NULL, NULL);
274
g_return_val_if_fail (prop_value != NULL, NULL);
276
for (l = node->children; l && !found; l = l->next) {
279
if (!xmlHasProp (l, prop_name)) {
283
prop = xmlGetProp (l, prop_name);
284
if (prop && strcmp (prop, prop_value) == 0) {
295
gossip_dbus_type_to_g_type (const gchar *dbus_type_string)
297
if (dbus_type_string == NULL)
300
if (dbus_type_string[0] == 's') {
301
return G_TYPE_STRING;
303
else if (dbus_type_string[0] == 'b') {
304
return G_TYPE_BOOLEAN;
306
else if (dbus_type_string[0] == 'q') {
309
else if (dbus_type_string[0] == 'n') {
313
g_assert_not_reached ();
318
gossip_g_type_to_dbus_type (GType g_type)
330
g_assert_not_reached ();
337
gossip_g_value_to_string (const GValue *value)
339
gchar *return_string = NULL;
340
GValue string_g_value = {0, };
342
g_value_init (&string_g_value, G_TYPE_STRING);
343
g_value_transform (value, &string_g_value);
344
return_string = g_value_dup_string (&string_g_value);
345
g_value_unset (&string_g_value);
347
return return_string;
351
gossip_string_to_g_value (const gchar *str, GType type)
355
g_value = g_new0 (GValue, 1);
356
g_value_init (g_value, type);
360
g_value_set_string (g_value, str);
363
g_value_set_boolean (g_value, (str[0] == 'y' || str[0] == 'T'));
366
g_value_set_uint (g_value, atoi (str));
369
g_value_set_int (g_value, atoi (str));
372
g_assert_not_reached ();
379
gossip_g_value_equal (const GValue *value1,
380
const GValue *value2)
384
g_return_val_if_fail (value1 != NULL, FALSE);
385
g_return_val_if_fail (value2 != NULL, FALSE);
387
type = G_VALUE_TYPE (value1);
388
if (type != G_VALUE_TYPE (value2)) {
394
case G_TYPE_STRING: {
398
str1 = g_value_get_string (value1);
399
str2 = g_value_get_string (value2);
400
return (str1 && str2 && strcmp (str1, str2) == 0) ||
401
(G_STR_EMPTY (str1) && G_STR_EMPTY (str2));
404
return g_value_get_boolean (value1) == g_value_get_boolean (value2);
406
return g_value_get_uint (value1) == g_value_get_uint (value2);
408
return g_value_get_int (value1) == g_value_get_int (value2);
410
g_warning ("Unsupported GType in value comparaison");
417
gossip_account_hash (gconstpointer key)
419
return g_str_hash (mc_account_get_unique_name (MC_ACCOUNT (key)));
423
gossip_account_equal (gconstpointer a,
429
name_a = mc_account_get_unique_name (MC_ACCOUNT (a));
430
name_b = mc_account_get_unique_name (MC_ACCOUNT (b));
432
return g_str_equal (name_a, name_b);
436
gossip_mission_control_new (void)
438
static MissionControl *mc = NULL;
441
mc = mission_control_new (tp_get_bus ());
442
g_object_add_weak_pointer (G_OBJECT (mc), (gpointer) &mc);