www.pudn.com > MailAccess.rar > Utility.cs


/****************************************************************************** 
	Copyright 2003-2004 Hamid Qureshi and Unruled Boy  
	OpenPOP.Net is free software; you can redistribute it and/or modify 
	it under the terms of the Lesser GNU General Public License as published by 
	the Free Software Foundation; either version 2 of the License, or 
	(at your option) any later version. 
 
	OpenPOP.Net is distributed in the hope that it will be useful, 
	but WITHOUT ANY WARRANTY; without even the implied warranty of 
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
	Lesser GNU General Public License for more details. 
 
	You should have received a copy of the Lesser GNU General Public License 
	along with this program; if not, write to the Free Software 
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
/*******************************************************************************/ 
 
/* 
*Name:			OpenPOP.Utility 
*Function:		Utility 
*Author:		Hamid Qureshi 
*Created:		2003/8 
*Modified:		2004/5/31 14:22 GMT+8 by Unruled Boy 
*Description: 
*Changes:		 
*				2004/5/31 14:22 GMT+8 by Unruled Boy 
*					1.Fixed a bug in decoding Base64 text when using non-standard encoding 
*				2004/5/30 15:04 GMT+8 by Unruled Boy 
*					1.Added all description to all functions 
*				2004/5/25 13:55 GMT+8 by Unruled Boy 
*					1.Rewrote the DecodeText function using Regular Expression 
*				2004/5/17 14:20 GMT+8 by Unruled Boy 
*					1.Added ParseFileName 
*				2004/4/29 19:05 GMT+8 by Unruled Boy 
*					1.Adding ReadPlainTextFromFile function 
*				2004/4/28 19:06 GMT+8 by Unruled Boy 
*					1.Rewriting the Decode method 
*				2004/3/29 12:25 GMT+8 by Unruled Boy 
*					1.GetMimeType support for MONO 
*					2.cleaning up the names of variants 
*/ 
using System; 
using System.Text; 
using System.IO; 
using System.Threading; 
using System.Text.RegularExpressions; 
 
namespace OpenPOP.MIMEParser 
{ 
	///  
	/// Summary description for Utility. 
	///  
	public class Utility 
	{ 
		private static bool m_blnLog=false; 
		private static string m_strLogFile = "OpenPOP.log"; 
 
		public Utility() 
		{ 
		} 
 
		//		public static string[] SplitText(string strText, string strSplitter) 
		//		{ 
		//			string []segments=new string[0]; 
		//			int indexOfstrSplitter=strText.IndexOf(strSplitter); 
		//			if(indexOfstrSplitter!=-1) 
		//			{ 
		// 
		//			} 
		//			return segments; 
		//		} 
		// 
 
		///  
		/// Verifies whether the file is of picture type or not 
		///  
		/// File to be verified 
		/// True if picture file, false if not 
		public static bool IsPictureFile(string strFile) 
		{ 
			try 
			{ 
				if(strFile!=null&&strFile!="") 
				{ 
					strFile=strFile.ToLower(); 
					if(strFile.EndsWith(".jpg")||strFile.EndsWith(".bmp")||strFile.EndsWith(".ico")||strFile.EndsWith(".gif")||strFile.EndsWith(".png")) 
						return true; 
					else 
						return false; 
				} 
				else 
					return false; 
			} 
			catch 
			{ 
				return false; 
			} 
		} 
 
		///  
		/// Parse date time info from MIME header 
		///  
		/// Encoded MIME date time 
		/// Decoded date time info 
		public static string ParseEmailDate(string strDate) 
		{ 
			string strRet=strDate.Trim(); 
			int indexOfTag=strRet.IndexOf(","); 
			if(indexOfTag!=-1) 
			{ 
				strRet=strRet.Substring(indexOfTag+1); 
			} 
 
			strRet=QuoteText(strRet,"+"); 
			strRet=QuoteText(strRet,"-"); 
			strRet=QuoteText(strRet,"GMT"); 
			strRet=QuoteText(strRet,"CST"); 
			return strRet.Trim(); 
		} 
 
		///  
		/// Quote the text according to a tag 
		///  
		/// Text to be quoted 
		/// Quote tag 
		/// Quoted Text 
		public static string QuoteText(string strText, string strTag) 
		{ 
			int indexOfTag=strText.IndexOf(strTag); 
			if(indexOfTag!=-1) 
				return strText.Substring(0,indexOfTag-1); 
			else 
				return strText; 
		} 
 
		///  
		/// Parse file name from MIME header 
		///  
		/// MIME header 
		/// Decoded file name 
		public static string ParseFileName(string strHeader) 
		{ 
			string strTag; 
			strTag="filename="; 
			int intPos=strHeader.ToLower().IndexOf(strTag); 
			if(intPos==-1) 
			{ 
				strTag="name="; 
				intPos=strHeader.ToLower().IndexOf(strTag); 
			} 
			string strRet; 
			if(intPos!=-1) 
			{ 
				strRet=strHeader.Substring(intPos+strTag.Length); 
				intPos=strRet.ToLower().IndexOf(";"); 
				if(intPos!=-1) 
					strRet=strRet.Substring(1,intPos-1); 
				strRet=RemoveQuote(strRet); 
			} 
			else 
				strRet=""; 
 
			return strRet; 
		} 
 
		///  
		/// Parse email address from MIME header 
		///  
		/// MIME header 
		/// Decoded user name 
		/// Decoded email address 
		/// True if decoding succeeded, false if failed 
		public static bool ParseEmailAddress(string strEmailAddress,ref string strUser, ref string strAddress) 
		{ 
			int indexOfAB=strEmailAddress.Trim().LastIndexOf("<"); 
			int indexOfEndAB=strEmailAddress.Trim().LastIndexOf(">"); 
			strUser=strEmailAddress; 
			strAddress=strEmailAddress; 
			if(indexOfAB>=0&&indexOfEndAB>=0) 
			{ 
				if(indexOfAB>0) 
				{ 
					strUser=strUser.Substring(0,indexOfAB-1); 
					//					strUser=strUser.Substring(0,indexOfAB-1).Trim('\"'); 
					//					if(strUser.IndexOf("\"")>=0) 
					//					{ 
					//						strUser=strUser.Substring(1,strUser.Length-1); 
					//					} 
				} 
				strUser=strUser.Trim(); 
				strUser=strUser.Trim('\"'); 
				strAddress=strAddress.Substring(indexOfAB+1,indexOfEndAB-(indexOfAB+1)); 
			} 
			strUser=strUser.Trim(); 
			strUser=DecodeText(strUser); 
			strAddress=strAddress.Trim(); 
 
			return true; 
		} 
 
		///  
		/// Save byte content to a file 
		///  
		/// File to be saved to 
		/// Byte array content 
		/// True if saving succeeded, false if failed 
		public static bool SaveByteContentToFile(string strFile,byte[] bytContent) 
		{ 
			try 
			{ 
				if(File.Exists(strFile)) 
					File.Delete(strFile); 
				FileStream fs=File.Create(strFile); 
				fs.Write(bytContent,0,bytContent.Length); 
				fs.Close(); 
				return true; 
			} 
			catch(Exception e) 
			{ 
				Utility.LogError("SaveByteContentToFile():"+e.Message); 
				return false; 
			} 
		} 
 
		///  
		/// Save text content to a file 
		///  
		/// File to be saved to 
		/// Text content 
		/// Replace file if exists 
		/// True if saving succeeded, false if failed 
		public static bool SavePlainTextToFile(string strFile, string strText, bool blnReplaceExists) 
		{ 
			try 
			{ 
				bool blnRet=true; 
 
				if(File.Exists(strFile)) 
				{ 
					if(blnReplaceExists) 
						File.Delete(strFile); 
					else 
						blnRet=false; 
				} 
 
				if(blnRet==true) 
				{ 
					StreamWriter sw=File.CreateText(strFile); 
					sw.Write(strText); 
					sw.Close(); 
				} 
 
				return blnRet; 
			} 
			catch(Exception e) 
			{ 
				Utility.LogError("SavePlainTextToFile():"+e.Message); 
				return false; 
			} 
		} 
 
		///  
		/// Read text content from a file 
		///  
		/// File to be read from 
		/// Read text content 
		/// True if reading succeeded, false if failed 
		public static bool ReadPlainTextFromFile(string strFile, ref string strText) 
		{ 
			if(File.Exists(strFile)) 
			{ 
				StreamReader fs=new StreamReader(strFile); 
				strText=fs.ReadToEnd(); 
				fs.Close(); 
				return true; 
			} 
			else 
				return false; 
		} 
 
		///  
		/// Sepearte header name and header value 
		///  
		///  
		///  
		public static string[] GetHeadersValue(string strRawHeader) 
		{ 
			if(strRawHeader==null) 
				throw new ArgumentNullException("strRawHeader","Argument was null"); 
 
			string []array=new string[2]{"",""}; 
			int indexOfColon=strRawHeader.IndexOf(":");			 
 
			try 
			{ 
				array[0]=strRawHeader.Substring(0,indexOfColon).Trim(); 
				array[1]=strRawHeader.Substring(indexOfColon+1).Trim(); 
			} 
			catch(Exception){} 
 
			return array; 
		} 
 
		///  
		/// Get quoted text 
		///  
		/// Text with quotes 
		/// Splitter 
		/// Target tag 
		/// Text without quote 
		public static string GetQuotedValue(string strText, string strSplitter, string strTag) 
		{ 
			if(strText==null) 
				throw new ArgumentNullException("strText","Argument was null"); 
 
			string []array=new string[2]{"",""}; 
			int indexOfstrSplitter=strText.IndexOf(strSplitter);			 
 
			try 
			{ 
				array[0]=strText.Substring(0,indexOfstrSplitter).Trim(); 
				array[1]=strText.Substring(indexOfstrSplitter+1).Trim(); 
				int pos=array[1].IndexOf("\""); 
				if(pos!=-1) 
				{ 
					int pos2=array[1].IndexOf("\"",pos+1); 
					array[1]=array[1].Substring(pos+1,pos2-pos-1); 
				} 
			} 
			catch(Exception){} 
 
			//return array; 
			if(array[0].ToLower()==strTag.ToLower()) 
				return array[1].Trim(); 
			else 
				return null; 
 
/*			string []array=null; 
			try 
			{ 
				array=Regex.Split(strText,strSplitter); 
				//return array; 
				if(array[0].ToLower()==strTag.ToLower()) 
					return RemoveQuote(array[1].Trim()); 
				else 
					return null; 
			} 
			catch 
			{return null;}*/ 
		} 
 
		///  
		/// Change text encoding 
		///  
		/// Source encoded text 
		/// New charset 
		/// Encoded text with new charset 
		public static string Change(string strText,string strCharset) 
		{ 
			if (strCharset==null || strCharset=="") 
				return strText; 
			byte[] b=Encoding.Default.GetBytes(strText); 
			return new string(Encoding.GetEncoding(strCharset).GetChars(b)); 
		} 
 
		///  
		/// Remove non-standard base 64 characters 
		///  
		/// Source text 
		/// standard base 64 text 
		public static string RemoveNonB64(string strText) 
		{ 
			return strText.Replace("\0",""); 
		} 
 
		///  
		/// Remove white blank characters 
		///  
		/// Source text 
		/// Text with white blanks 
		public static string RemoveWhiteBlanks(string strText) 
		{ 
			return strText.Replace("\0","").Replace("\r\n",""); 
		} 
 
		///  
		/// Remove quotes 
		///  
		/// Text with quotes 
		/// Text without quotes 
		public static string RemoveQuote(string strText)			 
		{ 
			string strRet=strText; 
			if(strRet.StartsWith("\"")) 
				strRet=strRet.Substring(1); 
			if(strRet.EndsWith("\"")) 
				strRet=strRet.Substring(0,strRet.Length-1); 
			return strRet; 
		} 
 
		///  
		/// Decode one line of text 
		///  
		/// Encoded text 
		/// Decoded text 
		public static string DecodeLine(string strText) 
		{ 
			return DecodeText(RemoveWhiteBlanks(strText)); 
		} 
 
		///  
		/// Verifies wether the text is a valid MIME Text or not 
		///  
		/// Text to be verified 
		/// True if MIME text, false if not 
		private static bool IsValidMIMEText(string strText) 
		{ 
			int intPos=strText.IndexOf("=?"); 
			return (intPos!=-1&&strText.IndexOf("?=",intPos+6)!=-1&&strText.Length>7); 
		} 
 
		///  
		/// Decode text 
		///  
		/// Source text 
		/// Decoded text 
		public static string DecodeText(string strText) 
		{ 
			/*try 
			{ 
				string strRet=""; 
				string strBody=""; 
				MatchCollection mc=Regex.Matches(strText,@"\=\?(?\S+)\?(?\w)\?(?\S+)\?\="); 
 
				for(int i=0;i\S+)\?(?\w)\?(?\S+)\?\="; 
				Match m=null; 
 
				for(int i=0;i3) 
						{ 
							string strCharset=strText.Substring(2,intPos2-2); 
							string strEncoding=strText.Substring(intPos2+1,1); 
							int intPos3=strText.IndexOf("?=",intPos2+3); 
							string strBody=strText.Substring(intPos2+3,intPos3-intPos2-3); 
							string strHead=""; 
							if(intPos>0) 
							{ 
								strHead=strText.Substring(0,intPos-1); 
							} 
							string strEnd=""; 
							if(intPos3 
		///  
		///  
		///  
		///  
		public static string deCodeB64s(string strText) 
		{ 
			return Encoding.Default.GetString(deCodeB64(strText)); 
		} 
		 
		public static string deCodeB64s(string strText,string strEncoding) 
		{ 
			try 
			{ 
				if(strEncoding.ToLower()=="ISO-8859-1".ToLower()) 
					return deCodeB64s(strText); 
				else 
					return Encoding.GetEncoding(strEncoding).GetString(deCodeB64(strText)); 
			} 
			catch 
			{ 
				return deCodeB64s(strText); 
			} 
		} 
		 
		private static byte []deCodeB64(string strText) 
		{ 
			byte[] by=null; 
			try 
			{  
				if(strText!="") 
				{ 
					by=Convert.FromBase64String(strText);  
					//strText=Encoding.Default.GetString(by); 
				} 
			}  
			catch(Exception e)  
			{ 
				by=Encoding.Default.GetBytes("\0"); 
				LogError("deCodeB64():"+e.Message); 
			} 
			return by; 
		} 
 
		///  
		/// Turns file logging on and off. 
		///  
		/// Comming soon. 
		public static bool Log 
		{ 
			get 
			{ 
				return m_blnLog; 
			} 
			set 
			{ 
				m_blnLog = value; 
			} 
		} 
 
		internal static void LogError(string strText)  
		{ 
			//Log=true; 
			if(Log) 
			{ 
				FileInfo file = null; 
				FileStream fs = null; 
				StreamWriter sw = null; 
				try 
				{ 
					file = new FileInfo(m_strLogFile); 
					sw = file.AppendText(); 
					//fs = new FileStream(m_strLogFile, FileMode.OpenOrCreate, FileAccess.Write); 
					//sw = new StreamWriter(fs); 
					sw.WriteLine(DateTime.Now); 
					sw.WriteLine(strText); 
					sw.WriteLine("\r\n"); 
					sw.Flush(); 
				} 
				finally 
				{ 
					if(sw != null) 
					{ 
						sw.Close(); 
						sw = null; 
					} 
					if(fs != null) 
					{ 
						fs.Close(); 
						fs = null; 
					} 
					 
				} 
			} 
		} 
 
		public static bool IsQuotedPrintable(string strText) 
		{ 
			if(strText!=null) 
				return (strText.ToLower()=="quoted-printable".ToLower()); 
			else 
				return false; 
		} 
 
		public static bool IsBase64(string strText) 
		{ 
			if(strText!=null) 
				return (strText.ToLower()=="base64".ToLower()); 
			else 
				return false; 
		} 
 
		public static string[] SplitOnSemiColon(string strText) 
		{ 
			if(strText==null) 
				throw new ArgumentNullException("strText","Argument was null"); 
 
			string []array=null; 
			int indexOfColon=strText.IndexOf(";");			 
 
			if(indexOfColon<0) 
			{ 
				array=new string[1]; 
				array[0]=strText; 
				return array; 
			} 
			else 
			{ 
				array=new string[2]; 
			} 
 
			try 
			{ 
				array[0]=strText.Substring(0,indexOfColon).Trim(); 
				array[1]=strText.Substring(indexOfColon+1).Trim(); 
			} 
			catch(Exception){} 
 
			return array; 
		} 
 
		public static bool IsNotNullText(string strText) 
		{ 
			try 
			{ 
				return (strText!=null&&strText!=""); 
			} 
			catch 
			{ 
				return false; 
			} 
		} 
 
		public static bool IsNotNullTextEx(string strText) 
		{ 
			try 
			{ 
				return (strText!=null&&strText.Trim()!=""); 
			} 
			catch 
			{ 
				return false; 
			} 
		} 
 
		public static bool IsOrNullTextEx(string strText) 
		{ 
			try 
			{ 
				return (strText==null||strText.Trim()==""); 
			} 
			catch 
			{ 
				return false; 
			} 
		} 
 
	} 
}