~bcurtiswx/ubuntu/precise/empathy/3.4.2.1-0ubuntu1

« back to all changes in this revision

Viewing changes to libempathy/gossip-utils.c

  • Committer: Bazaar Package Importer
  • Author(s): Sjoerd Simons
  • Date: 2007-05-20 15:31:42 UTC
  • Revision ID: james.westby@ubuntu.com-20070520153142-r3auwguxdgxhktqb
Tags: upstream-0.4
ImportĀ upstreamĀ versionĀ 0.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
 
2
/*
 
3
 * Copyright (C) 2003-2007 Imendio AB
 
4
 * Copyright (C) 2007 Collabora Ltd.
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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.
 
20
 *
 
21
 * Authors: Richard Hult <richard@imendio.com>
 
22
 *          Martyn Russell <martyn@imendio.com>
 
23
 *          Xavier Claessens <xclaesse@gmail.com>
 
24
 */
 
25
 
 
26
#include "config.h"
 
27
 
 
28
#include <string.h>
 
29
#include <time.h>
 
30
#include <sys/types.h>
 
31
#include <regex.h>
 
32
 
 
33
#include <glib/gi18n.h>
 
34
 
 
35
#include <libxml/uri.h>
 
36
#include <libmissioncontrol/mc-account.h>
 
37
#include <libtelepathy/tp-helpers.h>
 
38
 
 
39
#include "gossip-debug.h"
 
40
#include "gossip-utils.h"
 
41
#include "gossip-paths.h"
 
42
#include "empathy-contact-manager.h"
 
43
 
 
44
#define DEBUG_DOMAIN "Utils"
 
45
 
 
46
static void regex_init (void);
 
47
 
 
48
gchar *
 
49
gossip_substring (const gchar *str,
 
50
                  gint         start,
 
51
                  gint         end)
 
52
{
 
53
        return g_strndup (str + start, end - start);
 
54
}
 
55
 
 
56
/*
 
57
 * Regular Expression code to match urls.
 
58
 */
 
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,\\\"]"
 
66
 
 
67
static regex_t dingus[GOSSIP_REGEX_ALL];
 
68
 
 
69
static void
 
70
regex_init (void)
 
71
{
 
72
        static gboolean  inited = FALSE;
 
73
        const gchar     *expression;
 
74
        gint             i;
 
75
 
 
76
        if (inited) {
 
77
                return;
 
78
        }
 
79
 
 
80
        for (i = 0; i < GOSSIP_REGEX_ALL; i++) {
 
81
                switch (i) {
 
82
                case GOSSIP_REGEX_AS_IS:
 
83
                        expression =
 
84
                                SCHEME "//(" USER "@)?[" HOSTCHARS ".]+"
 
85
                                "(:[0-9]+)?(" URLPATH ")?";
 
86
                        break;
 
87
                case GOSSIP_REGEX_BROWSER:
 
88
                        expression =
 
89
                                "(www|ftp)[" HOSTCHARS "]*\\.[" HOSTCHARS ".]+"
 
90
                                "(:[0-9]+)?(" URLPATH ")?";
 
91
                        break;
 
92
                case GOSSIP_REGEX_EMAIL:
 
93
                        expression =
 
94
                                "(mailto:)?[a-z0-9][a-z0-9.-]*@[a-z0-9]"
 
95
                                "[a-z0-9-]*(\\.[a-z0-9][a-z0-9-]*)+";
 
96
                        break;
 
97
                case GOSSIP_REGEX_OTHER:
 
98
                        expression =
 
99
                                "news:[-A-Z\\^_a-z{|}~!\"#$%&'()*+,./0-9;:=?`]+"
 
100
                                "@[" HOSTCHARS ".]+(:[0-9]+)?";
 
101
                        break;
 
102
                default:
 
103
                        /* Silence the compiler. */
 
104
                        expression = NULL;
 
105
                        continue;
 
106
                }
 
107
 
 
108
                memset (&dingus[i], 0, sizeof (regex_t));
 
109
                regcomp (&dingus[i], expression, REG_EXTENDED | REG_ICASE);
 
110
        }
 
111
 
 
112
        inited = TRUE;
 
113
}
 
114
 
 
115
gint
 
116
gossip_regex_match (GossipRegExType  type,
 
117
                    const gchar     *msg,
 
118
                    GArray          *start,
 
119
                    GArray          *end)
 
120
{
 
121
        regmatch_t matches[1];
 
122
        gint       ret = 0;
 
123
        gint       num_matches = 0;
 
124
        gint       offset = 0;
 
125
        gint       i;
 
126
 
 
127
        g_return_val_if_fail (type >= 0 || type <= GOSSIP_REGEX_ALL, 0);
 
128
 
 
129
        regex_init ();
 
130
 
 
131
        while (!ret && type != GOSSIP_REGEX_ALL) {
 
132
                ret = regexec (&dingus[type], msg + offset, 1, matches, 0);
 
133
                if (ret == 0) {
 
134
                        gint s;
 
135
 
 
136
                        num_matches++;
 
137
 
 
138
                        s = matches[0].rm_so + offset;
 
139
                        offset = matches[0].rm_eo + offset;
 
140
 
 
141
                        g_array_append_val (start, s);
 
142
                        g_array_append_val (end, offset);
 
143
                }
 
144
        }
 
145
 
 
146
        if (type != GOSSIP_REGEX_ALL) {
 
147
                gossip_debug (DEBUG_DOMAIN,
 
148
                              "Found %d matches for regex type:%d",
 
149
                              num_matches, type);
 
150
                return num_matches;
 
151
        }
 
152
 
 
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) {
 
155
                while (!ret) {
 
156
                        ret = regexec (&dingus[i], msg + offset, 1, matches, 0);
 
157
                        if (ret == 0) {
 
158
                                gint s;
 
159
 
 
160
                                num_matches++;
 
161
 
 
162
                                s = matches[0].rm_so + offset;
 
163
                                offset = matches[0].rm_eo + offset;
 
164
 
 
165
                                g_array_append_val (start, s);
 
166
                                g_array_append_val (end, offset);
 
167
                        }
 
168
                }
 
169
        }
 
170
 
 
171
        gossip_debug (DEBUG_DOMAIN,
 
172
                      "Found %d matches for ALL regex types",
 
173
                      num_matches);
 
174
 
 
175
        return num_matches;
 
176
}
 
177
 
 
178
gint
 
179
gossip_strcasecmp (const gchar *s1,
 
180
                   const gchar *s2)
 
181
{
 
182
        return gossip_strncasecmp (s1, s2, -1);
 
183
}
 
184
 
 
185
gint
 
186
gossip_strncasecmp (const gchar *s1,
 
187
                    const gchar *s2,
 
188
                    gsize        n)
 
189
{
 
190
        gchar *u1, *u2;
 
191
        gint   ret_val;
 
192
 
 
193
        u1 = g_utf8_casefold (s1, n);
 
194
        u2 = g_utf8_casefold (s2, n);
 
195
 
 
196
        ret_val = g_utf8_collate (u1, u2);
 
197
        g_free (u1);
 
198
        g_free (u2);
 
199
 
 
200
        return ret_val;
 
201
}
 
202
 
 
203
gboolean
 
204
gossip_xml_validate (xmlDoc      *doc,
 
205
                     const gchar *dtd_filename)
 
206
{
 
207
        gchar        *path, *escaped;
 
208
        xmlValidCtxt  cvp;
 
209
        xmlDtd       *dtd;
 
210
        gboolean      ret;
 
211
 
 
212
        path = gossip_paths_get_dtd_path (dtd_filename);
 
213
 
 
214
        /* The list of valid chars is taken from libxml. */
 
215
        escaped = xmlURIEscapeStr (path, ":@&=+$,/?;");
 
216
 
 
217
        g_free (path);
 
218
 
 
219
        memset (&cvp, 0, sizeof (cvp));
 
220
        dtd = xmlParseDTD (NULL, escaped);
 
221
        ret = xmlValidateDtd (&cvp, doc, dtd);
 
222
 
 
223
        xmlFree (escaped);
 
224
        xmlFreeDtd (dtd);
 
225
 
 
226
        return ret;
 
227
}
 
228
 
 
229
xmlNodePtr
 
230
gossip_xml_node_get_child (xmlNodePtr   node, 
 
231
                           const gchar *child_name)
 
232
{
 
233
        xmlNodePtr l;
 
234
 
 
235
        g_return_val_if_fail (node != NULL, NULL);
 
236
        g_return_val_if_fail (child_name != NULL, NULL);
 
237
 
 
238
        for (l = node->children; l; l = l->next) {
 
239
                if (l->name && strcmp (l->name, child_name) == 0) {
 
240
                        return l;
 
241
                }
 
242
        }
 
243
 
 
244
        return NULL;
 
245
}
 
246
 
 
247
xmlChar *
 
248
gossip_xml_node_get_child_content (xmlNodePtr   node, 
 
249
                                   const gchar *child_name)
 
250
{
 
251
        xmlNodePtr l;
 
252
 
 
253
        g_return_val_if_fail (node != NULL, NULL);
 
254
        g_return_val_if_fail (child_name != NULL, NULL);
 
255
 
 
256
        l = gossip_xml_node_get_child (node, child_name);
 
257
        if (l) {
 
258
                return xmlNodeGetContent (l);
 
259
        }
 
260
                
 
261
        return NULL;
 
262
}
 
263
 
 
264
xmlNodePtr
 
265
gossip_xml_node_find_child_prop_value (xmlNodePtr   node, 
 
266
                                       const gchar *prop_name,
 
267
                                       const gchar *prop_value)
 
268
{
 
269
        xmlNodePtr l;
 
270
        xmlNodePtr found = NULL;
 
271
 
 
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);
 
275
 
 
276
        for (l = node->children; l && !found; l = l->next) {
 
277
                xmlChar *prop;
 
278
 
 
279
                if (!xmlHasProp (l, prop_name)) {
 
280
                        continue;
 
281
                }
 
282
 
 
283
                prop = xmlGetProp (l, prop_name);
 
284
                if (prop && strcmp (prop, prop_value) == 0) {
 
285
                        found = l;
 
286
                }
 
287
                
 
288
                xmlFree (prop);
 
289
        }
 
290
                
 
291
        return found;
 
292
}
 
293
 
 
294
GType
 
295
gossip_dbus_type_to_g_type (const gchar *dbus_type_string)
 
296
{
 
297
        if (dbus_type_string == NULL)
 
298
                return G_TYPE_NONE;
 
299
 
 
300
        if (dbus_type_string[0] == 's') {
 
301
                return G_TYPE_STRING;
 
302
        }
 
303
        else if (dbus_type_string[0] == 'b') {
 
304
                return G_TYPE_BOOLEAN;
 
305
        }
 
306
        else if (dbus_type_string[0] == 'q') {
 
307
                return G_TYPE_UINT;
 
308
        }
 
309
        else if (dbus_type_string[0] == 'n') {
 
310
                return G_TYPE_INT;
 
311
        }
 
312
 
 
313
        g_assert_not_reached ();
 
314
        return G_TYPE_NONE;
 
315
}
 
316
 
 
317
const gchar *
 
318
gossip_g_type_to_dbus_type (GType g_type)
 
319
{
 
320
        switch (g_type) {
 
321
        case G_TYPE_STRING:
 
322
                return "s";
 
323
        case G_TYPE_BOOLEAN:
 
324
                return "b";
 
325
        case G_TYPE_UINT:
 
326
                return "q";
 
327
        case G_TYPE_INT:
 
328
                return "n";
 
329
        default:
 
330
                g_assert_not_reached ();
 
331
        }
 
332
 
 
333
        return NULL;
 
334
}
 
335
 
 
336
gchar *
 
337
gossip_g_value_to_string (const GValue *value)
 
338
{
 
339
        gchar  *return_string = NULL;
 
340
        GValue  string_g_value = {0, };
 
341
 
 
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);
 
346
 
 
347
        return return_string;
 
348
}
 
349
 
 
350
GValue *
 
351
gossip_string_to_g_value (const gchar *str, GType type)
 
352
{
 
353
        GValue *g_value;
 
354
 
 
355
        g_value = g_new0 (GValue, 1);
 
356
        g_value_init (g_value, type);
 
357
 
 
358
        switch (type) {
 
359
        case G_TYPE_STRING:
 
360
                g_value_set_string (g_value, str);
 
361
                break;
 
362
        case G_TYPE_BOOLEAN:
 
363
                g_value_set_boolean (g_value, (str[0] == 'y' || str[0] == 'T'));
 
364
                break;
 
365
        case G_TYPE_UINT:
 
366
                g_value_set_uint (g_value, atoi (str));
 
367
                break;
 
368
        case G_TYPE_INT:
 
369
                g_value_set_int (g_value, atoi (str));
 
370
                break;
 
371
        default:
 
372
                g_assert_not_reached ();
 
373
        }
 
374
 
 
375
        return g_value;
 
376
}
 
377
 
 
378
gboolean
 
379
gossip_g_value_equal (const GValue *value1,
 
380
                      const GValue *value2)
 
381
{
 
382
        GType type;
 
383
 
 
384
        g_return_val_if_fail (value1 != NULL, FALSE);
 
385
        g_return_val_if_fail (value2 != NULL, FALSE);
 
386
 
 
387
        type = G_VALUE_TYPE (value1);
 
388
        if (type != G_VALUE_TYPE (value2)) {
 
389
                return FALSE;
 
390
        }
 
391
 
 
392
        switch (type)
 
393
        {
 
394
        case G_TYPE_STRING: {
 
395
                const gchar *str1;
 
396
                const gchar *str2;
 
397
 
 
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));
 
402
        }
 
403
        case G_TYPE_BOOLEAN:
 
404
                return g_value_get_boolean (value1) == g_value_get_boolean (value2);
 
405
        case G_TYPE_UINT:
 
406
                return g_value_get_uint (value1) == g_value_get_uint (value2);
 
407
        case G_TYPE_INT:
 
408
                return g_value_get_int (value1) == g_value_get_int (value2);
 
409
        default:
 
410
                g_warning ("Unsupported GType in value comparaison");
 
411
        }
 
412
 
 
413
        return FALSE;
 
414
}
 
415
 
 
416
guint
 
417
gossip_account_hash (gconstpointer key)
 
418
{
 
419
        return g_str_hash (mc_account_get_unique_name (MC_ACCOUNT (key)));
 
420
}
 
421
 
 
422
gboolean
 
423
gossip_account_equal (gconstpointer a,
 
424
                      gconstpointer b)
 
425
{
 
426
        const gchar *name_a;
 
427
        const gchar *name_b;
 
428
 
 
429
        name_a = mc_account_get_unique_name (MC_ACCOUNT (a));
 
430
        name_b = mc_account_get_unique_name (MC_ACCOUNT (b));
 
431
 
 
432
        return g_str_equal (name_a, name_b);
 
433
}
 
434
 
 
435
MissionControl *
 
436
gossip_mission_control_new (void)
 
437
{
 
438
        static MissionControl *mc = NULL;
 
439
 
 
440
        if (!mc) {
 
441
                mc = mission_control_new (tp_get_bus ());
 
442
                g_object_add_weak_pointer (G_OBJECT (mc), (gpointer) &mc);
 
443
        } else {
 
444
                g_object_ref (mc);
 
445
        }
 
446
 
 
447
        return mc;
 
448
}
 
449