Esempio n. 1
0
        public void Invoke(string[] args)
        {
            bool showHelp       = false;
            bool?mute           = null;
            bool changePassword = false;
            bool setTimeZone    = false;
            bool?timeStamps     = null;
            bool?autoFollow     = null;
            bool?replyNotify    = null;
            bool?messageNotify  = null;
            bool?registration   = null;

            var options = new OptionSet();

            options.Add(
                "?|help",
                "Show help information.",
                x => showHelp = x != null
                );
            options.Add(
                "m|mute",
                "Mute/unmute the U413 typing sound.",
                x => mute = x != null
                );
            options.Add(
                "changePassword",
                "Change your current password.",
                x => changePassword = x != null
                );
            options.Add(
                "setTimeZone",
                "Set your current time zone.",
                x => setTimeZone = x != null
                );
            options.Add(
                "timeStamps",
                "Turn timestamps on/off.",
                x => timeStamps = x != null
                );
            options.Add(
                "autoFollow",
                "Auto-follow topics you create or reply to.",
                x => autoFollow = x != null
                );
            options.Add(
                "replyNotify",
                "Display notifications about replies to your followed topics.",
                x => replyNotify = x != null
                );
            options.Add(
                "msgNotify",
                "Display notifications for unread messages in your inbox.",
                x => messageNotify = x != null
                );
            if (this.CommandResult.CurrentUser.IsAdministrator)
            {
                options.Add(
                    "reg|registration",
                    "Open or close registration.",
                    x => registration = x != null
                    );
            }

            if (args == null)
            {
                this.CommandResult.WriteLine(DisplayTemplates.InvalidArguments);
            }
            else
            {
                try
                {
                    var parsedArgs = options.Parse(args).ToArray();

                    if (parsedArgs.Length == args.Length)
                    {
                        this.CommandResult.WriteLine(DisplayTemplates.InvalidArguments);
                    }
                    else
                    {
                        if (showHelp)
                        {
                            HelpUtility.WriteHelpInformation(
                                this.CommandResult,
                                this.Name,
                                this.Parameters,
                                this.Description,
                                options
                                );
                        }
                        else if (changePassword)
                        {
                            if (this.CommandResult.CommandContext.PromptData == null)
                            {
                                this.CommandResult.WriteLine("Type your new password.");
                                this.CommandResult.PasswordField = true;
                                this.CommandResult.CommandContext.SetPrompt(this.Name, args, "NEW PASSWORD");
                            }
                            else if (this.CommandResult.CommandContext.PromptData.Length == 1)
                            {
                                this.CommandResult.WriteLine("Confirm your new password.");
                                this.CommandResult.PasswordField = true;
                                this.CommandResult.CommandContext.SetPrompt(this.Name, args, "CONFIRM PASSWORD");
                            }
                            else if (this.CommandResult.CommandContext.PromptData.Length == 2)
                            {
                                string password        = this.CommandResult.CommandContext.PromptData[0];
                                string confirmPassword = this.CommandResult.CommandContext.PromptData[1];
                                if (password == confirmPassword)
                                {
                                    this.CommandResult.CurrentUser.Password = password;
                                    _userRepository.UpdateUser(this.CommandResult.CurrentUser);
                                    this.CommandResult.WriteLine("Password changed successfully.");
                                }
                                else
                                {
                                    this.CommandResult.WriteLine("Passwords did not match.");
                                }
                                this.CommandResult.CommandContext.Restore();
                            }
                        }
                        else if (setTimeZone)
                        {
                            var timeZones = TimeZoneInfo.GetSystemTimeZones();
                            if (this.CommandResult.CommandContext.PromptData == null)
                            {
                                for (int index = 0; index < timeZones.Count; index++)
                                {
                                    var timeZone = timeZones[index];
                                    this.CommandResult.WriteLine(DisplayMode.DontType, "{{{0}}} {1}", index, timeZone.DisplayName);
                                }
                                this.CommandResult.WriteLine();
                                this.CommandResult.WriteLine("Enter time zone ID.");
                                this.CommandResult.CommandContext.SetPrompt(this.Name, args, "CHANGE TIME ZONE");
                            }
                            else if (this.CommandResult.CommandContext.PromptData.Length == 1)
                            {
                                string promptData = this.CommandResult.CommandContext.PromptData[0];
                                if (promptData.IsInt())
                                {
                                    int timeZoneId = promptData.ToInt();
                                    if (timeZoneId >= 0 && timeZoneId < timeZones.Count)
                                    {
                                        var timeZone = timeZones[timeZoneId];
                                        this.CommandResult.CurrentUser.TimeZone = timeZone.Id;
                                        _userRepository.UpdateUser(this.CommandResult.CurrentUser);
                                        this.CommandResult.WriteLine("'{0}' successfully set as your current time zone.", timeZone.Id);
                                        this.CommandResult.CommandContext.Restore();
                                    }
                                    else
                                    {
                                        this.CommandResult.WriteLine("'{0}' does not match any available time zone ID.", timeZoneId);
                                        this.CommandResult.CommandContext.Restore();
                                        this.CommandResult.WriteLine("Enter time zone ID.");
                                        this.CommandResult.CommandContext.SetPrompt(this.Name, args, "CHANGE TIME ZONE");
                                    }
                                }
                                else
                                {
                                    this.CommandResult.WriteLine("'{0}' is not a valid time zone ID.", promptData);
                                    this.CommandResult.CommandContext.Restore();
                                    this.CommandResult.WriteLine("Enter time zone ID.");
                                    this.CommandResult.CommandContext.SetPrompt(this.Name, args, "CHANGE TIME ZONE");
                                }
                            }
                        }
                        else
                        {
                            if (mute != null)
                            {
                                this.CommandResult.CurrentUser.Sound = !(bool)mute;
                                _userRepository.UpdateUser(this.CommandResult.CurrentUser);
                                this.CommandResult.WriteLine("Sound successfully {0}.", (bool)mute ? "muted" : "unmuted");
                            }
                            if (timeStamps != null)
                            {
                                this.CommandResult.CurrentUser.ShowTimestamps = (bool)timeStamps;
                                _userRepository.UpdateUser(this.CommandResult.CurrentUser);
                                this.CommandResult.WriteLine("Timestamps were successfully {0}.", (bool)timeStamps ? "enabled" : "disabled");
                            }
                            if (autoFollow != null)
                            {
                                this.CommandResult.CurrentUser.AutoFollow = (bool)autoFollow;
                                _userRepository.UpdateUser(this.CommandResult.CurrentUser);
                                this.CommandResult.WriteLine("Auto-follow {0}.", (bool)autoFollow ? "activated" : "deactivated");
                            }
                            if (replyNotify != null)
                            {
                                this.CommandResult.CurrentUser.NotifyReplies = (bool)replyNotify;
                                _userRepository.UpdateUser(this.CommandResult.CurrentUser);
                                this.CommandResult.WriteLine("Reply notifications were successfully turned {0}.", (bool)replyNotify ? "on" : "off");
                            }
                            if (messageNotify != null)
                            {
                                this.CommandResult.CurrentUser.NotifyMessages = (bool)messageNotify;
                                _userRepository.UpdateUser(this.CommandResult.CurrentUser);
                                this.CommandResult.WriteLine("Message notifications were successfully turned {0}.", (bool)messageNotify ? "on" : "off");
                            }
                            if (registration != null)
                            {
                                var registrationStatus = _variableRepository.GetVariable("Registration");
                                if ((bool)registration)
                                {
                                    registrationStatus.Value = "Open";
                                }
                                else if (!(bool)registration)
                                {
                                    registrationStatus.Value = "Closed";
                                }
                                _variableRepository.ModifyVariable(registrationStatus);
                                this.CommandResult.WriteLine("U413 registration was successfully {0}.", (bool)registration ? "opened" : "closed");
                            }
                        }
                    }
                }
                catch (OptionException ex)
                {
                    this.CommandResult.WriteLine(ex.Message);
                }
            }
        }
Esempio n. 2
0
        private async Task SetCurrentVariableIdAsync(IVariableDbDescription variableDbDescription)
        {
            var variable = await _variableRepository.GetVariable(variableDbDescription.VariableName, variableDbDescription.VariableGroupName);

            CurrenVariableId = variable.Id;
        }
Esempio n. 3
0
        public void Invoke(string[] args)
        {
            var options = new OptionSet();

            options.Add(
                "?|help",
                "Show help information.",
                x =>
            {
                HelpUtility.WriteHelpInformation(
                    this.CommandResult,
                    this.Name,
                    this.Parameters,
                    this.Description,
                    options
                    );
            }
                );

            bool matchFound = false;

            if (args != null)
            {
                try
                {
                    var extra = options.Parse(args);
                    matchFound = args.Length != extra.Count;
                }
                catch (OptionException ex)
                {
                    this.CommandResult.WriteLine(ex.Message);
                }
            }

            if (!matchFound)
            {
                var registrationStatus = _variableRepository.GetVariable("Registration");
                if (registrationStatus.Value.Equals("Open", StringComparison.InvariantCultureIgnoreCase))
                {
                    if (args.IsNullOrEmpty())
                    {
                        this.CommandResult.WriteLine("Enter your desired username.");
                        this.CommandResult.CommandContext.Set(ContextStatus.Forced, this.Name, args, "Username");
                    }
                    else if (args.Length == 1)
                    {
                        if (args[0].Length > 3)
                        {
                            if (!_userRepository.CheckUserExists(args[0]))
                            {
                                this.CommandResult.WriteLine("Enter your desired password.");
                                this.CommandResult.PasswordField = true;
                                this.CommandResult.CommandContext.Set(ContextStatus.Forced, this.Name, args, "Password");
                            }
                            else
                            {
                                this.CommandResult.WriteLine("Username already exists.");
                                this.CommandResult.WriteLine("Enter a different username.");
                                this.CommandResult.CommandContext.Set(ContextStatus.Forced, this.Name, null, "Username");
                            }
                        }
                        else
                        {
                            this.CommandResult.WriteLine("Username must be at least four characters long.");
                            this.CommandResult.WriteLine("Enter a different username.");
                            this.CommandResult.CommandContext.Set(ContextStatus.Forced, this.Name, null, "Username");
                        }
                    }
                    else if (args.Length == 2)
                    {
                        if (args[0].Length > 3)
                        {
                            if (!_userRepository.CheckUserExists(args[0]))
                            {
                                this.CommandResult.WriteLine("Re-enter your desired password.");
                                this.CommandResult.PasswordField = true;
                                this.CommandResult.CommandContext.Set(ContextStatus.Forced, this.Name, args, "Confirm Password");
                            }
                            else
                            {
                                this.CommandResult.WriteLine("Username already exists.");
                                this.CommandResult.WriteLine("Enter your desired username.");
                                this.CommandResult.CommandContext.Set(ContextStatus.Forced, this.Name, null, "Username");
                            }
                        }
                        else
                        {
                            this.CommandResult.WriteLine("Username must be at least four characters long.");
                            this.CommandResult.WriteLine("Enter a different username.");
                            this.CommandResult.CommandContext.Set(ContextStatus.Forced, this.Name, null, "Username");
                        }
                    }
                    else if (args.Length == 3)
                    {
                        if (args[0].Length > 3)
                        {
                            var user = this._userRepository.GetUser(args[0]);
                            if (user == null)
                            {
                                if (args[1] == args[2])
                                {
                                    user = new User
                                    {
                                        Username  = args[0],
                                        Password  = args[1],
                                        JoinDate  = DateTime.UtcNow,
                                        LastLogin = DateTime.UtcNow,
                                        TimeZone  = "UTC",
                                        Sound     = true
                                    };
                                    _userRepository.AddRoleToUser(user, "User");
                                    _userRepository.AddUser(user);

                                    this.CommandResult.CurrentUser = user;
                                    this.CommandResult.WriteLine("Thank you for registering.");
                                    this.CommandResult.WriteLine();
                                    var STATS = this.AvailableCommands.SingleOrDefault(x => x.Name.Is("STATS"));
                                    STATS.Invoke(new string[] { "-users" });
                                    this.CommandResult.WriteLine();
                                    this.CommandResult.WriteLine("You are now logged in as {0}.", this.CommandResult.CurrentUser.Username);
                                    this.CommandResult.CommandContext.Deactivate();
                                }
                                else
                                {
                                    this.CommandResult.WriteLine("Passwords did not match.");
                                    this.CommandResult.WriteLine("Enter your desired password.");
                                    this.CommandResult.PasswordField = true;
                                    this.CommandResult.CommandContext.Set(ContextStatus.Forced, this.Name, new string[] { args[0] }, "Password");
                                }
                            }
                            else
                            {
                                this.CommandResult.WriteLine("Username already exists.");
                                this.CommandResult.WriteLine("Enter your desired username.");
                                this.CommandResult.CommandContext.Set(ContextStatus.Forced, this.Name, null, "Username");
                            }
                        }
                        else
                        {
                            this.CommandResult.WriteLine("Username must be at least four characters long.");
                            this.CommandResult.WriteLine("Enter a different username.");
                            this.CommandResult.CommandContext.Set(ContextStatus.Forced, this.Name, null, "Username");
                        }
                    }
                }
                else
                {
                    this.CommandResult.WriteLine("Registration is currently closed.");
                }
            }
        }