src/mn-sasl.c (1653B) - raw
1 /*
2 * Mail Notification
3 * Copyright (C) 2003-2008 Jean-Yves Lefort <jylefort@brutele.be>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include <glib.h>
21 #include <glib/gi18n.h>
22 #include <sasl/sasl.h>
23
24 static gboolean attempted = FALSE;
25 static gboolean initialized = FALSE;
26 static char *init_error = NULL;
27
28 G_LOCK_DEFINE_STATIC(init);
29
30 gboolean
31 mn_sasl_init (GError **err)
32 {
33 /*
34 * Bad things may happen if we call sasl_client_init() again after
35 * having called sasl_done(), so we just keep SASL initialized for
36 * the whole application lifetime.
37 */
38
39 G_LOCK(init);
40 if (! attempted)
41 {
42 int status;
43
44 status = sasl_client_init(NULL);
45 if (status == SASL_OK)
46 initialized = TRUE;
47 else
48 init_error = g_strdup(sasl_errstring(status, NULL, NULL));
49
50 attempted = TRUE;
51 }
52
53 if (! initialized)
54 {
55 g_assert(init_error != NULL);
56 g_set_error(err, 0, 0, "%s", init_error);
57 }
58 G_UNLOCK(init);
59
60 return initialized;
61 }