summaryrefslogtreecommitdiff
path: root/src/disco.c
blob: 764df14607255896449ba4f15cb526cb9926b0bd (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
/*
 * disco.c - Source for Salut service discovery
 *
 * Copyright (C) 2006-2008 Collabora Ltd.
 * Copyright (C) 2006-2008 Nokia Corporation
 *
 * 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
 *
 * -- LET'S DISCO!!!  \o/ \o_ _o/ /\o/\ _/o/- -\o\_ --
 */

#include "config.h"
#include "disco.h"

#include <string.h>

#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>

#include <telepathy-glib/telepathy-glib.h>

#define DEBUG_FLAG DEBUG_DISCO

#include "debug.h"
#include "capabilities.h"
#include "caps-hash.h"
#include "connection.h"

/* Properties */
enum
{
  PROP_CONNECTION = 1,
  LAST_PROPERTY
};

G_DEFINE_TYPE(SalutDisco, salut_disco, G_TYPE_OBJECT);

struct _SalutDiscoPrivate
{
  SalutConnection *connection;

  guint caps_req_stanza_id;
  guint caps_req_stanza_id_broken;

  GList *requests;

  gboolean dispose_has_run;
};

struct _SalutDiscoRequest
{
  SalutDisco *disco;

  SalutDiscoType type;
  SalutContact *contact;

  GCancellable *cancellable;

  /* uri as in XEP-0115 */
  gchar *node;

  SalutDiscoCb callback;
  gpointer user_data;
  GObject *bound_object;
};

GQuark
salut_disco_error_quark (void)
{
  static GQuark quark = 0;
  if (!quark)
    quark = g_quark_from_static_string ("disco-error");
  return quark;
}

static void
salut_disco_init (SalutDisco *obj)
{
  SalutDiscoPrivate *priv =
     G_TYPE_INSTANCE_GET_PRIVATE (obj, SALUT_TYPE_DISCO, SalutDiscoPrivate);
  obj->priv = priv;
}

static void salut_disco_constructed (GObject *obj);
static void salut_disco_set_property (GObject *object, guint property_id,
    const GValue *value, GParamSpec *pspec);
static void salut_disco_get_property (GObject *object, guint property_id,
    GValue *value, GParamSpec *pspec);
static void salut_disco_dispose (GObject *object);

static const char *
disco_type_to_xmlns (SalutDiscoType type)
{
  switch (type) {
    case SALUT_DISCO_TYPE_INFO:
      return WOCKY_NS_DISCO_INFO;
    case SALUT_DISCO_TYPE_ITEMS:
      return WOCKY_NS_DISCO_ITEMS;
    default:
      g_assert_not_reached ();
  }

  return NULL;
}

static void notify_delete_request (gpointer data, GObject *obj);

static void
delete_request (SalutDiscoRequest *request)
{
  SalutDisco *disco = request->disco;
  SalutDiscoPrivate *priv;

  /* if we've already disposed the SalutDisco object, we should not
   * mess around with anything referenced by that. */
  if (disco != NULL)
    {
      g_assert (SALUT_IS_DISCO (disco));

      priv = disco->priv;

      g_assert (NULL != g_list_find (priv->requests, request));

      priv->requests = g_list_remove (priv->requests, request);
    }

  if (NULL != request->bound_object)
    {
      g_object_weak_unref (request->bound_object, notify_delete_request,
          request);
    }

  g_object_unref (request->contact);
  g_object_unref (request->cancellable);
  g_free (request->node);
  g_slice_free (SalutDiscoRequest, request);
}

static void
notify_delete_request (gpointer data, GObject *obj)
{
  SalutDiscoRequest *request = (SalutDiscoRequest *) data;
  request->bound_object = NULL;

  /* This will cause the callback to be called with the cancelled
     error. */
  g_cancellable_cancel (request->cancellable);
}

static void
salut_disco_class_init (SalutDiscoClass *salut_disco_class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (salut_disco_class);
  GParamSpec *param_spec;

  g_type_class_add_private (salut_disco_class, sizeof (SalutDiscoPrivate));

  object_class->constructed = salut_disco_constructed;

  object_class->get_property = salut_disco_get_property;
  object_class->set_property = salut_disco_set_property;

  object_class->dispose = salut_disco_dispose;

  param_spec = g_param_spec_object ("connection", "SalutConnection object",
                                    "Salut connection object that owns this "
                                    "XMPP Discovery object.",
                                    SALUT_TYPE_CONNECTION,
                                    G_PARAM_CONSTRUCT_ONLY |
                                    G_PARAM_READWRITE |
                                    G_PARAM_STATIC_NICK |
                                    G_PARAM_STATIC_BLURB);
  g_object_class_install_property (object_class, PROP_CONNECTION, param_spec);
}

static void
salut_disco_get_property (GObject    *object,
                                guint       property_id,
                                GValue     *value,
                                GParamSpec *pspec)
{
  SalutDisco *self = SALUT_DISCO (object);
  SalutDiscoPrivate *priv = self->priv;

  switch (property_id)
    {
      case PROP_CONNECTION:
        g_value_set_object (value, priv->connection);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
        break;
    }
}

static void
salut_disco_set_property (GObject     *object,
                           guint        property_id,
                           const GValue *value,
                           GParamSpec   *pspec)
{
  SalutDisco *self = SALUT_DISCO (object);
  SalutDiscoPrivate *priv = self->priv;

  switch (property_id)
    {
      case PROP_CONNECTION:
        priv->connection = g_value_get_object (value);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
        break;
    }
}

static void
send_item_not_found (WockyPorter *porter,
    WockyStanza *iq,
    const gchar *node)
{
  WockyStanza *result;

  /* Return <item-not-found>. It is possible that the remote contact
   * requested an old version (old hash) of our capabilities. In the
   * meantime, it will have gotten a new hash, and query the new hash
   * anyway. */
  result = wocky_stanza_build_iq_error (iq,
      '(', "query",
        ':', WOCKY_NS_DISCO_INFO,
        '@', "node", node,
        '(', "error",
          '@', "type", "cancel",
          '(', "item-not-found",
            ':', WOCKY_XMPP_NS_STANZAS,
          ')',
        ')',
      ')',
      NULL);

  DEBUG ("sending item-not-found as disco response");

  wocky_porter_send_async (porter, result, NULL, NULL, NULL);

  g_object_unref (result);
}

static void
add_feature_foreach (gpointer ns,
    gpointer result_query)
{
  WockyNode *feature_node;

  feature_node = wocky_node_add_child (result_query, "feature");
  wocky_node_set_attribute (feature_node, "var", ns);
}

static void
add_data_form_foreach (gpointer data,
    gpointer user_data)
{
  WockyDataForm *form = data;
  WockyNode *query = user_data;

  wocky_data_form_add_to_node (form, query);
}

static gboolean
caps_req_stanza_callback (WockyPorter *porter,
    WockyStanza *stanza,
    gpointer user_data)
{
  SalutDisco *self = SALUT_DISCO (user_data);
  SalutDiscoPrivate *priv = self->priv;
  WockyNode *iq, *result_iq, *query, *result_query;
  const gchar *node;
  const gchar *suffix;
  SalutSelf *salut_self;
  WockyStanza *result;
  const GabbleCapabilitySet *caps;
  const GPtrArray *data_forms;

  iq = wocky_stanza_get_top_node (stanza);
  query = wocky_node_get_child_ns (iq, "query", WOCKY_NS_DISCO_INFO);
  g_assert (query != NULL);

  node = wocky_node_get_attribute (query, "node");
  if (node == NULL)
    {
      send_item_not_found (porter, stanza, "");
      return TRUE;
    }

  if (!g_str_has_prefix (node, WOCKY_TELEPATHY_NS_CAPS "#"))
    {
      send_item_not_found (porter, stanza, node);
      return TRUE;
    }
  else
    {
      suffix = node + strlen (WOCKY_TELEPATHY_NS_CAPS) + 1;
    }

  DEBUG ("got disco request for node %s", node);

  g_object_get (priv->connection, "self", &salut_self, NULL);
  /* Salut only supports XEP-0115 version 1.5. Bundles from old version 1.3 are
   * not implemented. */

  if (salut_self == NULL)
    return TRUE;

  if (tp_strdiff (suffix, salut_self->ver))
    {
      g_object_unref (salut_self);
      return TRUE;
    }

  /* Every entity MUST have at least one identity (XEP-0030). Salut publishs
   * one identity. If you change the identity here, you also need to change
   * caps_hash_compute_from_self_presence(). */
  result = wocky_stanza_build_iq_result (stanza,
      '(', "query",
        ':', WOCKY_NS_DISCO_INFO,
        '@', "node", node,
        '(', "identity",
          '@', "category", "client",
          '@', "name", PACKAGE_STRING,
          /* FIXME: maybe we should add a connection property allowing to
           * set the type attribute instead of hardcoding "pc". */
          '@', "type", "pc",
        ')',
      ')',
      NULL);

  result_iq = wocky_stanza_get_top_node (result);
  result_query = wocky_node_get_child_ns (result_iq, "query", NULL);

  caps = salut_self_get_caps (salut_self);
  gabble_capability_set_foreach (caps, add_feature_foreach, result_query);

  data_forms = wocky_xep_0115_capabilities_get_data_forms (
      WOCKY_XEP_0115_CAPABILITIES (salut_self));
  g_ptr_array_foreach ((GPtrArray *) data_forms, add_data_form_foreach,
      result_query);

  DEBUG ("sending disco response");

  wocky_porter_send_async (porter, result, NULL, NULL, NULL);

  g_object_unref (result);
  g_object_unref (salut_self);

  return TRUE;
}

static void
salut_disco_constructed (GObject *obj)
{
  SalutDisco *disco = SALUT_DISCO (obj);
  SalutDiscoPrivate *priv = disco->priv;
  WockyPorter *porter = priv->connection->porter;

  if (G_OBJECT_CLASS (salut_disco_parent_class)->constructed != NULL)
    G_OBJECT_CLASS (salut_disco_parent_class)->constructed (obj);

  priv->requests = NULL;

  /* receive discovery requests */
  priv->caps_req_stanza_id = wocky_porter_register_handler_from_anyone (porter,
      WOCKY_STANZA_TYPE_IQ, WOCKY_STANZA_SUB_TYPE_GET,
      WOCKY_PORTER_HANDLER_PRIORITY_NORMAL,
      caps_req_stanza_callback, obj,
      '(', "query",
        ':', WOCKY_NS_DISCO_INFO,
      ')', NULL);

  /* Salut used to send disco requests with <iq type='set' ...> so we
   * should listen for that too. */
  priv->caps_req_stanza_id_broken =
    wocky_porter_register_handler_from_anyone (porter,
        WOCKY_STANZA_TYPE_IQ, WOCKY_STANZA_SUB_TYPE_SET,
        WOCKY_PORTER_HANDLER_PRIORITY_NORMAL,
        caps_req_stanza_callback, obj,
        '(', "query",
          ':', WOCKY_NS_DISCO_INFO,
        ')', NULL);
}

static void
salut_disco_dispose (GObject *object)
{
  SalutDisco *self = SALUT_DISCO (object);
  SalutDiscoPrivate *priv = self->priv;
  WockyPorter *porter = priv->connection->porter;
  GList *l;

  if (priv->dispose_has_run)
    return;

  priv->dispose_has_run = TRUE;

  DEBUG ("dispose called");

  wocky_porter_unregister_handler (porter, priv->caps_req_stanza_id);
  priv->caps_req_stanza_id = 0;

  wocky_porter_unregister_handler (porter,
      priv->caps_req_stanza_id_broken);
  priv->caps_req_stanza_id_broken = 0;

  for (l = priv->requests; l != NULL; l = l->next)
    {
      SalutDiscoRequest *r = l->data;

      r->disco = NULL;
      g_cancellable_cancel (r->cancellable);
    }

  g_list_free (priv->requests);

  if (G_OBJECT_CLASS (salut_disco_parent_class)->dispose)
    G_OBJECT_CLASS (salut_disco_parent_class)->dispose (object);
}

/**
 * salut_disco_new:
 * @conn: The #SalutConnection to use for service discovery
 *
 * Creates an object to use for Jabber service discovery (DISCO)
 * There should be one of these per connection
 */
SalutDisco *
salut_disco_new (SalutConnection *connection)
{
  SalutDisco *disco;

  g_return_val_if_fail (SALUT_IS_CONNECTION (connection), NULL);

  disco = SALUT_DISCO (g_object_new (SALUT_TYPE_DISCO,
        "connection", connection,
        NULL));

  return disco;
}

static void
disco_request_sent_cb (GObject *source_object,
    GAsyncResult *result,
    gpointer user_data)
{
  WockyPorter *porter = WOCKY_PORTER (source_object);
  GError *error = NULL;
  WockyStanza *reply;
  WockyNode *reply_node, *query_node = NULL;
  SalutDiscoRequest *request = user_data;

  reply = wocky_porter_send_iq_finish (porter, result, &error);

  if (reply == NULL)
    {
      DEBUG ("error: %s", error->message);
      goto out;
    }

  if (wocky_stanza_extract_errors (reply, NULL, &error, NULL, NULL))
    goto out;

  reply_node = wocky_stanza_get_top_node (reply);
  query_node = wocky_node_get_child_ns (reply_node, "query",
      disco_type_to_xmlns (request->type));

  if (query_node == NULL)
    {
      error = g_error_new (SALUT_DISCO_ERROR, SALUT_DISCO_ERROR_UNKNOWN,
          "disco response contained no <query> node");
      goto out;
    }

out:
  /* the cancellable is cancelled if the object given to
   * salut_disco_request is disposed, which claims to not call the
   * callback, so let's not. */
  if (!g_cancellable_is_cancelled (request->cancellable))
    {
      request->callback (request->disco, request, request->contact, request->node,
          query_node, error, request->user_data);
    }

  delete_request (request);

  if (error != NULL)
    g_clear_error (&error);
}

/**
 * salut_disco_request:
 * @self: #SalutDisco object to use for request
 * @type: type of request
 * @jid: Jabber ID to request on
 * @node: node to request on @jid, or NULL
 * @callback: #SalutDiscoCb to call on request fullfilment
 * @object: GObject to bind request to. the callback will not be
 *          called if this object has been unrefed. NULL if not needed
 * @error: #GError to return a telepathy error in if unable to make
 *         request, NULL if unneeded.
 *
 * Make a disco request on the given jid.
 */
SalutDiscoRequest *
salut_disco_request (SalutDisco *self,
    SalutDiscoType type,
    SalutContact *contact,
    const char *node,
    SalutDiscoCb callback,
    gpointer user_data,
    GObject *object,
    GError **error)
{
  SalutDiscoPrivate *priv = self->priv;
  SalutDiscoRequest *request;
  WockyPorter *porter = priv->connection->porter;
  WockyStanza *stanza;

  g_assert (node != NULL);
  g_assert (strlen (node) > 0);

  request = g_slice_new0 (SalutDiscoRequest);
  request->disco = self;
  request->type = type;
  request->contact = g_object_ref (contact);
  if (node)
    request->node = g_strdup (node);
  request->callback = callback;
  request->user_data = user_data;
  request->bound_object = object;
  request->cancellable = g_cancellable_new ();

  if (NULL != object)
    g_object_weak_ref (object, notify_delete_request, request);

  DEBUG ("Creating disco request %p for %s",
           request, request->contact->name);

  stanza = wocky_stanza_build_to_contact (WOCKY_STANZA_TYPE_IQ,
      WOCKY_STANZA_SUB_TYPE_GET,
      NULL, WOCKY_CONTACT (contact),
      '(', "query",
        ':', disco_type_to_xmlns (request->type),
        '@', "node", request->node,
      ')',
      NULL);

  wocky_porter_send_iq_async (porter, stanza, request->cancellable,
      disco_request_sent_cb, request);

  priv->requests = g_list_append (priv->requests,
      request);

  g_object_unref (stanza);

  return request;
}

void
salut_disco_cancel_request (SalutDisco *disco,
    SalutDiscoRequest *request)
{
  SalutDiscoPrivate *priv;

  g_return_if_fail (SALUT_IS_DISCO (disco));
  g_return_if_fail (NULL != request);

  priv = disco->priv;

  g_return_if_fail (NULL != g_list_find (priv->requests, request));

  g_cancellable_cancel (request->cancellable);
}