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
|
/*
* conn-presence - Gabble connection presence interface
* Copyright (C) 2005-2007 Collabora Ltd.
* Copyright (C) 2005-2007 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
*/
#include "config.h"
#include "conn-presence.h"
#include <string.h>
#include <telepathy-glib/dbus.h>
#include <telepathy-glib/presence-mixin.h>
#include <telepathy-glib/svc-connection.h>
#include <telepathy-glib/util.h>
#include <telepathy-glib/interfaces.h>
#define DEBUG_FLAG GABBLE_DEBUG_CONNECTION
#include "extensions/extensions.h" /* for Decloak */
#include "connection.h"
#include "debug.h"
#include "presence-cache.h"
#include "presence.h"
#include "roster.h"
static const TpPresenceStatusOptionalArgumentSpec gabble_status_arguments[] = {
{ "message", "s", NULL, NULL },
{ "priority", "n", NULL, NULL },
{ NULL, NULL, NULL, NULL }
};
/* order must match PresenceId enum in connection.h */
/* in increasing order of presence */
static const TpPresenceStatusSpec gabble_statuses[] = {
{ "offline", TP_CONNECTION_PRESENCE_TYPE_OFFLINE, FALSE,
gabble_status_arguments, NULL, NULL },
{ "unknown", TP_CONNECTION_PRESENCE_TYPE_UNKNOWN, FALSE,
gabble_status_arguments, NULL, NULL },
{ "error", TP_CONNECTION_PRESENCE_TYPE_ERROR, FALSE,
gabble_status_arguments, NULL, NULL },
{ "hidden", TP_CONNECTION_PRESENCE_TYPE_HIDDEN, TRUE, gabble_status_arguments,
NULL, NULL },
{ "xa", TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY, TRUE,
gabble_status_arguments, NULL, NULL },
{ "away", TP_CONNECTION_PRESENCE_TYPE_AWAY, TRUE, gabble_status_arguments,
NULL, NULL },
{ "dnd", TP_CONNECTION_PRESENCE_TYPE_BUSY, TRUE, gabble_status_arguments,
NULL, NULL },
{ "available", TP_CONNECTION_PRESENCE_TYPE_AVAILABLE, TRUE,
gabble_status_arguments, NULL, NULL },
{ "chat", TP_CONNECTION_PRESENCE_TYPE_AVAILABLE, TRUE,
gabble_status_arguments, NULL, NULL },
{ NULL, 0, FALSE, NULL, NULL, NULL }
};
static GHashTable *
construct_contact_statuses_cb (GObject *obj,
const GArray *contact_handles,
GError **error)
{
GabbleConnection *self = GABBLE_CONNECTION (obj);
TpBaseConnection *base = (TpBaseConnection *) self;
guint i;
TpHandle handle;
GHashTable *contact_statuses, *parameters;
TpPresenceStatus *contact_status;
GValue *message;
GabblePresence *presence;
GabblePresenceId status;
const gchar *status_message;
TpHandleRepoIface *handle_repo = tp_base_connection_get_handles (base,
TP_HANDLE_TYPE_CONTACT);
if (!tp_handles_are_valid (handle_repo, contact_handles, FALSE, error))
return NULL;
contact_statuses = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL,
(GDestroyNotify) tp_presence_status_free);
for (i = 0; i < contact_handles->len; i++)
{
handle = g_array_index (contact_handles, TpHandle, i);
if (handle == base->self_handle)
presence = self->self_presence;
else
presence = gabble_presence_cache_get (self->presence_cache, handle);
if (presence)
{
status = presence->status;
status_message = presence->status_message;
}
else
{
if (gabble_roster_handle_get_subscription (self->roster, handle)
& GABBLE_ROSTER_SUBSCRIPTION_FROM)
status = GABBLE_PRESENCE_OFFLINE;
else
status = GABBLE_PRESENCE_UNKNOWN;
status_message = NULL;
}
parameters = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
(GDestroyNotify) tp_g_value_slice_free);
if (status_message != NULL)
{
message = tp_g_value_slice_new (G_TYPE_STRING);
g_value_set_static_string (message, status_message);
g_hash_table_insert (parameters, "message", message);
}
contact_status = tp_presence_status_new (status, parameters);
g_hash_table_destroy (parameters);
g_hash_table_insert (contact_statuses, GUINT_TO_POINTER (handle),
contact_status);
}
return contact_statuses;
}
/**
* conn_presence_emit_presence_update:
* @self: A #GabbleConnection
* @contact_handles: A zero-terminated array of #TpHandle for
* the contacts to emit presence for
*
* Emits the Telepathy PresenceUpdate signal with the current
* stored presence information for the given contact.
*/
void
conn_presence_emit_presence_update (
GabbleConnection *self,
const GArray *contact_handles)
{
GHashTable *contact_statuses;
contact_statuses = construct_contact_statuses_cb ((GObject *) self,
contact_handles, NULL);
tp_presence_mixin_emit_presence_update ((GObject *) self, contact_statuses);
g_hash_table_destroy (contact_statuses);
}
/**
* emit_one_presence_update:
* Convenience function for calling conn_presence_emit_presence_update with
* one handle.
*/
static void
emit_one_presence_update (GabbleConnection *self,
TpHandle handle)
{
GArray *handles = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
g_array_insert_val (handles, 0, handle);
conn_presence_emit_presence_update (self, handles);
g_array_free (handles, TRUE);
}
static gboolean
set_own_status_cb (GObject *obj,
const TpPresenceStatus *status,
GError **error)
{
GabbleConnection *conn = GABBLE_CONNECTION (obj);
TpBaseConnection *base = (TpBaseConnection *) conn;
GabblePresenceId i = GABBLE_PRESENCE_AVAILABLE;
const gchar *message_str = NULL;
gchar *resource;
gint8 prio;
gboolean retval = TRUE;
g_object_get (conn,
"resource", &resource,
"priority", &prio,
NULL);
if (status)
{
GHashTable *args = status->optional_arguments;
GValue *message = NULL, *priority = NULL;
i = status->index;
/* Workaround for tp-glib not checking whether we support setting
* a particular status (can be removed once we depend on tp-glib
* with the check enabled). Assumes PresenceId value ordering. */
if (i < GABBLE_PRESENCE_HIDDEN)
{
g_set_error (error, TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
"Status '%s' can not be requested in this connection",
gabble_statuses[i].name);
retval = FALSE;
goto OUT;
}
if (args != NULL)
{
message = g_hash_table_lookup (args, "message");
priority = g_hash_table_lookup (args, "priority");
}
if (message)
{
if (!G_VALUE_HOLDS_STRING (message))
{
DEBUG ("got a status message which was not a string");
g_set_error (error, TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
"Status argument 'message' requires a string");
retval = FALSE;
goto OUT;
}
message_str = g_value_get_string (message);
}
if (priority)
{
if (!G_VALUE_HOLDS_INT (priority))
{
DEBUG ("got a priority value which was not a signed integer");
g_set_error (error, TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
"Status argument 'priority' requires a signed integer");
retval = FALSE;
goto OUT;
}
prio = CLAMP (g_value_get_int (priority), G_MININT8, G_MAXINT8);
}
}
if (gabble_presence_update (conn->self_presence, resource, i,
message_str, prio))
{
if (base->status == TP_CONNECTION_STATUS_CONNECTED)
{
emit_one_presence_update (conn, base->self_handle);
retval = _gabble_connection_signal_own_presence (conn, NULL, error);
}
else
{
retval = TRUE;
}
}
OUT:
g_free (resource);
return retval;
}
static void
connection_presences_updated_cb (
GabblePresenceCache *cache,
const GArray *handles,
gpointer user_data)
{
GabbleConnection *conn = GABBLE_CONNECTION (user_data);
conn_presence_emit_presence_update (conn, handles);
}
static void
connection_status_changed_cb (
GabbleConnection *conn,
TpConnectionStatus status,
TpConnectionStatusReason reason,
gpointer user_data)
{
TpBaseConnection *base = (TpBaseConnection *) conn;
if (status == TP_CONNECTION_STATUS_CONNECTED)
emit_one_presence_update (conn, base->self_handle);
}
static gboolean
status_available_cb (GObject *obj, guint status)
{
GabbleConnection *conn = GABBLE_CONNECTION (obj);
TpBaseConnection *base = (TpBaseConnection *) conn;
/* If we've gone online and found that the server doesn't support invisible,
* reject it.
*/
if (base->status == TP_CONNECTION_STATUS_CONNECTED &&
gabble_statuses[status].presence_type == TP_CONNECTION_PRESENCE_TYPE_HIDDEN &&
(conn->features & GABBLE_CONNECTION_FEATURES_PRESENCE_INVISIBLE) == 0)
return FALSE;
else
return TRUE;
}
void
conn_presence_class_init (GabbleConnectionClass *klass)
{
tp_presence_mixin_class_init ((GObjectClass *) klass,
G_STRUCT_OFFSET (GabbleConnectionClass, presence_class),
status_available_cb, construct_contact_statuses_cb,
set_own_status_cb, gabble_statuses);
tp_presence_mixin_simple_presence_init_dbus_properties (
(GObjectClass *) klass);
}
void
conn_presence_init (GabbleConnection *conn)
{
g_signal_connect (conn->presence_cache, "presences-updated",
G_CALLBACK (connection_presences_updated_cb), conn);
g_signal_connect (conn, "status-changed",
G_CALLBACK (connection_status_changed_cb), conn);
tp_presence_mixin_init ((GObject *) conn,
G_STRUCT_OFFSET (GabbleConnection, presence));
tp_presence_mixin_simple_presence_register_with_contacts_mixin (
G_OBJECT (conn));
}
void
conn_presence_finalize (GabbleConnection *conn)
{
tp_presence_mixin_finalize ((GObject *) conn);
}
void
conn_presence_iface_init (gpointer g_iface, gpointer iface_data)
{
tp_presence_mixin_iface_init (g_iface, iface_data);
}
static void
conn_presence_send_directed_presence (
GabbleSvcConnectionInterfaceGabbleDecloak *conn,
guint contact,
gboolean full,
DBusGMethodInvocation *context)
{
GabbleConnection *self = GABBLE_CONNECTION (conn);
TpBaseConnection *base = TP_BASE_CONNECTION (conn);
TpHandleRepoIface *contact_handles = tp_base_connection_get_handles (base,
TP_HANDLE_TYPE_CONTACT);
const gchar *jid = tp_handle_inspect (contact_handles, contact);
gboolean ok;
GError *error = NULL;
g_return_if_fail (jid != NULL);
/* We don't strictly respect @full - we'll always send full presence to
* people we think ought to be receiving it anyway, because if we didn't,
* you could confuse them by sending directed presence that was less
* informative than the broadcast presence they already saw. */
if (full || gabble_connection_visible_to (self, contact))
{
ok = _gabble_connection_signal_own_presence (self, jid, &error);
}
else
{
ok = gabble_connection_send_capabilities (self, jid, &error);
}
if (ok)
{
gabble_svc_connection_interface_gabble_decloak_return_from_send_directed_presence (context);
}
else
{
dbus_g_method_return_error (context, error);
g_error_free (error);
}
}
void
conn_decloak_emit_requested (GabbleConnection *conn,
TpHandle contact,
const gchar *reason,
gboolean decloaked)
{
gabble_svc_connection_interface_gabble_decloak_emit_decloak_requested (conn,
contact, reason, decloaked);
}
void
conn_decloak_iface_init (gpointer g_iface,
gpointer iface_data)
{
#define IMPLEMENT(x) \
gabble_svc_connection_interface_gabble_decloak_implement_##x (\
g_iface, conn_presence_##x)
IMPLEMENT (send_directed_presence);
#undef IMPLEMENT
}
|