1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Authors: Michael Zucchi <notzed@ximian.com>
5 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
7 * This library is free software you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation.
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this library; if not, see <http://www.gnu.org/licenses/>.
28 #include <glib/gi18n-lib.h>
30 #include "camel-sasl-popb4smtp.h"
31 #include "camel-service.h"
32 #include "camel-session.h"
33 #include "camel-store.h"
35 #define CAMEL_SASL_POPB4SMTP_GET_PRIVATE(obj) \
36 (G_TYPE_INSTANCE_GET_PRIVATE \
37 ((obj), CAMEL_TYPE_SASL_POPB4SMTP, CamelSaslPOPB4SMTPPrivate))
39 struct _CamelSaslPOPB4SMTPPrivate {
40 gint placeholder; /* allow for future expansion */
43 static CamelServiceAuthType sasl_popb4smtp_auth_type = {
44 N_("POP before SMTP"),
46 N_("This option will authorise a POP connection before attempting SMTP"),
52 /* last time the pop was accessed (through the auth method anyway), *time_t */
53 static GHashTable *poplast;
55 /* use 1 hour as our pop timeout */
56 #define POPB4SMTP_TIMEOUT (60*60)
59 #define POPB4SMTP_LOCK(l) g_mutex_lock(&l)
60 #define POPB4SMTP_UNLOCK(l) g_mutex_unlock(&l)
62 G_DEFINE_TYPE (CamelSaslPOPB4SMTP, camel_sasl_popb4smtp, CAMEL_TYPE_SASL)
65 sasl_popb4smtp_challenge_sync (CamelSasl *sasl,
67 GCancellable *cancellable,
70 CamelService *service;
71 CamelSession *session;
73 const gchar *type_name;
76 service = camel_sasl_get_service (sasl);
77 session = camel_service_ref_session (service);
79 camel_sasl_set_authenticated (sasl, FALSE);
81 pop_uid = camel_session_get_password (
82 session, service, _("POP Source UID"),
83 "popb4smtp_uid", 0, error);
86 service = camel_session_ref_service (session, pop_uid);
90 g_object_unref (session);
92 if (service == NULL) {
94 error, CAMEL_SERVICE_ERROR,
95 CAMEL_SERVICE_ERROR_CANT_AUTHENTICATE,
96 _("POP Before SMTP authentication "
97 "using an unknown transport"));
102 type_name = G_OBJECT_TYPE_NAME (service);
104 if (!CAMEL_IS_STORE (service)) {
106 error, CAMEL_SERVICE_ERROR,
107 CAMEL_SERVICE_ERROR_CANT_AUTHENTICATE,
108 _("POP Before SMTP authentication attempted "
109 "with a %s service"), type_name);
113 if (strstr (type_name, "POP") == NULL) {
115 error, CAMEL_SERVICE_ERROR,
116 CAMEL_SERVICE_ERROR_CANT_AUTHENTICATE,
117 _("POP Before SMTP authentication attempted "
118 "with a %s service"), type_name);
122 /* check if we've done it before recently in this session */
125 /* need to lock around the whole thing until finished with timep */
127 POPB4SMTP_LOCK (lock);
129 timep = g_hash_table_lookup (poplast, pop_uid);
131 if ((*timep + POPB4SMTP_TIMEOUT) > now) {
132 camel_sasl_set_authenticated (sasl, TRUE);
133 POPB4SMTP_UNLOCK (lock);
137 timep = g_malloc0 (sizeof (*timep));
138 g_hash_table_insert (poplast, g_strdup (pop_uid), timep);
141 /* connect to pop session */
142 if (camel_service_connect_sync (service, cancellable, error)) {
143 camel_sasl_set_authenticated (sasl, TRUE);
146 camel_sasl_set_authenticated (sasl, FALSE);
150 POPB4SMTP_UNLOCK (lock);
153 g_object_unref (service);
160 camel_sasl_popb4smtp_class_init (CamelSaslPOPB4SMTPClass *class)
162 CamelSaslClass *sasl_class;
164 g_type_class_add_private (class, sizeof (CamelSaslPOPB4SMTPPrivate));
166 sasl_class = CAMEL_SASL_CLASS (class);
167 sasl_class->auth_type = &sasl_popb4smtp_auth_type;
168 sasl_class->challenge_sync = sasl_popb4smtp_challenge_sync;
170 poplast = g_hash_table_new (g_str_hash, g_str_equal);
174 camel_sasl_popb4smtp_init (CamelSaslPOPB4SMTP *sasl)
176 sasl->priv = CAMEL_SASL_POPB4SMTP_GET_PRIVATE (sasl);