Fix compile time issues
[collectd.git] / src / notify_email.c
index f42e66c..0e140e4 100644 (file)
@@ -24,8 +24,8 @@
 
 #include "collectd.h"
 
-#include "common.h"
 #include "plugin.h"
+#include "utils/common/common.h"
 
 #include <auth-client.h>
 #include <libesmtp.h>
@@ -38,19 +38,19 @@ static const char *config_keys[] = {"SMTPServer",   "SMTPPort", "SMTPUser",
 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
 
 static char **recipients;
-static int recipients_len = 0;
+static int recipients_len;
 
 static smtp_session_t session;
 static pthread_mutex_t session_lock = PTHREAD_MUTEX_INITIALIZER;
 static smtp_message_t message;
-static auth_context_t authctx = NULL;
+static auth_context_t authctx;
 
 static int smtp_port = 25;
-static char *smtp_host = NULL;
-static char *smtp_user = NULL;
-static char *smtp_password = NULL;
-static char *email_from = NULL;
-static char *email_subject = NULL;
+static char *smtp_host;
+static char *smtp_user;
+static char *smtp_password;
+static char *email_from;
+static char *email_subject;
 
 #define DEFAULT_SMTP_HOST "localhost"
 #define DEFAULT_SMTP_FROM "root@localhost"
@@ -115,7 +115,7 @@ static int notify_email_init(void) {
   if (session == NULL) {
     pthread_mutex_unlock(&session_lock);
     ERROR("notify_email plugin: cannot create SMTP session");
-    return (-1);
+    return -1;
   }
 
   smtp_set_monitorcb(session, monitor_cb, NULL, 1);
@@ -131,11 +131,11 @@ static int notify_email_init(void) {
   if (!smtp_auth_set_context(session, authctx)) {
     pthread_mutex_unlock(&session_lock);
     ERROR("notify_email plugin: cannot set SMTP auth context");
-    return (-1);
+    return -1;
   }
 
   pthread_mutex_unlock(&session_lock);
-  return (0);
+  return 0;
 } /* int notify_email_init */
 
 static int notify_email_shutdown(void) {
@@ -152,7 +152,7 @@ static int notify_email_shutdown(void) {
   auth_client_exit();
 
   pthread_mutex_unlock(&session_lock);
-  return (0);
+  return 0;
 } /* int notify_email_shutdown */
 
 static int notify_email_config(const char *key, const char *value) {
@@ -162,14 +162,14 @@ static int notify_email_config(const char *key, const char *value) {
     tmp = realloc(recipients, (recipients_len + 1) * sizeof(char *));
     if (tmp == NULL) {
       ERROR("notify_email: realloc failed.");
-      return (-1);
+      return -1;
     }
 
     recipients = tmp;
     recipients[recipients_len] = strdup(value);
     if (recipients[recipients_len] == NULL) {
       ERROR("notify_email: strdup failed.");
-      return (-1);
+      return -1;
     }
     recipients_len++;
   } else if (0 == strcasecmp(key, "SMTPServer")) {
@@ -179,7 +179,7 @@ static int notify_email_config(const char *key, const char *value) {
     int port_tmp = atoi(value);
     if (port_tmp < 1 || port_tmp > 65535) {
       WARNING("notify_email plugin: Invalid SMTP port: %i", port_tmp);
-      return (1);
+      return 1;
     }
     smtp_port = port_tmp;
   } else if (0 == strcasecmp(key, "SMTPUser")) {
@@ -211,8 +211,8 @@ static int notify_email_notification(const notification_t *n,
   char subject[MAXSTRING];
 
   char buf[4096] = "";
+  char *buf_ptr = buf;
   int buf_len = sizeof(buf);
-  int i;
 
   ssnprintf(severity, sizeof(severity), "%s",
             (n->severity == NOTIF_FAILURE)
@@ -231,34 +231,55 @@ static int notify_email_notification(const notification_t *n,
   timestamp_str[sizeof(timestamp_str) - 1] = '\0';
 
   /* Let's make RFC822 message text with \r\n EOLs */
-  ssnprintf(buf, buf_len, "MIME-Version: 1.0\r\n"
-                          "Content-Type: text/plain; charset=\"US-ASCII\"\r\n"
-                          "Content-Transfer-Encoding: 8bit\r\n"
-                          "Subject: %s\r\n"
-                          "\r\n"
-                          "%s - %s@%s\r\n"
-                          "\r\n"
-                          "Message: %s",
-            subject, timestamp_str, severity, n->host, n->message);
+  int status = ssnprintf(buf, buf_len,
+                         "MIME-Version: 1.0\r\n"
+                         "Content-Type: text/plain; charset=\"US-ASCII\"\r\n"
+                         "Content-Transfer-Encoding: 8bit\r\n"
+                         "Subject: %s\r\n"
+                         "\r\n"
+                         "%s - %s@%s\r\n"
+                         "\r\n",
+                         subject, timestamp_str, severity, n->host);
+
+  if (status > 0) {
+    buf_ptr += status;
+    buf_len -= status;
+  }
+
+#define APPEND(format, value)                                                  \
+  if ((buf_len > 0) && (strlen(value) > 0)) {                                  \
+    status = ssnprintf(buf_ptr, buf_len, format "\r\n", value);                \
+    if (status > 0) {                                                          \
+      buf_ptr += status;                                                       \
+      buf_len -= status;                                                       \
+    }                                                                          \
+  }
+
+  APPEND("Host: %s", n->host);
+  APPEND("Plugin: %s", n->plugin);
+  APPEND("Plugin instance: %s", n->plugin_instance);
+  APPEND("Type: %s", n->type);
+  APPEND("Type instance: %s", n->type_instance);
+  APPEND("\r\nMessage: %s", n->message);
 
   pthread_mutex_lock(&session_lock);
 
   if (session == NULL) {
     /* Initialization failed or we're in the process of shutting down. */
     pthread_mutex_unlock(&session_lock);
-    return (-1);
+    return -1;
   }
 
   if (!(message = smtp_add_message(session))) {
     pthread_mutex_unlock(&session_lock);
     ERROR("notify_email plugin: cannot set SMTP message");
-    return (-1);
+    return -1;
   }
   smtp_set_reverse_path(message, email_from);
   smtp_set_header(message, "To", NULL, NULL);
   smtp_set_message_str(message, buf);
 
-  for (i = 0; i < recipients_len; i++)
+  for (int i = 0; i < recipients_len; i++)
     smtp_add_recipient(message, recipients[i]);
 
   /* Initiate a connection to the SMTP server and transfer the message. */
@@ -266,7 +287,7 @@ static int notify_email_notification(const notification_t *n,
     ERROR("notify_email plugin: SMTP server problem: %s",
           smtp_strerror(smtp_errno(), buf, sizeof buf));
     pthread_mutex_unlock(&session_lock);
-    return (-1);
+    return -1;
   } else {
 #if COLLECT_DEBUG
     const smtp_status_t *status;
@@ -279,7 +300,7 @@ static int notify_email_notification(const notification_t *n,
   }
 
   pthread_mutex_unlock(&session_lock);
-  return (0);
+  return 0;
 } /* int notify_email_notification */
 
 void module_register(void) {
@@ -290,5 +311,3 @@ void module_register(void) {
   plugin_register_notification("notify_email", notify_email_notification,
                                /* user_data = */ NULL);
 } /* void module_register (void) */
-
-/* vim: set sw=2 sts=2 ts=8 et : */