namespace ReadyPOP3
{
using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
using System;
using System.Collections;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
public class Message
{
private string boundary;
private ArrayList m_AttachList;
private bool m_Attachments;
private ReadyPOP3.Body m_Body;
private string m_CC;
private ArrayList m_Children;
private string m_ContentType;
private string m_Date;
private string m_From;
private ArrayList m_Headers;
private string m_HTMLBody;
private string m_ID;
private int m_MaxBufferSize;
private string m_MessageData;
private ReadyPOP3.MIMEAttachment m_MIMEAttachment;
private bool m_MultipartMIME;
private string m_PlainBody;
private string m_Subject;
private string m_To;
public Message()
{
this.m_MaxBufferSize = 0x100000;
this.m_Headers = new ArrayList();
this.m_Body = new ReadyPOP3.Body();
this.m_MultipartMIME = false;
this.m_Children = new ArrayList();
this.m_AttachList = new ArrayList();
}
public Message(string msg, [Optional, DefaultParameterValue(false)] bool forceMIME) : this()
{
if (forceMIME)
{
this.Headers.Add(new Header("MIME-Version", "1.0"));
}
this.m_MessageData = msg;
this.ParseMessage();
this.CheckAttachments(this);
}
private void CheckAttachments(Message msg)
{
if (msg.IsMIME())
{
if (!msg.IsMultipartMIME)
{
if (msg.MIMEAttachment.ContentTransferEncoding.StartsWith("quoted-printable") & msg.GetHeader("Content-type").Attributes.ContainsKey("name"))
{
this.m_Attachments = true;
object objectValue = RuntimeHelpers.GetObjectValue(msg.GetHeader("Content-type").Attributes["name"]);
this.m_AttachList.Add(msg.MIMEAttachment);
msg.MIMEAttachment.Name = StringType.FromObject(objectValue);
}
else if (!(msg.MIMEAttachment.ContentTransferEncoding.StartsWith("quoted-printable") & !msg.GetHeader("Content-type").Attributes.ContainsKey("name")))
{
if (!(msg.MIMEAttachment.ContentType.StartsWith("text/html") & !msg.GetHeader("Content-type").Attributes.ContainsKey("name")))
{
if (!(msg.MIMEAttachment.ContentType.StartsWith("text/plain") & !msg.GetHeader("Content-type").Attributes.ContainsKey("name")))
{
if (!(msg.MIMEAttachment.ContentType.StartsWith("TEXT/PLAIN") & !msg.GetHeader("Content-type").Attributes.ContainsKey("name")))
{
if (!msg.MIMEAttachment.ContentTransferEncoding.StartsWith("quoted-printable"))
{
if ((msg.GetHeader("Content-type") != null) && msg.GetHeader("Content-type").Attributes.ContainsKey("name"))
{
this.m_Attachments = true;
object obj3 = RuntimeHelpers.GetObjectValue(msg.GetHeader("Content-type").Attributes["name"]);
this.m_AttachList.Add(msg.MIMEAttachment);
msg.MIMEAttachment.Name = StringType.FromObject(obj3);
}
else if ((msg.GetHeader("Content-type") != null) && msg.GetHeader("Content-type").Attributes.ContainsKey("Name"))
{
this.m_Attachments = true;
object obj4 = RuntimeHelpers.GetObjectValue(msg.GetHeader("Content-type").Attributes["Name"]);
this.m_AttachList.Add(msg.MIMEAttachment);
msg.MIMEAttachment.Name = StringType.FromObject(obj4);
}
else if ((msg.GetHeader("Content-Type") != null) && msg.GetHeader("Content-Type").Attributes.ContainsKey("name"))
{
this.m_Attachments = true;
object obj5 = RuntimeHelpers.GetObjectValue(msg.GetHeader("Content-Type").Attributes["Name"]);
this.m_AttachList.Add(msg.MIMEAttachment);
msg.MIMEAttachment.Name = StringType.FromObject(obj5);
}
}
}
else
{
this.m_PlainBody = msg.MIMEAttachment.ToaString();
}
}
else
{
this.m_PlainBody = msg.MIMEAttachment.ToaString();
}
}
else
{
this.m_HTMLBody = msg.MIMEAttachment.ToaString();
}
}
else
{
string data = msg.MIMEAttachment.Data;
if (msg.GetHeader("Content-type").Data.StartsWith("text/html"))
{
this.m_HTMLBody = ReadyPOP3.MIMEAttachment.ConvertFromQuotedPrintable(data);
}
else if (msg.GetHeader("Content-type").Data.StartsWith("text/plain"))
{
this.m_PlainBody = ReadyPOP3.MIMEAttachment.ConvertFromQuotedPrintable(data);
}
else if (msg.GetHeader("Content-Type").Data.StartsWith("text/html"))
{
this.m_HTMLBody = ReadyPOP3.MIMEAttachment.ConvertFromQuotedPrintable(data);
}
else if (msg.GetHeader("Content-Type").Data.StartsWith("text/plain"))
{
this.m_PlainBody = ReadyPOP3.MIMEAttachment.ConvertFromQuotedPrintable(data);
}
else if (msg.GetHeader("Content-Type").Data.StartsWith("TEXT/PLAIN"))
{
this.m_PlainBody = ReadyPOP3.MIMEAttachment.ConvertFromQuotedPrintable(data);
}
else if (msg.GetHeader("content-type").Data.StartsWith("text/html"))
{
this.m_HTMLBody = ReadyPOP3.MIMEAttachment.ConvertFromQuotedPrintable(data);
}
else if (msg.GetHeader("content-type").Data.StartsWith("text/plain"))
{
this.m_PlainBody = ReadyPOP3.MIMEAttachment.ConvertFromQuotedPrintable(data);
}
}
}
else
{
Header header = msg.GetHeader("Content-type");
if (header != null)
{
string str = header.Data;
}
int num = 1;
foreach (Message message in msg.Children)
{
if (!Information.IsNothing(message))
{
this.CheckAttachments(message);
num++;
}
}
}
}
else
{
try
{
if (StringType.StrCmp(msg.GetHeader("Content-type").Data, "text/html", false) == 0)
{
this.m_HTMLBody = msg.Body.Data.ToString();
}
else
{
this.m_PlainBody = msg.Body.Data.ToString();
}
}
catch (Exception exception1)
{
ProjectData.SetProjectError(exception1);
try
{
this.m_PlainBody = msg.Body.Data.ToString();
}
catch (Exception exception2)
{
ProjectData.SetProjectError(exception2);
Exception exception = exception2;
ProjectData.ClearProjectError();
}
ProjectData.ClearProjectError();
}
}
}
public Header GetHeader(string hdr)
{
foreach (Header header2 in this.Headers)
{
if (string.Compare(Strings.LCase(header2.Field), Strings.LCase(hdr), true) == 0)
{
return header2;
}
}
return null;
}
private DateTime GetLocalDateTime()
{
TimeSpan zero = new TimeSpan(0);
Header header = this.GetHeader("Date");
if (header == null)
{
return DateTime.Now;
}
Match match = Regex.Match(header.Data, @"^(Fri|Sat|Sun|Mon|Tue|Wed|Thu),\s(\d\d|\d)\s(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s(\d\d|\d\d\d\d)\s(\d|\d\d):(\d|\d\d):(\d|\d\d)\s([\+|-]\d\d\d\d|UT|GMT|EST|EDT|CST|CDT|MST|MDT|PST|PDT|[A-Z]).*$");
if (!match.Success)
{
return DateType.FromObject(null);
}
string str7 = match.Result("$1");
string str = match.Result("$2");
string str4 = match.Result("$3");
string str8 = match.Result("$4");
string str2 = match.Result("$5");
string str3 = match.Result("$6");
string str5 = match.Result("$7");
string str6 = match.Result("$8");
try
{
if (str6.Length == 0)
{
zero = TimeSpan.Zero;
}
int num = 0;
string sLeft = str6;
if ((StringType.StrCmp(sLeft, "UT", false) == 0) || (StringType.StrCmp(sLeft, "GMT", false) == 0))
{
num = 0;
zero = TimeSpan.FromHours((double) num);
}
else if (StringType.StrCmp(sLeft, "EDT", false) == 0)
{
num = -4;
zero = TimeSpan.FromHours((double) num);
}
else if ((StringType.StrCmp(sLeft, "EST", false) == 0) || (StringType.StrCmp(sLeft, "CDT", false) == 0))
{
num = -5;
zero = TimeSpan.FromHours((double) num);
}
else if ((StringType.StrCmp(sLeft, "CST", false) == 0) || (StringType.StrCmp(sLeft, "MDT", false) == 0))
{
num = -6;
zero = TimeSpan.FromHours((double) num);
}
else if ((StringType.StrCmp(sLeft, "MST", false) == 0) || (StringType.StrCmp(sLeft, "PDT", false) == 0))
{
num = -7;
zero = TimeSpan.FromHours((double) num);
}
else if (StringType.StrCmp(sLeft, "PST", false) == 0)
{
num = -8;
zero = TimeSpan.FromHours((double) num);
}
else if (str6.StartsWith("+"))
{
zero = TimeSpan.Parse(str6.Substring(1, 2) + ":" + str6.Substring(3, 2));
}
else
{
zero = TimeSpan.Parse(str6.Insert(str6.Length - 2, ":"));
}
}
catch (Exception exception1)
{
ProjectData.SetProjectError(exception1);
ProjectData.ClearProjectError();
}
return TimeZone.CurrentTimeZone.ToLocalTime(DateTime.Parse(match.Result("$1, $2 $3 $4 $5:$6:$7")).Subtract(zero));
}
private bool IsMIME()
{
Header header = this.GetHeader("MIME-Version");
return ((header != null) && (string.Compare(header.WithoutComment(), "1.0") == 0));
}
private void ParseMessage()
{
try
{
if (this.MessageData.Length == 0)
{
return;
}
}
catch (Exception exception1)
{
ProjectData.SetProjectError(exception1);
ProjectData.ClearProjectError();
}
bool flag = false;
MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(this.MessageData.ToCharArray()));
StreamReader reader = new StreamReader(stream, Encoding.ASCII, true, this.m_MaxBufferSize);
for (string str = reader.ReadLine(); str != null; str = reader.ReadLine())
{
if (!flag)
{
try
{
if (str.Length == 0)
{
flag = true;
}
else
{
Header header = new Header("","");
if ((header != null) && (str.StartsWith(" ") | str.StartsWith("\t")))
{
header.Data = header.Data + " " + str.TrimStart(new char[0]);
}
else
{
object index = str.IndexOf(':');
string fld = str.Substring(0, IntegerType.FromObject(index));
string dat = str.Substring(IntegerType.FromObject(ObjectType.AddObj(index, 1))).TrimStart(new char[0]);
header = new Header(fld, dat);
this.Headers.Add(header);
}
}
}
catch (Exception exception2)
{
ProjectData.SetProjectError(exception2);
ProjectData.ClearProjectError();
}
}
else
{
this.Body.Data.Append(str + "\r\n");
}
}
if (this.IsMIME())
{
this.ParseMIME();
}
}
private void ParseMIME()
{
Header header2 = this.GetHeader("Content-type");
Header header = this.GetHeader("Content-Transfer-Encoding");
if (header2 == null)
{
header2 = new Header("Content-Type", "text/plain");
}
this.m_ContentType = header2.Data;
if (header2.Data.StartsWith("multipart") | header2.Data.StartsWith("MULTIPART"))
{
this.m_MultipartMIME = true;
Match mBoundry = Regex.Match(header2.Data, "boundary=[\"|](.*)[\"|]");
if (mBoundry.Captures.Count == 0) return;
this.boundary = mBoundry.Result("$1");
if (Information.Err().Number != 0)
{
Information.Err().Clear();
this.boundary = Regex.Match(header2.Data, "boundary=(.*)").Result("$1");
}
if (Information.Err().Number != 0)
{
Information.Err().Clear();
this.boundary = Regex.Match(header2.Data, "Boundary=[\"|](.*)[\"|]").Result("$1");
}
if (Information.Err().Number != 0)
{
Information.Err().Clear();
this.boundary = Regex.Match(header2.Data, "Boundary=(.*)").Result("$1");
}
if (Information.Err().Number != 0)
{
Information.Err().Clear();
this.boundary = Regex.Match(header2.Data, "BOUNDARY=[\"|](.*)[\"|]").Result("$1");
}
if (Information.Err().Number != 0)
{
Information.Err().Clear();
this.boundary = Regex.Match(header2.Data, "BOUNDARY=(.*)").Result("$1");
}
string[] sourceArray = Regex.Split(this.Body.Data.ToString(), "--" + this.boundary);
try
{
if (sourceArray.Length != 1)
{
string[] destinationArray = new string[(sourceArray.Length - 3) + 1];
Array.Copy(sourceArray, 1, destinationArray, 0, sourceArray.Length - 2);
foreach (string str in destinationArray)
{
Message message = new Message(Regex.Replace(str, "^\r\n", ""), true);
this.Children.Add(message);
}
}
}
catch (Exception exception1)
{
ProjectData.SetProjectError(exception1);
ProjectData.ClearProjectError();
}
}
else
{
this.m_MultipartMIME = false;
ReadyPOP3.MIMEAttachment attachment = new ReadyPOP3.MIMEAttachment();
attachment.Data = this.Body.Data.ToString();
attachment.ContentType = header2.Data;
if (header == null)
{
header = new Header("Content-Transfer-Encoding", "7bit");
}
attachment.ContentTransferEncoding = header.Data;
this.m_MIMEAttachment = attachment;
}
}
public ArrayList Attachments
{
get
{
return this.m_AttachList;
}
}
public ReadyPOP3.Body Body
{
get
{
return this.m_Body;
}
}
public string CC
{
get
{
try
{
this.m_CC = Strings.Replace(this.GetHeader("Cc").Data, "<", "", 1, -1, CompareMethod.Binary);
this.m_CC = Strings.Replace(this.m_CC, ">", "", 1, -1, CompareMethod.Binary);
}
catch (Exception exception1)
{
ProjectData.SetProjectError(exception1);
ProjectData.ClearProjectError();
}
return this.m_CC;
}
}
private ArrayList Children
{
get
{
return this.m_Children;
}
}
public string ContentType
{
get
{
return this.m_ContentType;
}
}
public string DateSent
{
get
{
this.m_Date = StringType.FromDate(this.GetLocalDateTime());
return this.m_Date;
}
}
public string From
{
get
{
string from = "";
try
{
this.m_From = Strings.Replace(this.GetHeader("From").Data, "<", "", 1, -1, CompareMethod.Binary);
this.m_From = Strings.Replace(this.m_From, ">", "", 1, -1, CompareMethod.Binary);
this.m_From = Strings.Replace(this.m_From, "\"", "", 1, -1, CompareMethod.Binary);
if (Strings.InStr(1, this.m_From, " ", CompareMethod.Binary) != 0)
{
object o = Strings.Split(this.m_From, " ", -1, CompareMethod.Binary);
this.m_From = StringType.FromObject(LateBinding.LateIndexGet(o, new object[] { Information.UBound((Array) o, 1) }, null));
}
from = this.m_From;
}
catch (Exception exception1)
{
ProjectData.SetProjectError(exception1);
try
{
this.m_From = Strings.Replace(this.GetHeader("Sender").Data, "<", "", 1, -1, CompareMethod.Binary);
this.m_From = Strings.Replace(this.m_From, ">", "", 1, -1, CompareMethod.Binary);
this.m_From = Strings.Replace(this.m_From, "\"", "", 1, -1, CompareMethod.Binary);
if (Strings.InStr(1, this.m_From, " ", CompareMethod.Binary) != 0)
{
object obj3 = Strings.Split(this.m_From, " ", -1, CompareMethod.Binary);
this.m_From = StringType.FromObject(LateBinding.LateIndexGet(obj3, new object[] { Information.UBound((Array) obj3, 1) }, null));
}
from = this.m_From;
ProjectData.ClearProjectError();
return from;
}
catch (Exception exception2)
{
ProjectData.SetProjectError(exception2);
Exception exception = exception2;
ProjectData.ClearProjectError();
}
ProjectData.ClearProjectError();
}
return from;
}
}
public bool HasAttachments
{
get
{
return this.m_Attachments;
}
}
private ArrayList Headers
{
get
{
return this.m_Headers;
}
}
public string HtmlBody
{
get
{
return this.m_HTMLBody;
}
}
private bool IsMultipartMIME
{
get
{
return this.m_MultipartMIME;
}
}
private string MessageData
{
get
{
return this.m_MessageData;
}
set
{
this.m_MessageData = value;
}
}
public string MessageID
{
get
{
try
{
this.m_ID = Strings.Replace(this.GetHeader("Message-ID").Data, "<", "", 1, -1, CompareMethod.Binary);
this.m_ID = Strings.Replace(this.m_ID, ">", "", 1, -1, CompareMethod.Binary);
}
catch (Exception exception1)
{
ProjectData.SetProjectError(exception1);
this.m_ID = this.m_Date;
ProjectData.ClearProjectError();
}
return this.m_ID;
}
}
private ReadyPOP3.MIMEAttachment MIMEAttachment
{
get
{
return this.m_MIMEAttachment;
}
}
public string MIMEBoundary
{
get
{
return this.boundary;
}
}
public string PlainBody
{
get
{
return this.m_PlainBody;
}
}
public string SentTo
{
get
{
this.m_To = Strings.Replace(this.GetHeader("To").Data, "<", "", 1, -1, CompareMethod.Binary);
this.m_To = Strings.Replace(this.m_To, ">", "", 1, -1, CompareMethod.Binary);
return this.m_To;
}
}
public string Subject
{
get
{
try
{
this.m_Subject = this.GetHeader("Subject").Data;
if (StringType.StrCmp(this.m_Subject, "", false) != 0)
{
this.m_Subject = Strings.Replace(this.m_Subject, "<", "", 1, -1, CompareMethod.Binary);
this.m_Subject = Strings.Replace(this.m_Subject, ">", "", 1, -1, CompareMethod.Binary);
this.m_Subject = Strings.Replace(this.m_Subject, "'", "''", 1, -1, CompareMethod.Binary);
}
else
{
this.m_Subject = "(none)";
}
}
catch (Exception exception1)
{
ProjectData.SetProjectError(exception1);
this.m_Subject = "(none)";
ProjectData.ClearProjectError();
}
return this.m_Subject;
}
}
}
}