From 6b2af7de8f4664e304de359760358413b4f8129e Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Sun, 12 Oct 2014 10:53:40 +0200 Subject: [PATCH] SolipaMail: aggiunto il supporto a TLS. Aggiustamenti per funzionare con camel >= 3.8. --- src/camelsession.c | 18 +++++++++++++++ src/mail.c | 57 +++++++++++++++++++++++++++++++++++++++------- src/mail.h | 2 ++ tests/mail.c | 9 +++++++- tests/mailui.c | 15 +++++++++++- 5 files changed, 91 insertions(+), 10 deletions(-) diff --git a/src/camelsession.c b/src/camelsession.c index 1c0c1d8..a1ed563 100644 --- a/src/camelsession.c +++ b/src/camelsession.c @@ -24,9 +24,18 @@ G_DEFINE_TYPE (SolipaCamelSession, solipa_camel_session, CAMEL_TYPE_SESSION) +CamelCertTrust +solipa_camel_session_trust_prompt (CamelSession *session, + CamelService *service, + GTlsCertificate *certificate, + GTlsCertificateFlags errors); + static void 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; } static void @@ -51,3 +60,12 @@ solipa_camel_session_new (const gchar *path) return session; } + +CamelCertTrust +solipa_camel_session_trust_prompt (CamelSession *session, + CamelService *service, + GTlsCertificate *certificate, + GTlsCertificateFlags errors) +{ + return CAMEL_CERT_TRUST_TEMPORARY; +} \ No newline at end of file diff --git a/src/mail.c b/src/mail.c index 976d32e..d409e7c 100644 --- a/src/mail.c +++ b/src/mail.c @@ -56,6 +56,7 @@ struct _SolipaMailPrivate CamelInternetAddress *recipients_bcc; gboolean with_notification; + gboolean use_tls; gchar *body_plain; gchar *body_html; @@ -363,6 +364,7 @@ static CamelMimeMessage GError *error; GFileInfo *file_info; CamelStream *cstream; + GInputStream *istream; attachs = priv->attachments; while (attachs != NULL) @@ -393,14 +395,20 @@ static CamelMimeMessage camel_data_wrapper_set_mime_type (dw, g_content_type_get_mime_type (g_file_info_get_content_type (file_info))); error = NULL; +#ifdef CAMEL38 + istream = (GInputStream *)g_file_read (file, NULL, &error); + error = NULL; + camel_data_wrapper_construct_from_input_stream_sync (dw, istream, NULL, &error); + g_object_unref (istream); +#elif defined CAMEL3 cstream = camel_stream_fs_new_with_name ((gchar *)attachs->data, O_RDONLY, 0, &error); error = NULL; -#ifdef CAMEL3 camel_data_wrapper_construct_from_stream_sync (dw, cstream, NULL, &error); + g_object_unref (cstream); #else camel_data_wrapper_construct_from_stream (dw, cstream, &error); -#endif g_object_unref (cstream); +#endif part = camel_mime_part_new (); camel_medium_set_content (CAMEL_MEDIUM (part), dw); @@ -411,7 +419,7 @@ static CamelMimeMessage camel_multipart_add_part (mp, part); g_object_unref (part); - } + } } g_object_unref (file); @@ -510,6 +518,15 @@ solipa_mail_set_notification (SolipaMail *mail, gboolean with_notification) priv->with_notification = with_notification; } +void solipa_mail_enable_tls (SolipaMail *mail, gboolean use_tls) +{ + SolipaMailPrivate *priv; + + priv = SOLIPA_MAIL_GET_PRIVATE (mail); + + priv->use_tls = use_tls; +} + void solipa_mail_send (SolipaMail *mail, const gchar *url) { @@ -600,11 +617,35 @@ solipa_mail_send (SolipaMail *mail, const gchar *url) error != NULL && error->message != NULL ? error->message : "no details"); return; } -#ifdef CAMEL38 - trans = camel_session_ref_service_by_url (solipa_get_camel_session (priv->solipa), camel_url, CAMEL_PROVIDER_TRANSPORT); -#else - trans = camel_session_get_service_by_url (solipa_get_camel_session (priv->solipa), camel_url, CAMEL_PROVIDER_TRANSPORT); -#endif + + error = NULL; + trans = camel_session_add_service (solipa_get_camel_session (priv->solipa), url, camel_url->protocol, CAMEL_PROVIDER_TRANSPORT, &error); + if (trans == NULL || error != NULL) + { + g_warning ("Cannot add service: %s", + error != NULL && error->message != NULL ? error->message : "no details"); + return; + } + CamelSettings *settings = camel_service_ref_settings (trans); + camel_network_settings_set_host (CAMEL_NETWORK_SETTINGS (settings), camel_url->host); + camel_network_settings_set_user (CAMEL_NETWORK_SETTINGS (settings), ""); + if (camel_url->port == 0) + { + camel_network_settings_set_port (CAMEL_NETWORK_SETTINGS (settings), 25); + } + else + { + 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), CAMEL_NETWORK_SECURITY_METHOD_STARTTLS_ON_STANDARD_PORT); + } + else + { + camel_network_settings_set_security_method (CAMEL_NETWORK_SETTINGS (settings), CAMEL_NETWORK_SECURITY_METHOD_NONE); + } + camel_service_set_settings (trans, settings); #else trans = camel_session_get_service (solipa_get_camel_session (priv->solipa), url, CAMEL_PROVIDER_TRANSPORT, &error); #endif diff --git a/src/mail.h b/src/mail.h index 2112fd3..9a906f3 100644 --- a/src/mail.h +++ b/src/mail.h @@ -76,6 +76,8 @@ 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_send (SolipaMail *mail, const gchar *url); gboolean solipa_mail_check_email_address (const gchar *address); diff --git a/tests/mail.c b/tests/mail.c index 47b9593..ae5778e 100644 --- a/tests/mail.c +++ b/tests/mail.c @@ -31,6 +31,8 @@ main (int argc, char **argv) gchar *from; gchar *to; gchar *url; + gboolean notification; + gboolean tls; GError *error; @@ -39,6 +41,8 @@ main (int argc, char **argv) { "from", 'f', 0, G_OPTION_ARG_STRING, &from, "Indirizzo FROM", NULL }, { "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, ¬ification, "Richiedere notifica", NULL }, + { "tls", 's', 0, G_OPTION_ARG_INT, &tls, "Utilizzare TLS", NULL }, { NULL } }; @@ -50,6 +54,8 @@ main (int argc, char **argv) from = NULL; to = NULL; url = NULL; + notification = FALSE; + tls = FALSE; /* gestione degli argomenti della riga di comando */ context = g_option_context_new (""); @@ -116,7 +122,8 @@ main (int argc, char **argv) solipa_mail_add_recipient (smail, addr, SOLIPA_MAIL_RECIPIENT_TYPE_TO); g_object_unref (addr); - solipa_mail_set_notification (smail, TRUE); + solipa_mail_set_notification (smail, notification); + solipa_mail_enable_tls (smail, tls); 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.", "\n" diff --git a/tests/mailui.c b/tests/mailui.c index 10936ca..54b2254 100644 --- a/tests/mailui.c +++ b/tests/mailui.c @@ -57,7 +57,20 @@ on_btn_ok_clicked (gpointer instance, gpointer user_data) "smtp-uri", smtp, NULL); - solipa_mail_ui_send (mailui); + if (solipa_mail_ui_send (mailui)) + { + solipa_message_dialog (w, + GTK_MESSAGE_INFO, + GTK_BUTTONS_OK, + "EMail inviata con successo."); + } + else + { + solipa_message_dialog (w, + GTK_MESSAGE_WARNING, + GTK_BUTTONS_OK, + "EMail non inviata."); + } } gboolean -- 2.49.0