using System; using System.Collections.Generic; using System.ComponentModel; using System.Text; using System.Net.Mail; using System.Media; using System.Windows.Forms; using System.Drawing.Design; using RightEdge.Common; namespace RightEdge_AlertAction_Plugin { public enum AlertSoundType {None,File,Asterisk,Beep,Exclamation,Hand,Question}; [YYEAction(Name = "Alert Action", Description = "Send an alert", Id = "{6E2437C1-080B-42de-A229-FCBF891E12E3}", HelpText = "This action sends an alert.")] [Serializable] public class AlertAction : ActionBase { private bool _alert_debug = true; private bool _alert_do_output = true; private bool _alert_do_email = true; private bool _alert_do_popup = false; private bool _alert_do_sound = true; public bool SoundEnabled { set { _alert_do_sound = value; } get { return (_alert_do_sound); } } private string _alert_message = string.Empty; public string Message { set { _alert_message = value; } get { return (_alert_message); } } private bool _alert_persist = false; private bool _alert_enabled = true; private string _alert_host = string.Empty; private string _alert_source = string.Empty; private string _alert_dest = string.Empty; private string _alert_user = string.Empty; private string _alert_pw = string.Empty; private bool _alert_ssl = true; private AlertSoundType _alert_sound_type = AlertSoundType.Asterisk; public AlertSoundType SoundType { set { _alert_sound_type = value; } get { return (_alert_sound_type); } } private string _alert_file = null; [Editor(typeof(FilePickUITypeEditor), typeof(UITypeEditor))] public string SoundFile { set { _alert_file = value; } get { return (_alert_file); } } [ConstructorArgument("OutputLog", ConstructorArgumentType.Boolean, Value = true)] [ConstructorArgument("SendEmail", ConstructorArgumentType.Boolean, Value = true)] [ConstructorArgument("PlaySound", ConstructorArgumentType.Boolean, Value = true)] [ConstructorArgument("ShowPopup", ConstructorArgumentType.Boolean, Value = false)] [ConstructorArgument("AlertMessage", ConstructorArgumentType.String, Value = "alert text")] [ConstructorArgument("AlertPersist", ConstructorArgumentType.Boolean, Value = false)] [ConstructorArgument("SoundType", ConstructorArgumentType.Enum, Value = AlertSoundType.Asterisk)] [ConstructorArgument("SoundFile", ConstructorArgumentType.String, Value = null)] [ConstructorArgument("MailHost", ConstructorArgumentType.String, Value = null)] [ConstructorArgument("MailUser", ConstructorArgumentType.String, Value = null)] [ConstructorArgument("MailPW", ConstructorArgumentType.String, Value = null)] [ConstructorArgument("MailFrom", ConstructorArgumentType.String, Value = null)] [ConstructorArgument("MailTo", ConstructorArgumentType.String, Value = null)] [ConstructorArgument("MailUseSSL", ConstructorArgumentType.Boolean, Value = false)] public AlertAction(bool OutputLog, bool SendEmail, bool PlaySound, bool ShowPopup, string AlertMessage, bool AlertPersist, AlertSoundType SoundType, string SoundFile, string MailHost, string MailUser, string MailPW, string MailFrom, string MailTo, bool MailUseSSL) : base() { init(OutputLog, SendEmail, PlaySound, ShowPopup, AlertMessage, AlertPersist, SoundType, SoundFile, MailHost, MailUser, MailPW, MailFrom, MailTo, MailUseSSL); } private void init(bool OutputLog, bool SendEmail, bool PlaySound, bool ShowPopup, string AlertMessage, bool AlertPersist, AlertSoundType SoundType, string SoundFile, string MailHost, string MailUser, string MailPW, string MailFrom, string MailTo, bool MailUseSSL) { _alert_do_output = OutputLog; _alert_do_email = SendEmail; _alert_do_sound = PlaySound; _alert_do_popup = ShowPopup; _alert_message = AlertMessage; _alert_persist = AlertPersist; _alert_host = MailHost; _alert_user = MailUser; _alert_pw = MailPW; _alert_source = MailFrom; _alert_dest = MailTo; _alert_ssl = MailUseSSL; _alert_sound_type = SoundType; _alert_file = SoundFile; } public override void Run(ActionContext context) { if (_alert_debug) { SystemData.Output.Add(OutputSeverityLevel.Informational, "Debug Alert Fired : " + _alert_message, context.symbol, "AlertAction"); } if (!_alert_enabled) { return; } if (!_alert_persist) { _alert_enabled = false; } if (_alert_do_output) { SystemData.Output.Add(OutputSeverityLevel.Informational, "Alert Fired : " + _alert_message, context.symbol, "AlertAction"); } if (_alert_do_email) { SmtpClient client = new SmtpClient(_alert_host); MailMessage mail = new MailMessage(_alert_source, _alert_dest, "Alert for " + context.symbol.Name, _alert_message); client.Credentials = new System.Net.NetworkCredential(_alert_user, _alert_pw); client.EnableSsl = _alert_ssl; try { client.Send(mail); } catch (Exception e) { SystemData.Output.Add(OutputSeverityLevel.Error, "alert transmission failed : " + e.Message, context.symbol, "AlertAction"); } } if (_alert_do_sound) { switch(_alert_sound_type) { case AlertSoundType.Asterisk: SystemSounds.Asterisk.Play(); break; case AlertSoundType.Beep: SystemSounds.Beep.Play(); break; case AlertSoundType.Exclamation: SystemSounds.Exclamation.Play(); break; case AlertSoundType.Hand: SystemSounds.Hand.Play(); break; case AlertSoundType.Question: SystemSounds.Question.Play(); break; case AlertSoundType.File: SoundPlayer sp = new SoundPlayer(_alert_file); try { sp.Play(); } catch (Exception e) { SystemData.Output.Add(OutputSeverityLevel.Error, "sound file playback failed : " + e.Message, context.symbol, "AlertAction"); } break; case AlertSoundType.None: default: break; } } if (_alert_do_popup) { MessageBox.Show(_alert_message, "Alert for " + context.symbol.Name, MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } } } }