ServerAPI.cs
上传用户:horngjaan
上传日期:2009-12-12
资源大小:2882k
文件大小:75k
- using System;
- using System.IO;
- using System.Data;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Security.Cryptography;
- using LumiSoft.MailServer;
- namespace LumiSoft.MailServer
- {
- /// <summary>
- /// Specifies server database type.
- /// </summary>
- public enum DB_Type
- {
- /// <summary>
- /// Data will be stored to XML.
- /// </summary>
- XML = 1,
- /// <summary>
- /// Data will be stored to MS SQL.
- /// </summary>
- MSSQL = 2,
- }
- /// <summary>
- /// Summary Description for ServerAPI.
- /// </summary>
- public class ServerAPI
- {
- private string m_DataPath = "";
- private string m_MailStorePath = "";
- private string m_ConStr = "";
- private DataSet dsUsers = null;
- private DataSet dsAliases = null;
- private DataSet dsDomains = null;
- private DataSet dsRouting = null;
- private DataSet dsSecurity = null;
- private DB_Type m_DB_Type = DB_Type.XML;
- /// <summary>
- /// Defaul constructor.
- /// </summary>
- /// <param name="dataPath">xml files location.</param>
- /// <param name="mailStorePath">MailStore path.</param>
- /// <param name="conStr">Connection string.</param>
- /// <param name="dbType">Database type.</param>
- public ServerAPI(string dataPath,string mailStorePath,string conStr,DB_Type dbType)
- {
- m_DataPath = dataPath;
- m_MailStorePath = mailStorePath;
- m_ConStr = conStr;
- m_DB_Type = dbType;
- dsUsers = new DataSet();
- dsAliases = new DataSet();
- dsDomains = new DataSet();
- dsRouting = new DataSet();
- dsSecurity = new DataSet();
- CreateUsersSchema(dsUsers);
- CreateAliasesSchema(dsAliases);
- CreateDomainsSchema(dsDomains);
- CreateSecuritySchema(dsSecurity);
- CreateRoutingsSchema(dsRouting);
-
- // We need to load all stuff to memory for XML
- if(dbType == DB_Type.XML){
- LoadUsers();
- LoadAliases();
- LoadRouting();
- LoadDomains();
- LoadSecurity();
- }
- }
- #region DomainName related
- #region function GetDomainList
- /// <summary>
- /// Gets DomainName list.
- /// </summary>
- /// <returns></returns>
- public DataView GetDomainList()
- {
- DataView retVal = null;
- switch(m_DB_Type)
- {
- #region DB_Type.XML
- case DB_Type.XML:
- retVal = dsDomains.Tables["DOMAINS"].DefaultView;
- break;
- #endregion
- #region DB_Type.MSSQL
- case DB_Type.MSSQL:
- using(WSqlCommand sqlCmd = new WSqlCommand(m_ConStr,"lspr_GetDomainList")){
- DataSet ds = sqlCmd.Execute();
- ds.Tables[0].TableName = "Domains";
- return ds.Tables["Domains"].DefaultView;
- }
- #endregion
- }
- return retVal;
- }
- #endregion
- #region function AddDomain
- /// <summary>
- /// Adds new DomainName.
- /// </summary>
- /// <param name="domainName"></param>
- /// <param name="Description"></param>
- /// <returns>If successful returns DomainName ID, otherwise null.</returns>
- public DataRow AddDomain(string domainName,string Description)
- {
- DataRow retVal = null;
- try
- {
- switch(m_DB_Type)
- {
- #region DB_Type.XML
- case DB_Type.XML:
-
- DataSet dsDomainsCopy = dsDomains.Copy();
- DataRow dr = dsDomainsCopy.Tables["Domains"].NewRow();
- dr["DomainID"] = Guid.NewGuid().ToString();
- dr["DomainName"] = domainName;
- dr["Description"] = Description;
-
- dsDomainsCopy.Tables["Domains"].Rows.Add(dr);
- dsDomainsCopy.WriteXml(m_DataPath + "Domains.xml",XmlWriteMode.IgnoreSchema);
- retVal = dr;
- break;
- #endregion
- #region DB_Type.MSSQL
- case DB_Type.MSSQL:
- using(WSqlCommand sqlCmd = new WSqlCommand(m_ConStr,"lspr_AddDomain")){
- sqlCmd.AddParameter("@DomainName" ,SqlDbType.NVarChar,domainName);
- sqlCmd.AddParameter("@Description",SqlDbType.NVarChar,Description);
-
- DataSet ds = sqlCmd.Execute();
- ds.Tables[0].TableName = "Domains";
- if(ds.Tables["Domains"].Rows.Count > 0){
- return ds.Tables["Domains"].Rows[0];
- }
- }
- break;
- #endregion
- }
- }
- catch(Exception x)
- {
- retVal = null;
- }
- return retVal;
- }
- #endregion
- #region function DeleteDomain
- /// <summary>
- /// Deletes specified DomainName.
- /// </summary>
- /// <param name="domainID"></param>
- /// <returns>Returns true if DomainName deleted successfully.</returns>
- public bool DeleteDomain(string domainID)
- {
- try
- {
- switch(m_DB_Type)
- {
- #region DB_Type.XML
- case DB_Type.XML:
- // 1) delete specified DomainName users
- // 2) delete specified DomainName aliases
- // 3) delete specified DomainName routing
- // 4) delete specified DomainName
- DataSet dsUsersCopy = dsUsers.Copy();
- DataSet dsAliasesCopy = dsAliases.Copy();
- DataSet dsRoutingCopy = dsRouting.Copy();
- DataSet dsDomainsCopy = dsDomains.Copy();
-
- //---- Delete specified DomainName users ----------------------------//
- using(DataView dv = new DataView(dsUsersCopy.Tables["Users"])){
- dv.RowFilter = "DomainID='" + domainID + "'";
-
- if(dv.Count > 0){
- foreach(DataRowView drv in dv){
- drv.Row.Delete();
- }
- }
- }
- //----------------------------------------------------------------//
- //---- Delete specified DomainName aliases ---------------------------//
- using(DataView dvA = new DataView(dsAliasesCopy.Tables["Aliases"])){
- dvA.RowFilter = "DomainID='" + domainID + "'";
-
- if(dvA.Count > 0){
- foreach(DataRowView drv in dvA){
- drv.Row.Delete();
- }
- }
- }
- //----------------------------------------------------------------//
- //---- Delete specified DomainName routing ---------------------------//
- using(DataView dvR = new DataView(dsRoutingCopy.Tables["Routing"])){
- dvR.RowFilter = "DomainID='" + domainID + "'";
-
- if(dvR.Count > 0){
- foreach(DataRowView drv in dvR){
- drv.Row.Delete();
- }
- }
- }
- //----------------------------------------------------------------//
- //---- Delete DomainName itself --------------------------------------//
- using(DataView dv = new DataView(dsDomainsCopy.Tables["Domains"])){
- dv.RowFilter = "DomainID='" + domainID + "'";
- if(dv.Count > 0){
- dsDomainsCopy.Tables["Domains"].Rows.Remove(dv[0].Row);
- }
- }
- //----------------------------------------------------------------//
- dsDomainsCopy.WriteXml(m_DataPath + "Domains.xml",XmlWriteMode.IgnoreSchema);
- dsUsersCopy.WriteXml (m_DataPath + "Users.xml" ,XmlWriteMode.IgnoreSchema);
- dsAliasesCopy.WriteXml(m_DataPath + "Aliases.xml",XmlWriteMode.IgnoreSchema);
- dsRoutingCopy.WriteXml(m_DataPath + "Routing.xml",XmlWriteMode.IgnoreSchema);
- LoadUsers();
- LoadAliases();
- LoadRouting();
- return true;
- #endregion
- #region DB_Type.MSSQL
- case DB_Type.MSSQL:
- using(WSqlCommand sqlCmd = new WSqlCommand(m_ConStr,"lspr_DeleteDomain")){
- sqlCmd.AddParameter("@DomainID" ,SqlDbType.NVarChar,domainID);
-
- DataSet ds = sqlCmd.Execute();
- return true;
- }
- #endregion
- }
- }
- catch(Exception x)
- {
- // System.Windows.Forms.MessageBox.Show(x.Message);
- }
- return false;
- }
- #endregion
- #region function DomainExists
- /// <summary>
- /// Checks if specified DomainName exists.
- /// </summary>
- /// <param name="source">DomainName or Emails address.</param>
- /// <returns></returns>
- public bool DomainExists(string source)
- {
- bool retVal = false;
- // Source is Emails
- if(source.IndexOf("@") > -1){
- source = source.Substring(source.IndexOf("@")+1);
- }
- switch(m_DB_Type)
- {
- #region DB_Type.XML
- case DB_Type.XML:
- using(DataView dv = new DataView(dsDomains.Tables["Domains"])){
- dv.RowFilter = "DomainName='" + source + "'";
- if(dv.Count > 0){
- retVal = true;
- }
- }
- break;
- #endregion
- #region DB_Type.MSSQL
- case DB_Type.MSSQL:
- using(WSqlCommand sqlCmd = new WSqlCommand(m_ConStr,"lspr_DomainExists")){
- sqlCmd.AddParameter("@DomainName",SqlDbType.NVarChar,source);
- DataSet ds = sqlCmd.Execute();
- ds.Tables[0].TableName = "Domains";
- if(ds.Tables["Domains"].Rows.Count > 0){
- return true;
- }
- else{
- return false;
- }
- }
- #endregion
- }
- return retVal;
- }
- #endregion
- #endregion
- #region User related
- #region function GetUserList
- /// <summary>
- /// Gets user list in specified DomainName.
- /// </summary>
- /// <param name="domainID">DomainID of Domain which user list to retrieve.To get all use value 'ALL'.</param>
- /// <returns></returns>
- public DataView GetUserList(string domainID)
- {
- DataView retVal = null;
- switch(m_DB_Type)
- {
- #region DB_Type.XML
- case DB_Type.XML:
- DataView dv = new DataView(dsUsers.Tables["Users"]);
- if(domainID != "ALL"){
- dv.RowFilter = "DomainID='" + domainID + "'";
- }
- retVal = dv;
- break;
- #endregion
- #region DB_Type.MSSQL
- case DB_Type.MSSQL:
- using(WSqlCommand sqlCmd = new WSqlCommand(m_ConStr,"lspr_GetUserList")){
- if(domainID != "ALL"){
- sqlCmd.AddParameter("@DomainID",SqlDbType.UniqueIdentifier,domainID);
- }
- DataSet ds = sqlCmd.Execute();
- ds.Tables[0].TableName = "Users";
- return ds.Tables["Users"].DefaultView;
- }
- #endregion
- }
- return retVal;
- }
- #endregion
- #region function AddUser
- /// <summary>
- /// Adds new user to specified DomainName.
- /// </summary>
- /// <param name="fullName">User full name.</param>
- /// <param name="userName">User login name.</param>
- /// <param name="password">User login password.</param>
- /// <param name="Description">User Description.</param>
- /// <param name="emails">User Emails addresses.</param>
- /// <param name="domainID">DomainName ID of DomainName where to add user.</param>
- /// <param name="mailboxSize">Maximum mailbox size.</param>
- /// <returns></returns>
- public DataRow AddUser(string fullName,string userName,string password,string Description,string emails,string domainID,int mailboxSize)
- {
- DataRow retVal = null;
- switch(m_DB_Type)
- {
- #region DB_Type.XML
- case DB_Type.XML:
- DataSet dsUsersCopy = dsUsers.Copy();
- DataRow dr = dsUsersCopy.Tables["Users"].NewRow();
- dr["UserID"] = Guid.NewGuid().ToString();
- dr["DomainID"] = domainID;
- // dr["DomainName"] = domainName;
- dr["FULLNAME"] = fullName;
- dr["USERNAME"] = userName;
- dr["PASSWORD"] = password;
- dr["Description"] = Description;
- dr["Emails"] = emails;
- dr["Mailbox_Size"] = mailboxSize;
- dsUsersCopy.Tables["Users"].Rows.Add(dr);
- dsUsersCopy.WriteXml(m_DataPath + "Users.xml",XmlWriteMode.IgnoreSchema);
- return dr;
- #endregion
- #region DB_Type.MSSQL
- case DB_Type.MSSQL:
- using(WSqlCommand sqlCmd = new WSqlCommand(m_ConStr,"lspr_AddUser")){
- sqlCmd.AddParameter("@FullName" ,SqlDbType.NVarChar,fullName);
- sqlCmd.AddParameter("@UserName" ,SqlDbType.NVarChar,userName);
- sqlCmd.AddParameter("@Password" ,SqlDbType.NVarChar,password);
- sqlCmd.AddParameter("@Description" ,SqlDbType.NVarChar,Description);
- sqlCmd.AddParameter("@Emails" ,SqlDbType.NVarChar,emails);
- sqlCmd.AddParameter("@DomainID" ,SqlDbType.NVarChar,domainID);
- sqlCmd.AddParameter("@MailboxSize" ,SqlDbType.NVarChar,mailboxSize);
-
- DataSet ds = sqlCmd.Execute();
- ds.Tables[0].TableName = "Users";
- if(ds.Tables["Users"].Rows.Count > 0){
- return ds.Tables["Users"].Rows[0];
- }
- }
- break;
- #endregion
- }
- return retVal;
- }
- #endregion
- #region function DeleteUser
- /// <summary>
- /// Deletes user.
- /// </summary>
- /// <param name="userID">UserID of the user which to delete.</param>
- public void DeleteUser(string userID)
- {
- switch(m_DB_Type)
- {
- #region DB_Type.XML
- case DB_Type.XML:
- DataSet dsUsersCopy = dsUsers.Copy();
- using(DataView dv = new DataView(dsUsersCopy.Tables["Users"])){
- dv.RowFilter = "UserID='" + userID + "'";
- if(dv.Count > 0){
- dsUsersCopy.Tables["Users"].Rows.Remove(dv[0].Row);
- }
- dsUsersCopy.WriteXml(m_DataPath + "Users.xml",XmlWriteMode.IgnoreSchema);
- // ToDo delete user folders
- }
- break;
- #endregion
- #region DB_Type.MSSQL
- case DB_Type.MSSQL:
- using(WSqlCommand sqlCmd = new WSqlCommand(m_ConStr,"lspr_DeleteUser")){
- sqlCmd.AddParameter("@UserID" ,SqlDbType.NVarChar,userID);
-
- DataSet ds = sqlCmd.Execute();
- }
- break;
- #endregion
- }
- }
- #endregion
- #region function UpdateUser
- /// <summary>
- /// Updates new user to specified DomainName.
- /// </summary>
- /// <param name="userID"></param>
- /// <param name="fullName">User full name.</param>
- /// <param name="password">User login password.</param>
- /// <param name="Description">User Description.</param>
- /// <param name="emails">User Emails addresses.</param>
- /// <param name="domainID">DomainName ID of DomainName where to add user.</param>
- /// <param name="mailboxSize">Maximum mailbox size.</param>
- public void UpdateUser(string userID,string fullName,string password,string Description,string emails,string domainID,int mailboxSize)
- {
- switch(m_DB_Type)
- {
- #region DB_Type.XML
- case DB_Type.XML:
- DataSet dsUsersCopy = dsUsers.Copy();
- using(DataView dv = new DataView(dsUsersCopy.Tables["Users"])){
- dv.RowFilter = "UserID='" + userID + "'";
- if(dv.Count > 0){
- dv[0]["FULLNAME"] = fullName;
- dv[0]["PASSWORD"] = password;
- dv[0]["Description"] = Description;
- dv[0]["Emails"] = emails;
- dv[0]["DomainID"] = domainID;
- dv[0]["Mailbox_Size"] = mailboxSize;
- }
- dsUsersCopy.WriteXml(m_DataPath + "Users.xml",XmlWriteMode.IgnoreSchema);
- }
- break;
-
- #endregion
- #region DB_Type.MSSQL
- case DB_Type.MSSQL:
- using(WSqlCommand sqlCmd = new WSqlCommand(m_ConStr,"lspr_UpdateUser")){
- sqlCmd.AddParameter("@UserID" ,SqlDbType.NVarChar,userID);
- sqlCmd.AddParameter("@FullName" ,SqlDbType.NVarChar,fullName);
- sqlCmd.AddParameter("@Password" ,SqlDbType.NVarChar,password);
- sqlCmd.AddParameter("@Description" ,SqlDbType.NVarChar,Description);
- sqlCmd.AddParameter("@Emails" ,SqlDbType.NVarChar,emails);
- sqlCmd.AddParameter("@DomainID" ,SqlDbType.NVarChar,domainID);
- sqlCmd.AddParameter("@MailboxSize" ,SqlDbType.NVarChar,mailboxSize);
-
- DataSet ds = sqlCmd.Execute();
- }
- break;
- #endregion
- }
- }
- #endregion
- #region function MailboxExists
- /// <summary>
- /// Checks if mailbox exists.
- /// </summary>
- /// <param name="userName">User name.</param>
- /// <returns></returns>
- public bool MailboxExists(string userName)
- {
- bool retVal = false;
- switch(m_DB_Type)
- {
- #region DB_Type.XML
- case DB_Type.XML:
- using(DataView dv = new DataView(dsUsers.Tables["Users"])){
- dv.RowFilter = "USERNAME='" + userName + "'";
- if(dv.Count > 0){
- retVal = true;
- }
- }
- break;
- #endregion
- #region DB_Type.MSSQL
- case DB_Type.MSSQL:
- using(WSqlCommand sqlCmd = new WSqlCommand(m_ConStr,"lspr_UserExists")){
- sqlCmd.AddParameter("@UserName",SqlDbType.NVarChar,userName);
- DataSet ds = sqlCmd.Execute();
- ds.Tables[0].TableName = "Users";
- if(ds.Tables["Users"].Rows.Count > 0){
- return true;
- }
- else{
- return false;
- }
- }
- #endregion
- }
- return retVal;
- }
- #endregion
- #region function AuthUser
- /// <summary>
- /// Authenticates user.
- /// </summary>
- /// <param name="userName">User name.</param>
- /// <param name="passwData">Password data.</param>
- /// <param name="authData">Authentication specific data(as tag).</param>
- /// <param name="authType">Authentication type.</param>
- /// <returns></returns>
- public bool AuthUser(string userName,string passwData,string authData,AuthType authType)
- {
- bool retVal = false;
- switch(m_DB_Type)
- {
- #region DB_Type.XML
- case DB_Type.XML:
- using(DataView dv = new DataView(dsUsers.Tables["Users"])){
- dv.RowFilter = "USERNAME='" + userName + "'";
- // User exists, try to authenticate user.
- if(dv.Count > 0){
- switch(authType)
- {
- case AuthType.APOP:
- string password = dv[0]["PASSWORD"].ToString().ToLower();
- byte[] data = System.Text.Encoding.ASCII.GetBytes(authData + password);
-
- MD5 md5 = new MD5CryptoServiceProvider();
- byte[] hash = md5.ComputeHash(data);
- string hexHash = BitConverter.ToString(hash).ToLower().Replace("-","");
- if(hexHash == passwData){
- retVal = true;
- }
- break;
- case AuthType.Plain:
- if(dv[0]["PASSWORD"].ToString().ToLower() == passwData.ToLower()){
- retVal = true;
- }
- break;
- }
- }
- }
- break;
- #endregion
- #region DB_Type.MSSQL
- case DB_Type.MSSQL:
- using(WSqlCommand sqlCmd = new WSqlCommand(m_ConStr,"lspr_UserExists")){
- sqlCmd.AddParameter("@UserName",SqlDbType.NVarChar,userName);
-
- DataSet ds = sqlCmd.Execute();
- ds.Tables[0].TableName = "Users";
- if(ds.Tables["Users"].Rows.Count > 0){
-
- switch(authType)
- {
- case AuthType.APOP:
- string password = ds.Tables["Users"].Rows[0]["PASSWORD"].ToString().ToLower();
- byte[] data = System.Text.Encoding.ASCII.GetBytes(authData + password);
-
- MD5 md5 = new MD5CryptoServiceProvider();
- byte[] hash = md5.ComputeHash(data);
- string hexHash = BitConverter.ToString(hash).ToLower().Replace("-","");
- if(hexHash == passwData){
- return true;
- }
- break;
- case AuthType.Plain:
- if(ds.Tables["Users"].Rows[0]["PASSWORD"].ToString().ToLower() == passwData.ToLower()){
- return true;
- }
- break;
- }
- }
- break;
- }
- #endregion
- }
- return retVal;
- }
- #endregion
- #region function EmailAddressExists
- /// <summary>
- /// Checks if specifeid Emails address belongs to somebody in this server.
- /// </summary>
- /// <param name="emailAddress">Emails address which to check.</param>
- /// <returns>Returns true if Emails address is found.</returns>
- public bool EmailAddressExists(string emailAddress)
- {
- bool retVal = false;
- switch(m_DB_Type)
- {
- #region DB_Type.XML
- case DB_Type.XML:
- using(DataView dv = new DataView(dsUsers.Tables["Users"])){
- dv.RowFilter = "Emails LIKE '*[<]" + emailAddress + "[>]*'";
- if(dv.Count > 0){
- retVal = true;
- }
- }
- break;
- #endregion
- #region DB_Type.MSSQL
- case DB_Type.MSSQL:
- using(WSqlCommand sqlCmd = new WSqlCommand(m_ConStr,"lspr_EmailAddressExists")){
- sqlCmd.AddParameter("@EmailAddress",SqlDbType.NVarChar,emailAddress);
- DataSet ds = sqlCmd.Execute();
- ds.Tables[0].TableName = "Users";
- if(ds.Tables["Users"].Rows.Count > 0){
- return true;
- }
- else{
- return false;
- }
- }
- #endregion
- }
- return retVal;
- }
- #endregion
- #region function MapUser
- /// <summary>
- /// Maps Emails address to mailbox.
- /// </summary>
- /// <param name="emailAddress"></param>
- /// <returns>Returns mailbox or null if map failed.</returns>
- public string MapUser(string emailAddress)
- {
- string retVal = null;
- /* RFC 2821 3.6
- NOTE:
- The reserved mailbox name "postmaster" may be used in a RCPT
- command without DomainName qualification (see section 4.1.1.3) and
- MUST be accepted if so used.
- */
- //--- Check if postmaster eAddress -----------------//
- if(emailAddress.ToUpper().IndexOf("postmaster") > 0){
- return "postmaster";
- }
- //--------------------------------------------------//
- switch(m_DB_Type)
- {
- #region DB_Type.XML
- case DB_Type.XML:
- using(DataView dv = new DataView(dsUsers.Tables["Users"])){
- dv.RowFilter = "Emails LIKE '*[<]" + emailAddress + "[>]*'";
-
- if(dv.Count > 0){
- // UserName == MailBoxName, return MailBoxName
- retVal = dv[0].Row["USERNAME"].ToString();
- }
- }
- break;
- #endregion
- #region DB_Type.MSSQL
- case DB_Type.MSSQL:
- using(WSqlCommand sqlCmd = new WSqlCommand(m_ConStr,"lspr_EmailAddressExists")){
- sqlCmd.AddParameter("@EmailAddress",SqlDbType.NVarChar,emailAddress);
- DataSet ds = sqlCmd.Execute();
- ds.Tables[0].TableName = "Users";
- if(ds.Tables["Users"].Rows.Count > 0){
- return ds.Tables["Users"].Rows[0]["USERNAME"].ToString();
- }
- }
- break;
- #endregion
- }
- return retVal;
- }
- #endregion
- #region function ValidateMailboxSize
- /// <summary>
- /// Checks if specified mailbox size is exceeded.
- /// </summary>
- /// <param name="mailbox"></param>
- /// <returns>Returns true if exceeded.</returns>
- public bool ValidateMailboxSize(string mailbox)
- {
- switch(m_DB_Type)
- {
- #region DB_Type.XML
- case DB_Type.XML:
- using(DataView dv = new DataView(dsUsers.Tables["Users"])){
- dv.RowFilter = "USERNAME='" + mailbox + "'";
- //--- Check if user directory exists ----//
- if(!Directory.Exists(m_MailStorePath + "Mailboxes\" + mailbox)){
- Directory.CreateDirectory(m_MailStorePath + "Mailboxes\" + mailbox);
- }
- //---------------------------------------//
-
- if(dv.Count > 0){
- string[] files = Directory.GetFiles(m_MailStorePath + "Mailboxes\" + mailbox);
-
- //*********************************************************
- // Note, code below speed is bull... when big count of files
- int maxSize = Convert.ToInt32(dv[0]["Mailbox_Size"]);
- decimal sizeTotal = 0;
- foreach(string file in files){
- using(FileStream strm = File.OpenRead(file)){
- sizeTotal += strm.Length;
- }
- // If maximum size exceeded, return
- if(sizeTotal > maxSize*1000000){
- return true;
- }
- }
- }
- }
- break;
- #endregion
- #region DB_Type.MSSQL
- case DB_Type.MSSQL:
- using(WSqlCommand sqlCmd = new WSqlCommand(m_ConStr,"lspr_ValidateMailboxSize")){
- sqlCmd.AddParameter("@MailBox",SqlDbType.NVarChar,mailbox);
- DataSet ds = sqlCmd.Execute();
- ds.Tables[0].TableName = "Detail";
- if(ds.Tables["Detail"].Rows.Count > 0){
- return !Convert.ToBoolean(ds.Tables["Detail"].Rows[0]["VALIDATED"]);
- }
- }
- break;
- #endregion
- }
- return false;
- }
- #endregion
- #endregion
- #region AliasName related
- #region function GetAliasesList
- /// <summary>
- /// Gets aliases.
- /// </summary>
- /// <param name="DomainName"></param>
- /// <returns></returns>
- public DataView GetAliasesList(string DomainName)
- {
- DataView retVal = null;
- switch(m_DB_Type)
- {
- #region DB_Type.XML
- case DB_Type.XML:
- retVal = dsAliases.Tables["Aliases"].DefaultView;
- break;
- #endregion
- #region DB_Type.MSSQL
- case DB_Type.MSSQL:
- using(WSqlCommand sqlCmd = new WSqlCommand(m_ConStr,"lspr_GetAliasesList")){
- // sqlCmd.AddParameter("@DomainName",SqlDbType.NVarChar,source);
- DataSet ds = sqlCmd.Execute();
- ds.Tables[0].TableName = "Aliases";
- return ds.Tables["Aliases"].DefaultView;
- }
- #endregion
- }
- return retVal;
- }
- #endregion
-
- #region function AddAlias
- /// <summary>
- /// Adds AliasName(mailing list).
- /// </summary>
- /// <param name="aliasName">AliasName name. eg. all@lumisoft.ee</param>
- /// <param name="Description">AliasName Description.</param>
- /// <param name="AliasMembers">AliasName AliasMembers.</param>
- /// <param name="domainID">DomainID where AliasName belongs.</param>
- /// <returns></returns>
- public DataRow AddAlias(string aliasName,string Description,string AliasMembers,string domainID)
- {
- DataRow retVal = null;
- switch(m_DB_Type)
- {
- #region DB_Type.XML
- case DB_Type.XML:
- DataSet dsAliasesCopy = dsAliases.Copy();
- DataRow dr = dsAliasesCopy.Tables["Aliases"].NewRow();
- dr["AliasID"] = Guid.NewGuid().ToString();
- dr["AliasName"] = aliasName;
- dr["Description"] = Description;
- dr["AliasMembers"] = AliasMembers;
- dr["DomainID"] = domainID;
- dsAliasesCopy.Tables["Aliases"].Rows.Add(dr);
- dsAliasesCopy.WriteXml(m_DataPath + "Aliases.xml",XmlWriteMode.IgnoreSchema);
- retVal = dr;
- break;
- #endregion
- #region DB_Type.MSSQL
- case DB_Type.MSSQL:
- using(WSqlCommand sqlCmd = new WSqlCommand(m_ConStr,"lspr_AddAlias")){
- sqlCmd.AddParameter("@AliasName" ,SqlDbType.NVarChar,aliasName);
- sqlCmd.AddParameter("@Description" ,SqlDbType.NVarChar,Description);
- sqlCmd.AddParameter("@AliasMembers" ,SqlDbType.NVarChar,AliasMembers);
- sqlCmd.AddParameter("@DomainID" ,SqlDbType.UniqueIdentifier,domainID);
-
- DataSet ds = sqlCmd.Execute();
- ds.Tables[0].TableName = "Aliases";
- if(ds.Tables["Aliases"].Rows.Count > 0){
- return ds.Tables["Aliases"].Rows[0];
- }
- }
- break;
- #endregion
- }
- return retVal;
- }
- #endregion
- #region function DeleteAlias
- /// <summary>
- /// Deletes specified AliasName.
- /// </summary>
- /// <param name="aliasID"></param>
- /// <returns></returns>
- public void DeleteAlias(string aliasID)
- {
- switch(m_DB_Type)
- {
- #region DB_Type.XML
- case DB_Type.XML:
- DataSet dsAliasesCopy = dsAliases.Copy();
- using(DataView dv = new DataView(dsAliasesCopy.Tables["Aliases"])){
- dv.RowFilter = "AliasID='" + aliasID + "'";
- if(dv.Count > 0){
- dsAliasesCopy.Tables["Aliases"].Rows.Remove(dv[0].Row);
- }
- dsAliasesCopy.WriteXml(m_DataPath + "Aliases.xml",XmlWriteMode.IgnoreSchema);
- }
- break;
- #endregion
- #region DB_Type.MSSQL
- case DB_Type.MSSQL:
- using(WSqlCommand sqlCmd = new WSqlCommand(m_ConStr,"lspr_DeleteAlias")){
- sqlCmd.AddParameter("@AliasID" ,SqlDbType.UniqueIdentifier,aliasID);
-
- DataSet ds = sqlCmd.Execute();
- }
- break;
- #endregion
- }
- }
- #endregion
- #region function UpdateAlias
- /// <summary>
- /// Updates AliasName.
- /// </summary>
- /// <param name="aliasID"></param>
- /// <param name="aliasName"></param>
- /// <param name="Description"></param>
- /// <param name="AliasMembers"></param>
- /// <param name="domainID"></param>
- public void UpdateAlias(string aliasID,string aliasName,string Description,string AliasMembers,string domainID)
- {
- switch(m_DB_Type)
- {
- #region DB_Type.XML
- case DB_Type.XML:
- DataSet dsAliasesCopy = dsAliases.Copy();
- using(DataView dv = new DataView(dsAliasesCopy.Tables["Aliases"])){
- dv.RowFilter = "AliasID='" + aliasID + "'";
- if(dv.Count > 0){
- dv[0]["AliasName"] = aliasName;
- dv[0]["Description"] = Description;
- dv[0]["AliasMembers"] = AliasMembers;
- dv[0]["DomainID"] = domainID;
- }
- dsAliasesCopy.WriteXml(m_DataPath + "Aliases.xml",XmlWriteMode.IgnoreSchema);
- }
- break;
- #endregion
- #region DB_Type.MSSQL
- case DB_Type.MSSQL:
- using(WSqlCommand sqlCmd = new WSqlCommand(m_ConStr,"lspr_UpdateAlias")){
- sqlCmd.AddParameter("@AliasID" ,SqlDbType.UniqueIdentifier,aliasID);
- sqlCmd.AddParameter("@AliasName" ,SqlDbType.NVarChar,aliasName);
- sqlCmd.AddParameter("@Description" ,SqlDbType.NVarChar,Description);
- sqlCmd.AddParameter("@AliasMembers" ,SqlDbType.NVarChar,AliasMembers);
- sqlCmd.AddParameter("@DomainID" ,SqlDbType.UniqueIdentifier,domainID);
-
- DataSet ds = sqlCmd.Execute();
- }
- break;
- #endregion
- }
- }
- #endregion
- #region function GetAliasMembers
- /// <summary>
- /// Gets AliasName AliasMembers.
- /// </summary>
- /// <param name="emailAddress"></param>
- /// <returns>Return null, if AliasName not found.</returns>
- public string[] GetAliasMembers(string emailAddress)
- {
- string[] retVal = null;
- switch(m_DB_Type)
- {
- #region DB_Type.XML
- case DB_Type.XML:
- using(DataView dv = new DataView(dsAliases.Tables["Aliases"])){
- dv.RowFilter = "AliasName = '" + emailAddress + "'";
-
- if(dv.Count > 0){
- string AliasMembers = dv[0]["AliasMembers"].ToString();
- retVal = AliasMembers.Split(new char[]{';'});
- }
- }
- break;
- #endregion
- #region DB_Type.MSSQL
- case DB_Type.MSSQL:
- using(WSqlCommand sqlCmd = new WSqlCommand(m_ConStr,"lspr_GetAliasMembers")){
- sqlCmd.AddParameter("@AliasName",SqlDbType.NVarChar,emailAddress);
- DataSet ds = sqlCmd.Execute();
- ds.Tables[0].TableName = "Aliases";
- if(ds.Tables["Aliases"].Rows.Count > 0){
- string AliasMembers = ds.Tables["Aliases"].Rows[0]["AliasMembers"].ToString();
- return AliasMembers.Split(new char[]{';'});
- }
- }
- break;
- #endregion
- }
- return retVal;
- }
- #endregion
- #endregion
- #region Routing related
- #region function GetRouteList
- /// <summary>
- /// Gets Emails address routes.
- /// </summary>
- /// <returns></returns>
- public DataView GetRouteList()
- {
-
- switch(m_DB_Type)
- {
- #region DB_Type.XML
- case DB_Type.XML:
- if(!dsRouting.Tables["Routing"].Columns.Contains("Length")){
- dsRouting.Tables["Routing"].Columns.Add("Length",Type.GetType("System.Int32"),"Len(Pattern)");
- }
- DataView dv = new DataView(dsRouting.Tables["Routing"]);
- dv.Sort = "DomainID ASC,Length DESC,PATTERN DESC";
- return dv;
- #endregion
- #region DB_Type.MSSQL
- case DB_Type.MSSQL:
- using(WSqlCommand sqlCmd = new WSqlCommand(m_ConStr,"lspr_GetRouteList")){
- // sqlCmd.AddParameter("@DomainName",SqlDbType.NVarChar,source);
- DataSet ds = sqlCmd.Execute();
- ds.Tables[0].TableName = "Routing";
- if(!ds.Tables["Routing"].Columns.Contains("Length")){
- ds.Tables["Routing"].Columns.Add("Length",Type.GetType("System.Int32"),"Len(Pattern)");
- }
- ds.Tables["Routing"].DefaultView.Sort = "DomainName DESC,Length DESC,PATTERN DESC";
- return ds.Tables["Routing"].DefaultView;
- }
- #endregion
- }
- return null;
- }
- #endregion
- #region function AddRoute
- /// <summary>
- /// Adds new Emails route.
- /// </summary>
- /// <param name="pattern">Match pattern.</param>
- /// <param name="mailbox">Mailbox to route.</param>
- /// <param name="Description">Description.</param>
- /// <param name="domainID">DomainName ID.</param>
- /// <returns></returns>
- public DataRow AddRoute(string pattern,string mailbox,string Description,string domainID)
- {
- DataRow retVal = null;
- switch(m_DB_Type)
- {
- #region DB_Type.XML
- case DB_Type.XML:
- //-- Find domainName from domainID ---------------------------//
- string domainName = "";
- foreach(DataRowView drV in this.GetDomainList()){
- if(drV["DomainID"].ToString().ToUpper() == domainID.ToUpper()){
- domainName = drV["DomainName"].ToString();
- }
- }
- DataSet dsRoutingCopy = dsRouting.Copy();
- DataRow dr = dsRoutingCopy.Tables["Routing"].NewRow();
- dr["RouteID"] = Guid.NewGuid().ToString();
- dr["Pattern"] = pattern;
- dr["Mailbox"] = mailbox;
- dr["Description"] = Description;
- dr["DomainID"] = domainID;
- dr["DomainName"] = domainName;
-
- dsRoutingCopy.Tables["Routing"].Rows.Add(dr);
- dsRoutingCopy.Tables["Routing"].Columns.Remove("Length");
- dsRoutingCopy.WriteXml(m_DataPath + "Routing.xml",XmlWriteMode.IgnoreSchema);
- retVal = dr;
- break;
- #endregion
- #region DB_Type.MSSQL
- case DB_Type.MSSQL:
- using(WSqlCommand sqlCmd = new WSqlCommand(m_ConStr,"lspr_AddRoute")){
- sqlCmd.AddParameter("@Pattern" ,SqlDbType.NVarChar ,pattern);
- sqlCmd.AddParameter("@Mailbox" ,SqlDbType.NVarChar ,mailbox);
- sqlCmd.AddParameter("@Description" ,SqlDbType.NVarChar ,Description);
- sqlCmd.AddParameter("@DomainID" ,SqlDbType.UniqueIdentifier ,domainID);
-
- DataSet ds = sqlCmd.Execute();
- ds.Tables[0].TableName = "Routing";
- if(ds.Tables["Routing"].Rows.Count > 0){
- return ds.Tables["Routing"].Rows[0];
- }
- }
- break;
- #endregion
- }
- return retVal;
- }
- #endregion
- #region function DeleteRoute
- /// <summary>
- /// Deletes route.
- /// </summary>
- /// <param name="routeID"></param>
- public void DeleteRoute(string routeID)
- {
- switch(m_DB_Type)
- {
- #region DB_Type.XML
- case DB_Type.XML:
- DataSet dsRoutingCopy = dsRouting.Copy();
- using(DataView dv = new DataView(dsRoutingCopy.Tables["Routing"])){
- dv.RowFilter = "RouteID='" + routeID + "'";
- if(dv.Count > 0){
- dsRoutingCopy.Tables["Routing"].Rows.Remove(dv[0].Row);
- }
- dsRoutingCopy.Tables["Routing"].Columns.Remove("Length");
- dsRoutingCopy.WriteXml(m_DataPath + "Routing.xml",XmlWriteMode.IgnoreSchema);
- }
- break;
- #endregion
- #region DB_Type.MSSQL
- case DB_Type.MSSQL:
- using(WSqlCommand sqlCmd = new WSqlCommand(m_ConStr,"lspr_DeleteRoute")){
- sqlCmd.AddParameter("@RouteID" ,SqlDbType.UniqueIdentifier,routeID);
-
- DataSet ds = sqlCmd.Execute();
- }
- break;
- #endregion
- }
- }
- #endregion
- #region function UpdateRoute
- /// <summary>
- /// Updates Emails route.
- /// </summary>
- /// <param name="routeID"></param>
- /// <param name="pattern"></param>
- /// <param name="mailbox"></param>
- /// <param name="Description"></param>
- /// <param name="domainID"></param>
- public void UpdateRoute(string routeID,string pattern,string mailbox,string Description,string domainID)
- {
- switch(m_DB_Type)
- {
- #region DB_Type.XML
- case DB_Type.XML:
- //-- Find domainName from domainID ---------------------------//
- string domainName = "";
- foreach(DataRow drx in dsDomains.Tables["Domains"].Rows){
- if(drx["DomainID"].ToString() == domainID){
- domainName = drx["DomainName"].ToString();
- }
- }
- DataSet dsRoutingCopy = dsRouting.Copy();
- using(DataView dv = new DataView(dsRoutingCopy.Tables["Routing"])){
- dv.RowFilter = "RouteID='" + routeID + "'";
- if(dv.Count > 0){
- dv[0]["Pattern"] = pattern;
- dv[0]["Mailbox"] = mailbox;
- dv[0]["Description"] = Description;
- dv[0]["DomainID"] = domainID;
- dv[0]["DomainName"] = domainName;
- }
- dsRoutingCopy.Tables["Routing"].Columns.Remove("Length");
- dsRoutingCopy.WriteXml(m_DataPath + "Routing.xml",XmlWriteMode.IgnoreSchema);
- }
- break;
- #endregion
- #region DB_Type.MSSQL
- case DB_Type.MSSQL:
- using(WSqlCommand sqlCmd = new WSqlCommand(m_ConStr,"lspr_UpdateRoute")){
- sqlCmd.AddParameter("@RouteID" ,SqlDbType.UniqueIdentifier,routeID);
- sqlCmd.AddParameter("@Pattern" ,SqlDbType.NVarChar,pattern);
- sqlCmd.AddParameter("@Mailbox" ,SqlDbType.NVarChar,mailbox);
- sqlCmd.AddParameter("@Description" ,SqlDbType.NVarChar,Description);
- sqlCmd.AddParameter("@DomainID" ,SqlDbType.UniqueIdentifier,domainID);
-
- DataSet ds = sqlCmd.Execute();
- }
- break;
- #endregion
- }
- }
- #endregion
- #region function GetMailboxFromPattern
- /// <summary>
- /// Gets mailbox from pattern.
- /// </summary>
- /// <param name="emailAddress"></param>
- /// <returns>Returns mailbox,if any match or null for no match.</returns>
- public string GetMailboxFromPattern(string emailAddress)
- {
- string localPart = "";
- string DomainName = "";
- if(emailAddress.IndexOf("@") > -1){
- localPart = emailAddress.Substring(0,emailAddress.IndexOf("@"));
- DomainName = emailAddress.Substring(emailAddress.IndexOf("@")+1);
- }
- else{
- return null; // Emails address is invalid.
- }
- switch(m_DB_Type)
- {
- #region DB_Type.XML
- case DB_Type.XML:
- //-- Find domainID for domainName ---------------------------//
- string domainID = null;
- foreach(DataRow dr in dsDomains.Tables["Domains"].Rows){
- if(dr["DomainName"].ToString().ToUpper() == DomainName.ToUpper()){
- domainID = dr["DomainID"].ToString();
- }
- }
- // we didin't find any suitable domin
- if(domainID == null){
- return null;
- }
- //----------------------------------------------------------//
- // Find routing patterns for this DomainName
- using(DataView dv = this.GetRouteList()){
- dv.RowFilter = "DomainID='" + domainID + "'";
- // Start tring patterns from 0 to ++ ...
- foreach(DataRowView vDr in dv){
- string pattern = vDr["PATTERN"].ToString();
- string mailbox = vDr["MAILBOX"].ToString();
- if(pattern.StartsWith("*")){
- if(pattern.EndsWith("*")){
- if(localPart.ToUpper().IndexOf(pattern.Replace("*","").ToUpper()) > -1){
- return mailbox;
- }
- }
- else{
- if(localPart.ToUpper().EndsWith(pattern.Replace("*","").ToUpper())){
- return mailbox;
- }
- }
- }
- else if(pattern.EndsWith("*")){
- if(localPart.ToUpper().StartsWith(pattern.Replace("*","").ToUpper())){
- return mailbox;
- }
- }
- }
- }
- break;
- #endregion
- #region DB_Type.XML
- case DB_Type.MSSQL:
- using(WSqlCommand sqlCmd = new WSqlCommand(m_ConStr,"lspr_GetMailboxFromPattern")){
- sqlCmd.AddParameter("@DomainName",SqlDbType.NVarChar,DomainName);
- sqlCmd.AddParameter("@LocalPart" ,SqlDbType.NVarChar,localPart);
- DataSet ds = sqlCmd.Execute();
- ds.Tables[0].TableName = "Detail";
- if(ds.Tables["Detail"].Rows.Count > 0 && !ds.Tables["Detail"].Rows[0].IsNull("Mailbox")){
- return ds.Tables["Detail"].Rows[0]["Mailbox"].ToString();
- }
- }
- break;
- #endregion
- }
- return null;
- }
- #endregion
- #endregion
- #region MailStore related
- #region function GetMessageList
- /// <summary>
- ///
- /// </summary>
- /// <param name="mailBox"></param>
- /// <param name="msgs"></param>
- public void GetMessageList(string mailBox,LumiSoft.MailServer.POP3.POP3_Messages msgs)
- {
- switch(m_DB_Type)
- {
- #region DB_Type.XML
- case DB_Type.XML:
- string path = m_MailStorePath + "Mailboxes\" + mailBox + "\";
- // Check if Directory exists, if not Create
- if(!Directory.Exists(path)){
- Directory.CreateDirectory(path);
- }
-
- string[] files = Directory.GetFiles(path,"*.eml");
- foreach(string file in files){
- int messageSize = 0;
- using(FileStream fStream = File.OpenRead(file)){
- messageSize = (int)fStream.Length;
- }
- msgs.AddMessage(Path.GetFileNameWithoutExtension(file),messageSize);
- }
- break;
- #endregion
- #region DB_Type.MSSQL
- case DB_Type.MSSQL:
- using(WSqlCommand sqlCmd = new WSqlCommand(m_ConStr,"lspr_GetMessageList")){
- sqlCmd.AddParameter("@Mailbox",SqlDbType.NVarChar,mailBox);
- DataSet ds = sqlCmd.Execute();
- ds.Tables[0].TableName = "lsMailStore";
- foreach(DataRow dr in ds.Tables["lsMailStore"].Rows){
- string messageID = dr["MessageID"].ToString();
- int size = Convert.ToInt32(dr["Size"]);
- msgs.AddMessage(messageID,size);
- }
- }
- break;
- #endregion
- }
- }
- #endregion
- #region function StoreMessage
- /// <summary>
- /// Stores message to specified mailbox.
- /// </summary>
- /// <param name="mailbox">Mailbox name.</param>
- /// <param name="msgStream">Stream where message has stored.</param>
- public void StoreMessage(string mailbox,MemoryStream msgStream)
- {
- switch(m_DB_Type)
- {
- #region DB_Type.XML
- case DB_Type.XML:
- // Create dummy file name
- string filename = Guid.NewGuid().ToString();
- filename = filename.Substring(0,22);
- filename = filename.Replace("-","_");
- string path = m_MailStorePath + "MailBoxes\" + mailbox;
- // Check if Directory exists, if not Create
- if(!Directory.Exists(path)){
- Directory.CreateDirectory(path);
- }
-
- //---- Write message data to file -------------------------------//
- using(FileStream fStream = File.Create(path + "\" + filename + ".eml",(int)msgStream.Length)){
- msgStream.WriteTo(fStream);
- // // Write message
- // fStream.Write(msg,0,msg.Length);
- }
- //---------------------------------------------------------------//
- break;
- #endregion
- #region DB_Type.MSSQL
- case DB_Type.MSSQL:
- byte[] topLines = null;
- topLines = GetTopLines(msgStream,50);
-
- using(WSqlCommand sqlCmd = new WSqlCommand(m_ConStr,"lspr_StoreMessage")){
- sqlCmd.AddParameter("@MailBox" ,SqlDbType.NVarChar,mailbox);
- sqlCmd.AddParameter("@Data" ,SqlDbType.Image ,msgStream.ToArray());
- sqlCmd.AddParameter("@Size" ,SqlDbType.BigInt ,msgStream.Length);
- sqlCmd.AddParameter("@TopLines" ,SqlDbType.Image ,topLines);
- DataSet ds = sqlCmd.Execute();
- }
- break;
- #endregion
- }
- }
- #endregion
- #region function DeleteMessage
- /// <summary>
- /// Deletes message from mailbox.
- /// </summary>
- /// <param name="mailbox">MailBox name.</param>
- /// <param name="msgID">MessageID.</param>
- public void DeleteMessage(string mailbox,string msgID)
- {
- switch(m_DB_Type)
- {
- #region DB_Type.XML
- case DB_Type.XML:
- string msgFile = m_MailStorePath + "Mailboxes\" + mailbox + "\" + msgID + ".eml";
- // Check if file exists
- if(File.Exists(msgFile)){
- File.Delete(msgFile);
- }
- break;
- #endregion
- #region DB_Type.MSSQL
- case DB_Type.MSSQL:
- using(WSqlCommand sqlCmd = new WSqlCommand(m_ConStr,"lspr_DeleteMessage")){
- sqlCmd.AddParameter("@MessageID",SqlDbType.NVarChar,msgID);
- DataSet ds = sqlCmd.Execute();
- }
- break;
- #endregion
- }
- }
- #endregion
- #region function GetMessage
- /// <summary>
- /// Gets message from mailbox.
- /// </summary>
- /// <param name="mailbox">Mailbox name.</param>
- /// <param name="msgID">MessageID</param>
- public byte[] GetMessage(string mailbox,string msgID)
- {
- byte[] retVal = null;
- switch(m_DB_Type)
- {
- #region DB_Type.XML
- case DB_Type.XML:
- string msgFile = m_MailStorePath + "Mailboxes\" + mailbox + "\" + msgID + ".eml";
- // Check if file exists
- if(File.Exists(msgFile)){
- //---- Read message from file -----------//
- using(FileStream fStrm = File.OpenRead(msgFile)){
- retVal = new byte[fStrm.Length];
- fStrm.Read(retVal,0,(int)fStrm.Length);
- }
- //---------------------------------------//
- }
- break;
- #endregion
- #region DB_Type.MSSQL
- case DB_Type.MSSQL:
- using(WSqlCommand sqlCmd = new WSqlCommand(m_ConStr,"lspr_GetMessage")){
- sqlCmd.AddParameter("@MessageID",SqlDbType.NVarChar,msgID);
- DataSet ds = sqlCmd.Execute();
- ds.Tables[0].TableName = "lsMailStore";
- return (byte[])ds.Tables["lsMailStore"].Rows[0]["Data"];
- }
- #endregion
- }
- return retVal;
- }
- #endregion
- #region function GetMessageTopLines
- /// <summary>
- /// Gets message header + number of specified lines.
- /// </summary>
- /// <param name="mailbox">Mailbox.</param>
- /// <param name="msgID">MessageID.</param>
- /// <param name="nrLines">Number of lines to retrieve. NOTE: line counting starts at theend of header.</param>
- /// <returns>Returns message header + number of specified lines.</returns>
- public byte[] GetMessageTopLines(string mailbox,string msgID,int nrLines)
- {
- switch(m_DB_Type)
- {
- #region DB_Type.XML
- case DB_Type.XML:
- string msgFile = m_MailStorePath + "Mailboxes\" + mailbox + "\" + msgID + ".eml";
- using(FileStream strm = File.OpenRead(msgFile)){
- return GetTopLines(strm,nrLines);
- }
- #endregion
- #region DB_Type.MSSQL
- case DB_Type.MSSQL:
- using(WSqlCommand sqlCmd = new WSqlCommand(m_ConStr,"lspr_GetMessageTopLines")){
- sqlCmd.AddParameter("@MessageID",SqlDbType.NVarChar,msgID);
- DataSet ds = sqlCmd.Execute();
- ds.Tables[0].TableName = "lsMailStore";
- return (byte[])ds.Tables["lsMailStore"].Rows[0]["TopLines"];
- }
- #endregion
- }
- return null;
- }
- #endregion
- #endregion
-
- #region Security related
- #region function GetSecurity
- /// <summary>
- /// Gets security entries list.
- /// </summary>
- public DataView GetSecurityList()
- {
- DataView retVal = null;
- switch(m_DB_Type)
- {
- #region DB_Type.XML
- case DB_Type.XML:
- retVal = dsSecurity.Tables["Security_List"].DefaultView;
- break;
- #endregion
- #region DB_Type.MSSQL
- case DB_Type.MSSQL:
- using(WSqlCommand sqlCmd = new WSqlCommand(m_ConStr,"lspr_GetSecurityList")){
- // sqlCmd.AddParameter("@DomainName",SqlDbType.NVarChar,source);
- DataSet ds = sqlCmd.Execute();
- ds.Tables[0].TableName = "Security_List";
- return ds.Tables["Security_List"].DefaultView;
- }
- #endregion
- }
- return retVal;
- }
- #endregion
- #region function AddSecurityEntry
- /// <summary>
- /// Adds secuity entry.
- /// </summary>
- /// <param name="Description"></param>
- /// <param name="protocol"></param>
- /// <param name="type"></param>
- /// <param name="action"></param>
- /// <param name="content"></param>
- /// <param name="startIP"></param>
- /// <param name="endIP"></param>
- /// <returns></returns>
- public DataRow AddSecurityEntry(string Description,string protocol,string type,string action,string content,long startIP,long endIP)
- {
- DataRow retVal = null;
- switch(m_DB_Type)
- {
- #region DB_Type.XML
- case DB_Type.XML:
- DataSet dsSecurityCopy = dsAliases.Copy();
- DataRow dr = dsSecurityCopy.Tables["Security_List"].NewRow();
- dr["SecurityID"] = Guid.NewGuid().ToString();
- dr["Description"] = Description;
- dr["Protocol"] = protocol;
- dr["Type"] = type;
- dr["Action"] = action;
- dsSecurityCopy.Tables["Security_List"].Rows.Add(dr);
- dsSecurityCopy.WriteXml(m_DataPath + "Security.xml",XmlWriteMode.IgnoreSchema);
- retVal = dr;
- break;
- #endregion
- #region DB_Type.MSSQL
- case DB_Type.MSSQL:
- using(WSqlCommand sqlCmd = new WSqlCommand(m_ConStr,"lspr_AddSecurityEntry")){
- sqlCmd.AddParameter("@Description" ,SqlDbType.NVarChar,Description);
- sqlCmd.AddParameter("@Protocol" ,SqlDbType.NVarChar,protocol);
- sqlCmd.AddParameter("@Type" ,SqlDbType.NVarChar,type);
- sqlCmd.AddParameter("@Action" ,SqlDbType.NVarChar,action);
- sqlCmd.AddParameter("@Content" ,SqlDbType.NVarChar,content);
- sqlCmd.AddParameter("@StartIP" ,SqlDbType.NVarChar,startIP);
- sqlCmd.AddParameter("@EndIP" ,SqlDbType.NVarChar,endIP);
-
- DataSet ds = sqlCmd.Execute();
- ds.Tables[0].TableName = "Security_List";
- if(ds.Tables["Security_List"].Rows.Count > 0){
- return ds.Tables["Security_List"].Rows[0];
- }
- }
- break;
- #endregion
- }
- return retVal;
- }
- #endregion
- #region function DeleteSecurityEntry
- /// <summary>
- /// Deletes security entry.
- /// </summary>
- /// <param name="securityID"></param>
- public void DeleteSecurityEntry(string securityID)
- {
- switch(m_DB_Type)
- {
- #region DB_Type.XML
- case DB_Type.XML:
- using(DataView dv = new DataView(dsSecurity.Tables["Security_List"])){
- dv.RowFilter = "SecurityID='" + securityID + "'";
- if(dv.Count > 0){
- dsSecurity.Tables["Aliases"].Rows.Remove(dv[0].Row);
- }
- dsSecurity.WriteXml(m_DataPath + "Security.xml",XmlWriteMode.IgnoreSchema);
- }
- break;
- #endregion
- #region DB_Type.MSSQL
- case DB_Type.MSSQL:
- using(WSqlCommand sqlCmd = new WSqlCommand(m_ConStr,"lspr_DeleteSecurityEntry")){
- sqlCmd.AddParameter("@SecurityID" ,SqlDbType.UniqueIdentifier,securityID);
-
- DataSet ds = sqlCmd.Execute();
- }
- break;
- #endregion
- }
- }
- #endregion
- #region function UpdateSecurityEntry
- /// <summary>
- /// Updates security entry.
- /// </summary>
- /// <param name="securityID"></param>
- /// <param name="Description"></param>
- /// <param name="protocol"></param>
- /// <param name="type"></param>
- /// <param name="action"></param>
- /// <param name="content"></param>
- /// <param name="startIP"></param>
- /// <param name="endIP"></param>
- /// <returns></returns>
- public void UpdateSecurityEntry(string securityID,string Description,string protocol,string type,string action,string content,long startIP,long endIP)
- {
- switch(m_DB_Type)
- {
- #region DB_Type.XML
- case DB_Type.XML:
- DataSet dsSecurityCopy = dsAliases.Copy();
- DataRow dr = dsSecurityCopy.Tables["Security_List"].NewRow();
- dr["SecurityID"] = Guid.NewGuid().ToString();
- dr["Description"] = Description;
- dr["Protocol"] = protocol;
- dr["Type"] = type;
- dr["Action"] = action;
- dsSecurityCopy.Tables["Security_List"].Rows.Add(dr);
- dsSecurityCopy.WriteXml(m_DataPath + "Security.xml",XmlWriteMode.IgnoreSchema);
- break;
- #endregion
- #region DB_Type.MSSQL
- case DB_Type.MSSQL:
- using(WSqlCommand sqlCmd = new WSqlCommand(m_ConStr,"lspr_UpdateSecurityEntry")){
- sqlCmd.AddParameter("@SecurityID" ,SqlDbType.UniqueIdentifier,securityID);
- sqlCmd.AddParameter("@Description" ,SqlDbType.NVarChar ,Description);
- sqlCmd.AddParameter("@Protocol" ,SqlDbType.NVarChar ,protocol);
- sqlCmd.AddParameter("@Type" ,SqlDbType.NVarChar ,type);
- sqlCmd.AddParameter("@Action" ,SqlDbType.NVarChar ,action);
- sqlCmd.AddParameter("@Content" ,SqlDbType.NVarChar ,content);
- sqlCmd.AddParameter("@StartIP" ,SqlDbType.BigInt ,startIP);
- sqlCmd.AddParameter("@EndIP" ,SqlDbType.BigInt ,endIP);
-
- DataSet ds = sqlCmd.Execute();
- }
- break;
- #endregion
- }
- }
- #endregion
- #region function IsRelayAllowed
- /// <summary>
- /// Checks if relay is allowed to specified IP.
- /// </summary>
- /// <param name="ip"></param>
- /// <returns>Returns true if relay is allowed.</returns>
- public bool IsRelayAllowed(string ip)
- {
- switch(m_DB_Type)
- {
- #region DB_Type.XML
- case DB_Type.XML:
- //---------------------------------------------------------//
- // First Check if ip is denied, if not check if is allowed
- using(DataView dv = new DataView(dsSecurity.Tables["Security_List"])){
- dv.RowFilter = "Protocol='SMTP' AND Action='Deny Relay' AND SartIP <= " + ip + " AND EndIP >= " + ip;
-
- if(dv.Count > 0){
- return false;
- }
- // Check if ip is allowed
- dv.RowFilter = "Protocol='SMTP' AND Action='Allow Relay' AND SartIP <= " + ip + " AND EndIP >= " + ip;
- if(dv.Count > 0){
- return true;
- }
- }
- break;
- #endregion
- #region DB_Type.MSSQL
- case DB_Type.MSSQL:
- using(WSqlCommand sqlCmd = new WSqlCommand(m_ConStr,"lspr_IsRelayAllowed")){
- sqlCmd.AddParameter("@IP",SqlDbType.BigInt,ip);
- DataSet ds = sqlCmd.Execute();
- ds.Tables[0].TableName = "Detail";
- if(ds.Tables["Detail"].Rows.Count > 0){
- return Convert.ToBoolean(ds.Tables["Detail"].Rows[0]["Allowed"]);
- }
- }
- break;
- #endregion
- }
- return false;
- }
- #endregion
- #region function IsSmtpAccessAllowed
- /// <summary>
- /// Checks if smtp access is allowed for specified IP.
- /// </summary>
- /// <param name="ip"></param>
- /// <returns>Returns true if allowed.</returns>
- public bool IsSmtpAccessAllowed(string ip)
- {
- switch(m_DB_Type)
- {
- #region DB_Type.XML
- case DB_Type.XML:
- using(DataView dv = new DataView(dsSecurity.Tables["Security_List"])){
- //---------------------------------------------------------//
- // First Check if ip is denied, if not check if is allowed
- dv.RowFilter = "Protocol='SMTP' AND Action='Deny' AND SartIP <= " + ip + " AND EndIP >= " + ip;
-
- if(dv.Count > 0){
- return false;
- }
- // Check if ip is allowed
- dv.RowFilter = "Protocol='SMTP' AND Action='Allow' AND SartIP <= " + ip + " AND EndIP >= " + ip;
- if(dv.Count > 0){
- return true;
- }
- }
- break;
- #endregion
- #region DB_Type.MSSQL
- case DB_Type.MSSQL:
- using(WSqlCommand sqlCmd = new WSqlCommand(m_ConStr,"lspr_IsSmtpAccessAllowed")){
- sqlCmd.AddParameter("@IP",SqlDbType.BigInt,ip);
- DataSet ds = sqlCmd.Execute();
- ds.Tables[0].TableName = "Detail";
- if(ds.Tables["Detail"].Rows.Count > 0){
- return Convert.ToBoolean(ds.Tables["Detail"].Rows[0]["Allowed"]);
- }
- }
- break;
- #endregion
- }
- return false;
- }
- #endregion
- #region function IsPop3AccessAllowed
- /// <summary>
- /// Checks if pop3 access is allowed for specified IP.
- /// </summary>
- /// <param name="ip"></param>
- /// <returns>Returns true if allowed.</returns>
- public bool IsPop3AccessAllowed(string ip)
- {
- switch(m_DB_Type)
- {
- #region DB_Type.XML
- case DB_Type.XML:
- using(DataView dv = new DataView(dsSecurity.Tables["Security_List"])){
- //---------------------------------------------------------//
- // First Check if ip is denied, if not check if is allowed
- dv.RowFilter = "Protocol='POP3' AND Action='Deny' AND SartIP <= " + ip + " AND EndIP >= " + ip;
-
- if(dv.Count > 0){
- return false;
- }
- // Check if ip is allowed
- dv.RowFilter = "Protocol='POP3' AND Action='Allow' AND SartIP <= " + ip + " AND EndIP >= " + ip;
- if(dv.Count > 0){
- return true;
- }
- }
- break;
- #endregion
- #region DB_Type.MSSQL
- case DB_Type.MSSQL:
- using(WSqlCommand sqlCmd = new WSqlCommand(m_ConStr,"lspr_IsPop3AccessAllowed")){
- sqlCmd.AddParameter("@IP",SqlDbType.BigInt,ip);
- DataSet ds = sqlCmd.Execute();
- ds.Tables[0].TableName = "Detail";
- if(ds.Tables["Detail"].Rows.Count > 0){
- return Convert.ToBoolean(ds.Tables["Detail"].Rows[0]["Allowed"]);
- }
- }
- break;
- #endregion
- }
- return false;
- }
- #endregion
- #endregion
- #region BackUp related
- #region function CreateBackUp
- /// <summary>
- /// Backups all server.(settings,users,...).
- /// </summary>
- /// <param name="fileName">File name to which store backup.</param>
- /// <param name="dsSettings"></param>
- public void CreateBackUp(string fileName,DataSet dsSettings)
- {
- DataSet dsAll = new DataSet();
- switch(m_DB_Type)
- {
- #region DB_Type.XML
- case DB_Type.XML:
- dsAll.Merge(dsDomains);
- dsAll.Merge(dsUsers);
- dsAll.Merge(dsAliases);
- dsAll.Merge(dsRouting);
- dsAll.Merge(dsSettings);
- dsAll.Merge(dsSecurity);
- break;
- #endregion
- #region DB_Type.MSSQL
- case DB_Type.MSSQL:
- dsAll.Merge(this.GetDomainList().Table);
- dsAll.Merge(this.GetUserList("").Table);
- dsAll.Merge(this.GetAliasesList("").Table);
- dsAll.Merge(this.GetRouteList().Table);
- dsAll.Merge(dsSettings);
- dsAll.Merge(this.GetSecurityList().Table);
- break;
- #endregion
- }
- dsAll.WriteXml(fileName,XmlWriteMode.IgnoreSchema);
- }
- #endregion
- #region function RestoreBackUp
- /// <summary>
- /// Restores server from backup.(settings,users,...).
- /// </summary>
- /// <param name="fileName">File name from which to restore settings.</param>
- public void RestoreBackUp(string fileName)
- {
- DataSet dsAll = new DataSet();
- dsAll.ReadXml(fileName);
- DB_Type dbType = (DB_Type)Enum.Parse(typeof(DB_Type),dsAll.Tables["Settings"].Rows[0]["DataBaseType"].ToString());
- switch(dbType)
- {
- #region DB_Type.XML
- case DB_Type.XML:
-
- if(dsAll.Tables.Contains("Domains")){
- DataSet dsX = new DataSet();
- dsX.Merge(dsAll.Tables["Domains"]);
- dsX.WriteXml(m_DataPath + "Domains.xml",XmlWriteMode.IgnoreSchema);
- }
- if(dsAll.Tables.Contains("Users")){
- DataSet dsX = new DataSet();
- dsX.Merge(dsAll.Tables["Users"]);
- dsX.WriteXml(m_DataPath + "Users.xml",XmlWriteMode.IgnoreSchema);
- }
- if(dsAll.Tables.Contains("Aliases")){
- DataSet dsX = new DataSet();
- dsX.Merge(dsAll.Tables["Aliases"]);
- dsX.WriteXml(m_DataPath + "Aliases.xml",XmlWriteMode.IgnoreSchema);
- }
- if(dsAll.Tables.Contains("Routing")){
- DataSet dsX = new DataSet();
- dsX.Merge(dsAll.Tables["Routing"]);
- dsX.WriteXml(m_DataPath + "Routing.xml",XmlWriteMode.IgnoreSchema);
- }
- if(dsAll.Tables.Contains("Settings")){
- DataSet dsX = new DataSet();
- dsX.Merge(dsAll.Tables["Settings"]);
- dsX.WriteXml(m_DataPath + "Settings.xml",XmlWriteMode.IgnoreSchema);
- }
-
- if(dsAll.Tables.Contains("Security_List")){
- DataSet dsX = new DataSet();
- dsX.Merge(dsAll.Tables["Security_List"]);
- dsX.WriteXml(m_DataPath + "Security.xml",XmlWriteMode.IgnoreSchema);
- }
- break;
- #endregion
- #region DB_Type.MSSQL
- case DB_Type.MSSQL:
- #region Clear old settings
- using(WSqlCommand sqlCmd = new WSqlCommand(m_ConStr,"lspr_TruncateSettings")){
- DataSet ds = sqlCmd.Execute();
- }
- #endregion
- #region Restore domains
- if(dsAll.Tables.Contains("Domains"))
- {
- foreach(DataRow dr in dsAll.Tables["Domains"].Rows){
- using(WSqlCommand sqlCmd = new WSqlCommand(m_ConStr,"lspr_AddDomain")){
- if(dr.Table.Columns.Contains("DomainName")){
- sqlCmd.AddParameter("@DomainName" ,SqlDbType.NVarChar,dr["DomainName"].ToString());
- }
- if(dr.Table.Columns.Contains("Description")){
- sqlCmd.AddParameter("@Description",SqlDbType.NVarChar,dr["Description"].ToString());
- }
- if(dr.Table.Columns.Contains("DomainID")){
- sqlCmd.AddParameter("@DomainID" ,SqlDbType.NVarChar,dr["DomainID"].ToString());
- }
-
- DataSet ds = sqlCmd.Execute();
- }
- }
- }
- #endregion
- #region Restore users
- if(dsAll.Tables.Contains("Users")){
- foreach(DataRow dr in dsAll.Tables["Users"].Rows){
- using(WSqlCommand sqlCmd = new WSqlCommand(m_ConStr,"lspr_AddUser")){
- if(dr.Table.Columns.Contains("FULLNAME")){
- sqlCmd.AddParameter("@FullName" ,SqlDbType.NVarChar,dr["FULLNAME"].ToString());
- }
- if(dr.Table.Columns.Contains("USERNAME")){
- sqlCmd.AddParameter("@UserName" ,SqlDbType.NVarChar,dr["USERNAME"].ToString());
- }
- if(dr.Table.Columns.Contains("PASSWORD")){
- sqlCmd.AddParameter("@Password" ,SqlDbType.NVarChar,dr["PASSWORD"].ToString());
- }
- if(dr.Table.Columns.Contains("Description")){
- sqlCmd.AddParameter("@Description" ,SqlDbType.NVarChar,dr["Description"].ToString());
- }
- if(dr.Table.Columns.Contains("Emails")){
- sqlCmd.AddParameter("@Emails" ,SqlDbType.NVarChar,dr["Emails"].ToString());
- }
- if(dr.Table.Columns.Contains("Mailbox_Size")){
- sqlCmd.AddParameter("@MailboxSize" ,SqlDbType.NVarChar,dr["Mailbox_Size"].ToString());
- }
- if(dr.Table.Columns.Contains("DomainID")){
- sqlCmd.AddParameter("@DomainID" ,SqlDbType.NVarChar,dr["DomainID"].ToString());
- }
- if(dr.Table.Columns.Contains("UserID")){
- sqlCmd.AddParameter("@UserID" ,SqlDbType.NVarChar,dr["UserID"].ToString());
- }
-
- DataSet ds = sqlCmd.Execute();
- }
- }
- }
- #endregion
- #region Restore aliases
- if(dsAll.Tables.Contains("Aliases")){
- foreach(DataRow dr in dsAll.Tables["Aliases"].Rows){
- using(WSqlCommand sqlCmd = new WSqlCommand(m_ConStr,"lspr_AddAlias")){
- if(dr.Table.Columns.Contains("AliasName")){
- sqlCmd.AddParameter("@AliasName" ,SqlDbType.NVarChar,dr["AliasName"].ToString());
- }
- if(dr.Table.Columns.Contains("Description")){
- sqlCmd.AddParameter("@Description" ,SqlDbType.NVarChar,dr["Description"].ToString());
- }
- if(dr.Table.Columns.Contains("AliasMembers")){
- sqlCmd.AddParameter("@Members" ,SqlDbType.NVarChar,dr["AliasMembers"].ToString());
- }
- if(dr.Table.Columns.Contains("DomainID")){
- sqlCmd.AddParameter("@DomainID" ,SqlDbType.UniqueIdentifier,dr["DomainID"].ToString());
- }
- if(dr.Table.Columns.Contains("AliasID")){
- sqlCmd.AddParameter("@AliasID" ,SqlDbType.UniqueIdentifier,dr["AliasID"].ToString());
- }
-
- DataSet ds = sqlCmd.Execute();
- }
- }
- }
- #endregion
- #region Restore routing
- if(dsAll.Tables.Contains("Routing")){
- foreach(DataRow dr in dsAll.Tables["Routing"].Rows){
- using(WSqlCommand sqlCmd = new WSqlCommand(m_ConStr,"lspr_AddRoute")){
- if(dr.Table.Columns.Contains("Pattern")){
- sqlCmd.AddParameter("@Pattern" ,SqlDbType.NVarChar,dr["Pattern"].ToString());
- }
- if(dr.Table.Columns.Contains("Mailbox")){
- sqlCmd.AddParameter("@Mailbox" ,SqlDbType.NVarChar,dr["Mailbox"].ToString());
- }
- if(dr.Table.Columns.Contains("Description")){
- sqlCmd.AddParameter("@Description" ,SqlDbType.NVarChar,dr["Description"].ToString());
- }
- if(dr.Table.Columns.Contains("DomainID")){
- sqlCmd.AddParameter("@DomainID" ,SqlDbType.UniqueIdentifier,dr["DomainID"].ToString());
- }
- if(dr.Table.Columns.Contains("AliasID")){
- sqlCmd.AddParameter("@RouteID" ,SqlDbType.UniqueIdentifier,dr["RouteID"].ToString());
- }
-
- DataSet ds = sqlCmd.Execute();
- }
- }
- }
- #endregion
- #region Restore settings
- if(dsAll.Tables.Contains("Settings")){
- DataSet dsX = new DataSet();
- dsX.Merge(dsAll.Tables["Settings"]);
- dsX.WriteXml(m_DataPath + "Settings.xml",XmlWriteMode.IgnoreSchema);
- }
- #endregion
- #region Restore security
-
- if(dsAll.Tables.Contains("Security_List"))
- {
- foreach(DataRow dr in dsAll.Tables["Security_List"].Rows){
- using(WSqlCommand sqlCmd = new WSqlCommand(m_ConStr,"lspr_AddSecurityEntry")){
- if(dr.Table.Columns.Contains("Description")){
- sqlCmd.AddParameter("@Description" ,SqlDbType.NVarChar,dr["Description"].ToString());
- }
- if(dr.Table.Columns.Contains("Protocol")){
- sqlCmd.AddParameter("@Protocol" ,SqlDbType.NVarChar,dr["Protocol"].ToString());
- }
- if(dr.Table.Columns.Contains("Type")){
- sqlCmd.AddParameter("@Type" ,SqlDbType.NVarChar,dr["Type"].ToString());
- }
- if(dr.Table.Columns.Contains("Action")){
- sqlCmd.AddParameter("@Action" ,SqlDbType.NVarChar,dr["Action"].ToString());
- }
- if(dr.Table.Columns.Contains("Content")){
- sqlCmd.AddParameter("@Content" ,SqlDbType.NVarChar,dr["Content"].ToString());
- }
- if(dr.Table.Columns.Contains("StartIP")){
- sqlCmd.AddParameter("@StartIP" ,SqlDbType.NVarChar,dr["StartIP"].ToString());
- }
- if(dr.Table.Columns.Contains("EndIP")){
- sqlCmd.AddParameter("@EndIP" ,SqlDbType.NVarChar,dr["EndIP"].ToString());
- }
- if(dr.Table.Columns.Contains("SecurityID")){
- sqlCmd.AddParameter("@SecurityID" ,SqlDbType.NVarChar,dr["SecurityID"].ToString());
- }
-
- DataSet ds = sqlCmd.Execute();
- }
- }
- }
- #endregion
- break;
- #endregion
- }
- }
- #endregion
- #endregion
- #region DB_Type.Xml helpers
- #region Load stuff
- /// <summary>
- ///
- /// </summary>
- public void LoadUsers()
- {
- dsUsers.Clear();
- dsUsers.ReadXml(m_DataPath + "Users.xml");
- }
- /// <summary>
- ///
- /// </summary>
- public void LoadAliases()
- {
- dsAliases.Clear();
- dsAliases.ReadXml(m_DataPath + "Aliases.xml");
- }
- /// <summary>
- ///
- /// </summary>
- public void LoadRouting()
- {
- dsRouting.Clear();
- dsRouting.ReadXml(m_DataPath + "Routing.xml");
- }
- /// <summary>
- ///
- /// </summary>
- public void LoadDomains()
- {
- dsDomains.Clear();
- dsDomains.ReadXml(m_DataPath + "Domains.xml");
- }
- /// <summary>
- ///
- /// </summary>
- public void LoadSecurity()
- {
- dsSecurity.Clear();
- dsSecurity.ReadXml(m_DataPath + "Security.xml");
- }
- #endregion
-
- #region Schema stuff
- #region function CreateDomainsSchema
- private void CreateDomainsSchema(DataSet ds)
- {
- // If table is missing, add it
- if(!ds.Tables.Contains("Domains")){
- ds.Tables.Add("Domains");
- }
- // If DomainName column is missing, add it
- if(!ds.Tables["Domains"].Columns.Contains("DomainID")){
- ds.Tables["Domains"].Columns.Add("DomainID",Type.GetType("System.String"));
- }
- // If DomainName column is missing, add it
- if(!ds.Tables["Domains"].Columns.Contains("DomainName")){
- ds.Tables["Domains"].Columns.Add("DomainName",Type.GetType("System.String"));
- }
- // If Description column is missing, add it
- if(!ds.Tables["Domains"].Columns.Contains("Description")){
- ds.Tables["Domains"].Columns.Add("Description",Type.GetType("System.String"));
- }
- }
- #endregion
- #region function CreateUsersSchema
- private void CreateUsersSchema(DataSet ds)
- {
- // If table is missing, add it
- if(!ds.Tables.Contains("Users")){
- ds.Tables.Add("Users");
- }
- // If UserID column is missing, add it
- if(!ds.Tables["Users"].Columns.Contains("UserID")){
- ds.Tables["Users"].Columns.Add("UserID",Type.GetType("System.String"));
- }
- // If DomainID column is missing, add it
- if(!ds.Tables["Users"].Columns.Contains("DomainID")){
- ds.Tables["Users"].Columns.Add("DomainID",Type.GetType("System.String"));
- }
- // If FullName column is missing, add it
- if(!ds.Tables["Users"].Columns.Contains("FullName")){
- ds.Tables["Users"].Columns.Add("FullName",Type.GetType("System.String"));
- }
- // If USERNAME column is missing, add it
- if(!ds.Tables["Users"].Columns.Contains("UserName")){
- ds.Tables["Users"].Columns.Add("UserName",Type.GetType("System.String"));
- }
- // If Description column is missing, add it
- if(!ds.Tables["Users"].Columns.Contains("Password")){
- ds.Tables["Users"].Columns.Add("Password",Type.GetType("System.String"));
- }
- // If Description column is missing, add it
- if(!ds.Tables["Users"].Columns.Contains("Description")){
- ds.Tables["Users"].Columns.Add("Description",Type.GetType("System.String"));
- }
- // If Emails column is missing, add it
- if(!ds.Tables["Users"].Columns.Contains("Emails")){
- ds.Tables["Users"].Columns.Add("Emails",Type.GetType("System.String"));
- }
-
- // If DomainName column is missing, add it
- if(!ds.Tables["Users"].Columns.Contains("DomainName")){
- ds.Tables["Users"].Columns.Add("DomainName",Type.GetType("System.String"));
- }
- // If DomainName column is missing, add it
- if(!ds.Tables["Users"].Columns.Contains("Mailbox_Size")){
- ds.Tables["Users"].Columns.Add("Mailbox_Size",Type.GetType("System.Int32"));
- }
- }
- #endregion
- #region function CreateAliasesSchema
- private void CreateAliasesSchema(DataSet ds)
- {
- // If table is missing, add it
- if(!ds.Tables.Contains("Aliases")){
- ds.Tables.Add("Aliases");
- }
- // If AliasID column is missing, add it
- if(!ds.Tables["Aliases"].Columns.Contains("AliasID")){
- ds.Tables["Aliases"].Columns.Add("AliasID",Type.GetType("System.String"));
- }
- // If DomainID column is missing, add it
- if(!ds.Tables["Aliases"].Columns.Contains("DomainID")){
- ds.Tables["Aliases"].Columns.Add("DomainID",Type.GetType("System.String"));
- }
- // If AliasName column is missing, add it
- if(!ds.Tables["Aliases"].Columns.Contains("AliasName")){
- ds.Tables["Aliases"].Columns.Add("AliasName",Type.GetType("System.String"));
- }
- // If Description column is missing, add it
- if(!ds.Tables["Aliases"].Columns.Contains("Description")){
- ds.Tables["Aliases"].Columns.Add("Description",Type.GetType("System.String"));
- }
- // If AliasMembers column is missing, add it
- if(!ds.Tables["Aliases"].Columns.Contains("AliasMembers")){
- ds.Tables["Aliases"].Columns.Add("AliasMembers",Type.GetType("System.String"));
- }
- }
- #endregion
- #region function CreateRoutingsSchema
- private void CreateRoutingsSchema(DataSet ds)
- {
- // If table is missing, add it
- if(!ds.Tables.Contains("Routing")){
- ds.Tables.Add("Routing");
- }
- // If DomainID column is missing, add it
- if(!ds.Tables["Routing"].Columns.Contains("DomainID")){
- ds.Tables["Routing"].Columns.Add("DomainID",Type.GetType("System.String"));
- }
- // If DomainID column is missing, add it
- if(!ds.Tables["Routing"].Columns.Contains("DomainName")){
- ds.Tables["Routing"].Columns.Add("DomainName",Type.GetType("System.String"));
- }
- // If RouteID column is missing, add it
- if(!ds.Tables["Routing"].Columns.Contains("RouteID")){
- ds.Tables["Routing"].Columns.Add("RouteID",Type.GetType("System.String"));
- }
- // If Pattern column is missing, add it
- if(!ds.Tables["Routing"].Columns.Contains("Pattern")){
- ds.Tables["Routing"].Columns.Add("Pattern",Type.GetType("System.String"));
- }
- // If Mailbox column is missing, add it
- if(!ds.Tables["Routing"].Columns.Contains("Mailbox")){
- ds.Tables["Routing"].Columns.Add("Mailbox",Type.GetType("System.String"));
- }
- // If Description column is missing, add it
- if(!ds.Tables["Routing"].Columns.Contains("Description")){
- ds.Tables["Routing"].Columns.Add("Description",Type.GetType("System.String"));
- }
- }
- #endregion
- #region function CreateSecuritySchema
- private void CreateSecuritySchema(DataSet ds)
- {
- // If table is missing, add it
- if(!ds.Tables.Contains("Security_List")){
- ds.Tables.Add("Security_List");
- }
- // If SecurityID column is missing, add it
- if(!ds.Tables["Security_List"].Columns.Contains("SecurityID")){
- ds.Tables["Security_List"].Columns.Add("SecurityID",Type.GetType("System.String"));
- }
- // If Description column is missing, add it
- if(!ds.Tables["Security_List"].Columns.Contains("Description")){
- ds.Tables["Security_List"].Columns.Add("Description",Type.GetType("System.String"));
- }
- // If Protocol column is missing, add it
- if(!ds.Tables["Security_List"].Columns.Contains("Protocol")){
- ds.Tables["Security_List"].Columns.Add("Protocol",Type.GetType("System.String"));
- }
- // If Type column is missing, add it
- if(!ds.Tables["Security_List"].Columns.Contains("Type")){
- ds.Tables["Security_List"].Columns.Add("Type",Type.GetType("System.String"));
- }
- // If Action column is missing, add it
- if(!ds.Tables["Security_List"].Columns.Contains("Action")){
- ds.Tables["Security_List"].Columns.Add("Action",Type.GetType("System.String"));
- }
- // If Content column is missing, add it
- if(!ds.Tables["Security_List"].Columns.Contains("Content")){
- ds.Tables["Security_List"].Columns.Add("Content",Type.GetType("System.String"));
- }
- // If SartIP column is missing, add it
- if(!ds.Tables["Security_List"].Columns.Contains("SartIP")){
- ds.Tables["Security_List"].Columns.Add("SartIP",Type.GetType("System.String"));
- }
-
- // If EndIP column is missing, add it
- if(!ds.Tables["Security_List"].Columns.Contains("EndIP")){
- ds.Tables["Security_List"].Columns.Add("EndIP",Type.GetType("System.String"));
- }
- }
- #endregion
- #endregion
- #endregion
- #region Helpers
- #region function GetTopLines
- private byte[] GetTopLines(Stream strm,int nrLines)
- {
- TextReader reader = (TextReader)new StreamReader(strm);
-
- int lCounter = 0;
- int msgLine = -1;
- bool msgLines = false;
- StringBuilder strBuilder = new StringBuilder();
- while(true){
- string line = reader.ReadLine();
- // Reached end of message
- if(line == null){
- break;
- }
- else{
- // End of header reached
- if(!msgLines && line.Length == 0){
- // Set flag, that message lines reading start.
- msgLines = true;
- }
- // Check that wanted message lines count isn't exceeded
- if(msgLines){
- if(msgLine > nrLines){
- break;
- }
- msgLine++;
- }
- strBuilder.Append(line + "rn");
- }
- // Don't allow read more than 150 lines
- if(lCounter > 150){
- break;
- }
- lCounter++;
- }
-
- return System.Text.Encoding.ASCII.GetBytes(strBuilder.ToString());
- }
- #endregion
- #endregion
- #region static function IsConnection
- /// <summary>
- /// Checks if database connection is ok.
- /// </summary>
- /// <param name="dataPath"></param>
- /// <param name="conStr"></param>
- /// <param name="dbType"></param>
- /// <returns></returns>
- public static bool IsConnection(string dataPath,string conStr,DB_Type dbType)
- {
- try
- {
- switch(dbType)
- {
- case DB_Type.XML:
- return Directory.Exists(dataPath);
- case DB_Type.MSSQL:
- using(WSqlCommand sqlCmd = new WSqlCommand(conStr,"lspr_CheckConnection")){
- sqlCmd.Execute();
- }
- return true;
- }
- }
- catch{
- }
- return false;
- }
- #endregion
-
- }
- }