telepathy: use "server-message" for connection error
[siplcs.git] / src / telepathy / telepathy-connection.c
blob3a083dc50c7d777ab3a2f5decc0e46b512b5535d
1 /**
2 * @file telepathy-connection.c
4 * pidgin-sipe
6 * Copyright (C) 2012 SIPE Project <http://sipe.sourceforge.net/>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
27 #include <string.h>
29 #include <glib-object.h>
30 #include <telepathy-glib/base-connection.h>
31 #include <telepathy-glib/base-protocol.h>
32 #include <telepathy-glib/handle-repo-dynamic.h>
33 #include <telepathy-glib/simple-password-manager.h>
34 #include <telepathy-glib/telepathy-glib.h>
36 #include "sipe-backend.h"
37 #include "sipe-common.h"
38 #include "sipe-core.h"
40 #include "telepathy-private.h"
42 G_BEGIN_DECLS
44 * Connection class - data structures
46 typedef struct _SipeConnectionClass {
47 TpBaseConnectionClass parent_class;
48 } SipeConnectionClass;
50 typedef struct _SipeConnection {
51 TpBaseConnection parent;
53 TpSimplePasswordManager *password_manager;
55 struct sipe_backend_private private;
56 gchar *account;
57 gchar *login;
58 gchar *password;
59 gchar *server;
60 gchar *port;
61 guint transport;
62 gchar *user_agent;
63 gchar *authentication;
64 gboolean is_disconnecting;
65 } SipeConnection;
67 #define SIPE_PUBLIC_TO_CONNECTION sipe_public->backend_private->connection
70 * Connection class - type macros
72 static GType sipe_connection_get_type(void) G_GNUC_CONST;
73 #define SIPE_TYPE_CONNECTION \
74 (sipe_connection_get_type())
75 #define SIPE_CONNECTION(obj) \
76 (G_TYPE_CHECK_INSTANCE_CAST((obj), SIPE_TYPE_CONNECTION, \
77 SipeConnection))
78 G_END_DECLS
81 * Connection class - type definition
83 G_DEFINE_TYPE(SipeConnection,
84 sipe_connection,
85 TP_TYPE_BASE_CONNECTION)
88 * Connection class - instance methods
90 static gchar *normalize_contact(SIPE_UNUSED_PARAMETER TpHandleRepoIface *repo,
91 const gchar *id,
92 SIPE_UNUSED_PARAMETER gpointer context,
93 GError **error)
95 return(sipe_telepathy_protocol_normalize_contact(NULL, id, error));
98 static void create_handle_repos(SIPE_UNUSED_PARAMETER TpBaseConnection *conn,
99 TpHandleRepoIface *repos[NUM_TP_HANDLE_TYPES])
101 repos[TP_HANDLE_TYPE_CONTACT] = tp_dynamic_handle_repo_new(TP_HANDLE_TYPE_CONTACT,
102 normalize_contact,
103 NULL);
106 static gboolean connect_to_core(SipeConnection *self,
107 GError **error)
109 gchar *login_domain = NULL;
110 gchar *login_account = NULL;
111 struct sipe_core_public *sipe_public;
112 const gchar *errmsg;
114 /* login name specified? */
115 if (self->login && strlen(self->login)) {
116 /* Allowed domain-account separators are / or \ */
117 gchar **domain_user = g_strsplit_set(self->login, "/\\", 2);
118 gboolean has_domain = domain_user[1] != NULL;
119 SIPE_DEBUG_INFO("connect_to_core: login '%s'", self->login);
120 login_domain = has_domain ? g_strdup(domain_user[0]) : NULL;
121 login_account = g_strdup(domain_user[has_domain ? 1 : 0]);
122 SIPE_DEBUG_INFO("connect_to_core: auth domain '%s' user '%s'",
123 login_domain ? login_domain : "",
124 login_account);
125 g_strfreev(domain_user);
128 sipe_public = sipe_core_allocate(self->account,
129 login_domain, login_account,
130 self->password,
131 NULL, /* @TODO: email */
132 NULL, /* @TODO: email_url */
133 &errmsg);
134 g_free(login_domain);
135 g_free(login_account);
137 SIPE_DEBUG_INFO("connect_to_core: created %p", sipe_public);
139 if (sipe_public) {
140 struct sipe_backend_private *telepathy_private = &self->private;
142 /* initialize backend private data */
143 sipe_public->backend_private = telepathy_private;
144 telepathy_private->public = sipe_public;
145 telepathy_private->connection = self;
146 telepathy_private->transport = NULL;
147 telepathy_private->ipaddress = NULL;
149 /* map option list to flags - default is NTLM */
150 SIPE_CORE_FLAG_UNSET(KRB5);
151 SIPE_CORE_FLAG_UNSET(TLS_DSK);
152 #ifdef HAVE_LIBKRB5
153 if (sipe_strequal(self->authentication, "krb5")) {
154 SIPE_DEBUG_INFO_NOFORMAT("connect_to_core: KRB5 selected");
155 SIPE_CORE_FLAG_SET(KRB5);
156 } else
157 #endif
158 if (sipe_strequal(self->authentication, "tls-dsk")) {
159 SIPE_DEBUG_INFO_NOFORMAT("connect_to_core: TLS-DSK selected");
160 SIPE_CORE_FLAG_SET(TLS_DSK);
163 /* @TODO: add parameter for SSO */
164 SIPE_CORE_FLAG_UNSET(SSO);
166 sipe_core_transport_sip_connect(sipe_public,
167 self->transport,
168 self->server,
169 self->port);
171 return(TRUE);
172 } else {
173 g_set_error_literal(error, TP_ERROR, TP_ERROR_INVALID_ARGUMENT,
174 errmsg);
175 return(FALSE);
179 static void password_manager_cb(GObject *source,
180 GAsyncResult *result,
181 gpointer data)
183 SipeConnection *self = data;
184 TpBaseConnection *base = TP_BASE_CONNECTION(self);
185 GError *error = NULL;
186 const GString *password = tp_simple_password_manager_prompt_finish(
187 TP_SIMPLE_PASSWORD_MANAGER(source),
188 result,
189 &error);
191 if (password == NULL) {
192 SIPE_DEBUG_ERROR("password_manager_cb: failed: %s",
193 error ? error->message : "UNKNOWN");
195 if (base->status != TP_CONNECTION_STATUS_DISCONNECTED) {
196 tp_base_connection_disconnect_with_dbus_error(base,
197 error ? tp_error_get_dbus_name(error->code) : "",
198 NULL,
199 TP_CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED);
201 g_error_free(error);
202 } else {
204 g_free(self->password);
205 self->password = g_strdup(password->str);
207 if (!connect_to_core(self, &error)) {
208 if (base->status != TP_CONNECTION_STATUS_DISCONNECTED) {
209 tp_base_connection_disconnect_with_dbus_error(base,
210 tp_error_get_dbus_name(error->code),
211 NULL,
212 TP_CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED);
214 g_error_free(error);
219 static gboolean start_connecting(TpBaseConnection *base,
220 GError **error)
222 SipeConnection *self = SIPE_CONNECTION(base);
223 gboolean rc = TRUE;
224 gchar *uri = sipe_telepathy_protocol_normalize_contact(NULL,
225 self->account,
226 error);
228 SIPE_DEBUG_INFO_NOFORMAT("SipeConnection::start_connecting");
230 /* set up mandatory self-handle */
231 if (uri) {
232 base->self_handle = tp_handle_ensure(tp_base_connection_get_handles(base,
233 TP_HANDLE_TYPE_CONTACT),
234 uri,
235 NULL,
236 error);
237 g_free(uri);
238 if (!base->self_handle) {
239 SIPE_DEBUG_ERROR("SipeConnection::start_connecting: self handle creation failed: %s",
240 (*error)->message);
241 return(FALSE);
243 } else {
244 SIPE_DEBUG_ERROR("SipeConnection::start_connecting: %s",
245 (*error)->message);
246 return(FALSE);
249 tp_base_connection_change_status(base, TP_CONNECTION_STATUS_CONNECTING,
250 TP_CONNECTION_STATUS_REASON_REQUESTED);
252 /* we need a password */
253 if (self->password)
254 rc = connect_to_core(self, error);
255 else {
256 /* @TODO: this doesn't work, i.e. UI doesn't show a dialog */
257 SIPE_DEBUG_INFO("SipeConnection::start_connecting: requesting password from user: %p",
258 self->password_manager);
259 tp_simple_password_manager_prompt_async(self->password_manager,
260 password_manager_cb,
261 self);
264 return(rc);
267 static gboolean disconnect_from_core(gpointer data)
269 TpBaseConnection *base = data;
270 SipeConnection *self = SIPE_CONNECTION(base);
271 struct sipe_backend_private *telepathy_private = &self->private;
272 struct sipe_core_public *sipe_public = telepathy_private->public;
274 SIPE_DEBUG_INFO("disconnect_from_core: %p", sipe_public);
276 if (sipe_public)
277 sipe_core_deallocate(sipe_public);
278 telepathy_private->public = NULL;
279 telepathy_private->transport = NULL;
281 g_free(telepathy_private->ipaddress);
282 telepathy_private->ipaddress = NULL;
284 SIPE_DEBUG_INFO_NOFORMAT("disconnect_from_core: core deallocated");
286 /* now it is OK to destroy the connection object */
287 tp_base_connection_finish_shutdown(base);
289 return(FALSE);
292 static void shut_down(TpBaseConnection *base)
294 SIPE_DEBUG_INFO_NOFORMAT("SipeConnection::shut_down");
296 /* this can be called synchronously, defer destruction */
297 g_idle_add(disconnect_from_core, base);
300 static GPtrArray *create_channel_managers(TpBaseConnection *base)
302 SipeConnection *self = SIPE_CONNECTION(base);
303 GPtrArray *channel_managers = g_ptr_array_new();
305 SIPE_DEBUG_INFO_NOFORMAT("SipeConnection::create_channel_managers");
307 self->password_manager = tp_simple_password_manager_new(base);
308 g_ptr_array_add(channel_managers, self->password_manager);
309 SIPE_DEBUG_INFO("SipeConnection::create_channel_managers: password %p",
310 self->password_manager);
312 return(channel_managers);
315 static void sipe_connection_finalize(GObject *object)
317 SipeConnection *self = SIPE_CONNECTION(object);
319 SIPE_DEBUG_INFO_NOFORMAT("SipeConnection::finalize");
321 g_free(self->authentication);
322 g_free(self->user_agent);
323 g_free(self->port);
324 g_free(self->server);
325 g_free(self->password);
326 g_free(self->login);
327 g_free(self->account);
329 G_OBJECT_CLASS(sipe_connection_parent_class)->finalize(object);
333 * Connection class - type implementation
335 static void sipe_connection_class_init(SipeConnectionClass *klass)
337 GObjectClass *object_class = G_OBJECT_CLASS(klass);
338 TpBaseConnectionClass *base_class = TP_BASE_CONNECTION_CLASS(klass);
340 SIPE_DEBUG_INFO_NOFORMAT("SipeConnection::class_init");
342 object_class->finalize = sipe_connection_finalize;
344 base_class->create_handle_repos = create_handle_repos;
345 base_class->start_connecting = start_connecting;
346 base_class->shut_down = shut_down;
347 base_class->create_channel_managers = create_channel_managers;
350 static void sipe_connection_init(SIPE_UNUSED_PARAMETER SipeConnection *self)
352 SIPE_DEBUG_INFO_NOFORMAT("SipeConnection::init");
355 /* create new connection object */
356 TpBaseConnection *sipe_telepathy_connection_new(TpBaseProtocol *protocol,
357 GHashTable *params,
358 SIPE_UNUSED_PARAMETER GError **error)
360 SipeConnection *conn = g_object_new(SIPE_TYPE_CONNECTION,
361 "protocol", tp_base_protocol_get_name(protocol),
362 NULL);
363 const gchar *value;
364 guint port;
365 gboolean valid;
367 SIPE_DEBUG_INFO_NOFORMAT("sipe_telepathy_connection_new");
369 /* initialize private fields */
370 conn->is_disconnecting = FALSE;
372 /* account & login are required fields */
373 conn->account = g_strdup(tp_asv_get_string(params, "account"));
374 conn->login = g_strdup(tp_asv_get_string(params, "login"));
376 /* password */
377 value = tp_asv_get_string(params, "password");
378 if (value && strlen(value))
379 conn->password = g_strdup(value);
380 else
381 conn->password = NULL;
383 /* server name */
384 value = tp_asv_get_string(params, "server");
385 if (value && strlen(value))
386 conn->server = g_strdup(value);
387 else
388 conn->server = NULL;
390 /* server port: core expects a string */
391 port = tp_asv_get_uint32(params, "port", &valid);
392 if (valid)
393 conn->port = g_strdup_printf("%d", port);
394 else
395 conn->port = NULL;
397 /* transport type */
398 value = tp_asv_get_string(params, "transport");
399 if (sipe_strequal(value, "auto")) {
400 conn->transport = conn->server ?
401 SIPE_TRANSPORT_TLS : SIPE_TRANSPORT_AUTO;
402 } else if (sipe_strequal(value, "tls")) {
403 conn->transport = SIPE_TRANSPORT_TLS;
404 } else {
405 conn->transport = SIPE_TRANSPORT_TCP;
408 /* User-Agent: override */
409 value = tp_asv_get_string(params, "useragent");
410 if (value && strlen(value))
411 conn->user_agent = g_strdup(value);
412 else
413 conn->user_agent = NULL;
415 /* authentication type */
416 value = tp_asv_get_string(params, "authentication");
417 if (value && strlen(value) && strcmp(value, "ntlm"))
418 conn->authentication = g_strdup(value);
419 else
420 conn->authentication = NULL; /* NTLM is default */
422 return(TP_BASE_CONNECTION(conn));
427 * Backend adaptor functions
429 void sipe_backend_connection_completed(struct sipe_core_public *sipe_public)
431 SipeConnection *self = SIPE_PUBLIC_TO_CONNECTION;
432 TpBaseConnection *base = TP_BASE_CONNECTION(self);
434 /* we are only allowed to do this once */
435 if (base->status != TP_CONNECTION_STATUS_CONNECTED)
436 tp_base_connection_change_status(base,
437 TP_CONNECTION_STATUS_CONNECTED,
438 TP_CONNECTION_STATUS_REASON_REQUESTED);
441 void sipe_backend_connection_error(struct sipe_core_public *sipe_public,
442 sipe_connection_error error,
443 const gchar *msg)
445 SipeConnection *self = SIPE_PUBLIC_TO_CONNECTION;
446 TpBaseConnection *base = TP_BASE_CONNECTION(self);
447 GHashTable *details = tp_asv_new("server-message", G_TYPE_STRING, msg,
448 NULL);
449 TpConnectionStatusReason reason;
450 const gchar *name;
452 self->is_disconnecting = TRUE;
454 switch (error) {
455 case SIPE_CONNECTION_ERROR_NETWORK:
456 reason = TP_CONNECTION_STATUS_REASON_NETWORK_ERROR;
457 if (base->status == TP_CONNECTION_STATUS_CONNECTING)
458 name = TP_ERROR_STR_CONNECTION_FAILED;
459 else
460 name = TP_ERROR_STR_CONNECTION_LOST;
461 break;
463 case SIPE_CONNECTION_ERROR_INVALID_USERNAME:
464 case SIPE_CONNECTION_ERROR_INVALID_SETTINGS:
465 case SIPE_CONNECTION_ERROR_AUTHENTICATION_FAILED:
466 case SIPE_CONNECTION_ERROR_AUTHENTICATION_IMPOSSIBLE:
467 /* copied from haze code. I agree there should be better ones */
468 reason = TP_CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED;
469 name = TP_ERROR_STR_AUTHENTICATION_FAILED;
470 break;
472 default:
473 reason = TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED;
474 name = TP_ERROR_STR_DISCONNECTED;
475 break;
478 SIPE_DEBUG_ERROR("sipe_backend_connection_error: %s (%s)", name, msg);
479 tp_base_connection_disconnect_with_dbus_error(base,
480 name,
481 details,
482 reason);
483 g_hash_table_unref(details);
486 gboolean sipe_backend_connection_is_disconnecting(struct sipe_core_public *sipe_public)
488 SipeConnection *self = SIPE_PUBLIC_TO_CONNECTION;
490 /* disconnect was requested or transport was already disconnected */
491 return(self->is_disconnecting ||
492 self->private.transport == NULL);
495 gboolean sipe_backend_connection_is_valid(struct sipe_core_public *sipe_public)
497 return(!sipe_backend_connection_is_disconnecting(sipe_public));
500 const gchar *sipe_backend_setting(struct sipe_core_public *sipe_public,
501 sipe_setting type)
503 SipeConnection *self = SIPE_PUBLIC_TO_CONNECTION;
504 const gchar *value;
506 switch (type) {
507 case SIPE_SETTING_USER_AGENT:
508 value = self->user_agent;
509 break;
510 default:
511 /* @TODO: update when settings are implemented */
512 value = NULL;
513 break;
516 return(value);
521 Local Variables:
522 mode: c
523 c-file-style: "bsd"
524 indent-tabs-mode: t
525 tab-width: 8
526 End: