1
0

EmailModule.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Reflection;
  29. using System.Collections.Generic;
  30. using System.Text.RegularExpressions;
  31. using OpenMetaverse;
  32. using OpenSim.Framework;
  33. using OpenSim.Region.Environment.Interfaces;
  34. using OpenSim.Region.Environment.Scenes;
  35. using log4net;
  36. using Nini.Config;
  37. using DotNetOpenMail;
  38. using DotNetOpenMail.SmtpAuth;
  39. namespace OpenSim.Region.Environment.Modules.Scripting.EmailModules
  40. {
  41. public class EmailModule : IEmailModule
  42. {
  43. //
  44. // Log
  45. //
  46. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  47. //
  48. // Module vars
  49. //
  50. private IConfigSource m_Config;
  51. private string m_HostName = string.Empty;
  52. //private string m_RegionName = string.Empty;
  53. private string SMTP_SERVER_HOSTNAME = string.Empty;
  54. private int SMTP_SERVER_PORT = 25;
  55. private string SMTP_SERVER_LOGIN = string.Empty;
  56. private string SMTP_SERVER_PASSWORD = string.Empty;
  57. // Scenes by Region Handle
  58. private Dictionary<ulong, Scene> m_Scenes =
  59. new Dictionary<ulong, Scene>();
  60. private bool m_Enabled = false;
  61. public void Initialise(Scene scene, IConfigSource config)
  62. {
  63. m_Config = config;
  64. IConfig SMTPConfig;
  65. //FIXME: RegionName is correct??
  66. //m_RegionName = scene.RegionInfo.RegionName;
  67. IConfig startupConfig = m_Config.Configs["Startup"];
  68. m_Enabled = (startupConfig.GetString("emailmodule", "DefaultEmailModule") == "DefaultEmailModule");
  69. //Load SMTP SERVER config
  70. try
  71. {
  72. if ((SMTPConfig = m_Config.Configs["SMTP"]) == null)
  73. {
  74. m_log.InfoFormat("[SMTP] SMTP server not configured");
  75. m_Enabled = false;
  76. return;
  77. }
  78. if (!SMTPConfig.GetBoolean("enabled", false))
  79. {
  80. m_log.InfoFormat("[SMTP] module disabled in configuration");
  81. m_Enabled = false;
  82. return;
  83. }
  84. m_HostName = SMTPConfig.GetString("host_domain_header_from", m_HostName);
  85. SMTP_SERVER_HOSTNAME = SMTPConfig.GetString("SMTP_SERVER_HOSTNAME",SMTP_SERVER_HOSTNAME);
  86. SMTP_SERVER_PORT = SMTPConfig.GetInt("SMTP_SERVER_PORT", SMTP_SERVER_PORT);
  87. SMTP_SERVER_LOGIN = SMTPConfig.GetString("SMTP_SERVER_LOGIN", SMTP_SERVER_LOGIN);
  88. SMTP_SERVER_PASSWORD = SMTPConfig.GetString("SMTP_SERVER_PASSWORD", SMTP_SERVER_PASSWORD);
  89. }
  90. catch (Exception e)
  91. {
  92. m_log.Error("[EMAIL] DefaultEmailModule not configured: "+ e.Message);
  93. m_Enabled = false;
  94. return;
  95. }
  96. // It's a go!
  97. if (m_Enabled)
  98. {
  99. lock (m_Scenes)
  100. {
  101. // Claim the interface slot
  102. scene.RegisterModuleInterface<IEmailModule>(this);
  103. // Add to scene list
  104. if (m_Scenes.ContainsKey(scene.RegionInfo.RegionHandle))
  105. {
  106. m_Scenes[scene.RegionInfo.RegionHandle] = scene;
  107. }
  108. else
  109. {
  110. m_Scenes.Add(scene.RegionInfo.RegionHandle, scene);
  111. }
  112. }
  113. m_log.Info("[EMAIL] Activated DefaultEmailModule");
  114. }
  115. }
  116. public void PostInitialise()
  117. {
  118. }
  119. public void Close()
  120. {
  121. }
  122. public string Name
  123. {
  124. get { return "DefaultEmailModule"; }
  125. }
  126. public bool IsSharedModule
  127. {
  128. get { return true; }
  129. }
  130. /// <summary>
  131. ///
  132. /// </summary>
  133. /// <param name="seconds"></param>
  134. private void DelayInSeconds(int seconds)
  135. {
  136. TimeSpan DiffDelay = new TimeSpan(0, 0, seconds);
  137. DateTime EndDelay = DateTime.Now.Add(DiffDelay);
  138. while (DateTime.Now < EndDelay)
  139. {
  140. ;//Do nothing!!
  141. }
  142. }
  143. private SceneObjectPart findPrim(UUID objectID, out string ObjectRegionName)
  144. {
  145. lock (m_Scenes)
  146. {
  147. foreach (Scene s in m_Scenes.Values)
  148. {
  149. SceneObjectPart part = s.GetSceneObjectPart(objectID);
  150. if (part != null)
  151. {
  152. ObjectRegionName = s.RegionInfo.RegionName;
  153. return part;
  154. }
  155. }
  156. }
  157. ObjectRegionName = string.Empty;
  158. return null;
  159. }
  160. private void resolveNamePositionRegionName(UUID objectID, out string ObjectName, out string ObjectAbsolutePosition, out string ObjectRegionName)
  161. {
  162. string m_ObjectRegionName;
  163. SceneObjectPart part = findPrim(objectID, out m_ObjectRegionName);
  164. if (part != null)
  165. {
  166. ObjectAbsolutePosition = part.AbsolutePosition.ToString();
  167. ObjectName = part.Name;
  168. ObjectRegionName = m_ObjectRegionName;
  169. return;
  170. }
  171. ObjectAbsolutePosition = part.AbsolutePosition.ToString();
  172. ObjectName = part.Name;
  173. ObjectRegionName = m_ObjectRegionName;
  174. return;
  175. }
  176. /// <summary>
  177. /// SendMail function utilized by llEMail
  178. /// </summary>
  179. /// <param name="objectID"></param>
  180. /// <param name="address"></param>
  181. /// <param name="subject"></param>
  182. /// <param name="body"></param>
  183. public void SendEmail(UUID objectID, string address, string subject, string body)
  184. {
  185. //Check if address is empty
  186. if (address == string.Empty)
  187. return;
  188. //FIXED:Check the email is correct form in REGEX
  189. string EMailpatternStrict = @"^(([^<>()[\]\\.,;:\s@\""]+"
  190. + @"(\.[^<>()[\]\\.,;:\s@\""]+)*)|(\"".+\""))@"
  191. + @"((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
  192. + @"\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+"
  193. + @"[a-zA-Z]{2,}))$";
  194. Regex EMailreStrict = new Regex(EMailpatternStrict);
  195. bool isEMailStrictMatch = EMailreStrict.IsMatch(address);
  196. if (!isEMailStrictMatch)
  197. {
  198. m_log.Error("[EMAIL] REGEX Problem in EMail Address: "+address);
  199. return;
  200. }
  201. //FIXME:Check if subject + body = 4096 Byte
  202. if ((subject.Length + body.Length) > 1024)
  203. {
  204. m_log.Error("[EMAIL] subject + body > 1024 Byte");
  205. return;
  206. }
  207. try
  208. {
  209. string LastObjectName = string.Empty;
  210. string LastObjectPosition = string.Empty;
  211. string LastObjectRegionName = string.Empty;
  212. //DONE: Message as Second Life style
  213. //20 second delay - AntiSpam System - for now only 10 seconds
  214. DelayInSeconds(10);
  215. //Creation EmailMessage
  216. EmailMessage emailMessage = new EmailMessage();
  217. //From
  218. emailMessage.FromAddress = new EmailAddress(objectID.ToString()+"@"+m_HostName);
  219. //To - Only One
  220. emailMessage.AddToAddress(new EmailAddress(address));
  221. //Subject
  222. emailMessage.Subject = subject;
  223. //TEXT Body
  224. resolveNamePositionRegionName(objectID, out LastObjectName, out LastObjectPosition, out LastObjectRegionName);
  225. emailMessage.TextPart = new TextAttachment("Object-Name: " + LastObjectName +
  226. "\r\nRegion: " + LastObjectRegionName + "\r\nLocal-Position: " +
  227. LastObjectPosition+"\r\n\r\n\r\n" + body);
  228. //HTML Body
  229. emailMessage.HtmlPart = new HtmlAttachment("<html><body><p>" +
  230. "<BR>Object-Name: " + LastObjectName +
  231. "<BR>Region: " + LastObjectRegionName +
  232. "<BR>Local-Position: " + LastObjectPosition + "<BR><BR><BR>"
  233. +body+"\r\n</p></body><html>");
  234. //Set SMTP SERVER config
  235. SmtpServer smtpServer=new SmtpServer(SMTP_SERVER_HOSTNAME,SMTP_SERVER_PORT);
  236. //Authentication
  237. smtpServer.SmtpAuthToken=new SmtpAuthToken(SMTP_SERVER_LOGIN, SMTP_SERVER_PASSWORD);
  238. //Send Email Message
  239. emailMessage.Send(smtpServer);
  240. //Log
  241. m_log.Info("[EMAIL] EMail sent to: " + address + " from object: " + objectID.ToString());
  242. }
  243. catch (Exception e)
  244. {
  245. m_log.Error("[EMAIL] DefaultEmailModule Exception: "+e.Message);
  246. return;
  247. }
  248. }
  249. /// <summary>
  250. ///
  251. /// </summary>
  252. /// <param name="objectID"></param>
  253. /// <param name="sender"></param>
  254. /// <param name="subject"></param>
  255. /// <returns></returns>
  256. public Email GetNextEmail(UUID objectID, string sender, string subject)
  257. {
  258. return null;
  259. }
  260. }
  261. }