From: Andrea Zagli Date: Mon, 29 Nov 2010 10:06:14 +0000 (+0100) Subject: Correzioni. X-Git-Tag: Pre_EvolutionDataServer_2.32~18 X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=8d4af244cfbb467412750319ba65c828be4aced3;p=solipa%2Flibsolipa Correzioni. Implementazione del test (e test riuscito) per l'invio di una mail. --- diff --git a/src/Makefile.am b/src/Makefile.am index 01aab5f..46da5ff 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -6,6 +6,7 @@ lib_LTLIBRARIES = libsolipa.la libsolipa_la_SOURCES = solipa.c \ allegato.c \ + camelsession.c \ mail.c libsolipa_la_LDFLAGS = -no-undefined @@ -13,6 +14,7 @@ libsolipa_la_LDFLAGS = -no-undefined libsolipa_include_HEADERS = libsolipa.h \ solipa.h \ allegato.h \ + camelsession.h \ mail.h libsolipa_includedir = $(includedir)/libsolipa diff --git a/src/camelsession.c b/src/camelsession.c new file mode 100644 index 0000000..c3e0b52 --- /dev/null +++ b/src/camelsession.c @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2010 Andrea Zagli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifdef HAVE_CONFIG_H + #include +#endif + + +#include "camelsession.h" + +static void +class_init (SolipaCamelSessionClass *solipa_camel_session_class) +{ + CamelSessionClass *camel_session_class = CAMEL_SESSION_CLASS (solipa_camel_session_class); +} + +CamelType +solipa_camel_session_get_type (void) +{ + static CamelType type = CAMEL_INVALID_TYPE; + + if (type == CAMEL_INVALID_TYPE) { + type = camel_type_register ( + camel_session_get_type (), + "SolipaCamelSession", + sizeof (SolipaCamelSession), + sizeof (SolipaCamelSessionClass), + (CamelObjectClassInitFunc) class_init, + NULL, + NULL, + NULL); + } + + return type; +} + +CamelSession * +solipa_camel_session_new (const gchar *path) +{ + CamelSession *session; + + session = CAMEL_SESSION (camel_object_new (SOLIPA_CAMEL_SESSION_TYPE)); + + camel_session_construct (session, path); + + return session; +} diff --git a/src/camelsession.h b/src/camelsession.h new file mode 100644 index 0000000..d628016 --- /dev/null +++ b/src/camelsession.h @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2010 Andrea Zagli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef __SOLIPA_CAMEL_SESSION_H__ +#define __SOLIPA_CAMEL_SESSION_H__ + +#include + + +G_BEGIN_DECLS + + +#define SOLIPA_CAMEL_SESSION_TYPE (solipa_camel_session_get_type ()) +#define SOLIPA_CAMEL_SESSION(obj) (CAMEL_CHECK_CAST((obj), SOLIPA_CAMEL_SESSION_TYPE, SolipaCamelSession)) +#define SOLIPA_CAMEL_SESSION_CLASS(k) (CAMEL_CHECK_CLASS_CAST ((k), SOLIPA_CAMEL_SESSION_TYPE, SolipaCamelSessionClass)) +#define SOLIPA_CAMEL_IS_SESSION(o) (CAMEL_CHECK_TYPE((o), SOLIPA_CAMEL_SESSION_TYPE)) +#define SOLIPA_CAMEL_SESSION_IS_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), SOLIPA_CAMEL_SESSION_TYPE)) +#define SOLIPA_CAMEL_SESSION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), SOLIPA_CAMEL_SESSION_TYPE, SolipaCamelSessionClass)) + +typedef struct _SolipaCamelSession SolipaCamelSession; +typedef struct _SolipaCamelSessionClass SolipaCamelSessionClass; + +typedef struct _SolipaCamelSession + { + CamelSession parent_object; + + }; + +typedef struct _SolipaCamelSessionClass + { + CamelSessionClass parent_class; + }; + +CamelType solipa_camel_session_get_type (void); + +CamelSession *solipa_camel_session_new (const gchar *path); + + +G_END_DECLS + + +#endif /* __SOLIPA_CAMEL_SESSION_H__ */ diff --git a/src/mail.c b/src/mail.c index 23cd481..3b922ca 100644 --- a/src/mail.c +++ b/src/mail.c @@ -76,10 +76,10 @@ solipa_mail_init (SolipaMail *solipa_mail) SolipaMailPrivate *priv = SOLIPA_MAIL_GET_PRIVATE (solipa_mail); priv->subject = "{No subject}"; - priv->from = NULL; - priv->recipients_to = NULL; - priv->recipients_cc = NULL; - priv->recipients_bcc = NULL; + priv->from = camel_internet_address_new (); + priv->recipients_to = camel_internet_address_new (); + priv->recipients_cc = camel_internet_address_new (); + priv->recipients_bcc = camel_internet_address_new (); priv->body_plain = NULL; priv->body_html = NULL; priv->attachments = NULL; @@ -139,24 +139,23 @@ solipa_mail_set_from (SolipaMail *mail, CamelInternetAddress *from) SolipaMailPrivate *priv; guint addrs; + const gchar *namep; + const gchar *addressp; g_return_if_fail (IS_SOLIPA_MAIL (mail)); priv = SOLIPA_MAIL_GET_PRIVATE (mail); - priv->from = g_memdup (from, sizeof (CamelInternetAddress)); - - /* if addresses are more then one - * then it removes they */ - addrs = camel_address_length (CAMEL_ADDRESS (priv->from)); - if (addrs > 1) + addrs = camel_address_length (CAMEL_ADDRESS (from)); + if (addrs > 0) { - guint i; + camel_address_remove (CAMEL_ADDRESS (priv->from), -1); - for (i = addrs - 1; i > 0; i--) - { - camel_address_remove (CAMEL_ADDRESS (priv->from), i); - } + namep = g_strdup (""); + addressp = g_strdup (""); + camel_internet_address_get (from, 0, + &namep, &addressp); + camel_internet_address_add (priv->from, namep, addressp); } } @@ -174,7 +173,6 @@ solipa_mail_add_recipient (SolipaMail *mail, CamelInternetAddress *recipient, So const gchar *addressp; g_return_if_fail (IS_SOLIPA_MAIL (mail)); - g_return_if_fail (CAMEL_IS_INTERNET_ADDRESS (recipient)); priv = SOLIPA_MAIL_GET_PRIVATE (mail); @@ -198,14 +196,11 @@ solipa_mail_add_recipient (SolipaMail *mail, CamelInternetAddress *recipient, So return; } - if (!CAMEL_IS_INTERNET_ADDRESS (rcpt)) - { - rcpt = camel_internet_address_new (); - } - addrs = camel_address_length (CAMEL_ADDRESS (recipient)); for (i = 0; i < addrs; i++) { + namep = g_strdup (""); + addressp = g_strdup (""); camel_internet_address_get (recipient, i, &namep, &addressp); camel_internet_address_add (rcpt, namep, addressp); @@ -237,24 +232,19 @@ solipa_mail_add_attachment (SolipaMail *mail, const gchar *filename) priv->attachments = g_slist_append (priv->attachments, (gpointer)filename); } -gchar * -solipa_mail_get_as_string (SolipaMail *mail) +static CamelMimeMessage +*solipa_mail_create_message (SolipaMail *mail) { - gchar *ret; - SolipaMailPrivate *priv; CamelMimeMessage *msg; - CamelStream *mem; - gchar tmp_buf[4096]; - gssize nb_read; - g_return_val_if_fail (IS_SOLIPA_MAIL (mail), NULL); priv = SOLIPA_MAIL_GET_PRIVATE (mail); - if (priv->from == NULL) + if (priv->from == NULL + || camel_address_length (CAMEL_ADDRESS (priv->from)) == 0) { g_warning ("From cannot be empty."); return NULL; @@ -263,9 +253,9 @@ solipa_mail_get_as_string (SolipaMail *mail) if ((priv->recipients_to == NULL || camel_address_length (CAMEL_ADDRESS (priv->recipients_to)) == 0) && (priv->recipients_cc == NULL - || camel_address_length (CAMEL_ADDRESS (priv->recipients_cc))== 0) + || camel_address_length (CAMEL_ADDRESS (priv->recipients_cc)) == 0) && (priv->recipients_bcc == NULL - || camel_address_length (CAMEL_ADDRESS (priv->recipients_bcc))== 0)) + || camel_address_length (CAMEL_ADDRESS (priv->recipients_bcc)) == 0)) { g_warning ("Recipients cannot be empty."); return NULL; @@ -292,6 +282,7 @@ solipa_mail_get_as_string (SolipaMail *mail) camel_mime_message_set_recipients (msg, CAMEL_RECIPIENT_TYPE_BCC, priv->recipients_bcc); } + /* TODO multipart message */ if (priv->body_plain != NULL) { camel_mime_part_set_content (CAMEL_MIME_PART (msg), priv->body_plain, strlen (priv->body_plain), "text/plain"); @@ -306,11 +297,32 @@ solipa_mail_get_as_string (SolipaMail *mail) /* multipart message */ } + return msg; +} + +gchar * +solipa_mail_get_as_string (SolipaMail *mail) +{ + gchar *ret; + + CamelMimeMessage *msg; + + CamelStream *mem; + gchar tmp_buf[4096]; + gssize nb_read; + + msg = solipa_mail_create_message (mail); + if (msg == NULL) + { + g_warning ("Unable to create the message."); + return NULL; + } + mem = camel_stream_mem_new (); camel_data_wrapper_write_to_stream (CAMEL_DATA_WRAPPER (msg), mem); if (camel_stream_reset (mem) < 0) { - g_warning ("Unable to reset the mem stream."); + g_warning ("Unable to reset the camel mem stream."); return NULL; } @@ -320,7 +332,7 @@ solipa_mail_get_as_string (SolipaMail *mail) nb_read = camel_stream_read (mem, tmp_buf, sizeof (tmp_buf)); if (nb_read < 0) { - g_warning ("Errore."); + g_warning ("Error on camel stream reading."); break; } else if (nb_read > 0) @@ -332,6 +344,73 @@ solipa_mail_get_as_string (SolipaMail *mail) return ret; } +void +solipa_mail_send (SolipaMail *mail, const gchar *url) +{ + SolipaMailPrivate *priv; + + CamelMimeMessage *msg; + + CamelException *ex; + CamelInternetAddress *from, *addr, *to; + CamelService *trans; + + CamelSession *session; + + guint i; + + char *types[] = { "to", "cc", "bcc" }; + + g_return_if_fail (IS_SOLIPA_MAIL (mail)); + + priv = SOLIPA_MAIL_GET_PRIVATE (mail); + + msg = solipa_mail_create_message (mail); + if (msg == NULL) + { + g_warning ("Unable to create the message."); + return; + } + + ex = camel_exception_new (); + + if (camel_mime_message_get_from (msg) == NULL) + { + camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM, "Cannot send message: no from address set."); + return; + } + + trans = camel_session_get_service (solipa_get_camel_session (priv->solipa), url, CAMEL_PROVIDER_TRANSPORT, ex); + if (camel_exception_is_set (ex)) + { + g_warning ("Not service: %s", camel_exception_get_description (ex)); + return; + } + gboolean ret = camel_service_connect (trans, ex); + if (camel_exception_is_set(ex)) + { + g_warning ("Not connected: %s", camel_exception_get_description (ex)); + goto noconnect; + } + + to = camel_internet_address_new (); + for (i = 0; i < sizeof (types) / sizeof (types[0]); i++) { + addr = (CamelInternetAddress *)camel_mime_message_get_recipients (msg, types[i]); + + if (addr) + { + camel_address_cat (CAMEL_ADDRESS (to), CAMEL_ADDRESS (addr)); + } + } + + camel_transport_send_to (CAMEL_TRANSPORT (trans), msg, CAMEL_ADDRESS (camel_mime_message_get_from (msg)), CAMEL_ADDRESS (to), ex); + camel_object_unref (to); + camel_service_disconnect (trans, FALSE, NULL); + +noconnect: + camel_object_unref (trans); +} + /* PRIVATE */ static void solipa_mail_set_property (GObject *object, diff --git a/src/mail.h b/src/mail.h index f2873d9..348bce3 100644 --- a/src/mail.h +++ b/src/mail.h @@ -74,6 +74,8 @@ void solipa_mail_add_attachment (SolipaMail *mail, const gchar *filename); gchar *solipa_mail_get_as_string (SolipaMail *mail); +void solipa_mail_send (SolipaMail *mail, const gchar *url); + G_END_DECLS diff --git a/src/solipa.c b/src/solipa.c index 46072af..44ff59b 100644 --- a/src/solipa.c +++ b/src/solipa.c @@ -21,7 +21,6 @@ #endif #include -#include #include "solipa.h" @@ -42,6 +41,8 @@ static void solipa_get_property (GObject *object, typedef struct _SolipaPrivate SolipaPrivate; struct _SolipaPrivate { + CamelSession *camel_session; + GdaEx *gdaex; }; @@ -73,6 +74,7 @@ Solipa *solipa_new () { Solipa *solipa; + SolipaPrivate *priv; gchar *camel_tmpdir; @@ -80,6 +82,9 @@ Solipa g_type_init (); g_thread_init (NULL); + solipa = SOLIPA (g_object_new (solipa_get_type (), NULL)); + priv = SOLIPA_GET_PRIVATE (solipa); + /* creo la directory temporanea per camel */ camel_tmpdir = mkdtemp (g_build_filename (g_get_tmp_dir (), g_strdup ("solipa-camel-XXXXXX"), NULL)); @@ -87,11 +92,23 @@ Solipa camel_init (camel_tmpdir, FALSE); camel_provider_init (); - solipa = SOLIPA (g_object_new (solipa_get_type (), NULL)); + priv->camel_session = solipa_camel_session_new (camel_tmpdir); return solipa; } +CamelSession +*solipa_get_camel_session (Solipa *solipa) +{ + SolipaPrivate *priv; + + g_return_val_if_fail (IS_SOLIPA (solipa), NULL); + + priv = SOLIPA_GET_PRIVATE (solipa); + + return priv->camel_session; +} + void solipa_set_gdaex (Solipa *solipa, GdaEx *gdaex) { diff --git a/src/solipa.h b/src/solipa.h index cad5363..6d5d0c0 100644 --- a/src/solipa.h +++ b/src/solipa.h @@ -22,6 +22,8 @@ #include #include +#include + #include @@ -53,6 +55,8 @@ GType solipa_get_type (void) G_GNUC_CONST; Solipa *solipa_new (void); +CamelSession *solipa_get_camel_session (Solipa *solipa); + void solipa_set_gdaex (Solipa *solipa, GdaEx *gdaex); GdaEx *solipa_get_gdaex (Solipa *solipa); diff --git a/tests/mail.c b/tests/mail.c index 30a97fb..0c112f6 100644 --- a/tests/mail.c +++ b/tests/mail.c @@ -23,8 +23,33 @@ int main (int argc, char **argv) { Solipa *solipa; + SolipaMail *smail; + CamelInternetAddress *addr; + + gchar *strmail; solipa = solipa_new (); + smail = solipa_mail_new (solipa); + + solipa_mail_set_subject (smail, "Trying to send an e-mail with SolipaMail."); + + addr = camel_internet_address_new (); + camel_internet_address_add (addr, "Andrea Zagli", "azagli@libero.it"); + solipa_mail_set_from (smail, addr); + + camel_address_remove (CAMEL_ADDRESS (addr), -1); + + camel_internet_address_add (addr, "tux", "tux@sid-sviluppo.miodominio.it"); + solipa_mail_add_recipient (smail, addr, SOLIPA_MAIL_RECIPIENT_TYPE_TO); + camel_object_unref (addr); + + 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.", NULL); + + strmail = solipa_mail_get_as_string (smail); + g_message ("The email text:\n%s", strmail); + + solipa_mail_send (smail, "smtp://10.0.0.102/"); + return 0; }