]> saetta.ns0.it Git - solipa/libsolipa/commitdiff
SolipaMail: aggiunto il supporto a TLS.
authorAndrea Zagli <azagli@libero.it>
Sun, 12 Oct 2014 08:53:40 +0000 (10:53 +0200)
committerAndrea Zagli <azagli@libero.it>
Sun, 12 Oct 2014 08:53:40 +0000 (10:53 +0200)
Aggiustamenti per funzionare con camel >= 3.8.

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

index 1c0c1d81f3e9e6963728b4de069e56ef28913927..a1ed563f8dfb9f2da507b6acb1dcc8f0403cb336 100644 (file)
 
 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
index 976d32eb1dab6777cdf44d62866503ad03fd9df6..d409e7c1c100b1f70b4272e591ca92e66533801b 100644 (file)
@@ -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
index 2112fd387264f64df1b747fd06626aed73777f05..9a906f3351500abe957961e2ac98ce367dca9650 100644 (file)
@@ -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);
index 47b95934c84fd6cb88b7fbb22a48bbdc07b9901f..ae5778e9872e823b0a7722403fbb273efbaf2f61 100644 (file)
@@ -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, &notification, "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.",
                              "<html>\n"
index 10936ca0e5c2e835030fa48d36af985f8fd26c7c..54b2254e2f96bcad8ac0b2b29cb3f0e485c97ef5 100644 (file)
@@ -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