[balsa] SMTP logging



commit 07efe4de10b5113989cd58423c3051f9e53a77df
Author: Albrecht Dreß <albrecht dress arcor de>
Date:   Sat Aug 3 14:09:08 2019 -0400

    SMTP logging
    
    See <URL:https://mail.gnome.org/archives/balsa-list/2019-July/msg00005.html>
    
    When Balsa sends a message to a local or remote SMTP server,
    a feedback is given only on failure.  However, sometimes it is necessary
    to trace sent messages on the SMTP server.
    
    * libbalsa/send.c: new function balsa_send_message_syslog(),
      changed API of net_client_smtp_send_msg();
    * libnetclient/net-client-smtp.[ch]: extend API of net_client_smtp_send_msg()
      for optionally passing the final SMTP server reply.

 ChangeLog                      | 16 ++++++++++++++++
 libbalsa/send.c                | 26 +++++++++++++++++++++++++-
 libnetclient/net-client-smtp.c |  4 ++--
 libnetclient/net-client-smtp.h |  3 ++-
 4 files changed, 45 insertions(+), 4 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 3062b7412..e9daa60d8 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,19 @@
+2019-08-03  Albrecht Dreß  <albrecht dress arcor de>
+
+       SMTP logging
+
+       See
+       <URL:https://mail.gnome.org/archives/balsa-list/2019-July/msg00005.html>
+
+       When Balsa sends a message to a local or remote SMTP server,
+       a feedback is given only on failure.  However, sometimes it is necessary
+       to trace sent messages on the SMTP server.
+
+       * libbalsa/send.c: new function balsa_send_message_syslog(),
+         changed API of net_client_smtp_send_msg();
+       * libnetclient/net-client-smtp.[ch]: extend API of net_client_smtp_send_msg()
+         for optionally passing the final SMTP server reply.
+
 2019-08-03  Peter Bloomfield  <pbloomfield bellsouth net>
 
        mailbox-imap: Clear search filter when mailbox is closed
diff --git a/libbalsa/send.c b/libbalsa/send.c
index a3e96a8ef..dd63de210 100644
--- a/libbalsa/send.c
+++ b/libbalsa/send.c
@@ -29,6 +29,7 @@
 #include <math.h>
 #include <stdlib.h>
 #include <string.h>
+#include <syslog.h>
 
 #include "libbalsa.h"
 #include "libbalsa_private.h"
@@ -1008,6 +1009,26 @@ balsa_send_message_error(MessageQueueItem *mqi,
                error->message);
 }
 
+static void
+balsa_send_message_syslog(const gchar            *smtp_server,
+                                                 const MessageQueueItem *mqi,
+                                                 gboolean                result,
+                                                 const gchar            *server_response,
+                                                 const GError           *server_error)
+{
+       GString *syslog_msg;
+
+       syslog_msg = g_string_new(NULL);
+       g_string_append_printf(syslog_msg, "[%d:%s] SMTP=%s Message-ID=%s", (int) getpid(), 
g_get_user_name(), smtp_server,
+               libbalsa_message_get_message_id(mqi->orig));
+       if (result) {
+               syslog(LOG_MAIL | LOG_INFO, "%s Result='%s'", syslog_msg->str, server_response);
+       } else {
+               syslog(LOG_MAIL | LOG_NOTICE, "%s Error='%s'", syslog_msg->str,
+                               (server_error != NULL) ? server_error->message : "unknown");
+       }
+}
+
 static gpointer
 balsa_send_message_real(SendMessageInfo *info)
 {
@@ -1036,6 +1057,7 @@ balsa_send_message_real(SendMessageInfo *info)
         for (this_msg = info->items; this_msg != NULL; this_msg = this_msg->next) {
             MessageQueueItem *mqi = (MessageQueueItem *) this_msg->data;
             gboolean send_res;
+            gchar *server_reply = NULL;
             LibBalsaMailbox *mailbox;
 
             mailbox = mqi->orig != NULL ? libbalsa_message_get_mailbox(mqi->orig) : NULL;
@@ -1043,7 +1065,9 @@ balsa_send_message_real(SendMessageInfo *info)
             info->curr_msg++;
             g_debug("%s: %u/%u mqi = %p", __func__, info->msg_count, info->curr_msg, mqi);
             /* send the message */
-            send_res = net_client_smtp_send_msg(info->session, mqi->smtp_msg, &error);
+            send_res = net_client_smtp_send_msg(info->session, mqi->smtp_msg, &server_reply, &error);
+            balsa_send_message_syslog(net_client_get_host(NET_CLIENT(info->session)), mqi, send_res, 
server_reply, error);
+            g_free(server_reply);
 
             g_mutex_lock(&send_messages_lock);
             if (mailbox != NULL) {
diff --git a/libnetclient/net-client-smtp.c b/libnetclient/net-client-smtp.c
index f532222e3..0c81dcc47 100644
--- a/libnetclient/net-client-smtp.c
+++ b/libnetclient/net-client-smtp.c
@@ -197,7 +197,7 @@ net_client_smtp_can_dsn(NetClientSmtp *client)
 
 
 gboolean
-net_client_smtp_send_msg(NetClientSmtp *client, const NetClientSmtpMessage *message, GError **error)
+net_client_smtp_send_msg(NetClientSmtp *client, const NetClientSmtpMessage *message, gchar **server_stat, 
GError **error)
 {
        NetClient *netclient;
        gboolean result;
@@ -267,7 +267,7 @@ net_client_smtp_send_msg(NetClientSmtp *client, const NetClientSmtpMessage *mess
 
        if (result) {
                (void) net_client_set_timeout(netclient, 10U * 60U);    /* RFC 5321, Sect 4.5.3.2.6.: 10 
minutes timeout */
-               result = net_client_smtp_read_reply(client, -1, NULL, error);
+               result = net_client_smtp_read_reply(client, -1, server_stat, error);
                client->data_state = FALSE;
        }
 
diff --git a/libnetclient/net-client-smtp.h b/libnetclient/net-client-smtp.h
index 65b62c3b4..70ecef1e5 100644
--- a/libnetclient/net-client-smtp.h
+++ b/libnetclient/net-client-smtp.h
@@ -161,12 +161,13 @@ gboolean net_client_smtp_can_dsn(NetClientSmtp *client);
  *
  * @param client connected SMTP network client object
  * @param message message data
+ * @param server_stat filled with the final server response on success, may be NULL
  * @param error filled with error information if the connection fails
  * @return TRUE on success or FALSE if sending the message failed
  *
  * Send the passed SMTP message to the connected SMTP server.
  */
-gboolean net_client_smtp_send_msg(NetClientSmtp *client, const NetClientSmtpMessage *message, GError 
**error);
+gboolean net_client_smtp_send_msg(NetClientSmtp *client, const NetClientSmtpMessage *message, gchar 
**server_stat, GError **error);
 
 
 /** @brief Create a SMTP message


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]