2 * @file telepathy-connection.c
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
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"
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;
63 gchar
*authentication
;
64 gboolean is_disconnecting
;
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, \
81 * Connection class - type definition
83 G_DEFINE_TYPE(SipeConnection
,
85 TP_TYPE_BASE_CONNECTION
)
88 * Connection class - instance methods
90 static gchar
*normalize_contact(SIPE_UNUSED_PARAMETER TpHandleRepoIface
*repo
,
92 SIPE_UNUSED_PARAMETER gpointer context
,
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
,
106 static gboolean
connect_to_core(SipeConnection
*self
,
109 gchar
*login_domain
= NULL
;
110 gchar
*login_account
= NULL
;
111 struct sipe_core_public
*sipe_public
;
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
: "",
125 g_strfreev(domain_user
);
128 sipe_public
= sipe_core_allocate(self
->account
,
129 login_domain
, login_account
,
131 NULL
, /* @TODO: email */
132 NULL
, /* @TODO: email_url */
134 g_free(login_domain
);
135 g_free(login_account
);
137 SIPE_DEBUG_INFO("connect_to_core: created %p", 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
);
153 if (sipe_strequal(self
->authentication
, "krb5")) {
154 SIPE_DEBUG_INFO_NOFORMAT("connect_to_core: KRB5 selected");
155 SIPE_CORE_FLAG_SET(KRB5
);
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
,
173 g_set_error_literal(error
, TP_ERROR
, TP_ERROR_INVALID_ARGUMENT
,
179 static void password_manager_cb(GObject
*source
,
180 GAsyncResult
*result
,
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
),
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
) : "",
199 TP_CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED
);
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
),
212 TP_CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED
);
219 static gboolean
start_connecting(TpBaseConnection
*base
,
222 SipeConnection
*self
= SIPE_CONNECTION(base
);
224 gchar
*uri
= sipe_telepathy_protocol_normalize_contact(NULL
,
228 SIPE_DEBUG_INFO_NOFORMAT("SipeConnection::start_connecting");
230 /* set up mandatory self-handle */
232 base
->self_handle
= tp_handle_ensure(tp_base_connection_get_handles(base
,
233 TP_HANDLE_TYPE_CONTACT
),
238 if (!base
->self_handle
) {
239 SIPE_DEBUG_ERROR("SipeConnection::start_connecting: self handle creation failed: %s",
244 SIPE_DEBUG_ERROR("SipeConnection::start_connecting: %s",
249 tp_base_connection_change_status(base
, TP_CONNECTION_STATUS_CONNECTING
,
250 TP_CONNECTION_STATUS_REASON_REQUESTED
);
252 /* we need a password */
254 rc
= connect_to_core(self
, error
);
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
,
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
);
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
);
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
);
324 g_free(self
->server
);
325 g_free(self
->password
);
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
,
358 SIPE_UNUSED_PARAMETER GError
**error
)
360 SipeConnection
*conn
= g_object_new(SIPE_TYPE_CONNECTION
,
361 "protocol", tp_base_protocol_get_name(protocol
),
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"));
377 value
= tp_asv_get_string(params
, "password");
378 if (value
&& strlen(value
))
379 conn
->password
= g_strdup(value
);
381 conn
->password
= NULL
;
384 value
= tp_asv_get_string(params
, "server");
385 if (value
&& strlen(value
))
386 conn
->server
= g_strdup(value
);
390 /* server port: core expects a string */
391 port
= tp_asv_get_uint32(params
, "port", &valid
);
393 conn
->port
= g_strdup_printf("%d", port
);
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
;
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
);
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
);
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
,
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
,
449 TpConnectionStatusReason reason
;
452 self
->is_disconnecting
= TRUE
;
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
;
460 name
= TP_ERROR_STR_CONNECTION_LOST
;
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
;
473 reason
= TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED
;
474 name
= TP_ERROR_STR_DISCONNECTED
;
478 SIPE_DEBUG_ERROR("sipe_backend_connection_error: %s (%s)", name
, msg
);
479 tp_base_connection_disconnect_with_dbus_error(base
,
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
,
503 SipeConnection
*self
= SIPE_PUBLIC_TO_CONNECTION
;
507 case SIPE_SETTING_USER_AGENT
:
508 value
= self
->user_agent
;
511 /* @TODO: update when settings are implemented */