Fix broken markup in Finnish user docs translation
[empathy-mirror.git] / src / empathy-auth-client.c
blob3e70c434055f333bb5f1c7329c3a8b5628b963a1
1 /*
2 * Copyright (C) 2010 Collabora Ltd.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301 USA
19 * Authors: Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
22 #include "config.h"
24 #include <glib/gi18n.h>
26 #include "empathy-auth-factory.h"
27 #include "empathy-bad-password-dialog.h"
28 #include "empathy-password-dialog.h"
29 #include "empathy-sanity-cleaning.h"
30 #include "empathy-server-tls-handler.h"
31 #include "empathy-tls-dialog.h"
32 #include "empathy-tls-verifier.h"
33 #include "empathy-ui-utils.h"
35 #define DEBUG_FLAG EMPATHY_DEBUG_TLS
36 #include "empathy-debug.h"
38 #define TIMEOUT 60
40 static gboolean use_timer = TRUE;
41 static guint timeout_id = 0;
42 static guint num_windows = 0;
44 static gboolean
45 timeout_cb (gpointer p)
47 DEBUG ("Timeout reached; exiting...");
49 gtk_main_quit ();
50 return FALSE;
53 static void
54 start_timer (void)
56 if (!use_timer)
57 return;
59 if (timeout_id != 0)
60 return;
62 DEBUG ("Start timer");
64 timeout_id = g_timeout_add_seconds (TIMEOUT, timeout_cb, NULL);
67 static void
68 stop_timer (void)
70 if (timeout_id == 0)
71 return;
73 DEBUG ("Stop timer");
75 g_source_remove (timeout_id);
76 timeout_id = 0;
79 static void
80 tls_dialog_response_cb (GtkDialog *dialog,
81 gint response_id,
82 gpointer user_data)
84 TpTLSCertificate *certificate = NULL;
85 TpTLSCertificateRejectReason reason = 0;
86 GHashTable *details = NULL;
87 EmpathyTLSDialog *tls_dialog = EMPATHY_TLS_DIALOG (dialog);
88 gboolean remember = FALSE;
89 EmpathyTLSVerifier *verifier = EMPATHY_TLS_VERIFIER (user_data);
91 g_object_get (tls_dialog,
92 "certificate", &certificate,
93 "reason", &reason,
94 "remember", &remember,
95 "details", &details,
96 NULL);
98 DEBUG ("Response %d (remember: %d)", response_id, remember);
100 gtk_widget_destroy (GTK_WIDGET (dialog));
102 if (response_id == GTK_RESPONSE_YES)
104 tp_tls_certificate_accept_async (certificate, NULL, NULL);
106 else
108 tp_asv_set_boolean (details, "user-requested", TRUE);
109 tp_tls_certificate_add_rejection (certificate, reason, NULL,
110 g_variant_new_parsed ("{ 'user-requested': <%b> }", TRUE));
112 tp_tls_certificate_reject_async (certificate, NULL, NULL);
115 if (remember)
116 empathy_tls_verifier_store_exception (verifier);
118 g_object_unref (certificate);
119 g_hash_table_unref (details);
121 /* restart the timeout */
122 num_windows--;
124 if (num_windows > 0)
125 return;
127 start_timer ();
130 static void
131 display_interactive_dialog (TpTLSCertificate *certificate,
132 EmpathyTLSVerifier *verifier,
133 TpTLSCertificateRejectReason reason,
134 GHashTable *details)
136 GtkWidget *tls_dialog;
138 /* stop the timeout */
139 num_windows++;
140 stop_timer ();
142 tls_dialog = empathy_tls_dialog_new (certificate, reason, details);
143 g_signal_connect_data (tls_dialog, "response",
144 G_CALLBACK (tls_dialog_response_cb), g_object_ref (verifier),
145 (GClosureNotify)g_object_unref, 0);
147 gtk_widget_show (tls_dialog);
150 static void
151 verifier_verify_cb (GObject *source,
152 GAsyncResult *result,
153 gpointer user_data)
155 TpTLSCertificateRejectReason reason;
156 GError *error = NULL;
157 TpTLSCertificate *certificate = NULL;
158 GHashTable *details = NULL;
159 gchar *hostname = NULL;
161 g_object_get (source,
162 "certificate", &certificate,
163 NULL);
165 empathy_tls_verifier_verify_finish (EMPATHY_TLS_VERIFIER (source),
166 result, &reason, &details, &error);
168 if (error != NULL)
170 DEBUG ("Error: %s", error->message);
171 display_interactive_dialog (certificate, EMPATHY_TLS_VERIFIER (source),
172 reason, details);
174 g_error_free (error);
176 else
178 tp_tls_certificate_accept_async (certificate, NULL, NULL);
181 g_free (hostname);
182 g_object_unref (certificate);
185 static void
186 auth_factory_new_tls_handler_cb (EmpathyAuthFactory *factory,
187 EmpathyServerTLSHandler *handler,
188 gpointer user_data)
190 TpTLSCertificate *certificate = NULL;
191 gchar *hostname = NULL;
192 gchar **reference_identities = NULL;
193 EmpathyTLSVerifier *verifier;
195 DEBUG ("New TLS server handler received from the factory");
197 g_object_get (handler,
198 "certificate", &certificate,
199 "hostname", &hostname,
200 "reference-identities", &reference_identities,
201 NULL);
203 verifier = empathy_tls_verifier_new (certificate, hostname,
204 (const gchar **) reference_identities);
205 empathy_tls_verifier_verify_async (verifier,
206 verifier_verify_cb, NULL);
208 g_object_unref (verifier);
209 g_object_unref (certificate);
210 g_free (hostname);
211 g_strfreev (reference_identities);
214 static void
215 auth_factory_new_sasl_handler_cb (EmpathyAuthFactory *factory,
216 EmpathyServerSASLHandler *handler,
217 gpointer user_data)
219 GtkWidget *dialog;
221 DEBUG ("New SASL server handler received from the factory");
223 /* If the handler has the password it will deal with it itself. */
224 if (!empathy_server_sasl_handler_has_password (handler))
226 DEBUG ("SASL handler doesn't have a password, prompt for one");
228 dialog = empathy_password_dialog_new (handler);
229 gtk_widget_show (dialog);
233 static void
234 retry_account_cb (GtkWidget *dialog,
235 TpAccount *account,
236 const gchar *password,
237 EmpathyAuthFactory *factory)
239 DEBUG ("Try reconnecting to %s", tp_account_get_path_suffix (account));
241 empathy_auth_factory_save_retry_password (factory, account, password);
243 tp_account_reconnect_async (account, NULL, NULL);
246 static void
247 auth_factory_auth_passsword_failed (EmpathyAuthFactory *factory,
248 TpAccount *account,
249 const gchar *password,
250 gpointer user_data)
252 GtkWidget *dialog;
254 DEBUG ("Authentication on %s failed, popup password dialog",
255 tp_account_get_path_suffix (account));
257 dialog = empathy_bad_password_dialog_new (account, password);
259 tp_g_signal_connect_object (dialog, "retry",
260 G_CALLBACK (retry_account_cb), factory, 0);
262 gtk_widget_show (dialog);
265 static void
266 sanity_cb (GObject *source,
267 GAsyncResult *result,
268 gpointer user_data)
270 start_timer ();
274 main (int argc,
275 char **argv)
277 GOptionContext *context;
278 GError *error = NULL;
279 EmpathyAuthFactory *factory;
280 TpDebugSender *debug_sender;
281 TpSimpleClientFactory *tp_factory;
282 TpDBusDaemon *dbus;
284 context = g_option_context_new (N_(" — Empathy authentication client"));
285 g_option_context_add_group (context, gtk_get_option_group (TRUE));
286 g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
288 if (!g_option_context_parse (context, &argc, &argv, &error))
290 g_print ("%s\nRun '%s --help' to see a full list of available command "
291 "line options.\n", error->message, argv[0]);
292 g_warning ("Error in empathy-auth-client init: %s", error->message);
293 return EXIT_FAILURE;
296 g_option_context_free (context);
298 empathy_gtk_init ();
299 g_set_application_name (_("Empathy authentication client"));
301 /* Make empathy and empathy-auth-client appear as the same app in
302 * gnome-shell */
303 g_set_prgname ("empathy");
304 gtk_window_set_default_icon_name ("empathy");
305 textdomain (GETTEXT_PACKAGE);
307 /* There is no 'main' UI window so just use the default GdkScreen */
308 empathy_set_css_provider (NULL);
310 #ifdef ENABLE_DEBUG
311 /* Set up debug sender */
312 debug_sender = tp_debug_sender_dup ();
313 g_log_set_default_handler (tp_debug_sender_log_handler, G_LOG_DOMAIN);
314 #endif
316 dbus = tp_dbus_daemon_dup (NULL);
317 tp_factory = tp_simple_client_factory_new (dbus);
318 tp_simple_client_factory_add_account_features_varargs (tp_factory,
319 TP_ACCOUNT_FEATURE_STORAGE,
322 factory = empathy_auth_factory_new (tp_factory);
323 g_object_unref (tp_factory);
324 g_object_unref (dbus);
326 g_signal_connect (factory, "new-server-tls-handler",
327 G_CALLBACK (auth_factory_new_tls_handler_cb), NULL);
329 g_signal_connect (factory, "new-server-sasl-handler",
330 G_CALLBACK (auth_factory_new_sasl_handler_cb), NULL);
332 g_signal_connect (factory, "auth-password-failed",
333 G_CALLBACK (auth_factory_auth_passsword_failed), NULL);
335 if (!empathy_auth_factory_register (factory, &error))
337 g_critical ("Failed to register the auth factory: %s\n", error->message);
338 g_error_free (error);
339 g_object_unref (factory);
341 return EXIT_FAILURE;
344 DEBUG ("Empathy auth client started.");
346 if (g_getenv ("EMPATHY_PERSIST") != NULL)
348 DEBUG ("Timed-exit disabled");
350 use_timer = FALSE;
353 /* Wait for the migration code to be done before starting the timer */
354 empathy_sanity_checking_run_async (sanity_cb, NULL);
356 gtk_main ();
358 g_object_unref (factory);
359 g_object_unref (debug_sender);
361 return EXIT_SUCCESS;