1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Authors: Jeffrey Stedfast <fejj@ximian.com>
5 * Copyright 2001-2003 Ximian, Inc. (www.ximian.com)
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.
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.
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.
31 #include <glib/gi18n-lib.h>
33 #include "camel-internet-address.h"
34 #include "camel-sasl-anonymous.h"
36 CamelServiceAuthType camel_sasl_anonymous_authtype = {
39 N_("This option will connect to the server using an anonymous login."),
45 static CamelSaslClass *parent_class = NULL;
47 /* Returns the class for a CamelSaslAnonymous */
48 #define CSA_CLASS(so) CAMEL_SASL_ANONYMOUS_CLASS (CAMEL_OBJECT_GET_CLASS (so))
50 static GByteArray *anon_challenge (CamelSasl *sasl, GByteArray *token, CamelException *ex);
53 camel_sasl_anonymous_class_init (CamelSaslAnonymousClass *camel_sasl_anonymous_class)
55 CamelSaslClass *camel_sasl_class = CAMEL_SASL_CLASS (camel_sasl_anonymous_class);
57 parent_class = CAMEL_SASL_CLASS (camel_type_get_global_classfuncs (camel_sasl_get_type ()));
59 /* virtual method overload */
60 camel_sasl_class->challenge = anon_challenge;
64 camel_sasl_anonymous_finalize (CamelObject *object)
66 CamelSaslAnonymous *sasl = CAMEL_SASL_ANONYMOUS (object);
68 g_free (sasl->trace_info);
73 camel_sasl_anonymous_get_type (void)
75 static CamelType type = CAMEL_INVALID_TYPE;
77 if (type == CAMEL_INVALID_TYPE) {
78 type = camel_type_register (camel_sasl_get_type (),
80 sizeof (CamelSaslAnonymous),
81 sizeof (CamelSaslAnonymousClass),
82 (CamelObjectClassInitFunc) camel_sasl_anonymous_class_init,
85 (CamelObjectFinalizeFunc) camel_sasl_anonymous_finalize);
93 * camel_sasl_anonymous_new:
95 * @trace_info: trace info
97 * Create a new #CamelSaslAnonymous object.
99 * Returns a new #CamelSasl object
102 camel_sasl_anonymous_new (CamelSaslAnonTraceType type, const char *trace_info)
104 CamelSaslAnonymous *sasl_anon;
106 if (!trace_info && type != CAMEL_SASL_ANON_TRACE_EMPTY) return NULL;
108 sasl_anon = CAMEL_SASL_ANONYMOUS (camel_object_new (camel_sasl_anonymous_get_type ()));
109 sasl_anon->trace_info = g_strdup (trace_info);
110 sasl_anon->type = type;
112 return CAMEL_SASL (sasl_anon);
116 anon_challenge (CamelSasl *sasl, GByteArray *token, CamelException *ex)
118 CamelSaslAnonymous *sasl_anon = CAMEL_SASL_ANONYMOUS (sasl);
119 CamelInternetAddress *cia;
120 GByteArray *ret = NULL;
123 camel_exception_set (ex, CAMEL_EXCEPTION_SERVICE_CANT_AUTHENTICATE,
124 _("Authentication failed."));
128 switch (sasl_anon->type) {
129 case CAMEL_SASL_ANON_TRACE_EMAIL:
130 cia = camel_internet_address_new ();
131 if (camel_internet_address_add (cia, NULL, sasl_anon->trace_info) != 1) {
132 camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_CANT_AUTHENTICATE,
133 _("Invalid email address trace information:\n%s"),
134 sasl_anon->trace_info);
135 camel_object_unref (cia);
138 camel_object_unref (cia);
139 ret = g_byte_array_new ();
140 g_byte_array_append (ret, sasl_anon->trace_info, strlen (sasl_anon->trace_info));
142 case CAMEL_SASL_ANON_TRACE_OPAQUE:
143 if (strchr (sasl_anon->trace_info, '@')) {
144 camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_CANT_AUTHENTICATE,
145 _("Invalid opaque trace information:\n%s"),
146 sasl_anon->trace_info);
149 ret = g_byte_array_new ();
150 g_byte_array_append (ret, sasl_anon->trace_info, strlen (sasl_anon->trace_info));
152 case CAMEL_SASL_ANON_TRACE_EMPTY:
153 ret = g_byte_array_new ();
156 camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_CANT_AUTHENTICATE,
157 _("Invalid trace information:\n%s"),
158 sasl_anon->trace_info);
162 sasl->authenticated = TRUE;