public ActionResult Edit(Profile profile)
 {
     if (_service.EditProfile(profile))
     {
         //var _profile = _service.GetProfile(profile.ProfileId);
         _sshService.EditPPPProfile(profile.ProfileId);
         return View("Index", PrepareIndex());
     }
     return View(profile);
 }
        public bool CreateProfile(Profile profile)
        {
            // Validation logic
            if (!ValidateProfile(profile))
                return false;

            // Database logic
            try
            {
                _repository.CreateProfile(profile);
            }
            catch (Exception ex)
            {
                _validationDictionary.AddError("_FORM", "Profile is not saved. " + ex.Message);
                return false;
            }
            return true;
        }
        public string ppp_profile_add(Profile profile)
        {
            var command = new StringBuilder("ppp profile add ");
            command.Append(Repository.BuildCommand("name", profile.Name))
                .Append(Repository.BuildCommand("local-address", profile.LocalAddress))
                .Append(Repository.BuildCommand("remote-address", profile.PoolName))
                .Append(Repository.BuildCommand("rate-limit", profile.RateLimit));

            return Repository.RunCommand(command.ToString());
        }
        public List<Profile> BuildProfiles(string profiles)
        {
            var _profiles = new List<Profile>();
            foreach (var val in Regex.Split(profiles, "\r\n\r\n"))
            {
                var _profile = new Profile();
                var properties = Repository.BuildProperties(val);
                _profile.Name = Repository.GetValue(properties, "name");
                _profile.LocalAddress = Repository.GetValue(properties, "local-address");
                _profile.PoolName = Repository.GetValue(properties, "remote-address");
                _profile.RateLimit = Repository.GetValue(properties, "rate-limit");
                if (!string.IsNullOrEmpty(_profile.Name))
                    _profiles.Add(_profile);
            }

            //var r = new Regex(PATTERN, RegexOptions.IgnorePatternWhitespace);
            //var r2 = new Regex(@"\x0D\x0A\x20+", RegexOptions.IgnorePatternWhitespace);
            //var str = r2.Split(queue);
            //var safeQueue = new StringBuilder();
            //foreach (var s in str)
            //{
            //    safeQueue.Append(s);
            //}
            //var m = r.Match(safeQueue.ToString());
            //while (m.Success)
            //{
            //    var gk = m.Groups["Key"];
            //    var gv = m.Groups["Value"];
            //    var profile = new Profile();
            //    for (var i = 0; i < gk.Captures.Count; i++)
            //    {
            //        switch (gk.Captures[i].Value)
            //        {
            //            case "name":
            //                profile.Name = gv.Captures[i].Value.Trim();
            //                break;
            //            case "* name":
            //                profile.Name = gv.Captures[i].Value.Trim();
            //                break;
            //            case "local-address":
            //                profile.LocalAddress = gv.Captures[i].Value.Trim();
            //                break;
            //            case "remote-address":
            //                profile.PoolName = gv.Captures[i].Value.Trim();
            //                break;
            //            case "rate-limit":
            //                profile.RateLimit = gv.Captures[i].Value.Trim();
            //                break;
            //            //case "max-limit":
            //            //    var l = gv.Captures[i].Value.Trim().Split(new Char[] { '/' });
            //            //    tariff.MaxUploadLimit = BuildSpeed(l[0].Trim());
            //            //    tariff.MaxDownloadLimit = BuildSpeed(l[1].Trim());
            //            //    break;
            //        }
            //    }
            //    if (!String.IsNullOrEmpty(profile.Name))
            //        profiles.Add(profile);
            //    m = m.NextMatch();
            //}
            return _profiles;
        }
        public string ppp_profile_set(Profile profile)
        {
            var command = new StringBuilder("ppp profile set ");
            string retVal;
            command.Append((string.IsNullOrEmpty(profile.OldName)? profile.Name : profile.OldName) + " ")
                .Append(Repository.BuildCommand("name", profile.Name))
                .Append(Repository.BuildCommand("local-address", profile.LocalAddress))
                .Append(Repository.BuildCommand("remote-address", profile.PoolName))
                .Append(Repository.BuildCommand("rate-limit", profile.RateLimit));
            retVal = Repository.RunCommand(command.ToString());

            command.Remove(0, command.Length);

            if (string.IsNullOrEmpty(profile.PoolName))
            {
                Repository.RunCommand(string.Format("ppp profile unset {0} remote-address", profile.Name));
            }
            if (string.IsNullOrEmpty(profile.LocalAddress))
            {
                Repository.RunCommand(string.Format("ppp profile unset {0} local-address", profile.Name));
            }
            if (string.IsNullOrEmpty(profile.RateLimit))
            {
                Repository.RunCommand(string.Format("ppp profile unset {0} rate-limit", profile.Name));
            }

            return retVal;
        }
        public string ppp_profile_set(Profile profile)
        {
            var command = new StringBuilder("ppp profile set ");
            command.Append(profile.Name + " ")
                .Append(BuildCommand("local-address", profile.LocalAddress))
                .Append(BuildCommand("remote-address", profile.PoolName))
                .Append(BuildCommand("rate-limit", profile.RateLimit));
            _sshStream.Write(command.ToString());

            command.Remove(0, command.Length);

            if (string.IsNullOrEmpty(profile.PoolName))
            {
                _sshStream.ReadResponse();
                _sshStream.Write(string.Format("ppp profile unset {0} remote-address", profile.Name));
            }
            if (string.IsNullOrEmpty(profile.LocalAddress))
            {
                _sshStream.ReadResponse();
                _sshStream.Write(string.Format("ppp profile unset {0} local-address", profile.Name));
            }
            if (string.IsNullOrEmpty(profile.RateLimit))
            {
                _sshStream.ReadResponse();
                _sshStream.Write(string.Format("ppp profile unset {0} rate-limit", profile.Name));
            }

            return ParseResponse(_sshStream.ReadResponse(), command.ToString());
        }
        public string ppp_profile_add(Profile profile)
        {
            var command = new StringBuilder("ppp profile add ");
            command.Append(BuildCommand("name", profile.Name))
                .Append(BuildCommand("local-address", profile.LocalAddress))
                .Append(BuildCommand("remote-address", profile.PoolName))
                .Append(BuildCommand("rate-limit", profile.RateLimit));

            _sshStream.Write(command.ToString());
            return ParseResponse(_sshStream.ReadResponse(), command.ToString());
        }
 public bool EditProfile(Profile profile)
 {
     if (!ValidateProfile(profile))
         return false;
     try
     {
         _repository.EditProfile(profile);
     }
     catch (Exception ex)
     {
         _validationDictionary.AddError("_FORM", "Profile is not saved. " + ex.Message);
         return false;
     }
     return true;
 }
        public bool ValidateProfile(Profile profile)
        {
            bool isValid = true;
            var r = new Regex(PATTERN, RegexOptions.Compiled);

            if (string.IsNullOrEmpty(profile.Name))
            {
                _validationDictionary.AddError("Name", "Profile Name is required.");
                isValid = false;
            }
            if (r.Match(profile.Name).Success)
            {
                _validationDictionary.AddError("Name", "Profile Name " + profile.Name + " contain not allowed symbols '()[]'.");
                isValid = false;
            }
            return isValid;
        }