EmailModule.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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 OpenSimulator 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.Collections.Generic;
  29. using System.Reflection;
  30. using System.Text.RegularExpressions;
  31. using DotNetOpenMail;
  32. using DotNetOpenMail.SmtpAuth;
  33. using log4net;
  34. using Nini.Config;
  35. using OpenMetaverse;
  36. using OpenSim.Framework;
  37. using OpenSim.Region.Framework.Interfaces;
  38. using OpenSim.Region.Framework.Scenes;
  39. using Mono.Addins;
  40. namespace OpenSim.Region.CoreModules.Scripting.EmailModules
  41. {
  42. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "EmailModule")]
  43. public class EmailModule : ISharedRegionModule, IEmailModule
  44. {
  45. //
  46. // Log
  47. //
  48. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  49. //
  50. // Module vars
  51. //
  52. private IConfigSource m_Config;
  53. private string m_HostName = string.Empty;
  54. //private string m_RegionName = string.Empty;
  55. private string SMTP_SERVER_HOSTNAME = string.Empty;
  56. private int SMTP_SERVER_PORT = 25;
  57. private string SMTP_SERVER_LOGIN = string.Empty;
  58. private string SMTP_SERVER_PASSWORD = string.Empty;
  59. private int m_MaxQueueSize = 50; // maximum size of an object mail queue
  60. private Dictionary<UUID, List<Email>> m_MailQueues = new Dictionary<UUID, List<Email>>();
  61. private Dictionary<UUID, DateTime> m_LastGetEmailCall = new Dictionary<UUID, DateTime>();
  62. private TimeSpan m_QueueTimeout = new TimeSpan(2, 0, 0); // 2 hours without llGetNextEmail drops the queue
  63. private string m_InterObjectHostname = "lsl.opensim.local";
  64. private int m_MaxEmailSize = 4096; // largest email allowed by default, as per lsl docs.
  65. // Scenes by Region Handle
  66. private Dictionary<ulong, Scene> m_Scenes =
  67. new Dictionary<ulong, Scene>();
  68. private bool m_Enabled = false;
  69. #region ISharedRegionModule
  70. public void Initialise(IConfigSource config)
  71. {
  72. m_Config = config;
  73. IConfig SMTPConfig;
  74. //FIXME: RegionName is correct??
  75. //m_RegionName = scene.RegionInfo.RegionName;
  76. IConfig startupConfig = m_Config.Configs["Startup"];
  77. m_Enabled = (startupConfig.GetString("emailmodule", "DefaultEmailModule") == "DefaultEmailModule");
  78. //Load SMTP SERVER config
  79. try
  80. {
  81. if ((SMTPConfig = m_Config.Configs["SMTP"]) == null)
  82. {
  83. m_Enabled = false;
  84. return;
  85. }
  86. if (!SMTPConfig.GetBoolean("enabled", false))
  87. {
  88. m_Enabled = false;
  89. return;
  90. }
  91. m_HostName = SMTPConfig.GetString("host_domain_header_from", m_HostName);
  92. m_InterObjectHostname = SMTPConfig.GetString("internal_object_host", m_InterObjectHostname);
  93. SMTP_SERVER_HOSTNAME = SMTPConfig.GetString("SMTP_SERVER_HOSTNAME", SMTP_SERVER_HOSTNAME);
  94. SMTP_SERVER_PORT = SMTPConfig.GetInt("SMTP_SERVER_PORT", SMTP_SERVER_PORT);
  95. SMTP_SERVER_LOGIN = SMTPConfig.GetString("SMTP_SERVER_LOGIN", SMTP_SERVER_LOGIN);
  96. SMTP_SERVER_PASSWORD = SMTPConfig.GetString("SMTP_SERVER_PASSWORD", SMTP_SERVER_PASSWORD);
  97. m_MaxEmailSize = SMTPConfig.GetInt("email_max_size", m_MaxEmailSize);
  98. }
  99. catch (Exception e)
  100. {
  101. m_log.Error("[EMAIL]: DefaultEmailModule not configured: " + e.Message);
  102. m_Enabled = false;
  103. return;
  104. }
  105. }
  106. public void AddRegion(Scene scene)
  107. {
  108. if (!m_Enabled)
  109. return;
  110. // It's a go!
  111. lock (m_Scenes)
  112. {
  113. // Claim the interface slot
  114. scene.RegisterModuleInterface<IEmailModule>(this);
  115. // Add to scene list
  116. if (m_Scenes.ContainsKey(scene.RegionInfo.RegionHandle))
  117. {
  118. m_Scenes[scene.RegionInfo.RegionHandle] = scene;
  119. }
  120. else
  121. {
  122. m_Scenes.Add(scene.RegionInfo.RegionHandle, scene);
  123. }
  124. }
  125. m_log.Info("[EMAIL]: Activated DefaultEmailModule");
  126. }
  127. public void RemoveRegion(Scene scene)
  128. {
  129. }
  130. public void PostInitialise()
  131. {
  132. }
  133. public void Close()
  134. {
  135. }
  136. public string Name
  137. {
  138. get { return "DefaultEmailModule"; }
  139. }
  140. public Type ReplaceableInterface
  141. {
  142. get { return null; }
  143. }
  144. public void RegionLoaded(Scene scene)
  145. {
  146. }
  147. #endregion
  148. public void InsertEmail(UUID to, Email email)
  149. {
  150. // It's tempting to create the queue here. Don't; objects which have
  151. // not yet called GetNextEmail should have no queue, and emails to them
  152. // should be silently dropped.
  153. lock (m_MailQueues)
  154. {
  155. if (m_MailQueues.ContainsKey(to))
  156. {
  157. if (m_MailQueues[to].Count >= m_MaxQueueSize)
  158. {
  159. // fail silently
  160. return;
  161. }
  162. lock (m_MailQueues[to])
  163. {
  164. m_MailQueues[to].Add(email);
  165. }
  166. }
  167. }
  168. }
  169. private bool IsLocal(UUID objectID)
  170. {
  171. string unused;
  172. return (null != findPrim(objectID, out unused));
  173. }
  174. private SceneObjectPart findPrim(UUID objectID, out string ObjectRegionName)
  175. {
  176. lock (m_Scenes)
  177. {
  178. foreach (Scene s in m_Scenes.Values)
  179. {
  180. SceneObjectPart part = s.GetSceneObjectPart(objectID);
  181. if (part != null)
  182. {
  183. ObjectRegionName = s.RegionInfo.RegionName;
  184. uint localX = s.RegionInfo.WorldLocX;
  185. uint localY = s.RegionInfo.WorldLocY;
  186. ObjectRegionName = ObjectRegionName + " (" + localX + ", " + localY + ")";
  187. return part;
  188. }
  189. }
  190. }
  191. ObjectRegionName = string.Empty;
  192. return null;
  193. }
  194. private bool resolveNamePositionRegionName(UUID objectID, out string ObjectName, out string ObjectAbsolutePosition, out string ObjectRegionName)
  195. {
  196. ObjectName = ObjectAbsolutePosition = ObjectRegionName = String.Empty;
  197. string m_ObjectRegionName;
  198. int objectLocX;
  199. int objectLocY;
  200. int objectLocZ;
  201. SceneObjectPart part = findPrim(objectID, out m_ObjectRegionName);
  202. if (part != null)
  203. {
  204. objectLocX = (int)part.AbsolutePosition.X;
  205. objectLocY = (int)part.AbsolutePosition.Y;
  206. objectLocZ = (int)part.AbsolutePosition.Z;
  207. ObjectAbsolutePosition = "(" + objectLocX + ", " + objectLocY + ", " + objectLocZ + ")";
  208. ObjectName = part.Name;
  209. ObjectRegionName = m_ObjectRegionName;
  210. return true;
  211. }
  212. return false;
  213. }
  214. /// <summary>
  215. /// SendMail function utilized by llEMail
  216. /// </summary>
  217. /// <param name="objectID"></param>
  218. /// <param name="address"></param>
  219. /// <param name="subject"></param>
  220. /// <param name="body"></param>
  221. public void SendEmail(UUID objectID, string address, string subject, string body)
  222. {
  223. //Check if address is empty
  224. if (address == string.Empty)
  225. return;
  226. //FIXED:Check the email is correct form in REGEX
  227. string EMailpatternStrict = @"^(([^<>()[\]\\.,;:\s@\""]+"
  228. + @"(\.[^<>()[\]\\.,;:\s@\""]+)*)|(\"".+\""))@"
  229. + @"((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
  230. + @"\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+"
  231. + @"[a-zA-Z]{2,}))$";
  232. Regex EMailreStrict = new Regex(EMailpatternStrict);
  233. bool isEMailStrictMatch = EMailreStrict.IsMatch(address);
  234. if (!isEMailStrictMatch)
  235. {
  236. m_log.Error("[EMAIL]: REGEX Problem in EMail Address: "+address);
  237. return;
  238. }
  239. if ((subject.Length + body.Length) > m_MaxEmailSize)
  240. {
  241. m_log.Error("[EMAIL]: subject + body larger than limit of " + m_MaxEmailSize + " bytes");
  242. return;
  243. }
  244. string LastObjectName = string.Empty;
  245. string LastObjectPosition = string.Empty;
  246. string LastObjectRegionName = string.Empty;
  247. if (!resolveNamePositionRegionName(objectID, out LastObjectName, out LastObjectPosition, out LastObjectRegionName))
  248. return;
  249. if (!address.EndsWith(m_InterObjectHostname))
  250. {
  251. // regular email, send it out
  252. try
  253. {
  254. //Creation EmailMessage
  255. EmailMessage emailMessage = new EmailMessage();
  256. //From
  257. emailMessage.FromAddress = new EmailAddress(objectID.ToString() + "@" + m_HostName);
  258. //To - Only One
  259. emailMessage.AddToAddress(new EmailAddress(address));
  260. //Subject
  261. emailMessage.Subject = subject;
  262. //TEXT Body
  263. if (!resolveNamePositionRegionName(objectID, out LastObjectName, out LastObjectPosition, out LastObjectRegionName))
  264. return;
  265. emailMessage.BodyText = "Object-Name: " + LastObjectName +
  266. "\nRegion: " + LastObjectRegionName + "\nLocal-Position: " +
  267. LastObjectPosition + "\n\n" + body;
  268. //Config SMTP Server
  269. //Set SMTP SERVER config
  270. SmtpServer smtpServer=new SmtpServer(SMTP_SERVER_HOSTNAME,SMTP_SERVER_PORT);
  271. // Add authentication only when requested
  272. //
  273. if (SMTP_SERVER_LOGIN != String.Empty && SMTP_SERVER_PASSWORD != String.Empty)
  274. {
  275. //Authentication
  276. smtpServer.SmtpAuthToken=new SmtpAuthToken(SMTP_SERVER_LOGIN, SMTP_SERVER_PASSWORD);
  277. }
  278. //Send Email Message
  279. emailMessage.Send(smtpServer);
  280. //Log
  281. m_log.Info("[EMAIL]: EMail sent to: " + address + " from object: " + objectID.ToString() + "@" + m_HostName);
  282. }
  283. catch (Exception e)
  284. {
  285. m_log.Error("[EMAIL]: DefaultEmailModule Exception: " + e.Message);
  286. }
  287. }
  288. else
  289. {
  290. // inter object email, keep it in the family
  291. Email email = new Email();
  292. email.time = ((int)((DateTime.UtcNow - new DateTime(1970,1,1,0,0,0)).TotalSeconds)).ToString();
  293. email.subject = subject;
  294. email.sender = objectID.ToString() + "@" + m_InterObjectHostname;
  295. email.message = "Object-Name: " + LastObjectName +
  296. "\nRegion: " + LastObjectRegionName + "\nLocal-Position: " +
  297. LastObjectPosition + "\n\n" + body;
  298. string guid = address.Substring(0, address.IndexOf("@"));
  299. UUID toID = new UUID(guid);
  300. if (IsLocal(toID)) // TODO FIX check to see if it is local
  301. {
  302. // object in this region
  303. InsertEmail(toID, email);
  304. }
  305. else
  306. {
  307. // object on another region
  308. // TODO FIX
  309. }
  310. }
  311. }
  312. /// <summary>
  313. ///
  314. /// </summary>
  315. /// <param name="objectID"></param>
  316. /// <param name="sender"></param>
  317. /// <param name="subject"></param>
  318. /// <returns></returns>
  319. public Email GetNextEmail(UUID objectID, string sender, string subject)
  320. {
  321. List<Email> queue = null;
  322. lock (m_LastGetEmailCall)
  323. {
  324. if (m_LastGetEmailCall.ContainsKey(objectID))
  325. {
  326. m_LastGetEmailCall.Remove(objectID);
  327. }
  328. m_LastGetEmailCall.Add(objectID, DateTime.Now);
  329. // Hopefully this isn't too time consuming. If it is, we can always push it into a worker thread.
  330. DateTime now = DateTime.Now;
  331. List<UUID> removal = new List<UUID>();
  332. foreach (UUID uuid in m_LastGetEmailCall.Keys)
  333. {
  334. if ((now - m_LastGetEmailCall[uuid]) > m_QueueTimeout)
  335. {
  336. removal.Add(uuid);
  337. }
  338. }
  339. foreach (UUID remove in removal)
  340. {
  341. m_LastGetEmailCall.Remove(remove);
  342. lock (m_MailQueues)
  343. {
  344. m_MailQueues.Remove(remove);
  345. }
  346. }
  347. }
  348. lock (m_MailQueues)
  349. {
  350. if (m_MailQueues.ContainsKey(objectID))
  351. {
  352. queue = m_MailQueues[objectID];
  353. }
  354. }
  355. if (queue != null)
  356. {
  357. lock (queue)
  358. {
  359. if (queue.Count > 0)
  360. {
  361. int i;
  362. for (i = 0; i < queue.Count; i++)
  363. {
  364. if ((sender == null || sender.Equals("") || sender.Equals(queue[i].sender)) &&
  365. (subject == null || subject.Equals("") || subject.Equals(queue[i].subject)))
  366. {
  367. break;
  368. }
  369. }
  370. if (i != queue.Count)
  371. {
  372. Email ret = queue[i];
  373. queue.Remove(ret);
  374. ret.numLeft = queue.Count;
  375. return ret;
  376. }
  377. }
  378. }
  379. }
  380. else
  381. {
  382. lock (m_MailQueues)
  383. {
  384. m_MailQueues.Add(objectID, new List<Email>());
  385. }
  386. }
  387. return null;
  388. }
  389. }
  390. }