get mails
parent
377047a8c9
commit
fa4f21b587
|
@ -1,4 +1,6 @@
|
|||
using MailKit.Net.Smtp;
|
||||
using MailKit;
|
||||
using MailKit.Net.Imap;
|
||||
using MailKit.Net.Smtp;
|
||||
using MailKit.Security;
|
||||
using MimeKit;
|
||||
using Mono.Addins;
|
||||
|
@ -11,6 +13,7 @@ using System.Collections.Generic;
|
|||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Timers;
|
||||
|
||||
[assembly: Addin("MailKitMailModule", "0.1")]
|
||||
[assembly: AddinDependency("OpenSim.Region.Framework", OpenSim.VersionInfo.VersionNumber)]
|
||||
|
@ -24,6 +27,10 @@ namespace OpenSim.Modules.EMail
|
|||
|
||||
private Scene m_scene = null;
|
||||
|
||||
private Timer m_timer = null;
|
||||
|
||||
private List<MimeMessage> m_messages = new List<MimeMessage>();
|
||||
|
||||
private String SMTP_SERVER_HOSTNAME = null;
|
||||
private String SMTP_SERVER_LOGIN = null;
|
||||
private String SMTP_SERVER_PASSWORD = null;
|
||||
|
@ -39,6 +46,7 @@ namespace OpenSim.Modules.EMail
|
|||
private bool IMAP_SERVER_SSL = false;
|
||||
private bool IMAP_SERVER_TLS = false;
|
||||
|
||||
|
||||
#region ISharedRegionModule
|
||||
public string Name
|
||||
{
|
||||
|
@ -57,6 +65,13 @@ namespace OpenSim.Modules.EMail
|
|||
|
||||
scene.RegisterModuleInterface<IEmailModule>(this);
|
||||
m_scene = scene;
|
||||
|
||||
m_timer = new Timer();
|
||||
|
||||
m_timer.Interval = 1000 * 60;
|
||||
m_timer.Elapsed += checkForMails;
|
||||
m_timer.Enabled = true;
|
||||
m_timer.Start();
|
||||
}
|
||||
|
||||
public void Close()
|
||||
|
@ -108,6 +123,52 @@ namespace OpenSim.Modules.EMail
|
|||
}
|
||||
#endregion
|
||||
|
||||
private void checkForMails(object sender, ElapsedEventArgs e)
|
||||
{
|
||||
using (var client = new ImapClient())
|
||||
{
|
||||
client.CheckCertificateRevocation = false;
|
||||
|
||||
if (IMAP_SERVER_SSL == true)
|
||||
{
|
||||
client.Connect(IMAP_SERVER_HOSTNAME, IMAP_SERVER_PORT, SecureSocketOptions.Auto);
|
||||
}
|
||||
else if (IMAP_SERVER_SSL == true)
|
||||
{
|
||||
client.Connect(IMAP_SERVER_HOSTNAME, IMAP_SERVER_PORT, SecureSocketOptions.StartTlsWhenAvailable);
|
||||
}
|
||||
else
|
||||
{
|
||||
client.Connect(IMAP_SERVER_HOSTNAME, IMAP_SERVER_PORT, SecureSocketOptions.None);
|
||||
}
|
||||
|
||||
if (IMAP_SERVER_LOGIN != String.Empty && IMAP_SERVER_PASSWORD != String.Empty)
|
||||
client.Authenticate(IMAP_SERVER_LOGIN, IMAP_SERVER_PASSWORD);
|
||||
|
||||
|
||||
IMailFolder IMAPInbox = client.Inbox;
|
||||
IMAPInbox.Open(FolderAccess.ReadWrite);
|
||||
|
||||
for (int i = 0; i < IMAPInbox.Count; i++)
|
||||
{
|
||||
MimeMessage message = IMAPInbox.GetMessage(i);
|
||||
|
||||
foreach(MailboxAddress adress in message.To.Mailboxes)
|
||||
{
|
||||
SceneObjectPart sceneObject = m_scene.GetSceneObjectPart(adress.Address.Split('@')[0]);
|
||||
|
||||
if(sceneObject != null)
|
||||
{
|
||||
m_messages.Add(message);
|
||||
IMAPInbox.SetFlags(i, MessageFlags.Deleted, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
IMAPInbox.Expunge();
|
||||
client.Disconnect(true);
|
||||
}
|
||||
}
|
||||
|
||||
public void SendEmail(UUID objectID, string address, string subject, string body)
|
||||
{
|
||||
|
@ -141,7 +202,7 @@ namespace OpenSim.Modules.EMail
|
|||
}
|
||||
|
||||
if(SMTP_SERVER_LOGIN != String.Empty && SMTP_SERVER_PASSWORD != String.Empty)
|
||||
client.Authenticate(SMTP_SERVER_LOGIN, SMTP_SERVER_PASSWORD);
|
||||
client.Authenticate(SMTP_SERVER_LOGIN, SMTP_SERVER_PASSWORD);
|
||||
|
||||
client.Send(message);
|
||||
client.Disconnect(true);
|
||||
|
@ -150,6 +211,25 @@ namespace OpenSim.Modules.EMail
|
|||
|
||||
public Email GetNextEmail(UUID objectID, string sender, string subject)
|
||||
{
|
||||
SceneObjectPart sceneObject = m_scene.GetSceneObjectPart(objectID);
|
||||
List<MimeMessage> messages = m_messages.FindAll(X => X.Subject.Contains(subject) && X.Sender.GetAddress(true).Contains(sender));
|
||||
|
||||
foreach (MimeMessage message in messages)
|
||||
{
|
||||
foreach (MailboxAddress adress in message.To.Mailboxes)
|
||||
{
|
||||
Email lslMessage = new Email();
|
||||
lslMessage.time = messages[0].Date.ToUnixTimeSeconds().ToString();
|
||||
lslMessage.message = messages[0].Body.ToString();
|
||||
lslMessage.sender = messages[0].Sender.GetAddress(true);
|
||||
lslMessage.subject = messages[0].Subject;
|
||||
|
||||
m_messages.Remove(messages[0]);
|
||||
|
||||
lslMessage.numLeft = messages.Count - 1;
|
||||
return lslMessage;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue