summaryrefslogtreecommitdiff
path: root/wocky/wocky-utils.c
blob: 88f75afe6bb016aec72a6f97c498421a4ecfe63d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
/*
 * wocky-utils.c - Code for Wocky utility functions
 * Copyright © 2007–2010 Collabora Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * wocky_g_value_slice_* functions have been copied from telepathy-glib's
 * util.c file:
 *  Copyright (C) 2006-2007 Collabora Ltd. <http://www.collabora.co.uk/>
 *  Copyright (C) 2006-2007 Nokia Corporation
 */

#include "wocky-utils.h"

#include <string.h>

#include <gio/gio.h>

/**
 * wocky_strdiff:
 * @left: The first string to compare (may be NULL)
 * @right: The second string to compare (may be NULL)
 *
 * Return %TRUE if the given strings are different. Unlike #strcmp this
 * function will handle null pointers, treating them as distinct from any
 * string.
 *
 * Returns: %FALSE if @left and @right are both %NULL, or if
 *          neither is %NULL and both have the same contents; %TRUE otherwise
 */
gboolean
wocky_strdiff (const gchar *left,
    const gchar *right)
{
  return g_strcmp0 (left, right) != 0;
}

static gboolean
validate_jid_node (const gchar *node)
{
  /* See RFC 3920 §3.3. */
  const gchar *c;

  for (c = node; *c; c++)
    if (strchr ("\"&'/:<>@", *c))
      /* RFC 3920 §A.5 */
      return FALSE;

  return TRUE;
}

static gboolean
validate_jid_domain (const gchar *domain)
{
  /* XXX: This doesn't do proper validation: it checks the character
   * range for ASCII characters, but lets through any non-ASCII characters. See
   * the ifdef-d out tests in wocky-jid-validation-test.c for examples of
   * erroneously accepted JIDs. In theory, we check that the domain is a
   * well-formed IDN or an IPv4/IPv6 address literal.
   *
   * See RFC 3920 §3.2.
   */

  const gchar *c;

  for (c = domain; *c; c++)
    {
      if ((unsigned char) *c >= 0x7F)
        continue;

      if (!g_ascii_isalnum (*c) && !strchr (":-.", *c))
        return FALSE;
    }

  return TRUE;
}

/**
 * wocky_decode_jid:
 * @jid: a JID
 * @node: address to which return the username/room part of the JID
 * @domain: address to which return the server/service part of the JID
 * @resource: address to which return the resource/nick part of the JID
 *
 *  If the JID is valid, returns TRUE and sets the caller's
 * node/domain/resource pointers if they are not NULL. The node and resource
 * pointers will be set to NULL if the respective part is not present in the
 * JID. The node and domain are lower-cased because the Jabber protocol treats
 * them case-insensitively.
 *
 * XXX: Do nodeprep/resourceprep and length checking.
 *
 * See RFC 3920 §3.
 *
 * Returns: %TRUE if the JID is valid
 */
gboolean
wocky_decode_jid (const gchar *jid,
    gchar **node,
    gchar **domain,
    gchar **resource)
{
  char *tmp_jid, *tmp_node, *tmp_domain, *tmp_resource;

  g_assert (jid != NULL);

  if (node != NULL)
    *node = NULL;

  if (domain != NULL)
    *domain = NULL;

  if (resource != NULL)
    *resource = NULL;

  /* Take a local copy so we don't modify the caller's string. */
  tmp_jid = g_strdup (jid);

  /* If there's a slash in tmp_jid, split it in two and take the second part as
   * the resource.
   */
  tmp_resource = strchr (tmp_jid, '/');

  if (tmp_resource != NULL)
    {
      *tmp_resource = '\0';
      tmp_resource++;
    }
  else
    {
      tmp_resource = NULL;
    }

  /* If there's an at sign in tmp_jid, split it in two and set tmp_node and
   * tmp_domain appropriately. Otherwise, tmp_node is NULL and the domain is
   * the whole string.
   */
  tmp_domain = strchr (tmp_jid, '@');

  if (tmp_domain != NULL)
    {
      *tmp_domain = '\0';
      tmp_domain++;
      tmp_node = tmp_jid;
    }
  else
    {
      tmp_domain = tmp_jid;
      tmp_node = NULL;
    }

  /* Domain must be non-empty and not contain invalid characters. If the node
   * or the resource exist, they must be non-empty and the node must not
   * contain invalid characters.
   */
  if (*tmp_domain == '\0' ||
      !validate_jid_domain (tmp_domain) ||
      (tmp_node != NULL &&
         (*tmp_node == '\0' || !validate_jid_node (tmp_node))) ||
      (tmp_resource != NULL && *tmp_resource == '\0'))
    {
      g_free (tmp_jid);
      return FALSE;
    }

  /* the server must be stored after we find the resource, in case we
   * truncated a resource from it */
  if (domain != NULL)
    *domain = g_utf8_strdown (tmp_domain, -1);

  /* store the username if the user provided a pointer */
  if (tmp_node != NULL && node != NULL)
    *node = g_utf8_strdown (tmp_node, -1);

  /* store the resource if the user provided a pointer */
  if (tmp_resource != NULL && resource != NULL)
    *resource = g_strdup (tmp_resource);

  /* free our working copy */
  g_free (tmp_jid);
  return TRUE;
}

/**
 * wocky_normalise_jid:
 * @jid: a JID
 *
 * Returns: a normalised JID, using the same rules as wocky_decode_jid(),
 * or %NULL if the JID could not be sensibly decoded.
 * This value should be freed when you are done with it.
 */
gchar *
wocky_normalise_jid (const gchar *jid)
{
  gchar *node = NULL;
  gchar *domain = NULL;
  gchar *resource = NULL;
  gchar *rval = NULL;

  if (jid == NULL)
    return NULL;

  if (!wocky_decode_jid (jid, &node, &domain, &resource))
    return NULL;

  rval = wocky_compose_jid (node, domain, resource);
  g_free (node);
  g_free (domain);
  g_free (resource);
  return rval;
}

static inline gsize
strlen0 (const gchar *s)
{
  return (s == NULL ? 0 : strlen (s));
}

/**
 * wocky_compose_jid:
 * @node: the node part of a JID, possibly empty or %NULL
 * @domain: the non-%NULL domain part of a JID
 * @resource: the resource part of a JID, possibly empty or %NULL
 *
 * Composes a JID from its parts. If @node is empty or %NULL, the '&commat;'
 * separator is also omitted; if @resource is empty or %NULL, the '/' separator
 * is also omitted.
 *
 * Returns: a JID constructed from @node, @domain and @resource
 */
gchar *
wocky_compose_jid (const gchar *node,
    const gchar *domain,
    const gchar *resource)
{
  GString *normal = NULL;

  normal = g_string_sized_new (strlen0 (node) + strlen0 (domain) +
      strlen0 (resource) + 2);

  if (node != NULL && *node != '\0')
    g_string_printf (normal, "%s@%s", node, domain);
  else
    g_string_printf (normal, "%s", domain);

  if (resource != NULL && *resource != '\0' && normal->len > 0)
    g_string_append_printf (normal, "/%s", resource);

  return g_string_free (normal, FALSE);
}

/**
 * wocky_g_value_slice_new:
 * @type: The type desired for the new GValue
 *
 * Slice-allocate an empty #GValue. wocky_g_value_slice_new_boolean() and similar
 * functions are likely to be more convenient to use for the types supported.
 *
 * Returns: a newly allocated, newly initialized #GValue, to be freed with
 * wocky_g_value_slice_free() or g_slice_free().
 * Since: 0.5.14
 */
GValue *
wocky_g_value_slice_new (GType type)
{
  GValue *ret = g_slice_new0 (GValue);

  g_value_init (ret, type);
  return ret;
}

/**
 * wocky_g_value_slice_new_boolean:
 * @b: a boolean value
 *
 * Slice-allocate and initialize a #GValue. This function is convenient to
 * use when constructing hash tables from string to #GValue, for example.
 *
 * Returns: a #GValue of type %G_TYPE_BOOLEAN with value @b, to be freed with
 * wocky_g_value_slice_free() or g_slice_free()
 *
 * Since: 0.7.27
 */
GValue *
wocky_g_value_slice_new_boolean (gboolean b)
{
  GValue *v = wocky_g_value_slice_new (G_TYPE_BOOLEAN);

  g_value_set_boolean (v, b);
  return v;
}

/**
 * wocky_g_value_slice_new_int:
 * @n: an integer
 *
 * Slice-allocate and initialize a #GValue. This function is convenient to
 * use when constructing hash tables from string to #GValue, for example.
 *
 * Returns: a #GValue of type %G_TYPE_INT with value @n, to be freed with
 * wocky_g_value_slice_free() or g_slice_free()
 *
 * Since: 0.7.27
 */
GValue *
wocky_g_value_slice_new_int (gint n)
{
  GValue *v = wocky_g_value_slice_new (G_TYPE_INT);

  g_value_set_int (v, n);
  return v;
}

/**
 * wocky_g_value_slice_new_int64:
 * @n: a 64-bit integer
 *
 * Slice-allocate and initialize a #GValue. This function is convenient to
 * use when constructing hash tables from string to #GValue, for example.
 *
 * Returns: a #GValue of type %G_TYPE_INT64 with value @n, to be freed with
 * wocky_g_value_slice_free() or g_slice_free()
 *
 * Since: 0.7.27
 */
GValue *
wocky_g_value_slice_new_int64 (gint64 n)
{
  GValue *v = wocky_g_value_slice_new (G_TYPE_INT64);

  g_value_set_int64 (v, n);
  return v;
}

/**
 * wocky_g_value_slice_new_uint:
 * @n: an unsigned integer
 *
 * Slice-allocate and initialize a #GValue. This function is convenient to
 * use when constructing hash tables from string to #GValue, for example.
 *
 * Returns: a #GValue of type %G_TYPE_UINT with value @n, to be freed with
 * wocky_g_value_slice_free() or g_slice_free()
 *
 * Since: 0.7.27
 */
GValue *
wocky_g_value_slice_new_uint (guint n)
{
  GValue *v = wocky_g_value_slice_new (G_TYPE_UINT);

  g_value_set_uint (v, n);
  return v;
}

/**
 * wocky_g_value_slice_new_uint64:
 * @n: a 64-bit unsigned integer
 *
 * Slice-allocate and initialize a #GValue. This function is convenient to
 * use when constructing hash tables from string to #GValue, for example.
 *
 * Returns: a #GValue of type %G_TYPE_UINT64 with value @n, to be freed with
 * wocky_g_value_slice_free() or g_slice_free()
 *
 * Since: 0.7.27
 */
GValue *
wocky_g_value_slice_new_uint64 (guint64 n)
{
  GValue *v = wocky_g_value_slice_new (G_TYPE_UINT64);

  g_value_set_uint64 (v, n);
  return v;
}

/**
 * wocky_g_value_slice_new_double:
 * @d: a number
 *
 * Slice-allocate and initialize a #GValue. This function is convenient to
 * use when constructing hash tables from string to #GValue, for example.
 *
 * Returns: a #GValue of type %G_TYPE_DOUBLE with value @n, to be freed with
 * wocky_g_value_slice_free() or g_slice_free()
 *
 * Since: 0.7.27
 */
GValue *
wocky_g_value_slice_new_double (double n)
{
  GValue *v = wocky_g_value_slice_new (G_TYPE_DOUBLE);

  g_value_set_double (v, n);
  return v;
}

/**
 * wocky_g_value_slice_new_string:
 * @string: a string to be copied into the value
 *
 * Slice-allocate and initialize a #GValue. This function is convenient to
 * use when constructing hash tables from string to #GValue, for example.
 *
 * Returns: a #GValue of type %G_TYPE_STRING whose value is a copy of @string,
 * to be freed with wocky_g_value_slice_free() or g_slice_free()
 *
 * Since: 0.7.27
 */
GValue *
wocky_g_value_slice_new_string (const gchar *string)
{
  GValue *v = wocky_g_value_slice_new (G_TYPE_STRING);

  g_value_set_string (v, string);
  return v;
}

/**
 * wocky_g_value_slice_new_static_string:
 * @string: a static string which must remain valid forever, to be pointed to
 *  by the value
 *
 * Slice-allocate and initialize a #GValue. This function is convenient to
 * use when constructing hash tables from string to #GValue, for example.
 *
 * Returns: a #GValue of type %G_TYPE_STRING whose value is @string,
 * to be freed with wocky_g_value_slice_free() or g_slice_free()
 *
 * Since: 0.7.27
 */
GValue *
wocky_g_value_slice_new_static_string (const gchar *string)
{
  GValue *v = wocky_g_value_slice_new (G_TYPE_STRING);

  g_value_set_static_string (v, string);
  return v;
}

/**
 * wocky_g_value_slice_new_take_string:
 * @string: a string which will be freed with g_free() by the returned #GValue
 *  (the caller must own it before calling this function, but no longer owns
 *  it after this function returns)
 *
 * Slice-allocate and initialize a #GValue. This function is convenient to
 * use when constructing hash tables from string to #GValue, for example.
 *
 * Returns: a #GValue of type %G_TYPE_STRING whose value is @string,
 * to be freed with wocky_g_value_slice_free() or g_slice_free()
 *
 * Since: 0.7.27
 */
GValue *
wocky_g_value_slice_new_take_string (gchar *string)
{
  GValue *v = wocky_g_value_slice_new (G_TYPE_STRING);

  g_value_take_string (v, string);
  return v;
}

/**
 * wocky_g_value_slice_new_boxed:
 * @type: a boxed type
 * @p: a pointer of type @type, which will be copied
 *
 * Slice-allocate and initialize a #GValue. This function is convenient to
 * use when constructing hash tables from string to #GValue, for example.
 *
 * Returns: a #GValue of type @type whose value is a copy of @p,
 * to be freed with wocky_g_value_slice_free() or g_slice_free()
 *
 * Since: 0.7.27
 */
GValue *
wocky_g_value_slice_new_boxed (GType type,
                            gconstpointer p)
{
  GValue *v;

  g_return_val_if_fail (G_TYPE_FUNDAMENTAL (type) == G_TYPE_BOXED, NULL);
  v = wocky_g_value_slice_new (type);
  g_value_set_boxed (v, p);
  return v;
}

/**
 * wocky_g_value_slice_new_static_boxed:
 * @type: a boxed type
 * @p: a pointer of type @type, which must remain valid forever
 *
 * Slice-allocate and initialize a #GValue. This function is convenient to
 * use when constructing hash tables from string to #GValue, for example.
 *
 * Returns: a #GValue of type @type whose value is @p,
 * to be freed with wocky_g_value_slice_free() or g_slice_free()
 *
 * Since: 0.7.27
 */
GValue *
wocky_g_value_slice_new_static_boxed (GType type,
                                   gconstpointer p)
{
  GValue *v;

  g_return_val_if_fail (G_TYPE_FUNDAMENTAL (type) == G_TYPE_BOXED, NULL);
  v = wocky_g_value_slice_new (type);
  g_value_set_static_boxed (v, p);
  return v;
}

/**
 * wocky_g_value_slice_new_take_boxed:
 * @type: a boxed type
 * @p: a pointer of type @type which will be freed with g_boxed_free() by the
 *  returned #GValue (the caller must own it before calling this function, but
 *  no longer owns it after this function returns)
 *
 * Slice-allocate and initialize a #GValue. This function is convenient to
 * use when constructing hash tables from string to #GValue, for example.
 *
 * Returns: a #GValue of type @type whose value is @p,
 * to be freed with wocky_g_value_slice_free() or g_slice_free()
 *
 * Since: 0.7.27
 */
GValue *
wocky_g_value_slice_new_take_boxed (GType type,
                                 gpointer p)
{
  GValue *v;

  g_return_val_if_fail (G_TYPE_FUNDAMENTAL (type) == G_TYPE_BOXED, NULL);
  v = wocky_g_value_slice_new (type);
  g_value_take_boxed (v, p);
  return v;
}

/**
 * wocky_g_value_slice_free:
 * @value: A GValue which was allocated with the g_slice API
 *
 * Unset and free a slice-allocated GValue.
 *
 * <literal>(GDestroyNotify) wocky_g_value_slice_free</literal> can be used
 * as a destructor for values in a #GHashTable, for example.
 */

void
wocky_g_value_slice_free (GValue *value)
{
  g_value_unset (value);
  g_slice_free (GValue, value);
}


/**
 * wocky_g_value_slice_dup:
 * @value: A GValue
 *
 * <!-- 'Returns' says it all -->
 *
 * Returns: a newly allocated copy of @value, to be freed with
 * wocky_g_value_slice_free() or g_slice_free().
 * Since: 0.5.14
 */
GValue *
wocky_g_value_slice_dup (const GValue *value)
{
  GValue *ret = wocky_g_value_slice_new (G_VALUE_TYPE (value));

  g_value_copy (value, ret);
  return ret;
}

/**
 * wocky_enum_from_nick:
 * @enum_type: the GType of a subtype of GEnum
 * @nick: a non-%NULL string purporting to be the nickname of a value of
 *        @enum_type
 * @value: the address at which to store the value of @enum_type corresponding
 *         to @nick if this functions returns %TRUE; if this function returns
 *         %FALSE, this variable will be left untouched.
 *
 * <!-- -->
 *
 * Returns: %TRUE if @nick is a member of @enum_type, or %FALSE otherwise
 */
gboolean
wocky_enum_from_nick (
    GType enum_type,
    const gchar *nick,
    gint *value)
{
  GEnumClass *klass = g_type_class_ref (enum_type);
  GEnumValue *enum_value;

  g_return_val_if_fail (klass != NULL, FALSE);
  g_return_val_if_fail (value != NULL, FALSE);

  enum_value = g_enum_get_value_by_nick (klass, nick);
  g_type_class_unref (klass);

  if (enum_value != NULL)
    {
      *value = enum_value->value;
      return TRUE;
    }
  else
    {
      return FALSE;
    }
}

/**
 * wocky_enum_to_nick:
 * @enum_type: the GType of a subtype of GEnum
 * @value: a value of @enum_type
 *
 * <!-- -->
 *
 * Returns: the nickname of @value, or %NULL if it is not, in fact, a value of
 * @enum_type
 */
const gchar *
wocky_enum_to_nick (
    GType enum_type,
    gint value)
{
  GEnumClass *klass = g_type_class_ref (enum_type);
  GEnumValue *enum_value;

  g_return_val_if_fail (klass != NULL, NULL);

  enum_value = g_enum_get_value (klass, value);
  g_type_class_unref (klass);

  if (enum_value != NULL)
    return enum_value->value_nick;
  else
    return NULL;
}

/**
 * wocky_absolutize_path:
 * @path: an absolute or relative path
 *
 * Return an absolute form of @path. This cleans up duplicate slashes, "." or
 * ".." path segments, etc., and prepends g_get_current_dir() if necessary, but
 * does not necessarily resolve symlinks.
 *
 * Returns: an absolute path which must be freed with g_free(), or possibly
 *  %NULL for invalid filenames
 */
gchar *
wocky_absolutize_path (const gchar *path)
{
  GFile *cwd, *absolute;
  gchar *cwd_str, *ret;

  cwd_str = g_get_current_dir ();
  cwd = g_file_new_for_path (cwd_str);
  g_free (cwd_str);

  if (cwd == NULL)
    return NULL;

  absolute = g_file_resolve_relative_path (cwd, path);

  if (absolute == NULL)
    {
      g_object_unref (cwd);
      return NULL;
    }

  ret = g_file_get_path (absolute);   /* possibly NULL */

  g_object_unref (cwd);
  g_object_unref (absolute);
  return ret;
}

GList *
wocky_list_deep_copy (GBoxedCopyFunc copy,
    const GList *items)
{
  GList *ret = NULL;
  const GList *l;

  g_return_val_if_fail (copy != NULL, NULL);

  for (l = items; l != NULL; l = l->next)
    ret = g_list_prepend (ret, copy (l->data));

  return g_list_reverse (ret);
}

GString *
wocky_g_string_dup (const GString *str)
{
  if (str == NULL)
    return NULL;

  return g_string_new_len (str->str, str->len);
}

void
wocky_g_string_free (GString *str)
{
  if (str != NULL)
    g_string_free (str, TRUE);
}