From c3aa88621dcef31aa992c03cc298b6aabf6d06b8 Mon Sep 17 00:00:00 2001 From: Ryan Wagoner Date: Sat, 14 Dec 2019 23:27:29 -0500 Subject: [PATCH] - Add controller name and TLS support to email notifications --- OmniLinkBridge/CoreServer.cs | 7 +++--- OmniLinkBridge/Extensions.cs | 3 --- OmniLinkBridge/Global.cs | 2 ++ .../Notifications/EmailNotification.cs | 15 ++++++----- OmniLinkBridge/Notifications/Notification.cs | 2 +- .../Notifications/ProwlNotification.cs | 25 ++++++++++--------- .../Notifications/PushoverNotification.cs | 12 ++++----- OmniLinkBridge/OmniLinkBridge.ini | 4 +++ OmniLinkBridge/Settings.cs | 3 ++- 9 files changed, 40 insertions(+), 33 deletions(-) diff --git a/OmniLinkBridge/CoreServer.cs b/OmniLinkBridge/CoreServer.cs index 8a8fa3a..b380bfe 100644 --- a/OmniLinkBridge/CoreServer.cs +++ b/OmniLinkBridge/CoreServer.cs @@ -1,6 +1,5 @@ -using OmniLinkBridge.Modules; -using OmniLinkBridge.OmniLink; -using log4net; +using log4net; +using OmniLinkBridge.Modules; using System.Collections.Generic; using System.Reflection; using System.Threading; @@ -10,7 +9,7 @@ namespace OmniLinkBridge { public class CoreServer { - private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); private OmniLinkII omnilink; private readonly List modules = new List(); diff --git a/OmniLinkBridge/Extensions.cs b/OmniLinkBridge/Extensions.cs index 37192ac..41bc9a0 100644 --- a/OmniLinkBridge/Extensions.cs +++ b/OmniLinkBridge/Extensions.cs @@ -1,9 +1,6 @@ using System; using System.Collections.Generic; -using System.IO; using System.Linq; -using System.Runtime.Serialization.Json; -using System.Text; namespace OmniLinkBridge { diff --git a/OmniLinkBridge/Global.cs b/OmniLinkBridge/Global.cs index 58ad32e..9bbb00d 100644 --- a/OmniLinkBridge/Global.cs +++ b/OmniLinkBridge/Global.cs @@ -16,6 +16,7 @@ namespace OmniLinkBridge public static int controller_port; public static string controller_key1; public static string controller_key2; + public static string controller_name; // Time Sync public static bool time_sync; @@ -61,6 +62,7 @@ namespace OmniLinkBridge // Email Notifications public static string mail_server; + public static bool mail_tls; public static int mail_port; public static string mail_username; public static string mail_password; diff --git a/OmniLinkBridge/Notifications/EmailNotification.cs b/OmniLinkBridge/Notifications/EmailNotification.cs index 3f4b47f..ceecab1 100644 --- a/OmniLinkBridge/Notifications/EmailNotification.cs +++ b/OmniLinkBridge/Notifications/EmailNotification.cs @@ -8,20 +8,23 @@ namespace OmniLinkBridge.Notifications { public class EmailNotification : INotification { - private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); public void Notify(string source, string description, NotificationPriority priority) { foreach (MailAddress address in Global.mail_to) { - MailMessage mail = new MailMessage(); - mail.From = Global.mail_from; + MailMessage mail = new MailMessage + { + From = Global.mail_from, + Subject = $"{Global.controller_name} - {source}", + Body = $"{source}: {description}" + }; mail.To.Add(address); - mail.Subject = "OmniLinkBridge - " + source; - mail.Body = source + ": " + description; using (SmtpClient smtp = new SmtpClient(Global.mail_server, Global.mail_port)) { + smtp.EnableSsl = Global.mail_tls; if (!string.IsNullOrEmpty(Global.mail_username)) { smtp.UseDefaultCredentials = false; @@ -34,7 +37,7 @@ namespace OmniLinkBridge.Notifications } catch (Exception ex) { - log.Error("An error occurred sending notification", ex); + log.Error("An error occurred sending email notification", ex); } } } diff --git a/OmniLinkBridge/Notifications/Notification.cs b/OmniLinkBridge/Notifications/Notification.cs index db14f28..22beb6f 100644 --- a/OmniLinkBridge/Notifications/Notification.cs +++ b/OmniLinkBridge/Notifications/Notification.cs @@ -8,7 +8,7 @@ namespace OmniLinkBridge.Notifications { public static class Notification { - private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); private static readonly List providers = new List() { diff --git a/OmniLinkBridge/Notifications/ProwlNotification.cs b/OmniLinkBridge/Notifications/ProwlNotification.cs index d34cf50..bd631b9 100644 --- a/OmniLinkBridge/Notifications/ProwlNotification.cs +++ b/OmniLinkBridge/Notifications/ProwlNotification.cs @@ -8,35 +8,36 @@ namespace OmniLinkBridge.Notifications { public class ProwlNotification : INotification { - private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); - private static Uri URI = new Uri("https://api.prowlapp.com/publicapi/add"); + private static readonly Uri URI = new Uri("https://api.prowlapp.com/publicapi/add"); public void Notify(string source, string description, NotificationPriority priority) { foreach (string key in Global.prowl_key) { - List parameters = new List(); - - parameters.Add("apikey=" + key); - parameters.Add("priority= " + (int)priority); - parameters.Add("application=OmniLinkBridge"); - parameters.Add("event=" + source); - parameters.Add("description=" + description); + List parameters = new List + { + "apikey=" + key, + "priority= " + (int)priority, + "application=" + Global.controller_name, + "event=" + source, + "description=" + description + }; using (WebClient client = new WebClient()) { client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; client.UploadStringAsync(URI, string.Join("&", parameters.ToArray())); - client.UploadStringCompleted += client_UploadStringCompleted; + client.UploadStringCompleted += Client_UploadStringCompleted; } } } - private void client_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e) + private void Client_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e) { if (e.Error != null) - log.Error("An error occurred sending notification", e.Error); + log.Error("An error occurred sending prowl notification", e.Error); } } } diff --git a/OmniLinkBridge/Notifications/PushoverNotification.cs b/OmniLinkBridge/Notifications/PushoverNotification.cs index a360759..f54f5d1 100644 --- a/OmniLinkBridge/Notifications/PushoverNotification.cs +++ b/OmniLinkBridge/Notifications/PushoverNotification.cs @@ -8,9 +8,9 @@ namespace OmniLinkBridge.Notifications { public class PushoverNotification : INotification { - private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); - private static Uri URI = new Uri("https://api.pushover.net/1/messages.json"); + private static readonly Uri URI = new Uri("https://api.pushover.net/1/messages.json"); public void Notify(string source, string description, NotificationPriority priority) { @@ -20,22 +20,22 @@ namespace OmniLinkBridge.Notifications { "token", Global.pushover_token }, { "user", key }, { "priority", ((int)priority).ToString() }, - { "title", source }, + { "title", $"{Global.controller_name} - {source}" }, { "message", description } }; using (WebClient client = new WebClient()) { client.UploadValues(URI, parameters); - client.UploadStringCompleted += client_UploadStringCompleted; + client.UploadStringCompleted += Client_UploadStringCompleted; } } } - private void client_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e) + private void Client_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e) { if (e.Error != null) - log.Error("An error occurred sending notification", e.Error); + log.Error("An error occurred sending pushover notification", e.Error); } } } diff --git a/OmniLinkBridge/OmniLinkBridge.ini b/OmniLinkBridge/OmniLinkBridge.ini index cc8192a..c951623 100644 --- a/OmniLinkBridge/OmniLinkBridge.ini +++ b/OmniLinkBridge/OmniLinkBridge.ini @@ -3,6 +3,8 @@ controller_address = controller_port = 4369 controller_key1 = 00-00-00-00-00-00-00-00 controller_key2 = 00-00-00-00-00-00-00-00 +# Used in notifications +controller_name = OmniLinkBridge # Controller Time Sync (yes/no) # time_interval is interval in minutes to check controller time @@ -62,6 +64,8 @@ notify_message = no # Email Notifications # mail_username and mail_password optional for authenticated mail mail_server = +mail_tls = no +# When TLS is enabled the port is usually 587 mail_port = 25 mail_username = mail_password = diff --git a/OmniLinkBridge/Settings.cs b/OmniLinkBridge/Settings.cs index 6bd651f..c437ce3 100644 --- a/OmniLinkBridge/Settings.cs +++ b/OmniLinkBridge/Settings.cs @@ -5,7 +5,6 @@ using System.Collections.Generic; using System.Collections.Specialized; using System.IO; using System.Linq; -using System.Net; using System.Net.Mail; using System.Reflection; @@ -24,6 +23,7 @@ namespace OmniLinkBridge Global.controller_port = ValidatePort(settings, "controller_port"); Global.controller_key1 = settings["controller_key1"]; Global.controller_key2 = settings["controller_key2"]; + Global.controller_name = settings["controller_name"] ?? "OmniLinkBridge"; // Controller Time Sync Global.time_sync = ValidateYesNo(settings, "time_sync"); @@ -72,6 +72,7 @@ namespace OmniLinkBridge // Email Notifications Global.mail_server = settings["mail_server"]; + Global.mail_tls = ValidateYesNo(settings, "mail_tls"); Global.mail_port = ValidatePort(settings, "mail_port"); Global.mail_username = settings["mail_username"]; Global.mail_password = settings["mail_password"];