StandardActions.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using InABox.Core;
  2. using InABox.Database;
  3. using PRS.Shared.Events;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Text.RegularExpressions;
  10. using System.Threading.Tasks;
  11. namespace PRSStores.Events;
  12. [Caption("Send Email")]
  13. public partial class SendEmailEventAction<TEvent> : IStandardEventAction<TEvent>
  14. where TEvent : IEvent
  15. {
  16. #region Public Properties
  17. // Each of these properties are expressions.
  18. public string To { get; set; } = "";
  19. public string CC { get; set; } = "";
  20. public string BCC { get; set; } = "";
  21. public string Subject { get; set; } = "";
  22. public string Message { get; set; } = "";
  23. #endregion
  24. #region Expressions
  25. private CoreExpression? _toExpression;
  26. public CoreExpression ToExpression
  27. {
  28. get
  29. {
  30. if(_toExpression is null || _toExpression.ExpressionString != To)
  31. {
  32. _toExpression = new CoreExpression(To);
  33. }
  34. return _toExpression;
  35. }
  36. }
  37. private CoreExpression? _ccExpression;
  38. public CoreExpression CCExpression
  39. {
  40. get
  41. {
  42. if(_ccExpression is null || _ccExpression.ExpressionString != CC)
  43. {
  44. _ccExpression = new CoreExpression(CC);
  45. }
  46. return _ccExpression;
  47. }
  48. }
  49. private CoreExpression? _bccExpression;
  50. public CoreExpression BCCExpression
  51. {
  52. get
  53. {
  54. if(_bccExpression is null || _bccExpression.ExpressionString != BCC)
  55. {
  56. _bccExpression = new CoreExpression(BCC);
  57. }
  58. return _bccExpression;
  59. }
  60. }
  61. private CoreExpression? _subjectExpression;
  62. public CoreExpression SubjectExpression
  63. {
  64. get
  65. {
  66. if(_subjectExpression is null || _subjectExpression.ExpressionString != Subject)
  67. {
  68. _subjectExpression = new CoreExpression(Subject, returnType: typeof(string));
  69. }
  70. return _subjectExpression;
  71. }
  72. }
  73. private CoreExpression? _messageExpression;
  74. public CoreExpression MessageExpression
  75. {
  76. get
  77. {
  78. if(_messageExpression is null || _messageExpression.ExpressionString != Message)
  79. {
  80. _messageExpression = new CoreExpression(Message, returnType: typeof(string));
  81. }
  82. return _messageExpression;
  83. }
  84. }
  85. #endregion
  86. public IEnumerable<string> ReferencedVariables => ToExpression.ReferencedVariables
  87. .Concat(CCExpression.ReferencedVariables)
  88. .Concat(BCCExpression.ReferencedVariables)
  89. .Concat(SubjectExpression.ReferencedVariables)
  90. .Concat(MessageExpression.ReferencedVariables);
  91. public string Description => "Create Email";
  92. public object? Execute(IEventDataModel dataModel)
  93. {
  94. var mailer = DbFactory.Mailer;
  95. if(mailer is not null)
  96. {
  97. if (mailer.Connect())
  98. {
  99. var msg = mailer.CreateMessage();
  100. // msg.From = DbFactory.EmailAddress;
  101. msg.To = ConvertAddressList(ToExpression.Evaluate(dataModel));
  102. msg.CC = ConvertAddressList(CCExpression.Evaluate(dataModel));
  103. msg.BCC = ConvertAddressList(BCCExpression.Evaluate(dataModel));
  104. msg.Subject = SubjectExpression.Evaluate<string>(dataModel) ?? "";
  105. msg.Body = MessageExpression.Evaluate<string>(dataModel) ?? "";
  106. mailer.SendMessage(msg);
  107. }
  108. }
  109. return null;
  110. }
  111. private IEnumerable<string> ConvertAddressList(object? value)
  112. {
  113. if(value is IEnumerable<string> list)
  114. {
  115. return list;
  116. }
  117. else if(value is IEnumerable enumerable && value is not string)
  118. {
  119. var lst = new List<string>();
  120. foreach(var obj in enumerable)
  121. {
  122. var objStr = obj.ToString();
  123. if(objStr is not null)
  124. {
  125. lst.Add(objStr);
  126. }
  127. }
  128. return lst;
  129. }
  130. var str = value?.ToString() ?? "";
  131. var regex = AddressListRegex();
  132. return regex.Matches(str).Select(x => x.Value);
  133. }
  134. [GeneratedRegex(@"[^\s,;]+")]
  135. private static partial Regex AddressListRegex();
  136. }