예제 #1
0
파일: TOPIC.cs 프로젝트: mBr001/u413-C-
        public void Invoke(string[] args)
        {
            bool  showHelp          = false;
            bool  replyToTopic      = false;
            bool  refresh           = false;
            bool  edit              = false;
            bool  delete            = false;
            bool  report            = false;
            bool  modReply          = false;
            bool? lockTopic         = null;
            bool? stickyTopic       = null;
            bool? globalStickyTopic = null;
            long? replyId           = null;
            short?moveToBoard       = null;

            var options = new OptionSet();

            options.Add(
                "?|help",
                "Show help information.",
                x => showHelp = x != null
                );
            options.Add(
                "r|reply",
                "Reply to the topic.",
                x => replyToTopic = x != null
                );
            options.Add(
                "R|refresh",
                "Refresh the current topic.",
                x => refresh = x != null
                );
            options.Add(
                "e|edit:",
                "Edits the topic or a reply if a {ReplyID} is specified.",
                x =>
            {
                edit = true;
                if (x.IsLong())
                {
                    replyId = x.ToLong();
                }
            }
                );
            options.Add(
                "d|delete:",
                "Deletes the topic or a reply if a {ReplyID} is specified.",
                x =>
            {
                delete = true;
                if (x.IsLong())
                {
                    replyId = x.ToLong();
                }
            }
                );
            options.Add(
                "report:",
                "Report abuse for the topic or a reply if a {ReplyID} is specified.",
                x =>
            {
                report = true;
                if (x.IsLong())
                {
                    replyId = x.ToLong();
                }
            }
                );

            if (this.CommandResult.CurrentUser.IsModerator || this.CommandResult.CurrentUser.IsAdministrator)
            {
                options.Add(
                    "mr|modReply",
                    "A reply only moderators can see.",
                    x =>
                {
                    modReply     = x != null;
                    replyToTopic = x != null;
                }
                    );
                options.Add(
                    "l|lock",
                    "Locks the topic preventing further replies except modreplies.",
                    x => lockTopic = x != null
                    );
                options.Add(
                    "s|sticky",
                    "Sticky's the topic keeping it at the top of the board regardless of last reply date.",
                    x => stickyTopic = x != null
                    );
                options.Add(
                    "g|globalSticky",
                    "Sticky's the topic keeping it at the top of \"All Activity Board\".",
                    x => globalStickyTopic = x != null
                    );
                options.Add(
                    "m|move=",
                    "Moves the topic to the board with the specified {BoardID}.",
                    x => moveToBoard = x.ToShort()
                    );
            }

            try
            {
                if (args == null)
                {
                    this.CommandResult.WriteLine(DisplayTemplates.InvalidArguments);
                }
                else
                {
                    var parsedArgs = options.Parse(args).ToArray();
                    if (parsedArgs.Length == args.Length)
                    {
                        if (parsedArgs.Length == 1)
                        {
                            if (parsedArgs[0].IsLong())
                            {
                                var topicId     = parsedArgs[0].ToLong();
                                var page        = 1;
                                var topic       = _topicRepository.GetTopic(topicId);
                                var isAnonymous = _boardRepository.GetBoard(topic.BoardID).Anonymous;
                                if (topic != null)
                                {
                                    if (topic.ModsOnly)
                                    {
                                        if (this.CommandResult.CurrentUser.IsModerator || this.CommandResult.CurrentUser.IsAdministrator)
                                        {
                                            WriteTopic(topicId, page);
                                        }
                                        else
                                        {
                                            this.CommandResult.WriteLine("You do not have permission to view this topic.");
                                        }
                                    }
                                    else
                                    {
                                        WriteTopic(topicId, page);
                                    }
                                }
                                else
                                {
                                    this.CommandResult.WriteLine("'{0}' is not a valid topic ID.", parsedArgs[0]);
                                }
                            }
                            else
                            {
                                this.CommandResult.WriteLine("'{0}' is not a valid topic ID.", parsedArgs[0]);
                            }
                        }
                        else if (parsedArgs.Length == 2)
                        {
                            if (parsedArgs[0].IsInt())
                            {
                                var topicId = parsedArgs[0].ToLong();
                                if (parsedArgs[1].IsInt())
                                {
                                    var page        = parsedArgs[1].ToInt();
                                    var topic       = _topicRepository.GetTopic(topicId);
                                    var isAnonymous = _boardRepository.GetBoard(topic.BoardID).Anonymous;
                                    if (topic != null)
                                    {
                                        if (topic.ModsOnly)
                                        {
                                            if (this.CommandResult.CurrentUser.IsModerator || this.CommandResult.CurrentUser.IsAdministrator)
                                            {
                                                WriteTopic(topicId, page);
                                            }
                                            else
                                            {
                                                this.CommandResult.WriteLine("You do not have permission to view this topic.");
                                            }
                                        }
                                        else
                                        {
                                            WriteTopic(topicId, page);
                                        }
                                    }
                                    else
                                    {
                                        this.CommandResult.WriteLine("'{0}' is not a valid topic ID.", parsedArgs[0]);
                                    }
                                }
                                else if (PagingUtility.Shortcuts.Any(x => parsedArgs[1].Is(x)))
                                {
                                    var page = PagingUtility.TranslateShortcut(parsedArgs[1], this.CommandResult.CommandContext.CurrentPage);
                                    WriteTopic(topicId, page);
                                    if (parsedArgs[1].Is("last") || parsedArgs[1].Is("prev"))
                                    {
                                        this.CommandResult.ScrollToBottom = true;
                                    }
                                }
                                else
                                {
                                    this.CommandResult.WriteLine("'{0}' is not a valid page number.", parsedArgs[1]);
                                }
                            }
                            else
                            {
                                this.CommandResult.WriteLine("'{0}' is not a valid topic ID.", parsedArgs[0]);
                            }
                        }
                        else
                        {
                            this.CommandResult.WriteLine(DisplayTemplates.InvalidArguments);
                        }
                    }
                    else
                    {
                        if (showHelp)
                        {
                            HelpUtility.WriteHelpInformation(
                                this.CommandResult,
                                this.Name,
                                this.Parameters,
                                this.Description,
                                options
                                );
                        }
                        else if (replyToTopic)
                        {
                            if (parsedArgs.Length > 0)
                            {
                                if (parsedArgs[0].IsLong())
                                {
                                    var topicId = parsedArgs[0].ToLong();
                                    var topic   = _topicRepository.GetTopic(topicId);
                                    if (topic != null)
                                    {
                                        if (!topic.Locked || (!topic.IsModsOnly() && modReply && this.CommandResult.CurrentUser.IsModerator) || this.CommandResult.CurrentUser.IsAdministrator)
                                        {
                                            if (!topic.IsModsOnly() || this.CommandResult.CurrentUser.IsModerator || this.CommandResult.CurrentUser.IsAdministrator)
                                            {
                                                if (this.CommandResult.CommandContext.PromptData == null)
                                                {
                                                    this.CommandResult.WriteLine("Type your reply.");
                                                    this.CommandResult.CommandContext.SetPrompt(this.Name, args, string.Format("{0} REPLY", topicId));
                                                }
                                                else
                                                {
                                                    _replyRepository.AddReply(new Reply
                                                    {
                                                        Username   = this.CommandResult.CurrentUser.Username,
                                                        PostedDate = DateTime.UtcNow,
                                                        LastEdit   = DateTime.UtcNow,
                                                        TopicID    = topicId,
                                                        Body       = BBCodeUtility.SimplifyComplexTags(
                                                            this.CommandResult.CommandContext.PromptData[0],
                                                            _replyRepository,
                                                            this.CommandResult.CurrentUser.IsModerator || this.CommandResult.CurrentUser.IsAdministrator
                                                            ),
                                                        ModsOnly = modReply && !topic.IsModsOnly()
                                                    });
                                                    this.CommandResult.CommandContext.PromptData = null;
                                                    var TOPIC = this.AvailableCommands.SingleOrDefault(x => x.Name.Is("TOPIC"));
                                                    TOPIC.Invoke(new string[] { topicId.ToString(), "last" });
                                                    this.CommandResult.WriteLine("Reply successfully posted.");
                                                }
                                            }
                                            else
                                            {
                                                this.CommandResult.WriteLine("Topic '{0}' is for moderators only.", topicId);
                                            }
                                        }
                                        else
                                        {
                                            this.CommandResult.WriteLine("Topic '{0}' is locked.", topicId);
                                        }
                                    }
                                    else
                                    {
                                        this.CommandResult.WriteLine("There is no topic with ID '{0}'.", topicId);
                                    }
                                }
                                else
                                {
                                    this.CommandResult.WriteLine("'{0}' is not a valid topic ID.", parsedArgs[0]);
                                }
                            }
                            else
                            {
                                this.CommandResult.WriteLine("You must supply a topic ID.");
                            }
                        }
                        else if (edit)
                        {
                            if (parsedArgs.Length > 0)
                            {
                                if (parsedArgs[0].IsLong())
                                {
                                    var topicId = parsedArgs[0].ToLong();
                                    if (replyId == null)
                                    {
                                        var topic = _topicRepository.GetTopic(topicId);
                                        if (topic != null)
                                        {
                                            if (!topic.Locked || this.CommandResult.CurrentUser.IsAdministrator)
                                            {
                                                if (topic.Username.Is(this.CommandResult.CurrentUser.Username) ||
                                                    (this.CommandResult.CurrentUser.IsModerator && !topic.IsModsOnly() && !topic.User.IsModerator && !topic.User.IsAdministrator) ||
                                                    this.CommandResult.CurrentUser.IsAdministrator)
                                                {
                                                    if (this.CommandResult.CommandContext.PromptData == null)
                                                    {
                                                        this.CommandResult.WriteLine("Edit the topic title.");
                                                        this.CommandResult.EditText = topic.Title;
                                                        this.CommandResult.CommandContext.SetPrompt(this.Name, args, string.Format("{0} EDIT Title", topicId));
                                                    }
                                                    else if (this.CommandResult.CommandContext.PromptData.Length == 1)
                                                    {
                                                        this.CommandResult.WriteLine("Edit the topic body.");
                                                        this.CommandResult.EditText = topic.Body;
                                                        this.CommandResult.CommandContext.SetPrompt(this.Name, args, string.Format("{0} EDIT Body", topicId));
                                                    }
                                                    else if (this.CommandResult.CommandContext.PromptData.Length == 2)
                                                    {
                                                        topic.Title = this.CommandResult.CommandContext.PromptData[0];
                                                        topic.Body  = BBCodeUtility.SimplifyComplexTags(
                                                            this.CommandResult.CommandContext.PromptData[1],
                                                            _replyRepository,
                                                            this.CommandResult.CurrentUser.IsModerator || this.CommandResult.CurrentUser.IsAdministrator
                                                            );
                                                        topic.LastEdit = DateTime.UtcNow;
                                                        topic.EditedBy = this.CommandResult.CurrentUser.Username;
                                                        _topicRepository.UpdateTopic(topic);
                                                        this.CommandResult.CommandContext.PromptData = null;
                                                        this.CommandResult.WriteLine("Topic '{0}' was edited successfully.", topicId);
                                                        this.CommandResult.CommandContext.Restore();
                                                    }
                                                }
                                                else
                                                {
                                                    this.CommandResult.WriteLine("Topic '{0}' belongs to '{1}'. You are not authorized to edit it.", topicId, topic.Username);
                                                }
                                            }
                                            else
                                            {
                                                this.CommandResult.WriteLine("Topic '{0}' is locked.", topicId);
                                            }
                                        }
                                        else
                                        {
                                            this.CommandResult.WriteLine("There is no topic with ID '{0}'.", topicId);
                                        }
                                    }
                                    else
                                    {
                                        var reply = _replyRepository.GetReply((long)replyId);
                                        if (reply != null)
                                        {
                                            if (reply.TopicID == topicId)
                                            {
                                                if (!reply.Topic.Locked || (reply.ModsOnly && !reply.Topic.IsModsOnly()) || this.CommandResult.CurrentUser.IsAdministrator)
                                                {
                                                    if (reply.Username.Is(this.CommandResult.CurrentUser.Username) ||
                                                        (this.CommandResult.CurrentUser.IsModerator && !reply.IsModsOnly() && !reply.User.IsModerator && !reply.User.IsAdministrator) ||
                                                        this.CommandResult.CurrentUser.IsAdministrator)
                                                    {
                                                        if (this.CommandResult.CommandContext.PromptData == null)
                                                        {
                                                            this.CommandResult.WriteLine("Edit the reply body.");
                                                            this.CommandResult.EditText = reply.Body;
                                                            this.CommandResult.CommandContext.SetPrompt(this.Name, args, string.Format("{0} EDIT Reply {1}", topicId, replyId));
                                                        }
                                                        else if (this.CommandResult.CommandContext.PromptData.Length == 1)
                                                        {
                                                            reply.Body = BBCodeUtility.SimplifyComplexTags(
                                                                this.CommandResult.CommandContext.PromptData[0],
                                                                _replyRepository,
                                                                this.CommandResult.CurrentUser.IsModerator || this.CommandResult.CurrentUser.IsAdministrator
                                                                );
                                                            reply.LastEdit = DateTime.UtcNow;
                                                            reply.EditedBy = this.CommandResult.CurrentUser.Username;
                                                            _replyRepository.UpdateReply(reply);
                                                            this.CommandResult.CommandContext.PromptData = null;
                                                            this.CommandResult.WriteLine("Reply '{0}' was edited successfully.", replyId);
                                                            this.CommandResult.CommandContext.Restore();
                                                        }
                                                    }
                                                    else
                                                    {
                                                        this.CommandResult.WriteLine("Reply '{0}' belongs to '{1}'. You are not authorized to edit it.", replyId, reply.Username);
                                                    }
                                                }
                                                else
                                                {
                                                    this.CommandResult.WriteLine("Topic '{0}' is locked.", topicId);
                                                }
                                            }
                                            else
                                            {
                                                this.CommandResult.WriteLine("Topic '{0}' does not contain a reply with ID '{1}'.", topicId, replyId);
                                            }
                                        }
                                        else
                                        {
                                            this.CommandResult.WriteLine("Reply '{0}' does not exist.", replyId);
                                        }
                                    }
                                }
                                else
                                {
                                    this.CommandResult.WriteLine("'{0}' is not a valid topic ID.", parsedArgs[0]);
                                }
                            }
                            else
                            {
                                this.CommandResult.WriteLine("You must supply a topic ID.");
                            }
                        }
                        else if (delete)
                        {
                            if (parsedArgs.Length > 0)
                            {
                                if (parsedArgs[0].IsLong())
                                {
                                    var topicId = parsedArgs[0].ToLong();
                                    if (replyId == null)
                                    {
                                        var topic = _topicRepository.GetTopic(topicId);
                                        if (topic != null)
                                        {
                                            if (!topic.Locked || this.CommandResult.CurrentUser.IsAdministrator)
                                            {
                                                if (this.CommandResult.CurrentUser.IsAdministrator)
                                                {
                                                    if (this.CommandResult.CommandContext.PromptData == null)
                                                    {
                                                        this.CommandResult.WriteLine("Are you sure you want to delete the topic titled \"{0}\"? (Y/N)", topic.Title);
                                                        this.CommandResult.CommandContext.SetPrompt(this.Name, args, string.Format("{0} DELETE CONFIRM", topicId));
                                                    }
                                                    else if (this.CommandResult.CommandContext.PromptData.Length == 1)
                                                    {
                                                        if (this.CommandResult.CommandContext.PromptData[0].Is("Y"))
                                                        {
                                                            _topicRepository.DeleteTopic(topic);
                                                            this.CommandResult.WriteLine("Topic '{0}' was deleted successfully.", topicId);
                                                            this.CommandResult.CommandContext.PromptData = null;
                                                            if (this.CommandResult.CommandContext.PreviousContext != null &&
                                                                this.CommandResult.CommandContext.PreviousContext.Command.Is("TOPIC") &&
                                                                this.CommandResult.CommandContext.PreviousContext.Args.Contains(topicId.ToString()) &&
                                                                this.CommandResult.CommandContext.PreviousContext.Status == ContextStatus.Passive)
                                                            {
                                                                this.CommandResult.ClearScreen = true;
                                                                this.CommandResult.CommandContext.Deactivate();
                                                            }
                                                            else
                                                            {
                                                                this.CommandResult.CommandContext.Restore();
                                                            }
                                                        }
                                                        else
                                                        {
                                                            this.CommandResult.WriteLine("Topic '{0}' was not deleted.", topicId);
                                                            this.CommandResult.CommandContext.PromptData = null;
                                                            this.CommandResult.CommandContext.Restore();
                                                        }
                                                    }
                                                }
                                                else
                                                {
                                                    this.CommandResult.WriteLine("You are not an administrator. You are not authorized to delete topics.");
                                                }
                                            }
                                            else
                                            {
                                                this.CommandResult.WriteLine("Topic '{0}' is locked.", topicId);
                                            }
                                        }
                                        else
                                        {
                                            this.CommandResult.WriteLine("There is no topic with ID {0}.", topicId);
                                        }
                                    }
                                    else
                                    {
                                        var reply = _replyRepository.GetReply((long)replyId);
                                        if (reply != null)
                                        {
                                            if (reply.TopicID == topicId)
                                            {
                                                if (!reply.Topic.Locked || (reply.ModsOnly && !reply.Topic.IsModsOnly()) || this.CommandResult.CurrentUser.IsAdministrator)
                                                {
                                                    if (reply.Username.Is(this.CommandResult.CurrentUser.Username) ||
                                                        (this.CommandResult.CurrentUser.IsModerator && !reply.IsModsOnly() && !reply.User.IsModerator && !reply.User.IsAdministrator) ||
                                                        this.CommandResult.CurrentUser.IsAdministrator)
                                                    {
                                                        _replyRepository.DeleteReply(reply);
                                                        this.CommandResult.WriteLine("Reply '{0}' was deleted successfully.", replyId);
                                                    }
                                                    else
                                                    {
                                                        this.CommandResult.WriteLine("Reply '{0}' belongs to '{1}'. You are not authorized to delete it.", replyId, reply.Username);
                                                    }
                                                }
                                                else
                                                {
                                                    this.CommandResult.WriteLine("Topic '{0}' is locked.", topicId);
                                                }
                                            }
                                            else
                                            {
                                                this.CommandResult.WriteLine("Topic '{0}' does not contain a reply with ID '{1}'.", topicId, replyId);
                                            }
                                        }
                                        else
                                        {
                                            this.CommandResult.WriteLine("Reply '{0}' does not exist.", replyId);
                                        }
                                    }
                                }
                                else
                                {
                                    this.CommandResult.WriteLine("'{0}' is not a valid topic ID.", parsedArgs[0]);
                                }
                            }
                            else
                            {
                                this.CommandResult.WriteLine("You must supply a topic ID.");
                            }
                        }
                        else if (report)
                        {
                            // allow user to report abuse.
                        }
                        else
                        {
                            if (lockTopic != null)
                            {
                                if (parsedArgs.Length > 0)
                                {
                                    if (parsedArgs[0].IsLong())
                                    {
                                        var topicId = parsedArgs[0].ToLong();
                                        var topic   = _topicRepository.GetTopic(topicId);
                                        if (topic != null)
                                        {
                                            if (!topic.IsModsOnly() || this.CommandResult.CurrentUser.IsAdministrator)
                                            {
                                                if ((bool)lockTopic || (!(bool)lockTopic && this.CommandResult.CurrentUser.IsAdministrator))
                                                {
                                                    topic.Locked = (bool)lockTopic;
                                                    _topicRepository.UpdateTopic(topic);
                                                    string status = (bool)lockTopic ? "locked" : "unlocked";
                                                    this.CommandResult.WriteLine("Topic '{0}' was successfully {1}.", topicId, status);
                                                }
                                                else
                                                {
                                                    this.CommandResult.WriteLine("Only administrators can unlock topics.");
                                                }
                                            }
                                            else
                                            {
                                                this.CommandResult.WriteLine("You are not authorized to lock moderator topics.");
                                            }
                                        }
                                        else
                                        {
                                            this.CommandResult.WriteLine("There is no topic with ID {0}.", topicId);
                                        }
                                    }
                                    else
                                    {
                                        this.CommandResult.WriteLine("'{0}' is not a valid topic ID.", parsedArgs[0]);
                                    }
                                }
                                else
                                {
                                    this.CommandResult.WriteLine("You must supply a topic ID.");
                                }
                            }
                            if (stickyTopic != null)
                            {
                                if (parsedArgs.Length > 0)
                                {
                                    if (parsedArgs[0].IsLong())
                                    {
                                        var topicId = parsedArgs[0].ToLong();
                                        var topic   = _topicRepository.GetTopic(topicId);
                                        if (topic != null)
                                        {
                                            if (!topic.IsModsOnly() || this.CommandResult.CurrentUser.IsAdministrator)
                                            {
                                                topic.Stickied = (bool)stickyTopic;
                                                _topicRepository.UpdateTopic(topic);
                                                string status = (bool)stickyTopic ? "stickied" : "unstickied";
                                                this.CommandResult.WriteLine("Topic '{0}' was successfully {1}.", topicId, status);
                                            }
                                            else
                                            {
                                                this.CommandResult.WriteLine("You are not authorized to sticky/unsticky moderator topics.");
                                            }
                                        }
                                        else
                                        {
                                            this.CommandResult.WriteLine("There is no topic with ID '{0}'.", topicId);
                                        }
                                    }
                                    else
                                    {
                                        this.CommandResult.WriteLine("'{0}' is not a valid topic ID.", parsedArgs[0]);
                                    }
                                }
                                else
                                {
                                    this.CommandResult.WriteLine("You must supply a topic ID.");
                                }
                            }
                            if (globalStickyTopic != null)
                            {
                                if (parsedArgs.Length > 0)
                                {
                                    if (parsedArgs[0].IsLong())
                                    {
                                        var topicId = parsedArgs[0].ToLong();
                                        var topic   = _topicRepository.GetTopic(topicId);
                                        if (topic != null)
                                        {
                                            if (!topic.IsModsOnly() || this.CommandResult.CurrentUser.IsAdministrator)
                                            {
                                                topic.GlobalSticky = (bool)globalStickyTopic;
                                                _topicRepository.UpdateTopic(topic);
                                                string status = (bool)globalStickyTopic ? "stickied" : "unstickied";
                                                this.CommandResult.WriteLine("Topic '{0}' was successfully globally {1}.", topicId, status);
                                            }
                                            else
                                            {
                                                this.CommandResult.WriteLine("You are not authorized to globally sticky/unsticky moderator topics.");
                                            }
                                        }
                                        else
                                        {
                                            this.CommandResult.WriteLine("There is no topic with ID '{0}'.", topicId);
                                        }
                                    }
                                    else
                                    {
                                        this.CommandResult.WriteLine("'{0}' is not a valid topic ID.", parsedArgs[0]);
                                    }
                                }
                                else
                                {
                                    this.CommandResult.WriteLine("You must supply a topic ID.");
                                }
                            }
                            if (moveToBoard != null)
                            {
                                if (parsedArgs.Length > 0)
                                {
                                    if (parsedArgs[0].IsLong())
                                    {
                                        var topicId = parsedArgs[0].ToLong();
                                        var topic   = _topicRepository.GetTopic(topicId);
                                        if (topic != null)
                                        {
                                            var board = _boardRepository.GetBoard((short)moveToBoard);
                                            if (board != null)
                                            {
                                                if (!board.Locked || this.CommandResult.CurrentUser.IsAdministrator)
                                                {
                                                    if (!topic.IsModsOnly() || this.CommandResult.CurrentUser.IsAdministrator)
                                                    {
                                                        if (!board.ModsOnly || this.CommandResult.CurrentUser.IsAdministrator)
                                                        {
                                                            topic.BoardID = (short)moveToBoard;
                                                            _topicRepository.UpdateTopic(topic);
                                                            this.CommandResult.WriteLine("Topic '{0}' was successfully moved to board '{1}'.", topicId, moveToBoard);
                                                        }
                                                        else
                                                        {
                                                            this.CommandResult.WriteLine("You are not authorized to move topics onto moderator boards.");
                                                        }
                                                    }
                                                    else
                                                    {
                                                        this.CommandResult.WriteLine("You are not authorized to move moderator topics.");
                                                    }
                                                }
                                                else
                                                {
                                                    this.CommandResult.WriteLine("Board '{0}' is locked.", moveToBoard);
                                                }
                                            }
                                            else
                                            {
                                                this.CommandResult.WriteLine("There is no board with ID '{0}'.", moveToBoard);
                                            }
                                        }
                                        else
                                        {
                                            this.CommandResult.WriteLine("There is no topic with ID '{0}'.", topicId);
                                        }
                                    }
                                    else
                                    {
                                        this.CommandResult.WriteLine("'{0}' is not a valid topic ID.", parsedArgs[0]);
                                    }
                                }
                                else
                                {
                                    this.CommandResult.WriteLine("You must supply a board ID.");
                                }
                            }
                            if (refresh)
                            {
                                if (parsedArgs.Length > 0)
                                {
                                    if (parsedArgs[0].IsLong())
                                    {
                                        var topicId = parsedArgs[0].ToLong();
                                        WriteTopic(topicId, this.CommandResult.CommandContext.CurrentPage);
                                    }
                                    else
                                    {
                                        this.CommandResult.WriteLine("'{0}' is not a valid topic ID.", parsedArgs[0]);
                                    }
                                }
                                else
                                {
                                    this.CommandResult.WriteLine("You must supply a topic ID.");
                                }
                            }
                        }
                    }
                }
            }
            catch (OptionException ex)
            {
                this.CommandResult.WriteLine(ex.Message);
            }
        }
예제 #2
0
        public void Invoke(string[] args)
        {
            bool showHelp  = false;
            bool newTopic  = false;
            bool modTopic  = false;
            bool refresh   = false;
            bool?lockBoard = null;

            var options = new OptionSet();

            options.Add(
                "?|help",
                "Show help information.",
                x => showHelp = x != null
                );
            options.Add(
                "R|refresh",
                "Refresh the current board.",
                x => refresh = x != null
                );
            options.Add(
                "nt|newTopic",
                "Create new topic on the specified board.",
                x => newTopic = x != null
                );
            if (this.CommandResult.CurrentUser.IsModerator || this.CommandResult.CurrentUser.IsAdministrator)
            {
                options.Add(
                    "mt|modTopic",
                    "Create a topic that only moderators can see.",
                    x =>
                {
                    newTopic = x != null;
                    modTopic = x != null;
                }
                    );
            }
            if (this.CommandResult.CurrentUser.IsAdministrator)
            {
                options.Add(
                    "l|lock",
                    "Lock the board to prevent creation of topics.",
                    x => lockBoard = x != null
                    );
            }

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

                    if (parsedArgs.Length == args.Length)
                    {
                        if (parsedArgs.Length == 1)
                        {
                            if (parsedArgs[0].IsShort())
                            {
                                var boardId = parsedArgs[0].ToShort();
                                var board   = _boardRepository.GetBoard(boardId);
                                var page    = 1;
                                if (board != null)
                                {
                                    if (board.ModsOnly)
                                    {
                                        if (this.CommandResult.CurrentUser.IsModerator || this.CommandResult.CurrentUser.IsAdministrator)
                                        {
                                            WriteTopics(boardId, page);
                                        }
                                        else
                                        {
                                            this.CommandResult.WriteLine("You do not have permission to access this board.");
                                        }
                                    }
                                    else
                                    {
                                        WriteTopics(boardId, page);
                                    }
                                }
                                else
                                {
                                    this.CommandResult.WriteLine("'{0}' is not a valid board ID.", parsedArgs[0]);
                                }
                            }
                            else
                            {
                                this.CommandResult.WriteLine("'{0}' is not a valid board ID.", parsedArgs[0]);
                            }
                        }
                        else if (parsedArgs.Length == 2)
                        {
                            if (parsedArgs[0].IsShort())
                            {
                                var boardId = parsedArgs[0].ToShort();
                                if (parsedArgs[1].IsInt())
                                {
                                    var page  = parsedArgs[1].ToInt();
                                    var board = _boardRepository.GetBoard(boardId);
                                    if (board != null)
                                    {
                                        if (board.ModsOnly)
                                        {
                                            if (this.CommandResult.CurrentUser.IsModerator || this.CommandResult.CurrentUser.IsAdministrator)
                                            {
                                                WriteTopics(boardId, page);
                                            }
                                            else
                                            {
                                                this.CommandResult.WriteLine("You do not have permission to access this board.");
                                            }
                                        }
                                        else
                                        {
                                            WriteTopics(boardId, page);
                                        }
                                    }
                                    else
                                    {
                                        this.CommandResult.WriteLine("'{0}' is not a valid board ID.", parsedArgs[0]);
                                    }
                                }
                                else if (PagingUtility.Shortcuts.Any(x => parsedArgs[1].Is(x)))
                                {
                                    var page = PagingUtility.TranslateShortcut(parsedArgs[1], this.CommandResult.CommandContext.CurrentPage);
                                    WriteTopics(boardId, page);
                                    if (parsedArgs[1].Is("last") || parsedArgs[1].Is("prev"))
                                    {
                                        this.CommandResult.ScrollToBottom = false;
                                    }
                                }
                                else
                                {
                                    this.CommandResult.WriteLine("'{0}' is not a valid page number.", parsedArgs[1]);
                                }
                            }
                            else
                            {
                                this.CommandResult.WriteLine("'{0}' is not a valid board ID.", parsedArgs[0]);
                            }
                        }
                        else
                        {
                            this.CommandResult.WriteLine(DisplayTemplates.InvalidArguments);
                        }
                    }
                    else
                    {
                        if (showHelp)
                        {
                            HelpUtility.WriteHelpInformation(
                                this.CommandResult,
                                this.Name,
                                this.Parameters,
                                this.Description,
                                options
                                );
                        }
                        else if (newTopic)
                        {
                            if (parsedArgs.Length >= 1)
                            {
                                if (parsedArgs[0].IsShort())
                                {
                                    var boardId = parsedArgs[0].ToShort();
                                    var board   = _boardRepository.GetBoard(boardId);
                                    if (board != null)
                                    {
                                        if (!board.Locked || this.CommandResult.CurrentUser.IsAdministrator)
                                        {
                                            if (!board.ModsOnly || this.CommandResult.CurrentUser.IsModerator || this.CommandResult.CurrentUser.IsAdministrator)
                                            {
                                                if (this.CommandResult.CommandContext.PromptData == null)
                                                {
                                                    this.CommandResult.WriteLine("Create a title for your topic.");
                                                    this.CommandResult.CommandContext.SetPrompt(this.Name, args, string.Format("{0} NEW TOPIC Title", boardId));
                                                }
                                                else if (this.CommandResult.CommandContext.PromptData.Length == 1)
                                                {
                                                    this.CommandResult.WriteLine("Create the body for your topic.");
                                                    this.CommandResult.CommandContext.SetPrompt(this.Name, args, string.Format("{0} NEW TOPIC Body", boardId));
                                                }
                                                else if (this.CommandResult.CommandContext.PromptData.Length == 2)
                                                {
                                                    Topic topic = new Topic
                                                    {
                                                        BoardID = boardId,
                                                        Title   = this.CommandResult.CommandContext.PromptData[0],
                                                        Body    = BBCodeUtility.SimplifyComplexTags(
                                                            this.CommandResult.CommandContext.PromptData[1],
                                                            _replyRepository,
                                                            this.CommandResult.CurrentUser.IsModerator || this.CommandResult.CurrentUser.IsAdministrator
                                                            ),
                                                        Username   = this.CommandResult.CurrentUser.Username,
                                                        PostedDate = DateTime.UtcNow,
                                                        LastEdit   = DateTime.UtcNow,
                                                        ModsOnly   = modTopic && !board.ModsOnly
                                                    };
                                                    _topicRepository.AddTopic(topic);
                                                    this.CommandResult.CommandContext.Restore();
                                                    var TOPIC = this.AvailableCommands.SingleOrDefault(x => x.Name.Is("TOPIC"));
                                                    TOPIC.Invoke(new string[] { topic.TopicID.ToString() });
                                                    this.CommandResult.WriteLine("New topic succesfully posted.");
                                                }
                                            }
                                            else
                                            {
                                                this.CommandResult.WriteLine("Board '{0}' is for moderators only.", boardId);
                                            }
                                        }
                                        else
                                        {
                                            this.CommandResult.WriteLine("Board '{0}' is locked. You cannot create topics on this board.", boardId);
                                        }
                                    }
                                    else
                                    {
                                        this.CommandResult.WriteLine("There is no board with ID '{0}'.", boardId);
                                    }
                                }
                                else
                                {
                                    this.CommandResult.WriteLine("'{0}' is not a valid board ID.", parsedArgs[0]);
                                }
                            }
                            else
                            {
                                this.CommandResult.WriteLine("You must supply a board ID.");
                            }
                        }
                        else
                        {
                            if (lockBoard != null)
                            {
                                if (parsedArgs.Length > 0)
                                {
                                    if (parsedArgs[0].IsShort())
                                    {
                                        var boardId = parsedArgs[0].ToShort();
                                        var board   = _boardRepository.GetBoard(boardId);
                                        if (board != null)
                                        {
                                            board.Locked = (bool)lockBoard;
                                            _boardRepository.UpdateBoard(board);
                                            string status = (bool)lockBoard ? "locked" : "unlocked";
                                            this.CommandResult.WriteLine("Board '{0}' was successfully {1}.", boardId, status);
                                        }
                                        else
                                        {
                                            this.CommandResult.WriteLine("There is no board with ID '{0}'.", boardId);
                                        }
                                    }
                                    else
                                    {
                                        this.CommandResult.WriteLine("'{0}' is not a valid board ID.", parsedArgs[0]);
                                    }
                                }
                                else
                                {
                                    this.CommandResult.WriteLine("You must supply a board ID.");
                                }
                            }
                            if (refresh)
                            {
                                if (parsedArgs.Length > 0)
                                {
                                    if (parsedArgs[0].IsShort())
                                    {
                                        var boardId = parsedArgs[0].ToShort();
                                        WriteTopics(boardId, this.CommandResult.CommandContext.CurrentPage);
                                    }
                                    else
                                    {
                                        this.CommandResult.WriteLine("'{0}' is not a valid board ID.", parsedArgs[0]);
                                    }
                                }
                                else
                                {
                                    this.CommandResult.WriteLine("You must supply a board ID.");
                                }
                            }
                        }
                    }
                }
                catch (OptionException ex)
                {
                    this.CommandResult.WriteLine(ex.Message);
                }
            }
        }