]> saetta.ns0.it Git - solipa/libsolipa/commitdiff
SolipaMail: aggiunta l'autenticazione.
authorAndrea Zagli <azagli@libero.it>
Mon, 3 Nov 2014 09:42:05 +0000 (10:42 +0100)
committerAndrea Zagli <azagli@libero.it>
Mon, 3 Nov 2014 09:42:05 +0000 (10:42 +0100)
SolipaMail: aggiunta la possibilità di usare STARTTLS o SSL.

src/camelsession.c
src/mail.c
src/mail.h
tests/mail.c

index a1ed563f8dfb9f2da507b6acb1dcc8f0403cb336..820c22e565017f586eeae9878ece6725794803ba 100644 (file)
@@ -29,6 +29,12 @@ solipa_camel_session_trust_prompt (CamelSession *session,
                             CamelService *service,
                             GTlsCertificate *certificate,
                             GTlsCertificateFlags errors);
+gboolean
+solipa_camel_session_authenticate_sync (CamelSession *session,
+                                        CamelService *service,
+                                        const gchar *mechanism,
+                                        GCancellable *cancellable,
+                                        GError **error);
 
 static void
 solipa_camel_session_class_init (SolipaCamelSessionClass *class)
@@ -36,6 +42,7 @@ solipa_camel_session_class_init (SolipaCamelSessionClass *class)
        CamelSessionClass *camel_session_class = CAMEL_SESSION_CLASS (class);
 
        camel_session_class->trust_prompt = solipa_camel_session_trust_prompt;
+       camel_session_class->authenticate_sync = solipa_camel_session_authenticate_sync;
 }
 
 static void
@@ -68,4 +75,14 @@ solipa_camel_session_trust_prompt (CamelSession *session,
                             GTlsCertificateFlags errors)
 {
        return CAMEL_CERT_TRUST_TEMPORARY;
+}
+
+gboolean
+solipa_camel_session_authenticate_sync (CamelSession *session,
+                                        CamelService *service,
+                                        const gchar *mechanism,
+                                        GCancellable *cancellable,
+                                        GError **error)
+{
+       return (camel_service_authenticate_sync (service, mechanism, cancellable, error) == CAMEL_AUTHENTICATION_ACCEPTED);
 }
\ No newline at end of file
index ac665908a27406698ed2cff381b973cbf04bc4dc..7d94334e530a709ac2892ef166f8adcfb7a82870 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010-2012 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2010-2014 Andrea Zagli <azagli@libero.it>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -56,7 +56,10 @@ struct _SolipaMailPrivate
                CamelInternetAddress *recipients_bcc;
 
                gboolean with_notification;
-               gboolean use_tls;
+               CamelNetworkSecurityMethod security_method;
+
+               gchar *username;
+               gchar *password;
 
                gchar *body_plain;
                gchar *body_html;
@@ -87,6 +90,8 @@ solipa_mail_init (SolipaMail *solipa_mail)
        priv->recipients_to = camel_internet_address_new ();
        priv->recipients_cc = camel_internet_address_new ();
        priv->recipients_bcc = camel_internet_address_new ();
+       priv->username = NULL;
+       priv->password = NULL;
        priv->body_plain = NULL;
        priv->body_html = NULL;
        priv->attachments = NULL;
@@ -528,13 +533,48 @@ solipa_mail_set_notification (SolipaMail *mail, gboolean with_notification)
        priv->with_notification = with_notification;
 }
 
-void solipa_mail_enable_tls (SolipaMail *mail, gboolean use_tls)
+void
+solipa_mail_set_security_method (SolipaMail *mail, CamelNetworkSecurityMethod method)
 {
        SolipaMailPrivate *priv;
 
        priv = SOLIPA_MAIL_GET_PRIVATE (mail);
 
-       priv->use_tls = use_tls;
+       priv->security_method = method;
+}
+
+void 
+solipa_mail_set_username_password (SolipaMail *mail, const gchar *username, const gchar *password)
+{
+       SolipaMailPrivate *priv;
+
+       priv = SOLIPA_MAIL_GET_PRIVATE (mail);
+
+       if (username != NULL)
+               {
+                       priv->username = g_strdup (username);
+                       if (g_strcmp0 (g_strstrip (priv->username), "") == 0)
+                               {
+                                       priv->username = NULL;
+                               }
+               }
+       else
+               {
+                       priv->username = NULL;
+               }
+
+       if (password != NULL)
+               {
+                       priv->password = g_strdup (password);
+                       if (g_strcmp0 (g_strstrip (priv->password), "") == 0)
+                               {
+                                       priv->password = NULL;
+                               }
+               }
+       else
+               {
+                       priv->password = NULL;
+               }
 }
 
 void
@@ -647,14 +687,20 @@ solipa_mail_send (SolipaMail *mail, const gchar *url)
                {
                        camel_network_settings_set_port (CAMEL_NETWORK_SETTINGS (settings), camel_url->port);
                }
-       if (priv->use_tls)
+
+       camel_network_settings_set_security_method (CAMEL_NETWORK_SETTINGS (settings),
+                                                   priv->security_method);
+
+       if (priv->username != NULL)
                {
-                       camel_network_settings_set_security_method (CAMEL_NETWORK_SETTINGS (settings), CAMEL_NETWORK_SECURITY_METHOD_STARTTLS_ON_STANDARD_PORT);
+                       camel_network_settings_set_user (CAMEL_NETWORK_SETTINGS (settings),
+                                                        (const gchar *)priv->username);
                }
-       else
+       if (priv->password != NULL)
                {
-                       camel_network_settings_set_security_method (CAMEL_NETWORK_SETTINGS (settings), CAMEL_NETWORK_SECURITY_METHOD_NONE);
+                       camel_service_set_password (trans, priv->password);
                }
+
        camel_service_set_settings (trans, settings);
 #else
        trans = camel_session_get_service (solipa_get_camel_session (priv->solipa), url, CAMEL_PROVIDER_TRANSPORT, &error);
@@ -681,6 +727,24 @@ solipa_mail_send (SolipaMail *mail, const gchar *url)
                        goto noconnect;
                }
 
+#ifdef CAMEL3
+       if (priv->username != NULL)
+               {
+                       error = NULL;
+                       if (!camel_session_authenticate_sync (solipa_get_camel_session (priv->solipa),
+                                                        trans,
+                                                        "PLAIN",
+                                                        NULL,
+                                                        &error)
+                           || error != NULL)
+                               {
+                                       g_warning ("Not authenticated: %s",
+                                                  error != NULL && error->message != NULL ? error->message : "no details");
+                                       goto noconnect;
+                               }
+               }
+#endif
+
        to = camel_internet_address_new ();
        for (i = 0; i < sizeof (types) / sizeof (types[0]); i++)
                {
index 9a906f3351500abe957961e2ac98ce367dca9650..517dd5fbb747ab22e048bfdb0b18a770933bf1ac 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010-2012 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2010-2014 Andrea Zagli <azagli@libero.it>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -76,7 +76,9 @@ gchar *solipa_mail_get_as_string (SolipaMail *mail);
 
 void solipa_mail_set_notification (SolipaMail *mail, gboolean with_notification);
 
-void solipa_mail_enable_tls (SolipaMail *mail, gboolean use_tls);
+void solipa_mail_set_security_method (SolipaMail *mail, CamelNetworkSecurityMethod method);
+
+void solipa_mail_set_username_password (SolipaMail *mail, const gchar *username, const gchar *password);
 
 void solipa_mail_send (SolipaMail *mail, const gchar *url);
 
index ae5778e9872e823b0a7722403fbb273efbaf2f61..8439a6967468a137316aab8777f02999199497b4 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010-2012 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2010-2014 Andrea Zagli <azagli@libero.it>
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -32,7 +32,9 @@ main (int argc, char **argv)
        gchar *to;
        gchar *url;
        gboolean notification;
-       gboolean tls;
+       CamelNetworkSecurityMethod security_method;
+       gchar *username;
+       gchar *password;
 
        GError *error;
 
@@ -42,7 +44,9 @@ main (int argc, char **argv)
                { "to", 't', 0, G_OPTION_ARG_STRING, &to, "Indirizzo TO", NULL },
                { "url", 'u', 0, G_OPTION_ARG_STRING, &url, "Url smtp server", NULL },
                { "notification", 'n', 0, G_OPTION_ARG_INT, &notification, "Richiedere notifica", NULL },
-               { "tls", 's', 0, G_OPTION_ARG_INT, &tls, "Utilizzare TLS", NULL },
+               { "security method", 's', 0, G_OPTION_ARG_INT, &security_method, "Metodo per connessione sicura (0 - nessuno; 1 - SSL/TLS; 2 - STARTTLS", NULL },
+               { "username", 'm', 0, G_OPTION_ARG_STRING, &username, "Nome utente", NULL },
+               { "password", 'p', 0, G_OPTION_ARG_STRING, &password, "Password", NULL },
                { NULL }
        };
 
@@ -55,7 +59,9 @@ main (int argc, char **argv)
        to = NULL;
        url = NULL;
        notification = FALSE;
-       tls = FALSE;
+       security_method = CAMEL_NETWORK_SECURITY_METHOD_NONE;
+       username = NULL;
+       password = NULL;
 
        /* gestione degli argomenti della riga di comando */
        context = g_option_context_new ("");
@@ -123,7 +129,7 @@ main (int argc, char **argv)
        g_object_unref (addr);
 
        solipa_mail_set_notification (smail, notification);
-       solipa_mail_enable_tls (smail, tls);
+       solipa_mail_set_security_method (smail, security_method);
 
        solipa_mail_set_body (smail, "This is the text\nof the email\n that i'm trying to send to myself.\n\nBye bye\n\nSee you later.",
                              "<html>\n"
@@ -142,6 +148,11 @@ main (int argc, char **argv)
        strmail = solipa_mail_get_as_string (smail);
        g_message ("The email text:\n%s", strmail);
 
+       if (username != NULL && password != NULL)
+               {
+                       solipa_mail_set_username_password (smail, username, password);
+               }
+
        solipa_mail_send (smail, url);
 
        return 0;