aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/CoreModules/Scripting/EMailModules/EmailModule.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/CoreModules/Scripting/EMailModules/EmailModule.cs')
-rw-r--r--OpenSim/Region/CoreModules/Scripting/EMailModules/EmailModule.cs288
1 files changed, 288 insertions, 0 deletions
diff --git a/OpenSim/Region/CoreModules/Scripting/EMailModules/EmailModule.cs b/OpenSim/Region/CoreModules/Scripting/EMailModules/EmailModule.cs
new file mode 100644
index 0000000..c23ff1e
--- /dev/null
+++ b/OpenSim/Region/CoreModules/Scripting/EMailModules/EmailModule.cs
@@ -0,0 +1,288 @@
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
28using System;
29using System.Reflection;
30using System.Collections.Generic;
31using System.Text.RegularExpressions;
32using OpenMetaverse;
33using OpenSim.Framework;
34using OpenSim.Region.Framework.Interfaces;
35using OpenSim.Region.Framework.Scenes;
36using log4net;
37using Nini.Config;
38using DotNetOpenMail;
39using DotNetOpenMail.SmtpAuth;
40
41namespace OpenSim.Region.CoreModules.Scripting.EmailModules
42{
43 public class EmailModule : IEmailModule
44 {
45 //
46 // Log
47 //
48 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
49
50 //
51 // Module vars
52 //
53 private IConfigSource m_Config;
54 private string m_HostName = string.Empty;
55 //private string m_RegionName = string.Empty;
56 private string SMTP_SERVER_HOSTNAME = string.Empty;
57 private int SMTP_SERVER_PORT = 25;
58 private string SMTP_SERVER_LOGIN = string.Empty;
59 private string SMTP_SERVER_PASSWORD = string.Empty;
60
61 // Scenes by Region Handle
62 private Dictionary<ulong, Scene> m_Scenes =
63 new Dictionary<ulong, Scene>();
64
65 private bool m_Enabled = false;
66
67 public void Initialise(Scene scene, IConfigSource config)
68 {
69 m_Config = config;
70 IConfig SMTPConfig;
71
72 //FIXME: RegionName is correct??
73 //m_RegionName = scene.RegionInfo.RegionName;
74
75 IConfig startupConfig = m_Config.Configs["Startup"];
76
77 m_Enabled = (startupConfig.GetString("emailmodule", "DefaultEmailModule") == "DefaultEmailModule");
78
79 //Load SMTP SERVER config
80 try
81 {
82 if ((SMTPConfig = m_Config.Configs["SMTP"]) == null)
83 {
84 m_log.InfoFormat("[SMTP] SMTP server not configured");
85 m_Enabled = false;
86 return;
87 }
88
89 if (!SMTPConfig.GetBoolean("enabled", false))
90 {
91 m_log.InfoFormat("[SMTP] module disabled in configuration");
92 m_Enabled = false;
93 return;
94 }
95
96 m_HostName = SMTPConfig.GetString("host_domain_header_from", m_HostName);
97 SMTP_SERVER_HOSTNAME = SMTPConfig.GetString("SMTP_SERVER_HOSTNAME",SMTP_SERVER_HOSTNAME);
98 SMTP_SERVER_PORT = SMTPConfig.GetInt("SMTP_SERVER_PORT", SMTP_SERVER_PORT);
99 SMTP_SERVER_LOGIN = SMTPConfig.GetString("SMTP_SERVER_LOGIN", SMTP_SERVER_LOGIN);
100 SMTP_SERVER_PASSWORD = SMTPConfig.GetString("SMTP_SERVER_PASSWORD", SMTP_SERVER_PASSWORD);
101 }
102 catch (Exception e)
103 {
104 m_log.Error("[EMAIL] DefaultEmailModule not configured: "+ e.Message);
105 m_Enabled = false;
106 return;
107 }
108
109 // It's a go!
110 if (m_Enabled)
111 {
112 lock (m_Scenes)
113 {
114 // Claim the interface slot
115 scene.RegisterModuleInterface<IEmailModule>(this);
116
117 // Add to scene list
118 if (m_Scenes.ContainsKey(scene.RegionInfo.RegionHandle))
119 {
120 m_Scenes[scene.RegionInfo.RegionHandle] = scene;
121 }
122 else
123 {
124 m_Scenes.Add(scene.RegionInfo.RegionHandle, scene);
125 }
126 }
127
128 m_log.Info("[EMAIL] Activated DefaultEmailModule");
129 }
130 }
131
132 public void PostInitialise()
133 {
134 }
135
136 public void Close()
137 {
138 }
139
140 public string Name
141 {
142 get { return "DefaultEmailModule"; }
143 }
144
145 public bool IsSharedModule
146 {
147 get { return true; }
148 }
149
150 /// <summary>
151 ///
152 /// </summary>
153 /// <param name="seconds"></param>
154 private void DelayInSeconds(int seconds)
155 {
156 TimeSpan DiffDelay = new TimeSpan(0, 0, seconds);
157 DateTime EndDelay = DateTime.Now.Add(DiffDelay);
158 while (DateTime.Now < EndDelay)
159 {
160 ;//Do nothing!!
161 }
162 }
163
164 private SceneObjectPart findPrim(UUID objectID, out string ObjectRegionName)
165 {
166 lock (m_Scenes)
167 {
168 foreach (Scene s in m_Scenes.Values)
169 {
170 SceneObjectPart part = s.GetSceneObjectPart(objectID);
171 if (part != null)
172 {
173 ObjectRegionName = s.RegionInfo.RegionName;
174 return part;
175 }
176 }
177 }
178 ObjectRegionName = string.Empty;
179 return null;
180 }
181
182 private void resolveNamePositionRegionName(UUID objectID, out string ObjectName, out string ObjectAbsolutePosition, out string ObjectRegionName)
183 {
184 string m_ObjectRegionName;
185 SceneObjectPart part = findPrim(objectID, out m_ObjectRegionName);
186 if (part != null)
187 {
188 ObjectAbsolutePosition = part.AbsolutePosition.ToString();
189 ObjectName = part.Name;
190 ObjectRegionName = m_ObjectRegionName;
191 return;
192 }
193 ObjectAbsolutePosition = part.AbsolutePosition.ToString();
194 ObjectName = part.Name;
195 ObjectRegionName = m_ObjectRegionName;
196 return;
197 }
198
199 /// <summary>
200 /// SendMail function utilized by llEMail
201 /// </summary>
202 /// <param name="objectID"></param>
203 /// <param name="address"></param>
204 /// <param name="subject"></param>
205 /// <param name="body"></param>
206 public void SendEmail(UUID objectID, string address, string subject, string body)
207 {
208 //Check if address is empty
209 if (address == string.Empty)
210 return;
211
212 //FIXED:Check the email is correct form in REGEX
213 string EMailpatternStrict = @"^(([^<>()[\]\\.,;:\s@\""]+"
214 + @"(\.[^<>()[\]\\.,;:\s@\""]+)*)|(\"".+\""))@"
215 + @"((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
216 + @"\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+"
217 + @"[a-zA-Z]{2,}))$";
218 Regex EMailreStrict = new Regex(EMailpatternStrict);
219 bool isEMailStrictMatch = EMailreStrict.IsMatch(address);
220 if (!isEMailStrictMatch)
221 {
222 m_log.Error("[EMAIL] REGEX Problem in EMail Address: "+address);
223 return;
224 }
225 //FIXME:Check if subject + body = 4096 Byte
226 if ((subject.Length + body.Length) > 1024)
227 {
228 m_log.Error("[EMAIL] subject + body > 1024 Byte");
229 return;
230 }
231
232 try
233 {
234 string LastObjectName = string.Empty;
235 string LastObjectPosition = string.Empty;
236 string LastObjectRegionName = string.Empty;
237 //DONE: Message as Second Life style
238 //20 second delay - AntiSpam System - for now only 10 seconds
239 DelayInSeconds(10);
240 //Creation EmailMessage
241 EmailMessage emailMessage = new EmailMessage();
242 //From
243 emailMessage.FromAddress = new EmailAddress(objectID.ToString()+"@"+m_HostName);
244 //To - Only One
245 emailMessage.AddToAddress(new EmailAddress(address));
246 //Subject
247 emailMessage.Subject = subject;
248 //TEXT Body
249 resolveNamePositionRegionName(objectID, out LastObjectName, out LastObjectPosition, out LastObjectRegionName);
250 emailMessage.TextPart = new TextAttachment("Object-Name: " + LastObjectName +
251 "\r\nRegion: " + LastObjectRegionName + "\r\nLocal-Position: " +
252 LastObjectPosition+"\r\n\r\n\r\n" + body);
253 //HTML Body
254 emailMessage.HtmlPart = new HtmlAttachment("<html><body><p>" +
255 "<BR>Object-Name: " + LastObjectName +
256 "<BR>Region: " + LastObjectRegionName +
257 "<BR>Local-Position: " + LastObjectPosition + "<BR><BR><BR>"
258 +body+"\r\n</p></body><html>");
259
260 //Set SMTP SERVER config
261 SmtpServer smtpServer=new SmtpServer(SMTP_SERVER_HOSTNAME,SMTP_SERVER_PORT);
262 //Authentication
263 smtpServer.SmtpAuthToken=new SmtpAuthToken(SMTP_SERVER_LOGIN, SMTP_SERVER_PASSWORD);
264 //Send Email Message
265 emailMessage.Send(smtpServer);
266 //Log
267 m_log.Info("[EMAIL] EMail sent to: " + address + " from object: " + objectID.ToString());
268 }
269 catch (Exception e)
270 {
271 m_log.Error("[EMAIL] DefaultEmailModule Exception: "+e.Message);
272 return;
273 }
274 }
275
276 /// <summary>
277 ///
278 /// </summary>
279 /// <param name="objectID"></param>
280 /// <param name="sender"></param>
281 /// <param name="subject"></param>
282 /// <returns></returns>
283 public Email GetNextEmail(UUID objectID, string sender, string subject)
284 {
285 return null;
286 }
287 }
288}