Set prgname instead of program_class
[nijm-empathy.git] / src / empathy-auth-client.c
blob3ee478d3e29cb9449c6b71049a1a291ad7c5eb1f
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>
25 #include <gnutls/gnutls.h>
27 #include "empathy-auth-factory.h"
28 #include "empathy-bad-password-dialog.h"
29 #include "empathy-password-dialog.h"
30 #include "empathy-sanity-cleaning.h"
31 #include "empathy-server-tls-handler.h"
32 #include "empathy-tls-dialog.h"
33 #include "empathy-tls-verifier.h"
34 #include "empathy-ui-utils.h"
36 #define DEBUG_FLAG EMPATHY_DEBUG_TLS
37 #include "empathy-debug.h"
39 #define TIMEOUT 60
41 static gboolean use_timer = TRUE;
42 static guint timeout_id = 0;
43 static guint num_windows = 0;
45 static gboolean
46 timeout_cb (gpointer p)
48 DEBUG ("Timeout reached; exiting...");
50 gtk_main_quit ();
51 return FALSE;
54 static void
55 start_timer (void)
57 if (!use_timer)
58 return;
60 if (timeout_id != 0)
61 return;
63 DEBUG ("Start timer");
65 timeout_id = g_timeout_add_seconds (TIMEOUT, timeout_cb, NULL);
68 static void
69 stop_timer (void)
71 if (timeout_id == 0)
72 return;
74 DEBUG ("Stop timer");
76 g_source_remove (timeout_id);
77 timeout_id = 0;
80 static void
81 tls_dialog_response_cb (GtkDialog *dialog,
82 gint response_id,
83 gpointer user_data)
85 TpTLSCertificate *certificate = NULL;
86 TpTLSCertificateRejectReason reason = 0;
87 GHashTable *details = NULL;
88 EmpathyTLSDialog *tls_dialog = EMPATHY_TLS_DIALOG (dialog);
89 gboolean remember = FALSE;
90 EmpathyTLSVerifier *verifier = EMPATHY_TLS_VERIFIER (user_data);
92 g_object_get (tls_dialog,
93 "certificate", &certificate,
94 "reason", &reason,
95 "remember", &remember,
96 "details", &details,
97 NULL);
99 DEBUG ("Response %d (remember: %d)", response_id, remember);
101 gtk_widget_destroy (GTK_WIDGET (dialog));
103 if (response_id == GTK_RESPONSE_YES)
105 tp_tls_certificate_accept_async (certificate, NULL, NULL);
107 else
109 tp_asv_set_boolean (details, "user-requested", TRUE);
110 tp_tls_certificate_add_rejection (certificate, reason, NULL,
111 g_variant_new_parsed ("{ 'user-requested': <%b> }", TRUE));
113 tp_tls_certificate_reject_async (certificate, NULL, NULL);
116 if (remember)
117 empathy_tls_verifier_store_exception (verifier);
119 g_object_unref (certificate);
120 g_hash_table_unref (details);
122 /* restart the timeout */
123 num_windows--;
125 if (num_windows > 0)
126 return;
128 start_timer ();
131 static void
132 display_interactive_dialog (TpTLSCertificate *certificate,
133 EmpathyTLSVerifier *verifier,
134 TpTLSCertificateRejectReason reason,
135 GHashTable *details)
137 GtkWidget *tls_dialog;
139 /* stop the timeout */
140 num_windows++;
141 stop_timer ();
143 tls_dialog = empathy_tls_dialog_new (certificate, reason, details);
144 g_signal_connect_data (tls_dialog, "response",
145 G_CALLBACK (tls_dialog_response_cb), g_object_ref (verifier),
146 (GClosureNotify)g_object_unref, 0);
148 gtk_widget_show (tls_dialog);
151 static void
152 verifier_verify_cb (GObject *source,
153 GAsyncResult *result,
154 gpointer user_data)
156 TpTLSCertificateRejectReason reason;
157 GError *error = NULL;
158 TpTLSCertificate *certificate = NULL;
159 GHashTable *details = NULL;
160 gchar *hostname = NULL;
162 g_object_get (source,
163 "certificate", &certificate,
164 NULL);
166 empathy_tls_verifier_verify_finish (EMPATHY_TLS_VERIFIER (source),
167 result, &reason, &details, &error);
169 if (error != NULL)
171 DEBUG ("Error: %s", error->message);
172 display_interactive_dialog (certificate, EMPATHY_TLS_VERIFIER (source),
173 reason, details);
175 g_error_free (error);
177 else
179 tp_tls_certificate_accept_async (certificate, NULL, NULL);
182 g_free (hostname);
183 g_object_unref (certificate);
186 static void
187 auth_factory_new_tls_handler_cb (EmpathyAuthFactory *factory,
188 EmpathyServerTLSHandler *handler,
189 gpointer user_data)
191 TpTLSCertificate *certificate = NULL;
192 gchar *hostname = NULL;
193 gchar **reference_identities = NULL;
194 EmpathyTLSVerifier *verifier;
196 DEBUG ("New TLS server handler received from the factory");
198 g_object_get (handler,
199 "certificate", &certificate,
200 "hostname", &hostname,
201 "reference-identities", &reference_identities,
202 NULL);
204 verifier = empathy_tls_verifier_new (certificate, hostname,
205 (const gchar **) reference_identities);
206 empathy_tls_verifier_verify_async (verifier,
207 verifier_verify_cb, NULL);
209 g_object_unref (verifier);
210 g_object_unref (certificate);
211 g_free (hostname);
212 g_strfreev (reference_identities);
215 static void
216 auth_factory_new_sasl_handler_cb (EmpathyAuthFactory *factory,
217 EmpathyServerSASLHandler *handler,
218 gpointer user_data)
220 GtkWidget *dialog;
222 DEBUG ("New SASL server handler received from the factory");
224 /* If the handler has the password it will deal with it itself. */
225 if (!empathy_server_sasl_handler_has_password (handler))
227 DEBUG ("SASL handler doesn't have a password, prompt for one");
229 dialog = empathy_password_dialog_new (handler);
230 gtk_widget_show (dialog);
234 static void
235 retry_account_cb (GtkWidget *dialog,
236 TpAccount *account,
237 const gchar *password,
238 EmpathyAuthFactory *factory)
240 DEBUG ("Try reconnecting to %s", tp_account_get_path_suffix (account));
242 empathy_auth_factory_save_retry_password (factory, account, password);
244 tp_account_reconnect_async (account, NULL, NULL);
247 static void
248 auth_factory_auth_passsword_failed (EmpathyAuthFactory *factory,
249 TpAccount *account,
250 const gchar *password,
251 gpointer user_data)
253 GtkWidget *dialog;
255 DEBUG ("Authentication on %s failed, popup password dialog",
256 tp_account_get_path_suffix (account));
258 dialog = empathy_bad_password_dialog_new (account, password);
260 tp_g_signal_connect_object (dialog, "retry",
261 G_CALLBACK (retry_account_cb), factory, 0);
263 gtk_widget_show (dialog);
266 static void
267 sanity_cb (GObject *source,
268 GAsyncResult *result,
269 gpointer user_data)
271 start_timer ();
275 main (int argc,
276 char **argv)
278 GOptionContext *context;
279 GError *error = NULL;
280 EmpathyAuthFactory *factory;
281 TpDebugSender *debug_sender;
282 TpSimpleClientFactory *tp_factory;
283 TpDBusDaemon *dbus;
285 context = g_option_context_new (N_(" - Empathy authentication client"));
286 g_option_context_add_group (context, gtk_get_option_group (TRUE));
287 g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
289 if (!g_option_context_parse (context, &argc, &argv, &error))
291 g_print ("%s\nRun '%s --help' to see a full list of available command "
292 "line options.\n", error->message, argv[0]);
293 g_warning ("Error in empathy-auth-client init: %s", error->message);
294 return EXIT_FAILURE;
297 g_option_context_free (context);
299 empathy_gtk_init ();
300 gnutls_global_init ();
301 g_set_application_name (_("Empathy authentication client"));
303 /* Make empathy and empathy-auth-client appear as the same app in
304 * gnome-shell */
305 g_set_prgname ("empathy");
306 gtk_window_set_default_icon_name ("empathy");
307 textdomain (GETTEXT_PACKAGE);
309 /* There is no 'main' UI window so just use the default GdkScreen */
310 empathy_set_css_provider (NULL);
312 #ifdef ENABLE_DEBUG
313 /* Set up debug sender */
314 debug_sender = tp_debug_sender_dup ();
315 g_log_set_default_handler (tp_debug_sender_log_handler, G_LOG_DOMAIN);
316 #endif
318 dbus = tp_dbus_daemon_dup (NULL);
319 tp_factory = tp_simple_client_factory_new (dbus);
320 tp_simple_client_factory_add_account_features_varargs (tp_factory,
321 TP_ACCOUNT_FEATURE_STORAGE,
324 factory = empathy_auth_factory_new (tp_factory);
325 g_object_unref (tp_factory);
326 g_object_unref (dbus);
328 g_signal_connect (factory, "new-server-tls-handler",
329 G_CALLBACK (auth_factory_new_tls_handler_cb), NULL);
331 g_signal_connect (factory, "new-server-sasl-handler",
332 G_CALLBACK (auth_factory_new_sasl_handler_cb), NULL);
334 g_signal_connect (factory, "auth-password-failed",
335 G_CALLBACK (auth_factory_auth_passsword_failed), NULL);
337 if (!empathy_auth_factory_register (factory, &error))
339 g_critical ("Failed to register the auth factory: %s\n", error->message);
340 g_error_free (error);
341 g_object_unref (factory);
343 return EXIT_FAILURE;
346 DEBUG ("Empathy auth client started.");
348 if (g_getenv ("EMPATHY_PERSIST") != NULL)
350 DEBUG ("Timed-exit disabled");
352 use_timer = FALSE;
355 /* Wait for the migration code to be done before starting the timer */
356 empathy_sanity_checking_run_async (sanity_cb, NULL);
358 gtk_main ();
360 g_object_unref (factory);
361 g_object_unref (debug_sender);
363 return EXIT_SUCCESS;