- Add controller name and TLS support to email notifications

This commit is contained in:
Ryan Wagoner 2019-12-14 23:27:29 -05:00
parent 336e02e8e8
commit c3aa88621d
9 changed files with 40 additions and 33 deletions

View file

@ -1,6 +1,5 @@
using OmniLinkBridge.Modules; using log4net;
using OmniLinkBridge.OmniLink; using OmniLinkBridge.Modules;
using log4net;
using System.Collections.Generic; using System.Collections.Generic;
using System.Reflection; using System.Reflection;
using System.Threading; using System.Threading;
@ -10,7 +9,7 @@ namespace OmniLinkBridge
{ {
public class CoreServer 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 OmniLinkII omnilink;
private readonly List<IModule> modules = new List<IModule>(); private readonly List<IModule> modules = new List<IModule>();

View file

@ -1,9 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
namespace OmniLinkBridge namespace OmniLinkBridge
{ {

View file

@ -16,6 +16,7 @@ namespace OmniLinkBridge
public static int controller_port; public static int controller_port;
public static string controller_key1; public static string controller_key1;
public static string controller_key2; public static string controller_key2;
public static string controller_name;
// Time Sync // Time Sync
public static bool time_sync; public static bool time_sync;
@ -61,6 +62,7 @@ namespace OmniLinkBridge
// Email Notifications // Email Notifications
public static string mail_server; public static string mail_server;
public static bool mail_tls;
public static int mail_port; public static int mail_port;
public static string mail_username; public static string mail_username;
public static string mail_password; public static string mail_password;

View file

@ -8,20 +8,23 @@ namespace OmniLinkBridge.Notifications
{ {
public class EmailNotification : INotification 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) public void Notify(string source, string description, NotificationPriority priority)
{ {
foreach (MailAddress address in Global.mail_to) foreach (MailAddress address in Global.mail_to)
{ {
MailMessage mail = new MailMessage(); MailMessage mail = new MailMessage
mail.From = Global.mail_from; {
From = Global.mail_from,
Subject = $"{Global.controller_name} - {source}",
Body = $"{source}: {description}"
};
mail.To.Add(address); mail.To.Add(address);
mail.Subject = "OmniLinkBridge - " + source;
mail.Body = source + ": " + description;
using (SmtpClient smtp = new SmtpClient(Global.mail_server, Global.mail_port)) using (SmtpClient smtp = new SmtpClient(Global.mail_server, Global.mail_port))
{ {
smtp.EnableSsl = Global.mail_tls;
if (!string.IsNullOrEmpty(Global.mail_username)) if (!string.IsNullOrEmpty(Global.mail_username))
{ {
smtp.UseDefaultCredentials = false; smtp.UseDefaultCredentials = false;
@ -34,7 +37,7 @@ namespace OmniLinkBridge.Notifications
} }
catch (Exception ex) catch (Exception ex)
{ {
log.Error("An error occurred sending notification", ex); log.Error("An error occurred sending email notification", ex);
} }
} }
} }

View file

@ -8,7 +8,7 @@ namespace OmniLinkBridge.Notifications
{ {
public static class Notification 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<INotification> providers = new List<INotification>() private static readonly List<INotification> providers = new List<INotification>()
{ {

View file

@ -8,35 +8,36 @@ namespace OmniLinkBridge.Notifications
{ {
public class ProwlNotification : INotification 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) public void Notify(string source, string description, NotificationPriority priority)
{ {
foreach (string key in Global.prowl_key) foreach (string key in Global.prowl_key)
{ {
List<string> parameters = new List<string>(); List<string> parameters = new List<string>
{
parameters.Add("apikey=" + key); "apikey=" + key,
parameters.Add("priority= " + (int)priority); "priority= " + (int)priority,
parameters.Add("application=OmniLinkBridge"); "application=" + Global.controller_name,
parameters.Add("event=" + source); "event=" + source,
parameters.Add("description=" + description); "description=" + description
};
using (WebClient client = new WebClient()) using (WebClient client = new WebClient())
{ {
client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
client.UploadStringAsync(URI, string.Join("&", parameters.ToArray())); 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) if (e.Error != null)
log.Error("An error occurred sending notification", e.Error); log.Error("An error occurred sending prowl notification", e.Error);
} }
} }
} }

View file

@ -8,9 +8,9 @@ namespace OmniLinkBridge.Notifications
{ {
public class PushoverNotification : INotification 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) public void Notify(string source, string description, NotificationPriority priority)
{ {
@ -20,22 +20,22 @@ namespace OmniLinkBridge.Notifications
{ "token", Global.pushover_token }, { "token", Global.pushover_token },
{ "user", key }, { "user", key },
{ "priority", ((int)priority).ToString() }, { "priority", ((int)priority).ToString() },
{ "title", source }, { "title", $"{Global.controller_name} - {source}" },
{ "message", description } { "message", description }
}; };
using (WebClient client = new WebClient()) using (WebClient client = new WebClient())
{ {
client.UploadValues(URI, parameters); 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) if (e.Error != null)
log.Error("An error occurred sending notification", e.Error); log.Error("An error occurred sending pushover notification", e.Error);
} }
} }
} }

View file

@ -3,6 +3,8 @@ controller_address =
controller_port = 4369 controller_port = 4369
controller_key1 = 00-00-00-00-00-00-00-00 controller_key1 = 00-00-00-00-00-00-00-00
controller_key2 = 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) # Controller Time Sync (yes/no)
# time_interval is interval in minutes to check controller time # time_interval is interval in minutes to check controller time
@ -62,6 +64,8 @@ notify_message = no
# Email Notifications # Email Notifications
# mail_username and mail_password optional for authenticated mail # mail_username and mail_password optional for authenticated mail
mail_server = mail_server =
mail_tls = no
# When TLS is enabled the port is usually 587
mail_port = 25 mail_port = 25
mail_username = mail_username =
mail_password = mail_password =

View file

@ -5,7 +5,6 @@ using System.Collections.Generic;
using System.Collections.Specialized; using System.Collections.Specialized;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net;
using System.Net.Mail; using System.Net.Mail;
using System.Reflection; using System.Reflection;
@ -24,6 +23,7 @@ namespace OmniLinkBridge
Global.controller_port = ValidatePort(settings, "controller_port"); Global.controller_port = ValidatePort(settings, "controller_port");
Global.controller_key1 = settings["controller_key1"]; Global.controller_key1 = settings["controller_key1"];
Global.controller_key2 = settings["controller_key2"]; Global.controller_key2 = settings["controller_key2"];
Global.controller_name = settings["controller_name"] ?? "OmniLinkBridge";
// Controller Time Sync // Controller Time Sync
Global.time_sync = ValidateYesNo(settings, "time_sync"); Global.time_sync = ValidateYesNo(settings, "time_sync");
@ -72,6 +72,7 @@ namespace OmniLinkBridge
// Email Notifications // Email Notifications
Global.mail_server = settings["mail_server"]; Global.mail_server = settings["mail_server"];
Global.mail_tls = ValidateYesNo(settings, "mail_tls");
Global.mail_port = ValidatePort(settings, "mail_port"); Global.mail_port = ValidatePort(settings, "mail_port");
Global.mail_username = settings["mail_username"]; Global.mail_username = settings["mail_username"];
Global.mail_password = settings["mail_password"]; Global.mail_password = settings["mail_password"];