Esempio n. 1
0
        /// <summary>
        /// Gets server groups and binds them to this.
        /// </summary>
        private void Bind()
        {
            /* GetRoutes <virtualServerID>
             *    Responses:
             +OK <sizeOfData>
             *      <data>
             *
             *      -ERR <errorText>
             */

            lock (m_pVirtualServer.Server.LockSynchronizer){
                // Call TCP GetRoutes
                m_pVirtualServer.Server.TcpClient.TcpStream.WriteLine("GetRoutes " + m_pVirtualServer.VirtualServerID);

                string response = m_pVirtualServer.Server.ReadLine();
                if (!response.ToUpper().StartsWith("+OK"))
                {
                    throw new Exception(response);
                }

                int          sizeOfData = Convert.ToInt32(response.Split(new char[] { ' ' }, 2)[1]);
                MemoryStream ms         = new MemoryStream();
                m_pVirtualServer.Server.TcpClient.TcpStream.ReadFixedCount(ms, sizeOfData);

                // Decompress dataset
                DataSet ds = Utils.DecompressDataSet(ms);

                if (ds.Tables.Contains("Routing"))
                {
                    foreach (DataRow dr in ds.Tables["Routing"].Rows)
                    {
                        RouteAction_enum actionType = (RouteAction_enum)Convert.ToInt32(dr["Action"]);
                        RouteActionBase  action     = null;
                        if (actionType == RouteAction_enum.RouteToEmail)
                        {
                            action = new RouteAction_RouteToEmail(Convert.FromBase64String(dr["ActionData"].ToString()));
                        }
                        else if (actionType == RouteAction_enum.RouteToHost)
                        {
                            action = new RouteAction_RouteToHost(Convert.FromBase64String(dr["ActionData"].ToString()));
                        }
                        else if (actionType == RouteAction_enum.RouteToMailbox)
                        {
                            action = new RouteAction_RouteToMailbox(Convert.FromBase64String(dr["ActionData"].ToString()));
                        }

                        m_pRoutes.Add(new Route(
                                          this,
                                          dr["RouteID"].ToString(),
                                          Convert.ToInt64(dr["Cost"]),
                                          dr["Description"].ToString(),
                                          dr["Pattern"].ToString(),
                                          Convert.ToBoolean(dr["Enabled"].ToString()),
                                          action
                                          ));
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Gets server groups and binds them to this.
        /// </summary>
        private void Bind()
        {
            /* GetRoutes <virtualServerID>
                  Responses:
                    +OK <sizeOfData>
                    <data>
                    
                    -ERR <errorText>
            */
            
            lock(m_pVirtualServer.Server.LockSynchronizer){
                // Call TCP GetRoutes
                m_pVirtualServer.Server.TcpClient.TcpStream.WriteLine("GetRoutes " + m_pVirtualServer.VirtualServerID);

                string response = m_pVirtualServer.Server.ReadLine();
                if(!response.ToUpper().StartsWith("+OK")){
                    throw new Exception(response);
                }

                int sizeOfData = Convert.ToInt32(response.Split(new char[]{' '},2)[1]);
                MemoryStream ms = new MemoryStream();
                m_pVirtualServer.Server.TcpClient.TcpStream.ReadFixedCount(ms,sizeOfData);
                
                // Decompress dataset
                DataSet ds = Utils.DecompressDataSet(ms);

                if(ds.Tables.Contains("Routing")){
                    foreach(DataRow dr in ds.Tables["Routing"].Rows){
                        RouteAction_enum actionType = (RouteAction_enum)Convert.ToInt32(dr["Action"]);
                        RouteActionBase action = null;
                        if(actionType == RouteAction_enum.RouteToEmail){
                            action = new RouteAction_RouteToEmail(Convert.FromBase64String(dr["ActionData"].ToString()));
                        }
                        else if(actionType == RouteAction_enum.RouteToHost){
                            action = new RouteAction_RouteToHost(Convert.FromBase64String(dr["ActionData"].ToString()));
                        }
                        else if(actionType == RouteAction_enum.RouteToMailbox){
                            action = new RouteAction_RouteToMailbox(Convert.FromBase64String(dr["ActionData"].ToString()));
                        }
                      
                        m_pRoutes.Add(new Route(
                            this,
                            dr["RouteID"].ToString(),
                            Convert.ToInt64(dr["Cost"]),
                            dr["Description"].ToString(),
                            dr["Pattern"].ToString(),
                            Convert.ToBoolean(dr["Enabled"].ToString()),
                            action
                        ));
                    }
                }
            }
        }
        /// <summary>
        /// Restores all virtual server settings from the specified stream.
        /// </summary>
        /// <param name="stream">Stream what conatins backup.</param>
        /// <param name="restoreFlags">Specifies restore options.</param>
        public void Restore(Stream stream,RestoreFlags_enum restoreFlags)
        {
            DataSet ds = new DataSet();
            ds.ReadXml(stream);

            #region Settings

            /* NOTE: Settings need special handling, we can't comapre settings, so just always overwrite it.
            */

            if(ds.Tables.Contains("Settings")){
                this.SystemSettings.LoadSettings(ds);
                this.SystemSettings.Commit();
            }

            #endregion

            #region Domains

            /* NOTE: Domains must act differenly compared other object, because deleting domain looses mailboxes.
                     We need to update domain, if ResoreFlags_enum.Replace, we can't delete domain !!!
            */

            if(ds.Tables.Contains("Domains")){
                foreach(DataRow dr in ds.Tables["Domains"].Rows){
                    // Update exisiting domain, if ResoreFlags_enum.Replace
                    if(this.Domains.Contains(dr["DomainName"].ToString())){
                        if((restoreFlags & RestoreFlags_enum.Replace) != 0){
                            // TODO:
                        }
                    }
                    // Add domain list, if ResoreFlags_enum.Add
                    else if((restoreFlags & RestoreFlags_enum.Add) != 0){
                        Domain domain = this.Domains.Add(
                            dr["DomainName"].ToString(),
                            dr["Description"].ToString()
                        );
                    }
                }
            }

            #endregion

            #region Users

            /* NOTE: Users must act differenly compared other object, because deleting user looses mailbox.
                     We need to update user, if ResoreFlags_enum.Replace, we can't delete user !!!
            */

            if(ds.Tables.Contains("Users")){
                foreach(DataRow dr in ds.Tables["Users"].Rows){
                    User user        = null;
                    bool userChanged = false;
                    // Update exisiting domain, if ResoreFlags_enum.Replace
                    if(this.Users.Contains(dr["UserName"].ToString())){
                        if((restoreFlags & RestoreFlags_enum.Replace) != 0){
                            user = this.Users.GetUserByName(dr["UserName"].ToString());
                            user.UserName           = dr["UserName"].ToString();
                            user.FullName           = dr["FullName"].ToString();
                            user.Password           = dr["Password"].ToString();
                            user.Description        = dr["Description"].ToString();
                            user.MaximumMailboxSize = ConvertEx.ToInt32(dr["Mailbox_Size"]);
                            user.Enabled            = ConvertEx.ToBoolean(dr["Enabled"]);
                            user.Permissions        = (UserPermissions_enum)ConvertEx.ToInt32(dr["Permissions"]);
                            user.Commit();

                            // Delete user email addresses
                            foreach(string emailAddress in user.EmailAddresses.ToArray()){
                                user.EmailAddresses.Remove(emailAddress);
                            }

                            // Delete user remote servers
                            foreach(UserRemoteServer server in user.RemoteServers.ToArray()){
                                user.RemoteServers.Remove(server);
                            }

                            // Delete user message rules
                            foreach(UserMessageRule rule in user.MessageRules.ToArray()){
                                user.MessageRules.Remove(rule);
                            }

                            userChanged = true;
                        }
                    }
                    // Add domain list, if ResoreFlags_enum.Add
                    else if((restoreFlags & RestoreFlags_enum.Add) != 0){
                        user = this.Users.Add(
                            dr["UserName"].ToString(),
                            dr["FullName"].ToString(),
                            dr["Password"].ToString(),
                            dr["Description"].ToString(),
                            ConvertEx.ToInt32(dr["Mailbox_Size"]),
                            ConvertEx.ToBoolean(dr["Enabled"]),
                            (UserPermissions_enum)ConvertEx.ToInt32(dr["Permissions"])
                        );
                        userChanged = true;
                    }

                    if(userChanged){
                        // Add user remote servers
                        if(ds.Tables.Contains("User_EmailAddresses")){
                            DataView dv = new DataView(ds.Tables["User_EmailAddresses"]);
                            dv.RowFilter = "UserID='" + dr["UserID"].ToString() + "'";
                            foreach(DataRowView drV in dv){
                                user.EmailAddresses.Add(
                                    drV["EmailAddress"].ToString()
                                );
                            }
                        }

                        // Add user remote servers
                        if(ds.Tables.Contains("User_RemoteServers")){
                            DataView dv = new DataView(ds.Tables["User_RemoteServers"]);
                            dv.RowFilter = "UserID='" + dr["UserID"].ToString() + "'";
                            foreach(DataRowView drV in dv){
                                user.RemoteServers.Add(
                                    drV["Description"].ToString(),
                                    drV["RemoteServer"].ToString(),
                                    ConvertEx.ToInt32(drV["RemotePort"]),
                                    ConvertEx.ToBoolean(drV["UseSSL"]),
                                    drV["RemoteUserName"].ToString(),
                                    drV["RemotePassword"].ToString(),
                                    ConvertEx.ToBoolean(drV["Enabled"])
                                );
                            }
                        }

                        // Add user message rules
                        if(ds.Tables.Contains("User_MessageRules")){
                            DataView dv = new DataView(ds.Tables["User_MessageRules"]);
                            dv.RowFilter = "UserID='" + dr["UserID"].ToString() + "'";
                            foreach(DataRowView drV in dv){
                                UserMessageRule rule = user.MessageRules.Add(
                                    ConvertEx.ToBoolean(drV["Enabled"]),
                                    drV["Description"].ToString(),
                                    drV["MatchExpression"].ToString(),
                                    (GlobalMessageRule_CheckNextRule_enum)ConvertEx.ToInt32(drV["CheckNextRuleIf"])
                                );

                                // Add rule actions
                                if(ds.Tables.Contains("User_MessageRuleActions")){
                                    DataView dvRuleActions = new DataView(ds.Tables["User_MessageRuleActions"]);
                                    dvRuleActions.RowFilter = "UserID='" + dr["UserID"].ToString() + "' AND RuleID='" + drV["RuleID"].ToString() + "'";
                                    foreach(DataRowView drvAction in dvRuleActions){
                                        rule.Actions.Add(
                                            drvAction["ActionID"].ToString(),
                                            drvAction["Description"].ToString(),
                                            (UserMessageRuleAction_enum)Convert.ToInt32(drvAction["ActionType"]),
                                            Convert.FromBase64String(drvAction["ActionData"].ToString()),
                                            true
                                        );
                                    }
                                }
                            }
                        }
                    }
                }
            }

            #endregion

            #region Groups

            if(ds.Tables.Contains("Groups")){
                foreach(DataRow dr in ds.Tables["Groups"].Rows){
                    // Delete exisiting group, if ResoreFlags_enum.Replace
                    bool replaceAdd = false;
                    if(this.Groups.Contains(dr["GroupName"].ToString()) && (restoreFlags & RestoreFlags_enum.Replace) != 0){
                        this.Groups.Remove(this.Groups.GetGroupByName(dr["GroupName"].ToString()));
                        replaceAdd = true;
                    }

                    // Add group, if ResoreFlags_enum.Add or Replace group add
                    if(!this.Groups.Contains(dr["GroupName"].ToString()) && (replaceAdd || (restoreFlags & RestoreFlags_enum.Add) != 0)){
                        Group group = this.Groups.Add(
                            dr["GroupName"].ToString(),
                            dr["Description"].ToString(),
                            ConvertEx.ToBoolean(dr["Enabled"])
                        );

                        // Add group members
                        if(ds.Tables.Contains("Group_Members")){
                            DataView dv = new DataView(ds.Tables["Group_Members"]);
                            dv.RowFilter = "GroupID='" + dr["GroupID"].ToString() + "'";
                            foreach(DataRowView drV in dv){
                                group.Members.Add(drV["UserOrGroup"].ToString());
                            }
                        }
                    }
                }
            }

            #endregion

            #region Mailing lists

            if(ds.Tables.Contains("MailingLists")){
                foreach(DataRow dr in ds.Tables["MailingLists"].Rows){
                    // Delete exisiting mailing list, if ResoreFlags_enum.Replace
                    bool replaceAdd = false;
                    if(this.MailingLists.Contains(dr["MailingListName"].ToString()) && (restoreFlags & RestoreFlags_enum.Replace) != 0){
                        this.MailingLists.Remove(this.MailingLists.GetMailingListByName(dr["MailingListName"].ToString()));
                        replaceAdd = true;
                    }

                    // Add mailing list, if ResoreFlags_enum.Add or Replace mailing list add
                    if(!this.MailingLists.Contains(dr["MailingListName"].ToString()) && (replaceAdd || (restoreFlags & RestoreFlags_enum.Add) != 0)){
                        MailingList list = this.MailingLists.Add(
                            dr["MailingListName"].ToString(),
                            dr["Description"].ToString(),
                            ConvertEx.ToBoolean(dr["Enabled"])
                        );

                        // Add mailing list members
                        if(ds.Tables.Contains("MailingList_Members")){
                            DataView dv = new DataView(ds.Tables["MailingList_Members"]);
                            dv.RowFilter = "MailingListID='" + dr["MailingListID"].ToString() + "'";
                            foreach(DataRowView drV in dv){
                                list.Members.Add(drV["Address"].ToString());
                            }
                        }

                        // Add mailing list ACL
                        if(ds.Tables.Contains("MailingList_ACL")){
                            DataView dv = new DataView(ds.Tables["MailingList_ACL"]);
                            dv.RowFilter = "MailingListID='" + dr["MailingListID"].ToString() + "'";
                            foreach(DataRowView drV in dv){
                                list.ACL.Add(drV["UserOrGroup"].ToString());
                            }
                        }
                    }
                }
            }

            #endregion

            #region Routes

            if(ds.Tables.Contains("Routing")){
                foreach(DataRow dr in ds.Tables["Routing"].Rows){
                    // Delete exisiting route, if ResoreFlags_enum.Replace
                    bool replaceAdd = false;
                    if(this.Routes.ContainsPattern(dr["Pattern"].ToString()) && (restoreFlags & RestoreFlags_enum.Replace) != 0){
                        this.Routes.Remove(this.Routes.GetRouteByPattern(dr["Pattern"].ToString()));
                        replaceAdd = true;
                    }

                    // Add route, if ResoreFlags_enum.Add or Replace route add
                    if(!this.Routes.ContainsPattern(dr["Pattern"].ToString()) && (replaceAdd || (restoreFlags & RestoreFlags_enum.Add) != 0)){
                        RouteAction_enum actionType = (RouteAction_enum)Convert.ToInt32(dr["Action"]);
                        RouteActionBase action = null;
                        if(actionType == RouteAction_enum.RouteToEmail){
                            action = new RouteAction_RouteToEmail(Convert.FromBase64String(dr["ActionData"].ToString()));
                        }
                        else if(actionType == RouteAction_enum.RouteToHost){
                            action = new RouteAction_RouteToHost(Convert.FromBase64String(dr["ActionData"].ToString()));
                        }
                        else if(actionType == RouteAction_enum.RouteToMailbox){
                            action = new RouteAction_RouteToMailbox(Convert.FromBase64String(dr["ActionData"].ToString()));
                        }

                        Route route = this.Routes.Add(
                            dr["Description"].ToString(),
                            dr["Pattern"].ToString(),
                            ConvertEx.ToBoolean(dr["Enabled"]),
                            action
                        );
                    }
                }
             }

            #endregion

            #region Global message rules

            // Add user message rules
            if(ds.Tables.Contains("GlobalMessageRules")){
                // Delete exisiting global message rules, if ResoreFlags_enum.Replace
                if((restoreFlags & RestoreFlags_enum.Replace) != 0){
                    foreach(GlobalMessageRule rule in this.GlobalMessageRules){
                        this.GlobalMessageRules.Remove(rule);
                    }
                }

                DataView dv = new DataView(ds.Tables["GlobalMessageRules"]);
                foreach(DataRowView drV in dv){
                    GlobalMessageRule rule = this.GlobalMessageRules.Add(
                        ConvertEx.ToBoolean(drV["Enabled"]),
                        drV["Description"].ToString(),
                        drV["MatchExpression"].ToString(),
                        (GlobalMessageRule_CheckNextRule_enum)ConvertEx.ToInt32(drV["CheckNextRuleIf"])
                     );

                     // Add rule actions
                     if(ds.Tables.Contains("GlobalMessageRuleActions")){
                         DataView dvRuleActions = new DataView(ds.Tables["GlobalMessageRuleActions"]);
                         dvRuleActions.RowFilter = "RuleID='" + drV["RuleID"].ToString() + "'";
                         foreach(DataRowView drvAction in dvRuleActions){
                             rule.Actions.Add(
                                 drvAction["ActionID"].ToString(),
                                 drvAction["Description"].ToString(),
                                 (GlobalMessageRuleAction_enum)Convert.ToInt32(drvAction["ActionType"]),
                                 Convert.FromBase64String(drvAction["ActionData"].ToString()),
                                 true
                             );
                         }
                     }
                 }
             }

            #endregion

            #region IP Security

            /* NOTE: IP security need special handling, we can't comapre values, so just always overwrite it.
            */

            if(ds.Tables.Contains("IP_Security")){
                // Delete filters
                foreach(IPSecurity entry in this.IpSecurity.ToArray()){
                    this.IpSecurity.Remove(entry);
                }

                foreach(DataRow dr in ds.Tables["IP_Security"].Rows){
                    this.IpSecurity.Add(
                        ConvertEx.ToBoolean(dr["Enabled"]),
                        dr["Description"].ToString(),
                        (Service_enum)ConvertEx.ToInt32(dr["Service"]),
                        (IPSecurityAction_enum)ConvertEx.ToInt32(dr["Action"]),
                        IPAddress.Parse(dr["StartIP"].ToString()),
                        IPAddress.Parse(dr["EndIP"].ToString())
                    );
                }
             }

            #endregion

            #region Filters

            /* NOTE: Filters need special handling, we can't comapre values, so just always overwrite it.
            */

            if(ds.Tables.Contains("Filters")){
                // Delete filters
                foreach(Filter filter in this.Filters.ToArray()){
                    this.Filters.Remove(filter);
                }

                foreach(DataRow dr in ds.Tables["Filters"].Rows){
                    this.Filters.Add(
                        ConvertEx.ToBoolean(dr["Enabled"]),
                        dr["Description"].ToString(),
                        dr["Assembly"].ToString(),
                        dr["ClassName"].ToString()
                    );
                }
             }

            #endregion

            #region Users Default Folders

            if(ds.Tables.Contains("UsersDefaultFolders")){
                foreach(DataRow dr in ds.Tables["UsersDefaultFolders"].Rows){
                    // Delete exisiting folder and add new, if ResoreFlags_enum.Replace
                    if(this.UsersDefaultFolders.Contains(dr["FolderName"].ToString())){
                        if((restoreFlags & RestoreFlags_enum.Replace) != 0){
                            this.UsersDefaultFolders.Remove(this.UsersDefaultFolders.GetFolderByName(dr["FolderName"].ToString()));

                            UsersDefaultFolder folder = this.UsersDefaultFolders.Add(
                                dr["FolderName"].ToString(),
                                ConvertEx.ToBoolean(dr["Permanent"])
                            );
                        }
                    }
                    // Add domain list, if ResoreFlags_enum.Add
                    else if((restoreFlags & RestoreFlags_enum.Add) != 0){
                        UsersDefaultFolder folder = this.UsersDefaultFolders.Add(
                            dr["FolderName"].ToString(),
                            ConvertEx.ToBoolean(dr["Permanent"])
                        );
                    }
                }
            }

            #endregion
        }
Esempio n. 4
0
        private void m_pOk_Click(object sender, EventArgs e)
        {
            if(m_pPattern.Text.Length == 0){
                MessageBox.Show(this,"Please specify match pattern !","Error:",MessageBoxButtons.OK,MessageBoxIcon.Error);
                return;
            }

            RouteAction_enum action     = (RouteAction_enum)((WComboBoxItem)m_pAction.SelectedItem).Tag;
            RouteActionBase  actionData = null;

            #region Route To Mailbox

            if(action == RouteAction_enum.RouteToMailbox){
                //--- Validate values ------------------------------------------------//
                if(m_pRouteToMailbox_Mailbox.Text == ""){
                    MessageBox.Show(this,"Mailbox: value can't empty !","Error:",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    return;
                }
                //--------------------------------------------------------------------//

                actionData = new RouteAction_RouteToMailbox(m_pRouteToMailbox_Mailbox.Text);
            }

            #endregion

            #region Route To Email

            else if(action == RouteAction_enum.RouteToEmail){
                //--- Validate values ------------------------------------------------//
                if(m_pRouteToEmail_Email.Text == ""){
                    MessageBox.Show(this,"Email: value can't empty !","Error:",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    return;
                }
                //--------------------------------------------------------------------//

                actionData = new RouteAction_RouteToEmail(m_pRouteToEmail_Email.Text);
            }

            #endregion

            #region Route To Host

            else if(action == RouteAction_enum.RouteToHost){
                //--- Validate values ------------------------------------------------//
                if(m_pRouteToHost_Host.Text == ""){
                    MessageBox.Show(this,"Host: value can't empty !","Error:",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    return;
                }
                //--------------------------------------------------------------------//

                actionData = new RouteAction_RouteToHost(m_pRouteToHost_Host.Text,(int)m_pRouteToHost_Port.Value);
            }

            #endregion
            
            if(m_pRoute == null){
                m_pRoute = m_pVirtualServer.Routes.Add(
                    m_pDescription.Text,
                    m_pPattern.Text,
                    m_pEnabled.Checked,
                    actionData
                );
            }
            else{
                m_pRoute.Description = m_pDescription.Text;
                m_pRoute.Pattern = m_pPattern.Text;
                m_pRoute.Enabled = m_pEnabled.Checked;
                m_pRoute.Action = actionData;
                m_pRoute.Commit();
            }
            
            this.DialogResult = DialogResult.OK;
            this.Close();
        }