Extending test-client-custom-summary to try e_book_client_get_contacts_uids()
[platform/upstream/evolution-data-server.git] / camel / camel-sasl-login.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  *  Authors: Jeffrey Stedfast <fejj@ximian.com>
4  *
5  *  Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of version 2 of the GNU Lesser General Public
9  * License as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this program; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <string.h>
28
29 #include <glib/gi18n-lib.h>
30
31 #include "camel-network-settings.h"
32 #include "camel-sasl-login.h"
33 #include "camel-service.h"
34
35 #define CAMEL_SASL_LOGIN_GET_PRIVATE(obj) \
36         (G_TYPE_INSTANCE_GET_PRIVATE \
37         ((obj), CAMEL_TYPE_SASL_LOGIN, CamelSaslLoginPrivate))
38
39 static CamelServiceAuthType sasl_login_auth_type = {
40         N_("Login"),
41
42         N_("This option will connect to the server using a "
43            "simple password."),
44
45         "LOGIN",
46         TRUE
47 };
48
49 enum {
50         LOGIN_USER,
51         LOGIN_PASSWD
52 };
53
54 struct _CamelSaslLoginPrivate {
55         gint state;
56 };
57
58 G_DEFINE_TYPE (CamelSaslLogin, camel_sasl_login, CAMEL_TYPE_SASL)
59
60 static GByteArray *
61 sasl_login_challenge_sync (CamelSasl *sasl,
62                            GByteArray *token,
63                            GCancellable *cancellable,
64                            GError **error)
65 {
66         CamelSaslLoginPrivate *priv;
67         CamelNetworkSettings *network_settings;
68         CamelSettings *settings;
69         CamelService *service;
70         GByteArray *buf = NULL;
71         const gchar *password;
72         gchar *user;
73
74         /* Need to wait for the server */
75         if (token == NULL)
76                 return NULL;
77
78         priv = CAMEL_SASL_LOGIN_GET_PRIVATE (sasl);
79
80         service = camel_sasl_get_service (sasl);
81
82         settings = camel_service_ref_settings (service);
83         g_return_val_if_fail (CAMEL_IS_NETWORK_SETTINGS (settings), NULL);
84
85         network_settings = CAMEL_NETWORK_SETTINGS (settings);
86         user = camel_network_settings_dup_user (network_settings);
87
88         g_object_unref (settings);
89
90         g_return_val_if_fail (user != NULL, NULL);
91
92         password = camel_service_get_password (service);
93         g_return_val_if_fail (password != NULL, NULL);
94
95         switch (priv->state) {
96         case LOGIN_USER:
97                 buf = g_byte_array_new ();
98                 g_byte_array_append (buf, (guint8 *) user, strlen (user));
99                 break;
100         case LOGIN_PASSWD:
101                 buf = g_byte_array_new ();
102                 g_byte_array_append (buf, (guint8 *) password, strlen (password));
103
104                 camel_sasl_set_authenticated (sasl, TRUE);
105                 break;
106         default:
107                 g_set_error (
108                         error, CAMEL_SERVICE_ERROR,
109                         CAMEL_SERVICE_ERROR_CANT_AUTHENTICATE,
110                         _("Unknown authentication state."));
111         }
112
113         priv->state++;
114
115         g_free (user);
116
117         return buf;
118 }
119
120 static void
121 camel_sasl_login_class_init (CamelSaslLoginClass *class)
122 {
123         CamelSaslClass *sasl_class;
124
125         g_type_class_add_private (class, sizeof (CamelSaslLoginPrivate));
126
127         sasl_class = CAMEL_SASL_CLASS (class);
128         sasl_class->auth_type = &sasl_login_auth_type;
129         sasl_class->challenge_sync = sasl_login_challenge_sync;
130 }
131
132 static void
133 camel_sasl_login_init (CamelSaslLogin *sasl)
134 {
135         sasl->priv = CAMEL_SASL_LOGIN_GET_PRIVATE (sasl);
136 }